i2c-s3c2410.c 30 KB

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