blob: d86a993b75d64ad7a73de08814d10e20c82ddc5f [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>
Jon Hunter718917b2016-08-26 14:09:05 +010031#include <linux/pinctrl/consumer.h>
Jon Hunter1f50ad22016-08-26 14:09:04 +010032#include <linux/pm_runtime.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080033
34#include <asm/unaligned.h>
35
Colin Crossdb811ca2011-02-20 17:14:21 -080036#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
37#define BYTES_PER_FIFO_WORD 4
38
39#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060040#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010041#define I2C_CNFG_PACKET_MODE_EN BIT(10)
42#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
43#define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
Todd Poynorcb63c622011-04-25 15:32:25 -060044#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080045#define I2C_SL_CNFG 0x020
Jon Hunter2929be22016-08-26 14:08:58 +010046#define I2C_SL_CNFG_NACK BIT(1)
47#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080048#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060049#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080050#define I2C_TX_FIFO 0x050
51#define I2C_RX_FIFO 0x054
52#define I2C_PACKET_TRANSFER_STATUS 0x058
53#define I2C_FIFO_CONTROL 0x05c
Jon Hunter2929be22016-08-26 14:08:58 +010054#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
55#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080056#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
57#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
58#define I2C_FIFO_STATUS 0x060
59#define I2C_FIFO_STATUS_TX_MASK 0xF0
60#define I2C_FIFO_STATUS_TX_SHIFT 4
61#define I2C_FIFO_STATUS_RX_MASK 0x0F
62#define I2C_FIFO_STATUS_RX_SHIFT 0
63#define I2C_INT_MASK 0x064
64#define I2C_INT_STATUS 0x068
Jon Hunter2929be22016-08-26 14:08:58 +010065#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
66#define I2C_INT_ALL_PACKETS_XFER_COMPLETE BIT(6)
67#define I2C_INT_TX_FIFO_OVERFLOW BIT(5)
68#define I2C_INT_RX_FIFO_UNDERFLOW BIT(4)
69#define I2C_INT_NO_ACK BIT(3)
70#define I2C_INT_ARBITRATION_LOST BIT(2)
71#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
72#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080073#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053074#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
75#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080076
77#define DVC_CTRL_REG1 0x000
Jon Hunter2929be22016-08-26 14:08:58 +010078#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080079#define DVC_CTRL_REG2 0x004
80#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010081#define DVC_CTRL_REG3_SW_PROG BIT(26)
82#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080083#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010084#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080085
86#define I2C_ERR_NONE 0x00
87#define I2C_ERR_NO_ACK 0x01
88#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060089#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080090
91#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
92#define PACKET_HEADER0_PACKET_ID_SHIFT 16
93#define PACKET_HEADER0_CONT_ID_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010094#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080095
Jon Hunter2929be22016-08-26 14:08:58 +010096#define I2C_HEADER_HIGHSPEED_MODE BIT(22)
97#define I2C_HEADER_CONT_ON_NAK BIT(21)
98#define I2C_HEADER_SEND_START_BYTE BIT(20)
99#define I2C_HEADER_READ BIT(19)
100#define I2C_HEADER_10BIT_ADDR BIT(18)
101#define I2C_HEADER_IE_ENABLE BIT(17)
102#define I2C_HEADER_REPEAT_START BIT(16)
103#define I2C_HEADER_CONTINUE_XFER BIT(15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800104#define I2C_HEADER_MASTER_ADDR_SHIFT 12
105#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530106
107#define I2C_CONFIG_LOAD 0x08C
Jon Hunter2929be22016-08-26 14:08:58 +0100108#define I2C_MSTR_CONFIG_LOAD BIT(0)
109#define I2C_SLV_CONFIG_LOAD BIT(1)
110#define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530111
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530112#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100113#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530114
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530115/*
116 * msg_end_type: The bus control which need to be send at end of transfer.
117 * @MSG_END_STOP: Send stop pulse at end of transfer.
118 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
119 * @MSG_END_CONTINUE: The following on message is coming and so do not send
120 * stop or repeat start.
121 */
122enum msg_end_type {
123 MSG_END_STOP,
124 MSG_END_REPEAT_START,
125 MSG_END_CONTINUE,
126};
Colin Crossdb811ca2011-02-20 17:14:21 -0800127
128/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530129 * struct tegra_i2c_hw_feature : Different HW support on Tegra
130 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530131 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
132 * complete interrupt per packet basis.
133 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
134 * and earlier Socs has two clock sources i.e. div-clk and
135 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530136 * @has_config_load_reg: Has the config load register to load the new
137 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530138 * @clk_divisor_hs_mode: Clock divisor in HS mode.
139 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
140 * applicable if there is no fast clock source i.e. single clock
141 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530142 */
143
144struct tegra_i2c_hw_feature {
145 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530146 bool has_per_pkt_xfer_complete_irq;
147 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530148 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530149 int clk_divisor_hs_mode;
150 int clk_divisor_std_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530151 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530152 bool has_multi_master_mode;
153 bool has_slcg_override_reg;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530154};
155
156/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800157 * struct tegra_i2c_dev - per device i2c context
158 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530159 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800160 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530161 * @div_clk: clock reference for div clock of i2c controller.
162 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800163 * @base: ioremapped registers cookie
164 * @cont_id: i2c controller id, used for for packet header
165 * @irq: irq number of transfer complete interrupt
166 * @is_dvc: identifies the DVC i2c controller, has a different register layout
167 * @msg_complete: transfer completion notifier
168 * @msg_err: error code for completed message
169 * @msg_buf: pointer to current message data
170 * @msg_buf_remaining: size of unsent data in the message buffer
171 * @msg_read: identifies read transfers
172 * @bus_clk_rate: current i2c bus clock rate
173 * @is_suspended: prevents i2c controller accesses after suspend is called
174 */
175struct tegra_i2c_dev {
176 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530177 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800178 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530179 struct clk *div_clk;
180 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700181 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800182 void __iomem *base;
183 int cont_id;
184 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600185 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800186 int is_dvc;
187 struct completion msg_complete;
188 int msg_err;
189 u8 *msg_buf;
190 size_t msg_buf_remaining;
191 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000192 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530193 u16 clk_divisor_non_hs_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800194 bool is_suspended;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530195 bool is_multimaster_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800196};
197
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100198static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
199 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800200{
201 writel(val, i2c_dev->base + reg);
202}
203
204static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
205{
206 return readl(i2c_dev->base + reg);
207}
208
209/*
210 * i2c_writel and i2c_readl will offset the register if necessary to talk
211 * to the I2C block inside the DVC block
212 */
213static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
214 unsigned long reg)
215{
216 if (i2c_dev->is_dvc)
217 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
218 return reg;
219}
220
221static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
222 unsigned long reg)
223{
224 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530225
226 /* Read back register to make sure that register writes completed */
227 if (reg != I2C_TX_FIFO)
228 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800229}
230
231static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
232{
233 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
234}
235
236static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
237 unsigned long reg, int len)
238{
239 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
240}
241
242static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
243 unsigned long reg, int len)
244{
245 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
246}
247
248static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
249{
Jon Hunterf5076682016-08-26 14:08:59 +0100250 u32 int_mask;
251
252 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800253 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
254}
255
256static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
257{
Jon Hunterf5076682016-08-26 14:08:59 +0100258 u32 int_mask;
259
260 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800261 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
262}
263
264static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
265{
266 unsigned long timeout = jiffies + HZ;
267 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
Jon Hunterf5076682016-08-26 14:08:59 +0100268
Colin Crossdb811ca2011-02-20 17:14:21 -0800269 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
270 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
271
272 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
273 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
274 if (time_after(jiffies, timeout)) {
275 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
276 return -ETIMEDOUT;
277 }
278 msleep(1);
279 }
280 return 0;
281}
282
283static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
284{
285 u32 val;
286 int rx_fifo_avail;
287 u8 *buf = i2c_dev->msg_buf;
288 size_t buf_remaining = i2c_dev->msg_buf_remaining;
289 int words_to_transfer;
290
291 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
292 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
293 I2C_FIFO_STATUS_RX_SHIFT;
294
295 /* Rounds down to not include partial word at the end of buf */
296 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
297 if (words_to_transfer > rx_fifo_avail)
298 words_to_transfer = rx_fifo_avail;
299
300 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
301
302 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
303 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
304 rx_fifo_avail -= words_to_transfer;
305
306 /*
307 * If there is a partial word at the end of buf, handle it manually to
308 * prevent overwriting past the end of buf
309 */
310 if (rx_fifo_avail > 0 && buf_remaining > 0) {
311 BUG_ON(buf_remaining > 3);
312 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300313 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800314 memcpy(buf, &val, buf_remaining);
315 buf_remaining = 0;
316 rx_fifo_avail--;
317 }
318
319 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
320 i2c_dev->msg_buf_remaining = buf_remaining;
321 i2c_dev->msg_buf = buf;
322 return 0;
323}
324
325static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
326{
327 u32 val;
328 int tx_fifo_avail;
329 u8 *buf = i2c_dev->msg_buf;
330 size_t buf_remaining = i2c_dev->msg_buf_remaining;
331 int words_to_transfer;
332
333 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
334 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
335 I2C_FIFO_STATUS_TX_SHIFT;
336
337 /* Rounds down to not include partial word at the end of buf */
338 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800339
Doug Anderson96219c32011-08-30 11:46:10 -0600340 /* It's very common to have < 4 bytes, so optimize that case. */
341 if (words_to_transfer) {
342 if (words_to_transfer > tx_fifo_avail)
343 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800344
Doug Anderson96219c32011-08-30 11:46:10 -0600345 /*
346 * Update state before writing to FIFO. If this casues us
347 * to finish writing all bytes (AKA buf_remaining goes to 0) we
348 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
349 * not maskable). We need to make sure that the isr sees
350 * buf_remaining as 0 and doesn't call us back re-entrantly.
351 */
352 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
353 tx_fifo_avail -= words_to_transfer;
354 i2c_dev->msg_buf_remaining = buf_remaining;
355 i2c_dev->msg_buf = buf +
356 words_to_transfer * BYTES_PER_FIFO_WORD;
357 barrier();
358
359 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
360
361 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
362 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800363
364 /*
365 * If there is a partial word at the end of buf, handle it manually to
366 * prevent reading past the end of buf, which could cross a page
367 * boundary and fault.
368 */
369 if (tx_fifo_avail > 0 && buf_remaining > 0) {
370 BUG_ON(buf_remaining > 3);
371 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300372 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600373
374 /* Again update before writing to FIFO to make sure isr sees. */
375 i2c_dev->msg_buf_remaining = 0;
376 i2c_dev->msg_buf = NULL;
377 barrier();
378
Colin Crossdb811ca2011-02-20 17:14:21 -0800379 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800380 }
381
Colin Crossdb811ca2011-02-20 17:14:21 -0800382 return 0;
383}
384
385/*
386 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
387 * block. This block is identical to the rest of the I2C blocks, except that
388 * it only supports master mode, it has registers moved around, and it needs
389 * some extra init to get it into I2C mode. The register moves are handled
390 * by i2c_readl and i2c_writel
391 */
392static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
393{
Jon Hunterf5076682016-08-26 14:08:59 +0100394 u32 val;
395
Colin Crossdb811ca2011-02-20 17:14:21 -0800396 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
397 val |= DVC_CTRL_REG3_SW_PROG;
398 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
399 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
400
401 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
402 val |= DVC_CTRL_REG1_INTR_EN;
403 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
404}
405
Jon Hunter1f50ad22016-08-26 14:09:04 +0100406static int tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530407{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100408 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530409 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100410
Jon Hunter718917b2016-08-26 14:09:05 +0100411 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
412 if (ret)
413 return ret;
414
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530415 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300416 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530417 if (ret < 0) {
418 dev_err(i2c_dev->dev,
419 "Enabling fast clk failed, err %d\n", ret);
420 return ret;
421 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530422 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100423
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300424 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530425 if (ret < 0) {
426 dev_err(i2c_dev->dev,
427 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300428 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100429 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530430 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100431
432 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530433}
434
Jon Hunter1f50ad22016-08-26 14:09:04 +0100435static int tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530436{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100437 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
438
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300439 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530440 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300441 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100442
Jon Hunter718917b2016-08-26 14:09:05 +0100443 return pinctrl_pm_select_idle_state(i2c_dev->dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530444}
445
Colin Crossdb811ca2011-02-20 17:14:21 -0800446static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
447{
448 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100449 int err;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530450 u32 clk_divisor;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530451 unsigned long timeout = jiffies + HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -0800452
Jon Hunter1f50ad22016-08-26 14:09:04 +0100453 err = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000454 if (err < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100455 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000456 return err;
457 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800458
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700459 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800460 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700461 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800462
463 if (i2c_dev->is_dvc)
464 tegra_dvc_init(i2c_dev);
465
Jay Cheng40abcf72011-04-25 15:32:27 -0600466 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
467 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530468
469 if (i2c_dev->hw->has_multi_master_mode)
470 val |= I2C_CNFG_MULTI_MASTER_MODE;
471
Colin Crossdb811ca2011-02-20 17:14:21 -0800472 i2c_writel(i2c_dev, val, I2C_CNFG);
473 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530474
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530475 /* Make sure clock divisor programmed correctly */
476 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530477 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530478 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
479 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800480
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600481 if (!i2c_dev->is_dvc) {
482 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100483
Stephen Warren5afa9d32011-06-06 11:25:19 -0600484 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
485 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
486 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
487 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600488 }
489
Colin Crossdb811ca2011-02-20 17:14:21 -0800490 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
491 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
492 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
493
Jon Hunter1f50ad22016-08-26 14:09:04 +0100494 err = tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800495
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530496 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
497 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
498
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530499 if (i2c_dev->hw->has_config_load_reg) {
500 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
501 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
502 if (time_after(jiffies, timeout)) {
503 dev_warn(i2c_dev->dev,
504 "timeout waiting for config load\n");
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530505 err = -ETIMEDOUT;
506 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530507 }
508 msleep(1);
509 }
510 }
511
Todd Poynorcb63c622011-04-25 15:32:25 -0600512 if (i2c_dev->irq_disabled) {
513 i2c_dev->irq_disabled = 0;
514 enable_irq(i2c_dev->irq);
515 }
516
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530517err:
Jon Hunter1f50ad22016-08-26 14:09:04 +0100518 pm_runtime_put(i2c_dev->dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800519 return err;
520}
521
522static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
523{
524 u32 status;
525 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
526 struct tegra_i2c_dev *i2c_dev = dev_id;
527
528 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
529
530 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600531 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
532 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
533 i2c_readl(i2c_dev, I2C_STATUS),
534 i2c_readl(i2c_dev, I2C_CNFG));
535 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
536
537 if (!i2c_dev->irq_disabled) {
538 disable_irq_nosync(i2c_dev->irq);
539 i2c_dev->irq_disabled = 1;
540 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600541 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800542 }
543
544 if (unlikely(status & status_err)) {
545 if (status & I2C_INT_NO_ACK)
546 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
547 if (status & I2C_INT_ARBITRATION_LOST)
548 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800549 goto err;
550 }
551
552 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
553 if (i2c_dev->msg_buf_remaining)
554 tegra_i2c_empty_rx_fifo(i2c_dev);
555 else
556 BUG();
557 }
558
559 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
560 if (i2c_dev->msg_buf_remaining)
561 tegra_i2c_fill_tx_fifo(i2c_dev);
562 else
563 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
564 }
565
Laxman Dewanganc889e912012-05-07 12:16:19 +0530566 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
567 if (i2c_dev->is_dvc)
568 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
569
Doug Anderson96219c32011-08-30 11:46:10 -0600570 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
571 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800572 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600573 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800574 return IRQ_HANDLED;
575err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300576 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800577 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
578 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
579 I2C_INT_RX_FIFO_DATA_REQ);
580 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600581 if (i2c_dev->is_dvc)
582 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530583
584 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800585 return IRQ_HANDLED;
586}
587
588static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530589 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800590{
591 u32 packet_header;
592 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500593 unsigned long time_left;
Colin Crossdb811ca2011-02-20 17:14:21 -0800594
595 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800596
597 if (msg->len == 0)
598 return -EINVAL;
599
600 i2c_dev->msg_buf = msg->buf;
601 i2c_dev->msg_buf_remaining = msg->len;
602 i2c_dev->msg_err = I2C_ERR_NONE;
603 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800604 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800605
606 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
607 PACKET_HEADER0_PROTOCOL_I2C |
608 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
609 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
610 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
611
612 packet_header = msg->len - 1;
613 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
614
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530615 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530616 if (end_state == MSG_END_CONTINUE)
617 packet_header |= I2C_HEADER_CONTINUE_XFER;
618 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600619 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530620 if (msg->flags & I2C_M_TEN) {
621 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800622 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530623 } else {
624 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
625 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800626 if (msg->flags & I2C_M_IGNORE_NAK)
627 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800628 if (msg->flags & I2C_M_RD)
629 packet_header |= I2C_HEADER_READ;
630 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
631
632 if (!(msg->flags & I2C_M_RD))
633 tegra_i2c_fill_tx_fifo(i2c_dev);
634
635 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530636 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
637 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800638 if (msg->flags & I2C_M_RD)
639 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
640 else if (i2c_dev->msg_buf_remaining)
641 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
642 tegra_i2c_unmask_irq(i2c_dev, int_mask);
643 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
644 i2c_readl(i2c_dev, I2C_INT_MASK));
645
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500646 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
647 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800648 tegra_i2c_mask_irq(i2c_dev, int_mask);
649
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500650 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800651 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
652
653 tegra_i2c_init(i2c_dev);
654 return -ETIMEDOUT;
655 }
656
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500657 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
658 time_left, completion_done(&i2c_dev->msg_complete),
659 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800660
661 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
662 return 0;
663
Alok Chauhanf70893d02012-04-02 11:23:02 +0530664 /*
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100665 * NACK interrupt is generated before the I2C controller generates
666 * the STOP condition on the bus. So wait for 2 clock periods
667 * before resetting the controller so that the STOP condition has
668 * been delivered properly.
Alok Chauhanf70893d02012-04-02 11:23:02 +0530669 */
670 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
671 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
672
Colin Crossdb811ca2011-02-20 17:14:21 -0800673 tegra_i2c_init(i2c_dev);
674 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
675 if (msg->flags & I2C_M_IGNORE_NAK)
676 return 0;
677 return -EREMOTEIO;
678 }
679
680 return -EIO;
681}
682
683static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
684 int num)
685{
686 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
687 int i;
688 int ret = 0;
689
690 if (i2c_dev->is_suspended)
691 return -EBUSY;
692
Jon Hunter1f50ad22016-08-26 14:09:04 +0100693 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000694 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100695 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000696 return ret;
697 }
698
Colin Crossdb811ca2011-02-20 17:14:21 -0800699 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530700 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +0100701
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530702 if (i < (num - 1)) {
703 if (msgs[i + 1].flags & I2C_M_NOSTART)
704 end_type = MSG_END_CONTINUE;
705 else
706 end_type = MSG_END_REPEAT_START;
707 }
708 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800709 if (ret)
710 break;
711 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100712
713 pm_runtime_put(i2c_dev->dev);
714
Colin Crossdb811ca2011-02-20 17:14:21 -0800715 return ret ?: i;
716}
717
718static u32 tegra_i2c_func(struct i2c_adapter *adap)
719{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530720 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200721 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
722 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530723
724 if (i2c_dev->hw->has_continue_xfer_support)
725 ret |= I2C_FUNC_NOSTART;
726 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800727}
728
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530729static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
730{
731 struct device_node *np = i2c_dev->dev->of_node;
732 int ret;
733
734 ret = of_property_read_u32(np, "clock-frequency",
735 &i2c_dev->bus_clk_rate);
736 if (ret)
737 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
738
739 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
740 "multi-master");
741}
742
Colin Crossdb811ca2011-02-20 17:14:21 -0800743static const struct i2c_algorithm tegra_i2c_algo = {
744 .master_xfer = tegra_i2c_xfer,
745 .functionality = tegra_i2c_func,
746};
747
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200748/* payload size is only 12 bit */
749static struct i2c_adapter_quirks tegra_i2c_quirks = {
750 .max_read_len = 4096,
751 .max_write_len = 4096,
752};
753
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530754static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
755 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530756 .has_per_pkt_xfer_complete_irq = false,
757 .has_single_clk_source = false,
758 .clk_divisor_hs_mode = 3,
759 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530760 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530761 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530762 .has_multi_master_mode = false,
763 .has_slcg_override_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530764};
765
766static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
767 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530768 .has_per_pkt_xfer_complete_irq = false,
769 .has_single_clk_source = false,
770 .clk_divisor_hs_mode = 3,
771 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530772 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530773 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530774 .has_multi_master_mode = false,
775 .has_slcg_override_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530776};
777
778static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
779 .has_continue_xfer_support = true,
780 .has_per_pkt_xfer_complete_irq = true,
781 .has_single_clk_source = true,
782 .clk_divisor_hs_mode = 1,
783 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530784 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530785 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530786 .has_multi_master_mode = false,
787 .has_slcg_override_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530788};
789
790static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
791 .has_continue_xfer_support = true,
792 .has_per_pkt_xfer_complete_irq = true,
793 .has_single_clk_source = true,
794 .clk_divisor_hs_mode = 1,
795 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530796 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530797 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530798 .has_multi_master_mode = false,
799 .has_slcg_override_reg = true,
800};
801
802static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
803 .has_continue_xfer_support = true,
804 .has_per_pkt_xfer_complete_irq = true,
805 .has_single_clk_source = true,
806 .clk_divisor_hs_mode = 1,
807 .clk_divisor_std_fast_mode = 0x19,
808 .clk_divisor_fast_plus_mode = 0x10,
809 .has_config_load_reg = true,
810 .has_multi_master_mode = true,
811 .has_slcg_override_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530812};
813
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530814/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500815static const struct of_device_id tegra_i2c_of_match[] = {
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530816 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530817 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530818 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530819 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
820 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
821 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
822 {},
823};
824MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530825
Bill Pemberton0b255e92012-11-27 15:59:38 -0500826static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800827{
828 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800829 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530830 struct clk *div_clk;
831 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700832 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800833 int irq;
834 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300835 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800836
837 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100838 base = devm_ioremap_resource(&pdev->dev, res);
839 if (IS_ERR(base))
840 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800841
842 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
843 if (!res) {
844 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530845 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800846 }
847 irq = res->start;
848
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530849 div_clk = devm_clk_get(&pdev->dev, "div-clk");
850 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100851 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530852 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800853 }
854
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530855 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900856 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530857 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800858
859 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530860 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800861 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200862 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800863 i2c_dev->irq = irq;
864 i2c_dev->cont_id = pdev->id;
865 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700866
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700867 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
868 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100869 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700870 return PTR_ERR(i2c_dev->rst);
871 }
872
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530873 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800874
Jon Huntera9e32cd2016-08-26 14:09:01 +0100875 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
876 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
877 "nvidia,tegra20-i2c-dvc");
Colin Crossdb811ca2011-02-20 17:14:21 -0800878 init_completion(&i2c_dev->msg_complete);
879
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530880 if (!i2c_dev->hw->has_single_clk_source) {
881 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
882 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100883 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530884 return PTR_ERR(fast_clk);
885 }
886 i2c_dev->fast_clk = fast_clk;
887 }
888
Colin Crossdb811ca2011-02-20 17:14:21 -0800889 platform_set_drvdata(pdev, i2c_dev);
890
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300891 if (!i2c_dev->hw->has_single_clk_source) {
892 ret = clk_prepare(i2c_dev->fast_clk);
893 if (ret < 0) {
894 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
895 return ret;
896 }
897 }
898
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530899 i2c_dev->clk_divisor_non_hs_mode =
900 i2c_dev->hw->clk_divisor_std_fast_mode;
901 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
902 (i2c_dev->bus_clk_rate == 1000000))
903 i2c_dev->clk_divisor_non_hs_mode =
904 i2c_dev->hw->clk_divisor_fast_plus_mode;
905
906 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300907 ret = clk_set_rate(i2c_dev->div_clk,
908 i2c_dev->bus_clk_rate * clk_multiplier);
909 if (ret) {
910 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
911 goto unprepare_fast_clk;
912 }
913
914 ret = clk_prepare(i2c_dev->div_clk);
915 if (ret < 0) {
916 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
917 goto unprepare_fast_clk;
918 }
919
Jon Hunter1f50ad22016-08-26 14:09:04 +0100920 pm_runtime_enable(&pdev->dev);
921 if (!pm_runtime_enabled(&pdev->dev)) {
922 ret = tegra_i2c_runtime_resume(&pdev->dev);
923 if (ret < 0) {
924 dev_err(&pdev->dev, "runtime resume failed\n");
925 goto unprepare_div_clk;
926 }
927 }
928
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530929 if (i2c_dev->is_multimaster_mode) {
930 ret = clk_enable(i2c_dev->div_clk);
931 if (ret < 0) {
932 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
933 ret);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100934 goto disable_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530935 }
936 }
937
Colin Crossdb811ca2011-02-20 17:14:21 -0800938 ret = tegra_i2c_init(i2c_dev);
939 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100940 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Jon Huntereab09982016-06-14 21:26:46 +0100941 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800942 }
943
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530944 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530945 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800946 if (ret) {
947 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530948 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800949 }
950
Colin Crossdb811ca2011-02-20 17:14:21 -0800951 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
952 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +0200953 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +0100954 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -0800955 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -0800956 i2c_dev->adapter.dev.parent = &pdev->dev;
957 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700958 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800959
960 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +0200961 if (ret)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530962 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800963
Colin Crossdb811ca2011-02-20 17:14:21 -0800964 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300965
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530966disable_div_clk:
967 if (i2c_dev->is_multimaster_mode)
968 clk_disable(i2c_dev->div_clk);
969
Jon Hunter1f50ad22016-08-26 14:09:04 +0100970disable_rpm:
971 pm_runtime_disable(&pdev->dev);
972 if (!pm_runtime_status_suspended(&pdev->dev))
973 tegra_i2c_runtime_suspend(&pdev->dev);
974
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300975unprepare_div_clk:
976 clk_unprepare(i2c_dev->div_clk);
977
978unprepare_fast_clk:
979 if (!i2c_dev->hw->has_single_clk_source)
980 clk_unprepare(i2c_dev->fast_clk);
981
982 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800983}
984
Bill Pemberton0b255e92012-11-27 15:59:38 -0500985static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800986{
987 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +0100988
Colin Crossdb811ca2011-02-20 17:14:21 -0800989 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300990
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530991 if (i2c_dev->is_multimaster_mode)
992 clk_disable(i2c_dev->div_clk);
993
Jon Hunter1f50ad22016-08-26 14:09:04 +0100994 pm_runtime_disable(&pdev->dev);
995 if (!pm_runtime_status_suspended(&pdev->dev))
996 tegra_i2c_runtime_suspend(&pdev->dev);
997
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300998 clk_unprepare(i2c_dev->div_clk);
999 if (!i2c_dev->hw->has_single_clk_source)
1000 clk_unprepare(i2c_dev->fast_clk);
1001
Colin Crossdb811ca2011-02-20 17:14:21 -08001002 return 0;
1003}
1004
Laxman Dewangan371e67c2012-08-18 17:49:58 +05301005#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +02001006static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001007{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001008 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001009
1010 i2c_lock_adapter(&i2c_dev->adapter);
1011 i2c_dev->is_suspended = true;
1012 i2c_unlock_adapter(&i2c_dev->adapter);
1013
1014 return 0;
1015}
1016
Wolfram Sang5db20c42012-07-24 17:32:45 +02001017static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001018{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001019 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001020 int ret;
1021
1022 i2c_lock_adapter(&i2c_dev->adapter);
1023
1024 ret = tegra_i2c_init(i2c_dev);
Jon Hunterf4c2d892016-08-26 14:09:03 +01001025 if (!ret)
1026 i2c_dev->is_suspended = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001027
1028 i2c_unlock_adapter(&i2c_dev->adapter);
1029
Jon Hunterf4c2d892016-08-26 14:09:03 +01001030 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001031}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001032
Jon Hunter1f50ad22016-08-26 14:09:04 +01001033static const struct dev_pm_ops tegra_i2c_pm = {
1034 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1035 NULL)
1036 SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1037};
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001038#define TEGRA_I2C_PM (&tegra_i2c_pm)
1039#else
1040#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001041#endif
1042
1043static struct platform_driver tegra_i2c_driver = {
1044 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001045 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001046 .driver = {
1047 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001048 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001049 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001050 },
1051};
1052
1053static int __init tegra_i2c_init_driver(void)
1054{
1055 return platform_driver_register(&tegra_i2c_driver);
1056}
1057
1058static void __exit tegra_i2c_exit_driver(void)
1059{
1060 platform_driver_unregister(&tegra_i2c_driver);
1061}
1062
1063subsys_initcall(tegra_i2c_init_driver);
1064module_exit(tegra_i2c_exit_driver);
1065
1066MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1067MODULE_AUTHOR("Colin Cross");
1068MODULE_LICENSE("GPL v2");