i2c-s3c2410.c 29 KB

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