i2c-omap.c 41 KB

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