i2c-s3c2410.c 20 KB

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