i2c-s3c2410.c 29 KB

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