i2c-s3c2410.c 26 KB

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