i2c-omap.c 32 KB

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