i2c-s3c2410.c 22 KB

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