i2c-s3c2410.c 32 KB

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