i2c-s3c2410.c 25 KB

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