i2c-s3c2410.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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/arch/regs-iic.h>
  39. #include <asm/arch/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", __FUNCTION__);
  216. goto out;
  217. break;
  218. case STATE_STOP:
  219. dev_err(i2c->dev, "%s: called in STATE_STOP\n", __FUNCTION__);
  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, -EREMOTEIO);
  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. * (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. retry_write:
  252. if (!is_msgend(i2c)) {
  253. byte = i2c->msg->buf[i2c->msg_ptr++];
  254. writeb(byte, i2c->regs + S3C2410_IICDS);
  255. /* delay after writing the byte to allow the
  256. * data setup time on the bus, as writing the
  257. * data to the register causes the first bit
  258. * to appear on SDA, and SCL will change as
  259. * soon as the interrupt is acknowledged */
  260. ndelay(i2c->tx_setup);
  261. } else if (!is_lastmsg(i2c)) {
  262. /* we need to go to the next i2c message */
  263. dev_dbg(i2c->dev, "WRITE: Next Message\n");
  264. i2c->msg_ptr = 0;
  265. i2c->msg_idx ++;
  266. i2c->msg++;
  267. /* check to see if we need to do another message */
  268. if (i2c->msg->flags & I2C_M_NOSTART) {
  269. if (i2c->msg->flags & I2C_M_RD) {
  270. /* cannot do this, the controller
  271. * forces us to send a new START
  272. * when we change direction */
  273. s3c24xx_i2c_stop(i2c, -EINVAL);
  274. }
  275. goto retry_write;
  276. } else {
  277. /* send the new start */
  278. s3c24xx_i2c_message_start(i2c, i2c->msg);
  279. i2c->state = STATE_START;
  280. }
  281. } else {
  282. /* send stop */
  283. s3c24xx_i2c_stop(i2c, 0);
  284. }
  285. break;
  286. case STATE_READ:
  287. /* we have a byte of data in the data register, do
  288. * something with it, and then work out wether we are
  289. * going to do any more read/write
  290. */
  291. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK) &&
  292. !(is_msglast(i2c) && is_lastmsg(i2c))) {
  293. if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  294. dev_dbg(i2c->dev, "READ: No Ack\n");
  295. s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  296. goto out_ack;
  297. }
  298. }
  299. byte = readb(i2c->regs + S3C2410_IICDS);
  300. i2c->msg->buf[i2c->msg_ptr++] = byte;
  301. prepare_read:
  302. if (is_msglast(i2c)) {
  303. /* last byte of buffer */
  304. if (is_lastmsg(i2c))
  305. s3c24xx_i2c_disable_ack(i2c);
  306. } else if (is_msgend(i2c)) {
  307. /* ok, we've read the entire buffer, see if there
  308. * is anything else we need to do */
  309. if (is_lastmsg(i2c)) {
  310. /* last message, send stop and complete */
  311. dev_dbg(i2c->dev, "READ: Send Stop\n");
  312. s3c24xx_i2c_stop(i2c, 0);
  313. } else {
  314. /* go to the next transfer */
  315. dev_dbg(i2c->dev, "READ: Next Transfer\n");
  316. i2c->msg_ptr = 0;
  317. i2c->msg_idx++;
  318. i2c->msg++;
  319. }
  320. }
  321. break;
  322. }
  323. /* acknowlegde the IRQ and get back on with the work */
  324. out_ack:
  325. tmp = readl(i2c->regs + S3C2410_IICCON);
  326. tmp &= ~S3C2410_IICCON_IRQPEND;
  327. writel(tmp, i2c->regs + S3C2410_IICCON);
  328. out:
  329. return ret;
  330. }
  331. /* s3c24xx_i2c_irq
  332. *
  333. * top level IRQ servicing routine
  334. */
  335. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
  336. {
  337. struct s3c24xx_i2c *i2c = dev_id;
  338. unsigned long status;
  339. unsigned long tmp;
  340. status = readl(i2c->regs + S3C2410_IICSTAT);
  341. if (status & S3C2410_IICSTAT_ARBITR) {
  342. // deal with arbitration loss
  343. dev_err(i2c->dev, "deal with arbitration loss\n");
  344. }
  345. if (i2c->state == STATE_IDLE) {
  346. dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
  347. tmp = readl(i2c->regs + S3C2410_IICCON);
  348. tmp &= ~S3C2410_IICCON_IRQPEND;
  349. writel(tmp, i2c->regs + S3C2410_IICCON);
  350. goto out;
  351. }
  352. /* pretty much this leaves us with the fact that we've
  353. * transmitted or received whatever byte we last sent */
  354. i2s_s3c_irq_nextbyte(i2c, status);
  355. out:
  356. return IRQ_HANDLED;
  357. }
  358. /* s3c24xx_i2c_set_master
  359. *
  360. * get the i2c bus for a master transaction
  361. */
  362. static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
  363. {
  364. unsigned long iicstat;
  365. int timeout = 400;
  366. while (timeout-- > 0) {
  367. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  368. if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
  369. return 0;
  370. msleep(1);
  371. }
  372. dev_dbg(i2c->dev, "timeout: GPEDAT is %08x\n",
  373. __raw_readl(S3C2410_GPEDAT));
  374. return -ETIMEDOUT;
  375. }
  376. /* s3c24xx_i2c_doxfer
  377. *
  378. * this starts an i2c transfer
  379. */
  380. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num)
  381. {
  382. unsigned long timeout;
  383. int ret;
  384. ret = s3c24xx_i2c_set_master(i2c);
  385. if (ret != 0) {
  386. dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  387. ret = -EAGAIN;
  388. goto out;
  389. }
  390. spin_lock_irq(&i2c->lock);
  391. i2c->msg = msgs;
  392. i2c->msg_num = num;
  393. i2c->msg_ptr = 0;
  394. i2c->msg_idx = 0;
  395. i2c->state = STATE_START;
  396. s3c24xx_i2c_enable_irq(i2c);
  397. s3c24xx_i2c_message_start(i2c, msgs);
  398. spin_unlock_irq(&i2c->lock);
  399. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  400. ret = i2c->msg_idx;
  401. /* having these next two as dev_err() makes life very
  402. * noisy when doing an i2cdetect */
  403. if (timeout == 0)
  404. dev_dbg(i2c->dev, "timeout\n");
  405. else if (ret != num)
  406. dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  407. /* ensure the stop has been through the bus */
  408. msleep(1);
  409. out:
  410. return ret;
  411. }
  412. /* s3c24xx_i2c_xfer
  413. *
  414. * first port of call from the i2c bus code when an message needs
  415. * transferring across the i2c bus.
  416. */
  417. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  418. struct i2c_msg *msgs, int num)
  419. {
  420. struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  421. int retry;
  422. int ret;
  423. for (retry = 0; retry < adap->retries; retry++) {
  424. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  425. if (ret != -EAGAIN)
  426. return ret;
  427. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  428. udelay(100);
  429. }
  430. return -EREMOTEIO;
  431. }
  432. /* declare our i2c functionality */
  433. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  434. {
  435. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  436. }
  437. /* i2c bus registration info */
  438. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  439. .master_xfer = s3c24xx_i2c_xfer,
  440. .functionality = s3c24xx_i2c_func,
  441. };
  442. static struct s3c24xx_i2c s3c24xx_i2c = {
  443. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),
  444. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
  445. .tx_setup = 50,
  446. .adap = {
  447. .name = "s3c2410-i2c",
  448. .owner = THIS_MODULE,
  449. .algo = &s3c24xx_i2c_algorithm,
  450. .retries = 2,
  451. .class = I2C_CLASS_HWMON,
  452. },
  453. };
  454. /* s3c24xx_i2c_calcdivisor
  455. *
  456. * return the divisor settings for a given frequency
  457. */
  458. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  459. unsigned int *div1, unsigned int *divs)
  460. {
  461. unsigned int calc_divs = clkin / wanted;
  462. unsigned int calc_div1;
  463. if (calc_divs > (16*16))
  464. calc_div1 = 512;
  465. else
  466. calc_div1 = 16;
  467. calc_divs += calc_div1-1;
  468. calc_divs /= calc_div1;
  469. if (calc_divs == 0)
  470. calc_divs = 1;
  471. if (calc_divs > 17)
  472. calc_divs = 17;
  473. *divs = calc_divs;
  474. *div1 = calc_div1;
  475. return clkin / (calc_divs * calc_div1);
  476. }
  477. /* freq_acceptable
  478. *
  479. * test wether a frequency is within the acceptable range of error
  480. */
  481. static inline int freq_acceptable(unsigned int freq, unsigned int wanted)
  482. {
  483. int diff = freq - wanted;
  484. return (diff >= -2 && diff <= 2);
  485. }
  486. /* s3c24xx_i2c_getdivisor
  487. *
  488. * work out a divisor for the user requested frequency setting,
  489. * either by the requested frequency, or scanning the acceptable
  490. * range of frequencies until something is found
  491. */
  492. static int s3c24xx_i2c_getdivisor(struct s3c24xx_i2c *i2c,
  493. struct s3c2410_platform_i2c *pdata,
  494. unsigned long *iicon,
  495. unsigned int *got)
  496. {
  497. unsigned long clkin = clk_get_rate(i2c->clk);
  498. unsigned int divs, div1;
  499. int freq;
  500. int start, end;
  501. clkin /= 1000; /* clkin now in KHz */
  502. dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n",
  503. pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);
  504. if (pdata->bus_freq != 0) {
  505. freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
  506. &div1, &divs);
  507. if (freq_acceptable(freq, pdata->bus_freq/1000))
  508. goto found;
  509. }
  510. /* ok, we may have to search for something suitable... */
  511. start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;
  512. end = pdata->min_freq;
  513. start /= 1000;
  514. end /= 1000;
  515. /* search loop... */
  516. for (; start > end; start--) {
  517. freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
  518. if (freq_acceptable(freq, start))
  519. goto found;
  520. }
  521. /* cannot find frequency spec */
  522. return -EINVAL;
  523. found:
  524. *got = freq;
  525. *iicon |= (divs-1);
  526. *iicon |= (div1 == 512) ? S3C2410_IICCON_TXDIV_512 : 0;
  527. return 0;
  528. }
  529. /* s3c24xx_i2c_init
  530. *
  531. * initialise the controller, set the IO lines and frequency
  532. */
  533. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  534. {
  535. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  536. struct s3c2410_platform_i2c *pdata;
  537. unsigned int freq;
  538. /* get the plafrom data */
  539. pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);
  540. /* inititalise the gpio */
  541. s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA);
  542. s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL);
  543. /* write slave address */
  544. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  545. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  546. /* we need to work out the divisors for the clock... */
  547. if (s3c24xx_i2c_getdivisor(i2c, pdata, &iicon, &freq) != 0) {
  548. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  549. return -EINVAL;
  550. }
  551. /* todo - check that the i2c lines aren't being dragged anywhere */
  552. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  553. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  554. writel(iicon, i2c->regs + S3C2410_IICCON);
  555. /* check for s3c2440 i2c controller */
  556. if (s3c24xx_i2c_is2440(i2c)) {
  557. dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay);
  558. writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
  559. }
  560. return 0;
  561. }
  562. /* s3c24xx_i2c_probe
  563. *
  564. * called by the bus driver when a suitable device is found
  565. */
  566. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  567. {
  568. struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
  569. struct resource *res;
  570. int ret;
  571. /* find the clock and enable it */
  572. i2c->dev = &pdev->dev;
  573. i2c->clk = clk_get(&pdev->dev, "i2c");
  574. if (IS_ERR(i2c->clk)) {
  575. dev_err(&pdev->dev, "cannot get clock\n");
  576. ret = -ENOENT;
  577. goto err_noclk;
  578. }
  579. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  580. clk_enable(i2c->clk);
  581. /* map the registers */
  582. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. if (res == NULL) {
  584. dev_err(&pdev->dev, "cannot find IO resource\n");
  585. ret = -ENOENT;
  586. goto err_clk;
  587. }
  588. i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
  589. pdev->name);
  590. if (i2c->ioarea == NULL) {
  591. dev_err(&pdev->dev, "cannot request IO\n");
  592. ret = -ENXIO;
  593. goto err_clk;
  594. }
  595. i2c->regs = ioremap(res->start, (res->end-res->start)+1);
  596. if (i2c->regs == NULL) {
  597. dev_err(&pdev->dev, "cannot map IO\n");
  598. ret = -ENXIO;
  599. goto err_ioarea;
  600. }
  601. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", i2c->regs, i2c->ioarea, res);
  602. /* setup info block for the i2c core */
  603. i2c->adap.algo_data = i2c;
  604. i2c->adap.dev.parent = &pdev->dev;
  605. /* initialise the i2c controller */
  606. ret = s3c24xx_i2c_init(i2c);
  607. if (ret != 0)
  608. goto err_iomap;
  609. /* find the IRQ for this unit (note, this relies on the init call to
  610. * ensure no current IRQs pending
  611. */
  612. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  613. if (res == NULL) {
  614. dev_err(&pdev->dev, "cannot find IRQ\n");
  615. ret = -ENOENT;
  616. goto err_iomap;
  617. }
  618. ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,
  619. pdev->name, i2c);
  620. if (ret != 0) {
  621. dev_err(&pdev->dev, "cannot claim IRQ\n");
  622. goto err_iomap;
  623. }
  624. i2c->irq = res;
  625. dev_dbg(&pdev->dev, "irq resource %p (%ld)\n", res, 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");