blob: 7f31a103c04a36cd8b26b6bc50412126bec0c0d3 [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
Jon Hunter2929be22016-08-26 14:08:58 +010039#define I2C_CNFG_PACKET_MODE_EN BIT(10)
40#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
41#define I2C_CNFG_MULTI_MASTER_MODE BIT(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
Jon Hunter2929be22016-08-26 14:08:58 +010044#define I2C_SL_CNFG_NACK BIT(1)
45#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080046#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
Jon Hunter2929be22016-08-26 14:08:58 +010052#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
53#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080054#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
Jon Hunter2929be22016-08-26 14:08:58 +010063#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
64#define I2C_INT_ALL_PACKETS_XFER_COMPLETE BIT(6)
65#define I2C_INT_TX_FIFO_OVERFLOW BIT(5)
66#define I2C_INT_RX_FIFO_UNDERFLOW BIT(4)
67#define I2C_INT_NO_ACK BIT(3)
68#define I2C_INT_ARBITRATION_LOST BIT(2)
69#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
70#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080071#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
Jon Hunter2929be22016-08-26 14:08:58 +010076#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080077#define DVC_CTRL_REG2 0x004
78#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010079#define DVC_CTRL_REG3_SW_PROG BIT(26)
80#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080081#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010082#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080083
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
Jon Hunter2929be22016-08-26 14:08:58 +010092#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080093
Jon Hunter2929be22016-08-26 14:08:58 +010094#define I2C_HEADER_HIGHSPEED_MODE BIT(22)
95#define I2C_HEADER_CONT_ON_NAK BIT(21)
96#define I2C_HEADER_SEND_START_BYTE BIT(20)
97#define I2C_HEADER_READ BIT(19)
98#define I2C_HEADER_10BIT_ADDR BIT(18)
99#define I2C_HEADER_IE_ENABLE BIT(17)
100#define I2C_HEADER_REPEAT_START BIT(16)
101#define I2C_HEADER_CONTINUE_XFER BIT(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
Jon Hunter2929be22016-08-26 14:08:58 +0100106#define I2C_MSTR_CONFIG_LOAD BIT(0)
107#define I2C_SLV_CONFIG_LOAD BIT(1)
108#define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530109
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530110#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100111#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530112
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{
Jon Hunterf5076682016-08-26 14:08:59 +0100248 u32 int_mask;
249
250 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800251 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
252}
253
254static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
255{
Jon Hunterf5076682016-08-26 14:08:59 +0100256 u32 int_mask;
257
258 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800259 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
260}
261
262static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
263{
264 unsigned long timeout = jiffies + HZ;
265 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
Jon Hunterf5076682016-08-26 14:08:59 +0100266
Colin Crossdb811ca2011-02-20 17:14:21 -0800267 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
268 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
269
270 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
271 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
272 if (time_after(jiffies, timeout)) {
273 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
274 return -ETIMEDOUT;
275 }
276 msleep(1);
277 }
278 return 0;
279}
280
281static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
282{
283 u32 val;
284 int rx_fifo_avail;
285 u8 *buf = i2c_dev->msg_buf;
286 size_t buf_remaining = i2c_dev->msg_buf_remaining;
287 int words_to_transfer;
288
289 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
290 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
291 I2C_FIFO_STATUS_RX_SHIFT;
292
293 /* Rounds down to not include partial word at the end of buf */
294 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
295 if (words_to_transfer > rx_fifo_avail)
296 words_to_transfer = rx_fifo_avail;
297
298 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
299
300 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
301 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
302 rx_fifo_avail -= words_to_transfer;
303
304 /*
305 * If there is a partial word at the end of buf, handle it manually to
306 * prevent overwriting past the end of buf
307 */
308 if (rx_fifo_avail > 0 && buf_remaining > 0) {
309 BUG_ON(buf_remaining > 3);
310 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300311 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800312 memcpy(buf, &val, buf_remaining);
313 buf_remaining = 0;
314 rx_fifo_avail--;
315 }
316
317 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
318 i2c_dev->msg_buf_remaining = buf_remaining;
319 i2c_dev->msg_buf = buf;
320 return 0;
321}
322
323static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
324{
325 u32 val;
326 int tx_fifo_avail;
327 u8 *buf = i2c_dev->msg_buf;
328 size_t buf_remaining = i2c_dev->msg_buf_remaining;
329 int words_to_transfer;
330
331 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
332 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
333 I2C_FIFO_STATUS_TX_SHIFT;
334
335 /* Rounds down to not include partial word at the end of buf */
336 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800337
Doug Anderson96219c32011-08-30 11:46:10 -0600338 /* It's very common to have < 4 bytes, so optimize that case. */
339 if (words_to_transfer) {
340 if (words_to_transfer > tx_fifo_avail)
341 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800342
Doug Anderson96219c32011-08-30 11:46:10 -0600343 /*
344 * Update state before writing to FIFO. If this casues us
345 * to finish writing all bytes (AKA buf_remaining goes to 0) we
346 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
347 * not maskable). We need to make sure that the isr sees
348 * buf_remaining as 0 and doesn't call us back re-entrantly.
349 */
350 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
351 tx_fifo_avail -= words_to_transfer;
352 i2c_dev->msg_buf_remaining = buf_remaining;
353 i2c_dev->msg_buf = buf +
354 words_to_transfer * BYTES_PER_FIFO_WORD;
355 barrier();
356
357 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
358
359 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
360 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800361
362 /*
363 * If there is a partial word at the end of buf, handle it manually to
364 * prevent reading past the end of buf, which could cross a page
365 * boundary and fault.
366 */
367 if (tx_fifo_avail > 0 && buf_remaining > 0) {
368 BUG_ON(buf_remaining > 3);
369 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300370 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600371
372 /* Again update before writing to FIFO to make sure isr sees. */
373 i2c_dev->msg_buf_remaining = 0;
374 i2c_dev->msg_buf = NULL;
375 barrier();
376
Colin Crossdb811ca2011-02-20 17:14:21 -0800377 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800378 }
379
Colin Crossdb811ca2011-02-20 17:14:21 -0800380 return 0;
381}
382
383/*
384 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
385 * block. This block is identical to the rest of the I2C blocks, except that
386 * it only supports master mode, it has registers moved around, and it needs
387 * some extra init to get it into I2C mode. The register moves are handled
388 * by i2c_readl and i2c_writel
389 */
390static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
391{
Jon Hunterf5076682016-08-26 14:08:59 +0100392 u32 val;
393
Colin Crossdb811ca2011-02-20 17:14:21 -0800394 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
395 val |= DVC_CTRL_REG3_SW_PROG;
396 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
397 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
398
399 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
400 val |= DVC_CTRL_REG1_INTR_EN;
401 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
402}
403
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530404static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
405{
406 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100407
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530408 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300409 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530410 if (ret < 0) {
411 dev_err(i2c_dev->dev,
412 "Enabling fast clk failed, err %d\n", ret);
413 return ret;
414 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530415 }
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300416 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530417 if (ret < 0) {
418 dev_err(i2c_dev->dev,
419 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300420 clk_disable(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530421 }
422 return ret;
423}
424
425static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
426{
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300427 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530428 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300429 clk_disable(i2c_dev->fast_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530430}
431
Colin Crossdb811ca2011-02-20 17:14:21 -0800432static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
433{
434 u32 val;
435 int err = 0;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530436 u32 clk_divisor;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530437 unsigned long timeout = jiffies + HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -0800438
Laxman Dewangan132c8032013-03-15 05:34:08 +0000439 err = tegra_i2c_clock_enable(i2c_dev);
440 if (err < 0) {
441 dev_err(i2c_dev->dev, "Clock enable failed %d\n", err);
442 return err;
443 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800444
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700445 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800446 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700447 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800448
449 if (i2c_dev->is_dvc)
450 tegra_dvc_init(i2c_dev);
451
Jay Cheng40abcf72011-04-25 15:32:27 -0600452 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
453 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530454
455 if (i2c_dev->hw->has_multi_master_mode)
456 val |= I2C_CNFG_MULTI_MASTER_MODE;
457
Colin Crossdb811ca2011-02-20 17:14:21 -0800458 i2c_writel(i2c_dev, val, I2C_CNFG);
459 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530460
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530461 /* Make sure clock divisor programmed correctly */
462 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530463 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530464 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
465 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800466
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600467 if (!i2c_dev->is_dvc) {
468 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100469
Stephen Warren5afa9d32011-06-06 11:25:19 -0600470 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
471 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
472 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
473 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600474 }
475
Colin Crossdb811ca2011-02-20 17:14:21 -0800476 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
477 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
478 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
479
480 if (tegra_i2c_flush_fifos(i2c_dev))
481 err = -ETIMEDOUT;
482
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530483 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
484 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
485
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530486 if (i2c_dev->hw->has_config_load_reg) {
487 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
488 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
489 if (time_after(jiffies, timeout)) {
490 dev_warn(i2c_dev->dev,
491 "timeout waiting for config load\n");
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530492 err = -ETIMEDOUT;
493 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530494 }
495 msleep(1);
496 }
497 }
498
Todd Poynorcb63c622011-04-25 15:32:25 -0600499 if (i2c_dev->irq_disabled) {
500 i2c_dev->irq_disabled = 0;
501 enable_irq(i2c_dev->irq);
502 }
503
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530504err:
505 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800506 return err;
507}
508
509static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
510{
511 u32 status;
512 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
513 struct tegra_i2c_dev *i2c_dev = dev_id;
514
515 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
516
517 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600518 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
519 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
520 i2c_readl(i2c_dev, I2C_STATUS),
521 i2c_readl(i2c_dev, I2C_CNFG));
522 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
523
524 if (!i2c_dev->irq_disabled) {
525 disable_irq_nosync(i2c_dev->irq);
526 i2c_dev->irq_disabled = 1;
527 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600528 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800529 }
530
531 if (unlikely(status & status_err)) {
532 if (status & I2C_INT_NO_ACK)
533 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
534 if (status & I2C_INT_ARBITRATION_LOST)
535 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800536 goto err;
537 }
538
539 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
540 if (i2c_dev->msg_buf_remaining)
541 tegra_i2c_empty_rx_fifo(i2c_dev);
542 else
543 BUG();
544 }
545
546 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
547 if (i2c_dev->msg_buf_remaining)
548 tegra_i2c_fill_tx_fifo(i2c_dev);
549 else
550 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
551 }
552
Laxman Dewanganc889e912012-05-07 12:16:19 +0530553 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
554 if (i2c_dev->is_dvc)
555 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
556
Doug Anderson96219c32011-08-30 11:46:10 -0600557 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
558 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800559 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600560 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800561 return IRQ_HANDLED;
562err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300563 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800564 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
565 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
566 I2C_INT_RX_FIFO_DATA_REQ);
567 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600568 if (i2c_dev->is_dvc)
569 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530570
571 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800572 return IRQ_HANDLED;
573}
574
575static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530576 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800577{
578 u32 packet_header;
579 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500580 unsigned long time_left;
Colin Crossdb811ca2011-02-20 17:14:21 -0800581
582 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800583
584 if (msg->len == 0)
585 return -EINVAL;
586
587 i2c_dev->msg_buf = msg->buf;
588 i2c_dev->msg_buf_remaining = msg->len;
589 i2c_dev->msg_err = I2C_ERR_NONE;
590 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800591 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800592
593 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
594 PACKET_HEADER0_PROTOCOL_I2C |
595 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
596 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
597 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
598
599 packet_header = msg->len - 1;
600 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
601
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530602 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530603 if (end_state == MSG_END_CONTINUE)
604 packet_header |= I2C_HEADER_CONTINUE_XFER;
605 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600606 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530607 if (msg->flags & I2C_M_TEN) {
608 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800609 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530610 } else {
611 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
612 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800613 if (msg->flags & I2C_M_IGNORE_NAK)
614 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800615 if (msg->flags & I2C_M_RD)
616 packet_header |= I2C_HEADER_READ;
617 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
618
619 if (!(msg->flags & I2C_M_RD))
620 tegra_i2c_fill_tx_fifo(i2c_dev);
621
622 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530623 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
624 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800625 if (msg->flags & I2C_M_RD)
626 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
627 else if (i2c_dev->msg_buf_remaining)
628 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
629 tegra_i2c_unmask_irq(i2c_dev, int_mask);
630 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
631 i2c_readl(i2c_dev, I2C_INT_MASK));
632
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500633 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
634 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800635 tegra_i2c_mask_irq(i2c_dev, int_mask);
636
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500637 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800638 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
639
640 tegra_i2c_init(i2c_dev);
641 return -ETIMEDOUT;
642 }
643
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500644 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
645 time_left, completion_done(&i2c_dev->msg_complete),
646 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800647
648 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
649 return 0;
650
Alok Chauhanf70893d02012-04-02 11:23:02 +0530651 /*
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100652 * NACK interrupt is generated before the I2C controller generates
653 * the STOP condition on the bus. So wait for 2 clock periods
654 * before resetting the controller so that the STOP condition has
655 * been delivered properly.
Alok Chauhanf70893d02012-04-02 11:23:02 +0530656 */
657 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
658 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
659
Colin Crossdb811ca2011-02-20 17:14:21 -0800660 tegra_i2c_init(i2c_dev);
661 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
662 if (msg->flags & I2C_M_IGNORE_NAK)
663 return 0;
664 return -EREMOTEIO;
665 }
666
667 return -EIO;
668}
669
670static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
671 int num)
672{
673 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
674 int i;
675 int ret = 0;
676
677 if (i2c_dev->is_suspended)
678 return -EBUSY;
679
Laxman Dewangan132c8032013-03-15 05:34:08 +0000680 ret = tegra_i2c_clock_enable(i2c_dev);
681 if (ret < 0) {
682 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
683 return ret;
684 }
685
Colin Crossdb811ca2011-02-20 17:14:21 -0800686 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530687 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +0100688
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530689 if (i < (num - 1)) {
690 if (msgs[i + 1].flags & I2C_M_NOSTART)
691 end_type = MSG_END_CONTINUE;
692 else
693 end_type = MSG_END_REPEAT_START;
694 }
695 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800696 if (ret)
697 break;
698 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530699 tegra_i2c_clock_disable(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800700 return ret ?: i;
701}
702
703static u32 tegra_i2c_func(struct i2c_adapter *adap)
704{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530705 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200706 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
707 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530708
709 if (i2c_dev->hw->has_continue_xfer_support)
710 ret |= I2C_FUNC_NOSTART;
711 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800712}
713
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530714static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
715{
716 struct device_node *np = i2c_dev->dev->of_node;
717 int ret;
718
719 ret = of_property_read_u32(np, "clock-frequency",
720 &i2c_dev->bus_clk_rate);
721 if (ret)
722 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
723
724 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
725 "multi-master");
726}
727
Colin Crossdb811ca2011-02-20 17:14:21 -0800728static const struct i2c_algorithm tegra_i2c_algo = {
729 .master_xfer = tegra_i2c_xfer,
730 .functionality = tegra_i2c_func,
731};
732
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200733/* payload size is only 12 bit */
734static struct i2c_adapter_quirks tegra_i2c_quirks = {
735 .max_read_len = 4096,
736 .max_write_len = 4096,
737};
738
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530739static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
740 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530741 .has_per_pkt_xfer_complete_irq = false,
742 .has_single_clk_source = false,
743 .clk_divisor_hs_mode = 3,
744 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530745 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530746 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530747 .has_multi_master_mode = false,
748 .has_slcg_override_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530749};
750
751static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
752 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530753 .has_per_pkt_xfer_complete_irq = false,
754 .has_single_clk_source = false,
755 .clk_divisor_hs_mode = 3,
756 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530757 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530758 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530759 .has_multi_master_mode = false,
760 .has_slcg_override_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530761};
762
763static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
764 .has_continue_xfer_support = true,
765 .has_per_pkt_xfer_complete_irq = true,
766 .has_single_clk_source = true,
767 .clk_divisor_hs_mode = 1,
768 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530769 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530770 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530771 .has_multi_master_mode = false,
772 .has_slcg_override_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530773};
774
775static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
776 .has_continue_xfer_support = true,
777 .has_per_pkt_xfer_complete_irq = true,
778 .has_single_clk_source = true,
779 .clk_divisor_hs_mode = 1,
780 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530781 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530782 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530783 .has_multi_master_mode = false,
784 .has_slcg_override_reg = true,
785};
786
787static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
788 .has_continue_xfer_support = true,
789 .has_per_pkt_xfer_complete_irq = true,
790 .has_single_clk_source = true,
791 .clk_divisor_hs_mode = 1,
792 .clk_divisor_std_fast_mode = 0x19,
793 .clk_divisor_fast_plus_mode = 0x10,
794 .has_config_load_reg = true,
795 .has_multi_master_mode = true,
796 .has_slcg_override_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530797};
798
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530799/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500800static const struct of_device_id tegra_i2c_of_match[] = {
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530801 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530802 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530803 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530804 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
805 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
806 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
807 {},
808};
809MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530810
Bill Pemberton0b255e92012-11-27 15:59:38 -0500811static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800812{
813 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800814 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530815 struct clk *div_clk;
816 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700817 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800818 int irq;
819 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300820 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800821
822 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100823 base = devm_ioremap_resource(&pdev->dev, res);
824 if (IS_ERR(base))
825 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800826
827 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
828 if (!res) {
829 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530830 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800831 }
832 irq = res->start;
833
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530834 div_clk = devm_clk_get(&pdev->dev, "div-clk");
835 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100836 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530837 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800838 }
839
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530840 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900841 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530842 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800843
844 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530845 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800846 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200847 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800848 i2c_dev->irq = irq;
849 i2c_dev->cont_id = pdev->id;
850 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700851
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700852 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
853 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100854 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700855 return PTR_ERR(i2c_dev->rst);
856 }
857
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530858 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800859
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530860 i2c_dev->hw = &tegra20_i2c_hw;
861
862 if (pdev->dev.of_node) {
Wolfram Sangda4753e2016-02-21 14:57:42 +0100863 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
Stephen Warren68fb6692011-12-17 23:29:30 -0700864 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
865 "nvidia,tegra20-i2c-dvc");
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530866 } else if (pdev->id == 3) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800867 i2c_dev->is_dvc = 1;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530868 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800869 init_completion(&i2c_dev->msg_complete);
870
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530871 if (!i2c_dev->hw->has_single_clk_source) {
872 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
873 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100874 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530875 return PTR_ERR(fast_clk);
876 }
877 i2c_dev->fast_clk = fast_clk;
878 }
879
Colin Crossdb811ca2011-02-20 17:14:21 -0800880 platform_set_drvdata(pdev, i2c_dev);
881
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300882 if (!i2c_dev->hw->has_single_clk_source) {
883 ret = clk_prepare(i2c_dev->fast_clk);
884 if (ret < 0) {
885 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
886 return ret;
887 }
888 }
889
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530890 i2c_dev->clk_divisor_non_hs_mode =
891 i2c_dev->hw->clk_divisor_std_fast_mode;
892 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
893 (i2c_dev->bus_clk_rate == 1000000))
894 i2c_dev->clk_divisor_non_hs_mode =
895 i2c_dev->hw->clk_divisor_fast_plus_mode;
896
897 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300898 ret = clk_set_rate(i2c_dev->div_clk,
899 i2c_dev->bus_clk_rate * clk_multiplier);
900 if (ret) {
901 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
902 goto unprepare_fast_clk;
903 }
904
905 ret = clk_prepare(i2c_dev->div_clk);
906 if (ret < 0) {
907 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
908 goto unprepare_fast_clk;
909 }
910
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530911 if (i2c_dev->is_multimaster_mode) {
912 ret = clk_enable(i2c_dev->div_clk);
913 if (ret < 0) {
914 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
915 ret);
916 goto unprepare_div_clk;
917 }
918 }
919
Colin Crossdb811ca2011-02-20 17:14:21 -0800920 ret = tegra_i2c_init(i2c_dev);
921 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100922 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Jon Huntereab09982016-06-14 21:26:46 +0100923 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800924 }
925
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530926 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530927 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800928 if (ret) {
929 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530930 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800931 }
932
Colin Crossdb811ca2011-02-20 17:14:21 -0800933 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
934 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +0200935 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Colin Crossdb811ca2011-02-20 17:14:21 -0800936 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
937 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -0800938 i2c_dev->adapter.dev.parent = &pdev->dev;
939 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700940 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800941
942 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +0200943 if (ret)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530944 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800945
Colin Crossdb811ca2011-02-20 17:14:21 -0800946 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300947
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530948disable_div_clk:
949 if (i2c_dev->is_multimaster_mode)
950 clk_disable(i2c_dev->div_clk);
951
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300952unprepare_div_clk:
953 clk_unprepare(i2c_dev->div_clk);
954
955unprepare_fast_clk:
956 if (!i2c_dev->hw->has_single_clk_source)
957 clk_unprepare(i2c_dev->fast_clk);
958
959 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800960}
961
Bill Pemberton0b255e92012-11-27 15:59:38 -0500962static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800963{
964 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +0100965
Colin Crossdb811ca2011-02-20 17:14:21 -0800966 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300967
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530968 if (i2c_dev->is_multimaster_mode)
969 clk_disable(i2c_dev->div_clk);
970
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300971 clk_unprepare(i2c_dev->div_clk);
972 if (!i2c_dev->hw->has_single_clk_source)
973 clk_unprepare(i2c_dev->fast_clk);
974
Colin Crossdb811ca2011-02-20 17:14:21 -0800975 return 0;
976}
977
Laxman Dewangan371e67c2012-08-18 17:49:58 +0530978#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +0200979static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800980{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200981 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800982
983 i2c_lock_adapter(&i2c_dev->adapter);
984 i2c_dev->is_suspended = true;
985 i2c_unlock_adapter(&i2c_dev->adapter);
986
987 return 0;
988}
989
Wolfram Sang5db20c42012-07-24 17:32:45 +0200990static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800991{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +0200992 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800993 int ret;
994
995 i2c_lock_adapter(&i2c_dev->adapter);
996
997 ret = tegra_i2c_init(i2c_dev);
998
999 if (ret) {
1000 i2c_unlock_adapter(&i2c_dev->adapter);
1001 return ret;
1002 }
1003
1004 i2c_dev->is_suspended = false;
1005
1006 i2c_unlock_adapter(&i2c_dev->adapter);
1007
1008 return 0;
1009}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001010
Wolfram Sang5db20c42012-07-24 17:32:45 +02001011static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume);
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001012#define TEGRA_I2C_PM (&tegra_i2c_pm)
1013#else
1014#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001015#endif
1016
1017static struct platform_driver tegra_i2c_driver = {
1018 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001019 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001020 .driver = {
1021 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001022 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001023 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001024 },
1025};
1026
1027static int __init tegra_i2c_init_driver(void)
1028{
1029 return platform_driver_register(&tegra_i2c_driver);
1030}
1031
1032static void __exit tegra_i2c_exit_driver(void)
1033{
1034 platform_driver_unregister(&tegra_i2c_driver);
1035}
1036
1037subsys_initcall(tegra_i2c_init_driver);
1038module_exit(tegra_i2c_exit_driver);
1039
1040MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1041MODULE_AUTHOR("Colin Cross");
1042MODULE_LICENSE("GPL v2");