root/lm-sensors/trunk/kernel/busses/i2c-keywest.c @ 2772

Revision 2772, 15.7 KB (checked in by khali, 9 years ago)

Remove owner from i2c_adapter, restore inc_use and dec_use
instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    i2c Support for Apple Keywest I2C Bus Controller
3
4    Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5
6    Original work by
7   
8    Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24    Changes:
25
26    2001/12/13 BenH     New implementation
27    2001/12/15 BenH     Add support for "byte" and "quick"
28                        transfers. Add i2c_xfer routine.
29
30    My understanding of the various modes supported by keywest are:
31
32     - Dumb mode : not implemented, probably direct tweaking of lines
33     - Standard mode : simple i2c transaction of type
34         S Addr R/W A Data A Data ... T
35     - Standard sub mode : combined 8 bit subaddr write with data read
36         S Addr R/W A SubAddr A Data A Data ... T
37     - Combined mode : Subaddress and Data sequences appended with no stop
38         S Addr R/W A SubAddr S Addr R/W A Data A Data ... T
39
40    Currently, this driver uses only Standard mode for i2c xfer, and
41    smbus byte & quick transfers ; and uses StandardSub mode for
42    other smbus transfers instead of combined as we need that for the
43    sound driver to be happy
44*/
45
46#include <linux/module.h>
47#include <linux/config.h>
48#include <linux/kernel.h>
49#include <linux/ioport.h>
50#include <linux/pci.h>
51#include <linux/types.h>
52#include <linux/delay.h>
53#include <linux/i2c.h>
54#include <linux/init.h>
55#include <linux/mm.h>
56#include <linux/timer.h>
57#include <linux/spinlock.h>
58#include <linux/completion.h>
59
60#include <asm/io.h>
61#include <asm/prom.h>
62#include <asm/machdep.h>
63#include <asm/pmac_feature.h>
64
65#include "i2c-keywest.h"
66
67#define DBG(x...) do {\
68        if (debug > 0) \
69                printk(KERN_DEBUG "KW:" x); \
70        } while(0)
71
72
73MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
74MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
75MODULE_LICENSE("GPL");
76MODULE_PARM(probe, "i");
77MODULE_PARM(debug, "i");
78
79
80int probe = 0;
81int debug = 0;
82
83static struct keywest_iface *ifaces = NULL;
84
85
86static void
87do_stop(struct keywest_iface* iface, int result)
88{
89        write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_STOP);
90        iface->state = state_stop;
91        iface->result = result;
92}
93
94/* Main state machine for standard & standard sub mode */
95static int
96handle_interrupt(struct keywest_iface *iface, u8 isr)
97{
98        int ack;
99        int rearm_timer = 1;
100       
101        DBG("handle_interrupt(), got: %x, status: %x, state: %d\n",
102                isr, read_reg(reg_status), iface->state);
103        if (isr == 0 && iface->state != state_stop) {
104                do_stop(iface, -1);
105                return rearm_timer;
106        }
107        if (isr & KW_I2C_IRQ_STOP && iface->state != state_stop) {
108                iface->result = -1;
109                iface->state = state_stop;
110        }
111        switch(iface->state) {
112        case state_addr:
113                if (!(isr & KW_I2C_IRQ_ADDR)) {
114                        do_stop(iface, -1);
115                        break;
116                }
117                ack = read_reg(reg_status);
118                DBG("ack on set address: %x\n", ack);
119                if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
120                        do_stop(iface, -1);
121                        break;
122                }
123                /* Handle rw "quick" mode */
124                if (iface->datalen == 0)
125                        do_stop(iface, 0);
126                else if (iface->read_write == I2C_SMBUS_READ) {
127                        iface->state = state_read;
128                        if (iface->datalen > 1)
129                                write_reg(reg_control, read_reg(reg_control)
130                                        | KW_I2C_CTL_AAK);
131                } else {
132                        iface->state = state_write;
133                        DBG("write byte: %x\n", *(iface->data));
134                        write_reg(reg_data, *(iface->data++));
135                        iface->datalen--;
136                }
137               
138                break;
139        case state_read:
140                if (!(isr & KW_I2C_IRQ_DATA)) {
141                        do_stop(iface, -1);
142                        break;
143                }
144                *(iface->data++) = read_reg(reg_data);
145                DBG("read byte: %x\n", *(iface->data-1));
146                iface->datalen--;
147                if (iface->datalen == 0)
148                        iface->state = state_stop;
149                else
150                        write_reg(reg_control, 0);
151                break;
152        case state_write:
153                if (!(isr & KW_I2C_IRQ_DATA)) {
154                        do_stop(iface, -1);
155                        break;
156                }
157                /* Check ack status */
158                ack = read_reg(reg_status);
159                DBG("ack on data write: %x\n", ack);
160                if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
161                        do_stop(iface, -1);
162                        break;
163                }
164                if (iface->datalen) {
165                        DBG("write byte: %x\n", *(iface->data));
166                        write_reg(reg_data, *(iface->data++));
167                        iface->datalen--;
168                } else
169                        do_stop(iface, 0);
170                break;
171               
172        case state_stop:
173                if (!(isr & KW_I2C_IRQ_STOP) && (++iface->stopretry) < 10)
174                        do_stop(iface, -1);
175                else {
176                        rearm_timer = 0;
177                        iface->state = state_idle;
178                        write_reg(reg_control, 0x00);
179                        write_reg(reg_ier, 0x00);
180                        complete(&iface->complete);
181                }
182                break;
183        }
184       
185        write_reg(reg_isr, isr);
186
187        return rearm_timer;
188}
189
190/* Interrupt handler */
191static void
192keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
193{
194        struct keywest_iface *iface = (struct keywest_iface *)dev_id;
195
196        spin_lock(&iface->lock);
197        del_timer(&iface->timeout_timer);
198        if (handle_interrupt(iface, read_reg(reg_isr))) {
199                iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
200                add_timer(&iface->timeout_timer);
201        }
202        spin_unlock(&iface->lock);
203}
204
205static void
206keywest_timeout(unsigned long data)
207{
208        struct keywest_iface *iface = (struct keywest_iface *)data;
209
210        DBG("timeout !\n");
211        spin_lock_irq(&iface->lock);
212        if (iface->state != state_idle) {
213                iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
214                add_timer(&iface->timeout_timer);
215        }
216        spin_unlock(&iface->lock);
217}
218
219/*
220 * SMBUS-type transfer entrypoint
221 */
222static s32
223keywest_smbus_xfer(     struct i2c_adapter*     adap,
224                        u16                     addr,
225                        unsigned short          flags,
226                        char                    read_write,
227                        u8                      command,
228                        int                     size,
229                        union i2c_smbus_data*   data)
230{
231        struct keywest_chan* chan = (struct keywest_chan*)adap->data;
232        struct keywest_iface* iface = chan->iface;
233        int len;
234        u8* buffer;
235        u16 cur_word;
236        int rc = 0;
237
238        if (iface->state == state_dead)
239                return -1;
240               
241        /* Prepare datas & select mode */
242        iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
243        switch (size) {
244            case I2C_SMBUS_QUICK:
245                len = 0;
246                buffer = NULL;
247                iface->cur_mode |= KW_I2C_MODE_STANDARD;
248                break;
249            case I2C_SMBUS_BYTE:
250                len = 1;
251                buffer = &data->byte;
252                iface->cur_mode |= KW_I2C_MODE_STANDARD;
253                break;
254            case I2C_SMBUS_BYTE_DATA:
255                len = 1;
256                buffer = &data->byte;
257                iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
258                //iface->cur_mode |= KW_I2C_MODE_COMBINED;
259                break;
260            case I2C_SMBUS_WORD_DATA:
261                len = 2;
262                cur_word = cpu_to_le16(data->word);
263                buffer = (u8 *)&cur_word;
264                iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
265                //iface->cur_mode |= KW_I2C_MODE_COMBINED;
266                break;
267            case I2C_SMBUS_BLOCK_DATA:
268                len = data->block[0];
269                buffer = &data->block[1];
270                iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
271                //iface->cur_mode |= KW_I2C_MODE_COMBINED;
272                break;
273            default:
274                return -1;
275        }
276
277        /* Original driver had this limitation */
278        if (len > 32)
279                len = 32;
280
281        down(&iface->sem);
282
283        DBG("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
284                chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
285
286        iface->data = buffer;
287        iface->datalen = len;
288        iface->state = state_addr;
289        iface->result = 0;
290        iface->stopretry = 0;
291        iface->read_write = read_write;
292       
293        /* Setup channel & clear pending irqs */
294        write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
295        write_reg(reg_isr, read_reg(reg_isr));
296        write_reg(reg_status, 0);
297
298        /* Set up address and r/w bit */
299        write_reg(reg_addr,
300                (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
301
302        /* Set up the sub address */
303        if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
304            || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
305                write_reg(reg_subaddr, command);
306
307        /* Arm timeout */
308        iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
309        add_timer(&iface->timeout_timer);
310
311        /* Start sending address & enable interrupt*/
312        write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
313        write_reg(reg_ier, KW_I2C_IRQ_MASK);
314
315        wait_for_completion(&iface->complete); 
316
317        rc = iface->result;     
318        DBG("transfer done, result: %d\n", rc);
319
320        if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
321                data->word = le16_to_cpu(cur_word);
322       
323        /* Release sem */
324        up(&iface->sem);
325       
326        return rc;
327}
328
329/*
330 * Generic i2c master transfer entrypoint
331 */
332static int
333keywest_xfer(   struct i2c_adapter *adap,
334                struct i2c_msg msgs[], 
335                int num)
336{
337        struct keywest_chan* chan = (struct keywest_chan*)adap->data;
338        struct keywest_iface* iface = chan->iface;
339        struct i2c_msg *pmsg;
340        int i, completed;
341        int rc = 0;
342
343        down(&iface->sem);
344
345        /* Set adapter to standard mode */
346        iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
347        iface->cur_mode |= KW_I2C_MODE_STANDARD;
348
349        completed = 0;
350        for (i = 0; rc >= 0 && i < num;) {
351                u8 addr;
352               
353                pmsg = &msgs[i++];
354                addr = pmsg->addr;
355                if (pmsg->flags & I2C_M_TEN) {
356                        printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
357                        rc = -EINVAL;
358                        break;
359                }
360                DBG("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
361                     chan->chan_no,
362                     pmsg->flags & I2C_M_RD ? "read" : "write",
363                     pmsg->len, addr, i, num);
364   
365                /* Setup channel & clear pending irqs */
366                write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
367                write_reg(reg_isr, read_reg(reg_isr));
368                write_reg(reg_status, 0);
369               
370                iface->data = pmsg->buf;
371                iface->datalen = pmsg->len;
372                iface->state = state_addr;
373                iface->result = 0;
374                iface->stopretry = 0;
375                if (pmsg->flags & I2C_M_RD)
376                        iface->read_write = I2C_SMBUS_READ;
377                else
378                        iface->read_write = I2C_SMBUS_WRITE;
379
380                /* Set up address and r/w bit */
381                if (pmsg->flags & I2C_M_REV_DIR_ADDR)
382                        addr ^= 1;             
383                write_reg(reg_addr,
384                        (addr << 1) |
385                        ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
386
387                /* Arm timeout */
388                iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
389                add_timer(&iface->timeout_timer);
390
391                /* Start sending address & enable interrupt*/
392                write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
393                write_reg(reg_ier, KW_I2C_IRQ_MASK);
394
395                wait_for_completion(&iface->complete); 
396
397                rc = iface->result;
398                if (rc == 0)
399                        completed++;
400                DBG("transfer done, result: %d\n", rc);
401        }
402
403        /* Release sem */
404        up(&iface->sem);
405
406        return completed;
407}
408
409static u32
410keywest_func(struct i2c_adapter * adapter)
411{
412        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
413               I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
414               I2C_FUNC_SMBUS_BLOCK_DATA;
415}
416
417static void
418keywest_inc(struct i2c_adapter *adapter)
419{
420#ifdef MODULE
421        MOD_INC_USE_COUNT;
422#endif
423}
424
425static void
426keywest_dec(struct i2c_adapter *adapter)
427{
428#ifdef MODULE
429        MOD_DEC_USE_COUNT;
430#endif
431}
432
433/* For now, we only handle combined mode (smbus) */
434static struct i2c_algorithm keywest_algorithm = {
435        .name           = "Keywest i2c",
436        .id             = I2C_ALGO_SMBUS,
437        .smbus_xfer     = keywest_smbus_xfer,
438        .master_xfer    = keywest_xfer,
439        .functionality  = keywest_func,
440};
441
442
443static int __init
444create_iface(struct device_node* np)
445{
446        unsigned long steps, *psteps, *prate;
447        unsigned bsteps, tsize, i, nchan, addroffset;
448        struct keywest_iface* iface;
449        int error;
450
451        psteps = (unsigned long *)get_property(np, "AAPL,address-step", NULL);
452        steps = psteps ? (*psteps) : 0x10;
453
454        /* Hrm... maybe we can be smarter here */
455        for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
456                steps >>= 1;
457
458        if (!strcmp(np->parent->name, "uni-n")) {
459                nchan = 2;
460                addroffset = 3;
461        } else {
462                addroffset = 0;
463                nchan = 1;
464        }
465
466        tsize = sizeof(struct keywest_iface) +
467                (sizeof(struct keywest_chan) + 4) * nchan;
468        iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
469        if (iface == NULL) {
470                printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
471                return -ENOMEM;
472        }
473        memset(iface, 0, tsize);
474        init_MUTEX(&iface->sem);
475        spin_lock_init(&iface->lock);
476        init_completion(&iface->complete);
477        iface->bsteps = bsteps;
478        iface->chan_count = nchan;
479        iface->state = state_idle;
480        iface->irq = np->intrs[0].line;
481        iface->channels = (struct keywest_chan *)
482                (((unsigned long)(iface + 1) + 3UL) & ~3UL);
483        iface->base = (unsigned long)ioremap(np->addrs[0].address + addroffset,
484                                                np->addrs[0].size);
485        if (iface->base == 0) {
486                printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
487                kfree(iface);
488                return -ENOMEM;
489        }
490
491        init_timer(&iface->timeout_timer);
492        iface->timeout_timer.function = keywest_timeout;
493        iface->timeout_timer.data = (unsigned long)iface;
494
495        /* Select interface rate */
496        iface->cur_mode = KW_I2C_MODE_100KHZ;
497        prate = (unsigned long *)get_property(np, "AAPL,i2c-rate", NULL);
498        if (prate) switch(*prate) {
499        case 100:
500                iface->cur_mode = KW_I2C_MODE_100KHZ;
501                break;
502        case 50:
503                iface->cur_mode = KW_I2C_MODE_50KHZ;
504                break;
505        case 25:
506                iface->cur_mode = KW_I2C_MODE_25KHZ;
507                break;
508        default:
509                printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
510                        *prate);
511        }
512       
513        /* Select standard mode by default */
514        iface->cur_mode |= KW_I2C_MODE_STANDARD;
515       
516        /* Write mode */
517        write_reg(reg_mode, iface->cur_mode);
518       
519        /* Switch interrupts off & clear them*/
520        write_reg(reg_ier, 0x00);
521        write_reg(reg_isr, KW_I2C_IRQ_MASK);
522
523        /* Request chip interrupt */   
524        error = request_irq(iface->irq, keywest_irq, 0, "keywest i2c", iface);
525        if (error) {
526                printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
527                iounmap((void *)iface->base);
528                kfree(iface);
529                return -ENODEV;
530        }
531
532        for (i=0; i<nchan; i++) {
533                struct keywest_chan* chan = &iface->channels[i];
534                u8 addr;
535               
536                sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
537                chan->iface = iface;
538                chan->chan_no = i;
539                chan->adapter.id = I2C_ALGO_SMBUS;
540                chan->adapter.algo = &keywest_algorithm;
541                chan->adapter.algo_data = NULL;
542                chan->adapter.inc_use = keywest_inc;
543                chan->adapter.dec_use = keywest_dec;
544                chan->adapter.client_register = NULL;
545                chan->adapter.client_unregister = NULL;
546                chan->adapter.data = chan;
547
548                error = i2c_add_adapter(&chan->adapter);
549                if (error) {
550                        printk("i2c-keywest.c: Adapter %s registration failed\n",
551                                chan->adapter.name);
552                        chan->adapter.data = NULL;
553                }
554                if (probe) {
555                        printk("Probe: ");
556                        for (addr = 0x00; addr <= 0x7f; addr++) {
557                                if (i2c_smbus_xfer(&chan->adapter,addr,
558                                    0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
559                                        printk("%02x ", addr);
560                        }
561                        printk("\n");
562                }
563        }
564
565        printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
566                np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
567               
568        iface->next = ifaces;
569        ifaces = iface;
570        return 0;
571}
572
573static void __exit
574dispose_iface(struct keywest_iface *iface)
575{
576        int i, error;
577       
578        ifaces = iface->next;
579
580        /* Make sure we stop all activity */
581        down(&iface->sem);
582        spin_lock_irq(&iface->lock);
583        while (iface->state != state_idle) {
584                spin_unlock_irq(&iface->lock);
585                set_task_state(current,TASK_UNINTERRUPTIBLE);
586                schedule_timeout(HZ/10);
587                spin_lock_irq(&iface->lock);
588        }
589        iface->state = state_dead;
590        spin_unlock_irq(&iface->lock);
591        free_irq(iface->irq, iface);
592        up(&iface->sem);
593
594        /* Release all channels */
595        for (i=0; i<iface->chan_count; i++) {
596                struct keywest_chan* chan = &iface->channels[i];
597                if (!chan->adapter.data)
598                        continue;
599                i2c_del_adapter(&chan->adapter);
600                chan->adapter.data = NULL;
601                /* We aren't that prepared to deal with this... */
602                if (error)
603                        printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
604        }
605        iounmap((void *)iface->base);
606        kfree(iface);
607}
608
609static int __init i2c_keywest_init(void)
610{
611        struct device_node *np;
612        int error = -ENODEV;
613       
614        np = find_compatible_devices("i2c", "keywest");
615        while (np != 0) {
616                if (np->n_addrs >= 1 && np->n_intrs >= 1)
617                        error = create_iface(np);
618                np = np->next;
619        }
620        if (ifaces)
621                error = 0;
622        return error;
623}
624
625static void __exit i2c_keywest_exit(void)
626{
627        while(ifaces)
628                dispose_iface(ifaces);
629}
630
631module_init(i2c_keywest_init);
632module_exit(i2c_keywest_exit);
Note: See TracBrowser for help on using the browser.