i2c-omap.c 35 KB

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