Lines Matching full:i2c
3 * This is a combined i2c adapter and algorithm driver for the
5 * the same I2C unit (8240, 8245, 85xx).
24 #include <linux/i2c.h>
32 #define DRV_NAME "mpc-i2c"
111 void (*setup)(struct device_node *node, struct mpc_i2c *i2c, u32 clock);
114 static inline void writeccr(struct mpc_i2c *i2c, u32 x) in writeccr() argument
116 writeb(x, i2c->base + MPC_I2C_CR); in writeccr()
124 static void mpc_i2c_fixup(struct mpc_i2c *i2c) in mpc_i2c_fixup() argument
130 writeccr(i2c, 0); in mpc_i2c_fixup()
131 writeb(0, i2c->base + MPC_I2C_SR); /* clear any status bits */ in mpc_i2c_fixup()
132 writeccr(i2c, CCR_MEN | CCR_MSTA); /* START */ in mpc_i2c_fixup()
133 readb(i2c->base + MPC_I2C_DR); /* init xfer */ in mpc_i2c_fixup()
136 writeccr(i2c, CCR_MEN | CCR_MSTA | CCR_RSTA); /* delay SDA */ in mpc_i2c_fixup()
137 readb(i2c->base + MPC_I2C_DR); in mpc_i2c_fixup()
142 writeccr(i2c, CCR_MEN); /* Initiate STOP */ in mpc_i2c_fixup()
143 readb(i2c->base + MPC_I2C_DR); in mpc_i2c_fixup()
145 writeccr(i2c, 0); in mpc_i2c_fixup()
148 static int i2c_mpc_wait_sr(struct mpc_i2c *i2c, int mask) in i2c_mpc_wait_sr() argument
150 void __iomem *addr = i2c->base + MPC_I2C_SR; in i2c_mpc_wait_sr()
176 static void mpc_i2c_fixup_A004447(struct mpc_i2c *i2c) in mpc_i2c_fixup_A004447() argument
181 writeccr(i2c, CCR_MEN | CCR_MSTA); in mpc_i2c_fixup_A004447()
182 ret = i2c_mpc_wait_sr(i2c, CSR_MBB); in mpc_i2c_fixup_A004447()
184 dev_err(i2c->dev, "timeout waiting for CSR_MBB\n"); in mpc_i2c_fixup_A004447()
188 val = readb(i2c->base + MPC_I2C_SR); in mpc_i2c_fixup_A004447()
191 writeccr(i2c, 0x00); in mpc_i2c_fixup_A004447()
192 writeccr(i2c, CCR_MSTA | CCR_RSVD); in mpc_i2c_fixup_A004447()
193 writeccr(i2c, CCR_MEN | CCR_MSTA | CCR_RSVD); in mpc_i2c_fixup_A004447()
194 ret = i2c_mpc_wait_sr(i2c, CSR_MBB); in mpc_i2c_fixup_A004447()
196 dev_err(i2c->dev, "timeout waiting for CSR_MBB\n"); in mpc_i2c_fixup_A004447()
199 val = readb(i2c->base + MPC_I2C_DR); in mpc_i2c_fixup_A004447()
200 ret = i2c_mpc_wait_sr(i2c, CSR_MIF); in mpc_i2c_fixup_A004447()
202 dev_err(i2c->dev, "timeout waiting for CSR_MIF\n"); in mpc_i2c_fixup_A004447()
205 writeccr(i2c, CCR_MEN | CCR_RSVD); in mpc_i2c_fixup_A004447()
207 val = readb(i2c->base + MPC_I2C_DR); in mpc_i2c_fixup_A004447()
208 ret = i2c_mpc_wait_sr(i2c, CSR_MIF); in mpc_i2c_fixup_A004447()
210 dev_err(i2c->dev, "timeout waiting for CSR_MIF\n"); in mpc_i2c_fixup_A004447()
213 writeccr(i2c, CCR_MEN); in mpc_i2c_fixup_A004447()
258 * We want to choose an FDR/DFSR that generates an I2C bus speed that in mpc_i2c_get_fdr_52xx()
275 struct mpc_i2c *i2c, in mpc_i2c_setup_52xx() argument
281 dev_dbg(i2c->dev, "using fdr %d\n", in mpc_i2c_setup_52xx()
282 readb(i2c->base + MPC_I2C_FDR)); in mpc_i2c_setup_52xx()
286 ret = mpc_i2c_get_fdr_52xx(node, clock, &i2c->real_clk); in mpc_i2c_setup_52xx()
289 writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); in mpc_i2c_setup_52xx()
292 dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk, in mpc_i2c_setup_52xx()
297 struct mpc_i2c *i2c, in mpc_i2c_setup_52xx() argument
305 struct mpc_i2c *i2c, in mpc_i2c_setup_512x() argument
313 /* Enable I2C interrupts for mpc5121 */ in mpc_i2c_setup_512x()
315 "fsl,mpc5121-i2c-ctrl"); in mpc_i2c_setup_512x()
319 /* Interrupt enable bits for i2c-0/1/2: bit 24/26/28 */ in mpc_i2c_setup_512x()
329 mpc_i2c_setup_52xx(node, i2c, clock); in mpc_i2c_setup_512x()
333 struct mpc_i2c *i2c, in mpc_i2c_setup_512x() argument
443 pr_debug("I2C: src_clock=%d clock=%d divider=%d\n", in mpc_i2c_get_fdr_8xxx()
447 * We want to choose an FDR/DFSR that generates an I2C bus speed that in mpc_i2c_get_fdr_8xxx()
461 struct mpc_i2c *i2c, in mpc_i2c_setup_8xxx() argument
467 dev_dbg(i2c->dev, "using dfsrr %d, fdr %d\n", in mpc_i2c_setup_8xxx()
468 readb(i2c->base + MPC_I2C_DFSRR), in mpc_i2c_setup_8xxx()
469 readb(i2c->base + MPC_I2C_FDR)); in mpc_i2c_setup_8xxx()
473 ret = mpc_i2c_get_fdr_8xxx(node, clock, &i2c->real_clk); in mpc_i2c_setup_8xxx()
476 writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR); in mpc_i2c_setup_8xxx()
477 writeb((fdr >> 8) & 0xff, i2c->base + MPC_I2C_DFSRR); in mpc_i2c_setup_8xxx()
480 dev_info(i2c->dev, "clock %d Hz (dfsrr=%d fdr=%d)\n", in mpc_i2c_setup_8xxx()
481 i2c->real_clk, fdr >> 8, fdr & 0xff); in mpc_i2c_setup_8xxx()
486 struct mpc_i2c *i2c, in mpc_i2c_setup_8xxx() argument
492 static void mpc_i2c_finish(struct mpc_i2c *i2c, int rc) in mpc_i2c_finish() argument
494 i2c->rc = rc; in mpc_i2c_finish()
495 i2c->block = 0; in mpc_i2c_finish()
496 i2c->cntl_bits = CCR_MEN; in mpc_i2c_finish()
497 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_finish()
498 wake_up(&i2c->waitq); in mpc_i2c_finish()
501 static void mpc_i2c_do_action(struct mpc_i2c *i2c) in mpc_i2c_do_action() argument
508 dev_dbg(i2c->dev, "action = %s\n", action_str[i2c->action]); in mpc_i2c_do_action()
510 i2c->cntl_bits &= ~(CCR_RSTA | CCR_MTX | CCR_TXAK); in mpc_i2c_do_action()
512 if (i2c->action != MPC_I2C_ACTION_STOP) { in mpc_i2c_do_action()
513 msg = &i2c->msgs[i2c->curr_msg]; in mpc_i2c_do_action()
520 switch (i2c->action) { in mpc_i2c_do_action()
522 i2c->cntl_bits |= CCR_RSTA; in mpc_i2c_do_action()
526 i2c->cntl_bits |= CCR_MSTA | CCR_MTX; in mpc_i2c_do_action()
527 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_do_action()
528 writeb((msg->addr << 1) | dir, i2c->base + MPC_I2C_DR); in mpc_i2c_do_action()
529 i2c->expect_rxack = 1; in mpc_i2c_do_action()
530 i2c->action = dir ? MPC_I2C_ACTION_READ_BEGIN : MPC_I2C_ACTION_WRITE; in mpc_i2c_do_action()
536 i2c->cntl_bits |= CCR_TXAK; in mpc_i2c_do_action()
538 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_do_action()
540 readb(i2c->base + MPC_I2C_DR); in mpc_i2c_do_action()
542 i2c->action = MPC_I2C_ACTION_READ_BYTE; in mpc_i2c_do_action()
546 if (i2c->byte_posn || !recv_len) { in mpc_i2c_do_action()
548 if (i2c->byte_posn == msg->len - 2) in mpc_i2c_do_action()
549 i2c->cntl_bits |= CCR_TXAK; in mpc_i2c_do_action()
551 if (i2c->byte_posn == msg->len - 1) in mpc_i2c_do_action()
552 i2c->cntl_bits |= CCR_MTX; in mpc_i2c_do_action()
554 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_do_action()
557 byte = readb(i2c->base + MPC_I2C_DR); in mpc_i2c_do_action()
559 if (i2c->byte_posn == 0 && recv_len) { in mpc_i2c_do_action()
561 mpc_i2c_finish(i2c, -EPROTO); in mpc_i2c_do_action()
570 i2c->cntl_bits |= CCR_TXAK; in mpc_i2c_do_action()
571 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_do_action()
575 dev_dbg(i2c->dev, "%s %02x\n", action_str[i2c->action], byte); in mpc_i2c_do_action()
576 msg->buf[i2c->byte_posn++] = byte; in mpc_i2c_do_action()
580 dev_dbg(i2c->dev, "%s %02x\n", action_str[i2c->action], in mpc_i2c_do_action()
581 msg->buf[i2c->byte_posn]); in mpc_i2c_do_action()
582 writeb(msg->buf[i2c->byte_posn++], i2c->base + MPC_I2C_DR); in mpc_i2c_do_action()
583 i2c->expect_rxack = 1; in mpc_i2c_do_action()
587 mpc_i2c_finish(i2c, 0); in mpc_i2c_do_action()
591 WARN(1, "Unexpected action %d\n", i2c->action); in mpc_i2c_do_action()
595 if (msg && msg->len == i2c->byte_posn) { in mpc_i2c_do_action()
596 i2c->curr_msg++; in mpc_i2c_do_action()
597 i2c->byte_posn = 0; in mpc_i2c_do_action()
599 if (i2c->curr_msg == i2c->num_msgs) { in mpc_i2c_do_action()
600 i2c->action = MPC_I2C_ACTION_STOP; in mpc_i2c_do_action()
606 mpc_i2c_finish(i2c, 0); in mpc_i2c_do_action()
608 i2c->action = MPC_I2C_ACTION_RESTART; in mpc_i2c_do_action()
613 static void mpc_i2c_do_intr(struct mpc_i2c *i2c, u8 status) in mpc_i2c_do_intr() argument
615 spin_lock(&i2c->lock); in mpc_i2c_do_intr()
618 dev_dbg(i2c->dev, "unfinished\n"); in mpc_i2c_do_intr()
619 mpc_i2c_finish(i2c, -EIO); in mpc_i2c_do_intr()
624 dev_dbg(i2c->dev, "arbitration lost\n"); in mpc_i2c_do_intr()
625 mpc_i2c_finish(i2c, -EAGAIN); in mpc_i2c_do_intr()
629 if (i2c->expect_rxack && (status & CSR_RXAK)) { in mpc_i2c_do_intr()
630 dev_dbg(i2c->dev, "no Rx ACK\n"); in mpc_i2c_do_intr()
631 mpc_i2c_finish(i2c, -ENXIO); in mpc_i2c_do_intr()
634 i2c->expect_rxack = 0; in mpc_i2c_do_intr()
636 mpc_i2c_do_action(i2c); in mpc_i2c_do_intr()
639 spin_unlock(&i2c->lock); in mpc_i2c_do_intr()
644 struct mpc_i2c *i2c = dev_id; in mpc_i2c_isr() local
647 status = readb(i2c->base + MPC_I2C_SR); in mpc_i2c_isr()
650 readb_poll_timeout_atomic(i2c->base + MPC_I2C_SR, status, status & CSR_MCF, 0, 100); in mpc_i2c_isr()
651 writeb(0, i2c->base + MPC_I2C_SR); in mpc_i2c_isr()
652 mpc_i2c_do_intr(i2c, status); in mpc_i2c_isr()
658 static int mpc_i2c_wait_for_completion(struct mpc_i2c *i2c) in mpc_i2c_wait_for_completion() argument
662 time_left = wait_event_timeout(i2c->waitq, !i2c->block, i2c->adap.timeout); in mpc_i2c_wait_for_completion()
671 static int mpc_i2c_execute_msg(struct mpc_i2c *i2c) in mpc_i2c_execute_msg() argument
677 spin_lock_irqsave(&i2c->lock, flags); in mpc_i2c_execute_msg()
679 i2c->curr_msg = 0; in mpc_i2c_execute_msg()
680 i2c->rc = 0; in mpc_i2c_execute_msg()
681 i2c->byte_posn = 0; in mpc_i2c_execute_msg()
682 i2c->block = 1; in mpc_i2c_execute_msg()
683 i2c->action = MPC_I2C_ACTION_START; in mpc_i2c_execute_msg()
685 i2c->cntl_bits = CCR_MEN | CCR_MIEN; in mpc_i2c_execute_msg()
686 writeb(0, i2c->base + MPC_I2C_SR); in mpc_i2c_execute_msg()
687 writeccr(i2c, i2c->cntl_bits); in mpc_i2c_execute_msg()
689 mpc_i2c_do_action(i2c); in mpc_i2c_execute_msg()
691 spin_unlock_irqrestore(&i2c->lock, flags); in mpc_i2c_execute_msg()
693 ret = mpc_i2c_wait_for_completion(i2c); in mpc_i2c_execute_msg()
695 i2c->rc = ret; in mpc_i2c_execute_msg()
697 if (i2c->rc == -EIO || i2c->rc == -EAGAIN || i2c->rc == -ETIMEDOUT) in mpc_i2c_execute_msg()
698 i2c_recover_bus(&i2c->adap); in mpc_i2c_execute_msg()
702 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) { in mpc_i2c_execute_msg()
704 u8 status = readb(i2c->base + MPC_I2C_SR); in mpc_i2c_execute_msg()
706 dev_dbg(i2c->dev, "timeout\n"); in mpc_i2c_execute_msg()
709 i2c->base + MPC_I2C_SR); in mpc_i2c_execute_msg()
710 i2c_recover_bus(&i2c->adap); in mpc_i2c_execute_msg()
717 return i2c->rc; in mpc_i2c_execute_msg()
723 struct mpc_i2c *i2c = i2c_get_adapdata(adap); in mpc_xfer() local
726 dev_dbg(i2c->dev, "num = %d\n", num); in mpc_xfer()
728 dev_dbg(i2c->dev, " addr = %02x, flags = %02x, len = %d, %*ph\n", in mpc_xfer()
733 WARN_ON(i2c->msgs != NULL); in mpc_xfer()
734 i2c->msgs = msgs; in mpc_xfer()
735 i2c->num_msgs = num; in mpc_xfer()
737 rc = mpc_i2c_execute_msg(i2c); in mpc_xfer()
741 i2c->num_msgs = 0; in mpc_xfer()
742 i2c->msgs = NULL; in mpc_xfer()
755 struct mpc_i2c *i2c = i2c_get_adapdata(adap); in fsl_i2c_bus_recovery() local
757 if (i2c->has_errata_A004447) in fsl_i2c_bus_recovery()
758 mpc_i2c_fixup_A004447(i2c); in fsl_i2c_bus_recovery()
760 mpc_i2c_fixup(i2c); in fsl_i2c_bus_recovery()
783 struct mpc_i2c *i2c; in fsl_i2c_probe() local
791 i2c = devm_kzalloc(&op->dev, sizeof(*i2c), GFP_KERNEL); in fsl_i2c_probe()
792 if (!i2c) in fsl_i2c_probe()
795 i2c->dev = &op->dev; /* for debug and error output */ in fsl_i2c_probe()
797 init_waitqueue_head(&i2c->waitq); in fsl_i2c_probe()
798 spin_lock_init(&i2c->lock); in fsl_i2c_probe()
800 i2c->base = devm_platform_ioremap_resource(op, 0); in fsl_i2c_probe()
801 if (IS_ERR(i2c->base)) in fsl_i2c_probe()
802 return PTR_ERR(i2c->base); in fsl_i2c_probe()
804 i2c->irq = platform_get_irq(op, 0); in fsl_i2c_probe()
805 if (i2c->irq < 0) in fsl_i2c_probe()
806 return i2c->irq; in fsl_i2c_probe()
808 result = devm_request_irq(&op->dev, i2c->irq, mpc_i2c_isr, in fsl_i2c_probe()
809 IRQF_SHARED, "i2c-mpc", i2c); in fsl_i2c_probe()
811 dev_err(i2c->dev, "failed to attach interrupt\n"); in fsl_i2c_probe()
816 * enable clock for the I2C peripheral (non fatal), in fsl_i2c_probe()
829 i2c->clk_per = clk; in fsl_i2c_probe()
842 data->setup(op->dev.of_node, i2c, clock); in fsl_i2c_probe()
846 mpc_i2c_setup_8xxx(op->dev.of_node, i2c, clock); in fsl_i2c_probe()
855 dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ); in fsl_i2c_probe()
857 if (of_property_read_bool(op->dev.of_node, "fsl,i2c-erratum-a004447")) in fsl_i2c_probe()
858 i2c->has_errata_A004447 = true; in fsl_i2c_probe()
860 i2c->adap = mpc_ops; in fsl_i2c_probe()
861 scnprintf(i2c->adap.name, sizeof(i2c->adap.name), in fsl_i2c_probe()
863 i2c->adap.dev.parent = &op->dev; in fsl_i2c_probe()
864 i2c->adap.nr = op->id; in fsl_i2c_probe()
865 i2c->adap.dev.of_node = of_node_get(op->dev.of_node); in fsl_i2c_probe()
866 i2c->adap.bus_recovery_info = &fsl_i2c_recovery_info; in fsl_i2c_probe()
867 platform_set_drvdata(op, i2c); in fsl_i2c_probe()
868 i2c_set_adapdata(&i2c->adap, i2c); in fsl_i2c_probe()
870 result = i2c_add_numbered_adapter(&i2c->adap); in fsl_i2c_probe()
877 clk_disable_unprepare(i2c->clk_per); in fsl_i2c_probe()
884 struct mpc_i2c *i2c = platform_get_drvdata(op); in fsl_i2c_remove() local
886 i2c_del_adapter(&i2c->adap); in fsl_i2c_remove()
888 clk_disable_unprepare(i2c->clk_per); in fsl_i2c_remove()
895 struct mpc_i2c *i2c = dev_get_drvdata(dev); in mpc_i2c_suspend() local
897 i2c->fdr = readb(i2c->base + MPC_I2C_FDR); in mpc_i2c_suspend()
898 i2c->dfsrr = readb(i2c->base + MPC_I2C_DFSRR); in mpc_i2c_suspend()
905 struct mpc_i2c *i2c = dev_get_drvdata(dev); in mpc_i2c_resume() local
907 writeb(i2c->fdr, i2c->base + MPC_I2C_FDR); in mpc_i2c_resume()
908 writeb(i2c->dfsrr, i2c->base + MPC_I2C_DFSRR); in mpc_i2c_resume()
935 {.compatible = "mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
936 {.compatible = "fsl,mpc5200b-i2c", .data = &mpc_i2c_data_52xx, },
937 {.compatible = "fsl,mpc5200-i2c", .data = &mpc_i2c_data_52xx, },
938 {.compatible = "fsl,mpc5121-i2c", .data = &mpc_i2c_data_512x, },
939 {.compatible = "fsl,mpc8313-i2c", .data = &mpc_i2c_data_8313, },
940 {.compatible = "fsl,mpc8543-i2c", .data = &mpc_i2c_data_8543, },
941 {.compatible = "fsl,mpc8544-i2c", .data = &mpc_i2c_data_8544, },
943 {.compatible = "fsl-i2c", },
962 MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "