i2c-omap.c 33 KB

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