i2c-s3c2410.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 <asm/irq.h>
  37. #include <asm/io.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 timeout;
  373. int ret;
  374. if (i2c->suspended)
  375. return -EIO;
  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. /* s3c24xx_i2c_calcdivisor
  435. *
  436. * return the divisor settings for a given frequency
  437. */
  438. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  439. unsigned int *div1, unsigned int *divs)
  440. {
  441. unsigned int calc_divs = clkin / wanted;
  442. unsigned int calc_div1;
  443. if (calc_divs > (16*16))
  444. calc_div1 = 512;
  445. else
  446. calc_div1 = 16;
  447. calc_divs += calc_div1-1;
  448. calc_divs /= calc_div1;
  449. if (calc_divs == 0)
  450. calc_divs = 1;
  451. if (calc_divs > 17)
  452. calc_divs = 17;
  453. *divs = calc_divs;
  454. *div1 = calc_div1;
  455. return clkin / (calc_divs * calc_div1);
  456. }
  457. /* s3c24xx_i2c_clockrate
  458. *
  459. * work out a divisor for the user requested frequency setting,
  460. * either by the requested frequency, or scanning the acceptable
  461. * range of frequencies until something is found
  462. */
  463. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  464. {
  465. struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data;
  466. unsigned long clkin = clk_get_rate(i2c->clk);
  467. unsigned int divs, div1;
  468. unsigned long target_frequency;
  469. u32 iiccon;
  470. int freq;
  471. i2c->clkrate = clkin;
  472. clkin /= 1000; /* clkin now in KHz */
  473. dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
  474. target_frequency = pdata->frequency ? pdata->frequency : 100000;
  475. target_frequency /= 1000; /* Target frequency now in KHz */
  476. freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
  477. if (freq > target_frequency) {
  478. dev_err(i2c->dev,
  479. "Unable to achieve desired frequency %luKHz." \
  480. " Lowest achievable %dKHz\n", target_frequency, freq);
  481. return -EINVAL;
  482. }
  483. *got = freq;
  484. iiccon = readl(i2c->regs + S3C2410_IICCON);
  485. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  486. iiccon |= (divs-1);
  487. if (div1 == 512)
  488. iiccon |= S3C2410_IICCON_TXDIV_512;
  489. writel(iiccon, i2c->regs + S3C2410_IICCON);
  490. if (s3c24xx_i2c_is2440(i2c)) {
  491. unsigned long sda_delay;
  492. if (pdata->sda_delay) {
  493. sda_delay = (freq / 1000) * pdata->sda_delay;
  494. sda_delay /= 1000000;
  495. sda_delay = DIV_ROUND_UP(sda_delay, 5);
  496. if (sda_delay > 3)
  497. sda_delay = 3;
  498. sda_delay |= S3C2410_IICLC_FILTER_ON;
  499. } else
  500. sda_delay = 0;
  501. dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
  502. writel(sda_delay, i2c->regs + S3C2440_IICLC);
  503. }
  504. return 0;
  505. }
  506. #ifdef CONFIG_CPU_FREQ
  507. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  508. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  509. unsigned long val, void *data)
  510. {
  511. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  512. unsigned long flags;
  513. unsigned int got;
  514. int delta_f;
  515. int ret;
  516. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  517. /* if we're post-change and the input clock has slowed down
  518. * or at pre-change and the clock is about to speed up, then
  519. * adjust our clock rate. <0 is slow, >0 speedup.
  520. */
  521. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  522. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  523. spin_lock_irqsave(&i2c->lock, flags);
  524. ret = s3c24xx_i2c_clockrate(i2c, &got);
  525. spin_unlock_irqrestore(&i2c->lock, flags);
  526. if (ret < 0)
  527. dev_err(i2c->dev, "cannot find frequency\n");
  528. else
  529. dev_info(i2c->dev, "setting freq %d\n", got);
  530. }
  531. return 0;
  532. }
  533. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  534. {
  535. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  536. return cpufreq_register_notifier(&i2c->freq_transition,
  537. CPUFREQ_TRANSITION_NOTIFIER);
  538. }
  539. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  540. {
  541. cpufreq_unregister_notifier(&i2c->freq_transition,
  542. CPUFREQ_TRANSITION_NOTIFIER);
  543. }
  544. #else
  545. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  546. {
  547. return 0;
  548. }
  549. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  550. {
  551. }
  552. #endif
  553. /* s3c24xx_i2c_init
  554. *
  555. * initialise the controller, set the IO lines and frequency
  556. */
  557. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  558. {
  559. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  560. struct s3c2410_platform_i2c *pdata;
  561. unsigned int freq;
  562. /* get the plafrom data */
  563. pdata = i2c->dev->platform_data;
  564. /* inititalise the gpio */
  565. if (pdata->cfg_gpio)
  566. pdata->cfg_gpio(to_platform_device(i2c->dev));
  567. /* write slave address */
  568. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  569. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  570. writel(iicon, i2c->regs + S3C2410_IICCON);
  571. /* we need to work out the divisors for the clock... */
  572. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  573. writel(0, i2c->regs + S3C2410_IICCON);
  574. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  575. return -EINVAL;
  576. }
  577. /* todo - check that the i2c lines aren't being dragged anywhere */
  578. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  579. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  580. return 0;
  581. }
  582. /* s3c24xx_i2c_probe
  583. *
  584. * called by the bus driver when a suitable device is found
  585. */
  586. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  587. {
  588. struct s3c24xx_i2c *i2c;
  589. struct s3c2410_platform_i2c *pdata;
  590. struct resource *res;
  591. int ret;
  592. pdata = pdev->dev.platform_data;
  593. if (!pdata) {
  594. dev_err(&pdev->dev, "no platform data\n");
  595. return -EINVAL;
  596. }
  597. i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
  598. if (!i2c) {
  599. dev_err(&pdev->dev, "no memory for state\n");
  600. return -ENOMEM;
  601. }
  602. strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
  603. i2c->adap.owner = THIS_MODULE;
  604. i2c->adap.algo = &s3c24xx_i2c_algorithm;
  605. i2c->adap.retries = 2;
  606. i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  607. i2c->tx_setup = 50;
  608. spin_lock_init(&i2c->lock);
  609. init_waitqueue_head(&i2c->wait);
  610. /* find the clock and enable it */
  611. i2c->dev = &pdev->dev;
  612. i2c->clk = clk_get(&pdev->dev, "i2c");
  613. if (IS_ERR(i2c->clk)) {
  614. dev_err(&pdev->dev, "cannot get clock\n");
  615. ret = -ENOENT;
  616. goto err_noclk;
  617. }
  618. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  619. clk_enable(i2c->clk);
  620. /* map the registers */
  621. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  622. if (res == NULL) {
  623. dev_err(&pdev->dev, "cannot find IO resource\n");
  624. ret = -ENOENT;
  625. goto err_clk;
  626. }
  627. i2c->ioarea = request_mem_region(res->start, resource_size(res),
  628. pdev->name);
  629. if (i2c->ioarea == NULL) {
  630. dev_err(&pdev->dev, "cannot request IO\n");
  631. ret = -ENXIO;
  632. goto err_clk;
  633. }
  634. i2c->regs = ioremap(res->start, resource_size(res));
  635. if (i2c->regs == NULL) {
  636. dev_err(&pdev->dev, "cannot map IO\n");
  637. ret = -ENXIO;
  638. goto err_ioarea;
  639. }
  640. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  641. i2c->regs, i2c->ioarea, res);
  642. /* setup info block for the i2c core */
  643. i2c->adap.algo_data = i2c;
  644. i2c->adap.dev.parent = &pdev->dev;
  645. /* initialise the i2c controller */
  646. ret = s3c24xx_i2c_init(i2c);
  647. if (ret != 0)
  648. goto err_iomap;
  649. /* find the IRQ for this unit (note, this relies on the init call to
  650. * ensure no current IRQs pending
  651. */
  652. i2c->irq = ret = platform_get_irq(pdev, 0);
  653. if (ret <= 0) {
  654. dev_err(&pdev->dev, "cannot find IRQ\n");
  655. goto err_iomap;
  656. }
  657. ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
  658. dev_name(&pdev->dev), i2c);
  659. if (ret != 0) {
  660. dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
  661. goto err_iomap;
  662. }
  663. ret = s3c24xx_i2c_register_cpufreq(i2c);
  664. if (ret < 0) {
  665. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  666. goto err_irq;
  667. }
  668. /* Note, previous versions of the driver used i2c_add_adapter()
  669. * to add the bus at any number. We now pass the bus number via
  670. * the platform data, so if unset it will now default to always
  671. * being bus 0.
  672. */
  673. i2c->adap.nr = pdata->bus_num;
  674. ret = i2c_add_numbered_adapter(&i2c->adap);
  675. if (ret < 0) {
  676. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  677. goto err_cpufreq;
  678. }
  679. platform_set_drvdata(pdev, i2c);
  680. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
  681. return 0;
  682. err_cpufreq:
  683. s3c24xx_i2c_deregister_cpufreq(i2c);
  684. err_irq:
  685. free_irq(i2c->irq, i2c);
  686. err_iomap:
  687. iounmap(i2c->regs);
  688. err_ioarea:
  689. release_resource(i2c->ioarea);
  690. kfree(i2c->ioarea);
  691. err_clk:
  692. clk_disable(i2c->clk);
  693. clk_put(i2c->clk);
  694. err_noclk:
  695. kfree(i2c);
  696. return ret;
  697. }
  698. /* s3c24xx_i2c_remove
  699. *
  700. * called when device is removed from the bus
  701. */
  702. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  703. {
  704. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  705. s3c24xx_i2c_deregister_cpufreq(i2c);
  706. i2c_del_adapter(&i2c->adap);
  707. free_irq(i2c->irq, i2c);
  708. clk_disable(i2c->clk);
  709. clk_put(i2c->clk);
  710. iounmap(i2c->regs);
  711. release_resource(i2c->ioarea);
  712. kfree(i2c->ioarea);
  713. kfree(i2c);
  714. return 0;
  715. }
  716. #ifdef CONFIG_PM
  717. static int s3c24xx_i2c_suspend_noirq(struct device *dev)
  718. {
  719. struct platform_device *pdev = to_platform_device(dev);
  720. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  721. i2c->suspended = 1;
  722. return 0;
  723. }
  724. static int s3c24xx_i2c_resume(struct device *dev)
  725. {
  726. struct platform_device *pdev = to_platform_device(dev);
  727. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  728. i2c->suspended = 0;
  729. s3c24xx_i2c_init(i2c);
  730. return 0;
  731. }
  732. static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
  733. .suspend_noirq = s3c24xx_i2c_suspend_noirq,
  734. .resume = s3c24xx_i2c_resume,
  735. };
  736. #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
  737. #else
  738. #define S3C24XX_DEV_PM_OPS NULL
  739. #endif
  740. /* device driver for platform bus bits */
  741. static struct platform_device_id s3c24xx_driver_ids[] = {
  742. {
  743. .name = "s3c2410-i2c",
  744. .driver_data = TYPE_S3C2410,
  745. }, {
  746. .name = "s3c2440-i2c",
  747. .driver_data = TYPE_S3C2440,
  748. }, { },
  749. };
  750. MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
  751. static struct platform_driver s3c24xx_i2c_driver = {
  752. .probe = s3c24xx_i2c_probe,
  753. .remove = s3c24xx_i2c_remove,
  754. .id_table = s3c24xx_driver_ids,
  755. .driver = {
  756. .owner = THIS_MODULE,
  757. .name = "s3c-i2c",
  758. .pm = S3C24XX_DEV_PM_OPS,
  759. },
  760. };
  761. static int __init i2c_adap_s3c_init(void)
  762. {
  763. return platform_driver_register(&s3c24xx_i2c_driver);
  764. }
  765. subsys_initcall(i2c_adap_s3c_init);
  766. static void __exit i2c_adap_s3c_exit(void)
  767. {
  768. platform_driver_unregister(&s3c24xx_i2c_driver);
  769. }
  770. module_exit(i2c_adap_s3c_exit);
  771. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  772. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  773. MODULE_LICENSE("GPL");