blob: 2dadb964c4649850c6cc619c5b93f6293bc1541b [file] [log] [blame]
Colin Crossdb811ca2011-02-20 17:14:21 -08001/*
2 * drivers/i2c/busses/i2c-tegra.c
3 *
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
6 *
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.
10 *
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.
15 *
16 */
17
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>
24#include <linux/io.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/i2c-tegra.h>
John Bonesio5c470f32011-06-22 09:16:56 -070029#include <linux/of_i2c.h>
Laxman Dewangan6ad068e2012-08-19 00:47:46 +053030#include <linux/of_device.h>
Paul Gortmaker93cf5d72011-07-29 21:14:30 -070031#include <linux/module.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080032
33#include <asm/unaligned.h>
34
35#include <mach/clk.h>
36
37#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
38#define BYTES_PER_FIFO_WORD 4
39
40#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060041#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Colin Crossdb811ca2011-02-20 17:14:21 -080042#define I2C_CNFG_PACKET_MODE_EN (1<<10)
43#define I2C_CNFG_NEW_MASTER_FSM (1<<11)
Todd Poynorcb63c622011-04-25 15:32:25 -060044#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080045#define I2C_SL_CNFG 0x020
Stephen Warren5afa9d32011-06-06 11:25:19 -060046#define I2C_SL_CNFG_NACK (1<<1)
Colin Crossdb811ca2011-02-20 17:14:21 -080047#define I2C_SL_CNFG_NEWSL (1<<2)
48#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060049#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080050#define I2C_TX_FIFO 0x050
51#define I2C_RX_FIFO 0x054
52#define I2C_PACKET_TRANSFER_STATUS 0x058
53#define I2C_FIFO_CONTROL 0x05c
54#define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
55#define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
56#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
57#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
58#define I2C_FIFO_STATUS 0x060
59#define I2C_FIFO_STATUS_TX_MASK 0xF0
60#define I2C_FIFO_STATUS_TX_SHIFT 4
61#define I2C_FIFO_STATUS_RX_MASK 0x0F
62#define I2C_FIFO_STATUS_RX_SHIFT 0
63#define I2C_INT_MASK 0x064
64#define I2C_INT_STATUS 0x068
65#define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
66#define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
67#define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
68#define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
69#define I2C_INT_NO_ACK (1<<3)
70#define I2C_INT_ARBITRATION_LOST (1<<2)
71#define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
72#define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
73#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053074#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
75#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080076
77#define DVC_CTRL_REG1 0x000
78#define DVC_CTRL_REG1_INTR_EN (1<<10)
79#define DVC_CTRL_REG2 0x004
80#define DVC_CTRL_REG3 0x008
81#define DVC_CTRL_REG3_SW_PROG (1<<26)
82#define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
83#define DVC_STATUS 0x00c
84#define DVC_STATUS_I2C_DONE_INTR (1<<30)
85
86#define I2C_ERR_NONE 0x00
87#define I2C_ERR_NO_ACK 0x01
88#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060089#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080090
91#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
92#define PACKET_HEADER0_PACKET_ID_SHIFT 16
93#define PACKET_HEADER0_CONT_ID_SHIFT 12
94#define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
95
96#define I2C_HEADER_HIGHSPEED_MODE (1<<22)
97#define I2C_HEADER_CONT_ON_NAK (1<<21)
98#define I2C_HEADER_SEND_START_BYTE (1<<20)
99#define I2C_HEADER_READ (1<<19)
100#define I2C_HEADER_10BIT_ADDR (1<<18)
101#define I2C_HEADER_IE_ENABLE (1<<17)
102#define I2C_HEADER_REPEAT_START (1<<16)
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530103#define I2C_HEADER_CONTINUE_XFER (1<<15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800104#define I2C_HEADER_MASTER_ADDR_SHIFT 12
105#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530106/*
107 * msg_end_type: The bus control which need to be send at end of transfer.
108 * @MSG_END_STOP: Send stop pulse at end of transfer.
109 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
110 * @MSG_END_CONTINUE: The following on message is coming and so do not send
111 * stop or repeat start.
112 */
113enum msg_end_type {
114 MSG_END_STOP,
115 MSG_END_REPEAT_START,
116 MSG_END_CONTINUE,
117};
Colin Crossdb811ca2011-02-20 17:14:21 -0800118
119/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530120 * struct tegra_i2c_hw_feature : Different HW support on Tegra
121 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530122 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
123 * complete interrupt per packet basis.
124 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
125 * and earlier Socs has two clock sources i.e. div-clk and
126 * fast-clk.
127 * @clk_divisor_hs_mode: Clock divisor in HS mode.
128 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
129 * applicable if there is no fast clock source i.e. single clock
130 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530131 */
132
133struct tegra_i2c_hw_feature {
134 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530135 bool has_per_pkt_xfer_complete_irq;
136 bool has_single_clk_source;
137 int clk_divisor_hs_mode;
138 int clk_divisor_std_fast_mode;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530139};
140
141/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800142 * struct tegra_i2c_dev - per device i2c context
143 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530144 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800145 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530146 * @div_clk: clock reference for div clock of i2c controller.
147 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800148 * @base: ioremapped registers cookie
149 * @cont_id: i2c controller id, used for for packet header
150 * @irq: irq number of transfer complete interrupt
151 * @is_dvc: identifies the DVC i2c controller, has a different register layout
152 * @msg_complete: transfer completion notifier
153 * @msg_err: error code for completed message
154 * @msg_buf: pointer to current message data
155 * @msg_buf_remaining: size of unsent data in the message buffer
156 * @msg_read: identifies read transfers
157 * @bus_clk_rate: current i2c bus clock rate
158 * @is_suspended: prevents i2c controller accesses after suspend is called
159 */
160struct tegra_i2c_dev {
161 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530162 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800163 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530164 struct clk *div_clk;
165 struct clk *fast_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800166 void __iomem *base;
167 int cont_id;
168 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600169 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800170 int is_dvc;
171 struct completion msg_complete;
172 int msg_err;
173 u8 *msg_buf;
174 size_t msg_buf_remaining;
175 int msg_read;
176 unsigned long bus_clk_rate;
177 bool is_suspended;
178};
179
180static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
181{
182 writel(val, i2c_dev->base + reg);
183}
184
185static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
186{
187 return readl(i2c_dev->base + reg);
188}
189
190/*
191 * i2c_writel and i2c_readl will offset the register if necessary to talk
192 * to the I2C block inside the DVC block
193 */
194static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
195 unsigned long reg)
196{
197 if (i2c_dev->is_dvc)
198 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
199 return reg;
200}
201
202static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
203 unsigned long reg)
204{
205 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530206
207 /* Read back register to make sure that register writes completed */
208 if (reg != I2C_TX_FIFO)
209 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800210}
211
212static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
213{
214 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
215}
216
217static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
218 unsigned long reg, int len)
219{
220 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
221}
222
223static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
224 unsigned long reg, int len)
225{
226 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
227}
228
229static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
230{
231 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
232 int_mask &= ~mask;
233 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
234}
235
236static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
237{
238 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
239 int_mask |= mask;
240 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
241}
242
243static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
244{
245 unsigned long timeout = jiffies + HZ;
246 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
247 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
248 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
249
250 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
251 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
252 if (time_after(jiffies, timeout)) {
253 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
254 return -ETIMEDOUT;
255 }
256 msleep(1);
257 }
258 return 0;
259}
260
261static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
262{
263 u32 val;
264 int rx_fifo_avail;
265 u8 *buf = i2c_dev->msg_buf;
266 size_t buf_remaining = i2c_dev->msg_buf_remaining;
267 int words_to_transfer;
268
269 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
270 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
271 I2C_FIFO_STATUS_RX_SHIFT;
272
273 /* Rounds down to not include partial word at the end of buf */
274 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
275 if (words_to_transfer > rx_fifo_avail)
276 words_to_transfer = rx_fifo_avail;
277
278 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
279
280 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
281 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
282 rx_fifo_avail -= words_to_transfer;
283
284 /*
285 * If there is a partial word at the end of buf, handle it manually to
286 * prevent overwriting past the end of buf
287 */
288 if (rx_fifo_avail > 0 && buf_remaining > 0) {
289 BUG_ON(buf_remaining > 3);
290 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
291 memcpy(buf, &val, buf_remaining);
292 buf_remaining = 0;
293 rx_fifo_avail--;
294 }
295
296 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
297 i2c_dev->msg_buf_remaining = buf_remaining;
298 i2c_dev->msg_buf = buf;
299 return 0;
300}
301
302static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
303{
304 u32 val;
305 int tx_fifo_avail;
306 u8 *buf = i2c_dev->msg_buf;
307 size_t buf_remaining = i2c_dev->msg_buf_remaining;
308 int words_to_transfer;
309
310 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
311 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
312 I2C_FIFO_STATUS_TX_SHIFT;
313
314 /* Rounds down to not include partial word at the end of buf */
315 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800316
Doug Anderson96219c32011-08-30 11:46:10 -0600317 /* It's very common to have < 4 bytes, so optimize that case. */
318 if (words_to_transfer) {
319 if (words_to_transfer > tx_fifo_avail)
320 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800321
Doug Anderson96219c32011-08-30 11:46:10 -0600322 /*
323 * Update state before writing to FIFO. If this casues us
324 * to finish writing all bytes (AKA buf_remaining goes to 0) we
325 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
326 * not maskable). We need to make sure that the isr sees
327 * buf_remaining as 0 and doesn't call us back re-entrantly.
328 */
329 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
330 tx_fifo_avail -= words_to_transfer;
331 i2c_dev->msg_buf_remaining = buf_remaining;
332 i2c_dev->msg_buf = buf +
333 words_to_transfer * BYTES_PER_FIFO_WORD;
334 barrier();
335
336 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
337
338 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
339 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800340
341 /*
342 * If there is a partial word at the end of buf, handle it manually to
343 * prevent reading past the end of buf, which could cross a page
344 * boundary and fault.
345 */
346 if (tx_fifo_avail > 0 && buf_remaining > 0) {
347 BUG_ON(buf_remaining > 3);
348 memcpy(&val, buf, buf_remaining);
Doug Anderson96219c32011-08-30 11:46:10 -0600349
350 /* Again update before writing to FIFO to make sure isr sees. */
351 i2c_dev->msg_buf_remaining = 0;
352 i2c_dev->msg_buf = NULL;
353 barrier();
354
Colin Crossdb811ca2011-02-20 17:14:21 -0800355 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800356 }
357
Colin Crossdb811ca2011-02-20 17:14:21 -0800358 return 0;
359}
360
361/*
362 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
363 * block. This block is identical to the rest of the I2C blocks, except that
364 * it only supports master mode, it has registers moved around, and it needs
365 * some extra init to get it into I2C mode. The register moves are handled
366 * by i2c_readl and i2c_writel
367 */
368static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
369{
370 u32 val = 0;
371 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
372 val |= DVC_CTRL_REG3_SW_PROG;
373 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
374 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
375
376 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
377 val |= DVC_CTRL_REG1_INTR_EN;
378 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
379}
380
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530381static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
382{
383 int ret;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530384 if (!i2c_dev->hw->has_single_clk_source) {
385 ret = clk_prepare_enable(i2c_dev->fast_clk);
386 if (ret < 0) {
387 dev_err(i2c_dev->dev,
388 "Enabling fast clk failed, err %d\n", ret);
389 return ret;
390 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530391 }
392 ret = clk_prepare_enable(i2c_dev->div_clk);
393 if (ret < 0) {
394 dev_err(i2c_dev->dev,
395 "Enabling div clk failed, err %d\n", ret);
396 clk_disable_unprepare(i2c_dev->fast_clk);
397 }
398 return ret;
399}
400
401static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
402{
403 clk_disable_unprepare(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530404 if (!i2c_dev->hw->has_single_clk_source)
405 clk_disable_unprepare(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530406}
407
Colin Crossdb811ca2011-02-20 17:14:21 -0800408static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
409{
410 u32 val;
411 int err = 0;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530412 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
413 u32 clk_divisor;
Colin Crossdb811ca2011-02-20 17:14:21 -0800414
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530415 tegra_i2c_clock_enable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800416
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530417 tegra_periph_reset_assert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800418 udelay(2);
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530419 tegra_periph_reset_deassert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800420
421 if (i2c_dev->is_dvc)
422 tegra_dvc_init(i2c_dev);
423
Jay Cheng40abcf72011-04-25 15:32:27 -0600424 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
425 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800426 i2c_writel(i2c_dev, val, I2C_CNFG);
427 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530428
429 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
430 clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate * clk_multiplier);
431
432 /* Make sure clock divisor programmed correctly */
433 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
434 clk_divisor |= i2c_dev->hw->clk_divisor_std_fast_mode <<
435 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
436 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800437
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600438 if (!i2c_dev->is_dvc) {
439 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600440 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
441 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
442 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
443 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
444
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600445 }
446
Colin Crossdb811ca2011-02-20 17:14:21 -0800447 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
448 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
449 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
450
451 if (tegra_i2c_flush_fifos(i2c_dev))
452 err = -ETIMEDOUT;
453
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530454 tegra_i2c_clock_disable(i2c_dev);
Todd Poynorcb63c622011-04-25 15:32:25 -0600455
456 if (i2c_dev->irq_disabled) {
457 i2c_dev->irq_disabled = 0;
458 enable_irq(i2c_dev->irq);
459 }
460
Colin Crossdb811ca2011-02-20 17:14:21 -0800461 return err;
462}
463
464static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
465{
466 u32 status;
467 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
468 struct tegra_i2c_dev *i2c_dev = dev_id;
469
470 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
471
472 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600473 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
474 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
475 i2c_readl(i2c_dev, I2C_STATUS),
476 i2c_readl(i2c_dev, I2C_CNFG));
477 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
478
479 if (!i2c_dev->irq_disabled) {
480 disable_irq_nosync(i2c_dev->irq);
481 i2c_dev->irq_disabled = 1;
482 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600483 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800484 }
485
486 if (unlikely(status & status_err)) {
487 if (status & I2C_INT_NO_ACK)
488 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
489 if (status & I2C_INT_ARBITRATION_LOST)
490 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800491 goto err;
492 }
493
494 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
495 if (i2c_dev->msg_buf_remaining)
496 tegra_i2c_empty_rx_fifo(i2c_dev);
497 else
498 BUG();
499 }
500
501 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
502 if (i2c_dev->msg_buf_remaining)
503 tegra_i2c_fill_tx_fifo(i2c_dev);
504 else
505 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
506 }
507
Laxman Dewanganc889e912012-05-07 12:16:19 +0530508 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
509 if (i2c_dev->is_dvc)
510 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
511
Doug Anderson96219c32011-08-30 11:46:10 -0600512 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
513 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800514 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600515 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800516 return IRQ_HANDLED;
517err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300518 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800519 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
520 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
521 I2C_INT_RX_FIFO_DATA_REQ);
522 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600523 if (i2c_dev->is_dvc)
524 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530525
526 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800527 return IRQ_HANDLED;
528}
529
530static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530531 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800532{
533 u32 packet_header;
534 u32 int_mask;
535 int ret;
536
537 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800538
539 if (msg->len == 0)
540 return -EINVAL;
541
542 i2c_dev->msg_buf = msg->buf;
543 i2c_dev->msg_buf_remaining = msg->len;
544 i2c_dev->msg_err = I2C_ERR_NONE;
545 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
546 INIT_COMPLETION(i2c_dev->msg_complete);
547
548 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
549 PACKET_HEADER0_PROTOCOL_I2C |
550 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
551 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
552 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
553
554 packet_header = msg->len - 1;
555 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
556
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530557 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530558 if (end_state == MSG_END_CONTINUE)
559 packet_header |= I2C_HEADER_CONTINUE_XFER;
560 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600561 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530562 if (msg->flags & I2C_M_TEN) {
563 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800564 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530565 } else {
566 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
567 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800568 if (msg->flags & I2C_M_IGNORE_NAK)
569 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800570 if (msg->flags & I2C_M_RD)
571 packet_header |= I2C_HEADER_READ;
572 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
573
574 if (!(msg->flags & I2C_M_RD))
575 tegra_i2c_fill_tx_fifo(i2c_dev);
576
577 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530578 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
579 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800580 if (msg->flags & I2C_M_RD)
581 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
582 else if (i2c_dev->msg_buf_remaining)
583 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
584 tegra_i2c_unmask_irq(i2c_dev, int_mask);
585 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
586 i2c_readl(i2c_dev, I2C_INT_MASK));
587
588 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
589 tegra_i2c_mask_irq(i2c_dev, int_mask);
590
591 if (WARN_ON(ret == 0)) {
592 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
593
594 tegra_i2c_init(i2c_dev);
595 return -ETIMEDOUT;
596 }
597
598 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
599 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
600
601 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
602 return 0;
603
Alok Chauhanf70893d02012-04-02 11:23:02 +0530604 /*
605 * NACK interrupt is generated before the I2C controller generates the
606 * STOP condition on the bus. So wait for 2 clock periods before resetting
607 * the controller so that STOP condition has been delivered properly.
608 */
609 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
610 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
611
Colin Crossdb811ca2011-02-20 17:14:21 -0800612 tegra_i2c_init(i2c_dev);
613 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
614 if (msg->flags & I2C_M_IGNORE_NAK)
615 return 0;
616 return -EREMOTEIO;
617 }
618
619 return -EIO;
620}
621
622static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
623 int num)
624{
625 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
626 int i;
627 int ret = 0;
628
629 if (i2c_dev->is_suspended)
630 return -EBUSY;
631
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530632 tegra_i2c_clock_enable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800633 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530634 enum msg_end_type end_type = MSG_END_STOP;
635 if (i < (num - 1)) {
636 if (msgs[i + 1].flags & I2C_M_NOSTART)
637 end_type = MSG_END_CONTINUE;
638 else
639 end_type = MSG_END_REPEAT_START;
640 }
641 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800642 if (ret)
643 break;
644 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530645 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800646 return ret ?: i;
647}
648
649static u32 tegra_i2c_func(struct i2c_adapter *adap)
650{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530651 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
652 u32 ret = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
653 I2C_FUNC_PROTOCOL_MANGLING;
654
655 if (i2c_dev->hw->has_continue_xfer_support)
656 ret |= I2C_FUNC_NOSTART;
657 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800658}
659
660static const struct i2c_algorithm tegra_i2c_algo = {
661 .master_xfer = tegra_i2c_xfer,
662 .functionality = tegra_i2c_func,
663};
664
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530665static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
666 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530667 .has_per_pkt_xfer_complete_irq = false,
668 .has_single_clk_source = false,
669 .clk_divisor_hs_mode = 3,
670 .clk_divisor_std_fast_mode = 0,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530671};
672
673static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
674 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530675 .has_per_pkt_xfer_complete_irq = false,
676 .has_single_clk_source = false,
677 .clk_divisor_hs_mode = 3,
678 .clk_divisor_std_fast_mode = 0,
679};
680
681static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
682 .has_continue_xfer_support = true,
683 .has_per_pkt_xfer_complete_irq = true,
684 .has_single_clk_source = true,
685 .clk_divisor_hs_mode = 1,
686 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530687};
688
689#if defined(CONFIG_OF)
690/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500691static const struct of_device_id tegra_i2c_of_match[] = {
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530692 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530693 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
694 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
695 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
696 {},
697};
698MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
699#endif
700
Bill Pemberton0b255e92012-11-27 15:59:38 -0500701static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800702{
703 struct tegra_i2c_dev *i2c_dev;
704 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
705 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530706 struct clk *div_clk;
707 struct clk *fast_clk;
John Bonesio5c470f32011-06-22 09:16:56 -0700708 const unsigned int *prop;
Olof Johanssonf533c612011-10-12 17:33:00 -0700709 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800710 int irq;
711 int ret = 0;
712
713 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
714 if (!res) {
715 dev_err(&pdev->dev, "no mem resource\n");
716 return -EINVAL;
717 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800718
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530719 base = devm_request_and_ioremap(&pdev->dev, res);
Colin Crossdb811ca2011-02-20 17:14:21 -0800720 if (!base) {
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530721 dev_err(&pdev->dev, "Cannot request/ioremap I2C registers\n");
722 return -EADDRNOTAVAIL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800723 }
724
725 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
726 if (!res) {
727 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530728 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800729 }
730 irq = res->start;
731
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530732 div_clk = devm_clk_get(&pdev->dev, "div-clk");
733 if (IS_ERR(div_clk)) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800734 dev_err(&pdev->dev, "missing controller clock");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530735 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800736 }
737
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530738 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Colin Crossdb811ca2011-02-20 17:14:21 -0800739 if (!i2c_dev) {
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530740 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
741 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800742 }
743
744 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530745 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800746 i2c_dev->adapter.algo = &tegra_i2c_algo;
747 i2c_dev->irq = irq;
748 i2c_dev->cont_id = pdev->id;
749 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700750
751 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
752 if (pdata) {
753 i2c_dev->bus_clk_rate = pdata->bus_clk_rate;
754
755 } else if (i2c_dev->dev->of_node) { /* if there is a device tree node ... */
756 prop = of_get_property(i2c_dev->dev->of_node,
757 "clock-frequency", NULL);
758 if (prop)
759 i2c_dev->bus_clk_rate = be32_to_cpup(prop);
760 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800761
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530762 i2c_dev->hw = &tegra20_i2c_hw;
763
764 if (pdev->dev.of_node) {
765 const struct of_device_id *match;
766 match = of_match_device(of_match_ptr(tegra_i2c_of_match),
767 &pdev->dev);
768 i2c_dev->hw = match->data;
Stephen Warren68fb6692011-12-17 23:29:30 -0700769 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
770 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530771 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800772 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530773 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800774 init_completion(&i2c_dev->msg_complete);
775
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530776 if (!i2c_dev->hw->has_single_clk_source) {
777 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
778 if (IS_ERR(fast_clk)) {
779 dev_err(&pdev->dev, "missing fast clock");
780 return PTR_ERR(fast_clk);
781 }
782 i2c_dev->fast_clk = fast_clk;
783 }
784
Colin Crossdb811ca2011-02-20 17:14:21 -0800785 platform_set_drvdata(pdev, i2c_dev);
786
787 ret = tegra_i2c_init(i2c_dev);
788 if (ret) {
789 dev_err(&pdev->dev, "Failed to initialize i2c controller");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530790 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800791 }
792
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530793 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530794 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800795 if (ret) {
796 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530797 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800798 }
799
Colin Crossdb811ca2011-02-20 17:14:21 -0800800 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
801 i2c_dev->adapter.owner = THIS_MODULE;
802 i2c_dev->adapter.class = I2C_CLASS_HWMON;
803 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
804 sizeof(i2c_dev->adapter.name));
805 i2c_dev->adapter.algo = &tegra_i2c_algo;
806 i2c_dev->adapter.dev.parent = &pdev->dev;
807 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700808 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800809
810 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
811 if (ret) {
812 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530813 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800814 }
815
John Bonesio5c470f32011-06-22 09:16:56 -0700816 of_i2c_register_devices(&i2c_dev->adapter);
817
Colin Crossdb811ca2011-02-20 17:14:21 -0800818 return 0;
Colin Crossdb811ca2011-02-20 17:14:21 -0800819}
820
Bill Pemberton0b255e92012-11-27 15:59:38 -0500821static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800822{
823 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
824 i2c_del_adapter(&i2c_dev->adapter);
Colin Crossdb811ca2011-02-20 17:14:21 -0800825 return 0;
826}
827
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530828#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200829static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800830{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200831 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800832
833 i2c_lock_adapter(&i2c_dev->adapter);
834 i2c_dev->is_suspended = true;
835 i2c_unlock_adapter(&i2c_dev->adapter);
836
837 return 0;
838}
839
Wolfram Sang5db20c42012-07-24 17:32:45 +0200840static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800841{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200842 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800843 int ret;
844
845 i2c_lock_adapter(&i2c_dev->adapter);
846
847 ret = tegra_i2c_init(i2c_dev);
848
849 if (ret) {
850 i2c_unlock_adapter(&i2c_dev->adapter);
851 return ret;
852 }
853
854 i2c_dev->is_suspended = false;
855
856 i2c_unlock_adapter(&i2c_dev->adapter);
857
858 return 0;
859}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200860
Wolfram Sang5db20c42012-07-24 17:32:45 +0200861static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200862#define TEGRA_I2C_PM (&tegra_i2c_pm)
863#else
864#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -0800865#endif
866
867static struct platform_driver tegra_i2c_driver = {
868 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500869 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -0800870 .driver = {
871 .name = "tegra-i2c",
872 .owner = THIS_MODULE,
Laxman Dewangan02d8bf82012-07-10 16:50:42 +0530873 .of_match_table = of_match_ptr(tegra_i2c_of_match),
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200874 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -0800875 },
876};
877
878static int __init tegra_i2c_init_driver(void)
879{
880 return platform_driver_register(&tegra_i2c_driver);
881}
882
883static void __exit tegra_i2c_exit_driver(void)
884{
885 platform_driver_unregister(&tegra_i2c_driver);
886}
887
888subsys_initcall(tegra_i2c_init_driver);
889module_exit(tegra_i2c_exit_driver);
890
891MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
892MODULE_AUTHOR("Colin Cross");
893MODULE_LICENSE("GPL v2");