blob: b90bc326907d77bcb765c5c5009dca38d79b2a76 [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)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +053041#define I2C_CNFG_MULTI_MASTER_MODE (1<<17)
Todd Poynorcb63c622011-04-25 15:32:25 -060042#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080043#define I2C_SL_CNFG 0x020
Stephen Warren5afa9d32011-06-06 11:25:19 -060044#define I2C_SL_CNFG_NACK (1<<1)
Colin Crossdb811ca2011-02-20 17:14:21 -080045#define I2C_SL_CNFG_NEWSL (1<<2)
46#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060047#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080048#define I2C_TX_FIFO 0x050
49#define I2C_RX_FIFO 0x054
50#define I2C_PACKET_TRANSFER_STATUS 0x058
51#define I2C_FIFO_CONTROL 0x05c
52#define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
53#define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
54#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
55#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
56#define I2C_FIFO_STATUS 0x060
57#define I2C_FIFO_STATUS_TX_MASK 0xF0
58#define I2C_FIFO_STATUS_TX_SHIFT 4
59#define I2C_FIFO_STATUS_RX_MASK 0x0F
60#define I2C_FIFO_STATUS_RX_SHIFT 0
61#define I2C_INT_MASK 0x064
62#define I2C_INT_STATUS 0x068
63#define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
64#define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
65#define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
66#define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
67#define I2C_INT_NO_ACK (1<<3)
68#define I2C_INT_ARBITRATION_LOST (1<<2)
69#define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
70#define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
71#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053072#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
73#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080074
75#define DVC_CTRL_REG1 0x000
76#define DVC_CTRL_REG1_INTR_EN (1<<10)
77#define DVC_CTRL_REG2 0x004
78#define DVC_CTRL_REG3 0x008
79#define DVC_CTRL_REG3_SW_PROG (1<<26)
80#define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
81#define DVC_STATUS 0x00c
82#define DVC_STATUS_I2C_DONE_INTR (1<<30)
83
84#define I2C_ERR_NONE 0x00
85#define I2C_ERR_NO_ACK 0x01
86#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060087#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080088
89#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
90#define PACKET_HEADER0_PACKET_ID_SHIFT 16
91#define PACKET_HEADER0_CONT_ID_SHIFT 12
92#define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
93
94#define I2C_HEADER_HIGHSPEED_MODE (1<<22)
95#define I2C_HEADER_CONT_ON_NAK (1<<21)
96#define I2C_HEADER_SEND_START_BYTE (1<<20)
97#define I2C_HEADER_READ (1<<19)
98#define I2C_HEADER_10BIT_ADDR (1<<18)
99#define I2C_HEADER_IE_ENABLE (1<<17)
100#define I2C_HEADER_REPEAT_START (1<<16)
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530101#define I2C_HEADER_CONTINUE_XFER (1<<15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800102#define I2C_HEADER_MASTER_ADDR_SHIFT 12
103#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530104
105#define I2C_CONFIG_LOAD 0x08C
106#define I2C_MSTR_CONFIG_LOAD (1 << 0)
107#define I2C_SLV_CONFIG_LOAD (1 << 1)
108#define I2C_TIMEOUT_CONFIG_LOAD (1 << 2)
109
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530110#define I2C_CLKEN_OVERRIDE 0x090
111#define I2C_MST_CORE_CLKEN_OVR (1 << 0)
112
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530113/*
114 * msg_end_type: The bus control which need to be send at end of transfer.
115 * @MSG_END_STOP: Send stop pulse at end of transfer.
116 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
117 * @MSG_END_CONTINUE: The following on message is coming and so do not send
118 * stop or repeat start.
119 */
120enum msg_end_type {
121 MSG_END_STOP,
122 MSG_END_REPEAT_START,
123 MSG_END_CONTINUE,
124};
Colin Crossdb811ca2011-02-20 17:14:21 -0800125
126/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530127 * struct tegra_i2c_hw_feature : Different HW support on Tegra
128 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530129 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
130 * complete interrupt per packet basis.
131 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
132 * and earlier Socs has two clock sources i.e. div-clk and
133 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530134 * @has_config_load_reg: Has the config load register to load the new
135 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530136 * @clk_divisor_hs_mode: Clock divisor in HS mode.
137 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
138 * applicable if there is no fast clock source i.e. single clock
139 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530140 */
141
142struct tegra_i2c_hw_feature {
143 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530144 bool has_per_pkt_xfer_complete_irq;
145 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530146 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530147 int clk_divisor_hs_mode;
148 int clk_divisor_std_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530149 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530150 bool has_multi_master_mode;
151 bool has_slcg_override_reg;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530152};
153
154/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800155 * struct tegra_i2c_dev - per device i2c context
156 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530157 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800158 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530159 * @div_clk: clock reference for div clock of i2c controller.
160 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800161 * @base: ioremapped registers cookie
162 * @cont_id: i2c controller id, used for for packet header
163 * @irq: irq number of transfer complete interrupt
164 * @is_dvc: identifies the DVC i2c controller, has a different register layout
165 * @msg_complete: transfer completion notifier
166 * @msg_err: error code for completed message
167 * @msg_buf: pointer to current message data
168 * @msg_buf_remaining: size of unsent data in the message buffer
169 * @msg_read: identifies read transfers
170 * @bus_clk_rate: current i2c bus clock rate
171 * @is_suspended: prevents i2c controller accesses after suspend is called
172 */
173struct tegra_i2c_dev {
174 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530175 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800176 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530177 struct clk *div_clk;
178 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700179 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800180 void __iomem *base;
181 int cont_id;
182 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600183 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800184 int is_dvc;
185 struct completion msg_complete;
186 int msg_err;
187 u8 *msg_buf;
188 size_t msg_buf_remaining;
189 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000190 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530191 u16 clk_divisor_non_hs_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800192 bool is_suspended;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530193 bool is_multimaster_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800194};
195
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100196static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
197 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800198{
199 writel(val, i2c_dev->base + reg);
200}
201
202static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
203{
204 return readl(i2c_dev->base + reg);
205}
206
207/*
208 * i2c_writel and i2c_readl will offset the register if necessary to talk
209 * to the I2C block inside the DVC block
210 */
211static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
212 unsigned long reg)
213{
214 if (i2c_dev->is_dvc)
215 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
216 return reg;
217}
218
219static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
220 unsigned long reg)
221{
222 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530223
224 /* Read back register to make sure that register writes completed */
225 if (reg != I2C_TX_FIFO)
226 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800227}
228
229static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
230{
231 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
232}
233
234static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
235 unsigned long reg, int len)
236{
237 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
238}
239
240static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
241 unsigned long reg, int len)
242{
243 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
244}
245
246static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
247{
248 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
249 int_mask &= ~mask;
250 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
251}
252
253static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
254{
255 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
256 int_mask |= mask;
257 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
258}
259
260static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
261{
262 unsigned long timeout = jiffies + HZ;
263 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
264 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
265 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
266
267 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
268 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
269 if (time_after(jiffies, timeout)) {
270 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
271 return -ETIMEDOUT;
272 }
273 msleep(1);
274 }
275 return 0;
276}
277
278static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
279{
280 u32 val;
281 int rx_fifo_avail;
282 u8 *buf = i2c_dev->msg_buf;
283 size_t buf_remaining = i2c_dev->msg_buf_remaining;
284 int words_to_transfer;
285
286 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
287 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
288 I2C_FIFO_STATUS_RX_SHIFT;
289
290 /* Rounds down to not include partial word at the end of buf */
291 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
292 if (words_to_transfer > rx_fifo_avail)
293 words_to_transfer = rx_fifo_avail;
294
295 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
296
297 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
298 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
299 rx_fifo_avail -= words_to_transfer;
300
301 /*
302 * If there is a partial word at the end of buf, handle it manually to
303 * prevent overwriting past the end of buf
304 */
305 if (rx_fifo_avail > 0 && buf_remaining > 0) {
306 BUG_ON(buf_remaining > 3);
307 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300308 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800309 memcpy(buf, &val, buf_remaining);
310 buf_remaining = 0;
311 rx_fifo_avail--;
312 }
313
314 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
315 i2c_dev->msg_buf_remaining = buf_remaining;
316 i2c_dev->msg_buf = buf;
317 return 0;
318}
319
320static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
321{
322 u32 val;
323 int tx_fifo_avail;
324 u8 *buf = i2c_dev->msg_buf;
325 size_t buf_remaining = i2c_dev->msg_buf_remaining;
326 int words_to_transfer;
327
328 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
329 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
330 I2C_FIFO_STATUS_TX_SHIFT;
331
332 /* Rounds down to not include partial word at the end of buf */
333 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800334
Doug Anderson96219c32011-08-30 11:46:10 -0600335 /* It's very common to have < 4 bytes, so optimize that case. */
336 if (words_to_transfer) {
337 if (words_to_transfer > tx_fifo_avail)
338 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800339
Doug Anderson96219c32011-08-30 11:46:10 -0600340 /*
341 * Update state before writing to FIFO. If this casues us
342 * to finish writing all bytes (AKA buf_remaining goes to 0) we
343 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
344 * not maskable). We need to make sure that the isr sees
345 * buf_remaining as 0 and doesn't call us back re-entrantly.
346 */
347 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
348 tx_fifo_avail -= words_to_transfer;
349 i2c_dev->msg_buf_remaining = buf_remaining;
350 i2c_dev->msg_buf = buf +
351 words_to_transfer * BYTES_PER_FIFO_WORD;
352 barrier();
353
354 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
355
356 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
357 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800358
359 /*
360 * If there is a partial word at the end of buf, handle it manually to
361 * prevent reading past the end of buf, which could cross a page
362 * boundary and fault.
363 */
364 if (tx_fifo_avail > 0 && buf_remaining > 0) {
365 BUG_ON(buf_remaining > 3);
366 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300367 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600368
369 /* Again update before writing to FIFO to make sure isr sees. */
370 i2c_dev->msg_buf_remaining = 0;
371 i2c_dev->msg_buf = NULL;
372 barrier();
373
Colin Crossdb811ca2011-02-20 17:14:21 -0800374 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800375 }
376
Colin Crossdb811ca2011-02-20 17:14:21 -0800377 return 0;
378}
379
380/*
381 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
382 * block. This block is identical to the rest of the I2C blocks, except that
383 * it only supports master mode, it has registers moved around, and it needs
384 * some extra init to get it into I2C mode. The register moves are handled
385 * by i2c_readl and i2c_writel
386 */
387static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
388{
389 u32 val = 0;
390 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
391 val |= DVC_CTRL_REG3_SW_PROG;
392 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
393 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
394
395 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
396 val |= DVC_CTRL_REG1_INTR_EN;
397 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
398}
399
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530400static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
401{
402 int ret;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530403 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300404 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530405 if (ret < 0) {
406 dev_err(i2c_dev->dev,
407 "Enabling fast clk failed, err %d\n", ret);
408 return ret;
409 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530410 }
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300411 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530412 if (ret < 0) {
413 dev_err(i2c_dev->dev,
414 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300415 clk_disable(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530416 }
417 return ret;
418}
419
420static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
421{
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300422 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530423 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300424 clk_disable(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530425}
426
Colin Crossdb811ca2011-02-20 17:14:21 -0800427static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
428{
429 u32 val;
430 int err = 0;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530431 u32 clk_divisor;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530432 unsigned long timeout = jiffies + HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -0800433
Laxman Dewangan132c8032013-03-15 05:34:08 +0000434 err = tegra_i2c_clock_enable(i2c_dev);
435 if (err < 0) {
436 dev_err(i2c_dev->dev, "Clock enable failed %d\n", err);
437 return err;
438 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800439
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700440 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800441 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700442 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800443
444 if (i2c_dev->is_dvc)
445 tegra_dvc_init(i2c_dev);
446
Jay Cheng40abcf72011-04-25 15:32:27 -0600447 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
448 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530449
450 if (i2c_dev->hw->has_multi_master_mode)
451 val |= I2C_CNFG_MULTI_MASTER_MODE;
452
Colin Crossdb811ca2011-02-20 17:14:21 -0800453 i2c_writel(i2c_dev, val, I2C_CNFG);
454 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530455
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530456 /* Make sure clock divisor programmed correctly */
457 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530458 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530459 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
460 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800461
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600462 if (!i2c_dev->is_dvc) {
463 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Stephen Warren5afa9d32011-06-06 11:25:19 -0600464 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
465 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
466 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
467 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
468
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600469 }
470
Colin Crossdb811ca2011-02-20 17:14:21 -0800471 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
472 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
473 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
474
475 if (tegra_i2c_flush_fifos(i2c_dev))
476 err = -ETIMEDOUT;
477
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530478 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
479 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
480
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530481 if (i2c_dev->hw->has_config_load_reg) {
482 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
483 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
484 if (time_after(jiffies, timeout)) {
485 dev_warn(i2c_dev->dev,
486 "timeout waiting for config load\n");
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530487 err = -ETIMEDOUT;
488 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530489 }
490 msleep(1);
491 }
492 }
493
Todd Poynorcb63c622011-04-25 15:32:25 -0600494 if (i2c_dev->irq_disabled) {
495 i2c_dev->irq_disabled = 0;
496 enable_irq(i2c_dev->irq);
497 }
498
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530499err:
500 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800501 return err;
502}
503
504static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
505{
506 u32 status;
507 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
508 struct tegra_i2c_dev *i2c_dev = dev_id;
509
510 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
511
512 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600513 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
514 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
515 i2c_readl(i2c_dev, I2C_STATUS),
516 i2c_readl(i2c_dev, I2C_CNFG));
517 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
518
519 if (!i2c_dev->irq_disabled) {
520 disable_irq_nosync(i2c_dev->irq);
521 i2c_dev->irq_disabled = 1;
522 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600523 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800524 }
525
526 if (unlikely(status & status_err)) {
527 if (status & I2C_INT_NO_ACK)
528 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
529 if (status & I2C_INT_ARBITRATION_LOST)
530 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800531 goto err;
532 }
533
534 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
535 if (i2c_dev->msg_buf_remaining)
536 tegra_i2c_empty_rx_fifo(i2c_dev);
537 else
538 BUG();
539 }
540
541 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
542 if (i2c_dev->msg_buf_remaining)
543 tegra_i2c_fill_tx_fifo(i2c_dev);
544 else
545 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
546 }
547
Laxman Dewanganc889e912012-05-07 12:16:19 +0530548 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
549 if (i2c_dev->is_dvc)
550 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
551
Doug Anderson96219c32011-08-30 11:46:10 -0600552 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
553 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800554 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600555 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800556 return IRQ_HANDLED;
557err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300558 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800559 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
560 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
561 I2C_INT_RX_FIFO_DATA_REQ);
562 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600563 if (i2c_dev->is_dvc)
564 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530565
566 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800567 return IRQ_HANDLED;
568}
569
570static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530571 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800572{
573 u32 packet_header;
574 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500575 unsigned long time_left;
Colin Crossdb811ca2011-02-20 17:14:21 -0800576
577 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800578
579 if (msg->len == 0)
580 return -EINVAL;
581
582 i2c_dev->msg_buf = msg->buf;
583 i2c_dev->msg_buf_remaining = msg->len;
584 i2c_dev->msg_err = I2C_ERR_NONE;
585 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800586 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800587
588 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
589 PACKET_HEADER0_PROTOCOL_I2C |
590 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
591 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
592 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
593
594 packet_header = msg->len - 1;
595 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
596
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530597 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530598 if (end_state == MSG_END_CONTINUE)
599 packet_header |= I2C_HEADER_CONTINUE_XFER;
600 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600601 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530602 if (msg->flags & I2C_M_TEN) {
603 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800604 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530605 } else {
606 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
607 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800608 if (msg->flags & I2C_M_IGNORE_NAK)
609 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800610 if (msg->flags & I2C_M_RD)
611 packet_header |= I2C_HEADER_READ;
612 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
613
614 if (!(msg->flags & I2C_M_RD))
615 tegra_i2c_fill_tx_fifo(i2c_dev);
616
617 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530618 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
619 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800620 if (msg->flags & I2C_M_RD)
621 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
622 else if (i2c_dev->msg_buf_remaining)
623 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
624 tegra_i2c_unmask_irq(i2c_dev, int_mask);
625 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
626 i2c_readl(i2c_dev, I2C_INT_MASK));
627
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500628 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
629 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800630 tegra_i2c_mask_irq(i2c_dev, int_mask);
631
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500632 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800633 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
634
635 tegra_i2c_init(i2c_dev);
636 return -ETIMEDOUT;
637 }
638
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500639 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
640 time_left, completion_done(&i2c_dev->msg_complete),
641 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800642
643 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
644 return 0;
645
Alok Chauhanf70893d02012-04-02 11:23:02 +0530646 /*
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100647 * NACK interrupt is generated before the I2C controller generates
648 * the STOP condition on the bus. So wait for 2 clock periods
649 * before resetting the controller so that the STOP condition has
650 * been delivered properly.
Alok Chauhanf70893d02012-04-02 11:23:02 +0530651 */
652 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
653 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
654
Colin Crossdb811ca2011-02-20 17:14:21 -0800655 tegra_i2c_init(i2c_dev);
656 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
657 if (msg->flags & I2C_M_IGNORE_NAK)
658 return 0;
659 return -EREMOTEIO;
660 }
661
662 return -EIO;
663}
664
665static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
666 int num)
667{
668 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
669 int i;
670 int ret = 0;
671
672 if (i2c_dev->is_suspended)
673 return -EBUSY;
674
Laxman Dewangan132c8032013-03-15 05:34:08 +0000675 ret = tegra_i2c_clock_enable(i2c_dev);
676 if (ret < 0) {
677 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
678 return ret;
679 }
680
Colin Crossdb811ca2011-02-20 17:14:21 -0800681 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530682 enum msg_end_type end_type = MSG_END_STOP;
683 if (i < (num - 1)) {
684 if (msgs[i + 1].flags & I2C_M_NOSTART)
685 end_type = MSG_END_CONTINUE;
686 else
687 end_type = MSG_END_REPEAT_START;
688 }
689 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800690 if (ret)
691 break;
692 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530693 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800694 return ret ?: i;
695}
696
697static u32 tegra_i2c_func(struct i2c_adapter *adap)
698{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530699 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200700 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
701 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530702
703 if (i2c_dev->hw->has_continue_xfer_support)
704 ret |= I2C_FUNC_NOSTART;
705 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800706}
707
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530708static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
709{
710 struct device_node *np = i2c_dev->dev->of_node;
711 int ret;
712
713 ret = of_property_read_u32(np, "clock-frequency",
714 &i2c_dev->bus_clk_rate);
715 if (ret)
716 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
717
718 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
719 "multi-master");
720}
721
Colin Crossdb811ca2011-02-20 17:14:21 -0800722static const struct i2c_algorithm tegra_i2c_algo = {
723 .master_xfer = tegra_i2c_xfer,
724 .functionality = tegra_i2c_func,
725};
726
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200727/* payload size is only 12 bit */
728static struct i2c_adapter_quirks tegra_i2c_quirks = {
729 .max_read_len = 4096,
730 .max_write_len = 4096,
731};
732
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530733static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
734 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530735 .has_per_pkt_xfer_complete_irq = false,
736 .has_single_clk_source = false,
737 .clk_divisor_hs_mode = 3,
738 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530739 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530740 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530741 .has_multi_master_mode = false,
742 .has_slcg_override_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530743};
744
745static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
746 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530747 .has_per_pkt_xfer_complete_irq = false,
748 .has_single_clk_source = false,
749 .clk_divisor_hs_mode = 3,
750 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530751 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530752 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530753 .has_multi_master_mode = false,
754 .has_slcg_override_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530755};
756
757static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
758 .has_continue_xfer_support = true,
759 .has_per_pkt_xfer_complete_irq = true,
760 .has_single_clk_source = true,
761 .clk_divisor_hs_mode = 1,
762 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530763 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530764 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530765 .has_multi_master_mode = false,
766 .has_slcg_override_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530767};
768
769static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
770 .has_continue_xfer_support = true,
771 .has_per_pkt_xfer_complete_irq = true,
772 .has_single_clk_source = true,
773 .clk_divisor_hs_mode = 1,
774 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530775 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530776 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530777 .has_multi_master_mode = false,
778 .has_slcg_override_reg = true,
779};
780
781static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
782 .has_continue_xfer_support = true,
783 .has_per_pkt_xfer_complete_irq = true,
784 .has_single_clk_source = true,
785 .clk_divisor_hs_mode = 1,
786 .clk_divisor_std_fast_mode = 0x19,
787 .clk_divisor_fast_plus_mode = 0x10,
788 .has_config_load_reg = true,
789 .has_multi_master_mode = true,
790 .has_slcg_override_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530791};
792
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530793/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500794static const struct of_device_id tegra_i2c_of_match[] = {
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530795 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530796 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530797 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530798 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
799 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
800 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
801 {},
802};
803MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530804
Bill Pemberton0b255e92012-11-27 15:59:38 -0500805static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800806{
807 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800808 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530809 struct clk *div_clk;
810 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700811 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800812 int irq;
813 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300814 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800815
816 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100817 base = devm_ioremap_resource(&pdev->dev, res);
818 if (IS_ERR(base))
819 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800820
821 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
822 if (!res) {
823 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530824 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800825 }
826 irq = res->start;
827
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530828 div_clk = devm_clk_get(&pdev->dev, "div-clk");
829 if (IS_ERR(div_clk)) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800830 dev_err(&pdev->dev, "missing controller clock");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530831 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800832 }
833
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530834 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900835 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530836 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800837
838 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530839 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800840 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200841 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800842 i2c_dev->irq = irq;
843 i2c_dev->cont_id = pdev->id;
844 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700845
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700846 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
847 if (IS_ERR(i2c_dev->rst)) {
848 dev_err(&pdev->dev, "missing controller reset");
849 return PTR_ERR(i2c_dev->rst);
850 }
851
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530852 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800853
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530854 i2c_dev->hw = &tegra20_i2c_hw;
855
856 if (pdev->dev.of_node) {
Wolfram Sangda4753e2016-02-21 14:57:42 +0100857 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
Stephen Warren68fb6692011-12-17 23:29:30 -0700858 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
859 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530860 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800861 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530862 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800863 init_completion(&i2c_dev->msg_complete);
864
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530865 if (!i2c_dev->hw->has_single_clk_source) {
866 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
867 if (IS_ERR(fast_clk)) {
868 dev_err(&pdev->dev, "missing fast clock");
869 return PTR_ERR(fast_clk);
870 }
871 i2c_dev->fast_clk = fast_clk;
872 }
873
Colin Crossdb811ca2011-02-20 17:14:21 -0800874 platform_set_drvdata(pdev, i2c_dev);
875
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300876 if (!i2c_dev->hw->has_single_clk_source) {
877 ret = clk_prepare(i2c_dev->fast_clk);
878 if (ret < 0) {
879 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
880 return ret;
881 }
882 }
883
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530884 i2c_dev->clk_divisor_non_hs_mode =
885 i2c_dev->hw->clk_divisor_std_fast_mode;
886 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
887 (i2c_dev->bus_clk_rate == 1000000))
888 i2c_dev->clk_divisor_non_hs_mode =
889 i2c_dev->hw->clk_divisor_fast_plus_mode;
890
891 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300892 ret = clk_set_rate(i2c_dev->div_clk,
893 i2c_dev->bus_clk_rate * clk_multiplier);
894 if (ret) {
895 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
896 goto unprepare_fast_clk;
897 }
898
899 ret = clk_prepare(i2c_dev->div_clk);
900 if (ret < 0) {
901 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
902 goto unprepare_fast_clk;
903 }
904
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530905 if (i2c_dev->is_multimaster_mode) {
906 ret = clk_enable(i2c_dev->div_clk);
907 if (ret < 0) {
908 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
909 ret);
910 goto unprepare_div_clk;
911 }
912 }
913
Colin Crossdb811ca2011-02-20 17:14:21 -0800914 ret = tegra_i2c_init(i2c_dev);
915 if (ret) {
916 dev_err(&pdev->dev, "Failed to initialize i2c controller");
Jon Huntereab09982016-06-14 21:26:46 +0100917 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800918 }
919
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530920 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530921 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800922 if (ret) {
923 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530924 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800925 }
926
Colin Crossdb811ca2011-02-20 17:14:21 -0800927 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
928 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +0200929 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Colin Crossdb811ca2011-02-20 17:14:21 -0800930 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
931 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -0800932 i2c_dev->adapter.dev.parent = &pdev->dev;
933 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700934 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800935
936 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +0200937 if (ret)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530938 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800939
Colin Crossdb811ca2011-02-20 17:14:21 -0800940 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300941
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530942disable_div_clk:
943 if (i2c_dev->is_multimaster_mode)
944 clk_disable(i2c_dev->div_clk);
945
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300946unprepare_div_clk:
947 clk_unprepare(i2c_dev->div_clk);
948
949unprepare_fast_clk:
950 if (!i2c_dev->hw->has_single_clk_source)
951 clk_unprepare(i2c_dev->fast_clk);
952
953 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800954}
955
Bill Pemberton0b255e92012-11-27 15:59:38 -0500956static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800957{
958 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
959 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300960
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530961 if (i2c_dev->is_multimaster_mode)
962 clk_disable(i2c_dev->div_clk);
963
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300964 clk_unprepare(i2c_dev->div_clk);
965 if (!i2c_dev->hw->has_single_clk_source)
966 clk_unprepare(i2c_dev->fast_clk);
967
Colin Crossdb811ca2011-02-20 17:14:21 -0800968 return 0;
969}
970
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530971#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200972static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800973{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200974 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800975
976 i2c_lock_adapter(&i2c_dev->adapter);
977 i2c_dev->is_suspended = true;
978 i2c_unlock_adapter(&i2c_dev->adapter);
979
980 return 0;
981}
982
Wolfram Sang5db20c42012-07-24 17:32:45 +0200983static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800984{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200985 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800986 int ret;
987
988 i2c_lock_adapter(&i2c_dev->adapter);
989
990 ret = tegra_i2c_init(i2c_dev);
991
992 if (ret) {
993 i2c_unlock_adapter(&i2c_dev->adapter);
994 return ret;
995 }
996
997 i2c_dev->is_suspended = false;
998
999 i2c_unlock_adapter(&i2c_dev->adapter);
1000
1001 return 0;
1002}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001003
Wolfram Sang5db20c42012-07-24 17:32:45 +02001004static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001005#define TEGRA_I2C_PM (&tegra_i2c_pm)
1006#else
1007#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001008#endif
1009
1010static struct platform_driver tegra_i2c_driver = {
1011 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001012 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001013 .driver = {
1014 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001015 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001016 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001017 },
1018};
1019
1020static int __init tegra_i2c_init_driver(void)
1021{
1022 return platform_driver_register(&tegra_i2c_driver);
1023}
1024
1025static void __exit tegra_i2c_exit_driver(void)
1026{
1027 platform_driver_unregister(&tegra_i2c_driver);
1028}
1029
1030subsys_initcall(tegra_i2c_init_driver);
1031module_exit(tegra_i2c_exit_driver);
1032
1033MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1034MODULE_AUTHOR("Colin Cross");
1035MODULE_LICENSE("GPL v2");