i2c-s3c2410.c 29 KB

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