i2c-s3c2410.c 22 KB

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