i2c-omap.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. if (dev->rev < OMAP_I2C_REV_ON_4430)
  340. omap_i2c_write_reg(dev, OMAP_I2C_WE_REG,
  341. dev->westate);
  342. }
  343. }
  344. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  345. if (cpu_class_is_omap1()) {
  346. /*
  347. * The I2C functional clock is the armxor_ck, so there's
  348. * no need to get "armxor_ck" separately. Now, if OMAP2420
  349. * always returns 12MHz for the functional clock, we can
  350. * do this bit unconditionally.
  351. */
  352. fclk = clk_get(dev->dev, "fck");
  353. fclk_rate = clk_get_rate(fclk);
  354. clk_put(fclk);
  355. /* TRM for 5912 says the I2C clock must be prescaled to be
  356. * between 7 - 12 MHz. The XOR input clock is typically
  357. * 12, 13 or 19.2 MHz. So we should have code that produces:
  358. *
  359. * XOR MHz Divider Prescaler
  360. * 12 1 0
  361. * 13 2 1
  362. * 19.2 2 1
  363. */
  364. if (fclk_rate > 12000000)
  365. psc = fclk_rate / 12000000;
  366. }
  367. if (!(cpu_class_is_omap1() || cpu_is_omap2420())) {
  368. /*
  369. * HSI2C controller internal clk rate should be 19.2 Mhz for
  370. * HS and for all modes on 2430. On 34xx we can use lower rate
  371. * to get longer filter period for better noise suppression.
  372. * The filter is iclk (fclk for HS) period.
  373. */
  374. if (dev->speed > 400 || cpu_is_omap2430())
  375. internal_clk = 19200;
  376. else if (dev->speed > 100)
  377. internal_clk = 9600;
  378. else
  379. internal_clk = 4000;
  380. fclk = clk_get(dev->dev, "fck");
  381. fclk_rate = clk_get_rate(fclk) / 1000;
  382. clk_put(fclk);
  383. /* Compute prescaler divisor */
  384. psc = fclk_rate / internal_clk;
  385. psc = psc - 1;
  386. /* If configured for High Speed */
  387. if (dev->speed > 400) {
  388. unsigned long scl;
  389. /* For first phase of HS mode */
  390. scl = internal_clk / 400;
  391. fsscll = scl - (scl / 3) - 7;
  392. fssclh = (scl / 3) - 5;
  393. /* For second phase of HS mode */
  394. scl = fclk_rate / dev->speed;
  395. hsscll = scl - (scl / 3) - 7;
  396. hssclh = (scl / 3) - 5;
  397. } else if (dev->speed > 100) {
  398. unsigned long scl;
  399. /* Fast mode */
  400. scl = internal_clk / dev->speed;
  401. fsscll = scl - (scl / 3) - 7;
  402. fssclh = (scl / 3) - 5;
  403. } else {
  404. /* Standard mode */
  405. fsscll = internal_clk / (dev->speed * 2) - 7;
  406. fssclh = internal_clk / (dev->speed * 2) - 5;
  407. }
  408. scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
  409. sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
  410. } else {
  411. /* Program desired operating rate */
  412. fclk_rate /= (psc + 1) * 1000;
  413. if (psc > 2)
  414. psc = 2;
  415. scll = fclk_rate / (dev->speed * 2) - 7 + psc;
  416. sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
  417. }
  418. /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
  419. omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);
  420. /* SCL low and high time values */
  421. omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, scll);
  422. omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, sclh);
  423. if (dev->fifo_size) {
  424. /* Note: setup required fifo size - 1. RTRSH and XTRSH */
  425. buf = (dev->fifo_size - 1) << 8 | OMAP_I2C_BUF_RXFIF_CLR |
  426. (dev->fifo_size - 1) | OMAP_I2C_BUF_TXFIF_CLR;
  427. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, buf);
  428. }
  429. /* Take the I2C module out of reset: */
  430. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  431. dev->errata = 0;
  432. if (cpu_is_omap2430() || cpu_is_omap34xx())
  433. dev->errata |= I2C_OMAP_ERRATA_I207;
  434. /* Enable interrupts */
  435. dev->iestate = (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
  436. OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
  437. OMAP_I2C_IE_AL) | ((dev->fifo_size) ?
  438. (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0);
  439. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
  440. if (cpu_is_omap34xx()) {
  441. dev->pscstate = psc;
  442. dev->scllstate = scll;
  443. dev->sclhstate = sclh;
  444. dev->bufstate = buf;
  445. }
  446. return 0;
  447. }
  448. /*
  449. * Waiting on Bus Busy
  450. */
  451. static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
  452. {
  453. unsigned long timeout;
  454. timeout = jiffies + OMAP_I2C_TIMEOUT;
  455. while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
  456. if (time_after(jiffies, timeout)) {
  457. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  458. return -ETIMEDOUT;
  459. }
  460. msleep(1);
  461. }
  462. return 0;
  463. }
  464. /*
  465. * Low level master read/write transaction.
  466. */
  467. static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
  468. struct i2c_msg *msg, int stop)
  469. {
  470. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  471. int r;
  472. u16 w;
  473. dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  474. msg->addr, msg->len, msg->flags, stop);
  475. if (msg->len == 0)
  476. return -EINVAL;
  477. omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
  478. /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
  479. dev->buf = msg->buf;
  480. dev->buf_len = msg->len;
  481. omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
  482. /* Clear the FIFO Buffers */
  483. w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
  484. w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
  485. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
  486. init_completion(&dev->cmd_complete);
  487. dev->cmd_err = 0;
  488. w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
  489. /* High speed configuration */
  490. if (dev->speed > 400)
  491. w |= OMAP_I2C_CON_OPMODE_HS;
  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. if (dev->set_mpu_wkup_lat != NULL)
  524. dev->set_mpu_wkup_lat(dev->dev, dev->latency);
  525. r = wait_for_completion_timeout(&dev->cmd_complete,
  526. OMAP_I2C_TIMEOUT);
  527. if (dev->set_mpu_wkup_lat != NULL)
  528. dev->set_mpu_wkup_lat(dev->dev, -1);
  529. dev->buf_len = 0;
  530. if (r < 0)
  531. return r;
  532. if (r == 0) {
  533. dev_err(dev->dev, "controller timed out\n");
  534. omap_i2c_init(dev);
  535. return -ETIMEDOUT;
  536. }
  537. if (likely(!dev->cmd_err))
  538. return 0;
  539. /* We have an error */
  540. if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
  541. OMAP_I2C_STAT_XUDF)) {
  542. omap_i2c_init(dev);
  543. return -EIO;
  544. }
  545. if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
  546. if (msg->flags & I2C_M_IGNORE_NAK)
  547. return 0;
  548. if (stop) {
  549. w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  550. w |= OMAP_I2C_CON_STP;
  551. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  552. }
  553. return -EREMOTEIO;
  554. }
  555. return -EIO;
  556. }
  557. /*
  558. * Prepare controller for a transaction and call omap_i2c_xfer_msg
  559. * to do the work during IRQ processing.
  560. */
  561. static int
  562. omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  563. {
  564. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  565. int i;
  566. int r;
  567. omap_i2c_unidle(dev);
  568. r = omap_i2c_wait_for_bb(dev);
  569. if (r < 0)
  570. goto out;
  571. for (i = 0; i < num; i++) {
  572. r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  573. if (r != 0)
  574. break;
  575. }
  576. if (r == 0)
  577. r = num;
  578. omap_i2c_wait_for_bb(dev);
  579. out:
  580. omap_i2c_idle(dev);
  581. return r;
  582. }
  583. static u32
  584. omap_i2c_func(struct i2c_adapter *adap)
  585. {
  586. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  587. }
  588. static inline void
  589. omap_i2c_complete_cmd(struct omap_i2c_dev *dev, u16 err)
  590. {
  591. dev->cmd_err |= err;
  592. complete(&dev->cmd_complete);
  593. }
  594. static inline void
  595. omap_i2c_ack_stat(struct omap_i2c_dev *dev, u16 stat)
  596. {
  597. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
  598. }
  599. static inline void i2c_omap_errata_i207(struct omap_i2c_dev *dev, u16 stat)
  600. {
  601. /*
  602. * I2C Errata(Errata Nos. OMAP2: 1.67, OMAP3: 1.8)
  603. * Not applicable for OMAP4.
  604. * Under certain rare conditions, RDR could be set again
  605. * when the bus is busy, then ignore the interrupt and
  606. * clear the interrupt.
  607. */
  608. if (stat & OMAP_I2C_STAT_RDR) {
  609. /* Step 1: If RDR is set, clear it */
  610. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  611. /* Step 2: */
  612. if (!(omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  613. & OMAP_I2C_STAT_BB)) {
  614. /* Step 3: */
  615. if (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  616. & OMAP_I2C_STAT_RDR) {
  617. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  618. dev_dbg(dev->dev, "RDR when bus is busy.\n");
  619. }
  620. }
  621. }
  622. }
  623. /* rev1 devices are apparently only on some 15xx */
  624. #ifdef CONFIG_ARCH_OMAP15XX
  625. static irqreturn_t
  626. omap_i2c_rev1_isr(int this_irq, void *dev_id)
  627. {
  628. struct omap_i2c_dev *dev = dev_id;
  629. u16 iv, w;
  630. if (dev->idle)
  631. return IRQ_NONE;
  632. iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
  633. switch (iv) {
  634. case 0x00: /* None */
  635. break;
  636. case 0x01: /* Arbitration lost */
  637. dev_err(dev->dev, "Arbitration lost\n");
  638. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
  639. break;
  640. case 0x02: /* No acknowledgement */
  641. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
  642. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
  643. break;
  644. case 0x03: /* Register access ready */
  645. omap_i2c_complete_cmd(dev, 0);
  646. break;
  647. case 0x04: /* Receive data ready */
  648. if (dev->buf_len) {
  649. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  650. *dev->buf++ = w;
  651. dev->buf_len--;
  652. if (dev->buf_len) {
  653. *dev->buf++ = w >> 8;
  654. dev->buf_len--;
  655. }
  656. } else
  657. dev_err(dev->dev, "RRDY IRQ while no data requested\n");
  658. break;
  659. case 0x05: /* Transmit data ready */
  660. if (dev->buf_len) {
  661. w = *dev->buf++;
  662. dev->buf_len--;
  663. if (dev->buf_len) {
  664. w |= *dev->buf++ << 8;
  665. dev->buf_len--;
  666. }
  667. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  668. } else
  669. dev_err(dev->dev, "XRDY IRQ while no data to send\n");
  670. break;
  671. default:
  672. return IRQ_NONE;
  673. }
  674. return IRQ_HANDLED;
  675. }
  676. #else
  677. #define omap_i2c_rev1_isr NULL
  678. #endif
  679. /*
  680. * OMAP3430 Errata 1.153: When an XRDY/XDR is hit, wait for XUDF before writing
  681. * data to DATA_REG. Otherwise some data bytes can be lost while transferring
  682. * them from the memory to the I2C interface.
  683. */
  684. static int errata_omap3_1p153(struct omap_i2c_dev *dev, u16 *stat, int *err)
  685. {
  686. unsigned long timeout = 10000;
  687. while (--timeout && !(*stat & OMAP_I2C_STAT_XUDF)) {
  688. if (*stat & (OMAP_I2C_STAT_NACK | OMAP_I2C_STAT_AL)) {
  689. omap_i2c_ack_stat(dev, *stat & (OMAP_I2C_STAT_XRDY |
  690. OMAP_I2C_STAT_XDR));
  691. *err |= OMAP_I2C_STAT_XUDF;
  692. return -ETIMEDOUT;
  693. }
  694. cpu_relax();
  695. *stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  696. }
  697. if (!timeout) {
  698. dev_err(dev->dev, "timeout waiting on XUDF bit\n");
  699. return 0;
  700. }
  701. return 0;
  702. }
  703. static irqreturn_t
  704. omap_i2c_isr(int this_irq, void *dev_id)
  705. {
  706. struct omap_i2c_dev *dev = dev_id;
  707. u16 bits;
  708. u16 stat, w;
  709. int err, count = 0;
  710. if (dev->idle)
  711. return IRQ_NONE;
  712. bits = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  713. while ((stat = (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG))) & bits) {
  714. dev_dbg(dev->dev, "IRQ (ISR = 0x%04x)\n", stat);
  715. if (count++ == 100) {
  716. dev_warn(dev->dev, "Too much work in one IRQ\n");
  717. break;
  718. }
  719. err = 0;
  720. complete:
  721. /*
  722. * Ack the stat in one go, but [R/X]DR and [R/X]RDY should be
  723. * acked after the data operation is complete.
  724. * Ref: TRM SWPU114Q Figure 18-31
  725. */
  726. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat &
  727. ~(OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR |
  728. OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR));
  729. if (stat & OMAP_I2C_STAT_NACK) {
  730. err |= OMAP_I2C_STAT_NACK;
  731. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
  732. OMAP_I2C_CON_STP);
  733. }
  734. if (stat & OMAP_I2C_STAT_AL) {
  735. dev_err(dev->dev, "Arbitration lost\n");
  736. err |= OMAP_I2C_STAT_AL;
  737. }
  738. if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
  739. OMAP_I2C_STAT_AL)) {
  740. omap_i2c_ack_stat(dev, stat &
  741. (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR |
  742. OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR));
  743. omap_i2c_complete_cmd(dev, err);
  744. return IRQ_HANDLED;
  745. }
  746. if (stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR)) {
  747. u8 num_bytes = 1;
  748. if (dev->errata & I2C_OMAP_ERRATA_I207)
  749. i2c_omap_errata_i207(dev, stat);
  750. if (dev->fifo_size) {
  751. if (stat & OMAP_I2C_STAT_RRDY)
  752. num_bytes = dev->fifo_size;
  753. else /* read RXSTAT on RDR interrupt */
  754. num_bytes = (omap_i2c_read_reg(dev,
  755. OMAP_I2C_BUFSTAT_REG)
  756. >> 8) & 0x3F;
  757. }
  758. while (num_bytes) {
  759. num_bytes--;
  760. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  761. if (dev->buf_len) {
  762. *dev->buf++ = w;
  763. dev->buf_len--;
  764. /*
  765. * Data reg in 2430, omap3 and
  766. * omap4 is 8 bit wide
  767. */
  768. if (cpu_class_is_omap1() ||
  769. cpu_is_omap2420()) {
  770. if (dev->buf_len) {
  771. *dev->buf++ = w >> 8;
  772. dev->buf_len--;
  773. }
  774. }
  775. } else {
  776. if (stat & OMAP_I2C_STAT_RRDY)
  777. dev_err(dev->dev,
  778. "RRDY IRQ while no data"
  779. " requested\n");
  780. if (stat & OMAP_I2C_STAT_RDR)
  781. dev_err(dev->dev,
  782. "RDR IRQ while no data"
  783. " requested\n");
  784. break;
  785. }
  786. }
  787. omap_i2c_ack_stat(dev,
  788. stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR));
  789. continue;
  790. }
  791. if (stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR)) {
  792. u8 num_bytes = 1;
  793. if (dev->fifo_size) {
  794. if (stat & OMAP_I2C_STAT_XRDY)
  795. num_bytes = dev->fifo_size;
  796. else /* read TXSTAT on XDR interrupt */
  797. num_bytes = omap_i2c_read_reg(dev,
  798. OMAP_I2C_BUFSTAT_REG)
  799. & 0x3F;
  800. }
  801. while (num_bytes) {
  802. num_bytes--;
  803. w = 0;
  804. if (dev->buf_len) {
  805. w = *dev->buf++;
  806. dev->buf_len--;
  807. /*
  808. * Data reg in 2430, omap3 and
  809. * omap4 is 8 bit wide
  810. */
  811. if (cpu_class_is_omap1() ||
  812. cpu_is_omap2420()) {
  813. if (dev->buf_len) {
  814. w |= *dev->buf++ << 8;
  815. dev->buf_len--;
  816. }
  817. }
  818. } else {
  819. if (stat & OMAP_I2C_STAT_XRDY)
  820. dev_err(dev->dev,
  821. "XRDY IRQ while no "
  822. "data to send\n");
  823. if (stat & OMAP_I2C_STAT_XDR)
  824. dev_err(dev->dev,
  825. "XDR IRQ while no "
  826. "data to send\n");
  827. break;
  828. }
  829. if ((dev->errata & I2C_OMAP3_1P153) &&
  830. errata_omap3_1p153(dev, &stat, &err))
  831. goto complete;
  832. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  833. }
  834. omap_i2c_ack_stat(dev,
  835. stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR));
  836. continue;
  837. }
  838. if (stat & OMAP_I2C_STAT_ROVR) {
  839. dev_err(dev->dev, "Receive overrun\n");
  840. dev->cmd_err |= OMAP_I2C_STAT_ROVR;
  841. }
  842. if (stat & OMAP_I2C_STAT_XUDF) {
  843. dev_err(dev->dev, "Transmit underflow\n");
  844. dev->cmd_err |= OMAP_I2C_STAT_XUDF;
  845. }
  846. }
  847. return count ? IRQ_HANDLED : IRQ_NONE;
  848. }
  849. static const struct i2c_algorithm omap_i2c_algo = {
  850. .master_xfer = omap_i2c_xfer,
  851. .functionality = omap_i2c_func,
  852. };
  853. static int __devinit
  854. omap_i2c_probe(struct platform_device *pdev)
  855. {
  856. struct omap_i2c_dev *dev;
  857. struct i2c_adapter *adap;
  858. struct resource *mem, *irq, *ioarea;
  859. struct omap_i2c_bus_platform_data *pdata = pdev->dev.platform_data;
  860. irq_handler_t isr;
  861. int r;
  862. u32 speed = 0;
  863. /* NOTE: driver uses the static register mapping */
  864. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  865. if (!mem) {
  866. dev_err(&pdev->dev, "no mem resource?\n");
  867. return -ENODEV;
  868. }
  869. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  870. if (!irq) {
  871. dev_err(&pdev->dev, "no irq resource?\n");
  872. return -ENODEV;
  873. }
  874. ioarea = request_mem_region(mem->start, resource_size(mem),
  875. pdev->name);
  876. if (!ioarea) {
  877. dev_err(&pdev->dev, "I2C region already claimed\n");
  878. return -EBUSY;
  879. }
  880. dev = kzalloc(sizeof(struct omap_i2c_dev), GFP_KERNEL);
  881. if (!dev) {
  882. r = -ENOMEM;
  883. goto err_release_region;
  884. }
  885. if (pdata != NULL) {
  886. speed = pdata->clkrate;
  887. dev->set_mpu_wkup_lat = pdata->set_mpu_wkup_lat;
  888. } else {
  889. speed = 100; /* Default speed */
  890. dev->set_mpu_wkup_lat = NULL;
  891. }
  892. dev->speed = speed;
  893. dev->idle = 1;
  894. dev->dev = &pdev->dev;
  895. dev->irq = irq->start;
  896. dev->base = ioremap(mem->start, resource_size(mem));
  897. if (!dev->base) {
  898. r = -ENOMEM;
  899. goto err_free_mem;
  900. }
  901. platform_set_drvdata(pdev, dev);
  902. if (cpu_is_omap7xx())
  903. dev->reg_shift = 1;
  904. else if (cpu_is_omap44xx())
  905. dev->reg_shift = 0;
  906. else
  907. dev->reg_shift = 2;
  908. if (cpu_is_omap44xx())
  909. dev->regs = (u8 *) omap4_reg_map;
  910. else
  911. dev->regs = (u8 *) reg_map;
  912. pm_runtime_enable(&pdev->dev);
  913. omap_i2c_unidle(dev);
  914. dev->rev = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) & 0xff;
  915. if (dev->rev <= OMAP_I2C_REV_ON_3430)
  916. dev->errata |= I2C_OMAP3_1P153;
  917. if (!(cpu_class_is_omap1() || cpu_is_omap2420())) {
  918. u16 s;
  919. /* Set up the fifo size - Get total size */
  920. s = (omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3;
  921. dev->fifo_size = 0x8 << s;
  922. /*
  923. * Set up notification threshold as half the total available
  924. * size. This is to ensure that we can handle the status on int
  925. * call back latencies.
  926. */
  927. if (dev->rev >= OMAP_I2C_REV_ON_4430) {
  928. dev->fifo_size = 0;
  929. dev->b_hw = 0; /* Disable hardware fixes */
  930. } else {
  931. dev->fifo_size = (dev->fifo_size / 2);
  932. dev->b_hw = 1; /* Enable hardware fixes */
  933. }
  934. /* calculate wakeup latency constraint for MPU */
  935. if (dev->set_mpu_wkup_lat != NULL)
  936. dev->latency = (1000000 * dev->fifo_size) /
  937. (1000 * speed / 8);
  938. }
  939. /* reset ASAP, clearing any IRQs */
  940. omap_i2c_init(dev);
  941. isr = (dev->rev < OMAP_I2C_REV_2) ? omap_i2c_rev1_isr : omap_i2c_isr;
  942. r = request_irq(dev->irq, isr, 0, pdev->name, dev);
  943. if (r) {
  944. dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
  945. goto err_unuse_clocks;
  946. }
  947. dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n",
  948. pdev->id, dev->rev >> 4, dev->rev & 0xf, dev->speed);
  949. omap_i2c_idle(dev);
  950. adap = &dev->adapter;
  951. i2c_set_adapdata(adap, dev);
  952. adap->owner = THIS_MODULE;
  953. adap->class = I2C_CLASS_HWMON;
  954. strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  955. adap->algo = &omap_i2c_algo;
  956. adap->dev.parent = &pdev->dev;
  957. /* i2c device drivers may be active on return from add_adapter() */
  958. adap->nr = pdev->id;
  959. r = i2c_add_numbered_adapter(adap);
  960. if (r) {
  961. dev_err(dev->dev, "failure adding adapter\n");
  962. goto err_free_irq;
  963. }
  964. return 0;
  965. err_free_irq:
  966. free_irq(dev->irq, dev);
  967. err_unuse_clocks:
  968. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  969. omap_i2c_idle(dev);
  970. iounmap(dev->base);
  971. err_free_mem:
  972. platform_set_drvdata(pdev, NULL);
  973. kfree(dev);
  974. err_release_region:
  975. release_mem_region(mem->start, resource_size(mem));
  976. return r;
  977. }
  978. static int
  979. omap_i2c_remove(struct platform_device *pdev)
  980. {
  981. struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
  982. struct resource *mem;
  983. platform_set_drvdata(pdev, NULL);
  984. free_irq(dev->irq, dev);
  985. i2c_del_adapter(&dev->adapter);
  986. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  987. iounmap(dev->base);
  988. kfree(dev);
  989. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  990. release_mem_region(mem->start, resource_size(mem));
  991. return 0;
  992. }
  993. static struct platform_driver omap_i2c_driver = {
  994. .probe = omap_i2c_probe,
  995. .remove = omap_i2c_remove,
  996. .driver = {
  997. .name = "omap_i2c",
  998. .owner = THIS_MODULE,
  999. },
  1000. };
  1001. /* I2C may be needed to bring up other drivers */
  1002. static int __init
  1003. omap_i2c_init_driver(void)
  1004. {
  1005. return platform_driver_register(&omap_i2c_driver);
  1006. }
  1007. subsys_initcall(omap_i2c_init_driver);
  1008. static void __exit omap_i2c_exit_driver(void)
  1009. {
  1010. platform_driver_unregister(&omap_i2c_driver);
  1011. }
  1012. module_exit(omap_i2c_exit_driver);
  1013. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1014. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1015. MODULE_LICENSE("GPL");
  1016. MODULE_ALIAS("platform:omap_i2c");