i2c-omap.c 32 KB

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