i2c-exynos5.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /**
  2. * i2c-exynos5.c - Samsung Exynos5 I2C Controller Driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/time.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/spinlock.h>
  25. /*
  26. * HSI2C controller from Samsung supports 2 modes of operation
  27. * 1. Auto mode: Where in master automatically controls the whole transaction
  28. * 2. Manual mode: Software controls the transaction by issuing commands
  29. * START, READ, WRITE, STOP, RESTART in I2C_MANUAL_CMD register.
  30. *
  31. * Operation mode can be selected by setting AUTO_MODE bit in I2C_CONF register
  32. *
  33. * Special bits are available for both modes of operation to set commands
  34. * and for checking transfer status
  35. */
  36. /* Register Map */
  37. #define HSI2C_CTL 0x00
  38. #define HSI2C_FIFO_CTL 0x04
  39. #define HSI2C_TRAILIG_CTL 0x08
  40. #define HSI2C_CLK_CTL 0x0C
  41. #define HSI2C_CLK_SLOT 0x10
  42. #define HSI2C_INT_ENABLE 0x20
  43. #define HSI2C_INT_STATUS 0x24
  44. #define HSI2C_ERR_STATUS 0x2C
  45. #define HSI2C_FIFO_STATUS 0x30
  46. #define HSI2C_TX_DATA 0x34
  47. #define HSI2C_RX_DATA 0x38
  48. #define HSI2C_CONF 0x40
  49. #define HSI2C_AUTO_CONF 0x44
  50. #define HSI2C_TIMEOUT 0x48
  51. #define HSI2C_MANUAL_CMD 0x4C
  52. #define HSI2C_TRANS_STATUS 0x50
  53. #define HSI2C_TIMING_HS1 0x54
  54. #define HSI2C_TIMING_HS2 0x58
  55. #define HSI2C_TIMING_HS3 0x5C
  56. #define HSI2C_TIMING_FS1 0x60
  57. #define HSI2C_TIMING_FS2 0x64
  58. #define HSI2C_TIMING_FS3 0x68
  59. #define HSI2C_TIMING_SLA 0x6C
  60. #define HSI2C_ADDR 0x70
  61. /* I2C_CTL Register bits */
  62. #define HSI2C_FUNC_MODE_I2C (1u << 0)
  63. #define HSI2C_MASTER (1u << 3)
  64. #define HSI2C_RXCHON (1u << 6)
  65. #define HSI2C_TXCHON (1u << 7)
  66. #define HSI2C_SW_RST (1u << 31)
  67. /* I2C_FIFO_CTL Register bits */
  68. #define HSI2C_RXFIFO_EN (1u << 0)
  69. #define HSI2C_TXFIFO_EN (1u << 1)
  70. #define HSI2C_RXFIFO_TRIGGER_LEVEL(x) ((x) << 4)
  71. #define HSI2C_TXFIFO_TRIGGER_LEVEL(x) ((x) << 16)
  72. /* As per user manual FIFO max depth is 64bytes */
  73. #define HSI2C_FIFO_MAX 0x40
  74. /* default trigger levels for Tx and Rx FIFOs */
  75. #define HSI2C_DEF_TXFIFO_LVL (HSI2C_FIFO_MAX - 0x30)
  76. #define HSI2C_DEF_RXFIFO_LVL (HSI2C_FIFO_MAX - 0x10)
  77. /* I2C_TRAILING_CTL Register bits */
  78. #define HSI2C_TRAILING_COUNT (0xf)
  79. /* I2C_INT_EN Register bits */
  80. #define HSI2C_INT_TX_ALMOSTEMPTY_EN (1u << 0)
  81. #define HSI2C_INT_RX_ALMOSTFULL_EN (1u << 1)
  82. #define HSI2C_INT_TRAILING_EN (1u << 6)
  83. #define HSI2C_INT_I2C_EN (1u << 9)
  84. /* I2C_INT_STAT Register bits */
  85. #define HSI2C_INT_TX_ALMOSTEMPTY (1u << 0)
  86. #define HSI2C_INT_RX_ALMOSTFULL (1u << 1)
  87. #define HSI2C_INT_TX_UNDERRUN (1u << 2)
  88. #define HSI2C_INT_TX_OVERRUN (1u << 3)
  89. #define HSI2C_INT_RX_UNDERRUN (1u << 4)
  90. #define HSI2C_INT_RX_OVERRUN (1u << 5)
  91. #define HSI2C_INT_TRAILING (1u << 6)
  92. #define HSI2C_INT_I2C (1u << 9)
  93. /* I2C_FIFO_STAT Register bits */
  94. #define HSI2C_RX_FIFO_EMPTY (1u << 24)
  95. #define HSI2C_RX_FIFO_FULL (1u << 23)
  96. #define HSI2C_RX_FIFO_LVL(x) ((x >> 16) & 0x7f)
  97. #define HSI2C_TX_FIFO_EMPTY (1u << 8)
  98. #define HSI2C_TX_FIFO_FULL (1u << 7)
  99. #define HSI2C_TX_FIFO_LVL(x) ((x >> 0) & 0x7f)
  100. /* I2C_CONF Register bits */
  101. #define HSI2C_AUTO_MODE (1u << 31)
  102. #define HSI2C_10BIT_ADDR_MODE (1u << 30)
  103. #define HSI2C_HS_MODE (1u << 29)
  104. /* I2C_AUTO_CONF Register bits */
  105. #define HSI2C_READ_WRITE (1u << 16)
  106. #define HSI2C_STOP_AFTER_TRANS (1u << 17)
  107. #define HSI2C_MASTER_RUN (1u << 31)
  108. /* I2C_TIMEOUT Register bits */
  109. #define HSI2C_TIMEOUT_EN (1u << 31)
  110. #define HSI2C_TIMEOUT_MASK 0xff
  111. /* I2C_TRANS_STATUS register bits */
  112. #define HSI2C_MASTER_BUSY (1u << 17)
  113. #define HSI2C_SLAVE_BUSY (1u << 16)
  114. #define HSI2C_TIMEOUT_AUTO (1u << 4)
  115. #define HSI2C_NO_DEV (1u << 3)
  116. #define HSI2C_NO_DEV_ACK (1u << 2)
  117. #define HSI2C_TRANS_ABORT (1u << 1)
  118. #define HSI2C_TRANS_DONE (1u << 0)
  119. /* I2C_ADDR register bits */
  120. #define HSI2C_SLV_ADDR_SLV(x) ((x & 0x3ff) << 0)
  121. #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
  122. #define HSI2C_MASTER_ID(x) ((x & 0xff) << 24)
  123. #define MASTER_ID(x) ((x & 0x7) + 0x08)
  124. /*
  125. * Controller operating frequency, timing values for operation
  126. * are calculated against this frequency
  127. */
  128. #define HSI2C_HS_TX_CLOCK 1000000
  129. #define HSI2C_FS_TX_CLOCK 100000
  130. #define HSI2C_HIGH_SPD 1
  131. #define HSI2C_FAST_SPD 0
  132. #define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(1000))
  133. struct exynos5_i2c {
  134. struct i2c_adapter adap;
  135. unsigned int suspended:1;
  136. struct i2c_msg *msg;
  137. struct completion msg_complete;
  138. unsigned int msg_ptr;
  139. unsigned int irq;
  140. void __iomem *regs;
  141. struct clk *clk;
  142. struct device *dev;
  143. int state;
  144. spinlock_t lock; /* IRQ synchronization */
  145. /*
  146. * Since the TRANS_DONE bit is cleared on read, and we may read it
  147. * either during an IRQ or after a transaction, keep track of its
  148. * state here.
  149. */
  150. int trans_done;
  151. /* Controller operating frequency */
  152. unsigned int fs_clock;
  153. unsigned int hs_clock;
  154. /*
  155. * HSI2C Controller can operate in
  156. * 1. High speed upto 3.4Mbps
  157. * 2. Fast speed upto 1Mbps
  158. */
  159. int speed_mode;
  160. };
  161. static const struct of_device_id exynos5_i2c_match[] = {
  162. { .compatible = "samsung,exynos5-hsi2c" },
  163. {},
  164. };
  165. MODULE_DEVICE_TABLE(of, exynos5_i2c_match);
  166. static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
  167. {
  168. writel(readl(i2c->regs + HSI2C_INT_STATUS),
  169. i2c->regs + HSI2C_INT_STATUS);
  170. }
  171. /*
  172. * exynos5_i2c_set_timing: updates the registers with appropriate
  173. * timing values calculated
  174. *
  175. * Returns 0 on success, -EINVAL if the cycle length cannot
  176. * be calculated.
  177. */
  178. static int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, int mode)
  179. {
  180. u32 i2c_timing_s1;
  181. u32 i2c_timing_s2;
  182. u32 i2c_timing_s3;
  183. u32 i2c_timing_sla;
  184. unsigned int t_start_su, t_start_hd;
  185. unsigned int t_stop_su;
  186. unsigned int t_data_su, t_data_hd;
  187. unsigned int t_scl_l, t_scl_h;
  188. unsigned int t_sr_release;
  189. unsigned int t_ftl_cycle;
  190. unsigned int clkin = clk_get_rate(i2c->clk);
  191. unsigned int div, utemp0 = 0, utemp1 = 0, clk_cycle;
  192. unsigned int op_clk = (mode == HSI2C_HIGH_SPD) ?
  193. i2c->hs_clock : i2c->fs_clock;
  194. /*
  195. * FPCLK / FI2C =
  196. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  197. * utemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  198. * utemp1 = (TSCLK_L + TSCLK_H + 2)
  199. */
  200. t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
  201. utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
  202. /* CLK_DIV max is 256 */
  203. for (div = 0; div < 256; div++) {
  204. utemp1 = utemp0 / (div + 1);
  205. /*
  206. * SCL_L and SCL_H each has max value of 255
  207. * Hence, For the clk_cycle to the have right value
  208. * utemp1 has to be less then 512 and more than 4.
  209. */
  210. if ((utemp1 < 512) && (utemp1 > 4)) {
  211. clk_cycle = utemp1 - 2;
  212. break;
  213. } else if (div == 255) {
  214. dev_warn(i2c->dev, "Failed to calculate divisor");
  215. return -EINVAL;
  216. }
  217. }
  218. t_scl_l = clk_cycle / 2;
  219. t_scl_h = clk_cycle / 2;
  220. t_start_su = t_scl_l;
  221. t_start_hd = t_scl_l;
  222. t_stop_su = t_scl_l;
  223. t_data_su = t_scl_l / 2;
  224. t_data_hd = t_scl_l / 2;
  225. t_sr_release = clk_cycle;
  226. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  227. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  228. i2c_timing_s3 = div << 16 | t_sr_release << 0;
  229. i2c_timing_sla = t_data_hd << 0;
  230. dev_dbg(i2c->dev, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
  231. t_start_su, t_start_hd, t_stop_su);
  232. dev_dbg(i2c->dev, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
  233. t_data_su, t_scl_l, t_scl_h);
  234. dev_dbg(i2c->dev, "nClkDiv: %X, tSR_RELEASE: %X\n",
  235. div, t_sr_release);
  236. dev_dbg(i2c->dev, "tDATA_HD: %X\n", t_data_hd);
  237. if (mode == HSI2C_HIGH_SPD) {
  238. writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_HS1);
  239. writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_HS2);
  240. writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_HS3);
  241. } else {
  242. writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_FS1);
  243. writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_FS2);
  244. writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_FS3);
  245. }
  246. writel(i2c_timing_sla, i2c->regs + HSI2C_TIMING_SLA);
  247. return 0;
  248. }
  249. static int exynos5_hsi2c_clock_setup(struct exynos5_i2c *i2c)
  250. {
  251. /*
  252. * Configure the Fast speed timing values
  253. * Even the High Speed mode initially starts with Fast mode
  254. */
  255. if (exynos5_i2c_set_timing(i2c, HSI2C_FAST_SPD)) {
  256. dev_err(i2c->dev, "HSI2C FS Clock set up failed\n");
  257. return -EINVAL;
  258. }
  259. /* configure the High speed timing values */
  260. if (i2c->speed_mode == HSI2C_HIGH_SPD) {
  261. if (exynos5_i2c_set_timing(i2c, HSI2C_HIGH_SPD)) {
  262. dev_err(i2c->dev, "HSI2C HS Clock set up failed\n");
  263. return -EINVAL;
  264. }
  265. }
  266. return 0;
  267. }
  268. /*
  269. * exynos5_i2c_init: configures the controller for I2C functionality
  270. * Programs I2C controller for Master mode operation
  271. */
  272. static void exynos5_i2c_init(struct exynos5_i2c *i2c)
  273. {
  274. u32 i2c_conf = readl(i2c->regs + HSI2C_CONF);
  275. u32 i2c_timeout = readl(i2c->regs + HSI2C_TIMEOUT);
  276. /* Clear to disable Timeout */
  277. i2c_timeout &= ~HSI2C_TIMEOUT_EN;
  278. writel(i2c_timeout, i2c->regs + HSI2C_TIMEOUT);
  279. writel((HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  280. i2c->regs + HSI2C_CTL);
  281. writel(HSI2C_TRAILING_COUNT, i2c->regs + HSI2C_TRAILIG_CTL);
  282. if (i2c->speed_mode == HSI2C_HIGH_SPD) {
  283. writel(HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr)),
  284. i2c->regs + HSI2C_ADDR);
  285. i2c_conf |= HSI2C_HS_MODE;
  286. }
  287. writel(i2c_conf | HSI2C_AUTO_MODE, i2c->regs + HSI2C_CONF);
  288. }
  289. static void exynos5_i2c_reset(struct exynos5_i2c *i2c)
  290. {
  291. u32 i2c_ctl;
  292. /* Set and clear the bit for reset */
  293. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  294. i2c_ctl |= HSI2C_SW_RST;
  295. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  296. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  297. i2c_ctl &= ~HSI2C_SW_RST;
  298. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  299. /* We don't expect calculations to fail during the run */
  300. exynos5_hsi2c_clock_setup(i2c);
  301. /* Initialize the configure registers */
  302. exynos5_i2c_init(i2c);
  303. }
  304. /*
  305. * exynos5_i2c_irq: top level IRQ servicing routine
  306. *
  307. * INT_STATUS registers gives the interrupt details. Further,
  308. * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
  309. * state of the bus.
  310. */
  311. static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
  312. {
  313. struct exynos5_i2c *i2c = dev_id;
  314. u32 fifo_level, int_status, fifo_status, trans_status;
  315. unsigned char byte;
  316. int len = 0;
  317. i2c->state = -EINVAL;
  318. spin_lock(&i2c->lock);
  319. int_status = readl(i2c->regs + HSI2C_INT_STATUS);
  320. writel(int_status, i2c->regs + HSI2C_INT_STATUS);
  321. fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
  322. /* handle interrupt related to the transfer status */
  323. if (int_status & HSI2C_INT_I2C) {
  324. trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
  325. if (trans_status & HSI2C_NO_DEV_ACK) {
  326. dev_dbg(i2c->dev, "No ACK from device\n");
  327. i2c->state = -ENXIO;
  328. goto stop;
  329. } else if (trans_status & HSI2C_NO_DEV) {
  330. dev_dbg(i2c->dev, "No device\n");
  331. i2c->state = -ENXIO;
  332. goto stop;
  333. } else if (trans_status & HSI2C_TRANS_ABORT) {
  334. dev_dbg(i2c->dev, "Deal with arbitration lose\n");
  335. i2c->state = -EAGAIN;
  336. goto stop;
  337. } else if (trans_status & HSI2C_TIMEOUT_AUTO) {
  338. dev_dbg(i2c->dev, "Accessing device timed out\n");
  339. i2c->state = -EAGAIN;
  340. goto stop;
  341. } else if (trans_status & HSI2C_TRANS_DONE) {
  342. i2c->trans_done = 1;
  343. i2c->state = 0;
  344. }
  345. }
  346. if ((i2c->msg->flags & I2C_M_RD) && (int_status &
  347. (HSI2C_INT_TRAILING | HSI2C_INT_RX_ALMOSTFULL))) {
  348. fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
  349. fifo_level = HSI2C_RX_FIFO_LVL(fifo_status);
  350. len = min(fifo_level, i2c->msg->len - i2c->msg_ptr);
  351. while (len > 0) {
  352. byte = (unsigned char)
  353. readl(i2c->regs + HSI2C_RX_DATA);
  354. i2c->msg->buf[i2c->msg_ptr++] = byte;
  355. len--;
  356. }
  357. i2c->state = 0;
  358. } else if (int_status & HSI2C_INT_TX_ALMOSTEMPTY) {
  359. fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
  360. fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
  361. len = HSI2C_FIFO_MAX - fifo_level;
  362. if (len > (i2c->msg->len - i2c->msg_ptr))
  363. len = i2c->msg->len - i2c->msg_ptr;
  364. while (len > 0) {
  365. byte = i2c->msg->buf[i2c->msg_ptr++];
  366. writel(byte, i2c->regs + HSI2C_TX_DATA);
  367. len--;
  368. }
  369. i2c->state = 0;
  370. }
  371. stop:
  372. if ((i2c->trans_done && (i2c->msg->len == i2c->msg_ptr)) ||
  373. (i2c->state < 0)) {
  374. writel(0, i2c->regs + HSI2C_INT_ENABLE);
  375. exynos5_i2c_clr_pend_irq(i2c);
  376. complete(&i2c->msg_complete);
  377. }
  378. spin_unlock(&i2c->lock);
  379. return IRQ_HANDLED;
  380. }
  381. /*
  382. * exynos5_i2c_wait_bus_idle
  383. *
  384. * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
  385. * cleared.
  386. *
  387. * Returns -EBUSY if the bus cannot be bought to idle
  388. */
  389. static int exynos5_i2c_wait_bus_idle(struct exynos5_i2c *i2c)
  390. {
  391. unsigned long stop_time;
  392. u32 trans_status;
  393. /* wait for 100 milli seconds for the bus to be idle */
  394. stop_time = jiffies + msecs_to_jiffies(100) + 1;
  395. do {
  396. trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
  397. if (!(trans_status & HSI2C_MASTER_BUSY))
  398. return 0;
  399. usleep_range(50, 200);
  400. } while (time_before(jiffies, stop_time));
  401. return -EBUSY;
  402. }
  403. /*
  404. * exynos5_i2c_message_start: Configures the bus and starts the xfer
  405. * i2c: struct exynos5_i2c pointer for the current bus
  406. * stop: Enables stop after transfer if set. Set for last transfer of
  407. * in the list of messages.
  408. *
  409. * Configures the bus for read/write function
  410. * Sets chip address to talk to, message length to be sent.
  411. * Enables appropriate interrupts and sends start xfer command.
  412. */
  413. static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
  414. {
  415. u32 i2c_ctl;
  416. u32 int_en = HSI2C_INT_I2C_EN;
  417. u32 i2c_auto_conf = 0;
  418. u32 fifo_ctl;
  419. unsigned long flags;
  420. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  421. i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
  422. fifo_ctl = HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN;
  423. if (i2c->msg->flags & I2C_M_RD) {
  424. i2c_ctl |= HSI2C_RXCHON;
  425. i2c_auto_conf = HSI2C_READ_WRITE;
  426. fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(HSI2C_DEF_TXFIFO_LVL);
  427. int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
  428. HSI2C_INT_TRAILING_EN);
  429. } else {
  430. i2c_ctl |= HSI2C_TXCHON;
  431. fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(HSI2C_DEF_RXFIFO_LVL);
  432. int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
  433. }
  434. writel(HSI2C_SLV_ADDR_MAS(i2c->msg->addr), i2c->regs + HSI2C_ADDR);
  435. writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL);
  436. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  437. /*
  438. * Enable interrupts before starting the transfer so that we don't
  439. * miss any INT_I2C interrupts.
  440. */
  441. spin_lock_irqsave(&i2c->lock, flags);
  442. writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
  443. if (stop == 1)
  444. i2c_auto_conf |= HSI2C_STOP_AFTER_TRANS;
  445. i2c_auto_conf |= i2c->msg->len;
  446. i2c_auto_conf |= HSI2C_MASTER_RUN;
  447. writel(i2c_auto_conf, i2c->regs + HSI2C_AUTO_CONF);
  448. spin_unlock_irqrestore(&i2c->lock, flags);
  449. }
  450. static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
  451. struct i2c_msg *msgs, int stop)
  452. {
  453. unsigned long timeout;
  454. int ret;
  455. i2c->msg = msgs;
  456. i2c->msg_ptr = 0;
  457. i2c->trans_done = 0;
  458. reinit_completion(&i2c->msg_complete);
  459. exynos5_i2c_message_start(i2c, stop);
  460. timeout = wait_for_completion_timeout(&i2c->msg_complete,
  461. EXYNOS5_I2C_TIMEOUT);
  462. if (timeout == 0)
  463. ret = -ETIMEDOUT;
  464. else
  465. ret = i2c->state;
  466. /*
  467. * If this is the last message to be transfered (stop == 1)
  468. * Then check if the bus can be brought back to idle.
  469. */
  470. if (ret == 0 && stop)
  471. ret = exynos5_i2c_wait_bus_idle(i2c);
  472. if (ret < 0) {
  473. exynos5_i2c_reset(i2c);
  474. if (ret == -ETIMEDOUT)
  475. dev_warn(i2c->dev, "%s timeout\n",
  476. (msgs->flags & I2C_M_RD) ? "rx" : "tx");
  477. }
  478. /* Return the state as in interrupt routine */
  479. return ret;
  480. }
  481. static int exynos5_i2c_xfer(struct i2c_adapter *adap,
  482. struct i2c_msg *msgs, int num)
  483. {
  484. struct exynos5_i2c *i2c = (struct exynos5_i2c *)adap->algo_data;
  485. int i = 0, ret = 0, stop = 0;
  486. if (i2c->suspended) {
  487. dev_err(i2c->dev, "HS-I2C is not initialized.\n");
  488. return -EIO;
  489. }
  490. clk_prepare_enable(i2c->clk);
  491. for (i = 0; i < num; i++, msgs++) {
  492. stop = (i == num - 1);
  493. ret = exynos5_i2c_xfer_msg(i2c, msgs, stop);
  494. if (ret < 0)
  495. goto out;
  496. }
  497. if (i == num) {
  498. ret = num;
  499. } else {
  500. /* Only one message, cannot access the device */
  501. if (i == 1)
  502. ret = -EREMOTEIO;
  503. else
  504. ret = i;
  505. dev_warn(i2c->dev, "xfer message failed\n");
  506. }
  507. out:
  508. clk_disable_unprepare(i2c->clk);
  509. return ret;
  510. }
  511. static u32 exynos5_i2c_func(struct i2c_adapter *adap)
  512. {
  513. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  514. }
  515. static const struct i2c_algorithm exynos5_i2c_algorithm = {
  516. .master_xfer = exynos5_i2c_xfer,
  517. .functionality = exynos5_i2c_func,
  518. };
  519. static int exynos5_i2c_probe(struct platform_device *pdev)
  520. {
  521. struct device_node *np = pdev->dev.of_node;
  522. struct exynos5_i2c *i2c;
  523. struct resource *mem;
  524. unsigned int op_clock;
  525. int ret;
  526. i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
  527. if (!i2c) {
  528. dev_err(&pdev->dev, "no memory for state\n");
  529. return -ENOMEM;
  530. }
  531. if (of_property_read_u32(np, "clock-frequency", &op_clock)) {
  532. i2c->speed_mode = HSI2C_FAST_SPD;
  533. i2c->fs_clock = HSI2C_FS_TX_CLOCK;
  534. } else {
  535. if (op_clock >= HSI2C_HS_TX_CLOCK) {
  536. i2c->speed_mode = HSI2C_HIGH_SPD;
  537. i2c->fs_clock = HSI2C_FS_TX_CLOCK;
  538. i2c->hs_clock = op_clock;
  539. } else {
  540. i2c->speed_mode = HSI2C_FAST_SPD;
  541. i2c->fs_clock = op_clock;
  542. }
  543. }
  544. strlcpy(i2c->adap.name, "exynos5-i2c", sizeof(i2c->adap.name));
  545. i2c->adap.owner = THIS_MODULE;
  546. i2c->adap.algo = &exynos5_i2c_algorithm;
  547. i2c->adap.retries = 3;
  548. i2c->dev = &pdev->dev;
  549. i2c->clk = devm_clk_get(&pdev->dev, "hsi2c");
  550. if (IS_ERR(i2c->clk)) {
  551. dev_err(&pdev->dev, "cannot get clock\n");
  552. return -ENOENT;
  553. }
  554. clk_prepare_enable(i2c->clk);
  555. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  556. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  557. if (IS_ERR(i2c->regs)) {
  558. ret = PTR_ERR(i2c->regs);
  559. goto err_clk;
  560. }
  561. i2c->adap.dev.of_node = np;
  562. i2c->adap.algo_data = i2c;
  563. i2c->adap.dev.parent = &pdev->dev;
  564. /* Clear pending interrupts from u-boot or misc causes */
  565. exynos5_i2c_clr_pend_irq(i2c);
  566. spin_lock_init(&i2c->lock);
  567. init_completion(&i2c->msg_complete);
  568. i2c->irq = ret = platform_get_irq(pdev, 0);
  569. if (ret <= 0) {
  570. dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
  571. ret = -EINVAL;
  572. goto err_clk;
  573. }
  574. ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
  575. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  576. dev_name(&pdev->dev), i2c);
  577. if (ret != 0) {
  578. dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
  579. goto err_clk;
  580. }
  581. ret = exynos5_hsi2c_clock_setup(i2c);
  582. if (ret)
  583. goto err_clk;
  584. exynos5_i2c_init(i2c);
  585. ret = i2c_add_adapter(&i2c->adap);
  586. if (ret < 0) {
  587. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  588. goto err_clk;
  589. }
  590. platform_set_drvdata(pdev, i2c);
  591. err_clk:
  592. clk_disable_unprepare(i2c->clk);
  593. return ret;
  594. }
  595. static int exynos5_i2c_remove(struct platform_device *pdev)
  596. {
  597. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  598. i2c_del_adapter(&i2c->adap);
  599. return 0;
  600. }
  601. static int exynos5_i2c_suspend_noirq(struct device *dev)
  602. {
  603. struct platform_device *pdev = to_platform_device(dev);
  604. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  605. i2c->suspended = 1;
  606. return 0;
  607. }
  608. static int exynos5_i2c_resume_noirq(struct device *dev)
  609. {
  610. struct platform_device *pdev = to_platform_device(dev);
  611. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  612. int ret = 0;
  613. clk_prepare_enable(i2c->clk);
  614. ret = exynos5_hsi2c_clock_setup(i2c);
  615. if (ret) {
  616. clk_disable_unprepare(i2c->clk);
  617. return ret;
  618. }
  619. exynos5_i2c_init(i2c);
  620. clk_disable_unprepare(i2c->clk);
  621. i2c->suspended = 0;
  622. return 0;
  623. }
  624. static SIMPLE_DEV_PM_OPS(exynos5_i2c_dev_pm_ops, exynos5_i2c_suspend_noirq,
  625. exynos5_i2c_resume_noirq);
  626. static struct platform_driver exynos5_i2c_driver = {
  627. .probe = exynos5_i2c_probe,
  628. .remove = exynos5_i2c_remove,
  629. .driver = {
  630. .owner = THIS_MODULE,
  631. .name = "exynos5-hsi2c",
  632. .pm = &exynos5_i2c_dev_pm_ops,
  633. .of_match_table = exynos5_i2c_match,
  634. },
  635. };
  636. module_platform_driver(exynos5_i2c_driver);
  637. MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
  638. MODULE_AUTHOR("Naveen Krishna Chatradhi, <ch.naveen@samsung.com>");
  639. MODULE_AUTHOR("Taekgyun Ko, <taeggyun.ko@samsung.com>");
  640. MODULE_LICENSE("GPL v2");