i2c-s3c2410.c 23 KB

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