i2c-omap.c 33 KB

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