34 #include <linux/module.h>
35 #include <linux/kernel.h>
38 #include <linux/slab.h>
43 #include <linux/i2c.h>
49 #define DRIVER_VERSION "2.2"
54 static bool iic_force_poll;
58 static bool iic_force_fast;
73 # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
75 # define DBG(f,x...) ((void)0)
78 # define DBG2(f,x...) DBG(f, ##x)
80 # define DBG2(f,x...) ((void)0)
88 " cntl = 0x%02x, mdcntl = 0x%02x\n"
89 " sts = 0x%02x, extsts = 0x%02x\n"
90 " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
91 " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
96 # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
98 # define DUMP_REGS(h,dev) ((void)0)
102 static struct i2c_timings {
139 DBG(
"%d: init\n", dev->
idx);
168 iic_interrupt_mode(dev, dev->
irq >= 0);
186 DBG(
"%d: soft reset\n", dev->
idx);
195 DBG(
"%d: trying to regain bus control\n", dev->
idx);
201 for (i = 0; i < 100; ++
i){
233 while ((
in_8(&iic->directcntl) & mask) != mask){
244 const struct i2c_timings*
t = &timings[dev->
fast_mode ? 1 : 0];
250 DBG(
"%d: smbus_quick - 10 bit addresses are not supported\n",
255 DBG(
"%d: smbus_quick(0x%02x)\n", dev->
idx, p->
addr);
273 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
308 DBG(
"%d: smbus_quick -> %s\n", dev->
idx, res ?
"NACK" :
"ACK");
318 DBG(
"%d: smbus_quick - bus is stuck\n", dev->
idx);
331 DBG2(
"%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
350 DBG(
"%d: xfer error, EXTSTS = 0x%02x\n", dev->
idx,
366 DBG(
"%d: bus is stuck, resetting\n", dev->
idx);
383 DBG(
"%d: iic_abort_xfer\n", dev->
idx);
394 DBG(
"%d: abort timeout, resetting...\n", dev->
idx);
402 iic_xfer_result(dev);
421 DBG(
"%d: wait interrupted\n", dev->
idx);
423 DBG(
"%d: wait timeout\n", dev->
idx);
433 DBG(
"%d: poll timeout\n", dev->
idx);
439 DBG(
"%d: poll interrupted\n", dev->
idx);
450 ret = iic_xfer_result(dev);
452 DBG2(
"%d: iic_wait_for_tc -> %d\n", dev->
idx, ret);
465 int i,
j, loops, ret = 0;
472 loops = (len + 3) / 4;
473 for (i = 0; i < loops; ++
i, len -= 4){
474 int count = len > 4 ? 4 : len;
478 for (j = 0; j <
count; ++
j)
483 else if (combined_xfer)
486 DBG2(
"%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->
idx, count, cmd);
492 ret = iic_wait_for_tc(dev);
497 DBG(
"%d: xfer_bytes, requested %d, transferred %d\n",
498 dev->
idx, count, ret);
501 if (combined_xfer || (i < loops - 1))
509 for (j = 0; j <
count; ++
j)
513 return ret > 0 ? 0 :
ret;
524 DBG2(
"%d: iic_address, 0x%03x (%d-bit)\n", dev->
idx,
530 out_8(&iic->
hmadr, 0xf0 | ((addr >> 7) & 0x06));
538 static inline int iic_invalid_address(
const struct i2c_msg* p)
543 static inline int iic_address_neq(
const struct i2c_msg*
p1,
560 DBG2(
"%d: iic_xfer, %d msg(s)\n", dev->
idx, num);
568 if (
unlikely(iic_invalid_address(&msgs[0]))){
569 DBG(
"%d: invalid address 0x%03x (%d-bit)\n", dev->
idx,
573 for (i = 0; i < num; ++
i){
575 if (num == 1 && !msgs[0].len){
580 return iic_smbus_quick(dev, &msgs[0]);
582 DBG(
"%d: invalid len %d in msg[%d]\n", dev->
idx,
586 if (
unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
587 DBG(
"%d: invalid addr in msg[%d]\n", dev->
idx, i);
594 DBG(
"%d: iic_xfer, bus is not free\n", dev->
idx);
607 DBG(
"%d: iic_xfer, bus is still not free\n", dev->
idx);
617 iic_address(dev, &msgs[0]);
620 for (i = 0; i < num && !
ret; ++
i)
621 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
623 return ret < 0 ? ret : num;
632 .master_xfer = iic_xfer,
633 .functionality = iic_func
639 static inline u8 iic_clckdiv(
unsigned int opb)
648 " fix your board specific setup\n");
655 if (opb < 20 || opb > 150){
658 opb = opb < 20 ? 20 : 150;
660 return (
u8)((opb + 9) / 10 - 1);
674 dev_err(&ofdev->
dev,
"irq_of_parse_and_map failed\n");
681 iic_interrupt_mode(dev, 0);
682 if (
request_irq(irq, iic_handler, 0,
"IBM IIC", dev)) {
683 dev_err(&ofdev->
dev,
"request_irq %d failed\n", irq);
704 dev_err(&ofdev->
dev,
"failed to allocate device data\n");
712 dev_err(&ofdev->
dev,
"failed to iomap device\n");
719 dev->
irq = iic_request_irq(ofdev, dev);
731 dev_err(&ofdev->
dev,
"Unable to get bus frequency\n");
737 dev->
clckdiv = iic_clckdiv(*freq);
745 adap->
dev.parent = &ofdev->
dev;
746 adap->
dev.of_node = of_node_get(np);
748 i2c_set_adapdata(adap, dev);
750 adap->
algo = &iic_algo;
755 dev_err(&ofdev->
dev,
"failed to register i2c adapter\n");
760 dev->
fast_mode ?
"fast (400 kHz)" :
"standard (100 kHz)");
769 iic_interrupt_mode(dev, 0);
793 iic_interrupt_mode(dev, 0);
804 { .compatible =
"ibm,iic", },
812 .of_match_table = ibm_iic_match,