i2c-s3c2410.c 30 KB

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