2 * drivers/i2c/busses/i2c-ibm_iic.c
4 * Support for the IIC peripheral on IBM PPC 4xx
6 * Copyright (c) 2003, 2004 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Copyright (c) 2008 PIKA Technologies
10 * Sean MacLennan <smaclennan@pikatech.com>
12 * Based on original work by
13 * Ian DaSilva <idasilva@mvista.com>
14 * Armin Kuster <akuster@mvista.com>
15 * Matt Porter <mporter@mvista.com>
17 * Copyright 2000-2003 MontaVista Software Inc.
19 * Original driver version was highly leveraged from i2c-elektor.c
21 * Copyright 1995-97 Simon G. Vogl
22 * 1998-99 Hans Berglund
24 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
25 * and even Frodo Looijaard <frodol@dds.nl>
27 * This program is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU General Public License as published by the
29 * Free Software Foundation; either version 2 of the License, or (at your
30 * option) any later version.
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/ioport.h>
37 #include <linux/delay.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-id.h>
48 #include <asm/ibm4xx.h>
50 #include <linux/of_platform.h>
53 #include "i2c-ibm_iic.h"
55 #define DRIVER_VERSION "2.2"
57 MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
58 MODULE_LICENSE("GPL");
60 static int iic_force_poll;
61 module_param(iic_force_poll, bool, 0);
62 MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
64 static int iic_force_fast;
65 module_param(iic_force_fast, bool, 0);
66 MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
79 # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
81 # define DBG(f,x...) ((void)0)
84 # define DBG2(f,x...) DBG(f, ##x)
86 # define DBG2(f,x...) ((void)0)
89 static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
91 volatile struct iic_regs __iomem *iic = dev->vaddr;
92 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
93 printk(KERN_DEBUG " cntl = 0x%02x, mdcntl = 0x%02x\n"
94 KERN_DEBUG " sts = 0x%02x, extsts = 0x%02x\n"
95 KERN_DEBUG " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
96 KERN_DEBUG " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
97 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
98 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
99 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
101 # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
103 # define DUMP_REGS(h,dev) ((void)0)
106 /* Bus timings (in ns) for bit-banging */
107 static struct i2c_timings {
114 /* Standard mode (100 KHz) */
122 /* Fast mode (400 KHz) */
131 /* Enable/disable interrupt generation */
132 static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
134 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
138 * Initialize IIC interface.
140 static void iic_dev_init(struct ibm_iic_private* dev)
142 volatile struct iic_regs __iomem *iic = dev->vaddr;
144 DBG("%d: init\n", dev->idx);
146 /* Clear master address */
147 out_8(&iic->lmadr, 0);
148 out_8(&iic->hmadr, 0);
150 /* Clear slave address */
151 out_8(&iic->lsadr, 0);
152 out_8(&iic->hsadr, 0);
154 /* Clear status & extended status */
155 out_8(&iic->sts, STS_SCMP | STS_IRQA);
156 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
157 | EXTSTS_ICT | EXTSTS_XFRA);
159 /* Set clock divider */
160 out_8(&iic->clkdiv, dev->clckdiv);
162 /* Clear transfer count */
163 out_8(&iic->xfrcnt, 0);
165 /* Clear extended control and status */
166 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
169 /* Clear control register */
170 out_8(&iic->cntl, 0);
172 /* Enable interrupts if possible */
173 iic_interrupt_mode(dev, dev->irq >= 0);
175 /* Set mode control */
176 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
177 | (dev->fast_mode ? MDCNTL_FSM : 0));
179 DUMP_REGS("iic_init", dev);
183 * Reset IIC interface
185 static void iic_dev_reset(struct ibm_iic_private* dev)
187 volatile struct iic_regs __iomem *iic = dev->vaddr;
191 DBG("%d: soft reset\n", dev->idx);
192 DUMP_REGS("reset", dev);
194 /* Place chip in the reset state */
195 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
197 /* Check if bus is free */
198 dc = in_8(&iic->directcntl);
199 if (!DIRCTNL_FREE(dc)){
200 DBG("%d: trying to regain bus control\n", dev->idx);
202 /* Try to set bus free state */
203 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
205 /* Wait until we regain bus control */
206 for (i = 0; i < 100; ++i){
207 dc = in_8(&iic->directcntl);
208 if (DIRCTNL_FREE(dc))
211 /* Toggle SCL line */
213 out_8(&iic->directcntl, dc);
216 out_8(&iic->directcntl, dc);
224 out_8(&iic->xtcntlss, 0);
226 /* Reinitialize interface */
231 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
234 /* Wait for SCL and/or SDA to be high */
235 static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
237 unsigned long x = jiffies + HZ / 28 + 2;
238 while ((in_8(&iic->directcntl) & mask) != mask){
239 if (unlikely(time_after(jiffies, x)))
246 static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
248 volatile struct iic_regs __iomem *iic = dev->vaddr;
249 const struct i2c_timings* t = &timings[dev->fast_mode ? 1 : 0];
253 /* Only 7-bit addresses are supported */
254 if (unlikely(p->flags & I2C_M_TEN)){
255 DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
260 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
262 /* Reset IIC interface */
263 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
265 /* Wait for bus to become free */
266 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
267 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
272 out_8(&iic->directcntl, DIRCNTL_SCC);
277 v = (u8)((p->addr << 1) | ((p->flags & I2C_M_RD) ? 1 : 0));
278 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
279 out_8(&iic->directcntl, sda);
281 sda = (v & mask) ? DIRCNTL_SDAC : 0;
282 out_8(&iic->directcntl, sda);
285 out_8(&iic->directcntl, DIRCNTL_SCC | sda);
286 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
292 out_8(&iic->directcntl, sda);
294 out_8(&iic->directcntl, DIRCNTL_SDAC);
296 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
297 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
299 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
303 out_8(&iic->directcntl, 0);
305 out_8(&iic->directcntl, DIRCNTL_SCC);
306 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
309 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
313 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
316 out_8(&iic->xtcntlss, 0);
318 /* Reinitialize interface */
323 DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
329 * IIC interrupt handler
331 static irqreturn_t iic_handler(int irq, void *dev_id)
333 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
334 volatile struct iic_regs __iomem *iic = dev->vaddr;
336 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
337 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
339 /* Acknowledge IRQ and wakeup iic_wait_for_tc */
340 out_8(&iic->sts, STS_IRQA | STS_SCMP);
341 wake_up_interruptible(&dev->wq);
347 * Get master transfer result and clear errors if any.
348 * Returns the number of actually transferred bytes or error (<0)
350 static int iic_xfer_result(struct ibm_iic_private* dev)
352 volatile struct iic_regs __iomem *iic = dev->vaddr;
354 if (unlikely(in_8(&iic->sts) & STS_ERR)){
355 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
358 /* Clear errors and possible pending IRQs */
359 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
360 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
362 /* Flush master data buffer */
363 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
366 * If error happened during combined xfer
367 * IIC interface is usually stuck in some strange
368 * state, the only way out - soft reset.
370 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
371 DBG("%d: bus is stuck, resetting\n", dev->idx);
377 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
381 * Try to abort active transfer.
383 static void iic_abort_xfer(struct ibm_iic_private* dev)
385 volatile struct iic_regs __iomem *iic = dev->vaddr;
388 DBG("%d: iic_abort_xfer\n", dev->idx);
390 out_8(&iic->cntl, CNTL_HMT);
393 * Wait for the abort command to complete.
394 * It's not worth to be optimized, just poll (timeout >= 1 tick)
397 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
398 if (time_after(jiffies, x)){
399 DBG("%d: abort timeout, resetting...\n", dev->idx);
406 /* Just to clear errors */
407 iic_xfer_result(dev);
411 * Wait for master transfer to complete.
412 * It puts current process to sleep until we get interrupt or timeout expires.
413 * Returns the number of transferred bytes or error (<0)
415 static int iic_wait_for_tc(struct ibm_iic_private* dev){
417 volatile struct iic_regs __iomem *iic = dev->vaddr;
422 ret = wait_event_interruptible_timeout(dev->wq,
423 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout * HZ);
425 if (unlikely(ret < 0))
426 DBG("%d: wait interrupted\n", dev->idx);
427 else if (unlikely(in_8(&iic->sts) & STS_PT)){
428 DBG("%d: wait timeout\n", dev->idx);
434 unsigned long x = jiffies + dev->adap.timeout * HZ;
436 while (in_8(&iic->sts) & STS_PT){
437 if (unlikely(time_after(jiffies, x))){
438 DBG("%d: poll timeout\n", dev->idx);
443 if (unlikely(signal_pending(current))){
444 DBG("%d: poll interrupted\n", dev->idx);
452 if (unlikely(ret < 0))
455 ret = iic_xfer_result(dev);
457 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
463 * Low level master transfer routine
465 static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
468 volatile struct iic_regs __iomem *iic = dev->vaddr;
470 int i, j, loops, ret = 0;
473 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
474 if (pm->flags & I2C_M_RD)
477 loops = (len + 3) / 4;
478 for (i = 0; i < loops; ++i, len -= 4){
479 int count = len > 4 ? 4 : len;
480 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
482 if (!(cntl & CNTL_RW))
483 for (j = 0; j < count; ++j)
484 out_8((void __iomem *)&iic->mdbuf, *buf++);
488 else if (combined_xfer)
491 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
494 out_8(&iic->cntl, cmd);
496 /* Wait for completion */
497 ret = iic_wait_for_tc(dev);
499 if (unlikely(ret < 0))
501 else if (unlikely(ret != count)){
502 DBG("%d: xfer_bytes, requested %d, transfered %d\n",
503 dev->idx, count, ret);
505 /* If it's not a last part of xfer, abort it */
506 if (combined_xfer || (i < loops - 1))
514 for (j = 0; j < count; ++j)
515 *buf++ = in_8((void __iomem *)&iic->mdbuf);
518 return ret > 0 ? 0 : ret;
522 * Set target slave address for master transfer
524 static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
526 volatile struct iic_regs __iomem *iic = dev->vaddr;
527 u16 addr = msg->addr;
529 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
530 addr, msg->flags & I2C_M_TEN ? 10 : 7);
532 if (msg->flags & I2C_M_TEN){
533 out_8(&iic->cntl, CNTL_AMD);
534 out_8(&iic->lmadr, addr);
535 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
538 out_8(&iic->cntl, 0);
539 out_8(&iic->lmadr, addr << 1);
543 static inline int iic_invalid_address(const struct i2c_msg* p)
545 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
548 static inline int iic_address_neq(const struct i2c_msg* p1,
549 const struct i2c_msg* p2)
551 return (p1->addr != p2->addr)
552 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
556 * Generic master transfer entrypoint.
557 * Returns the number of processed messages or error (<0)
559 static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
561 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
562 volatile struct iic_regs __iomem *iic = dev->vaddr;
565 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
570 /* Check the sanity of the passed messages.
571 * Uhh, generic i2c layer is more suitable place for such code...
573 if (unlikely(iic_invalid_address(&msgs[0]))){
574 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
575 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
578 for (i = 0; i < num; ++i){
579 if (unlikely(msgs[i].len <= 0)){
580 if (num == 1 && !msgs[0].len){
581 /* Special case for I2C_SMBUS_QUICK emulation.
582 * IBM IIC doesn't support 0-length transactions
583 * so we have to emulate them using bit-banging.
585 return iic_smbus_quick(dev, &msgs[0]);
587 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
591 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
592 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
597 /* Check bus state */
598 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
599 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
601 /* Usually it means something serious has happend.
602 * We *cannot* have unfinished previous transfer
603 * so it doesn't make any sense to try to stop it.
604 * Probably we were not able to recover from the
606 * The only *reasonable* thing I can think of here
607 * is soft reset. --ebs
611 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
612 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
617 /* Flush master data buffer (just in case) */
618 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
621 /* Load slave address */
622 iic_address(dev, &msgs[0]);
624 /* Do real transfer */
625 for (i = 0; i < num && !ret; ++i)
626 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
628 return ret < 0 ? ret : num;
631 static u32 iic_func(struct i2c_adapter *adap)
633 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
636 static const struct i2c_algorithm iic_algo = {
637 .master_xfer = iic_xfer,
638 .functionality = iic_func
642 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
644 static inline u8 iic_clckdiv(unsigned int opb)
646 /* Compatibility kludge, should go away after all cards
647 * are fixed to fill correct value for opbfreq.
648 * Previous driver version used hardcoded divider value 4,
649 * it corresponds to OPB frequency from the range (40, 50] MHz
652 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
653 " fix your board specific setup\n");
660 if (opb < 20 || opb > 150){
661 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
663 opb = opb < 20 ? 20 : 150;
665 return (u8)((opb + 9) / 10 - 1);
668 #ifdef CONFIG_IBM_OCP
670 * Register single IIC interface
672 static int __devinit iic_probe(struct ocp_device *ocp){
674 struct ibm_iic_private* dev;
675 struct i2c_adapter* adap;
676 struct ocp_func_iic_data* iic_data = ocp->def->additions;
680 printk(KERN_WARNING"ibm-iic%d: missing additional data!\n",
683 if (!(dev = kzalloc(sizeof(*dev), GFP_KERNEL))) {
684 printk(KERN_ERR "ibm-iic%d: failed to allocate device data\n",
689 dev->idx = ocp->def->index;
690 ocp_set_drvdata(ocp, dev);
692 if (!request_mem_region(ocp->def->paddr, sizeof(struct iic_regs),
698 if (!(dev->vaddr = ioremap(ocp->def->paddr, sizeof(struct iic_regs)))){
699 printk(KERN_ERR "ibm-iic%d: failed to ioremap device registers\n",
705 init_waitqueue_head(&dev->wq);
707 dev->irq = iic_force_poll ? -1 : ocp->def->irq;
709 /* Disable interrupts until we finish initialization,
710 assumes level-sensitive IRQ setup...
712 iic_interrupt_mode(dev, 0);
713 if (request_irq(dev->irq, iic_handler, 0, "IBM IIC", dev)){
714 printk(KERN_ERR "ibm-iic%d: request_irq %d failed\n",
716 /* Fallback to the polling mode */
722 printk(KERN_WARNING "ibm-iic%d: using polling mode\n",
725 /* Board specific settings */
726 dev->fast_mode = iic_force_fast ? 1 : (iic_data ? iic_data->fast_mode : 0);
728 /* clckdiv is the same for *all* IIC interfaces,
729 * but I'd rather make a copy than introduce another global. --ebs
731 dev->clckdiv = iic_clckdiv(ocp_sys_info.opb_bus_freq);
732 DBG("%d: clckdiv = %d\n", dev->idx, dev->clckdiv);
734 /* Initialize IIC interface */
737 /* Register it with i2c layer */
739 adap->dev.parent = &ocp->dev;
740 strcpy(adap->name, "IBM IIC");
741 i2c_set_adapdata(adap, dev);
742 adap->id = I2C_HW_OCP;
743 adap->class = I2C_CLASS_HWMON;
744 adap->algo = &iic_algo;
745 adap->client_register = NULL;
746 adap->client_unregister = NULL;
750 * If "dev->idx" is negative we consider it as zero.
751 * The reason to do so is to avoid sysfs names that only make
752 * sense when there are multiple adapters.
754 adap->nr = dev->idx >= 0 ? dev->idx : 0;
756 if ((ret = i2c_add_numbered_adapter(adap)) < 0) {
757 printk(KERN_ERR "ibm-iic%d: failed to register i2c adapter\n",
762 printk(KERN_INFO "ibm-iic%d: using %s mode\n", dev->idx,
763 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
769 iic_interrupt_mode(dev, 0);
770 free_irq(dev->irq, dev);
775 release_mem_region(ocp->def->paddr, sizeof(struct iic_regs));
777 ocp_set_drvdata(ocp, NULL);
783 * Cleanup initialized IIC interface
785 static void __devexit iic_remove(struct ocp_device *ocp)
787 struct ibm_iic_private* dev = (struct ibm_iic_private*)ocp_get_drvdata(ocp);
789 if (i2c_del_adapter(&dev->adap)){
790 printk(KERN_ERR "ibm-iic%d: failed to delete i2c adapter :(\n",
792 /* That's *very* bad, just shutdown IRQ ... */
794 iic_interrupt_mode(dev, 0);
795 free_irq(dev->irq, dev);
800 iic_interrupt_mode(dev, 0);
801 free_irq(dev->irq, dev);
804 release_mem_region(ocp->def->paddr, sizeof(struct iic_regs));
809 static struct ocp_device_id ibm_iic_ids[] __devinitdata =
811 { .vendor = OCP_VENDOR_IBM, .function = OCP_FUNC_IIC },
812 { .vendor = OCP_VENDOR_INVALID }
815 MODULE_DEVICE_TABLE(ocp, ibm_iic_ids);
817 static struct ocp_driver ibm_iic_driver =
820 .id_table = ibm_iic_ids,
822 .remove = __devexit_p(iic_remove),
823 #if defined(CONFIG_PM)
829 static int __init iic_init(void)
831 printk(KERN_INFO "IBM IIC driver v" DRIVER_VERSION "\n");
832 return ocp_register_driver(&ibm_iic_driver);
835 static void __exit iic_exit(void)
837 ocp_unregister_driver(&ibm_iic_driver);
840 #else /* !CONFIG_IBM_OCP */
842 static int __devinit iic_request_irq(struct of_device *ofdev,
843 struct ibm_iic_private *dev)
845 struct device_node *np = ofdev->node;
851 irq = irq_of_parse_and_map(np, 0);
853 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
857 /* Disable interrupts until we finish initialization, assumes
858 * level-sensitive IRQ setup...
860 iic_interrupt_mode(dev, 0);
861 if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
862 dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
863 /* Fallback to the polling mode */
871 * Register single IIC interface
873 static int __devinit iic_probe(struct of_device *ofdev,
874 const struct of_device_id *match)
876 struct device_node *np = ofdev->node;
877 struct ibm_iic_private *dev;
878 struct i2c_adapter *adap;
879 const u32 *indexp, *freq;
882 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
884 dev_err(&ofdev->dev, "failed to allocate device data\n");
888 dev_set_drvdata(&ofdev->dev, dev);
890 indexp = of_get_property(np, "index", NULL);
892 dev_err(&ofdev->dev, "no index specified\n");
898 dev->vaddr = of_iomap(np, 0);
899 if (dev->vaddr == NULL) {
900 dev_err(&ofdev->dev, "failed to iomap device\n");
905 init_waitqueue_head(&dev->wq);
907 dev->irq = iic_request_irq(ofdev, dev);
908 if (dev->irq == NO_IRQ)
909 dev_warn(&ofdev->dev, "using polling mode\n");
911 /* Board specific settings */
912 if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
915 freq = of_get_property(np, "clock-frequency", NULL);
917 freq = of_get_property(np->parent, "clock-frequency", NULL);
919 dev_err(&ofdev->dev, "Unable to get bus frequency\n");
925 dev->clckdiv = iic_clckdiv(*freq);
926 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
928 /* Initialize IIC interface */
931 /* Register it with i2c layer */
933 adap->dev.parent = &ofdev->dev;
934 strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
935 i2c_set_adapdata(adap, dev);
936 adap->id = I2C_HW_OCP;
937 adap->class = I2C_CLASS_HWMON;
938 adap->algo = &iic_algo;
942 ret = i2c_add_numbered_adapter(adap);
944 dev_err(&ofdev->dev, "failed to register i2c adapter\n");
948 dev_info(&ofdev->dev, "using %s mode\n",
949 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
954 if (dev->irq != NO_IRQ) {
955 iic_interrupt_mode(dev, 0);
956 free_irq(dev->irq, dev);
962 dev_set_drvdata(&ofdev->dev, NULL);
968 * Cleanup initialized IIC interface
970 static int __devexit iic_remove(struct of_device *ofdev)
972 struct ibm_iic_private *dev = dev_get_drvdata(&ofdev->dev);
974 dev_set_drvdata(&ofdev->dev, NULL);
976 i2c_del_adapter(&dev->adap);
978 if (dev->irq != NO_IRQ) {
979 iic_interrupt_mode(dev, 0);
980 free_irq(dev->irq, dev);
989 static const struct of_device_id ibm_iic_match[] = {
990 { .compatible = "ibm,iic-405ex", },
991 { .compatible = "ibm,iic-405gp", },
992 { .compatible = "ibm,iic-440gp", },
993 { .compatible = "ibm,iic-440gpx", },
994 { .compatible = "ibm,iic-440grx", },
998 static struct of_platform_driver ibm_iic_driver = {
1000 .match_table = ibm_iic_match,
1002 .remove = __devexit_p(iic_remove),
1005 static int __init iic_init(void)
1007 return of_register_platform_driver(&ibm_iic_driver);
1010 static void __exit iic_exit(void)
1012 of_unregister_platform_driver(&ibm_iic_driver);
1014 #endif /* CONFIG_IBM_OCP */
1016 module_init(iic_init);
1017 module_exit(iic_exit);