i2c-mt65xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Xudong Chen <xudong.chen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/completion.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/i2c.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #define I2C_RS_TRANSFER (1 << 4)
  35. #define I2C_HS_NACKERR (1 << 2)
  36. #define I2C_ACKERR (1 << 1)
  37. #define I2C_TRANSAC_COMP (1 << 0)
  38. #define I2C_TRANSAC_START (1 << 0)
  39. #define I2C_RS_MUL_CNFG (1 << 15)
  40. #define I2C_RS_MUL_TRIG (1 << 14)
  41. #define I2C_DCM_DISABLE 0x0000
  42. #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
  43. #define I2C_IO_CONFIG_PUSH_PULL 0x0000
  44. #define I2C_SOFT_RST 0x0001
  45. #define I2C_FIFO_ADDR_CLR 0x0001
  46. #define I2C_DELAY_LEN 0x0002
  47. #define I2C_ST_START_CON 0x8001
  48. #define I2C_FS_START_CON 0x1800
  49. #define I2C_TIME_CLR_VALUE 0x0000
  50. #define I2C_TIME_DEFAULT_VALUE 0x0003
  51. #define I2C_FS_TIME_INIT_VALUE 0x1303
  52. #define I2C_WRRD_TRANAC_VALUE 0x0002
  53. #define I2C_RD_TRANAC_VALUE 0x0001
  54. #define I2C_DMA_CON_TX 0x0000
  55. #define I2C_DMA_CON_RX 0x0001
  56. #define I2C_DMA_START_EN 0x0001
  57. #define I2C_DMA_INT_FLAG_NONE 0x0000
  58. #define I2C_DMA_CLR_FLAG 0x0000
  59. #define I2C_DMA_HARD_RST 0x0002
  60. #define I2C_DEFAULT_SPEED 100000 /* hz */
  61. #define MAX_FS_MODE_SPEED 400000
  62. #define MAX_HS_MODE_SPEED 3400000
  63. #define MAX_SAMPLE_CNT_DIV 8
  64. #define MAX_STEP_CNT_DIV 64
  65. #define MAX_HS_STEP_CNT_DIV 8
  66. #define I2C_CONTROL_RS (0x1 << 1)
  67. #define I2C_CONTROL_DMA_EN (0x1 << 2)
  68. #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
  69. #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
  70. #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
  71. #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
  72. #define I2C_CONTROL_WRAPPER (0x1 << 0)
  73. #define I2C_DRV_NAME "i2c-mt65xx"
  74. enum DMA_REGS_OFFSET {
  75. OFFSET_INT_FLAG = 0x0,
  76. OFFSET_INT_EN = 0x04,
  77. OFFSET_EN = 0x08,
  78. OFFSET_RST = 0x0c,
  79. OFFSET_CON = 0x18,
  80. OFFSET_TX_MEM_ADDR = 0x1c,
  81. OFFSET_RX_MEM_ADDR = 0x20,
  82. OFFSET_TX_LEN = 0x24,
  83. OFFSET_RX_LEN = 0x28,
  84. };
  85. enum i2c_trans_st_rs {
  86. I2C_TRANS_STOP = 0,
  87. I2C_TRANS_REPEATED_START,
  88. };
  89. enum mtk_trans_op {
  90. I2C_MASTER_WR = 1,
  91. I2C_MASTER_RD,
  92. I2C_MASTER_WRRD,
  93. };
  94. enum I2C_REGS_OFFSET {
  95. OFFSET_DATA_PORT = 0x0,
  96. OFFSET_SLAVE_ADDR = 0x04,
  97. OFFSET_INTR_MASK = 0x08,
  98. OFFSET_INTR_STAT = 0x0c,
  99. OFFSET_CONTROL = 0x10,
  100. OFFSET_TRANSFER_LEN = 0x14,
  101. OFFSET_TRANSAC_LEN = 0x18,
  102. OFFSET_DELAY_LEN = 0x1c,
  103. OFFSET_TIMING = 0x20,
  104. OFFSET_START = 0x24,
  105. OFFSET_EXT_CONF = 0x28,
  106. OFFSET_FIFO_STAT = 0x30,
  107. OFFSET_FIFO_THRESH = 0x34,
  108. OFFSET_FIFO_ADDR_CLR = 0x38,
  109. OFFSET_IO_CONFIG = 0x40,
  110. OFFSET_RSV_DEBUG = 0x44,
  111. OFFSET_HS = 0x48,
  112. OFFSET_SOFTRESET = 0x50,
  113. OFFSET_DCM_EN = 0x54,
  114. OFFSET_PATH_DIR = 0x60,
  115. OFFSET_DEBUGSTAT = 0x64,
  116. OFFSET_DEBUGCTRL = 0x68,
  117. OFFSET_TRANSFER_LEN_AUX = 0x6c,
  118. };
  119. struct mtk_i2c_compatible {
  120. const struct i2c_adapter_quirks *quirks;
  121. unsigned char pmic_i2c: 1;
  122. unsigned char dcm: 1;
  123. unsigned char auto_restart: 1;
  124. };
  125. struct mtk_i2c {
  126. struct i2c_adapter adap; /* i2c host adapter */
  127. struct device *dev;
  128. struct completion msg_complete;
  129. /* set in i2c probe */
  130. void __iomem *base; /* i2c base addr */
  131. void __iomem *pdmabase; /* dma base address*/
  132. struct clk *clk_main; /* main clock for i2c bus */
  133. struct clk *clk_dma; /* DMA clock for i2c via DMA */
  134. struct clk *clk_pmic; /* PMIC clock for i2c from PMIC */
  135. bool have_pmic; /* can use i2c pins from PMIC */
  136. bool use_push_pull; /* IO config push-pull mode */
  137. u16 irq_stat; /* interrupt status */
  138. unsigned int speed_hz; /* The speed in transfer */
  139. enum mtk_trans_op op;
  140. u16 timing_reg;
  141. u16 high_speed_reg;
  142. const struct mtk_i2c_compatible *dev_comp;
  143. };
  144. static const struct i2c_adapter_quirks mt6577_i2c_quirks = {
  145. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  146. .max_num_msgs = 1,
  147. .max_write_len = 255,
  148. .max_read_len = 255,
  149. .max_comb_1st_msg_len = 255,
  150. .max_comb_2nd_msg_len = 31,
  151. };
  152. static const struct i2c_adapter_quirks mt8173_i2c_quirks = {
  153. .max_num_msgs = 65535,
  154. .max_write_len = 65535,
  155. .max_read_len = 65535,
  156. .max_comb_1st_msg_len = 65535,
  157. .max_comb_2nd_msg_len = 65535,
  158. };
  159. static const struct mtk_i2c_compatible mt6577_compat = {
  160. .quirks = &mt6577_i2c_quirks,
  161. .pmic_i2c = 0,
  162. .dcm = 1,
  163. .auto_restart = 0,
  164. };
  165. static const struct mtk_i2c_compatible mt6589_compat = {
  166. .quirks = &mt6577_i2c_quirks,
  167. .pmic_i2c = 1,
  168. .dcm = 0,
  169. .auto_restart = 0,
  170. };
  171. static const struct mtk_i2c_compatible mt8173_compat = {
  172. .quirks = &mt8173_i2c_quirks,
  173. .pmic_i2c = 0,
  174. .dcm = 1,
  175. .auto_restart = 1,
  176. };
  177. static const struct of_device_id mtk_i2c_of_match[] = {
  178. { .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat },
  179. { .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat },
  180. { .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat },
  181. {}
  182. };
  183. MODULE_DEVICE_TABLE(of, mtk_i2c_of_match);
  184. static int mtk_i2c_clock_enable(struct mtk_i2c *i2c)
  185. {
  186. int ret;
  187. ret = clk_prepare_enable(i2c->clk_dma);
  188. if (ret)
  189. return ret;
  190. ret = clk_prepare_enable(i2c->clk_main);
  191. if (ret)
  192. goto err_main;
  193. if (i2c->have_pmic) {
  194. ret = clk_prepare_enable(i2c->clk_pmic);
  195. if (ret)
  196. goto err_pmic;
  197. }
  198. return 0;
  199. err_pmic:
  200. clk_disable_unprepare(i2c->clk_main);
  201. err_main:
  202. clk_disable_unprepare(i2c->clk_dma);
  203. return ret;
  204. }
  205. static void mtk_i2c_clock_disable(struct mtk_i2c *i2c)
  206. {
  207. if (i2c->have_pmic)
  208. clk_disable_unprepare(i2c->clk_pmic);
  209. clk_disable_unprepare(i2c->clk_main);
  210. clk_disable_unprepare(i2c->clk_dma);
  211. }
  212. static void mtk_i2c_init_hw(struct mtk_i2c *i2c)
  213. {
  214. u16 control_reg;
  215. writew(I2C_SOFT_RST, i2c->base + OFFSET_SOFTRESET);
  216. /* Set ioconfig */
  217. if (i2c->use_push_pull)
  218. writew(I2C_IO_CONFIG_PUSH_PULL, i2c->base + OFFSET_IO_CONFIG);
  219. else
  220. writew(I2C_IO_CONFIG_OPEN_DRAIN, i2c->base + OFFSET_IO_CONFIG);
  221. if (i2c->dev_comp->dcm)
  222. writew(I2C_DCM_DISABLE, i2c->base + OFFSET_DCM_EN);
  223. writew(i2c->timing_reg, i2c->base + OFFSET_TIMING);
  224. writew(i2c->high_speed_reg, i2c->base + OFFSET_HS);
  225. /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
  226. if (i2c->have_pmic)
  227. writew(I2C_CONTROL_WRAPPER, i2c->base + OFFSET_PATH_DIR);
  228. control_reg = I2C_CONTROL_ACKERR_DET_EN |
  229. I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN;
  230. writew(control_reg, i2c->base + OFFSET_CONTROL);
  231. writew(I2C_DELAY_LEN, i2c->base + OFFSET_DELAY_LEN);
  232. writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST);
  233. udelay(50);
  234. writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST);
  235. }
  236. /*
  237. * Calculate i2c port speed
  238. *
  239. * Hardware design:
  240. * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
  241. * clock_div: fixed in hardware, but may be various in different SoCs
  242. *
  243. * The calculation want to pick the highest bus frequency that is still
  244. * less than or equal to i2c->speed_hz. The calculation try to get
  245. * sample_cnt and step_cn
  246. */
  247. static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk,
  248. unsigned int clock_div)
  249. {
  250. unsigned int clk_src;
  251. unsigned int step_cnt;
  252. unsigned int sample_cnt;
  253. unsigned int max_step_cnt;
  254. unsigned int target_speed;
  255. unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV;
  256. unsigned int base_step_cnt;
  257. unsigned int opt_div;
  258. unsigned int best_mul;
  259. unsigned int cnt_mul;
  260. clk_src = parent_clk / clock_div;
  261. target_speed = i2c->speed_hz;
  262. if (target_speed > MAX_HS_MODE_SPEED)
  263. target_speed = MAX_HS_MODE_SPEED;
  264. if (target_speed > MAX_FS_MODE_SPEED)
  265. max_step_cnt = MAX_HS_STEP_CNT_DIV;
  266. else
  267. max_step_cnt = MAX_STEP_CNT_DIV;
  268. base_step_cnt = max_step_cnt;
  269. /* Find the best combination */
  270. opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed);
  271. best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
  272. /* Search for the best pair (sample_cnt, step_cnt) with
  273. * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
  274. * 0 < step_cnt < max_step_cnt
  275. * sample_cnt * step_cnt >= opt_div
  276. * optimizing for sample_cnt * step_cnt being minimal
  277. */
  278. for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
  279. step_cnt = DIV_ROUND_UP(opt_div, sample_cnt);
  280. cnt_mul = step_cnt * sample_cnt;
  281. if (step_cnt > max_step_cnt)
  282. continue;
  283. if (cnt_mul < best_mul) {
  284. best_mul = cnt_mul;
  285. base_sample_cnt = sample_cnt;
  286. base_step_cnt = step_cnt;
  287. if (best_mul == opt_div)
  288. break;
  289. }
  290. }
  291. sample_cnt = base_sample_cnt;
  292. step_cnt = base_step_cnt;
  293. if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) {
  294. /* In this case, hardware can't support such
  295. * low i2c_bus_freq
  296. */
  297. dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n", target_speed);
  298. return -EINVAL;
  299. }
  300. step_cnt--;
  301. sample_cnt--;
  302. if (target_speed > MAX_FS_MODE_SPEED) {
  303. /* Set the high speed mode register */
  304. i2c->timing_reg = I2C_FS_TIME_INIT_VALUE;
  305. i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE |
  306. (sample_cnt << 12) | (step_cnt << 8);
  307. } else {
  308. i2c->timing_reg = (sample_cnt << 8) | (step_cnt << 0);
  309. /* Disable the high speed transaction */
  310. i2c->high_speed_reg = I2C_TIME_CLR_VALUE;
  311. }
  312. return 0;
  313. }
  314. static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
  315. int num, int left_num)
  316. {
  317. u16 addr_reg;
  318. u16 start_reg;
  319. u16 control_reg;
  320. u16 restart_flag = 0;
  321. dma_addr_t rpaddr = 0;
  322. dma_addr_t wpaddr = 0;
  323. int ret;
  324. i2c->irq_stat = 0;
  325. if (i2c->dev_comp->auto_restart)
  326. restart_flag = I2C_RS_TRANSFER;
  327. reinit_completion(&i2c->msg_complete);
  328. control_reg = readw(i2c->base + OFFSET_CONTROL) &
  329. ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS);
  330. if ((i2c->speed_hz > 400000) || (left_num >= 1))
  331. control_reg |= I2C_CONTROL_RS;
  332. if (i2c->op == I2C_MASTER_WRRD)
  333. control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS;
  334. writew(control_reg, i2c->base + OFFSET_CONTROL);
  335. /* set start condition */
  336. if (i2c->speed_hz <= 100000)
  337. writew(I2C_ST_START_CON, i2c->base + OFFSET_EXT_CONF);
  338. else
  339. writew(I2C_FS_START_CON, i2c->base + OFFSET_EXT_CONF);
  340. addr_reg = msgs->addr << 1;
  341. if (i2c->op == I2C_MASTER_RD)
  342. addr_reg |= 0x1;
  343. writew(addr_reg, i2c->base + OFFSET_SLAVE_ADDR);
  344. /* Clear interrupt status */
  345. writew(restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
  346. I2C_TRANSAC_COMP, i2c->base + OFFSET_INTR_STAT);
  347. writew(I2C_FIFO_ADDR_CLR, i2c->base + OFFSET_FIFO_ADDR_CLR);
  348. /* Enable interrupt */
  349. writew(restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
  350. I2C_TRANSAC_COMP, i2c->base + OFFSET_INTR_MASK);
  351. /* Set transfer and transaction len */
  352. if (i2c->op == I2C_MASTER_WRRD) {
  353. writew(msgs->len | ((msgs + 1)->len) << 8,
  354. i2c->base + OFFSET_TRANSFER_LEN);
  355. writew(I2C_WRRD_TRANAC_VALUE, i2c->base + OFFSET_TRANSAC_LEN);
  356. } else {
  357. writew(msgs->len, i2c->base + OFFSET_TRANSFER_LEN);
  358. writew(num, i2c->base + OFFSET_TRANSAC_LEN);
  359. }
  360. /* Prepare buffer data to start transfer */
  361. if (i2c->op == I2C_MASTER_RD) {
  362. writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
  363. writel(I2C_DMA_CON_RX, i2c->pdmabase + OFFSET_CON);
  364. rpaddr = dma_map_single(i2c->dev, msgs->buf,
  365. msgs->len, DMA_FROM_DEVICE);
  366. if (dma_mapping_error(i2c->dev, rpaddr))
  367. return -ENOMEM;
  368. writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
  369. writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN);
  370. } else if (i2c->op == I2C_MASTER_WR) {
  371. writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
  372. writel(I2C_DMA_CON_TX, i2c->pdmabase + OFFSET_CON);
  373. wpaddr = dma_map_single(i2c->dev, msgs->buf,
  374. msgs->len, DMA_TO_DEVICE);
  375. if (dma_mapping_error(i2c->dev, wpaddr))
  376. return -ENOMEM;
  377. writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
  378. writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
  379. } else {
  380. writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG);
  381. writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_CON);
  382. wpaddr = dma_map_single(i2c->dev, msgs->buf,
  383. msgs->len, DMA_TO_DEVICE);
  384. if (dma_mapping_error(i2c->dev, wpaddr))
  385. return -ENOMEM;
  386. rpaddr = dma_map_single(i2c->dev, (msgs + 1)->buf,
  387. (msgs + 1)->len,
  388. DMA_FROM_DEVICE);
  389. if (dma_mapping_error(i2c->dev, rpaddr)) {
  390. dma_unmap_single(i2c->dev, wpaddr,
  391. msgs->len, DMA_TO_DEVICE);
  392. return -ENOMEM;
  393. }
  394. writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
  395. writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
  396. writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
  397. writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN);
  398. }
  399. writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN);
  400. if (!i2c->dev_comp->auto_restart) {
  401. start_reg = I2C_TRANSAC_START;
  402. } else {
  403. start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG;
  404. if (left_num >= 1)
  405. start_reg |= I2C_RS_MUL_CNFG;
  406. }
  407. writew(start_reg, i2c->base + OFFSET_START);
  408. ret = wait_for_completion_timeout(&i2c->msg_complete,
  409. i2c->adap.timeout);
  410. /* Clear interrupt mask */
  411. writew(~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
  412. I2C_TRANSAC_COMP), i2c->base + OFFSET_INTR_MASK);
  413. if (i2c->op == I2C_MASTER_WR) {
  414. dma_unmap_single(i2c->dev, wpaddr,
  415. msgs->len, DMA_TO_DEVICE);
  416. } else if (i2c->op == I2C_MASTER_RD) {
  417. dma_unmap_single(i2c->dev, rpaddr,
  418. msgs->len, DMA_FROM_DEVICE);
  419. } else {
  420. dma_unmap_single(i2c->dev, wpaddr, msgs->len,
  421. DMA_TO_DEVICE);
  422. dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len,
  423. DMA_FROM_DEVICE);
  424. }
  425. if (ret == 0) {
  426. dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr);
  427. mtk_i2c_init_hw(i2c);
  428. return -ETIMEDOUT;
  429. }
  430. completion_done(&i2c->msg_complete);
  431. if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) {
  432. dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr);
  433. mtk_i2c_init_hw(i2c);
  434. return -ENXIO;
  435. }
  436. return 0;
  437. }
  438. static int mtk_i2c_transfer(struct i2c_adapter *adap,
  439. struct i2c_msg msgs[], int num)
  440. {
  441. int ret;
  442. int left_num = num;
  443. struct mtk_i2c *i2c = i2c_get_adapdata(adap);
  444. ret = mtk_i2c_clock_enable(i2c);
  445. if (ret)
  446. return ret;
  447. while (left_num--) {
  448. if (!msgs->buf) {
  449. dev_dbg(i2c->dev, "data buffer is NULL.\n");
  450. ret = -EINVAL;
  451. goto err_exit;
  452. }
  453. if (msgs->flags & I2C_M_RD)
  454. i2c->op = I2C_MASTER_RD;
  455. else
  456. i2c->op = I2C_MASTER_WR;
  457. if (!i2c->dev_comp->auto_restart) {
  458. if (num > 1) {
  459. /* combined two messages into one transaction */
  460. i2c->op = I2C_MASTER_WRRD;
  461. left_num--;
  462. }
  463. }
  464. /* always use DMA mode. */
  465. ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num);
  466. if (ret < 0)
  467. goto err_exit;
  468. msgs++;
  469. }
  470. /* the return value is number of executed messages */
  471. ret = num;
  472. err_exit:
  473. mtk_i2c_clock_disable(i2c);
  474. return ret;
  475. }
  476. static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id)
  477. {
  478. struct mtk_i2c *i2c = dev_id;
  479. u16 restart_flag = 0;
  480. u16 intr_stat;
  481. if (i2c->dev_comp->auto_restart)
  482. restart_flag = I2C_RS_TRANSFER;
  483. intr_stat = readw(i2c->base + OFFSET_INTR_STAT);
  484. writew(intr_stat, i2c->base + OFFSET_INTR_STAT);
  485. /*
  486. * when occurs ack error, i2c controller generate two interrupts
  487. * first is the ack error interrupt, then the complete interrupt
  488. * i2c->irq_stat need keep the two interrupt value.
  489. */
  490. i2c->irq_stat |= intr_stat;
  491. if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag))
  492. complete(&i2c->msg_complete);
  493. return IRQ_HANDLED;
  494. }
  495. static u32 mtk_i2c_functionality(struct i2c_adapter *adap)
  496. {
  497. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  498. }
  499. static const struct i2c_algorithm mtk_i2c_algorithm = {
  500. .master_xfer = mtk_i2c_transfer,
  501. .functionality = mtk_i2c_functionality,
  502. };
  503. static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c,
  504. unsigned int *clk_src_div)
  505. {
  506. int ret;
  507. ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz);
  508. if (ret < 0)
  509. i2c->speed_hz = I2C_DEFAULT_SPEED;
  510. ret = of_property_read_u32(np, "clock-div", clk_src_div);
  511. if (ret < 0)
  512. return ret;
  513. if (*clk_src_div == 0)
  514. return -EINVAL;
  515. i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic");
  516. i2c->use_push_pull =
  517. of_property_read_bool(np, "mediatek,use-push-pull");
  518. return 0;
  519. }
  520. static int mtk_i2c_probe(struct platform_device *pdev)
  521. {
  522. const struct of_device_id *of_id;
  523. int ret = 0;
  524. struct mtk_i2c *i2c;
  525. struct clk *clk;
  526. unsigned int clk_src_div;
  527. struct resource *res;
  528. int irq;
  529. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  530. if (!i2c)
  531. return -ENOMEM;
  532. ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c, &clk_src_div);
  533. if (ret)
  534. return -EINVAL;
  535. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  536. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  537. if (IS_ERR(i2c->base))
  538. return PTR_ERR(i2c->base);
  539. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  540. i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res);
  541. if (IS_ERR(i2c->pdmabase))
  542. return PTR_ERR(i2c->pdmabase);
  543. irq = platform_get_irq(pdev, 0);
  544. if (irq <= 0)
  545. return irq;
  546. init_completion(&i2c->msg_complete);
  547. of_id = of_match_node(mtk_i2c_of_match, pdev->dev.of_node);
  548. if (!of_id)
  549. return -EINVAL;
  550. i2c->dev_comp = of_id->data;
  551. i2c->adap.dev.of_node = pdev->dev.of_node;
  552. i2c->dev = &pdev->dev;
  553. i2c->adap.dev.parent = &pdev->dev;
  554. i2c->adap.owner = THIS_MODULE;
  555. i2c->adap.algo = &mtk_i2c_algorithm;
  556. i2c->adap.quirks = i2c->dev_comp->quirks;
  557. i2c->adap.timeout = 2 * HZ;
  558. i2c->adap.retries = 1;
  559. if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c)
  560. return -EINVAL;
  561. i2c->clk_main = devm_clk_get(&pdev->dev, "main");
  562. if (IS_ERR(i2c->clk_main)) {
  563. dev_err(&pdev->dev, "cannot get main clock\n");
  564. return PTR_ERR(i2c->clk_main);
  565. }
  566. i2c->clk_dma = devm_clk_get(&pdev->dev, "dma");
  567. if (IS_ERR(i2c->clk_dma)) {
  568. dev_err(&pdev->dev, "cannot get dma clock\n");
  569. return PTR_ERR(i2c->clk_dma);
  570. }
  571. clk = i2c->clk_main;
  572. if (i2c->have_pmic) {
  573. i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic");
  574. if (IS_ERR(i2c->clk_pmic)) {
  575. dev_err(&pdev->dev, "cannot get pmic clock\n");
  576. return PTR_ERR(i2c->clk_pmic);
  577. }
  578. clk = i2c->clk_pmic;
  579. }
  580. strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name));
  581. ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk), clk_src_div);
  582. if (ret) {
  583. dev_err(&pdev->dev, "Failed to set the speed.\n");
  584. return -EINVAL;
  585. }
  586. ret = mtk_i2c_clock_enable(i2c);
  587. if (ret) {
  588. dev_err(&pdev->dev, "clock enable failed!\n");
  589. return ret;
  590. }
  591. mtk_i2c_init_hw(i2c);
  592. mtk_i2c_clock_disable(i2c);
  593. ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq,
  594. IRQF_TRIGGER_NONE, I2C_DRV_NAME, i2c);
  595. if (ret < 0) {
  596. dev_err(&pdev->dev,
  597. "Request I2C IRQ %d fail\n", irq);
  598. return ret;
  599. }
  600. i2c_set_adapdata(&i2c->adap, i2c);
  601. ret = i2c_add_adapter(&i2c->adap);
  602. if (ret) {
  603. dev_err(&pdev->dev, "Failed to add i2c bus to i2c core\n");
  604. return ret;
  605. }
  606. platform_set_drvdata(pdev, i2c);
  607. return 0;
  608. }
  609. static int mtk_i2c_remove(struct platform_device *pdev)
  610. {
  611. struct mtk_i2c *i2c = platform_get_drvdata(pdev);
  612. i2c_del_adapter(&i2c->adap);
  613. return 0;
  614. }
  615. #ifdef CONFIG_PM_SLEEP
  616. static int mtk_i2c_resume(struct device *dev)
  617. {
  618. struct mtk_i2c *i2c = dev_get_drvdata(dev);
  619. mtk_i2c_init_hw(i2c);
  620. return 0;
  621. }
  622. #endif
  623. static const struct dev_pm_ops mtk_i2c_pm = {
  624. SET_SYSTEM_SLEEP_PM_OPS(NULL, mtk_i2c_resume)
  625. };
  626. static struct platform_driver mtk_i2c_driver = {
  627. .probe = mtk_i2c_probe,
  628. .remove = mtk_i2c_remove,
  629. .driver = {
  630. .name = I2C_DRV_NAME,
  631. .pm = &mtk_i2c_pm,
  632. .of_match_table = of_match_ptr(mtk_i2c_of_match),
  633. },
  634. };
  635. module_platform_driver(mtk_i2c_driver);
  636. MODULE_LICENSE("GPL v2");
  637. MODULE_DESCRIPTION("MediaTek I2C Bus Driver");
  638. MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");