i2c-s3c2410.c 26 KB

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