i2c-s3c2410.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. {},
  100. };
  101. MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
  102. #endif
  103. /* s3c24xx_get_device_quirks
  104. *
  105. * Get controller type either from device tree or platform device variant.
  106. */
  107. static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
  108. {
  109. if (pdev->dev.of_node) {
  110. const struct of_device_id *match;
  111. match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
  112. return (unsigned int)match->data;
  113. }
  114. return platform_get_device_id(pdev)->driver_data;
  115. }
  116. /* s3c24xx_i2c_master_complete
  117. *
  118. * complete the message and wake up the caller, using the given return code,
  119. * or zero to mean ok.
  120. */
  121. static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
  122. {
  123. dev_dbg(i2c->dev, "master_complete %d\n", ret);
  124. i2c->msg_ptr = 0;
  125. i2c->msg = NULL;
  126. i2c->msg_idx++;
  127. i2c->msg_num = 0;
  128. if (ret)
  129. i2c->msg_idx = ret;
  130. wake_up(&i2c->wait);
  131. }
  132. static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
  133. {
  134. unsigned long tmp;
  135. tmp = readl(i2c->regs + S3C2410_IICCON);
  136. writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  137. }
  138. static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
  139. {
  140. unsigned long tmp;
  141. tmp = readl(i2c->regs + S3C2410_IICCON);
  142. writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  143. }
  144. /* irq enable/disable functions */
  145. static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
  146. {
  147. unsigned long tmp;
  148. tmp = readl(i2c->regs + S3C2410_IICCON);
  149. writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  150. }
  151. static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
  152. {
  153. unsigned long tmp;
  154. tmp = readl(i2c->regs + S3C2410_IICCON);
  155. writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  156. }
  157. /* s3c24xx_i2c_message_start
  158. *
  159. * put the start of a message onto the bus
  160. */
  161. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
  162. struct i2c_msg *msg)
  163. {
  164. unsigned int addr = (msg->addr & 0x7f) << 1;
  165. unsigned long stat;
  166. unsigned long iiccon;
  167. stat = 0;
  168. stat |= S3C2410_IICSTAT_TXRXEN;
  169. if (msg->flags & I2C_M_RD) {
  170. stat |= S3C2410_IICSTAT_MASTER_RX;
  171. addr |= 1;
  172. } else
  173. stat |= S3C2410_IICSTAT_MASTER_TX;
  174. if (msg->flags & I2C_M_REV_DIR_ADDR)
  175. addr ^= 1;
  176. /* todo - check for wether ack wanted or not */
  177. s3c24xx_i2c_enable_ack(i2c);
  178. iiccon = readl(i2c->regs + S3C2410_IICCON);
  179. writel(stat, i2c->regs + S3C2410_IICSTAT);
  180. dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
  181. writeb(addr, i2c->regs + S3C2410_IICDS);
  182. /* delay here to ensure the data byte has gotten onto the bus
  183. * before the transaction is started */
  184. ndelay(i2c->tx_setup);
  185. dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
  186. writel(iiccon, i2c->regs + S3C2410_IICCON);
  187. stat |= S3C2410_IICSTAT_START;
  188. writel(stat, i2c->regs + S3C2410_IICSTAT);
  189. }
  190. static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
  191. {
  192. unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  193. dev_dbg(i2c->dev, "STOP\n");
  194. /*
  195. * The datasheet says that the STOP sequence should be:
  196. * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
  197. * 2) I2CCON.4 = 0 - Clear IRQPEND
  198. * 3) Wait until the stop condition takes effect.
  199. * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
  200. *
  201. * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
  202. *
  203. * However, after much experimentation, it appears that:
  204. * a) normal buses automatically clear BUSY and transition from
  205. * Master->Slave when they complete generating a STOP condition.
  206. * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
  207. * after starting the STOP generation here.
  208. * b) HDMIPHY bus does neither, so there is no way to do step 3.
  209. * There is no indication when this bus has finished generating
  210. * STOP.
  211. *
  212. * In fact, we have found that as soon as the IRQPEND bit is cleared in
  213. * step 2, the HDMIPHY bus generates the STOP condition, and then
  214. * immediately starts transferring another data byte, even though the
  215. * bus is supposedly stopped. This is presumably because the bus is
  216. * still in "Master" mode, and its BUSY bit is still set.
  217. *
  218. * To avoid these extra post-STOP transactions on HDMI phy devices, we
  219. * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
  220. * instead of first generating a proper STOP condition. This should
  221. * float SDA & SCK terminating the transfer. Subsequent transfers
  222. * start with a proper START condition, and proceed normally.
  223. *
  224. * The HDMIPHY bus is an internal bus that always has exactly two
  225. * devices, the host as Master and the HDMIPHY device as the slave.
  226. * Skipping the STOP condition has been tested on this bus and works.
  227. */
  228. if (i2c->quirks & QUIRK_HDMIPHY) {
  229. /* Stop driving the I2C pins */
  230. iicstat &= ~S3C2410_IICSTAT_TXRXEN;
  231. } else {
  232. /* stop the transfer */
  233. iicstat &= ~S3C2410_IICSTAT_START;
  234. }
  235. writel(iicstat, i2c->regs + S3C2410_IICSTAT);
  236. i2c->state = STATE_STOP;
  237. s3c24xx_i2c_master_complete(i2c, ret);
  238. s3c24xx_i2c_disable_irq(i2c);
  239. }
  240. /* helper functions to determine the current state in the set of
  241. * messages we are sending */
  242. /* is_lastmsg()
  243. *
  244. * returns TRUE if the current message is the last in the set
  245. */
  246. static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
  247. {
  248. return i2c->msg_idx >= (i2c->msg_num - 1);
  249. }
  250. /* is_msglast
  251. *
  252. * returns TRUE if we this is the last byte in the current message
  253. */
  254. static inline int is_msglast(struct s3c24xx_i2c *i2c)
  255. {
  256. return i2c->msg_ptr == i2c->msg->len-1;
  257. }
  258. /* is_msgend
  259. *
  260. * returns TRUE if we reached the end of the current message
  261. */
  262. static inline int is_msgend(struct s3c24xx_i2c *i2c)
  263. {
  264. return i2c->msg_ptr >= i2c->msg->len;
  265. }
  266. /* i2c_s3c_irq_nextbyte
  267. *
  268. * process an interrupt and work out what to do
  269. */
  270. static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
  271. {
  272. unsigned long tmp;
  273. unsigned char byte;
  274. int ret = 0;
  275. switch (i2c->state) {
  276. case STATE_IDLE:
  277. dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
  278. goto out;
  279. case STATE_STOP:
  280. dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
  281. s3c24xx_i2c_disable_irq(i2c);
  282. goto out_ack;
  283. case STATE_START:
  284. /* last thing we did was send a start condition on the
  285. * bus, or started a new i2c message
  286. */
  287. if (iicstat & S3C2410_IICSTAT_LASTBIT &&
  288. !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  289. /* ack was not received... */
  290. dev_dbg(i2c->dev, "ack was not received\n");
  291. s3c24xx_i2c_stop(i2c, -ENXIO);
  292. goto out_ack;
  293. }
  294. if (i2c->msg->flags & I2C_M_RD)
  295. i2c->state = STATE_READ;
  296. else
  297. i2c->state = STATE_WRITE;
  298. /* terminate the transfer if there is nothing to do
  299. * as this is used by the i2c probe to find devices. */
  300. if (is_lastmsg(i2c) && i2c->msg->len == 0) {
  301. s3c24xx_i2c_stop(i2c, 0);
  302. goto out_ack;
  303. }
  304. if (i2c->state == STATE_READ)
  305. goto prepare_read;
  306. /* fall through to the write state, as we will need to
  307. * send a byte as well */
  308. case STATE_WRITE:
  309. /* we are writing data to the device... check for the
  310. * end of the message, and if so, work out what to do
  311. */
  312. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  313. if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  314. dev_dbg(i2c->dev, "WRITE: No Ack\n");
  315. s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  316. goto out_ack;
  317. }
  318. }
  319. retry_write:
  320. if (!is_msgend(i2c)) {
  321. byte = i2c->msg->buf[i2c->msg_ptr++];
  322. writeb(byte, i2c->regs + S3C2410_IICDS);
  323. /* delay after writing the byte to allow the
  324. * data setup time on the bus, as writing the
  325. * data to the register causes the first bit
  326. * to appear on SDA, and SCL will change as
  327. * soon as the interrupt is acknowledged */
  328. ndelay(i2c->tx_setup);
  329. } else if (!is_lastmsg(i2c)) {
  330. /* we need to go to the next i2c message */
  331. dev_dbg(i2c->dev, "WRITE: Next Message\n");
  332. i2c->msg_ptr = 0;
  333. i2c->msg_idx++;
  334. i2c->msg++;
  335. /* check to see if we need to do another message */
  336. if (i2c->msg->flags & I2C_M_NOSTART) {
  337. if (i2c->msg->flags & I2C_M_RD) {
  338. /* cannot do this, the controller
  339. * forces us to send a new START
  340. * when we change direction */
  341. s3c24xx_i2c_stop(i2c, -EINVAL);
  342. }
  343. goto retry_write;
  344. } else {
  345. /* send the new start */
  346. s3c24xx_i2c_message_start(i2c, i2c->msg);
  347. i2c->state = STATE_START;
  348. }
  349. } else {
  350. /* send stop */
  351. s3c24xx_i2c_stop(i2c, 0);
  352. }
  353. break;
  354. case STATE_READ:
  355. /* we have a byte of data in the data register, do
  356. * something with it, and then work out wether we are
  357. * going to do any more read/write
  358. */
  359. byte = readb(i2c->regs + S3C2410_IICDS);
  360. i2c->msg->buf[i2c->msg_ptr++] = byte;
  361. prepare_read:
  362. if (is_msglast(i2c)) {
  363. /* last byte of buffer */
  364. if (is_lastmsg(i2c))
  365. s3c24xx_i2c_disable_ack(i2c);
  366. } else if (is_msgend(i2c)) {
  367. /* ok, we've read the entire buffer, see if there
  368. * is anything else we need to do */
  369. if (is_lastmsg(i2c)) {
  370. /* last message, send stop and complete */
  371. dev_dbg(i2c->dev, "READ: Send Stop\n");
  372. s3c24xx_i2c_stop(i2c, 0);
  373. } else {
  374. /* go to the next transfer */
  375. dev_dbg(i2c->dev, "READ: Next Transfer\n");
  376. i2c->msg_ptr = 0;
  377. i2c->msg_idx++;
  378. i2c->msg++;
  379. }
  380. }
  381. break;
  382. }
  383. /* acknowlegde the IRQ and get back on with the work */
  384. out_ack:
  385. tmp = readl(i2c->regs + S3C2410_IICCON);
  386. tmp &= ~S3C2410_IICCON_IRQPEND;
  387. writel(tmp, i2c->regs + S3C2410_IICCON);
  388. out:
  389. return ret;
  390. }
  391. /* s3c24xx_i2c_irq
  392. *
  393. * top level IRQ servicing routine
  394. */
  395. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
  396. {
  397. struct s3c24xx_i2c *i2c = dev_id;
  398. unsigned long status;
  399. unsigned long tmp;
  400. status = readl(i2c->regs + S3C2410_IICSTAT);
  401. if (status & S3C2410_IICSTAT_ARBITR) {
  402. /* deal with arbitration loss */
  403. dev_err(i2c->dev, "deal with arbitration loss\n");
  404. }
  405. if (i2c->state == STATE_IDLE) {
  406. dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
  407. tmp = readl(i2c->regs + S3C2410_IICCON);
  408. tmp &= ~S3C2410_IICCON_IRQPEND;
  409. writel(tmp, i2c->regs + S3C2410_IICCON);
  410. goto out;
  411. }
  412. /* pretty much this leaves us with the fact that we've
  413. * transmitted or received whatever byte we last sent */
  414. i2c_s3c_irq_nextbyte(i2c, status);
  415. out:
  416. return IRQ_HANDLED;
  417. }
  418. /* s3c24xx_i2c_set_master
  419. *
  420. * get the i2c bus for a master transaction
  421. */
  422. static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
  423. {
  424. unsigned long iicstat;
  425. int timeout = 400;
  426. /* the timeout for HDMIPHY is reduced to 10 ms because
  427. * the hangup is expected to happen, so waiting 400 ms
  428. * causes only unnecessary system hangup
  429. */
  430. if (i2c->quirks & QUIRK_HDMIPHY)
  431. timeout = 10;
  432. while (timeout-- > 0) {
  433. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  434. if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
  435. return 0;
  436. msleep(1);
  437. }
  438. /* hang-up of bus dedicated for HDMIPHY occurred, resetting */
  439. if (i2c->quirks & QUIRK_HDMIPHY) {
  440. writel(0, i2c->regs + S3C2410_IICCON);
  441. writel(0, i2c->regs + S3C2410_IICSTAT);
  442. writel(0, i2c->regs + S3C2410_IICDS);
  443. return 0;
  444. }
  445. return -ETIMEDOUT;
  446. }
  447. /* s3c24xx_i2c_wait_idle
  448. *
  449. * wait for the i2c bus to become idle.
  450. */
  451. static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
  452. {
  453. unsigned long iicstat;
  454. ktime_t start, now;
  455. unsigned long delay;
  456. /* ensure the stop has been through the bus */
  457. dev_dbg(i2c->dev, "waiting for bus idle\n");
  458. start = now = ktime_get();
  459. /*
  460. * Most of the time, the bus is already idle within a few usec of the
  461. * end of a transaction. However, really slow i2c devices can stretch
  462. * the clock, delaying STOP generation.
  463. *
  464. * As a compromise between idle detection latency for the normal, fast
  465. * case, and system load in the slow device case, use an exponential
  466. * back off in the polling loop, up to 1/10th of the total timeout,
  467. * then continue to poll at a constant rate up to the timeout.
  468. */
  469. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  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. ret = gpio_request(gpio, "i2c-bus");
  690. if (ret) {
  691. dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
  692. goto free_gpio;
  693. }
  694. }
  695. return 0;
  696. free_gpio:
  697. while (--idx >= 0)
  698. gpio_free(i2c->gpios[idx]);
  699. return -EINVAL;
  700. }
  701. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  702. {
  703. unsigned int idx;
  704. if (i2c->quirks & QUIRK_NO_GPIO)
  705. return;
  706. for (idx = 0; idx < 2; idx++)
  707. gpio_free(i2c->gpios[idx]);
  708. }
  709. #else
  710. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  711. {
  712. return 0;
  713. }
  714. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  715. {
  716. }
  717. #endif
  718. /* s3c24xx_i2c_init
  719. *
  720. * initialise the controller, set the IO lines and frequency
  721. */
  722. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  723. {
  724. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  725. struct s3c2410_platform_i2c *pdata;
  726. unsigned int freq;
  727. /* get the plafrom data */
  728. pdata = i2c->pdata;
  729. /* inititalise the gpio */
  730. if (pdata->cfg_gpio)
  731. pdata->cfg_gpio(to_platform_device(i2c->dev));
  732. else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
  733. return -EINVAL;
  734. /* write slave address */
  735. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  736. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  737. writel(iicon, i2c->regs + S3C2410_IICCON);
  738. /* we need to work out the divisors for the clock... */
  739. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  740. writel(0, i2c->regs + S3C2410_IICCON);
  741. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  742. return -EINVAL;
  743. }
  744. /* todo - check that the i2c lines aren't being dragged anywhere */
  745. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  746. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  747. return 0;
  748. }
  749. #ifdef CONFIG_OF
  750. /* s3c24xx_i2c_parse_dt
  751. *
  752. * Parse the device tree node and retreive the platform data.
  753. */
  754. static void
  755. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  756. {
  757. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  758. if (!np)
  759. return;
  760. pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
  761. of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
  762. of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
  763. of_property_read_u32(np, "samsung,i2c-max-bus-freq",
  764. (u32 *)&pdata->frequency);
  765. }
  766. #else
  767. static void
  768. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  769. {
  770. return;
  771. }
  772. #endif
  773. /* s3c24xx_i2c_probe
  774. *
  775. * called by the bus driver when a suitable device is found
  776. */
  777. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  778. {
  779. struct s3c24xx_i2c *i2c;
  780. struct s3c2410_platform_i2c *pdata = NULL;
  781. struct resource *res;
  782. int ret;
  783. if (!pdev->dev.of_node) {
  784. pdata = pdev->dev.platform_data;
  785. if (!pdata) {
  786. dev_err(&pdev->dev, "no platform data\n");
  787. return -EINVAL;
  788. }
  789. }
  790. i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  791. if (!i2c) {
  792. dev_err(&pdev->dev, "no memory for state\n");
  793. return -ENOMEM;
  794. }
  795. i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  796. if (!i2c->pdata) {
  797. ret = -ENOMEM;
  798. goto err_noclk;
  799. }
  800. i2c->quirks = s3c24xx_get_device_quirks(pdev);
  801. if (pdata)
  802. memcpy(i2c->pdata, pdata, sizeof(*pdata));
  803. else
  804. s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
  805. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  806. i2c->adap.owner = THIS_MODULE;
  807. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  808. i2c->adap.retries = 2;
  809. i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  810. i2c->tx_setup = 50;
  811. init_waitqueue_head(&i2c->wait);
  812. /* find the clock and enable it */
  813. i2c->dev = &pdev->dev;
  814. i2c->clk = clk_get(&pdev->dev, "i2c");
  815. if (IS_ERR(i2c->clk)) {
  816. dev_err(&pdev->dev, "cannot get clock\n");
  817. ret = -ENOENT;
  818. goto err_noclk;
  819. }
  820. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  821. clk_prepare_enable(i2c->clk);
  822. /* map the registers */
  823. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  824. if (res == NULL) {
  825. dev_err(&pdev->dev, "cannot find IO resource\n");
  826. ret = -ENOENT;
  827. goto err_clk;
  828. }
  829. i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
  830. if (i2c->regs == NULL) {
  831. dev_err(&pdev->dev, "cannot map IO\n");
  832. ret = -ENXIO;
  833. goto err_clk;
  834. }
  835. dev_dbg(&pdev->dev, "registers %p (%p)\n",
  836. i2c->regs, res);
  837. /* setup info block for the i2c core */
  838. i2c->adap.algo_data = i2c;
  839. i2c->adap.dev.parent = &pdev->dev;
  840. i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
  841. /* initialise the i2c controller */
  842. ret = s3c24xx_i2c_init(i2c);
  843. if (ret != 0)
  844. goto err_clk;
  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. goto err_clk;
  852. }
  853. ret = request_irq(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. goto err_clk;
  858. }
  859. ret = s3c24xx_i2c_register_cpufreq(i2c);
  860. if (ret < 0) {
  861. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  862. goto err_irq;
  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. goto err_cpufreq;
  875. }
  876. of_i2c_register_devices(&i2c->adap);
  877. platform_set_drvdata(pdev, i2c);
  878. pm_runtime_enable(&pdev->dev);
  879. pm_runtime_enable(&i2c->adap.dev);
  880. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  881. clk_disable_unprepare(i2c->clk);
  882. return 0;
  883. err_cpufreq:
  884. s3c24xx_i2c_deregister_cpufreq(i2c);
  885. err_irq:
  886. free_irq(i2c->irq, i2c);
  887. err_clk:
  888. clk_disable_unprepare(i2c->clk);
  889. clk_put(i2c->clk);
  890. err_noclk:
  891. return ret;
  892. }
  893. /* s3c24xx_i2c_remove
  894. *
  895. * called when device is removed from the bus
  896. */
  897. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  898. {
  899. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  900. pm_runtime_disable(&i2c->adap.dev);
  901. pm_runtime_disable(&pdev->dev);
  902. s3c24xx_i2c_deregister_cpufreq(i2c);
  903. i2c_del_adapter(&i2c->adap);
  904. free_irq(i2c->irq, i2c);
  905. clk_disable_unprepare(i2c->clk);
  906. clk_put(i2c->clk);
  907. if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
  908. s3c24xx_i2c_dt_gpio_free(i2c);
  909. return 0;
  910. }
  911. #ifdef CONFIG_PM_SLEEP
  912. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  913. {
  914. struct platform_device *pdev = to_platform_device(dev);
  915. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  916. i2c->suspended = 1;
  917. return 0;
  918. }
  919. static int s3c24xx_i2c_resume(struct device *dev)
  920. {
  921. struct platform_device *pdev = to_platform_device(dev);
  922. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  923. i2c->suspended = 0;
  924. clk_prepare_enable(i2c->clk);
  925. s3c24xx_i2c_init(i2c);
  926. clk_disable_unprepare(i2c->clk);
  927. return 0;
  928. }
  929. #endif
  930. #ifdef CONFIG_PM
  931. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  932. #ifdef CONFIG_PM_SLEEP
  933. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  934. .resume = s3c24xx_i2c_resume,
  935. #endif
  936. };
  937. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  938. #else
  939. #define S3C24XX_DEV_PM_OPS NULL
  940. #endif
  941. /* device driver for platform bus bits */
  942. static struct platform_driver s3c24xx_i2c_driver = {
  943. .probe = s3c24xx_i2c_probe,
  944. .remove = s3c24xx_i2c_remove,
  945. .id_table = s3c24xx_driver_ids,
  946. .driver = {
  947. .owner = THIS_MODULE,
  948. .name = "s3c-i2c",
  949. .pm = S3C24XX_DEV_PM_OPS,
  950. .of_match_table = of_match_ptr(s3c24xx_i2c_match),
  951. },
  952. };
  953. static int __init i2c_adap_s3c_init(void)
  954. {
  955. return platform_driver_register(&s3c24xx_i2c_driver);
  956. }
  957. subsys_initcall(i2c_adap_s3c_init);
  958. static void __exit i2c_adap_s3c_exit(void)
  959. {
  960. platform_driver_unregister(&s3c24xx_i2c_driver);
  961. }
  962. module_exit(i2c_adap_s3c_exit);
  963. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  964. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  965. MODULE_LICENSE("GPL");