i2c-s3c2410.c 26 KB

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