i2c-imx.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2002 Motorola GSG-China
  4. *
  5. * Author:
  6. * Darius Augulis, Teltonika Inc.
  7. *
  8. * Desc.:
  9. * Implementation of I2C Adapter/Algorithm Driver
  10. * for I2C Bus integrated in Freescale i.MX/MXC processors
  11. *
  12. * Derived from Motorola GSG China I2C example driver
  13. *
  14. * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de
  15. * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de
  16. * Copyright (C) 2007 RightHand Technologies, Inc.
  17. * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
  18. *
  19. * Copyright 2013 Freescale Semiconductor, Inc.
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/completion.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/err.h>
  29. #include <linux/errno.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/i2c.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_dma.h>
  40. #include <linux/pinctrl/consumer.h>
  41. #include <linux/platform_data/i2c-imx.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/sched.h>
  45. #include <linux/slab.h>
  46. /* This will be the driver name the kernel reports */
  47. #define DRIVER_NAME "imx-i2c"
  48. /* Default value */
  49. #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
  50. /*
  51. * Enable DMA if transfer byte size is bigger than this threshold.
  52. * As the hardware request, it must bigger than 4 bytes.\
  53. * I have set '16' here, maybe it's not the best but I think it's
  54. * the appropriate.
  55. */
  56. #define DMA_THRESHOLD 16
  57. #define DMA_TIMEOUT 1000
  58. /* IMX I2C registers:
  59. * the I2C register offset is different between SoCs,
  60. * to provid support for all these chips, split the
  61. * register offset into a fixed base address and a
  62. * variable shift value, then the full register offset
  63. * will be calculated by
  64. * reg_off = ( reg_base_addr << reg_shift)
  65. */
  66. #define IMX_I2C_IADR 0x00 /* i2c slave address */
  67. #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
  68. #define IMX_I2C_I2CR 0x02 /* i2c control */
  69. #define IMX_I2C_I2SR 0x03 /* i2c status */
  70. #define IMX_I2C_I2DR 0x04 /* i2c transfer data */
  71. #define IMX_I2C_REGSHIFT 2
  72. #define VF610_I2C_REGSHIFT 0
  73. /* Bits of IMX I2C registers */
  74. #define I2SR_RXAK 0x01
  75. #define I2SR_IIF 0x02
  76. #define I2SR_SRW 0x04
  77. #define I2SR_IAL 0x10
  78. #define I2SR_IBB 0x20
  79. #define I2SR_IAAS 0x40
  80. #define I2SR_ICF 0x80
  81. #define I2CR_DMAEN 0x02
  82. #define I2CR_RSTA 0x04
  83. #define I2CR_TXAK 0x08
  84. #define I2CR_MTX 0x10
  85. #define I2CR_MSTA 0x20
  86. #define I2CR_IIEN 0x40
  87. #define I2CR_IEN 0x80
  88. /* register bits different operating codes definition:
  89. * 1) I2SR: Interrupt flags clear operation differ between SoCs:
  90. * - write zero to clear(w0c) INT flag on i.MX,
  91. * - but write one to clear(w1c) INT flag on Vybrid.
  92. * 2) I2CR: I2C module enable operation also differ between SoCs:
  93. * - set I2CR_IEN bit enable the module on i.MX,
  94. * - but clear I2CR_IEN bit enable the module on Vybrid.
  95. */
  96. #define I2SR_CLR_OPCODE_W0C 0x0
  97. #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
  98. #define I2CR_IEN_OPCODE_0 0x0
  99. #define I2CR_IEN_OPCODE_1 I2CR_IEN
  100. #define I2C_PM_TIMEOUT 10 /* ms */
  101. /*
  102. * sorted list of clock divider, register value pairs
  103. * taken from table 26-5, p.26-9, Freescale i.MX
  104. * Integrated Portable System Processor Reference Manual
  105. * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
  106. *
  107. * Duplicated divider values removed from list
  108. */
  109. struct imx_i2c_clk_pair {
  110. u16 div;
  111. u16 val;
  112. };
  113. static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
  114. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  115. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  116. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  117. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  118. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  119. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  120. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  121. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  122. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  123. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  124. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  125. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  126. { 3072, 0x1E }, { 3840, 0x1F }
  127. };
  128. /* Vybrid VF610 clock divider, register value pairs */
  129. static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
  130. { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
  131. { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
  132. { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
  133. { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
  134. { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
  135. { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
  136. { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
  137. { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
  138. { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
  139. { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
  140. { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
  141. { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
  142. { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
  143. { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
  144. { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
  145. };
  146. enum imx_i2c_type {
  147. IMX1_I2C,
  148. IMX21_I2C,
  149. VF610_I2C,
  150. };
  151. struct imx_i2c_hwdata {
  152. enum imx_i2c_type devtype;
  153. unsigned regshift;
  154. struct imx_i2c_clk_pair *clk_div;
  155. unsigned ndivs;
  156. unsigned i2sr_clr_opcode;
  157. unsigned i2cr_ien_opcode;
  158. };
  159. struct imx_i2c_dma {
  160. struct dma_chan *chan_tx;
  161. struct dma_chan *chan_rx;
  162. struct dma_chan *chan_using;
  163. struct completion cmd_complete;
  164. dma_addr_t dma_buf;
  165. unsigned int dma_len;
  166. enum dma_transfer_direction dma_transfer_dir;
  167. enum dma_data_direction dma_data_dir;
  168. };
  169. struct imx_i2c_struct {
  170. struct i2c_adapter adapter;
  171. struct clk *clk;
  172. struct notifier_block clk_change_nb;
  173. void __iomem *base;
  174. wait_queue_head_t queue;
  175. unsigned long i2csr;
  176. unsigned int disable_delay;
  177. int stopped;
  178. unsigned int ifdr; /* IMX_I2C_IFDR */
  179. unsigned int cur_clk;
  180. unsigned int bitrate;
  181. const struct imx_i2c_hwdata *hwdata;
  182. struct i2c_bus_recovery_info rinfo;
  183. struct pinctrl *pinctrl;
  184. struct pinctrl_state *pinctrl_pins_default;
  185. struct pinctrl_state *pinctrl_pins_gpio;
  186. struct imx_i2c_dma *dma;
  187. };
  188. static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
  189. .devtype = IMX1_I2C,
  190. .regshift = IMX_I2C_REGSHIFT,
  191. .clk_div = imx_i2c_clk_div,
  192. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  193. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  194. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  195. };
  196. static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
  197. .devtype = IMX21_I2C,
  198. .regshift = IMX_I2C_REGSHIFT,
  199. .clk_div = imx_i2c_clk_div,
  200. .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
  201. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
  202. .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
  203. };
  204. static struct imx_i2c_hwdata vf610_i2c_hwdata = {
  205. .devtype = VF610_I2C,
  206. .regshift = VF610_I2C_REGSHIFT,
  207. .clk_div = vf610_i2c_clk_div,
  208. .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
  209. .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
  210. .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
  211. };
  212. static const struct platform_device_id imx_i2c_devtype[] = {
  213. {
  214. .name = "imx1-i2c",
  215. .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
  216. }, {
  217. .name = "imx21-i2c",
  218. .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
  219. }, {
  220. /* sentinel */
  221. }
  222. };
  223. MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
  224. static const struct of_device_id i2c_imx_dt_ids[] = {
  225. { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
  226. { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
  227. { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
  228. { /* sentinel */ }
  229. };
  230. MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
  231. static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
  232. {
  233. return i2c_imx->hwdata->devtype == IMX1_I2C;
  234. }
  235. static inline void imx_i2c_write_reg(unsigned int val,
  236. struct imx_i2c_struct *i2c_imx, unsigned int reg)
  237. {
  238. writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  239. }
  240. static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
  241. unsigned int reg)
  242. {
  243. return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
  244. }
  245. /* Functions for DMA support */
  246. static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
  247. dma_addr_t phy_addr)
  248. {
  249. struct imx_i2c_dma *dma;
  250. struct dma_slave_config dma_sconfig;
  251. struct device *dev = &i2c_imx->adapter.dev;
  252. int ret;
  253. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  254. if (!dma)
  255. return;
  256. dma->chan_tx = dma_request_slave_channel(dev, "tx");
  257. if (!dma->chan_tx) {
  258. dev_dbg(dev, "can't request DMA tx channel\n");
  259. goto fail_al;
  260. }
  261. dma_sconfig.dst_addr = phy_addr +
  262. (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
  263. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  264. dma_sconfig.dst_maxburst = 1;
  265. dma_sconfig.direction = DMA_MEM_TO_DEV;
  266. ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
  267. if (ret < 0) {
  268. dev_dbg(dev, "can't configure tx channel\n");
  269. goto fail_tx;
  270. }
  271. dma->chan_rx = dma_request_slave_channel(dev, "rx");
  272. if (!dma->chan_rx) {
  273. dev_dbg(dev, "can't request DMA rx channel\n");
  274. goto fail_tx;
  275. }
  276. dma_sconfig.src_addr = phy_addr +
  277. (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
  278. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  279. dma_sconfig.src_maxburst = 1;
  280. dma_sconfig.direction = DMA_DEV_TO_MEM;
  281. ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
  282. if (ret < 0) {
  283. dev_dbg(dev, "can't configure rx channel\n");
  284. goto fail_rx;
  285. }
  286. i2c_imx->dma = dma;
  287. init_completion(&dma->cmd_complete);
  288. dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  289. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  290. return;
  291. fail_rx:
  292. dma_release_channel(dma->chan_rx);
  293. fail_tx:
  294. dma_release_channel(dma->chan_tx);
  295. fail_al:
  296. devm_kfree(dev, dma);
  297. dev_info(dev, "can't use DMA, using PIO instead.\n");
  298. }
  299. static void i2c_imx_dma_callback(void *arg)
  300. {
  301. struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
  302. struct imx_i2c_dma *dma = i2c_imx->dma;
  303. dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
  304. dma->dma_len, dma->dma_data_dir);
  305. complete(&dma->cmd_complete);
  306. }
  307. static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
  308. struct i2c_msg *msgs)
  309. {
  310. struct imx_i2c_dma *dma = i2c_imx->dma;
  311. struct dma_async_tx_descriptor *txdesc;
  312. struct device *dev = &i2c_imx->adapter.dev;
  313. struct device *chan_dev = dma->chan_using->device->dev;
  314. dma->dma_buf = dma_map_single(chan_dev, msgs->buf,
  315. dma->dma_len, dma->dma_data_dir);
  316. if (dma_mapping_error(chan_dev, dma->dma_buf)) {
  317. dev_err(dev, "DMA mapping failed\n");
  318. goto err_map;
  319. }
  320. txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
  321. dma->dma_len, dma->dma_transfer_dir,
  322. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  323. if (!txdesc) {
  324. dev_err(dev, "Not able to get desc for DMA xfer\n");
  325. goto err_desc;
  326. }
  327. reinit_completion(&dma->cmd_complete);
  328. txdesc->callback = i2c_imx_dma_callback;
  329. txdesc->callback_param = i2c_imx;
  330. if (dma_submit_error(dmaengine_submit(txdesc))) {
  331. dev_err(dev, "DMA submit failed\n");
  332. goto err_submit;
  333. }
  334. dma_async_issue_pending(dma->chan_using);
  335. return 0;
  336. err_submit:
  337. dmaengine_terminate_all(dma->chan_using);
  338. err_desc:
  339. dma_unmap_single(chan_dev, dma->dma_buf,
  340. dma->dma_len, dma->dma_data_dir);
  341. err_map:
  342. return -EINVAL;
  343. }
  344. static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
  345. {
  346. struct imx_i2c_dma *dma = i2c_imx->dma;
  347. dma->dma_buf = 0;
  348. dma->dma_len = 0;
  349. dma_release_channel(dma->chan_tx);
  350. dma->chan_tx = NULL;
  351. dma_release_channel(dma->chan_rx);
  352. dma->chan_rx = NULL;
  353. dma->chan_using = NULL;
  354. }
  355. static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
  356. {
  357. unsigned long orig_jiffies = jiffies;
  358. unsigned int temp;
  359. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  360. while (1) {
  361. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  362. /* check for arbitration lost */
  363. if (temp & I2SR_IAL) {
  364. temp &= ~I2SR_IAL;
  365. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  366. return -EAGAIN;
  367. }
  368. if (for_busy && (temp & I2SR_IBB)) {
  369. i2c_imx->stopped = 0;
  370. break;
  371. }
  372. if (!for_busy && !(temp & I2SR_IBB)) {
  373. i2c_imx->stopped = 1;
  374. break;
  375. }
  376. if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
  377. dev_dbg(&i2c_imx->adapter.dev,
  378. "<%s> I2C bus is busy\n", __func__);
  379. return -ETIMEDOUT;
  380. }
  381. schedule();
  382. }
  383. return 0;
  384. }
  385. static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
  386. {
  387. wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
  388. if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
  389. dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
  390. return -ETIMEDOUT;
  391. }
  392. dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
  393. i2c_imx->i2csr = 0;
  394. return 0;
  395. }
  396. static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
  397. {
  398. if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
  399. dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
  400. return -ENXIO; /* No ACK */
  401. }
  402. dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
  403. return 0;
  404. }
  405. static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
  406. unsigned int i2c_clk_rate)
  407. {
  408. struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
  409. unsigned int div;
  410. int i;
  411. /* Divider value calculation */
  412. if (i2c_imx->cur_clk == i2c_clk_rate)
  413. return;
  414. i2c_imx->cur_clk = i2c_clk_rate;
  415. div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
  416. if (div < i2c_clk_div[0].div)
  417. i = 0;
  418. else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
  419. i = i2c_imx->hwdata->ndivs - 1;
  420. else
  421. for (i = 0; i2c_clk_div[i].div < div; i++)
  422. ;
  423. /* Store divider value */
  424. i2c_imx->ifdr = i2c_clk_div[i].val;
  425. /*
  426. * There dummy delay is calculated.
  427. * It should be about one I2C clock period long.
  428. * This delay is used in I2C bus disable function
  429. * to fix chip hardware bug.
  430. */
  431. i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
  432. + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
  433. #ifdef CONFIG_I2C_DEBUG_BUS
  434. dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
  435. i2c_clk_rate, div);
  436. dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
  437. i2c_clk_div[i].val, i2c_clk_div[i].div);
  438. #endif
  439. }
  440. static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
  441. unsigned long action, void *data)
  442. {
  443. struct clk_notifier_data *ndata = data;
  444. struct imx_i2c_struct *i2c_imx = container_of(&ndata->clk,
  445. struct imx_i2c_struct,
  446. clk);
  447. if (action & POST_RATE_CHANGE)
  448. i2c_imx_set_clk(i2c_imx, ndata->new_rate);
  449. return NOTIFY_OK;
  450. }
  451. static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
  452. {
  453. unsigned int temp = 0;
  454. int result;
  455. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  456. imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
  457. /* Enable I2C controller */
  458. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  459. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
  460. /* Wait controller to be stable */
  461. usleep_range(50, 150);
  462. /* Start I2C transaction */
  463. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  464. temp |= I2CR_MSTA;
  465. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  466. result = i2c_imx_bus_busy(i2c_imx, 1);
  467. if (result)
  468. return result;
  469. temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
  470. temp &= ~I2CR_DMAEN;
  471. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  472. return result;
  473. }
  474. static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
  475. {
  476. unsigned int temp = 0;
  477. if (!i2c_imx->stopped) {
  478. /* Stop I2C transaction */
  479. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  480. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  481. temp &= ~(I2CR_MSTA | I2CR_MTX);
  482. if (i2c_imx->dma)
  483. temp &= ~I2CR_DMAEN;
  484. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  485. }
  486. if (is_imx1_i2c(i2c_imx)) {
  487. /*
  488. * This delay caused by an i.MXL hardware bug.
  489. * If no (or too short) delay, no "STOP" bit will be generated.
  490. */
  491. udelay(i2c_imx->disable_delay);
  492. }
  493. if (!i2c_imx->stopped)
  494. i2c_imx_bus_busy(i2c_imx, 0);
  495. /* Disable I2C controller */
  496. temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  497. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  498. }
  499. static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
  500. {
  501. struct imx_i2c_struct *i2c_imx = dev_id;
  502. unsigned int temp;
  503. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  504. if (temp & I2SR_IIF) {
  505. /* save status register */
  506. i2c_imx->i2csr = temp;
  507. temp &= ~I2SR_IIF;
  508. temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
  509. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
  510. wake_up(&i2c_imx->queue);
  511. return IRQ_HANDLED;
  512. }
  513. return IRQ_NONE;
  514. }
  515. static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
  516. struct i2c_msg *msgs)
  517. {
  518. int result;
  519. unsigned long time_left;
  520. unsigned int temp = 0;
  521. unsigned long orig_jiffies = jiffies;
  522. struct imx_i2c_dma *dma = i2c_imx->dma;
  523. struct device *dev = &i2c_imx->adapter.dev;
  524. dma->chan_using = dma->chan_tx;
  525. dma->dma_transfer_dir = DMA_MEM_TO_DEV;
  526. dma->dma_data_dir = DMA_TO_DEVICE;
  527. dma->dma_len = msgs->len - 1;
  528. result = i2c_imx_dma_xfer(i2c_imx, msgs);
  529. if (result)
  530. return result;
  531. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  532. temp |= I2CR_DMAEN;
  533. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  534. /*
  535. * Write slave address.
  536. * The first byte must be transmitted by the CPU.
  537. */
  538. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  539. time_left = wait_for_completion_timeout(
  540. &i2c_imx->dma->cmd_complete,
  541. msecs_to_jiffies(DMA_TIMEOUT));
  542. if (time_left == 0) {
  543. dmaengine_terminate_all(dma->chan_using);
  544. return -ETIMEDOUT;
  545. }
  546. /* Waiting for transfer complete. */
  547. while (1) {
  548. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  549. if (temp & I2SR_ICF)
  550. break;
  551. if (time_after(jiffies, orig_jiffies +
  552. msecs_to_jiffies(DMA_TIMEOUT))) {
  553. dev_dbg(dev, "<%s> Timeout\n", __func__);
  554. return -ETIMEDOUT;
  555. }
  556. schedule();
  557. }
  558. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  559. temp &= ~I2CR_DMAEN;
  560. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  561. /* The last data byte must be transferred by the CPU. */
  562. imx_i2c_write_reg(msgs->buf[msgs->len-1],
  563. i2c_imx, IMX_I2C_I2DR);
  564. result = i2c_imx_trx_complete(i2c_imx);
  565. if (result)
  566. return result;
  567. return i2c_imx_acked(i2c_imx);
  568. }
  569. static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
  570. struct i2c_msg *msgs, bool is_lastmsg)
  571. {
  572. int result;
  573. unsigned long time_left;
  574. unsigned int temp;
  575. unsigned long orig_jiffies = jiffies;
  576. struct imx_i2c_dma *dma = i2c_imx->dma;
  577. struct device *dev = &i2c_imx->adapter.dev;
  578. dma->chan_using = dma->chan_rx;
  579. dma->dma_transfer_dir = DMA_DEV_TO_MEM;
  580. dma->dma_data_dir = DMA_FROM_DEVICE;
  581. /* The last two data bytes must be transferred by the CPU. */
  582. dma->dma_len = msgs->len - 2;
  583. result = i2c_imx_dma_xfer(i2c_imx, msgs);
  584. if (result)
  585. return result;
  586. time_left = wait_for_completion_timeout(
  587. &i2c_imx->dma->cmd_complete,
  588. msecs_to_jiffies(DMA_TIMEOUT));
  589. if (time_left == 0) {
  590. dmaengine_terminate_all(dma->chan_using);
  591. return -ETIMEDOUT;
  592. }
  593. /* waiting for transfer complete. */
  594. while (1) {
  595. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  596. if (temp & I2SR_ICF)
  597. break;
  598. if (time_after(jiffies, orig_jiffies +
  599. msecs_to_jiffies(DMA_TIMEOUT))) {
  600. dev_dbg(dev, "<%s> Timeout\n", __func__);
  601. return -ETIMEDOUT;
  602. }
  603. schedule();
  604. }
  605. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  606. temp &= ~I2CR_DMAEN;
  607. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  608. /* read n-1 byte data */
  609. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  610. temp |= I2CR_TXAK;
  611. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  612. msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  613. /* read n byte data */
  614. result = i2c_imx_trx_complete(i2c_imx);
  615. if (result)
  616. return result;
  617. if (is_lastmsg) {
  618. /*
  619. * It must generate STOP before read I2DR to prevent
  620. * controller from generating another clock cycle
  621. */
  622. dev_dbg(dev, "<%s> clear MSTA\n", __func__);
  623. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  624. temp &= ~(I2CR_MSTA | I2CR_MTX);
  625. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  626. i2c_imx_bus_busy(i2c_imx, 0);
  627. } else {
  628. /*
  629. * For i2c master receiver repeat restart operation like:
  630. * read -> repeat MSTA -> read/write
  631. * The controller must set MTX before read the last byte in
  632. * the first read operation, otherwise the first read cost
  633. * one extra clock cycle.
  634. */
  635. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  636. temp |= I2CR_MTX;
  637. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  638. }
  639. msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  640. return 0;
  641. }
  642. static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
  643. {
  644. int i, result;
  645. dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
  646. __func__, i2c_8bit_addr_from_msg(msgs));
  647. /* write slave address */
  648. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  649. result = i2c_imx_trx_complete(i2c_imx);
  650. if (result)
  651. return result;
  652. result = i2c_imx_acked(i2c_imx);
  653. if (result)
  654. return result;
  655. dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
  656. /* write data */
  657. for (i = 0; i < msgs->len; i++) {
  658. dev_dbg(&i2c_imx->adapter.dev,
  659. "<%s> write byte: B%d=0x%X\n",
  660. __func__, i, msgs->buf[i]);
  661. imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
  662. result = i2c_imx_trx_complete(i2c_imx);
  663. if (result)
  664. return result;
  665. result = i2c_imx_acked(i2c_imx);
  666. if (result)
  667. return result;
  668. }
  669. return 0;
  670. }
  671. static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
  672. {
  673. int i, result;
  674. unsigned int temp;
  675. int block_data = msgs->flags & I2C_M_RECV_LEN;
  676. int use_dma = i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data;
  677. dev_dbg(&i2c_imx->adapter.dev,
  678. "<%s> write slave address: addr=0x%x\n",
  679. __func__, i2c_8bit_addr_from_msg(msgs));
  680. /* write slave address */
  681. imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
  682. result = i2c_imx_trx_complete(i2c_imx);
  683. if (result)
  684. return result;
  685. result = i2c_imx_acked(i2c_imx);
  686. if (result)
  687. return result;
  688. dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
  689. /* setup bus to read data */
  690. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  691. temp &= ~I2CR_MTX;
  692. /*
  693. * Reset the I2CR_TXAK flag initially for SMBus block read since the
  694. * length is unknown
  695. */
  696. if ((msgs->len - 1) || block_data)
  697. temp &= ~I2CR_TXAK;
  698. if (use_dma)
  699. temp |= I2CR_DMAEN;
  700. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  701. imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
  702. dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
  703. if (use_dma)
  704. return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
  705. /* read data */
  706. for (i = 0; i < msgs->len; i++) {
  707. u8 len = 0;
  708. result = i2c_imx_trx_complete(i2c_imx);
  709. if (result)
  710. return result;
  711. /*
  712. * First byte is the length of remaining packet
  713. * in the SMBus block data read. Add it to
  714. * msgs->len.
  715. */
  716. if ((!i) && block_data) {
  717. len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  718. if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
  719. return -EPROTO;
  720. dev_dbg(&i2c_imx->adapter.dev,
  721. "<%s> read length: 0x%X\n",
  722. __func__, len);
  723. msgs->len += len;
  724. }
  725. if (i == (msgs->len - 1)) {
  726. if (is_lastmsg) {
  727. /*
  728. * It must generate STOP before read I2DR to prevent
  729. * controller from generating another clock cycle
  730. */
  731. dev_dbg(&i2c_imx->adapter.dev,
  732. "<%s> clear MSTA\n", __func__);
  733. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  734. temp &= ~(I2CR_MSTA | I2CR_MTX);
  735. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  736. i2c_imx_bus_busy(i2c_imx, 0);
  737. } else {
  738. /*
  739. * For i2c master receiver repeat restart operation like:
  740. * read -> repeat MSTA -> read/write
  741. * The controller must set MTX before read the last byte in
  742. * the first read operation, otherwise the first read cost
  743. * one extra clock cycle.
  744. */
  745. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  746. temp |= I2CR_MTX;
  747. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  748. }
  749. } else if (i == (msgs->len - 2)) {
  750. dev_dbg(&i2c_imx->adapter.dev,
  751. "<%s> set TXAK\n", __func__);
  752. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  753. temp |= I2CR_TXAK;
  754. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  755. }
  756. if ((!i) && block_data)
  757. msgs->buf[0] = len;
  758. else
  759. msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
  760. dev_dbg(&i2c_imx->adapter.dev,
  761. "<%s> read byte: B%d=0x%X\n",
  762. __func__, i, msgs->buf[i]);
  763. }
  764. return 0;
  765. }
  766. static int i2c_imx_xfer(struct i2c_adapter *adapter,
  767. struct i2c_msg *msgs, int num)
  768. {
  769. unsigned int i, temp;
  770. int result;
  771. bool is_lastmsg = false;
  772. struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
  773. dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
  774. result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
  775. if (result < 0)
  776. goto out;
  777. /* Start I2C transfer */
  778. result = i2c_imx_start(i2c_imx);
  779. if (result) {
  780. if (i2c_imx->adapter.bus_recovery_info) {
  781. i2c_recover_bus(&i2c_imx->adapter);
  782. result = i2c_imx_start(i2c_imx);
  783. }
  784. }
  785. if (result)
  786. goto fail0;
  787. /* read/write data */
  788. for (i = 0; i < num; i++) {
  789. if (i == num - 1)
  790. is_lastmsg = true;
  791. if (i) {
  792. dev_dbg(&i2c_imx->adapter.dev,
  793. "<%s> repeated start\n", __func__);
  794. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  795. temp |= I2CR_RSTA;
  796. imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
  797. result = i2c_imx_bus_busy(i2c_imx, 1);
  798. if (result)
  799. goto fail0;
  800. }
  801. dev_dbg(&i2c_imx->adapter.dev,
  802. "<%s> transfer message: %d\n", __func__, i);
  803. /* write/read data */
  804. #ifdef CONFIG_I2C_DEBUG_BUS
  805. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
  806. dev_dbg(&i2c_imx->adapter.dev,
  807. "<%s> CONTROL: IEN=%d, IIEN=%d, MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n",
  808. __func__,
  809. (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
  810. (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
  811. (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
  812. temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
  813. dev_dbg(&i2c_imx->adapter.dev,
  814. "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n",
  815. __func__,
  816. (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
  817. (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
  818. (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
  819. (temp & I2SR_RXAK ? 1 : 0));
  820. #endif
  821. if (msgs[i].flags & I2C_M_RD)
  822. result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
  823. else {
  824. if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
  825. result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
  826. else
  827. result = i2c_imx_write(i2c_imx, &msgs[i]);
  828. }
  829. if (result)
  830. goto fail0;
  831. }
  832. fail0:
  833. /* Stop I2C transfer */
  834. i2c_imx_stop(i2c_imx);
  835. pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent);
  836. pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent);
  837. out:
  838. dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
  839. (result < 0) ? "error" : "success msg",
  840. (result < 0) ? result : num);
  841. return (result < 0) ? result : num;
  842. }
  843. static void i2c_imx_prepare_recovery(struct i2c_adapter *adap)
  844. {
  845. struct imx_i2c_struct *i2c_imx;
  846. i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
  847. pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_gpio);
  848. }
  849. static void i2c_imx_unprepare_recovery(struct i2c_adapter *adap)
  850. {
  851. struct imx_i2c_struct *i2c_imx;
  852. i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
  853. pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_default);
  854. }
  855. /*
  856. * We switch SCL and SDA to their GPIO function and do some bitbanging
  857. * for bus recovery. These alternative pinmux settings can be
  858. * described in the device tree by a separate pinctrl state "gpio". If
  859. * this is missing this is not a big problem, the only implication is
  860. * that we can't do bus recovery.
  861. */
  862. static int i2c_imx_init_recovery_info(struct imx_i2c_struct *i2c_imx,
  863. struct platform_device *pdev)
  864. {
  865. struct i2c_bus_recovery_info *rinfo = &i2c_imx->rinfo;
  866. i2c_imx->pinctrl = devm_pinctrl_get(&pdev->dev);
  867. if (!i2c_imx->pinctrl || IS_ERR(i2c_imx->pinctrl)) {
  868. dev_info(&pdev->dev, "can't get pinctrl, bus recovery not supported\n");
  869. return PTR_ERR(i2c_imx->pinctrl);
  870. }
  871. i2c_imx->pinctrl_pins_default = pinctrl_lookup_state(i2c_imx->pinctrl,
  872. PINCTRL_STATE_DEFAULT);
  873. i2c_imx->pinctrl_pins_gpio = pinctrl_lookup_state(i2c_imx->pinctrl,
  874. "gpio");
  875. rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
  876. rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
  877. if (PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER ||
  878. PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER) {
  879. return -EPROBE_DEFER;
  880. } else if (IS_ERR(rinfo->sda_gpiod) ||
  881. IS_ERR(rinfo->scl_gpiod) ||
  882. IS_ERR(i2c_imx->pinctrl_pins_default) ||
  883. IS_ERR(i2c_imx->pinctrl_pins_gpio)) {
  884. dev_dbg(&pdev->dev, "recovery information incomplete\n");
  885. return 0;
  886. }
  887. dev_dbg(&pdev->dev, "using scl%s for recovery\n",
  888. rinfo->sda_gpiod ? ",sda" : "");
  889. rinfo->prepare_recovery = i2c_imx_prepare_recovery;
  890. rinfo->unprepare_recovery = i2c_imx_unprepare_recovery;
  891. rinfo->recover_bus = i2c_generic_scl_recovery;
  892. i2c_imx->adapter.bus_recovery_info = rinfo;
  893. return 0;
  894. }
  895. static u32 i2c_imx_func(struct i2c_adapter *adapter)
  896. {
  897. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  898. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  899. }
  900. static const struct i2c_algorithm i2c_imx_algo = {
  901. .master_xfer = i2c_imx_xfer,
  902. .functionality = i2c_imx_func,
  903. };
  904. static int i2c_imx_probe(struct platform_device *pdev)
  905. {
  906. const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
  907. &pdev->dev);
  908. struct imx_i2c_struct *i2c_imx;
  909. struct resource *res;
  910. struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
  911. void __iomem *base;
  912. int irq, ret;
  913. dma_addr_t phy_addr;
  914. dev_dbg(&pdev->dev, "<%s>\n", __func__);
  915. irq = platform_get_irq(pdev, 0);
  916. if (irq < 0) {
  917. dev_err(&pdev->dev, "can't get irq number\n");
  918. return irq;
  919. }
  920. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  921. base = devm_ioremap_resource(&pdev->dev, res);
  922. if (IS_ERR(base))
  923. return PTR_ERR(base);
  924. phy_addr = (dma_addr_t)res->start;
  925. i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
  926. if (!i2c_imx)
  927. return -ENOMEM;
  928. if (of_id)
  929. i2c_imx->hwdata = of_id->data;
  930. else
  931. i2c_imx->hwdata = (struct imx_i2c_hwdata *)
  932. platform_get_device_id(pdev)->driver_data;
  933. /* Setup i2c_imx driver structure */
  934. strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
  935. i2c_imx->adapter.owner = THIS_MODULE;
  936. i2c_imx->adapter.algo = &i2c_imx_algo;
  937. i2c_imx->adapter.dev.parent = &pdev->dev;
  938. i2c_imx->adapter.nr = pdev->id;
  939. i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
  940. i2c_imx->base = base;
  941. /* Get I2C clock */
  942. i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
  943. if (IS_ERR(i2c_imx->clk)) {
  944. dev_err(&pdev->dev, "can't get I2C clock\n");
  945. return PTR_ERR(i2c_imx->clk);
  946. }
  947. ret = clk_prepare_enable(i2c_imx->clk);
  948. if (ret) {
  949. dev_err(&pdev->dev, "can't enable I2C clock, ret=%d\n", ret);
  950. return ret;
  951. }
  952. /* Request IRQ */
  953. ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, IRQF_SHARED,
  954. pdev->name, i2c_imx);
  955. if (ret) {
  956. dev_err(&pdev->dev, "can't claim irq %d\n", irq);
  957. goto clk_disable;
  958. }
  959. /* Init queue */
  960. init_waitqueue_head(&i2c_imx->queue);
  961. /* Set up adapter data */
  962. i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
  963. /* Set up platform driver data */
  964. platform_set_drvdata(pdev, i2c_imx);
  965. pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
  966. pm_runtime_use_autosuspend(&pdev->dev);
  967. pm_runtime_set_active(&pdev->dev);
  968. pm_runtime_enable(&pdev->dev);
  969. ret = pm_runtime_get_sync(&pdev->dev);
  970. if (ret < 0)
  971. goto rpm_disable;
  972. /* Set up clock divider */
  973. i2c_imx->bitrate = IMX_I2C_BIT_RATE;
  974. ret = of_property_read_u32(pdev->dev.of_node,
  975. "clock-frequency", &i2c_imx->bitrate);
  976. if (ret < 0 && pdata && pdata->bitrate)
  977. i2c_imx->bitrate = pdata->bitrate;
  978. i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
  979. clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
  980. i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
  981. /* Set up chip registers to defaults */
  982. imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
  983. i2c_imx, IMX_I2C_I2CR);
  984. imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
  985. /* Init optional bus recovery function */
  986. ret = i2c_imx_init_recovery_info(i2c_imx, pdev);
  987. /* Give it another chance if pinctrl used is not ready yet */
  988. if (ret == -EPROBE_DEFER)
  989. goto clk_notifier_unregister;
  990. /* Add I2C adapter */
  991. ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
  992. if (ret < 0)
  993. goto clk_notifier_unregister;
  994. pm_runtime_mark_last_busy(&pdev->dev);
  995. pm_runtime_put_autosuspend(&pdev->dev);
  996. dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
  997. dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
  998. dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
  999. i2c_imx->adapter.name);
  1000. dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
  1001. /* Init DMA config if supported */
  1002. i2c_imx_dma_request(i2c_imx, phy_addr);
  1003. return 0; /* Return OK */
  1004. clk_notifier_unregister:
  1005. clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
  1006. rpm_disable:
  1007. pm_runtime_put_noidle(&pdev->dev);
  1008. pm_runtime_disable(&pdev->dev);
  1009. pm_runtime_set_suspended(&pdev->dev);
  1010. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1011. clk_disable:
  1012. clk_disable_unprepare(i2c_imx->clk);
  1013. return ret;
  1014. }
  1015. static int i2c_imx_remove(struct platform_device *pdev)
  1016. {
  1017. struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
  1018. int ret;
  1019. ret = pm_runtime_get_sync(&pdev->dev);
  1020. if (ret < 0)
  1021. return ret;
  1022. /* remove adapter */
  1023. dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
  1024. i2c_del_adapter(&i2c_imx->adapter);
  1025. if (i2c_imx->dma)
  1026. i2c_imx_dma_free(i2c_imx);
  1027. /* setup chip registers to defaults */
  1028. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
  1029. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
  1030. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
  1031. imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
  1032. clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
  1033. clk_disable_unprepare(i2c_imx->clk);
  1034. pm_runtime_put_noidle(&pdev->dev);
  1035. pm_runtime_disable(&pdev->dev);
  1036. return 0;
  1037. }
  1038. #ifdef CONFIG_PM
  1039. static int i2c_imx_runtime_suspend(struct device *dev)
  1040. {
  1041. struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
  1042. clk_disable(i2c_imx->clk);
  1043. return 0;
  1044. }
  1045. static int i2c_imx_runtime_resume(struct device *dev)
  1046. {
  1047. struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
  1048. int ret;
  1049. ret = clk_enable(i2c_imx->clk);
  1050. if (ret)
  1051. dev_err(dev, "can't enable I2C clock, ret=%d\n", ret);
  1052. return ret;
  1053. }
  1054. static const struct dev_pm_ops i2c_imx_pm_ops = {
  1055. SET_RUNTIME_PM_OPS(i2c_imx_runtime_suspend,
  1056. i2c_imx_runtime_resume, NULL)
  1057. };
  1058. #define I2C_IMX_PM_OPS (&i2c_imx_pm_ops)
  1059. #else
  1060. #define I2C_IMX_PM_OPS NULL
  1061. #endif /* CONFIG_PM */
  1062. static struct platform_driver i2c_imx_driver = {
  1063. .probe = i2c_imx_probe,
  1064. .remove = i2c_imx_remove,
  1065. .driver = {
  1066. .name = DRIVER_NAME,
  1067. .pm = I2C_IMX_PM_OPS,
  1068. .of_match_table = i2c_imx_dt_ids,
  1069. },
  1070. .id_table = imx_i2c_devtype,
  1071. };
  1072. static int __init i2c_adap_imx_init(void)
  1073. {
  1074. return platform_driver_register(&i2c_imx_driver);
  1075. }
  1076. subsys_initcall(i2c_adap_imx_init);
  1077. static void __exit i2c_adap_imx_exit(void)
  1078. {
  1079. platform_driver_unregister(&i2c_imx_driver);
  1080. }
  1081. module_exit(i2c_adap_imx_exit);
  1082. MODULE_LICENSE("GPL");
  1083. MODULE_AUTHOR("Darius Augulis");
  1084. MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
  1085. MODULE_ALIAS("platform:" DRIVER_NAME);