i2c-s3c2410.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. struct s3c2410_platform_i2c *pdata;
  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. /* i2c_s3c_irq_nextbyte
  197. *
  198. * process an interrupt and work out what to do
  199. */
  200. static int i2c_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. 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. i2c_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. clk_enable(i2c->clk);
  427. for (retry = 0; retry < adap->retries; retry++) {
  428. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  429. if (ret != -EAGAIN) {
  430. clk_disable(i2c->clk);
  431. return ret;
  432. }
  433. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  434. udelay(100);
  435. }
  436. clk_disable(i2c->clk);
  437. return -EREMOTEIO;
  438. }
  439. /* declare our i2c functionality */
  440. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  441. {
  442. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  443. }
  444. /* i2c bus registration info */
  445. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  446. .master_xfer = s3c24xx_i2c_xfer,
  447. .functionality = s3c24xx_i2c_func,
  448. };
  449. /* s3c24xx_i2c_calcdivisor
  450. *
  451. * return the divisor settings for a given frequency
  452. */
  453. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  454. unsigned int *div1, unsigned int *divs)
  455. {
  456. unsigned int calc_divs = clkin / wanted;
  457. unsigned int calc_div1;
  458. if (calc_divs > (16*16))
  459. calc_div1 = 512;
  460. else
  461. calc_div1 = 16;
  462. calc_divs += calc_div1-1;
  463. calc_divs /= calc_div1;
  464. if (calc_divs == 0)
  465. calc_divs = 1;
  466. if (calc_divs > 17)
  467. calc_divs = 17;
  468. *divs = calc_divs;
  469. *div1 = calc_div1;
  470. return clkin / (calc_divs * calc_div1);
  471. }
  472. /* s3c24xx_i2c_clockrate
  473. *
  474. * work out a divisor for the user requested frequency setting,
  475. * either by the requested frequency, or scanning the acceptable
  476. * range of frequencies until something is found
  477. */
  478. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  479. {
  480. struct s3c2410_platform_i2c *pdata = i2c->pdata;
  481. unsigned long clkin = clk_get_rate(i2c->clk);
  482. unsigned int divs, div1;
  483. unsigned long target_frequency;
  484. u32 iiccon;
  485. int freq;
  486. i2c->clkrate = clkin;
  487. clkin /= 1000; /* clkin now in KHz */
  488. dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
  489. target_frequency = pdata->frequency ? pdata->frequency : 100000;
  490. target_frequency /= 1000; /* Target frequency now in KHz */
  491. freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
  492. if (freq > target_frequency) {
  493. dev_err(i2c->dev,
  494. "Unable to achieve desired frequency %luKHz." \
  495. " Lowest achievable %dKHz\n", target_frequency, freq);
  496. return -EINVAL;
  497. }
  498. *got = freq;
  499. iiccon = readl(i2c->regs + S3C2410_IICCON);
  500. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  501. iiccon |= (divs-1);
  502. if (div1 == 512)
  503. iiccon |= S3C2410_IICCON_TXDIV_512;
  504. writel(iiccon, i2c->regs + S3C2410_IICCON);
  505. if (s3c24xx_i2c_is2440(i2c)) {
  506. unsigned long sda_delay;
  507. if (pdata->sda_delay) {
  508. sda_delay = clkin * pdata->sda_delay;
  509. sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
  510. sda_delay = DIV_ROUND_UP(sda_delay, 5);
  511. if (sda_delay > 3)
  512. sda_delay = 3;
  513. sda_delay |= S3C2410_IICLC_FILTER_ON;
  514. } else
  515. sda_delay = 0;
  516. dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
  517. writel(sda_delay, i2c->regs + S3C2440_IICLC);
  518. }
  519. return 0;
  520. }
  521. #ifdef CONFIG_CPU_FREQ
  522. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  523. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  524. unsigned long val, void *data)
  525. {
  526. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  527. unsigned long flags;
  528. unsigned int got;
  529. int delta_f;
  530. int ret;
  531. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  532. /* if we're post-change and the input clock has slowed down
  533. * or at pre-change and the clock is about to speed up, then
  534. * adjust our clock rate. <0 is slow, >0 speedup.
  535. */
  536. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  537. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  538. spin_lock_irqsave(&i2c->lock, flags);
  539. ret = s3c24xx_i2c_clockrate(i2c, &got);
  540. spin_unlock_irqrestore(&i2c->lock, flags);
  541. if (ret < 0)
  542. dev_err(i2c->dev, "cannot find frequency\n");
  543. else
  544. dev_info(i2c->dev, "setting freq %d\n", got);
  545. }
  546. return 0;
  547. }
  548. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  549. {
  550. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  551. return cpufreq_register_notifier(&i2c->freq_transition,
  552. CPUFREQ_TRANSITION_NOTIFIER);
  553. }
  554. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  555. {
  556. cpufreq_unregister_notifier(&i2c->freq_transition,
  557. CPUFREQ_TRANSITION_NOTIFIER);
  558. }
  559. #else
  560. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  561. {
  562. return 0;
  563. }
  564. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  565. {
  566. }
  567. #endif
  568. /* s3c24xx_i2c_init
  569. *
  570. * initialise the controller, set the IO lines and frequency
  571. */
  572. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  573. {
  574. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  575. struct s3c2410_platform_i2c *pdata;
  576. unsigned int freq;
  577. /* get the plafrom data */
  578. pdata = i2c->pdata;
  579. /* inititalise the gpio */
  580. if (pdata->cfg_gpio)
  581. pdata->cfg_gpio(to_platform_device(i2c->dev));
  582. /* write slave address */
  583. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  584. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  585. writel(iicon, i2c->regs + S3C2410_IICCON);
  586. /* we need to work out the divisors for the clock... */
  587. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  588. writel(0, i2c->regs + S3C2410_IICCON);
  589. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  590. return -EINVAL;
  591. }
  592. /* todo - check that the i2c lines aren't being dragged anywhere */
  593. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  594. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  595. return 0;
  596. }
  597. /* s3c24xx_i2c_probe
  598. *
  599. * called by the bus driver when a suitable device is found
  600. */
  601. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  602. {
  603. struct s3c24xx_i2c *i2c;
  604. struct s3c2410_platform_i2c *pdata = NULL;
  605. struct resource *res;
  606. int ret;
  607. pdata = pdev->dev.platform_data;
  608. if (!pdata) {
  609. dev_err(&pdev->dev, "no platform data\n");
  610. return -EINVAL;
  611. }
  612. i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  613. if (!i2c) {
  614. dev_err(&pdev->dev, "no memory for state\n");
  615. return -ENOMEM;
  616. }
  617. i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  618. if (!i2c->pdata) {
  619. ret = -ENOMEM;
  620. goto err_noclk;
  621. }
  622. if (pdata)
  623. memcpy(i2c->pdata, pdata, sizeof(*pdata));
  624. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  625. i2c->adap.owner = THIS_MODULE;
  626. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  627. i2c->adap.retries = 2;
  628. i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  629. i2c->tx_setup = 50;
  630. spin_lock_init(&i2c->lock);
  631. init_waitqueue_head(&i2c->wait);
  632. /* find the clock and enable it */
  633. i2c->dev = &pdev->dev;
  634. i2c->clk = clk_get(&pdev->dev, "i2c");
  635. if (IS_ERR(i2c->clk)) {
  636. dev_err(&pdev->dev, "cannot get clock\n");
  637. ret = -ENOENT;
  638. goto err_noclk;
  639. }
  640. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  641. clk_enable(i2c->clk);
  642. /* map the registers */
  643. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  644. if (res == NULL) {
  645. dev_err(&pdev->dev, "cannot find IO resource\n");
  646. ret = -ENOENT;
  647. goto err_clk;
  648. }
  649. i2c->ioarea = request_mem_region(res->start, resource_size(res),
  650. pdev->name);
  651. if (i2c->ioarea == NULL) {
  652. dev_err(&pdev->dev, "cannot request IO\n");
  653. ret = -ENXIO;
  654. goto err_clk;
  655. }
  656. i2c->regs = ioremap(res->start, resource_size(res));
  657. if (i2c->regs == NULL) {
  658. dev_err(&pdev->dev, "cannot map IO\n");
  659. ret = -ENXIO;
  660. goto err_ioarea;
  661. }
  662. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  663. i2c->regs, i2c->ioarea, res);
  664. /* setup info block for the i2c core */
  665. i2c->adap.algo_data = i2c;
  666. i2c->adap.dev.parent = &pdev->dev;
  667. /* initialise the i2c controller */
  668. ret = s3c24xx_i2c_init(i2c);
  669. if (ret != 0)
  670. goto err_iomap;
  671. /* find the IRQ for this unit (note, this relies on the init call to
  672. * ensure no current IRQs pending
  673. */
  674. i2c->irq = ret = platform_get_irq(pdev, 0);
  675. if (ret <= 0) {
  676. dev_err(&pdev->dev, "cannot find IRQ\n");
  677. goto err_iomap;
  678. }
  679. ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
  680. dev_name(&pdev->dev), i2c);
  681. if (ret != 0) {
  682. dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  683. goto err_iomap;
  684. }
  685. ret = s3c24xx_i2c_register_cpufreq(i2c);
  686. if (ret < 0) {
  687. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  688. goto err_irq;
  689. }
  690. /* Note, previous versions of the driver used i2c_add_adapter()
  691. * to add the bus at any number. We now pass the bus number via
  692. * the platform data, so if unset it will now default to always
  693. * being bus 0.
  694. */
  695. i2c->adap.nr = i2c->pdata->bus_num;
  696. ret = i2c_add_numbered_adapter(&i2c->adap);
  697. if (ret < 0) {
  698. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  699. goto err_cpufreq;
  700. }
  701. platform_set_drvdata(pdev, i2c);
  702. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  703. clk_disable(i2c->clk);
  704. return 0;
  705. err_cpufreq:
  706. s3c24xx_i2c_deregister_cpufreq(i2c);
  707. err_irq:
  708. free_irq(i2c->irq, i2c);
  709. err_iomap:
  710. iounmap(i2c->regs);
  711. err_ioarea:
  712. release_resource(i2c->ioarea);
  713. kfree(i2c->ioarea);
  714. err_clk:
  715. clk_disable(i2c->clk);
  716. clk_put(i2c->clk);
  717. err_noclk:
  718. kfree(i2c);
  719. return ret;
  720. }
  721. /* s3c24xx_i2c_remove
  722. *
  723. * called when device is removed from the bus
  724. */
  725. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  726. {
  727. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  728. s3c24xx_i2c_deregister_cpufreq(i2c);
  729. i2c_del_adapter(&i2c->adap);
  730. free_irq(i2c->irq, i2c);
  731. clk_disable(i2c->clk);
  732. clk_put(i2c->clk);
  733. iounmap(i2c->regs);
  734. release_resource(i2c->ioarea);
  735. kfree(i2c->ioarea);
  736. kfree(i2c);
  737. return 0;
  738. }
  739. #ifdef CONFIG_PM
  740. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  741. {
  742. struct platform_device *pdev = to_platform_device(dev);
  743. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  744. i2c->suspended = 1;
  745. return 0;
  746. }
  747. static int s3c24xx_i2c_resume(struct device *dev)
  748. {
  749. struct platform_device *pdev = to_platform_device(dev);
  750. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  751. i2c->suspended = 0;
  752. clk_enable(i2c->clk);
  753. s3c24xx_i2c_init(i2c);
  754. clk_disable(i2c->clk);
  755. return 0;
  756. }
  757. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  758. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  759. .resume = s3c24xx_i2c_resume,
  760. };
  761. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  762. #else
  763. #define S3C24XX_DEV_PM_OPS NULL
  764. #endif
  765. /* device driver for platform bus bits */
  766. static struct platform_device_id s3c24xx_driver_ids[] = {
  767. {
  768. .name = "s3c2410-i2c",
  769. .driver_data = TYPE_S3C2410,
  770. }, {
  771. .name = "s3c2440-i2c",
  772. .driver_data = TYPE_S3C2440,
  773. }, { },
  774. };
  775. MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
  776. static struct platform_driver s3c24xx_i2c_driver = {
  777. .probe = s3c24xx_i2c_probe,
  778. .remove = s3c24xx_i2c_remove,
  779. .id_table = s3c24xx_driver_ids,
  780. .driver = {
  781. .owner = THIS_MODULE,
  782. .name = "s3c-i2c",
  783. .pm = S3C24XX_DEV_PM_OPS,
  784. },
  785. };
  786. static int __init i2c_adap_s3c_init(void)
  787. {
  788. return platform_driver_register(&s3c24xx_i2c_driver);
  789. }
  790. subsys_initcall(i2c_adap_s3c_init);
  791. static void __exit i2c_adap_s3c_exit(void)
  792. {
  793. platform_driver_unregister(&s3c24xx_i2c_driver);
  794. }
  795. module_exit(i2c_adap_s3c_exit);
  796. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  797. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  798. MODULE_LICENSE("GPL");