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.
30 My understanding of the various modes supported by keywest are:
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
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
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>
56 #include <linux/timer.h>
57 #include <linux/spinlock.h>
58 #include <linux/completion.h>
62 #include <asm/machdep.h>
63 #include <asm/pmac_feature.h>
65 #include "i2c-keywest.h"
67 #define DBG(x...) do {\
69 printk(KERN_DEBUG "KW:" x); \
73 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
74 MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
75 MODULE_LICENSE("GPL");
76 MODULE_PARM(probe, "i");
77 MODULE_PARM(debug, "i");
83 static struct keywest_iface *ifaces = NULL;
87 do_stop(struct keywest_iface* iface, int result)
89 write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_STOP);
90 iface->state = state_stop;
91 iface->result = result;
94 /* Main state machine for standard & standard sub mode */
96 handle_interrupt(struct keywest_iface *iface, u8 isr)
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) {
107 if (isr & KW_I2C_IRQ_STOP && iface->state != state_stop) {
109 iface->state = state_stop;
111 switch(iface->state) {
113 if (!(isr & KW_I2C_IRQ_ADDR)) {
117 ack = read_reg(reg_status);
118 DBG("ack on set address: %x\n", ack);
119 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
123 /* Handle rw "quick" mode */
124 if (iface->datalen == 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)
132 iface->state = state_write;
133 DBG("write byte: %x\n", *(iface->data));
134 write_reg(reg_data, *(iface->data++));
140 if (!(isr & KW_I2C_IRQ_DATA)) {
144 *(iface->data++) = read_reg(reg_data);
145 DBG("read byte: %x\n", *(iface->data-1));
147 if (iface->datalen == 0)
148 iface->state = state_stop;
150 write_reg(reg_control, 0);
153 if (!(isr & KW_I2C_IRQ_DATA)) {
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) {
164 if (iface->datalen) {
165 DBG("write byte: %x\n", *(iface->data));
166 write_reg(reg_data, *(iface->data++));
173 if (!(isr & KW_I2C_IRQ_STOP) && (++iface->stopretry) < 10)
177 iface->state = state_idle;
178 write_reg(reg_control, 0x00);
179 write_reg(reg_ier, 0x00);
180 complete(&iface->complete);
185 write_reg(reg_isr, isr);
190 /* Interrupt handler */
192 keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
194 struct keywest_iface *iface = (struct keywest_iface *)dev_id;
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);
202 spin_unlock(&iface->lock);
206 keywest_timeout(unsigned long data)
208 struct keywest_iface *iface = (struct keywest_iface *)data;
211 spin_lock_irq(&iface->lock);
212 if (handle_interrupt(iface, read_reg(reg_isr))) {
213 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
214 add_timer(&iface->timeout_timer);
216 spin_unlock(&iface->lock);
220 * SMBUS-type transfer entrypoint
223 keywest_smbus_xfer( struct i2c_adapter* adap,
225 unsigned short flags,
229 union i2c_smbus_data* data)
231 struct keywest_chan* chan = (struct keywest_chan*)adap->data;
232 struct keywest_iface* iface = chan->iface;
238 if (iface->state == state_dead)
241 /* Prepare datas & select mode */
242 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
244 case I2C_SMBUS_QUICK:
247 iface->cur_mode |= KW_I2C_MODE_STANDARD;
251 buffer = &data->byte;
252 iface->cur_mode |= KW_I2C_MODE_STANDARD;
254 case I2C_SMBUS_BYTE_DATA:
256 buffer = &data->byte;
257 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
259 case I2C_SMBUS_WORD_DATA:
261 cur_word = cpu_to_le16(data->word);
262 buffer = (u8 *)&cur_word;
263 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
265 case I2C_SMBUS_BLOCK_DATA:
266 len = data->block[0];
267 buffer = &data->block[1];
268 iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
274 /* Original driver had this limitation */
280 DBG("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
281 chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
283 iface->data = buffer;
284 iface->datalen = len;
285 iface->state = state_addr;
287 iface->stopretry = 0;
288 iface->read_write = read_write;
290 /* Setup channel & clear pending irqs */
291 write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
292 write_reg(reg_isr, read_reg(reg_isr));
293 write_reg(reg_status, 0);
295 /* Set up address and r/w bit */
297 (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
299 /* Set up the sub address */
300 if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
301 || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
302 write_reg(reg_subaddr, command);
305 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
306 add_timer(&iface->timeout_timer);
308 /* Start sending address & enable interrupt*/
309 write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
310 write_reg(reg_ier, KW_I2C_IRQ_MASK);
312 wait_for_completion(&iface->complete);
315 DBG("transfer done, result: %d\n", rc);
317 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
318 data->word = le16_to_cpu(cur_word);
327 * Generic i2c master transfer entrypoint
330 keywest_xfer( struct i2c_adapter *adap,
331 struct i2c_msg msgs[],
334 struct keywest_chan* chan = (struct keywest_chan*)adap->data;
335 struct keywest_iface* iface = chan->iface;
336 struct i2c_msg *pmsg;
342 /* Set adapter to standard mode */
343 iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
344 iface->cur_mode |= KW_I2C_MODE_STANDARD;
347 for (i = 0; rc >= 0 && i < num;) {
352 if (pmsg->flags & I2C_M_TEN) {
353 printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
357 DBG("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
359 pmsg->flags & I2C_M_RD ? "read" : "write",
360 pmsg->len, addr, i, num);
362 /* Setup channel & clear pending irqs */
363 write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
364 write_reg(reg_isr, read_reg(reg_isr));
365 write_reg(reg_status, 0);
367 iface->data = pmsg->buf;
368 iface->datalen = pmsg->len;
369 iface->state = state_addr;
371 iface->stopretry = 0;
372 if (pmsg->flags & I2C_M_RD)
373 iface->read_write = I2C_SMBUS_READ;
375 iface->read_write = I2C_SMBUS_WRITE;
377 /* Set up address and r/w bit */
378 if (pmsg->flags & I2C_M_REV_DIR_ADDR)
382 ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
385 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
386 add_timer(&iface->timeout_timer);
388 /* Start sending address & enable interrupt*/
389 write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
390 write_reg(reg_ier, KW_I2C_IRQ_MASK);
392 wait_for_completion(&iface->complete);
397 DBG("transfer done, result: %d\n", rc);
407 keywest_func(struct i2c_adapter * adapter)
409 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
410 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
411 I2C_FUNC_SMBUS_BLOCK_DATA;
415 keywest_inc(struct i2c_adapter *adapter)
421 keywest_dec(struct i2c_adapter *adapter)
426 /* For now, we only handle combined mode (smbus) */
427 static struct i2c_algorithm keywest_algorithm = {
430 smbus_xfer: keywest_smbus_xfer,
431 master_xfer: keywest_xfer,
432 functionality: keywest_func,
437 create_iface(struct device_node* np)
439 unsigned long steps, *psteps, *prate;
440 unsigned bsteps, tsize, i, nchan, addroffset;
441 struct keywest_iface* iface;
444 psteps = (unsigned long *)get_property(np, "AAPL,address-step", NULL);
445 steps = psteps ? (*psteps) : 0x10;
447 /* Hrm... maybe we can be smarter here */
448 for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
451 if (!strcmp(np->parent->name, "uni-n")) {
459 tsize = sizeof(struct keywest_iface) +
460 (sizeof(struct keywest_chan) + 4) * nchan;
461 iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
463 printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
466 memset(iface, 0, tsize);
467 init_MUTEX(&iface->sem);
468 spin_lock_init(&iface->lock);
469 init_completion(&iface->complete);
470 iface->bsteps = bsteps;
471 iface->chan_count = nchan;
472 iface->state = state_idle;
473 iface->irq = np->intrs[0].line;
474 iface->channels = (struct keywest_chan *)
475 (((unsigned long)(iface + 1) + 3UL) & ~3UL);
476 iface->base = (unsigned long)ioremap(np->addrs[0].address + addroffset,
478 if (iface->base == 0) {
479 printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
484 init_timer(&iface->timeout_timer);
485 iface->timeout_timer.function = keywest_timeout;
486 iface->timeout_timer.data = (unsigned long)iface;
488 /* Select interface rate */
489 iface->cur_mode = KW_I2C_MODE_100KHZ;
490 prate = (unsigned long *)get_property(np, "AAPL,i2c-rate", NULL);
491 if (prate) switch(*prate) {
493 iface->cur_mode = KW_I2C_MODE_100KHZ;
496 iface->cur_mode = KW_I2C_MODE_50KHZ;
499 iface->cur_mode = KW_I2C_MODE_25KHZ;
502 printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
506 /* Select standard mode by default */
507 iface->cur_mode |= KW_I2C_MODE_STANDARD;
510 write_reg(reg_mode, iface->cur_mode);
512 /* Switch interrupts off & clear them*/
513 write_reg(reg_ier, 0x00);
514 write_reg(reg_isr, KW_I2C_IRQ_MASK);
516 /* Request chip interrupt */
517 rc = request_irq(iface->irq, keywest_irq, 0, "keywest i2c", iface);
519 printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
520 iounmap((void *)iface->base);
525 for (i=0; i<nchan; i++) {
526 struct keywest_chan* chan = &iface->channels[i];
529 sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
532 chan->adapter.id = I2C_ALGO_SMBUS;
533 chan->adapter.algo = &keywest_algorithm;
534 chan->adapter.algo_data = NULL;
535 chan->adapter.inc_use = keywest_inc;
536 chan->adapter.dec_use = keywest_dec;
537 chan->adapter.client_register = NULL;
538 chan->adapter.client_unregister = NULL;
539 chan->adapter.data = chan;
541 rc = i2c_add_adapter(&chan->adapter);
543 printk("i2c-keywest.c: Adapter %s registration failed\n",
545 chan->adapter.data = NULL;
549 for (addr = 0x00; addr <= 0x7f; addr++) {
550 if (i2c_smbus_xfer(&chan->adapter,addr,
551 0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
552 printk("%02x ", addr);
558 printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
559 np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
561 iface->next = ifaces;
567 dispose_iface(struct keywest_iface *iface)
571 ifaces = iface->next;
573 /* Make sure we stop all activity */
575 spin_lock_irq(&iface->lock);
576 while (iface->state != state_idle) {
577 spin_unlock_irq(&iface->lock);
578 set_task_state(current,TASK_UNINTERRUPTIBLE);
579 schedule_timeout(HZ/10);
580 spin_lock_irq(&iface->lock);
582 iface->state = state_dead;
583 spin_unlock_irq(&iface->lock);
584 free_irq(iface->irq, iface);
587 /* Release all channels */
588 for (i=0; i<iface->chan_count; i++) {
589 struct keywest_chan* chan = &iface->channels[i];
590 if (!chan->adapter.data)
592 rc = i2c_del_adapter(&chan->adapter);
593 chan->adapter.data = NULL;
594 /* We aren't that prepared to deal with this... */
596 printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
598 iounmap((void *)iface->base);
603 i2c_keywest_init(void)
605 struct device_node *np;
608 np = find_compatible_devices("i2c", "keywest");
610 if (np->n_addrs >= 1 && np->n_intrs >= 1)
611 rc = create_iface(np);
620 i2c_keywest_cleanup(void)
623 dispose_iface(ifaces);
626 module_init(i2c_keywest_init);
627 module_exit(i2c_keywest_cleanup);