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_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 omap_i2c_bus_platform_data *pdata;
  248. WARN_ON(!dev->idle);
  249. pdata = dev->dev->platform_data;
  250. pm_runtime_get_sync(dev->dev);
  251. if (pdata->flags & OMAP_I2C_FLAG_RESET_REGS_POSTIDLE) {
  252. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  253. omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, dev->pscstate);
  254. omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, dev->scllstate);
  255. omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, dev->sclhstate);
  256. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, dev->bufstate);
  257. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, dev->syscstate);
  258. omap_i2c_write_reg(dev, OMAP_I2C_WE_REG, dev->westate);
  259. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  260. }
  261. dev->idle = 0;
  262. /*
  263. * Don't write to this register if the IE state is 0 as it can
  264. * cause deadlock.
  265. */
  266. if (dev->iestate)
  267. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
  268. }
  269. static void omap_i2c_idle(struct omap_i2c_dev *dev)
  270. {
  271. struct omap_i2c_bus_platform_data *pdata;
  272. u16 iv;
  273. WARN_ON(dev->idle);
  274. pdata = dev->dev->platform_data;
  275. dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  276. if (pdata->rev == OMAP_I2C_IP_VERSION_2)
  277. omap_i2c_write_reg(dev, OMAP_I2C_IP_V2_IRQENABLE_CLR, 1);
  278. else
  279. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);
  280. if (dev->rev < OMAP_I2C_OMAP1_REV_2) {
  281. iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG); /* Read clears */
  282. } else {
  283. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);
  284. /* Flush posted write before the dev->idle store occurs */
  285. omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  286. }
  287. dev->idle = 1;
  288. pm_runtime_put_sync(dev->dev);
  289. }
  290. static int omap_i2c_init(struct omap_i2c_dev *dev)
  291. {
  292. u16 psc = 0, scll = 0, sclh = 0, buf = 0;
  293. u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
  294. unsigned long fclk_rate = 12000000;
  295. unsigned long timeout;
  296. unsigned long internal_clk = 0;
  297. struct clk *fclk;
  298. struct omap_i2c_bus_platform_data *pdata;
  299. pdata = dev->dev->platform_data;
  300. if (dev->rev >= OMAP_I2C_OMAP1_REV_2) {
  301. /* Disable I2C controller before soft reset */
  302. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
  303. omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
  304. ~(OMAP_I2C_CON_EN));
  305. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
  306. /* For some reason we need to set the EN bit before the
  307. * reset done bit gets set. */
  308. timeout = jiffies + OMAP_I2C_TIMEOUT;
  309. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  310. while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
  311. SYSS_RESETDONE_MASK)) {
  312. if (time_after(jiffies, timeout)) {
  313. dev_warn(dev->dev, "timeout waiting "
  314. "for controller reset\n");
  315. return -ETIMEDOUT;
  316. }
  317. msleep(1);
  318. }
  319. /* SYSC register is cleared by the reset; rewrite it */
  320. if (dev->rev == OMAP_I2C_REV_ON_2430) {
  321. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
  322. SYSC_AUTOIDLE_MASK);
  323. } else if (dev->rev >= OMAP_I2C_REV_ON_3430) {
  324. dev->syscstate = SYSC_AUTOIDLE_MASK;
  325. dev->syscstate |= SYSC_ENAWAKEUP_MASK;
  326. dev->syscstate |= (SYSC_IDLEMODE_SMART <<
  327. __ffs(SYSC_SIDLEMODE_MASK));
  328. dev->syscstate |= (SYSC_CLOCKACTIVITY_FCLK <<
  329. __ffs(SYSC_CLOCKACTIVITY_MASK));
  330. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG,
  331. dev->syscstate);
  332. /*
  333. * Enabling all wakup sources to stop I2C freezing on
  334. * WFI instruction.
  335. * REVISIT: Some wkup sources might not be needed.
  336. */
  337. dev->westate = OMAP_I2C_WE_ALL;
  338. omap_i2c_write_reg(dev, OMAP_I2C_WE_REG,
  339. dev->westate);
  340. }
  341. }
  342. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  343. if (pdata->flags & OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK) {
  344. /*
  345. * The I2C functional clock is the armxor_ck, so there's
  346. * no need to get "armxor_ck" separately. Now, if OMAP2420
  347. * always returns 12MHz for the functional clock, we can
  348. * do this bit unconditionally.
  349. */
  350. fclk = clk_get(dev->dev, "fck");
  351. fclk_rate = clk_get_rate(fclk);
  352. clk_put(fclk);
  353. /* TRM for 5912 says the I2C clock must be prescaled to be
  354. * between 7 - 12 MHz. The XOR input clock is typically
  355. * 12, 13 or 19.2 MHz. So we should have code that produces:
  356. *
  357. * XOR MHz Divider Prescaler
  358. * 12 1 0
  359. * 13 2 1
  360. * 19.2 2 1
  361. */
  362. if (fclk_rate > 12000000)
  363. psc = fclk_rate / 12000000;
  364. }
  365. if (!(pdata->flags & OMAP_I2C_FLAG_SIMPLE_CLOCK)) {
  366. /*
  367. * HSI2C controller internal clk rate should be 19.2 Mhz for
  368. * HS and for all modes on 2430. On 34xx we can use lower rate
  369. * to get longer filter period for better noise suppression.
  370. * The filter is iclk (fclk for HS) period.
  371. */
  372. if (dev->speed > 400 ||
  373. pdata->flags & OMAP_I2C_FLAG_FORCE_19200_INT_CLK)
  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 (pdata->flags & OMAP_I2C_FLAG_APPLY_ERRATA_I207)
  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 (pdata->flags & OMAP_I2C_FLAG_RESET_REGS_POSTIDLE) {
  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_omap1_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_omap1_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. struct omap_i2c_bus_platform_data *pdata;
  710. pdata = dev->dev->platform_data;
  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 (pdata->flags &
  774. OMAP_I2C_FLAG_16BIT_DATA_REG) {
  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 (pdata->flags &
  817. OMAP_I2C_FLAG_16BIT_DATA_REG) {
  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. dev->reg_shift = (pdata->flags >> OMAP_I2C_FLAG_BUS_SHIFT__SHIFT) & 3;
  908. if (pdata->rev == OMAP_I2C_IP_VERSION_2)
  909. dev->regs = (u8 *)reg_map_ip_v2;
  910. else
  911. dev->regs = (u8 *)reg_map_ip_v1;
  912. pm_runtime_enable(dev->dev);
  913. omap_i2c_unidle(dev);
  914. dev->rev = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) & 0xff;
  915. if (dev->rev <= OMAP_I2C_REV_ON_3430)
  916. dev->errata |= I2C_OMAP3_1P153;
  917. if (!(pdata->flags & OMAP_I2C_FLAG_NO_FIFO)) {
  918. u16 s;
  919. /* Set up the fifo size - Get total size */
  920. s = (omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3;
  921. dev->fifo_size = 0x8 << s;
  922. /*
  923. * Set up notification threshold as half the total available
  924. * size. This is to ensure that we can handle the status on int
  925. * call back latencies.
  926. */
  927. if (dev->rev >= OMAP_I2C_REV_ON_3530_4430) {
  928. dev->fifo_size = 0;
  929. dev->b_hw = 0; /* Disable hardware fixes */
  930. } else {
  931. dev->fifo_size = (dev->fifo_size / 2);
  932. dev->b_hw = 1; /* Enable hardware fixes */
  933. }
  934. /* calculate wakeup latency constraint for MPU */
  935. if (dev->set_mpu_wkup_lat != NULL)
  936. dev->latency = (1000000 * dev->fifo_size) /
  937. (1000 * speed / 8);
  938. }
  939. /* reset ASAP, clearing any IRQs */
  940. omap_i2c_init(dev);
  941. isr = (dev->rev < OMAP_I2C_OMAP1_REV_2) ? omap_i2c_omap1_isr :
  942. omap_i2c_isr;
  943. r = request_irq(dev->irq, isr, 0, pdev->name, dev);
  944. if (r) {
  945. dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
  946. goto err_unuse_clocks;
  947. }
  948. dev_info(dev->dev, "bus %d rev%d.%d.%d at %d kHz\n", pdev->id,
  949. pdata->rev, dev->rev >> 4, dev->rev & 0xf, dev->speed);
  950. omap_i2c_idle(dev);
  951. adap = &dev->adapter;
  952. i2c_set_adapdata(adap, dev);
  953. adap->owner = THIS_MODULE;
  954. adap->class = I2C_CLASS_HWMON;
  955. strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  956. adap->algo = &omap_i2c_algo;
  957. adap->dev.parent = &pdev->dev;
  958. /* i2c device drivers may be active on return from add_adapter() */
  959. adap->nr = pdev->id;
  960. r = i2c_add_numbered_adapter(adap);
  961. if (r) {
  962. dev_err(dev->dev, "failure adding adapter\n");
  963. goto err_free_irq;
  964. }
  965. return 0;
  966. err_free_irq:
  967. free_irq(dev->irq, dev);
  968. err_unuse_clocks:
  969. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  970. omap_i2c_idle(dev);
  971. iounmap(dev->base);
  972. err_free_mem:
  973. platform_set_drvdata(pdev, NULL);
  974. kfree(dev);
  975. err_release_region:
  976. release_mem_region(mem->start, resource_size(mem));
  977. return r;
  978. }
  979. static int
  980. omap_i2c_remove(struct platform_device *pdev)
  981. {
  982. struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
  983. struct resource *mem;
  984. platform_set_drvdata(pdev, NULL);
  985. free_irq(dev->irq, dev);
  986. i2c_del_adapter(&dev->adapter);
  987. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  988. iounmap(dev->base);
  989. kfree(dev);
  990. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  991. release_mem_region(mem->start, resource_size(mem));
  992. return 0;
  993. }
  994. static struct platform_driver omap_i2c_driver = {
  995. .probe = omap_i2c_probe,
  996. .remove = omap_i2c_remove,
  997. .driver = {
  998. .name = "omap_i2c",
  999. .owner = THIS_MODULE,
  1000. },
  1001. };
  1002. /* I2C may be needed to bring up other drivers */
  1003. static int __init
  1004. omap_i2c_init_driver(void)
  1005. {
  1006. return platform_driver_register(&omap_i2c_driver);
  1007. }
  1008. subsys_initcall(omap_i2c_init_driver);
  1009. static void __exit omap_i2c_exit_driver(void)
  1010. {
  1011. platform_driver_unregister(&omap_i2c_driver);
  1012. }
  1013. module_exit(omap_i2c_exit_driver);
  1014. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1015. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1016. MODULE_LICENSE("GPL");
  1017. MODULE_ALIAS("platform:omap_i2c");