i2c-omap.c 34 KB

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