2597db6a0c37edbc7485294937e384ec5e570949
[linux-2.6.git] / drivers / i2c / busses / i2c-tegra.c
1 /*
2  * drivers/i2c/busses/i2c-tegra.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Colin Cross <ccross@android.com>
6  *
7  * Copyright (C) 2010-2012 NVIDIA Corporation
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 /*#define DEBUG           1*/
21 /*#define VERBOSE_DEBUG   1*/
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/platform_device.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/i2c.h>
29 #include <linux/io.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/i2c-tegra.h>
34 #include <linux/of_i2c.h>
35 #include <linux/spinlock.h>
36
37 #include <asm/unaligned.h>
38
39 #include <mach/clk.h>
40 #include <mach/pinmux.h>
41
42 #define TEGRA_I2C_TIMEOUT                       (msecs_to_jiffies(1000))
43 #define TEGRA_I2C_RETRIES                       3
44 #define BYTES_PER_FIFO_WORD                     4
45
46 #define I2C_CNFG                                0x000
47 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT             12
48 #define I2C_CNFG_PACKET_MODE_EN                 (1<<10)
49 #define I2C_CNFG_NEW_MASTER_FSM                 (1<<11)
50 #define I2C_STATUS                              0x01C
51 #define I2C_STATUS_BUSY                         (1<<8)
52 #define I2C_SL_CNFG                             0x020
53 #define I2C_SL_CNFG_NACK                        (1<<1)
54 #define I2C_SL_CNFG_NEWSL                       (1<<2)
55 #define I2C_SL_ADDR1                            0x02c
56 #define I2C_SL_ADDR2                            0x030
57 #define I2C_TX_FIFO                             0x050
58 #define I2C_RX_FIFO                             0x054
59 #define I2C_PACKET_TRANSFER_STATUS              0x058
60 #define I2C_FIFO_CONTROL                        0x05c
61 #define I2C_FIFO_CONTROL_TX_FLUSH               (1<<1)
62 #define I2C_FIFO_CONTROL_RX_FLUSH               (1<<0)
63 #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT          5
64 #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT          2
65 #define I2C_FIFO_STATUS                         0x060
66 #define I2C_FIFO_STATUS_TX_MASK                 0xF0
67 #define I2C_FIFO_STATUS_TX_SHIFT                4
68 #define I2C_FIFO_STATUS_RX_MASK                 0x0F
69 #define I2C_FIFO_STATUS_RX_SHIFT                0
70 #define I2C_INT_MASK                            0x064
71 #define I2C_INT_STATUS                          0x068
72 #define I2C_INT_PACKET_XFER_COMPLETE            (1<<7)
73 #define I2C_INT_ALL_PACKETS_XFER_COMPLETE       (1<<6)
74 #define I2C_INT_TX_FIFO_OVERFLOW                (1<<5)
75 #define I2C_INT_RX_FIFO_UNDERFLOW               (1<<4)
76 #define I2C_INT_NO_ACK                          (1<<3)
77 #define I2C_INT_ARBITRATION_LOST                (1<<2)
78 #define I2C_INT_TX_FIFO_DATA_REQ                (1<<1)
79 #define I2C_INT_RX_FIFO_DATA_REQ                (1<<0)
80 #define I2C_CLK_DIVISOR                         0x06c
81
82 #define DVC_CTRL_REG1                           0x000
83 #define DVC_CTRL_REG1_INTR_EN                   (1<<10)
84 #define DVC_CTRL_REG2                           0x004
85 #define DVC_CTRL_REG3                           0x008
86 #define DVC_CTRL_REG3_SW_PROG                   (1<<26)
87 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN          (1<<30)
88 #define DVC_STATUS                              0x00c
89 #define DVC_STATUS_I2C_DONE_INTR                (1<<30)
90
91 #define I2C_ERR_NONE                            0x00
92 #define I2C_ERR_NO_ACK                          0x01
93 #define I2C_ERR_ARBITRATION_LOST                0x02
94 #define I2C_ERR_UNKNOWN_INTERRUPT               0x04
95 #define I2C_ERR_UNEXPECTED_STATUS               0x08
96
97 #define PACKET_HEADER0_HEADER_SIZE_SHIFT        28
98 #define PACKET_HEADER0_PACKET_ID_SHIFT          16
99 #define PACKET_HEADER0_CONT_ID_SHIFT            12
100 #define PACKET_HEADER0_PROTOCOL_I2C             (1<<4)
101
102 #define I2C_HEADER_HIGHSPEED_MODE               (1<<22)
103 #define I2C_HEADER_CONT_ON_NAK                  (1<<21)
104 #define I2C_HEADER_SEND_START_BYTE              (1<<20)
105 #define I2C_HEADER_READ                         (1<<19)
106 #define I2C_HEADER_10BIT_ADDR                   (1<<18)
107 #define I2C_HEADER_IE_ENABLE                    (1<<17)
108 #define I2C_HEADER_REPEAT_START                 (1<<16)
109 #define I2C_HEADER_CONTINUE_XFER                (1<<15)
110 #define I2C_HEADER_MASTER_ADDR_SHIFT            12
111 #define I2C_HEADER_SLAVE_ADDR_SHIFT             1
112
113 #define SL_ADDR1(addr) (addr & 0xff)
114 #define SL_ADDR2(addr) ((addr >> 8) & 0xff)
115
116 /*
117  * msg_end_type: The bus control which need to be send at end of transfer.
118  * @MSG_END_STOP: Send stop pulse at end of transfer.
119  * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
120  * @MSG_END_CONTINUE: The following on message is coming and so do not send
121  *              stop or repeat start.
122  */
123
124 enum msg_end_type {
125         MSG_END_STOP,
126         MSG_END_REPEAT_START,
127         MSG_END_CONTINUE,
128 };
129
130 struct tegra_i2c_dev;
131
132 /**
133  * struct tegra_i2c_hw_feature : Different HW support on Tegra
134  * @has_continue_xfer_support: Continue transfer supports.
135  */
136
137 struct tegra_i2c_hw_feature {
138         bool has_continue_xfer_support;
139 };
140
141 struct tegra_i2c_bus {
142         struct tegra_i2c_dev *dev;
143         const struct tegra_pingroup_config *mux;
144         int mux_len;
145         unsigned long bus_clk_rate;
146         struct i2c_adapter adapter;
147         int scl_gpio;
148         int sda_gpio;
149 };
150
151 /**
152  * struct tegra_i2c_dev - per device i2c context
153  * @dev: device reference for power management
154  * @hw: Tegra i2c hw feature.
155  * @adapter: core i2c layer adapter information
156  * @div_clk: clock reference for div clock of i2c controller.
157  * @fast_clk: clock reference for fast clock of i2c controller.
158  * @base: ioremapped registers cookie
159  * @cont_id: i2c controller id, used for for packet header
160  * @irq: irq number of transfer complete interrupt
161  * @is_dvc: identifies the DVC i2c controller, has a different register layout
162  * @msg_complete: transfer completion notifier
163  * @msg_err: error code for completed message
164  * @msg_buf: pointer to current message data
165  * @msg_buf_remaining: size of unsent data in the message buffer
166  * @msg_read: identifies read transfers
167  * @bus_clk_rate: current i2c bus clock rate
168  * @is_suspended: prevents i2c controller accesses after suspend is called
169  */
170 struct tegra_i2c_dev {
171         struct device *dev;
172         struct tegra_i2c_hw_feature *hw;
173         struct clk *div_clk;
174         struct clk *fast_clk;
175         struct rt_mutex dev_lock;
176         spinlock_t fifo_lock;
177         void __iomem *base;
178         int cont_id;
179         int irq;
180         bool irq_disabled;
181         int is_dvc;
182         struct completion msg_complete;
183         int msg_err;
184         u8 *msg_buf;
185         u32 packet_header;
186         u32 payload_size;
187         u32 io_header;
188         size_t msg_buf_remaining;
189         int msg_read;
190         struct i2c_msg *msgs;
191         int msg_add;
192         int msgs_num;
193         bool is_suspended;
194         int bus_count;
195         const struct tegra_pingroup_config *last_mux;
196         int last_mux_len;
197         unsigned long last_bus_clk_rate;
198         u16 slave_addr;
199         bool is_clkon_always;
200         bool is_high_speed_enable;
201         u16 hs_master_code;
202         int (*arb_recovery)(int scl_gpio, int sda_gpio);
203         struct tegra_i2c_bus busses[1];
204 };
205
206 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
207 {
208         writel(val, i2c_dev->base + reg);
209 }
210
211 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
212 {
213         return readl(i2c_dev->base + reg);
214 }
215
216 static void dvc_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
217 {
218         u32 int_mask = dvc_readl(i2c_dev, DVC_CTRL_REG3);
219         int_mask &= ~mask;
220         dvc_writel(i2c_dev, int_mask, DVC_CTRL_REG3);
221 }
222
223 static void dvc_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
224 {
225         u32 int_mask = dvc_readl(i2c_dev, DVC_CTRL_REG3);
226         int_mask |= mask;
227         dvc_writel(i2c_dev, int_mask, DVC_CTRL_REG3);
228 }
229
230 /*
231  * i2c_writel and i2c_readl will offset the register if necessary to talk
232  * to the I2C block inside the DVC block
233  */
234 static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
235         unsigned long reg)
236 {
237         if (i2c_dev->is_dvc)
238                 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
239         return reg;
240 }
241
242 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
243         unsigned long reg)
244 {
245         writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
246
247         /* Read back register to make sure that register writes completed */
248         if (reg != I2C_TX_FIFO)
249                 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
250 }
251
252 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
253 {
254         return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
255 }
256
257 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
258         unsigned long reg, int len)
259 {
260         writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
261 }
262
263 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
264         unsigned long reg, int len)
265 {
266         readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
267 }
268
269 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
270 {
271         u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
272         int_mask &= ~mask;
273         i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
274 }
275
276 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
277 {
278         u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
279         int_mask |= mask;
280         i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
281 }
282
283 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
284 {
285         unsigned long timeout = jiffies + HZ;
286         u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
287         val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
288         i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
289
290         while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
291                 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
292                 if (time_after(jiffies, timeout)) {
293                         dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
294                         return -ETIMEDOUT;
295                 }
296                 msleep(1);
297         }
298         return 0;
299 }
300
301 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
302 {
303         u32 val;
304         int rx_fifo_avail;
305         u8 *buf = i2c_dev->msg_buf;
306         size_t buf_remaining = i2c_dev->msg_buf_remaining;
307         int words_to_transfer;
308
309         val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
310         rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
311                 I2C_FIFO_STATUS_RX_SHIFT;
312
313         /* Rounds down to not include partial word at the end of buf */
314         words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
315         if (words_to_transfer > rx_fifo_avail)
316                 words_to_transfer = rx_fifo_avail;
317
318         i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
319
320         buf += words_to_transfer * BYTES_PER_FIFO_WORD;
321         buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
322         rx_fifo_avail -= words_to_transfer;
323
324         /*
325          * If there is a partial word at the end of buf, handle it manually to
326          * prevent overwriting past the end of buf
327          */
328         if (rx_fifo_avail > 0 && buf_remaining > 0) {
329                 BUG_ON(buf_remaining > 3);
330                 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
331                 memcpy(buf, &val, buf_remaining);
332                 buf_remaining = 0;
333                 rx_fifo_avail--;
334         }
335
336         BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
337         i2c_dev->msg_buf_remaining = buf_remaining;
338         i2c_dev->msg_buf = buf;
339         return 0;
340 }
341
342 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
343 {
344         u32 val;
345         int tx_fifo_avail;
346         u8 *buf;
347         size_t buf_remaining;
348         int words_to_transfer;
349         unsigned long flags;
350
351         spin_lock_irqsave(&i2c_dev->fifo_lock, flags);
352         if (!i2c_dev->msg_buf_remaining) {
353                 spin_unlock_irqrestore(&i2c_dev->fifo_lock, flags);
354                 return 0;
355         }
356
357         buf = i2c_dev->msg_buf;
358         buf_remaining = i2c_dev->msg_buf_remaining;
359
360         val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
361         tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
362                 I2C_FIFO_STATUS_TX_SHIFT;
363
364         /* Rounds down to not include partial word at the end of buf */
365         words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
366
367         /* It's very common to have < 4 bytes, so optimize that case. */
368         if (words_to_transfer) {
369                 if (words_to_transfer > tx_fifo_avail)
370                         words_to_transfer = tx_fifo_avail;
371
372                 /*
373                  * Update state before writing to FIFO.  If this casues us
374                  * to finish writing all bytes (AKA buf_remaining goes to 0) we
375                  * have a potential for an interrupt (PACKET_XFER_COMPLETE is
376                  * not maskable).  We need to make sure that the isr sees
377                  * buf_remaining as 0 and doesn't call us back re-entrantly.
378                  */
379                 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
380                 tx_fifo_avail -= words_to_transfer;
381                 i2c_dev->msg_buf_remaining = buf_remaining;
382                 i2c_dev->msg_buf = buf +
383                         words_to_transfer * BYTES_PER_FIFO_WORD;
384                 barrier();
385
386                 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
387
388                 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
389         }
390
391         /*
392          * If there is a partial word at the end of buf, handle it manually to
393          * prevent reading past the end of buf, which could cross a page
394          * boundary and fault.
395          */
396         if (tx_fifo_avail > 0 && buf_remaining > 0) {
397                 if (buf_remaining > 3) {
398                         dev_err(i2c_dev->dev,
399                                 "Remaining buffer more than 3 %d\n",
400                                 buf_remaining);
401                         BUG();
402                 }
403                 memcpy(&val, buf, buf_remaining);
404
405                 /* Again update before writing to FIFO to make sure isr sees. */
406                 i2c_dev->msg_buf_remaining = 0;
407                 i2c_dev->msg_buf = NULL;
408                 barrier();
409
410                 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
411         }
412
413         spin_unlock_irqrestore(&i2c_dev->fifo_lock, flags);
414
415         return 0;
416 }
417
418 /*
419  * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
420  * block.  This block is identical to the rest of the I2C blocks, except that
421  * it only supports master mode, it has registers moved around, and it needs
422  * some extra init to get it into I2C mode.  The register moves are handled
423  * by i2c_readl and i2c_writel
424  */
425 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
426 {
427         u32 val = 0;
428         val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
429         val |= DVC_CTRL_REG3_SW_PROG;
430         dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
431
432         val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
433         val |= DVC_CTRL_REG1_INTR_EN;
434         dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
435 }
436
437 static void tegra_i2c_slave_init(struct tegra_i2c_dev *i2c_dev)
438 {
439         u32 val = I2C_SL_CNFG_NEWSL | I2C_SL_CNFG_NACK;
440
441         i2c_writel(i2c_dev, val, I2C_SL_CNFG);
442
443         if (i2c_dev->slave_addr) {
444                 u16 addr = i2c_dev->slave_addr;
445
446                 i2c_writel(i2c_dev, SL_ADDR1(addr), I2C_SL_ADDR1);
447                 i2c_writel(i2c_dev, SL_ADDR2(addr), I2C_SL_ADDR2);
448         }
449 }
450
451 static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
452 {
453         int ret;
454         ret = clk_enable(i2c_dev->fast_clk);
455         if (ret < 0) {
456                 dev_err(i2c_dev->dev,
457                         "Enabling fast clk failed, err %d\n", ret);
458                 return ret;
459         }
460         ret = clk_enable(i2c_dev->div_clk);
461         if (ret < 0) {
462                 dev_err(i2c_dev->dev,
463                         "Enabling div clk failed, err %d\n", ret);
464                 clk_disable(i2c_dev->fast_clk);
465         }
466         return ret;
467 }
468
469 static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
470 {
471         clk_disable(i2c_dev->div_clk);
472         clk_disable(i2c_dev->fast_clk);
473 }
474
475 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
476 {
477         u32 val;
478         int err = 0;
479
480         tegra_i2c_clock_enable(i2c_dev);
481
482         /* Interrupt generated before sending stop signal so
483         * wait for some time so that stop signal can be send proerly */
484         mdelay(1);
485
486         tegra_periph_reset_assert(i2c_dev->div_clk);
487         udelay(2);
488         tegra_periph_reset_deassert(i2c_dev->div_clk);
489
490         if (i2c_dev->is_dvc)
491                 tegra_dvc_init(i2c_dev);
492
493         val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
494                 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
495         i2c_writel(i2c_dev, val, I2C_CNFG);
496         i2c_writel(i2c_dev, 0, I2C_INT_MASK);
497         clk_set_rate(i2c_dev->div_clk, i2c_dev->last_bus_clk_rate * 8);
498         i2c_writel(i2c_dev, 0x3, I2C_CLK_DIVISOR);
499
500         if (!i2c_dev->is_dvc) {
501                 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
502                 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
503                 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
504                 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
505                 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
506
507         }
508
509         val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
510                 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
511         i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
512
513         if (!i2c_dev->is_dvc)
514                 tegra_i2c_slave_init(i2c_dev);
515
516         if (tegra_i2c_flush_fifos(i2c_dev))
517                 err = -ETIMEDOUT;
518
519         tegra_i2c_clock_disable(i2c_dev);
520
521         if (i2c_dev->irq_disabled) {
522                 i2c_dev->irq_disabled = 0;
523                 enable_irq(i2c_dev->irq);
524         }
525
526         return err;
527 }
528
529 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
530 {
531         u32 status;
532         const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST | I2C_INT_TX_FIFO_OVERFLOW;
533         struct tegra_i2c_dev *i2c_dev = dev_id;
534
535         status = i2c_readl(i2c_dev, I2C_INT_STATUS);
536
537         if (status == 0) {
538                 dev_warn(i2c_dev->dev, "unknown interrupt Add 0x%02x\n",
539                                                 i2c_dev->msg_add);
540                 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
541
542                 if (!i2c_dev->irq_disabled) {
543                         disable_irq_nosync(i2c_dev->irq);
544                         i2c_dev->irq_disabled = 1;
545                 }
546
547                 goto err;
548         }
549
550         if (unlikely(status & status_err)) {
551                 dev_warn(i2c_dev->dev, "I2c error status 0x%08x\n", status);
552                 if (status & I2C_INT_NO_ACK) {
553                         i2c_dev->msg_err |= I2C_ERR_NO_ACK;
554                         dev_warn(i2c_dev->dev, "no acknowledge from address"
555                                         " 0x%x\n", i2c_dev->msg_add);
556                         dev_warn(i2c_dev->dev, "Packet status 0x%08x\n",
557                                 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
558                 }
559
560                 if (status & I2C_INT_ARBITRATION_LOST) {
561                         i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
562                         dev_warn(i2c_dev->dev, "arbitration lost during "
563                                 " communicate to add 0x%x\n", i2c_dev->msg_add);
564                         dev_warn(i2c_dev->dev, "Packet status 0x%08x\n",
565                                 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
566                 }
567
568                 if (status & I2C_INT_TX_FIFO_OVERFLOW) {
569                         i2c_dev->msg_err |= I2C_INT_TX_FIFO_OVERFLOW;
570                         dev_warn(i2c_dev->dev, "Tx fifo overflow during "
571                                 " communicate to add 0x%x\n", i2c_dev->msg_add);
572                         dev_warn(i2c_dev->dev, "Packet status 0x%08x\n",
573                                 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
574                 }
575                 goto err;
576         }
577
578         if (unlikely((i2c_readl(i2c_dev, I2C_STATUS) & I2C_STATUS_BUSY)
579                                 && (status == I2C_INT_TX_FIFO_DATA_REQ)
580                                 && i2c_dev->msg_read
581                                 && i2c_dev->msg_buf_remaining)) {
582                 dev_warn(i2c_dev->dev, "unexpected status\n");
583                 i2c_dev->msg_err |= I2C_ERR_UNEXPECTED_STATUS;
584
585                 if (!i2c_dev->irq_disabled) {
586                         disable_irq_nosync(i2c_dev->irq);
587                         i2c_dev->irq_disabled = 1;
588                 }
589
590                 goto err;
591         }
592
593         if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
594                 if (i2c_dev->msg_buf_remaining)
595                         tegra_i2c_empty_rx_fifo(i2c_dev);
596                 else
597                         BUG();
598         }
599
600         if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
601                 if (i2c_dev->msg_buf_remaining)
602                         tegra_i2c_fill_tx_fifo(i2c_dev);
603                 else
604                         tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
605         }
606
607         i2c_writel(i2c_dev, status, I2C_INT_STATUS);
608
609         if (i2c_dev->is_dvc)
610                 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
611
612         if (status & I2C_INT_PACKET_XFER_COMPLETE) {
613                 BUG_ON(i2c_dev->msg_buf_remaining);
614                 complete(&i2c_dev->msg_complete);
615         }
616
617         return IRQ_HANDLED;
618
619 err:
620         dev_dbg(i2c_dev->dev, "reg: 0x%08x 0x%08x 0x%08x 0x%08x\n",
621                  i2c_readl(i2c_dev, I2C_CNFG), i2c_readl(i2c_dev, I2C_STATUS),
622                  i2c_readl(i2c_dev, I2C_INT_STATUS),
623                  i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
624
625         dev_dbg(i2c_dev->dev, "packet: 0x%08x %u 0x%08x\n",
626                  i2c_dev->packet_header, i2c_dev->payload_size,
627                  i2c_dev->io_header);
628
629         if (i2c_dev->msgs) {
630                 struct i2c_msg *msgs = i2c_dev->msgs;
631                 int i;
632
633                 for (i = 0; i < i2c_dev->msgs_num; i++)
634                         dev_dbg(i2c_dev->dev,
635                                  "msgs[%d] %c, addr=0x%04x, len=%d\n",
636                                  i, (msgs[i].flags & I2C_M_RD) ? 'R' : 'W',
637                                  msgs[i].addr, msgs[i].len);
638         }
639
640         /* An error occurred, mask all interrupts */
641         tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
642                 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
643                 I2C_INT_RX_FIFO_DATA_REQ | I2C_INT_TX_FIFO_OVERFLOW);
644
645         i2c_writel(i2c_dev, status, I2C_INT_STATUS);
646
647         /* An error occured, mask dvc interrupt */
648         if (i2c_dev->is_dvc)
649                 dvc_i2c_mask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
650
651         if (i2c_dev->is_dvc)
652                 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
653
654         complete(&i2c_dev->msg_complete);
655         return IRQ_HANDLED;
656 }
657
658 static int tegra_i2c_xfer_msg(struct tegra_i2c_bus *i2c_bus,
659         struct i2c_msg *msg, enum msg_end_type end_state)
660 {
661         struct tegra_i2c_dev *i2c_dev = i2c_bus->dev;
662         u32 int_mask;
663         int ret;
664         int arb_stat;
665
666         if (msg->len == 0)
667                 return -EINVAL;
668
669         tegra_i2c_flush_fifos(i2c_dev);
670
671         /* Toggle the direction flag if rev dir is selected */
672         if (msg->flags & I2C_M_REV_DIR_ADDR)
673                 msg->flags ^= I2C_M_RD;
674
675         i2c_dev->msg_buf = msg->buf;
676         i2c_dev->msg_buf_remaining = msg->len;
677         i2c_dev->msg_err = I2C_ERR_NONE;
678         i2c_dev->msg_read = (msg->flags & I2C_M_RD);
679         INIT_COMPLETION(i2c_dev->msg_complete);
680         i2c_dev->msg_add = msg->addr;
681
682         i2c_dev->packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
683                         PACKET_HEADER0_PROTOCOL_I2C |
684                         (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
685                         (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
686         i2c_writel(i2c_dev, i2c_dev->packet_header, I2C_TX_FIFO);
687
688         i2c_dev->payload_size = msg->len - 1;
689         i2c_writel(i2c_dev, i2c_dev->payload_size, I2C_TX_FIFO);
690
691         i2c_dev->io_header = I2C_HEADER_IE_ENABLE;
692         if (end_state == MSG_END_CONTINUE)
693                 i2c_dev->io_header |= I2C_HEADER_CONTINUE_XFER;
694         else if (end_state == MSG_END_REPEAT_START)
695                 i2c_dev->io_header |= I2C_HEADER_REPEAT_START;
696
697         if (msg->flags & I2C_M_TEN) {
698                 i2c_dev->io_header |= msg->addr;
699                 i2c_dev->io_header |= I2C_HEADER_10BIT_ADDR;
700         } else {
701                 i2c_dev->io_header |= (msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT);
702         }
703         if (msg->flags & I2C_M_IGNORE_NAK)
704                 i2c_dev->io_header |= I2C_HEADER_CONT_ON_NAK;
705         if (msg->flags & I2C_M_RD)
706                 i2c_dev->io_header |= I2C_HEADER_READ;
707         if (i2c_dev->is_high_speed_enable) {
708                 i2c_dev->io_header |= I2C_HEADER_HIGHSPEED_MODE;
709                 i2c_dev->io_header |= ((i2c_dev->hs_master_code & 0x7) <<  I2C_HEADER_MASTER_ADDR_SHIFT);
710         }
711         i2c_writel(i2c_dev, i2c_dev->io_header, I2C_TX_FIFO);
712
713         if (!(msg->flags & I2C_M_RD))
714                 tegra_i2c_fill_tx_fifo(i2c_dev);
715
716         if (i2c_dev->is_dvc)
717                 dvc_i2c_unmask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
718
719         int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST | I2C_INT_TX_FIFO_OVERFLOW;
720         if (msg->flags & I2C_M_RD)
721                 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
722         else if (i2c_dev->msg_buf_remaining)
723                 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
724         tegra_i2c_unmask_irq(i2c_dev, int_mask);
725         dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
726                 i2c_readl(i2c_dev, I2C_INT_MASK));
727
728         ret = wait_for_completion_timeout(&i2c_dev->msg_complete,
729                                         TEGRA_I2C_TIMEOUT);
730         tegra_i2c_mask_irq(i2c_dev, int_mask);
731
732         if (i2c_dev->is_dvc)
733                 dvc_i2c_mask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
734
735         /* Restore the message flag */
736         if (msg->flags & I2C_M_REV_DIR_ADDR)
737                 msg->flags ^= I2C_M_RD;
738
739         if (WARN_ON(ret == 0)) {
740                 dev_err(i2c_dev->dev,
741                         "i2c transfer timed out, addr 0x%04x, data 0x%02x\n",
742                         msg->addr, msg->buf[0]);
743
744                 tegra_i2c_init(i2c_dev);
745                 return -ETIMEDOUT;
746         }
747
748         dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
749                 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
750
751         if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
752                 return 0;
753
754         /* Arbitration Lost occurs, Start recovery */
755         if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
756                 if (i2c_dev->arb_recovery) {
757                         arb_stat = i2c_dev->arb_recovery(i2c_bus->scl_gpio, i2c_bus->sda_gpio);
758                         if (!arb_stat)
759                                 return -EAGAIN;
760                 }
761         }
762
763         tegra_i2c_init(i2c_dev);
764
765         if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
766                 if (msg->flags & I2C_M_IGNORE_NAK)
767                         return 0;
768                 return -EREMOTEIO;
769         }
770
771         if (i2c_dev->msg_err & I2C_ERR_UNEXPECTED_STATUS)
772                 return -EAGAIN;
773
774         return -EIO;
775 }
776
777 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
778         int num)
779 {
780         struct tegra_i2c_bus *i2c_bus = i2c_get_adapdata(adap);
781         struct tegra_i2c_dev *i2c_dev = i2c_bus->dev;
782         int i;
783         int ret = 0;
784
785         rt_mutex_lock(&i2c_dev->dev_lock);
786
787         if (i2c_dev->is_suspended) {
788                 rt_mutex_unlock(&i2c_dev->dev_lock);
789                 return -EBUSY;
790         }
791
792         /* Support I2C_M_NOSTART only if HW support continue xfer. */
793         for (i = 0; i < num - 1; i++) {
794                         if ((msgs[i + 1].flags & I2C_M_NOSTART) &&
795                         !i2c_dev->hw->has_continue_xfer_support) {
796                         dev_err(i2c_dev->dev,
797                                 "mesg %d have illegal flag\n", i + 1);
798                         rt_mutex_unlock(&i2c_dev->dev_lock);
799                         return -EINVAL;
800                 }
801         }
802
803         if (i2c_dev->last_mux != i2c_bus->mux) {
804                 tegra_pinmux_set_safe_pinmux_table(i2c_dev->last_mux,
805                         i2c_dev->last_mux_len);
806                 tegra_pinmux_config_pinmux_table(i2c_bus->mux,
807                         i2c_bus->mux_len);
808                 i2c_dev->last_mux = i2c_bus->mux;
809                 i2c_dev->last_mux_len = i2c_bus->mux_len;
810         }
811
812         if (i2c_dev->last_bus_clk_rate != i2c_bus->bus_clk_rate) {
813                 clk_set_rate(i2c_dev->div_clk, i2c_bus->bus_clk_rate * 8);
814                 i2c_dev->last_bus_clk_rate = i2c_bus->bus_clk_rate;
815         }
816
817         i2c_dev->msgs = msgs;
818         i2c_dev->msgs_num = num;
819
820         tegra_i2c_clock_enable(i2c_dev);
821
822         for (i = 0; i < num; i++) {
823                 enum msg_end_type end_type = MSG_END_STOP;
824                 if (i < (num - 1)) {
825                         if (msgs[i + 1].flags & I2C_M_NOSTART)
826                                 end_type = MSG_END_CONTINUE;
827                         else
828                                 end_type = MSG_END_REPEAT_START;
829                 }
830                 ret = tegra_i2c_xfer_msg(i2c_bus, &msgs[i], end_type);
831                 if (ret)
832                         break;
833         }
834
835         tegra_i2c_clock_disable(i2c_dev);
836
837         rt_mutex_unlock(&i2c_dev->dev_lock);
838
839         i2c_dev->msgs = NULL;
840         i2c_dev->msgs_num = 0;
841
842         return ret ?: i;
843 }
844
845 static u32 tegra_i2c_func(struct i2c_adapter *adap)
846 {
847         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
848                         I2C_FUNC_PROTOCOL_MANGLING;
849 }
850
851 static const struct i2c_algorithm tegra_i2c_algo = {
852         .master_xfer    = tegra_i2c_xfer,
853         .functionality  = tegra_i2c_func,
854 };
855
856 static struct tegra_i2c_hw_feature tegra20_i2c_hw = {
857         .has_continue_xfer_support = false,
858 };
859
860 static struct tegra_i2c_hw_feature tegra30_i2c_hw = {
861         .has_continue_xfer_support = true,
862 };
863
864 #if defined(CONFIG_OF)
865 /* Match table for of_platform binding */
866 static const struct of_device_id tegra_i2c_of_match[] __devinitconst = {
867         { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
868         {},
869 };
870 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
871 #endif
872
873 static int __devinit tegra_i2c_probe(struct platform_device *pdev)
874 {
875         struct tegra_i2c_dev *i2c_dev;
876         struct tegra_i2c_platform_data *plat = pdev->dev.platform_data;
877         struct resource *res;
878         struct clk *div_clk;
879         struct clk *fast_clk = NULL;
880         const unsigned int *prop;
881         void __iomem *base;
882         int irq;
883         int nbus;
884         int i = 0;
885         int ret = 0;
886
887         if (!plat) {
888                 dev_err(&pdev->dev, "no platform data?\n");
889                 return -ENODEV;
890         }
891
892         if (plat->bus_count <= 0 || plat->adapter_nr < 0) {
893                 dev_err(&pdev->dev, "invalid platform data?\n");
894                 return -ENODEV;
895         }
896
897         WARN_ON(plat->bus_count > TEGRA_I2C_MAX_BUS);
898         nbus = min(TEGRA_I2C_MAX_BUS, plat->bus_count);
899
900         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
901         if (!res) {
902                 dev_err(&pdev->dev, "no mem resource\n");
903                 return -EINVAL;
904         }
905
906         base = devm_request_and_ioremap(&pdev->dev, res);
907         if (!base) {
908                 dev_err(&pdev->dev, "Cannot request/ioremap I2C registers\n");
909                 return -EADDRNOTAVAIL;
910         }
911
912         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
913         if (!res) {
914                 dev_err(&pdev->dev, "no irq resource\n");
915                 return -EINVAL;
916         }
917         irq = res->start;
918
919         div_clk = devm_clk_get(&pdev->dev, "i2c-div");
920         if (IS_ERR(div_clk)) {
921                 dev_err(&pdev->dev, "missing controller clock");
922                 return PTR_ERR(div_clk);
923         }
924
925         fast_clk = devm_clk_get(&pdev->dev, "i2c-fast");
926         if (IS_ERR(fast_clk)) {
927                 dev_err(&pdev->dev, "missing controller fast clock");
928                 return PTR_ERR(fast_clk);
929         }
930
931         i2c_dev = devm_kzalloc(&pdev->dev, sizeof(struct tegra_i2c_dev) +
932                           (nbus-1) * sizeof(struct tegra_i2c_bus), GFP_KERNEL);
933         if (!i2c_dev) {
934                 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
935                 return -ENOMEM;
936         }
937
938         i2c_dev->base = base;
939         i2c_dev->div_clk = div_clk;
940         i2c_dev->fast_clk = fast_clk;
941         i2c_dev->irq = irq;
942         i2c_dev->cont_id = pdev->id;
943         i2c_dev->dev = &pdev->dev;
944         i2c_dev->is_clkon_always = plat->is_clkon_always;
945
946
947 #ifdef ARCH_TEGRA_2x_SOC
948         i2c_dev->hw = &tegra20_i2c_hw;
949 #else
950         i2c_dev->hw = &tegra30_i2c_hw;
951 #endif
952
953         i2c_dev->last_bus_clk_rate = 100000; /* default clock rate */
954         if (plat) {
955                 i2c_dev->last_bus_clk_rate = plat->bus_clk_rate[0];
956
957         } else if (i2c_dev->dev->of_node) {    /* if there is a device tree node ... */
958                 /* TODO: DAN: this doesn't work for DT */
959                 prop = of_get_property(i2c_dev->dev->of_node,
960                                 "clock-frequency", NULL);
961                 if (prop)
962                         i2c_dev->last_bus_clk_rate = be32_to_cpup(prop);
963
964                 /* FIXME! Populate the Tegra30 and then support M_NOSTART */
965                 i2c_dev->hw = &tegra20_i2c_hw;
966         }
967
968         i2c_dev->is_high_speed_enable = plat->is_high_speed_enable;
969         i2c_dev->last_bus_clk_rate = plat->bus_clk_rate[0] ?: 100000;
970         i2c_dev->msgs = NULL;
971         i2c_dev->msgs_num = 0;
972         rt_mutex_init(&i2c_dev->dev_lock);
973         spin_lock_init(&i2c_dev->fifo_lock);
974
975         i2c_dev->slave_addr = plat->slave_addr;
976         i2c_dev->hs_master_code = plat->hs_master_code;
977         i2c_dev->is_dvc = plat->is_dvc;
978         i2c_dev->arb_recovery = plat->arb_recovery;
979         init_completion(&i2c_dev->msg_complete);
980
981         platform_set_drvdata(pdev, i2c_dev);
982
983         if (i2c_dev->is_clkon_always)
984                 tegra_i2c_clock_enable(i2c_dev);
985
986         ret = tegra_i2c_init(i2c_dev);
987         if (ret) {
988                 dev_err(&pdev->dev, "Failed to initialize i2c controller");
989                 return ret;
990         }
991
992         ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
993                         tegra_i2c_isr, 0, pdev->name, i2c_dev);
994         if (ret) {
995                 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
996                 return ret;
997         }
998
999
1000         for (i = 0; i < nbus; i++) {
1001                 struct tegra_i2c_bus *i2c_bus = &i2c_dev->busses[i];
1002
1003                 i2c_bus->dev = i2c_dev;
1004                 i2c_bus->mux = plat->bus_mux[i];
1005                 i2c_bus->mux_len = plat->bus_mux_len[i];
1006                 i2c_bus->bus_clk_rate = plat->bus_clk_rate[i] ?: 100000;
1007
1008                 i2c_bus->scl_gpio = plat->scl_gpio[i];
1009                 i2c_bus->sda_gpio = plat->sda_gpio[i];
1010
1011                 i2c_bus->adapter.dev.of_node = pdev->dev.of_node;
1012                 i2c_bus->adapter.algo = &tegra_i2c_algo;
1013                 i2c_set_adapdata(&i2c_bus->adapter, i2c_bus);
1014                 i2c_bus->adapter.owner = THIS_MODULE;
1015                 i2c_bus->adapter.class = I2C_CLASS_HWMON;
1016                 strlcpy(i2c_bus->adapter.name, "Tegra I2C adapter",
1017                         sizeof(i2c_bus->adapter.name));
1018                 i2c_bus->adapter.dev.parent = &pdev->dev;
1019                 i2c_bus->adapter.nr = plat->adapter_nr + i;
1020
1021                 if (plat->retries)
1022                         i2c_bus->adapter.retries = plat->retries;
1023                 else
1024                         i2c_bus->adapter.retries = TEGRA_I2C_RETRIES;
1025
1026                 if (plat->timeout)
1027                         i2c_bus->adapter.timeout = plat->timeout;
1028
1029                 ret = i2c_add_numbered_adapter(&i2c_bus->adapter);
1030                 if (ret) {
1031                         dev_err(&pdev->dev, "Failed to add I2C adapter\n");
1032                         goto err_del_bus;
1033                 }
1034                 of_i2c_register_devices(&i2c_bus->adapter);
1035
1036                 i2c_dev->bus_count++;
1037         }
1038
1039         return 0;
1040
1041 err_del_bus:
1042         while (i2c_dev->bus_count--)
1043                 i2c_del_adapter(&i2c_dev->busses[i2c_dev->bus_count].adapter);
1044         return ret;
1045 }
1046
1047 static int __devexit tegra_i2c_remove(struct platform_device *pdev)
1048 {
1049         struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1050
1051         while (i2c_dev->bus_count--)
1052                 i2c_del_adapter(&i2c_dev->busses[i2c_dev->bus_count].adapter);
1053
1054         if (i2c_dev->is_clkon_always)
1055                 tegra_i2c_clock_disable(i2c_dev);
1056         return 0;
1057 }
1058
1059 #ifdef CONFIG_PM_SLEEP
1060 static int tegra_i2c_suspend_noirq(struct device *dev)
1061 {
1062         struct platform_device *pdev = to_platform_device(dev);
1063         struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1064
1065         rt_mutex_lock(&i2c_dev->dev_lock);
1066
1067         i2c_dev->is_suspended = true;
1068         if (i2c_dev->is_clkon_always)
1069                 tegra_i2c_clock_disable(i2c_dev);
1070
1071         rt_mutex_unlock(&i2c_dev->dev_lock);
1072
1073         return 0;
1074 }
1075
1076 static int tegra_i2c_resume_noirq(struct device *dev)
1077 {
1078         struct platform_device *pdev = to_platform_device(dev);
1079         struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1080         int ret;
1081
1082         rt_mutex_lock(&i2c_dev->dev_lock);
1083
1084         if (i2c_dev->is_clkon_always)
1085                 tegra_i2c_clock_enable(i2c_dev);
1086
1087         ret = tegra_i2c_init(i2c_dev);
1088
1089         if (ret) {
1090                 rt_mutex_unlock(&i2c_dev->dev_lock);
1091                 return ret;
1092         }
1093
1094         i2c_dev->is_suspended = false;
1095
1096         rt_mutex_unlock(&i2c_dev->dev_lock);
1097
1098         return 0;
1099 }
1100
1101 static const struct dev_pm_ops tegra_i2c_pm = {
1102         .suspend_noirq = tegra_i2c_suspend_noirq,
1103         .resume_noirq = tegra_i2c_resume_noirq,
1104 };
1105 #define TEGRA_I2C_PM    (&tegra_i2c_pm)
1106 #else
1107 #define TEGRA_I2C_PM    NULL
1108 #endif
1109
1110 static struct platform_driver tegra_i2c_driver = {
1111         .probe   = tegra_i2c_probe,
1112         .remove  = __devexit_p(tegra_i2c_remove),
1113         .driver  = {
1114                 .name  = "tegra-i2c",
1115                 .owner = THIS_MODULE,
1116                 .of_match_table = of_match_ptr(tegra_i2c_of_match),
1117                 .pm    = TEGRA_I2C_PM,
1118         },
1119 };
1120
1121 static int __init tegra_i2c_init_driver(void)
1122 {
1123         return platform_driver_register(&tegra_i2c_driver);
1124 }
1125
1126 static void __exit tegra_i2c_exit_driver(void)
1127 {
1128         platform_driver_unregister(&tegra_i2c_driver);
1129 }
1130
1131 subsys_initcall(tegra_i2c_init_driver);
1132 module_exit(tegra_i2c_exit_driver);
1133
1134 MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1135 MODULE_AUTHOR("Colin Cross");
1136 MODULE_LICENSE("GPL v2");