i2c-s3c2410.c 32 KB

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