blob: 36100f453f314e095dba183ce8c6cf11188fcb3b [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>
Shardar Shariff Md685143a12016-08-31 18:58:40 +053033#include <linux/iopoll.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080034
35#include <asm/unaligned.h>
36
Colin Crossdb811ca2011-02-20 17:14:21 -080037#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
38#define BYTES_PER_FIFO_WORD 4
39
40#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060041#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010042#define I2C_CNFG_PACKET_MODE_EN BIT(10)
43#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
44#define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
Todd Poynorcb63c622011-04-25 15:32:25 -060045#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080046#define I2C_SL_CNFG 0x020
Jon Hunter2929be22016-08-26 14:08:58 +010047#define I2C_SL_CNFG_NACK BIT(1)
48#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080049#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060050#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080051#define I2C_TX_FIFO 0x050
52#define I2C_RX_FIFO 0x054
53#define I2C_PACKET_TRANSFER_STATUS 0x058
54#define I2C_FIFO_CONTROL 0x05c
Jon Hunter2929be22016-08-26 14:08:58 +010055#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
56#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080057#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
58#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
59#define I2C_FIFO_STATUS 0x060
60#define I2C_FIFO_STATUS_TX_MASK 0xF0
61#define I2C_FIFO_STATUS_TX_SHIFT 4
62#define I2C_FIFO_STATUS_RX_MASK 0x0F
63#define I2C_FIFO_STATUS_RX_SHIFT 0
64#define I2C_INT_MASK 0x064
65#define I2C_INT_STATUS 0x068
Jon Hunter2929be22016-08-26 14:08:58 +010066#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
67#define I2C_INT_ALL_PACKETS_XFER_COMPLETE BIT(6)
68#define I2C_INT_TX_FIFO_OVERFLOW BIT(5)
69#define I2C_INT_RX_FIFO_UNDERFLOW BIT(4)
70#define I2C_INT_NO_ACK BIT(3)
71#define I2C_INT_ARBITRATION_LOST BIT(2)
72#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
73#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080074#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053075#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
76#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080077
78#define DVC_CTRL_REG1 0x000
Jon Hunter2929be22016-08-26 14:08:58 +010079#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080080#define DVC_CTRL_REG2 0x004
81#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010082#define DVC_CTRL_REG3_SW_PROG BIT(26)
83#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080084#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010085#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080086
87#define I2C_ERR_NONE 0x00
88#define I2C_ERR_NO_ACK 0x01
89#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060090#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080091
92#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
93#define PACKET_HEADER0_PACKET_ID_SHIFT 16
94#define PACKET_HEADER0_CONT_ID_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010095#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080096
Jon Hunter2929be22016-08-26 14:08:58 +010097#define I2C_HEADER_HIGHSPEED_MODE BIT(22)
98#define I2C_HEADER_CONT_ON_NAK BIT(21)
99#define I2C_HEADER_SEND_START_BYTE BIT(20)
100#define I2C_HEADER_READ BIT(19)
101#define I2C_HEADER_10BIT_ADDR BIT(18)
102#define I2C_HEADER_IE_ENABLE BIT(17)
103#define I2C_HEADER_REPEAT_START BIT(16)
104#define I2C_HEADER_CONTINUE_XFER BIT(15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800105#define I2C_HEADER_MASTER_ADDR_SHIFT 12
106#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530107
108#define I2C_CONFIG_LOAD 0x08C
Jon Hunter2929be22016-08-26 14:08:58 +0100109#define I2C_MSTR_CONFIG_LOAD BIT(0)
110#define I2C_SLV_CONFIG_LOAD BIT(1)
111#define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530112
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530113#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100114#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530115
Shardar Shariff Md685143a12016-08-31 18:58:40 +0530116#define I2C_CONFIG_LOAD_TIMEOUT 1000000
117
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530118/*
119 * msg_end_type: The bus control which need to be send at end of transfer.
120 * @MSG_END_STOP: Send stop pulse at end of transfer.
121 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
122 * @MSG_END_CONTINUE: The following on message is coming and so do not send
123 * stop or repeat start.
124 */
125enum msg_end_type {
126 MSG_END_STOP,
127 MSG_END_REPEAT_START,
128 MSG_END_CONTINUE,
129};
Colin Crossdb811ca2011-02-20 17:14:21 -0800130
131/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530132 * struct tegra_i2c_hw_feature : Different HW support on Tegra
133 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530134 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
135 * complete interrupt per packet basis.
136 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
137 * and earlier Socs has two clock sources i.e. div-clk and
138 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530139 * @has_config_load_reg: Has the config load register to load the new
140 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530141 * @clk_divisor_hs_mode: Clock divisor in HS mode.
142 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
143 * applicable if there is no fast clock source i.e. single clock
144 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530145 */
146
147struct tegra_i2c_hw_feature {
148 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530149 bool has_per_pkt_xfer_complete_irq;
150 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530151 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530152 int clk_divisor_hs_mode;
153 int clk_divisor_std_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530154 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530155 bool has_multi_master_mode;
156 bool has_slcg_override_reg;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530157};
158
159/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800160 * struct tegra_i2c_dev - per device i2c context
161 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530162 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800163 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530164 * @div_clk: clock reference for div clock of i2c controller.
165 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800166 * @base: ioremapped registers cookie
167 * @cont_id: i2c controller id, used for for packet header
168 * @irq: irq number of transfer complete interrupt
169 * @is_dvc: identifies the DVC i2c controller, has a different register layout
170 * @msg_complete: transfer completion notifier
171 * @msg_err: error code for completed message
172 * @msg_buf: pointer to current message data
173 * @msg_buf_remaining: size of unsent data in the message buffer
174 * @msg_read: identifies read transfers
175 * @bus_clk_rate: current i2c bus clock rate
176 * @is_suspended: prevents i2c controller accesses after suspend is called
177 */
178struct tegra_i2c_dev {
179 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530180 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800181 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530182 struct clk *div_clk;
183 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700184 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800185 void __iomem *base;
186 int cont_id;
187 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600188 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800189 int is_dvc;
190 struct completion msg_complete;
191 int msg_err;
192 u8 *msg_buf;
193 size_t msg_buf_remaining;
194 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000195 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530196 u16 clk_divisor_non_hs_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800197 bool is_suspended;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530198 bool is_multimaster_mode;
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530199 spinlock_t xfer_lock;
Colin Crossdb811ca2011-02-20 17:14:21 -0800200};
201
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100202static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
203 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800204{
205 writel(val, i2c_dev->base + reg);
206}
207
208static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
209{
210 return readl(i2c_dev->base + reg);
211}
212
213/*
214 * i2c_writel and i2c_readl will offset the register if necessary to talk
215 * to the I2C block inside the DVC block
216 */
217static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
218 unsigned long reg)
219{
220 if (i2c_dev->is_dvc)
221 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
222 return reg;
223}
224
225static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
226 unsigned long reg)
227{
228 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530229
230 /* Read back register to make sure that register writes completed */
231 if (reg != I2C_TX_FIFO)
232 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800233}
234
235static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
236{
237 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
238}
239
240static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
241 unsigned long reg, int len)
242{
243 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
244}
245
246static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
247 unsigned long reg, int len)
248{
249 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
250}
251
252static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
253{
Jon Hunterf5076682016-08-26 14:08:59 +0100254 u32 int_mask;
255
256 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800257 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
258}
259
260static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
261{
Jon Hunterf5076682016-08-26 14:08:59 +0100262 u32 int_mask;
263
264 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800265 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
266}
267
268static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
269{
270 unsigned long timeout = jiffies + HZ;
271 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
Jon Hunterf5076682016-08-26 14:08:59 +0100272
Colin Crossdb811ca2011-02-20 17:14:21 -0800273 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
274 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
275
276 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
277 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
278 if (time_after(jiffies, timeout)) {
279 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
280 return -ETIMEDOUT;
281 }
282 msleep(1);
283 }
284 return 0;
285}
286
287static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
288{
289 u32 val;
290 int rx_fifo_avail;
291 u8 *buf = i2c_dev->msg_buf;
292 size_t buf_remaining = i2c_dev->msg_buf_remaining;
293 int words_to_transfer;
294
295 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
296 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
297 I2C_FIFO_STATUS_RX_SHIFT;
298
299 /* Rounds down to not include partial word at the end of buf */
300 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
301 if (words_to_transfer > rx_fifo_avail)
302 words_to_transfer = rx_fifo_avail;
303
304 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
305
306 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
307 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
308 rx_fifo_avail -= words_to_transfer;
309
310 /*
311 * If there is a partial word at the end of buf, handle it manually to
312 * prevent overwriting past the end of buf
313 */
314 if (rx_fifo_avail > 0 && buf_remaining > 0) {
315 BUG_ON(buf_remaining > 3);
316 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300317 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800318 memcpy(buf, &val, buf_remaining);
319 buf_remaining = 0;
320 rx_fifo_avail--;
321 }
322
323 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
324 i2c_dev->msg_buf_remaining = buf_remaining;
325 i2c_dev->msg_buf = buf;
326 return 0;
327}
328
329static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
330{
331 u32 val;
332 int tx_fifo_avail;
333 u8 *buf = i2c_dev->msg_buf;
334 size_t buf_remaining = i2c_dev->msg_buf_remaining;
335 int words_to_transfer;
336
337 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
338 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
339 I2C_FIFO_STATUS_TX_SHIFT;
340
341 /* Rounds down to not include partial word at the end of buf */
342 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800343
Doug Anderson96219c32011-08-30 11:46:10 -0600344 /* It's very common to have < 4 bytes, so optimize that case. */
345 if (words_to_transfer) {
346 if (words_to_transfer > tx_fifo_avail)
347 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800348
Doug Anderson96219c32011-08-30 11:46:10 -0600349 /*
350 * Update state before writing to FIFO. If this casues us
351 * to finish writing all bytes (AKA buf_remaining goes to 0) we
352 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
353 * not maskable). We need to make sure that the isr sees
354 * buf_remaining as 0 and doesn't call us back re-entrantly.
355 */
356 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
357 tx_fifo_avail -= words_to_transfer;
358 i2c_dev->msg_buf_remaining = buf_remaining;
359 i2c_dev->msg_buf = buf +
360 words_to_transfer * BYTES_PER_FIFO_WORD;
361 barrier();
362
363 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
364
365 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
366 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800367
368 /*
369 * If there is a partial word at the end of buf, handle it manually to
370 * prevent reading past the end of buf, which could cross a page
371 * boundary and fault.
372 */
373 if (tx_fifo_avail > 0 && buf_remaining > 0) {
374 BUG_ON(buf_remaining > 3);
375 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300376 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600377
378 /* Again update before writing to FIFO to make sure isr sees. */
379 i2c_dev->msg_buf_remaining = 0;
380 i2c_dev->msg_buf = NULL;
381 barrier();
382
Colin Crossdb811ca2011-02-20 17:14:21 -0800383 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800384 }
385
Colin Crossdb811ca2011-02-20 17:14:21 -0800386 return 0;
387}
388
389/*
390 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
391 * block. This block is identical to the rest of the I2C blocks, except that
392 * it only supports master mode, it has registers moved around, and it needs
393 * some extra init to get it into I2C mode. The register moves are handled
394 * by i2c_readl and i2c_writel
395 */
396static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
397{
Jon Hunterf5076682016-08-26 14:08:59 +0100398 u32 val;
399
Colin Crossdb811ca2011-02-20 17:14:21 -0800400 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
401 val |= DVC_CTRL_REG3_SW_PROG;
402 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
403 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
404
405 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
406 val |= DVC_CTRL_REG1_INTR_EN;
407 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
408}
409
Jon Hunter1f50ad22016-08-26 14:09:04 +0100410static int tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530411{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100412 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530413 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100414
Jon Hunter718917b2016-08-26 14:09:05 +0100415 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
416 if (ret)
417 return ret;
418
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530419 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300420 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530421 if (ret < 0) {
422 dev_err(i2c_dev->dev,
423 "Enabling fast clk failed, err %d\n", ret);
424 return ret;
425 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530426 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100427
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300428 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530429 if (ret < 0) {
430 dev_err(i2c_dev->dev,
431 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300432 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100433 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530434 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100435
436 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530437}
438
Jon Hunter1f50ad22016-08-26 14:09:04 +0100439static int tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530440{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100441 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
442
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300443 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530444 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300445 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100446
Jon Hunter718917b2016-08-26 14:09:05 +0100447 return pinctrl_pm_select_idle_state(i2c_dev->dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530448}
449
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530450static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
451{
452 unsigned long reg_offset;
453 void __iomem *addr;
454 u32 val;
455 int err;
456
457 if (i2c_dev->hw->has_config_load_reg) {
458 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
459 addr = i2c_dev->base + reg_offset;
460 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
Shardar Shariff Md2bc445e2016-08-31 18:58:43 +0530461 if (in_interrupt())
462 err = readl_poll_timeout_atomic(addr, val, val == 0,
463 1000, I2C_CONFIG_LOAD_TIMEOUT);
464 else
465 err = readl_poll_timeout(addr, val, val == 0,
466 1000, I2C_CONFIG_LOAD_TIMEOUT);
467
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530468 if (err) {
469 dev_warn(i2c_dev->dev,
470 "timeout waiting for config load\n");
471 return err;
472 }
473 }
474
475 return 0;
476}
477
Colin Crossdb811ca2011-02-20 17:14:21 -0800478static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
479{
480 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100481 int err;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530482 u32 clk_divisor;
Colin Crossdb811ca2011-02-20 17:14:21 -0800483
Jon Hunter1f50ad22016-08-26 14:09:04 +0100484 err = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000485 if (err < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100486 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000487 return err;
488 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800489
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700490 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800491 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700492 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800493
494 if (i2c_dev->is_dvc)
495 tegra_dvc_init(i2c_dev);
496
Jay Cheng40abcf72011-04-25 15:32:27 -0600497 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
498 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530499
500 if (i2c_dev->hw->has_multi_master_mode)
501 val |= I2C_CNFG_MULTI_MASTER_MODE;
502
Colin Crossdb811ca2011-02-20 17:14:21 -0800503 i2c_writel(i2c_dev, val, I2C_CNFG);
504 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530505
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530506 /* Make sure clock divisor programmed correctly */
507 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530508 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530509 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
510 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800511
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600512 if (!i2c_dev->is_dvc) {
513 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100514
Stephen Warren5afa9d32011-06-06 11:25:19 -0600515 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
516 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
517 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
518 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600519 }
520
Colin Crossdb811ca2011-02-20 17:14:21 -0800521 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
522 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
523 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
524
Jon Hunter1f50ad22016-08-26 14:09:04 +0100525 err = tegra_i2c_flush_fifos(i2c_dev);
Shardar Shariff Md2148c012016-08-31 18:58:41 +0530526 if (err)
527 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800528
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530529 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
530 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
531
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530532 err = tegra_i2c_wait_for_config_load(i2c_dev);
533 if (err)
534 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530535
Todd Poynorcb63c622011-04-25 15:32:25 -0600536 if (i2c_dev->irq_disabled) {
Jon Hunterfbf80902016-09-06 10:50:45 +0100537 i2c_dev->irq_disabled = false;
Todd Poynorcb63c622011-04-25 15:32:25 -0600538 enable_irq(i2c_dev->irq);
539 }
540
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530541err:
Jon Hunter1f50ad22016-08-26 14:09:04 +0100542 pm_runtime_put(i2c_dev->dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800543 return err;
544}
545
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530546static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
547{
548 u32 cnfg;
549
Jon Huntere78e3702018-07-03 09:55:43 +0100550 /*
551 * NACK interrupt is generated before the I2C controller generates
552 * the STOP condition on the bus. So wait for 2 clock periods
553 * before disabling the controller so that the STOP condition has
554 * been delivered properly.
555 */
556 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
557
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530558 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
559 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
560 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
561
562 return tegra_i2c_wait_for_config_load(i2c_dev);
563}
564
Colin Crossdb811ca2011-02-20 17:14:21 -0800565static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
566{
567 u32 status;
568 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
569 struct tegra_i2c_dev *i2c_dev = dev_id;
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530570 unsigned long flags;
Colin Crossdb811ca2011-02-20 17:14:21 -0800571
572 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
573
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530574 spin_lock_irqsave(&i2c_dev->xfer_lock, flags);
Colin Crossdb811ca2011-02-20 17:14:21 -0800575 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600576 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
577 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
578 i2c_readl(i2c_dev, I2C_STATUS),
579 i2c_readl(i2c_dev, I2C_CNFG));
580 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
581
582 if (!i2c_dev->irq_disabled) {
583 disable_irq_nosync(i2c_dev->irq);
Jon Hunterfbf80902016-09-06 10:50:45 +0100584 i2c_dev->irq_disabled = true;
Todd Poynorcb63c622011-04-25 15:32:25 -0600585 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600586 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800587 }
588
589 if (unlikely(status & status_err)) {
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530590 tegra_i2c_disable_packet_mode(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800591 if (status & I2C_INT_NO_ACK)
592 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
593 if (status & I2C_INT_ARBITRATION_LOST)
594 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800595 goto err;
596 }
597
598 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
599 if (i2c_dev->msg_buf_remaining)
600 tegra_i2c_empty_rx_fifo(i2c_dev);
601 else
602 BUG();
603 }
604
605 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
606 if (i2c_dev->msg_buf_remaining)
607 tegra_i2c_fill_tx_fifo(i2c_dev);
608 else
609 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
610 }
611
Laxman Dewanganc889e912012-05-07 12:16:19 +0530612 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
613 if (i2c_dev->is_dvc)
614 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
615
Doug Anderson96219c32011-08-30 11:46:10 -0600616 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
617 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800618 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600619 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530620 goto done;
Colin Crossdb811ca2011-02-20 17:14:21 -0800621err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300622 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800623 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
624 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
625 I2C_INT_RX_FIFO_DATA_REQ);
626 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600627 if (i2c_dev->is_dvc)
628 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530629
630 complete(&i2c_dev->msg_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530631done:
632 spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
Colin Crossdb811ca2011-02-20 17:14:21 -0800633 return IRQ_HANDLED;
634}
635
636static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530637 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800638{
639 u32 packet_header;
640 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500641 unsigned long time_left;
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530642 unsigned long flags;
Colin Crossdb811ca2011-02-20 17:14:21 -0800643
644 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800645
646 if (msg->len == 0)
647 return -EINVAL;
648
649 i2c_dev->msg_buf = msg->buf;
650 i2c_dev->msg_buf_remaining = msg->len;
651 i2c_dev->msg_err = I2C_ERR_NONE;
652 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800653 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800654
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530655 spin_lock_irqsave(&i2c_dev->xfer_lock, flags);
656
657 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
658 tegra_i2c_unmask_irq(i2c_dev, int_mask);
659
Colin Crossdb811ca2011-02-20 17:14:21 -0800660 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
661 PACKET_HEADER0_PROTOCOL_I2C |
662 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
663 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
664 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
665
666 packet_header = msg->len - 1;
667 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
668
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530669 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530670 if (end_state == MSG_END_CONTINUE)
671 packet_header |= I2C_HEADER_CONTINUE_XFER;
672 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600673 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530674 if (msg->flags & I2C_M_TEN) {
675 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800676 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530677 } else {
678 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
679 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800680 if (msg->flags & I2C_M_IGNORE_NAK)
681 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800682 if (msg->flags & I2C_M_RD)
683 packet_header |= I2C_HEADER_READ;
684 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
685
686 if (!(msg->flags & I2C_M_RD))
687 tegra_i2c_fill_tx_fifo(i2c_dev);
688
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530689 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
690 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800691 if (msg->flags & I2C_M_RD)
692 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
693 else if (i2c_dev->msg_buf_remaining)
694 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530695
Colin Crossdb811ca2011-02-20 17:14:21 -0800696 tegra_i2c_unmask_irq(i2c_dev, int_mask);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530697 spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
Colin Crossdb811ca2011-02-20 17:14:21 -0800698 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
699 i2c_readl(i2c_dev, I2C_INT_MASK));
700
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500701 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
702 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800703 tegra_i2c_mask_irq(i2c_dev, int_mask);
704
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500705 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800706 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
707
708 tegra_i2c_init(i2c_dev);
709 return -ETIMEDOUT;
710 }
711
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500712 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
713 time_left, completion_done(&i2c_dev->msg_complete),
714 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800715
716 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
717 return 0;
718
719 tegra_i2c_init(i2c_dev);
720 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
721 if (msg->flags & I2C_M_IGNORE_NAK)
722 return 0;
723 return -EREMOTEIO;
724 }
725
726 return -EIO;
727}
728
729static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
730 int num)
731{
732 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
733 int i;
734 int ret = 0;
735
736 if (i2c_dev->is_suspended)
737 return -EBUSY;
738
Jon Hunter1f50ad22016-08-26 14:09:04 +0100739 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000740 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100741 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000742 return ret;
743 }
744
Colin Crossdb811ca2011-02-20 17:14:21 -0800745 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530746 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +0100747
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530748 if (i < (num - 1)) {
749 if (msgs[i + 1].flags & I2C_M_NOSTART)
750 end_type = MSG_END_CONTINUE;
751 else
752 end_type = MSG_END_REPEAT_START;
753 }
754 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800755 if (ret)
756 break;
757 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100758
759 pm_runtime_put(i2c_dev->dev);
760
Colin Crossdb811ca2011-02-20 17:14:21 -0800761 return ret ?: i;
762}
763
764static u32 tegra_i2c_func(struct i2c_adapter *adap)
765{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530766 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200767 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
768 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530769
770 if (i2c_dev->hw->has_continue_xfer_support)
771 ret |= I2C_FUNC_NOSTART;
772 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800773}
774
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530775static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
776{
777 struct device_node *np = i2c_dev->dev->of_node;
778 int ret;
779
780 ret = of_property_read_u32(np, "clock-frequency",
781 &i2c_dev->bus_clk_rate);
782 if (ret)
783 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
784
785 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
786 "multi-master");
787}
788
Colin Crossdb811ca2011-02-20 17:14:21 -0800789static const struct i2c_algorithm tegra_i2c_algo = {
790 .master_xfer = tegra_i2c_xfer,
791 .functionality = tegra_i2c_func,
792};
793
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200794/* payload size is only 12 bit */
795static struct i2c_adapter_quirks tegra_i2c_quirks = {
796 .max_read_len = 4096,
Sowjanya Komatineni54589072019-02-12 11:06:44 -0800797 .max_write_len = 4096 - 12,
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200798};
799
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530800static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
801 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530802 .has_per_pkt_xfer_complete_irq = false,
803 .has_single_clk_source = false,
804 .clk_divisor_hs_mode = 3,
805 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530806 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530807 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530808 .has_multi_master_mode = false,
809 .has_slcg_override_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530810};
811
812static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
813 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530814 .has_per_pkt_xfer_complete_irq = false,
815 .has_single_clk_source = false,
816 .clk_divisor_hs_mode = 3,
817 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530818 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530819 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530820 .has_multi_master_mode = false,
821 .has_slcg_override_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530822};
823
824static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
825 .has_continue_xfer_support = true,
826 .has_per_pkt_xfer_complete_irq = true,
827 .has_single_clk_source = true,
828 .clk_divisor_hs_mode = 1,
829 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530830 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530831 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530832 .has_multi_master_mode = false,
833 .has_slcg_override_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530834};
835
836static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
837 .has_continue_xfer_support = true,
838 .has_per_pkt_xfer_complete_irq = true,
839 .has_single_clk_source = true,
840 .clk_divisor_hs_mode = 1,
841 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530842 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530843 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530844 .has_multi_master_mode = false,
845 .has_slcg_override_reg = true,
846};
847
848static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
849 .has_continue_xfer_support = true,
850 .has_per_pkt_xfer_complete_irq = true,
851 .has_single_clk_source = true,
852 .clk_divisor_hs_mode = 1,
853 .clk_divisor_std_fast_mode = 0x19,
854 .clk_divisor_fast_plus_mode = 0x10,
855 .has_config_load_reg = true,
856 .has_multi_master_mode = true,
857 .has_slcg_override_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530858};
859
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530860/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500861static const struct of_device_id tegra_i2c_of_match[] = {
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530862 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530863 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530864 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530865 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
866 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
867 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
868 {},
869};
870MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530871
Bill Pemberton0b255e92012-11-27 15:59:38 -0500872static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800873{
874 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800875 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530876 struct clk *div_clk;
877 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700878 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800879 int irq;
880 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300881 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800882
883 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100884 base = devm_ioremap_resource(&pdev->dev, res);
885 if (IS_ERR(base))
886 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800887
888 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
889 if (!res) {
890 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530891 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800892 }
893 irq = res->start;
894
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530895 div_clk = devm_clk_get(&pdev->dev, "div-clk");
896 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100897 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530898 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800899 }
900
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530901 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900902 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530903 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800904
905 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530906 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800907 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200908 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800909 i2c_dev->irq = irq;
910 i2c_dev->cont_id = pdev->id;
911 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700912
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700913 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
914 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100915 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700916 return PTR_ERR(i2c_dev->rst);
917 }
918
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530919 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800920
Jon Huntera9e32cd2016-08-26 14:09:01 +0100921 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
922 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
923 "nvidia,tegra20-i2c-dvc");
Colin Crossdb811ca2011-02-20 17:14:21 -0800924 init_completion(&i2c_dev->msg_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530925 spin_lock_init(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -0800926
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530927 if (!i2c_dev->hw->has_single_clk_source) {
928 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
929 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100930 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530931 return PTR_ERR(fast_clk);
932 }
933 i2c_dev->fast_clk = fast_clk;
934 }
935
Colin Crossdb811ca2011-02-20 17:14:21 -0800936 platform_set_drvdata(pdev, i2c_dev);
937
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300938 if (!i2c_dev->hw->has_single_clk_source) {
939 ret = clk_prepare(i2c_dev->fast_clk);
940 if (ret < 0) {
941 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
942 return ret;
943 }
944 }
945
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530946 i2c_dev->clk_divisor_non_hs_mode =
947 i2c_dev->hw->clk_divisor_std_fast_mode;
948 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
949 (i2c_dev->bus_clk_rate == 1000000))
950 i2c_dev->clk_divisor_non_hs_mode =
951 i2c_dev->hw->clk_divisor_fast_plus_mode;
952
953 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300954 ret = clk_set_rate(i2c_dev->div_clk,
955 i2c_dev->bus_clk_rate * clk_multiplier);
956 if (ret) {
957 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
958 goto unprepare_fast_clk;
959 }
960
961 ret = clk_prepare(i2c_dev->div_clk);
962 if (ret < 0) {
963 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
964 goto unprepare_fast_clk;
965 }
966
Jon Hunter1f50ad22016-08-26 14:09:04 +0100967 pm_runtime_enable(&pdev->dev);
968 if (!pm_runtime_enabled(&pdev->dev)) {
969 ret = tegra_i2c_runtime_resume(&pdev->dev);
970 if (ret < 0) {
971 dev_err(&pdev->dev, "runtime resume failed\n");
972 goto unprepare_div_clk;
973 }
974 }
975
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530976 if (i2c_dev->is_multimaster_mode) {
977 ret = clk_enable(i2c_dev->div_clk);
978 if (ret < 0) {
979 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
980 ret);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100981 goto disable_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530982 }
983 }
984
Colin Crossdb811ca2011-02-20 17:14:21 -0800985 ret = tegra_i2c_init(i2c_dev);
986 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100987 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Jon Huntereab09982016-06-14 21:26:46 +0100988 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800989 }
990
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530991 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530992 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800993 if (ret) {
994 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530995 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800996 }
997
Colin Crossdb811ca2011-02-20 17:14:21 -0800998 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
999 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +02001000 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +01001001 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -08001002 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -08001003 i2c_dev->adapter.dev.parent = &pdev->dev;
1004 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -07001005 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -08001006
1007 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +02001008 if (ret)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301009 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -08001010
Colin Crossdb811ca2011-02-20 17:14:21 -08001011 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001012
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301013disable_div_clk:
1014 if (i2c_dev->is_multimaster_mode)
1015 clk_disable(i2c_dev->div_clk);
1016
Jon Hunter1f50ad22016-08-26 14:09:04 +01001017disable_rpm:
1018 pm_runtime_disable(&pdev->dev);
1019 if (!pm_runtime_status_suspended(&pdev->dev))
1020 tegra_i2c_runtime_suspend(&pdev->dev);
1021
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001022unprepare_div_clk:
1023 clk_unprepare(i2c_dev->div_clk);
1024
1025unprepare_fast_clk:
1026 if (!i2c_dev->hw->has_single_clk_source)
1027 clk_unprepare(i2c_dev->fast_clk);
1028
1029 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001030}
1031
Bill Pemberton0b255e92012-11-27 15:59:38 -05001032static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001033{
1034 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +01001035
Colin Crossdb811ca2011-02-20 17:14:21 -08001036 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001037
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301038 if (i2c_dev->is_multimaster_mode)
1039 clk_disable(i2c_dev->div_clk);
1040
Jon Hunter1f50ad22016-08-26 14:09:04 +01001041 pm_runtime_disable(&pdev->dev);
1042 if (!pm_runtime_status_suspended(&pdev->dev))
1043 tegra_i2c_runtime_suspend(&pdev->dev);
1044
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001045 clk_unprepare(i2c_dev->div_clk);
1046 if (!i2c_dev->hw->has_single_clk_source)
1047 clk_unprepare(i2c_dev->fast_clk);
1048
Colin Crossdb811ca2011-02-20 17:14:21 -08001049 return 0;
1050}
1051
Laxman Dewangan371e67c2012-08-18 17:49:58 +05301052#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +02001053static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001054{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001055 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001056
1057 i2c_lock_adapter(&i2c_dev->adapter);
1058 i2c_dev->is_suspended = true;
1059 i2c_unlock_adapter(&i2c_dev->adapter);
1060
1061 return 0;
1062}
1063
Wolfram Sang5db20c42012-07-24 17:32:45 +02001064static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001065{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001066 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001067 int ret;
1068
1069 i2c_lock_adapter(&i2c_dev->adapter);
1070
1071 ret = tegra_i2c_init(i2c_dev);
Jon Hunterf4c2d892016-08-26 14:09:03 +01001072 if (!ret)
1073 i2c_dev->is_suspended = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001074
1075 i2c_unlock_adapter(&i2c_dev->adapter);
1076
Jon Hunterf4c2d892016-08-26 14:09:03 +01001077 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001078}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001079
Jon Hunter1f50ad22016-08-26 14:09:04 +01001080static const struct dev_pm_ops tegra_i2c_pm = {
1081 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1082 NULL)
1083 SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1084};
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001085#define TEGRA_I2C_PM (&tegra_i2c_pm)
1086#else
1087#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001088#endif
1089
1090static struct platform_driver tegra_i2c_driver = {
1091 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001092 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001093 .driver = {
1094 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001095 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001096 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001097 },
1098};
1099
1100static int __init tegra_i2c_init_driver(void)
1101{
1102 return platform_driver_register(&tegra_i2c_driver);
1103}
1104
1105static void __exit tegra_i2c_exit_driver(void)
1106{
1107 platform_driver_unregister(&tegra_i2c_driver);
1108}
1109
1110subsys_initcall(tegra_i2c_init_driver);
1111module_exit(tegra_i2c_exit_driver);
1112
1113MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1114MODULE_AUTHOR("Colin Cross");
1115MODULE_LICENSE("GPL v2");