i2c-s3c2410.c 22 KB

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