i2c-exynos5.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. /* I2C_TRAILING_CTL Register bits */
  73. #define HSI2C_TRAILING_COUNT (0xf)
  74. /* I2C_INT_EN Register bits */
  75. #define HSI2C_INT_TX_ALMOSTEMPTY_EN (1u << 0)
  76. #define HSI2C_INT_RX_ALMOSTFULL_EN (1u << 1)
  77. #define HSI2C_INT_TRAILING_EN (1u << 6)
  78. /* I2C_INT_STAT Register bits */
  79. #define HSI2C_INT_TX_ALMOSTEMPTY (1u << 0)
  80. #define HSI2C_INT_RX_ALMOSTFULL (1u << 1)
  81. #define HSI2C_INT_TX_UNDERRUN (1u << 2)
  82. #define HSI2C_INT_TX_OVERRUN (1u << 3)
  83. #define HSI2C_INT_RX_UNDERRUN (1u << 4)
  84. #define HSI2C_INT_RX_OVERRUN (1u << 5)
  85. #define HSI2C_INT_TRAILING (1u << 6)
  86. #define HSI2C_INT_I2C (1u << 9)
  87. #define HSI2C_INT_TRANS_DONE (1u << 7)
  88. #define HSI2C_INT_TRANS_ABORT (1u << 8)
  89. #define HSI2C_INT_NO_DEV_ACK (1u << 9)
  90. #define HSI2C_INT_NO_DEV (1u << 10)
  91. #define HSI2C_INT_TIMEOUT (1u << 11)
  92. #define HSI2C_INT_I2C_TRANS (HSI2C_INT_TRANS_DONE | \
  93. HSI2C_INT_TRANS_ABORT | \
  94. HSI2C_INT_NO_DEV_ACK | \
  95. HSI2C_INT_NO_DEV | \
  96. HSI2C_INT_TIMEOUT)
  97. /* I2C_FIFO_STAT Register bits */
  98. #define HSI2C_RX_FIFO_EMPTY (1u << 24)
  99. #define HSI2C_RX_FIFO_FULL (1u << 23)
  100. #define HSI2C_RX_FIFO_LVL(x) ((x >> 16) & 0x7f)
  101. #define HSI2C_TX_FIFO_EMPTY (1u << 8)
  102. #define HSI2C_TX_FIFO_FULL (1u << 7)
  103. #define HSI2C_TX_FIFO_LVL(x) ((x >> 0) & 0x7f)
  104. /* I2C_CONF Register bits */
  105. #define HSI2C_AUTO_MODE (1u << 31)
  106. #define HSI2C_10BIT_ADDR_MODE (1u << 30)
  107. #define HSI2C_HS_MODE (1u << 29)
  108. /* I2C_AUTO_CONF Register bits */
  109. #define HSI2C_READ_WRITE (1u << 16)
  110. #define HSI2C_STOP_AFTER_TRANS (1u << 17)
  111. #define HSI2C_MASTER_RUN (1u << 31)
  112. /* I2C_TIMEOUT Register bits */
  113. #define HSI2C_TIMEOUT_EN (1u << 31)
  114. #define HSI2C_TIMEOUT_MASK 0xff
  115. /* I2C_TRANS_STATUS register bits */
  116. #define HSI2C_MASTER_BUSY (1u << 17)
  117. #define HSI2C_SLAVE_BUSY (1u << 16)
  118. #define HSI2C_TIMEOUT_AUTO (1u << 4)
  119. #define HSI2C_NO_DEV (1u << 3)
  120. #define HSI2C_NO_DEV_ACK (1u << 2)
  121. #define HSI2C_TRANS_ABORT (1u << 1)
  122. #define HSI2C_TRANS_DONE (1u << 0)
  123. /* I2C_ADDR register bits */
  124. #define HSI2C_SLV_ADDR_SLV(x) ((x & 0x3ff) << 0)
  125. #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
  126. #define HSI2C_MASTER_ID(x) ((x & 0xff) << 24)
  127. #define MASTER_ID(x) ((x & 0x7) + 0x08)
  128. /*
  129. * Controller operating frequency, timing values for operation
  130. * are calculated against this frequency
  131. */
  132. #define HSI2C_HS_TX_CLOCK 1000000
  133. #define HSI2C_FS_TX_CLOCK 100000
  134. #define HSI2C_HIGH_SPD 1
  135. #define HSI2C_FAST_SPD 0
  136. #define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(1000))
  137. #define HSI2C_EXYNOS7 BIT(0)
  138. struct exynos5_i2c {
  139. struct i2c_adapter adap;
  140. unsigned int suspended:1;
  141. struct i2c_msg *msg;
  142. struct completion msg_complete;
  143. unsigned int msg_ptr;
  144. unsigned int irq;
  145. void __iomem *regs;
  146. struct clk *clk;
  147. struct device *dev;
  148. int state;
  149. spinlock_t lock; /* IRQ synchronization */
  150. /*
  151. * Since the TRANS_DONE bit is cleared on read, and we may read it
  152. * either during an IRQ or after a transaction, keep track of its
  153. * state here.
  154. */
  155. int trans_done;
  156. /* Controller operating frequency */
  157. unsigned int fs_clock;
  158. unsigned int hs_clock;
  159. /*
  160. * HSI2C Controller can operate in
  161. * 1. High speed upto 3.4Mbps
  162. * 2. Fast speed upto 1Mbps
  163. */
  164. int speed_mode;
  165. /* Version of HS-I2C Hardware */
  166. struct exynos_hsi2c_variant *variant;
  167. };
  168. /**
  169. * struct exynos_hsi2c_variant - platform specific HSI2C driver data
  170. * @fifo_depth: the fifo depth supported by the HSI2C module
  171. *
  172. * Specifies platform specific configuration of HSI2C module.
  173. * Note: A structure for driver specific platform data is used for future
  174. * expansion of its usage.
  175. */
  176. struct exynos_hsi2c_variant {
  177. unsigned int fifo_depth;
  178. unsigned int hw;
  179. };
  180. static const struct exynos_hsi2c_variant exynos5250_hsi2c_data = {
  181. .fifo_depth = 64,
  182. };
  183. static const struct exynos_hsi2c_variant exynos5260_hsi2c_data = {
  184. .fifo_depth = 16,
  185. };
  186. static const struct exynos_hsi2c_variant exynos7_hsi2c_data = {
  187. .fifo_depth = 16,
  188. .hw = HSI2C_EXYNOS7,
  189. };
  190. static const struct of_device_id exynos5_i2c_match[] = {
  191. {
  192. .compatible = "samsung,exynos5-hsi2c",
  193. .data = &exynos5250_hsi2c_data
  194. }, {
  195. .compatible = "samsung,exynos5250-hsi2c",
  196. .data = &exynos5250_hsi2c_data
  197. }, {
  198. .compatible = "samsung,exynos5260-hsi2c",
  199. .data = &exynos5260_hsi2c_data
  200. }, {
  201. .compatible = "samsung,exynos7-hsi2c",
  202. .data = &exynos7_hsi2c_data
  203. }, {},
  204. };
  205. MODULE_DEVICE_TABLE(of, exynos5_i2c_match);
  206. static inline struct exynos_hsi2c_variant *exynos5_i2c_get_variant
  207. (struct platform_device *pdev)
  208. {
  209. const struct of_device_id *match;
  210. match = of_match_node(exynos5_i2c_match, pdev->dev.of_node);
  211. return (struct exynos_hsi2c_variant *)match->data;
  212. }
  213. static void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
  214. {
  215. writel(readl(i2c->regs + HSI2C_INT_STATUS),
  216. i2c->regs + HSI2C_INT_STATUS);
  217. }
  218. /*
  219. * exynos5_i2c_set_timing: updates the registers with appropriate
  220. * timing values calculated
  221. *
  222. * Returns 0 on success, -EINVAL if the cycle length cannot
  223. * be calculated.
  224. */
  225. static int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, int mode)
  226. {
  227. u32 i2c_timing_s1;
  228. u32 i2c_timing_s2;
  229. u32 i2c_timing_s3;
  230. u32 i2c_timing_sla;
  231. unsigned int t_start_su, t_start_hd;
  232. unsigned int t_stop_su;
  233. unsigned int t_data_su, t_data_hd;
  234. unsigned int t_scl_l, t_scl_h;
  235. unsigned int t_sr_release;
  236. unsigned int t_ftl_cycle;
  237. unsigned int clkin = clk_get_rate(i2c->clk);
  238. unsigned int div, utemp0 = 0, utemp1 = 0, clk_cycle;
  239. unsigned int op_clk = (mode == HSI2C_HIGH_SPD) ?
  240. i2c->hs_clock : i2c->fs_clock;
  241. /*
  242. * In case of HSI2C controller in Exynos5 series
  243. * FPCLK / FI2C =
  244. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  245. *
  246. * In case of HSI2C controllers in Exynos7 series
  247. * FPCLK / FI2C =
  248. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + FLT_CYCLE
  249. *
  250. * utemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  251. * utemp1 = (TSCLK_L + TSCLK_H + 2)
  252. */
  253. t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
  254. utemp0 = (clkin / op_clk) - 8;
  255. if (i2c->variant->hw == HSI2C_EXYNOS7)
  256. utemp0 -= t_ftl_cycle;
  257. else
  258. utemp0 -= 2 * t_ftl_cycle;
  259. /* CLK_DIV max is 256 */
  260. for (div = 0; div < 256; div++) {
  261. utemp1 = utemp0 / (div + 1);
  262. /*
  263. * SCL_L and SCL_H each has max value of 255
  264. * Hence, For the clk_cycle to the have right value
  265. * utemp1 has to be less then 512 and more than 4.
  266. */
  267. if ((utemp1 < 512) && (utemp1 > 4)) {
  268. clk_cycle = utemp1 - 2;
  269. break;
  270. } else if (div == 255) {
  271. dev_warn(i2c->dev, "Failed to calculate divisor");
  272. return -EINVAL;
  273. }
  274. }
  275. t_scl_l = clk_cycle / 2;
  276. t_scl_h = clk_cycle / 2;
  277. t_start_su = t_scl_l;
  278. t_start_hd = t_scl_l;
  279. t_stop_su = t_scl_l;
  280. t_data_su = t_scl_l / 2;
  281. t_data_hd = t_scl_l / 2;
  282. t_sr_release = clk_cycle;
  283. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  284. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  285. i2c_timing_s3 = div << 16 | t_sr_release << 0;
  286. i2c_timing_sla = t_data_hd << 0;
  287. dev_dbg(i2c->dev, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
  288. t_start_su, t_start_hd, t_stop_su);
  289. dev_dbg(i2c->dev, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
  290. t_data_su, t_scl_l, t_scl_h);
  291. dev_dbg(i2c->dev, "nClkDiv: %X, tSR_RELEASE: %X\n",
  292. div, t_sr_release);
  293. dev_dbg(i2c->dev, "tDATA_HD: %X\n", t_data_hd);
  294. if (mode == HSI2C_HIGH_SPD) {
  295. writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_HS1);
  296. writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_HS2);
  297. writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_HS3);
  298. } else {
  299. writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_FS1);
  300. writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_FS2);
  301. writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_FS3);
  302. }
  303. writel(i2c_timing_sla, i2c->regs + HSI2C_TIMING_SLA);
  304. return 0;
  305. }
  306. static int exynos5_hsi2c_clock_setup(struct exynos5_i2c *i2c)
  307. {
  308. /*
  309. * Configure the Fast speed timing values
  310. * Even the High Speed mode initially starts with Fast mode
  311. */
  312. if (exynos5_i2c_set_timing(i2c, HSI2C_FAST_SPD)) {
  313. dev_err(i2c->dev, "HSI2C FS Clock set up failed\n");
  314. return -EINVAL;
  315. }
  316. /* configure the High speed timing values */
  317. if (i2c->speed_mode == HSI2C_HIGH_SPD) {
  318. if (exynos5_i2c_set_timing(i2c, HSI2C_HIGH_SPD)) {
  319. dev_err(i2c->dev, "HSI2C HS Clock set up failed\n");
  320. return -EINVAL;
  321. }
  322. }
  323. return 0;
  324. }
  325. /*
  326. * exynos5_i2c_init: configures the controller for I2C functionality
  327. * Programs I2C controller for Master mode operation
  328. */
  329. static void exynos5_i2c_init(struct exynos5_i2c *i2c)
  330. {
  331. u32 i2c_conf = readl(i2c->regs + HSI2C_CONF);
  332. u32 i2c_timeout = readl(i2c->regs + HSI2C_TIMEOUT);
  333. /* Clear to disable Timeout */
  334. i2c_timeout &= ~HSI2C_TIMEOUT_EN;
  335. writel(i2c_timeout, i2c->regs + HSI2C_TIMEOUT);
  336. writel((HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  337. i2c->regs + HSI2C_CTL);
  338. writel(HSI2C_TRAILING_COUNT, i2c->regs + HSI2C_TRAILIG_CTL);
  339. if (i2c->speed_mode == HSI2C_HIGH_SPD) {
  340. writel(HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr)),
  341. i2c->regs + HSI2C_ADDR);
  342. i2c_conf |= HSI2C_HS_MODE;
  343. }
  344. writel(i2c_conf | HSI2C_AUTO_MODE, i2c->regs + HSI2C_CONF);
  345. }
  346. static void exynos5_i2c_reset(struct exynos5_i2c *i2c)
  347. {
  348. u32 i2c_ctl;
  349. /* Set and clear the bit for reset */
  350. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  351. i2c_ctl |= HSI2C_SW_RST;
  352. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  353. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  354. i2c_ctl &= ~HSI2C_SW_RST;
  355. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  356. /* We don't expect calculations to fail during the run */
  357. exynos5_hsi2c_clock_setup(i2c);
  358. /* Initialize the configure registers */
  359. exynos5_i2c_init(i2c);
  360. }
  361. /*
  362. * exynos5_i2c_irq: top level IRQ servicing routine
  363. *
  364. * INT_STATUS registers gives the interrupt details. Further,
  365. * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
  366. * state of the bus.
  367. */
  368. static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
  369. {
  370. struct exynos5_i2c *i2c = dev_id;
  371. u32 fifo_level, int_status, fifo_status, trans_status;
  372. unsigned char byte;
  373. int len = 0;
  374. i2c->state = -EINVAL;
  375. spin_lock(&i2c->lock);
  376. int_status = readl(i2c->regs + HSI2C_INT_STATUS);
  377. writel(int_status, i2c->regs + HSI2C_INT_STATUS);
  378. /* handle interrupt related to the transfer status */
  379. if (i2c->variant->hw == HSI2C_EXYNOS7) {
  380. if (int_status & HSI2C_INT_TRANS_DONE) {
  381. i2c->trans_done = 1;
  382. i2c->state = 0;
  383. } else if (int_status & HSI2C_INT_TRANS_ABORT) {
  384. dev_dbg(i2c->dev, "Deal with arbitration lose\n");
  385. i2c->state = -EAGAIN;
  386. goto stop;
  387. } else if (int_status & HSI2C_INT_NO_DEV_ACK) {
  388. dev_dbg(i2c->dev, "No ACK from device\n");
  389. i2c->state = -ENXIO;
  390. goto stop;
  391. } else if (int_status & HSI2C_INT_NO_DEV) {
  392. dev_dbg(i2c->dev, "No device\n");
  393. i2c->state = -ENXIO;
  394. goto stop;
  395. } else if (int_status & HSI2C_INT_TIMEOUT) {
  396. dev_dbg(i2c->dev, "Accessing device timed out\n");
  397. i2c->state = -ETIMEDOUT;
  398. goto stop;
  399. }
  400. } else if (int_status & HSI2C_INT_I2C) {
  401. trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
  402. if (trans_status & HSI2C_NO_DEV_ACK) {
  403. dev_dbg(i2c->dev, "No ACK from device\n");
  404. i2c->state = -ENXIO;
  405. goto stop;
  406. } else if (trans_status & HSI2C_NO_DEV) {
  407. dev_dbg(i2c->dev, "No device\n");
  408. i2c->state = -ENXIO;
  409. goto stop;
  410. } else if (trans_status & HSI2C_TRANS_ABORT) {
  411. dev_dbg(i2c->dev, "Deal with arbitration lose\n");
  412. i2c->state = -EAGAIN;
  413. goto stop;
  414. } else if (trans_status & HSI2C_TIMEOUT_AUTO) {
  415. dev_dbg(i2c->dev, "Accessing device timed out\n");
  416. i2c->state = -ETIMEDOUT;
  417. goto stop;
  418. } else if (trans_status & HSI2C_TRANS_DONE) {
  419. i2c->trans_done = 1;
  420. i2c->state = 0;
  421. }
  422. }
  423. if ((i2c->msg->flags & I2C_M_RD) && (int_status &
  424. (HSI2C_INT_TRAILING | HSI2C_INT_RX_ALMOSTFULL))) {
  425. fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
  426. fifo_level = HSI2C_RX_FIFO_LVL(fifo_status);
  427. len = min(fifo_level, i2c->msg->len - i2c->msg_ptr);
  428. while (len > 0) {
  429. byte = (unsigned char)
  430. readl(i2c->regs + HSI2C_RX_DATA);
  431. i2c->msg->buf[i2c->msg_ptr++] = byte;
  432. len--;
  433. }
  434. i2c->state = 0;
  435. } else if (int_status & HSI2C_INT_TX_ALMOSTEMPTY) {
  436. fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
  437. fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
  438. len = i2c->variant->fifo_depth - fifo_level;
  439. if (len > (i2c->msg->len - i2c->msg_ptr))
  440. len = i2c->msg->len - i2c->msg_ptr;
  441. while (len > 0) {
  442. byte = i2c->msg->buf[i2c->msg_ptr++];
  443. writel(byte, i2c->regs + HSI2C_TX_DATA);
  444. len--;
  445. }
  446. i2c->state = 0;
  447. }
  448. stop:
  449. if ((i2c->trans_done && (i2c->msg->len == i2c->msg_ptr)) ||
  450. (i2c->state < 0)) {
  451. writel(0, i2c->regs + HSI2C_INT_ENABLE);
  452. exynos5_i2c_clr_pend_irq(i2c);
  453. complete(&i2c->msg_complete);
  454. }
  455. spin_unlock(&i2c->lock);
  456. return IRQ_HANDLED;
  457. }
  458. /*
  459. * exynos5_i2c_wait_bus_idle
  460. *
  461. * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
  462. * cleared.
  463. *
  464. * Returns -EBUSY if the bus cannot be bought to idle
  465. */
  466. static int exynos5_i2c_wait_bus_idle(struct exynos5_i2c *i2c)
  467. {
  468. unsigned long stop_time;
  469. u32 trans_status;
  470. /* wait for 100 milli seconds for the bus to be idle */
  471. stop_time = jiffies + msecs_to_jiffies(100) + 1;
  472. do {
  473. trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
  474. if (!(trans_status & HSI2C_MASTER_BUSY))
  475. return 0;
  476. usleep_range(50, 200);
  477. } while (time_before(jiffies, stop_time));
  478. return -EBUSY;
  479. }
  480. /*
  481. * exynos5_i2c_message_start: Configures the bus and starts the xfer
  482. * i2c: struct exynos5_i2c pointer for the current bus
  483. * stop: Enables stop after transfer if set. Set for last transfer of
  484. * in the list of messages.
  485. *
  486. * Configures the bus for read/write function
  487. * Sets chip address to talk to, message length to be sent.
  488. * Enables appropriate interrupts and sends start xfer command.
  489. */
  490. static void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
  491. {
  492. u32 i2c_ctl;
  493. u32 int_en = 0;
  494. u32 i2c_auto_conf = 0;
  495. u32 fifo_ctl;
  496. unsigned long flags;
  497. unsigned short trig_lvl;
  498. if (i2c->variant->hw == HSI2C_EXYNOS7)
  499. int_en |= HSI2C_INT_I2C_TRANS;
  500. else
  501. int_en |= HSI2C_INT_I2C;
  502. i2c_ctl = readl(i2c->regs + HSI2C_CTL);
  503. i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
  504. fifo_ctl = HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN;
  505. if (i2c->msg->flags & I2C_M_RD) {
  506. i2c_ctl |= HSI2C_RXCHON;
  507. i2c_auto_conf |= HSI2C_READ_WRITE;
  508. trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
  509. (i2c->variant->fifo_depth * 3 / 4) : i2c->msg->len;
  510. fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(trig_lvl);
  511. int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
  512. HSI2C_INT_TRAILING_EN);
  513. } else {
  514. i2c_ctl |= HSI2C_TXCHON;
  515. trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
  516. (i2c->variant->fifo_depth * 1 / 4) : i2c->msg->len;
  517. fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(trig_lvl);
  518. int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
  519. }
  520. writel(HSI2C_SLV_ADDR_MAS(i2c->msg->addr), i2c->regs + HSI2C_ADDR);
  521. writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL);
  522. writel(i2c_ctl, i2c->regs + HSI2C_CTL);
  523. /*
  524. * Enable interrupts before starting the transfer so that we don't
  525. * miss any INT_I2C interrupts.
  526. */
  527. spin_lock_irqsave(&i2c->lock, flags);
  528. writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
  529. if (stop == 1)
  530. i2c_auto_conf |= HSI2C_STOP_AFTER_TRANS;
  531. i2c_auto_conf |= i2c->msg->len;
  532. i2c_auto_conf |= HSI2C_MASTER_RUN;
  533. writel(i2c_auto_conf, i2c->regs + HSI2C_AUTO_CONF);
  534. spin_unlock_irqrestore(&i2c->lock, flags);
  535. }
  536. static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
  537. struct i2c_msg *msgs, int stop)
  538. {
  539. unsigned long timeout;
  540. int ret;
  541. i2c->msg = msgs;
  542. i2c->msg_ptr = 0;
  543. i2c->trans_done = 0;
  544. reinit_completion(&i2c->msg_complete);
  545. exynos5_i2c_message_start(i2c, stop);
  546. timeout = wait_for_completion_timeout(&i2c->msg_complete,
  547. EXYNOS5_I2C_TIMEOUT);
  548. if (timeout == 0)
  549. ret = -ETIMEDOUT;
  550. else
  551. ret = i2c->state;
  552. /*
  553. * If this is the last message to be transfered (stop == 1)
  554. * Then check if the bus can be brought back to idle.
  555. */
  556. if (ret == 0 && stop)
  557. ret = exynos5_i2c_wait_bus_idle(i2c);
  558. if (ret < 0) {
  559. exynos5_i2c_reset(i2c);
  560. if (ret == -ETIMEDOUT)
  561. dev_warn(i2c->dev, "%s timeout\n",
  562. (msgs->flags & I2C_M_RD) ? "rx" : "tx");
  563. }
  564. /* Return the state as in interrupt routine */
  565. return ret;
  566. }
  567. static int exynos5_i2c_xfer(struct i2c_adapter *adap,
  568. struct i2c_msg *msgs, int num)
  569. {
  570. struct exynos5_i2c *i2c = adap->algo_data;
  571. int i = 0, ret = 0, stop = 0;
  572. if (i2c->suspended) {
  573. dev_err(i2c->dev, "HS-I2C is not initialized.\n");
  574. return -EIO;
  575. }
  576. ret = clk_enable(i2c->clk);
  577. if (ret)
  578. return ret;
  579. for (i = 0; i < num; i++, msgs++) {
  580. stop = (i == num - 1);
  581. ret = exynos5_i2c_xfer_msg(i2c, msgs, stop);
  582. if (ret < 0)
  583. goto out;
  584. }
  585. if (i == num) {
  586. ret = num;
  587. } else {
  588. /* Only one message, cannot access the device */
  589. if (i == 1)
  590. ret = -EREMOTEIO;
  591. else
  592. ret = i;
  593. dev_warn(i2c->dev, "xfer message failed\n");
  594. }
  595. out:
  596. clk_disable(i2c->clk);
  597. return ret;
  598. }
  599. static u32 exynos5_i2c_func(struct i2c_adapter *adap)
  600. {
  601. return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  602. }
  603. static const struct i2c_algorithm exynos5_i2c_algorithm = {
  604. .master_xfer = exynos5_i2c_xfer,
  605. .functionality = exynos5_i2c_func,
  606. };
  607. static int exynos5_i2c_probe(struct platform_device *pdev)
  608. {
  609. struct device_node *np = pdev->dev.of_node;
  610. struct exynos5_i2c *i2c;
  611. struct resource *mem;
  612. unsigned int op_clock;
  613. int ret;
  614. i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
  615. if (!i2c)
  616. return -ENOMEM;
  617. if (of_property_read_u32(np, "clock-frequency", &op_clock)) {
  618. i2c->speed_mode = HSI2C_FAST_SPD;
  619. i2c->fs_clock = HSI2C_FS_TX_CLOCK;
  620. } else {
  621. if (op_clock >= HSI2C_HS_TX_CLOCK) {
  622. i2c->speed_mode = HSI2C_HIGH_SPD;
  623. i2c->fs_clock = HSI2C_FS_TX_CLOCK;
  624. i2c->hs_clock = op_clock;
  625. } else {
  626. i2c->speed_mode = HSI2C_FAST_SPD;
  627. i2c->fs_clock = op_clock;
  628. }
  629. }
  630. strlcpy(i2c->adap.name, "exynos5-i2c", sizeof(i2c->adap.name));
  631. i2c->adap.owner = THIS_MODULE;
  632. i2c->adap.algo = &exynos5_i2c_algorithm;
  633. i2c->adap.retries = 3;
  634. i2c->dev = &pdev->dev;
  635. i2c->clk = devm_clk_get(&pdev->dev, "hsi2c");
  636. if (IS_ERR(i2c->clk)) {
  637. dev_err(&pdev->dev, "cannot get clock\n");
  638. return -ENOENT;
  639. }
  640. ret = clk_prepare_enable(i2c->clk);
  641. if (ret)
  642. return ret;
  643. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  644. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  645. if (IS_ERR(i2c->regs)) {
  646. ret = PTR_ERR(i2c->regs);
  647. goto err_clk;
  648. }
  649. i2c->adap.dev.of_node = np;
  650. i2c->adap.algo_data = i2c;
  651. i2c->adap.dev.parent = &pdev->dev;
  652. /* Clear pending interrupts from u-boot or misc causes */
  653. exynos5_i2c_clr_pend_irq(i2c);
  654. spin_lock_init(&i2c->lock);
  655. init_completion(&i2c->msg_complete);
  656. i2c->irq = ret = platform_get_irq(pdev, 0);
  657. if (ret <= 0) {
  658. dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
  659. ret = -EINVAL;
  660. goto err_clk;
  661. }
  662. ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
  663. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  664. dev_name(&pdev->dev), i2c);
  665. if (ret != 0) {
  666. dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
  667. goto err_clk;
  668. }
  669. /* Need to check the variant before setting up. */
  670. i2c->variant = exynos5_i2c_get_variant(pdev);
  671. ret = exynos5_hsi2c_clock_setup(i2c);
  672. if (ret)
  673. goto err_clk;
  674. exynos5_i2c_reset(i2c);
  675. ret = i2c_add_adapter(&i2c->adap);
  676. if (ret < 0) {
  677. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  678. goto err_clk;
  679. }
  680. platform_set_drvdata(pdev, i2c);
  681. clk_disable(i2c->clk);
  682. return 0;
  683. err_clk:
  684. clk_disable_unprepare(i2c->clk);
  685. return ret;
  686. }
  687. static int exynos5_i2c_remove(struct platform_device *pdev)
  688. {
  689. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  690. i2c_del_adapter(&i2c->adap);
  691. clk_unprepare(i2c->clk);
  692. return 0;
  693. }
  694. #ifdef CONFIG_PM_SLEEP
  695. static int exynos5_i2c_suspend_noirq(struct device *dev)
  696. {
  697. struct platform_device *pdev = to_platform_device(dev);
  698. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  699. i2c->suspended = 1;
  700. clk_unprepare(i2c->clk);
  701. return 0;
  702. }
  703. static int exynos5_i2c_resume_noirq(struct device *dev)
  704. {
  705. struct platform_device *pdev = to_platform_device(dev);
  706. struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
  707. int ret = 0;
  708. ret = clk_prepare_enable(i2c->clk);
  709. if (ret)
  710. return ret;
  711. ret = exynos5_hsi2c_clock_setup(i2c);
  712. if (ret) {
  713. clk_disable_unprepare(i2c->clk);
  714. return ret;
  715. }
  716. exynos5_i2c_init(i2c);
  717. clk_disable(i2c->clk);
  718. i2c->suspended = 0;
  719. return 0;
  720. }
  721. #endif
  722. static const struct dev_pm_ops exynos5_i2c_dev_pm_ops = {
  723. #ifdef CONFIG_PM_SLEEP
  724. .suspend_noirq = exynos5_i2c_suspend_noirq,
  725. .resume_noirq = exynos5_i2c_resume_noirq,
  726. .freeze_noirq = exynos5_i2c_suspend_noirq,
  727. .thaw_noirq = exynos5_i2c_resume_noirq,
  728. .poweroff_noirq = exynos5_i2c_suspend_noirq,
  729. .restore_noirq = exynos5_i2c_resume_noirq,
  730. #endif
  731. };
  732. static struct platform_driver exynos5_i2c_driver = {
  733. .probe = exynos5_i2c_probe,
  734. .remove = exynos5_i2c_remove,
  735. .driver = {
  736. .name = "exynos5-hsi2c",
  737. .pm = &exynos5_i2c_dev_pm_ops,
  738. .of_match_table = exynos5_i2c_match,
  739. },
  740. };
  741. module_platform_driver(exynos5_i2c_driver);
  742. MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
  743. MODULE_AUTHOR("Naveen Krishna Chatradhi, <ch.naveen@samsung.com>");
  744. MODULE_AUTHOR("Taekgyun Ko, <taeggyun.ko@samsung.com>");
  745. MODULE_LICENSE("GPL v2");