i2c-s3c2410.c 32 KB

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