i2c-s3c2410.c 26 KB

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