i2c-imx.c 35 KB

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