i2c-s3c2410.c 25 KB

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