i2c-imx.c 33 KB

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