2 * arch/powerpc/platforms/powermac/low_i2c.c
4 * Copyright (C) 2003-2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * The linux i2c layer isn't completely suitable for our needs for various
12 * reasons ranging from too late initialisation to semantics not perfectly
13 * matching some requirements of the apple platform functions etc...
15 * This file thus provides a simple low level unified i2c interface for
16 * powermac that covers the various types of i2c busses used in Apple machines.
17 * For now, keywest, PMU and SMU, though we could add Cuda, or other bit
18 * banging busses found on older chipstes in earlier machines if we ever need
21 * The drivers in this file are synchronous/blocking. In addition, the
22 * keywest one is fairly slow due to the use of msleep instead of interrupts
23 * as the interrupt is currently used by i2c-keywest. In the long run, we
24 * might want to get rid of those high-level interfaces to linux i2c layer
25 * either completely (converting all drivers) or replacing them all with a
26 * single stub driver on top of this one. Once done, the interrupt will be
27 * available for our use.
33 #include <linux/config.h>
34 #include <linux/types.h>
35 #include <linux/sched.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/adb.h>
39 #include <linux/pmu.h>
40 #include <linux/delay.h>
41 #include <linux/completion.h>
42 #include <linux/platform_device.h>
43 #include <linux/interrupt.h>
44 #include <linux/completion.h>
45 #include <linux/timer.h>
46 #include <asm/keylargo.h>
47 #include <asm/uninorth.h>
50 #include <asm/machdep.h>
52 #include <asm/pmac_low_i2c.h>
55 #define DBG(x...) do {\
56 printk(KERN_DEBUG "low_i2c:" x); \
63 #define DBG_LOW(x...) do {\
64 printk(KERN_DEBUG "low_i2c:" x); \
71 static int pmac_i2c_force_poll = 1;
74 * A bus structure. Each bus in the system has such a structure associated.
78 struct list_head link;
79 struct device_node *controller;
80 struct device_node *busnode;
83 struct i2c_adapter *adapter;
85 int channel; /* some hosts have multiple */
86 int mode; /* current mode */
89 int polled; /* open mode */
90 struct platform_device *platform_dev;
93 int (*open)(struct pmac_i2c_bus *bus);
94 void (*close)(struct pmac_i2c_bus *bus);
95 int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
96 u32 subaddr, u8 *data, int len);
99 static LIST_HEAD(pmac_i2c_busses);
102 * Keywest implementation
105 struct pmac_i2c_host_kw
107 struct semaphore mutex; /* Access mutex for use by
109 void __iomem *base; /* register base address */
110 int bsteps; /* register stepping */
111 int speed; /* speed */
119 struct completion complete;
121 struct timer_list timeout_timer;
124 /* Register indices */
136 /* The Tumbler audio equalizer can be really slow sometimes */
137 #define KW_POLL_TIMEOUT (2*HZ)
140 #define KW_I2C_MODE_100KHZ 0x00
141 #define KW_I2C_MODE_50KHZ 0x01
142 #define KW_I2C_MODE_25KHZ 0x02
143 #define KW_I2C_MODE_DUMB 0x00
144 #define KW_I2C_MODE_STANDARD 0x04
145 #define KW_I2C_MODE_STANDARDSUB 0x08
146 #define KW_I2C_MODE_COMBINED 0x0C
147 #define KW_I2C_MODE_MODE_MASK 0x0C
148 #define KW_I2C_MODE_CHAN_MASK 0xF0
150 /* Control register */
151 #define KW_I2C_CTL_AAK 0x01
152 #define KW_I2C_CTL_XADDR 0x02
153 #define KW_I2C_CTL_STOP 0x04
154 #define KW_I2C_CTL_START 0x08
156 /* Status register */
157 #define KW_I2C_STAT_BUSY 0x01
158 #define KW_I2C_STAT_LAST_AAK 0x02
159 #define KW_I2C_STAT_LAST_RW 0x04
160 #define KW_I2C_STAT_SDA 0x08
161 #define KW_I2C_STAT_SCL 0x10
163 /* IER & ISR registers */
164 #define KW_I2C_IRQ_DATA 0x01
165 #define KW_I2C_IRQ_ADDR 0x02
166 #define KW_I2C_IRQ_STOP 0x04
167 #define KW_I2C_IRQ_START 0x08
168 #define KW_I2C_IRQ_MASK 0x0F
170 /* State machine states */
180 #define WRONG_STATE(name) do {\
181 printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \
183 name, __kw_state_names[host->state], isr); \
186 static const char *__kw_state_names[] = {
195 static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)
197 return readb(host->base + (((unsigned int)reg) << host->bsteps));
200 static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,
203 writeb(val, host->base + (((unsigned)reg) << host->bsteps));
204 (void)__kw_read_reg(host, reg_subaddr);
207 #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
208 #define kw_read_reg(reg) __kw_read_reg(host, reg)
210 static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)
215 for (i = 0; i < 1000; i++) {
216 isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
220 /* This code is used with the timebase frozen, we cannot rely
221 * on udelay nor schedule when in polled mode !
222 * For now, just use a bogus loop....
225 for (j = 1; j < 100000; j++)
233 static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)
237 DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",
238 __kw_state_names[host->state], isr);
240 if (host->state == state_idle) {
241 printk(KERN_WARNING "low_i2c: Keywest got an out of state"
242 " interrupt, ignoring\n");
243 kw_write_reg(reg_isr, isr);
248 if (host->state != state_stop) {
249 DBG_LOW("KW: Timeout !\n");
253 if (host->state == state_stop) {
254 ack = kw_read_reg(reg_status);
255 if (ack & KW_I2C_STAT_BUSY)
256 kw_write_reg(reg_status, 0);
257 host->state = state_idle;
258 kw_write_reg(reg_ier, 0x00);
260 complete(&host->complete);
265 if (isr & KW_I2C_IRQ_ADDR) {
266 ack = kw_read_reg(reg_status);
267 if (host->state != state_addr) {
268 kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
269 WRONG_STATE("KW_I2C_IRQ_ADDR");
273 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
274 host->result = -ENODEV;
275 DBG_LOW("KW: NAK on address\n");
276 host->state = state_stop;
279 if (host->len == 0) {
280 kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
284 host->state = state_read;
286 kw_write_reg(reg_control,
289 host->state = state_write;
290 kw_write_reg(reg_data, *(host->data++));
294 kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
297 if (isr & KW_I2C_IRQ_DATA) {
298 if (host->state == state_read) {
299 *(host->data++) = kw_read_reg(reg_data);
301 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
303 host->state = state_stop;
304 else if (host->len == 1)
305 kw_write_reg(reg_control, 0);
306 } else if (host->state == state_write) {
307 ack = kw_read_reg(reg_status);
308 if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
309 DBG_LOW("KW: nack on data write\n");
312 } else if (host->len) {
313 kw_write_reg(reg_data, *(host->data++));
316 kw_write_reg(reg_control, KW_I2C_CTL_STOP);
317 host->state = state_stop;
320 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
322 kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
323 WRONG_STATE("KW_I2C_IRQ_DATA");
324 if (host->state != state_stop) {
331 if (isr & KW_I2C_IRQ_STOP) {
332 kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
333 if (host->state != state_stop) {
334 WRONG_STATE("KW_I2C_IRQ_STOP");
337 host->state = state_idle;
339 complete(&host->complete);
342 if (isr & KW_I2C_IRQ_START)
343 kw_write_reg(reg_isr, KW_I2C_IRQ_START);
347 kw_write_reg(reg_control, KW_I2C_CTL_STOP);
348 host->state = state_stop;
352 /* Interrupt handler */
353 static irqreturn_t kw_i2c_irq(int irq, void *dev_id, struct pt_regs *regs)
355 struct pmac_i2c_host_kw *host = dev_id;
358 spin_lock_irqsave(&host->lock, flags);
359 del_timer(&host->timeout_timer);
360 kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
361 if (host->state != state_idle) {
362 host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
363 add_timer(&host->timeout_timer);
365 spin_unlock_irqrestore(&host->lock, flags);
369 static void kw_i2c_timeout(unsigned long data)
371 struct pmac_i2c_host_kw *host = (struct pmac_i2c_host_kw *)data;
374 spin_lock_irqsave(&host->lock, flags);
375 kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));
376 if (host->state != state_idle) {
377 host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
378 add_timer(&host->timeout_timer);
380 spin_unlock_irqrestore(&host->lock, flags);
383 static int kw_i2c_open(struct pmac_i2c_bus *bus)
385 struct pmac_i2c_host_kw *host = bus->hostdata;
390 static void kw_i2c_close(struct pmac_i2c_bus *bus)
392 struct pmac_i2c_host_kw *host = bus->hostdata;
396 static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
397 u32 subaddr, u8 *data, int len)
399 struct pmac_i2c_host_kw *host = bus->hostdata;
400 u8 mode_reg = host->speed;
401 int use_irq = host->irq != NO_IRQ && !bus->polled;
403 /* Setup mode & subaddress if any */
405 case pmac_i2c_mode_dumb:
407 case pmac_i2c_mode_std:
408 mode_reg |= KW_I2C_MODE_STANDARD;
412 case pmac_i2c_mode_stdsub:
413 mode_reg |= KW_I2C_MODE_STANDARDSUB;
417 case pmac_i2c_mode_combined:
418 mode_reg |= KW_I2C_MODE_COMBINED;
424 /* Setup channel & clear pending irqs */
425 kw_write_reg(reg_isr, kw_read_reg(reg_isr));
426 kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));
427 kw_write_reg(reg_status, 0);
429 /* Set up address and r/w bit, strip possible stale bus number from
432 kw_write_reg(reg_addr, addrdir & 0xff);
434 /* Set up the sub address */
435 if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
436 || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
437 kw_write_reg(reg_subaddr, subaddr);
439 /* Prepare for async operations */
442 host->state = state_addr;
444 host->rw = (addrdir & 1);
445 host->polled = bus->polled;
447 /* Enable interrupt if not using polled mode and interrupt is
451 /* Clear completion */
452 INIT_COMPLETION(host->complete);
453 /* Ack stale interrupts */
454 kw_write_reg(reg_isr, kw_read_reg(reg_isr));
456 host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;
457 add_timer(&host->timeout_timer);
458 /* Enable emission */
459 kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);
462 /* Start sending address */
463 kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
465 /* Wait for completion */
467 wait_for_completion(&host->complete);
469 while(host->state != state_idle) {
472 u8 isr = kw_i2c_wait_interrupt(host);
473 spin_lock_irqsave(&host->lock, flags);
474 kw_i2c_handle_interrupt(host, isr);
475 spin_unlock_irqrestore(&host->lock, flags);
479 /* Disable emission */
480 kw_write_reg(reg_ier, 0);
485 static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)
487 struct pmac_i2c_host_kw *host;
488 u32 *psteps, *prate, *addrp, steps;
490 host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);
492 printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
497 /* Apple is kind enough to provide a valid AAPL,address property
498 * on all i2c keywest nodes so far ... we would have to fallback
499 * to macio parsing if that wasn't the case
501 addrp = (u32 *)get_property(np, "AAPL,address", NULL);
503 printk(KERN_ERR "low_i2c: Can't find address for %s\n",
508 init_MUTEX(&host->mutex);
509 init_completion(&host->complete);
510 spin_lock_init(&host->lock);
511 init_timer(&host->timeout_timer);
512 host->timeout_timer.function = kw_i2c_timeout;
513 host->timeout_timer.data = (unsigned long)host;
515 psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
516 steps = psteps ? (*psteps) : 0x10;
517 for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
519 /* Select interface rate */
520 host->speed = KW_I2C_MODE_25KHZ;
521 prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
522 if (prate) switch(*prate) {
524 host->speed = KW_I2C_MODE_100KHZ;
527 host->speed = KW_I2C_MODE_50KHZ;
530 host->speed = KW_I2C_MODE_25KHZ;
534 host->irq = np->intrs[0].line;
538 host->base = ioremap((*addrp), 0x1000);
539 if (host->base == NULL) {
540 printk(KERN_ERR "low_i2c: Can't map registers for %s\n",
546 /* Make sure IRA is disabled */
547 kw_write_reg(reg_ier, 0);
549 /* Request chip interrupt */
550 if (request_irq(host->irq, kw_i2c_irq, SA_SHIRQ, "keywest i2c", host))
553 printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",
554 *addrp, host->irq, np->full_name);
560 static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,
561 struct device_node *controller,
562 struct device_node *busnode,
565 struct pmac_i2c_bus *bus;
567 bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);
571 bus->controller = of_node_get(controller);
572 bus->busnode = of_node_get(busnode);
573 bus->type = pmac_i2c_bus_keywest;
574 bus->hostdata = host;
575 bus->channel = channel;
576 bus->mode = pmac_i2c_mode_std;
577 bus->open = kw_i2c_open;
578 bus->close = kw_i2c_close;
579 bus->xfer = kw_i2c_xfer;
580 init_MUTEX(&bus->sem);
581 if (controller == busnode)
582 bus->flags = pmac_i2c_multibus;
583 list_add(&bus->link, &pmac_i2c_busses);
585 printk(KERN_INFO " channel %d bus %s\n", channel,
586 (controller == busnode) ? "<multibus>" : busnode->full_name);
589 static void __init kw_i2c_probe(void)
591 struct device_node *np, *child, *parent;
593 /* Probe keywest-i2c busses */
595 (np = of_find_compatible_node(np, "i2c","keywest-i2c")) != NULL;){
596 struct pmac_i2c_host_kw *host;
597 int multibus, chans, i;
599 /* Found one, init a host structure */
600 host = kw_i2c_host_init(np);
604 /* Now check if we have a multibus setup (old style) or if we
605 * have proper bus nodes. Note that the "new" way (proper bus
606 * nodes) might cause us to not create some busses that are
607 * kept hidden in the device-tree. In the future, we might
608 * want to work around that by creating busses without a node
611 child = of_get_next_child(np, NULL);
612 multibus = !child || strcmp(child->name, "i2c-bus");
615 /* For a multibus setup, we get the bus count based on the
619 parent = of_get_parent(np);
622 chans = parent->name[0] == 'u' ? 2 : 1;
623 for (i = 0; i < chans; i++)
624 kw_i2c_add(host, np, np, i);
627 (child = of_get_next_child(np, child)) != NULL;) {
629 (u32 *)get_property(child, "reg", NULL);
632 kw_i2c_add(host, np, child, *reg);
645 #ifdef CONFIG_ADB_PMU
648 * i2c command block to the PMU
661 static void pmu_i2c_complete(struct adb_request *req)
666 static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
667 u32 subaddr, u8 *data, int len)
669 struct adb_request *req = bus->hostdata;
670 struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];
671 struct completion comp;
672 int read = addrdir & 1;
676 /* For now, limit ourselves to 16 bytes transfers */
680 init_completion(&comp);
682 for (retry = 0; retry < 16; retry++) {
683 memset(req, 0, sizeof(struct adb_request));
684 hdr->bus = bus->channel;
688 case pmac_i2c_mode_std:
691 hdr->address = addrdir;
692 hdr->mode = PMU_I2C_MODE_SIMPLE;
694 case pmac_i2c_mode_stdsub:
695 case pmac_i2c_mode_combined:
698 hdr->address = addrdir & 0xfe;
699 hdr->comb_addr = addrdir;
700 hdr->sub_addr = subaddr;
701 if (bus->mode == pmac_i2c_mode_stdsub)
702 hdr->mode = PMU_I2C_MODE_STDSUB;
704 hdr->mode = PMU_I2C_MODE_COMBINED;
710 INIT_COMPLETION(comp);
711 req->data[0] = PMU_I2C_CMD;
712 req->reply[0] = 0xff;
713 req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;
714 req->done = pmu_i2c_complete;
717 memcpy(hdr->data, data, len);
720 rc = pmu_queue_request(req);
723 wait_for_completion(&comp);
724 if (req->reply[0] == PMU_I2C_STATUS_OK)
728 if (req->reply[0] != PMU_I2C_STATUS_OK)
731 for (retry = 0; retry < 16; retry++) {
732 memset(req, 0, sizeof(struct adb_request));
734 /* I know that looks like a lot, slow as hell, but darwin
735 * does it so let's be on the safe side for now
739 hdr->bus = PMU_I2C_BUS_STATUS;
741 INIT_COMPLETION(comp);
742 req->data[0] = PMU_I2C_CMD;
743 req->reply[0] = 0xff;
745 req->done = pmu_i2c_complete;
747 rc = pmu_queue_request(req);
750 wait_for_completion(&comp);
752 if (req->reply[0] == PMU_I2C_STATUS_OK && !read)
754 if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {
755 int rlen = req->reply_len - 1;
758 printk(KERN_WARNING "low_i2c: PMU returned %d"
759 " bytes, expected %d !\n", rlen, len);
763 memcpy(data, &req->reply[1], len);
770 static void __init pmu_i2c_probe(void)
772 struct pmac_i2c_bus *bus;
773 struct device_node *busnode;
779 /* There might or might not be a "pmu-i2c" node, we use that
780 * or via-pmu itself, whatever we find. I haven't seen a machine
781 * with separate bus nodes, so we assume a multibus setup
783 busnode = of_find_node_by_name(NULL, "pmu-i2c");
785 busnode = of_find_node_by_name(NULL, "via-pmu");
789 printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);
792 * We add bus 1 and 2 only for now, bus 0 is "special"
794 for (channel = 1; channel <= 2; channel++) {
795 sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);
796 bus = kzalloc(sz, GFP_KERNEL);
800 bus->controller = busnode;
801 bus->busnode = busnode;
802 bus->type = pmac_i2c_bus_pmu;
803 bus->channel = channel;
804 bus->mode = pmac_i2c_mode_std;
805 bus->hostdata = bus + 1;
806 bus->xfer = pmu_i2c_xfer;
807 init_MUTEX(&bus->sem);
808 bus->flags = pmac_i2c_multibus;
809 list_add(&bus->link, &pmac_i2c_busses);
811 printk(KERN_INFO " channel %d bus <multibus>\n", channel);
815 #endif /* CONFIG_ADB_PMU */
824 #ifdef CONFIG_PMAC_SMU
826 static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)
831 static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
832 u32 subaddr, u8 *data, int len)
834 struct smu_i2c_cmd *cmd = bus->hostdata;
835 struct completion comp;
836 int read = addrdir & 1;
839 if ((read && len > SMU_I2C_READ_MAX) ||
840 ((!read) && len > SMU_I2C_WRITE_MAX))
843 memset(cmd, 0, sizeof(struct smu_i2c_cmd));
844 cmd->info.bus = bus->channel;
845 cmd->info.devaddr = addrdir;
846 cmd->info.datalen = len;
849 case pmac_i2c_mode_std:
852 cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;
854 case pmac_i2c_mode_stdsub:
855 case pmac_i2c_mode_combined:
856 if (subsize > 3 || subsize < 1)
858 cmd->info.sublen = subsize;
859 /* that's big-endian only but heh ! */
860 memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),
862 if (bus->mode == pmac_i2c_mode_stdsub)
863 cmd->info.type = SMU_I2C_TRANSFER_STDSUB;
865 cmd->info.type = SMU_I2C_TRANSFER_COMBINED;
871 memcpy(cmd->info.data, data, len);
873 init_completion(&comp);
874 cmd->done = smu_i2c_complete;
876 rc = smu_queue_i2c(cmd);
879 wait_for_completion(&comp);
883 memcpy(data, cmd->info.data, len);
884 return rc < 0 ? rc : 0;
887 static void __init smu_i2c_probe(void)
889 struct device_node *controller, *busnode;
890 struct pmac_i2c_bus *bus;
897 controller = of_find_node_by_name(NULL, "smu-i2c-control");
898 if (controller == NULL)
899 controller = of_find_node_by_name(NULL, "smu");
900 if (controller == NULL)
903 printk(KERN_INFO "SMU i2c %s\n", controller->full_name);
905 /* Look for childs, note that they might not be of the right
906 * type as older device trees mix i2c busses and other thigns
910 (busnode = of_get_next_child(controller, busnode)) != NULL;) {
911 if (strcmp(busnode->type, "i2c") &&
912 strcmp(busnode->type, "i2c-bus"))
914 reg = (u32 *)get_property(busnode, "reg", NULL);
918 sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);
919 bus = kzalloc(sz, GFP_KERNEL);
923 bus->controller = controller;
924 bus->busnode = of_node_get(busnode);
925 bus->type = pmac_i2c_bus_smu;
927 bus->mode = pmac_i2c_mode_std;
928 bus->hostdata = bus + 1;
929 bus->xfer = smu_i2c_xfer;
930 init_MUTEX(&bus->sem);
932 list_add(&bus->link, &pmac_i2c_busses);
934 printk(KERN_INFO " channel %x bus %s\n",
935 bus->channel, busnode->full_name);
939 #endif /* CONFIG_PMAC_SMU */
948 struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)
950 struct device_node *p = of_node_get(node);
951 struct device_node *prev = NULL;
952 struct pmac_i2c_bus *bus;
955 list_for_each_entry(bus, &pmac_i2c_busses, link) {
956 if (p == bus->busnode) {
957 if (prev && bus->flags & pmac_i2c_multibus) {
959 reg = (u32 *)get_property(prev, "reg",
963 if (((*reg) >> 8) != bus->channel)
973 p = of_get_parent(p);
977 EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);
979 u8 pmac_i2c_get_dev_addr(struct device_node *device)
981 u32 *reg = (u32 *)get_property(device, "reg", NULL);
986 return (*reg) & 0xff;
988 EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);
990 struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)
992 return bus->controller;
994 EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);
996 struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)
1000 EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);
1002 int pmac_i2c_get_type(struct pmac_i2c_bus *bus)
1006 EXPORT_SYMBOL_GPL(pmac_i2c_get_type);
1008 int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)
1012 EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);
1014 int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)
1016 return bus->channel;
1018 EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);
1021 void pmac_i2c_attach_adapter(struct pmac_i2c_bus *bus,
1022 struct i2c_adapter *adapter)
1024 WARN_ON(bus->adapter != NULL);
1025 bus->adapter = adapter;
1027 EXPORT_SYMBOL_GPL(pmac_i2c_attach_adapter);
1029 void pmac_i2c_detach_adapter(struct pmac_i2c_bus *bus,
1030 struct i2c_adapter *adapter)
1032 WARN_ON(bus->adapter != adapter);
1033 bus->adapter = NULL;
1035 EXPORT_SYMBOL_GPL(pmac_i2c_detach_adapter);
1037 struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)
1039 return bus->adapter;
1041 EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);
1043 struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)
1045 struct pmac_i2c_bus *bus;
1047 list_for_each_entry(bus, &pmac_i2c_busses, link)
1048 if (bus->adapter == adapter)
1052 EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);
1054 extern int pmac_i2c_match_adapter(struct device_node *dev,
1055 struct i2c_adapter *adapter)
1057 struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);
1061 return (bus->adapter == adapter);
1063 EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);
1065 int pmac_low_i2c_lock(struct device_node *np)
1067 struct pmac_i2c_bus *bus, *found = NULL;
1069 list_for_each_entry(bus, &pmac_i2c_busses, link) {
1070 if (np == bus->controller) {
1077 return pmac_i2c_open(bus, 0);
1079 EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);
1081 int pmac_low_i2c_unlock(struct device_node *np)
1083 struct pmac_i2c_bus *bus, *found = NULL;
1085 list_for_each_entry(bus, &pmac_i2c_busses, link) {
1086 if (np == bus->controller) {
1093 pmac_i2c_close(bus);
1096 EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);
1099 int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)
1104 bus->polled = polled || pmac_i2c_force_poll;
1106 bus->mode = pmac_i2c_mode_std;
1107 if (bus->open && (rc = bus->open(bus)) != 0) {
1114 EXPORT_SYMBOL_GPL(pmac_i2c_open);
1116 void pmac_i2c_close(struct pmac_i2c_bus *bus)
1118 WARN_ON(!bus->opened);
1124 EXPORT_SYMBOL_GPL(pmac_i2c_close);
1126 int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)
1128 WARN_ON(!bus->opened);
1130 /* Report me if you see the error below as there might be a new
1131 * "combined4" mode that I need to implement for the SMU bus
1133 if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {
1134 printk(KERN_ERR "low_i2c: Invalid mode %d requested on"
1135 " bus %s !\n", mode, bus->busnode->full_name);
1142 EXPORT_SYMBOL_GPL(pmac_i2c_setmode);
1144 int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,
1145 u32 subaddr, u8 *data, int len)
1149 WARN_ON(!bus->opened);
1151 DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"
1152 " %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,
1153 subaddr, len, bus->busnode->full_name);
1155 rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);
1159 DBG("xfer error %d\n", rc);
1163 EXPORT_SYMBOL_GPL(pmac_i2c_xfer);
1166 * Initialize us: probe all i2c busses on the machine and instantiate
1169 /* This is non-static as it might be called early by smp code */
1170 int __init pmac_i2c_init(void)
1172 static int i2c_inited;
1178 /* Probe keywest-i2c busses */
1181 #ifdef CONFIG_ADB_PMU
1182 /* Probe PMU i2c busses */
1186 #ifdef CONFIG_PMAC_SMU
1187 /* Probe SMU i2c busses */
1192 arch_initcall(pmac_i2c_init);
1194 /* Since pmac_i2c_init can be called too early for the platform device
1195 * registration, we need to do it at a later time. In our case, subsys
1196 * happens to fit well, though I agree it's a bit of a hack...
1198 static int __init pmac_i2c_create_platform_devices(void)
1200 struct pmac_i2c_bus *bus;
1203 /* In the case where we are initialized from smp_init(), we must
1204 * not use the timer (and thus the irq). It's safe from now on
1207 pmac_i2c_force_poll = 0;
1209 /* Create platform devices */
1210 list_for_each_entry(bus, &pmac_i2c_busses, link) {
1212 platform_device_alloc("i2c-powermac", i++);
1213 if (bus->platform_dev == NULL)
1215 bus->platform_dev->dev.platform_data = bus;
1216 platform_device_add(bus->platform_dev);
1221 subsys_initcall(pmac_i2c_create_platform_devices);