]>
Commit | Line | Data |
---|---|---|
aa11e38c DA |
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 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
17 | * USA. | |
18 | * | |
19 | * Author: | |
20 | * Darius Augulis, Teltonika Inc. | |
21 | * | |
22 | * Desc.: | |
23 | * Implementation of I2C Adapter/Algorithm Driver | |
24 | * for I2C Bus integrated in Freescale i.MX/MXC processors | |
25 | * | |
26 | * Derived from Motorola GSG China I2C example driver | |
27 | * | |
28 | * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de | |
29 | * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de | |
30 | * Copyright (C) 2007 RightHand Technologies, Inc. | |
31 | * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt> | |
32 | * | |
d533f049 JL |
33 | * Copyright 2013 Freescale Semiconductor, Inc. |
34 | * | |
aa11e38c DA |
35 | */ |
36 | ||
37 | /** Includes ******************************************************************* | |
38 | *******************************************************************************/ | |
39 | ||
40 | #include <linux/init.h> | |
41 | #include <linux/kernel.h> | |
42 | #include <linux/module.h> | |
43 | #include <linux/errno.h> | |
44 | #include <linux/err.h> | |
45 | #include <linux/interrupt.h> | |
46 | #include <linux/delay.h> | |
47 | #include <linux/i2c.h> | |
48 | #include <linux/io.h> | |
49 | #include <linux/sched.h> | |
50 | #include <linux/platform_device.h> | |
51 | #include <linux/clk.h> | |
5a0e3ad6 | 52 | #include <linux/slab.h> |
dfcd04b1 SG |
53 | #include <linux/of.h> |
54 | #include <linux/of_device.h> | |
82906b13 | 55 | #include <linux/platform_data/i2c-imx.h> |
aa11e38c DA |
56 | |
57 | /** Defines ******************************************************************** | |
58 | *******************************************************************************/ | |
59 | ||
60 | /* This will be the driver name the kernel reports */ | |
61 | #define DRIVER_NAME "imx-i2c" | |
62 | ||
63 | /* Default value */ | |
64 | #define IMX_I2C_BIT_RATE 100000 /* 100kHz */ | |
65 | ||
8cc7331f JL |
66 | /* IMX I2C registers: |
67 | * the I2C register offset is different between SoCs, | |
68 | * to provid support for all these chips, split the | |
69 | * register offset into a fixed base address and a | |
70 | * variable shift value, then the full register offset | |
71 | * will be calculated by | |
72 | * reg_off = ( reg_base_addr << reg_shift) | |
73 | */ | |
aa11e38c | 74 | #define IMX_I2C_IADR 0x00 /* i2c slave address */ |
8cc7331f JL |
75 | #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */ |
76 | #define IMX_I2C_I2CR 0x02 /* i2c control */ | |
77 | #define IMX_I2C_I2SR 0x03 /* i2c status */ | |
78 | #define IMX_I2C_I2DR 0x04 /* i2c transfer data */ | |
79 | ||
80 | #define IMX_I2C_REGSHIFT 2 | |
ad90efae | 81 | #define VF610_I2C_REGSHIFT 0 |
aa11e38c DA |
82 | |
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_RSTA 0x04 | |
92 | #define I2CR_TXAK 0x08 | |
93 | #define I2CR_MTX 0x10 | |
94 | #define I2CR_MSTA 0x20 | |
95 | #define I2CR_IIEN 0x40 | |
96 | #define I2CR_IEN 0x80 | |
97 | ||
171408c2 JL |
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 | ||
aa11e38c DA |
111 | /** Variables ****************************************************************** |
112 | *******************************************************************************/ | |
113 | ||
aa11e38c DA |
114 | /* |
115 | * sorted list of clock divider, register value pairs | |
116 | * taken from table 26-5, p.26-9, Freescale i.MX | |
117 | * Integrated Portable System Processor Reference Manual | |
118 | * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007 | |
119 | * | |
120 | * Duplicated divider values removed from list | |
121 | */ | |
d533f049 JL |
122 | struct imx_i2c_clk_pair { |
123 | u16 div; | |
124 | u16 val; | |
125 | }; | |
aa11e38c | 126 | |
4b775022 | 127 | static struct imx_i2c_clk_pair imx_i2c_clk_div[] = { |
aa11e38c DA |
128 | { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 }, |
129 | { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 }, | |
130 | { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 }, | |
131 | { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B }, | |
132 | { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, | |
133 | { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 }, | |
134 | { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, | |
135 | { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 }, | |
136 | { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 }, | |
137 | { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, | |
138 | { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E }, | |
139 | { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, | |
140 | { 3072, 0x1E }, { 3840, 0x1F } | |
141 | }; | |
142 | ||
ad90efae JL |
143 | /* Vybrid VF610 clock divider, register value pairs */ |
144 | static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = { | |
145 | { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 }, | |
146 | { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 }, | |
147 | { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D }, | |
148 | { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 }, | |
149 | { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 }, | |
150 | { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 }, | |
151 | { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 }, | |
152 | { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 }, | |
153 | { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 }, | |
154 | { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B }, | |
155 | { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 }, | |
156 | { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 }, | |
157 | { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B }, | |
158 | { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A }, | |
159 | { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E }, | |
160 | }; | |
161 | ||
5bdfba29 SG |
162 | enum imx_i2c_type { |
163 | IMX1_I2C, | |
164 | IMX21_I2C, | |
ad90efae | 165 | VF610_I2C, |
5bdfba29 SG |
166 | }; |
167 | ||
4b775022 JL |
168 | struct imx_i2c_hwdata { |
169 | enum imx_i2c_type devtype; | |
170 | unsigned regshift; | |
171 | struct imx_i2c_clk_pair *clk_div; | |
172 | unsigned ndivs; | |
173 | unsigned i2sr_clr_opcode; | |
174 | unsigned i2cr_ien_opcode; | |
175 | }; | |
176 | ||
aa11e38c DA |
177 | struct imx_i2c_struct { |
178 | struct i2c_adapter adapter; | |
aa11e38c DA |
179 | struct clk *clk; |
180 | void __iomem *base; | |
aa11e38c DA |
181 | wait_queue_head_t queue; |
182 | unsigned long i2csr; | |
65de394d | 183 | unsigned int disable_delay; |
43309f3b | 184 | int stopped; |
db3a3d4e | 185 | unsigned int ifdr; /* IMX_I2C_IFDR */ |
9b2a6da3 FD |
186 | unsigned int cur_clk; |
187 | unsigned int bitrate; | |
4b775022 JL |
188 | const struct imx_i2c_hwdata *hwdata; |
189 | }; | |
190 | ||
191 | static const struct imx_i2c_hwdata imx1_i2c_hwdata = { | |
192 | .devtype = IMX1_I2C, | |
193 | .regshift = IMX_I2C_REGSHIFT, | |
194 | .clk_div = imx_i2c_clk_div, | |
195 | .ndivs = ARRAY_SIZE(imx_i2c_clk_div), | |
196 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C, | |
197 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_1, | |
198 | ||
199 | }; | |
200 | ||
201 | static const struct imx_i2c_hwdata imx21_i2c_hwdata = { | |
202 | .devtype = IMX21_I2C, | |
203 | .regshift = IMX_I2C_REGSHIFT, | |
204 | .clk_div = imx_i2c_clk_div, | |
205 | .ndivs = ARRAY_SIZE(imx_i2c_clk_div), | |
206 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C, | |
207 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_1, | |
208 | ||
5bdfba29 SG |
209 | }; |
210 | ||
ad90efae JL |
211 | static struct imx_i2c_hwdata vf610_i2c_hwdata = { |
212 | .devtype = VF610_I2C, | |
213 | .regshift = VF610_I2C_REGSHIFT, | |
214 | .clk_div = vf610_i2c_clk_div, | |
215 | .ndivs = ARRAY_SIZE(vf610_i2c_clk_div), | |
216 | .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C, | |
217 | .i2cr_ien_opcode = I2CR_IEN_OPCODE_0, | |
218 | ||
219 | }; | |
220 | ||
5bdfba29 SG |
221 | static struct platform_device_id imx_i2c_devtype[] = { |
222 | { | |
223 | .name = "imx1-i2c", | |
4b775022 | 224 | .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata, |
5bdfba29 SG |
225 | }, { |
226 | .name = "imx21-i2c", | |
4b775022 | 227 | .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata, |
5bdfba29 SG |
228 | }, { |
229 | /* sentinel */ | |
230 | } | |
aa11e38c | 231 | }; |
5bdfba29 | 232 | MODULE_DEVICE_TABLE(platform, imx_i2c_devtype); |
aa11e38c | 233 | |
dfcd04b1 | 234 | static const struct of_device_id i2c_imx_dt_ids[] = { |
4b775022 JL |
235 | { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, }, |
236 | { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, }, | |
ad90efae | 237 | { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, }, |
dfcd04b1 SG |
238 | { /* sentinel */ } |
239 | }; | |
2f641a8b | 240 | MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids); |
dfcd04b1 | 241 | |
5bdfba29 SG |
242 | static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx) |
243 | { | |
4b775022 | 244 | return i2c_imx->hwdata->devtype == IMX1_I2C; |
5bdfba29 SG |
245 | } |
246 | ||
1d5ef2a8 JL |
247 | static inline void imx_i2c_write_reg(unsigned int val, |
248 | struct imx_i2c_struct *i2c_imx, unsigned int reg) | |
249 | { | |
4b775022 | 250 | writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); |
1d5ef2a8 JL |
251 | } |
252 | ||
253 | static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx, | |
254 | unsigned int reg) | |
255 | { | |
4b775022 | 256 | return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); |
1d5ef2a8 JL |
257 | } |
258 | ||
aa11e38c DA |
259 | /** Functions for IMX I2C adapter driver *************************************** |
260 | *******************************************************************************/ | |
261 | ||
43309f3b | 262 | static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy) |
aa11e38c DA |
263 | { |
264 | unsigned long orig_jiffies = jiffies; | |
43309f3b | 265 | unsigned int temp; |
aa11e38c DA |
266 | |
267 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); | |
268 | ||
43309f3b | 269 | while (1) { |
1d5ef2a8 | 270 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
639a26cf HC |
271 | |
272 | /* check for arbitration lost */ | |
273 | if (temp & I2SR_IAL) { | |
274 | temp &= ~I2SR_IAL; | |
275 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR); | |
276 | return -EAGAIN; | |
277 | } | |
278 | ||
43309f3b RZ |
279 | if (for_busy && (temp & I2SR_IBB)) |
280 | break; | |
281 | if (!for_busy && !(temp & I2SR_IBB)) | |
282 | break; | |
da9c99fc | 283 | if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) { |
aa11e38c DA |
284 | dev_dbg(&i2c_imx->adapter.dev, |
285 | "<%s> I2C bus is busy\n", __func__); | |
da9c99fc | 286 | return -ETIMEDOUT; |
aa11e38c DA |
287 | } |
288 | schedule(); | |
289 | } | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) | |
295 | { | |
e39428d5 | 296 | wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10); |
aa11e38c | 297 | |
e39428d5 | 298 | if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { |
aa11e38c DA |
299 | dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); |
300 | return -ETIMEDOUT; | |
301 | } | |
302 | dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__); | |
303 | i2c_imx->i2csr = 0; | |
304 | return 0; | |
305 | } | |
306 | ||
307 | static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) | |
308 | { | |
1d5ef2a8 | 309 | if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) { |
aa11e38c DA |
310 | dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__); |
311 | return -EIO; /* No ACK */ | |
312 | } | |
313 | ||
314 | dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__); | |
315 | return 0; | |
316 | } | |
317 | ||
9b2a6da3 FD |
318 | static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx) |
319 | { | |
320 | struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div; | |
321 | unsigned int i2c_clk_rate; | |
322 | unsigned int div; | |
323 | int i; | |
324 | ||
325 | /* Divider value calculation */ | |
326 | i2c_clk_rate = clk_get_rate(i2c_imx->clk); | |
327 | if (i2c_imx->cur_clk == i2c_clk_rate) | |
328 | return; | |
329 | else | |
330 | i2c_imx->cur_clk = i2c_clk_rate; | |
331 | ||
332 | div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate; | |
333 | if (div < i2c_clk_div[0].div) | |
334 | i = 0; | |
335 | else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div) | |
336 | i = i2c_imx->hwdata->ndivs - 1; | |
337 | else | |
338 | for (i = 0; i2c_clk_div[i].div < div; i++); | |
339 | ||
340 | /* Store divider value */ | |
341 | i2c_imx->ifdr = i2c_clk_div[i].val; | |
342 | ||
343 | /* | |
344 | * There dummy delay is calculated. | |
345 | * It should be about one I2C clock period long. | |
346 | * This delay is used in I2C bus disable function | |
347 | * to fix chip hardware bug. | |
348 | */ | |
349 | i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div | |
350 | + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2); | |
351 | ||
352 | #ifdef CONFIG_I2C_DEBUG_BUS | |
353 | dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n", | |
354 | i2c_clk_rate, div); | |
355 | dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n", | |
356 | i2c_clk_div[i].val, i2c_clk_div[i].div); | |
357 | #endif | |
358 | } | |
359 | ||
43309f3b | 360 | static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) |
aa11e38c DA |
361 | { |
362 | unsigned int temp = 0; | |
43309f3b | 363 | int result; |
aa11e38c DA |
364 | |
365 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); | |
366 | ||
9b2a6da3 FD |
367 | i2c_imx_set_clk(i2c_imx); |
368 | ||
e5bf216a FE |
369 | result = clk_prepare_enable(i2c_imx->clk); |
370 | if (result) | |
371 | return result; | |
1d5ef2a8 | 372 | imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR); |
aa11e38c | 373 | /* Enable I2C controller */ |
4b775022 JL |
374 | imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); |
375 | imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR); | |
43309f3b RZ |
376 | |
377 | /* Wait controller to be stable */ | |
378 | udelay(50); | |
379 | ||
aa11e38c | 380 | /* Start I2C transaction */ |
1d5ef2a8 | 381 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
aa11e38c | 382 | temp |= I2CR_MSTA; |
1d5ef2a8 | 383 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
43309f3b RZ |
384 | result = i2c_imx_bus_busy(i2c_imx, 1); |
385 | if (result) | |
386 | return result; | |
387 | i2c_imx->stopped = 0; | |
388 | ||
aa11e38c | 389 | temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; |
1d5ef2a8 | 390 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
43309f3b | 391 | return result; |
aa11e38c DA |
392 | } |
393 | ||
394 | static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) | |
395 | { | |
396 | unsigned int temp = 0; | |
397 | ||
43309f3b RZ |
398 | if (!i2c_imx->stopped) { |
399 | /* Stop I2C transaction */ | |
400 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); | |
1d5ef2a8 | 401 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
43309f3b | 402 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
1d5ef2a8 | 403 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
43309f3b | 404 | } |
5bdfba29 | 405 | if (is_imx1_i2c(i2c_imx)) { |
a4094a76 RZ |
406 | /* |
407 | * This delay caused by an i.MXL hardware bug. | |
408 | * If no (or too short) delay, no "STOP" bit will be generated. | |
409 | */ | |
410 | udelay(i2c_imx->disable_delay); | |
411 | } | |
43309f3b | 412 | |
a1ee06b7 | 413 | if (!i2c_imx->stopped) { |
43309f3b | 414 | i2c_imx_bus_busy(i2c_imx, 0); |
a1ee06b7 VL |
415 | i2c_imx->stopped = 1; |
416 | } | |
43309f3b | 417 | |
aa11e38c | 418 | /* Disable I2C controller */ |
4b775022 JL |
419 | temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, |
420 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); | |
83914337 | 421 | clk_disable_unprepare(i2c_imx->clk); |
aa11e38c DA |
422 | } |
423 | ||
aa11e38c DA |
424 | static irqreturn_t i2c_imx_isr(int irq, void *dev_id) |
425 | { | |
426 | struct imx_i2c_struct *i2c_imx = dev_id; | |
427 | unsigned int temp; | |
428 | ||
1d5ef2a8 | 429 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
aa11e38c DA |
430 | if (temp & I2SR_IIF) { |
431 | /* save status register */ | |
432 | i2c_imx->i2csr = temp; | |
433 | temp &= ~I2SR_IIF; | |
4b775022 | 434 | temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF); |
1d5ef2a8 | 435 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR); |
e39428d5 | 436 | wake_up(&i2c_imx->queue); |
aa11e38c DA |
437 | return IRQ_HANDLED; |
438 | } | |
439 | ||
440 | return IRQ_NONE; | |
441 | } | |
442 | ||
443 | static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) | |
444 | { | |
445 | int i, result; | |
446 | ||
447 | dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n", | |
448 | __func__, msgs->addr << 1); | |
449 | ||
450 | /* write slave address */ | |
1d5ef2a8 | 451 | imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR); |
aa11e38c DA |
452 | result = i2c_imx_trx_complete(i2c_imx); |
453 | if (result) | |
454 | return result; | |
455 | result = i2c_imx_acked(i2c_imx); | |
456 | if (result) | |
457 | return result; | |
458 | dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__); | |
459 | ||
460 | /* write data */ | |
461 | for (i = 0; i < msgs->len; i++) { | |
462 | dev_dbg(&i2c_imx->adapter.dev, | |
463 | "<%s> write byte: B%d=0x%X\n", | |
464 | __func__, i, msgs->buf[i]); | |
1d5ef2a8 | 465 | imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR); |
aa11e38c DA |
466 | result = i2c_imx_trx_complete(i2c_imx); |
467 | if (result) | |
468 | return result; | |
469 | result = i2c_imx_acked(i2c_imx); | |
470 | if (result) | |
471 | return result; | |
472 | } | |
473 | return 0; | |
474 | } | |
475 | ||
054b62d9 | 476 | static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg) |
aa11e38c DA |
477 | { |
478 | int i, result; | |
479 | unsigned int temp; | |
8e8782c7 | 480 | int block_data = msgs->flags & I2C_M_RECV_LEN; |
aa11e38c DA |
481 | |
482 | dev_dbg(&i2c_imx->adapter.dev, | |
483 | "<%s> write slave address: addr=0x%x\n", | |
484 | __func__, (msgs->addr << 1) | 0x01); | |
485 | ||
486 | /* write slave address */ | |
1d5ef2a8 | 487 | imx_i2c_write_reg((msgs->addr << 1) | 0x01, i2c_imx, IMX_I2C_I2DR); |
aa11e38c DA |
488 | result = i2c_imx_trx_complete(i2c_imx); |
489 | if (result) | |
490 | return result; | |
491 | result = i2c_imx_acked(i2c_imx); | |
492 | if (result) | |
493 | return result; | |
494 | ||
495 | dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__); | |
496 | ||
497 | /* setup bus to read data */ | |
1d5ef2a8 | 498 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
aa11e38c | 499 | temp &= ~I2CR_MTX; |
8e8782c7 KB |
500 | |
501 | /* | |
502 | * Reset the I2CR_TXAK flag initially for SMBus block read since the | |
503 | * length is unknown | |
504 | */ | |
505 | if ((msgs->len - 1) || block_data) | |
aa11e38c | 506 | temp &= ~I2CR_TXAK; |
1d5ef2a8 JL |
507 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
508 | imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */ | |
aa11e38c DA |
509 | |
510 | dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); | |
511 | ||
512 | /* read data */ | |
513 | for (i = 0; i < msgs->len; i++) { | |
8e8782c7 | 514 | u8 len = 0; |
aa11e38c DA |
515 | result = i2c_imx_trx_complete(i2c_imx); |
516 | if (result) | |
517 | return result; | |
8e8782c7 KB |
518 | /* |
519 | * First byte is the length of remaining packet | |
520 | * in the SMBus block data read. Add it to | |
521 | * msgs->len. | |
522 | */ | |
523 | if ((!i) && block_data) { | |
524 | len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); | |
525 | if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) | |
526 | return -EPROTO; | |
527 | dev_dbg(&i2c_imx->adapter.dev, | |
528 | "<%s> read length: 0x%X\n", | |
529 | __func__, len); | |
530 | msgs->len += len; | |
531 | } | |
aa11e38c | 532 | if (i == (msgs->len - 1)) { |
054b62d9 FD |
533 | if (is_lastmsg) { |
534 | /* | |
535 | * It must generate STOP before read I2DR to prevent | |
536 | * controller from generating another clock cycle | |
537 | */ | |
538 | dev_dbg(&i2c_imx->adapter.dev, | |
539 | "<%s> clear MSTA\n", __func__); | |
540 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); | |
541 | temp &= ~(I2CR_MSTA | I2CR_MTX); | |
542 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); | |
543 | i2c_imx_bus_busy(i2c_imx, 0); | |
544 | i2c_imx->stopped = 1; | |
545 | } else { | |
546 | /* | |
547 | * For i2c master receiver repeat restart operation like: | |
548 | * read -> repeat MSTA -> read/write | |
549 | * The controller must set MTX before read the last byte in | |
550 | * the first read operation, otherwise the first read cost | |
551 | * one extra clock cycle. | |
552 | */ | |
553 | temp = readb(i2c_imx->base + IMX_I2C_I2CR); | |
554 | temp |= I2CR_MTX; | |
555 | writeb(temp, i2c_imx->base + IMX_I2C_I2CR); | |
556 | } | |
aa11e38c DA |
557 | } else if (i == (msgs->len - 2)) { |
558 | dev_dbg(&i2c_imx->adapter.dev, | |
559 | "<%s> set TXAK\n", __func__); | |
1d5ef2a8 | 560 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
aa11e38c | 561 | temp |= I2CR_TXAK; |
1d5ef2a8 | 562 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
aa11e38c | 563 | } |
8e8782c7 KB |
564 | if ((!i) && block_data) |
565 | msgs->buf[0] = len; | |
566 | else | |
567 | msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); | |
aa11e38c DA |
568 | dev_dbg(&i2c_imx->adapter.dev, |
569 | "<%s> read byte: B%d=0x%X\n", | |
570 | __func__, i, msgs->buf[i]); | |
571 | } | |
572 | return 0; | |
573 | } | |
574 | ||
575 | static int i2c_imx_xfer(struct i2c_adapter *adapter, | |
576 | struct i2c_msg *msgs, int num) | |
577 | { | |
578 | unsigned int i, temp; | |
579 | int result; | |
054b62d9 | 580 | bool is_lastmsg = false; |
aa11e38c DA |
581 | struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); |
582 | ||
583 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); | |
584 | ||
43309f3b RZ |
585 | /* Start I2C transfer */ |
586 | result = i2c_imx_start(i2c_imx); | |
aa11e38c DA |
587 | if (result) |
588 | goto fail0; | |
589 | ||
aa11e38c DA |
590 | /* read/write data */ |
591 | for (i = 0; i < num; i++) { | |
054b62d9 FD |
592 | if (i == num - 1) |
593 | is_lastmsg = true; | |
594 | ||
aa11e38c DA |
595 | if (i) { |
596 | dev_dbg(&i2c_imx->adapter.dev, | |
597 | "<%s> repeated start\n", __func__); | |
1d5ef2a8 | 598 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
aa11e38c | 599 | temp |= I2CR_RSTA; |
1d5ef2a8 | 600 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
43309f3b RZ |
601 | result = i2c_imx_bus_busy(i2c_imx, 1); |
602 | if (result) | |
603 | goto fail0; | |
aa11e38c DA |
604 | } |
605 | dev_dbg(&i2c_imx->adapter.dev, | |
606 | "<%s> transfer message: %d\n", __func__, i); | |
607 | /* write/read data */ | |
608 | #ifdef CONFIG_I2C_DEBUG_BUS | |
1d5ef2a8 | 609 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
aa11e38c DA |
610 | dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, " |
611 | "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__, | |
612 | (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0), | |
613 | (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0), | |
614 | (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0)); | |
1d5ef2a8 | 615 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); |
aa11e38c DA |
616 | dev_dbg(&i2c_imx->adapter.dev, |
617 | "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, " | |
618 | "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__, | |
619 | (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0), | |
620 | (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0), | |
621 | (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0), | |
622 | (temp & I2SR_RXAK ? 1 : 0)); | |
623 | #endif | |
624 | if (msgs[i].flags & I2C_M_RD) | |
054b62d9 | 625 | result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg); |
aa11e38c DA |
626 | else |
627 | result = i2c_imx_write(i2c_imx, &msgs[i]); | |
da9c99fc AP |
628 | if (result) |
629 | goto fail0; | |
aa11e38c DA |
630 | } |
631 | ||
632 | fail0: | |
633 | /* Stop I2C transfer */ | |
634 | i2c_imx_stop(i2c_imx); | |
635 | ||
636 | dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__, | |
637 | (result < 0) ? "error" : "success msg", | |
638 | (result < 0) ? result : num); | |
639 | return (result < 0) ? result : num; | |
640 | } | |
641 | ||
642 | static u32 i2c_imx_func(struct i2c_adapter *adapter) | |
643 | { | |
8e8782c7 KB |
644 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
645 | | I2C_FUNC_SMBUS_READ_BLOCK_DATA; | |
aa11e38c DA |
646 | } |
647 | ||
648 | static struct i2c_algorithm i2c_imx_algo = { | |
649 | .master_xfer = i2c_imx_xfer, | |
650 | .functionality = i2c_imx_func, | |
651 | }; | |
652 | ||
3611431c | 653 | static int i2c_imx_probe(struct platform_device *pdev) |
aa11e38c | 654 | { |
5bdfba29 SG |
655 | const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids, |
656 | &pdev->dev); | |
aa11e38c DA |
657 | struct imx_i2c_struct *i2c_imx; |
658 | struct resource *res; | |
6d4028c6 | 659 | struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev); |
aa11e38c | 660 | void __iomem *base; |
8c88ab04 | 661 | int irq, ret; |
aa11e38c DA |
662 | |
663 | dev_dbg(&pdev->dev, "<%s>\n", __func__); | |
664 | ||
aa11e38c DA |
665 | irq = platform_get_irq(pdev, 0); |
666 | if (irq < 0) { | |
667 | dev_err(&pdev->dev, "can't get irq number\n"); | |
a8763f33 | 668 | return irq; |
aa11e38c DA |
669 | } |
670 | ||
3cc2d009 | 671 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
84dbf809 TR |
672 | base = devm_ioremap_resource(&pdev->dev, res); |
673 | if (IS_ERR(base)) | |
674 | return PTR_ERR(base); | |
aa11e38c | 675 | |
9f8a3e7f RZ |
676 | i2c_imx = devm_kzalloc(&pdev->dev, sizeof(struct imx_i2c_struct), |
677 | GFP_KERNEL); | |
46797a2a | 678 | if (!i2c_imx) |
9f8a3e7f | 679 | return -ENOMEM; |
309c18d2 | 680 | |
5bdfba29 | 681 | if (of_id) |
4b775022 | 682 | i2c_imx->hwdata = of_id->data; |
0fc1347a | 683 | else |
4b775022 JL |
684 | i2c_imx->hwdata = (struct imx_i2c_hwdata *) |
685 | platform_get_device_id(pdev)->driver_data; | |
5bdfba29 | 686 | |
aa11e38c | 687 | /* Setup i2c_imx driver structure */ |
973c5ed4 | 688 | strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name)); |
aa11e38c DA |
689 | i2c_imx->adapter.owner = THIS_MODULE; |
690 | i2c_imx->adapter.algo = &i2c_imx_algo; | |
691 | i2c_imx->adapter.dev.parent = &pdev->dev; | |
692 | i2c_imx->adapter.nr = pdev->id; | |
dfcd04b1 | 693 | i2c_imx->adapter.dev.of_node = pdev->dev.of_node; |
aa11e38c | 694 | i2c_imx->base = base; |
aa11e38c DA |
695 | |
696 | /* Get I2C clock */ | |
1f09c672 | 697 | i2c_imx->clk = devm_clk_get(&pdev->dev, NULL); |
aa11e38c | 698 | if (IS_ERR(i2c_imx->clk)) { |
aa11e38c | 699 | dev_err(&pdev->dev, "can't get I2C clock\n"); |
9f8a3e7f | 700 | return PTR_ERR(i2c_imx->clk); |
aa11e38c | 701 | } |
aa11e38c | 702 | |
46f2832b JL |
703 | ret = clk_prepare_enable(i2c_imx->clk); |
704 | if (ret) { | |
705 | dev_err(&pdev->dev, "can't enable I2C clock\n"); | |
706 | return ret; | |
707 | } | |
aa11e38c | 708 | /* Request IRQ */ |
9f8a3e7f RZ |
709 | ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, 0, |
710 | pdev->name, i2c_imx); | |
aa11e38c | 711 | if (ret) { |
9f8a3e7f RZ |
712 | dev_err(&pdev->dev, "can't claim irq %d\n", irq); |
713 | return ret; | |
aa11e38c DA |
714 | } |
715 | ||
716 | /* Init queue */ | |
717 | init_waitqueue_head(&i2c_imx->queue); | |
718 | ||
719 | /* Set up adapter data */ | |
720 | i2c_set_adapdata(&i2c_imx->adapter, i2c_imx); | |
721 | ||
722 | /* Set up clock divider */ | |
9b2a6da3 | 723 | i2c_imx->bitrate = IMX_I2C_BIT_RATE; |
dfcd04b1 | 724 | ret = of_property_read_u32(pdev->dev.of_node, |
9b2a6da3 | 725 | "clock-frequency", &i2c_imx->bitrate); |
dfcd04b1 | 726 | if (ret < 0 && pdata && pdata->bitrate) |
9b2a6da3 | 727 | i2c_imx->bitrate = pdata->bitrate; |
aa11e38c DA |
728 | |
729 | /* Set up chip registers to defaults */ | |
4b775022 JL |
730 | imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, |
731 | i2c_imx, IMX_I2C_I2CR); | |
732 | imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR); | |
aa11e38c DA |
733 | |
734 | /* Add I2C adapter */ | |
735 | ret = i2c_add_numbered_adapter(&i2c_imx->adapter); | |
736 | if (ret < 0) { | |
737 | dev_err(&pdev->dev, "registration failed\n"); | |
9f8a3e7f | 738 | return ret; |
aa11e38c DA |
739 | } |
740 | ||
741 | /* Set up platform driver data */ | |
742 | platform_set_drvdata(pdev, i2c_imx); | |
46f2832b | 743 | clk_disable_unprepare(i2c_imx->clk); |
aa11e38c | 744 | |
9f8a3e7f | 745 | dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq); |
64bdfbfc | 746 | dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res); |
aa11e38c DA |
747 | dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n", |
748 | i2c_imx->adapter.name); | |
06d141e9 | 749 | dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n"); |
aa11e38c DA |
750 | |
751 | return 0; /* Return OK */ | |
aa11e38c DA |
752 | } |
753 | ||
3611431c | 754 | static int i2c_imx_remove(struct platform_device *pdev) |
aa11e38c DA |
755 | { |
756 | struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev); | |
aa11e38c DA |
757 | |
758 | /* remove adapter */ | |
759 | dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n"); | |
760 | i2c_del_adapter(&i2c_imx->adapter); | |
aa11e38c | 761 | |
aa11e38c | 762 | /* setup chip registers to defaults */ |
1d5ef2a8 JL |
763 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); |
764 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); | |
765 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); | |
766 | imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); | |
aa11e38c | 767 | |
aa11e38c DA |
768 | return 0; |
769 | } | |
770 | ||
771 | static struct platform_driver i2c_imx_driver = { | |
3611431c WS |
772 | .probe = i2c_imx_probe, |
773 | .remove = i2c_imx_remove, | |
aa11e38c DA |
774 | .driver = { |
775 | .name = DRIVER_NAME, | |
776 | .owner = THIS_MODULE, | |
dfcd04b1 | 777 | .of_match_table = i2c_imx_dt_ids, |
5bdfba29 SG |
778 | }, |
779 | .id_table = imx_i2c_devtype, | |
aa11e38c DA |
780 | }; |
781 | ||
782 | static int __init i2c_adap_imx_init(void) | |
783 | { | |
3611431c | 784 | return platform_driver_register(&i2c_imx_driver); |
aa11e38c | 785 | } |
5d3f3331 | 786 | subsys_initcall(i2c_adap_imx_init); |
aa11e38c DA |
787 | |
788 | static void __exit i2c_adap_imx_exit(void) | |
789 | { | |
790 | platform_driver_unregister(&i2c_imx_driver); | |
791 | } | |
aa11e38c DA |
792 | module_exit(i2c_adap_imx_exit); |
793 | ||
794 | MODULE_LICENSE("GPL"); | |
795 | MODULE_AUTHOR("Darius Augulis"); | |
796 | MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus"); | |
797 | MODULE_ALIAS("platform:" DRIVER_NAME); |