i2c-s3c2410.c 25 KB

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