i2c-s3c2410.c 33 KB

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