2 * Copyright (C) 2002 Motorola GSG-China
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.
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.
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,
20 * Darius Augulis, Teltonika Inc.
23 * Implementation of I2C Adapter/Algorithm Driver
24 * for I2C Bus integrated in Freescale i.MX/MXC processors
26 * Derived from Motorola GSG China I2C example driver
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>
35 /** Includes *******************************************************************
36 *******************************************************************************/
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/err.h>
43 #include <linux/interrupt.h>
44 #include <linux/delay.h>
45 #include <linux/i2c.h>
47 #include <linux/sched.h>
48 #include <linux/platform_device.h>
49 #include <linux/clk.h>
50 #include <linux/slab.h>
52 #include <mach/irqs.h>
53 #include <mach/hardware.h>
56 /** Defines ********************************************************************
57 *******************************************************************************/
59 /* This will be the driver name the kernel reports */
60 #define DRIVER_NAME "imx-i2c"
63 #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
65 /* IMX I2C registers */
66 #define IMX_I2C_IADR 0x00 /* i2c slave address */
67 #define IMX_I2C_IFDR 0x04 /* i2c frequency divider */
68 #define IMX_I2C_I2CR 0x08 /* i2c control */
69 #define IMX_I2C_I2SR 0x0C /* i2c status */
70 #define IMX_I2C_I2DR 0x10 /* i2c transfer data */
72 /* Bits of IMX I2C registers */
73 #define I2SR_RXAK 0x01
78 #define I2SR_IAAS 0x40
80 #define I2CR_RSTA 0x04
81 #define I2CR_TXAK 0x08
83 #define I2CR_MSTA 0x20
84 #define I2CR_IIEN 0x40
87 /** Variables ******************************************************************
88 *******************************************************************************/
91 * sorted list of clock divider, register value pairs
92 * taken from table 26-5, p.26-9, Freescale i.MX
93 * Integrated Portable System Processor Reference Manual
94 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
96 * Duplicated divider values removed from list
99 static u16 __initdata i2c_clk_div
[50][2] = {
100 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
101 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
102 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
103 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
104 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
105 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
106 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
107 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
108 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
109 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
110 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
111 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
112 { 3072, 0x1E }, { 3840, 0x1F }
115 struct imx_i2c_struct
{
116 struct i2c_adapter adapter
;
117 struct resource
*res
;
121 wait_queue_head_t queue
;
123 unsigned int disable_delay
;
125 unsigned int ifdr
; /* IMX_I2C_IFDR */
128 /** Functions for IMX I2C adapter driver ***************************************
129 *******************************************************************************/
131 static int i2c_imx_bus_busy(struct imx_i2c_struct
*i2c_imx
, int for_busy
)
133 unsigned long orig_jiffies
= jiffies
;
136 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s>\n", __func__
);
139 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2SR
);
140 if (for_busy
&& (temp
& I2SR_IBB
))
142 if (!for_busy
&& !(temp
& I2SR_IBB
))
144 if (signal_pending(current
)) {
145 dev_dbg(&i2c_imx
->adapter
.dev
,
146 "<%s> I2C Interrupted\n", __func__
);
149 if (time_after(jiffies
, orig_jiffies
+ msecs_to_jiffies(500))) {
150 dev_dbg(&i2c_imx
->adapter
.dev
,
151 "<%s> I2C bus is busy\n", __func__
);
160 static int i2c_imx_trx_complete(struct imx_i2c_struct
*i2c_imx
)
164 result
= wait_event_interruptible_timeout(i2c_imx
->queue
,
165 i2c_imx
->i2csr
& I2SR_IIF
, HZ
/ 10);
167 if (unlikely(result
< 0)) {
168 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> result < 0\n", __func__
);
170 } else if (unlikely(!(i2c_imx
->i2csr
& I2SR_IIF
))) {
171 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> Timeout\n", __func__
);
174 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> TRX complete\n", __func__
);
179 static int i2c_imx_acked(struct imx_i2c_struct
*i2c_imx
)
181 if (readb(i2c_imx
->base
+ IMX_I2C_I2SR
) & I2SR_RXAK
) {
182 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> No ACK\n", __func__
);
183 return -EIO
; /* No ACK */
186 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> ACK received\n", __func__
);
190 static int i2c_imx_start(struct imx_i2c_struct
*i2c_imx
)
192 unsigned int temp
= 0;
195 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s>\n", __func__
);
197 clk_enable(i2c_imx
->clk
);
198 writeb(i2c_imx
->ifdr
, i2c_imx
->base
+ IMX_I2C_IFDR
);
199 /* Enable I2C controller */
200 writeb(0, i2c_imx
->base
+ IMX_I2C_I2SR
);
201 writeb(I2CR_IEN
, i2c_imx
->base
+ IMX_I2C_I2CR
);
203 /* Wait controller to be stable */
206 /* Start I2C transaction */
207 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
209 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
210 result
= i2c_imx_bus_busy(i2c_imx
, 1);
213 i2c_imx
->stopped
= 0;
215 temp
|= I2CR_IIEN
| I2CR_MTX
| I2CR_TXAK
;
216 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
220 static void i2c_imx_stop(struct imx_i2c_struct
*i2c_imx
)
222 unsigned int temp
= 0;
224 if (!i2c_imx
->stopped
) {
225 /* Stop I2C transaction */
226 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s>\n", __func__
);
227 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
228 temp
&= ~(I2CR_MSTA
| I2CR_MTX
);
229 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
233 * This delay caused by an i.MXL hardware bug.
234 * If no (or too short) delay, no "STOP" bit will be generated.
236 udelay(i2c_imx
->disable_delay
);
239 if (!i2c_imx
->stopped
) {
240 i2c_imx_bus_busy(i2c_imx
, 0);
241 i2c_imx
->stopped
= 1;
244 /* Disable I2C controller */
245 writeb(0, i2c_imx
->base
+ IMX_I2C_I2CR
);
246 clk_disable(i2c_imx
->clk
);
249 static void __init
i2c_imx_set_clk(struct imx_i2c_struct
*i2c_imx
,
252 unsigned int i2c_clk_rate
;
256 /* Divider value calculation */
257 i2c_clk_rate
= clk_get_rate(i2c_imx
->clk
);
258 div
= (i2c_clk_rate
+ rate
- 1) / rate
;
259 if (div
< i2c_clk_div
[0][0])
261 else if (div
> i2c_clk_div
[ARRAY_SIZE(i2c_clk_div
) - 1][0])
262 i
= ARRAY_SIZE(i2c_clk_div
) - 1;
264 for (i
= 0; i2c_clk_div
[i
][0] < div
; i
++);
266 /* Store divider value */
267 i2c_imx
->ifdr
= i2c_clk_div
[i
][1];
270 * There dummy delay is calculated.
271 * It should be about one I2C clock period long.
272 * This delay is used in I2C bus disable function
273 * to fix chip hardware bug.
275 i2c_imx
->disable_delay
= (500000U * i2c_clk_div
[i
][0]
276 + (i2c_clk_rate
/ 2) - 1) / (i2c_clk_rate
/ 2);
278 /* dev_dbg() can't be used, because adapter is not yet registered */
279 #ifdef CONFIG_I2C_DEBUG_BUS
280 printk(KERN_DEBUG
"I2C: <%s> I2C_CLK=%d, REQ DIV=%d\n",
281 __func__
, i2c_clk_rate
, div
);
282 printk(KERN_DEBUG
"I2C: <%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
283 __func__
, i2c_clk_div
[i
][1], i2c_clk_div
[i
][0]);
287 static irqreturn_t
i2c_imx_isr(int irq
, void *dev_id
)
289 struct imx_i2c_struct
*i2c_imx
= dev_id
;
292 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2SR
);
293 if (temp
& I2SR_IIF
) {
294 /* save status register */
295 i2c_imx
->i2csr
= temp
;
297 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2SR
);
298 wake_up_interruptible(&i2c_imx
->queue
);
305 static int i2c_imx_write(struct imx_i2c_struct
*i2c_imx
, struct i2c_msg
*msgs
)
309 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> write slave address: addr=0x%x\n",
310 __func__
, msgs
->addr
<< 1);
312 /* write slave address */
313 writeb(msgs
->addr
<< 1, i2c_imx
->base
+ IMX_I2C_I2DR
);
314 result
= i2c_imx_trx_complete(i2c_imx
);
317 result
= i2c_imx_acked(i2c_imx
);
320 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> write data\n", __func__
);
323 for (i
= 0; i
< msgs
->len
; i
++) {
324 dev_dbg(&i2c_imx
->adapter
.dev
,
325 "<%s> write byte: B%d=0x%X\n",
326 __func__
, i
, msgs
->buf
[i
]);
327 writeb(msgs
->buf
[i
], i2c_imx
->base
+ IMX_I2C_I2DR
);
328 result
= i2c_imx_trx_complete(i2c_imx
);
331 result
= i2c_imx_acked(i2c_imx
);
338 static int i2c_imx_read(struct imx_i2c_struct
*i2c_imx
, struct i2c_msg
*msgs
)
343 dev_dbg(&i2c_imx
->adapter
.dev
,
344 "<%s> write slave address: addr=0x%x\n",
345 __func__
, (msgs
->addr
<< 1) | 0x01);
347 /* write slave address */
348 writeb((msgs
->addr
<< 1) | 0x01, i2c_imx
->base
+ IMX_I2C_I2DR
);
349 result
= i2c_imx_trx_complete(i2c_imx
);
352 result
= i2c_imx_acked(i2c_imx
);
356 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> setup bus\n", __func__
);
358 /* setup bus to read data */
359 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
363 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
364 readb(i2c_imx
->base
+ IMX_I2C_I2DR
); /* dummy read */
366 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> read data\n", __func__
);
369 for (i
= 0; i
< msgs
->len
; i
++) {
370 result
= i2c_imx_trx_complete(i2c_imx
);
373 if (i
== (msgs
->len
- 1)) {
374 /* It must generate STOP before read I2DR to prevent
375 controller from generating another clock cycle */
376 dev_dbg(&i2c_imx
->adapter
.dev
,
377 "<%s> clear MSTA\n", __func__
);
378 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
379 temp
&= ~(I2CR_MSTA
| I2CR_MTX
);
380 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
381 i2c_imx_bus_busy(i2c_imx
, 0);
382 i2c_imx
->stopped
= 1;
383 } else if (i
== (msgs
->len
- 2)) {
384 dev_dbg(&i2c_imx
->adapter
.dev
,
385 "<%s> set TXAK\n", __func__
);
386 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
388 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
390 msgs
->buf
[i
] = readb(i2c_imx
->base
+ IMX_I2C_I2DR
);
391 dev_dbg(&i2c_imx
->adapter
.dev
,
392 "<%s> read byte: B%d=0x%X\n",
393 __func__
, i
, msgs
->buf
[i
]);
398 static int i2c_imx_xfer(struct i2c_adapter
*adapter
,
399 struct i2c_msg
*msgs
, int num
)
401 unsigned int i
, temp
;
403 struct imx_i2c_struct
*i2c_imx
= i2c_get_adapdata(adapter
);
405 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s>\n", __func__
);
407 /* Start I2C transfer */
408 result
= i2c_imx_start(i2c_imx
);
412 /* read/write data */
413 for (i
= 0; i
< num
; i
++) {
415 dev_dbg(&i2c_imx
->adapter
.dev
,
416 "<%s> repeated start\n", __func__
);
417 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
419 writeb(temp
, i2c_imx
->base
+ IMX_I2C_I2CR
);
420 result
= i2c_imx_bus_busy(i2c_imx
, 1);
424 dev_dbg(&i2c_imx
->adapter
.dev
,
425 "<%s> transfer message: %d\n", __func__
, i
);
426 /* write/read data */
427 #ifdef CONFIG_I2C_DEBUG_BUS
428 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2CR
);
429 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> CONTROL: IEN=%d, IIEN=%d, "
430 "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__
,
431 (temp
& I2CR_IEN
? 1 : 0), (temp
& I2CR_IIEN
? 1 : 0),
432 (temp
& I2CR_MSTA
? 1 : 0), (temp
& I2CR_MTX
? 1 : 0),
433 (temp
& I2CR_TXAK
? 1 : 0), (temp
& I2CR_RSTA
? 1 : 0));
434 temp
= readb(i2c_imx
->base
+ IMX_I2C_I2SR
);
435 dev_dbg(&i2c_imx
->adapter
.dev
,
436 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
437 "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__
,
438 (temp
& I2SR_ICF
? 1 : 0), (temp
& I2SR_IAAS
? 1 : 0),
439 (temp
& I2SR_IBB
? 1 : 0), (temp
& I2SR_IAL
? 1 : 0),
440 (temp
& I2SR_SRW
? 1 : 0), (temp
& I2SR_IIF
? 1 : 0),
441 (temp
& I2SR_RXAK
? 1 : 0));
443 if (msgs
[i
].flags
& I2C_M_RD
)
444 result
= i2c_imx_read(i2c_imx
, &msgs
[i
]);
446 result
= i2c_imx_write(i2c_imx
, &msgs
[i
]);
452 /* Stop I2C transfer */
453 i2c_imx_stop(i2c_imx
);
455 dev_dbg(&i2c_imx
->adapter
.dev
, "<%s> exit with: %s: %d\n", __func__
,
456 (result
< 0) ? "error" : "success msg",
457 (result
< 0) ? result
: num
);
458 return (result
< 0) ? result
: num
;
461 static u32
i2c_imx_func(struct i2c_adapter
*adapter
)
463 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
466 static struct i2c_algorithm i2c_imx_algo
= {
467 .master_xfer
= i2c_imx_xfer
,
468 .functionality
= i2c_imx_func
,
471 static int __init
i2c_imx_probe(struct platform_device
*pdev
)
473 struct imx_i2c_struct
*i2c_imx
;
474 struct resource
*res
;
475 struct imxi2c_platform_data
*pdata
;
477 resource_size_t res_size
;
481 dev_dbg(&pdev
->dev
, "<%s>\n", __func__
);
483 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
485 dev_err(&pdev
->dev
, "can't get device resources\n");
488 irq
= platform_get_irq(pdev
, 0);
490 dev_err(&pdev
->dev
, "can't get irq number\n");
494 pdata
= pdev
->dev
.platform_data
;
496 if (pdata
&& pdata
->init
) {
497 ret
= pdata
->init(&pdev
->dev
);
502 res_size
= resource_size(res
);
504 if (!request_mem_region(res
->start
, res_size
, DRIVER_NAME
)) {
509 base
= ioremap(res
->start
, res_size
);
511 dev_err(&pdev
->dev
, "ioremap failed\n");
516 i2c_imx
= kzalloc(sizeof(struct imx_i2c_struct
), GFP_KERNEL
);
518 dev_err(&pdev
->dev
, "can't allocate interface\n");
523 /* Setup i2c_imx driver structure */
524 strcpy(i2c_imx
->adapter
.name
, pdev
->name
);
525 i2c_imx
->adapter
.owner
= THIS_MODULE
;
526 i2c_imx
->adapter
.algo
= &i2c_imx_algo
;
527 i2c_imx
->adapter
.dev
.parent
= &pdev
->dev
;
528 i2c_imx
->adapter
.nr
= pdev
->id
;
530 i2c_imx
->base
= base
;
534 i2c_imx
->clk
= clk_get(&pdev
->dev
, "i2c_clk");
535 if (IS_ERR(i2c_imx
->clk
)) {
536 ret
= PTR_ERR(i2c_imx
->clk
);
537 dev_err(&pdev
->dev
, "can't get I2C clock\n");
542 ret
= request_irq(i2c_imx
->irq
, i2c_imx_isr
, 0, pdev
->name
, i2c_imx
);
544 dev_err(&pdev
->dev
, "can't claim irq %d\n", i2c_imx
->irq
);
549 init_waitqueue_head(&i2c_imx
->queue
);
551 /* Set up adapter data */
552 i2c_set_adapdata(&i2c_imx
->adapter
, i2c_imx
);
554 /* Set up clock divider */
555 if (pdata
&& pdata
->bitrate
)
556 i2c_imx_set_clk(i2c_imx
, pdata
->bitrate
);
558 i2c_imx_set_clk(i2c_imx
, IMX_I2C_BIT_RATE
);
560 /* Set up chip registers to defaults */
561 writeb(0, i2c_imx
->base
+ IMX_I2C_I2CR
);
562 writeb(0, i2c_imx
->base
+ IMX_I2C_I2SR
);
564 /* Add I2C adapter */
565 ret
= i2c_add_numbered_adapter(&i2c_imx
->adapter
);
567 dev_err(&pdev
->dev
, "registration failed\n");
571 /* Set up platform driver data */
572 platform_set_drvdata(pdev
, i2c_imx
);
574 dev_dbg(&i2c_imx
->adapter
.dev
, "claimed irq %d\n", i2c_imx
->irq
);
575 dev_dbg(&i2c_imx
->adapter
.dev
, "device resources from 0x%x to 0x%x\n",
576 i2c_imx
->res
->start
, i2c_imx
->res
->end
);
577 dev_dbg(&i2c_imx
->adapter
.dev
, "allocated %d bytes at 0x%x \n",
578 res_size
, i2c_imx
->res
->start
);
579 dev_dbg(&i2c_imx
->adapter
.dev
, "adapter name: \"%s\"\n",
580 i2c_imx
->adapter
.name
);
581 dev_dbg(&i2c_imx
->adapter
.dev
, "IMX I2C adapter registered\n");
583 return 0; /* Return OK */
586 free_irq(i2c_imx
->irq
, i2c_imx
);
588 clk_put(i2c_imx
->clk
);
594 release_mem_region(res
->start
, resource_size(res
));
596 if (pdata
&& pdata
->exit
)
597 pdata
->exit(&pdev
->dev
);
598 return ret
; /* Return error number */
601 static int __exit
i2c_imx_remove(struct platform_device
*pdev
)
603 struct imx_i2c_struct
*i2c_imx
= platform_get_drvdata(pdev
);
604 struct imxi2c_platform_data
*pdata
= pdev
->dev
.platform_data
;
607 dev_dbg(&i2c_imx
->adapter
.dev
, "adapter removed\n");
608 i2c_del_adapter(&i2c_imx
->adapter
);
609 platform_set_drvdata(pdev
, NULL
);
612 free_irq(i2c_imx
->irq
, i2c_imx
);
614 /* setup chip registers to defaults */
615 writeb(0, i2c_imx
->base
+ IMX_I2C_IADR
);
616 writeb(0, i2c_imx
->base
+ IMX_I2C_IFDR
);
617 writeb(0, i2c_imx
->base
+ IMX_I2C_I2CR
);
618 writeb(0, i2c_imx
->base
+ IMX_I2C_I2SR
);
620 /* Shut down hardware */
621 if (pdata
&& pdata
->exit
)
622 pdata
->exit(&pdev
->dev
);
624 clk_put(i2c_imx
->clk
);
626 iounmap(i2c_imx
->base
);
627 release_mem_region(i2c_imx
->res
->start
, resource_size(i2c_imx
->res
));
632 static struct platform_driver i2c_imx_driver
= {
633 .remove
= __exit_p(i2c_imx_remove
),
636 .owner
= THIS_MODULE
,
640 static int __init
i2c_adap_imx_init(void)
642 return platform_driver_probe(&i2c_imx_driver
, i2c_imx_probe
);
644 subsys_initcall(i2c_adap_imx_init
);
646 static void __exit
i2c_adap_imx_exit(void)
648 platform_driver_unregister(&i2c_imx_driver
);
650 module_exit(i2c_adap_imx_exit
);
652 MODULE_LICENSE("GPL");
653 MODULE_AUTHOR("Darius Augulis");
654 MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
655 MODULE_ALIAS("platform:" DRIVER_NAME
);