i2c-omap.c 32 KB

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