i2c-omap.c 31 KB

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