blob: 78a3668146967408146c81a213e09d6219543718 [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>
Stephen Warrendda9d6a2013-11-06 16:42:05 -070030#include <linux/reset.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;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700163 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800164 void __iomem *base;
165 int cont_id;
166 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600167 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800168 int is_dvc;
169 struct completion msg_complete;
170 int msg_err;
171 u8 *msg_buf;
172 size_t msg_buf_remaining;
173 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000174 u32 bus_clk_rate;
Colin Crossdb811ca2011-02-20 17:14:21 -0800175 bool is_suspended;
176};
177
178static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
179{
180 writel(val, i2c_dev->base + reg);
181}
182
183static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
184{
185 return readl(i2c_dev->base + reg);
186}
187
188/*
189 * i2c_writel and i2c_readl will offset the register if necessary to talk
190 * to the I2C block inside the DVC block
191 */
192static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
193 unsigned long reg)
194{
195 if (i2c_dev->is_dvc)
196 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
197 return reg;
198}
199
200static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
201 unsigned long reg)
202{
203 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530204
205 /* Read back register to make sure that register writes completed */
206 if (reg != I2C_TX_FIFO)
207 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800208}
209
210static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
211{
212 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
213}
214
215static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
216 unsigned long reg, int len)
217{
218 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
219}
220
221static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
222 unsigned long reg, int len)
223{
224 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
225}
226
227static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
228{
229 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
230 int_mask &= ~mask;
231 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
232}
233
234static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
235{
236 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
237 int_mask |= mask;
238 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
239}
240
241static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
242{
243 unsigned long timeout = jiffies + HZ;
244 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
245 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
246 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
247
248 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
249 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
250 if (time_after(jiffies, timeout)) {
251 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
252 return -ETIMEDOUT;
253 }
254 msleep(1);
255 }
256 return 0;
257}
258
259static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
260{
261 u32 val;
262 int rx_fifo_avail;
263 u8 *buf = i2c_dev->msg_buf;
264 size_t buf_remaining = i2c_dev->msg_buf_remaining;
265 int words_to_transfer;
266
267 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
268 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
269 I2C_FIFO_STATUS_RX_SHIFT;
270
271 /* Rounds down to not include partial word at the end of buf */
272 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
273 if (words_to_transfer > rx_fifo_avail)
274 words_to_transfer = rx_fifo_avail;
275
276 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
277
278 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
279 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
280 rx_fifo_avail -= words_to_transfer;
281
282 /*
283 * If there is a partial word at the end of buf, handle it manually to
284 * prevent overwriting past the end of buf
285 */
286 if (rx_fifo_avail > 0 && buf_remaining > 0) {
287 BUG_ON(buf_remaining > 3);
288 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300289 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800290 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);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300348 val = le32_to_cpu(val);
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) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300385 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530386 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 }
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300392 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530393 if (ret < 0) {
394 dev_err(i2c_dev->dev,
395 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300396 clk_disable(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530397 }
398 return ret;
399}
400
401static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
402{
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300403 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530404 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300405 clk_disable(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 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
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700420 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800421 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700422 reset_control_deassert(i2c_dev->rst);
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
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530432 /* 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;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500535 unsigned long time_left;
Colin Crossdb811ca2011-02-20 17:14:21 -0800536
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);
Wolfram Sang16735d02013-11-14 14:32:02 -0800546 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800547
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
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500588 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
589 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800590 tegra_i2c_mask_irq(i2c_dev, int_mask);
591
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500592 if (time_left == 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
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500599 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
600 time_left, completion_done(&i2c_dev->msg_complete),
601 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800602
603 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
604 return 0;
605
Alok Chauhanf70893d02012-04-02 11:23:02 +0530606 /*
607 * NACK interrupt is generated before the I2C controller generates the
608 * STOP condition on the bus. So wait for 2 clock periods before resetting
609 * the controller so that STOP condition has been delivered properly.
610 */
611 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
612 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
613
Colin Crossdb811ca2011-02-20 17:14:21 -0800614 tegra_i2c_init(i2c_dev);
615 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
616 if (msg->flags & I2C_M_IGNORE_NAK)
617 return 0;
618 return -EREMOTEIO;
619 }
620
621 return -EIO;
622}
623
624static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
625 int num)
626{
627 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
628 int i;
629 int ret = 0;
630
631 if (i2c_dev->is_suspended)
632 return -EBUSY;
633
Laxman Dewangan132c8032013-03-15 05:34:08 +0000634 ret = tegra_i2c_clock_enable(i2c_dev);
635 if (ret < 0) {
636 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
637 return ret;
638 }
639
Colin Crossdb811ca2011-02-20 17:14:21 -0800640 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530641 enum msg_end_type end_type = MSG_END_STOP;
642 if (i < (num - 1)) {
643 if (msgs[i + 1].flags & I2C_M_NOSTART)
644 end_type = MSG_END_CONTINUE;
645 else
646 end_type = MSG_END_REPEAT_START;
647 }
648 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800649 if (ret)
650 break;
651 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530652 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800653 return ret ?: i;
654}
655
656static u32 tegra_i2c_func(struct i2c_adapter *adap)
657{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530658 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200659 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
660 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530661
662 if (i2c_dev->hw->has_continue_xfer_support)
663 ret |= I2C_FUNC_NOSTART;
664 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800665}
666
667static const struct i2c_algorithm tegra_i2c_algo = {
668 .master_xfer = tegra_i2c_xfer,
669 .functionality = tegra_i2c_func,
670};
671
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200672/* payload size is only 12 bit */
673static struct i2c_adapter_quirks tegra_i2c_quirks = {
674 .max_read_len = 4096,
675 .max_write_len = 4096,
676};
677
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530678static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
679 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530680 .has_per_pkt_xfer_complete_irq = false,
681 .has_single_clk_source = false,
682 .clk_divisor_hs_mode = 3,
683 .clk_divisor_std_fast_mode = 0,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530684};
685
686static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
687 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530688 .has_per_pkt_xfer_complete_irq = false,
689 .has_single_clk_source = false,
690 .clk_divisor_hs_mode = 3,
691 .clk_divisor_std_fast_mode = 0,
692};
693
694static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
695 .has_continue_xfer_support = true,
696 .has_per_pkt_xfer_complete_irq = true,
697 .has_single_clk_source = true,
698 .clk_divisor_hs_mode = 1,
699 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530700};
701
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530702/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500703static const struct of_device_id tegra_i2c_of_match[] = {
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530704 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530705 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
706 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
707 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
708 {},
709};
710MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530711
Bill Pemberton0b255e92012-11-27 15:59:38 -0500712static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800713{
714 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800715 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530716 struct clk *div_clk;
717 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700718 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800719 int irq;
720 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300721 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800722
723 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100724 base = devm_ioremap_resource(&pdev->dev, res);
725 if (IS_ERR(base))
726 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800727
728 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
729 if (!res) {
730 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530731 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800732 }
733 irq = res->start;
734
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530735 div_clk = devm_clk_get(&pdev->dev, "div-clk");
736 if (IS_ERR(div_clk)) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800737 dev_err(&pdev->dev, "missing controller clock");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530738 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800739 }
740
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530741 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900742 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530743 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800744
745 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530746 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800747 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200748 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800749 i2c_dev->irq = irq;
750 i2c_dev->cont_id = pdev->id;
751 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700752
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700753 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
754 if (IS_ERR(i2c_dev->rst)) {
755 dev_err(&pdev->dev, "missing controller reset");
756 return PTR_ERR(i2c_dev->rst);
757 }
758
Stephen Warren49a64ac2013-03-21 08:08:46 +0000759 ret = of_property_read_u32(i2c_dev->dev->of_node, "clock-frequency",
760 &i2c_dev->bus_clk_rate);
761 if (ret)
762 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
Colin Crossdb811ca2011-02-20 17:14:21 -0800763
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530764 i2c_dev->hw = &tegra20_i2c_hw;
765
766 if (pdev->dev.of_node) {
767 const struct of_device_id *match;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000768 match = of_match_device(tegra_i2c_of_match, &pdev->dev);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530769 i2c_dev->hw = match->data;
Stephen Warren68fb6692011-12-17 23:29:30 -0700770 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
771 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530772 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800773 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530774 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800775 init_completion(&i2c_dev->msg_complete);
776
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530777 if (!i2c_dev->hw->has_single_clk_source) {
778 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
779 if (IS_ERR(fast_clk)) {
780 dev_err(&pdev->dev, "missing fast clock");
781 return PTR_ERR(fast_clk);
782 }
783 i2c_dev->fast_clk = fast_clk;
784 }
785
Colin Crossdb811ca2011-02-20 17:14:21 -0800786 platform_set_drvdata(pdev, i2c_dev);
787
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300788 if (!i2c_dev->hw->has_single_clk_source) {
789 ret = clk_prepare(i2c_dev->fast_clk);
790 if (ret < 0) {
791 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
792 return ret;
793 }
794 }
795
796 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
797 ret = clk_set_rate(i2c_dev->div_clk,
798 i2c_dev->bus_clk_rate * clk_multiplier);
799 if (ret) {
800 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
801 goto unprepare_fast_clk;
802 }
803
804 ret = clk_prepare(i2c_dev->div_clk);
805 if (ret < 0) {
806 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
807 goto unprepare_fast_clk;
808 }
809
Colin Crossdb811ca2011-02-20 17:14:21 -0800810 ret = tegra_i2c_init(i2c_dev);
811 if (ret) {
812 dev_err(&pdev->dev, "Failed to initialize i2c controller");
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300813 goto unprepare_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800814 }
815
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530816 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530817 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800818 if (ret) {
819 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300820 goto unprepare_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800821 }
822
Colin Crossdb811ca2011-02-20 17:14:21 -0800823 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
824 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +0200825 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Colin Crossdb811ca2011-02-20 17:14:21 -0800826 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
827 sizeof(i2c_dev->adapter.name));
828 i2c_dev->adapter.algo = &tegra_i2c_algo;
829 i2c_dev->adapter.dev.parent = &pdev->dev;
830 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700831 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800832
833 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
834 if (ret) {
835 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300836 goto unprepare_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800837 }
838
Colin Crossdb811ca2011-02-20 17:14:21 -0800839 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300840
841unprepare_div_clk:
842 clk_unprepare(i2c_dev->div_clk);
843
844unprepare_fast_clk:
845 if (!i2c_dev->hw->has_single_clk_source)
846 clk_unprepare(i2c_dev->fast_clk);
847
848 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800849}
850
Bill Pemberton0b255e92012-11-27 15:59:38 -0500851static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800852{
853 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
854 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300855
856 clk_unprepare(i2c_dev->div_clk);
857 if (!i2c_dev->hw->has_single_clk_source)
858 clk_unprepare(i2c_dev->fast_clk);
859
Colin Crossdb811ca2011-02-20 17:14:21 -0800860 return 0;
861}
862
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530863#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200864static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800865{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200866 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800867
868 i2c_lock_adapter(&i2c_dev->adapter);
869 i2c_dev->is_suspended = true;
870 i2c_unlock_adapter(&i2c_dev->adapter);
871
872 return 0;
873}
874
Wolfram Sang5db20c42012-07-24 17:32:45 +0200875static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800876{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200877 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800878 int ret;
879
880 i2c_lock_adapter(&i2c_dev->adapter);
881
882 ret = tegra_i2c_init(i2c_dev);
883
884 if (ret) {
885 i2c_unlock_adapter(&i2c_dev->adapter);
886 return ret;
887 }
888
889 i2c_dev->is_suspended = false;
890
891 i2c_unlock_adapter(&i2c_dev->adapter);
892
893 return 0;
894}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200895
Wolfram Sang5db20c42012-07-24 17:32:45 +0200896static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200897#define TEGRA_I2C_PM (&tegra_i2c_pm)
898#else
899#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -0800900#endif
901
902static struct platform_driver tegra_i2c_driver = {
903 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500904 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -0800905 .driver = {
906 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +0000907 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200908 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -0800909 },
910};
911
912static int __init tegra_i2c_init_driver(void)
913{
914 return platform_driver_register(&tegra_i2c_driver);
915}
916
917static void __exit tegra_i2c_exit_driver(void)
918{
919 platform_driver_unregister(&tegra_i2c_driver);
920}
921
922subsys_initcall(tegra_i2c_init_driver);
923module_exit(tegra_i2c_exit_driver);
924
925MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
926MODULE_AUTHOR("Colin Cross");
927MODULE_LICENSE("GPL v2");