i2c-s3c2410.c 23 KB

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