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