i2c-omap.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. dev_err(omap->dev, "failure adding adapter\n");
  1209. goto err_unuse_clocks;
  1210. }
  1211. dev_info(omap->dev, "bus %d rev%d.%d at %d kHz\n", adap->nr,
  1212. major, minor, omap->speed);
  1213. pm_runtime_mark_last_busy(omap->dev);
  1214. pm_runtime_put_autosuspend(omap->dev);
  1215. return 0;
  1216. err_unuse_clocks:
  1217. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
  1218. pm_runtime_dont_use_autosuspend(omap->dev);
  1219. pm_runtime_put_sync(omap->dev);
  1220. pm_runtime_disable(&pdev->dev);
  1221. err_free_mem:
  1222. return r;
  1223. }
  1224. static int omap_i2c_remove(struct platform_device *pdev)
  1225. {
  1226. struct omap_i2c_dev *omap = platform_get_drvdata(pdev);
  1227. int ret;
  1228. i2c_del_adapter(&omap->adapter);
  1229. ret = pm_runtime_get_sync(&pdev->dev);
  1230. if (ret < 0)
  1231. return ret;
  1232. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
  1233. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1234. pm_runtime_put_sync(&pdev->dev);
  1235. pm_runtime_disable(&pdev->dev);
  1236. return 0;
  1237. }
  1238. #ifdef CONFIG_PM
  1239. static int omap_i2c_runtime_suspend(struct device *dev)
  1240. {
  1241. struct omap_i2c_dev *omap = dev_get_drvdata(dev);
  1242. omap->iestate = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  1243. if (omap->scheme == OMAP_I2C_SCHEME_0)
  1244. omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, 0);
  1245. else
  1246. omap_i2c_write_reg(omap, OMAP_I2C_IP_V2_IRQENABLE_CLR,
  1247. OMAP_I2C_IP_V2_INTERRUPTS_MASK);
  1248. if (omap->rev < OMAP_I2C_OMAP1_REV_2) {
  1249. omap_i2c_read_reg(omap, OMAP_I2C_IV_REG); /* Read clears */
  1250. } else {
  1251. omap_i2c_write_reg(omap, OMAP_I2C_STAT_REG, omap->iestate);
  1252. /* Flush posted write */
  1253. omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  1254. }
  1255. pinctrl_pm_select_sleep_state(dev);
  1256. return 0;
  1257. }
  1258. static int omap_i2c_runtime_resume(struct device *dev)
  1259. {
  1260. struct omap_i2c_dev *omap = dev_get_drvdata(dev);
  1261. pinctrl_pm_select_default_state(dev);
  1262. if (!omap->regs)
  1263. return 0;
  1264. __omap_i2c_init(omap);
  1265. return 0;
  1266. }
  1267. static struct dev_pm_ops omap_i2c_pm_ops = {
  1268. SET_RUNTIME_PM_OPS(omap_i2c_runtime_suspend,
  1269. omap_i2c_runtime_resume, NULL)
  1270. };
  1271. #define OMAP_I2C_PM_OPS (&omap_i2c_pm_ops)
  1272. #else
  1273. #define OMAP_I2C_PM_OPS NULL
  1274. #endif /* CONFIG_PM */
  1275. static struct platform_driver omap_i2c_driver = {
  1276. .probe = omap_i2c_probe,
  1277. .remove = omap_i2c_remove,
  1278. .driver = {
  1279. .name = "omap_i2c",
  1280. .pm = OMAP_I2C_PM_OPS,
  1281. .of_match_table = of_match_ptr(omap_i2c_of_match),
  1282. },
  1283. };
  1284. /* I2C may be needed to bring up other drivers */
  1285. static int __init
  1286. omap_i2c_init_driver(void)
  1287. {
  1288. return platform_driver_register(&omap_i2c_driver);
  1289. }
  1290. subsys_initcall(omap_i2c_init_driver);
  1291. static void __exit omap_i2c_exit_driver(void)
  1292. {
  1293. platform_driver_unregister(&omap_i2c_driver);
  1294. }
  1295. module_exit(omap_i2c_exit_driver);
  1296. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1297. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1298. MODULE_LICENSE("GPL");
  1299. MODULE_ALIAS("platform:omap_i2c");