Lines Matching full:i2c

5  *  I2C adapter for the PXA I2C bus access.
24 #include <linux/i2c.h>
34 #include <linux/platform_data/i2c-pxa.h>
37 /* I2C register field definitions */
111 * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty)
112 * 7 GCD 1 (Disables i2c unit response to general call messages as a slave)
114 * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL)
123 /* I2C status register init values
154 /* I2C register layout definitions */
206 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
209 { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 },
215 { "pxa2xx-i2c", REGS_PXA2XX },
217 { "ce4100-i2c", REGS_CE4100 },
218 { "pxa910-i2c", REGS_PXA910 },
219 { "armada-3700-i2c", REGS_A3700 },
269 #define _IBMR(i2c) ((i2c)->reg_ibmr) argument
270 #define _IDBR(i2c) ((i2c)->reg_idbr) argument
271 #define _ICR(i2c) ((i2c)->reg_icr) argument
272 #define _ISR(i2c) ((i2c)->reg_isr) argument
273 #define _ISAR(i2c) ((i2c)->reg_isar) argument
274 #define _ILCR(i2c) ((i2c)->reg_ilcr) argument
275 #define _IWCR(i2c) ((i2c)->reg_iwcr) argument
278 * I2C Slave mode address
350 static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname) in i2c_pxa_show_state() argument
352 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, in i2c_pxa_show_state()
353 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); in i2c_pxa_show_state()
356 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__) argument
358 static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why) in i2c_pxa_scream_blue_murder() argument
361 struct device *dev = &i2c->adap.dev; in i2c_pxa_scream_blue_murder()
364 i2c->req_slave_addr >> 1, why); in i2c_pxa_scream_blue_murder()
366 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr); in i2c_pxa_scream_blue_murder()
368 readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)), in i2c_pxa_scream_blue_murder()
369 readl(_ISR(i2c))); in i2c_pxa_scream_blue_murder()
371 for (i = 0; i < i2c->irqlogidx; i++) in i2c_pxa_scream_blue_murder()
372 pr_cont(" [%03x:%05x]", i2c->isrlog[i], i2c->icrlog[i]); in i2c_pxa_scream_blue_murder()
380 #define show_state(i2c) do { } while (0) argument
383 #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0) argument
387 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
389 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c) in i2c_pxa_is_slavemode() argument
391 return !(readl(_ICR(i2c)) & ICR_SCLE); in i2c_pxa_is_slavemode()
394 static void i2c_pxa_abort(struct pxa_i2c *i2c) in i2c_pxa_abort() argument
398 if (i2c_pxa_is_slavemode(i2c)) { in i2c_pxa_abort()
399 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__); in i2c_pxa_abort()
403 while ((i > 0) && (readl(_IBMR(i2c)) & IBMR_SDAS) == 0) { in i2c_pxa_abort()
404 unsigned long icr = readl(_ICR(i2c)); in i2c_pxa_abort()
409 writel(icr, _ICR(i2c)); in i2c_pxa_abort()
411 show_state(i2c); in i2c_pxa_abort()
417 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP), in i2c_pxa_abort()
418 _ICR(i2c)); in i2c_pxa_abort()
421 static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c) in i2c_pxa_wait_bus_not_busy() argument
427 isr = readl(_ISR(i2c)); in i2c_pxa_wait_bus_not_busy()
438 show_state(i2c); in i2c_pxa_wait_bus_not_busy()
441 show_state(i2c); in i2c_pxa_wait_bus_not_busy()
446 static int i2c_pxa_wait_master(struct pxa_i2c *i2c) in i2c_pxa_wait_master() argument
452 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n", in i2c_pxa_wait_master()
453 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); in i2c_pxa_wait_master()
455 if (readl(_ISR(i2c)) & ISR_SAD) { in i2c_pxa_wait_master()
457 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__); in i2c_pxa_wait_master()
462 * quick check of the i2c lines themselves to ensure they've in i2c_pxa_wait_master()
465 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && in i2c_pxa_wait_master()
466 readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) { in i2c_pxa_wait_master()
468 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); in i2c_pxa_wait_master()
476 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); in i2c_pxa_wait_master()
481 static int i2c_pxa_set_master(struct pxa_i2c *i2c) in i2c_pxa_set_master() argument
484 dev_dbg(&i2c->adap.dev, "setting to bus master\n"); in i2c_pxa_set_master()
486 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) { in i2c_pxa_set_master()
487 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__); in i2c_pxa_set_master()
488 if (!i2c_pxa_wait_master(i2c)) { in i2c_pxa_set_master()
489 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__); in i2c_pxa_set_master()
494 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); in i2c_pxa_set_master()
499 static int i2c_pxa_wait_slave(struct pxa_i2c *i2c) in i2c_pxa_wait_slave() argument
505 show_state(i2c); in i2c_pxa_wait_slave()
509 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n", in i2c_pxa_wait_slave()
510 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c))); in i2c_pxa_wait_slave()
512 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 || in i2c_pxa_wait_slave()
513 (readl(_ISR(i2c)) & ISR_SAD) != 0 || in i2c_pxa_wait_slave()
514 (readl(_ICR(i2c)) & ICR_SCLE) == 0) { in i2c_pxa_wait_slave()
516 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); in i2c_pxa_wait_slave()
524 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); in i2c_pxa_wait_slave()
532 static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode) in i2c_pxa_set_slave() argument
534 show_state(i2c); in i2c_pxa_set_slave()
542 if (readl(_ICR(i2c)) & ICR_STOP) { in i2c_pxa_set_slave()
544 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c)); in i2c_pxa_set_slave()
547 if (!i2c_pxa_wait_slave(i2c)) { in i2c_pxa_set_slave()
548 dev_err(&i2c->adap.dev, "%s: wait timedout\n", in i2c_pxa_set_slave()
554 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c)); in i2c_pxa_set_slave()
555 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); in i2c_pxa_set_slave()
558 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c))); in i2c_pxa_set_slave()
559 decode_ICR(readl(_ICR(i2c))); in i2c_pxa_set_slave()
563 #define i2c_pxa_set_slave(i2c, err) do { } while (0) argument
566 static void i2c_pxa_do_reset(struct pxa_i2c *i2c) in i2c_pxa_do_reset() argument
569 writel(ICR_UR, _ICR(i2c)); in i2c_pxa_do_reset()
570 writel(I2C_ISR_INIT, _ISR(i2c)); in i2c_pxa_do_reset()
571 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c)); in i2c_pxa_do_reset()
573 if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE)) in i2c_pxa_do_reset()
574 writel(i2c->slave_addr, _ISAR(i2c)); in i2c_pxa_do_reset()
577 writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c)); in i2c_pxa_do_reset()
578 writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c)); in i2c_pxa_do_reset()
581 dev_info(&i2c->adap.dev, "Enabling slave mode\n"); in i2c_pxa_do_reset()
582 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c)); in i2c_pxa_do_reset()
585 i2c_pxa_set_slave(i2c, 0); in i2c_pxa_do_reset()
588 static void i2c_pxa_enable(struct pxa_i2c *i2c) in i2c_pxa_enable() argument
591 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c)); in i2c_pxa_enable()
595 static void i2c_pxa_reset(struct pxa_i2c *i2c) in i2c_pxa_reset() argument
597 pr_debug("Resetting I2C Controller Unit\n"); in i2c_pxa_reset()
600 i2c_pxa_abort(i2c); in i2c_pxa_reset()
601 i2c_pxa_do_reset(i2c); in i2c_pxa_reset()
602 i2c_pxa_enable(i2c); in i2c_pxa_reset()
608 * PXA I2C Slave mode
611 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_txempty() argument
618 if (i2c->slave != NULL) in i2c_pxa_slave_txempty()
619 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED, in i2c_pxa_slave_txempty()
622 writel(byte, _IDBR(i2c)); in i2c_pxa_slave_txempty()
623 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */ in i2c_pxa_slave_txempty()
627 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_rxfull() argument
629 u8 byte = readl(_IDBR(i2c)); in i2c_pxa_slave_rxfull()
631 if (i2c->slave != NULL) in i2c_pxa_slave_rxfull()
632 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_RECEIVED, &byte); in i2c_pxa_slave_rxfull()
634 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); in i2c_pxa_slave_rxfull()
637 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_start() argument
642 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n", in i2c_pxa_slave_start()
645 if (i2c->slave != NULL) { in i2c_pxa_slave_start()
649 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED, in i2c_pxa_slave_start()
651 writel(byte, _IDBR(i2c)); in i2c_pxa_slave_start()
653 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED, in i2c_pxa_slave_start()
663 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); in i2c_pxa_slave_start()
664 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); in i2c_pxa_slave_start()
669 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) in i2c_pxa_slave_start()
675 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); in i2c_pxa_slave_start()
680 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); in i2c_pxa_slave_start()
683 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) in i2c_pxa_slave_stop() argument
686 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n"); in i2c_pxa_slave_stop()
688 if (i2c->slave != NULL) in i2c_pxa_slave_stop()
689 i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL); in i2c_pxa_slave_stop()
692 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n"); in i2c_pxa_slave_stop()
698 if (i2c->msg) in i2c_pxa_slave_stop()
699 i2c_pxa_master_complete(i2c, I2C_RETRY); in i2c_pxa_slave_stop()
704 struct pxa_i2c *i2c = slave->adapter->algo_data; in i2c_pxa_slave_reg() local
706 if (i2c->slave) in i2c_pxa_slave_reg()
709 if (!i2c->reg_isar) in i2c_pxa_slave_reg()
712 i2c->slave = slave; in i2c_pxa_slave_reg()
713 i2c->slave_addr = slave->addr; in i2c_pxa_slave_reg()
715 writel(i2c->slave_addr, _ISAR(i2c)); in i2c_pxa_slave_reg()
722 struct pxa_i2c *i2c = slave->adapter->algo_data; in i2c_pxa_slave_unreg() local
724 WARN_ON(!i2c->slave); in i2c_pxa_slave_unreg()
726 i2c->slave_addr = I2C_PXA_SLAVE_ADDR; in i2c_pxa_slave_unreg()
727 writel(i2c->slave_addr, _ISAR(i2c)); in i2c_pxa_slave_unreg()
729 i2c->slave = NULL; in i2c_pxa_slave_unreg()
734 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_txempty() argument
739 writel(0, _IDBR(i2c)); in i2c_pxa_slave_txempty()
740 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); in i2c_pxa_slave_txempty()
744 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_rxfull() argument
746 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); in i2c_pxa_slave_rxfull()
749 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_slave_start() argument
758 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c)); in i2c_pxa_slave_start()
759 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c)); in i2c_pxa_slave_start()
764 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) in i2c_pxa_slave_start()
770 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); in i2c_pxa_slave_start()
775 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c)); in i2c_pxa_slave_start()
778 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c) in i2c_pxa_slave_stop() argument
780 if (i2c->msg) in i2c_pxa_slave_stop()
781 i2c_pxa_master_complete(i2c, I2C_RETRY); in i2c_pxa_slave_stop()
786 * PXA I2C Master mode
789 static inline void i2c_pxa_start_message(struct pxa_i2c *i2c) in i2c_pxa_start_message() argument
796 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg); in i2c_pxa_start_message()
797 writel(i2c->req_slave_addr, _IDBR(i2c)); in i2c_pxa_start_message()
802 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE); in i2c_pxa_start_message()
803 writel(icr | ICR_START | ICR_TB, _ICR(i2c)); in i2c_pxa_start_message()
806 static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c) in i2c_pxa_stop_message() argument
811 icr = readl(_ICR(i2c)); in i2c_pxa_stop_message()
813 writel(icr, _ICR(i2c)); in i2c_pxa_stop_message()
817 * PXA I2C send master code
822 static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c) in i2c_pxa_send_mastercode() argument
827 spin_lock_irq(&i2c->lock); in i2c_pxa_send_mastercode()
828 i2c->highmode_enter = true; in i2c_pxa_send_mastercode()
829 writel(i2c->master_code, _IDBR(i2c)); in i2c_pxa_send_mastercode()
831 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE); in i2c_pxa_send_mastercode()
833 writel(icr, _ICR(i2c)); in i2c_pxa_send_mastercode()
835 spin_unlock_irq(&i2c->lock); in i2c_pxa_send_mastercode()
836 timeout = wait_event_timeout(i2c->wait, in i2c_pxa_send_mastercode()
837 i2c->highmode_enter == false, HZ * 1); in i2c_pxa_send_mastercode()
839 i2c->highmode_enter = false; in i2c_pxa_send_mastercode()
847 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret) in i2c_pxa_master_complete() argument
849 i2c->msg_ptr = 0; in i2c_pxa_master_complete()
850 i2c->msg = NULL; in i2c_pxa_master_complete()
851 i2c->msg_idx ++; in i2c_pxa_master_complete()
852 i2c->msg_num = 0; in i2c_pxa_master_complete()
854 i2c->msg_idx = ret; in i2c_pxa_master_complete()
855 if (!i2c->use_pio) in i2c_pxa_master_complete()
856 wake_up(&i2c->wait); in i2c_pxa_master_complete()
859 static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_irq_txempty() argument
861 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB); in i2c_pxa_irq_txempty()
872 i2c_pxa_scream_blue_murder(i2c, "ALD set"); in i2c_pxa_irq_txempty()
877 * they should, we end up causing an I2C error, which in i2c_pxa_irq_txempty()
884 (!((i2c->msg->flags & I2C_M_IGNORE_NAK) && in i2c_pxa_irq_txempty()
889 * I2C bus error - either the device NAK'd us, or in i2c_pxa_irq_txempty()
894 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0) in i2c_pxa_irq_txempty()
899 i2c_pxa_master_complete(i2c, ret); in i2c_pxa_irq_txempty()
905 if (i2c->msg_ptr == i2c->msg->len - 1 && in i2c_pxa_irq_txempty()
906 i2c->msg_idx == i2c->msg_num - 1) in i2c_pxa_irq_txempty()
910 } else if (i2c->msg_ptr < i2c->msg->len) { in i2c_pxa_irq_txempty()
914 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c)); in i2c_pxa_irq_txempty()
922 if ((i2c->msg_ptr == i2c->msg->len) && in i2c_pxa_irq_txempty()
923 ((i2c->msg->flags & I2C_M_STOP) || in i2c_pxa_irq_txempty()
924 (i2c->msg_idx == i2c->msg_num - 1))) in i2c_pxa_irq_txempty()
927 } else if (i2c->msg_idx < i2c->msg_num - 1) { in i2c_pxa_irq_txempty()
931 i2c->msg_ptr = 0; in i2c_pxa_irq_txempty()
932 i2c->msg_idx ++; in i2c_pxa_irq_txempty()
933 i2c->msg++; in i2c_pxa_irq_txempty()
940 if (i2c->msg->flags & I2C_M_NOSTART) in i2c_pxa_irq_txempty()
946 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg); in i2c_pxa_irq_txempty()
947 writel(i2c->req_slave_addr, _IDBR(i2c)); in i2c_pxa_irq_txempty()
955 if (i2c->msg->len == 0) in i2c_pxa_irq_txempty()
957 i2c_pxa_master_complete(i2c, 0); in i2c_pxa_irq_txempty()
960 i2c->icrlog[i2c->irqlogidx-1] = icr; in i2c_pxa_irq_txempty()
962 writel(icr, _ICR(i2c)); in i2c_pxa_irq_txempty()
963 show_state(i2c); in i2c_pxa_irq_txempty()
966 static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr) in i2c_pxa_irq_rxfull() argument
968 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB); in i2c_pxa_irq_rxfull()
973 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c)); in i2c_pxa_irq_rxfull()
975 if (i2c->msg_ptr < i2c->msg->len) { in i2c_pxa_irq_rxfull()
980 if (i2c->msg_ptr == i2c->msg->len - 1) in i2c_pxa_irq_rxfull()
985 i2c_pxa_master_complete(i2c, 0); in i2c_pxa_irq_rxfull()
988 i2c->icrlog[i2c->irqlogidx-1] = icr; in i2c_pxa_irq_rxfull()
990 writel(icr, _ICR(i2c)); in i2c_pxa_irq_rxfull()
997 struct pxa_i2c *i2c = dev_id; in i2c_pxa_handler() local
998 u32 isr = readl(_ISR(i2c)); in i2c_pxa_handler()
1004 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n", in i2c_pxa_handler()
1005 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c))); in i2c_pxa_handler()
1009 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog)) in i2c_pxa_handler()
1010 i2c->isrlog[i2c->irqlogidx++] = isr; in i2c_pxa_handler()
1012 show_state(i2c); in i2c_pxa_handler()
1017 writel(isr & VALID_INT_SOURCE, _ISR(i2c)); in i2c_pxa_handler()
1020 i2c_pxa_slave_start(i2c, isr); in i2c_pxa_handler()
1022 i2c_pxa_slave_stop(i2c); in i2c_pxa_handler()
1024 if (i2c_pxa_is_slavemode(i2c)) { in i2c_pxa_handler()
1026 i2c_pxa_slave_txempty(i2c, isr); in i2c_pxa_handler()
1028 i2c_pxa_slave_rxfull(i2c, isr); in i2c_pxa_handler()
1029 } else if (i2c->msg && (!i2c->highmode_enter)) { in i2c_pxa_handler()
1031 i2c_pxa_irq_txempty(i2c, isr); in i2c_pxa_handler()
1033 i2c_pxa_irq_rxfull(i2c, isr); in i2c_pxa_handler()
1034 } else if ((isr & ISR_ITE) && i2c->highmode_enter) { in i2c_pxa_handler()
1035 i2c->highmode_enter = false; in i2c_pxa_handler()
1036 wake_up(&i2c->wait); in i2c_pxa_handler()
1038 i2c_pxa_scream_blue_murder(i2c, "spurious irq"); in i2c_pxa_handler()
1047 static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num) in i2c_pxa_do_xfer() argument
1055 ret = i2c_pxa_wait_bus_not_busy(i2c); in i2c_pxa_do_xfer()
1057 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n"); in i2c_pxa_do_xfer()
1058 i2c_recover_bus(&i2c->adap); in i2c_pxa_do_xfer()
1065 ret = i2c_pxa_set_master(i2c); in i2c_pxa_do_xfer()
1067 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret); in i2c_pxa_do_xfer()
1071 if (i2c->high_mode) { in i2c_pxa_do_xfer()
1072 ret = i2c_pxa_send_mastercode(i2c); in i2c_pxa_do_xfer()
1074 dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n"); in i2c_pxa_do_xfer()
1079 spin_lock_irq(&i2c->lock); in i2c_pxa_do_xfer()
1081 i2c->msg = msg; in i2c_pxa_do_xfer()
1082 i2c->msg_num = num; in i2c_pxa_do_xfer()
1083 i2c->msg_idx = 0; in i2c_pxa_do_xfer()
1084 i2c->msg_ptr = 0; in i2c_pxa_do_xfer()
1085 i2c->irqlogidx = 0; in i2c_pxa_do_xfer()
1087 i2c_pxa_start_message(i2c); in i2c_pxa_do_xfer()
1089 spin_unlock_irq(&i2c->lock); in i2c_pxa_do_xfer()
1094 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); in i2c_pxa_do_xfer()
1095 i2c_pxa_stop_message(i2c); in i2c_pxa_do_xfer()
1098 * We place the return code in i2c->msg_idx. in i2c_pxa_do_xfer()
1100 ret = i2c->msg_idx; in i2c_pxa_do_xfer()
1102 if (!timeout && i2c->msg_num) { in i2c_pxa_do_xfer()
1103 i2c_pxa_scream_blue_murder(i2c, "timeout with active message"); in i2c_pxa_do_xfer()
1104 i2c_recover_bus(&i2c->adap); in i2c_pxa_do_xfer()
1112 static int i2c_pxa_internal_xfer(struct pxa_i2c *i2c, in i2c_pxa_internal_xfer() argument
1120 ret = xfer(i2c, msgs, num); in i2c_pxa_internal_xfer()
1123 if (++i >= i2c->adap.retries) in i2c_pxa_internal_xfer()
1127 dev_dbg(&i2c->adap.dev, "Retrying transmission\n"); in i2c_pxa_internal_xfer()
1131 i2c_pxa_scream_blue_murder(i2c, "exhausted retries"); in i2c_pxa_internal_xfer()
1134 i2c_pxa_set_slave(i2c, ret); in i2c_pxa_internal_xfer()
1141 struct pxa_i2c *i2c = adap->algo_data; in i2c_pxa_xfer() local
1143 return i2c_pxa_internal_xfer(i2c, msgs, num, i2c_pxa_do_xfer); in i2c_pxa_xfer()
1162 static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c) in i2c_pxa_pio_set_master() argument
1170 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) in i2c_pxa_pio_set_master()
1174 show_state(i2c); in i2c_pxa_pio_set_master()
1175 dev_err(&i2c->adap.dev, in i2c_pxa_pio_set_master()
1183 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c)); in i2c_pxa_pio_set_master()
1188 static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c, in i2c_pxa_do_pio_xfer() argument
1194 ret = i2c_pxa_pio_set_master(i2c); in i2c_pxa_do_pio_xfer()
1198 i2c->msg = msg; in i2c_pxa_do_pio_xfer()
1199 i2c->msg_num = num; in i2c_pxa_do_pio_xfer()
1200 i2c->msg_idx = 0; in i2c_pxa_do_pio_xfer()
1201 i2c->msg_ptr = 0; in i2c_pxa_do_pio_xfer()
1202 i2c->irqlogidx = 0; in i2c_pxa_do_pio_xfer()
1204 i2c_pxa_start_message(i2c); in i2c_pxa_do_pio_xfer()
1206 while (i2c->msg_num > 0 && --timeout) { in i2c_pxa_do_pio_xfer()
1207 i2c_pxa_handler(0, i2c); in i2c_pxa_do_pio_xfer()
1211 i2c_pxa_stop_message(i2c); in i2c_pxa_do_pio_xfer()
1214 * We place the return code in i2c->msg_idx. in i2c_pxa_do_pio_xfer()
1216 ret = i2c->msg_idx; in i2c_pxa_do_pio_xfer()
1220 i2c_pxa_scream_blue_murder(i2c, "timeout (do_pio_xfer)"); in i2c_pxa_do_pio_xfer()
1230 struct pxa_i2c *i2c = adap->algo_data; in i2c_pxa_pio_xfer() local
1232 /* If the I2C controller is disabled we need to reset it in i2c_pxa_pio_xfer()
1236 if (!(readl(_ICR(i2c)) & ICR_IUE)) in i2c_pxa_pio_xfer()
1237 i2c_pxa_reset(i2c); in i2c_pxa_pio_xfer()
1239 return i2c_pxa_internal_xfer(i2c, msgs, num, i2c_pxa_do_pio_xfer); in i2c_pxa_pio_xfer()
1251 static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c, in i2c_pxa_probe_dt() argument
1262 i2c->adap.nr = -1; in i2c_pxa_probe_dt()
1264 if (of_get_property(np, "mrvl,i2c-polling", NULL)) in i2c_pxa_probe_dt()
1265 i2c->use_pio = 1; in i2c_pxa_probe_dt()
1266 if (of_get_property(np, "mrvl,i2c-fast-mode", NULL)) in i2c_pxa_probe_dt()
1267 i2c->fast_mode = 1; in i2c_pxa_probe_dt()
1275 struct pxa_i2c *i2c, in i2c_pxa_probe_pdata() argument
1283 i2c->use_pio = plat->use_pio; in i2c_pxa_probe_pdata()
1284 i2c->fast_mode = plat->fast_mode; in i2c_pxa_probe_pdata()
1285 i2c->high_mode = plat->high_mode; in i2c_pxa_probe_pdata()
1286 i2c->master_code = plat->master_code; in i2c_pxa_probe_pdata()
1287 if (!i2c->master_code) in i2c_pxa_probe_pdata()
1288 i2c->master_code = 0xe; in i2c_pxa_probe_pdata()
1289 i2c->rate = plat->rate; in i2c_pxa_probe_pdata()
1296 struct pxa_i2c *i2c = adap->algo_data; in i2c_pxa_prepare_recovery() local
1297 u32 ibmr = readl(_IBMR(i2c)); in i2c_pxa_prepare_recovery()
1300 * Program the GPIOs to reflect the current I2C bus state while in i2c_pxa_prepare_recovery()
1303 gpiod_set_value(i2c->recovery.scl_gpiod, ibmr & IBMR_SCLS); in i2c_pxa_prepare_recovery()
1304 gpiod_set_value(i2c->recovery.sda_gpiod, ibmr & IBMR_SDAS); in i2c_pxa_prepare_recovery()
1309 struct pxa_i2c *i2c = adap->algo_data; in i2c_pxa_unprepare_recovery() local
1314 * The bus should now be free. Clear up the I2C controller before in i2c_pxa_unprepare_recovery()
1317 isr = readl(_ISR(i2c)); in i2c_pxa_unprepare_recovery()
1319 dev_dbg(&i2c->adap.dev, in i2c_pxa_unprepare_recovery()
1321 i2c_pxa_do_reset(i2c); in i2c_pxa_unprepare_recovery()
1326 dev_dbg(&i2c->adap.dev, "recovery: IBMR 0x%08x ISR 0x%08x\n", in i2c_pxa_unprepare_recovery()
1327 readl(_IBMR(i2c)), readl(_ISR(i2c))); in i2c_pxa_unprepare_recovery()
1329 i2c_pxa_enable(i2c); in i2c_pxa_unprepare_recovery()
1332 static int i2c_pxa_init_recovery(struct pxa_i2c *i2c) in i2c_pxa_init_recovery() argument
1334 struct i2c_bus_recovery_info *bri = &i2c->recovery; in i2c_pxa_init_recovery()
1335 struct device *dev = i2c->adap.dev.parent; in i2c_pxa_init_recovery()
1357 i2c->adap.bus_recovery_info = bri; in i2c_pxa_init_recovery()
1366 struct pxa_i2c *i2c; in i2c_pxa_probe() local
1370 i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL); in i2c_pxa_probe()
1371 if (!i2c) in i2c_pxa_probe()
1375 i2c->adap.nr = dev->id; in i2c_pxa_probe()
1376 i2c->adap.owner = THIS_MODULE; in i2c_pxa_probe()
1377 i2c->adap.retries = 5; in i2c_pxa_probe()
1378 i2c->adap.algo_data = i2c; in i2c_pxa_probe()
1379 i2c->adap.dev.parent = &dev->dev; in i2c_pxa_probe()
1381 i2c->adap.dev.of_node = dev->dev.of_node; in i2c_pxa_probe()
1385 i2c->reg_base = devm_ioremap_resource(&dev->dev, res); in i2c_pxa_probe()
1386 if (IS_ERR(i2c->reg_base)) in i2c_pxa_probe()
1387 return PTR_ERR(i2c->reg_base); in i2c_pxa_probe()
1393 ret = i2c_pxa_init_recovery(i2c); in i2c_pxa_probe()
1397 ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type); in i2c_pxa_probe()
1399 ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type); in i2c_pxa_probe()
1403 spin_lock_init(&i2c->lock); in i2c_pxa_probe()
1404 init_waitqueue_head(&i2c->wait); in i2c_pxa_probe()
1406 strscpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name)); in i2c_pxa_probe()
1408 i2c->clk = devm_clk_get(&dev->dev, NULL); in i2c_pxa_probe()
1409 if (IS_ERR(i2c->clk)) { in i2c_pxa_probe()
1410 dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk)); in i2c_pxa_probe()
1411 return PTR_ERR(i2c->clk); in i2c_pxa_probe()
1414 i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr; in i2c_pxa_probe()
1415 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr; in i2c_pxa_probe()
1416 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr; in i2c_pxa_probe()
1417 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr; in i2c_pxa_probe()
1418 i2c->fm_mask = pxa_reg_layout[i2c_type].fm; in i2c_pxa_probe()
1419 i2c->hs_mask = pxa_reg_layout[i2c_type].hs; in i2c_pxa_probe()
1422 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar; in i2c_pxa_probe()
1425 i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr; in i2c_pxa_probe()
1426 i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr; in i2c_pxa_probe()
1429 i2c->iobase = res->start; in i2c_pxa_probe()
1430 i2c->iosize = resource_size(res); in i2c_pxa_probe()
1432 i2c->irq = irq; in i2c_pxa_probe()
1434 i2c->slave_addr = I2C_PXA_SLAVE_ADDR; in i2c_pxa_probe()
1435 i2c->highmode_enter = false; in i2c_pxa_probe()
1438 i2c->adap.class = plat->class; in i2c_pxa_probe()
1441 if (i2c->high_mode) { in i2c_pxa_probe()
1442 if (i2c->rate) { in i2c_pxa_probe()
1443 clk_set_rate(i2c->clk, i2c->rate); in i2c_pxa_probe()
1444 pr_info("i2c: <%s> set rate to %ld\n", in i2c_pxa_probe()
1445 i2c->adap.name, clk_get_rate(i2c->clk)); in i2c_pxa_probe()
1447 pr_warn("i2c: <%s> clock rate not set\n", in i2c_pxa_probe()
1448 i2c->adap.name); in i2c_pxa_probe()
1451 clk_prepare_enable(i2c->clk); in i2c_pxa_probe()
1453 if (i2c->use_pio) { in i2c_pxa_probe()
1454 i2c->adap.algo = &i2c_pxa_pio_algorithm; in i2c_pxa_probe()
1456 i2c->adap.algo = &i2c_pxa_algorithm; in i2c_pxa_probe()
1459 dev_name(&dev->dev), i2c); in i2c_pxa_probe()
1466 i2c_pxa_reset(i2c); in i2c_pxa_probe()
1468 ret = i2c_add_numbered_adapter(&i2c->adap); in i2c_pxa_probe()
1472 platform_set_drvdata(dev, i2c); in i2c_pxa_probe()
1475 dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n", in i2c_pxa_probe()
1476 i2c->slave_addr); in i2c_pxa_probe()
1478 dev_info(&i2c->adap.dev, " PXA I2C adapter\n"); in i2c_pxa_probe()
1483 clk_disable_unprepare(i2c->clk); in i2c_pxa_probe()
1489 struct pxa_i2c *i2c = platform_get_drvdata(dev); in i2c_pxa_remove() local
1491 i2c_del_adapter(&i2c->adap); in i2c_pxa_remove()
1493 clk_disable_unprepare(i2c->clk); in i2c_pxa_remove()
1501 struct pxa_i2c *i2c = dev_get_drvdata(dev); in i2c_pxa_suspend_noirq() local
1503 clk_disable(i2c->clk); in i2c_pxa_suspend_noirq()
1510 struct pxa_i2c *i2c = dev_get_drvdata(dev); in i2c_pxa_resume_noirq() local
1512 clk_enable(i2c->clk); in i2c_pxa_resume_noirq()
1513 i2c_pxa_reset(i2c); in i2c_pxa_resume_noirq()
1532 .name = "pxa2xx-i2c",