22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/i2c.h>
26 #include <linux/time.h>
27 #include <linux/sched.h>
29 #include <linux/errno.h>
38 #include <linux/slab.h>
97 #define ICR_START (1 << 0)
98 #define ICR_STOP (1 << 1)
99 #define ICR_ACKNAK (1 << 2)
100 #define ICR_TB (1 << 3)
101 #define ICR_MA (1 << 4)
102 #define ICR_SCLE (1 << 5)
103 #define ICR_IUE (1 << 6)
104 #define ICR_GCD (1 << 7)
105 #define ICR_ITEIE (1 << 8)
106 #define ICR_IRFIE (1 << 9)
107 #define ICR_BEIE (1 << 10)
108 #define ICR_SSDIE (1 << 11)
109 #define ICR_ALDIE (1 << 12)
110 #define ICR_SADIE (1 << 13)
111 #define ICR_UR (1 << 14)
112 #define ICR_FM (1 << 15)
114 #define ISR_RWM (1 << 0)
115 #define ISR_ACKNAK (1 << 1)
116 #define ISR_UB (1 << 2)
117 #define ISR_IBB (1 << 3)
118 #define ISR_SSD (1 << 4)
119 #define ISR_ALD (1 << 5)
120 #define ISR_ITE (1 << 6)
121 #define ISR_IRF (1 << 7)
122 #define ISR_GCAD (1 << 8)
123 #define ISR_SAD (1 << 9)
124 #define ISR_BED (1 << 10)
137 #ifdef CONFIG_I2C_PXA_SLAVE
160 #define _IBMR(i2c) ((i2c)->reg_ibmr)
161 #define _IDBR(i2c) ((i2c)->reg_idbr)
162 #define _ICR(i2c) ((i2c)->reg_icr)
163 #define _ISR(i2c) ((i2c)->reg_isr)
164 #define _ISAR(i2c) ((i2c)->reg_isar)
169 #define I2C_PXA_SLAVE_ADDR 0x1
178 #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
183 printk(
"%s %08x: ", prefix, val);
185 const char *
str = val & bits->mask ? bits->set : bits->unset;
192 static const struct bits isr_bits[] = {
195 PXA_BIT(
ISR_UB,
"Bsy",
"Rdy"),
196 PXA_BIT(
ISR_IBB,
"BusBsy",
"BusRdy"),
212 static const struct bits icr_bits[] = {
219 PXA_BIT(
ICR_IUE,
"IUE",
"iue"),
227 PXA_BIT(
ICR_UR,
"UR",
"ur"),
230 #ifdef CONFIG_I2C_PXA_SLAVE
240 static void i2c_pxa_show_state(
struct pxa_i2c *i2c,
int lno,
const char *
fname)
242 dev_dbg(&i2c->
adap.dev,
"state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
246 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
266 #define show_state(i2c) do { } while (0)
267 #define decode_ISR(val) do { } while (0)
268 #define decode_ICR(val) do { } while (0)
269 #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
273 static void i2c_pxa_master_complete(
struct pxa_i2c *i2c,
int ret);
276 static inline int i2c_pxa_is_slavemode(
struct pxa_i2c *i2c)
281 static void i2c_pxa_abort(
struct pxa_i2c *i2c)
285 if (i2c_pxa_is_slavemode(i2c)) {
286 dev_dbg(&i2c->
adap.dev,
"%s: called in slave mode\n", __func__);
290 while ((i > 0) && (
readl(
_IBMR(i2c)) & 0x1) == 0) {
308 static int i2c_pxa_wait_bus_not_busy(
struct pxa_i2c *i2c)
326 static int i2c_pxa_wait_master(
struct pxa_i2c *i2c)
332 dev_dbg(&i2c->
adap.dev,
"%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
337 dev_dbg(&i2c->
adap.dev,
"%s: Slave detected\n", __func__);
355 dev_dbg(&i2c->
adap.dev,
"%s: did not free\n", __func__);
360 static int i2c_pxa_set_master(
struct pxa_i2c *i2c)
363 dev_dbg(&i2c->
adap.dev,
"setting to bus master\n");
366 dev_dbg(&i2c->
adap.dev,
"%s: unit is busy\n", __func__);
367 if (!i2c_pxa_wait_master(i2c)) {
368 dev_dbg(&i2c->
adap.dev,
"%s: error: unit busy\n", __func__);
377 #ifdef CONFIG_I2C_PXA_SLAVE
378 static int i2c_pxa_wait_slave(
struct pxa_i2c *i2c)
380 unsigned long timeout = jiffies + HZ*1;
388 dev_dbg(&i2c->
adap.dev,
"%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
403 dev_dbg(&i2c->
adap.dev,
"%s: did not free\n", __func__);
426 if (!i2c_pxa_wait_slave(i2c)) {
442 #define i2c_pxa_set_slave(i2c, err) do { } while (0)
445 static void i2c_pxa_reset(
struct pxa_i2c *i2c)
447 pr_debug(
"Resetting I2C Controller Unit\n");
463 #ifdef CONFIG_I2C_PXA_SLAVE
476 #ifdef CONFIG_I2C_PXA_SLAVE
481 static void i2c_pxa_slave_txempty(
struct pxa_i2c *i2c,
u32 isr)
488 if (i2c->slave !=
NULL)
489 ret = i2c->slave->read(i2c->slave->data);
496 static void i2c_pxa_slave_rxfull(
struct pxa_i2c *i2c,
u32 isr)
500 if (i2c->slave !=
NULL)
501 i2c->slave->write(i2c->slave->data, byte);
506 static void i2c_pxa_slave_start(
struct pxa_i2c *i2c,
u32 isr)
511 dev_dbg(&i2c->
adap.dev,
"SAD, mode is slave-%cx\n",
514 if (i2c->slave !=
NULL)
515 i2c->slave->event(i2c->slave->data,
535 dev_err(&i2c->
adap.dev,
"timeout waiting for SCL high\n");
543 static void i2c_pxa_slave_stop(
struct pxa_i2c *i2c)
546 dev_dbg(&i2c->
adap.dev,
"ISR: SSD (Slave Stop)\n");
548 if (i2c->slave !=
NULL)
552 dev_dbg(&i2c->
adap.dev,
"ISR: SSD (Slave Stop) acked\n");
562 static void i2c_pxa_slave_txempty(
struct pxa_i2c *i2c,
u32 isr)
572 static void i2c_pxa_slave_rxfull(
struct pxa_i2c *i2c,
u32 isr)
577 static void i2c_pxa_slave_start(
struct pxa_i2c *i2c,
u32 isr)
598 dev_err(&i2c->
adap.dev,
"timeout waiting for SCL high\n");
606 static void i2c_pxa_slave_stop(
struct pxa_i2c *i2c)
617 static inline unsigned int i2c_pxa_addr_byte(
struct i2c_msg *
msg)
619 unsigned int addr = (msg->
addr & 0x7f) << 1;
627 static inline void i2c_pxa_start_message(
struct pxa_i2c *i2c)
643 static inline void i2c_pxa_stop_message(
struct pxa_i2c *i2c)
655 static int i2c_pxa_pio_set_master(
struct pxa_i2c *i2c)
671 "i2c_pxa: timeout waiting for bus free\n");
683 static int i2c_pxa_do_pio_xfer(
struct pxa_i2c *i2c,
686 unsigned long timeout = 500000;
689 ret = i2c_pxa_pio_set_master(i2c);
699 i2c_pxa_start_message(i2c);
701 while (i2c->
msg_num > 0 && --timeout) {
702 i2c_pxa_handler(0, i2c);
706 i2c_pxa_stop_message(i2c);
723 static int i2c_pxa_do_xfer(
struct pxa_i2c *i2c,
struct i2c_msg *msg,
int num)
731 ret = i2c_pxa_wait_bus_not_busy(i2c);
733 dev_err(&i2c->
adap.dev,
"i2c_pxa: timeout waiting for bus free\n");
740 ret = i2c_pxa_set_master(i2c);
742 dev_err(&i2c->
adap.dev,
"i2c_pxa_set_master: error %d\n", ret);
746 spin_lock_irq(&i2c->
lock);
754 i2c_pxa_start_message(i2c);
756 spin_unlock_irq(&i2c->
lock);
762 i2c_pxa_stop_message(i2c);
769 if (!timeout && i2c->
msg_num) {
778 static int i2c_pxa_pio_xfer(
struct i2c_adapter *adap,
779 struct i2c_msg msgs[],
int num)
791 for (i = adap->
retries; i >= 0; i--) {
792 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
797 dev_dbg(&adap->
dev,
"Retrying transmission\n");
810 static void i2c_pxa_master_complete(
struct pxa_i2c *i2c,
int ret)
822 static void i2c_pxa_irq_txempty(
struct pxa_i2c *i2c,
u32 isr)
860 i2c_pxa_master_complete(i2c, ret);
861 }
else if (isr & ISR_RWM) {
913 if (i2c->
msg->len == 0) {
921 i2c_pxa_master_complete(i2c, 0);
930 static void i2c_pxa_irq_rxfull(
struct pxa_i2c *i2c,
u32 isr)
949 i2c_pxa_master_complete(i2c, 0);
957 #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
967 if (i2c_debug > 2 && 0) {
968 dev_dbg(&i2c->
adap.dev,
"%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
984 i2c_pxa_slave_start(i2c, isr);
986 i2c_pxa_slave_stop(i2c);
988 if (i2c_pxa_is_slavemode(i2c)) {
990 i2c_pxa_slave_txempty(i2c, isr);
992 i2c_pxa_slave_rxfull(i2c, isr);
993 }
else if (i2c->
msg) {
995 i2c_pxa_irq_txempty(i2c, isr);
997 i2c_pxa_irq_rxfull(i2c, isr);
1011 for (i = adap->
retries; i >= 0; i--) {
1012 ret = i2c_pxa_do_xfer(i2c, msgs, num);
1017 dev_dbg(&adap->
dev,
"Retrying transmission\n");
1033 .master_xfer = i2c_pxa_xfer,
1034 .functionality = i2c_pxa_functionality,
1038 .master_xfer = i2c_pxa_pio_xfer,
1039 .functionality = i2c_pxa_functionality,
1043 { .compatible =
"mrvl,pxa-i2c", .data = (
void *)
REGS_PXA2XX },
1044 { .compatible =
"mrvl,pwri2c", .data = (
void *)
REGS_PXA3XX },
1045 { .compatible =
"mrvl,mmp-twsi", .data = (
void *)
REGS_PXA2XX },
1062 dev_err(&pdev->
dev,
"failed to get alias id, errno %d\n", ret);
1070 *i2c_types = (
u32)(of_id->
data);
1081 *i2c_types =
id->driver_data;
1103 ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
1105 ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
1111 if (res ==
NULL || irq < 0) {
1122 i2c->
adap.retries = 5;
1132 if (IS_ERR(i2c->
clk)) {
1133 ret = PTR_ERR(i2c->
clk);
1151 i2c->
iosize = resource_size(res);
1158 #ifdef CONFIG_I2C_PXA_SLAVE
1160 i2c->slave = plat->
slave;
1168 i2c->
adap.algo = &i2c_pxa_pio_algorithm;
1170 i2c->
adap.algo = &i2c_pxa_algorithm;
1172 i2c->
adap.name, i2c);
1179 i2c->
adap.algo_data = i2c;
1180 i2c->
adap.dev.parent = &dev->
dev;
1182 i2c->
adap.dev.of_node = dev->
dev.of_node;
1192 platform_set_drvdata(dev, i2c);
1194 #ifdef CONFIG_I2C_PXA_SLAVE
1199 dev_name(&i2c->
adap.dev));
1220 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1222 platform_set_drvdata(dev,
NULL);
1239 static int i2c_pxa_suspend_noirq(
struct device *dev)
1242 struct pxa_i2c *i2c = platform_get_drvdata(pdev);
1249 static int i2c_pxa_resume_noirq(
struct device *dev)
1252 struct pxa_i2c *i2c = platform_get_drvdata(pdev);
1260 static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
1262 .resume_noirq = i2c_pxa_resume_noirq,
1265 #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
1267 #define I2C_PXA_DEV_PM_OPS NULL
1271 .probe = i2c_pxa_probe,
1272 .remove =
__exit_p(i2c_pxa_remove),
1274 .name =
"pxa2xx-i2c",
1277 .of_match_table = i2c_pxa_dt_ids,
1279 .id_table = i2c_pxa_id_table,
1282 static int __init i2c_adap_pxa_init(
void)
1287 static void __exit i2c_adap_pxa_exit(
void)