i2c-s3c2410.c 23 KB

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