GNU Linux-libre 4.4.284-gnu1
[releases.git] / drivers / i2c / busses / i2c-cpm.c
1 /*
2  * Freescale CPM1/CPM2 I2C interface.
3  * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
4  *
5  * moved into proper i2c interface;
6  * Brad Parker (brad@heeltoe.com)
7  *
8  * Parts from dbox2_i2c.c (cvs.tuxbox.org)
9  * (C) 2000-2001 Felix Domke (tmbinc@gmx.net), Gillem (htoa@gmx.net)
10  *
11  * (C) 2007 Montavista Software, Inc.
12  * Vitaly Bordug <vitb@kernel.crashing.org>
13  *
14  * Converted to of_platform_device. Renamed to i2c-cpm.c.
15  * (C) 2007,2008 Jochen Friedrich <jochen@scram.de>
16  *
17  *  This program is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 2 of the License, or
20  *  (at your option) any later version.
21  *
22  *  This program is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/interrupt.h>
33 #include <linux/errno.h>
34 #include <linux/stddef.h>
35 #include <linux/i2c.h>
36 #include <linux/io.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/of_address.h>
39 #include <linux/of_device.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_platform.h>
42 #include <sysdev/fsl_soc.h>
43 #include <asm/cpm.h>
44
45 /* Try to define this if you have an older CPU (earlier than rev D4) */
46 /* However, better use a GPIO based bitbang driver in this case :/   */
47 #undef  I2C_CHIP_ERRATA
48
49 #define CPM_MAX_READ    513
50 #define CPM_MAXBD       4
51
52 #define I2C_EB                  (0x10) /* Big endian mode */
53 #define I2C_EB_CPM2             (0x30) /* Big endian mode, memory snoop */
54
55 #define DPRAM_BASE              ((u8 __iomem __force *)cpm_muram_addr(0))
56
57 /* I2C parameter RAM. */
58 struct i2c_ram {
59         ushort  rbase;          /* Rx Buffer descriptor base address */
60         ushort  tbase;          /* Tx Buffer descriptor base address */
61         u_char  rfcr;           /* Rx function code */
62         u_char  tfcr;           /* Tx function code */
63         ushort  mrblr;          /* Max receive buffer length */
64         uint    rstate;         /* Internal */
65         uint    rdp;            /* Internal */
66         ushort  rbptr;          /* Rx Buffer descriptor pointer */
67         ushort  rbc;            /* Internal */
68         uint    rxtmp;          /* Internal */
69         uint    tstate;         /* Internal */
70         uint    tdp;            /* Internal */
71         ushort  tbptr;          /* Tx Buffer descriptor pointer */
72         ushort  tbc;            /* Internal */
73         uint    txtmp;          /* Internal */
74         char    res1[4];        /* Reserved */
75         ushort  rpbase;         /* Relocation pointer */
76         char    res2[2];        /* Reserved */
77         /* The following elements are only for CPM2 */
78         char    res3[4];        /* Reserved */
79         uint    sdmatmp;        /* Internal */
80 };
81
82 #define I2COM_START     0x80
83 #define I2COM_MASTER    0x01
84 #define I2CER_TXE       0x10
85 #define I2CER_BUSY      0x04
86 #define I2CER_TXB       0x02
87 #define I2CER_RXB       0x01
88 #define I2MOD_EN        0x01
89
90 /* I2C Registers */
91 struct i2c_reg {
92         u8      i2mod;
93         u8      res1[3];
94         u8      i2add;
95         u8      res2[3];
96         u8      i2brg;
97         u8      res3[3];
98         u8      i2com;
99         u8      res4[3];
100         u8      i2cer;
101         u8      res5[3];
102         u8      i2cmr;
103 };
104
105 struct cpm_i2c {
106         char *base;
107         struct platform_device *ofdev;
108         struct i2c_adapter adap;
109         uint dp_addr;
110         int version; /* CPM1=1, CPM2=2 */
111         int irq;
112         int cp_command;
113         int freq;
114         struct i2c_reg __iomem *i2c_reg;
115         struct i2c_ram __iomem *i2c_ram;
116         u16 i2c_addr;
117         wait_queue_head_t i2c_wait;
118         cbd_t __iomem *tbase;
119         cbd_t __iomem *rbase;
120         u_char *txbuf[CPM_MAXBD];
121         u_char *rxbuf[CPM_MAXBD];
122         dma_addr_t txdma[CPM_MAXBD];
123         dma_addr_t rxdma[CPM_MAXBD];
124 };
125
126 static irqreturn_t cpm_i2c_interrupt(int irq, void *dev_id)
127 {
128         struct cpm_i2c *cpm;
129         struct i2c_reg __iomem *i2c_reg;
130         struct i2c_adapter *adap = dev_id;
131         int i;
132
133         cpm = i2c_get_adapdata(dev_id);
134         i2c_reg = cpm->i2c_reg;
135
136         /* Clear interrupt. */
137         i = in_8(&i2c_reg->i2cer);
138         out_8(&i2c_reg->i2cer, i);
139
140         dev_dbg(&adap->dev, "Interrupt: %x\n", i);
141
142         wake_up(&cpm->i2c_wait);
143
144         return i ? IRQ_HANDLED : IRQ_NONE;
145 }
146
147 static void cpm_reset_i2c_params(struct cpm_i2c *cpm)
148 {
149         struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
150
151         /* Set up the I2C parameters in the parameter ram. */
152         out_be16(&i2c_ram->tbase, (u8 __iomem *)cpm->tbase - DPRAM_BASE);
153         out_be16(&i2c_ram->rbase, (u8 __iomem *)cpm->rbase - DPRAM_BASE);
154
155         if (cpm->version == 1) {
156                 out_8(&i2c_ram->tfcr, I2C_EB);
157                 out_8(&i2c_ram->rfcr, I2C_EB);
158         } else {
159                 out_8(&i2c_ram->tfcr, I2C_EB_CPM2);
160                 out_8(&i2c_ram->rfcr, I2C_EB_CPM2);
161         }
162
163         out_be16(&i2c_ram->mrblr, CPM_MAX_READ);
164
165         out_be32(&i2c_ram->rstate, 0);
166         out_be32(&i2c_ram->rdp, 0);
167         out_be16(&i2c_ram->rbptr, 0);
168         out_be16(&i2c_ram->rbc, 0);
169         out_be32(&i2c_ram->rxtmp, 0);
170         out_be32(&i2c_ram->tstate, 0);
171         out_be32(&i2c_ram->tdp, 0);
172         out_be16(&i2c_ram->tbptr, 0);
173         out_be16(&i2c_ram->tbc, 0);
174         out_be32(&i2c_ram->txtmp, 0);
175 }
176
177 static void cpm_i2c_force_close(struct i2c_adapter *adap)
178 {
179         struct cpm_i2c *cpm = i2c_get_adapdata(adap);
180         struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
181
182         dev_dbg(&adap->dev, "cpm_i2c_force_close()\n");
183
184         cpm_command(cpm->cp_command, CPM_CR_CLOSE_RX_BD);
185
186         out_8(&i2c_reg->i2cmr, 0x00);   /* Disable all interrupts */
187         out_8(&i2c_reg->i2cer, 0xff);
188 }
189
190 static void cpm_i2c_parse_message(struct i2c_adapter *adap,
191         struct i2c_msg *pmsg, int num, int tx, int rx)
192 {
193         cbd_t __iomem *tbdf;
194         cbd_t __iomem *rbdf;
195         u_char addr;
196         u_char *tb;
197         u_char *rb;
198         struct cpm_i2c *cpm = i2c_get_adapdata(adap);
199
200         tbdf = cpm->tbase + tx;
201         rbdf = cpm->rbase + rx;
202
203         addr = pmsg->addr << 1;
204         if (pmsg->flags & I2C_M_RD)
205                 addr |= 1;
206
207         tb = cpm->txbuf[tx];
208         rb = cpm->rxbuf[rx];
209
210         /* Align read buffer */
211         rb = (u_char *) (((ulong) rb + 1) & ~1);
212
213         tb[0] = addr;           /* Device address byte w/rw flag */
214
215         out_be16(&tbdf->cbd_datlen, pmsg->len + 1);
216         out_be16(&tbdf->cbd_sc, 0);
217
218         if (!(pmsg->flags & I2C_M_NOSTART))
219                 setbits16(&tbdf->cbd_sc, BD_I2C_START);
220
221         if (tx + 1 == num)
222                 setbits16(&tbdf->cbd_sc, BD_SC_LAST | BD_SC_WRAP);
223
224         if (pmsg->flags & I2C_M_RD) {
225                 /*
226                  * To read, we need an empty buffer of the proper length.
227                  * All that is used is the first byte for address, the remainder
228                  * is just used for timing (and doesn't really have to exist).
229                  */
230
231                 dev_dbg(&adap->dev, "cpm_i2c_read(abyte=0x%x)\n", addr);
232
233                 out_be16(&rbdf->cbd_datlen, 0);
234                 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
235
236                 if (rx + 1 == CPM_MAXBD)
237                         setbits16(&rbdf->cbd_sc, BD_SC_WRAP);
238
239                 eieio();
240                 setbits16(&tbdf->cbd_sc, BD_SC_READY);
241         } else {
242                 dev_dbg(&adap->dev, "cpm_i2c_write(abyte=0x%x)\n", addr);
243
244                 memcpy(tb+1, pmsg->buf, pmsg->len);
245
246                 eieio();
247                 setbits16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_INTRPT);
248         }
249 }
250
251 static int cpm_i2c_check_message(struct i2c_adapter *adap,
252         struct i2c_msg *pmsg, int tx, int rx)
253 {
254         cbd_t __iomem *tbdf;
255         cbd_t __iomem *rbdf;
256         u_char *tb;
257         u_char *rb;
258         struct cpm_i2c *cpm = i2c_get_adapdata(adap);
259
260         tbdf = cpm->tbase + tx;
261         rbdf = cpm->rbase + rx;
262
263         tb = cpm->txbuf[tx];
264         rb = cpm->rxbuf[rx];
265
266         /* Align read buffer */
267         rb = (u_char *) (((uint) rb + 1) & ~1);
268
269         eieio();
270         if (pmsg->flags & I2C_M_RD) {
271                 dev_dbg(&adap->dev, "tx sc 0x%04x, rx sc 0x%04x\n",
272                         in_be16(&tbdf->cbd_sc), in_be16(&rbdf->cbd_sc));
273
274                 if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
275                         dev_dbg(&adap->dev, "I2C read; No ack\n");
276                         return -ENXIO;
277                 }
278                 if (in_be16(&rbdf->cbd_sc) & BD_SC_EMPTY) {
279                         dev_err(&adap->dev,
280                                 "I2C read; complete but rbuf empty\n");
281                         return -EREMOTEIO;
282                 }
283                 if (in_be16(&rbdf->cbd_sc) & BD_SC_OV) {
284                         dev_err(&adap->dev, "I2C read; Overrun\n");
285                         return -EREMOTEIO;
286                 }
287                 memcpy(pmsg->buf, rb, pmsg->len);
288         } else {
289                 dev_dbg(&adap->dev, "tx sc %d 0x%04x\n", tx,
290                         in_be16(&tbdf->cbd_sc));
291
292                 if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
293                         dev_dbg(&adap->dev, "I2C write; No ack\n");
294                         return -ENXIO;
295                 }
296                 if (in_be16(&tbdf->cbd_sc) & BD_SC_UN) {
297                         dev_err(&adap->dev, "I2C write; Underrun\n");
298                         return -EIO;
299                 }
300                 if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
301                         dev_err(&adap->dev, "I2C write; Collision\n");
302                         return -EIO;
303                 }
304         }
305         return 0;
306 }
307
308 static int cpm_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
309 {
310         struct cpm_i2c *cpm = i2c_get_adapdata(adap);
311         struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
312         struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
313         struct i2c_msg *pmsg;
314         int ret;
315         int tptr;
316         int rptr;
317         cbd_t __iomem *tbdf;
318         cbd_t __iomem *rbdf;
319
320         /* Reset to use first buffer */
321         out_be16(&i2c_ram->rbptr, in_be16(&i2c_ram->rbase));
322         out_be16(&i2c_ram->tbptr, in_be16(&i2c_ram->tbase));
323
324         tbdf = cpm->tbase;
325         rbdf = cpm->rbase;
326
327         tptr = 0;
328         rptr = 0;
329
330         /*
331          * If there was a collision in the last i2c transaction,
332          * Set I2COM_MASTER as it was cleared during collision.
333          */
334         if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
335                 out_8(&cpm->i2c_reg->i2com, I2COM_MASTER);
336         }
337
338         while (tptr < num) {
339                 pmsg = &msgs[tptr];
340                 dev_dbg(&adap->dev, "R: %d T: %d\n", rptr, tptr);
341
342                 cpm_i2c_parse_message(adap, pmsg, num, tptr, rptr);
343                 if (pmsg->flags & I2C_M_RD)
344                         rptr++;
345                 tptr++;
346         }
347         /* Start transfer now */
348         /* Enable RX/TX/Error interupts */
349         out_8(&i2c_reg->i2cmr, I2CER_TXE | I2CER_TXB | I2CER_RXB);
350         out_8(&i2c_reg->i2cer, 0xff);   /* Clear interrupt status */
351         /* Chip bug, set enable here */
352         setbits8(&i2c_reg->i2mod, I2MOD_EN);    /* Enable */
353         /* Begin transmission */
354         setbits8(&i2c_reg->i2com, I2COM_START);
355
356         tptr = 0;
357         rptr = 0;
358
359         while (tptr < num) {
360                 /* Check for outstanding messages */
361                 dev_dbg(&adap->dev, "test ready.\n");
362                 pmsg = &msgs[tptr];
363                 if (pmsg->flags & I2C_M_RD)
364                         ret = wait_event_timeout(cpm->i2c_wait,
365                                 (in_be16(&tbdf[tptr].cbd_sc) & BD_SC_NAK) ||
366                                 !(in_be16(&rbdf[rptr].cbd_sc) & BD_SC_EMPTY),
367                                 1 * HZ);
368                 else
369                         ret = wait_event_timeout(cpm->i2c_wait,
370                                 !(in_be16(&tbdf[tptr].cbd_sc) & BD_SC_READY),
371                                 1 * HZ);
372                 if (ret == 0) {
373                         ret = -EREMOTEIO;
374                         dev_err(&adap->dev, "I2C transfer: timeout\n");
375                         goto out_err;
376                 }
377                 if (ret > 0) {
378                         dev_dbg(&adap->dev, "ready.\n");
379                         ret = cpm_i2c_check_message(adap, pmsg, tptr, rptr);
380                         tptr++;
381                         if (pmsg->flags & I2C_M_RD)
382                                 rptr++;
383                         if (ret)
384                                 goto out_err;
385                 }
386         }
387 #ifdef I2C_CHIP_ERRATA
388         /*
389          * Chip errata, clear enable. This is not needed on rev D4 CPUs.
390          * Disabling I2C too early may cause too short stop condition
391          */
392         udelay(4);
393         clrbits8(&i2c_reg->i2mod, I2MOD_EN);
394 #endif
395         return (num);
396
397 out_err:
398         cpm_i2c_force_close(adap);
399 #ifdef I2C_CHIP_ERRATA
400         /*
401          * Chip errata, clear enable. This is not needed on rev D4 CPUs.
402          */
403         clrbits8(&i2c_reg->i2mod, I2MOD_EN);
404 #endif
405         return ret;
406 }
407
408 static u32 cpm_i2c_func(struct i2c_adapter *adap)
409 {
410         return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
411 }
412
413 /* -----exported algorithm data: -------------------------------------  */
414
415 static const struct i2c_algorithm cpm_i2c_algo = {
416         .master_xfer = cpm_i2c_xfer,
417         .functionality = cpm_i2c_func,
418 };
419
420 /* CPM_MAX_READ is also limiting writes according to the code! */
421 static struct i2c_adapter_quirks cpm_i2c_quirks = {
422         .max_num_msgs = CPM_MAXBD,
423         .max_read_len = CPM_MAX_READ,
424         .max_write_len = CPM_MAX_READ,
425 };
426
427 static const struct i2c_adapter cpm_ops = {
428         .owner          = THIS_MODULE,
429         .name           = "i2c-cpm",
430         .algo           = &cpm_i2c_algo,
431         .quirks         = &cpm_i2c_quirks,
432 };
433
434 static int cpm_i2c_setup(struct cpm_i2c *cpm)
435 {
436         struct platform_device *ofdev = cpm->ofdev;
437         const u32 *data;
438         int len, ret, i;
439         void __iomem *i2c_base;
440         cbd_t __iomem *tbdf;
441         cbd_t __iomem *rbdf;
442         unsigned char brg;
443
444         dev_dbg(&cpm->ofdev->dev, "cpm_i2c_setup()\n");
445
446         init_waitqueue_head(&cpm->i2c_wait);
447
448         cpm->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
449         if (!cpm->irq)
450                 return -EINVAL;
451
452         /* Install interrupt handler. */
453         ret = request_irq(cpm->irq, cpm_i2c_interrupt, 0, "cpm_i2c",
454                           &cpm->adap);
455         if (ret)
456                 return ret;
457
458         /* I2C parameter RAM */
459         i2c_base = of_iomap(ofdev->dev.of_node, 1);
460         if (i2c_base == NULL) {
461                 ret = -EINVAL;
462                 goto out_irq;
463         }
464
465         if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm1-i2c")) {
466
467                 /* Check for and use a microcode relocation patch. */
468                 cpm->i2c_ram = i2c_base;
469                 cpm->i2c_addr = in_be16(&cpm->i2c_ram->rpbase);
470
471                 /*
472                  * Maybe should use cpm_muram_alloc instead of hardcoding
473                  * this in micropatch.c
474                  */
475                 if (cpm->i2c_addr) {
476                         cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
477                         iounmap(i2c_base);
478                 }
479
480                 cpm->version = 1;
481
482         } else if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm2-i2c")) {
483                 cpm->i2c_addr = cpm_muram_alloc(sizeof(struct i2c_ram), 64);
484                 cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
485                 out_be16(i2c_base, cpm->i2c_addr);
486                 iounmap(i2c_base);
487
488                 cpm->version = 2;
489
490         } else {
491                 iounmap(i2c_base);
492                 ret = -EINVAL;
493                 goto out_irq;
494         }
495
496         /* I2C control/status registers */
497         cpm->i2c_reg = of_iomap(ofdev->dev.of_node, 0);
498         if (cpm->i2c_reg == NULL) {
499                 ret = -EINVAL;
500                 goto out_ram;
501         }
502
503         data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
504         if (!data || len != 4) {
505                 ret = -EINVAL;
506                 goto out_reg;
507         }
508         cpm->cp_command = *data;
509
510         data = of_get_property(ofdev->dev.of_node, "linux,i2c-class", &len);
511         if (data && len == 4)
512                 cpm->adap.class = *data;
513
514         data = of_get_property(ofdev->dev.of_node, "clock-frequency", &len);
515         if (data && len == 4)
516                 cpm->freq = *data;
517         else
518                 cpm->freq = 60000; /* use 60kHz i2c clock by default */
519
520         /*
521          * Allocate space for CPM_MAXBD transmit and receive buffer
522          * descriptors in the DP ram.
523          */
524         cpm->dp_addr = cpm_muram_alloc(sizeof(cbd_t) * 2 * CPM_MAXBD, 8);
525         if (!cpm->dp_addr) {
526                 ret = -ENOMEM;
527                 goto out_reg;
528         }
529
530         cpm->tbase = cpm_muram_addr(cpm->dp_addr);
531         cpm->rbase = cpm_muram_addr(cpm->dp_addr + sizeof(cbd_t) * CPM_MAXBD);
532
533         /* Allocate TX and RX buffers */
534
535         tbdf = cpm->tbase;
536         rbdf = cpm->rbase;
537
538         for (i = 0; i < CPM_MAXBD; i++) {
539                 cpm->rxbuf[i] = dma_alloc_coherent(&cpm->ofdev->dev,
540                                                    CPM_MAX_READ + 1,
541                                                    &cpm->rxdma[i], GFP_KERNEL);
542                 if (!cpm->rxbuf[i]) {
543                         ret = -ENOMEM;
544                         goto out_muram;
545                 }
546                 out_be32(&rbdf[i].cbd_bufaddr, ((cpm->rxdma[i] + 1) & ~1));
547
548                 cpm->txbuf[i] = (unsigned char *)dma_alloc_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1, &cpm->txdma[i], GFP_KERNEL);
549                 if (!cpm->txbuf[i]) {
550                         ret = -ENOMEM;
551                         goto out_muram;
552                 }
553                 out_be32(&tbdf[i].cbd_bufaddr, cpm->txdma[i]);
554         }
555
556         /* Initialize Tx/Rx parameters. */
557
558         cpm_reset_i2c_params(cpm);
559
560         dev_dbg(&cpm->ofdev->dev, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
561                 cpm->i2c_ram, cpm->i2c_addr, cpm->freq);
562         dev_dbg(&cpm->ofdev->dev, "tbase 0x%04x, rbase 0x%04x\n",
563                 (u8 __iomem *)cpm->tbase - DPRAM_BASE,
564                 (u8 __iomem *)cpm->rbase - DPRAM_BASE);
565
566         cpm_command(cpm->cp_command, CPM_CR_INIT_TRX);
567
568         /*
569          * Select an invalid address. Just make sure we don't use loopback mode
570          */
571         out_8(&cpm->i2c_reg->i2add, 0x7f << 1);
572
573         /*
574          * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
575          * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
576          * the actual i2c bus frequency.
577          */
578         brg = get_brgfreq() / (32 * 2 * cpm->freq) - 3;
579         out_8(&cpm->i2c_reg->i2brg, brg);
580
581         out_8(&cpm->i2c_reg->i2mod, 0x00);
582         out_8(&cpm->i2c_reg->i2com, I2COM_MASTER);      /* Master mode */
583
584         /* Disable interrupts. */
585         out_8(&cpm->i2c_reg->i2cmr, 0);
586         out_8(&cpm->i2c_reg->i2cer, 0xff);
587
588         return 0;
589
590 out_muram:
591         for (i = 0; i < CPM_MAXBD; i++) {
592                 if (cpm->rxbuf[i])
593                         dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
594                                 cpm->rxbuf[i], cpm->rxdma[i]);
595                 if (cpm->txbuf[i])
596                         dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
597                                 cpm->txbuf[i], cpm->txdma[i]);
598         }
599         cpm_muram_free(cpm->dp_addr);
600 out_reg:
601         iounmap(cpm->i2c_reg);
602 out_ram:
603         if ((cpm->version == 1) && (!cpm->i2c_addr))
604                 iounmap(cpm->i2c_ram);
605         if (cpm->version == 2)
606                 cpm_muram_free(cpm->i2c_addr);
607 out_irq:
608         free_irq(cpm->irq, &cpm->adap);
609         return ret;
610 }
611
612 static void cpm_i2c_shutdown(struct cpm_i2c *cpm)
613 {
614         int i;
615
616         /* Shut down I2C. */
617         clrbits8(&cpm->i2c_reg->i2mod, I2MOD_EN);
618
619         /* Disable interrupts */
620         out_8(&cpm->i2c_reg->i2cmr, 0);
621         out_8(&cpm->i2c_reg->i2cer, 0xff);
622
623         free_irq(cpm->irq, &cpm->adap);
624
625         /* Free all memory */
626         for (i = 0; i < CPM_MAXBD; i++) {
627                 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
628                         cpm->rxbuf[i], cpm->rxdma[i]);
629                 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
630                         cpm->txbuf[i], cpm->txdma[i]);
631         }
632
633         cpm_muram_free(cpm->dp_addr);
634         iounmap(cpm->i2c_reg);
635
636         if ((cpm->version == 1) && (!cpm->i2c_addr))
637                 iounmap(cpm->i2c_ram);
638         if (cpm->version == 2)
639                 cpm_muram_free(cpm->i2c_addr);
640 }
641
642 static int cpm_i2c_probe(struct platform_device *ofdev)
643 {
644         int result, len;
645         struct cpm_i2c *cpm;
646         const u32 *data;
647
648         cpm = kzalloc(sizeof(struct cpm_i2c), GFP_KERNEL);
649         if (!cpm)
650                 return -ENOMEM;
651
652         cpm->ofdev = ofdev;
653
654         platform_set_drvdata(ofdev, cpm);
655
656         cpm->adap = cpm_ops;
657         i2c_set_adapdata(&cpm->adap, cpm);
658         cpm->adap.dev.parent = &ofdev->dev;
659         cpm->adap.dev.of_node = of_node_get(ofdev->dev.of_node);
660
661         result = cpm_i2c_setup(cpm);
662         if (result) {
663                 dev_err(&ofdev->dev, "Unable to init hardware\n");
664                 goto out_free;
665         }
666
667         /* register new adapter to i2c module... */
668
669         data = of_get_property(ofdev->dev.of_node, "linux,i2c-index", &len);
670         cpm->adap.nr = (data && len == 4) ? be32_to_cpup(data) : -1;
671         result = i2c_add_numbered_adapter(&cpm->adap);
672
673         if (result < 0) {
674                 dev_err(&ofdev->dev, "Unable to register with I2C\n");
675                 goto out_shut;
676         }
677
678         dev_dbg(&ofdev->dev, "hw routines for %s registered.\n",
679                 cpm->adap.name);
680
681         return 0;
682 out_shut:
683         cpm_i2c_shutdown(cpm);
684 out_free:
685         kfree(cpm);
686
687         return result;
688 }
689
690 static int cpm_i2c_remove(struct platform_device *ofdev)
691 {
692         struct cpm_i2c *cpm = platform_get_drvdata(ofdev);
693
694         i2c_del_adapter(&cpm->adap);
695
696         cpm_i2c_shutdown(cpm);
697
698         kfree(cpm);
699
700         return 0;
701 }
702
703 static const struct of_device_id cpm_i2c_match[] = {
704         {
705                 .compatible = "fsl,cpm1-i2c",
706         },
707         {
708                 .compatible = "fsl,cpm2-i2c",
709         },
710         {},
711 };
712
713 MODULE_DEVICE_TABLE(of, cpm_i2c_match);
714
715 static struct platform_driver cpm_i2c_driver = {
716         .probe          = cpm_i2c_probe,
717         .remove         = cpm_i2c_remove,
718         .driver = {
719                 .name = "fsl-i2c-cpm",
720                 .of_match_table = cpm_i2c_match,
721         },
722 };
723
724 module_platform_driver(cpm_i2c_driver);
725
726 MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
727 MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
728 MODULE_LICENSE("GPL");