i2c-omap.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*
  2. * TI OMAP I2C master mode driver
  3. *
  4. * Copyright (C) 2003 MontaVista Software, Inc.
  5. * Copyright (C) 2005 Nokia Corporation
  6. * Copyright (C) 2004 - 2007 Texas Instruments.
  7. *
  8. * Originally written by MontaVista Software, Inc.
  9. * Additional contributions by:
  10. * Tony Lindgren <tony@atomide.com>
  11. * Imre Deak <imre.deak@nokia.com>
  12. * Juha Yrjölä <juha.yrjola@solidboot.com>
  13. * Syed Khasim <x0khasim@ti.com>
  14. * Nishant Menon <nm@ti.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/i2c.h>
  33. #include <linux/err.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/completion.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/clk.h>
  38. #include <linux/io.h>
  39. #include <linux/of.h>
  40. #include <linux/of_i2c.h>
  41. #include <linux/of_device.h>
  42. #include <linux/slab.h>
  43. #include <linux/i2c-omap.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/pinctrl/consumer.h>
  46. /* I2C controller revisions */
  47. #define OMAP_I2C_OMAP1_REV_2 0x20
  48. /* I2C controller revisions present on specific hardware */
  49. #define OMAP_I2C_REV_ON_2430 0x00000036
  50. #define OMAP_I2C_REV_ON_3430_3530 0x0000003C
  51. #define OMAP_I2C_REV_ON_3630 0x00000040
  52. #define OMAP_I2C_REV_ON_4430_PLUS 0x50400002
  53. /* timeout waiting for the controller to respond */
  54. #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
  55. /* timeout for pm runtime autosuspend */
  56. #define OMAP_I2C_PM_TIMEOUT 1000 /* ms */
  57. /* For OMAP3 I2C_IV has changed to I2C_WE (wakeup enable) */
  58. enum {
  59. OMAP_I2C_REV_REG = 0,
  60. OMAP_I2C_IE_REG,
  61. OMAP_I2C_STAT_REG,
  62. OMAP_I2C_IV_REG,
  63. OMAP_I2C_WE_REG,
  64. OMAP_I2C_SYSS_REG,
  65. OMAP_I2C_BUF_REG,
  66. OMAP_I2C_CNT_REG,
  67. OMAP_I2C_DATA_REG,
  68. OMAP_I2C_SYSC_REG,
  69. OMAP_I2C_CON_REG,
  70. OMAP_I2C_OA_REG,
  71. OMAP_I2C_SA_REG,
  72. OMAP_I2C_PSC_REG,
  73. OMAP_I2C_SCLL_REG,
  74. OMAP_I2C_SCLH_REG,
  75. OMAP_I2C_SYSTEST_REG,
  76. OMAP_I2C_BUFSTAT_REG,
  77. /* only on OMAP4430 */
  78. OMAP_I2C_IP_V2_REVNB_LO,
  79. OMAP_I2C_IP_V2_REVNB_HI,
  80. OMAP_I2C_IP_V2_IRQSTATUS_RAW,
  81. OMAP_I2C_IP_V2_IRQENABLE_SET,
  82. OMAP_I2C_IP_V2_IRQENABLE_CLR,
  83. };
  84. /* I2C Interrupt Enable Register (OMAP_I2C_IE): */
  85. #define OMAP_I2C_IE_XDR (1 << 14) /* TX Buffer drain int enable */
  86. #define OMAP_I2C_IE_RDR (1 << 13) /* RX Buffer drain int enable */
  87. #define OMAP_I2C_IE_XRDY (1 << 4) /* TX data ready int enable */
  88. #define OMAP_I2C_IE_RRDY (1 << 3) /* RX data ready int enable */
  89. #define OMAP_I2C_IE_ARDY (1 << 2) /* Access ready int enable */
  90. #define OMAP_I2C_IE_NACK (1 << 1) /* No ack interrupt enable */
  91. #define OMAP_I2C_IE_AL (1 << 0) /* Arbitration lost int ena */
  92. /* I2C Status Register (OMAP_I2C_STAT): */
  93. #define OMAP_I2C_STAT_XDR (1 << 14) /* TX Buffer draining */
  94. #define OMAP_I2C_STAT_RDR (1 << 13) /* RX Buffer draining */
  95. #define OMAP_I2C_STAT_BB (1 << 12) /* Bus busy */
  96. #define OMAP_I2C_STAT_ROVR (1 << 11) /* Receive overrun */
  97. #define OMAP_I2C_STAT_XUDF (1 << 10) /* Transmit underflow */
  98. #define OMAP_I2C_STAT_AAS (1 << 9) /* Address as slave */
  99. #define OMAP_I2C_STAT_AD0 (1 << 8) /* Address zero */
  100. #define OMAP_I2C_STAT_XRDY (1 << 4) /* Transmit data ready */
  101. #define OMAP_I2C_STAT_RRDY (1 << 3) /* Receive data ready */
  102. #define OMAP_I2C_STAT_ARDY (1 << 2) /* Register access ready */
  103. #define OMAP_I2C_STAT_NACK (1 << 1) /* No ack interrupt enable */
  104. #define OMAP_I2C_STAT_AL (1 << 0) /* Arbitration lost int ena */
  105. /* I2C WE wakeup enable register */
  106. #define OMAP_I2C_WE_XDR_WE (1 << 14) /* TX drain wakup */
  107. #define OMAP_I2C_WE_RDR_WE (1 << 13) /* RX drain wakeup */
  108. #define OMAP_I2C_WE_AAS_WE (1 << 9) /* Address as slave wakeup*/
  109. #define OMAP_I2C_WE_BF_WE (1 << 8) /* Bus free wakeup */
  110. #define OMAP_I2C_WE_STC_WE (1 << 6) /* Start condition wakeup */
  111. #define OMAP_I2C_WE_GC_WE (1 << 5) /* General call wakeup */
  112. #define OMAP_I2C_WE_DRDY_WE (1 << 3) /* TX/RX data ready wakeup */
  113. #define OMAP_I2C_WE_ARDY_WE (1 << 2) /* Reg access ready wakeup */
  114. #define OMAP_I2C_WE_NACK_WE (1 << 1) /* No acknowledgment wakeup */
  115. #define OMAP_I2C_WE_AL_WE (1 << 0) /* Arbitration lost wakeup */
  116. #define OMAP_I2C_WE_ALL (OMAP_I2C_WE_XDR_WE | OMAP_I2C_WE_RDR_WE | \
  117. OMAP_I2C_WE_AAS_WE | OMAP_I2C_WE_BF_WE | \
  118. OMAP_I2C_WE_STC_WE | OMAP_I2C_WE_GC_WE | \
  119. OMAP_I2C_WE_DRDY_WE | OMAP_I2C_WE_ARDY_WE | \
  120. OMAP_I2C_WE_NACK_WE | OMAP_I2C_WE_AL_WE)
  121. /* I2C Buffer Configuration Register (OMAP_I2C_BUF): */
  122. #define OMAP_I2C_BUF_RDMA_EN (1 << 15) /* RX DMA channel enable */
  123. #define OMAP_I2C_BUF_RXFIF_CLR (1 << 14) /* RX FIFO Clear */
  124. #define OMAP_I2C_BUF_XDMA_EN (1 << 7) /* TX DMA channel enable */
  125. #define OMAP_I2C_BUF_TXFIF_CLR (1 << 6) /* TX FIFO Clear */
  126. /* I2C Configuration Register (OMAP_I2C_CON): */
  127. #define OMAP_I2C_CON_EN (1 << 15) /* I2C module enable */
  128. #define OMAP_I2C_CON_BE (1 << 14) /* Big endian mode */
  129. #define OMAP_I2C_CON_OPMODE_HS (1 << 12) /* High Speed support */
  130. #define OMAP_I2C_CON_STB (1 << 11) /* Start byte mode (master) */
  131. #define OMAP_I2C_CON_MST (1 << 10) /* Master/slave mode */
  132. #define OMAP_I2C_CON_TRX (1 << 9) /* TX/RX mode (master only) */
  133. #define OMAP_I2C_CON_XA (1 << 8) /* Expand address */
  134. #define OMAP_I2C_CON_RM (1 << 2) /* Repeat mode (master only) */
  135. #define OMAP_I2C_CON_STP (1 << 1) /* Stop cond (master only) */
  136. #define OMAP_I2C_CON_STT (1 << 0) /* Start condition (master) */
  137. /* I2C SCL time value when Master */
  138. #define OMAP_I2C_SCLL_HSSCLL 8
  139. #define OMAP_I2C_SCLH_HSSCLH 8
  140. /* I2C System Test Register (OMAP_I2C_SYSTEST): */
  141. #ifdef DEBUG
  142. #define OMAP_I2C_SYSTEST_ST_EN (1 << 15) /* System test enable */
  143. #define OMAP_I2C_SYSTEST_FREE (1 << 14) /* Free running mode */
  144. #define OMAP_I2C_SYSTEST_TMODE_MASK (3 << 12) /* Test mode select */
  145. #define OMAP_I2C_SYSTEST_TMODE_SHIFT (12) /* Test mode select */
  146. #define OMAP_I2C_SYSTEST_SCL_I (1 << 3) /* SCL line sense in */
  147. #define OMAP_I2C_SYSTEST_SCL_O (1 << 2) /* SCL line drive out */
  148. #define OMAP_I2C_SYSTEST_SDA_I (1 << 1) /* SDA line sense in */
  149. #define OMAP_I2C_SYSTEST_SDA_O (1 << 0) /* SDA line drive out */
  150. #endif
  151. /* OCP_SYSSTATUS bit definitions */
  152. #define SYSS_RESETDONE_MASK (1 << 0)
  153. /* OCP_SYSCONFIG bit definitions */
  154. #define SYSC_CLOCKACTIVITY_MASK (0x3 << 8)
  155. #define SYSC_SIDLEMODE_MASK (0x3 << 3)
  156. #define SYSC_ENAWAKEUP_MASK (1 << 2)
  157. #define SYSC_SOFTRESET_MASK (1 << 1)
  158. #define SYSC_AUTOIDLE_MASK (1 << 0)
  159. #define SYSC_IDLEMODE_SMART 0x2
  160. #define SYSC_CLOCKACTIVITY_FCLK 0x2
  161. /* Errata definitions */
  162. #define I2C_OMAP_ERRATA_I207 (1 << 0)
  163. #define I2C_OMAP_ERRATA_I462 (1 << 1)
  164. struct omap_i2c_dev {
  165. spinlock_t lock; /* IRQ synchronization */
  166. struct device *dev;
  167. void __iomem *base; /* virtual */
  168. int irq;
  169. int reg_shift; /* bit shift for I2C register addresses */
  170. struct completion cmd_complete;
  171. struct resource *ioarea;
  172. u32 latency; /* maximum mpu wkup latency */
  173. void (*set_mpu_wkup_lat)(struct device *dev,
  174. long latency);
  175. u32 speed; /* Speed of bus in kHz */
  176. u32 flags;
  177. u16 cmd_err;
  178. u8 *buf;
  179. u8 *regs;
  180. size_t buf_len;
  181. struct i2c_adapter adapter;
  182. u8 threshold;
  183. u8 fifo_size; /* use as flag and value
  184. * fifo_size==0 implies no fifo
  185. * if set, should be trsh+1
  186. */
  187. u32 rev;
  188. unsigned b_hw:1; /* bad h/w fixes */
  189. unsigned receiver:1; /* true when we're in receiver mode */
  190. u16 iestate; /* Saved interrupt register */
  191. u16 pscstate;
  192. u16 scllstate;
  193. u16 sclhstate;
  194. u16 syscstate;
  195. u16 westate;
  196. u16 errata;
  197. struct pinctrl *pins;
  198. };
  199. static const u8 reg_map_ip_v1[] = {
  200. [OMAP_I2C_REV_REG] = 0x00,
  201. [OMAP_I2C_IE_REG] = 0x01,
  202. [OMAP_I2C_STAT_REG] = 0x02,
  203. [OMAP_I2C_IV_REG] = 0x03,
  204. [OMAP_I2C_WE_REG] = 0x03,
  205. [OMAP_I2C_SYSS_REG] = 0x04,
  206. [OMAP_I2C_BUF_REG] = 0x05,
  207. [OMAP_I2C_CNT_REG] = 0x06,
  208. [OMAP_I2C_DATA_REG] = 0x07,
  209. [OMAP_I2C_SYSC_REG] = 0x08,
  210. [OMAP_I2C_CON_REG] = 0x09,
  211. [OMAP_I2C_OA_REG] = 0x0a,
  212. [OMAP_I2C_SA_REG] = 0x0b,
  213. [OMAP_I2C_PSC_REG] = 0x0c,
  214. [OMAP_I2C_SCLL_REG] = 0x0d,
  215. [OMAP_I2C_SCLH_REG] = 0x0e,
  216. [OMAP_I2C_SYSTEST_REG] = 0x0f,
  217. [OMAP_I2C_BUFSTAT_REG] = 0x10,
  218. };
  219. static const u8 reg_map_ip_v2[] = {
  220. [OMAP_I2C_REV_REG] = 0x04,
  221. [OMAP_I2C_IE_REG] = 0x2c,
  222. [OMAP_I2C_STAT_REG] = 0x28,
  223. [OMAP_I2C_IV_REG] = 0x34,
  224. [OMAP_I2C_WE_REG] = 0x34,
  225. [OMAP_I2C_SYSS_REG] = 0x90,
  226. [OMAP_I2C_BUF_REG] = 0x94,
  227. [OMAP_I2C_CNT_REG] = 0x98,
  228. [OMAP_I2C_DATA_REG] = 0x9c,
  229. [OMAP_I2C_SYSC_REG] = 0x10,
  230. [OMAP_I2C_CON_REG] = 0xa4,
  231. [OMAP_I2C_OA_REG] = 0xa8,
  232. [OMAP_I2C_SA_REG] = 0xac,
  233. [OMAP_I2C_PSC_REG] = 0xb0,
  234. [OMAP_I2C_SCLL_REG] = 0xb4,
  235. [OMAP_I2C_SCLH_REG] = 0xb8,
  236. [OMAP_I2C_SYSTEST_REG] = 0xbC,
  237. [OMAP_I2C_BUFSTAT_REG] = 0xc0,
  238. [OMAP_I2C_IP_V2_REVNB_LO] = 0x00,
  239. [OMAP_I2C_IP_V2_REVNB_HI] = 0x04,
  240. [OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24,
  241. [OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c,
  242. [OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30,
  243. };
  244. static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
  245. int reg, u16 val)
  246. {
  247. __raw_writew(val, i2c_dev->base +
  248. (i2c_dev->regs[reg] << i2c_dev->reg_shift));
  249. }
  250. static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
  251. {
  252. return __raw_readw(i2c_dev->base +
  253. (i2c_dev->regs[reg] << i2c_dev->reg_shift));
  254. }
  255. static void __omap_i2c_init(struct omap_i2c_dev *dev)
  256. {
  257. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  258. /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
  259. omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, dev->pscstate);
  260. /* SCL low and high time values */
  261. omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, dev->scllstate);
  262. omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, dev->sclhstate);
  263. if (dev->rev >= OMAP_I2C_REV_ON_3430_3530)
  264. omap_i2c_write_reg(dev, OMAP_I2C_WE_REG, dev->westate);
  265. /* Take the I2C module out of reset: */
  266. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  267. /*
  268. * Don't write to this register if the IE state is 0 as it can
  269. * cause deadlock.
  270. */
  271. if (dev->iestate)
  272. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
  273. }
  274. static int omap_i2c_reset(struct omap_i2c_dev *dev)
  275. {
  276. unsigned long timeout;
  277. u16 sysc;
  278. if (dev->rev >= OMAP_I2C_OMAP1_REV_2) {
  279. sysc = omap_i2c_read_reg(dev, OMAP_I2C_SYSC_REG);
  280. /* Disable I2C controller before soft reset */
  281. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
  282. omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
  283. ~(OMAP_I2C_CON_EN));
  284. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
  285. /* For some reason we need to set the EN bit before the
  286. * reset done bit gets set. */
  287. timeout = jiffies + OMAP_I2C_TIMEOUT;
  288. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  289. while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
  290. SYSS_RESETDONE_MASK)) {
  291. if (time_after(jiffies, timeout)) {
  292. dev_warn(dev->dev, "timeout waiting "
  293. "for controller reset\n");
  294. return -ETIMEDOUT;
  295. }
  296. msleep(1);
  297. }
  298. /* SYSC register is cleared by the reset; rewrite it */
  299. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, sysc);
  300. }
  301. return 0;
  302. }
  303. static int omap_i2c_init(struct omap_i2c_dev *dev)
  304. {
  305. u16 psc = 0, scll = 0, sclh = 0;
  306. u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
  307. unsigned long fclk_rate = 12000000;
  308. unsigned long internal_clk = 0;
  309. struct clk *fclk;
  310. if (dev->rev >= OMAP_I2C_REV_ON_3430_3530) {
  311. /*
  312. * Enabling all wakup sources to stop I2C freezing on
  313. * WFI instruction.
  314. * REVISIT: Some wkup sources might not be needed.
  315. */
  316. dev->westate = OMAP_I2C_WE_ALL;
  317. }
  318. if (dev->flags & OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK) {
  319. /*
  320. * The I2C functional clock is the armxor_ck, so there's
  321. * no need to get "armxor_ck" separately. Now, if OMAP2420
  322. * always returns 12MHz for the functional clock, we can
  323. * do this bit unconditionally.
  324. */
  325. fclk = clk_get(dev->dev, "fck");
  326. fclk_rate = clk_get_rate(fclk);
  327. clk_put(fclk);
  328. /* TRM for 5912 says the I2C clock must be prescaled to be
  329. * between 7 - 12 MHz. The XOR input clock is typically
  330. * 12, 13 or 19.2 MHz. So we should have code that produces:
  331. *
  332. * XOR MHz Divider Prescaler
  333. * 12 1 0
  334. * 13 2 1
  335. * 19.2 2 1
  336. */
  337. if (fclk_rate > 12000000)
  338. psc = fclk_rate / 12000000;
  339. }
  340. if (!(dev->flags & OMAP_I2C_FLAG_SIMPLE_CLOCK)) {
  341. /*
  342. * HSI2C controller internal clk rate should be 19.2 Mhz for
  343. * HS and for all modes on 2430. On 34xx we can use lower rate
  344. * to get longer filter period for better noise suppression.
  345. * The filter is iclk (fclk for HS) period.
  346. */
  347. if (dev->speed > 400 ||
  348. dev->flags & OMAP_I2C_FLAG_FORCE_19200_INT_CLK)
  349. internal_clk = 19200;
  350. else if (dev->speed > 100)
  351. internal_clk = 9600;
  352. else
  353. internal_clk = 4000;
  354. fclk = clk_get(dev->dev, "fck");
  355. fclk_rate = clk_get_rate(fclk) / 1000;
  356. clk_put(fclk);
  357. /* Compute prescaler divisor */
  358. psc = fclk_rate / internal_clk;
  359. psc = psc - 1;
  360. /* If configured for High Speed */
  361. if (dev->speed > 400) {
  362. unsigned long scl;
  363. /* For first phase of HS mode */
  364. scl = internal_clk / 400;
  365. fsscll = scl - (scl / 3) - 7;
  366. fssclh = (scl / 3) - 5;
  367. /* For second phase of HS mode */
  368. scl = fclk_rate / dev->speed;
  369. hsscll = scl - (scl / 3) - 7;
  370. hssclh = (scl / 3) - 5;
  371. } else if (dev->speed > 100) {
  372. unsigned long scl;
  373. /* Fast mode */
  374. scl = internal_clk / dev->speed;
  375. fsscll = scl - (scl / 3) - 7;
  376. fssclh = (scl / 3) - 5;
  377. } else {
  378. /* Standard mode */
  379. fsscll = internal_clk / (dev->speed * 2) - 7;
  380. fssclh = internal_clk / (dev->speed * 2) - 5;
  381. }
  382. scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
  383. sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
  384. } else {
  385. /* Program desired operating rate */
  386. fclk_rate /= (psc + 1) * 1000;
  387. if (psc > 2)
  388. psc = 2;
  389. scll = fclk_rate / (dev->speed * 2) - 7 + psc;
  390. sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
  391. }
  392. dev->iestate = (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
  393. OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
  394. OMAP_I2C_IE_AL) | ((dev->fifo_size) ?
  395. (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0);
  396. dev->pscstate = psc;
  397. dev->scllstate = scll;
  398. dev->sclhstate = sclh;
  399. __omap_i2c_init(dev);
  400. return 0;
  401. }
  402. /*
  403. * Waiting on Bus Busy
  404. */
  405. static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
  406. {
  407. unsigned long timeout;
  408. timeout = jiffies + OMAP_I2C_TIMEOUT;
  409. while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
  410. if (time_after(jiffies, timeout)) {
  411. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  412. return -ETIMEDOUT;
  413. }
  414. msleep(1);
  415. }
  416. return 0;
  417. }
  418. static void omap_i2c_resize_fifo(struct omap_i2c_dev *dev, u8 size, bool is_rx)
  419. {
  420. u16 buf;
  421. if (dev->flags & OMAP_I2C_FLAG_NO_FIFO)
  422. return;
  423. /*
  424. * Set up notification threshold based on message size. We're doing
  425. * this to try and avoid draining feature as much as possible. Whenever
  426. * we have big messages to transfer (bigger than our total fifo size)
  427. * then we might use draining feature to transfer the remaining bytes.
  428. */
  429. dev->threshold = clamp(size, (u8) 1, dev->fifo_size);
  430. buf = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
  431. if (is_rx) {
  432. /* Clear RX Threshold */
  433. buf &= ~(0x3f << 8);
  434. buf |= ((dev->threshold - 1) << 8) | OMAP_I2C_BUF_RXFIF_CLR;
  435. } else {
  436. /* Clear TX Threshold */
  437. buf &= ~0x3f;
  438. buf |= (dev->threshold - 1) | OMAP_I2C_BUF_TXFIF_CLR;
  439. }
  440. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, buf);
  441. if (dev->rev < OMAP_I2C_REV_ON_3630)
  442. dev->b_hw = 1; /* Enable hardware fixes */
  443. /* calculate wakeup latency constraint for MPU */
  444. if (dev->set_mpu_wkup_lat != NULL)
  445. dev->latency = (1000000 * dev->threshold) /
  446. (1000 * dev->speed / 8);
  447. }
  448. /*
  449. * Low level master read/write transaction.
  450. */
  451. static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
  452. struct i2c_msg *msg, int stop)
  453. {
  454. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  455. unsigned long timeout;
  456. u16 w;
  457. dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  458. msg->addr, msg->len, msg->flags, stop);
  459. if (msg->len == 0)
  460. return -EINVAL;
  461. dev->receiver = !!(msg->flags & I2C_M_RD);
  462. omap_i2c_resize_fifo(dev, msg->len, dev->receiver);
  463. omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
  464. /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
  465. dev->buf = msg->buf;
  466. dev->buf_len = msg->len;
  467. /* make sure writes to dev->buf_len are ordered */
  468. barrier();
  469. omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
  470. /* Clear the FIFO Buffers */
  471. w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
  472. w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
  473. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
  474. INIT_COMPLETION(dev->cmd_complete);
  475. dev->cmd_err = 0;
  476. w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
  477. /* High speed configuration */
  478. if (dev->speed > 400)
  479. w |= OMAP_I2C_CON_OPMODE_HS;
  480. if (msg->flags & I2C_M_STOP)
  481. stop = 1;
  482. if (msg->flags & I2C_M_TEN)
  483. w |= OMAP_I2C_CON_XA;
  484. if (!(msg->flags & I2C_M_RD))
  485. w |= OMAP_I2C_CON_TRX;
  486. if (!dev->b_hw && stop)
  487. w |= OMAP_I2C_CON_STP;
  488. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  489. /*
  490. * Don't write stt and stp together on some hardware.
  491. */
  492. if (dev->b_hw && stop) {
  493. unsigned long delay = jiffies + OMAP_I2C_TIMEOUT;
  494. u16 con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  495. while (con & OMAP_I2C_CON_STT) {
  496. con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  497. /* Let the user know if i2c is in a bad state */
  498. if (time_after(jiffies, delay)) {
  499. dev_err(dev->dev, "controller timed out "
  500. "waiting for start condition to finish\n");
  501. return -ETIMEDOUT;
  502. }
  503. cpu_relax();
  504. }
  505. w |= OMAP_I2C_CON_STP;
  506. w &= ~OMAP_I2C_CON_STT;
  507. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  508. }
  509. /*
  510. * REVISIT: We should abort the transfer on signals, but the bus goes
  511. * into arbitration and we're currently unable to recover from it.
  512. */
  513. timeout = wait_for_completion_timeout(&dev->cmd_complete,
  514. OMAP_I2C_TIMEOUT);
  515. if (timeout == 0) {
  516. dev_err(dev->dev, "controller timed out\n");
  517. omap_i2c_reset(dev);
  518. __omap_i2c_init(dev);
  519. return -ETIMEDOUT;
  520. }
  521. if (likely(!dev->cmd_err))
  522. return 0;
  523. /* We have an error */
  524. if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
  525. OMAP_I2C_STAT_XUDF)) {
  526. omap_i2c_reset(dev);
  527. __omap_i2c_init(dev);
  528. return -EIO;
  529. }
  530. if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
  531. if (msg->flags & I2C_M_IGNORE_NAK)
  532. return 0;
  533. if (stop) {
  534. w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  535. w |= OMAP_I2C_CON_STP;
  536. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  537. }
  538. return -EREMOTEIO;
  539. }
  540. return -EIO;
  541. }
  542. /*
  543. * Prepare controller for a transaction and call omap_i2c_xfer_msg
  544. * to do the work during IRQ processing.
  545. */
  546. static int
  547. omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  548. {
  549. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  550. int i;
  551. int r;
  552. r = pm_runtime_get_sync(dev->dev);
  553. if (IS_ERR_VALUE(r))
  554. goto out;
  555. r = omap_i2c_wait_for_bb(dev);
  556. if (r < 0)
  557. goto out;
  558. if (dev->set_mpu_wkup_lat != NULL)
  559. dev->set_mpu_wkup_lat(dev->dev, dev->latency);
  560. for (i = 0; i < num; i++) {
  561. r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  562. if (r != 0)
  563. break;
  564. }
  565. if (r == 0)
  566. r = num;
  567. omap_i2c_wait_for_bb(dev);
  568. if (dev->set_mpu_wkup_lat != NULL)
  569. dev->set_mpu_wkup_lat(dev->dev, -1);
  570. out:
  571. pm_runtime_mark_last_busy(dev->dev);
  572. pm_runtime_put_autosuspend(dev->dev);
  573. return r;
  574. }
  575. static u32
  576. omap_i2c_func(struct i2c_adapter *adap)
  577. {
  578. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  579. I2C_FUNC_PROTOCOL_MANGLING;
  580. }
  581. static inline void
  582. omap_i2c_complete_cmd(struct omap_i2c_dev *dev, u16 err)
  583. {
  584. dev->cmd_err |= err;
  585. complete(&dev->cmd_complete);
  586. }
  587. static inline void
  588. omap_i2c_ack_stat(struct omap_i2c_dev *dev, u16 stat)
  589. {
  590. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
  591. }
  592. static inline void i2c_omap_errata_i207(struct omap_i2c_dev *dev, u16 stat)
  593. {
  594. /*
  595. * I2C Errata(Errata Nos. OMAP2: 1.67, OMAP3: 1.8)
  596. * Not applicable for OMAP4.
  597. * Under certain rare conditions, RDR could be set again
  598. * when the bus is busy, then ignore the interrupt and
  599. * clear the interrupt.
  600. */
  601. if (stat & OMAP_I2C_STAT_RDR) {
  602. /* Step 1: If RDR is set, clear it */
  603. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  604. /* Step 2: */
  605. if (!(omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  606. & OMAP_I2C_STAT_BB)) {
  607. /* Step 3: */
  608. if (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  609. & OMAP_I2C_STAT_RDR) {
  610. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  611. dev_dbg(dev->dev, "RDR when bus is busy.\n");
  612. }
  613. }
  614. }
  615. }
  616. /* rev1 devices are apparently only on some 15xx */
  617. #ifdef CONFIG_ARCH_OMAP15XX
  618. static irqreturn_t
  619. omap_i2c_omap1_isr(int this_irq, void *dev_id)
  620. {
  621. struct omap_i2c_dev *dev = dev_id;
  622. u16 iv, w;
  623. if (pm_runtime_suspended(dev->dev))
  624. return IRQ_NONE;
  625. iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
  626. switch (iv) {
  627. case 0x00: /* None */
  628. break;
  629. case 0x01: /* Arbitration lost */
  630. dev_err(dev->dev, "Arbitration lost\n");
  631. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
  632. break;
  633. case 0x02: /* No acknowledgement */
  634. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
  635. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
  636. break;
  637. case 0x03: /* Register access ready */
  638. omap_i2c_complete_cmd(dev, 0);
  639. break;
  640. case 0x04: /* Receive data ready */
  641. if (dev->buf_len) {
  642. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  643. *dev->buf++ = w;
  644. dev->buf_len--;
  645. if (dev->buf_len) {
  646. *dev->buf++ = w >> 8;
  647. dev->buf_len--;
  648. }
  649. } else
  650. dev_err(dev->dev, "RRDY IRQ while no data requested\n");
  651. break;
  652. case 0x05: /* Transmit data ready */
  653. if (dev->buf_len) {
  654. w = *dev->buf++;
  655. dev->buf_len--;
  656. if (dev->buf_len) {
  657. w |= *dev->buf++ << 8;
  658. dev->buf_len--;
  659. }
  660. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  661. } else
  662. dev_err(dev->dev, "XRDY IRQ while no data to send\n");
  663. break;
  664. default:
  665. return IRQ_NONE;
  666. }
  667. return IRQ_HANDLED;
  668. }
  669. #else
  670. #define omap_i2c_omap1_isr NULL
  671. #endif
  672. /*
  673. * OMAP3430 Errata i462: When an XRDY/XDR is hit, wait for XUDF before writing
  674. * data to DATA_REG. Otherwise some data bytes can be lost while transferring
  675. * them from the memory to the I2C interface.
  676. */
  677. static int errata_omap3_i462(struct omap_i2c_dev *dev)
  678. {
  679. unsigned long timeout = 10000;
  680. u16 stat;
  681. do {
  682. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  683. if (stat & OMAP_I2C_STAT_XUDF)
  684. break;
  685. if (stat & (OMAP_I2C_STAT_NACK | OMAP_I2C_STAT_AL)) {
  686. omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_XRDY |
  687. OMAP_I2C_STAT_XDR));
  688. if (stat & OMAP_I2C_STAT_NACK) {
  689. dev->cmd_err |= OMAP_I2C_STAT_NACK;
  690. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
  691. }
  692. if (stat & OMAP_I2C_STAT_AL) {
  693. dev_err(dev->dev, "Arbitration lost\n");
  694. dev->cmd_err |= OMAP_I2C_STAT_AL;
  695. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_AL);
  696. }
  697. return -EIO;
  698. }
  699. cpu_relax();
  700. } while (--timeout);
  701. if (!timeout) {
  702. dev_err(dev->dev, "timeout waiting on XUDF bit\n");
  703. return 0;
  704. }
  705. return 0;
  706. }
  707. static void omap_i2c_receive_data(struct omap_i2c_dev *dev, u8 num_bytes,
  708. bool is_rdr)
  709. {
  710. u16 w;
  711. while (num_bytes--) {
  712. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  713. *dev->buf++ = w;
  714. dev->buf_len--;
  715. /*
  716. * Data reg in 2430, omap3 and
  717. * omap4 is 8 bit wide
  718. */
  719. if (dev->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  720. *dev->buf++ = w >> 8;
  721. dev->buf_len--;
  722. }
  723. }
  724. }
  725. static int omap_i2c_transmit_data(struct omap_i2c_dev *dev, u8 num_bytes,
  726. bool is_xdr)
  727. {
  728. u16 w;
  729. while (num_bytes--) {
  730. w = *dev->buf++;
  731. dev->buf_len--;
  732. /*
  733. * Data reg in 2430, omap3 and
  734. * omap4 is 8 bit wide
  735. */
  736. if (dev->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  737. w |= *dev->buf++ << 8;
  738. dev->buf_len--;
  739. }
  740. if (dev->errata & I2C_OMAP_ERRATA_I462) {
  741. int ret;
  742. ret = errata_omap3_i462(dev);
  743. if (ret < 0)
  744. return ret;
  745. }
  746. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  747. }
  748. return 0;
  749. }
  750. static irqreturn_t
  751. omap_i2c_isr(int irq, void *dev_id)
  752. {
  753. struct omap_i2c_dev *dev = dev_id;
  754. irqreturn_t ret = IRQ_HANDLED;
  755. u16 mask;
  756. u16 stat;
  757. spin_lock(&dev->lock);
  758. mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  759. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  760. if (stat & mask)
  761. ret = IRQ_WAKE_THREAD;
  762. spin_unlock(&dev->lock);
  763. return ret;
  764. }
  765. static irqreturn_t
  766. omap_i2c_isr_thread(int this_irq, void *dev_id)
  767. {
  768. struct omap_i2c_dev *dev = dev_id;
  769. unsigned long flags;
  770. u16 bits;
  771. u16 stat;
  772. int err = 0, count = 0;
  773. spin_lock_irqsave(&dev->lock, flags);
  774. do {
  775. bits = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  776. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  777. stat &= bits;
  778. /* If we're in receiver mode, ignore XDR/XRDY */
  779. if (dev->receiver)
  780. stat &= ~(OMAP_I2C_STAT_XDR | OMAP_I2C_STAT_XRDY);
  781. else
  782. stat &= ~(OMAP_I2C_STAT_RDR | OMAP_I2C_STAT_RRDY);
  783. if (!stat) {
  784. /* my work here is done */
  785. goto out;
  786. }
  787. dev_dbg(dev->dev, "IRQ (ISR = 0x%04x)\n", stat);
  788. if (count++ == 100) {
  789. dev_warn(dev->dev, "Too much work in one IRQ\n");
  790. break;
  791. }
  792. if (stat & OMAP_I2C_STAT_NACK) {
  793. err |= OMAP_I2C_STAT_NACK;
  794. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
  795. break;
  796. }
  797. if (stat & OMAP_I2C_STAT_AL) {
  798. dev_err(dev->dev, "Arbitration lost\n");
  799. err |= OMAP_I2C_STAT_AL;
  800. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_AL);
  801. break;
  802. }
  803. /*
  804. * ProDB0017052: Clear ARDY bit twice
  805. */
  806. if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
  807. OMAP_I2C_STAT_AL)) {
  808. omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_RRDY |
  809. OMAP_I2C_STAT_RDR |
  810. OMAP_I2C_STAT_XRDY |
  811. OMAP_I2C_STAT_XDR |
  812. OMAP_I2C_STAT_ARDY));
  813. break;
  814. }
  815. if (stat & OMAP_I2C_STAT_RDR) {
  816. u8 num_bytes = 1;
  817. if (dev->fifo_size)
  818. num_bytes = dev->buf_len;
  819. omap_i2c_receive_data(dev, num_bytes, true);
  820. if (dev->errata & I2C_OMAP_ERRATA_I207)
  821. i2c_omap_errata_i207(dev, stat);
  822. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  823. continue;
  824. }
  825. if (stat & OMAP_I2C_STAT_RRDY) {
  826. u8 num_bytes = 1;
  827. if (dev->threshold)
  828. num_bytes = dev->threshold;
  829. omap_i2c_receive_data(dev, num_bytes, false);
  830. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RRDY);
  831. continue;
  832. }
  833. if (stat & OMAP_I2C_STAT_XDR) {
  834. u8 num_bytes = 1;
  835. int ret;
  836. if (dev->fifo_size)
  837. num_bytes = dev->buf_len;
  838. ret = omap_i2c_transmit_data(dev, num_bytes, true);
  839. if (ret < 0)
  840. break;
  841. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XDR);
  842. continue;
  843. }
  844. if (stat & OMAP_I2C_STAT_XRDY) {
  845. u8 num_bytes = 1;
  846. int ret;
  847. if (dev->threshold)
  848. num_bytes = dev->threshold;
  849. ret = omap_i2c_transmit_data(dev, num_bytes, false);
  850. if (ret < 0)
  851. break;
  852. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XRDY);
  853. continue;
  854. }
  855. if (stat & OMAP_I2C_STAT_ROVR) {
  856. dev_err(dev->dev, "Receive overrun\n");
  857. err |= OMAP_I2C_STAT_ROVR;
  858. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_ROVR);
  859. break;
  860. }
  861. if (stat & OMAP_I2C_STAT_XUDF) {
  862. dev_err(dev->dev, "Transmit underflow\n");
  863. err |= OMAP_I2C_STAT_XUDF;
  864. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XUDF);
  865. break;
  866. }
  867. } while (stat);
  868. omap_i2c_complete_cmd(dev, err);
  869. out:
  870. spin_unlock_irqrestore(&dev->lock, flags);
  871. return IRQ_HANDLED;
  872. }
  873. static const struct i2c_algorithm omap_i2c_algo = {
  874. .master_xfer = omap_i2c_xfer,
  875. .functionality = omap_i2c_func,
  876. };
  877. #ifdef CONFIG_OF
  878. static struct omap_i2c_bus_platform_data omap3_pdata = {
  879. .rev = OMAP_I2C_IP_VERSION_1,
  880. .flags = OMAP_I2C_FLAG_BUS_SHIFT_2,
  881. };
  882. static struct omap_i2c_bus_platform_data omap4_pdata = {
  883. .rev = OMAP_I2C_IP_VERSION_2,
  884. };
  885. static const struct of_device_id omap_i2c_of_match[] = {
  886. {
  887. .compatible = "ti,omap4-i2c",
  888. .data = &omap4_pdata,
  889. },
  890. {
  891. .compatible = "ti,omap3-i2c",
  892. .data = &omap3_pdata,
  893. },
  894. { },
  895. };
  896. MODULE_DEVICE_TABLE(of, omap_i2c_of_match);
  897. #endif
  898. #define OMAP_I2C_SCHEME(rev) ((rev & 0xc000) >> 14)
  899. #define OMAP_I2C_REV_SCHEME_0_MAJOR(rev) (rev >> 4)
  900. #define OMAP_I2C_REV_SCHEME_0_MINOR(rev) (rev & 0xf)
  901. #define OMAP_I2C_REV_SCHEME_1_MAJOR(rev) ((rev & 0x0700) >> 7)
  902. #define OMAP_I2C_REV_SCHEME_1_MINOR(rev) (rev & 0x1f)
  903. #define OMAP_I2C_SCHEME_0 0
  904. #define OMAP_I2C_SCHEME_1 1
  905. static int
  906. omap_i2c_probe(struct platform_device *pdev)
  907. {
  908. struct omap_i2c_dev *dev;
  909. struct i2c_adapter *adap;
  910. struct resource *mem;
  911. const struct omap_i2c_bus_platform_data *pdata =
  912. pdev->dev.platform_data;
  913. struct device_node *node = pdev->dev.of_node;
  914. const struct of_device_id *match;
  915. int irq;
  916. int r;
  917. u32 rev;
  918. u16 minor, major, scheme;
  919. /* NOTE: driver uses the static register mapping */
  920. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  921. if (!mem) {
  922. dev_err(&pdev->dev, "no mem resource?\n");
  923. return -ENODEV;
  924. }
  925. irq = platform_get_irq(pdev, 0);
  926. if (irq < 0) {
  927. dev_err(&pdev->dev, "no irq resource?\n");
  928. return irq;
  929. }
  930. dev = devm_kzalloc(&pdev->dev, sizeof(struct omap_i2c_dev), GFP_KERNEL);
  931. if (!dev) {
  932. dev_err(&pdev->dev, "Menory allocation failed\n");
  933. return -ENOMEM;
  934. }
  935. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  936. if (IS_ERR(dev->base))
  937. return PTR_ERR(dev->base);
  938. match = of_match_device(of_match_ptr(omap_i2c_of_match), &pdev->dev);
  939. if (match) {
  940. u32 freq = 100000; /* default to 100000 Hz */
  941. pdata = match->data;
  942. dev->flags = pdata->flags;
  943. of_property_read_u32(node, "clock-frequency", &freq);
  944. /* convert DT freq value in Hz into kHz for speed */
  945. dev->speed = freq / 1000;
  946. } else if (pdata != NULL) {
  947. dev->speed = pdata->clkrate;
  948. dev->flags = pdata->flags;
  949. dev->set_mpu_wkup_lat = pdata->set_mpu_wkup_lat;
  950. }
  951. dev->pins = devm_pinctrl_get_select_default(&pdev->dev);
  952. if (IS_ERR(dev->pins)) {
  953. if (PTR_ERR(dev->pins) == -EPROBE_DEFER)
  954. return -EPROBE_DEFER;
  955. dev_warn(&pdev->dev, "did not get pins for i2c error: %li\n",
  956. PTR_ERR(dev->pins));
  957. dev->pins = NULL;
  958. }
  959. dev->dev = &pdev->dev;
  960. dev->irq = irq;
  961. spin_lock_init(&dev->lock);
  962. platform_set_drvdata(pdev, dev);
  963. init_completion(&dev->cmd_complete);
  964. dev->reg_shift = (dev->flags >> OMAP_I2C_FLAG_BUS_SHIFT__SHIFT) & 3;
  965. pm_runtime_enable(dev->dev);
  966. pm_runtime_set_autosuspend_delay(dev->dev, OMAP_I2C_PM_TIMEOUT);
  967. pm_runtime_use_autosuspend(dev->dev);
  968. r = pm_runtime_get_sync(dev->dev);
  969. if (IS_ERR_VALUE(r))
  970. goto err_free_mem;
  971. /*
  972. * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2.
  973. * On omap1/3/2 Offset 4 is IE Reg the bit [15:14] is 0 at reset.
  974. * Also since the omap_i2c_read_reg uses reg_map_ip_* a
  975. * raw_readw is done.
  976. */
  977. rev = __raw_readw(dev->base + 0x04);
  978. scheme = OMAP_I2C_SCHEME(rev);
  979. switch (scheme) {
  980. case OMAP_I2C_SCHEME_0:
  981. dev->regs = (u8 *)reg_map_ip_v1;
  982. dev->rev = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG);
  983. minor = OMAP_I2C_REV_SCHEME_0_MAJOR(dev->rev);
  984. major = OMAP_I2C_REV_SCHEME_0_MAJOR(dev->rev);
  985. break;
  986. case OMAP_I2C_SCHEME_1:
  987. /* FALLTHROUGH */
  988. default:
  989. dev->regs = (u8 *)reg_map_ip_v2;
  990. rev = (rev << 16) |
  991. omap_i2c_read_reg(dev, OMAP_I2C_IP_V2_REVNB_LO);
  992. minor = OMAP_I2C_REV_SCHEME_1_MINOR(rev);
  993. major = OMAP_I2C_REV_SCHEME_1_MAJOR(rev);
  994. dev->rev = rev;
  995. }
  996. dev->errata = 0;
  997. if (dev->rev >= OMAP_I2C_REV_ON_2430 &&
  998. dev->rev < OMAP_I2C_REV_ON_4430_PLUS)
  999. dev->errata |= I2C_OMAP_ERRATA_I207;
  1000. if (dev->rev <= OMAP_I2C_REV_ON_3430_3530)
  1001. dev->errata |= I2C_OMAP_ERRATA_I462;
  1002. if (!(dev->flags & OMAP_I2C_FLAG_NO_FIFO)) {
  1003. u16 s;
  1004. /* Set up the fifo size - Get total size */
  1005. s = (omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3;
  1006. dev->fifo_size = 0x8 << s;
  1007. /*
  1008. * Set up notification threshold as half the total available
  1009. * size. This is to ensure that we can handle the status on int
  1010. * call back latencies.
  1011. */
  1012. dev->fifo_size = (dev->fifo_size / 2);
  1013. if (dev->rev < OMAP_I2C_REV_ON_3630)
  1014. dev->b_hw = 1; /* Enable hardware fixes */
  1015. /* calculate wakeup latency constraint for MPU */
  1016. if (dev->set_mpu_wkup_lat != NULL)
  1017. dev->latency = (1000000 * dev->fifo_size) /
  1018. (1000 * dev->speed / 8);
  1019. }
  1020. /* reset ASAP, clearing any IRQs */
  1021. omap_i2c_init(dev);
  1022. if (dev->rev < OMAP_I2C_OMAP1_REV_2)
  1023. r = devm_request_irq(&pdev->dev, dev->irq, omap_i2c_omap1_isr,
  1024. IRQF_NO_SUSPEND, pdev->name, dev);
  1025. else
  1026. r = devm_request_threaded_irq(&pdev->dev, dev->irq,
  1027. omap_i2c_isr, omap_i2c_isr_thread,
  1028. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1029. pdev->name, dev);
  1030. if (r) {
  1031. dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
  1032. goto err_unuse_clocks;
  1033. }
  1034. adap = &dev->adapter;
  1035. i2c_set_adapdata(adap, dev);
  1036. adap->owner = THIS_MODULE;
  1037. adap->class = I2C_CLASS_HWMON;
  1038. strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  1039. adap->algo = &omap_i2c_algo;
  1040. adap->dev.parent = &pdev->dev;
  1041. adap->dev.of_node = pdev->dev.of_node;
  1042. /* i2c device drivers may be active on return from add_adapter() */
  1043. adap->nr = pdev->id;
  1044. r = i2c_add_numbered_adapter(adap);
  1045. if (r) {
  1046. dev_err(dev->dev, "failure adding adapter\n");
  1047. goto err_unuse_clocks;
  1048. }
  1049. dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n", adap->nr,
  1050. major, minor, dev->speed);
  1051. of_i2c_register_devices(adap);
  1052. pm_runtime_mark_last_busy(dev->dev);
  1053. pm_runtime_put_autosuspend(dev->dev);
  1054. return 0;
  1055. err_unuse_clocks:
  1056. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  1057. pm_runtime_put(dev->dev);
  1058. pm_runtime_disable(&pdev->dev);
  1059. err_free_mem:
  1060. return r;
  1061. }
  1062. static int omap_i2c_remove(struct platform_device *pdev)
  1063. {
  1064. struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
  1065. int ret;
  1066. i2c_del_adapter(&dev->adapter);
  1067. ret = pm_runtime_get_sync(&pdev->dev);
  1068. if (IS_ERR_VALUE(ret))
  1069. return ret;
  1070. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  1071. pm_runtime_put(&pdev->dev);
  1072. pm_runtime_disable(&pdev->dev);
  1073. return 0;
  1074. }
  1075. #ifdef CONFIG_PM
  1076. #ifdef CONFIG_PM_RUNTIME
  1077. static int omap_i2c_runtime_suspend(struct device *dev)
  1078. {
  1079. struct platform_device *pdev = to_platform_device(dev);
  1080. struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
  1081. _dev->iestate = omap_i2c_read_reg(_dev, OMAP_I2C_IE_REG);
  1082. omap_i2c_write_reg(_dev, OMAP_I2C_IE_REG, 0);
  1083. if (_dev->rev < OMAP_I2C_OMAP1_REV_2) {
  1084. omap_i2c_read_reg(_dev, OMAP_I2C_IV_REG); /* Read clears */
  1085. } else {
  1086. omap_i2c_write_reg(_dev, OMAP_I2C_STAT_REG, _dev->iestate);
  1087. /* Flush posted write */
  1088. omap_i2c_read_reg(_dev, OMAP_I2C_STAT_REG);
  1089. }
  1090. return 0;
  1091. }
  1092. static int omap_i2c_runtime_resume(struct device *dev)
  1093. {
  1094. struct platform_device *pdev = to_platform_device(dev);
  1095. struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
  1096. if (!_dev->regs)
  1097. return 0;
  1098. __omap_i2c_init(_dev);
  1099. return 0;
  1100. }
  1101. #endif /* CONFIG_PM_RUNTIME */
  1102. static struct dev_pm_ops omap_i2c_pm_ops = {
  1103. SET_RUNTIME_PM_OPS(omap_i2c_runtime_suspend,
  1104. omap_i2c_runtime_resume, NULL)
  1105. };
  1106. #define OMAP_I2C_PM_OPS (&omap_i2c_pm_ops)
  1107. #else
  1108. #define OMAP_I2C_PM_OPS NULL
  1109. #endif /* CONFIG_PM */
  1110. static struct platform_driver omap_i2c_driver = {
  1111. .probe = omap_i2c_probe,
  1112. .remove = omap_i2c_remove,
  1113. .driver = {
  1114. .name = "omap_i2c",
  1115. .owner = THIS_MODULE,
  1116. .pm = OMAP_I2C_PM_OPS,
  1117. .of_match_table = of_match_ptr(omap_i2c_of_match),
  1118. },
  1119. };
  1120. /* I2C may be needed to bring up other drivers */
  1121. static int __init
  1122. omap_i2c_init_driver(void)
  1123. {
  1124. return platform_driver_register(&omap_i2c_driver);
  1125. }
  1126. subsys_initcall(omap_i2c_init_driver);
  1127. static void __exit omap_i2c_exit_driver(void)
  1128. {
  1129. platform_driver_unregister(&omap_i2c_driver);
  1130. }
  1131. module_exit(omap_i2c_exit_driver);
  1132. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1133. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1134. MODULE_LICENSE("GPL");
  1135. MODULE_ALIAS("platform:omap_i2c");