i2c-omap.c 35 KB

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