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