i2c: imx: only imx1 needs disable delay
[linux-2.6.git] / drivers / i2c / busses / i2c-imx.c
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  *
33  */
34
35 /** Includes *******************************************************************
36 *******************************************************************************/
37
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>
46 #include <linux/io.h>
47 #include <linux/sched.h>
48 #include <linux/platform_device.h>
49 #include <linux/clk.h>
50
51 #include <mach/irqs.h>
52 #include <mach/hardware.h>
53 #include <mach/i2c.h>
54
55 /** Defines ********************************************************************
56 *******************************************************************************/
57
58 /* This will be the driver name the kernel reports */
59 #define DRIVER_NAME "imx-i2c"
60
61 /* Default value */
62 #define IMX_I2C_BIT_RATE        100000  /* 100kHz */
63
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 */
70
71 /* Bits of IMX I2C registers */
72 #define I2SR_RXAK       0x01
73 #define I2SR_IIF        0x02
74 #define I2SR_SRW        0x04
75 #define I2SR_IAL        0x10
76 #define I2SR_IBB        0x20
77 #define I2SR_IAAS       0x40
78 #define I2SR_ICF        0x80
79 #define I2CR_RSTA       0x04
80 #define I2CR_TXAK       0x08
81 #define I2CR_MTX        0x10
82 #define I2CR_MSTA       0x20
83 #define I2CR_IIEN       0x40
84 #define I2CR_IEN        0x80
85
86 /** Variables ******************************************************************
87 *******************************************************************************/
88
89 /*
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
94  *
95  * Duplicated divider values removed from list
96  */
97
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 }
112 };
113
114 struct imx_i2c_struct {
115         struct i2c_adapter      adapter;
116         struct resource         *res;
117         struct clk              *clk;
118         void __iomem            *base;
119         int                     irq;
120         wait_queue_head_t       queue;
121         unsigned long           i2csr;
122         unsigned int            disable_delay;
123         int                     stopped;
124 };
125
126 /** Functions for IMX I2C adapter driver ***************************************
127 *******************************************************************************/
128
129 static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
130 {
131         unsigned long orig_jiffies = jiffies;
132         unsigned int temp;
133
134         dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
135
136         while (1) {
137                 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
138                 if (for_busy && (temp & I2SR_IBB))
139                         break;
140                 if (!for_busy && !(temp & I2SR_IBB))
141                         break;
142                 if (signal_pending(current)) {
143                         dev_dbg(&i2c_imx->adapter.dev,
144                                 "<%s> I2C Interrupted\n", __func__);
145                         return -EINTR;
146                 }
147                 if (time_after(jiffies, orig_jiffies + HZ / 1000)) {
148                         dev_dbg(&i2c_imx->adapter.dev,
149                                 "<%s> I2C bus is busy\n", __func__);
150                         return -EIO;
151                 }
152                 schedule();
153         }
154
155         return 0;
156 }
157
158 static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
159 {
160         int result;
161
162         result = wait_event_interruptible_timeout(i2c_imx->queue,
163                 i2c_imx->i2csr & I2SR_IIF, HZ / 10);
164
165         if (unlikely(result < 0)) {
166                 dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__);
167                 return result;
168         } else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
169                 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
170                 return -ETIMEDOUT;
171         }
172         dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
173         i2c_imx->i2csr = 0;
174         return 0;
175 }
176
177 static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
178 {
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 */
182         }
183
184         dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
185         return 0;
186 }
187
188 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
189 {
190         unsigned int temp = 0;
191         int result;
192
193         dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
194
195         /* Enable I2C controller */
196         writeb(0, i2c_imx->base + IMX_I2C_I2SR);
197         writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
198
199         /* Wait controller to be stable */
200         udelay(50);
201
202         /* Start I2C transaction */
203         temp = readb(i2c_imx->base + IMX_I2C_I2CR);
204         temp |= I2CR_MSTA;
205         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
206         result = i2c_imx_bus_busy(i2c_imx, 1);
207         if (result)
208                 return result;
209         i2c_imx->stopped = 0;
210
211         temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
212         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
213         return result;
214 }
215
216 static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
217 {
218         unsigned int temp = 0;
219
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;
227         }
228         if (cpu_is_mx1()) {
229                 /*
230                  * This delay caused by an i.MXL hardware bug.
231                  * If no (or too short) delay, no "STOP" bit will be generated.
232                  */
233                 udelay(i2c_imx->disable_delay);
234         }
235
236         if (!i2c_imx->stopped)
237                 i2c_imx_bus_busy(i2c_imx, 0);
238
239         /* Disable I2C controller */
240         writeb(0, i2c_imx->base + IMX_I2C_I2CR);
241 }
242
243 static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
244                                                         unsigned int rate)
245 {
246         unsigned int i2c_clk_rate;
247         unsigned int div;
248         int i;
249
250         /* Divider value calculation */
251         i2c_clk_rate = clk_get_rate(i2c_imx->clk);
252         div = (i2c_clk_rate + rate - 1) / rate;
253         if (div < i2c_clk_div[0][0])
254                 i = 0;
255         else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
256                 i = ARRAY_SIZE(i2c_clk_div) - 1;
257         else
258                 for (i = 0; i2c_clk_div[i][0] < div; i++);
259
260         /* Write divider value to register */
261         writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);
262
263         /*
264          * There dummy delay is calculated.
265          * It should be about one I2C clock period long.
266          * This delay is used in I2C bus disable function
267          * to fix chip hardware bug.
268          */
269         i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
270                 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
271
272         /* dev_dbg() can't be used, because adapter is not yet registered */
273 #ifdef CONFIG_I2C_DEBUG_BUS
274         printk(KERN_DEBUG "I2C: <%s> I2C_CLK=%d, REQ DIV=%d\n",
275                 __func__, i2c_clk_rate, div);
276         printk(KERN_DEBUG "I2C: <%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
277                 __func__, i2c_clk_div[i][1], i2c_clk_div[i][0]);
278 #endif
279 }
280
281 static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
282 {
283         struct imx_i2c_struct *i2c_imx = dev_id;
284         unsigned int temp;
285
286         temp = readb(i2c_imx->base + IMX_I2C_I2SR);
287         if (temp & I2SR_IIF) {
288                 /* save status register */
289                 i2c_imx->i2csr = temp;
290                 temp &= ~I2SR_IIF;
291                 writeb(temp, i2c_imx->base + IMX_I2C_I2SR);
292                 wake_up_interruptible(&i2c_imx->queue);
293                 return IRQ_HANDLED;
294         }
295
296         return IRQ_NONE;
297 }
298
299 static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
300 {
301         int i, result;
302
303         dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
304                 __func__, msgs->addr << 1);
305
306         /* write slave address */
307         writeb(msgs->addr << 1, i2c_imx->base + IMX_I2C_I2DR);
308         result = i2c_imx_trx_complete(i2c_imx);
309         if (result)
310                 return result;
311         result = i2c_imx_acked(i2c_imx);
312         if (result)
313                 return result;
314         dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
315
316         /* write data */
317         for (i = 0; i < msgs->len; i++) {
318                 dev_dbg(&i2c_imx->adapter.dev,
319                         "<%s> write byte: B%d=0x%X\n",
320                         __func__, i, msgs->buf[i]);
321                 writeb(msgs->buf[i], i2c_imx->base + IMX_I2C_I2DR);
322                 result = i2c_imx_trx_complete(i2c_imx);
323                 if (result)
324                         return result;
325                 result = i2c_imx_acked(i2c_imx);
326                 if (result)
327                         return result;
328         }
329         return 0;
330 }
331
332 static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
333 {
334         int i, result;
335         unsigned int temp;
336
337         dev_dbg(&i2c_imx->adapter.dev,
338                 "<%s> write slave address: addr=0x%x\n",
339                 __func__, (msgs->addr << 1) | 0x01);
340
341         /* write slave address */
342         writeb((msgs->addr << 1) | 0x01, i2c_imx->base + IMX_I2C_I2DR);
343         result = i2c_imx_trx_complete(i2c_imx);
344         if (result)
345                 return result;
346         result = i2c_imx_acked(i2c_imx);
347         if (result)
348                 return result;
349
350         dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
351
352         /* setup bus to read data */
353         temp = readb(i2c_imx->base + IMX_I2C_I2CR);
354         temp &= ~I2CR_MTX;
355         if (msgs->len - 1)
356                 temp &= ~I2CR_TXAK;
357         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
358         readb(i2c_imx->base + IMX_I2C_I2DR); /* dummy read */
359
360         dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
361
362         /* read data */
363         for (i = 0; i < msgs->len; i++) {
364                 result = i2c_imx_trx_complete(i2c_imx);
365                 if (result)
366                         return result;
367                 if (i == (msgs->len - 1)) {
368                         /* It must generate STOP before read I2DR to prevent
369                            controller from generating another clock cycle */
370                         dev_dbg(&i2c_imx->adapter.dev,
371                                 "<%s> clear MSTA\n", __func__);
372                         temp = readb(i2c_imx->base + IMX_I2C_I2CR);
373                         temp &= ~(I2CR_MSTA | I2CR_MTX);
374                         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
375                         i2c_imx_bus_busy(i2c_imx, 0);
376                         i2c_imx->stopped = 1;
377                 } else if (i == (msgs->len - 2)) {
378                         dev_dbg(&i2c_imx->adapter.dev,
379                                 "<%s> set TXAK\n", __func__);
380                         temp = readb(i2c_imx->base + IMX_I2C_I2CR);
381                         temp |= I2CR_TXAK;
382                         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
383                 }
384                 msgs->buf[i] = readb(i2c_imx->base + IMX_I2C_I2DR);
385                 dev_dbg(&i2c_imx->adapter.dev,
386                         "<%s> read byte: B%d=0x%X\n",
387                         __func__, i, msgs->buf[i]);
388         }
389         return 0;
390 }
391
392 static int i2c_imx_xfer(struct i2c_adapter *adapter,
393                                                 struct i2c_msg *msgs, int num)
394 {
395         unsigned int i, temp;
396         int result;
397         struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
398
399         dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
400
401         /* Start I2C transfer */
402         result = i2c_imx_start(i2c_imx);
403         if (result)
404                 goto fail0;
405
406         /* read/write data */
407         for (i = 0; i < num; i++) {
408                 if (i) {
409                         dev_dbg(&i2c_imx->adapter.dev,
410                                 "<%s> repeated start\n", __func__);
411                         temp = readb(i2c_imx->base + IMX_I2C_I2CR);
412                         temp |= I2CR_RSTA;
413                         writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
414                         result =  i2c_imx_bus_busy(i2c_imx, 1);
415                         if (result)
416                                 goto fail0;
417                 }
418                 dev_dbg(&i2c_imx->adapter.dev,
419                         "<%s> transfer message: %d\n", __func__, i);
420                 /* write/read data */
421 #ifdef CONFIG_I2C_DEBUG_BUS
422                 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
423                 dev_dbg(&i2c_imx->adapter.dev, "<%s> CONTROL: IEN=%d, IIEN=%d, "
424                         "MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n", __func__,
425                         (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
426                         (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
427                         (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
428                 temp = readb(i2c_imx->base + IMX_I2C_I2SR);
429                 dev_dbg(&i2c_imx->adapter.dev,
430                         "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, "
431                         "IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n", __func__,
432                         (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
433                         (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
434                         (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
435                         (temp & I2SR_RXAK ? 1 : 0));
436 #endif
437                 if (msgs[i].flags & I2C_M_RD)
438                         result = i2c_imx_read(i2c_imx, &msgs[i]);
439                 else
440                         result = i2c_imx_write(i2c_imx, &msgs[i]);
441         }
442
443 fail0:
444         /* Stop I2C transfer */
445         i2c_imx_stop(i2c_imx);
446
447         dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
448                 (result < 0) ? "error" : "success msg",
449                         (result < 0) ? result : num);
450         return (result < 0) ? result : num;
451 }
452
453 static u32 i2c_imx_func(struct i2c_adapter *adapter)
454 {
455         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
456 }
457
458 static struct i2c_algorithm i2c_imx_algo = {
459         .master_xfer    = i2c_imx_xfer,
460         .functionality  = i2c_imx_func,
461 };
462
463 static int __init i2c_imx_probe(struct platform_device *pdev)
464 {
465         struct imx_i2c_struct *i2c_imx;
466         struct resource *res;
467         struct imxi2c_platform_data *pdata;
468         void __iomem *base;
469         resource_size_t res_size;
470         int irq;
471         int ret;
472
473         dev_dbg(&pdev->dev, "<%s>\n", __func__);
474
475         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
476         if (!res) {
477                 dev_err(&pdev->dev, "can't get device resources\n");
478                 return -ENOENT;
479         }
480         irq = platform_get_irq(pdev, 0);
481         if (irq < 0) {
482                 dev_err(&pdev->dev, "can't get irq number\n");
483                 return -ENOENT;
484         }
485
486         pdata = pdev->dev.platform_data;
487
488         if (pdata && pdata->init) {
489                 ret = pdata->init(&pdev->dev);
490                 if (ret)
491                         return ret;
492         }
493
494         res_size = resource_size(res);
495         base = ioremap(res->start, res_size);
496         if (!base) {
497                 dev_err(&pdev->dev, "ioremap failed\n");
498                 ret = -EIO;
499                 goto fail0;
500         }
501
502         i2c_imx = kzalloc(sizeof(struct imx_i2c_struct), GFP_KERNEL);
503         if (!i2c_imx) {
504                 dev_err(&pdev->dev, "can't allocate interface\n");
505                 ret = -ENOMEM;
506                 goto fail1;
507         }
508
509         if (!request_mem_region(res->start, res_size, DRIVER_NAME)) {
510                 ret = -EBUSY;
511                 goto fail2;
512         }
513
514         /* Setup i2c_imx driver structure */
515         strcpy(i2c_imx->adapter.name, pdev->name);
516         i2c_imx->adapter.owner          = THIS_MODULE;
517         i2c_imx->adapter.algo           = &i2c_imx_algo;
518         i2c_imx->adapter.dev.parent     = &pdev->dev;
519         i2c_imx->adapter.nr             = pdev->id;
520         i2c_imx->irq                    = irq;
521         i2c_imx->base                   = base;
522         i2c_imx->res                    = res;
523
524         /* Get I2C clock */
525         i2c_imx->clk = clk_get(&pdev->dev, "i2c_clk");
526         if (IS_ERR(i2c_imx->clk)) {
527                 ret = PTR_ERR(i2c_imx->clk);
528                 dev_err(&pdev->dev, "can't get I2C clock\n");
529                 goto fail3;
530         }
531         clk_enable(i2c_imx->clk);
532
533         /* Request IRQ */
534         ret = request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);
535         if (ret) {
536                 dev_err(&pdev->dev, "can't claim irq %d\n", i2c_imx->irq);
537                 goto fail4;
538         }
539
540         /* Init queue */
541         init_waitqueue_head(&i2c_imx->queue);
542
543         /* Set up adapter data */
544         i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
545
546         /* Set up clock divider */
547         if (pdata && pdata->bitrate)
548                 i2c_imx_set_clk(i2c_imx, pdata->bitrate);
549         else
550                 i2c_imx_set_clk(i2c_imx, IMX_I2C_BIT_RATE);
551
552         /* Set up chip registers to defaults */
553         writeb(0, i2c_imx->base + IMX_I2C_I2CR);
554         writeb(0, i2c_imx->base + IMX_I2C_I2SR);
555
556         /* Add I2C adapter */
557         ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
558         if (ret < 0) {
559                 dev_err(&pdev->dev, "registration failed\n");
560                 goto fail5;
561         }
562
563         /* Set up platform driver data */
564         platform_set_drvdata(pdev, i2c_imx);
565
566         dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", i2c_imx->irq);
567         dev_dbg(&i2c_imx->adapter.dev, "device resources from 0x%x to 0x%x\n",
568                 i2c_imx->res->start, i2c_imx->res->end);
569         dev_dbg(&i2c_imx->adapter.dev, "allocated %d bytes at 0x%x \n",
570                 res_size, i2c_imx->res->start);
571         dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
572                 i2c_imx->adapter.name);
573         dev_dbg(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
574
575         return 0;   /* Return OK */
576
577 fail5:
578         free_irq(i2c_imx->irq, i2c_imx);
579 fail4:
580         clk_disable(i2c_imx->clk);
581         clk_put(i2c_imx->clk);
582 fail3:
583         release_mem_region(i2c_imx->res->start, resource_size(res));
584 fail2:
585         kfree(i2c_imx);
586 fail1:
587         iounmap(base);
588 fail0:
589         if (pdata && pdata->exit)
590                 pdata->exit(&pdev->dev);
591         return ret; /* Return error number */
592 }
593
594 static int __exit i2c_imx_remove(struct platform_device *pdev)
595 {
596         struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
597         struct imxi2c_platform_data *pdata = pdev->dev.platform_data;
598
599         /* remove adapter */
600         dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
601         i2c_del_adapter(&i2c_imx->adapter);
602         platform_set_drvdata(pdev, NULL);
603
604         /* free interrupt */
605         free_irq(i2c_imx->irq, i2c_imx);
606
607         /* setup chip registers to defaults */
608         writeb(0, i2c_imx->base + IMX_I2C_IADR);
609         writeb(0, i2c_imx->base + IMX_I2C_IFDR);
610         writeb(0, i2c_imx->base + IMX_I2C_I2CR);
611         writeb(0, i2c_imx->base + IMX_I2C_I2SR);
612
613         /* Shut down hardware */
614         if (pdata && pdata->exit)
615                 pdata->exit(&pdev->dev);
616
617         /* Disable I2C clock */
618         clk_disable(i2c_imx->clk);
619         clk_put(i2c_imx->clk);
620
621         release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res));
622         iounmap(i2c_imx->base);
623         kfree(i2c_imx);
624         return 0;
625 }
626
627 static struct platform_driver i2c_imx_driver = {
628         .probe          = i2c_imx_probe,
629         .remove         = __exit_p(i2c_imx_remove),
630         .driver = {
631                 .name   = DRIVER_NAME,
632                 .owner  = THIS_MODULE,
633         }
634 };
635
636 static int __init i2c_adap_imx_init(void)
637 {
638         return platform_driver_probe(&i2c_imx_driver, i2c_imx_probe);
639 }
640 subsys_initcall(i2c_adap_imx_init);
641
642 static void __exit i2c_adap_imx_exit(void)
643 {
644         platform_driver_unregister(&i2c_imx_driver);
645 }
646 module_exit(i2c_adap_imx_exit);
647
648 MODULE_LICENSE("GPL");
649 MODULE_AUTHOR("Darius Augulis");
650 MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
651 MODULE_ALIAS("platform:" DRIVER_NAME);