i2c-omap.c 35 KB

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