2 * drivers/i2c/busses/i2c-tegra.c
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c-tegra.h>
29 #include <linux/of_i2c.h>
30 #include <linux/module.h>
32 #include <asm/unaligned.h>
36 #define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
37 #define BYTES_PER_FIFO_WORD 4
39 #define I2C_CNFG 0x000
40 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
41 #define I2C_CNFG_PACKET_MODE_EN (1<<10)
42 #define I2C_CNFG_NEW_MASTER_FSM (1<<11)
43 #define I2C_STATUS 0x01C
44 #define I2C_SL_CNFG 0x020
45 #define I2C_SL_CNFG_NACK (1<<1)
46 #define I2C_SL_CNFG_NEWSL (1<<2)
47 #define I2C_SL_ADDR1 0x02c
48 #define I2C_SL_ADDR2 0x030
49 #define I2C_TX_FIFO 0x050
50 #define I2C_RX_FIFO 0x054
51 #define I2C_PACKET_TRANSFER_STATUS 0x058
52 #define I2C_FIFO_CONTROL 0x05c
53 #define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
54 #define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
55 #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
56 #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
57 #define I2C_FIFO_STATUS 0x060
58 #define I2C_FIFO_STATUS_TX_MASK 0xF0
59 #define I2C_FIFO_STATUS_TX_SHIFT 4
60 #define I2C_FIFO_STATUS_RX_MASK 0x0F
61 #define I2C_FIFO_STATUS_RX_SHIFT 0
62 #define I2C_INT_MASK 0x064
63 #define I2C_INT_STATUS 0x068
64 #define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
65 #define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
66 #define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
67 #define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
68 #define I2C_INT_NO_ACK (1<<3)
69 #define I2C_INT_ARBITRATION_LOST (1<<2)
70 #define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
71 #define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
72 #define I2C_CLK_DIVISOR 0x06c
74 #define DVC_CTRL_REG1 0x000
75 #define DVC_CTRL_REG1_INTR_EN (1<<10)
76 #define DVC_CTRL_REG2 0x004
77 #define DVC_CTRL_REG3 0x008
78 #define DVC_CTRL_REG3_SW_PROG (1<<26)
79 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
80 #define DVC_STATUS 0x00c
81 #define DVC_STATUS_I2C_DONE_INTR (1<<30)
83 #define I2C_ERR_NONE 0x00
84 #define I2C_ERR_NO_ACK 0x01
85 #define I2C_ERR_ARBITRATION_LOST 0x02
86 #define I2C_ERR_UNKNOWN_INTERRUPT 0x04
88 #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
89 #define PACKET_HEADER0_PACKET_ID_SHIFT 16
90 #define PACKET_HEADER0_CONT_ID_SHIFT 12
91 #define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
93 #define I2C_HEADER_HIGHSPEED_MODE (1<<22)
94 #define I2C_HEADER_CONT_ON_NAK (1<<21)
95 #define I2C_HEADER_SEND_START_BYTE (1<<20)
96 #define I2C_HEADER_READ (1<<19)
97 #define I2C_HEADER_10BIT_ADDR (1<<18)
98 #define I2C_HEADER_IE_ENABLE (1<<17)
99 #define I2C_HEADER_REPEAT_START (1<<16)
100 #define I2C_HEADER_MASTER_ADDR_SHIFT 12
101 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
104 * struct tegra_i2c_dev - per device i2c context
105 * @dev: device reference for power management
106 * @adapter: core i2c layer adapter information
107 * @clk: clock reference for i2c controller
108 * @i2c_clk: clock reference for i2c bus
109 * @iomem: memory resource for registers
110 * @base: ioremapped registers cookie
111 * @cont_id: i2c controller id, used for for packet header
112 * @irq: irq number of transfer complete interrupt
113 * @is_dvc: identifies the DVC i2c controller, has a different register layout
114 * @msg_complete: transfer completion notifier
115 * @msg_err: error code for completed message
116 * @msg_buf: pointer to current message data
117 * @msg_buf_remaining: size of unsent data in the message buffer
118 * @msg_read: identifies read transfers
119 * @bus_clk_rate: current i2c bus clock rate
120 * @is_suspended: prevents i2c controller accesses after suspend is called
122 struct tegra_i2c_dev
{
124 struct i2c_adapter adapter
;
127 struct resource
*iomem
;
133 struct completion msg_complete
;
136 size_t msg_buf_remaining
;
138 unsigned long bus_clk_rate
;
142 static void dvc_writel(struct tegra_i2c_dev
*i2c_dev
, u32 val
, unsigned long reg
)
144 writel(val
, i2c_dev
->base
+ reg
);
147 static u32
dvc_readl(struct tegra_i2c_dev
*i2c_dev
, unsigned long reg
)
149 return readl(i2c_dev
->base
+ reg
);
153 * i2c_writel and i2c_readl will offset the register if necessary to talk
154 * to the I2C block inside the DVC block
156 static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev
*i2c_dev
,
160 reg
+= (reg
>= I2C_TX_FIFO
) ? 0x10 : 0x40;
164 static void i2c_writel(struct tegra_i2c_dev
*i2c_dev
, u32 val
,
167 writel(val
, i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
));
170 static u32
i2c_readl(struct tegra_i2c_dev
*i2c_dev
, unsigned long reg
)
172 return readl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
));
175 static void i2c_writesl(struct tegra_i2c_dev
*i2c_dev
, void *data
,
176 unsigned long reg
, int len
)
178 writesl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
), data
, len
);
181 static void i2c_readsl(struct tegra_i2c_dev
*i2c_dev
, void *data
,
182 unsigned long reg
, int len
)
184 readsl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
), data
, len
);
187 static void tegra_i2c_mask_irq(struct tegra_i2c_dev
*i2c_dev
, u32 mask
)
189 u32 int_mask
= i2c_readl(i2c_dev
, I2C_INT_MASK
);
191 i2c_writel(i2c_dev
, int_mask
, I2C_INT_MASK
);
194 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev
*i2c_dev
, u32 mask
)
196 u32 int_mask
= i2c_readl(i2c_dev
, I2C_INT_MASK
);
198 i2c_writel(i2c_dev
, int_mask
, I2C_INT_MASK
);
201 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev
*i2c_dev
)
203 unsigned long timeout
= jiffies
+ HZ
;
204 u32 val
= i2c_readl(i2c_dev
, I2C_FIFO_CONTROL
);
205 val
|= I2C_FIFO_CONTROL_TX_FLUSH
| I2C_FIFO_CONTROL_RX_FLUSH
;
206 i2c_writel(i2c_dev
, val
, I2C_FIFO_CONTROL
);
208 while (i2c_readl(i2c_dev
, I2C_FIFO_CONTROL
) &
209 (I2C_FIFO_CONTROL_TX_FLUSH
| I2C_FIFO_CONTROL_RX_FLUSH
)) {
210 if (time_after(jiffies
, timeout
)) {
211 dev_warn(i2c_dev
->dev
, "timeout waiting for fifo flush\n");
219 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev
*i2c_dev
)
223 u8
*buf
= i2c_dev
->msg_buf
;
224 size_t buf_remaining
= i2c_dev
->msg_buf_remaining
;
225 int words_to_transfer
;
227 val
= i2c_readl(i2c_dev
, I2C_FIFO_STATUS
);
228 rx_fifo_avail
= (val
& I2C_FIFO_STATUS_RX_MASK
) >>
229 I2C_FIFO_STATUS_RX_SHIFT
;
231 /* Rounds down to not include partial word at the end of buf */
232 words_to_transfer
= buf_remaining
/ BYTES_PER_FIFO_WORD
;
233 if (words_to_transfer
> rx_fifo_avail
)
234 words_to_transfer
= rx_fifo_avail
;
236 i2c_readsl(i2c_dev
, buf
, I2C_RX_FIFO
, words_to_transfer
);
238 buf
+= words_to_transfer
* BYTES_PER_FIFO_WORD
;
239 buf_remaining
-= words_to_transfer
* BYTES_PER_FIFO_WORD
;
240 rx_fifo_avail
-= words_to_transfer
;
243 * If there is a partial word at the end of buf, handle it manually to
244 * prevent overwriting past the end of buf
246 if (rx_fifo_avail
> 0 && buf_remaining
> 0) {
247 BUG_ON(buf_remaining
> 3);
248 val
= i2c_readl(i2c_dev
, I2C_RX_FIFO
);
249 memcpy(buf
, &val
, buf_remaining
);
254 BUG_ON(rx_fifo_avail
> 0 && buf_remaining
> 0);
255 i2c_dev
->msg_buf_remaining
= buf_remaining
;
256 i2c_dev
->msg_buf
= buf
;
260 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev
*i2c_dev
)
264 u8
*buf
= i2c_dev
->msg_buf
;
265 size_t buf_remaining
= i2c_dev
->msg_buf_remaining
;
266 int words_to_transfer
;
268 val
= i2c_readl(i2c_dev
, I2C_FIFO_STATUS
);
269 tx_fifo_avail
= (val
& I2C_FIFO_STATUS_TX_MASK
) >>
270 I2C_FIFO_STATUS_TX_SHIFT
;
272 /* Rounds down to not include partial word at the end of buf */
273 words_to_transfer
= buf_remaining
/ BYTES_PER_FIFO_WORD
;
274 if (words_to_transfer
> tx_fifo_avail
)
275 words_to_transfer
= tx_fifo_avail
;
277 i2c_writesl(i2c_dev
, buf
, I2C_TX_FIFO
, words_to_transfer
);
279 buf
+= words_to_transfer
* BYTES_PER_FIFO_WORD
;
280 buf_remaining
-= words_to_transfer
* BYTES_PER_FIFO_WORD
;
281 tx_fifo_avail
-= words_to_transfer
;
284 * If there is a partial word at the end of buf, handle it manually to
285 * prevent reading past the end of buf, which could cross a page
286 * boundary and fault.
288 if (tx_fifo_avail
> 0 && buf_remaining
> 0) {
289 BUG_ON(buf_remaining
> 3);
290 memcpy(&val
, buf
, buf_remaining
);
291 i2c_writel(i2c_dev
, val
, I2C_TX_FIFO
);
296 BUG_ON(tx_fifo_avail
> 0 && buf_remaining
> 0);
297 i2c_dev
->msg_buf_remaining
= buf_remaining
;
298 i2c_dev
->msg_buf
= buf
;
303 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
304 * block. This block is identical to the rest of the I2C blocks, except that
305 * it only supports master mode, it has registers moved around, and it needs
306 * some extra init to get it into I2C mode. The register moves are handled
307 * by i2c_readl and i2c_writel
309 static void tegra_dvc_init(struct tegra_i2c_dev
*i2c_dev
)
312 val
= dvc_readl(i2c_dev
, DVC_CTRL_REG3
);
313 val
|= DVC_CTRL_REG3_SW_PROG
;
314 val
|= DVC_CTRL_REG3_I2C_DONE_INTR_EN
;
315 dvc_writel(i2c_dev
, val
, DVC_CTRL_REG3
);
317 val
= dvc_readl(i2c_dev
, DVC_CTRL_REG1
);
318 val
|= DVC_CTRL_REG1_INTR_EN
;
319 dvc_writel(i2c_dev
, val
, DVC_CTRL_REG1
);
322 static int tegra_i2c_init(struct tegra_i2c_dev
*i2c_dev
)
327 clk_enable(i2c_dev
->clk
);
329 tegra_periph_reset_assert(i2c_dev
->clk
);
331 tegra_periph_reset_deassert(i2c_dev
->clk
);
334 tegra_dvc_init(i2c_dev
);
336 val
= I2C_CNFG_NEW_MASTER_FSM
| I2C_CNFG_PACKET_MODE_EN
|
337 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT
);
338 i2c_writel(i2c_dev
, val
, I2C_CNFG
);
339 i2c_writel(i2c_dev
, 0, I2C_INT_MASK
);
340 clk_set_rate(i2c_dev
->clk
, i2c_dev
->bus_clk_rate
* 8);
342 if (!i2c_dev
->is_dvc
) {
343 u32 sl_cfg
= i2c_readl(i2c_dev
, I2C_SL_CNFG
);
344 sl_cfg
|= I2C_SL_CNFG_NACK
| I2C_SL_CNFG_NEWSL
;
345 i2c_writel(i2c_dev
, sl_cfg
, I2C_SL_CNFG
);
346 i2c_writel(i2c_dev
, 0xfc, I2C_SL_ADDR1
);
347 i2c_writel(i2c_dev
, 0x00, I2C_SL_ADDR2
);
351 val
= 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT
|
352 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT
;
353 i2c_writel(i2c_dev
, val
, I2C_FIFO_CONTROL
);
355 if (tegra_i2c_flush_fifos(i2c_dev
))
358 clk_disable(i2c_dev
->clk
);
360 if (i2c_dev
->irq_disabled
) {
361 i2c_dev
->irq_disabled
= 0;
362 enable_irq(i2c_dev
->irq
);
368 static irqreturn_t
tegra_i2c_isr(int irq
, void *dev_id
)
371 const u32 status_err
= I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
;
372 struct tegra_i2c_dev
*i2c_dev
= dev_id
;
374 status
= i2c_readl(i2c_dev
, I2C_INT_STATUS
);
377 dev_warn(i2c_dev
->dev
, "irq status 0 %08x %08x %08x\n",
378 i2c_readl(i2c_dev
, I2C_PACKET_TRANSFER_STATUS
),
379 i2c_readl(i2c_dev
, I2C_STATUS
),
380 i2c_readl(i2c_dev
, I2C_CNFG
));
381 i2c_dev
->msg_err
|= I2C_ERR_UNKNOWN_INTERRUPT
;
383 if (!i2c_dev
->irq_disabled
) {
384 disable_irq_nosync(i2c_dev
->irq
);
385 i2c_dev
->irq_disabled
= 1;
388 complete(&i2c_dev
->msg_complete
);
392 if (unlikely(status
& status_err
)) {
393 if (status
& I2C_INT_NO_ACK
)
394 i2c_dev
->msg_err
|= I2C_ERR_NO_ACK
;
395 if (status
& I2C_INT_ARBITRATION_LOST
)
396 i2c_dev
->msg_err
|= I2C_ERR_ARBITRATION_LOST
;
397 complete(&i2c_dev
->msg_complete
);
401 if (i2c_dev
->msg_read
&& (status
& I2C_INT_RX_FIFO_DATA_REQ
)) {
402 if (i2c_dev
->msg_buf_remaining
)
403 tegra_i2c_empty_rx_fifo(i2c_dev
);
408 if (!i2c_dev
->msg_read
&& (status
& I2C_INT_TX_FIFO_DATA_REQ
)) {
409 if (i2c_dev
->msg_buf_remaining
)
410 tegra_i2c_fill_tx_fifo(i2c_dev
);
412 tegra_i2c_mask_irq(i2c_dev
, I2C_INT_TX_FIFO_DATA_REQ
);
415 if ((status
& I2C_INT_PACKET_XFER_COMPLETE
) &&
416 !i2c_dev
->msg_buf_remaining
)
417 complete(&i2c_dev
->msg_complete
);
419 i2c_writel(i2c_dev
, status
, I2C_INT_STATUS
);
421 dvc_writel(i2c_dev
, DVC_STATUS_I2C_DONE_INTR
, DVC_STATUS
);
424 /* An error occurred, mask all interrupts */
425 tegra_i2c_mask_irq(i2c_dev
, I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
|
426 I2C_INT_PACKET_XFER_COMPLETE
| I2C_INT_TX_FIFO_DATA_REQ
|
427 I2C_INT_RX_FIFO_DATA_REQ
);
428 i2c_writel(i2c_dev
, status
, I2C_INT_STATUS
);
430 dvc_writel(i2c_dev
, DVC_STATUS_I2C_DONE_INTR
, DVC_STATUS
);
434 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev
*i2c_dev
,
435 struct i2c_msg
*msg
, int stop
)
441 tegra_i2c_flush_fifos(i2c_dev
);
442 i2c_writel(i2c_dev
, 0xFF, I2C_INT_STATUS
);
447 i2c_dev
->msg_buf
= msg
->buf
;
448 i2c_dev
->msg_buf_remaining
= msg
->len
;
449 i2c_dev
->msg_err
= I2C_ERR_NONE
;
450 i2c_dev
->msg_read
= (msg
->flags
& I2C_M_RD
);
451 INIT_COMPLETION(i2c_dev
->msg_complete
);
453 packet_header
= (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT
) |
454 PACKET_HEADER0_PROTOCOL_I2C
|
455 (i2c_dev
->cont_id
<< PACKET_HEADER0_CONT_ID_SHIFT
) |
456 (1 << PACKET_HEADER0_PACKET_ID_SHIFT
);
457 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
459 packet_header
= msg
->len
- 1;
460 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
462 packet_header
= msg
->addr
<< I2C_HEADER_SLAVE_ADDR_SHIFT
;
463 packet_header
|= I2C_HEADER_IE_ENABLE
;
465 packet_header
|= I2C_HEADER_REPEAT_START
;
466 if (msg
->flags
& I2C_M_TEN
)
467 packet_header
|= I2C_HEADER_10BIT_ADDR
;
468 if (msg
->flags
& I2C_M_IGNORE_NAK
)
469 packet_header
|= I2C_HEADER_CONT_ON_NAK
;
470 if (msg
->flags
& I2C_M_RD
)
471 packet_header
|= I2C_HEADER_READ
;
472 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
474 if (!(msg
->flags
& I2C_M_RD
))
475 tegra_i2c_fill_tx_fifo(i2c_dev
);
477 int_mask
= I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
;
478 if (msg
->flags
& I2C_M_RD
)
479 int_mask
|= I2C_INT_RX_FIFO_DATA_REQ
;
480 else if (i2c_dev
->msg_buf_remaining
)
481 int_mask
|= I2C_INT_TX_FIFO_DATA_REQ
;
482 tegra_i2c_unmask_irq(i2c_dev
, int_mask
);
483 dev_dbg(i2c_dev
->dev
, "unmasked irq: %02x\n",
484 i2c_readl(i2c_dev
, I2C_INT_MASK
));
486 ret
= wait_for_completion_timeout(&i2c_dev
->msg_complete
, TEGRA_I2C_TIMEOUT
);
487 tegra_i2c_mask_irq(i2c_dev
, int_mask
);
489 if (WARN_ON(ret
== 0)) {
490 dev_err(i2c_dev
->dev
, "i2c transfer timed out\n");
492 tegra_i2c_init(i2c_dev
);
496 dev_dbg(i2c_dev
->dev
, "transfer complete: %d %d %d\n",
497 ret
, completion_done(&i2c_dev
->msg_complete
), i2c_dev
->msg_err
);
499 if (likely(i2c_dev
->msg_err
== I2C_ERR_NONE
))
502 tegra_i2c_init(i2c_dev
);
503 if (i2c_dev
->msg_err
== I2C_ERR_NO_ACK
) {
504 if (msg
->flags
& I2C_M_IGNORE_NAK
)
512 static int tegra_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[],
515 struct tegra_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
519 if (i2c_dev
->is_suspended
)
522 clk_enable(i2c_dev
->clk
);
523 for (i
= 0; i
< num
; i
++) {
524 int stop
= (i
== (num
- 1)) ? 1 : 0;
525 ret
= tegra_i2c_xfer_msg(i2c_dev
, &msgs
[i
], stop
);
529 clk_disable(i2c_dev
->clk
);
533 static u32
tegra_i2c_func(struct i2c_adapter
*adap
)
538 static const struct i2c_algorithm tegra_i2c_algo
= {
539 .master_xfer
= tegra_i2c_xfer
,
540 .functionality
= tegra_i2c_func
,
543 static int tegra_i2c_probe(struct platform_device
*pdev
)
545 struct tegra_i2c_dev
*i2c_dev
;
546 struct tegra_i2c_platform_data
*pdata
= pdev
->dev
.platform_data
;
547 struct resource
*res
;
548 struct resource
*iomem
;
551 const unsigned int *prop
;
556 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
558 dev_err(&pdev
->dev
, "no mem resource\n");
561 iomem
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
563 dev_err(&pdev
->dev
, "I2C region already claimed\n");
567 base
= ioremap(iomem
->start
, resource_size(iomem
));
569 dev_err(&pdev
->dev
, "Cannot ioremap I2C region\n");
573 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
575 dev_err(&pdev
->dev
, "no irq resource\n");
581 clk
= clk_get(&pdev
->dev
, NULL
);
583 dev_err(&pdev
->dev
, "missing controller clock");
585 goto err_release_region
;
588 i2c_clk
= clk_get(&pdev
->dev
, "i2c");
589 if (IS_ERR(i2c_clk
)) {
590 dev_err(&pdev
->dev
, "missing bus clock");
591 ret
= PTR_ERR(i2c_clk
);
595 i2c_dev
= kzalloc(sizeof(struct tegra_i2c_dev
), GFP_KERNEL
);
598 goto err_i2c_clk_put
;
601 i2c_dev
->base
= base
;
603 i2c_dev
->i2c_clk
= i2c_clk
;
604 i2c_dev
->iomem
= iomem
;
605 i2c_dev
->adapter
.algo
= &tegra_i2c_algo
;
607 i2c_dev
->cont_id
= pdev
->id
;
608 i2c_dev
->dev
= &pdev
->dev
;
610 i2c_dev
->bus_clk_rate
= 100000; /* default clock rate */
612 i2c_dev
->bus_clk_rate
= pdata
->bus_clk_rate
;
614 } else if (i2c_dev
->dev
->of_node
) { /* if there is a device tree node ... */
615 prop
= of_get_property(i2c_dev
->dev
->of_node
,
616 "clock-frequency", NULL
);
618 i2c_dev
->bus_clk_rate
= be32_to_cpup(prop
);
623 init_completion(&i2c_dev
->msg_complete
);
625 platform_set_drvdata(pdev
, i2c_dev
);
627 ret
= tegra_i2c_init(i2c_dev
);
629 dev_err(&pdev
->dev
, "Failed to initialize i2c controller");
633 ret
= request_irq(i2c_dev
->irq
, tegra_i2c_isr
, 0, pdev
->name
, i2c_dev
);
635 dev_err(&pdev
->dev
, "Failed to request irq %i\n", i2c_dev
->irq
);
639 clk_enable(i2c_dev
->i2c_clk
);
641 i2c_set_adapdata(&i2c_dev
->adapter
, i2c_dev
);
642 i2c_dev
->adapter
.owner
= THIS_MODULE
;
643 i2c_dev
->adapter
.class = I2C_CLASS_HWMON
;
644 strlcpy(i2c_dev
->adapter
.name
, "Tegra I2C adapter",
645 sizeof(i2c_dev
->adapter
.name
));
646 i2c_dev
->adapter
.algo
= &tegra_i2c_algo
;
647 i2c_dev
->adapter
.dev
.parent
= &pdev
->dev
;
648 i2c_dev
->adapter
.nr
= pdev
->id
;
649 i2c_dev
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
651 ret
= i2c_add_numbered_adapter(&i2c_dev
->adapter
);
653 dev_err(&pdev
->dev
, "Failed to add I2C adapter\n");
657 of_i2c_register_devices(&i2c_dev
->adapter
);
661 free_irq(i2c_dev
->irq
, i2c_dev
);
669 release_mem_region(iomem
->start
, resource_size(iomem
));
675 static int tegra_i2c_remove(struct platform_device
*pdev
)
677 struct tegra_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
678 i2c_del_adapter(&i2c_dev
->adapter
);
679 free_irq(i2c_dev
->irq
, i2c_dev
);
680 clk_put(i2c_dev
->i2c_clk
);
681 clk_put(i2c_dev
->clk
);
682 release_mem_region(i2c_dev
->iomem
->start
,
683 resource_size(i2c_dev
->iomem
));
684 iounmap(i2c_dev
->base
);
690 static int tegra_i2c_suspend(struct platform_device
*pdev
, pm_message_t state
)
692 struct tegra_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
694 i2c_lock_adapter(&i2c_dev
->adapter
);
695 i2c_dev
->is_suspended
= true;
696 i2c_unlock_adapter(&i2c_dev
->adapter
);
701 static int tegra_i2c_resume(struct platform_device
*pdev
)
703 struct tegra_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
706 i2c_lock_adapter(&i2c_dev
->adapter
);
708 ret
= tegra_i2c_init(i2c_dev
);
711 i2c_unlock_adapter(&i2c_dev
->adapter
);
715 i2c_dev
->is_suspended
= false;
717 i2c_unlock_adapter(&i2c_dev
->adapter
);
723 static struct platform_driver tegra_i2c_driver
= {
724 .probe
= tegra_i2c_probe
,
725 .remove
= tegra_i2c_remove
,
727 .suspend
= tegra_i2c_suspend
,
728 .resume
= tegra_i2c_resume
,
732 .owner
= THIS_MODULE
,
736 static int __init
tegra_i2c_init_driver(void)
738 return platform_driver_register(&tegra_i2c_driver
);
741 static void __exit
tegra_i2c_exit_driver(void)
743 platform_driver_unregister(&tegra_i2c_driver
);
746 subsys_initcall(tegra_i2c_init_driver
);
747 module_exit(tegra_i2c_exit_driver
);
749 MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
750 MODULE_AUTHOR("Colin Cross");
751 MODULE_LICENSE("GPL v2");