i2c-s3c2410.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /* linux/drivers/i2c/busses/i2c-s3c2410.c
  2. *
  3. * Copyright (C) 2004,2005,2009 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 I2C Controller
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/init.h>
  26. #include <linux/time.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/errno.h>
  30. #include <linux/err.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/clk.h>
  34. #include <linux/cpufreq.h>
  35. #include <linux/slab.h>
  36. #include <linux/io.h>
  37. #include <linux/of_i2c.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <asm/irq.h>
  41. #include <linux/platform_data/i2c-s3c2410.h>
  42. /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */
  43. #define S3C2410_IICREG(x) (x)
  44. #define S3C2410_IICCON S3C2410_IICREG(0x00)
  45. #define S3C2410_IICSTAT S3C2410_IICREG(0x04)
  46. #define S3C2410_IICADD S3C2410_IICREG(0x08)
  47. #define S3C2410_IICDS S3C2410_IICREG(0x0C)
  48. #define S3C2440_IICLC S3C2410_IICREG(0x10)
  49. #define S3C2410_IICCON_ACKEN (1<<7)
  50. #define S3C2410_IICCON_TXDIV_16 (0<<6)
  51. #define S3C2410_IICCON_TXDIV_512 (1<<6)
  52. #define S3C2410_IICCON_IRQEN (1<<5)
  53. #define S3C2410_IICCON_IRQPEND (1<<4)
  54. #define S3C2410_IICCON_SCALE(x) ((x)&15)
  55. #define S3C2410_IICCON_SCALEMASK (0xf)
  56. #define S3C2410_IICSTAT_MASTER_RX (2<<6)
  57. #define S3C2410_IICSTAT_MASTER_TX (3<<6)
  58. #define S3C2410_IICSTAT_SLAVE_RX (0<<6)
  59. #define S3C2410_IICSTAT_SLAVE_TX (1<<6)
  60. #define S3C2410_IICSTAT_MODEMASK (3<<6)
  61. #define S3C2410_IICSTAT_START (1<<5)
  62. #define S3C2410_IICSTAT_BUSBUSY (1<<5)
  63. #define S3C2410_IICSTAT_TXRXEN (1<<4)
  64. #define S3C2410_IICSTAT_ARBITR (1<<3)
  65. #define S3C2410_IICSTAT_ASSLAVE (1<<2)
  66. #define S3C2410_IICSTAT_ADDR0 (1<<1)
  67. #define S3C2410_IICSTAT_LASTBIT (1<<0)
  68. #define S3C2410_IICLC_SDA_DELAY0 (0 << 0)
  69. #define S3C2410_IICLC_SDA_DELAY5 (1 << 0)
  70. #define S3C2410_IICLC_SDA_DELAY10 (2 << 0)
  71. #define S3C2410_IICLC_SDA_DELAY15 (3 << 0)
  72. #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0)
  73. #define S3C2410_IICLC_FILTER_ON (1<<2)
  74. /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
  75. #define QUIRK_S3C2440 (1 << 0)
  76. #define QUIRK_HDMIPHY (1 << 1)
  77. #define QUIRK_NO_GPIO (1 << 2)
  78. /* Max time to wait for bus to become idle after a xfer (in us) */
  79. #define S3C2410_IDLE_TIMEOUT 5000
  80. /* i2c controller state */
  81. enum s3c24xx_i2c_state {
  82. STATE_IDLE,
  83. STATE_START,
  84. STATE_READ,
  85. STATE_WRITE,
  86. STATE_STOP
  87. };
  88. struct s3c24xx_i2c {
  89. wait_queue_head_t wait;
  90. unsigned int quirks;
  91. unsigned int suspended:1;
  92. struct i2c_msg *msg;
  93. unsigned int msg_num;
  94. unsigned int msg_idx;
  95. unsigned int msg_ptr;
  96. unsigned int tx_setup;
  97. unsigned int irq;
  98. enum s3c24xx_i2c_state state;
  99. unsigned long clkrate;
  100. void __iomem *regs;
  101. struct clk *clk;
  102. struct device *dev;
  103. struct i2c_adapter adap;
  104. struct s3c2410_platform_i2c *pdata;
  105. int gpios[2];
  106. struct pinctrl *pctrl;
  107. #ifdef CONFIG_CPU_FREQ
  108. struct notifier_block freq_transition;
  109. #endif
  110. };
  111. static struct platform_device_id s3c24xx_driver_ids[] = {
  112. {
  113. .name = "s3c2410-i2c",
  114. .driver_data = 0,
  115. }, {
  116. .name = "s3c2440-i2c",
  117. .driver_data = QUIRK_S3C2440,
  118. }, {
  119. .name = "s3c2440-hdmiphy-i2c",
  120. .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
  121. }, { },
  122. };
  123. MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
  124. #ifdef CONFIG_OF
  125. static const struct of_device_id s3c24xx_i2c_match[] = {
  126. { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
  127. { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
  128. { .compatible = "samsung,s3c2440-hdmiphy-i2c",
  129. .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
  130. { .compatible = "samsung,exynos5440-i2c",
  131. .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
  135. #endif
  136. /* s3c24xx_get_device_quirks
  137. *
  138. * Get controller type either from device tree or platform device variant.
  139. */
  140. static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
  141. {
  142. if (pdev->dev.of_node) {
  143. const struct of_device_id *match;
  144. match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
  145. return (unsigned int)match->data;
  146. }
  147. return platform_get_device_id(pdev)->driver_data;
  148. }
  149. /* s3c24xx_i2c_master_complete
  150. *
  151. * complete the message and wake up the caller, using the given return code,
  152. * or zero to mean ok.
  153. */
  154. static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
  155. {
  156. dev_dbg(i2c->dev, "master_complete %d\n", ret);
  157. i2c->msg_ptr = 0;
  158. i2c->msg = NULL;
  159. i2c->msg_idx++;
  160. i2c->msg_num = 0;
  161. if (ret)
  162. i2c->msg_idx = ret;
  163. wake_up(&i2c->wait);
  164. }
  165. static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
  166. {
  167. unsigned long tmp;
  168. tmp = readl(i2c->regs + S3C2410_IICCON);
  169. writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  170. }
  171. static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
  172. {
  173. unsigned long tmp;
  174. tmp = readl(i2c->regs + S3C2410_IICCON);
  175. writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  176. }
  177. /* irq enable/disable functions */
  178. static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
  179. {
  180. unsigned long tmp;
  181. tmp = readl(i2c->regs + S3C2410_IICCON);
  182. writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  183. }
  184. static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
  185. {
  186. unsigned long tmp;
  187. tmp = readl(i2c->regs + S3C2410_IICCON);
  188. writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  189. }
  190. /* s3c24xx_i2c_message_start
  191. *
  192. * put the start of a message onto the bus
  193. */
  194. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
  195. struct i2c_msg *msg)
  196. {
  197. unsigned int addr = (msg->addr & 0x7f) << 1;
  198. unsigned long stat;
  199. unsigned long iiccon;
  200. stat = 0;
  201. stat |= S3C2410_IICSTAT_TXRXEN;
  202. if (msg->flags & I2C_M_RD) {
  203. stat |= S3C2410_IICSTAT_MASTER_RX;
  204. addr |= 1;
  205. } else
  206. stat |= S3C2410_IICSTAT_MASTER_TX;
  207. if (msg->flags & I2C_M_REV_DIR_ADDR)
  208. addr ^= 1;
  209. /* todo - check for whether ack wanted or not */
  210. s3c24xx_i2c_enable_ack(i2c);
  211. iiccon = readl(i2c->regs + S3C2410_IICCON);
  212. writel(stat, i2c->regs + S3C2410_IICSTAT);
  213. dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
  214. writeb(addr, i2c->regs + S3C2410_IICDS);
  215. /* delay here to ensure the data byte has gotten onto the bus
  216. * before the transaction is started */
  217. ndelay(i2c->tx_setup);
  218. dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
  219. writel(iiccon, i2c->regs + S3C2410_IICCON);
  220. stat |= S3C2410_IICSTAT_START;
  221. writel(stat, i2c->regs + S3C2410_IICSTAT);
  222. }
  223. static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
  224. {
  225. unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  226. dev_dbg(i2c->dev, "STOP\n");
  227. /*
  228. * The datasheet says that the STOP sequence should be:
  229. * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
  230. * 2) I2CCON.4 = 0 - Clear IRQPEND
  231. * 3) Wait until the stop condition takes effect.
  232. * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
  233. *
  234. * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
  235. *
  236. * However, after much experimentation, it appears that:
  237. * a) normal buses automatically clear BUSY and transition from
  238. * Master->Slave when they complete generating a STOP condition.
  239. * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
  240. * after starting the STOP generation here.
  241. * b) HDMIPHY bus does neither, so there is no way to do step 3.
  242. * There is no indication when this bus has finished generating
  243. * STOP.
  244. *
  245. * In fact, we have found that as soon as the IRQPEND bit is cleared in
  246. * step 2, the HDMIPHY bus generates the STOP condition, and then
  247. * immediately starts transferring another data byte, even though the
  248. * bus is supposedly stopped. This is presumably because the bus is
  249. * still in "Master" mode, and its BUSY bit is still set.
  250. *
  251. * To avoid these extra post-STOP transactions on HDMI phy devices, we
  252. * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
  253. * instead of first generating a proper STOP condition. This should
  254. * float SDA & SCK terminating the transfer. Subsequent transfers
  255. * start with a proper START condition, and proceed normally.
  256. *
  257. * The HDMIPHY bus is an internal bus that always has exactly two
  258. * devices, the host as Master and the HDMIPHY device as the slave.
  259. * Skipping the STOP condition has been tested on this bus and works.
  260. */
  261. if (i2c->quirks & QUIRK_HDMIPHY) {
  262. /* Stop driving the I2C pins */
  263. iicstat &= ~S3C2410_IICSTAT_TXRXEN;
  264. } else {
  265. /* stop the transfer */
  266. iicstat &= ~S3C2410_IICSTAT_START;
  267. }
  268. writel(iicstat, i2c->regs + S3C2410_IICSTAT);
  269. i2c->state = STATE_STOP;
  270. s3c24xx_i2c_master_complete(i2c, ret);
  271. s3c24xx_i2c_disable_irq(i2c);
  272. }
  273. /* helper functions to determine the current state in the set of
  274. * messages we are sending */
  275. /* is_lastmsg()
  276. *
  277. * returns TRUE if the current message is the last in the set
  278. */
  279. static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
  280. {
  281. return i2c->msg_idx >= (i2c->msg_num - 1);
  282. }
  283. /* is_msglast
  284. *
  285. * returns TRUE if we this is the last byte in the current message
  286. */
  287. static inline int is_msglast(struct s3c24xx_i2c *i2c)
  288. {
  289. return i2c->msg_ptr == i2c->msg->len-1;
  290. }
  291. /* is_msgend
  292. *
  293. * returns TRUE if we reached the end of the current message
  294. */
  295. static inline int is_msgend(struct s3c24xx_i2c *i2c)
  296. {
  297. return i2c->msg_ptr >= i2c->msg->len;
  298. }
  299. /* i2c_s3c_irq_nextbyte
  300. *
  301. * process an interrupt and work out what to do
  302. */
  303. static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
  304. {
  305. unsigned long tmp;
  306. unsigned char byte;
  307. int ret = 0;
  308. switch (i2c->state) {
  309. case STATE_IDLE:
  310. dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
  311. goto out;
  312. case STATE_STOP:
  313. dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
  314. s3c24xx_i2c_disable_irq(i2c);
  315. goto out_ack;
  316. case STATE_START:
  317. /* last thing we did was send a start condition on the
  318. * bus, or started a new i2c message
  319. */
  320. if (iicstat & S3C2410_IICSTAT_LASTBIT &&
  321. !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  322. /* ack was not received... */
  323. dev_dbg(i2c->dev, "ack was not received\n");
  324. s3c24xx_i2c_stop(i2c, -ENXIO);
  325. goto out_ack;
  326. }
  327. if (i2c->msg->flags & I2C_M_RD)
  328. i2c->state = STATE_READ;
  329. else
  330. i2c->state = STATE_WRITE;
  331. /* terminate the transfer if there is nothing to do
  332. * as this is used by the i2c probe to find devices. */
  333. if (is_lastmsg(i2c) && i2c->msg->len == 0) {
  334. s3c24xx_i2c_stop(i2c, 0);
  335. goto out_ack;
  336. }
  337. if (i2c->state == STATE_READ)
  338. goto prepare_read;
  339. /* fall through to the write state, as we will need to
  340. * send a byte as well */
  341. case STATE_WRITE:
  342. /* we are writing data to the device... check for the
  343. * end of the message, and if so, work out what to do
  344. */
  345. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  346. if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  347. dev_dbg(i2c->dev, "WRITE: No Ack\n");
  348. s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  349. goto out_ack;
  350. }
  351. }
  352. retry_write:
  353. if (!is_msgend(i2c)) {
  354. byte = i2c->msg->buf[i2c->msg_ptr++];
  355. writeb(byte, i2c->regs + S3C2410_IICDS);
  356. /* delay after writing the byte to allow the
  357. * data setup time on the bus, as writing the
  358. * data to the register causes the first bit
  359. * to appear on SDA, and SCL will change as
  360. * soon as the interrupt is acknowledged */
  361. ndelay(i2c->tx_setup);
  362. } else if (!is_lastmsg(i2c)) {
  363. /* we need to go to the next i2c message */
  364. dev_dbg(i2c->dev, "WRITE: Next Message\n");
  365. i2c->msg_ptr = 0;
  366. i2c->msg_idx++;
  367. i2c->msg++;
  368. /* check to see if we need to do another message */
  369. if (i2c->msg->flags & I2C_M_NOSTART) {
  370. if (i2c->msg->flags & I2C_M_RD) {
  371. /* cannot do this, the controller
  372. * forces us to send a new START
  373. * when we change direction */
  374. s3c24xx_i2c_stop(i2c, -EINVAL);
  375. }
  376. goto retry_write;
  377. } else {
  378. /* send the new start */
  379. s3c24xx_i2c_message_start(i2c, i2c->msg);
  380. i2c->state = STATE_START;
  381. }
  382. } else {
  383. /* send stop */
  384. s3c24xx_i2c_stop(i2c, 0);
  385. }
  386. break;
  387. case STATE_READ:
  388. /* we have a byte of data in the data register, do
  389. * something with it, and then work out whether we are
  390. * going to do any more read/write
  391. */
  392. byte = readb(i2c->regs + S3C2410_IICDS);
  393. i2c->msg->buf[i2c->msg_ptr++] = byte;
  394. prepare_read:
  395. if (is_msglast(i2c)) {
  396. /* last byte of buffer */
  397. if (is_lastmsg(i2c))
  398. s3c24xx_i2c_disable_ack(i2c);
  399. } else if (is_msgend(i2c)) {
  400. /* ok, we've read the entire buffer, see if there
  401. * is anything else we need to do */
  402. if (is_lastmsg(i2c)) {
  403. /* last message, send stop and complete */
  404. dev_dbg(i2c->dev, "READ: Send Stop\n");
  405. s3c24xx_i2c_stop(i2c, 0);
  406. } else {
  407. /* go to the next transfer */
  408. dev_dbg(i2c->dev, "READ: Next Transfer\n");
  409. i2c->msg_ptr = 0;
  410. i2c->msg_idx++;
  411. i2c->msg++;
  412. }
  413. }
  414. break;
  415. }
  416. /* acknowlegde the IRQ and get back on with the work */
  417. out_ack:
  418. tmp = readl(i2c->regs + S3C2410_IICCON);
  419. tmp &= ~S3C2410_IICCON_IRQPEND;
  420. writel(tmp, i2c->regs + S3C2410_IICCON);
  421. out:
  422. return ret;
  423. }
  424. /* s3c24xx_i2c_irq
  425. *
  426. * top level IRQ servicing routine
  427. */
  428. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
  429. {
  430. struct s3c24xx_i2c *i2c = dev_id;
  431. unsigned long status;
  432. unsigned long tmp;
  433. status = readl(i2c->regs + S3C2410_IICSTAT);
  434. if (status & S3C2410_IICSTAT_ARBITR) {
  435. /* deal with arbitration loss */
  436. dev_err(i2c->dev, "deal with arbitration loss\n");
  437. }
  438. if (i2c->state == STATE_IDLE) {
  439. dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
  440. tmp = readl(i2c->regs + S3C2410_IICCON);
  441. tmp &= ~S3C2410_IICCON_IRQPEND;
  442. writel(tmp, i2c->regs + S3C2410_IICCON);
  443. goto out;
  444. }
  445. /* pretty much this leaves us with the fact that we've
  446. * transmitted or received whatever byte we last sent */
  447. i2c_s3c_irq_nextbyte(i2c, status);
  448. out:
  449. return IRQ_HANDLED;
  450. }
  451. /* s3c24xx_i2c_set_master
  452. *
  453. * get the i2c bus for a master transaction
  454. */
  455. static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
  456. {
  457. unsigned long iicstat;
  458. int timeout = 400;
  459. while (timeout-- > 0) {
  460. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  461. if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
  462. return 0;
  463. msleep(1);
  464. }
  465. return -ETIMEDOUT;
  466. }
  467. /* s3c24xx_i2c_wait_idle
  468. *
  469. * wait for the i2c bus to become idle.
  470. */
  471. static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
  472. {
  473. unsigned long iicstat;
  474. ktime_t start, now;
  475. unsigned long delay;
  476. int spins;
  477. /* ensure the stop has been through the bus */
  478. dev_dbg(i2c->dev, "waiting for bus idle\n");
  479. start = now = ktime_get();
  480. /*
  481. * Most of the time, the bus is already idle within a few usec of the
  482. * end of a transaction. However, really slow i2c devices can stretch
  483. * the clock, delaying STOP generation.
  484. *
  485. * On slower SoCs this typically happens within a very small number of
  486. * instructions so busy wait briefly to avoid scheduling overhead.
  487. */
  488. spins = 3;
  489. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  490. while ((iicstat & S3C2410_IICSTAT_START) && --spins) {
  491. cpu_relax();
  492. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  493. }
  494. /*
  495. * If we do get an appreciable delay as a compromise between idle
  496. * detection latency for the normal, fast case, and system load in the
  497. * slow device case, use an exponential back off in the polling loop,
  498. * up to 1/10th of the total timeout, then continue to poll at a
  499. * constant rate up to the timeout.
  500. */
  501. delay = 1;
  502. while ((iicstat & S3C2410_IICSTAT_START) &&
  503. ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
  504. usleep_range(delay, 2 * delay);
  505. if (delay < S3C2410_IDLE_TIMEOUT / 10)
  506. delay <<= 1;
  507. now = ktime_get();
  508. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  509. }
  510. if (iicstat & S3C2410_IICSTAT_START)
  511. dev_warn(i2c->dev, "timeout waiting for bus idle\n");
  512. }
  513. /* s3c24xx_i2c_doxfer
  514. *
  515. * this starts an i2c transfer
  516. */
  517. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
  518. struct i2c_msg *msgs, int num)
  519. {
  520. unsigned long timeout;
  521. int ret;
  522. if (i2c->suspended)
  523. return -EIO;
  524. ret = s3c24xx_i2c_set_master(i2c);
  525. if (ret != 0) {
  526. dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  527. ret = -EAGAIN;
  528. goto out;
  529. }
  530. i2c->msg = msgs;
  531. i2c->msg_num = num;
  532. i2c->msg_ptr = 0;
  533. i2c->msg_idx = 0;
  534. i2c->state = STATE_START;
  535. s3c24xx_i2c_enable_irq(i2c);
  536. s3c24xx_i2c_message_start(i2c, msgs);
  537. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  538. ret = i2c->msg_idx;
  539. /* having these next two as dev_err() makes life very
  540. * noisy when doing an i2cdetect */
  541. if (timeout == 0)
  542. dev_dbg(i2c->dev, "timeout\n");
  543. else if (ret != num)
  544. dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  545. /* For QUIRK_HDMIPHY, bus is already disabled */
  546. if (i2c->quirks & QUIRK_HDMIPHY)
  547. goto out;
  548. s3c24xx_i2c_wait_idle(i2c);
  549. out:
  550. return ret;
  551. }
  552. /* s3c24xx_i2c_xfer
  553. *
  554. * first port of call from the i2c bus code when an message needs
  555. * transferring across the i2c bus.
  556. */
  557. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  558. struct i2c_msg *msgs, int num)
  559. {
  560. struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  561. int retry;
  562. int ret;
  563. pm_runtime_get_sync(&adap->dev);
  564. clk_prepare_enable(i2c->clk);
  565. for (retry = 0; retry < adap->retries; retry++) {
  566. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  567. if (ret != -EAGAIN) {
  568. clk_disable_unprepare(i2c->clk);
  569. pm_runtime_put(&adap->dev);
  570. return ret;
  571. }
  572. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  573. udelay(100);
  574. }
  575. clk_disable_unprepare(i2c->clk);
  576. pm_runtime_put(&adap->dev);
  577. return -EREMOTEIO;
  578. }
  579. /* declare our i2c functionality */
  580. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  581. {
  582. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
  583. I2C_FUNC_PROTOCOL_MANGLING;
  584. }
  585. /* i2c bus registration info */
  586. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  587. .master_xfer = s3c24xx_i2c_xfer,
  588. .functionality = s3c24xx_i2c_func,
  589. };
  590. /* s3c24xx_i2c_calcdivisor
  591. *
  592. * return the divisor settings for a given frequency
  593. */
  594. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  595. unsigned int *div1, unsigned int *divs)
  596. {
  597. unsigned int calc_divs = clkin / wanted;
  598. unsigned int calc_div1;
  599. if (calc_divs > (16*16))
  600. calc_div1 = 512;
  601. else
  602. calc_div1 = 16;
  603. calc_divs += calc_div1-1;
  604. calc_divs /= calc_div1;
  605. if (calc_divs == 0)
  606. calc_divs = 1;
  607. if (calc_divs > 17)
  608. calc_divs = 17;
  609. *divs = calc_divs;
  610. *div1 = calc_div1;
  611. return clkin / (calc_divs * calc_div1);
  612. }
  613. /* s3c24xx_i2c_clockrate
  614. *
  615. * work out a divisor for the user requested frequency setting,
  616. * either by the requested frequency, or scanning the acceptable
  617. * range of frequencies until something is found
  618. */
  619. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  620. {
  621. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  622. unsigned long clkin = clk_get_rate(i2c->clk);
  623. unsigned int divs, div1;
  624. unsigned long target_frequency;
  625. u32 iiccon;
  626. int freq;
  627. i2c->clkrate = clkin;
  628. clkin /= 1000; /* clkin now in KHz */
  629. dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
  630. target_frequency = pdata->frequency ? pdata->frequency : 100000;
  631. target_frequency /= 1000; /* Target frequency now in KHz */
  632. freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
  633. if (freq > target_frequency) {
  634. dev_err(i2c->dev,
  635. "Unable to achieve desired frequency %luKHz." \
  636. " Lowest achievable %dKHz\n", target_frequency, freq);
  637. return -EINVAL;
  638. }
  639. *got = freq;
  640. iiccon = readl(i2c->regs + S3C2410_IICCON);
  641. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  642. iiccon |= (divs-1);
  643. if (div1 == 512)
  644. iiccon |= S3C2410_IICCON_TXDIV_512;
  645. writel(iiccon, i2c->regs + S3C2410_IICCON);
  646. if (i2c->quirks & QUIRK_S3C2440) {
  647. unsigned long sda_delay;
  648. if (pdata->sda_delay) {
  649. sda_delay = clkin * pdata->sda_delay;
  650. sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
  651. sda_delay = DIV_ROUND_UP(sda_delay, 5);
  652. if (sda_delay > 3)
  653. sda_delay = 3;
  654. sda_delay |= S3C2410_IICLC_FILTER_ON;
  655. } else
  656. sda_delay = 0;
  657. dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
  658. writel(sda_delay, i2c->regs + S3C2440_IICLC);
  659. }
  660. return 0;
  661. }
  662. #ifdef CONFIG_CPU_FREQ
  663. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  664. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  665. unsigned long val, void *data)
  666. {
  667. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  668. unsigned int got;
  669. int delta_f;
  670. int ret;
  671. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  672. /* if we're post-change and the input clock has slowed down
  673. * or at pre-change and the clock is about to speed up, then
  674. * adjust our clock rate. <0 is slow, >0 speedup.
  675. */
  676. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  677. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  678. i2c_lock_adapter(&i2c->adap);
  679. ret = s3c24xx_i2c_clockrate(i2c, &got);
  680. i2c_unlock_adapter(&i2c->adap);
  681. if (ret < 0)
  682. dev_err(i2c->dev, "cannot find frequency\n");
  683. else
  684. dev_info(i2c->dev, "setting freq %d\n", got);
  685. }
  686. return 0;
  687. }
  688. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  689. {
  690. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  691. return cpufreq_register_notifier(&i2c->freq_transition,
  692. CPUFREQ_TRANSITION_NOTIFIER);
  693. }
  694. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  695. {
  696. cpufreq_unregister_notifier(&i2c->freq_transition,
  697. CPUFREQ_TRANSITION_NOTIFIER);
  698. }
  699. #else
  700. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  701. {
  702. return 0;
  703. }
  704. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  705. {
  706. }
  707. #endif
  708. #ifdef CONFIG_OF
  709. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  710. {
  711. int idx, gpio, ret;
  712. if (i2c->quirks & QUIRK_NO_GPIO)
  713. return 0;
  714. for (idx = 0; idx < 2; idx++) {
  715. gpio = of_get_gpio(i2c->dev->of_node, idx);
  716. if (!gpio_is_valid(gpio)) {
  717. dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
  718. goto free_gpio;
  719. }
  720. i2c->gpios[idx] = gpio;
  721. ret = gpio_request(gpio, "i2c-bus");
  722. if (ret) {
  723. dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
  724. goto free_gpio;
  725. }
  726. }
  727. return 0;
  728. free_gpio:
  729. while (--idx >= 0)
  730. gpio_free(i2c->gpios[idx]);
  731. return -EINVAL;
  732. }
  733. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  734. {
  735. unsigned int idx;
  736. if (i2c->quirks & QUIRK_NO_GPIO)
  737. return;
  738. for (idx = 0; idx < 2; idx++)
  739. gpio_free(i2c->gpios[idx]);
  740. }
  741. #else
  742. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  743. {
  744. return 0;
  745. }
  746. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  747. {
  748. }
  749. #endif
  750. /* s3c24xx_i2c_init
  751. *
  752. * initialise the controller, set the IO lines and frequency
  753. */
  754. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  755. {
  756. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  757. struct s3c2410_platform_i2c *pdata;
  758. unsigned int freq;
  759. /* get the plafrom data */
  760. pdata = i2c->pdata;
  761. /* write slave address */
  762. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  763. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  764. writel(iicon, i2c->regs + S3C2410_IICCON);
  765. /* we need to work out the divisors for the clock... */
  766. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  767. writel(0, i2c->regs + S3C2410_IICCON);
  768. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  769. return -EINVAL;
  770. }
  771. /* todo - check that the i2c lines aren't being dragged anywhere */
  772. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  773. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  774. return 0;
  775. }
  776. #ifdef CONFIG_OF
  777. /* s3c24xx_i2c_parse_dt
  778. *
  779. * Parse the device tree node and retreive the platform data.
  780. */
  781. static void
  782. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  783. {
  784. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  785. if (!np)
  786. return;
  787. pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
  788. of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
  789. of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
  790. of_property_read_u32(np, "samsung,i2c-max-bus-freq",
  791. (u32 *)&pdata->frequency);
  792. }
  793. #else
  794. static void
  795. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  796. {
  797. return;
  798. }
  799. #endif
  800. /* s3c24xx_i2c_probe
  801. *
  802. * called by the bus driver when a suitable device is found
  803. */
  804. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  805. {
  806. struct s3c24xx_i2c *i2c;
  807. struct s3c2410_platform_i2c *pdata = NULL;
  808. struct resource *res;
  809. int ret;
  810. if (!pdev->dev.of_node) {
  811. pdata = pdev->dev.platform_data;
  812. if (!pdata) {
  813. dev_err(&pdev->dev, "no platform data\n");
  814. return -EINVAL;
  815. }
  816. }
  817. i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  818. if (!i2c) {
  819. dev_err(&pdev->dev, "no memory for state\n");
  820. return -ENOMEM;
  821. }
  822. i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  823. if (!i2c->pdata) {
  824. dev_err(&pdev->dev, "no memory for platform data\n");
  825. return -ENOMEM;
  826. }
  827. i2c->quirks = s3c24xx_get_device_quirks(pdev);
  828. if (pdata)
  829. memcpy(i2c->pdata, pdata, sizeof(*pdata));
  830. else
  831. s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
  832. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  833. i2c->adap.owner = THIS_MODULE;
  834. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  835. i2c->adap.retries = 2;
  836. i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  837. i2c->tx_setup = 50;
  838. init_waitqueue_head(&i2c->wait);
  839. /* find the clock and enable it */
  840. i2c->dev = &pdev->dev;
  841. i2c->clk = devm_clk_get(&pdev->dev, "i2c");
  842. if (IS_ERR(i2c->clk)) {
  843. dev_err(&pdev->dev, "cannot get clock\n");
  844. return -ENOENT;
  845. }
  846. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  847. /* map the registers */
  848. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  849. if (res == NULL) {
  850. dev_err(&pdev->dev, "cannot find IO resource\n");
  851. return -ENOENT;
  852. }
  853. i2c->regs = devm_ioremap_resource(&pdev->dev, res);
  854. if (IS_ERR(i2c->regs))
  855. return PTR_ERR(i2c->regs);
  856. dev_dbg(&pdev->dev, "registers %p (%p)\n",
  857. i2c->regs, res);
  858. /* setup info block for the i2c core */
  859. i2c->adap.algo_data = i2c;
  860. i2c->adap.dev.parent = &pdev->dev;
  861. i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
  862. /* inititalise the i2c gpio lines */
  863. if (i2c->pdata->cfg_gpio) {
  864. i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
  865. } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
  866. return -EINVAL;
  867. }
  868. /* initialise the i2c controller */
  869. clk_prepare_enable(i2c->clk);
  870. ret = s3c24xx_i2c_init(i2c);
  871. clk_disable_unprepare(i2c->clk);
  872. if (ret != 0) {
  873. dev_err(&pdev->dev, "I2C controller init failed\n");
  874. return ret;
  875. }
  876. /* find the IRQ for this unit (note, this relies on the init call to
  877. * ensure no current IRQs pending
  878. */
  879. i2c->irq = ret = platform_get_irq(pdev, 0);
  880. if (ret <= 0) {
  881. dev_err(&pdev->dev, "cannot find IRQ\n");
  882. return ret;
  883. }
  884. ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0,
  885. dev_name(&pdev->dev), i2c);
  886. if (ret != 0) {
  887. dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  888. return ret;
  889. }
  890. ret = s3c24xx_i2c_register_cpufreq(i2c);
  891. if (ret < 0) {
  892. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  893. return ret;
  894. }
  895. /* Note, previous versions of the driver used i2c_add_adapter()
  896. * to add the bus at any number. We now pass the bus number via
  897. * the platform data, so if unset it will now default to always
  898. * being bus 0.
  899. */
  900. i2c->adap.nr = i2c->pdata->bus_num;
  901. i2c->adap.dev.of_node = pdev->dev.of_node;
  902. ret = i2c_add_numbered_adapter(&i2c->adap);
  903. if (ret < 0) {
  904. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  905. s3c24xx_i2c_deregister_cpufreq(i2c);
  906. return ret;
  907. }
  908. of_i2c_register_devices(&i2c->adap);
  909. platform_set_drvdata(pdev, i2c);
  910. pm_runtime_enable(&pdev->dev);
  911. pm_runtime_enable(&i2c->adap.dev);
  912. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  913. return 0;
  914. }
  915. /* s3c24xx_i2c_remove
  916. *
  917. * called when device is removed from the bus
  918. */
  919. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  920. {
  921. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  922. pm_runtime_disable(&i2c->adap.dev);
  923. pm_runtime_disable(&pdev->dev);
  924. s3c24xx_i2c_deregister_cpufreq(i2c);
  925. i2c_del_adapter(&i2c->adap);
  926. clk_disable_unprepare(i2c->clk);
  927. if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
  928. s3c24xx_i2c_dt_gpio_free(i2c);
  929. return 0;
  930. }
  931. #ifdef CONFIG_PM_SLEEP
  932. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  933. {
  934. struct platform_device *pdev = to_platform_device(dev);
  935. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  936. i2c->suspended = 1;
  937. return 0;
  938. }
  939. static int s3c24xx_i2c_resume(struct device *dev)
  940. {
  941. struct platform_device *pdev = to_platform_device(dev);
  942. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  943. i2c->suspended = 0;
  944. clk_prepare_enable(i2c->clk);
  945. s3c24xx_i2c_init(i2c);
  946. clk_disable_unprepare(i2c->clk);
  947. return 0;
  948. }
  949. #endif
  950. #ifdef CONFIG_PM
  951. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  952. #ifdef CONFIG_PM_SLEEP
  953. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  954. .resume = s3c24xx_i2c_resume,
  955. #endif
  956. };
  957. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  958. #else
  959. #define S3C24XX_DEV_PM_OPS NULL
  960. #endif
  961. /* device driver for platform bus bits */
  962. static struct platform_driver s3c24xx_i2c_driver = {
  963. .probe = s3c24xx_i2c_probe,
  964. .remove = s3c24xx_i2c_remove,
  965. .id_table = s3c24xx_driver_ids,
  966. .driver = {
  967. .owner = THIS_MODULE,
  968. .name = "s3c-i2c",
  969. .pm = S3C24XX_DEV_PM_OPS,
  970. .of_match_table = of_match_ptr(s3c24xx_i2c_match),
  971. },
  972. };
  973. static int __init i2c_adap_s3c_init(void)
  974. {
  975. return platform_driver_register(&s3c24xx_i2c_driver);
  976. }
  977. subsys_initcall(i2c_adap_s3c_init);
  978. static void __exit i2c_adap_s3c_exit(void)
  979. {
  980. platform_driver_unregister(&s3c24xx_i2c_driver);
  981. }
  982. module_exit(i2c_adap_s3c_exit);
  983. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  984. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  985. MODULE_LICENSE("GPL");