i2c-omap.c 41 KB

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