blob: 9c1be9378dfdec08784a8e222c2fd062fd3b7e9e [file] [log] [blame]
Darius Augulisaa11e382009-01-30 10:32:28 +02001/*
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 *
Darius Augulisaa11e382009-01-30 10:32:28 +020014 * 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 *
Jingchang Lud533f042013-08-07 17:05:36 +080028 * Copyright 2013 Freescale Semiconductor, Inc.
29 *
Darius Augulisaa11e382009-01-30 10:32:28 +020030 */
31
Yao Yuan2fbed512014-11-18 18:31:05 +080032#include <linux/clk.h>
Yao Yuance1a7882014-11-18 18:31:06 +080033#include <linux/completion.h>
Yao Yuan2fbed512014-11-18 18:31:05 +080034#include <linux/delay.h>
Yao Yuance1a7882014-11-18 18:31:06 +080035#include <linux/dma-mapping.h>
36#include <linux/dmaengine.h>
37#include <linux/dmapool.h>
Yao Yuan2fbed512014-11-18 18:31:05 +080038#include <linux/err.h>
39#include <linux/errno.h>
40#include <linux/i2c.h>
Darius Augulisaa11e382009-01-30 10:32:28 +020041#include <linux/init.h>
Yao Yuan2fbed512014-11-18 18:31:05 +080042#include <linux/interrupt.h>
43#include <linux/io.h>
Darius Augulisaa11e382009-01-30 10:32:28 +020044#include <linux/kernel.h>
45#include <linux/module.h>
Shawn Guodfcd04b2011-09-08 15:09:35 +080046#include <linux/of.h>
47#include <linux/of_device.h>
Yao Yuance1a7882014-11-18 18:31:06 +080048#include <linux/of_dma.h>
Gao Pan1c4b6c32015-10-23 20:28:54 +080049#include <linux/of_gpio.h>
Hou Zhiqiang8bb6fd52015-11-17 17:53:18 +080050#include <linux/pinctrl/consumer.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020051#include <linux/platform_data/i2c-imx.h>
Yao Yuan2fbed512014-11-18 18:31:05 +080052#include <linux/platform_device.h>
Gao Pan588eb932015-12-11 10:24:09 +080053#include <linux/pm_runtime.h>
Yao Yuan2fbed512014-11-18 18:31:05 +080054#include <linux/sched.h>
55#include <linux/slab.h>
Darius Augulisaa11e382009-01-30 10:32:28 +020056
Darius Augulisaa11e382009-01-30 10:32:28 +020057/* This will be the driver name the kernel reports */
58#define DRIVER_NAME "imx-i2c"
59
60/* Default value */
61#define IMX_I2C_BIT_RATE 100000 /* 100kHz */
62
Yao Yuance1a7882014-11-18 18:31:06 +080063/*
64 * Enable DMA if transfer byte size is bigger than this threshold.
65 * As the hardware request, it must bigger than 4 bytes.\
66 * I have set '16' here, maybe it's not the best but I think it's
67 * the appropriate.
68 */
69#define DMA_THRESHOLD 16
70#define DMA_TIMEOUT 1000
71
Jingchang Lu8cc73312013-08-07 17:05:40 +080072/* IMX I2C registers:
73 * the I2C register offset is different between SoCs,
74 * to provid support for all these chips, split the
75 * register offset into a fixed base address and a
76 * variable shift value, then the full register offset
77 * will be calculated by
78 * reg_off = ( reg_base_addr << reg_shift)
79 */
Darius Augulisaa11e382009-01-30 10:32:28 +020080#define IMX_I2C_IADR 0x00 /* i2c slave address */
Jingchang Lu8cc73312013-08-07 17:05:40 +080081#define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
82#define IMX_I2C_I2CR 0x02 /* i2c control */
83#define IMX_I2C_I2SR 0x03 /* i2c status */
84#define IMX_I2C_I2DR 0x04 /* i2c transfer data */
85
86#define IMX_I2C_REGSHIFT 2
Jingchang Luad90efa2013-08-07 17:05:43 +080087#define VF610_I2C_REGSHIFT 0
Darius Augulisaa11e382009-01-30 10:32:28 +020088
89/* Bits of IMX I2C registers */
90#define I2SR_RXAK 0x01
91#define I2SR_IIF 0x02
92#define I2SR_SRW 0x04
93#define I2SR_IAL 0x10
94#define I2SR_IBB 0x20
95#define I2SR_IAAS 0x40
96#define I2SR_ICF 0x80
Yao Yuance1a7882014-11-18 18:31:06 +080097#define I2CR_DMAEN 0x02
Darius Augulisaa11e382009-01-30 10:32:28 +020098#define I2CR_RSTA 0x04
99#define I2CR_TXAK 0x08
100#define I2CR_MTX 0x10
101#define I2CR_MSTA 0x20
102#define I2CR_IIEN 0x40
103#define I2CR_IEN 0x80
104
Jingchang Lu171408c2013-08-07 17:05:41 +0800105/* register bits different operating codes definition:
106 * 1) I2SR: Interrupt flags clear operation differ between SoCs:
107 * - write zero to clear(w0c) INT flag on i.MX,
108 * - but write one to clear(w1c) INT flag on Vybrid.
109 * 2) I2CR: I2C module enable operation also differ between SoCs:
110 * - set I2CR_IEN bit enable the module on i.MX,
111 * - but clear I2CR_IEN bit enable the module on Vybrid.
112 */
113#define I2SR_CLR_OPCODE_W0C 0x0
114#define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
115#define I2CR_IEN_OPCODE_0 0x0
116#define I2CR_IEN_OPCODE_1 I2CR_IEN
117
Gao Pan588eb932015-12-11 10:24:09 +0800118#define I2C_PM_TIMEOUT 10 /* ms */
119
Darius Augulisaa11e382009-01-30 10:32:28 +0200120/*
121 * sorted list of clock divider, register value pairs
122 * taken from table 26-5, p.26-9, Freescale i.MX
123 * Integrated Portable System Processor Reference Manual
124 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
125 *
126 * Duplicated divider values removed from list
127 */
Jingchang Lud533f042013-08-07 17:05:36 +0800128struct imx_i2c_clk_pair {
129 u16 div;
130 u16 val;
131};
Darius Augulisaa11e382009-01-30 10:32:28 +0200132
Jingchang Lu4b775022013-08-07 17:05:42 +0800133static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
Darius Augulisaa11e382009-01-30 10:32:28 +0200134 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
135 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
136 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
137 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
138 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
139 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
140 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
141 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
142 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
143 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
144 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
145 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
146 { 3072, 0x1E }, { 3840, 0x1F }
147};
148
Jingchang Luad90efa2013-08-07 17:05:43 +0800149/* Vybrid VF610 clock divider, register value pairs */
150static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
151 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
152 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
153 { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
154 { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
155 { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
156 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
157 { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
158 { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
159 { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
160 { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
161 { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
162 { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
163 { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
164 { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
165 { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
166};
167
Shawn Guo5bdfba22012-09-14 15:19:00 +0800168enum imx_i2c_type {
169 IMX1_I2C,
170 IMX21_I2C,
Jingchang Luad90efa2013-08-07 17:05:43 +0800171 VF610_I2C,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800172};
173
Jingchang Lu4b775022013-08-07 17:05:42 +0800174struct imx_i2c_hwdata {
175 enum imx_i2c_type devtype;
176 unsigned regshift;
177 struct imx_i2c_clk_pair *clk_div;
178 unsigned ndivs;
179 unsigned i2sr_clr_opcode;
180 unsigned i2cr_ien_opcode;
181};
182
Yao Yuance1a7882014-11-18 18:31:06 +0800183struct imx_i2c_dma {
184 struct dma_chan *chan_tx;
185 struct dma_chan *chan_rx;
186 struct dma_chan *chan_using;
187 struct completion cmd_complete;
188 dma_addr_t dma_buf;
189 unsigned int dma_len;
190 enum dma_transfer_direction dma_transfer_dir;
191 enum dma_data_direction dma_data_dir;
192};
193
Darius Augulisaa11e382009-01-30 10:32:28 +0200194struct imx_i2c_struct {
195 struct i2c_adapter adapter;
Darius Augulisaa11e382009-01-30 10:32:28 +0200196 struct clk *clk;
197 void __iomem *base;
Darius Augulisaa11e382009-01-30 10:32:28 +0200198 wait_queue_head_t queue;
199 unsigned long i2csr;
Philipp Zabel4e355f52015-01-22 16:17:29 +0100200 unsigned int disable_delay;
Richard Zhao43309f32009-10-17 17:46:22 +0800201 int stopped;
Richard Zhaodb3a3d42009-10-17 17:46:24 +0800202 unsigned int ifdr; /* IMX_I2C_IFDR */
Fugang Duan9b2a6da2014-05-20 10:21:45 +0800203 unsigned int cur_clk;
204 unsigned int bitrate;
Jingchang Lu4b775022013-08-07 17:05:42 +0800205 const struct imx_i2c_hwdata *hwdata;
Gao Pan1c4b6c32015-10-23 20:28:54 +0800206 struct i2c_bus_recovery_info rinfo;
207
208 struct pinctrl *pinctrl;
209 struct pinctrl_state *pinctrl_pins_default;
210 struct pinctrl_state *pinctrl_pins_gpio;
Yao Yuance1a7882014-11-18 18:31:06 +0800211
212 struct imx_i2c_dma *dma;
Jingchang Lu4b775022013-08-07 17:05:42 +0800213};
214
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +0300215static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
Jingchang Lu4b775022013-08-07 17:05:42 +0800216 .devtype = IMX1_I2C,
217 .regshift = IMX_I2C_REGSHIFT,
218 .clk_div = imx_i2c_clk_div,
219 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
220 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
221 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
222
223};
224
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +0300225static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
Jingchang Lu4b775022013-08-07 17:05:42 +0800226 .devtype = IMX21_I2C,
227 .regshift = IMX_I2C_REGSHIFT,
228 .clk_div = imx_i2c_clk_div,
229 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
230 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
231 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
232
Darius Augulisaa11e382009-01-30 10:32:28 +0200233};
234
Jingchang Luad90efa2013-08-07 17:05:43 +0800235static struct imx_i2c_hwdata vf610_i2c_hwdata = {
236 .devtype = VF610_I2C,
237 .regshift = VF610_I2C_REGSHIFT,
238 .clk_div = vf610_i2c_clk_div,
239 .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
240 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
241 .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
242
243};
244
Krzysztof Kozlowskie9a02a32015-05-02 00:54:25 +0900245static const struct platform_device_id imx_i2c_devtype[] = {
Shawn Guo5bdfba22012-09-14 15:19:00 +0800246 {
247 .name = "imx1-i2c",
Jingchang Lu4b775022013-08-07 17:05:42 +0800248 .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800249 }, {
250 .name = "imx21-i2c",
Jingchang Lu4b775022013-08-07 17:05:42 +0800251 .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
Shawn Guo5bdfba22012-09-14 15:19:00 +0800252 }, {
253 /* sentinel */
254 }
255};
256MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
257
Shawn Guodfcd04b2011-09-08 15:09:35 +0800258static const struct of_device_id i2c_imx_dt_ids[] = {
Jingchang Lu4b775022013-08-07 17:05:42 +0800259 { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
260 { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
Jingchang Luad90efa2013-08-07 17:05:43 +0800261 { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
Shawn Guodfcd04b2011-09-08 15:09:35 +0800262 { /* sentinel */ }
263};
Arnaud Patard \(Rtp\)2f641a82013-06-20 23:07:06 +0200264MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
Shawn Guodfcd04b2011-09-08 15:09:35 +0800265
Shawn Guo5bdfba22012-09-14 15:19:00 +0800266static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
267{
Jingchang Lu4b775022013-08-07 17:05:42 +0800268 return i2c_imx->hwdata->devtype == IMX1_I2C;
Shawn Guo5bdfba22012-09-14 15:19:00 +0800269}
270
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800271static inline void imx_i2c_write_reg(unsigned int val,
272 struct imx_i2c_struct *i2c_imx, unsigned int reg)
273{
Jingchang Lu4b775022013-08-07 17:05:42 +0800274 writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800275}
276
277static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
278 unsigned int reg)
279{
Jingchang Lu4b775022013-08-07 17:05:42 +0800280 return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800281}
282
Yao Yuance1a7882014-11-18 18:31:06 +0800283/* Functions for DMA support */
284static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
285 dma_addr_t phy_addr)
286{
287 struct imx_i2c_dma *dma;
288 struct dma_slave_config dma_sconfig;
289 struct device *dev = &i2c_imx->adapter.dev;
290 int ret;
291
292 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
293 if (!dma)
294 return;
295
296 dma->chan_tx = dma_request_slave_channel(dev, "tx");
297 if (!dma->chan_tx) {
298 dev_dbg(dev, "can't request DMA tx channel\n");
Yao Yuance1a7882014-11-18 18:31:06 +0800299 goto fail_al;
300 }
301
302 dma_sconfig.dst_addr = phy_addr +
303 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
304 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
305 dma_sconfig.dst_maxburst = 1;
306 dma_sconfig.direction = DMA_MEM_TO_DEV;
307 ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
308 if (ret < 0) {
309 dev_dbg(dev, "can't configure tx channel\n");
310 goto fail_tx;
311 }
312
313 dma->chan_rx = dma_request_slave_channel(dev, "rx");
314 if (!dma->chan_rx) {
315 dev_dbg(dev, "can't request DMA rx channel\n");
Yao Yuance1a7882014-11-18 18:31:06 +0800316 goto fail_tx;
317 }
318
319 dma_sconfig.src_addr = phy_addr +
320 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
321 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
322 dma_sconfig.src_maxburst = 1;
323 dma_sconfig.direction = DMA_DEV_TO_MEM;
324 ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
325 if (ret < 0) {
326 dev_dbg(dev, "can't configure rx channel\n");
327 goto fail_rx;
328 }
329
330 i2c_imx->dma = dma;
331 init_completion(&dma->cmd_complete);
332 dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
333 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
334
335 return;
336
337fail_rx:
338 dma_release_channel(dma->chan_rx);
339fail_tx:
340 dma_release_channel(dma->chan_tx);
341fail_al:
342 devm_kfree(dev, dma);
Fabio Estevam5b661532015-11-01 14:22:51 -0200343 dev_info(dev, "can't use DMA, using PIO instead.\n");
Yao Yuance1a7882014-11-18 18:31:06 +0800344}
345
346static void i2c_imx_dma_callback(void *arg)
347{
348 struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
349 struct imx_i2c_dma *dma = i2c_imx->dma;
350
351 dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
352 dma->dma_len, dma->dma_data_dir);
353 complete(&dma->cmd_complete);
354}
355
356static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
357 struct i2c_msg *msgs)
358{
359 struct imx_i2c_dma *dma = i2c_imx->dma;
360 struct dma_async_tx_descriptor *txdesc;
361 struct device *dev = &i2c_imx->adapter.dev;
362 struct device *chan_dev = dma->chan_using->device->dev;
363
364 dma->dma_buf = dma_map_single(chan_dev, msgs->buf,
365 dma->dma_len, dma->dma_data_dir);
366 if (dma_mapping_error(chan_dev, dma->dma_buf)) {
367 dev_err(dev, "DMA mapping failed\n");
368 goto err_map;
369 }
370
371 txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
372 dma->dma_len, dma->dma_transfer_dir,
373 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
374 if (!txdesc) {
375 dev_err(dev, "Not able to get desc for DMA xfer\n");
376 goto err_desc;
377 }
378
Esben Haabendal7f8d5ff2018-07-09 11:43:01 +0200379 reinit_completion(&dma->cmd_complete);
Yao Yuance1a7882014-11-18 18:31:06 +0800380 txdesc->callback = i2c_imx_dma_callback;
381 txdesc->callback_param = i2c_imx;
382 if (dma_submit_error(dmaengine_submit(txdesc))) {
383 dev_err(dev, "DMA submit failed\n");
384 goto err_submit;
385 }
386
387 dma_async_issue_pending(dma->chan_using);
388 return 0;
389
390err_submit:
Gao Panc5528152016-01-08 13:33:15 +0800391 dmaengine_terminate_all(dma->chan_using);
Yao Yuance1a7882014-11-18 18:31:06 +0800392err_desc:
393 dma_unmap_single(chan_dev, dma->dma_buf,
394 dma->dma_len, dma->dma_data_dir);
395err_map:
396 return -EINVAL;
397}
398
399static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
400{
401 struct imx_i2c_dma *dma = i2c_imx->dma;
402
403 dma->dma_buf = 0;
404 dma->dma_len = 0;
405
406 dma_release_channel(dma->chan_tx);
407 dma->chan_tx = NULL;
408
409 dma_release_channel(dma->chan_rx);
410 dma->chan_rx = NULL;
411
412 dma->chan_using = NULL;
413}
414
Richard Zhao43309f32009-10-17 17:46:22 +0800415static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
Darius Augulisaa11e382009-01-30 10:32:28 +0200416{
417 unsigned long orig_jiffies = jiffies;
Richard Zhao43309f32009-10-17 17:46:22 +0800418 unsigned int temp;
Darius Augulisaa11e382009-01-30 10:32:28 +0200419
420 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
421
Richard Zhao43309f32009-10-17 17:46:22 +0800422 while (1) {
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800423 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Haibo Chen639a26c2014-09-03 13:52:07 +0800424
425 /* check for arbitration lost */
426 if (temp & I2SR_IAL) {
427 temp &= ~I2SR_IAL;
428 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
429 return -EAGAIN;
430 }
431
Richard Zhao43309f32009-10-17 17:46:22 +0800432 if (for_busy && (temp & I2SR_IBB))
433 break;
434 if (!for_busy && !(temp & I2SR_IBB))
435 break;
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100436 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200437 dev_dbg(&i2c_imx->adapter.dev,
438 "<%s> I2C bus is busy\n", __func__);
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100439 return -ETIMEDOUT;
Darius Augulisaa11e382009-01-30 10:32:28 +0200440 }
441 schedule();
442 }
443
444 return 0;
445}
446
447static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
448{
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200449 wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
Darius Augulisaa11e382009-01-30 10:32:28 +0200450
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200451 if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200452 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
453 return -ETIMEDOUT;
454 }
455 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
456 i2c_imx->i2csr = 0;
457 return 0;
458}
459
460static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
461{
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800462 if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
Darius Augulisaa11e382009-01-30 10:32:28 +0200463 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
Fabio Estevam4c0657a2015-10-22 14:41:20 -0200464 return -ENXIO; /* No ACK */
Darius Augulisaa11e382009-01-30 10:32:28 +0200465 }
466
467 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
468 return 0;
469}
470
Fugang Duan9b2a6da2014-05-20 10:21:45 +0800471static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx)
472{
473 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
474 unsigned int i2c_clk_rate;
475 unsigned int div;
476 int i;
477
478 /* Divider value calculation */
479 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
480 if (i2c_imx->cur_clk == i2c_clk_rate)
481 return;
Philipp Zabel4e355f52015-01-22 16:17:29 +0100482
483 i2c_imx->cur_clk = i2c_clk_rate;
Fugang Duan9b2a6da2014-05-20 10:21:45 +0800484
485 div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
486 if (div < i2c_clk_div[0].div)
487 i = 0;
488 else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
489 i = i2c_imx->hwdata->ndivs - 1;
490 else
Philipp Zabel4e355f52015-01-22 16:17:29 +0100491 for (i = 0; i2c_clk_div[i].div < div; i++)
492 ;
Fugang Duan9b2a6da2014-05-20 10:21:45 +0800493
494 /* Store divider value */
495 i2c_imx->ifdr = i2c_clk_div[i].val;
496
497 /*
498 * There dummy delay is calculated.
499 * It should be about one I2C clock period long.
500 * This delay is used in I2C bus disable function
501 * to fix chip hardware bug.
502 */
503 i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
504 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
505
506#ifdef CONFIG_I2C_DEBUG_BUS
507 dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
508 i2c_clk_rate, div);
509 dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
510 i2c_clk_div[i].val, i2c_clk_div[i].div);
511#endif
512}
513
Richard Zhao43309f32009-10-17 17:46:22 +0800514static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
Darius Augulisaa11e382009-01-30 10:32:28 +0200515{
516 unsigned int temp = 0;
Richard Zhao43309f32009-10-17 17:46:22 +0800517 int result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200518
519 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
520
Fugang Duan9b2a6da2014-05-20 10:21:45 +0800521 i2c_imx_set_clk(i2c_imx);
522
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800523 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200524 /* Enable I2C controller */
Jingchang Lu4b775022013-08-07 17:05:42 +0800525 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
526 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800527
528 /* Wait controller to be stable */
Oleksij Rempel2b899f32016-04-26 08:27:46 +0200529 usleep_range(50, 150);
Richard Zhao43309f32009-10-17 17:46:22 +0800530
Darius Augulisaa11e382009-01-30 10:32:28 +0200531 /* Start I2C transaction */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800532 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200533 temp |= I2CR_MSTA;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800534 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800535 result = i2c_imx_bus_busy(i2c_imx, 1);
536 if (result)
537 return result;
538 i2c_imx->stopped = 0;
539
Darius Augulisaa11e382009-01-30 10:32:28 +0200540 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
Yao Yuance1a7882014-11-18 18:31:06 +0800541 temp &= ~I2CR_DMAEN;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800542 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800543 return result;
Darius Augulisaa11e382009-01-30 10:32:28 +0200544}
545
546static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
547{
548 unsigned int temp = 0;
549
Richard Zhao43309f32009-10-17 17:46:22 +0800550 if (!i2c_imx->stopped) {
551 /* Stop I2C transaction */
552 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800553 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800554 temp &= ~(I2CR_MSTA | I2CR_MTX);
Yao Yuance1a7882014-11-18 18:31:06 +0800555 if (i2c_imx->dma)
556 temp &= ~I2CR_DMAEN;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800557 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Richard Zhao43309f32009-10-17 17:46:22 +0800558 }
Shawn Guo5bdfba22012-09-14 15:19:00 +0800559 if (is_imx1_i2c(i2c_imx)) {
Richard Zhaoa4094a72009-10-17 17:46:23 +0800560 /*
561 * This delay caused by an i.MXL hardware bug.
562 * If no (or too short) delay, no "STOP" bit will be generated.
563 */
564 udelay(i2c_imx->disable_delay);
565 }
Richard Zhao43309f32009-10-17 17:46:22 +0800566
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100567 if (!i2c_imx->stopped) {
Richard Zhao43309f32009-10-17 17:46:22 +0800568 i2c_imx_bus_busy(i2c_imx, 0);
Valentin Longchampa1ee06b2010-01-21 18:55:32 +0100569 i2c_imx->stopped = 1;
570 }
Richard Zhao43309f32009-10-17 17:46:22 +0800571
Darius Augulisaa11e382009-01-30 10:32:28 +0200572 /* Disable I2C controller */
Jingchang Lu4b775022013-08-07 17:05:42 +0800573 temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
574 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200575}
576
Darius Augulisaa11e382009-01-30 10:32:28 +0200577static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
578{
579 struct imx_i2c_struct *i2c_imx = dev_id;
580 unsigned int temp;
581
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800582 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200583 if (temp & I2SR_IIF) {
584 /* save status register */
585 i2c_imx->i2csr = temp;
586 temp &= ~I2SR_IIF;
Jingchang Lu4b775022013-08-07 17:05:42 +0800587 temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800588 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
Marc Kleine-Buddee39428d2010-06-21 09:27:05 +0200589 wake_up(&i2c_imx->queue);
Darius Augulisaa11e382009-01-30 10:32:28 +0200590 return IRQ_HANDLED;
591 }
592
593 return IRQ_NONE;
594}
595
Yao Yuance1a7882014-11-18 18:31:06 +0800596static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
597 struct i2c_msg *msgs)
598{
599 int result;
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500600 unsigned long time_left;
Yao Yuance1a7882014-11-18 18:31:06 +0800601 unsigned int temp = 0;
602 unsigned long orig_jiffies = jiffies;
603 struct imx_i2c_dma *dma = i2c_imx->dma;
604 struct device *dev = &i2c_imx->adapter.dev;
605
606 dma->chan_using = dma->chan_tx;
607 dma->dma_transfer_dir = DMA_MEM_TO_DEV;
608 dma->dma_data_dir = DMA_TO_DEVICE;
609 dma->dma_len = msgs->len - 1;
610 result = i2c_imx_dma_xfer(i2c_imx, msgs);
611 if (result)
612 return result;
613
614 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
615 temp |= I2CR_DMAEN;
616 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
617
618 /*
619 * Write slave address.
620 * The first byte must be transmitted by the CPU.
621 */
622 imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500623 time_left = wait_for_completion_timeout(
Yao Yuance1a7882014-11-18 18:31:06 +0800624 &i2c_imx->dma->cmd_complete,
625 msecs_to_jiffies(DMA_TIMEOUT));
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500626 if (time_left == 0) {
Yao Yuance1a7882014-11-18 18:31:06 +0800627 dmaengine_terminate_all(dma->chan_using);
Nicholas Mc Guirecb9eaba2014-12-27 08:33:53 -0500628 return -ETIMEDOUT;
Yao Yuance1a7882014-11-18 18:31:06 +0800629 }
630
631 /* Waiting for transfer complete. */
632 while (1) {
633 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
634 if (temp & I2SR_ICF)
635 break;
636 if (time_after(jiffies, orig_jiffies +
637 msecs_to_jiffies(DMA_TIMEOUT))) {
638 dev_dbg(dev, "<%s> Timeout\n", __func__);
639 return -ETIMEDOUT;
640 }
641 schedule();
642 }
643
644 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
645 temp &= ~I2CR_DMAEN;
646 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
647
648 /* The last data byte must be transferred by the CPU. */
649 imx_i2c_write_reg(msgs->buf[msgs->len-1],
650 i2c_imx, IMX_I2C_I2DR);
651 result = i2c_imx_trx_complete(i2c_imx);
652 if (result)
653 return result;
654
Wolfram Sangf5084932014-11-19 10:11:39 +0100655 return i2c_imx_acked(i2c_imx);
Yao Yuance1a7882014-11-18 18:31:06 +0800656}
657
658static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
659 struct i2c_msg *msgs, bool is_lastmsg)
660{
661 int result;
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500662 unsigned long time_left;
Yao Yuance1a7882014-11-18 18:31:06 +0800663 unsigned int temp;
664 unsigned long orig_jiffies = jiffies;
665 struct imx_i2c_dma *dma = i2c_imx->dma;
666 struct device *dev = &i2c_imx->adapter.dev;
667
Yao Yuance1a7882014-11-18 18:31:06 +0800668
669 dma->chan_using = dma->chan_rx;
670 dma->dma_transfer_dir = DMA_DEV_TO_MEM;
671 dma->dma_data_dir = DMA_FROM_DEVICE;
672 /* The last two data bytes must be transferred by the CPU. */
673 dma->dma_len = msgs->len - 2;
674 result = i2c_imx_dma_xfer(i2c_imx, msgs);
675 if (result)
676 return result;
677
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500678 time_left = wait_for_completion_timeout(
Yao Yuance1a7882014-11-18 18:31:06 +0800679 &i2c_imx->dma->cmd_complete,
680 msecs_to_jiffies(DMA_TIMEOUT));
Nicholas Mc Guire1ac63fe2015-02-08 06:39:56 -0500681 if (time_left == 0) {
Yao Yuance1a7882014-11-18 18:31:06 +0800682 dmaengine_terminate_all(dma->chan_using);
Nicholas Mc Guirecb9eaba2014-12-27 08:33:53 -0500683 return -ETIMEDOUT;
Yao Yuance1a7882014-11-18 18:31:06 +0800684 }
685
686 /* waiting for transfer complete. */
687 while (1) {
688 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
689 if (temp & I2SR_ICF)
690 break;
691 if (time_after(jiffies, orig_jiffies +
692 msecs_to_jiffies(DMA_TIMEOUT))) {
693 dev_dbg(dev, "<%s> Timeout\n", __func__);
694 return -ETIMEDOUT;
695 }
696 schedule();
697 }
698
699 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
700 temp &= ~I2CR_DMAEN;
701 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
702
703 /* read n-1 byte data */
704 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
705 temp |= I2CR_TXAK;
706 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
707
708 msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
709 /* read n byte data */
710 result = i2c_imx_trx_complete(i2c_imx);
711 if (result)
712 return result;
713
714 if (is_lastmsg) {
715 /*
716 * It must generate STOP before read I2DR to prevent
717 * controller from generating another clock cycle
718 */
719 dev_dbg(dev, "<%s> clear MSTA\n", __func__);
720 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
721 temp &= ~(I2CR_MSTA | I2CR_MTX);
722 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
723 i2c_imx_bus_busy(i2c_imx, 0);
724 i2c_imx->stopped = 1;
725 } else {
726 /*
727 * For i2c master receiver repeat restart operation like:
728 * read -> repeat MSTA -> read/write
729 * The controller must set MTX before read the last byte in
730 * the first read operation, otherwise the first read cost
731 * one extra clock cycle.
732 */
733 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
734 temp |= I2CR_MTX;
735 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
736 }
737 msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
738
739 return 0;
740}
741
Darius Augulisaa11e382009-01-30 10:32:28 +0200742static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
743{
744 int i, result;
745
746 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
747 __func__, msgs->addr << 1);
748
749 /* write slave address */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800750 imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200751 result = i2c_imx_trx_complete(i2c_imx);
752 if (result)
753 return result;
754 result = i2c_imx_acked(i2c_imx);
755 if (result)
756 return result;
757 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
758
759 /* write data */
760 for (i = 0; i < msgs->len; i++) {
761 dev_dbg(&i2c_imx->adapter.dev,
762 "<%s> write byte: B%d=0x%X\n",
763 __func__, i, msgs->buf[i]);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800764 imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200765 result = i2c_imx_trx_complete(i2c_imx);
766 if (result)
767 return result;
768 result = i2c_imx_acked(i2c_imx);
769 if (result)
770 return result;
771 }
772 return 0;
773}
774
Fugang Duan054b62d2014-04-30 14:24:58 +0800775static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
Darius Augulisaa11e382009-01-30 10:32:28 +0200776{
777 int i, result;
778 unsigned int temp;
Kaushal Butala8e8782c2014-04-04 14:56:10 +0200779 int block_data = msgs->flags & I2C_M_RECV_LEN;
Esben Haabendal44745bd2018-08-16 10:43:12 +0200780 int use_dma = i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data;
Darius Augulisaa11e382009-01-30 10:32:28 +0200781
782 dev_dbg(&i2c_imx->adapter.dev,
783 "<%s> write slave address: addr=0x%x\n",
784 __func__, (msgs->addr << 1) | 0x01);
785
786 /* write slave address */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800787 imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200788 result = i2c_imx_trx_complete(i2c_imx);
789 if (result)
790 return result;
791 result = i2c_imx_acked(i2c_imx);
792 if (result)
793 return result;
794
795 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
796
797 /* setup bus to read data */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800798 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200799 temp &= ~I2CR_MTX;
Kaushal Butala8e8782c2014-04-04 14:56:10 +0200800
801 /*
802 * Reset the I2CR_TXAK flag initially for SMBus block read since the
803 * length is unknown
804 */
805 if ((msgs->len - 1) || block_data)
Darius Augulisaa11e382009-01-30 10:32:28 +0200806 temp &= ~I2CR_TXAK;
Esben Haabendal44745bd2018-08-16 10:43:12 +0200807 if (use_dma)
808 temp |= I2CR_DMAEN;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800809 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
810 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
Darius Augulisaa11e382009-01-30 10:32:28 +0200811
812 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
813
Esben Haabendal44745bd2018-08-16 10:43:12 +0200814 if (use_dma)
Yao Yuance1a7882014-11-18 18:31:06 +0800815 return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
816
Darius Augulisaa11e382009-01-30 10:32:28 +0200817 /* read data */
818 for (i = 0; i < msgs->len; i++) {
Kaushal Butala8e8782c2014-04-04 14:56:10 +0200819 u8 len = 0;
Philipp Zabel4e355f52015-01-22 16:17:29 +0100820
Darius Augulisaa11e382009-01-30 10:32:28 +0200821 result = i2c_imx_trx_complete(i2c_imx);
822 if (result)
823 return result;
Kaushal Butala8e8782c2014-04-04 14:56:10 +0200824 /*
825 * First byte is the length of remaining packet
826 * in the SMBus block data read. Add it to
827 * msgs->len.
828 */
829 if ((!i) && block_data) {
830 len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
831 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
832 return -EPROTO;
833 dev_dbg(&i2c_imx->adapter.dev,
834 "<%s> read length: 0x%X\n",
835 __func__, len);
836 msgs->len += len;
837 }
Darius Augulisaa11e382009-01-30 10:32:28 +0200838 if (i == (msgs->len - 1)) {
Fugang Duan054b62d2014-04-30 14:24:58 +0800839 if (is_lastmsg) {
840 /*
841 * It must generate STOP before read I2DR to prevent
842 * controller from generating another clock cycle
843 */
844 dev_dbg(&i2c_imx->adapter.dev,
845 "<%s> clear MSTA\n", __func__);
846 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
847 temp &= ~(I2CR_MSTA | I2CR_MTX);
848 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
849 i2c_imx_bus_busy(i2c_imx, 0);
850 i2c_imx->stopped = 1;
851 } else {
852 /*
853 * For i2c master receiver repeat restart operation like:
854 * read -> repeat MSTA -> read/write
855 * The controller must set MTX before read the last byte in
856 * the first read operation, otherwise the first read cost
857 * one extra clock cycle.
858 */
859 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
860 temp |= I2CR_MTX;
861 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
862 }
Darius Augulisaa11e382009-01-30 10:32:28 +0200863 } else if (i == (msgs->len - 2)) {
864 dev_dbg(&i2c_imx->adapter.dev,
865 "<%s> set TXAK\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800866 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200867 temp |= I2CR_TXAK;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800868 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200869 }
Kaushal Butala8e8782c2014-04-04 14:56:10 +0200870 if ((!i) && block_data)
871 msgs->buf[0] = len;
872 else
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +0300873 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200874 dev_dbg(&i2c_imx->adapter.dev,
875 "<%s> read byte: B%d=0x%X\n",
876 __func__, i, msgs->buf[i]);
877 }
878 return 0;
879}
880
881static int i2c_imx_xfer(struct i2c_adapter *adapter,
882 struct i2c_msg *msgs, int num)
883{
884 unsigned int i, temp;
885 int result;
Fugang Duan054b62d2014-04-30 14:24:58 +0800886 bool is_lastmsg = false;
Darius Augulisaa11e382009-01-30 10:32:28 +0200887 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
888
889 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
890
Gao Pan588eb932015-12-11 10:24:09 +0800891 result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
892 if (result < 0)
893 goto out;
894
Richard Zhao43309f32009-10-17 17:46:22 +0800895 /* Start I2C transfer */
896 result = i2c_imx_start(i2c_imx);
Gao Pan1c4b6c32015-10-23 20:28:54 +0800897 if (result) {
898 if (i2c_imx->adapter.bus_recovery_info) {
899 i2c_recover_bus(&i2c_imx->adapter);
900 result = i2c_imx_start(i2c_imx);
901 }
902 }
903
Darius Augulisaa11e382009-01-30 10:32:28 +0200904 if (result)
905 goto fail0;
906
Darius Augulisaa11e382009-01-30 10:32:28 +0200907 /* read/write data */
908 for (i = 0; i < num; i++) {
Fugang Duan054b62d2014-04-30 14:24:58 +0800909 if (i == num - 1)
910 is_lastmsg = true;
911
Darius Augulisaa11e382009-01-30 10:32:28 +0200912 if (i) {
913 dev_dbg(&i2c_imx->adapter.dev,
914 "<%s> repeated start\n", __func__);
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800915 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200916 temp |= I2CR_RSTA;
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800917 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +0300918 result = i2c_imx_bus_busy(i2c_imx, 1);
Richard Zhao43309f32009-10-17 17:46:22 +0800919 if (result)
920 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200921 }
922 dev_dbg(&i2c_imx->adapter.dev,
923 "<%s> transfer message: %d\n", __func__, i);
924 /* write/read data */
925#ifdef CONFIG_I2C_DEBUG_BUS
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800926 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
Philipp Zabel4e355f52015-01-22 16:17:29 +0100927 dev_dbg(&i2c_imx->adapter.dev,
928 "<%s> CONTROL: IEN=%d, IIEN=%d, MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n",
929 __func__,
Darius Augulisaa11e382009-01-30 10:32:28 +0200930 (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
931 (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
932 (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +0800933 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +0200934 dev_dbg(&i2c_imx->adapter.dev,
Philipp Zabel4e355f52015-01-22 16:17:29 +0100935 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n",
936 __func__,
Darius Augulisaa11e382009-01-30 10:32:28 +0200937 (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
938 (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
939 (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
940 (temp & I2SR_RXAK ? 1 : 0));
941#endif
942 if (msgs[i].flags & I2C_M_RD)
Fugang Duan054b62d2014-04-30 14:24:58 +0800943 result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
Yao Yuance1a7882014-11-18 18:31:06 +0800944 else {
945 if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
946 result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
947 else
948 result = i2c_imx_write(i2c_imx, &msgs[i]);
949 }
Arnaud Patardda9c99f2010-03-23 17:28:28 +0100950 if (result)
951 goto fail0;
Darius Augulisaa11e382009-01-30 10:32:28 +0200952 }
953
954fail0:
955 /* Stop I2C transfer */
956 i2c_imx_stop(i2c_imx);
957
Gao Pan588eb932015-12-11 10:24:09 +0800958 pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent);
959 pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent);
960
961out:
Darius Augulisaa11e382009-01-30 10:32:28 +0200962 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
963 (result < 0) ? "error" : "success msg",
964 (result < 0) ? result : num);
965 return (result < 0) ? result : num;
966}
967
Gao Pan1c4b6c32015-10-23 20:28:54 +0800968static void i2c_imx_prepare_recovery(struct i2c_adapter *adap)
969{
970 struct imx_i2c_struct *i2c_imx;
971
972 i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
973
974 pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_gpio);
975}
976
977static void i2c_imx_unprepare_recovery(struct i2c_adapter *adap)
978{
979 struct imx_i2c_struct *i2c_imx;
980
981 i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
982
983 pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_default);
984}
985
Yang Lifd8961c2016-09-12 17:22:30 -0500986/*
987 * We switch SCL and SDA to their GPIO function and do some bitbanging
988 * for bus recovery. These alternative pinmux settings can be
989 * described in the device tree by a separate pinctrl state "gpio". If
990 * this is missing this is not a big problem, the only implication is
991 * that we can't do bus recovery.
992 */
993static int i2c_imx_init_recovery_info(struct imx_i2c_struct *i2c_imx,
Gao Pan1c4b6c32015-10-23 20:28:54 +0800994 struct platform_device *pdev)
995{
996 struct i2c_bus_recovery_info *rinfo = &i2c_imx->rinfo;
997
Yang Lifd8961c2016-09-12 17:22:30 -0500998 i2c_imx->pinctrl = devm_pinctrl_get(&pdev->dev);
999 if (!i2c_imx->pinctrl || IS_ERR(i2c_imx->pinctrl)) {
1000 dev_info(&pdev->dev, "can't get pinctrl, bus recovery not supported\n");
1001 return PTR_ERR(i2c_imx->pinctrl);
1002 }
1003
Gao Pan1c4b6c32015-10-23 20:28:54 +08001004 i2c_imx->pinctrl_pins_default = pinctrl_lookup_state(i2c_imx->pinctrl,
1005 PINCTRL_STATE_DEFAULT);
1006 i2c_imx->pinctrl_pins_gpio = pinctrl_lookup_state(i2c_imx->pinctrl,
1007 "gpio");
Gao Pane8e71292015-11-02 17:05:30 +08001008 rinfo->sda_gpio = of_get_named_gpio(pdev->dev.of_node, "sda-gpios", 0);
1009 rinfo->scl_gpio = of_get_named_gpio(pdev->dev.of_node, "scl-gpios", 0);
Gao Pan1c4b6c32015-10-23 20:28:54 +08001010
Stefan Agner533169d2016-09-26 17:18:58 -07001011 if (rinfo->sda_gpio == -EPROBE_DEFER ||
1012 rinfo->scl_gpio == -EPROBE_DEFER) {
1013 return -EPROBE_DEFER;
1014 } else if (!gpio_is_valid(rinfo->sda_gpio) ||
1015 !gpio_is_valid(rinfo->scl_gpio) ||
1016 IS_ERR(i2c_imx->pinctrl_pins_default) ||
1017 IS_ERR(i2c_imx->pinctrl_pins_gpio)) {
Gao Pan1c4b6c32015-10-23 20:28:54 +08001018 dev_dbg(&pdev->dev, "recovery information incomplete\n");
Yang Lifd8961c2016-09-12 17:22:30 -05001019 return 0;
Gao Pan1c4b6c32015-10-23 20:28:54 +08001020 }
1021
1022 dev_dbg(&pdev->dev, "using scl-gpio %d and sda-gpio %d for recovery\n",
1023 rinfo->sda_gpio, rinfo->scl_gpio);
1024
1025 rinfo->prepare_recovery = i2c_imx_prepare_recovery;
1026 rinfo->unprepare_recovery = i2c_imx_unprepare_recovery;
1027 rinfo->recover_bus = i2c_generic_gpio_recovery;
1028 i2c_imx->adapter.bus_recovery_info = rinfo;
Yang Lifd8961c2016-09-12 17:22:30 -05001029
1030 return 0;
Gao Pan1c4b6c32015-10-23 20:28:54 +08001031}
1032
Darius Augulisaa11e382009-01-30 10:32:28 +02001033static u32 i2c_imx_func(struct i2c_adapter *adapter)
1034{
Kaushal Butala8e8782c2014-04-04 14:56:10 +02001035 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
1036 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
Darius Augulisaa11e382009-01-30 10:32:28 +02001037}
1038
1039static struct i2c_algorithm i2c_imx_algo = {
1040 .master_xfer = i2c_imx_xfer,
1041 .functionality = i2c_imx_func,
1042};
1043
Wolfram Sang36114312013-10-08 22:35:34 +02001044static int i2c_imx_probe(struct platform_device *pdev)
Darius Augulisaa11e382009-01-30 10:32:28 +02001045{
Shawn Guo5bdfba22012-09-14 15:19:00 +08001046 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
1047 &pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +02001048 struct imx_i2c_struct *i2c_imx;
1049 struct resource *res;
Jingoo Han6d4028c2013-07-30 16:59:33 +09001050 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +02001051 void __iomem *base;
Wolfram Sang8c88ab02012-07-08 13:11:43 +02001052 int irq, ret;
Yao Yuance1a7882014-11-18 18:31:06 +08001053 dma_addr_t phy_addr;
Darius Augulisaa11e382009-01-30 10:32:28 +02001054
1055 dev_dbg(&pdev->dev, "<%s>\n", __func__);
1056
Darius Augulisaa11e382009-01-30 10:32:28 +02001057 irq = platform_get_irq(pdev, 0);
1058 if (irq < 0) {
1059 dev_err(&pdev->dev, "can't get irq number\n");
Wolfram Sanga8763f32013-12-12 22:51:31 +01001060 return irq;
Darius Augulisaa11e382009-01-30 10:32:28 +02001061 }
1062
Wolfram Sang3cc2d002013-05-10 10:16:54 +02001063 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +01001064 base = devm_ioremap_resource(&pdev->dev, res);
1065 if (IS_ERR(base))
1066 return PTR_ERR(base);
Uwe Kleine-König4927fbf2010-01-08 17:23:17 +01001067
Yao Yuance1a7882014-11-18 18:31:06 +08001068 phy_addr = (dma_addr_t)res->start;
Fabio Estevamd4ffeec2014-11-07 00:44:34 -02001069 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001070 if (!i2c_imx)
Richard Zhao9f8a3e72012-06-04 19:04:25 +08001071 return -ENOMEM;
Darius Augulis309c18d2009-03-31 14:52:54 +03001072
Shawn Guo5bdfba22012-09-14 15:19:00 +08001073 if (of_id)
Jingchang Lu4b775022013-08-07 17:05:42 +08001074 i2c_imx->hwdata = of_id->data;
Jingchang Lu0fc13472013-08-07 17:05:38 +08001075 else
Jingchang Lu4b775022013-08-07 17:05:42 +08001076 i2c_imx->hwdata = (struct imx_i2c_hwdata *)
1077 platform_get_device_id(pdev)->driver_data;
Shawn Guo5bdfba22012-09-14 15:19:00 +08001078
Darius Augulisaa11e382009-01-30 10:32:28 +02001079 /* Setup i2c_imx driver structure */
Wolfram Sang973c5ed2012-04-19 17:31:01 +02001080 strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
Darius Augulisaa11e382009-01-30 10:32:28 +02001081 i2c_imx->adapter.owner = THIS_MODULE;
1082 i2c_imx->adapter.algo = &i2c_imx_algo;
1083 i2c_imx->adapter.dev.parent = &pdev->dev;
Philipp Zabel4e355f52015-01-22 16:17:29 +01001084 i2c_imx->adapter.nr = pdev->id;
Shawn Guodfcd04b2011-09-08 15:09:35 +08001085 i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
Darius Augulisaa11e382009-01-30 10:32:28 +02001086 i2c_imx->base = base;
Darius Augulisaa11e382009-01-30 10:32:28 +02001087
1088 /* Get I2C clock */
Fabio Estevam1f09c672012-07-06 15:31:32 -03001089 i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
Darius Augulisaa11e382009-01-30 10:32:28 +02001090 if (IS_ERR(i2c_imx->clk)) {
Lucas Stacha50d2d42018-11-14 18:29:13 +01001091 if (PTR_ERR(i2c_imx->clk) != -EPROBE_DEFER)
1092 dev_err(&pdev->dev, "can't get I2C clock\n");
Richard Zhao9f8a3e72012-06-04 19:04:25 +08001093 return PTR_ERR(i2c_imx->clk);
Darius Augulisaa11e382009-01-30 10:32:28 +02001094 }
Darius Augulisaa11e382009-01-30 10:32:28 +02001095
Jingchang Lu46f28322013-08-07 17:05:37 +08001096 ret = clk_prepare_enable(i2c_imx->clk);
1097 if (ret) {
Gao Pan588eb932015-12-11 10:24:09 +08001098 dev_err(&pdev->dev, "can't enable I2C clock, ret=%d\n", ret);
Jingchang Lu46f28322013-08-07 17:05:37 +08001099 return ret;
1100 }
Gao Pan1c4b6c32015-10-23 20:28:54 +08001101
Darius Augulisaa11e382009-01-30 10:32:28 +02001102 /* Request IRQ */
Richard Zhao9f8a3e72012-06-04 19:04:25 +08001103 ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0,
1104 pdev->name, i2c_imx);
Darius Augulisaa11e382009-01-30 10:32:28 +02001105 if (ret) {
Richard Zhao9f8a3e72012-06-04 19:04:25 +08001106 dev_err(&pdev->dev, "can't claim irq %d\n", irq);
Fabio Estevama4ce47f2014-10-04 09:17:27 -03001107 goto clk_disable;
Darius Augulisaa11e382009-01-30 10:32:28 +02001108 }
1109
1110 /* Init queue */
1111 init_waitqueue_head(&i2c_imx->queue);
1112
1113 /* Set up adapter data */
1114 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
1115
Gao Pan588eb932015-12-11 10:24:09 +08001116 /* Set up platform driver data */
1117 platform_set_drvdata(pdev, i2c_imx);
1118
1119 pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
1120 pm_runtime_use_autosuspend(&pdev->dev);
1121 pm_runtime_set_active(&pdev->dev);
1122 pm_runtime_enable(&pdev->dev);
1123
1124 ret = pm_runtime_get_sync(&pdev->dev);
1125 if (ret < 0)
1126 goto rpm_disable;
1127
Darius Augulisaa11e382009-01-30 10:32:28 +02001128 /* Set up clock divider */
Fugang Duan9b2a6da2014-05-20 10:21:45 +08001129 i2c_imx->bitrate = IMX_I2C_BIT_RATE;
Shawn Guodfcd04b2011-09-08 15:09:35 +08001130 ret = of_property_read_u32(pdev->dev.of_node,
Fugang Duan9b2a6da2014-05-20 10:21:45 +08001131 "clock-frequency", &i2c_imx->bitrate);
Shawn Guodfcd04b2011-09-08 15:09:35 +08001132 if (ret < 0 && pdata && pdata->bitrate)
Fugang Duan9b2a6da2014-05-20 10:21:45 +08001133 i2c_imx->bitrate = pdata->bitrate;
Darius Augulisaa11e382009-01-30 10:32:28 +02001134
1135 /* Set up chip registers to defaults */
Jingchang Lu4b775022013-08-07 17:05:42 +08001136 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
1137 i2c_imx, IMX_I2C_I2CR);
1138 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +02001139
Yang Lifd8961c2016-09-12 17:22:30 -05001140 /* Init optional bus recovery function */
1141 ret = i2c_imx_init_recovery_info(i2c_imx, pdev);
1142 /* Give it another chance if pinctrl used is not ready yet */
1143 if (ret == -EPROBE_DEFER)
1144 goto rpm_disable;
Gao Pana5f65012015-12-09 11:08:22 +08001145
Darius Augulisaa11e382009-01-30 10:32:28 +02001146 /* Add I2C adapter */
1147 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +02001148 if (ret < 0)
Gao Pan588eb932015-12-11 10:24:09 +08001149 goto rpm_disable;
Darius Augulisaa11e382009-01-30 10:32:28 +02001150
Gao Pan588eb932015-12-11 10:24:09 +08001151 pm_runtime_mark_last_busy(&pdev->dev);
1152 pm_runtime_put_autosuspend(&pdev->dev);
Darius Augulisaa11e382009-01-30 10:32:28 +02001153
Richard Zhao9f8a3e72012-06-04 19:04:25 +08001154 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
Xiubo Li64bdfbf2014-08-06 11:45:08 +08001155 dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
Darius Augulisaa11e382009-01-30 10:32:28 +02001156 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
1157 i2c_imx->adapter.name);
Fabio Estevam06d141e2012-08-01 17:38:14 -03001158 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
Darius Augulisaa11e382009-01-30 10:32:28 +02001159
Philipp Zabel4e355f52015-01-22 16:17:29 +01001160 /* Init DMA config if supported */
Yao Yuance1a7882014-11-18 18:31:06 +08001161 i2c_imx_dma_request(i2c_imx, phy_addr);
1162
Darius Augulisaa11e382009-01-30 10:32:28 +02001163 return 0; /* Return OK */
Fabio Estevama4ce47f2014-10-04 09:17:27 -03001164
Gao Pan588eb932015-12-11 10:24:09 +08001165rpm_disable:
1166 pm_runtime_put_noidle(&pdev->dev);
1167 pm_runtime_disable(&pdev->dev);
1168 pm_runtime_set_suspended(&pdev->dev);
1169 pm_runtime_dont_use_autosuspend(&pdev->dev);
1170
Fabio Estevama4ce47f2014-10-04 09:17:27 -03001171clk_disable:
1172 clk_disable_unprepare(i2c_imx->clk);
1173 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +02001174}
1175
Wolfram Sang36114312013-10-08 22:35:34 +02001176static int i2c_imx_remove(struct platform_device *pdev)
Darius Augulisaa11e382009-01-30 10:32:28 +02001177{
1178 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
Gao Pan588eb932015-12-11 10:24:09 +08001179 int ret;
1180
1181 ret = pm_runtime_get_sync(&pdev->dev);
1182 if (ret < 0)
1183 return ret;
Darius Augulisaa11e382009-01-30 10:32:28 +02001184
1185 /* remove adapter */
1186 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
1187 i2c_del_adapter(&i2c_imx->adapter);
Darius Augulisaa11e382009-01-30 10:32:28 +02001188
Yao Yuance1a7882014-11-18 18:31:06 +08001189 if (i2c_imx->dma)
1190 i2c_imx_dma_free(i2c_imx);
1191
Darius Augulisaa11e382009-01-30 10:32:28 +02001192 /* setup chip registers to defaults */
Jingchang Lu1d5ef2a2013-08-07 17:05:39 +08001193 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
1194 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
1195 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
1196 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
Darius Augulisaa11e382009-01-30 10:32:28 +02001197
Gao Pan588eb932015-12-11 10:24:09 +08001198 clk_disable_unprepare(i2c_imx->clk);
1199
1200 pm_runtime_put_noidle(&pdev->dev);
1201 pm_runtime_disable(&pdev->dev);
1202
Darius Augulisaa11e382009-01-30 10:32:28 +02001203 return 0;
1204}
1205
Gao Pan588eb932015-12-11 10:24:09 +08001206#ifdef CONFIG_PM
1207static int i2c_imx_runtime_suspend(struct device *dev)
1208{
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +03001209 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
Gao Pan588eb932015-12-11 10:24:09 +08001210
1211 clk_disable_unprepare(i2c_imx->clk);
1212
1213 return 0;
1214}
1215
1216static int i2c_imx_runtime_resume(struct device *dev)
1217{
Dmitriy Baranov3bf58bb2016-01-25 15:48:32 +03001218 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
Gao Pan588eb932015-12-11 10:24:09 +08001219 int ret;
1220
1221 ret = clk_prepare_enable(i2c_imx->clk);
1222 if (ret)
1223 dev_err(dev, "can't enable I2C clock, ret=%d\n", ret);
1224
1225 return ret;
1226}
1227
1228static const struct dev_pm_ops i2c_imx_pm_ops = {
1229 SET_RUNTIME_PM_OPS(i2c_imx_runtime_suspend,
1230 i2c_imx_runtime_resume, NULL)
1231};
1232#define I2C_IMX_PM_OPS (&i2c_imx_pm_ops)
1233#else
1234#define I2C_IMX_PM_OPS NULL
1235#endif /* CONFIG_PM */
1236
Darius Augulisaa11e382009-01-30 10:32:28 +02001237static struct platform_driver i2c_imx_driver = {
Wolfram Sang36114312013-10-08 22:35:34 +02001238 .probe = i2c_imx_probe,
1239 .remove = i2c_imx_remove,
Gao Pan588eb932015-12-11 10:24:09 +08001240 .driver = {
1241 .name = DRIVER_NAME,
1242 .pm = I2C_IMX_PM_OPS,
Shawn Guodfcd04b2011-09-08 15:09:35 +08001243 .of_match_table = i2c_imx_dt_ids,
Shawn Guo5bdfba22012-09-14 15:19:00 +08001244 },
Gao Pan588eb932015-12-11 10:24:09 +08001245 .id_table = imx_i2c_devtype,
Darius Augulisaa11e382009-01-30 10:32:28 +02001246};
1247
1248static int __init i2c_adap_imx_init(void)
1249{
Wolfram Sang36114312013-10-08 22:35:34 +02001250 return platform_driver_register(&i2c_imx_driver);
Darius Augulisaa11e382009-01-30 10:32:28 +02001251}
Wolfram Sang5d3f3332009-09-19 09:09:50 +02001252subsys_initcall(i2c_adap_imx_init);
Darius Augulisaa11e382009-01-30 10:32:28 +02001253
1254static void __exit i2c_adap_imx_exit(void)
1255{
1256 platform_driver_unregister(&i2c_imx_driver);
1257}
Darius Augulisaa11e382009-01-30 10:32:28 +02001258module_exit(i2c_adap_imx_exit);
1259
1260MODULE_LICENSE("GPL");
1261MODULE_AUTHOR("Darius Augulis");
1262MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
1263MODULE_ALIAS("platform:" DRIVER_NAME);