i2c-omap.c 34 KB

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