2 i2c Support for Apple Keywest I2C Bus Controller
4 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
8 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
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.
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.
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.
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
33 My understanding of the various modes supported by keywest are:
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
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
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>
60 #include <linux/timer.h>
61 #include <linux/spinlock.h>
62 #include <linux/completion.h>
63 #include <linux/interrupt.h>
67 #include <asm/machdep.h>
68 #include <asm/pmac_feature.h>
69 #include <asm/pmac_low_i2c.h>
71 #include "i2c-keywest.h"
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); \
82 static const char *__kw_state_names[] = {
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");
100 /* Don't schedule, the g5 fan controller is too
104 wait_interrupt(struct keywest_iface* iface)
109 for (i = 0; i < 200000; i++) {
110 isr = read_reg(reg_isr) & KW_I2C_IRQ_MASK;
117 #endif /* POLLED_MODE */
120 do_stop(struct keywest_iface* iface, int result)
122 write_reg(reg_control, KW_I2C_CTL_STOP);
123 iface->state = state_stop;
124 iface->result = result;
127 /* Main state machine for standard & standard sub mode */
129 handle_interrupt(struct keywest_iface *iface, u8 isr)
134 if (iface->state != state_stop) {
135 pr_debug("KW: Timeout !\n");
136 do_stop(iface, -EIO);
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);
144 complete(&iface->complete);
145 #endif /* POLLED_MODE */
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);
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");
164 /* Handle rw "quick" mode */
165 if (iface->datalen == 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);
172 iface->state = state_write;
173 write_reg(reg_data, *(iface->data++));
177 write_reg(reg_isr, KW_I2C_IRQ_ADDR);
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);
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++));
200 write_reg(reg_control, KW_I2C_CTL_STOP);
201 iface->state = state_stop;
204 write_reg(reg_isr, KW_I2C_IRQ_DATA);
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);
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;
219 iface->state = state_idle;
220 write_reg(reg_ier, 0x00);
222 complete(&iface->complete);
223 #endif /* POLLED_MODE */
226 if (isr & KW_I2C_IRQ_START)
227 write_reg(reg_isr, KW_I2C_IRQ_START);
232 /* Interrupt handler */
234 keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
236 struct keywest_iface *iface = (struct keywest_iface *)dev_id;
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);
246 spin_unlock_irqrestore(&iface->lock, flags);
251 keywest_timeout(unsigned long data)
253 struct keywest_iface *iface = (struct keywest_iface *)data;
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);
263 spin_unlock_irqrestore(&iface->lock, flags);
266 #endif /* POLLED_MODE */
269 * SMBUS-type transfer entrypoint
272 keywest_smbus_xfer( struct i2c_adapter* adap,
274 unsigned short flags,
278 union i2c_smbus_data* data)
280 struct keywest_chan* chan = i2c_get_adapdata(adap);
281 struct keywest_iface* iface = chan->iface;
287 if (iface->state == state_dead)
290 /* Prepare datas & select mode */
291 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
293 case I2C_SMBUS_QUICK:
296 iface->cur_mode |= KW_I2C_MODE_STANDARD;
300 buffer = &data->byte;
301 iface->cur_mode |= KW_I2C_MODE_STANDARD;
303 case I2C_SMBUS_BYTE_DATA:
305 buffer = &data->byte;
306 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
308 case I2C_SMBUS_WORD_DATA:
310 cur_word = cpu_to_le16(data->word);
311 buffer = (u8 *)&cur_word;
312 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
314 case I2C_SMBUS_BLOCK_DATA:
315 len = data->block[0];
316 buffer = &data->block[1];
317 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
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;
330 /* Original driver had this limitation */
334 if (pmac_low_i2c_lock(iface->node))
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);
340 iface->data = buffer;
341 iface->datalen = len;
342 iface->state = state_addr;
344 iface->read_write = read_write;
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);
351 /* Set up address and r/w bit */
353 (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
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);
362 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
363 add_timer(&iface->timeout_timer);
366 /* Start sending address & enable interrupt*/
367 write_reg(reg_control, KW_I2C_CTL_XADDR);
368 write_reg(reg_ier, KW_I2C_IRQ_MASK);
371 pr_debug("using polled mode...\n");
372 /* State machine, to turn into an interrupt handler */
373 while(iface->state != state_idle) {
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);
381 #else /* POLLED_MODE */
382 pr_debug("using interrupt mode...\n");
383 wait_for_completion(&iface->complete);
384 #endif /* POLLED_MODE */
387 pr_debug("transfer done, result: %d\n", rc);
389 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
390 data->word = le16_to_cpu(cur_word);
393 pmac_low_i2c_unlock(iface->node);
399 * Generic i2c master transfer entrypoint
402 keywest_xfer( struct i2c_adapter *adap,
403 struct i2c_msg msgs[],
406 struct keywest_chan* chan = i2c_get_adapdata(adap);
407 struct keywest_iface* iface = chan->iface;
408 struct i2c_msg *pmsg;
412 if (iface->state == state_dead)
415 if (pmac_low_i2c_lock(iface->node))
418 /* Set adapter to standard mode */
419 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
420 iface->cur_mode |= KW_I2C_MODE_STANDARD;
423 for (i = 0; rc >= 0 && i < num;) {
428 if (pmsg->flags & I2C_M_TEN) {
429 printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
433 pr_debug("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
435 pmsg->flags & I2C_M_RD ? "read" : "write",
436 pmsg->len, addr, i, num);
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);
443 iface->data = pmsg->buf;
444 iface->datalen = pmsg->len;
445 iface->state = state_addr;
447 if (pmsg->flags & I2C_M_RD)
448 iface->read_write = I2C_SMBUS_READ;
450 iface->read_write = I2C_SMBUS_WRITE;
452 /* Set up address and r/w bit */
453 if (pmsg->flags & I2C_M_REV_DIR_ADDR)
457 ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
461 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
462 add_timer(&iface->timeout_timer);
465 /* Start sending address & enable interrupt*/
466 write_reg(reg_ier, KW_I2C_IRQ_MASK);
467 write_reg(reg_control, KW_I2C_CTL_XADDR);
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);
476 #else /* POLLED_MODE */
477 pr_debug("using interrupt mode...\n");
478 wait_for_completion(&iface->complete);
479 #endif /* POLLED_MODE */
484 pr_debug("transfer done, result: %d\n", rc);
488 pmac_low_i2c_unlock(iface->node);
494 keywest_func(struct i2c_adapter * adapter)
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;
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,
512 create_iface(struct device_node *np, struct device *dev)
515 unsigned bsteps, tsize, i, nchan, addroffset;
516 struct keywest_iface* iface;
520 if (pmac_low_i2c_lock(np))
523 psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
524 steps = psteps ? (*psteps) : 0x10;
526 /* Hrm... maybe we can be smarter here */
527 for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
530 if (np->parent->name[0] == 'u') {
538 tsize = sizeof(struct keywest_iface) +
539 (sizeof(struct keywest_chan) + 4) * nchan;
540 iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
542 printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
543 pmac_low_i2c_unlock(np);
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,
558 if (iface->base == 0) {
559 printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
561 pmac_low_i2c_unlock(np);
566 init_timer(&iface->timeout_timer);
567 iface->timeout_timer.function = keywest_timeout;
568 iface->timeout_timer.data = (unsigned long)iface;
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) {
576 iface->cur_mode = KW_I2C_MODE_100KHZ;
579 iface->cur_mode = KW_I2C_MODE_50KHZ;
582 iface->cur_mode = KW_I2C_MODE_25KHZ;
585 printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
589 /* Select standard mode by default */
590 iface->cur_mode |= KW_I2C_MODE_STANDARD;
593 write_reg(reg_mode, iface->cur_mode);
595 /* Switch interrupts off & clear them*/
596 write_reg(reg_ier, 0x00);
597 write_reg(reg_isr, KW_I2C_IRQ_MASK);
600 /* Request chip interrupt */
601 rc = request_irq(iface->irq, keywest_irq, SA_INTERRUPT, "keywest i2c", iface);
603 printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
604 iounmap((void *)iface->base);
606 pmac_low_i2c_unlock(np);
609 #endif /* POLLED_MODE */
611 pmac_low_i2c_unlock(np);
612 dev_set_drvdata(dev, iface);
614 for (i=0; i<nchan; i++) {
615 struct keywest_chan* chan = &iface->channels[i];
618 sprintf(chan->adapter.name, "%s %d", np->parent->name, 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;
629 rc = i2c_add_adapter(&chan->adapter);
631 printk("i2c-keywest.c: Adapter %s registration failed\n",
633 i2c_set_adapdata(&chan->adapter, NULL);
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);
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);
653 dispose_iface(struct device *dev)
655 struct keywest_iface *iface = dev_get_drvdata(dev);
658 /* Make sure we stop all activity */
659 if (pmac_low_i2c_lock(iface->node))
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);
670 #endif /* POLLED_MODE */
671 iface->state = state_dead;
673 spin_unlock_irq(&iface->lock);
674 free_irq(iface->irq, iface);
675 #endif /* POLLED_MODE */
677 pmac_low_i2c_unlock(iface->node);
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)
684 rc = i2c_del_adapter(&chan->adapter);
685 i2c_set_adapdata(&chan->adapter, NULL);
686 /* We aren't that prepared to deal with this... */
688 printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
690 iounmap((void *)iface->base);
691 dev_set_drvdata(dev, NULL);
692 of_node_put(iface->node);
699 create_iface_macio(struct macio_dev* dev, const struct of_match *match)
701 return create_iface(dev->ofdev.node, &dev->ofdev.dev);
705 dispose_iface_macio(struct macio_dev* dev)
707 return dispose_iface(&dev->ofdev.dev);
711 create_iface_of_platform(struct of_device* dev, const struct of_match *match)
713 return create_iface(dev->node, &dev->dev);
717 dispose_iface_of_platform(struct of_device* dev)
719 return dispose_iface(&dev->dev);
722 static struct of_match i2c_keywest_match[] =
725 .name = OF_ANY_MATCH,
727 .compatible = "keywest"
732 static struct macio_driver i2c_keywest_macio_driver =
734 .name = "i2c-keywest",
735 .match_table = i2c_keywest_match,
736 .probe = create_iface_macio,
737 .remove = dispose_iface_macio
740 static struct of_platform_driver i2c_keywest_of_platform_driver =
742 .name = "i2c-keywest",
743 .match_table = i2c_keywest_match,
744 .probe = create_iface_of_platform,
745 .remove = dispose_iface_of_platform
749 i2c_keywest_init(void)
751 of_register_driver(&i2c_keywest_of_platform_driver);
752 macio_register_driver(&i2c_keywest_macio_driver);
758 i2c_keywest_cleanup(void)
760 of_unregister_driver(&i2c_keywest_of_platform_driver);
761 macio_unregister_driver(&i2c_keywest_macio_driver);
764 module_init(i2c_keywest_init);
765 module_exit(i2c_keywest_cleanup);