31 #include <linux/module.h>
33 #include <linux/i2c.h>
43 #include <linux/slab.h>
48 #define OMAP_I2C_OMAP1_REV_2 0x20
51 #define OMAP_I2C_REV_ON_2430 0x36
52 #define OMAP_I2C_REV_ON_3430_3530 0x3C
53 #define OMAP_I2C_REV_ON_3630_4430 0x40
56 #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
59 #define OMAP_I2C_PM_TIMEOUT 1000
90 #define OMAP_I2C_IE_XDR (1 << 14)
91 #define OMAP_I2C_IE_RDR (1 << 13)
92 #define OMAP_I2C_IE_XRDY (1 << 4)
93 #define OMAP_I2C_IE_RRDY (1 << 3)
94 #define OMAP_I2C_IE_ARDY (1 << 2)
95 #define OMAP_I2C_IE_NACK (1 << 1)
96 #define OMAP_I2C_IE_AL (1 << 0)
99 #define OMAP_I2C_STAT_XDR (1 << 14)
100 #define OMAP_I2C_STAT_RDR (1 << 13)
101 #define OMAP_I2C_STAT_BB (1 << 12)
102 #define OMAP_I2C_STAT_ROVR (1 << 11)
103 #define OMAP_I2C_STAT_XUDF (1 << 10)
104 #define OMAP_I2C_STAT_AAS (1 << 9)
105 #define OMAP_I2C_STAT_AD0 (1 << 8)
106 #define OMAP_I2C_STAT_XRDY (1 << 4)
107 #define OMAP_I2C_STAT_RRDY (1 << 3)
108 #define OMAP_I2C_STAT_ARDY (1 << 2)
109 #define OMAP_I2C_STAT_NACK (1 << 1)
110 #define OMAP_I2C_STAT_AL (1 << 0)
113 #define OMAP_I2C_WE_XDR_WE (1 << 14)
114 #define OMAP_I2C_WE_RDR_WE (1 << 13)
115 #define OMAP_I2C_WE_AAS_WE (1 << 9)
116 #define OMAP_I2C_WE_BF_WE (1 << 8)
117 #define OMAP_I2C_WE_STC_WE (1 << 6)
118 #define OMAP_I2C_WE_GC_WE (1 << 5)
119 #define OMAP_I2C_WE_DRDY_WE (1 << 3)
120 #define OMAP_I2C_WE_ARDY_WE (1 << 2)
121 #define OMAP_I2C_WE_NACK_WE (1 << 1)
122 #define OMAP_I2C_WE_AL_WE (1 << 0)
124 #define OMAP_I2C_WE_ALL (OMAP_I2C_WE_XDR_WE | OMAP_I2C_WE_RDR_WE | \
125 OMAP_I2C_WE_AAS_WE | OMAP_I2C_WE_BF_WE | \
126 OMAP_I2C_WE_STC_WE | OMAP_I2C_WE_GC_WE | \
127 OMAP_I2C_WE_DRDY_WE | OMAP_I2C_WE_ARDY_WE | \
128 OMAP_I2C_WE_NACK_WE | OMAP_I2C_WE_AL_WE)
131 #define OMAP_I2C_BUF_RDMA_EN (1 << 15)
132 #define OMAP_I2C_BUF_RXFIF_CLR (1 << 14)
133 #define OMAP_I2C_BUF_XDMA_EN (1 << 7)
134 #define OMAP_I2C_BUF_TXFIF_CLR (1 << 6)
137 #define OMAP_I2C_CON_EN (1 << 15)
138 #define OMAP_I2C_CON_BE (1 << 14)
139 #define OMAP_I2C_CON_OPMODE_HS (1 << 12)
140 #define OMAP_I2C_CON_STB (1 << 11)
141 #define OMAP_I2C_CON_MST (1 << 10)
142 #define OMAP_I2C_CON_TRX (1 << 9)
143 #define OMAP_I2C_CON_XA (1 << 8)
144 #define OMAP_I2C_CON_RM (1 << 2)
145 #define OMAP_I2C_CON_STP (1 << 1)
146 #define OMAP_I2C_CON_STT (1 << 0)
149 #define OMAP_I2C_SCLL_HSSCLL 8
150 #define OMAP_I2C_SCLH_HSSCLH 8
154 #define OMAP_I2C_SYSTEST_ST_EN (1 << 15)
155 #define OMAP_I2C_SYSTEST_FREE (1 << 14)
156 #define OMAP_I2C_SYSTEST_TMODE_MASK (3 << 12)
157 #define OMAP_I2C_SYSTEST_TMODE_SHIFT (12)
158 #define OMAP_I2C_SYSTEST_SCL_I (1 << 3)
159 #define OMAP_I2C_SYSTEST_SCL_O (1 << 2)
160 #define OMAP_I2C_SYSTEST_SDA_I (1 << 1)
161 #define OMAP_I2C_SYSTEST_SDA_O (1 << 0)
165 #define SYSS_RESETDONE_MASK (1 << 0)
168 #define SYSC_CLOCKACTIVITY_MASK (0x3 << 8)
169 #define SYSC_SIDLEMODE_MASK (0x3 << 3)
170 #define SYSC_ENAWAKEUP_MASK (1 << 2)
171 #define SYSC_SOFTRESET_MASK (1 << 1)
172 #define SYSC_AUTOIDLE_MASK (1 << 0)
174 #define SYSC_IDLEMODE_SMART 0x2
175 #define SYSC_CLOCKACTIVITY_FCLK 0x2
178 #define I2C_OMAP_ERRATA_I207 (1 << 0)
179 #define I2C_OMAP_ERRATA_I462 (1 << 1)
218 static const u8 reg_map_ip_v1[] = {
239 static const u8 reg_map_ip_v2[] = {
280 u16 psc = 0, scll = 0, sclh = 0,
buf = 0;
281 u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
282 unsigned long fclk_rate = 12000000;
284 unsigned long internal_clk = 0;
302 "for controller reset\n");
356 if (fclk_rate > 12000000)
357 psc = fclk_rate / 12000000;
368 if (dev->
speed > 400 ||
370 internal_clk = 19200;
371 else if (dev->
speed > 100)
380 psc = fclk_rate / internal_clk;
384 if (dev->
speed > 400) {
388 scl = internal_clk / 400;
389 fsscll = scl - (scl / 3) - 7;
390 fssclh = (scl / 3) - 5;
393 scl = fclk_rate / dev->
speed;
394 hsscll = scl - (scl / 3) - 7;
395 hssclh = (scl / 3) - 5;
396 }
else if (dev->
speed > 100) {
400 scl = internal_clk / dev->
speed;
401 fsscll = scl - (scl / 3) - 7;
402 fssclh = (scl / 3) - 5;
405 fsscll = internal_clk / (dev->
speed * 2) - 7;
406 fssclh = internal_clk / (dev->
speed * 2) - 5;
412 fclk_rate /= (psc + 1) * 1000;
415 scll = fclk_rate / (dev->
speed * 2) - 7 + psc;
416 sclh = fclk_rate / (dev->
speed * 2) - 7 + psc;
447 static int omap_i2c_wait_for_bb(
struct omap_i2c_dev *dev)
449 unsigned long timeout;
454 dev_warn(dev->
dev,
"timeout waiting for bus ready\n");
499 (1000 * dev->
speed / 8);
505 static int omap_i2c_xfer_msg(
struct i2c_adapter *adap,
509 unsigned long timeout;
512 dev_dbg(dev->
dev,
"addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
519 omap_i2c_resize_fifo(dev, msg->
len, dev->
receiver);
543 if (dev->
speed > 400)
553 if (!dev->
b_hw && stop)
561 if (dev->
b_hw && stop) {
570 "waiting for start condition to finish\n");
577 w &= ~OMAP_I2C_CON_STT;
628 r = pm_runtime_get_sync(dev->
dev);
632 r = omap_i2c_wait_for_bb(dev);
639 for (i = 0; i < num; i++) {
640 r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
651 omap_i2c_wait_for_bb(dev);
653 pm_runtime_mark_last_busy(dev->
dev);
654 pm_runtime_put_autosuspend(dev->
dev);
689 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
693 & OMAP_I2C_STAT_BB)) {
697 & OMAP_I2C_STAT_RDR) {
698 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
707 #ifdef CONFIG_ARCH_OMAP15XX
715 if (pm_runtime_suspended(dev->
dev))
731 omap_i2c_complete_cmd(dev, 0);
739 *dev->
buf++ = w >> 8;
743 dev_err(dev->
dev,
"RRDY IRQ while no data requested\n");
750 w |= *dev->
buf++ << 8;
755 dev_err(dev->
dev,
"XRDY IRQ while no data to send\n");
764 #define omap_i2c_omap1_isr NULL
774 unsigned long timeout = 10000;
787 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
793 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
803 dev_err(dev->
dev,
"timeout waiting on XUDF bit\n");
815 while (num_bytes--) {
825 *dev->
buf++ = w >> 8;
831 static int omap_i2c_transmit_data(
struct omap_i2c_dev *dev,
u8 num_bytes,
836 while (num_bytes--) {
845 w |= *dev->
buf++ << 8;
852 ret = errata_omap3_i462(dev);
864 omap_i2c_isr(
int irq,
void *dev_id)
871 spin_lock(&dev->
lock);
878 spin_unlock(&dev->
lock);
884 omap_i2c_isr_thread(
int this_irq,
void *dev_id)
909 dev_dbg(dev->
dev,
"IRQ (ISR = 0x%04x)\n", stat);
910 if (
count++ == 100) {
917 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
924 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_AL);
941 if (stat & OMAP_I2C_STAT_RDR) {
947 omap_i2c_receive_data(dev, num_bytes,
true);
950 i2c_omap_errata_i207(dev, stat);
952 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
962 omap_i2c_receive_data(dev, num_bytes,
false);
963 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RRDY);
974 ret = omap_i2c_transmit_data(dev, num_bytes,
true);
978 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XDR);
989 ret = omap_i2c_transmit_data(dev, num_bytes,
false);
993 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XRDY);
1000 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_ROVR);
1007 omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XUDF);
1012 omap_i2c_complete_cmd(dev, err);
1015 spin_unlock_irqrestore(&dev->
lock, flags);
1021 .master_xfer = omap_i2c_xfer,
1022 .functionality = omap_i2c_func,
1037 static const struct of_device_id omap_i2c_of_match[] = {
1040 .data = &omap4_pdata,
1043 .compatible =
"ti,omap3-i2c",
1044 .data = &omap3_pdata,
1058 pdev->
dev.platform_data;
1079 dev_err(&pdev->
dev,
"Menory allocation failed\n");
1085 dev_err(&pdev->
dev,
"I2C region already claimed\n");
1093 pdata = match->
data;
1097 of_property_read_u32(node,
"clock-frequency", &freq);
1099 dev->
speed = freq / 1000;
1100 }
else if (pdata !=
NULL) {
1112 platform_set_drvdata(pdev, dev);
1118 dev->
regs = (
u8 *)reg_map_ip_v2;
1120 dev->
regs = (
u8 *)reg_map_ip_v1;
1124 pm_runtime_use_autosuspend(dev->
dev);
1126 r = pm_runtime_get_sync(dev->
dev);
1161 (1000 * dev->
speed / 8);
1172 omap_i2c_isr, omap_i2c_isr_thread,
1178 goto err_unuse_clocks;
1182 i2c_set_adapdata(adap, dev);
1186 adap->
algo = &omap_i2c_algo;
1187 adap->
dev.parent = &pdev->
dev;
1188 adap->
dev.of_node = pdev->
dev.of_node;
1191 adap->
nr = pdev->
id;
1194 dev_err(dev->
dev,
"failure adding adapter\n");
1195 goto err_unuse_clocks;
1198 dev_info(dev->
dev,
"bus %d rev%d.%d.%d at %d kHz\n", adap->
nr,
1203 pm_runtime_mark_last_busy(dev->
dev);
1204 pm_runtime_put_autosuspend(dev->
dev);
1210 pm_runtime_put(dev->
dev);
1211 pm_runtime_disable(&pdev->
dev);
1213 platform_set_drvdata(pdev,
NULL);
1223 platform_set_drvdata(pdev,
NULL);
1226 ret = pm_runtime_get_sync(&pdev->
dev);
1231 pm_runtime_put(&pdev->
dev);
1232 pm_runtime_disable(&pdev->
dev);
1237 #ifdef CONFIG_PM_RUNTIME
1238 static int omap_i2c_runtime_suspend(
struct device *dev)
1241 struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
1260 static int omap_i2c_runtime_resume(
struct device *dev)
1263 struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
1289 omap_i2c_runtime_resume,
NULL)
1291 #define OMAP_I2C_PM_OPS (&omap_i2c_pm_ops)
1293 #define OMAP_I2C_PM_OPS NULL
1297 .probe = omap_i2c_probe,
1309 omap_i2c_init_driver(
void)
1315 static void __exit omap_i2c_exit_driver(
void)