i2c-omap.c 35 KB

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