i2c-omap.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/err.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/completion.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/clk.h>
  34. #include <linux/io.h>
  35. #include <linux/of.h>
  36. #include <linux/of_device.h>
  37. #include <linux/slab.h>
  38. #include <linux/i2c-omap.h>
  39. #include <linux/pm_runtime.h>
  40. /* I2C controller revisions */
  41. #define OMAP_I2C_OMAP1_REV_2 0x20
  42. /* I2C controller revisions present on specific hardware */
  43. #define OMAP_I2C_REV_ON_2430 0x00000036
  44. #define OMAP_I2C_REV_ON_3430_3530 0x0000003C
  45. #define OMAP_I2C_REV_ON_3630 0x00000040
  46. #define OMAP_I2C_REV_ON_4430_PLUS 0x50400002
  47. /* timeout waiting for the controller to respond */
  48. #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
  49. /* timeout for pm runtime autosuspend */
  50. #define OMAP_I2C_PM_TIMEOUT 1000 /* ms */
  51. /* timeout for making decision on bus free status */
  52. #define OMAP_I2C_BUS_FREE_TIMEOUT (msecs_to_jiffies(10))
  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_BF (1 << 8) /* Bus Free */
  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. #define OMAP_I2C_SYSTEST_ST_EN (1 << 15) /* System test enable */
  138. #define OMAP_I2C_SYSTEST_FREE (1 << 14) /* Free running mode */
  139. #define OMAP_I2C_SYSTEST_TMODE_MASK (3 << 12) /* Test mode select */
  140. #define OMAP_I2C_SYSTEST_TMODE_SHIFT (12) /* Test mode select */
  141. /* Functional mode */
  142. #define OMAP_I2C_SYSTEST_SCL_I_FUNC (1 << 8) /* SCL line input value */
  143. #define OMAP_I2C_SYSTEST_SCL_O_FUNC (1 << 7) /* SCL line output value */
  144. #define OMAP_I2C_SYSTEST_SDA_I_FUNC (1 << 6) /* SDA line input value */
  145. #define OMAP_I2C_SYSTEST_SDA_O_FUNC (1 << 5) /* SDA line output value */
  146. /* SDA/SCL IO mode */
  147. #define OMAP_I2C_SYSTEST_SCL_I (1 << 3) /* SCL line sense in */
  148. #define OMAP_I2C_SYSTEST_SCL_O (1 << 2) /* SCL line drive out */
  149. #define OMAP_I2C_SYSTEST_SDA_I (1 << 1) /* SDA line sense in */
  150. #define OMAP_I2C_SYSTEST_SDA_O (1 << 0) /* SDA line drive out */
  151. /* OCP_SYSSTATUS bit definitions */
  152. #define SYSS_RESETDONE_MASK (1 << 0)
  153. /* OCP_SYSCONFIG bit definitions */
  154. #define SYSC_CLOCKACTIVITY_MASK (0x3 << 8)
  155. #define SYSC_SIDLEMODE_MASK (0x3 << 3)
  156. #define SYSC_ENAWAKEUP_MASK (1 << 2)
  157. #define SYSC_SOFTRESET_MASK (1 << 1)
  158. #define SYSC_AUTOIDLE_MASK (1 << 0)
  159. #define SYSC_IDLEMODE_SMART 0x2
  160. #define SYSC_CLOCKACTIVITY_FCLK 0x2
  161. /* Errata definitions */
  162. #define I2C_OMAP_ERRATA_I207 (1 << 0)
  163. #define I2C_OMAP_ERRATA_I462 (1 << 1)
  164. #define OMAP_I2C_IP_V2_INTERRUPTS_MASK 0x6FFF
  165. struct omap_i2c_dev {
  166. spinlock_t lock; /* IRQ synchronization */
  167. struct device *dev;
  168. void __iomem *base; /* virtual */
  169. int irq;
  170. int reg_shift; /* bit shift for I2C register addresses */
  171. struct completion cmd_complete;
  172. struct resource *ioarea;
  173. u32 latency; /* maximum mpu wkup latency */
  174. void (*set_mpu_wkup_lat)(struct device *dev,
  175. long latency);
  176. u32 speed; /* Speed of bus in kHz */
  177. u32 flags;
  178. u16 scheme;
  179. u16 cmd_err;
  180. u8 *buf;
  181. u8 *regs;
  182. size_t buf_len;
  183. struct i2c_adapter adapter;
  184. u8 threshold;
  185. u8 fifo_size; /* use as flag and value
  186. * fifo_size==0 implies no fifo
  187. * if set, should be trsh+1
  188. */
  189. u32 rev;
  190. unsigned b_hw:1; /* bad h/w fixes */
  191. unsigned bb_valid:1; /* true when BB-bit reflects
  192. * the I2C bus state
  193. */
  194. unsigned receiver:1; /* true when we're in receiver mode */
  195. u16 iestate; /* Saved interrupt register */
  196. u16 pscstate;
  197. u16 scllstate;
  198. u16 sclhstate;
  199. u16 syscstate;
  200. u16 westate;
  201. u16 errata;
  202. };
  203. static const u8 reg_map_ip_v1[] = {
  204. [OMAP_I2C_REV_REG] = 0x00,
  205. [OMAP_I2C_IE_REG] = 0x01,
  206. [OMAP_I2C_STAT_REG] = 0x02,
  207. [OMAP_I2C_IV_REG] = 0x03,
  208. [OMAP_I2C_WE_REG] = 0x03,
  209. [OMAP_I2C_SYSS_REG] = 0x04,
  210. [OMAP_I2C_BUF_REG] = 0x05,
  211. [OMAP_I2C_CNT_REG] = 0x06,
  212. [OMAP_I2C_DATA_REG] = 0x07,
  213. [OMAP_I2C_SYSC_REG] = 0x08,
  214. [OMAP_I2C_CON_REG] = 0x09,
  215. [OMAP_I2C_OA_REG] = 0x0a,
  216. [OMAP_I2C_SA_REG] = 0x0b,
  217. [OMAP_I2C_PSC_REG] = 0x0c,
  218. [OMAP_I2C_SCLL_REG] = 0x0d,
  219. [OMAP_I2C_SCLH_REG] = 0x0e,
  220. [OMAP_I2C_SYSTEST_REG] = 0x0f,
  221. [OMAP_I2C_BUFSTAT_REG] = 0x10,
  222. };
  223. static const u8 reg_map_ip_v2[] = {
  224. [OMAP_I2C_REV_REG] = 0x04,
  225. [OMAP_I2C_IE_REG] = 0x2c,
  226. [OMAP_I2C_STAT_REG] = 0x28,
  227. [OMAP_I2C_IV_REG] = 0x34,
  228. [OMAP_I2C_WE_REG] = 0x34,
  229. [OMAP_I2C_SYSS_REG] = 0x90,
  230. [OMAP_I2C_BUF_REG] = 0x94,
  231. [OMAP_I2C_CNT_REG] = 0x98,
  232. [OMAP_I2C_DATA_REG] = 0x9c,
  233. [OMAP_I2C_SYSC_REG] = 0x10,
  234. [OMAP_I2C_CON_REG] = 0xa4,
  235. [OMAP_I2C_OA_REG] = 0xa8,
  236. [OMAP_I2C_SA_REG] = 0xac,
  237. [OMAP_I2C_PSC_REG] = 0xb0,
  238. [OMAP_I2C_SCLL_REG] = 0xb4,
  239. [OMAP_I2C_SCLH_REG] = 0xb8,
  240. [OMAP_I2C_SYSTEST_REG] = 0xbC,
  241. [OMAP_I2C_BUFSTAT_REG] = 0xc0,
  242. [OMAP_I2C_IP_V2_REVNB_LO] = 0x00,
  243. [OMAP_I2C_IP_V2_REVNB_HI] = 0x04,
  244. [OMAP_I2C_IP_V2_IRQSTATUS_RAW] = 0x24,
  245. [OMAP_I2C_IP_V2_IRQENABLE_SET] = 0x2c,
  246. [OMAP_I2C_IP_V2_IRQENABLE_CLR] = 0x30,
  247. };
  248. static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
  249. int reg, u16 val)
  250. {
  251. writew_relaxed(val, i2c_dev->base +
  252. (i2c_dev->regs[reg] << i2c_dev->reg_shift));
  253. }
  254. static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
  255. {
  256. return readw_relaxed(i2c_dev->base +
  257. (i2c_dev->regs[reg] << i2c_dev->reg_shift));
  258. }
  259. static void __omap_i2c_init(struct omap_i2c_dev *dev)
  260. {
  261. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  262. /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
  263. omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, dev->pscstate);
  264. /* SCL low and high time values */
  265. omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, dev->scllstate);
  266. omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, dev->sclhstate);
  267. if (dev->rev >= OMAP_I2C_REV_ON_3430_3530)
  268. omap_i2c_write_reg(dev, OMAP_I2C_WE_REG, dev->westate);
  269. /* Take the I2C module out of reset: */
  270. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  271. /*
  272. * NOTE: right after setting CON_EN, STAT_BB could be 0 while the
  273. * bus is busy. It will be changed to 1 on the next IP FCLK clock.
  274. * udelay(1) will be enough to fix that.
  275. */
  276. /*
  277. * Don't write to this register if the IE state is 0 as it can
  278. * cause deadlock.
  279. */
  280. if (dev->iestate)
  281. omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
  282. }
  283. static int omap_i2c_reset(struct omap_i2c_dev *dev)
  284. {
  285. unsigned long timeout;
  286. u16 sysc;
  287. if (dev->rev >= OMAP_I2C_OMAP1_REV_2) {
  288. sysc = omap_i2c_read_reg(dev, OMAP_I2C_SYSC_REG);
  289. /* Disable I2C controller before soft reset */
  290. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
  291. omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) &
  292. ~(OMAP_I2C_CON_EN));
  293. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, SYSC_SOFTRESET_MASK);
  294. /* For some reason we need to set the EN bit before the
  295. * reset done bit gets set. */
  296. timeout = jiffies + OMAP_I2C_TIMEOUT;
  297. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  298. while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
  299. SYSS_RESETDONE_MASK)) {
  300. if (time_after(jiffies, timeout)) {
  301. dev_warn(dev->dev, "timeout waiting "
  302. "for controller reset\n");
  303. return -ETIMEDOUT;
  304. }
  305. msleep(1);
  306. }
  307. /* SYSC register is cleared by the reset; rewrite it */
  308. omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, sysc);
  309. if (dev->rev > OMAP_I2C_REV_ON_3430_3530) {
  310. /* Schedule I2C-bus monitoring on the next transfer */
  311. dev->bb_valid = 0;
  312. }
  313. }
  314. return 0;
  315. }
  316. static int omap_i2c_init(struct omap_i2c_dev *dev)
  317. {
  318. u16 psc = 0, scll = 0, sclh = 0;
  319. u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
  320. unsigned long fclk_rate = 12000000;
  321. unsigned long internal_clk = 0;
  322. struct clk *fclk;
  323. if (dev->rev >= OMAP_I2C_REV_ON_3430_3530) {
  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. }
  331. if (dev->flags & OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK) {
  332. /*
  333. * The I2C functional clock is the armxor_ck, so there's
  334. * no need to get "armxor_ck" separately. Now, if OMAP2420
  335. * always returns 12MHz for the functional clock, we can
  336. * do this bit unconditionally.
  337. */
  338. fclk = clk_get(dev->dev, "fck");
  339. fclk_rate = clk_get_rate(fclk);
  340. clk_put(fclk);
  341. /* TRM for 5912 says the I2C clock must be prescaled to be
  342. * between 7 - 12 MHz. The XOR input clock is typically
  343. * 12, 13 or 19.2 MHz. So we should have code that produces:
  344. *
  345. * XOR MHz Divider Prescaler
  346. * 12 1 0
  347. * 13 2 1
  348. * 19.2 2 1
  349. */
  350. if (fclk_rate > 12000000)
  351. psc = fclk_rate / 12000000;
  352. }
  353. if (!(dev->flags & OMAP_I2C_FLAG_SIMPLE_CLOCK)) {
  354. /*
  355. * HSI2C controller internal clk rate should be 19.2 Mhz for
  356. * HS and for all modes on 2430. On 34xx we can use lower rate
  357. * to get longer filter period for better noise suppression.
  358. * The filter is iclk (fclk for HS) period.
  359. */
  360. if (dev->speed > 400 ||
  361. dev->flags & OMAP_I2C_FLAG_FORCE_19200_INT_CLK)
  362. internal_clk = 19200;
  363. else if (dev->speed > 100)
  364. internal_clk = 9600;
  365. else
  366. internal_clk = 4000;
  367. fclk = clk_get(dev->dev, "fck");
  368. fclk_rate = clk_get_rate(fclk) / 1000;
  369. clk_put(fclk);
  370. /* Compute prescaler divisor */
  371. psc = fclk_rate / internal_clk;
  372. psc = psc - 1;
  373. /* If configured for High Speed */
  374. if (dev->speed > 400) {
  375. unsigned long scl;
  376. /* For first phase of HS mode */
  377. scl = internal_clk / 400;
  378. fsscll = scl - (scl / 3) - 7;
  379. fssclh = (scl / 3) - 5;
  380. /* For second phase of HS mode */
  381. scl = fclk_rate / dev->speed;
  382. hsscll = scl - (scl / 3) - 7;
  383. hssclh = (scl / 3) - 5;
  384. } else if (dev->speed > 100) {
  385. unsigned long scl;
  386. /* Fast mode */
  387. scl = internal_clk / dev->speed;
  388. fsscll = scl - (scl / 3) - 7;
  389. fssclh = (scl / 3) - 5;
  390. } else {
  391. /* Standard mode */
  392. fsscll = internal_clk / (dev->speed * 2) - 7;
  393. fssclh = internal_clk / (dev->speed * 2) - 5;
  394. }
  395. scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
  396. sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
  397. } else {
  398. /* Program desired operating rate */
  399. fclk_rate /= (psc + 1) * 1000;
  400. if (psc > 2)
  401. psc = 2;
  402. scll = fclk_rate / (dev->speed * 2) - 7 + psc;
  403. sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
  404. }
  405. dev->iestate = (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
  406. OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
  407. OMAP_I2C_IE_AL) | ((dev->fifo_size) ?
  408. (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0);
  409. dev->pscstate = psc;
  410. dev->scllstate = scll;
  411. dev->sclhstate = sclh;
  412. if (dev->rev <= OMAP_I2C_REV_ON_3430_3530) {
  413. /* Not implemented */
  414. dev->bb_valid = 1;
  415. }
  416. __omap_i2c_init(dev);
  417. return 0;
  418. }
  419. /*
  420. * Waiting on Bus Busy
  421. */
  422. static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
  423. {
  424. unsigned long timeout;
  425. timeout = jiffies + OMAP_I2C_TIMEOUT;
  426. while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
  427. if (time_after(jiffies, timeout)) {
  428. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  429. return -ETIMEDOUT;
  430. }
  431. msleep(1);
  432. }
  433. return 0;
  434. }
  435. /*
  436. * Wait while BB-bit doesn't reflect the I2C bus state
  437. *
  438. * In a multimaster environment, after IP software reset, BB-bit value doesn't
  439. * correspond to the current bus state. It may happen what BB-bit will be 0,
  440. * while the bus is busy due to another I2C master activity.
  441. * Here are BB-bit values after reset:
  442. * SDA SCL BB NOTES
  443. * 0 0 0 1, 2
  444. * 1 0 0 1, 2
  445. * 0 1 1
  446. * 1 1 0 3
  447. * Later, if IP detect SDA=0 and SCL=1 (ACK) or SDA 1->0 while SCL=1 (START)
  448. * combinations on the bus, it set BB-bit to 1.
  449. * If IP detect SDA 0->1 while SCL=1 (STOP) combination on the bus,
  450. * it set BB-bit to 0 and BF to 1.
  451. * BB and BF bits correctly tracks the bus state while IP is suspended
  452. * BB bit became valid on the next FCLK clock after CON_EN bit set
  453. *
  454. * NOTES:
  455. * 1. Any transfer started when BB=0 and bus is busy wouldn't be
  456. * completed by IP and results in controller timeout.
  457. * 2. Any transfer started when BB=0 and SCL=0 results in IP
  458. * starting to drive SDA low. In that case IP corrupt data
  459. * on the bus.
  460. * 3. Any transfer started in the middle of another master's transfer
  461. * results in unpredictable results and data corruption
  462. */
  463. static int omap_i2c_wait_for_bb_valid(struct omap_i2c_dev *dev)
  464. {
  465. unsigned long bus_free_timeout = 0;
  466. unsigned long timeout;
  467. int bus_free = 0;
  468. u16 stat, systest;
  469. if (dev->bb_valid)
  470. return 0;
  471. timeout = jiffies + OMAP_I2C_TIMEOUT;
  472. while (1) {
  473. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  474. /*
  475. * We will see BB or BF event in a case IP had detected any
  476. * activity on the I2C bus. Now IP correctly tracks the bus
  477. * state. BB-bit value is valid.
  478. */
  479. if (stat & (OMAP_I2C_STAT_BB | OMAP_I2C_STAT_BF))
  480. break;
  481. /*
  482. * Otherwise, we must look signals on the bus to make
  483. * the right decision.
  484. */
  485. systest = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  486. if ((systest & OMAP_I2C_SYSTEST_SCL_I_FUNC) &&
  487. (systest & OMAP_I2C_SYSTEST_SDA_I_FUNC)) {
  488. if (!bus_free) {
  489. bus_free_timeout = jiffies +
  490. OMAP_I2C_BUS_FREE_TIMEOUT;
  491. bus_free = 1;
  492. }
  493. /*
  494. * SDA and SCL lines was high for 10 ms without bus
  495. * activity detected. The bus is free. Consider
  496. * BB-bit value is valid.
  497. */
  498. if (time_after(jiffies, bus_free_timeout))
  499. break;
  500. } else {
  501. bus_free = 0;
  502. }
  503. if (time_after(jiffies, timeout)) {
  504. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  505. return -ETIMEDOUT;
  506. }
  507. msleep(1);
  508. }
  509. dev->bb_valid = 1;
  510. return 0;
  511. }
  512. static void omap_i2c_resize_fifo(struct omap_i2c_dev *dev, u8 size, bool is_rx)
  513. {
  514. u16 buf;
  515. if (dev->flags & OMAP_I2C_FLAG_NO_FIFO)
  516. return;
  517. /*
  518. * Set up notification threshold based on message size. We're doing
  519. * this to try and avoid draining feature as much as possible. Whenever
  520. * we have big messages to transfer (bigger than our total fifo size)
  521. * then we might use draining feature to transfer the remaining bytes.
  522. */
  523. dev->threshold = clamp(size, (u8) 1, dev->fifo_size);
  524. buf = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
  525. if (is_rx) {
  526. /* Clear RX Threshold */
  527. buf &= ~(0x3f << 8);
  528. buf |= ((dev->threshold - 1) << 8) | OMAP_I2C_BUF_RXFIF_CLR;
  529. } else {
  530. /* Clear TX Threshold */
  531. buf &= ~0x3f;
  532. buf |= (dev->threshold - 1) | OMAP_I2C_BUF_TXFIF_CLR;
  533. }
  534. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, buf);
  535. if (dev->rev < OMAP_I2C_REV_ON_3630)
  536. dev->b_hw = 1; /* Enable hardware fixes */
  537. /* calculate wakeup latency constraint for MPU */
  538. if (dev->set_mpu_wkup_lat != NULL)
  539. dev->latency = (1000000 * dev->threshold) /
  540. (1000 * dev->speed / 8);
  541. }
  542. /*
  543. * Low level master read/write transaction.
  544. */
  545. static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
  546. struct i2c_msg *msg, int stop)
  547. {
  548. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  549. unsigned long timeout;
  550. u16 w;
  551. dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  552. msg->addr, msg->len, msg->flags, stop);
  553. if (msg->len == 0)
  554. return -EINVAL;
  555. dev->receiver = !!(msg->flags & I2C_M_RD);
  556. omap_i2c_resize_fifo(dev, msg->len, dev->receiver);
  557. omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
  558. /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
  559. dev->buf = msg->buf;
  560. dev->buf_len = msg->len;
  561. /* make sure writes to dev->buf_len are ordered */
  562. barrier();
  563. omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
  564. /* Clear the FIFO Buffers */
  565. w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
  566. w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
  567. omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
  568. reinit_completion(&dev->cmd_complete);
  569. dev->cmd_err = 0;
  570. w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
  571. /* High speed configuration */
  572. if (dev->speed > 400)
  573. w |= OMAP_I2C_CON_OPMODE_HS;
  574. if (msg->flags & I2C_M_STOP)
  575. stop = 1;
  576. if (msg->flags & I2C_M_TEN)
  577. w |= OMAP_I2C_CON_XA;
  578. if (!(msg->flags & I2C_M_RD))
  579. w |= OMAP_I2C_CON_TRX;
  580. if (!dev->b_hw && stop)
  581. w |= OMAP_I2C_CON_STP;
  582. /*
  583. * NOTE: STAT_BB bit could became 1 here if another master occupy
  584. * the bus. IP successfully complete transfer when the bus will be
  585. * free again (BB reset to 0).
  586. */
  587. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  588. /*
  589. * Don't write stt and stp together on some hardware.
  590. */
  591. if (dev->b_hw && stop) {
  592. unsigned long delay = jiffies + OMAP_I2C_TIMEOUT;
  593. u16 con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  594. while (con & OMAP_I2C_CON_STT) {
  595. con = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  596. /* Let the user know if i2c is in a bad state */
  597. if (time_after(jiffies, delay)) {
  598. dev_err(dev->dev, "controller timed out "
  599. "waiting for start condition to finish\n");
  600. return -ETIMEDOUT;
  601. }
  602. cpu_relax();
  603. }
  604. w |= OMAP_I2C_CON_STP;
  605. w &= ~OMAP_I2C_CON_STT;
  606. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  607. }
  608. /*
  609. * REVISIT: We should abort the transfer on signals, but the bus goes
  610. * into arbitration and we're currently unable to recover from it.
  611. */
  612. timeout = wait_for_completion_timeout(&dev->cmd_complete,
  613. OMAP_I2C_TIMEOUT);
  614. if (timeout == 0) {
  615. dev_err(dev->dev, "controller timed out\n");
  616. omap_i2c_reset(dev);
  617. __omap_i2c_init(dev);
  618. return -ETIMEDOUT;
  619. }
  620. if (likely(!dev->cmd_err))
  621. return 0;
  622. /* We have an error */
  623. if (dev->cmd_err & (OMAP_I2C_STAT_ROVR | OMAP_I2C_STAT_XUDF)) {
  624. omap_i2c_reset(dev);
  625. __omap_i2c_init(dev);
  626. return -EIO;
  627. }
  628. if (dev->cmd_err & OMAP_I2C_STAT_AL)
  629. return -EAGAIN;
  630. if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
  631. if (msg->flags & I2C_M_IGNORE_NAK)
  632. return 0;
  633. w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
  634. w |= OMAP_I2C_CON_STP;
  635. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
  636. return -EREMOTEIO;
  637. }
  638. return -EIO;
  639. }
  640. /*
  641. * Prepare controller for a transaction and call omap_i2c_xfer_msg
  642. * to do the work during IRQ processing.
  643. */
  644. static int
  645. omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  646. {
  647. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  648. int i;
  649. int r;
  650. r = pm_runtime_get_sync(dev->dev);
  651. if (r < 0)
  652. goto out;
  653. r = omap_i2c_wait_for_bb_valid(dev);
  654. if (r < 0)
  655. goto out;
  656. r = omap_i2c_wait_for_bb(dev);
  657. if (r < 0)
  658. goto out;
  659. if (dev->set_mpu_wkup_lat != NULL)
  660. dev->set_mpu_wkup_lat(dev->dev, dev->latency);
  661. for (i = 0; i < num; i++) {
  662. r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  663. if (r != 0)
  664. break;
  665. }
  666. if (r == 0)
  667. r = num;
  668. omap_i2c_wait_for_bb(dev);
  669. if (dev->set_mpu_wkup_lat != NULL)
  670. dev->set_mpu_wkup_lat(dev->dev, -1);
  671. out:
  672. pm_runtime_mark_last_busy(dev->dev);
  673. pm_runtime_put_autosuspend(dev->dev);
  674. return r;
  675. }
  676. static u32
  677. omap_i2c_func(struct i2c_adapter *adap)
  678. {
  679. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  680. I2C_FUNC_PROTOCOL_MANGLING;
  681. }
  682. static inline void
  683. omap_i2c_complete_cmd(struct omap_i2c_dev *dev, u16 err)
  684. {
  685. dev->cmd_err |= err;
  686. complete(&dev->cmd_complete);
  687. }
  688. static inline void
  689. omap_i2c_ack_stat(struct omap_i2c_dev *dev, u16 stat)
  690. {
  691. omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
  692. }
  693. static inline void i2c_omap_errata_i207(struct omap_i2c_dev *dev, u16 stat)
  694. {
  695. /*
  696. * I2C Errata(Errata Nos. OMAP2: 1.67, OMAP3: 1.8)
  697. * Not applicable for OMAP4.
  698. * Under certain rare conditions, RDR could be set again
  699. * when the bus is busy, then ignore the interrupt and
  700. * clear the interrupt.
  701. */
  702. if (stat & OMAP_I2C_STAT_RDR) {
  703. /* Step 1: If RDR is set, clear it */
  704. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  705. /* Step 2: */
  706. if (!(omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  707. & OMAP_I2C_STAT_BB)) {
  708. /* Step 3: */
  709. if (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG)
  710. & OMAP_I2C_STAT_RDR) {
  711. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  712. dev_dbg(dev->dev, "RDR when bus is busy.\n");
  713. }
  714. }
  715. }
  716. }
  717. /* rev1 devices are apparently only on some 15xx */
  718. #ifdef CONFIG_ARCH_OMAP15XX
  719. static irqreturn_t
  720. omap_i2c_omap1_isr(int this_irq, void *dev_id)
  721. {
  722. struct omap_i2c_dev *dev = dev_id;
  723. u16 iv, w;
  724. if (pm_runtime_suspended(dev->dev))
  725. return IRQ_NONE;
  726. iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
  727. switch (iv) {
  728. case 0x00: /* None */
  729. break;
  730. case 0x01: /* Arbitration lost */
  731. dev_err(dev->dev, "Arbitration lost\n");
  732. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
  733. break;
  734. case 0x02: /* No acknowledgement */
  735. omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
  736. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
  737. break;
  738. case 0x03: /* Register access ready */
  739. omap_i2c_complete_cmd(dev, 0);
  740. break;
  741. case 0x04: /* Receive data ready */
  742. if (dev->buf_len) {
  743. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  744. *dev->buf++ = w;
  745. dev->buf_len--;
  746. if (dev->buf_len) {
  747. *dev->buf++ = w >> 8;
  748. dev->buf_len--;
  749. }
  750. } else
  751. dev_err(dev->dev, "RRDY IRQ while no data requested\n");
  752. break;
  753. case 0x05: /* Transmit data ready */
  754. if (dev->buf_len) {
  755. w = *dev->buf++;
  756. dev->buf_len--;
  757. if (dev->buf_len) {
  758. w |= *dev->buf++ << 8;
  759. dev->buf_len--;
  760. }
  761. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  762. } else
  763. dev_err(dev->dev, "XRDY IRQ while no data to send\n");
  764. break;
  765. default:
  766. return IRQ_NONE;
  767. }
  768. return IRQ_HANDLED;
  769. }
  770. #else
  771. #define omap_i2c_omap1_isr NULL
  772. #endif
  773. /*
  774. * OMAP3430 Errata i462: When an XRDY/XDR is hit, wait for XUDF before writing
  775. * data to DATA_REG. Otherwise some data bytes can be lost while transferring
  776. * them from the memory to the I2C interface.
  777. */
  778. static int errata_omap3_i462(struct omap_i2c_dev *dev)
  779. {
  780. unsigned long timeout = 10000;
  781. u16 stat;
  782. do {
  783. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  784. if (stat & OMAP_I2C_STAT_XUDF)
  785. break;
  786. if (stat & (OMAP_I2C_STAT_NACK | OMAP_I2C_STAT_AL)) {
  787. omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_XRDY |
  788. OMAP_I2C_STAT_XDR));
  789. if (stat & OMAP_I2C_STAT_NACK) {
  790. dev->cmd_err |= OMAP_I2C_STAT_NACK;
  791. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
  792. }
  793. if (stat & OMAP_I2C_STAT_AL) {
  794. dev_err(dev->dev, "Arbitration lost\n");
  795. dev->cmd_err |= OMAP_I2C_STAT_AL;
  796. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_AL);
  797. }
  798. return -EIO;
  799. }
  800. cpu_relax();
  801. } while (--timeout);
  802. if (!timeout) {
  803. dev_err(dev->dev, "timeout waiting on XUDF bit\n");
  804. return 0;
  805. }
  806. return 0;
  807. }
  808. static void omap_i2c_receive_data(struct omap_i2c_dev *dev, u8 num_bytes,
  809. bool is_rdr)
  810. {
  811. u16 w;
  812. while (num_bytes--) {
  813. w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
  814. *dev->buf++ = w;
  815. dev->buf_len--;
  816. /*
  817. * Data reg in 2430, omap3 and
  818. * omap4 is 8 bit wide
  819. */
  820. if (dev->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  821. *dev->buf++ = w >> 8;
  822. dev->buf_len--;
  823. }
  824. }
  825. }
  826. static int omap_i2c_transmit_data(struct omap_i2c_dev *dev, u8 num_bytes,
  827. bool is_xdr)
  828. {
  829. u16 w;
  830. while (num_bytes--) {
  831. w = *dev->buf++;
  832. dev->buf_len--;
  833. /*
  834. * Data reg in 2430, omap3 and
  835. * omap4 is 8 bit wide
  836. */
  837. if (dev->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  838. w |= *dev->buf++ << 8;
  839. dev->buf_len--;
  840. }
  841. if (dev->errata & I2C_OMAP_ERRATA_I462) {
  842. int ret;
  843. ret = errata_omap3_i462(dev);
  844. if (ret < 0)
  845. return ret;
  846. }
  847. omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
  848. }
  849. return 0;
  850. }
  851. static irqreturn_t
  852. omap_i2c_isr(int irq, void *dev_id)
  853. {
  854. struct omap_i2c_dev *dev = dev_id;
  855. irqreturn_t ret = IRQ_HANDLED;
  856. u16 mask;
  857. u16 stat;
  858. spin_lock(&dev->lock);
  859. mask = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  860. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  861. if (stat & mask)
  862. ret = IRQ_WAKE_THREAD;
  863. spin_unlock(&dev->lock);
  864. return ret;
  865. }
  866. static irqreturn_t
  867. omap_i2c_isr_thread(int this_irq, void *dev_id)
  868. {
  869. struct omap_i2c_dev *dev = dev_id;
  870. unsigned long flags;
  871. u16 bits;
  872. u16 stat;
  873. int err = 0, count = 0;
  874. spin_lock_irqsave(&dev->lock, flags);
  875. do {
  876. bits = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
  877. stat = omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG);
  878. stat &= bits;
  879. /* If we're in receiver mode, ignore XDR/XRDY */
  880. if (dev->receiver)
  881. stat &= ~(OMAP_I2C_STAT_XDR | OMAP_I2C_STAT_XRDY);
  882. else
  883. stat &= ~(OMAP_I2C_STAT_RDR | OMAP_I2C_STAT_RRDY);
  884. if (!stat) {
  885. /* my work here is done */
  886. goto out;
  887. }
  888. dev_dbg(dev->dev, "IRQ (ISR = 0x%04x)\n", stat);
  889. if (count++ == 100) {
  890. dev_warn(dev->dev, "Too much work in one IRQ\n");
  891. break;
  892. }
  893. if (stat & OMAP_I2C_STAT_NACK) {
  894. err |= OMAP_I2C_STAT_NACK;
  895. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_NACK);
  896. }
  897. if (stat & OMAP_I2C_STAT_AL) {
  898. dev_err(dev->dev, "Arbitration lost\n");
  899. err |= OMAP_I2C_STAT_AL;
  900. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_AL);
  901. }
  902. /*
  903. * ProDB0017052: Clear ARDY bit twice
  904. */
  905. if (stat & OMAP_I2C_STAT_ARDY)
  906. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_ARDY);
  907. if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
  908. OMAP_I2C_STAT_AL)) {
  909. omap_i2c_ack_stat(dev, (OMAP_I2C_STAT_RRDY |
  910. OMAP_I2C_STAT_RDR |
  911. OMAP_I2C_STAT_XRDY |
  912. OMAP_I2C_STAT_XDR |
  913. OMAP_I2C_STAT_ARDY));
  914. break;
  915. }
  916. if (stat & OMAP_I2C_STAT_RDR) {
  917. u8 num_bytes = 1;
  918. if (dev->fifo_size)
  919. num_bytes = dev->buf_len;
  920. if (dev->errata & I2C_OMAP_ERRATA_I207) {
  921. i2c_omap_errata_i207(dev, stat);
  922. num_bytes = (omap_i2c_read_reg(dev,
  923. OMAP_I2C_BUFSTAT_REG) >> 8) & 0x3F;
  924. }
  925. omap_i2c_receive_data(dev, num_bytes, true);
  926. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RDR);
  927. continue;
  928. }
  929. if (stat & OMAP_I2C_STAT_RRDY) {
  930. u8 num_bytes = 1;
  931. if (dev->threshold)
  932. num_bytes = dev->threshold;
  933. omap_i2c_receive_data(dev, num_bytes, false);
  934. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RRDY);
  935. continue;
  936. }
  937. if (stat & OMAP_I2C_STAT_XDR) {
  938. u8 num_bytes = 1;
  939. int ret;
  940. if (dev->fifo_size)
  941. num_bytes = dev->buf_len;
  942. ret = omap_i2c_transmit_data(dev, num_bytes, true);
  943. if (ret < 0)
  944. break;
  945. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XDR);
  946. continue;
  947. }
  948. if (stat & OMAP_I2C_STAT_XRDY) {
  949. u8 num_bytes = 1;
  950. int ret;
  951. if (dev->threshold)
  952. num_bytes = dev->threshold;
  953. ret = omap_i2c_transmit_data(dev, num_bytes, false);
  954. if (ret < 0)
  955. break;
  956. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XRDY);
  957. continue;
  958. }
  959. if (stat & OMAP_I2C_STAT_ROVR) {
  960. dev_err(dev->dev, "Receive overrun\n");
  961. err |= OMAP_I2C_STAT_ROVR;
  962. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_ROVR);
  963. break;
  964. }
  965. if (stat & OMAP_I2C_STAT_XUDF) {
  966. dev_err(dev->dev, "Transmit underflow\n");
  967. err |= OMAP_I2C_STAT_XUDF;
  968. omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XUDF);
  969. break;
  970. }
  971. } while (stat);
  972. omap_i2c_complete_cmd(dev, err);
  973. out:
  974. spin_unlock_irqrestore(&dev->lock, flags);
  975. return IRQ_HANDLED;
  976. }
  977. static const struct i2c_algorithm omap_i2c_algo = {
  978. .master_xfer = omap_i2c_xfer,
  979. .functionality = omap_i2c_func,
  980. };
  981. #ifdef CONFIG_OF
  982. static struct omap_i2c_bus_platform_data omap2420_pdata = {
  983. .rev = OMAP_I2C_IP_VERSION_1,
  984. .flags = OMAP_I2C_FLAG_NO_FIFO |
  985. OMAP_I2C_FLAG_SIMPLE_CLOCK |
  986. OMAP_I2C_FLAG_16BIT_DATA_REG |
  987. OMAP_I2C_FLAG_BUS_SHIFT_2,
  988. };
  989. static struct omap_i2c_bus_platform_data omap2430_pdata = {
  990. .rev = OMAP_I2C_IP_VERSION_1,
  991. .flags = OMAP_I2C_FLAG_BUS_SHIFT_2 |
  992. OMAP_I2C_FLAG_FORCE_19200_INT_CLK,
  993. };
  994. static struct omap_i2c_bus_platform_data omap3_pdata = {
  995. .rev = OMAP_I2C_IP_VERSION_1,
  996. .flags = OMAP_I2C_FLAG_BUS_SHIFT_2,
  997. };
  998. static struct omap_i2c_bus_platform_data omap4_pdata = {
  999. .rev = OMAP_I2C_IP_VERSION_2,
  1000. };
  1001. static const struct of_device_id omap_i2c_of_match[] = {
  1002. {
  1003. .compatible = "ti,omap4-i2c",
  1004. .data = &omap4_pdata,
  1005. },
  1006. {
  1007. .compatible = "ti,omap3-i2c",
  1008. .data = &omap3_pdata,
  1009. },
  1010. {
  1011. .compatible = "ti,omap2430-i2c",
  1012. .data = &omap2430_pdata,
  1013. },
  1014. {
  1015. .compatible = "ti,omap2420-i2c",
  1016. .data = &omap2420_pdata,
  1017. },
  1018. { },
  1019. };
  1020. MODULE_DEVICE_TABLE(of, omap_i2c_of_match);
  1021. #endif
  1022. #define OMAP_I2C_SCHEME(rev) ((rev & 0xc000) >> 14)
  1023. #define OMAP_I2C_REV_SCHEME_0_MAJOR(rev) (rev >> 4)
  1024. #define OMAP_I2C_REV_SCHEME_0_MINOR(rev) (rev & 0xf)
  1025. #define OMAP_I2C_REV_SCHEME_1_MAJOR(rev) ((rev & 0x0700) >> 7)
  1026. #define OMAP_I2C_REV_SCHEME_1_MINOR(rev) (rev & 0x1f)
  1027. #define OMAP_I2C_SCHEME_0 0
  1028. #define OMAP_I2C_SCHEME_1 1
  1029. static int
  1030. omap_i2c_probe(struct platform_device *pdev)
  1031. {
  1032. struct omap_i2c_dev *dev;
  1033. struct i2c_adapter *adap;
  1034. struct resource *mem;
  1035. const struct omap_i2c_bus_platform_data *pdata =
  1036. dev_get_platdata(&pdev->dev);
  1037. struct device_node *node = pdev->dev.of_node;
  1038. const struct of_device_id *match;
  1039. int irq;
  1040. int r;
  1041. u32 rev;
  1042. u16 minor, major;
  1043. irq = platform_get_irq(pdev, 0);
  1044. if (irq < 0) {
  1045. dev_err(&pdev->dev, "no irq resource?\n");
  1046. return irq;
  1047. }
  1048. dev = devm_kzalloc(&pdev->dev, sizeof(struct omap_i2c_dev), GFP_KERNEL);
  1049. if (!dev)
  1050. return -ENOMEM;
  1051. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1052. dev->base = devm_ioremap_resource(&pdev->dev, mem);
  1053. if (IS_ERR(dev->base))
  1054. return PTR_ERR(dev->base);
  1055. match = of_match_device(of_match_ptr(omap_i2c_of_match), &pdev->dev);
  1056. if (match) {
  1057. u32 freq = 100000; /* default to 100000 Hz */
  1058. pdata = match->data;
  1059. dev->flags = pdata->flags;
  1060. of_property_read_u32(node, "clock-frequency", &freq);
  1061. /* convert DT freq value in Hz into kHz for speed */
  1062. dev->speed = freq / 1000;
  1063. } else if (pdata != NULL) {
  1064. dev->speed = pdata->clkrate;
  1065. dev->flags = pdata->flags;
  1066. dev->set_mpu_wkup_lat = pdata->set_mpu_wkup_lat;
  1067. }
  1068. dev->dev = &pdev->dev;
  1069. dev->irq = irq;
  1070. spin_lock_init(&dev->lock);
  1071. platform_set_drvdata(pdev, dev);
  1072. init_completion(&dev->cmd_complete);
  1073. dev->reg_shift = (dev->flags >> OMAP_I2C_FLAG_BUS_SHIFT__SHIFT) & 3;
  1074. pm_runtime_enable(dev->dev);
  1075. pm_runtime_set_autosuspend_delay(dev->dev, OMAP_I2C_PM_TIMEOUT);
  1076. pm_runtime_use_autosuspend(dev->dev);
  1077. r = pm_runtime_get_sync(dev->dev);
  1078. if (r < 0)
  1079. goto err_free_mem;
  1080. /*
  1081. * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2.
  1082. * On omap1/3/2 Offset 4 is IE Reg the bit [15:14] is 0 at reset.
  1083. * Also since the omap_i2c_read_reg uses reg_map_ip_* a
  1084. * readw_relaxed is done.
  1085. */
  1086. rev = readw_relaxed(dev->base + 0x04);
  1087. dev->scheme = OMAP_I2C_SCHEME(rev);
  1088. switch (dev->scheme) {
  1089. case OMAP_I2C_SCHEME_0:
  1090. dev->regs = (u8 *)reg_map_ip_v1;
  1091. dev->rev = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG);
  1092. minor = OMAP_I2C_REV_SCHEME_0_MAJOR(dev->rev);
  1093. major = OMAP_I2C_REV_SCHEME_0_MAJOR(dev->rev);
  1094. break;
  1095. case OMAP_I2C_SCHEME_1:
  1096. /* FALLTHROUGH */
  1097. default:
  1098. dev->regs = (u8 *)reg_map_ip_v2;
  1099. rev = (rev << 16) |
  1100. omap_i2c_read_reg(dev, OMAP_I2C_IP_V2_REVNB_LO);
  1101. minor = OMAP_I2C_REV_SCHEME_1_MINOR(rev);
  1102. major = OMAP_I2C_REV_SCHEME_1_MAJOR(rev);
  1103. dev->rev = rev;
  1104. }
  1105. dev->errata = 0;
  1106. if (dev->rev >= OMAP_I2C_REV_ON_2430 &&
  1107. dev->rev < OMAP_I2C_REV_ON_4430_PLUS)
  1108. dev->errata |= I2C_OMAP_ERRATA_I207;
  1109. if (dev->rev <= OMAP_I2C_REV_ON_3430_3530)
  1110. dev->errata |= I2C_OMAP_ERRATA_I462;
  1111. if (!(dev->flags & OMAP_I2C_FLAG_NO_FIFO)) {
  1112. u16 s;
  1113. /* Set up the fifo size - Get total size */
  1114. s = (omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3;
  1115. dev->fifo_size = 0x8 << s;
  1116. /*
  1117. * Set up notification threshold as half the total available
  1118. * size. This is to ensure that we can handle the status on int
  1119. * call back latencies.
  1120. */
  1121. dev->fifo_size = (dev->fifo_size / 2);
  1122. if (dev->rev < OMAP_I2C_REV_ON_3630)
  1123. dev->b_hw = 1; /* Enable hardware fixes */
  1124. /* calculate wakeup latency constraint for MPU */
  1125. if (dev->set_mpu_wkup_lat != NULL)
  1126. dev->latency = (1000000 * dev->fifo_size) /
  1127. (1000 * dev->speed / 8);
  1128. }
  1129. /* reset ASAP, clearing any IRQs */
  1130. omap_i2c_init(dev);
  1131. if (dev->rev < OMAP_I2C_OMAP1_REV_2)
  1132. r = devm_request_irq(&pdev->dev, dev->irq, omap_i2c_omap1_isr,
  1133. IRQF_NO_SUSPEND, pdev->name, dev);
  1134. else
  1135. r = devm_request_threaded_irq(&pdev->dev, dev->irq,
  1136. omap_i2c_isr, omap_i2c_isr_thread,
  1137. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1138. pdev->name, dev);
  1139. if (r) {
  1140. dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
  1141. goto err_unuse_clocks;
  1142. }
  1143. adap = &dev->adapter;
  1144. i2c_set_adapdata(adap, dev);
  1145. adap->owner = THIS_MODULE;
  1146. adap->class = I2C_CLASS_DEPRECATED;
  1147. strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  1148. adap->algo = &omap_i2c_algo;
  1149. adap->dev.parent = &pdev->dev;
  1150. adap->dev.of_node = pdev->dev.of_node;
  1151. /* i2c device drivers may be active on return from add_adapter() */
  1152. adap->nr = pdev->id;
  1153. r = i2c_add_numbered_adapter(adap);
  1154. if (r) {
  1155. dev_err(dev->dev, "failure adding adapter\n");
  1156. goto err_unuse_clocks;
  1157. }
  1158. dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n", adap->nr,
  1159. major, minor, dev->speed);
  1160. pm_runtime_mark_last_busy(dev->dev);
  1161. pm_runtime_put_autosuspend(dev->dev);
  1162. return 0;
  1163. err_unuse_clocks:
  1164. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  1165. pm_runtime_put(dev->dev);
  1166. pm_runtime_disable(&pdev->dev);
  1167. err_free_mem:
  1168. return r;
  1169. }
  1170. static int omap_i2c_remove(struct platform_device *pdev)
  1171. {
  1172. struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
  1173. int ret;
  1174. i2c_del_adapter(&dev->adapter);
  1175. ret = pm_runtime_get_sync(&pdev->dev);
  1176. if (ret < 0)
  1177. return ret;
  1178. omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
  1179. pm_runtime_put(&pdev->dev);
  1180. pm_runtime_disable(&pdev->dev);
  1181. return 0;
  1182. }
  1183. #ifdef CONFIG_PM
  1184. static int omap_i2c_runtime_suspend(struct device *dev)
  1185. {
  1186. struct platform_device *pdev = to_platform_device(dev);
  1187. struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
  1188. _dev->iestate = omap_i2c_read_reg(_dev, OMAP_I2C_IE_REG);
  1189. if (_dev->scheme == OMAP_I2C_SCHEME_0)
  1190. omap_i2c_write_reg(_dev, OMAP_I2C_IE_REG, 0);
  1191. else
  1192. omap_i2c_write_reg(_dev, OMAP_I2C_IP_V2_IRQENABLE_CLR,
  1193. OMAP_I2C_IP_V2_INTERRUPTS_MASK);
  1194. if (_dev->rev < OMAP_I2C_OMAP1_REV_2) {
  1195. omap_i2c_read_reg(_dev, OMAP_I2C_IV_REG); /* Read clears */
  1196. } else {
  1197. omap_i2c_write_reg(_dev, OMAP_I2C_STAT_REG, _dev->iestate);
  1198. /* Flush posted write */
  1199. omap_i2c_read_reg(_dev, OMAP_I2C_STAT_REG);
  1200. }
  1201. return 0;
  1202. }
  1203. static int omap_i2c_runtime_resume(struct device *dev)
  1204. {
  1205. struct platform_device *pdev = to_platform_device(dev);
  1206. struct omap_i2c_dev *_dev = platform_get_drvdata(pdev);
  1207. if (!_dev->regs)
  1208. return 0;
  1209. __omap_i2c_init(_dev);
  1210. return 0;
  1211. }
  1212. static struct dev_pm_ops omap_i2c_pm_ops = {
  1213. SET_RUNTIME_PM_OPS(omap_i2c_runtime_suspend,
  1214. omap_i2c_runtime_resume, NULL)
  1215. };
  1216. #define OMAP_I2C_PM_OPS (&omap_i2c_pm_ops)
  1217. #else
  1218. #define OMAP_I2C_PM_OPS NULL
  1219. #endif /* CONFIG_PM */
  1220. static struct platform_driver omap_i2c_driver = {
  1221. .probe = omap_i2c_probe,
  1222. .remove = omap_i2c_remove,
  1223. .driver = {
  1224. .name = "omap_i2c",
  1225. .pm = OMAP_I2C_PM_OPS,
  1226. .of_match_table = of_match_ptr(omap_i2c_of_match),
  1227. },
  1228. };
  1229. /* I2C may be needed to bring up other drivers */
  1230. static int __init
  1231. omap_i2c_init_driver(void)
  1232. {
  1233. return platform_driver_register(&omap_i2c_driver);
  1234. }
  1235. subsys_initcall(omap_i2c_init_driver);
  1236. static void __exit omap_i2c_exit_driver(void)
  1237. {
  1238. platform_driver_unregister(&omap_i2c_driver);
  1239. }
  1240. module_exit(omap_i2c_exit_driver);
  1241. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1242. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1243. MODULE_LICENSE("GPL");
  1244. MODULE_ALIAS("platform:omap_i2c");