i2c-imx.c 31 KB

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