i2c-s3c2410.c 30 KB

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