ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / i2c / busses / i2c-keywest.c
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     2003/09/21 BenH     Rework state machine with Paulus help
30     2004/01/21 BenH     Merge in Greg KH changes, polled mode is back
31     2004/02/05 BenH     Merge 64 bits fixes from the g5 ppc64 tree
32
33     My understanding of the various modes supported by keywest are:
34
35      - Dumb mode : not implemented, probably direct tweaking of lines
36      - Standard mode : simple i2c transaction of type
37          S Addr R/W A Data A Data ... T
38      - Standard sub mode : combined 8 bit subaddr write with data read
39          S Addr R/W A SubAddr A Data A Data ... T
40      - Combined mode : Subaddress and Data sequences appended with no stop
41          S Addr R/W A SubAddr S Addr R/W A Data A Data ... T
42
43     Currently, this driver uses only Standard mode for i2c xfer, and
44     smbus byte & quick transfers ; and uses StandardSub mode for
45     other smbus transfers instead of combined as we need that for the
46     sound driver to be happy
47 */
48
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/config.h>
52 #include <linux/kernel.h>
53 #include <linux/ioport.h>
54 #include <linux/pci.h>
55 #include <linux/types.h>
56 #include <linux/delay.h>
57 #include <linux/i2c.h>
58 #include <linux/init.h>
59 #include <linux/mm.h>
60 #include <linux/timer.h>
61 #include <linux/spinlock.h>
62 #include <linux/completion.h>
63 #include <linux/interrupt.h>
64
65 #include <asm/io.h>
66 #include <asm/prom.h>
67 #include <asm/machdep.h>
68 #include <asm/pmac_feature.h>
69 #include <asm/pmac_low_i2c.h>
70
71 #include "i2c-keywest.h"
72
73 #undef POLLED_MODE
74
75 /* Some debug macros */
76 #define WRONG_STATE(name) do {\
77                 pr_debug("KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
78                          name, __kw_state_names[iface->state], isr);    \
79         } while(0)
80
81 #ifdef DEBUG
82 static const char *__kw_state_names[] = {
83         "state_idle",
84         "state_addr",
85         "state_read",
86         "state_write",
87         "state_stop",
88         "state_dead"
89 };
90 #endif /* DEBUG */
91
92 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
93 MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
94 MODULE_LICENSE("GPL");
95 MODULE_PARM(probe, "i");
96
97 static int probe = 0;
98
99 #ifdef POLLED_MODE
100 /* Don't schedule, the g5 fan controller is too
101  * timing sensitive
102  */
103 static u8
104 wait_interrupt(struct keywest_iface* iface)
105 {
106         int i;
107         u8 isr;
108         
109         for (i = 0; i < 200000; i++) {
110                 isr = read_reg(reg_isr) & KW_I2C_IRQ_MASK;
111                 if (isr != 0)
112                         return isr;
113                 udelay(10);
114         }
115         return isr;
116 }
117 #endif /* POLLED_MODE */
118
119 static void
120 do_stop(struct keywest_iface* iface, int result)
121 {
122         write_reg(reg_control, KW_I2C_CTL_STOP);
123         iface->state = state_stop;
124         iface->result = result;
125 }
126
127 /* Main state machine for standard & standard sub mode */
128 static void
129 handle_interrupt(struct keywest_iface *iface, u8 isr)
130 {
131         int ack;
132         
133         if (isr == 0) {
134                 if (iface->state != state_stop) {
135                         pr_debug("KW: Timeout !\n");
136                         do_stop(iface, -EIO);
137                 }
138                 if (iface->state == state_stop) {
139                         ack = read_reg(reg_status);
140                         if (!(ack & KW_I2C_STAT_BUSY)) {
141                                 iface->state = state_idle;
142                                 write_reg(reg_ier, 0x00);
143 #ifndef POLLED_MODE
144                                 complete(&iface->complete);
145 #endif /* POLLED_MODE */
146                         }
147                 }
148                 return;
149         }
150
151         if (isr & KW_I2C_IRQ_ADDR) {
152                 ack = read_reg(reg_status);
153                 if (iface->state != state_addr) {
154                         write_reg(reg_isr, KW_I2C_IRQ_ADDR);
155                         WRONG_STATE("KW_I2C_IRQ_ADDR"); 
156                         do_stop(iface, -EIO);
157                         return;
158                 }
159                 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
160                         iface->state = state_stop;                   
161                         iface->result = -ENODEV;
162                         pr_debug("KW: NAK on address\n");
163                 } else {
164                         /* Handle rw "quick" mode */
165                         if (iface->datalen == 0) {
166                                 do_stop(iface, 0);
167                         } else if (iface->read_write == I2C_SMBUS_READ) {
168                                 iface->state = state_read;
169                                 if (iface->datalen > 1)
170                                         write_reg(reg_control, KW_I2C_CTL_AAK);
171                         } else {
172                                 iface->state = state_write;
173                                 write_reg(reg_data, *(iface->data++));
174                                 iface->datalen--;
175                         }
176                 }
177                 write_reg(reg_isr, KW_I2C_IRQ_ADDR);
178         }
179
180         if (isr & KW_I2C_IRQ_DATA) {
181                 if (iface->state == state_read) {
182                         *(iface->data++) = read_reg(reg_data);
183                         write_reg(reg_isr, KW_I2C_IRQ_DATA);
184                         iface->datalen--;
185                         if (iface->datalen == 0)
186                                 iface->state = state_stop;
187                         else if (iface->datalen == 1)
188                                 write_reg(reg_control, 0);
189                 } else if (iface->state == state_write) {
190                         /* Check ack status */
191                         ack = read_reg(reg_status);
192                         if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
193                                 pr_debug("KW: nack on data write (%x): %x\n",
194                                     iface->data[-1], ack);
195                                 do_stop(iface, -EIO);
196                         } else if (iface->datalen) {
197                                 write_reg(reg_data, *(iface->data++));
198                                 iface->datalen--;
199                         } else {
200                                 write_reg(reg_control, KW_I2C_CTL_STOP);
201                                 iface->state = state_stop;
202                                 iface->result = 0;
203                         }
204                         write_reg(reg_isr, KW_I2C_IRQ_DATA);
205                 } else {
206                         write_reg(reg_isr, KW_I2C_IRQ_DATA);
207                         WRONG_STATE("KW_I2C_IRQ_DATA"); 
208                         if (iface->state != state_stop)
209                                 do_stop(iface, -EIO);
210                 }
211         }
212
213         if (isr & KW_I2C_IRQ_STOP) {
214                 write_reg(reg_isr, KW_I2C_IRQ_STOP);
215                 if (iface->state != state_stop) {
216                         WRONG_STATE("KW_I2C_IRQ_STOP");
217                         iface->result = -EIO;
218                 }
219                 iface->state = state_idle;
220                 write_reg(reg_ier, 0x00);
221 #ifndef POLLED_MODE
222                 complete(&iface->complete);
223 #endif /* POLLED_MODE */                        
224         }
225
226         if (isr & KW_I2C_IRQ_START)
227                 write_reg(reg_isr, KW_I2C_IRQ_START);
228 }
229
230 #ifndef POLLED_MODE
231
232 /* Interrupt handler */
233 static irqreturn_t
234 keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
235 {
236         struct keywest_iface *iface = (struct keywest_iface *)dev_id;
237         unsigned long flags;
238
239         spin_lock_irqsave(&iface->lock, flags);
240         del_timer(&iface->timeout_timer);
241         handle_interrupt(iface, read_reg(reg_isr));
242         if (iface->state != state_idle) {
243                 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
244                 add_timer(&iface->timeout_timer);
245         }
246         spin_unlock_irqrestore(&iface->lock, flags);
247         return IRQ_HANDLED;
248 }
249
250 static void
251 keywest_timeout(unsigned long data)
252 {
253         struct keywest_iface *iface = (struct keywest_iface *)data;
254         unsigned long flags;
255
256         pr_debug("timeout !\n");
257         spin_lock_irqsave(&iface->lock, flags);
258         handle_interrupt(iface, read_reg(reg_isr));
259         if (iface->state != state_idle) {
260                 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
261                 add_timer(&iface->timeout_timer);
262         }
263         spin_unlock_irqrestore(&iface->lock, flags);
264 }
265
266 #endif /* POLLED_MODE */
267
268 /*
269  * SMBUS-type transfer entrypoint
270  */
271 static s32
272 keywest_smbus_xfer(     struct i2c_adapter*     adap,
273                         u16                     addr,
274                         unsigned short          flags,
275                         char                    read_write,
276                         u8                      command,
277                         int                     size,
278                         union i2c_smbus_data*   data)
279 {
280         struct keywest_chan* chan = i2c_get_adapdata(adap);
281         struct keywest_iface* iface = chan->iface;
282         int len;
283         u8* buffer;
284         u16 cur_word;
285         int rc = 0;
286
287         if (iface->state == state_dead)
288                 return -ENXIO;
289                 
290         /* Prepare datas & select mode */
291         iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
292         switch (size) {
293         case I2C_SMBUS_QUICK:
294                 len = 0;
295                 buffer = NULL;
296                 iface->cur_mode |= KW_I2C_MODE_STANDARD;
297                 break;
298         case I2C_SMBUS_BYTE:
299                 len = 1;
300                 buffer = &data->byte;
301                 iface->cur_mode |= KW_I2C_MODE_STANDARD;
302                 break;
303         case I2C_SMBUS_BYTE_DATA:
304                 len = 1;
305                 buffer = &data->byte;
306                 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
307                 break;
308         case I2C_SMBUS_WORD_DATA:
309                 len = 2;
310                 cur_word = cpu_to_le16(data->word);
311                 buffer = (u8 *)&cur_word;
312                 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
313                 break;
314         case I2C_SMBUS_BLOCK_DATA:
315                 len = data->block[0];
316                 buffer = &data->block[1];
317                 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
318                 break;
319         default:
320                 return -1;
321         }
322
323         /* Turn a standardsub read into a combined mode access */
324         if (read_write == I2C_SMBUS_READ
325             && (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB) {
326                 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
327                 iface->cur_mode |= KW_I2C_MODE_COMBINED;
328         }
329
330         /* Original driver had this limitation */
331         if (len > 32)
332                 len = 32;
333
334         if (pmac_low_i2c_lock(iface->node))
335                 return -ENXIO;
336
337         pr_debug("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
338                 chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
339
340         iface->data = buffer;
341         iface->datalen = len;
342         iface->state = state_addr;
343         iface->result = 0;
344         iface->read_write = read_write;
345         
346         /* Setup channel & clear pending irqs */
347         write_reg(reg_isr, read_reg(reg_isr));
348         write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
349         write_reg(reg_status, 0);
350
351         /* Set up address and r/w bit */
352         write_reg(reg_addr,
353                 (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
354
355         /* Set up the sub address */
356         if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
357             || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
358                 write_reg(reg_subaddr, command);
359
360 #ifndef POLLED_MODE
361         /* Arm timeout */
362         iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
363         add_timer(&iface->timeout_timer);
364 #endif
365
366         /* Start sending address & enable interrupt*/
367         write_reg(reg_control, KW_I2C_CTL_XADDR);
368         write_reg(reg_ier, KW_I2C_IRQ_MASK);
369
370 #ifdef POLLED_MODE
371         pr_debug("using polled mode...\n");
372         /* State machine, to turn into an interrupt handler */
373         while(iface->state != state_idle) {
374                 unsigned long flags;
375
376                 u8 isr = wait_interrupt(iface);
377                 spin_lock_irqsave(&iface->lock, flags);
378                 handle_interrupt(iface, isr);
379                 spin_unlock_irqrestore(&iface->lock, flags);
380         }
381 #else /* POLLED_MODE */
382         pr_debug("using interrupt mode...\n");
383         wait_for_completion(&iface->complete);  
384 #endif /* POLLED_MODE */        
385
386         rc = iface->result;     
387         pr_debug("transfer done, result: %d\n", rc);
388
389         if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
390                 data->word = le16_to_cpu(cur_word);
391         
392         /* Release sem */
393         pmac_low_i2c_unlock(iface->node);
394         
395         return rc;
396 }
397
398 /*
399  * Generic i2c master transfer entrypoint
400  */
401 static int
402 keywest_xfer(   struct i2c_adapter *adap,
403                 struct i2c_msg msgs[], 
404                 int num)
405 {
406         struct keywest_chan* chan = i2c_get_adapdata(adap);
407         struct keywest_iface* iface = chan->iface;
408         struct i2c_msg *pmsg;
409         int i, completed;
410         int rc = 0;
411
412         if (iface->state == state_dead)
413                 return -ENXIO;
414
415         if (pmac_low_i2c_lock(iface->node))
416                 return -ENXIO;
417
418         /* Set adapter to standard mode */
419         iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
420         iface->cur_mode |= KW_I2C_MODE_STANDARD;
421
422         completed = 0;
423         for (i = 0; rc >= 0 && i < num;) {
424                 u8 addr;
425                 
426                 pmsg = &msgs[i++];
427                 addr = pmsg->addr;
428                 if (pmsg->flags & I2C_M_TEN) {
429                         printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
430                         rc = -EINVAL;
431                         break;
432                 }
433                 pr_debug("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
434                      chan->chan_no,
435                      pmsg->flags & I2C_M_RD ? "read" : "write",
436                      pmsg->len, addr, i, num);
437     
438                 /* Setup channel & clear pending irqs */
439                 write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
440                 write_reg(reg_isr, read_reg(reg_isr));
441                 write_reg(reg_status, 0);
442                 
443                 iface->data = pmsg->buf;
444                 iface->datalen = pmsg->len;
445                 iface->state = state_addr;
446                 iface->result = 0;
447                 if (pmsg->flags & I2C_M_RD)
448                         iface->read_write = I2C_SMBUS_READ;
449                 else
450                         iface->read_write = I2C_SMBUS_WRITE;
451
452                 /* Set up address and r/w bit */
453                 if (pmsg->flags & I2C_M_REV_DIR_ADDR)
454                         addr ^= 1;              
455                 write_reg(reg_addr,
456                         (addr << 1) |
457                         ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
458
459 #ifndef POLLED_MODE
460                 /* Arm timeout */
461                 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
462                 add_timer(&iface->timeout_timer);
463 #endif
464
465                 /* Start sending address & enable interrupt*/
466                 write_reg(reg_ier, KW_I2C_IRQ_MASK);
467                 write_reg(reg_control, KW_I2C_CTL_XADDR);
468
469 #ifdef POLLED_MODE
470                 pr_debug("using polled mode...\n");
471                 /* State machine, to turn into an interrupt handler */
472                 while(iface->state != state_idle) {
473                         u8 isr = wait_interrupt(iface);
474                         handle_interrupt(iface, isr);
475                 }
476 #else /* POLLED_MODE */
477                 pr_debug("using interrupt mode...\n");
478                 wait_for_completion(&iface->complete);  
479 #endif /* POLLED_MODE */        
480
481                 rc = iface->result;
482                 if (rc == 0)
483                         completed++;
484                 pr_debug("transfer done, result: %d\n", rc);
485         }
486
487         /* Release sem */
488         pmac_low_i2c_unlock(iface->node);
489
490         return completed;
491 }
492
493 static u32
494 keywest_func(struct i2c_adapter * adapter)
495 {
496         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
497                I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
498                I2C_FUNC_SMBUS_BLOCK_DATA;
499 }
500
501 /* For now, we only handle combined mode (smbus) */
502 static struct i2c_algorithm keywest_algorithm = {
503         .name           = "Keywest i2c",
504         .id             = I2C_ALGO_SMBUS,
505         .smbus_xfer     = keywest_smbus_xfer,
506         .master_xfer    = keywest_xfer,
507         .functionality  = keywest_func,
508 };
509
510
511 static int
512 create_iface(struct device_node *np, struct device *dev)
513 {
514         unsigned long steps;
515         unsigned bsteps, tsize, i, nchan, addroffset;
516         struct keywest_iface* iface;
517         u32 *psteps, *prate;
518         int rc;
519
520         if (pmac_low_i2c_lock(np))
521                 return -ENODEV;
522
523         psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
524         steps = psteps ? (*psteps) : 0x10;
525
526         /* Hrm... maybe we can be smarter here */
527         for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
528                 steps >>= 1;
529
530         if (np->parent->name[0] == 'u') {
531                 nchan = 2;
532                 addroffset = 3;
533         } else {
534                 addroffset = 0;
535                 nchan = 1;
536         }
537
538         tsize = sizeof(struct keywest_iface) +
539                 (sizeof(struct keywest_chan) + 4) * nchan;
540         iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
541         if (iface == NULL) {
542                 printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
543                 pmac_low_i2c_unlock(np);
544                 return -ENOMEM;
545         }
546         memset(iface, 0, tsize);
547         spin_lock_init(&iface->lock);
548         init_completion(&iface->complete);
549         iface->node = of_node_get(np);
550         iface->bsteps = bsteps;
551         iface->chan_count = nchan;
552         iface->state = state_idle;
553         iface->irq = np->intrs[0].line;
554         iface->channels = (struct keywest_chan *)
555                 (((unsigned long)(iface + 1) + 3UL) & ~3UL);
556         iface->base = (unsigned long)ioremap(np->addrs[0].address + addroffset,
557                                                 np->addrs[0].size);
558         if (iface->base == 0) {
559                 printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
560                 kfree(iface);
561                 pmac_low_i2c_unlock(np);
562                 return -ENOMEM;
563         }
564
565 #ifndef POLLED_MODE
566         init_timer(&iface->timeout_timer);
567         iface->timeout_timer.function = keywest_timeout;
568         iface->timeout_timer.data = (unsigned long)iface;
569 #endif
570
571         /* Select interface rate */
572         iface->cur_mode = KW_I2C_MODE_100KHZ;
573         prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
574         if (prate) switch(*prate) {
575         case 100:
576                 iface->cur_mode = KW_I2C_MODE_100KHZ;
577                 break;
578         case 50:
579                 iface->cur_mode = KW_I2C_MODE_50KHZ;
580                 break;
581         case 25:
582                 iface->cur_mode = KW_I2C_MODE_25KHZ;
583                 break;
584         default:
585                 printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
586                        (long)*prate);
587         }
588         
589         /* Select standard mode by default */
590         iface->cur_mode |= KW_I2C_MODE_STANDARD;
591         
592         /* Write mode */
593         write_reg(reg_mode, iface->cur_mode);
594         
595         /* Switch interrupts off & clear them*/
596         write_reg(reg_ier, 0x00);
597         write_reg(reg_isr, KW_I2C_IRQ_MASK);
598
599 #ifndef POLLED_MODE
600         /* Request chip interrupt */    
601         rc = request_irq(iface->irq, keywest_irq, SA_INTERRUPT, "keywest i2c", iface);
602         if (rc) {
603                 printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
604                 iounmap((void *)iface->base);
605                 kfree(iface);
606                 pmac_low_i2c_unlock(np);
607                 return -ENODEV;
608         }
609 #endif /* POLLED_MODE */
610
611         pmac_low_i2c_unlock(np);
612         dev_set_drvdata(dev, iface);
613         
614         for (i=0; i<nchan; i++) {
615                 struct keywest_chan* chan = &iface->channels[i];
616                 u8 addr;
617                 
618                 sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
619                 chan->iface = iface;
620                 chan->chan_no = i;
621                 chan->adapter.id = I2C_ALGO_SMBUS;
622                 chan->adapter.algo = &keywest_algorithm;
623                 chan->adapter.algo_data = NULL;
624                 chan->adapter.client_register = NULL;
625                 chan->adapter.client_unregister = NULL;
626                 i2c_set_adapdata(&chan->adapter, chan);
627                 chan->adapter.dev.parent = dev;
628
629                 rc = i2c_add_adapter(&chan->adapter);
630                 if (rc) {
631                         printk("i2c-keywest.c: Adapter %s registration failed\n",
632                                 chan->adapter.name);
633                         i2c_set_adapdata(&chan->adapter, NULL);
634                 }
635                 if (probe) {
636                         printk("Probe: ");
637                         for (addr = 0x00; addr <= 0x7f; addr++) {
638                                 if (i2c_smbus_xfer(&chan->adapter,addr,
639                                     0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
640                                         printk("%02x ", addr);
641                         }
642                         printk("\n");
643                 }
644         }
645
646         printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
647                 np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
648                 
649         return 0;
650 }
651
652 static int
653 dispose_iface(struct device *dev)
654 {
655         struct keywest_iface *iface = dev_get_drvdata(dev);
656         int i, rc;
657         
658         /* Make sure we stop all activity */
659         if (pmac_low_i2c_lock(iface->node))
660                 return -ENODEV;
661
662 #ifndef POLLED_MODE
663         spin_lock_irq(&iface->lock);
664         while (iface->state != state_idle) {
665                 spin_unlock_irq(&iface->lock);
666                 set_task_state(current,TASK_UNINTERRUPTIBLE);
667                 schedule_timeout(HZ/10);
668                 spin_lock_irq(&iface->lock);
669         }
670 #endif /* POLLED_MODE */
671         iface->state = state_dead;
672 #ifndef POLLED_MODE
673         spin_unlock_irq(&iface->lock);
674         free_irq(iface->irq, iface);
675 #endif /* POLLED_MODE */
676
677         pmac_low_i2c_unlock(iface->node);
678
679         /* Release all channels */
680         for (i=0; i<iface->chan_count; i++) {
681                 struct keywest_chan* chan = &iface->channels[i];
682                 if (i2c_get_adapdata(&chan->adapter) == NULL)
683                         continue;
684                 rc = i2c_del_adapter(&chan->adapter);
685                 i2c_set_adapdata(&chan->adapter, NULL);
686                 /* We aren't that prepared to deal with this... */
687                 if (rc)
688                         printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
689         }
690         iounmap((void *)iface->base);
691         dev_set_drvdata(dev, NULL);
692         of_node_put(iface->node);
693         kfree(iface);
694
695         return 0;
696 }
697
698 static int
699 create_iface_macio(struct macio_dev* dev, const struct of_match *match)
700 {
701         return create_iface(dev->ofdev.node, &dev->ofdev.dev);
702 }
703
704 static int
705 dispose_iface_macio(struct macio_dev* dev)
706 {
707         return dispose_iface(&dev->ofdev.dev);
708 }
709
710 static int
711 create_iface_of_platform(struct of_device* dev, const struct of_match *match)
712 {
713         return create_iface(dev->node, &dev->dev);
714 }
715
716 static int
717 dispose_iface_of_platform(struct of_device* dev)
718 {
719         return dispose_iface(&dev->dev);
720 }
721
722 static struct of_match i2c_keywest_match[] = 
723 {
724         {
725         .name           = OF_ANY_MATCH,
726         .type           = "i2c",
727         .compatible     = "keywest"
728         },
729         {},
730 };
731
732 static struct macio_driver i2c_keywest_macio_driver = 
733 {
734         .name           = "i2c-keywest",
735         .match_table    = i2c_keywest_match,
736         .probe          = create_iface_macio,
737         .remove         = dispose_iface_macio
738 };
739
740 static struct of_platform_driver i2c_keywest_of_platform_driver = 
741 {
742         .name           = "i2c-keywest",
743         .match_table    = i2c_keywest_match,
744         .probe          = create_iface_of_platform,
745         .remove         = dispose_iface_of_platform
746 };
747
748 static int __init
749 i2c_keywest_init(void)
750 {
751         of_register_driver(&i2c_keywest_of_platform_driver);
752         macio_register_driver(&i2c_keywest_macio_driver);
753
754         return 0;
755 }
756
757 static void __exit
758 i2c_keywest_cleanup(void)
759 {
760         of_unregister_driver(&i2c_keywest_of_platform_driver);
761         macio_unregister_driver(&i2c_keywest_macio_driver);
762 }
763
764 module_init(i2c_keywest_init);
765 module_exit(i2c_keywest_cleanup);