i2c-s3c2410.c 32 KB

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