i2c-s3c2410.c 23 KB

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