i2c-omap.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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/platform_data/i2c-omap.h>
  39. #include <linux/pm_runtime.h>
  40. #include <linux/pinctrl/consumer.h>
  41. /* I2C controller revisions */
  42. #define OMAP_I2C_OMAP1_REV_2 0x20
  43. /* I2C controller revisions present on specific hardware */
  44. #define OMAP_I2C_REV_ON_2430 0x00000036
  45. #define OMAP_I2C_REV_ON_3430_3530 0x0000003C
  46. #define OMAP_I2C_REV_ON_3630 0x00000040
  47. #define OMAP_I2C_REV_ON_4430_PLUS 0x50400002
  48. /* timeout waiting for the controller to respond */
  49. #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
  50. /* timeout for pm runtime autosuspend */
  51. #define OMAP_I2C_PM_TIMEOUT 1000 /* ms */
  52. /* timeout for making decision on bus free status */
  53. #define OMAP_I2C_BUS_FREE_TIMEOUT (msecs_to_jiffies(10))
  54. /* For OMAP3 I2C_IV has changed to I2C_WE (wakeup enable) */
  55. enum {
  56. OMAP_I2C_REV_REG = 0,
  57. OMAP_I2C_IE_REG,
  58. OMAP_I2C_STAT_REG,
  59. OMAP_I2C_IV_REG,
  60. OMAP_I2C_WE_REG,
  61. OMAP_I2C_SYSS_REG,
  62. OMAP_I2C_BUF_REG,
  63. OMAP_I2C_CNT_REG,
  64. OMAP_I2C_DATA_REG,
  65. OMAP_I2C_SYSC_REG,
  66. OMAP_I2C_CON_REG,
  67. OMAP_I2C_OA_REG,
  68. OMAP_I2C_SA_REG,
  69. OMAP_I2C_PSC_REG,
  70. OMAP_I2C_SCLL_REG,
  71. OMAP_I2C_SCLH_REG,
  72. OMAP_I2C_SYSTEST_REG,
  73. OMAP_I2C_BUFSTAT_REG,
  74. /* only on OMAP4430 */
  75. OMAP_I2C_IP_V2_REVNB_LO,
  76. OMAP_I2C_IP_V2_REVNB_HI,
  77. OMAP_I2C_IP_V2_IRQSTATUS_RAW,
  78. OMAP_I2C_IP_V2_IRQENABLE_SET,
  79. OMAP_I2C_IP_V2_IRQENABLE_CLR,
  80. };
  81. /* I2C Interrupt Enable Register (OMAP_I2C_IE): */
  82. #define OMAP_I2C_IE_XDR (1 << 14) /* TX Buffer drain int enable */
  83. #define OMAP_I2C_IE_RDR (1 << 13) /* RX Buffer drain int enable */
  84. #define OMAP_I2C_IE_XRDY (1 << 4) /* TX data ready int enable */
  85. #define OMAP_I2C_IE_RRDY (1 << 3) /* RX data ready int enable */
  86. #define OMAP_I2C_IE_ARDY (1 << 2) /* Access ready int enable */
  87. #define OMAP_I2C_IE_NACK (1 << 1) /* No ack interrupt enable */
  88. #define OMAP_I2C_IE_AL (1 << 0) /* Arbitration lost int ena */
  89. /* I2C Status Register (OMAP_I2C_STAT): */
  90. #define OMAP_I2C_STAT_XDR (1 << 14) /* TX Buffer draining */
  91. #define OMAP_I2C_STAT_RDR (1 << 13) /* RX Buffer draining */
  92. #define OMAP_I2C_STAT_BB (1 << 12) /* Bus busy */
  93. #define OMAP_I2C_STAT_ROVR (1 << 11) /* Receive overrun */
  94. #define OMAP_I2C_STAT_XUDF (1 << 10) /* Transmit underflow */
  95. #define OMAP_I2C_STAT_AAS (1 << 9) /* Address as slave */
  96. #define OMAP_I2C_STAT_BF (1 << 8) /* Bus Free */
  97. #define OMAP_I2C_STAT_XRDY (1 << 4) /* Transmit data ready */
  98. #define OMAP_I2C_STAT_RRDY (1 << 3) /* Receive data ready */
  99. #define OMAP_I2C_STAT_ARDY (1 << 2) /* Register access ready */
  100. #define OMAP_I2C_STAT_NACK (1 << 1) /* No ack interrupt enable */
  101. #define OMAP_I2C_STAT_AL (1 << 0) /* Arbitration lost int ena */
  102. /* I2C WE wakeup enable register */
  103. #define OMAP_I2C_WE_XDR_WE (1 << 14) /* TX drain wakup */
  104. #define OMAP_I2C_WE_RDR_WE (1 << 13) /* RX drain wakeup */
  105. #define OMAP_I2C_WE_AAS_WE (1 << 9) /* Address as slave wakeup*/
  106. #define OMAP_I2C_WE_BF_WE (1 << 8) /* Bus free wakeup */
  107. #define OMAP_I2C_WE_STC_WE (1 << 6) /* Start condition wakeup */
  108. #define OMAP_I2C_WE_GC_WE (1 << 5) /* General call wakeup */
  109. #define OMAP_I2C_WE_DRDY_WE (1 << 3) /* TX/RX data ready wakeup */
  110. #define OMAP_I2C_WE_ARDY_WE (1 << 2) /* Reg access ready wakeup */
  111. #define OMAP_I2C_WE_NACK_WE (1 << 1) /* No acknowledgment wakeup */
  112. #define OMAP_I2C_WE_AL_WE (1 << 0) /* Arbitration lost wakeup */
  113. #define OMAP_I2C_WE_ALL (OMAP_I2C_WE_XDR_WE | OMAP_I2C_WE_RDR_WE | \
  114. OMAP_I2C_WE_AAS_WE | OMAP_I2C_WE_BF_WE | \
  115. OMAP_I2C_WE_STC_WE | OMAP_I2C_WE_GC_WE | \
  116. OMAP_I2C_WE_DRDY_WE | OMAP_I2C_WE_ARDY_WE | \
  117. OMAP_I2C_WE_NACK_WE | OMAP_I2C_WE_AL_WE)
  118. /* I2C Buffer Configuration Register (OMAP_I2C_BUF): */
  119. #define OMAP_I2C_BUF_RDMA_EN (1 << 15) /* RX DMA channel enable */
  120. #define OMAP_I2C_BUF_RXFIF_CLR (1 << 14) /* RX FIFO Clear */
  121. #define OMAP_I2C_BUF_XDMA_EN (1 << 7) /* TX DMA channel enable */
  122. #define OMAP_I2C_BUF_TXFIF_CLR (1 << 6) /* TX FIFO Clear */
  123. /* I2C Configuration Register (OMAP_I2C_CON): */
  124. #define OMAP_I2C_CON_EN (1 << 15) /* I2C module enable */
  125. #define OMAP_I2C_CON_BE (1 << 14) /* Big endian mode */
  126. #define OMAP_I2C_CON_OPMODE_HS (1 << 12) /* High Speed support */
  127. #define OMAP_I2C_CON_STB (1 << 11) /* Start byte mode (master) */
  128. #define OMAP_I2C_CON_MST (1 << 10) /* Master/slave mode */
  129. #define OMAP_I2C_CON_TRX (1 << 9) /* TX/RX mode (master only) */
  130. #define OMAP_I2C_CON_XA (1 << 8) /* Expand address */
  131. #define OMAP_I2C_CON_RM (1 << 2) /* Repeat mode (master only) */
  132. #define OMAP_I2C_CON_STP (1 << 1) /* Stop cond (master only) */
  133. #define OMAP_I2C_CON_STT (1 << 0) /* Start condition (master) */
  134. /* I2C SCL time value when Master */
  135. #define OMAP_I2C_SCLL_HSSCLL 8
  136. #define OMAP_I2C_SCLH_HSSCLH 8
  137. /* I2C System Test Register (OMAP_I2C_SYSTEST): */
  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. /* Functional mode */
  143. #define OMAP_I2C_SYSTEST_SCL_I_FUNC (1 << 8) /* SCL line input value */
  144. #define OMAP_I2C_SYSTEST_SCL_O_FUNC (1 << 7) /* SCL line output value */
  145. #define OMAP_I2C_SYSTEST_SDA_I_FUNC (1 << 6) /* SDA line input value */
  146. #define OMAP_I2C_SYSTEST_SDA_O_FUNC (1 << 5) /* SDA line output value */
  147. /* SDA/SCL IO mode */
  148. #define OMAP_I2C_SYSTEST_SCL_I (1 << 3) /* SCL line sense in */
  149. #define OMAP_I2C_SYSTEST_SCL_O (1 << 2) /* SCL line drive out */
  150. #define OMAP_I2C_SYSTEST_SDA_I (1 << 1) /* SDA line sense in */
  151. #define OMAP_I2C_SYSTEST_SDA_O (1 << 0) /* SDA line drive out */
  152. /* OCP_SYSSTATUS bit definitions */
  153. #define SYSS_RESETDONE_MASK (1 << 0)
  154. /* OCP_SYSCONFIG bit definitions */
  155. #define SYSC_CLOCKACTIVITY_MASK (0x3 << 8)
  156. #define SYSC_SIDLEMODE_MASK (0x3 << 3)
  157. #define SYSC_ENAWAKEUP_MASK (1 << 2)
  158. #define SYSC_SOFTRESET_MASK (1 << 1)
  159. #define SYSC_AUTOIDLE_MASK (1 << 0)
  160. #define SYSC_IDLEMODE_SMART 0x2
  161. #define SYSC_CLOCKACTIVITY_FCLK 0x2
  162. /* Errata definitions */
  163. #define I2C_OMAP_ERRATA_I207 (1 << 0)
  164. #define I2C_OMAP_ERRATA_I462 (1 << 1)
  165. #define OMAP_I2C_IP_V2_INTERRUPTS_MASK 0x6FFF
  166. struct omap_i2c_dev {
  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 *omap,
  249. int reg, u16 val)
  250. {
  251. writew_relaxed(val, omap->base +
  252. (omap->regs[reg] << omap->reg_shift));
  253. }
  254. static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *omap, int reg)
  255. {
  256. return readw_relaxed(omap->base +
  257. (omap->regs[reg] << omap->reg_shift));
  258. }
  259. static void __omap_i2c_init(struct omap_i2c_dev *omap)
  260. {
  261. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
  262. /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
  263. omap_i2c_write_reg(omap, OMAP_I2C_PSC_REG, omap->pscstate);
  264. /* SCL low and high time values */
  265. omap_i2c_write_reg(omap, OMAP_I2C_SCLL_REG, omap->scllstate);
  266. omap_i2c_write_reg(omap, OMAP_I2C_SCLH_REG, omap->sclhstate);
  267. if (omap->rev >= OMAP_I2C_REV_ON_3430_3530)
  268. omap_i2c_write_reg(omap, OMAP_I2C_WE_REG, omap->westate);
  269. /* Take the I2C module out of reset: */
  270. omap_i2c_write_reg(omap, 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 (omap->iestate)
  281. omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, omap->iestate);
  282. }
  283. static int omap_i2c_reset(struct omap_i2c_dev *omap)
  284. {
  285. unsigned long timeout;
  286. u16 sysc;
  287. if (omap->rev >= OMAP_I2C_OMAP1_REV_2) {
  288. sysc = omap_i2c_read_reg(omap, OMAP_I2C_SYSC_REG);
  289. /* Disable I2C controller before soft reset */
  290. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG,
  291. omap_i2c_read_reg(omap, OMAP_I2C_CON_REG) &
  292. ~(OMAP_I2C_CON_EN));
  293. omap_i2c_write_reg(omap, 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(omap, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
  298. while (!(omap_i2c_read_reg(omap, OMAP_I2C_SYSS_REG) &
  299. SYSS_RESETDONE_MASK)) {
  300. if (time_after(jiffies, timeout)) {
  301. dev_warn(omap->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(omap, OMAP_I2C_SYSC_REG, sysc);
  309. if (omap->rev > OMAP_I2C_REV_ON_3430_3530) {
  310. /* Schedule I2C-bus monitoring on the next transfer */
  311. omap->bb_valid = 0;
  312. }
  313. }
  314. return 0;
  315. }
  316. static int omap_i2c_init(struct omap_i2c_dev *omap)
  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. int error;
  324. if (omap->rev >= OMAP_I2C_REV_ON_3430_3530) {
  325. /*
  326. * Enabling all wakup sources to stop I2C freezing on
  327. * WFI instruction.
  328. * REVISIT: Some wkup sources might not be needed.
  329. */
  330. omap->westate = OMAP_I2C_WE_ALL;
  331. }
  332. if (omap->flags & OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK) {
  333. /*
  334. * The I2C functional clock is the armxor_ck, so there's
  335. * no need to get "armxor_ck" separately. Now, if OMAP2420
  336. * always returns 12MHz for the functional clock, we can
  337. * do this bit unconditionally.
  338. */
  339. fclk = clk_get(omap->dev, "fck");
  340. if (IS_ERR(fclk)) {
  341. error = PTR_ERR(fclk);
  342. dev_err(omap->dev, "could not get fck: %i\n", error);
  343. return error;
  344. }
  345. fclk_rate = clk_get_rate(fclk);
  346. clk_put(fclk);
  347. /* TRM for 5912 says the I2C clock must be prescaled to be
  348. * between 7 - 12 MHz. The XOR input clock is typically
  349. * 12, 13 or 19.2 MHz. So we should have code that produces:
  350. *
  351. * XOR MHz Divider Prescaler
  352. * 12 1 0
  353. * 13 2 1
  354. * 19.2 2 1
  355. */
  356. if (fclk_rate > 12000000)
  357. psc = fclk_rate / 12000000;
  358. }
  359. if (!(omap->flags & OMAP_I2C_FLAG_SIMPLE_CLOCK)) {
  360. /*
  361. * HSI2C controller internal clk rate should be 19.2 Mhz for
  362. * HS and for all modes on 2430. On 34xx we can use lower rate
  363. * to get longer filter period for better noise suppression.
  364. * The filter is iclk (fclk for HS) period.
  365. */
  366. if (omap->speed > 400 ||
  367. omap->flags & OMAP_I2C_FLAG_FORCE_19200_INT_CLK)
  368. internal_clk = 19200;
  369. else if (omap->speed > 100)
  370. internal_clk = 9600;
  371. else
  372. internal_clk = 4000;
  373. fclk = clk_get(omap->dev, "fck");
  374. if (IS_ERR(fclk)) {
  375. error = PTR_ERR(fclk);
  376. dev_err(omap->dev, "could not get fck: %i\n", error);
  377. return error;
  378. }
  379. fclk_rate = clk_get_rate(fclk) / 1000;
  380. clk_put(fclk);
  381. /* Compute prescaler divisor */
  382. psc = fclk_rate / internal_clk;
  383. psc = psc - 1;
  384. /* If configured for High Speed */
  385. if (omap->speed > 400) {
  386. unsigned long scl;
  387. /* For first phase of HS mode */
  388. scl = internal_clk / 400;
  389. fsscll = scl - (scl / 3) - 7;
  390. fssclh = (scl / 3) - 5;
  391. /* For second phase of HS mode */
  392. scl = fclk_rate / omap->speed;
  393. hsscll = scl - (scl / 3) - 7;
  394. hssclh = (scl / 3) - 5;
  395. } else if (omap->speed > 100) {
  396. unsigned long scl;
  397. /* Fast mode */
  398. scl = internal_clk / omap->speed;
  399. fsscll = scl - (scl / 3) - 7;
  400. fssclh = (scl / 3) - 5;
  401. } else {
  402. /* Standard mode */
  403. fsscll = internal_clk / (omap->speed * 2) - 7;
  404. fssclh = internal_clk / (omap->speed * 2) - 5;
  405. }
  406. scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
  407. sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
  408. } else {
  409. /* Program desired operating rate */
  410. fclk_rate /= (psc + 1) * 1000;
  411. if (psc > 2)
  412. psc = 2;
  413. scll = fclk_rate / (omap->speed * 2) - 7 + psc;
  414. sclh = fclk_rate / (omap->speed * 2) - 7 + psc;
  415. }
  416. omap->iestate = (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
  417. OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
  418. OMAP_I2C_IE_AL) | ((omap->fifo_size) ?
  419. (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0);
  420. omap->pscstate = psc;
  421. omap->scllstate = scll;
  422. omap->sclhstate = sclh;
  423. if (omap->rev <= OMAP_I2C_REV_ON_3430_3530) {
  424. /* Not implemented */
  425. omap->bb_valid = 1;
  426. }
  427. __omap_i2c_init(omap);
  428. return 0;
  429. }
  430. /*
  431. * Try bus recovery, but only if SDA is actually low.
  432. */
  433. static int omap_i2c_recover_bus(struct omap_i2c_dev *omap)
  434. {
  435. u16 systest;
  436. systest = omap_i2c_read_reg(omap, OMAP_I2C_SYSTEST_REG);
  437. if ((systest & OMAP_I2C_SYSTEST_SCL_I_FUNC) &&
  438. (systest & OMAP_I2C_SYSTEST_SDA_I_FUNC))
  439. return 0; /* bus seems to already be fine */
  440. if (!(systest & OMAP_I2C_SYSTEST_SCL_I_FUNC))
  441. return -EBUSY; /* recovery would not fix SCL */
  442. return i2c_recover_bus(&omap->adapter);
  443. }
  444. /*
  445. * Waiting on Bus Busy
  446. */
  447. static int omap_i2c_wait_for_bb(struct omap_i2c_dev *omap)
  448. {
  449. unsigned long timeout;
  450. timeout = jiffies + OMAP_I2C_TIMEOUT;
  451. while (omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
  452. if (time_after(jiffies, timeout))
  453. return omap_i2c_recover_bus(omap);
  454. msleep(1);
  455. }
  456. return 0;
  457. }
  458. /*
  459. * Wait while BB-bit doesn't reflect the I2C bus state
  460. *
  461. * In a multimaster environment, after IP software reset, BB-bit value doesn't
  462. * correspond to the current bus state. It may happen what BB-bit will be 0,
  463. * while the bus is busy due to another I2C master activity.
  464. * Here are BB-bit values after reset:
  465. * SDA SCL BB NOTES
  466. * 0 0 0 1, 2
  467. * 1 0 0 1, 2
  468. * 0 1 1
  469. * 1 1 0 3
  470. * Later, if IP detect SDA=0 and SCL=1 (ACK) or SDA 1->0 while SCL=1 (START)
  471. * combinations on the bus, it set BB-bit to 1.
  472. * If IP detect SDA 0->1 while SCL=1 (STOP) combination on the bus,
  473. * it set BB-bit to 0 and BF to 1.
  474. * BB and BF bits correctly tracks the bus state while IP is suspended
  475. * BB bit became valid on the next FCLK clock after CON_EN bit set
  476. *
  477. * NOTES:
  478. * 1. Any transfer started when BB=0 and bus is busy wouldn't be
  479. * completed by IP and results in controller timeout.
  480. * 2. Any transfer started when BB=0 and SCL=0 results in IP
  481. * starting to drive SDA low. In that case IP corrupt data
  482. * on the bus.
  483. * 3. Any transfer started in the middle of another master's transfer
  484. * results in unpredictable results and data corruption
  485. */
  486. static int omap_i2c_wait_for_bb_valid(struct omap_i2c_dev *omap)
  487. {
  488. unsigned long bus_free_timeout = 0;
  489. unsigned long timeout;
  490. int bus_free = 0;
  491. u16 stat, systest;
  492. if (omap->bb_valid)
  493. return 0;
  494. timeout = jiffies + OMAP_I2C_TIMEOUT;
  495. while (1) {
  496. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  497. /*
  498. * We will see BB or BF event in a case IP had detected any
  499. * activity on the I2C bus. Now IP correctly tracks the bus
  500. * state. BB-bit value is valid.
  501. */
  502. if (stat & (OMAP_I2C_STAT_BB | OMAP_I2C_STAT_BF))
  503. break;
  504. /*
  505. * Otherwise, we must look signals on the bus to make
  506. * the right decision.
  507. */
  508. systest = omap_i2c_read_reg(omap, OMAP_I2C_SYSTEST_REG);
  509. if ((systest & OMAP_I2C_SYSTEST_SCL_I_FUNC) &&
  510. (systest & OMAP_I2C_SYSTEST_SDA_I_FUNC)) {
  511. if (!bus_free) {
  512. bus_free_timeout = jiffies +
  513. OMAP_I2C_BUS_FREE_TIMEOUT;
  514. bus_free = 1;
  515. }
  516. /*
  517. * SDA and SCL lines was high for 10 ms without bus
  518. * activity detected. The bus is free. Consider
  519. * BB-bit value is valid.
  520. */
  521. if (time_after(jiffies, bus_free_timeout))
  522. break;
  523. } else {
  524. bus_free = 0;
  525. }
  526. if (time_after(jiffies, timeout)) {
  527. /*
  528. * SDA or SCL were low for the entire timeout without
  529. * any activity detected. Most likely, a slave is
  530. * locking up the bus with no master driving the clock.
  531. */
  532. dev_warn(omap->dev, "timeout waiting for bus ready\n");
  533. return omap_i2c_recover_bus(omap);
  534. }
  535. msleep(1);
  536. }
  537. omap->bb_valid = 1;
  538. return 0;
  539. }
  540. static void omap_i2c_resize_fifo(struct omap_i2c_dev *omap, u8 size, bool is_rx)
  541. {
  542. u16 buf;
  543. if (omap->flags & OMAP_I2C_FLAG_NO_FIFO)
  544. return;
  545. /*
  546. * Set up notification threshold based on message size. We're doing
  547. * this to try and avoid draining feature as much as possible. Whenever
  548. * we have big messages to transfer (bigger than our total fifo size)
  549. * then we might use draining feature to transfer the remaining bytes.
  550. */
  551. omap->threshold = clamp(size, (u8) 1, omap->fifo_size);
  552. buf = omap_i2c_read_reg(omap, OMAP_I2C_BUF_REG);
  553. if (is_rx) {
  554. /* Clear RX Threshold */
  555. buf &= ~(0x3f << 8);
  556. buf |= ((omap->threshold - 1) << 8) | OMAP_I2C_BUF_RXFIF_CLR;
  557. } else {
  558. /* Clear TX Threshold */
  559. buf &= ~0x3f;
  560. buf |= (omap->threshold - 1) | OMAP_I2C_BUF_TXFIF_CLR;
  561. }
  562. omap_i2c_write_reg(omap, OMAP_I2C_BUF_REG, buf);
  563. if (omap->rev < OMAP_I2C_REV_ON_3630)
  564. omap->b_hw = 1; /* Enable hardware fixes */
  565. /* calculate wakeup latency constraint for MPU */
  566. if (omap->set_mpu_wkup_lat != NULL)
  567. omap->latency = (1000000 * omap->threshold) /
  568. (1000 * omap->speed / 8);
  569. }
  570. /*
  571. * Low level master read/write transaction.
  572. */
  573. static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
  574. struct i2c_msg *msg, int stop)
  575. {
  576. struct omap_i2c_dev *omap = i2c_get_adapdata(adap);
  577. unsigned long timeout;
  578. u16 w;
  579. dev_dbg(omap->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  580. msg->addr, msg->len, msg->flags, stop);
  581. omap->receiver = !!(msg->flags & I2C_M_RD);
  582. omap_i2c_resize_fifo(omap, msg->len, omap->receiver);
  583. omap_i2c_write_reg(omap, OMAP_I2C_SA_REG, msg->addr);
  584. /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
  585. omap->buf = msg->buf;
  586. omap->buf_len = msg->len;
  587. /* make sure writes to omap->buf_len are ordered */
  588. barrier();
  589. omap_i2c_write_reg(omap, OMAP_I2C_CNT_REG, omap->buf_len);
  590. /* Clear the FIFO Buffers */
  591. w = omap_i2c_read_reg(omap, OMAP_I2C_BUF_REG);
  592. w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
  593. omap_i2c_write_reg(omap, OMAP_I2C_BUF_REG, w);
  594. reinit_completion(&omap->cmd_complete);
  595. omap->cmd_err = 0;
  596. w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
  597. /* High speed configuration */
  598. if (omap->speed > 400)
  599. w |= OMAP_I2C_CON_OPMODE_HS;
  600. if (msg->flags & I2C_M_STOP)
  601. stop = 1;
  602. if (msg->flags & I2C_M_TEN)
  603. w |= OMAP_I2C_CON_XA;
  604. if (!(msg->flags & I2C_M_RD))
  605. w |= OMAP_I2C_CON_TRX;
  606. if (!omap->b_hw && stop)
  607. w |= OMAP_I2C_CON_STP;
  608. /*
  609. * NOTE: STAT_BB bit could became 1 here if another master occupy
  610. * the bus. IP successfully complete transfer when the bus will be
  611. * free again (BB reset to 0).
  612. */
  613. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, w);
  614. /*
  615. * Don't write stt and stp together on some hardware.
  616. */
  617. if (omap->b_hw && stop) {
  618. unsigned long delay = jiffies + OMAP_I2C_TIMEOUT;
  619. u16 con = omap_i2c_read_reg(omap, OMAP_I2C_CON_REG);
  620. while (con & OMAP_I2C_CON_STT) {
  621. con = omap_i2c_read_reg(omap, OMAP_I2C_CON_REG);
  622. /* Let the user know if i2c is in a bad state */
  623. if (time_after(jiffies, delay)) {
  624. dev_err(omap->dev, "controller timed out "
  625. "waiting for start condition to finish\n");
  626. return -ETIMEDOUT;
  627. }
  628. cpu_relax();
  629. }
  630. w |= OMAP_I2C_CON_STP;
  631. w &= ~OMAP_I2C_CON_STT;
  632. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, w);
  633. }
  634. /*
  635. * REVISIT: We should abort the transfer on signals, but the bus goes
  636. * into arbitration and we're currently unable to recover from it.
  637. */
  638. timeout = wait_for_completion_timeout(&omap->cmd_complete,
  639. OMAP_I2C_TIMEOUT);
  640. if (timeout == 0) {
  641. dev_err(omap->dev, "controller timed out\n");
  642. omap_i2c_reset(omap);
  643. __omap_i2c_init(omap);
  644. return -ETIMEDOUT;
  645. }
  646. if (likely(!omap->cmd_err))
  647. return 0;
  648. /* We have an error */
  649. if (omap->cmd_err & (OMAP_I2C_STAT_ROVR | OMAP_I2C_STAT_XUDF)) {
  650. omap_i2c_reset(omap);
  651. __omap_i2c_init(omap);
  652. return -EIO;
  653. }
  654. if (omap->cmd_err & OMAP_I2C_STAT_AL)
  655. return -EAGAIN;
  656. if (omap->cmd_err & OMAP_I2C_STAT_NACK) {
  657. if (msg->flags & I2C_M_IGNORE_NAK)
  658. return 0;
  659. w = omap_i2c_read_reg(omap, OMAP_I2C_CON_REG);
  660. w |= OMAP_I2C_CON_STP;
  661. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, w);
  662. return -EREMOTEIO;
  663. }
  664. return -EIO;
  665. }
  666. /*
  667. * Prepare controller for a transaction and call omap_i2c_xfer_msg
  668. * to do the work during IRQ processing.
  669. */
  670. static int
  671. omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  672. {
  673. struct omap_i2c_dev *omap = i2c_get_adapdata(adap);
  674. int i;
  675. int r;
  676. r = pm_runtime_get_sync(omap->dev);
  677. if (r < 0)
  678. goto out;
  679. r = omap_i2c_wait_for_bb_valid(omap);
  680. if (r < 0)
  681. goto out;
  682. r = omap_i2c_wait_for_bb(omap);
  683. if (r < 0)
  684. goto out;
  685. if (omap->set_mpu_wkup_lat != NULL)
  686. omap->set_mpu_wkup_lat(omap->dev, omap->latency);
  687. for (i = 0; i < num; i++) {
  688. r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
  689. if (r != 0)
  690. break;
  691. }
  692. if (r == 0)
  693. r = num;
  694. omap_i2c_wait_for_bb(omap);
  695. if (omap->set_mpu_wkup_lat != NULL)
  696. omap->set_mpu_wkup_lat(omap->dev, -1);
  697. out:
  698. pm_runtime_mark_last_busy(omap->dev);
  699. pm_runtime_put_autosuspend(omap->dev);
  700. return r;
  701. }
  702. static u32
  703. omap_i2c_func(struct i2c_adapter *adap)
  704. {
  705. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  706. I2C_FUNC_PROTOCOL_MANGLING;
  707. }
  708. static inline void
  709. omap_i2c_complete_cmd(struct omap_i2c_dev *omap, u16 err)
  710. {
  711. omap->cmd_err |= err;
  712. complete(&omap->cmd_complete);
  713. }
  714. static inline void
  715. omap_i2c_ack_stat(struct omap_i2c_dev *omap, u16 stat)
  716. {
  717. omap_i2c_write_reg(omap, OMAP_I2C_STAT_REG, stat);
  718. }
  719. static inline void i2c_omap_errata_i207(struct omap_i2c_dev *omap, u16 stat)
  720. {
  721. /*
  722. * I2C Errata(Errata Nos. OMAP2: 1.67, OMAP3: 1.8)
  723. * Not applicable for OMAP4.
  724. * Under certain rare conditions, RDR could be set again
  725. * when the bus is busy, then ignore the interrupt and
  726. * clear the interrupt.
  727. */
  728. if (stat & OMAP_I2C_STAT_RDR) {
  729. /* Step 1: If RDR is set, clear it */
  730. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_RDR);
  731. /* Step 2: */
  732. if (!(omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG)
  733. & OMAP_I2C_STAT_BB)) {
  734. /* Step 3: */
  735. if (omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG)
  736. & OMAP_I2C_STAT_RDR) {
  737. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_RDR);
  738. dev_dbg(omap->dev, "RDR when bus is busy.\n");
  739. }
  740. }
  741. }
  742. }
  743. /* rev1 devices are apparently only on some 15xx */
  744. #ifdef CONFIG_ARCH_OMAP15XX
  745. static irqreturn_t
  746. omap_i2c_omap1_isr(int this_irq, void *dev_id)
  747. {
  748. struct omap_i2c_dev *omap = dev_id;
  749. u16 iv, w;
  750. if (pm_runtime_suspended(omap->dev))
  751. return IRQ_NONE;
  752. iv = omap_i2c_read_reg(omap, OMAP_I2C_IV_REG);
  753. switch (iv) {
  754. case 0x00: /* None */
  755. break;
  756. case 0x01: /* Arbitration lost */
  757. dev_err(omap->dev, "Arbitration lost\n");
  758. omap_i2c_complete_cmd(omap, OMAP_I2C_STAT_AL);
  759. break;
  760. case 0x02: /* No acknowledgement */
  761. omap_i2c_complete_cmd(omap, OMAP_I2C_STAT_NACK);
  762. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
  763. break;
  764. case 0x03: /* Register access ready */
  765. omap_i2c_complete_cmd(omap, 0);
  766. break;
  767. case 0x04: /* Receive data ready */
  768. if (omap->buf_len) {
  769. w = omap_i2c_read_reg(omap, OMAP_I2C_DATA_REG);
  770. *omap->buf++ = w;
  771. omap->buf_len--;
  772. if (omap->buf_len) {
  773. *omap->buf++ = w >> 8;
  774. omap->buf_len--;
  775. }
  776. } else
  777. dev_err(omap->dev, "RRDY IRQ while no data requested\n");
  778. break;
  779. case 0x05: /* Transmit data ready */
  780. if (omap->buf_len) {
  781. w = *omap->buf++;
  782. omap->buf_len--;
  783. if (omap->buf_len) {
  784. w |= *omap->buf++ << 8;
  785. omap->buf_len--;
  786. }
  787. omap_i2c_write_reg(omap, OMAP_I2C_DATA_REG, w);
  788. } else
  789. dev_err(omap->dev, "XRDY IRQ while no data to send\n");
  790. break;
  791. default:
  792. return IRQ_NONE;
  793. }
  794. return IRQ_HANDLED;
  795. }
  796. #else
  797. #define omap_i2c_omap1_isr NULL
  798. #endif
  799. /*
  800. * OMAP3430 Errata i462: When an XRDY/XDR is hit, wait for XUDF before writing
  801. * data to DATA_REG. Otherwise some data bytes can be lost while transferring
  802. * them from the memory to the I2C interface.
  803. */
  804. static int errata_omap3_i462(struct omap_i2c_dev *omap)
  805. {
  806. unsigned long timeout = 10000;
  807. u16 stat;
  808. do {
  809. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  810. if (stat & OMAP_I2C_STAT_XUDF)
  811. break;
  812. if (stat & (OMAP_I2C_STAT_NACK | OMAP_I2C_STAT_AL)) {
  813. omap_i2c_ack_stat(omap, (OMAP_I2C_STAT_XRDY |
  814. OMAP_I2C_STAT_XDR));
  815. if (stat & OMAP_I2C_STAT_NACK) {
  816. omap->cmd_err |= OMAP_I2C_STAT_NACK;
  817. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_NACK);
  818. }
  819. if (stat & OMAP_I2C_STAT_AL) {
  820. dev_err(omap->dev, "Arbitration lost\n");
  821. omap->cmd_err |= OMAP_I2C_STAT_AL;
  822. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_AL);
  823. }
  824. return -EIO;
  825. }
  826. cpu_relax();
  827. } while (--timeout);
  828. if (!timeout) {
  829. dev_err(omap->dev, "timeout waiting on XUDF bit\n");
  830. return 0;
  831. }
  832. return 0;
  833. }
  834. static void omap_i2c_receive_data(struct omap_i2c_dev *omap, u8 num_bytes,
  835. bool is_rdr)
  836. {
  837. u16 w;
  838. while (num_bytes--) {
  839. w = omap_i2c_read_reg(omap, OMAP_I2C_DATA_REG);
  840. *omap->buf++ = w;
  841. omap->buf_len--;
  842. /*
  843. * Data reg in 2430, omap3 and
  844. * omap4 is 8 bit wide
  845. */
  846. if (omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  847. *omap->buf++ = w >> 8;
  848. omap->buf_len--;
  849. }
  850. }
  851. }
  852. static int omap_i2c_transmit_data(struct omap_i2c_dev *omap, u8 num_bytes,
  853. bool is_xdr)
  854. {
  855. u16 w;
  856. while (num_bytes--) {
  857. w = *omap->buf++;
  858. omap->buf_len--;
  859. /*
  860. * Data reg in 2430, omap3 and
  861. * omap4 is 8 bit wide
  862. */
  863. if (omap->flags & OMAP_I2C_FLAG_16BIT_DATA_REG) {
  864. w |= *omap->buf++ << 8;
  865. omap->buf_len--;
  866. }
  867. if (omap->errata & I2C_OMAP_ERRATA_I462) {
  868. int ret;
  869. ret = errata_omap3_i462(omap);
  870. if (ret < 0)
  871. return ret;
  872. }
  873. omap_i2c_write_reg(omap, OMAP_I2C_DATA_REG, w);
  874. }
  875. return 0;
  876. }
  877. static irqreturn_t
  878. omap_i2c_isr(int irq, void *dev_id)
  879. {
  880. struct omap_i2c_dev *omap = dev_id;
  881. irqreturn_t ret = IRQ_HANDLED;
  882. u16 mask;
  883. u16 stat;
  884. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  885. mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  886. if (stat & mask)
  887. ret = IRQ_WAKE_THREAD;
  888. return ret;
  889. }
  890. static irqreturn_t
  891. omap_i2c_isr_thread(int this_irq, void *dev_id)
  892. {
  893. struct omap_i2c_dev *omap = dev_id;
  894. u16 bits;
  895. u16 stat;
  896. int err = 0, count = 0;
  897. do {
  898. bits = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  899. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  900. stat &= bits;
  901. /* If we're in receiver mode, ignore XDR/XRDY */
  902. if (omap->receiver)
  903. stat &= ~(OMAP_I2C_STAT_XDR | OMAP_I2C_STAT_XRDY);
  904. else
  905. stat &= ~(OMAP_I2C_STAT_RDR | OMAP_I2C_STAT_RRDY);
  906. if (!stat) {
  907. /* my work here is done */
  908. goto out;
  909. }
  910. dev_dbg(omap->dev, "IRQ (ISR = 0x%04x)\n", stat);
  911. if (count++ == 100) {
  912. dev_warn(omap->dev, "Too much work in one IRQ\n");
  913. break;
  914. }
  915. if (stat & OMAP_I2C_STAT_NACK) {
  916. err |= OMAP_I2C_STAT_NACK;
  917. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_NACK);
  918. }
  919. if (stat & OMAP_I2C_STAT_AL) {
  920. dev_err(omap->dev, "Arbitration lost\n");
  921. err |= OMAP_I2C_STAT_AL;
  922. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_AL);
  923. }
  924. /*
  925. * ProDB0017052: Clear ARDY bit twice
  926. */
  927. if (stat & OMAP_I2C_STAT_ARDY)
  928. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_ARDY);
  929. if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
  930. OMAP_I2C_STAT_AL)) {
  931. omap_i2c_ack_stat(omap, (OMAP_I2C_STAT_RRDY |
  932. OMAP_I2C_STAT_RDR |
  933. OMAP_I2C_STAT_XRDY |
  934. OMAP_I2C_STAT_XDR |
  935. OMAP_I2C_STAT_ARDY));
  936. break;
  937. }
  938. if (stat & OMAP_I2C_STAT_RDR) {
  939. u8 num_bytes = 1;
  940. if (omap->fifo_size)
  941. num_bytes = omap->buf_len;
  942. if (omap->errata & I2C_OMAP_ERRATA_I207) {
  943. i2c_omap_errata_i207(omap, stat);
  944. num_bytes = (omap_i2c_read_reg(omap,
  945. OMAP_I2C_BUFSTAT_REG) >> 8) & 0x3F;
  946. }
  947. omap_i2c_receive_data(omap, num_bytes, true);
  948. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_RDR);
  949. continue;
  950. }
  951. if (stat & OMAP_I2C_STAT_RRDY) {
  952. u8 num_bytes = 1;
  953. if (omap->threshold)
  954. num_bytes = omap->threshold;
  955. omap_i2c_receive_data(omap, num_bytes, false);
  956. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_RRDY);
  957. continue;
  958. }
  959. if (stat & OMAP_I2C_STAT_XDR) {
  960. u8 num_bytes = 1;
  961. int ret;
  962. if (omap->fifo_size)
  963. num_bytes = omap->buf_len;
  964. ret = omap_i2c_transmit_data(omap, num_bytes, true);
  965. if (ret < 0)
  966. break;
  967. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_XDR);
  968. continue;
  969. }
  970. if (stat & OMAP_I2C_STAT_XRDY) {
  971. u8 num_bytes = 1;
  972. int ret;
  973. if (omap->threshold)
  974. num_bytes = omap->threshold;
  975. ret = omap_i2c_transmit_data(omap, num_bytes, false);
  976. if (ret < 0)
  977. break;
  978. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_XRDY);
  979. continue;
  980. }
  981. if (stat & OMAP_I2C_STAT_ROVR) {
  982. dev_err(omap->dev, "Receive overrun\n");
  983. err |= OMAP_I2C_STAT_ROVR;
  984. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_ROVR);
  985. break;
  986. }
  987. if (stat & OMAP_I2C_STAT_XUDF) {
  988. dev_err(omap->dev, "Transmit underflow\n");
  989. err |= OMAP_I2C_STAT_XUDF;
  990. omap_i2c_ack_stat(omap, OMAP_I2C_STAT_XUDF);
  991. break;
  992. }
  993. } while (stat);
  994. omap_i2c_complete_cmd(omap, err);
  995. out:
  996. return IRQ_HANDLED;
  997. }
  998. static const struct i2c_algorithm omap_i2c_algo = {
  999. .master_xfer = omap_i2c_xfer,
  1000. .functionality = omap_i2c_func,
  1001. };
  1002. static const struct i2c_adapter_quirks omap_i2c_quirks = {
  1003. .flags = I2C_AQ_NO_ZERO_LEN,
  1004. };
  1005. #ifdef CONFIG_OF
  1006. static struct omap_i2c_bus_platform_data omap2420_pdata = {
  1007. .rev = OMAP_I2C_IP_VERSION_1,
  1008. .flags = OMAP_I2C_FLAG_NO_FIFO |
  1009. OMAP_I2C_FLAG_SIMPLE_CLOCK |
  1010. OMAP_I2C_FLAG_16BIT_DATA_REG |
  1011. OMAP_I2C_FLAG_BUS_SHIFT_2,
  1012. };
  1013. static struct omap_i2c_bus_platform_data omap2430_pdata = {
  1014. .rev = OMAP_I2C_IP_VERSION_1,
  1015. .flags = OMAP_I2C_FLAG_BUS_SHIFT_2 |
  1016. OMAP_I2C_FLAG_FORCE_19200_INT_CLK,
  1017. };
  1018. static struct omap_i2c_bus_platform_data omap3_pdata = {
  1019. .rev = OMAP_I2C_IP_VERSION_1,
  1020. .flags = OMAP_I2C_FLAG_BUS_SHIFT_2,
  1021. };
  1022. static struct omap_i2c_bus_platform_data omap4_pdata = {
  1023. .rev = OMAP_I2C_IP_VERSION_2,
  1024. };
  1025. static const struct of_device_id omap_i2c_of_match[] = {
  1026. {
  1027. .compatible = "ti,omap4-i2c",
  1028. .data = &omap4_pdata,
  1029. },
  1030. {
  1031. .compatible = "ti,omap3-i2c",
  1032. .data = &omap3_pdata,
  1033. },
  1034. {
  1035. .compatible = "ti,omap2430-i2c",
  1036. .data = &omap2430_pdata,
  1037. },
  1038. {
  1039. .compatible = "ti,omap2420-i2c",
  1040. .data = &omap2420_pdata,
  1041. },
  1042. { },
  1043. };
  1044. MODULE_DEVICE_TABLE(of, omap_i2c_of_match);
  1045. #endif
  1046. #define OMAP_I2C_SCHEME(rev) ((rev & 0xc000) >> 14)
  1047. #define OMAP_I2C_REV_SCHEME_0_MAJOR(rev) (rev >> 4)
  1048. #define OMAP_I2C_REV_SCHEME_0_MINOR(rev) (rev & 0xf)
  1049. #define OMAP_I2C_REV_SCHEME_1_MAJOR(rev) ((rev & 0x0700) >> 7)
  1050. #define OMAP_I2C_REV_SCHEME_1_MINOR(rev) (rev & 0x1f)
  1051. #define OMAP_I2C_SCHEME_0 0
  1052. #define OMAP_I2C_SCHEME_1 1
  1053. static int omap_i2c_get_scl(struct i2c_adapter *adap)
  1054. {
  1055. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  1056. u32 reg;
  1057. reg = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  1058. return reg & OMAP_I2C_SYSTEST_SCL_I_FUNC;
  1059. }
  1060. static int omap_i2c_get_sda(struct i2c_adapter *adap)
  1061. {
  1062. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  1063. u32 reg;
  1064. reg = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  1065. return reg & OMAP_I2C_SYSTEST_SDA_I_FUNC;
  1066. }
  1067. static void omap_i2c_set_scl(struct i2c_adapter *adap, int val)
  1068. {
  1069. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  1070. u32 reg;
  1071. reg = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  1072. if (val)
  1073. reg |= OMAP_I2C_SYSTEST_SCL_O;
  1074. else
  1075. reg &= ~OMAP_I2C_SYSTEST_SCL_O;
  1076. omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, reg);
  1077. }
  1078. static void omap_i2c_prepare_recovery(struct i2c_adapter *adap)
  1079. {
  1080. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  1081. u32 reg;
  1082. reg = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  1083. /* enable test mode */
  1084. reg |= OMAP_I2C_SYSTEST_ST_EN;
  1085. /* select SDA/SCL IO mode */
  1086. reg |= 3 << OMAP_I2C_SYSTEST_TMODE_SHIFT;
  1087. /* set SCL to high-impedance state (reset value is 0) */
  1088. reg |= OMAP_I2C_SYSTEST_SCL_O;
  1089. /* set SDA to high-impedance state (reset value is 0) */
  1090. reg |= OMAP_I2C_SYSTEST_SDA_O;
  1091. omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, reg);
  1092. }
  1093. static void omap_i2c_unprepare_recovery(struct i2c_adapter *adap)
  1094. {
  1095. struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
  1096. u32 reg;
  1097. reg = omap_i2c_read_reg(dev, OMAP_I2C_SYSTEST_REG);
  1098. /* restore reset values */
  1099. reg &= ~OMAP_I2C_SYSTEST_ST_EN;
  1100. reg &= ~OMAP_I2C_SYSTEST_TMODE_MASK;
  1101. reg &= ~OMAP_I2C_SYSTEST_SCL_O;
  1102. reg &= ~OMAP_I2C_SYSTEST_SDA_O;
  1103. omap_i2c_write_reg(dev, OMAP_I2C_SYSTEST_REG, reg);
  1104. }
  1105. static struct i2c_bus_recovery_info omap_i2c_bus_recovery_info = {
  1106. .get_scl = omap_i2c_get_scl,
  1107. .get_sda = omap_i2c_get_sda,
  1108. .set_scl = omap_i2c_set_scl,
  1109. .prepare_recovery = omap_i2c_prepare_recovery,
  1110. .unprepare_recovery = omap_i2c_unprepare_recovery,
  1111. .recover_bus = i2c_generic_scl_recovery,
  1112. };
  1113. static int
  1114. omap_i2c_probe(struct platform_device *pdev)
  1115. {
  1116. struct omap_i2c_dev *omap;
  1117. struct i2c_adapter *adap;
  1118. struct resource *mem;
  1119. const struct omap_i2c_bus_platform_data *pdata =
  1120. dev_get_platdata(&pdev->dev);
  1121. struct device_node *node = pdev->dev.of_node;
  1122. const struct of_device_id *match;
  1123. int irq;
  1124. int r;
  1125. u32 rev;
  1126. u16 minor, major;
  1127. irq = platform_get_irq(pdev, 0);
  1128. if (irq < 0) {
  1129. dev_err(&pdev->dev, "no irq resource?\n");
  1130. return irq;
  1131. }
  1132. omap = devm_kzalloc(&pdev->dev, sizeof(struct omap_i2c_dev), GFP_KERNEL);
  1133. if (!omap)
  1134. return -ENOMEM;
  1135. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1136. omap->base = devm_ioremap_resource(&pdev->dev, mem);
  1137. if (IS_ERR(omap->base))
  1138. return PTR_ERR(omap->base);
  1139. match = of_match_device(of_match_ptr(omap_i2c_of_match), &pdev->dev);
  1140. if (match) {
  1141. u32 freq = 100000; /* default to 100000 Hz */
  1142. pdata = match->data;
  1143. omap->flags = pdata->flags;
  1144. of_property_read_u32(node, "clock-frequency", &freq);
  1145. /* convert DT freq value in Hz into kHz for speed */
  1146. omap->speed = freq / 1000;
  1147. } else if (pdata != NULL) {
  1148. omap->speed = pdata->clkrate;
  1149. omap->flags = pdata->flags;
  1150. omap->set_mpu_wkup_lat = pdata->set_mpu_wkup_lat;
  1151. }
  1152. omap->dev = &pdev->dev;
  1153. omap->irq = irq;
  1154. platform_set_drvdata(pdev, omap);
  1155. init_completion(&omap->cmd_complete);
  1156. omap->reg_shift = (omap->flags >> OMAP_I2C_FLAG_BUS_SHIFT__SHIFT) & 3;
  1157. pm_runtime_enable(omap->dev);
  1158. pm_runtime_set_autosuspend_delay(omap->dev, OMAP_I2C_PM_TIMEOUT);
  1159. pm_runtime_use_autosuspend(omap->dev);
  1160. r = pm_runtime_get_sync(omap->dev);
  1161. if (r < 0)
  1162. goto err_free_mem;
  1163. /*
  1164. * Read the Rev hi bit-[15:14] ie scheme this is 1 indicates ver2.
  1165. * On omap1/3/2 Offset 4 is IE Reg the bit [15:14] is 0 at reset.
  1166. * Also since the omap_i2c_read_reg uses reg_map_ip_* a
  1167. * readw_relaxed is done.
  1168. */
  1169. rev = readw_relaxed(omap->base + 0x04);
  1170. omap->scheme = OMAP_I2C_SCHEME(rev);
  1171. switch (omap->scheme) {
  1172. case OMAP_I2C_SCHEME_0:
  1173. omap->regs = (u8 *)reg_map_ip_v1;
  1174. omap->rev = omap_i2c_read_reg(omap, OMAP_I2C_REV_REG);
  1175. minor = OMAP_I2C_REV_SCHEME_0_MAJOR(omap->rev);
  1176. major = OMAP_I2C_REV_SCHEME_0_MAJOR(omap->rev);
  1177. break;
  1178. case OMAP_I2C_SCHEME_1:
  1179. /* FALLTHROUGH */
  1180. default:
  1181. omap->regs = (u8 *)reg_map_ip_v2;
  1182. rev = (rev << 16) |
  1183. omap_i2c_read_reg(omap, OMAP_I2C_IP_V2_REVNB_LO);
  1184. minor = OMAP_I2C_REV_SCHEME_1_MINOR(rev);
  1185. major = OMAP_I2C_REV_SCHEME_1_MAJOR(rev);
  1186. omap->rev = rev;
  1187. }
  1188. omap->errata = 0;
  1189. if (omap->rev >= OMAP_I2C_REV_ON_2430 &&
  1190. omap->rev < OMAP_I2C_REV_ON_4430_PLUS)
  1191. omap->errata |= I2C_OMAP_ERRATA_I207;
  1192. if (omap->rev <= OMAP_I2C_REV_ON_3430_3530)
  1193. omap->errata |= I2C_OMAP_ERRATA_I462;
  1194. if (!(omap->flags & OMAP_I2C_FLAG_NO_FIFO)) {
  1195. u16 s;
  1196. /* Set up the fifo size - Get total size */
  1197. s = (omap_i2c_read_reg(omap, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3;
  1198. omap->fifo_size = 0x8 << s;
  1199. /*
  1200. * Set up notification threshold as half the total available
  1201. * size. This is to ensure that we can handle the status on int
  1202. * call back latencies.
  1203. */
  1204. omap->fifo_size = (omap->fifo_size / 2);
  1205. if (omap->rev < OMAP_I2C_REV_ON_3630)
  1206. omap->b_hw = 1; /* Enable hardware fixes */
  1207. /* calculate wakeup latency constraint for MPU */
  1208. if (omap->set_mpu_wkup_lat != NULL)
  1209. omap->latency = (1000000 * omap->fifo_size) /
  1210. (1000 * omap->speed / 8);
  1211. }
  1212. /* reset ASAP, clearing any IRQs */
  1213. omap_i2c_init(omap);
  1214. if (omap->rev < OMAP_I2C_OMAP1_REV_2)
  1215. r = devm_request_irq(&pdev->dev, omap->irq, omap_i2c_omap1_isr,
  1216. IRQF_NO_SUSPEND, pdev->name, omap);
  1217. else
  1218. r = devm_request_threaded_irq(&pdev->dev, omap->irq,
  1219. omap_i2c_isr, omap_i2c_isr_thread,
  1220. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1221. pdev->name, omap);
  1222. if (r) {
  1223. dev_err(omap->dev, "failure requesting irq %i\n", omap->irq);
  1224. goto err_unuse_clocks;
  1225. }
  1226. adap = &omap->adapter;
  1227. i2c_set_adapdata(adap, omap);
  1228. adap->owner = THIS_MODULE;
  1229. adap->class = I2C_CLASS_DEPRECATED;
  1230. strlcpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
  1231. adap->algo = &omap_i2c_algo;
  1232. adap->quirks = &omap_i2c_quirks;
  1233. adap->dev.parent = &pdev->dev;
  1234. adap->dev.of_node = pdev->dev.of_node;
  1235. adap->bus_recovery_info = &omap_i2c_bus_recovery_info;
  1236. /* i2c device drivers may be active on return from add_adapter() */
  1237. adap->nr = pdev->id;
  1238. r = i2c_add_numbered_adapter(adap);
  1239. if (r)
  1240. goto err_unuse_clocks;
  1241. dev_info(omap->dev, "bus %d rev%d.%d at %d kHz\n", adap->nr,
  1242. major, minor, omap->speed);
  1243. pm_runtime_mark_last_busy(omap->dev);
  1244. pm_runtime_put_autosuspend(omap->dev);
  1245. return 0;
  1246. err_unuse_clocks:
  1247. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
  1248. pm_runtime_dont_use_autosuspend(omap->dev);
  1249. pm_runtime_put_sync(omap->dev);
  1250. pm_runtime_disable(&pdev->dev);
  1251. err_free_mem:
  1252. return r;
  1253. }
  1254. static int omap_i2c_remove(struct platform_device *pdev)
  1255. {
  1256. struct omap_i2c_dev *omap = platform_get_drvdata(pdev);
  1257. int ret;
  1258. i2c_del_adapter(&omap->adapter);
  1259. ret = pm_runtime_get_sync(&pdev->dev);
  1260. if (ret < 0)
  1261. return ret;
  1262. omap_i2c_write_reg(omap, OMAP_I2C_CON_REG, 0);
  1263. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1264. pm_runtime_put_sync(&pdev->dev);
  1265. pm_runtime_disable(&pdev->dev);
  1266. return 0;
  1267. }
  1268. static int __maybe_unused omap_i2c_runtime_suspend(struct device *dev)
  1269. {
  1270. struct omap_i2c_dev *omap = dev_get_drvdata(dev);
  1271. omap->iestate = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  1272. if (omap->scheme == OMAP_I2C_SCHEME_0)
  1273. omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, 0);
  1274. else
  1275. omap_i2c_write_reg(omap, OMAP_I2C_IP_V2_IRQENABLE_CLR,
  1276. OMAP_I2C_IP_V2_INTERRUPTS_MASK);
  1277. if (omap->rev < OMAP_I2C_OMAP1_REV_2) {
  1278. omap_i2c_read_reg(omap, OMAP_I2C_IV_REG); /* Read clears */
  1279. } else {
  1280. omap_i2c_write_reg(omap, OMAP_I2C_STAT_REG, omap->iestate);
  1281. /* Flush posted write */
  1282. omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  1283. }
  1284. pinctrl_pm_select_sleep_state(dev);
  1285. return 0;
  1286. }
  1287. static int __maybe_unused omap_i2c_runtime_resume(struct device *dev)
  1288. {
  1289. struct omap_i2c_dev *omap = dev_get_drvdata(dev);
  1290. pinctrl_pm_select_default_state(dev);
  1291. if (!omap->regs)
  1292. return 0;
  1293. __omap_i2c_init(omap);
  1294. return 0;
  1295. }
  1296. static const struct dev_pm_ops omap_i2c_pm_ops = {
  1297. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1298. pm_runtime_force_resume)
  1299. SET_RUNTIME_PM_OPS(omap_i2c_runtime_suspend,
  1300. omap_i2c_runtime_resume, NULL)
  1301. };
  1302. static struct platform_driver omap_i2c_driver = {
  1303. .probe = omap_i2c_probe,
  1304. .remove = omap_i2c_remove,
  1305. .driver = {
  1306. .name = "omap_i2c",
  1307. .pm = &omap_i2c_pm_ops,
  1308. .of_match_table = of_match_ptr(omap_i2c_of_match),
  1309. },
  1310. };
  1311. /* I2C may be needed to bring up other drivers */
  1312. static int __init
  1313. omap_i2c_init_driver(void)
  1314. {
  1315. return platform_driver_register(&omap_i2c_driver);
  1316. }
  1317. subsys_initcall(omap_i2c_init_driver);
  1318. static void __exit omap_i2c_exit_driver(void)
  1319. {
  1320. platform_driver_unregister(&omap_i2c_driver);
  1321. }
  1322. module_exit(omap_i2c_exit_driver);
  1323. MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
  1324. MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
  1325. MODULE_LICENSE("GPL");
  1326. MODULE_ALIAS("platform:omap_i2c");