ARM: PNX4008: use msecs_to_jiffies() rather than open-coding it
[safe/jmp/linux-2.6] / drivers / i2c / busses / i2c-pnx.c
1 /*
2  * Provides I2C support for Philips PNX010x/PNX4008 boards.
3  *
4  * Authors: Dennis Kovalev <dkovalev@ru.mvista.com>
5  *          Vitaly Wool <vwool@ru.mvista.com>
6  *
7  * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under
8  * the terms of the GNU General Public License version 2. This program
9  * is licensed "as is" without any warranty of any kind, whether express
10  * or implied.
11  */
12
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/timer.h>
19 #include <linux/completion.h>
20 #include <linux/platform_device.h>
21 #include <linux/i2c-pnx.h>
22 #include <linux/io.h>
23 #include <linux/err.h>
24 #include <linux/clk.h>
25
26 #include <mach/hardware.h>
27 #include <mach/i2c.h>
28
29 #define I2C_PNX_TIMEOUT         10 /* msec */
30 #define I2C_PNX_SPEED_KHZ       100
31 #define I2C_PNX_REGION_SIZE     0x100
32
33 static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
34 {
35         while (timeout > 0 &&
36                         (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
37                 mdelay(1);
38                 timeout--;
39         }
40         return (timeout <= 0);
41 }
42
43 static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
44 {
45         while (timeout > 0 &&
46                         (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
47                 mdelay(1);
48                 timeout--;
49         }
50         return (timeout <= 0);
51 }
52
53 static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
54 {
55         struct timer_list *timer = &alg_data->mif.timer;
56         unsigned long expires = msecs_to_jiffies(I2C_PNX_TIMEOUT);
57
58         if (expires <= 1)
59                 expires = 2;
60
61         del_timer_sync(timer);
62
63         dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %lu jiffies.\n",
64                 jiffies, expires);
65
66         timer->expires = jiffies + expires;
67         timer->data = (unsigned long)&alg_data;
68
69         add_timer(timer);
70 }
71
72 /**
73  * i2c_pnx_start - start a device
74  * @slave_addr:         slave address
75  * @adap:               pointer to adapter structure
76  *
77  * Generate a START signal in the desired mode.
78  */
79 static int i2c_pnx_start(unsigned char slave_addr,
80         struct i2c_pnx_algo_data *alg_data)
81 {
82         dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
83                 slave_addr, alg_data->mif.mode);
84
85         /* Check for 7 bit slave addresses only */
86         if (slave_addr & ~0x7f) {
87                 dev_err(&alg_data->adapter.dev,
88                         "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
89                         alg_data->adapter.name, slave_addr);
90                 return -EINVAL;
91         }
92
93         /* First, make sure bus is idle */
94         if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
95                 /* Somebody else is monopolizing the bus */
96                 dev_err(&alg_data->adapter.dev,
97                         "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
98                         alg_data->adapter.name, slave_addr,
99                         ioread32(I2C_REG_CTL(alg_data)),
100                         ioread32(I2C_REG_STS(alg_data)));
101                 return -EBUSY;
102         } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
103                 /* Sorry, we lost the bus */
104                 dev_err(&alg_data->adapter.dev,
105                         "%s: Arbitration failure. Slave addr = %02x\n",
106                         alg_data->adapter.name, slave_addr);
107                 return -EIO;
108         }
109
110         /*
111          * OK, I2C is enabled and we have the bus.
112          * Clear the current TDI and AFI status flags.
113          */
114         iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
115                   I2C_REG_STS(alg_data));
116
117         dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
118                 (slave_addr << 1) | start_bit | alg_data->mif.mode);
119
120         /* Write the slave address, START bit and R/W bit */
121         iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
122                   I2C_REG_TX(alg_data));
123
124         dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
125
126         return 0;
127 }
128
129 /**
130  * i2c_pnx_stop - stop a device
131  * @adap:               pointer to I2C adapter structure
132  *
133  * Generate a STOP signal to terminate the master transaction.
134  */
135 static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
136 {
137         /* Only 1 msec max timeout due to interrupt context */
138         long timeout = 1000;
139
140         dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
141                 __func__, ioread32(I2C_REG_STS(alg_data)));
142
143         /* Write a STOP bit to TX FIFO */
144         iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
145
146         /* Wait until the STOP is seen. */
147         while (timeout > 0 &&
148                (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
149                 /* may be called from interrupt context */
150                 udelay(1);
151                 timeout--;
152         }
153
154         dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
155                 __func__, ioread32(I2C_REG_STS(alg_data)));
156 }
157
158 /**
159  * i2c_pnx_master_xmit - transmit data to slave
160  * @adap:               pointer to I2C adapter structure
161  *
162  * Sends one byte of data to the slave
163  */
164 static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
165 {
166         u32 val;
167
168         dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
169                 __func__, ioread32(I2C_REG_STS(alg_data)));
170
171         if (alg_data->mif.len > 0) {
172                 /* We still have something to talk about... */
173                 val = *alg_data->mif.buf++;
174
175                 if (alg_data->mif.len == 1) {
176                         val |= stop_bit;
177                         if (!alg_data->last)
178                                 val |= start_bit;
179                 }
180
181                 alg_data->mif.len--;
182                 iowrite32(val, I2C_REG_TX(alg_data));
183
184                 dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n",
185                         __func__, val, alg_data->mif.len + 1);
186
187                 if (alg_data->mif.len == 0) {
188                         if (alg_data->last) {
189                                 /* Wait until the STOP is seen. */
190                                 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
191                                         dev_err(&alg_data->adapter.dev,
192                                                 "The bus is still active after timeout\n");
193                         }
194                         /* Disable master interrupts */
195                         iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
196                                 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
197                                   I2C_REG_CTL(alg_data));
198
199                         del_timer_sync(&alg_data->mif.timer);
200
201                         dev_dbg(&alg_data->adapter.dev,
202                                 "%s(): Waking up xfer routine.\n",
203                                 __func__);
204
205                         complete(&alg_data->mif.complete);
206                 }
207         } else if (alg_data->mif.len == 0) {
208                 /* zero-sized transfer */
209                 i2c_pnx_stop(alg_data);
210
211                 /* Disable master interrupts. */
212                 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
213                         ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
214                           I2C_REG_CTL(alg_data));
215
216                 /* Stop timer. */
217                 del_timer_sync(&alg_data->mif.timer);
218                 dev_dbg(&alg_data->adapter.dev,
219                         "%s(): Waking up xfer routine after zero-xfer.\n",
220                         __func__);
221
222                 complete(&alg_data->mif.complete);
223         }
224
225         dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
226                 __func__, ioread32(I2C_REG_STS(alg_data)));
227
228         return 0;
229 }
230
231 /**
232  * i2c_pnx_master_rcv - receive data from slave
233  * @adap:               pointer to I2C adapter structure
234  *
235  * Reads one byte data from the slave
236  */
237 static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
238 {
239         unsigned int val = 0;
240         u32 ctl = 0;
241
242         dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
243                 __func__, ioread32(I2C_REG_STS(alg_data)));
244
245         /* Check, whether there is already data,
246          * or we didn't 'ask' for it yet.
247          */
248         if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
249                 dev_dbg(&alg_data->adapter.dev,
250                         "%s(): Write dummy data to fill Rx-fifo...\n",
251                         __func__);
252
253                 if (alg_data->mif.len == 1) {
254                         /* Last byte, do not acknowledge next rcv. */
255                         val |= stop_bit;
256                         if (!alg_data->last)
257                                 val |= start_bit;
258
259                         /*
260                          * Enable interrupt RFDAIE (data in Rx fifo),
261                          * and disable DRMIE (need data for Tx)
262                          */
263                         ctl = ioread32(I2C_REG_CTL(alg_data));
264                         ctl |= mcntrl_rffie | mcntrl_daie;
265                         ctl &= ~mcntrl_drmie;
266                         iowrite32(ctl, I2C_REG_CTL(alg_data));
267                 }
268
269                 /*
270                  * Now we'll 'ask' for data:
271                  * For each byte we want to receive, we must
272                  * write a (dummy) byte to the Tx-FIFO.
273                  */
274                 iowrite32(val, I2C_REG_TX(alg_data));
275
276                 return 0;
277         }
278
279         /* Handle data. */
280         if (alg_data->mif.len > 0) {
281                 val = ioread32(I2C_REG_RX(alg_data));
282                 *alg_data->mif.buf++ = (u8) (val & 0xff);
283                 dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
284                         __func__, val, alg_data->mif.len);
285
286                 alg_data->mif.len--;
287                 if (alg_data->mif.len == 0) {
288                         if (alg_data->last)
289                                 /* Wait until the STOP is seen. */
290                                 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
291                                         dev_err(&alg_data->adapter.dev,
292                                                 "The bus is still active after timeout\n");
293
294                         /* Disable master interrupts */
295                         ctl = ioread32(I2C_REG_CTL(alg_data));
296                         ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
297                                  mcntrl_drmie | mcntrl_daie);
298                         iowrite32(ctl, I2C_REG_CTL(alg_data));
299
300                         /* Kill timer. */
301                         del_timer_sync(&alg_data->mif.timer);
302                         complete(&alg_data->mif.complete);
303                 }
304         }
305
306         dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
307                 __func__, ioread32(I2C_REG_STS(alg_data)));
308
309         return 0;
310 }
311
312 static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
313 {
314         struct i2c_pnx_algo_data *alg_data = dev_id;
315         u32 stat, ctl;
316
317         dev_dbg(&alg_data->adapter.dev,
318                 "%s(): mstat = %x mctrl = %x, mode = %d\n",
319                 __func__,
320                 ioread32(I2C_REG_STS(alg_data)),
321                 ioread32(I2C_REG_CTL(alg_data)),
322                 alg_data->mif.mode);
323         stat = ioread32(I2C_REG_STS(alg_data));
324
325         /* let's see what kind of event this is */
326         if (stat & mstatus_afi) {
327                 /* We lost arbitration in the midst of a transfer */
328                 alg_data->mif.ret = -EIO;
329
330                 /* Disable master interrupts. */
331                 ctl = ioread32(I2C_REG_CTL(alg_data));
332                 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
333                          mcntrl_drmie);
334                 iowrite32(ctl, I2C_REG_CTL(alg_data));
335
336                 /* Stop timer, to prevent timeout. */
337                 del_timer_sync(&alg_data->mif.timer);
338                 complete(&alg_data->mif.complete);
339         } else if (stat & mstatus_nai) {
340                 /* Slave did not acknowledge, generate a STOP */
341                 dev_dbg(&alg_data->adapter.dev,
342                         "%s(): Slave did not acknowledge, generating a STOP.\n",
343                         __func__);
344                 i2c_pnx_stop(alg_data);
345
346                 /* Disable master interrupts. */
347                 ctl = ioread32(I2C_REG_CTL(alg_data));
348                 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
349                          mcntrl_drmie);
350                 iowrite32(ctl, I2C_REG_CTL(alg_data));
351
352                 /* Our return value. */
353                 alg_data->mif.ret = -EIO;
354
355                 /* Stop timer, to prevent timeout. */
356                 del_timer_sync(&alg_data->mif.timer);
357                 complete(&alg_data->mif.complete);
358         } else {
359                 /*
360                  * Two options:
361                  * - Master Tx needs data.
362                  * - There is data in the Rx-fifo
363                  * The latter is only the case if we have requested for data,
364                  * via a dummy write. (See 'i2c_pnx_master_rcv'.)
365                  * We therefore check, as a sanity check, whether that interrupt
366                  * has been enabled.
367                  */
368                 if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
369                         if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
370                                 i2c_pnx_master_xmit(alg_data);
371                         } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
372                                 i2c_pnx_master_rcv(alg_data);
373                         }
374                 }
375         }
376
377         /* Clear TDI and AFI bits */
378         stat = ioread32(I2C_REG_STS(alg_data));
379         iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
380
381         dev_dbg(&alg_data->adapter.dev,
382                 "%s(): exiting, stat = %x ctrl = %x.\n",
383                  __func__, ioread32(I2C_REG_STS(alg_data)),
384                  ioread32(I2C_REG_CTL(alg_data)));
385
386         return IRQ_HANDLED;
387 }
388
389 static void i2c_pnx_timeout(unsigned long data)
390 {
391         struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
392         u32 ctl;
393
394         dev_err(&alg_data->adapter.dev,
395                 "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
396                 ioread32(I2C_REG_STS(alg_data)),
397                 ioread32(I2C_REG_CTL(alg_data)));
398
399         /* Reset master and disable interrupts */
400         ctl = ioread32(I2C_REG_CTL(alg_data));
401         ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
402         iowrite32(ctl, I2C_REG_CTL(alg_data));
403
404         ctl |= mcntrl_reset;
405         iowrite32(ctl, I2C_REG_CTL(alg_data));
406         wait_reset(I2C_PNX_TIMEOUT, alg_data);
407         alg_data->mif.ret = -EIO;
408         complete(&alg_data->mif.complete);
409 }
410
411 static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
412 {
413         u32 stat;
414
415         if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
416                 dev_err(&alg_data->adapter.dev,
417                         "%s: Bus is still active after xfer. Reset it...\n",
418                         alg_data->adapter.name);
419                 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
420                           I2C_REG_CTL(alg_data));
421                 wait_reset(I2C_PNX_TIMEOUT, alg_data);
422         } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
423                 /* If there is data in the fifo's after transfer,
424                  * flush fifo's by reset.
425                  */
426                 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
427                           I2C_REG_CTL(alg_data));
428                 wait_reset(I2C_PNX_TIMEOUT, alg_data);
429         } else if (stat & mstatus_nai) {
430                 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
431                           I2C_REG_CTL(alg_data));
432                 wait_reset(I2C_PNX_TIMEOUT, alg_data);
433         }
434 }
435
436 /**
437  * i2c_pnx_xfer - generic transfer entry point
438  * @adap:               pointer to I2C adapter structure
439  * @msgs:               array of messages
440  * @num:                number of messages
441  *
442  * Initiates the transfer
443  */
444 static int
445 i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
446 {
447         struct i2c_msg *pmsg;
448         int rc = 0, completed = 0, i;
449         struct i2c_pnx_algo_data *alg_data = adap->algo_data;
450         u32 stat = ioread32(I2C_REG_STS(alg_data));
451
452         dev_dbg(&alg_data->adapter.dev,
453                 "%s(): entering: %d messages, stat = %04x.\n",
454                 __func__, num, ioread32(I2C_REG_STS(alg_data)));
455
456         bus_reset_if_active(alg_data);
457
458         /* Process transactions in a loop. */
459         for (i = 0; rc >= 0 && i < num; i++) {
460                 u8 addr;
461
462                 pmsg = &msgs[i];
463                 addr = pmsg->addr;
464
465                 if (pmsg->flags & I2C_M_TEN) {
466                         dev_err(&alg_data->adapter.dev,
467                                 "%s: 10 bits addr not supported!\n",
468                                 alg_data->adapter.name);
469                         rc = -EINVAL;
470                         break;
471                 }
472
473                 alg_data->mif.buf = pmsg->buf;
474                 alg_data->mif.len = pmsg->len;
475                 alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
476                         I2C_SMBUS_READ : I2C_SMBUS_WRITE;
477                 alg_data->mif.ret = 0;
478                 alg_data->last = (i == num - 1);
479
480                 dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
481                         __func__, alg_data->mif.mode, alg_data->mif.len);
482
483                 i2c_pnx_arm_timer(alg_data);
484
485                 /* initialize the completion var */
486                 init_completion(&alg_data->mif.complete);
487
488                 /* Enable master interrupt */
489                 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
490                                 mcntrl_naie | mcntrl_drmie,
491                           I2C_REG_CTL(alg_data));
492
493                 /* Put start-code and slave-address on the bus. */
494                 rc = i2c_pnx_start(addr, alg_data);
495                 if (rc < 0)
496                         break;
497
498                 /* Wait for completion */
499                 wait_for_completion(&alg_data->mif.complete);
500
501                 if (!(rc = alg_data->mif.ret))
502                         completed++;
503                 dev_dbg(&alg_data->adapter.dev,
504                         "%s(): Complete, return code = %d.\n",
505                         __func__, rc);
506
507                 /* Clear TDI and AFI bits in case they are set. */
508                 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
509                         dev_dbg(&alg_data->adapter.dev,
510                                 "%s: TDI still set... clearing now.\n",
511                                 alg_data->adapter.name);
512                         iowrite32(stat, I2C_REG_STS(alg_data));
513                 }
514                 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
515                         dev_dbg(&alg_data->adapter.dev,
516                                 "%s: AFI still set... clearing now.\n",
517                                 alg_data->adapter.name);
518                         iowrite32(stat, I2C_REG_STS(alg_data));
519                 }
520         }
521
522         bus_reset_if_active(alg_data);
523
524         /* Cleanup to be sure... */
525         alg_data->mif.buf = NULL;
526         alg_data->mif.len = 0;
527
528         dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
529                 __func__, ioread32(I2C_REG_STS(alg_data)));
530
531         if (completed != num)
532                 return ((rc < 0) ? rc : -EREMOTEIO);
533
534         return num;
535 }
536
537 static u32 i2c_pnx_func(struct i2c_adapter *adapter)
538 {
539         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
540 }
541
542 static struct i2c_algorithm pnx_algorithm = {
543         .master_xfer = i2c_pnx_xfer,
544         .functionality = i2c_pnx_func,
545 };
546
547 #ifdef CONFIG_PM
548 static int i2c_pnx_controller_suspend(struct platform_device *pdev,
549                                       pm_message_t state)
550 {
551         struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
552
553         /* FIXME: shouldn't this be clk_disable? */
554         clk_enable(alg_data->clk);
555
556         return 0;
557 }
558
559 static int i2c_pnx_controller_resume(struct platform_device *pdev)
560 {
561         struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
562
563         return clk_enable(alg_data->clk);
564 }
565 #else
566 #define i2c_pnx_controller_suspend      NULL
567 #define i2c_pnx_controller_resume       NULL
568 #endif
569
570 static int __devinit i2c_pnx_probe(struct platform_device *pdev)
571 {
572         unsigned long tmp;
573         int ret = 0;
574         struct i2c_pnx_algo_data *alg_data;
575         unsigned long freq;
576         struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
577
578         if (!i2c_pnx || !i2c_pnx->name) {
579                 dev_err(&pdev->dev, "%s: no platform data supplied\n",
580                        __func__);
581                 ret = -EINVAL;
582                 goto out;
583         }
584
585         alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
586         if (!alg_data) {
587                 ret = -ENOMEM;
588                 goto err_kzalloc;
589         }
590
591         platform_set_drvdata(pdev, alg_data);
592
593         strlcpy(alg_data->adapter.name, i2c_pnx->name,
594                 sizeof(alg_data->adapter.name));
595         alg_data->adapter.dev.parent = &pdev->dev;
596         alg_data->adapter.algo = &pnx_algorithm;
597         alg_data->adapter.algo_data = alg_data;
598         alg_data->adapter.nr = pdev->id;
599         alg_data->i2c_pnx = i2c_pnx;
600
601         alg_data->clk = clk_get(&pdev->dev, NULL);
602         if (IS_ERR(alg_data->clk)) {
603                 ret = PTR_ERR(alg_data->clk);
604                 goto out_drvdata;
605         }
606
607         init_timer(&alg_data->mif.timer);
608         alg_data->mif.timer.function = i2c_pnx_timeout;
609         alg_data->mif.timer.data = (unsigned long)alg_data;
610
611         /* Register I/O resource */
612         if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE,
613                                 pdev->name)) {
614                 dev_err(&pdev->dev,
615                        "I/O region 0x%08x for I2C already in use.\n",
616                        i2c_pnx->base);
617                 ret = -ENODEV;
618                 goto out_clkget;
619         }
620
621         alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE);
622         if (!alg_data->ioaddr) {
623                 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
624                 ret = -ENOMEM;
625                 goto out_release;
626         }
627
628         ret = clk_enable(alg_data->clk);
629         if (ret)
630                 goto out_unmap;
631
632         freq = clk_get_rate(alg_data->clk);
633
634         /*
635          * Clock Divisor High This value is the number of system clocks
636          * the serial clock (SCL) will be high.
637          * For example, if the system clock period is 50 ns and the maximum
638          * desired serial period is 10000 ns (100 kHz), then CLKHI would be
639          * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
640          * programmed into CLKHI will vary from this slightly due to
641          * variations in the output pad's rise and fall times as well as
642          * the deglitching filter length.
643          */
644
645         tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
646         iowrite32(tmp, I2C_REG_CKH(alg_data));
647         iowrite32(tmp, I2C_REG_CKL(alg_data));
648
649         iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
650         if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
651                 ret = -ENODEV;
652                 goto out_clock;
653         }
654         init_completion(&alg_data->mif.complete);
655
656         ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt,
657                         0, pdev->name, alg_data);
658         if (ret)
659                 goto out_clock;
660
661         /* Register this adapter with the I2C subsystem */
662         ret = i2c_add_numbered_adapter(&alg_data->adapter);
663         if (ret < 0) {
664                 dev_err(&pdev->dev, "I2C: Failed to add bus\n");
665                 goto out_irq;
666         }
667
668         dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
669                alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq);
670
671         return 0;
672
673 out_irq:
674         free_irq(i2c_pnx->irq, alg_data);
675 out_clock:
676         clk_disable(alg_data->clk);
677 out_unmap:
678         iounmap(alg_data->ioaddr);
679 out_release:
680         release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
681 out_clkget:
682         clk_put(alg_data->clk);
683 out_drvdata:
684         kfree(alg_data);
685 err_kzalloc:
686         platform_set_drvdata(pdev, NULL);
687 out:
688         return ret;
689 }
690
691 static int __devexit i2c_pnx_remove(struct platform_device *pdev)
692 {
693         struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
694         struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
695
696         free_irq(i2c_pnx->irq, alg_data);
697         i2c_del_adapter(&alg_data->adapter);
698         clk_disable(alg_data->clk);
699         iounmap(alg_data->ioaddr);
700         release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
701         clk_put(alg_data->clk);
702         kfree(alg_data);
703         platform_set_drvdata(pdev, NULL);
704
705         return 0;
706 }
707
708 static struct platform_driver i2c_pnx_driver = {
709         .driver = {
710                 .name = "pnx-i2c",
711                 .owner = THIS_MODULE,
712         },
713         .probe = i2c_pnx_probe,
714         .remove = __devexit_p(i2c_pnx_remove),
715         .suspend = i2c_pnx_controller_suspend,
716         .resume = i2c_pnx_controller_resume,
717 };
718
719 static int __init i2c_adap_pnx_init(void)
720 {
721         return platform_driver_register(&i2c_pnx_driver);
722 }
723
724 static void __exit i2c_adap_pnx_exit(void)
725 {
726         platform_driver_unregister(&i2c_pnx_driver);
727 }
728
729 MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
730 MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
731 MODULE_LICENSE("GPL");
732 MODULE_ALIAS("platform:pnx-i2c");
733
734 /* We need to make sure I2C is initialized before USB */
735 subsys_initcall(i2c_adap_pnx_init);
736 module_exit(i2c_adap_pnx_exit);