blob: b714776b6ddd1828258ee1a81d33728d1aba3c75 [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>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053032#include <linux/clk/tegra.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080033
34#include <asm/unaligned.h>
35
Colin Crossdb811ca2011-02-20 17:14:21 -080036#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
37#define BYTES_PER_FIFO_WORD 4
38
39#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060040#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Colin Crossdb811ca2011-02-20 17:14:21 -080041#define I2C_CNFG_PACKET_MODE_EN (1<<10)
42#define I2C_CNFG_NEW_MASTER_FSM (1<<11)
Todd Poynorcb63c622011-04-25 15:32:25 -060043#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080044#define I2C_SL_CNFG 0x020
Stephen Warren5afa9d32011-06-06 11:25:19 -060045#define I2C_SL_CNFG_NACK (1<<1)
Colin Crossdb811ca2011-02-20 17:14:21 -080046#define I2C_SL_CNFG_NEWSL (1<<2)
47#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060048#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080049#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
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053073#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
74#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080075
76#define DVC_CTRL_REG1 0x000
77#define DVC_CTRL_REG1_INTR_EN (1<<10)
78#define DVC_CTRL_REG2 0x004
79#define DVC_CTRL_REG3 0x008
80#define DVC_CTRL_REG3_SW_PROG (1<<26)
81#define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
82#define DVC_STATUS 0x00c
83#define DVC_STATUS_I2C_DONE_INTR (1<<30)
84
85#define I2C_ERR_NONE 0x00
86#define I2C_ERR_NO_ACK 0x01
87#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060088#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080089
90#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
91#define PACKET_HEADER0_PACKET_ID_SHIFT 16
92#define PACKET_HEADER0_CONT_ID_SHIFT 12
93#define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
94
95#define I2C_HEADER_HIGHSPEED_MODE (1<<22)
96#define I2C_HEADER_CONT_ON_NAK (1<<21)
97#define I2C_HEADER_SEND_START_BYTE (1<<20)
98#define I2C_HEADER_READ (1<<19)
99#define I2C_HEADER_10BIT_ADDR (1<<18)
100#define I2C_HEADER_IE_ENABLE (1<<17)
101#define I2C_HEADER_REPEAT_START (1<<16)
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530102#define I2C_HEADER_CONTINUE_XFER (1<<15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800103#define I2C_HEADER_MASTER_ADDR_SHIFT 12
104#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530105/*
106 * msg_end_type: The bus control which need to be send at end of transfer.
107 * @MSG_END_STOP: Send stop pulse at end of transfer.
108 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
109 * @MSG_END_CONTINUE: The following on message is coming and so do not send
110 * stop or repeat start.
111 */
112enum msg_end_type {
113 MSG_END_STOP,
114 MSG_END_REPEAT_START,
115 MSG_END_CONTINUE,
116};
Colin Crossdb811ca2011-02-20 17:14:21 -0800117
118/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530119 * struct tegra_i2c_hw_feature : Different HW support on Tegra
120 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530121 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
122 * complete interrupt per packet basis.
123 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
124 * and earlier Socs has two clock sources i.e. div-clk and
125 * fast-clk.
126 * @clk_divisor_hs_mode: Clock divisor in HS mode.
127 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
128 * applicable if there is no fast clock source i.e. single clock
129 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530130 */
131
132struct tegra_i2c_hw_feature {
133 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530134 bool has_per_pkt_xfer_complete_irq;
135 bool has_single_clk_source;
136 int clk_divisor_hs_mode;
137 int clk_divisor_std_fast_mode;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530138};
139
140/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800141 * struct tegra_i2c_dev - per device i2c context
142 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530143 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800144 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530145 * @div_clk: clock reference for div clock of i2c controller.
146 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800147 * @base: ioremapped registers cookie
148 * @cont_id: i2c controller id, used for for packet header
149 * @irq: irq number of transfer complete interrupt
150 * @is_dvc: identifies the DVC i2c controller, has a different register layout
151 * @msg_complete: transfer completion notifier
152 * @msg_err: error code for completed message
153 * @msg_buf: pointer to current message data
154 * @msg_buf_remaining: size of unsent data in the message buffer
155 * @msg_read: identifies read transfers
156 * @bus_clk_rate: current i2c bus clock rate
157 * @is_suspended: prevents i2c controller accesses after suspend is called
158 */
159struct tegra_i2c_dev {
160 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530161 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800162 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530163 struct clk *div_clk;
164 struct clk *fast_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800165 void __iomem *base;
166 int cont_id;
167 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600168 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800169 int is_dvc;
170 struct completion msg_complete;
171 int msg_err;
172 u8 *msg_buf;
173 size_t msg_buf_remaining;
174 int msg_read;
175 unsigned long bus_clk_rate;
176 bool is_suspended;
177};
178
179static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
180{
181 writel(val, i2c_dev->base + reg);
182}
183
184static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
185{
186 return readl(i2c_dev->base + reg);
187}
188
189/*
190 * i2c_writel and i2c_readl will offset the register if necessary to talk
191 * to the I2C block inside the DVC block
192 */
193static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
194 unsigned long reg)
195{
196 if (i2c_dev->is_dvc)
197 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
198 return reg;
199}
200
201static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
202 unsigned long reg)
203{
204 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530205
206 /* Read back register to make sure that register writes completed */
207 if (reg != I2C_TX_FIFO)
208 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800209}
210
211static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
212{
213 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
214}
215
216static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
217 unsigned long reg, int len)
218{
219 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
220}
221
222static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
223 unsigned long reg, int len)
224{
225 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
226}
227
228static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
229{
230 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
231 int_mask &= ~mask;
232 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
233}
234
235static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
236{
237 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
238 int_mask |= mask;
239 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
240}
241
242static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
243{
244 unsigned long timeout = jiffies + HZ;
245 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
246 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
247 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
248
249 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
250 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
251 if (time_after(jiffies, timeout)) {
252 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
253 return -ETIMEDOUT;
254 }
255 msleep(1);
256 }
257 return 0;
258}
259
260static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
261{
262 u32 val;
263 int rx_fifo_avail;
264 u8 *buf = i2c_dev->msg_buf;
265 size_t buf_remaining = i2c_dev->msg_buf_remaining;
266 int words_to_transfer;
267
268 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
269 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
270 I2C_FIFO_STATUS_RX_SHIFT;
271
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 > rx_fifo_avail)
275 words_to_transfer = rx_fifo_avail;
276
277 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
278
279 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
280 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
281 rx_fifo_avail -= words_to_transfer;
282
283 /*
284 * If there is a partial word at the end of buf, handle it manually to
285 * prevent overwriting past the end of buf
286 */
287 if (rx_fifo_avail > 0 && buf_remaining > 0) {
288 BUG_ON(buf_remaining > 3);
289 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
290 memcpy(buf, &val, buf_remaining);
291 buf_remaining = 0;
292 rx_fifo_avail--;
293 }
294
295 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
296 i2c_dev->msg_buf_remaining = buf_remaining;
297 i2c_dev->msg_buf = buf;
298 return 0;
299}
300
301static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
302{
303 u32 val;
304 int tx_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 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
311 I2C_FIFO_STATUS_TX_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;
Colin Crossdb811ca2011-02-20 17:14:21 -0800315
Doug Anderson96219c32011-08-30 11:46:10 -0600316 /* It's very common to have < 4 bytes, so optimize that case. */
317 if (words_to_transfer) {
318 if (words_to_transfer > tx_fifo_avail)
319 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800320
Doug Anderson96219c32011-08-30 11:46:10 -0600321 /*
322 * Update state before writing to FIFO. If this casues us
323 * to finish writing all bytes (AKA buf_remaining goes to 0) we
324 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
325 * not maskable). We need to make sure that the isr sees
326 * buf_remaining as 0 and doesn't call us back re-entrantly.
327 */
328 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
329 tx_fifo_avail -= words_to_transfer;
330 i2c_dev->msg_buf_remaining = buf_remaining;
331 i2c_dev->msg_buf = buf +
332 words_to_transfer * BYTES_PER_FIFO_WORD;
333 barrier();
334
335 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
336
337 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
338 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800339
340 /*
341 * If there is a partial word at the end of buf, handle it manually to
342 * prevent reading past the end of buf, which could cross a page
343 * boundary and fault.
344 */
345 if (tx_fifo_avail > 0 && buf_remaining > 0) {
346 BUG_ON(buf_remaining > 3);
347 memcpy(&val, buf, buf_remaining);
Doug Anderson96219c32011-08-30 11:46:10 -0600348
349 /* Again update before writing to FIFO to make sure isr sees. */
350 i2c_dev->msg_buf_remaining = 0;
351 i2c_dev->msg_buf = NULL;
352 barrier();
353
Colin Crossdb811ca2011-02-20 17:14:21 -0800354 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800355 }
356
Colin Crossdb811ca2011-02-20 17:14:21 -0800357 return 0;
358}
359
360/*
361 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
362 * block. This block is identical to the rest of the I2C blocks, except that
363 * it only supports master mode, it has registers moved around, and it needs
364 * some extra init to get it into I2C mode. The register moves are handled
365 * by i2c_readl and i2c_writel
366 */
367static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
368{
369 u32 val = 0;
370 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
371 val |= DVC_CTRL_REG3_SW_PROG;
372 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
373 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
374
375 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
376 val |= DVC_CTRL_REG1_INTR_EN;
377 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
378}
379
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530380static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
381{
382 int ret;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530383 if (!i2c_dev->hw->has_single_clk_source) {
384 ret = clk_prepare_enable(i2c_dev->fast_clk);
385 if (ret < 0) {
386 dev_err(i2c_dev->dev,
387 "Enabling fast clk failed, err %d\n", ret);
388 return ret;
389 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530390 }
391 ret = clk_prepare_enable(i2c_dev->div_clk);
392 if (ret < 0) {
393 dev_err(i2c_dev->dev,
394 "Enabling div clk failed, err %d\n", ret);
395 clk_disable_unprepare(i2c_dev->fast_clk);
396 }
397 return ret;
398}
399
400static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
401{
402 clk_disable_unprepare(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530403 if (!i2c_dev->hw->has_single_clk_source)
404 clk_disable_unprepare(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530405}
406
Colin Crossdb811ca2011-02-20 17:14:21 -0800407static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
408{
409 u32 val;
410 int err = 0;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530411 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
412 u32 clk_divisor;
Colin Crossdb811ca2011-02-20 17:14:21 -0800413
Laxman Dewangan132c8032013-03-15 05:34:08 +0000414 err = tegra_i2c_clock_enable(i2c_dev);
415 if (err < 0) {
416 dev_err(i2c_dev->dev, "Clock enable failed %d\n", err);
417 return err;
418 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800419
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530420 tegra_periph_reset_assert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800421 udelay(2);
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530422 tegra_periph_reset_deassert(i2c_dev->div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800423
424 if (i2c_dev->is_dvc)
425 tegra_dvc_init(i2c_dev);
426
Jay Cheng40abcf72011-04-25 15:32:27 -0600427 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
428 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800429 i2c_writel(i2c_dev, val, I2C_CNFG);
430 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530431
432 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
433 clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate * clk_multiplier);
434
435 /* Make sure clock divisor programmed correctly */
436 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
437 clk_divisor |= i2c_dev->hw->clk_divisor_std_fast_mode <<
438 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
439 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800440
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600441 if (!i2c_dev->is_dvc) {
442 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600443 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
444 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
445 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
446 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
447
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600448 }
449
Colin Crossdb811ca2011-02-20 17:14:21 -0800450 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
451 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
452 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
453
454 if (tegra_i2c_flush_fifos(i2c_dev))
455 err = -ETIMEDOUT;
456
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530457 tegra_i2c_clock_disable(i2c_dev);
Todd Poynorcb63c622011-04-25 15:32:25 -0600458
459 if (i2c_dev->irq_disabled) {
460 i2c_dev->irq_disabled = 0;
461 enable_irq(i2c_dev->irq);
462 }
463
Colin Crossdb811ca2011-02-20 17:14:21 -0800464 return err;
465}
466
467static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
468{
469 u32 status;
470 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
471 struct tegra_i2c_dev *i2c_dev = dev_id;
472
473 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
474
475 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600476 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
477 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
478 i2c_readl(i2c_dev, I2C_STATUS),
479 i2c_readl(i2c_dev, I2C_CNFG));
480 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
481
482 if (!i2c_dev->irq_disabled) {
483 disable_irq_nosync(i2c_dev->irq);
484 i2c_dev->irq_disabled = 1;
485 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600486 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800487 }
488
489 if (unlikely(status & status_err)) {
490 if (status & I2C_INT_NO_ACK)
491 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
492 if (status & I2C_INT_ARBITRATION_LOST)
493 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800494 goto err;
495 }
496
497 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
498 if (i2c_dev->msg_buf_remaining)
499 tegra_i2c_empty_rx_fifo(i2c_dev);
500 else
501 BUG();
502 }
503
504 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
505 if (i2c_dev->msg_buf_remaining)
506 tegra_i2c_fill_tx_fifo(i2c_dev);
507 else
508 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
509 }
510
Laxman Dewanganc889e912012-05-07 12:16:19 +0530511 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
512 if (i2c_dev->is_dvc)
513 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
514
Doug Anderson96219c32011-08-30 11:46:10 -0600515 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
516 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800517 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600518 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800519 return IRQ_HANDLED;
520err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300521 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800522 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
523 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
524 I2C_INT_RX_FIFO_DATA_REQ);
525 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600526 if (i2c_dev->is_dvc)
527 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530528
529 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800530 return IRQ_HANDLED;
531}
532
533static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530534 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800535{
536 u32 packet_header;
537 u32 int_mask;
538 int ret;
539
540 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800541
542 if (msg->len == 0)
543 return -EINVAL;
544
545 i2c_dev->msg_buf = msg->buf;
546 i2c_dev->msg_buf_remaining = msg->len;
547 i2c_dev->msg_err = I2C_ERR_NONE;
548 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
549 INIT_COMPLETION(i2c_dev->msg_complete);
550
551 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
552 PACKET_HEADER0_PROTOCOL_I2C |
553 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
554 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
555 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
556
557 packet_header = msg->len - 1;
558 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
559
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530560 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530561 if (end_state == MSG_END_CONTINUE)
562 packet_header |= I2C_HEADER_CONTINUE_XFER;
563 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600564 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530565 if (msg->flags & I2C_M_TEN) {
566 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800567 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530568 } else {
569 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
570 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800571 if (msg->flags & I2C_M_IGNORE_NAK)
572 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800573 if (msg->flags & I2C_M_RD)
574 packet_header |= I2C_HEADER_READ;
575 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
576
577 if (!(msg->flags & I2C_M_RD))
578 tegra_i2c_fill_tx_fifo(i2c_dev);
579
580 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530581 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
582 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800583 if (msg->flags & I2C_M_RD)
584 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
585 else if (i2c_dev->msg_buf_remaining)
586 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
587 tegra_i2c_unmask_irq(i2c_dev, int_mask);
588 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
589 i2c_readl(i2c_dev, I2C_INT_MASK));
590
591 ret = wait_for_completion_timeout(&i2c_dev->msg_complete, TEGRA_I2C_TIMEOUT);
592 tegra_i2c_mask_irq(i2c_dev, int_mask);
593
Laxman Dewangan58823c72013-02-14 18:13:33 +0530594 if (ret == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800595 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
596
597 tegra_i2c_init(i2c_dev);
598 return -ETIMEDOUT;
599 }
600
601 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
602 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
603
604 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
605 return 0;
606
Alok Chauhanf70893d02012-04-02 11:23:02 +0530607 /*
608 * NACK interrupt is generated before the I2C controller generates the
609 * STOP condition on the bus. So wait for 2 clock periods before resetting
610 * the controller so that STOP condition has been delivered properly.
611 */
612 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
613 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
614
Colin Crossdb811ca2011-02-20 17:14:21 -0800615 tegra_i2c_init(i2c_dev);
616 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
617 if (msg->flags & I2C_M_IGNORE_NAK)
618 return 0;
619 return -EREMOTEIO;
620 }
621
622 return -EIO;
623}
624
625static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
626 int num)
627{
628 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
629 int i;
630 int ret = 0;
631
632 if (i2c_dev->is_suspended)
633 return -EBUSY;
634
Laxman Dewangan132c8032013-03-15 05:34:08 +0000635 ret = tegra_i2c_clock_enable(i2c_dev);
636 if (ret < 0) {
637 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
638 return ret;
639 }
640
Colin Crossdb811ca2011-02-20 17:14:21 -0800641 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530642 enum msg_end_type end_type = MSG_END_STOP;
643 if (i < (num - 1)) {
644 if (msgs[i + 1].flags & I2C_M_NOSTART)
645 end_type = MSG_END_CONTINUE;
646 else
647 end_type = MSG_END_REPEAT_START;
648 }
649 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800650 if (ret)
651 break;
652 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530653 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800654 return ret ?: i;
655}
656
657static u32 tegra_i2c_func(struct i2c_adapter *adap)
658{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530659 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
660 u32 ret = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
661 I2C_FUNC_PROTOCOL_MANGLING;
662
663 if (i2c_dev->hw->has_continue_xfer_support)
664 ret |= I2C_FUNC_NOSTART;
665 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800666}
667
668static const struct i2c_algorithm tegra_i2c_algo = {
669 .master_xfer = tegra_i2c_xfer,
670 .functionality = tegra_i2c_func,
671};
672
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530673static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
674 .has_continue_xfer_support = false,
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,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530679};
680
681static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
682 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530683 .has_per_pkt_xfer_complete_irq = false,
684 .has_single_clk_source = false,
685 .clk_divisor_hs_mode = 3,
686 .clk_divisor_std_fast_mode = 0,
687};
688
689static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
690 .has_continue_xfer_support = true,
691 .has_per_pkt_xfer_complete_irq = true,
692 .has_single_clk_source = true,
693 .clk_divisor_hs_mode = 1,
694 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530695};
696
697#if defined(CONFIG_OF)
698/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500699static const struct of_device_id tegra_i2c_of_match[] = {
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530700 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530701 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
702 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
703 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
704 {},
705};
706MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
707#endif
708
Bill Pemberton0b255e92012-11-27 15:59:38 -0500709static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800710{
711 struct tegra_i2c_dev *i2c_dev;
712 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
713 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530714 struct clk *div_clk;
715 struct clk *fast_clk;
John Bonesio5c470f32011-06-22 09:16:56 -0700716 const unsigned int *prop;
Olof Johanssonf533c612011-10-12 17:33:00 -0700717 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800718 int irq;
719 int ret = 0;
720
721 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
722 if (!res) {
723 dev_err(&pdev->dev, "no mem resource\n");
724 return -EINVAL;
725 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800726
Thierry Reding84dbf802013-01-21 11:09:03 +0100727 base = devm_ioremap_resource(&pdev->dev, res);
728 if (IS_ERR(base))
729 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800730
731 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
732 if (!res) {
733 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530734 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800735 }
736 irq = res->start;
737
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530738 div_clk = devm_clk_get(&pdev->dev, "div-clk");
739 if (IS_ERR(div_clk)) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800740 dev_err(&pdev->dev, "missing controller clock");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530741 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800742 }
743
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530744 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Colin Crossdb811ca2011-02-20 17:14:21 -0800745 if (!i2c_dev) {
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530746 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
747 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800748 }
749
750 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530751 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800752 i2c_dev->adapter.algo = &tegra_i2c_algo;
753 i2c_dev->irq = irq;
754 i2c_dev->cont_id = pdev->id;
755 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700756
757 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
758 if (pdata) {
759 i2c_dev->bus_clk_rate = pdata->bus_clk_rate;
760
761 } else if (i2c_dev->dev->of_node) { /* if there is a device tree node ... */
762 prop = of_get_property(i2c_dev->dev->of_node,
763 "clock-frequency", NULL);
764 if (prop)
765 i2c_dev->bus_clk_rate = be32_to_cpup(prop);
766 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800767
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530768 i2c_dev->hw = &tegra20_i2c_hw;
769
770 if (pdev->dev.of_node) {
771 const struct of_device_id *match;
772 match = of_match_device(of_match_ptr(tegra_i2c_of_match),
773 &pdev->dev);
774 i2c_dev->hw = match->data;
Stephen Warren68fb6692011-12-17 23:29:30 -0700775 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
776 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530777 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800778 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530779 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800780 init_completion(&i2c_dev->msg_complete);
781
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530782 if (!i2c_dev->hw->has_single_clk_source) {
783 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
784 if (IS_ERR(fast_clk)) {
785 dev_err(&pdev->dev, "missing fast clock");
786 return PTR_ERR(fast_clk);
787 }
788 i2c_dev->fast_clk = fast_clk;
789 }
790
Colin Crossdb811ca2011-02-20 17:14:21 -0800791 platform_set_drvdata(pdev, i2c_dev);
792
793 ret = tegra_i2c_init(i2c_dev);
794 if (ret) {
795 dev_err(&pdev->dev, "Failed to initialize i2c controller");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530796 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800797 }
798
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530799 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530800 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800801 if (ret) {
802 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530803 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800804 }
805
Colin Crossdb811ca2011-02-20 17:14:21 -0800806 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
807 i2c_dev->adapter.owner = THIS_MODULE;
808 i2c_dev->adapter.class = I2C_CLASS_HWMON;
809 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
810 sizeof(i2c_dev->adapter.name));
811 i2c_dev->adapter.algo = &tegra_i2c_algo;
812 i2c_dev->adapter.dev.parent = &pdev->dev;
813 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700814 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800815
816 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
817 if (ret) {
818 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530819 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800820 }
821
John Bonesio5c470f32011-06-22 09:16:56 -0700822 of_i2c_register_devices(&i2c_dev->adapter);
823
Colin Crossdb811ca2011-02-20 17:14:21 -0800824 return 0;
Colin Crossdb811ca2011-02-20 17:14:21 -0800825}
826
Bill Pemberton0b255e92012-11-27 15:59:38 -0500827static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800828{
829 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
830 i2c_del_adapter(&i2c_dev->adapter);
Colin Crossdb811ca2011-02-20 17:14:21 -0800831 return 0;
832}
833
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530834#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200835static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800836{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200837 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800838
839 i2c_lock_adapter(&i2c_dev->adapter);
840 i2c_dev->is_suspended = true;
841 i2c_unlock_adapter(&i2c_dev->adapter);
842
843 return 0;
844}
845
Wolfram Sang5db20c42012-07-24 17:32:45 +0200846static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800847{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200848 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800849 int ret;
850
851 i2c_lock_adapter(&i2c_dev->adapter);
852
853 ret = tegra_i2c_init(i2c_dev);
854
855 if (ret) {
856 i2c_unlock_adapter(&i2c_dev->adapter);
857 return ret;
858 }
859
860 i2c_dev->is_suspended = false;
861
862 i2c_unlock_adapter(&i2c_dev->adapter);
863
864 return 0;
865}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200866
Wolfram Sang5db20c42012-07-24 17:32:45 +0200867static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200868#define TEGRA_I2C_PM (&tegra_i2c_pm)
869#else
870#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -0800871#endif
872
873static struct platform_driver tegra_i2c_driver = {
874 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500875 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -0800876 .driver = {
877 .name = "tegra-i2c",
878 .owner = THIS_MODULE,
Laxman Dewangan02d8bf82012-07-10 16:50:42 +0530879 .of_match_table = of_match_ptr(tegra_i2c_of_match),
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200880 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -0800881 },
882};
883
884static int __init tegra_i2c_init_driver(void)
885{
886 return platform_driver_register(&tegra_i2c_driver);
887}
888
889static void __exit tegra_i2c_exit_driver(void)
890{
891 platform_driver_unregister(&tegra_i2c_driver);
892}
893
894subsys_initcall(tegra_i2c_init_driver);
895module_exit(tegra_i2c_exit_driver);
896
897MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
898MODULE_AUTHOR("Colin Cross");
899MODULE_LICENSE("GPL v2");