i2c-s3c2410.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 wether 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 wether 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. /* ensure the stop has been through the bus */
  444. dev_dbg(i2c->dev, "waiting for bus idle\n");
  445. start = now = ktime_get();
  446. /*
  447. * Most of the time, the bus is already idle within a few usec of the
  448. * end of a transaction. However, really slow i2c devices can stretch
  449. * the clock, delaying STOP generation.
  450. *
  451. * As a compromise between idle detection latency for the normal, fast
  452. * case, and system load in the slow device case, use an exponential
  453. * back off in the polling loop, up to 1/10th of the total timeout,
  454. * then continue to poll at a constant rate up to the timeout.
  455. */
  456. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  457. delay = 1;
  458. while ((iicstat & S3C2410_IICSTAT_START) &&
  459. ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
  460. usleep_range(delay, 2 * delay);
  461. if (delay < S3C2410_IDLE_TIMEOUT / 10)
  462. delay <<= 1;
  463. now = ktime_get();
  464. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  465. }
  466. if (iicstat & S3C2410_IICSTAT_START)
  467. dev_warn(i2c->dev, "timeout waiting for bus idle\n");
  468. }
  469. /* s3c24xx_i2c_doxfer
  470. *
  471. * this starts an i2c transfer
  472. */
  473. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
  474. struct i2c_msg *msgs, int num)
  475. {
  476. unsigned long timeout;
  477. int ret;
  478. if (i2c->suspended)
  479. return -EIO;
  480. ret = s3c24xx_i2c_set_master(i2c);
  481. if (ret != 0) {
  482. dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  483. ret = -EAGAIN;
  484. goto out;
  485. }
  486. i2c->msg = msgs;
  487. i2c->msg_num = num;
  488. i2c->msg_ptr = 0;
  489. i2c->msg_idx = 0;
  490. i2c->state = STATE_START;
  491. s3c24xx_i2c_enable_irq(i2c);
  492. s3c24xx_i2c_message_start(i2c, msgs);
  493. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  494. ret = i2c->msg_idx;
  495. /* having these next two as dev_err() makes life very
  496. * noisy when doing an i2cdetect */
  497. if (timeout == 0)
  498. dev_dbg(i2c->dev, "timeout\n");
  499. else if (ret != num)
  500. dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  501. /* For QUIRK_HDMIPHY, bus is already disabled */
  502. if (i2c->quirks & QUIRK_HDMIPHY)
  503. goto out;
  504. s3c24xx_i2c_wait_idle(i2c);
  505. out:
  506. return ret;
  507. }
  508. /* s3c24xx_i2c_xfer
  509. *
  510. * first port of call from the i2c bus code when an message needs
  511. * transferring across the i2c bus.
  512. */
  513. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  514. struct i2c_msg *msgs, int num)
  515. {
  516. struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  517. int retry;
  518. int ret;
  519. pm_runtime_get_sync(&adap->dev);
  520. clk_prepare_enable(i2c->clk);
  521. for (retry = 0; retry < adap->retries; retry++) {
  522. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  523. if (ret != -EAGAIN) {
  524. clk_disable_unprepare(i2c->clk);
  525. pm_runtime_put(&adap->dev);
  526. return ret;
  527. }
  528. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  529. udelay(100);
  530. }
  531. clk_disable_unprepare(i2c->clk);
  532. pm_runtime_put(&adap->dev);
  533. return -EREMOTEIO;
  534. }
  535. /* declare our i2c functionality */
  536. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  537. {
  538. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
  539. I2C_FUNC_PROTOCOL_MANGLING;
  540. }
  541. /* i2c bus registration info */
  542. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  543. .master_xfer = s3c24xx_i2c_xfer,
  544. .functionality = s3c24xx_i2c_func,
  545. };
  546. /* s3c24xx_i2c_calcdivisor
  547. *
  548. * return the divisor settings for a given frequency
  549. */
  550. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  551. unsigned int *div1, unsigned int *divs)
  552. {
  553. unsigned int calc_divs = clkin / wanted;
  554. unsigned int calc_div1;
  555. if (calc_divs > (16*16))
  556. calc_div1 = 512;
  557. else
  558. calc_div1 = 16;
  559. calc_divs += calc_div1-1;
  560. calc_divs /= calc_div1;
  561. if (calc_divs == 0)
  562. calc_divs = 1;
  563. if (calc_divs > 17)
  564. calc_divs = 17;
  565. *divs = calc_divs;
  566. *div1 = calc_div1;
  567. return clkin / (calc_divs * calc_div1);
  568. }
  569. /* s3c24xx_i2c_clockrate
  570. *
  571. * work out a divisor for the user requested frequency setting,
  572. * either by the requested frequency, or scanning the acceptable
  573. * range of frequencies until something is found
  574. */
  575. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  576. {
  577. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  578. unsigned long clkin = clk_get_rate(i2c->clk);
  579. unsigned int divs, div1;
  580. unsigned long target_frequency;
  581. u32 iiccon;
  582. int freq;
  583. i2c->clkrate = clkin;
  584. clkin /= 1000; /* clkin now in KHz */
  585. dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
  586. target_frequency = pdata->frequency ? pdata->frequency : 100000;
  587. target_frequency /= 1000; /* Target frequency now in KHz */
  588. freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
  589. if (freq > target_frequency) {
  590. dev_err(i2c->dev,
  591. "Unable to achieve desired frequency %luKHz." \
  592. " Lowest achievable %dKHz\n", target_frequency, freq);
  593. return -EINVAL;
  594. }
  595. *got = freq;
  596. iiccon = readl(i2c->regs + S3C2410_IICCON);
  597. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  598. iiccon |= (divs-1);
  599. if (div1 == 512)
  600. iiccon |= S3C2410_IICCON_TXDIV_512;
  601. writel(iiccon, i2c->regs + S3C2410_IICCON);
  602. if (i2c->quirks & QUIRK_S3C2440) {
  603. unsigned long sda_delay;
  604. if (pdata->sda_delay) {
  605. sda_delay = clkin * pdata->sda_delay;
  606. sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
  607. sda_delay = DIV_ROUND_UP(sda_delay, 5);
  608. if (sda_delay > 3)
  609. sda_delay = 3;
  610. sda_delay |= S3C2410_IICLC_FILTER_ON;
  611. } else
  612. sda_delay = 0;
  613. dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
  614. writel(sda_delay, i2c->regs + S3C2440_IICLC);
  615. }
  616. return 0;
  617. }
  618. #ifdef CONFIG_CPU_FREQ
  619. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  620. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  621. unsigned long val, void *data)
  622. {
  623. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  624. unsigned int got;
  625. int delta_f;
  626. int ret;
  627. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  628. /* if we're post-change and the input clock has slowed down
  629. * or at pre-change and the clock is about to speed up, then
  630. * adjust our clock rate. <0 is slow, >0 speedup.
  631. */
  632. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  633. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  634. i2c_lock_adapter(&i2c->adap);
  635. ret = s3c24xx_i2c_clockrate(i2c, &got);
  636. i2c_unlock_adapter(&i2c->adap);
  637. if (ret < 0)
  638. dev_err(i2c->dev, "cannot find frequency\n");
  639. else
  640. dev_info(i2c->dev, "setting freq %d\n", got);
  641. }
  642. return 0;
  643. }
  644. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  645. {
  646. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  647. return cpufreq_register_notifier(&i2c->freq_transition,
  648. CPUFREQ_TRANSITION_NOTIFIER);
  649. }
  650. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  651. {
  652. cpufreq_unregister_notifier(&i2c->freq_transition,
  653. CPUFREQ_TRANSITION_NOTIFIER);
  654. }
  655. #else
  656. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  657. {
  658. return 0;
  659. }
  660. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  661. {
  662. }
  663. #endif
  664. #ifdef CONFIG_OF
  665. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  666. {
  667. int idx, gpio, ret;
  668. if (i2c->quirks & QUIRK_NO_GPIO)
  669. return 0;
  670. for (idx = 0; idx < 2; idx++) {
  671. gpio = of_get_gpio(i2c->dev->of_node, idx);
  672. if (!gpio_is_valid(gpio)) {
  673. dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
  674. goto free_gpio;
  675. }
  676. i2c->gpios[idx] = gpio;
  677. ret = gpio_request(gpio, "i2c-bus");
  678. if (ret) {
  679. dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
  680. goto free_gpio;
  681. }
  682. }
  683. return 0;
  684. free_gpio:
  685. while (--idx >= 0)
  686. gpio_free(i2c->gpios[idx]);
  687. return -EINVAL;
  688. }
  689. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  690. {
  691. unsigned int idx;
  692. if (i2c->quirks & QUIRK_NO_GPIO)
  693. return;
  694. for (idx = 0; idx < 2; idx++)
  695. gpio_free(i2c->gpios[idx]);
  696. }
  697. #else
  698. static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
  699. {
  700. return 0;
  701. }
  702. static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
  703. {
  704. }
  705. #endif
  706. /* s3c24xx_i2c_init
  707. *
  708. * initialise the controller, set the IO lines and frequency
  709. */
  710. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  711. {
  712. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  713. struct s3c2410_platform_i2c *pdata;
  714. unsigned int freq;
  715. /* get the plafrom data */
  716. pdata = i2c->pdata;
  717. /* write slave address */
  718. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  719. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  720. writel(iicon, i2c->regs + S3C2410_IICCON);
  721. /* we need to work out the divisors for the clock... */
  722. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  723. writel(0, i2c->regs + S3C2410_IICCON);
  724. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  725. return -EINVAL;
  726. }
  727. /* todo - check that the i2c lines aren't being dragged anywhere */
  728. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  729. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  730. return 0;
  731. }
  732. #ifdef CONFIG_OF
  733. /* s3c24xx_i2c_parse_dt
  734. *
  735. * Parse the device tree node and retreive the platform data.
  736. */
  737. static void
  738. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  739. {
  740. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  741. if (!np)
  742. return;
  743. pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
  744. of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
  745. of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
  746. of_property_read_u32(np, "samsung,i2c-max-bus-freq",
  747. (u32 *)&pdata->frequency);
  748. }
  749. #else
  750. static void
  751. s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
  752. {
  753. return;
  754. }
  755. #endif
  756. /* s3c24xx_i2c_probe
  757. *
  758. * called by the bus driver when a suitable device is found
  759. */
  760. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  761. {
  762. struct s3c24xx_i2c *i2c;
  763. struct s3c2410_platform_i2c *pdata = NULL;
  764. struct resource *res;
  765. int ret;
  766. if (!pdev->dev.of_node) {
  767. pdata = pdev->dev.platform_data;
  768. if (!pdata) {
  769. dev_err(&pdev->dev, "no platform data\n");
  770. return -EINVAL;
  771. }
  772. }
  773. i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  774. if (!i2c) {
  775. dev_err(&pdev->dev, "no memory for state\n");
  776. return -ENOMEM;
  777. }
  778. i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  779. if (!i2c->pdata) {
  780. ret = -ENOMEM;
  781. goto err_noclk;
  782. }
  783. i2c->quirks = s3c24xx_get_device_quirks(pdev);
  784. if (pdata)
  785. memcpy(i2c->pdata, pdata, sizeof(*pdata));
  786. else
  787. s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
  788. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  789. i2c->adap.owner = THIS_MODULE;
  790. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  791. i2c->adap.retries = 2;
  792. i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  793. i2c->tx_setup = 50;
  794. init_waitqueue_head(&i2c->wait);
  795. /* find the clock and enable it */
  796. i2c->dev = &pdev->dev;
  797. i2c->clk = clk_get(&pdev->dev, "i2c");
  798. if (IS_ERR(i2c->clk)) {
  799. dev_err(&pdev->dev, "cannot get clock\n");
  800. ret = -ENOENT;
  801. goto err_noclk;
  802. }
  803. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  804. clk_prepare_enable(i2c->clk);
  805. /* map the registers */
  806. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  807. if (res == NULL) {
  808. dev_err(&pdev->dev, "cannot find IO resource\n");
  809. ret = -ENOENT;
  810. goto err_clk;
  811. }
  812. i2c->regs = devm_request_and_ioremap(&pdev->dev, res);
  813. if (i2c->regs == NULL) {
  814. dev_err(&pdev->dev, "cannot map IO\n");
  815. ret = -ENXIO;
  816. goto err_clk;
  817. }
  818. dev_dbg(&pdev->dev, "registers %p (%p)\n",
  819. i2c->regs, res);
  820. /* setup info block for the i2c core */
  821. i2c->adap.algo_data = i2c;
  822. i2c->adap.dev.parent = &pdev->dev;
  823. i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
  824. /* inititalise the i2c gpio lines */
  825. if (i2c->pdata->cfg_gpio) {
  826. i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
  827. } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) {
  828. ret = -EINVAL;
  829. goto err_clk;
  830. }
  831. /* initialise the i2c controller */
  832. ret = s3c24xx_i2c_init(i2c);
  833. if (ret != 0)
  834. goto err_clk;
  835. /* find the IRQ for this unit (note, this relies on the init call to
  836. * ensure no current IRQs pending
  837. */
  838. i2c->irq = ret = platform_get_irq(pdev, 0);
  839. if (ret <= 0) {
  840. dev_err(&pdev->dev, "cannot find IRQ\n");
  841. goto err_clk;
  842. }
  843. ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
  844. dev_name(&pdev->dev), i2c);
  845. if (ret != 0) {
  846. dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  847. goto err_clk;
  848. }
  849. ret = s3c24xx_i2c_register_cpufreq(i2c);
  850. if (ret < 0) {
  851. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  852. goto err_irq;
  853. }
  854. /* Note, previous versions of the driver used i2c_add_adapter()
  855. * to add the bus at any number. We now pass the bus number via
  856. * the platform data, so if unset it will now default to always
  857. * being bus 0.
  858. */
  859. i2c->adap.nr = i2c->pdata->bus_num;
  860. i2c->adap.dev.of_node = pdev->dev.of_node;
  861. ret = i2c_add_numbered_adapter(&i2c->adap);
  862. if (ret < 0) {
  863. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  864. goto err_cpufreq;
  865. }
  866. of_i2c_register_devices(&i2c->adap);
  867. platform_set_drvdata(pdev, i2c);
  868. pm_runtime_enable(&pdev->dev);
  869. pm_runtime_enable(&i2c->adap.dev);
  870. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  871. clk_disable_unprepare(i2c->clk);
  872. return 0;
  873. err_cpufreq:
  874. s3c24xx_i2c_deregister_cpufreq(i2c);
  875. err_irq:
  876. free_irq(i2c->irq, i2c);
  877. err_clk:
  878. clk_disable_unprepare(i2c->clk);
  879. clk_put(i2c->clk);
  880. err_noclk:
  881. return ret;
  882. }
  883. /* s3c24xx_i2c_remove
  884. *
  885. * called when device is removed from the bus
  886. */
  887. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  888. {
  889. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  890. pm_runtime_disable(&i2c->adap.dev);
  891. pm_runtime_disable(&pdev->dev);
  892. s3c24xx_i2c_deregister_cpufreq(i2c);
  893. i2c_del_adapter(&i2c->adap);
  894. free_irq(i2c->irq, i2c);
  895. clk_disable_unprepare(i2c->clk);
  896. clk_put(i2c->clk);
  897. if (pdev->dev.of_node && IS_ERR(i2c->pctrl))
  898. s3c24xx_i2c_dt_gpio_free(i2c);
  899. return 0;
  900. }
  901. #ifdef CONFIG_PM_SLEEP
  902. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  903. {
  904. struct platform_device *pdev = to_platform_device(dev);
  905. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  906. i2c->suspended = 1;
  907. return 0;
  908. }
  909. static int s3c24xx_i2c_resume(struct device *dev)
  910. {
  911. struct platform_device *pdev = to_platform_device(dev);
  912. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  913. i2c->suspended = 0;
  914. clk_prepare_enable(i2c->clk);
  915. s3c24xx_i2c_init(i2c);
  916. clk_disable_unprepare(i2c->clk);
  917. return 0;
  918. }
  919. #endif
  920. #ifdef CONFIG_PM
  921. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  922. #ifdef CONFIG_PM_SLEEP
  923. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  924. .resume = s3c24xx_i2c_resume,
  925. #endif
  926. };
  927. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  928. #else
  929. #define S3C24XX_DEV_PM_OPS NULL
  930. #endif
  931. /* device driver for platform bus bits */
  932. static struct platform_driver s3c24xx_i2c_driver = {
  933. .probe = s3c24xx_i2c_probe,
  934. .remove = s3c24xx_i2c_remove,
  935. .id_table = s3c24xx_driver_ids,
  936. .driver = {
  937. .owner = THIS_MODULE,
  938. .name = "s3c-i2c",
  939. .pm = S3C24XX_DEV_PM_OPS,
  940. .of_match_table = of_match_ptr(s3c24xx_i2c_match),
  941. },
  942. };
  943. static int __init i2c_adap_s3c_init(void)
  944. {
  945. return platform_driver_register(&s3c24xx_i2c_driver);
  946. }
  947. subsys_initcall(i2c_adap_s3c_init);
  948. static void __exit i2c_adap_s3c_exit(void)
  949. {
  950. platform_driver_unregister(&s3c24xx_i2c_driver);
  951. }
  952. module_exit(i2c_adap_s3c_exit);
  953. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  954. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  955. MODULE_LICENSE("GPL");