i2c-omap.c 41 KB

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