blob: 05e34dc29d5ae08c3e02c99c180102891ff1b4c6 [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 Hunter1f50ad22016-08-26 14:09:04 +010031#include <linux/pm_runtime.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080032
33#include <asm/unaligned.h>
34
Colin Crossdb811ca2011-02-20 17:14:21 -080035#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
36#define BYTES_PER_FIFO_WORD 4
37
38#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060039#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010040#define I2C_CNFG_PACKET_MODE_EN BIT(10)
41#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
42#define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
Todd Poynorcb63c622011-04-25 15:32:25 -060043#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080044#define I2C_SL_CNFG 0x020
Jon Hunter2929be22016-08-26 14:08:58 +010045#define I2C_SL_CNFG_NACK BIT(1)
46#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080047#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060048#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080049#define I2C_TX_FIFO 0x050
50#define I2C_RX_FIFO 0x054
51#define I2C_PACKET_TRANSFER_STATUS 0x058
52#define I2C_FIFO_CONTROL 0x05c
Jon Hunter2929be22016-08-26 14:08:58 +010053#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
54#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080055#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
56#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
57#define I2C_FIFO_STATUS 0x060
58#define I2C_FIFO_STATUS_TX_MASK 0xF0
59#define I2C_FIFO_STATUS_TX_SHIFT 4
60#define I2C_FIFO_STATUS_RX_MASK 0x0F
61#define I2C_FIFO_STATUS_RX_SHIFT 0
62#define I2C_INT_MASK 0x064
63#define I2C_INT_STATUS 0x068
Jon Hunter2929be22016-08-26 14:08:58 +010064#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
65#define I2C_INT_ALL_PACKETS_XFER_COMPLETE BIT(6)
66#define I2C_INT_TX_FIFO_OVERFLOW BIT(5)
67#define I2C_INT_RX_FIFO_UNDERFLOW BIT(4)
68#define I2C_INT_NO_ACK BIT(3)
69#define I2C_INT_ARBITRATION_LOST BIT(2)
70#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
71#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080072#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053073#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
74#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080075
76#define DVC_CTRL_REG1 0x000
Jon Hunter2929be22016-08-26 14:08:58 +010077#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080078#define DVC_CTRL_REG2 0x004
79#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010080#define DVC_CTRL_REG3_SW_PROG BIT(26)
81#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080082#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010083#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080084
85#define I2C_ERR_NONE 0x00
86#define I2C_ERR_NO_ACK 0x01
87#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060088#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080089
90#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
91#define PACKET_HEADER0_PACKET_ID_SHIFT 16
92#define PACKET_HEADER0_CONT_ID_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010093#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080094
Jon Hunter2929be22016-08-26 14:08:58 +010095#define I2C_HEADER_HIGHSPEED_MODE BIT(22)
96#define I2C_HEADER_CONT_ON_NAK BIT(21)
97#define I2C_HEADER_SEND_START_BYTE BIT(20)
98#define I2C_HEADER_READ BIT(19)
99#define I2C_HEADER_10BIT_ADDR BIT(18)
100#define I2C_HEADER_IE_ENABLE BIT(17)
101#define I2C_HEADER_REPEAT_START BIT(16)
102#define I2C_HEADER_CONTINUE_XFER BIT(15)
Colin Crossdb811ca2011-02-20 17:14:21 -0800103#define I2C_HEADER_MASTER_ADDR_SHIFT 12
104#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530105
106#define I2C_CONFIG_LOAD 0x08C
Jon Hunter2929be22016-08-26 14:08:58 +0100107#define I2C_MSTR_CONFIG_LOAD BIT(0)
108#define I2C_SLV_CONFIG_LOAD BIT(1)
109#define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530110
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530111#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100112#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530113
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530114/*
115 * msg_end_type: The bus control which need to be send at end of transfer.
116 * @MSG_END_STOP: Send stop pulse at end of transfer.
117 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
118 * @MSG_END_CONTINUE: The following on message is coming and so do not send
119 * stop or repeat start.
120 */
121enum msg_end_type {
122 MSG_END_STOP,
123 MSG_END_REPEAT_START,
124 MSG_END_CONTINUE,
125};
Colin Crossdb811ca2011-02-20 17:14:21 -0800126
127/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530128 * struct tegra_i2c_hw_feature : Different HW support on Tegra
129 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530130 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
131 * complete interrupt per packet basis.
132 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
133 * and earlier Socs has two clock sources i.e. div-clk and
134 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530135 * @has_config_load_reg: Has the config load register to load the new
136 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530137 * @clk_divisor_hs_mode: Clock divisor in HS mode.
138 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
139 * applicable if there is no fast clock source i.e. single clock
140 * source.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530141 */
142
143struct tegra_i2c_hw_feature {
144 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530145 bool has_per_pkt_xfer_complete_irq;
146 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530147 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530148 int clk_divisor_hs_mode;
149 int clk_divisor_std_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530150 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530151 bool has_multi_master_mode;
152 bool has_slcg_override_reg;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530153};
154
155/**
Colin Crossdb811ca2011-02-20 17:14:21 -0800156 * struct tegra_i2c_dev - per device i2c context
157 * @dev: device reference for power management
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530158 * @hw: Tegra i2c hw feature.
Colin Crossdb811ca2011-02-20 17:14:21 -0800159 * @adapter: core i2c layer adapter information
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530160 * @div_clk: clock reference for div clock of i2c controller.
161 * @fast_clk: clock reference for fast clock of i2c controller.
Colin Crossdb811ca2011-02-20 17:14:21 -0800162 * @base: ioremapped registers cookie
163 * @cont_id: i2c controller id, used for for packet header
164 * @irq: irq number of transfer complete interrupt
165 * @is_dvc: identifies the DVC i2c controller, has a different register layout
166 * @msg_complete: transfer completion notifier
167 * @msg_err: error code for completed message
168 * @msg_buf: pointer to current message data
169 * @msg_buf_remaining: size of unsent data in the message buffer
170 * @msg_read: identifies read transfers
171 * @bus_clk_rate: current i2c bus clock rate
172 * @is_suspended: prevents i2c controller accesses after suspend is called
173 */
174struct tegra_i2c_dev {
175 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530176 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800177 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530178 struct clk *div_clk;
179 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700180 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800181 void __iomem *base;
182 int cont_id;
183 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600184 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800185 int is_dvc;
186 struct completion msg_complete;
187 int msg_err;
188 u8 *msg_buf;
189 size_t msg_buf_remaining;
190 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000191 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530192 u16 clk_divisor_non_hs_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800193 bool is_suspended;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530194 bool is_multimaster_mode;
Colin Crossdb811ca2011-02-20 17:14:21 -0800195};
196
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100197static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
198 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800199{
200 writel(val, i2c_dev->base + reg);
201}
202
203static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
204{
205 return readl(i2c_dev->base + reg);
206}
207
208/*
209 * i2c_writel and i2c_readl will offset the register if necessary to talk
210 * to the I2C block inside the DVC block
211 */
212static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
213 unsigned long reg)
214{
215 if (i2c_dev->is_dvc)
216 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
217 return reg;
218}
219
220static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
221 unsigned long reg)
222{
223 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530224
225 /* Read back register to make sure that register writes completed */
226 if (reg != I2C_TX_FIFO)
227 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800228}
229
230static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
231{
232 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
233}
234
235static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
236 unsigned long reg, int len)
237{
238 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
239}
240
241static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
242 unsigned long reg, int len)
243{
244 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
245}
246
247static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
248{
Jon Hunterf5076682016-08-26 14:08:59 +0100249 u32 int_mask;
250
251 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800252 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
253}
254
255static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
256{
Jon Hunterf5076682016-08-26 14:08:59 +0100257 u32 int_mask;
258
259 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800260 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
261}
262
263static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
264{
265 unsigned long timeout = jiffies + HZ;
266 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
Jon Hunterf5076682016-08-26 14:08:59 +0100267
Colin Crossdb811ca2011-02-20 17:14:21 -0800268 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
269 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
270
271 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
272 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
273 if (time_after(jiffies, timeout)) {
274 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
275 return -ETIMEDOUT;
276 }
277 msleep(1);
278 }
279 return 0;
280}
281
282static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
283{
284 u32 val;
285 int rx_fifo_avail;
286 u8 *buf = i2c_dev->msg_buf;
287 size_t buf_remaining = i2c_dev->msg_buf_remaining;
288 int words_to_transfer;
289
290 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
291 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
292 I2C_FIFO_STATUS_RX_SHIFT;
293
294 /* Rounds down to not include partial word at the end of buf */
295 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
296 if (words_to_transfer > rx_fifo_avail)
297 words_to_transfer = rx_fifo_avail;
298
299 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
300
301 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
302 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
303 rx_fifo_avail -= words_to_transfer;
304
305 /*
306 * If there is a partial word at the end of buf, handle it manually to
307 * prevent overwriting past the end of buf
308 */
309 if (rx_fifo_avail > 0 && buf_remaining > 0) {
310 BUG_ON(buf_remaining > 3);
311 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300312 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800313 memcpy(buf, &val, buf_remaining);
314 buf_remaining = 0;
315 rx_fifo_avail--;
316 }
317
318 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
319 i2c_dev->msg_buf_remaining = buf_remaining;
320 i2c_dev->msg_buf = buf;
321 return 0;
322}
323
324static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
325{
326 u32 val;
327 int tx_fifo_avail;
328 u8 *buf = i2c_dev->msg_buf;
329 size_t buf_remaining = i2c_dev->msg_buf_remaining;
330 int words_to_transfer;
331
332 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
333 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
334 I2C_FIFO_STATUS_TX_SHIFT;
335
336 /* Rounds down to not include partial word at the end of buf */
337 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800338
Doug Anderson96219c32011-08-30 11:46:10 -0600339 /* It's very common to have < 4 bytes, so optimize that case. */
340 if (words_to_transfer) {
341 if (words_to_transfer > tx_fifo_avail)
342 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800343
Doug Anderson96219c32011-08-30 11:46:10 -0600344 /*
345 * Update state before writing to FIFO. If this casues us
346 * to finish writing all bytes (AKA buf_remaining goes to 0) we
347 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
348 * not maskable). We need to make sure that the isr sees
349 * buf_remaining as 0 and doesn't call us back re-entrantly.
350 */
351 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
352 tx_fifo_avail -= words_to_transfer;
353 i2c_dev->msg_buf_remaining = buf_remaining;
354 i2c_dev->msg_buf = buf +
355 words_to_transfer * BYTES_PER_FIFO_WORD;
356 barrier();
357
358 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
359
360 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
361 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800362
363 /*
364 * If there is a partial word at the end of buf, handle it manually to
365 * prevent reading past the end of buf, which could cross a page
366 * boundary and fault.
367 */
368 if (tx_fifo_avail > 0 && buf_remaining > 0) {
369 BUG_ON(buf_remaining > 3);
370 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300371 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600372
373 /* Again update before writing to FIFO to make sure isr sees. */
374 i2c_dev->msg_buf_remaining = 0;
375 i2c_dev->msg_buf = NULL;
376 barrier();
377
Colin Crossdb811ca2011-02-20 17:14:21 -0800378 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800379 }
380
Colin Crossdb811ca2011-02-20 17:14:21 -0800381 return 0;
382}
383
384/*
385 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
386 * block. This block is identical to the rest of the I2C blocks, except that
387 * it only supports master mode, it has registers moved around, and it needs
388 * some extra init to get it into I2C mode. The register moves are handled
389 * by i2c_readl and i2c_writel
390 */
391static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
392{
Jon Hunterf5076682016-08-26 14:08:59 +0100393 u32 val;
394
Colin Crossdb811ca2011-02-20 17:14:21 -0800395 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
396 val |= DVC_CTRL_REG3_SW_PROG;
397 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
398 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
399
400 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
401 val |= DVC_CTRL_REG1_INTR_EN;
402 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
403}
404
Jon Hunter1f50ad22016-08-26 14:09:04 +0100405static int tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530406{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100407 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530408 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100409
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530410 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300411 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530412 if (ret < 0) {
413 dev_err(i2c_dev->dev,
414 "Enabling fast clk failed, err %d\n", ret);
415 return ret;
416 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530417 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100418
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300419 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530420 if (ret < 0) {
421 dev_err(i2c_dev->dev,
422 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300423 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100424 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530425 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100426
427 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530428}
429
Jon Hunter1f50ad22016-08-26 14:09:04 +0100430static int tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530431{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100432 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
433
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300434 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530435 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300436 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100437
438 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530439}
440
Colin Crossdb811ca2011-02-20 17:14:21 -0800441static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
442{
443 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100444 int err;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530445 u32 clk_divisor;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530446 unsigned long timeout = jiffies + HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -0800447
Jon Hunter1f50ad22016-08-26 14:09:04 +0100448 err = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000449 if (err < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100450 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000451 return err;
452 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800453
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700454 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800455 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700456 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800457
458 if (i2c_dev->is_dvc)
459 tegra_dvc_init(i2c_dev);
460
Jay Cheng40abcf72011-04-25 15:32:27 -0600461 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
462 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530463
464 if (i2c_dev->hw->has_multi_master_mode)
465 val |= I2C_CNFG_MULTI_MASTER_MODE;
466
Colin Crossdb811ca2011-02-20 17:14:21 -0800467 i2c_writel(i2c_dev, val, I2C_CNFG);
468 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530469
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530470 /* Make sure clock divisor programmed correctly */
471 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530472 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530473 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
474 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800475
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600476 if (!i2c_dev->is_dvc) {
477 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100478
Stephen Warren5afa9d32011-06-06 11:25:19 -0600479 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
480 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
481 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
482 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600483 }
484
Colin Crossdb811ca2011-02-20 17:14:21 -0800485 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
486 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
487 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
488
Jon Hunter1f50ad22016-08-26 14:09:04 +0100489 err = tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800490
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530491 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
492 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
493
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530494 if (i2c_dev->hw->has_config_load_reg) {
495 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
496 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
497 if (time_after(jiffies, timeout)) {
498 dev_warn(i2c_dev->dev,
499 "timeout waiting for config load\n");
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530500 err = -ETIMEDOUT;
501 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530502 }
503 msleep(1);
504 }
505 }
506
Todd Poynorcb63c622011-04-25 15:32:25 -0600507 if (i2c_dev->irq_disabled) {
508 i2c_dev->irq_disabled = 0;
509 enable_irq(i2c_dev->irq);
510 }
511
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530512err:
Jon Hunter1f50ad22016-08-26 14:09:04 +0100513 pm_runtime_put(i2c_dev->dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800514 return err;
515}
516
517static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
518{
519 u32 status;
520 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
521 struct tegra_i2c_dev *i2c_dev = dev_id;
522
523 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
524
525 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600526 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
527 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
528 i2c_readl(i2c_dev, I2C_STATUS),
529 i2c_readl(i2c_dev, I2C_CNFG));
530 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
531
532 if (!i2c_dev->irq_disabled) {
533 disable_irq_nosync(i2c_dev->irq);
534 i2c_dev->irq_disabled = 1;
535 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600536 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800537 }
538
539 if (unlikely(status & status_err)) {
540 if (status & I2C_INT_NO_ACK)
541 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
542 if (status & I2C_INT_ARBITRATION_LOST)
543 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800544 goto err;
545 }
546
547 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
548 if (i2c_dev->msg_buf_remaining)
549 tegra_i2c_empty_rx_fifo(i2c_dev);
550 else
551 BUG();
552 }
553
554 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
555 if (i2c_dev->msg_buf_remaining)
556 tegra_i2c_fill_tx_fifo(i2c_dev);
557 else
558 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
559 }
560
Laxman Dewanganc889e912012-05-07 12:16:19 +0530561 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
562 if (i2c_dev->is_dvc)
563 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
564
Doug Anderson96219c32011-08-30 11:46:10 -0600565 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
566 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800567 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600568 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800569 return IRQ_HANDLED;
570err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300571 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800572 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
573 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
574 I2C_INT_RX_FIFO_DATA_REQ);
575 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600576 if (i2c_dev->is_dvc)
577 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530578
579 complete(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800580 return IRQ_HANDLED;
581}
582
583static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530584 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -0800585{
586 u32 packet_header;
587 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500588 unsigned long time_left;
Colin Crossdb811ca2011-02-20 17:14:21 -0800589
590 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800591
592 if (msg->len == 0)
593 return -EINVAL;
594
595 i2c_dev->msg_buf = msg->buf;
596 i2c_dev->msg_buf_remaining = msg->len;
597 i2c_dev->msg_err = I2C_ERR_NONE;
598 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -0800599 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -0800600
601 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
602 PACKET_HEADER0_PROTOCOL_I2C |
603 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
604 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
605 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
606
607 packet_header = msg->len - 1;
608 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
609
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530610 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530611 if (end_state == MSG_END_CONTINUE)
612 packet_header |= I2C_HEADER_CONTINUE_XFER;
613 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -0600614 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530615 if (msg->flags & I2C_M_TEN) {
616 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -0800617 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +0530618 } else {
619 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
620 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800621 if (msg->flags & I2C_M_IGNORE_NAK)
622 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -0800623 if (msg->flags & I2C_M_RD)
624 packet_header |= I2C_HEADER_READ;
625 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
626
627 if (!(msg->flags & I2C_M_RD))
628 tegra_i2c_fill_tx_fifo(i2c_dev);
629
630 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530631 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
632 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800633 if (msg->flags & I2C_M_RD)
634 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
635 else if (i2c_dev->msg_buf_remaining)
636 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
637 tegra_i2c_unmask_irq(i2c_dev, int_mask);
638 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
639 i2c_readl(i2c_dev, I2C_INT_MASK));
640
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500641 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
642 TEGRA_I2C_TIMEOUT);
Colin Crossdb811ca2011-02-20 17:14:21 -0800643 tegra_i2c_mask_irq(i2c_dev, int_mask);
644
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500645 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800646 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
647
648 tegra_i2c_init(i2c_dev);
649 return -ETIMEDOUT;
650 }
651
Nicholas Mc Guire6973a392015-03-01 09:17:41 -0500652 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
653 time_left, completion_done(&i2c_dev->msg_complete),
654 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -0800655
656 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
657 return 0;
658
Alok Chauhanf70893d02012-04-02 11:23:02 +0530659 /*
Jon Hunterc7ae44e2016-08-26 14:08:57 +0100660 * NACK interrupt is generated before the I2C controller generates
661 * the STOP condition on the bus. So wait for 2 clock periods
662 * before resetting the controller so that the STOP condition has
663 * been delivered properly.
Alok Chauhanf70893d02012-04-02 11:23:02 +0530664 */
665 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
666 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
667
Colin Crossdb811ca2011-02-20 17:14:21 -0800668 tegra_i2c_init(i2c_dev);
669 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
670 if (msg->flags & I2C_M_IGNORE_NAK)
671 return 0;
672 return -EREMOTEIO;
673 }
674
675 return -EIO;
676}
677
678static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
679 int num)
680{
681 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
682 int i;
683 int ret = 0;
684
685 if (i2c_dev->is_suspended)
686 return -EBUSY;
687
Jon Hunter1f50ad22016-08-26 14:09:04 +0100688 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000689 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100690 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000691 return ret;
692 }
693
Colin Crossdb811ca2011-02-20 17:14:21 -0800694 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530695 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +0100696
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530697 if (i < (num - 1)) {
698 if (msgs[i + 1].flags & I2C_M_NOSTART)
699 end_type = MSG_END_CONTINUE;
700 else
701 end_type = MSG_END_REPEAT_START;
702 }
703 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -0800704 if (ret)
705 break;
706 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100707
708 pm_runtime_put(i2c_dev->dev);
709
Colin Crossdb811ca2011-02-20 17:14:21 -0800710 return ret ?: i;
711}
712
713static u32 tegra_i2c_func(struct i2c_adapter *adap)
714{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530715 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +0200716 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
717 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530718
719 if (i2c_dev->hw->has_continue_xfer_support)
720 ret |= I2C_FUNC_NOSTART;
721 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800722}
723
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530724static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
725{
726 struct device_node *np = i2c_dev->dev->of_node;
727 int ret;
728
729 ret = of_property_read_u32(np, "clock-frequency",
730 &i2c_dev->bus_clk_rate);
731 if (ret)
732 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
733
734 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
735 "multi-master");
736}
737
Colin Crossdb811ca2011-02-20 17:14:21 -0800738static const struct i2c_algorithm tegra_i2c_algo = {
739 .master_xfer = tegra_i2c_xfer,
740 .functionality = tegra_i2c_func,
741};
742
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200743/* payload size is only 12 bit */
744static struct i2c_adapter_quirks tegra_i2c_quirks = {
745 .max_read_len = 4096,
746 .max_write_len = 4096,
747};
748
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530749static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
750 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530751 .has_per_pkt_xfer_complete_irq = false,
752 .has_single_clk_source = false,
753 .clk_divisor_hs_mode = 3,
754 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530755 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530756 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530757 .has_multi_master_mode = false,
758 .has_slcg_override_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530759};
760
761static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
762 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530763 .has_per_pkt_xfer_complete_irq = false,
764 .has_single_clk_source = false,
765 .clk_divisor_hs_mode = 3,
766 .clk_divisor_std_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530767 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530768 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530769 .has_multi_master_mode = false,
770 .has_slcg_override_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530771};
772
773static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
774 .has_continue_xfer_support = true,
775 .has_per_pkt_xfer_complete_irq = true,
776 .has_single_clk_source = true,
777 .clk_divisor_hs_mode = 1,
778 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530779 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530780 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530781 .has_multi_master_mode = false,
782 .has_slcg_override_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530783};
784
785static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
786 .has_continue_xfer_support = true,
787 .has_per_pkt_xfer_complete_irq = true,
788 .has_single_clk_source = true,
789 .clk_divisor_hs_mode = 1,
790 .clk_divisor_std_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530791 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530792 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530793 .has_multi_master_mode = false,
794 .has_slcg_override_reg = true,
795};
796
797static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
798 .has_continue_xfer_support = true,
799 .has_per_pkt_xfer_complete_irq = true,
800 .has_single_clk_source = true,
801 .clk_divisor_hs_mode = 1,
802 .clk_divisor_std_fast_mode = 0x19,
803 .clk_divisor_fast_plus_mode = 0x10,
804 .has_config_load_reg = true,
805 .has_multi_master_mode = true,
806 .has_slcg_override_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530807};
808
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530809/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -0500810static const struct of_device_id tegra_i2c_of_match[] = {
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530811 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530812 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530813 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530814 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
815 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
816 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
817 {},
818};
819MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530820
Bill Pemberton0b255e92012-11-27 15:59:38 -0500821static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800822{
823 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -0800824 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530825 struct clk *div_clk;
826 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -0700827 void __iomem *base;
Colin Crossdb811ca2011-02-20 17:14:21 -0800828 int irq;
829 int ret = 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300830 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
Colin Crossdb811ca2011-02-20 17:14:21 -0800831
832 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding84dbf802013-01-21 11:09:03 +0100833 base = devm_ioremap_resource(&pdev->dev, res);
834 if (IS_ERR(base))
835 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -0800836
837 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
838 if (!res) {
839 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530840 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -0800841 }
842 irq = res->start;
843
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530844 div_clk = devm_clk_get(&pdev->dev, "div-clk");
845 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100846 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530847 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -0800848 }
849
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530850 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +0900851 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530852 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -0800853
854 i2c_dev->base = base;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530855 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800856 i2c_dev->adapter.algo = &tegra_i2c_algo;
Wolfram Sang3aaa34b2015-06-16 19:57:29 +0200857 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
Colin Crossdb811ca2011-02-20 17:14:21 -0800858 i2c_dev->irq = irq;
859 i2c_dev->cont_id = pdev->id;
860 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -0700861
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700862 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
863 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100864 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700865 return PTR_ERR(i2c_dev->rst);
866 }
867
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530868 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800869
Jon Huntera9e32cd2016-08-26 14:09:01 +0100870 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
871 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
872 "nvidia,tegra20-i2c-dvc");
Colin Crossdb811ca2011-02-20 17:14:21 -0800873 init_completion(&i2c_dev->msg_complete);
874
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530875 if (!i2c_dev->hw->has_single_clk_source) {
876 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
877 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100878 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530879 return PTR_ERR(fast_clk);
880 }
881 i2c_dev->fast_clk = fast_clk;
882 }
883
Colin Crossdb811ca2011-02-20 17:14:21 -0800884 platform_set_drvdata(pdev, i2c_dev);
885
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300886 if (!i2c_dev->hw->has_single_clk_source) {
887 ret = clk_prepare(i2c_dev->fast_clk);
888 if (ret < 0) {
889 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
890 return ret;
891 }
892 }
893
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530894 i2c_dev->clk_divisor_non_hs_mode =
895 i2c_dev->hw->clk_divisor_std_fast_mode;
896 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
897 (i2c_dev->bus_clk_rate == 1000000))
898 i2c_dev->clk_divisor_non_hs_mode =
899 i2c_dev->hw->clk_divisor_fast_plus_mode;
900
901 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300902 ret = clk_set_rate(i2c_dev->div_clk,
903 i2c_dev->bus_clk_rate * clk_multiplier);
904 if (ret) {
905 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
906 goto unprepare_fast_clk;
907 }
908
909 ret = clk_prepare(i2c_dev->div_clk);
910 if (ret < 0) {
911 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
912 goto unprepare_fast_clk;
913 }
914
Jon Hunter1f50ad22016-08-26 14:09:04 +0100915 pm_runtime_enable(&pdev->dev);
916 if (!pm_runtime_enabled(&pdev->dev)) {
917 ret = tegra_i2c_runtime_resume(&pdev->dev);
918 if (ret < 0) {
919 dev_err(&pdev->dev, "runtime resume failed\n");
920 goto unprepare_div_clk;
921 }
922 }
923
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530924 if (i2c_dev->is_multimaster_mode) {
925 ret = clk_enable(i2c_dev->div_clk);
926 if (ret < 0) {
927 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
928 ret);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100929 goto disable_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530930 }
931 }
932
Colin Crossdb811ca2011-02-20 17:14:21 -0800933 ret = tegra_i2c_init(i2c_dev);
934 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +0100935 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Jon Huntereab09982016-06-14 21:26:46 +0100936 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800937 }
938
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +0530939 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +0530940 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800941 if (ret) {
942 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530943 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800944 }
945
Colin Crossdb811ca2011-02-20 17:14:21 -0800946 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
947 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +0200948 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +0100949 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -0800950 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -0800951 i2c_dev->adapter.dev.parent = &pdev->dev;
952 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -0700953 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -0800954
955 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +0200956 if (ret)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530957 goto disable_div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -0800958
Colin Crossdb811ca2011-02-20 17:14:21 -0800959 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300960
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530961disable_div_clk:
962 if (i2c_dev->is_multimaster_mode)
963 clk_disable(i2c_dev->div_clk);
964
Jon Hunter1f50ad22016-08-26 14:09:04 +0100965disable_rpm:
966 pm_runtime_disable(&pdev->dev);
967 if (!pm_runtime_status_suspended(&pdev->dev))
968 tegra_i2c_runtime_suspend(&pdev->dev);
969
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300970unprepare_div_clk:
971 clk_unprepare(i2c_dev->div_clk);
972
973unprepare_fast_clk:
974 if (!i2c_dev->hw->has_single_clk_source)
975 clk_unprepare(i2c_dev->fast_clk);
976
977 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -0800978}
979
Bill Pemberton0b255e92012-11-27 15:59:38 -0500980static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -0800981{
982 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +0100983
Colin Crossdb811ca2011-02-20 17:14:21 -0800984 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300985
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530986 if (i2c_dev->is_multimaster_mode)
987 clk_disable(i2c_dev->div_clk);
988
Jon Hunter1f50ad22016-08-26 14:09:04 +0100989 pm_runtime_disable(&pdev->dev);
990 if (!pm_runtime_status_suspended(&pdev->dev))
991 tegra_i2c_runtime_suspend(&pdev->dev);
992
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300993 clk_unprepare(i2c_dev->div_clk);
994 if (!i2c_dev->hw->has_single_clk_source)
995 clk_unprepare(i2c_dev->fast_clk);
996
Colin Crossdb811ca2011-02-20 17:14:21 -0800997 return 0;
998}
999
Laxman Dewangan371e67c2012-08-18 17:49:58 +05301000#ifdef CONFIG_PM_SLEEP
Wolfram Sang5db20c42012-07-24 17:32:45 +02001001static int tegra_i2c_suspend(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001002{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001003 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001004
1005 i2c_lock_adapter(&i2c_dev->adapter);
1006 i2c_dev->is_suspended = true;
1007 i2c_unlock_adapter(&i2c_dev->adapter);
1008
1009 return 0;
1010}
1011
Wolfram Sang5db20c42012-07-24 17:32:45 +02001012static int tegra_i2c_resume(struct device *dev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001013{
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001014 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001015 int ret;
1016
1017 i2c_lock_adapter(&i2c_dev->adapter);
1018
1019 ret = tegra_i2c_init(i2c_dev);
Jon Hunterf4c2d892016-08-26 14:09:03 +01001020 if (!ret)
1021 i2c_dev->is_suspended = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001022
1023 i2c_unlock_adapter(&i2c_dev->adapter);
1024
Jon Hunterf4c2d892016-08-26 14:09:03 +01001025 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001026}
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001027
Jon Hunter1f50ad22016-08-26 14:09:04 +01001028static const struct dev_pm_ops tegra_i2c_pm = {
1029 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1030 NULL)
1031 SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1032};
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001033#define TEGRA_I2C_PM (&tegra_i2c_pm)
1034#else
1035#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001036#endif
1037
1038static struct platform_driver tegra_i2c_driver = {
1039 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001040 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001041 .driver = {
1042 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001043 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001044 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001045 },
1046};
1047
1048static int __init tegra_i2c_init_driver(void)
1049{
1050 return platform_driver_register(&tegra_i2c_driver);
1051}
1052
1053static void __exit tegra_i2c_exit_driver(void)
1054{
1055 platform_driver_unregister(&tegra_i2c_driver);
1056}
1057
1058subsys_initcall(tegra_i2c_init_driver);
1059module_exit(tegra_i2c_exit_driver);
1060
1061MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1062MODULE_AUTHOR("Colin Cross");
1063MODULE_LICENSE("GPL v2");