i2c-s3c2410.c 20 KB

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