i2c-omap.c 31 KB

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