i2c-s3c2410.c 23 KB

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