blob: e661edee4d0cf0d92b7ab5e6899d1993bb264799 [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>
Laxman Dewangan6ad068e2012-08-19 00:47:46 +053028#include <linux/of_device.h>
Paul Gortmaker93cf5d72011-07-29 21:14:30 -070029#include <linux/module.h>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053030#include <linux/clk/tegra.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080031
32#include <asm/unaligned.h>
33
Colin Crossdb811ca2011-02-20 17:14:21 -080034#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
35#define BYTES_PER_FIFO_WORD 4
36
37#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060038#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Colin Crossdb811ca2011-02-20 17:14:21 -080039#define I2C_CNFG_PACKET_MODE_EN (1<<10)
40#define I2C_CNFG_NEW_MASTER_FSM (1<<11)
Todd Poynorcb63c622011-04-25 15:32:25 -060041#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080042#define I2C_SL_CNFG 0x020
Stephen Warren5afa9d32011-06-06 11:25:19 -060043#define I2C_SL_CNFG_NACK (1<<1)
Colin Crossdb811ca2011-02-20 17:14:21 -080044#define I2C_SL_CNFG_NEWSL (1<<2)
45#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060046#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080047#define I2C_TX_FIFO 0x050
48#define I2C_RX_FIFO 0x054
49#define I2C_PACKET_TRANSFER_STATUS 0x058
50#define I2C_FIFO_CONTROL 0x05c
51#define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
52#define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
53#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
54#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
55#define I2C_FIFO_STATUS 0x060
56#define I2C_FIFO_STATUS_TX_MASK 0xF0
57#define I2C_FIFO_STATUS_TX_SHIFT 4
58#define I2C_FIFO_STATUS_RX_MASK 0x0F
59#define I2C_FIFO_STATUS_RX_SHIFT 0
60#define I2C_INT_MASK 0x064
61#define I2C_INT_STATUS 0x068
62#define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
63#define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
64#define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
65#define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
66#define I2C_INT_NO_ACK (1<<3)
67#define I2C_INT_ARBITRATION_LOST (1<<2)
68#define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
69#define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
70#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053071#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
72#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080073
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)
82
83#define I2C_ERR_NONE 0x00
84#define I2C_ERR_NO_ACK 0x01
85#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060086#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080087
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)
92
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)
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530100#define I2C_HEADER_CONTINUE_XFER (1<<15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800101#define I2C_HEADER_MASTER_ADDR_SHIFT 12
102#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530103/*
104 * msg_end_type: The bus control which need to be send at end of transfer.
105 * @MSG_END_STOP: Send stop pulse at end of transfer.
106 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
107 * @MSG_END_CONTINUE: The following on message is coming and so do not send
108 * stop or repeat start.
109 */
110enum msg_end_type {
111 MSG_END_STOP,
112 MSG_END_REPEAT_START,
113 MSG_END_CONTINUE,
114};
Colin Crossdb811ca2011-02-20 17:14:21 -0800115
116/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530117 * struct tegra_i2c_hw_feature : Different HW support on Tegra
118 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530119 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
120 * complete interrupt per packet basis.
121 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
122 * and earlier Socs has two clock sources i.e. div-clk and
123 * fast-clk.
124 * @clk_divisor_hs_mode: Clock divisor in HS mode.
125 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
126 * applicable if there is no fast clock source i.e. single clock
127 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530128 */
129
130struct tegra_i2c_hw_feature {
131 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530132 bool has_per_pkt_xfer_complete_irq;
133 bool has_single_clk_source;
134 int clk_divisor_hs_mode;
135 int clk_divisor_std_fast_mode;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530136};
137
138/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800139 * struct tegra_i2c_dev - per device i2c context
140 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530141 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800142 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530143 * @div_clk: clock reference for div clock of i2c controller.
144 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800145 * @base: ioremapped registers cookie
146 * @cont_id: i2c controller id, used for for packet header
147 * @irq: irq number of transfer complete interrupt
148 * @is_dvc: identifies the DVC i2c controller, has a different register layout
149 * @msg_complete: transfer completion notifier
150 * @msg_err: error code for completed message
151 * @msg_buf: pointer to current message data
152 * @msg_buf_remaining: size of unsent data in the message buffer
153 * @msg_read: identifies read transfers
154 * @bus_clk_rate: current i2c bus clock rate
155 * @is_suspended: prevents i2c controller accesses after suspend is called
156 */
157struct tegra_i2c_dev {
158 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530159 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800160 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530161 struct clk *div_clk;
162 struct clk *fast_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800163 void __iomem *base;
164 int cont_id;
165 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600166 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800167 int is_dvc;
168 struct completion msg_complete;
169 int msg_err;
170 u8 *msg_buf;
171 size_t msg_buf_remaining;
172 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000173 u32 bus_clk_rate;
Colin Crossdb811ca2011-02-20 17:14:21 -0800174 bool is_suspended;
175};
176
177static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
178{
179 writel(val, i2c_dev->base + reg);
180}
181
182static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
183{
184 return readl(i2c_dev->base + reg);
185}
186
187/*
188 * i2c_writel and i2c_readl will offset the register if necessary to talk
189 * to the I2C block inside the DVC block
190 */
191static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
192 unsigned long reg)
193{
194 if (i2c_dev->is_dvc)
195 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
196 return reg;
197}
198
199static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
200 unsigned long reg)
201{
202 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530203
204 /* Read back register to make sure that register writes completed */
205 if (reg != I2C_TX_FIFO)
206 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800207}
208
209static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
210{
211 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
212}
213
214static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
215 unsigned long reg, int len)
216{
217 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
218}
219
220static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
221 unsigned long reg, int len)
222{
223 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
224}
225
226static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
227{
228 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
229 int_mask &= ~mask;
230 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
231}
232
233static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
234{
235 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
236 int_mask |= mask;
237 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
238}
239
240static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
241{
242 unsigned long timeout = jiffies + HZ;
243 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
244 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
245 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
246
247 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
248 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
249 if (time_after(jiffies, timeout)) {
250 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
251 return -ETIMEDOUT;
252 }
253 msleep(1);
254 }
255 return 0;
256}
257
258static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
259{
260 u32 val;
261 int rx_fifo_avail;
262 u8 *buf = i2c_dev->msg_buf;
263 size_t buf_remaining = i2c_dev->msg_buf_remaining;
264 int words_to_transfer;
265
266 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
267 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
268 I2C_FIFO_STATUS_RX_SHIFT;
269
270 /* Rounds down to not include partial word at the end of buf */
271 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
272 if (words_to_transfer > rx_fifo_avail)
273 words_to_transfer = rx_fifo_avail;
274
275 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
276
277 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
278 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
279 rx_fifo_avail -= words_to_transfer;
280
281 /*
282 * If there is a partial word at the end of buf, handle it manually to
283 * prevent overwriting past the end of buf
284 */
285 if (rx_fifo_avail > 0 && buf_remaining > 0) {
286 BUG_ON(buf_remaining > 3);
287 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
288 memcpy(buf, &val, buf_remaining);
289 buf_remaining = 0;
290 rx_fifo_avail--;
291 }
292
293 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
294 i2c_dev->msg_buf_remaining = buf_remaining;
295 i2c_dev->msg_buf = buf;
296 return 0;
297}
298
299static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
300{
301 u32 val;
302 int tx_fifo_avail;
303 u8 *buf = i2c_dev->msg_buf;
304 size_t buf_remaining = i2c_dev->msg_buf_remaining;
305 int words_to_transfer;
306
307 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
308 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
309 I2C_FIFO_STATUS_TX_SHIFT;
310
311 /* Rounds down to not include partial word at the end of buf */
312 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800313
Doug Anderson96219c32011-08-30 11:46:10 -0600314 /* It's very common to have < 4 bytes, so optimize that case. */
315 if (words_to_transfer) {
316 if (words_to_transfer > tx_fifo_avail)
317 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800318
Doug Anderson96219c32011-08-30 11:46:10 -0600319 /*
320 * Update state before writing to FIFO. If this casues us
321 * to finish writing all bytes (AKA buf_remaining goes to 0) we
322 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
323 * not maskable). We need to make sure that the isr sees
324 * buf_remaining as 0 and doesn't call us back re-entrantly.
325 */
326 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
327 tx_fifo_avail -= words_to_transfer;
328 i2c_dev->msg_buf_remaining = buf_remaining;
329 i2c_dev->msg_buf = buf +
330 words_to_transfer * BYTES_PER_FIFO_WORD;
331 barrier();
332
333 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
334
335 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
336 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800337
338 /*
339 * If there is a partial word at the end of buf, handle it manually to
340 * prevent reading past the end of buf, which could cross a page
341 * boundary and fault.
342 */
343 if (tx_fifo_avail > 0 && buf_remaining > 0) {
344 BUG_ON(buf_remaining > 3);
345 memcpy(&val, buf, buf_remaining);
Doug Anderson96219c32011-08-30 11:46:10 -0600346
347 /* Again update before writing to FIFO to make sure isr sees. */
348 i2c_dev->msg_buf_remaining = 0;
349 i2c_dev->msg_buf = NULL;
350 barrier();
351
Colin Crossdb811ca2011-02-20 17:14:21 -0800352 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800353 }
354
Colin Crossdb811ca2011-02-20 17:14:21 -0800355 return 0;
356}
357
358/*
359 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
360 * block. This block is identical to the rest of the I2C blocks, except that
361 * it only supports master mode, it has registers moved around, and it needs
362 * some extra init to get it into I2C mode. The register moves are handled
363 * by i2c_readl and i2c_writel
364 */
365static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
366{
367 u32 val = 0;
368 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
369 val |= DVC_CTRL_REG3_SW_PROG;
370 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
371 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
372
373 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
374 val |= DVC_CTRL_REG1_INTR_EN;
375 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
376}
377
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530378static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
379{
380 int ret;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530381 if (!i2c_dev->hw->has_single_clk_source) {
382 ret = clk_prepare_enable(i2c_dev->fast_clk);
383 if (ret < 0) {
384 dev_err(i2c_dev->dev,
385 "Enabling fast clk failed, err %d\n", ret);
386 return ret;
387 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530388 }
389 ret = clk_prepare_enable(i2c_dev->div_clk);
390 if (ret < 0) {
391 dev_err(i2c_dev->dev,
392 "Enabling div clk failed, err %d\n", ret);
393 clk_disable_unprepare(i2c_dev->fast_clk);
394 }
395 return ret;
396}
397
398static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
399{
400 clk_disable_unprepare(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530401 if (!i2c_dev->hw->has_single_clk_source)
402 clk_disable_unprepare(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530403}
404
Colin Crossdb811ca2011-02-20 17:14:21 -0800405static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
406{
407 u32 val;
408 int err = 0;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530409 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
410 u32 clk_divisor;
Colin Crossdb811ca2011-02-20 17:14:21 -0800411
Laxman Dewangan132c8032013-03-15 05:34:08 +0000412 err = tegra_i2c_clock_enable(i2c_dev);
413 if (err < 0) {
414 dev_err(i2c_dev->dev, "Clock enable failed %d\n", err);
415 return err;
416 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800417
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530418 tegra_periph_reset_assert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800419 udelay(2);
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530420 tegra_periph_reset_deassert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800421
422 if (i2c_dev->is_dvc)
423 tegra_dvc_init(i2c_dev);
424
Jay Cheng40abcf72011-04-25 15:32:27 -0600425 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
426 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800427 i2c_writel(i2c_dev, val, I2C_CNFG);
428 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530429
430 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
431 clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate * clk_multiplier);
432
433 /* Make sure clock divisor programmed correctly */
434 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
435 clk_divisor |= i2c_dev->hw->clk_divisor_std_fast_mode <<
436 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
437 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800438
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600439 if (!i2c_dev->is_dvc) {
440 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600441 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
442 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
443 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
444 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
445
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600446 }
447
Colin Crossdb811ca2011-02-20 17:14:21 -0800448 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
449 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
450 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
451
452 if (tegra_i2c_flush_fifos(i2c_dev))
453 err = -ETIMEDOUT;
454
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530455 tegra_i2c_clock_disable(i2c_dev);
Todd Poynorcb63c622011-04-25 15:32:25 -0600456
457 if (i2c_dev->irq_disabled) {
458 i2c_dev->irq_disabled = 0;
459 enable_irq(i2c_dev->irq);
460 }
461
Colin Crossdb811ca2011-02-20 17:14:21 -0800462 return err;
463}
464
465static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
466{
467 u32 status;
468 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
469 struct tegra_i2c_dev *i2c_dev = dev_id;
470
471 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
472
473 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600474 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
475 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
476 i2c_readl(i2c_dev, I2C_STATUS),
477 i2c_readl(i2c_dev, I2C_CNFG));
478 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
479
480 if (!i2c_dev->irq_disabled) {
481 disable_irq_nosync(i2c_dev->irq);
482 i2c_dev->irq_disabled = 1;
483 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600484 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800485 }
486
487 if (unlikely(status & status_err)) {
488 if (status & I2C_INT_NO_ACK)
489 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
490 if (status & I2C_INT_ARBITRATION_LOST)
491 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800492 goto err;
493 }
494
495 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
496 if (i2c_dev->msg_buf_remaining)
497 tegra_i2c_empty_rx_fifo(i2c_dev);
498 else
499 BUG();
500 }
501
502 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
503 if (i2c_dev->msg_buf_remaining)
504 tegra_i2c_fill_tx_fifo(i2c_dev);
505 else
506 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
507 }
508
Laxman Dewanganc889e912012-05-07 12:16:19 +0530509 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
510 if (i2c_dev->is_dvc)
511 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
512
Doug Anderson96219c32011-08-30 11:46:10 -0600513 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
514 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800515 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600516 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800517 return IRQ_HANDLED;
518err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300519 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800520 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
521 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
522 I2C_INT_RX_FIFO_DATA_REQ);
523 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600524 if (i2c_dev->is_dvc)
525 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530526
527 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800528 return IRQ_HANDLED;
529}
530
531static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530532 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800533{
534 u32 packet_header;
535 u32 int_mask;
536 int ret;
537
538 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800539
540 if (msg->len == 0)
541 return -EINVAL;
542
543 i2c_dev->msg_buf = msg->buf;
544 i2c_dev->msg_buf_remaining = msg->len;
545 i2c_dev->msg_err = I2C_ERR_NONE;
546 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800547 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800548
549 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
550 PACKET_HEADER0_PROTOCOL_I2C |
551 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
552 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
553 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
554
555 packet_header = msg->len - 1;
556 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
557
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530558 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530559 if (end_state == MSG_END_CONTINUE)
560 packet_header |= I2C_HEADER_CONTINUE_XFER;
561 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600562 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530563 if (msg->flags & I2C_M_TEN) {
564 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800565 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530566 } else {
567 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
568 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800569 if (msg->flags & I2C_M_IGNORE_NAK)
570 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800571 if (msg->flags & I2C_M_RD)
572 packet_header |= I2C_HEADER_READ;
573 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
574
575 if (!(msg->flags & I2C_M_RD))
576 tegra_i2c_fill_tx_fifo(i2c_dev);
577
578 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530579 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
580 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800581 if (msg->flags & I2C_M_RD)
582 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
583 else if (i2c_dev->msg_buf_remaining)
584 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
585 tegra_i2c_unmask_irq(i2c_dev, int_mask);
586 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
587 i2c_readl(i2c_dev, I2C_INT_MASK));
588
589 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
590 tegra_i2c_mask_irq(i2c_dev, int_mask);
591
Laxman Dewangan58823c72013-02-14 18:13:33 +0530592 if (ret == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800593 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
594
595 tegra_i2c_init(i2c_dev);
596 return -ETIMEDOUT;
597 }
598
599 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
600 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
601
602 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
603 return 0;
604
Alok Chauhanf70893d02012-04-02 11:23:02 +0530605 /*
606 * NACK interrupt is generated before the I2C controller generates the
607 * STOP condition on the bus. So wait for 2 clock periods before resetting
608 * the controller so that STOP condition has been delivered properly.
609 */
610 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
611 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
612
Colin Crossdb811ca2011-02-20 17:14:21 -0800613 tegra_i2c_init(i2c_dev);
614 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
615 if (msg->flags & I2C_M_IGNORE_NAK)
616 return 0;
617 return -EREMOTEIO;
618 }
619
620 return -EIO;
621}
622
623static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
624 int num)
625{
626 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
627 int i;
628 int ret = 0;
629
630 if (i2c_dev->is_suspended)
631 return -EBUSY;
632
Laxman Dewangan132c8032013-03-15 05:34:08 +0000633 ret = tegra_i2c_clock_enable(i2c_dev);
634 if (ret < 0) {
635 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
636 return ret;
637 }
638
Colin Crossdb811ca2011-02-20 17:14:21 -0800639 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530640 enum msg_end_type end_type = MSG_END_STOP;
641 if (i < (num - 1)) {
642 if (msgs[i + 1].flags & I2C_M_NOSTART)
643 end_type = MSG_END_CONTINUE;
644 else
645 end_type = MSG_END_REPEAT_START;
646 }
647 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800648 if (ret)
649 break;
650 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530651 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800652 return ret ?: i;
653}
654
655static u32 tegra_i2c_func(struct i2c_adapter *adap)
656{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530657 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
658 u32 ret = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
659 I2C_FUNC_PROTOCOL_MANGLING;
660
661 if (i2c_dev->hw->has_continue_xfer_support)
662 ret |= I2C_FUNC_NOSTART;
663 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800664}
665
666static const struct i2c_algorithm tegra_i2c_algo = {
667 .master_xfer = tegra_i2c_xfer,
668 .functionality = tegra_i2c_func,
669};
670
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530671static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
672 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530673 .has_per_pkt_xfer_complete_irq = false,
674 .has_single_clk_source = false,
675 .clk_divisor_hs_mode = 3,
676 .clk_divisor_std_fast_mode = 0,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530677};
678
679static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
680 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530681 .has_per_pkt_xfer_complete_irq = false,
682 .has_single_clk_source = false,
683 .clk_divisor_hs_mode = 3,
684 .clk_divisor_std_fast_mode = 0,
685};
686
687static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
688 .has_continue_xfer_support = true,
689 .has_per_pkt_xfer_complete_irq = true,
690 .has_single_clk_source = true,
691 .clk_divisor_hs_mode = 1,
692 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530693};
694
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530695/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500696static const struct of_device_id tegra_i2c_of_match[] = {
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530697 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530698 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
699 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
700 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
701 {},
702};
703MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530704
Bill Pemberton0b255e92012-11-27 15:59:38 -0500705static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800706{
707 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800708 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530709 struct clk *div_clk;
710 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700711 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800712 int irq;
713 int ret = 0;
714
715 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100716 base = devm_ioremap_resource(&pdev->dev, res);
717 if (IS_ERR(base))
718 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800719
720 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
721 if (!res) {
722 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530723 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800724 }
725 irq = res->start;
726
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530727 div_clk = devm_clk_get(&pdev->dev, "div-clk");
728 if (IS_ERR(div_clk)) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800729 dev_err(&pdev->dev, "missing controller clock");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530730 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800731 }
732
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530733 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Colin Crossdb811ca2011-02-20 17:14:21 -0800734 if (!i2c_dev) {
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530735 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
736 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800737 }
738
739 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530740 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800741 i2c_dev->adapter.algo = &tegra_i2c_algo;
742 i2c_dev->irq = irq;
743 i2c_dev->cont_id = pdev->id;
744 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700745
Stephen Warren49a64ac2013-03-21 08:08:46 +0000746 ret = of_property_read_u32(i2c_dev->dev->of_node, "clock-frequency",
747 &i2c_dev->bus_clk_rate);
748 if (ret)
749 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
Colin Crossdb811ca2011-02-20 17:14:21 -0800750
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530751 i2c_dev->hw = &tegra20_i2c_hw;
752
753 if (pdev->dev.of_node) {
754 const struct of_device_id *match;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000755 match = of_match_device(tegra_i2c_of_match, &pdev->dev);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530756 i2c_dev->hw = match->data;
Stephen Warren68fb6692011-12-17 23:29:30 -0700757 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
758 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530759 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800760 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530761 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800762 init_completion(&i2c_dev->msg_complete);
763
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530764 if (!i2c_dev->hw->has_single_clk_source) {
765 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
766 if (IS_ERR(fast_clk)) {
767 dev_err(&pdev->dev, "missing fast clock");
768 return PTR_ERR(fast_clk);
769 }
770 i2c_dev->fast_clk = fast_clk;
771 }
772
Colin Crossdb811ca2011-02-20 17:14:21 -0800773 platform_set_drvdata(pdev, i2c_dev);
774
775 ret = tegra_i2c_init(i2c_dev);
776 if (ret) {
777 dev_err(&pdev->dev, "Failed to initialize i2c controller");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530778 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800779 }
780
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530781 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530782 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800783 if (ret) {
784 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530785 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800786 }
787
Colin Crossdb811ca2011-02-20 17:14:21 -0800788 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
789 i2c_dev->adapter.owner = THIS_MODULE;
790 i2c_dev->adapter.class = I2C_CLASS_HWMON;
791 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
792 sizeof(i2c_dev->adapter.name));
793 i2c_dev->adapter.algo = &tegra_i2c_algo;
794 i2c_dev->adapter.dev.parent = &pdev->dev;
795 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700796 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800797
798 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
799 if (ret) {
800 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530801 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800802 }
803
Colin Crossdb811ca2011-02-20 17:14:21 -0800804 return 0;
Colin Crossdb811ca2011-02-20 17:14:21 -0800805}
806
Bill Pemberton0b255e92012-11-27 15:59:38 -0500807static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800808{
809 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
810 i2c_del_adapter(&i2c_dev->adapter);
Colin Crossdb811ca2011-02-20 17:14:21 -0800811 return 0;
812}
813
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530814#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200815static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800816{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200817 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800818
819 i2c_lock_adapter(&i2c_dev->adapter);
820 i2c_dev->is_suspended = true;
821 i2c_unlock_adapter(&i2c_dev->adapter);
822
823 return 0;
824}
825
Wolfram Sang5db20c42012-07-24 17:32:45 +0200826static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800827{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200828 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800829 int ret;
830
831 i2c_lock_adapter(&i2c_dev->adapter);
832
833 ret = tegra_i2c_init(i2c_dev);
834
835 if (ret) {
836 i2c_unlock_adapter(&i2c_dev->adapter);
837 return ret;
838 }
839
840 i2c_dev->is_suspended = false;
841
842 i2c_unlock_adapter(&i2c_dev->adapter);
843
844 return 0;
845}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200846
Wolfram Sang5db20c42012-07-24 17:32:45 +0200847static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200848#define TEGRA_I2C_PM (&tegra_i2c_pm)
849#else
850#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -0800851#endif
852
853static struct platform_driver tegra_i2c_driver = {
854 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500855 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -0800856 .driver = {
857 .name = "tegra-i2c",
858 .owner = THIS_MODULE,
Stephen Warren49a64ac2013-03-21 08:08:46 +0000859 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200860 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -0800861 },
862};
863
864static int __init tegra_i2c_init_driver(void)
865{
866 return platform_driver_register(&tegra_i2c_driver);
867}
868
869static void __exit tegra_i2c_exit_driver(void)
870{
871 platform_driver_unregister(&tegra_i2c_driver);
872}
873
874subsys_initcall(tegra_i2c_init_driver);
875module_exit(tegra_i2c_exit_driver);
876
877MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
878MODULE_AUTHOR("Colin Cross");
879MODULE_LICENSE("GPL v2");