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>
51 #include <mach/irqs.h>
52 #include <mach/hardware.h>
55 /** Defines ********************************************************************
56 *******************************************************************************/
58 /* This will be the driver name the kernel reports */
59 #define DRIVER_NAME "imx-i2c"
62 #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
64 /* IMX I2C registers */
65 #define IMX_I2C_IADR 0x00 /* i2c slave address */
66 #define IMX_I2C_IFDR 0x04 /* i2c frequency divider */
67 #define IMX_I2C_I2CR 0x08 /* i2c control */
68 #define IMX_I2C_I2SR 0x0C /* i2c status */
69 #define IMX_I2C_I2DR 0x10 /* i2c transfer data */
71 /* Bits of IMX I2C registers */
72 #define I2SR_RXAK 0x01
77 #define I2SR_IAAS 0x40
79 #define I2CR_RSTA 0x04
80 #define I2CR_TXAK 0x08
82 #define I2CR_MSTA 0x20
83 #define I2CR_IIEN 0x40
86 /** Variables ******************************************************************
87 *******************************************************************************/
90 * sorted list of clock divider, register value pairs
91 * taken from table 26-5, p.26-9, Freescale i.MX
92 * Integrated Portable System Processor Reference Manual
93 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
95 * Duplicated divider values removed from list
98 static u16 __initdata i2c_clk_div[50][2] = {
99 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
100 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
101 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
102 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
103 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
104 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
105 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
106 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
107 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
108 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
109 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
110 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
111 { 3072, 0x1E }, { 3840, 0x1F }
114 struct imx_i2c_struct {
115 struct i2c_adapter adapter;
116 struct resource *res;
120 wait_queue_head_t queue;
122 unsigned int disable_delay;
126 /** Functions for IMX I2C adapter driver ***************************************
127 *******************************************************************************/
129 static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
131 unsigned long orig_jiffies = jiffies;
134 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
137 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
138 if (for_busy && (temp & I2SR_IBB))
140 if (!for_busy && !(temp & I2SR_IBB))
142 if (signal_pending(current)) {
143 dev_dbg(&i2c_imx->adapter.dev,
144 "<%s> I2C Interrupted\n", __func__);
147 if (time_after(jiffies, orig_jiffies + HZ / 1000)) {
148 dev_dbg(&i2c_imx->adapter.dev,
149 "<%s> I2C bus is busy\n", __func__);
158 static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
162 result = wait_event_interruptible_timeout(i2c_imx->queue,
163 i2c_imx->i2csr & I2SR_IIF, HZ / 10);
165 if (unlikely(result < 0)) {
166 dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__);
168 } else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
169 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
172 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
177 static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
179 if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) {
180 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
181 return -EIO; /* No ACK */
184 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
188 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
190 unsigned int temp = 0;
193 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
195 /* Enable I2C controller */
196 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
197 writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
199 /* Wait controller to be stable */
202 /* Start I2C transaction */
203 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
205 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
206 result = i2c_imx_bus_busy(i2c_imx, 1);
209 i2c_imx->stopped = 0;
211 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
212 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
216 static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
218 unsigned int temp = 0;
220 if (!i2c_imx->stopped) {
221 /* Stop I2C transaction */
222 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
223 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
224 temp &= ~(I2CR_MSTA | I2CR_MTX);
225 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
226 i2c_imx->stopped = 1;
229 * This delay caused by an i.MXL hardware bug.
230 * If no (or too short) delay, no "STOP" bit will be generated.
232 udelay(i2c_imx->disable_delay);
234 if (!i2c_imx->stopped)
235 i2c_imx_bus_busy(i2c_imx, 0);
237 /* Disable I2C controller */
238 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
241 static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
244 unsigned int i2c_clk_rate;
248 /* Divider value calculation */
249 i2c_clk_rate = clk_get_rate(i2c_imx->clk);
250 div = (i2c_clk_rate + rate - 1) / rate;
251 if (div < i2c_clk_div[0][0])
253 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
254 i = ARRAY_SIZE(i2c_clk_div) - 1;
256 for (i = 0; i2c_clk_div[i][0] < div; i++);
258 /* Write divider value to register */
259 writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);
262 * There dummy delay is calculated.
263 * It should be about one I2C clock period long.
264 * This delay is used in I2C bus disable function
265 * to fix chip hardware bug.
267 i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
268 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
270 /* dev_dbg() can't be used, because adapter is not yet registered */
271 #ifdef CONFIG_I2C_DEBUG_BUS
272 printk(KERN_DEBUG "I2C: <%s> I2C_CLK=%d, REQ DIV=%d\n",
273 __func__, i2c_clk_rate, div);
274 printk(KERN_DEBUG "I2C: <%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
275 __func__, i2c_clk_div[i][1], i2c_clk_div[i][0]);
279 static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
281 struct imx_i2c_struct *i2c_imx = dev_id;
284 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
285 if (temp & I2SR_IIF) {
286 /* save status register */
287 i2c_imx->i2csr = temp;
289 writeb(temp, i2c_imx->base + IMX_I2C_I2SR);
290 wake_up_interruptible(&i2c_imx->queue);
297 static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
301 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
302 __func__, msgs->addr << 1);
304 /* write slave address */
305 writeb(msgs->addr << 1, i2c_imx->base + IMX_I2C_I2DR);
306 result = i2c_imx_trx_complete(i2c_imx);
309 result = i2c_imx_acked(i2c_imx);
312 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
315 for (i = 0; i < msgs->len; i++) {
316 dev_dbg(&i2c_imx->adapter.dev,
317 "<%s> write byte: B%d=0x%X\n",
318 __func__, i, msgs->buf[i]);
319 writeb(msgs->buf[i], i2c_imx->base + IMX_I2C_I2DR);
320 result = i2c_imx_trx_complete(i2c_imx);
323 result = i2c_imx_acked(i2c_imx);
330 static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
335 dev_dbg(&i2c_imx->adapter.dev,
336 "<%s> write slave address: addr=0x%x\n",
337 __func__, (msgs->addr << 1) | 0x01);
339 /* write slave address */
340 writeb((msgs->addr << 1) | 0x01, i2c_imx->base + IMX_I2C_I2DR);
341 result = i2c_imx_trx_complete(i2c_imx);
344 result = i2c_imx_acked(i2c_imx);
348 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
350 /* setup bus to read data */
351 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
355 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
356 readb(i2c_imx->base + IMX_I2C_I2DR); /* dummy read */
358 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
361 for (i = 0; i < msgs->len; i++) {
362 result = i2c_imx_trx_complete(i2c_imx);
365 if (i == (msgs->len - 1)) {
366 /* It must generate STOP before read I2DR to prevent
367 controller from generating another clock cycle */
368 dev_dbg(&i2c_imx->adapter.dev,
369 "<%s> clear MSTA\n", __func__);
370 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
371 temp &= ~(I2CR_MSTA | I2CR_MTX);
372 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
373 i2c_imx_bus_busy(i2c_imx, 0);
374 i2c_imx->stopped = 1;
375 } else if (i == (msgs->len - 2)) {
376 dev_dbg(&i2c_imx->adapter.dev,
377 "<%s> set TXAK\n", __func__);
378 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
380 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
382 msgs->buf[i] = readb(i2c_imx->base + IMX_I2C_I2DR);
383 dev_dbg(&i2c_imx->adapter.dev,
384 "<%s> read byte: B%d=0x%X\n",
385 __func__, i, msgs->buf[i]);
390 static int i2c_imx_xfer(struct i2c_adapter *adapter,
391 struct i2c_msg *msgs, int num)
393 unsigned int i, temp;
395 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
397 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
399 /* Start I2C transfer */
400 result = i2c_imx_start(i2c_imx);
404 /* read/write data */
405 for (i = 0; i < num; i++) {
407 dev_dbg(&i2c_imx->adapter.dev,
408 "<%s> repeated start\n", __func__);
409 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
411 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
412 result = i2c_imx_bus_busy(i2c_imx, 1);
416 dev_dbg(&i2c_imx->adapter.dev,
417 "<%s> transfer message: %d\n", __func__, i);
418 /* write/read data */
419 #ifdef CONFIG_I2C_DEBUG_BUS
420 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
421 dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
422 "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
423 (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
424 (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
425 (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
426 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
427 dev_dbg(&i2c_imx->adapter.dev,
428 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
429 "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
430 (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
431 (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
432 (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
433 (temp & I2SR_RXAK ? 1 : 0));
435 if (msgs[i].flags & I2C_M_RD)
436 result = i2c_imx_read(i2c_imx, &msgs[i]);
438 result = i2c_imx_write(i2c_imx, &msgs[i]);
442 /* Stop I2C transfer */
443 i2c_imx_stop(i2c_imx);
445 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
446 (result < 0) ? "error" : "success msg",
447 (result < 0) ? result : num);
448 return (result < 0) ? result : num;
451 static u32 i2c_imx_func(struct i2c_adapter *adapter)
453 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
456 static struct i2c_algorithm i2c_imx_algo = {
457 .master_xfer = i2c_imx_xfer,
458 .functionality = i2c_imx_func,
461 static int __init i2c_imx_probe(struct platform_device *pdev)
463 struct imx_i2c_struct *i2c_imx;
464 struct resource *res;
465 struct imxi2c_platform_data *pdata;
467 resource_size_t res_size;
471 dev_dbg(&pdev->dev, "<%s>\n", __func__);
473 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475 dev_err(&pdev->dev, "can't get device resources\n");
478 irq = platform_get_irq(pdev, 0);
480 dev_err(&pdev->dev, "can't get irq number\n");
484 pdata = pdev->dev.platform_data;
486 if (pdata && pdata->init) {
487 ret = pdata->init(&pdev->dev);
492 res_size = resource_size(res);
493 base = ioremap(res->start, res_size);
495 dev_err(&pdev->dev, "ioremap failed\n");
500 i2c_imx = kzalloc(sizeof(struct imx_i2c_struct), GFP_KERNEL);
502 dev_err(&pdev->dev, "can't allocate interface\n");
507 if (!request_mem_region(res->start, res_size, DRIVER_NAME)) {
512 /* Setup i2c_imx driver structure */
513 strcpy(i2c_imx->adapter.name, pdev->name);
514 i2c_imx->adapter.owner = THIS_MODULE;
515 i2c_imx->adapter.algo = &i2c_imx_algo;
516 i2c_imx->adapter.dev.parent = &pdev->dev;
517 i2c_imx->adapter.nr = pdev->id;
519 i2c_imx->base = base;
523 i2c_imx->clk = clk_get(&pdev->dev, "i2c_clk");
524 if (IS_ERR(i2c_imx->clk)) {
525 ret = PTR_ERR(i2c_imx->clk);
526 dev_err(&pdev->dev, "can't get I2C clock\n");
529 clk_enable(i2c_imx->clk);
532 ret = request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);
534 dev_err(&pdev->dev, "can't claim irq %d\n", i2c_imx->irq);
539 init_waitqueue_head(&i2c_imx->queue);
541 /* Set up adapter data */
542 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
544 /* Set up clock divider */
545 if (pdata && pdata->bitrate)
546 i2c_imx_set_clk(i2c_imx, pdata->bitrate);
548 i2c_imx_set_clk(i2c_imx, IMX_I2C_BIT_RATE);
550 /* Set up chip registers to defaults */
551 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
552 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
554 /* Add I2C adapter */
555 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
557 dev_err(&pdev->dev, "registration failed\n");
561 /* Set up platform driver data */
562 platform_set_drvdata(pdev, i2c_imx);
564 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", i2c_imx->irq);
565 dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
566 i2c_imx->res->start, i2c_imx->res->end);
567 dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x \n",
568 res_size, i2c_imx->res->start);
569 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
570 i2c_imx->adapter.name);
571 dev_dbg(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
573 return 0; /* Return OK */
576 free_irq(i2c_imx->irq, i2c_imx);
578 clk_disable(i2c_imx->clk);
579 clk_put(i2c_imx->clk);
581 release_mem_region(i2c_imx->res->start, resource_size(res));
587 if (pdata && pdata->exit)
588 pdata->exit(&pdev->dev);
589 return ret; /* Return error number */
592 static int __exit i2c_imx_remove(struct platform_device *pdev)
594 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
595 struct imxi2c_platform_data *pdata = pdev->dev.platform_data;
598 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
599 i2c_del_adapter(&i2c_imx->adapter);
600 platform_set_drvdata(pdev, NULL);
603 free_irq(i2c_imx->irq, i2c_imx);
605 /* setup chip registers to defaults */
606 writeb(0, i2c_imx->base + IMX_I2C_IADR);
607 writeb(0, i2c_imx->base + IMX_I2C_IFDR);
608 writeb(0, i2c_imx->base + IMX_I2C_I2CR);
609 writeb(0, i2c_imx->base + IMX_I2C_I2SR);
611 /* Shut down hardware */
612 if (pdata && pdata->exit)
613 pdata->exit(&pdev->dev);
615 /* Disable I2C clock */
616 clk_disable(i2c_imx->clk);
617 clk_put(i2c_imx->clk);
619 release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res));
620 iounmap(i2c_imx->base);
625 static struct platform_driver i2c_imx_driver = {
626 .probe = i2c_imx_probe,
627 .remove = __exit_p(i2c_imx_remove),
630 .owner = THIS_MODULE,
634 static int __init i2c_adap_imx_init(void)
636 return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe);
638 subsys_initcall(i2c_adap_imx_init);
640 static void __exit i2c_adap_imx_exit(void)
642 platform_driver_unregister(&i2c_imx_driver);
644 module_exit(i2c_adap_imx_exit);
646 MODULE_LICENSE("GPL");
647 MODULE_AUTHOR("Darius Augulis");
648 MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
649 MODULE_ALIAS("platform:" DRIVER_NAME);