i2c-s3c2410.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. /* 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. ret = clk_enable(i2c->clk);
  632. if (ret)
  633. return ret;
  634. for (retry = 0; retry < adap->retries; retry++) {
  635. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  636. if (ret != -EAGAIN) {
  637. clk_disable(i2c->clk);
  638. pm_runtime_put(&adap->dev);
  639. return ret;
  640. }
  641. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  642. udelay(100);
  643. }
  644. clk_disable(i2c->clk);
  645. pm_runtime_put(&adap->dev);
  646. return -EREMOTEIO;
  647. }
  648. /* declare our i2c functionality */
  649. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  650. {
  651. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
  652. I2C_FUNC_PROTOCOL_MANGLING;
  653. }
  654. /* i2c bus registration info */
  655. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  656. .master_xfer = s3c24xx_i2c_xfer,
  657. .functionality = s3c24xx_i2c_func,
  658. };
  659. /* s3c24xx_i2c_calcdivisor
  660. *
  661. * return the divisor settings for a given frequency
  662. */
  663. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  664. unsigned int *div1, unsigned int *divs)
  665. {
  666. unsigned int calc_divs = clkin / wanted;
  667. unsigned int calc_div1;
  668. if (calc_divs > (16*16))
  669. calc_div1 = 512;
  670. else
  671. calc_div1 = 16;
  672. calc_divs += calc_div1-1;
  673. calc_divs /= calc_div1;
  674. if (calc_divs == 0)
  675. calc_divs = 1;
  676. if (calc_divs > 17)
  677. calc_divs = 17;
  678. *divs = calc_divs;
  679. *div1 = calc_div1;
  680. return clkin / (calc_divs * calc_div1);
  681. }
  682. /* s3c24xx_i2c_clockrate
  683. *
  684. * work out a divisor for the user requested frequency setting,
  685. * either by the requested frequency, or scanning the acceptable
  686. * range of frequencies until something is found
  687. */
  688. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  689. {
  690. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  691. unsigned long clkin = clk_get_rate(i2c->clk);
  692. unsigned int divs, div1;
  693. unsigned long target_frequency;
  694. u32 iiccon;
  695. int freq;
  696. i2c->clkrate = clkin;
  697. clkin /= 1000; /* clkin now in KHz */
  698. dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
  699. target_frequency = pdata->frequency ? pdata->frequency : 100000;
  700. target_frequency /= 1000; /* Target frequency now in KHz */
  701. freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
  702. if (freq > target_frequency) {
  703. dev_err(i2c->dev,
  704. "Unable to achieve desired frequency %luKHz." \
  705. " Lowest achievable %dKHz\n", target_frequency, freq);
  706. return -EINVAL;
  707. }
  708. *got = freq;
  709. iiccon = readl(i2c->regs + S3C2410_IICCON);
  710. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  711. iiccon |= (divs-1);
  712. if (div1 == 512)
  713. iiccon |= S3C2410_IICCON_TXDIV_512;
  714. if (i2c->quirks & QUIRK_POLL)
  715. iiccon |= S3C2410_IICCON_SCALE(2);
  716. writel(iiccon, i2c->regs + S3C2410_IICCON);
  717. if (i2c->quirks & QUIRK_S3C2440) {
  718. unsigned long sda_delay;
  719. if (pdata->sda_delay) {
  720. sda_delay = clkin * pdata->sda_delay;
  721. sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
  722. sda_delay = DIV_ROUND_UP(sda_delay, 5);
  723. if (sda_delay > 3)
  724. sda_delay = 3;
  725. sda_delay |= S3C2410_IICLC_FILTER_ON;
  726. } else
  727. sda_delay = 0;
  728. dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
  729. writel(sda_delay, i2c->regs + S3C2440_IICLC);
  730. }
  731. return 0;
  732. }
  733. #if defined(CONFIG_ARM_S3C24XX_CPUFREQ)
  734. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  735. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  736. unsigned long val, void *data)
  737. {
  738. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  739. unsigned int got;
  740. int delta_f;
  741. int ret;
  742. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  743. /* if we're post-change and the input clock has slowed down
  744. * or at pre-change and the clock is about to speed up, then
  745. * adjust our clock rate. <0 is slow, >0 speedup.
  746. */
  747. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  748. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  749. i2c_lock_adapter(&i2c->adap);
  750. ret = s3c24xx_i2c_clockrate(i2c, &got);
  751. i2c_unlock_adapter(&i2c->adap);
  752. if (ret < 0)
  753. dev_err(i2c->dev, "cannot find frequency\n");
  754. else
  755. dev_info(i2c->dev, "setting freq %d\n", got);
  756. }
  757. return 0;
  758. }
  759. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  760. {
  761. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  762. return cpufreq_register_notifier(&i2c->freq_transition,
  763. CPUFREQ_TRANSITION_NOTIFIER);
  764. }
  765. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  766. {
  767. cpufreq_unregister_notifier(&i2c->freq_transition,
  768. CPUFREQ_TRANSITION_NOTIFIER);
  769. }
  770. #else
  771. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  772. {
  773. return 0;
  774. }
  775. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  776. {
  777. }
  778. #endif
  779. #ifdef CONFIG_OF
  780. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  781. {
  782. int idx, gpio, ret;
  783. if (i2c->quirks & QUIRK_NO_GPIO)
  784. return 0;
  785. for (idx = 0; idx < 2; idx++) {
  786. gpio = of_get_gpio(i2c->dev->of_node, idx);
  787. if (!gpio_is_valid(gpio)) {
  788. dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
  789. goto free_gpio;
  790. }
  791. i2c->gpios[idx] = gpio;
  792. ret = gpio_request(gpio, "i2c-bus");
  793. if (ret) {
  794. dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
  795. goto free_gpio;
  796. }
  797. }
  798. return 0;
  799. free_gpio:
  800. while (--idx >= 0)
  801. gpio_free(i2c->gpios[idx]);
  802. return -EINVAL;
  803. }
  804. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  805. {
  806. unsigned int idx;
  807. if (i2c->quirks & QUIRK_NO_GPIO)
  808. return;
  809. for (idx = 0; idx < 2; idx++)
  810. gpio_free(i2c->gpios[idx]);
  811. }
  812. #else
  813. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  814. {
  815. return 0;
  816. }
  817. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  818. {
  819. }
  820. #endif
  821. /* s3c24xx_i2c_init
  822. *
  823. * initialise the controller, set the IO lines and frequency
  824. */
  825. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  826. {
  827. struct s3c2410_platform_i2c *pdata;
  828. unsigned int freq;
  829. /* get the plafrom data */
  830. pdata = i2c->pdata;
  831. /* write slave address */
  832. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  833. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  834. writel(0, i2c->regs + S3C2410_IICCON);
  835. writel(0, i2c->regs + S3C2410_IICSTAT);
  836. /* we need to work out the divisors for the clock... */
  837. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  838. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  839. return -EINVAL;
  840. }
  841. /* todo - check that the i2c lines aren't being dragged anywhere */
  842. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  843. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n",
  844. readl(i2c->regs + S3C2410_IICCON));
  845. return 0;
  846. }
  847. #ifdef CONFIG_OF
  848. /* s3c24xx_i2c_parse_dt
  849. *
  850. * Parse the device tree node and retreive the platform data.
  851. */
  852. static void
  853. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  854. {
  855. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  856. int id;
  857. if (!np)
  858. return;
  859. pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
  860. of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
  861. of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
  862. of_property_read_u32(np, "samsung,i2c-max-bus-freq",
  863. (u32 *)&pdata->frequency);
  864. /*
  865. * Exynos5's legacy i2c controller and new high speed i2c
  866. * controller have muxed interrupt sources. By default the
  867. * interrupts for 4-channel HS-I2C controller are enabled.
  868. * If nodes for first four channels of legacy i2c controller
  869. * are available then re-configure the interrupts via the
  870. * system register.
  871. */
  872. id = of_alias_get_id(np, "i2c");
  873. i2c->sysreg = syscon_regmap_lookup_by_phandle(np,
  874. "samsung,sysreg-phandle");
  875. if (IS_ERR(i2c->sysreg))
  876. return;
  877. regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0);
  878. }
  879. #else
  880. static void
  881. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  882. {
  883. return;
  884. }
  885. #endif
  886. /* s3c24xx_i2c_probe
  887. *
  888. * called by the bus driver when a suitable device is found
  889. */
  890. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  891. {
  892. struct s3c24xx_i2c *i2c;
  893. struct s3c2410_platform_i2c *pdata = NULL;
  894. struct resource *res;
  895. int ret;
  896. if (!pdev->dev.of_node) {
  897. pdata = dev_get_platdata(&pdev->dev);
  898. if (!pdata) {
  899. dev_err(&pdev->dev, "no platform data\n");
  900. return -EINVAL;
  901. }
  902. }
  903. i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  904. if (!i2c)
  905. return -ENOMEM;
  906. i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  907. if (!i2c->pdata)
  908. return -ENOMEM;
  909. i2c->quirks = s3c24xx_get_device_quirks(pdev);
  910. i2c->sysreg = ERR_PTR(-ENOENT);
  911. if (pdata)
  912. memcpy(i2c->pdata, pdata, sizeof(*pdata));
  913. else
  914. s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
  915. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  916. i2c->adap.owner = THIS_MODULE;
  917. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  918. i2c->adap.retries = 2;
  919. i2c->adap.class = I2C_CLASS_DEPRECATED;
  920. i2c->tx_setup = 50;
  921. init_waitqueue_head(&i2c->wait);
  922. /* find the clock and enable it */
  923. i2c->dev = &pdev->dev;
  924. i2c->clk = devm_clk_get(&pdev->dev, "i2c");
  925. if (IS_ERR(i2c->clk)) {
  926. dev_err(&pdev->dev, "cannot get clock\n");
  927. return -ENOENT;
  928. }
  929. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  930. /* map the registers */
  931. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  932. i2c->regs = devm_ioremap_resource(&pdev->dev, res);
  933. if (IS_ERR(i2c->regs))
  934. return PTR_ERR(i2c->regs);
  935. dev_dbg(&pdev->dev, "registers %p (%p)\n",
  936. i2c->regs, res);
  937. /* setup info block for the i2c core */
  938. i2c->adap.algo_data = i2c;
  939. i2c->adap.dev.parent = &pdev->dev;
  940. i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
  941. /* inititalise the i2c gpio lines */
  942. if (i2c->pdata->cfg_gpio) {
  943. i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
  944. } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
  945. return -EINVAL;
  946. }
  947. /* initialise the i2c controller */
  948. clk_prepare_enable(i2c->clk);
  949. ret = s3c24xx_i2c_init(i2c);
  950. clk_disable(i2c->clk);
  951. if (ret != 0) {
  952. dev_err(&pdev->dev, "I2C controller init failed\n");
  953. return ret;
  954. }
  955. /* find the IRQ for this unit (note, this relies on the init call to
  956. * ensure no current IRQs pending
  957. */
  958. if (!(i2c->quirks & QUIRK_POLL)) {
  959. i2c->irq = ret = platform_get_irq(pdev, 0);
  960. if (ret <= 0) {
  961. dev_err(&pdev->dev, "cannot find IRQ\n");
  962. clk_unprepare(i2c->clk);
  963. return ret;
  964. }
  965. ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0,
  966. dev_name(&pdev->dev), i2c);
  967. if (ret != 0) {
  968. dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  969. clk_unprepare(i2c->clk);
  970. return ret;
  971. }
  972. }
  973. ret = s3c24xx_i2c_register_cpufreq(i2c);
  974. if (ret < 0) {
  975. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  976. clk_unprepare(i2c->clk);
  977. return ret;
  978. }
  979. /* Note, previous versions of the driver used i2c_add_adapter()
  980. * to add the bus at any number. We now pass the bus number via
  981. * the platform data, so if unset it will now default to always
  982. * being bus 0.
  983. */
  984. i2c->adap.nr = i2c->pdata->bus_num;
  985. i2c->adap.dev.of_node = pdev->dev.of_node;
  986. platform_set_drvdata(pdev, i2c);
  987. pm_runtime_enable(&pdev->dev);
  988. ret = i2c_add_numbered_adapter(&i2c->adap);
  989. if (ret < 0) {
  990. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  991. pm_runtime_disable(&pdev->dev);
  992. s3c24xx_i2c_deregister_cpufreq(i2c);
  993. clk_unprepare(i2c->clk);
  994. return ret;
  995. }
  996. pm_runtime_enable(&i2c->adap.dev);
  997. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  998. return 0;
  999. }
  1000. /* s3c24xx_i2c_remove
  1001. *
  1002. * called when device is removed from the bus
  1003. */
  1004. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  1005. {
  1006. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  1007. clk_unprepare(i2c->clk);
  1008. pm_runtime_disable(&i2c->adap.dev);
  1009. pm_runtime_disable(&pdev->dev);
  1010. s3c24xx_i2c_deregister_cpufreq(i2c);
  1011. i2c_del_adapter(&i2c->adap);
  1012. if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
  1013. s3c24xx_i2c_dt_gpio_free(i2c);
  1014. return 0;
  1015. }
  1016. #ifdef CONFIG_PM_SLEEP
  1017. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  1018. {
  1019. struct platform_device *pdev = to_platform_device(dev);
  1020. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  1021. i2c->suspended = 1;
  1022. if (!IS_ERR(i2c->sysreg))
  1023. regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg);
  1024. return 0;
  1025. }
  1026. static int s3c24xx_i2c_resume_noirq(struct device *dev)
  1027. {
  1028. struct platform_device *pdev = to_platform_device(dev);
  1029. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  1030. int ret;
  1031. if (!IS_ERR(i2c->sysreg))
  1032. regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg);
  1033. ret = clk_enable(i2c->clk);
  1034. if (ret)
  1035. return ret;
  1036. s3c24xx_i2c_init(i2c);
  1037. clk_disable(i2c->clk);
  1038. i2c->suspended = 0;
  1039. return 0;
  1040. }
  1041. #endif
  1042. #ifdef CONFIG_PM
  1043. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  1044. #ifdef CONFIG_PM_SLEEP
  1045. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  1046. .resume_noirq = s3c24xx_i2c_resume_noirq,
  1047. .freeze_noirq = s3c24xx_i2c_suspend_noirq,
  1048. .thaw_noirq = s3c24xx_i2c_resume_noirq,
  1049. .poweroff_noirq = s3c24xx_i2c_suspend_noirq,
  1050. .restore_noirq = s3c24xx_i2c_resume_noirq,
  1051. #endif
  1052. };
  1053. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  1054. #else
  1055. #define S3C24XX_DEV_PM_OPS NULL
  1056. #endif
  1057. /* device driver for platform bus bits */
  1058. static struct platform_driver s3c24xx_i2c_driver = {
  1059. .probe = s3c24xx_i2c_probe,
  1060. .remove = s3c24xx_i2c_remove,
  1061. .id_table = s3c24xx_driver_ids,
  1062. .driver = {
  1063. .name = "s3c-i2c",
  1064. .pm = S3C24XX_DEV_PM_OPS,
  1065. .of_match_table = of_match_ptr(s3c24xx_i2c_match),
  1066. },
  1067. };
  1068. static int __init i2c_adap_s3c_init(void)
  1069. {
  1070. return platform_driver_register(&s3c24xx_i2c_driver);
  1071. }
  1072. subsys_initcall(i2c_adap_s3c_init);
  1073. static void __exit i2c_adap_s3c_exit(void)
  1074. {
  1075. platform_driver_unregister(&s3c24xx_i2c_driver);
  1076. }
  1077. module_exit(i2c_adap_s3c_exit);
  1078. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  1079. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  1080. MODULE_LICENSE("GPL");