i2c-omap.c 31 KB

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