blob: db94e96aed77ec73632f7ff2b0785f3aaa418f50 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * drivers/i2c/busses/i2c-tegra.c
4 *
5 * Copyright (C) 2010 Google, Inc.
6 * Author: Colin Cross <ccross@android.com>
7 */
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/dmaengine.h>
12#include <linux/dma-mapping.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/irq.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/of_device.h>
23#include <linux/pinctrl/consumer.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/reset.h>
27
28#define BYTES_PER_FIFO_WORD 4
29
30#define I2C_CNFG 0x000
31#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
32#define I2C_CNFG_PACKET_MODE_EN BIT(10)
33#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
34#define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
35#define I2C_STATUS 0x01C
36#define I2C_SL_CNFG 0x020
37#define I2C_SL_CNFG_NACK BIT(1)
38#define I2C_SL_CNFG_NEWSL BIT(2)
39#define I2C_SL_ADDR1 0x02c
40#define I2C_SL_ADDR2 0x030
41#define I2C_TX_FIFO 0x050
42#define I2C_RX_FIFO 0x054
43#define I2C_PACKET_TRANSFER_STATUS 0x058
44#define I2C_FIFO_CONTROL 0x05c
45#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
46#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
47#define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
48#define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
49#define I2C_FIFO_STATUS 0x060
50#define I2C_FIFO_STATUS_TX_MASK 0xF0
51#define I2C_FIFO_STATUS_TX_SHIFT 4
52#define I2C_FIFO_STATUS_RX_MASK 0x0F
53#define I2C_FIFO_STATUS_RX_SHIFT 0
54#define I2C_INT_MASK 0x064
55#define I2C_INT_STATUS 0x068
56#define I2C_INT_BUS_CLR_DONE BIT(11)
57#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
58#define I2C_INT_NO_ACK BIT(3)
59#define I2C_INT_ARBITRATION_LOST BIT(2)
60#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
61#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
62#define I2C_CLK_DIVISOR 0x06c
63#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
64
65#define DVC_CTRL_REG1 0x000
66#define DVC_CTRL_REG1_INTR_EN BIT(10)
67#define DVC_CTRL_REG3 0x008
68#define DVC_CTRL_REG3_SW_PROG BIT(26)
69#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
70#define DVC_STATUS 0x00c
71#define DVC_STATUS_I2C_DONE_INTR BIT(30)
72
73#define I2C_ERR_NONE 0x00
74#define I2C_ERR_NO_ACK BIT(0)
75#define I2C_ERR_ARBITRATION_LOST BIT(1)
76#define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
77#define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
78
79#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
80#define PACKET_HEADER0_PACKET_ID_SHIFT 16
81#define PACKET_HEADER0_CONT_ID_SHIFT 12
82#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
83
84#define I2C_HEADER_CONT_ON_NAK BIT(21)
85#define I2C_HEADER_READ BIT(19)
86#define I2C_HEADER_10BIT_ADDR BIT(18)
87#define I2C_HEADER_IE_ENABLE BIT(17)
88#define I2C_HEADER_REPEAT_START BIT(16)
89#define I2C_HEADER_CONTINUE_XFER BIT(15)
90#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
91
92#define I2C_BUS_CLEAR_CNFG 0x084
93#define I2C_BC_SCLK_THRESHOLD 9
94#define I2C_BC_SCLK_THRESHOLD_SHIFT 16
95#define I2C_BC_STOP_COND BIT(2)
96#define I2C_BC_TERMINATE BIT(1)
97#define I2C_BC_ENABLE BIT(0)
98#define I2C_BUS_CLEAR_STATUS 0x088
99#define I2C_BC_STATUS BIT(0)
100
101#define I2C_CONFIG_LOAD 0x08C
102#define I2C_MSTR_CONFIG_LOAD BIT(0)
103
104#define I2C_CLKEN_OVERRIDE 0x090
105#define I2C_MST_CORE_CLKEN_OVR BIT(0)
106
107#define I2C_CONFIG_LOAD_TIMEOUT 1000000
108
109#define I2C_MST_FIFO_CONTROL 0x0b4
110#define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
111#define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
112#define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
113#define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
114
115#define I2C_MST_FIFO_STATUS 0x0b8
116#define I2C_MST_FIFO_STATUS_RX_MASK 0xff
117#define I2C_MST_FIFO_STATUS_RX_SHIFT 0
118#define I2C_MST_FIFO_STATUS_TX_MASK 0xff0000
119#define I2C_MST_FIFO_STATUS_TX_SHIFT 16
120
121#define I2C_INTERFACE_TIMING_0 0x94
122#define I2C_THIGH_SHIFT 8
123#define I2C_INTERFACE_TIMING_1 0x98
124
125#define I2C_STANDARD_MODE 100000
126#define I2C_FAST_MODE 400000
127#define I2C_FAST_PLUS_MODE 1000000
128
129/* Packet header size in bytes */
130#define I2C_PACKET_HEADER_SIZE 12
131
132/*
133 * Upto I2C_PIO_MODE_MAX_LEN bytes, controller will use PIO mode,
134 * above this, controller will use DMA to fill FIFO.
135 * MAX PIO len is 20 bytes excluding packet header.
136 */
137#define I2C_PIO_MODE_MAX_LEN 32
138
139/*
140 * msg_end_type: The bus control which need to be send at end of transfer.
141 * @MSG_END_STOP: Send stop pulse at end of transfer.
142 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
143 * @MSG_END_CONTINUE: The following on message is coming and so do not send
144 * stop or repeat start.
145 */
146enum msg_end_type {
147 MSG_END_STOP,
148 MSG_END_REPEAT_START,
149 MSG_END_CONTINUE,
150};
151
152/**
153 * struct tegra_i2c_hw_feature : Different HW support on Tegra
154 * @has_continue_xfer_support: Continue transfer supports.
155 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
156 * complete interrupt per packet basis.
157 * @has_single_clk_source: The I2C controller has single clock source. Tegra30
158 * and earlier SoCs have two clock sources i.e. div-clk and
159 * fast-clk.
160 * @has_config_load_reg: Has the config load register to load the new
161 * configuration.
162 * @clk_divisor_hs_mode: Clock divisor in HS mode.
163 * @clk_divisor_std_mode: Clock divisor in standard mode. It is
164 * applicable if there is no fast clock source i.e. single clock
165 * source.
166 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
167 * applicable if there is no fast clock source i.e. single clock
168 * source.
169 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
170 * applicable if there is no fast clock source (i.e. single
171 * clock source).
172 * @has_multi_master_mode: The I2C controller supports running in single-master
173 * or multi-master mode.
174 * @has_slcg_override_reg: The I2C controller supports a register that
175 * overrides the second level clock gating.
176 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
177 * provides additional features and allows for longer messages to
178 * be transferred in one go.
179 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
180 * allowing 0 length transfers.
181 * @supports_bus_clear: Bus Clear support to recover from bus hang during
182 * SDA stuck low from device for some unknown reasons.
183 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
184 * @tlow_std_mode: Low period of the clock in standard mode.
185 * @thigh_std_mode: High period of the clock in standard mode.
186 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
187 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
188 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
189 * in standard mode.
190 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
191 * conditions in fast/fast-plus modes.
192 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
193 * in HS mode.
194 * @has_interface_timing_reg: Has interface timing register to program the tuned
195 * timing settings.
196 */
197struct tegra_i2c_hw_feature {
198 bool has_continue_xfer_support;
199 bool has_per_pkt_xfer_complete_irq;
200 bool has_single_clk_source;
201 bool has_config_load_reg;
202 int clk_divisor_hs_mode;
203 int clk_divisor_std_mode;
204 int clk_divisor_fast_mode;
205 u16 clk_divisor_fast_plus_mode;
206 bool has_multi_master_mode;
207 bool has_slcg_override_reg;
208 bool has_mst_fifo;
209 const struct i2c_adapter_quirks *quirks;
210 bool supports_bus_clear;
211 bool has_apb_dma;
212 u8 tlow_std_mode;
213 u8 thigh_std_mode;
214 u8 tlow_fast_fastplus_mode;
215 u8 thigh_fast_fastplus_mode;
216 u32 setup_hold_time_std_mode;
217 u32 setup_hold_time_fast_fast_plus_mode;
218 u32 setup_hold_time_hs_mode;
219 bool has_interface_timing_reg;
220};
221
222/**
223 * struct tegra_i2c_dev - per device I2C context
224 * @dev: device reference for power management
225 * @hw: Tegra I2C HW feature
226 * @adapter: core I2C layer adapter information
227 * @div_clk: clock reference for div clock of I2C controller
228 * @fast_clk: clock reference for fast clock of I2C controller
229 * @rst: reset control for the I2C controller
230 * @base: ioremapped registers cookie
231 * @base_phys: physical base address of the I2C controller
232 * @cont_id: I2C controller ID, used for packet header
233 * @irq: IRQ number of transfer complete interrupt
234 * @is_dvc: identifies the DVC I2C controller, has a different register layout
235 * @msg_complete: transfer completion notifier
236 * @msg_err: error code for completed message
237 * @msg_buf: pointer to current message data
238 * @msg_buf_remaining: size of unsent data in the message buffer
239 * @msg_read: identifies read transfers
240 * @bus_clk_rate: current I2C bus clock rate
241 * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes
242 * @is_multimaster_mode: track if I2C controller is in multi-master mode
243 * @tx_dma_chan: DMA transmit channel
244 * @rx_dma_chan: DMA receive channel
245 * @dma_phys: handle to DMA resources
246 * @dma_buf: pointer to allocated DMA buffer
247 * @dma_buf_size: DMA buffer size
248 * @is_curr_dma_xfer: indicates active DMA transfer
249 * @dma_complete: DMA completion notifier
250 */
251struct tegra_i2c_dev {
252 struct device *dev;
253 const struct tegra_i2c_hw_feature *hw;
254 struct i2c_adapter adapter;
255 struct clk *div_clk;
256 struct clk *fast_clk;
257 struct reset_control *rst;
258 void __iomem *base;
259 phys_addr_t base_phys;
260 int cont_id;
261 int irq;
262 int is_dvc;
263 struct completion msg_complete;
264 int msg_err;
265 u8 *msg_buf;
266 size_t msg_buf_remaining;
267 int msg_read;
268 u32 bus_clk_rate;
269 u16 clk_divisor_non_hs_mode;
270 bool is_multimaster_mode;
271 struct dma_chan *tx_dma_chan;
272 struct dma_chan *rx_dma_chan;
273 dma_addr_t dma_phys;
274 u32 *dma_buf;
275 unsigned int dma_buf_size;
276 bool is_curr_dma_xfer;
277 struct completion dma_complete;
278};
279
280static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
281 unsigned long reg)
282{
283 writel(val, i2c_dev->base + reg);
284}
285
286static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
287{
288 return readl(i2c_dev->base + reg);
289}
290
291/*
292 * i2c_writel and i2c_readl will offset the register if necessary to talk
293 * to the I2C block inside the DVC block
294 */
295static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
296 unsigned long reg)
297{
298 if (i2c_dev->is_dvc)
299 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
300 return reg;
301}
302
303static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
304 unsigned long reg)
305{
306 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
307
308 /* Read back register to make sure that register writes completed */
309 if (reg != I2C_TX_FIFO)
310 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
311}
312
313static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
314{
315 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
316}
317
318static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
319 unsigned long reg, int len)
320{
321 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
322}
323
324static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
325 unsigned long reg, int len)
326{
327 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
328}
329
330static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
331{
332 u32 int_mask;
333
334 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
335 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
336}
337
338static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
339{
340 u32 int_mask;
341
342 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
343 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
344}
345
346static void tegra_i2c_dma_complete(void *args)
347{
348 struct tegra_i2c_dev *i2c_dev = args;
349
350 complete(&i2c_dev->dma_complete);
351}
352
353static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
354{
355 struct dma_async_tx_descriptor *dma_desc;
356 enum dma_transfer_direction dir;
357 struct dma_chan *chan;
358
359 dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
360 reinit_completion(&i2c_dev->dma_complete);
361 dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
362 chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
363 dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
364 len, dir, DMA_PREP_INTERRUPT |
365 DMA_CTRL_ACK);
366 if (!dma_desc) {
367 dev_err(i2c_dev->dev, "failed to get DMA descriptor\n");
368 return -EINVAL;
369 }
370
371 dma_desc->callback = tegra_i2c_dma_complete;
372 dma_desc->callback_param = i2c_dev;
373 dmaengine_submit(dma_desc);
374 dma_async_issue_pending(chan);
375 return 0;
376}
377
378static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
379{
380 if (i2c_dev->dma_buf) {
381 dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
382 i2c_dev->dma_buf, i2c_dev->dma_phys);
383 i2c_dev->dma_buf = NULL;
384 }
385
386 if (i2c_dev->tx_dma_chan) {
387 dma_release_channel(i2c_dev->tx_dma_chan);
388 i2c_dev->tx_dma_chan = NULL;
389 }
390
391 if (i2c_dev->rx_dma_chan) {
392 dma_release_channel(i2c_dev->rx_dma_chan);
393 i2c_dev->rx_dma_chan = NULL;
394 }
395}
396
397static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
398{
399 struct dma_chan *chan;
400 u32 *dma_buf;
401 dma_addr_t dma_phys;
402 int err;
403
404 if (!i2c_dev->hw->has_apb_dma)
405 return 0;
406
407 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
408 dev_dbg(i2c_dev->dev, "Support for APB DMA not enabled!\n");
409 return 0;
410 }
411
412 chan = dma_request_slave_channel_reason(i2c_dev->dev, "rx");
413 if (IS_ERR(chan)) {
414 err = PTR_ERR(chan);
415 goto err_out;
416 }
417
418 i2c_dev->rx_dma_chan = chan;
419
420 chan = dma_request_slave_channel_reason(i2c_dev->dev, "tx");
421 if (IS_ERR(chan)) {
422 err = PTR_ERR(chan);
423 goto err_out;
424 }
425
426 i2c_dev->tx_dma_chan = chan;
427
428 dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
429 &dma_phys, GFP_KERNEL | __GFP_NOWARN);
430 if (!dma_buf) {
431 dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n");
432 err = -ENOMEM;
433 goto err_out;
434 }
435
436 i2c_dev->dma_buf = dma_buf;
437 i2c_dev->dma_phys = dma_phys;
438 return 0;
439
440err_out:
441 tegra_i2c_release_dma(i2c_dev);
442 if (err != -EPROBE_DEFER) {
443 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
444 dev_err(i2c_dev->dev, "falling back to PIO\n");
445 return 0;
446 }
447
448 return err;
449}
450
451static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
452{
453 unsigned long timeout = jiffies + HZ;
454 unsigned int offset;
455 u32 mask, val;
456
457 if (i2c_dev->hw->has_mst_fifo) {
458 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
459 I2C_MST_FIFO_CONTROL_RX_FLUSH;
460 offset = I2C_MST_FIFO_CONTROL;
461 } else {
462 mask = I2C_FIFO_CONTROL_TX_FLUSH |
463 I2C_FIFO_CONTROL_RX_FLUSH;
464 offset = I2C_FIFO_CONTROL;
465 }
466
467 val = i2c_readl(i2c_dev, offset);
468 val |= mask;
469 i2c_writel(i2c_dev, val, offset);
470
471 while (i2c_readl(i2c_dev, offset) & mask) {
472 if (time_after(jiffies, timeout)) {
473 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
474 return -ETIMEDOUT;
475 }
476 usleep_range(1000, 2000);
477 }
478 return 0;
479}
480
481static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
482{
483 u32 val;
484 int rx_fifo_avail;
485 u8 *buf = i2c_dev->msg_buf;
486 size_t buf_remaining = i2c_dev->msg_buf_remaining;
487 int words_to_transfer;
488
489 /*
490 * Catch overflow due to message fully sent
491 * before the check for RX FIFO availability.
492 */
493 if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
494 return -EINVAL;
495
496 if (i2c_dev->hw->has_mst_fifo) {
497 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
498 rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
499 I2C_MST_FIFO_STATUS_RX_SHIFT;
500 } else {
501 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
502 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
503 I2C_FIFO_STATUS_RX_SHIFT;
504 }
505
506 /* Rounds down to not include partial word at the end of buf */
507 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
508 if (words_to_transfer > rx_fifo_avail)
509 words_to_transfer = rx_fifo_avail;
510
511 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
512
513 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
514 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
515 rx_fifo_avail -= words_to_transfer;
516
517 /*
518 * If there is a partial word at the end of buf, handle it manually to
519 * prevent overwriting past the end of buf
520 */
521 if (rx_fifo_avail > 0 && buf_remaining > 0) {
522 /*
523 * buf_remaining > 3 check not needed as rx_fifo_avail == 0
524 * when (words_to_transfer was > rx_fifo_avail) earlier
525 * in this function.
526 */
527 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
528 val = cpu_to_le32(val);
529 memcpy(buf, &val, buf_remaining);
530 buf_remaining = 0;
531 rx_fifo_avail--;
532 }
533
534 /* RX FIFO must be drained, otherwise it's an Overflow case. */
535 if (WARN_ON_ONCE(rx_fifo_avail))
536 return -EINVAL;
537
538 i2c_dev->msg_buf_remaining = buf_remaining;
539 i2c_dev->msg_buf = buf;
540
541 return 0;
542}
543
544static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
545{
546 u32 val;
547 int tx_fifo_avail;
548 u8 *buf = i2c_dev->msg_buf;
549 size_t buf_remaining = i2c_dev->msg_buf_remaining;
550 int words_to_transfer;
551
552 if (i2c_dev->hw->has_mst_fifo) {
553 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
554 tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
555 I2C_MST_FIFO_STATUS_TX_SHIFT;
556 } else {
557 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
558 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
559 I2C_FIFO_STATUS_TX_SHIFT;
560 }
561
562 /* Rounds down to not include partial word at the end of buf */
563 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
564
565 /* It's very common to have < 4 bytes, so optimize that case. */
566 if (words_to_transfer) {
567 if (words_to_transfer > tx_fifo_avail)
568 words_to_transfer = tx_fifo_avail;
569
570 /*
571 * Update state before writing to FIFO. If this casues us
572 * to finish writing all bytes (AKA buf_remaining goes to 0) we
573 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
574 * not maskable). We need to make sure that the isr sees
575 * buf_remaining as 0 and doesn't call us back re-entrantly.
576 */
577 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
578 tx_fifo_avail -= words_to_transfer;
579 i2c_dev->msg_buf_remaining = buf_remaining;
580 i2c_dev->msg_buf = buf +
581 words_to_transfer * BYTES_PER_FIFO_WORD;
582 barrier();
583
584 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
585
586 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
587 }
588
589 /*
590 * If there is a partial word at the end of buf, handle it manually to
591 * prevent reading past the end of buf, which could cross a page
592 * boundary and fault.
593 */
594 if (tx_fifo_avail > 0 && buf_remaining > 0) {
595 /*
596 * buf_remaining > 3 check not needed as tx_fifo_avail == 0
597 * when (words_to_transfer was > tx_fifo_avail) earlier
598 * in this function for non-zero words_to_transfer.
599 */
600 memcpy(&val, buf, buf_remaining);
601 val = le32_to_cpu(val);
602
603 /* Again update before writing to FIFO to make sure isr sees. */
604 i2c_dev->msg_buf_remaining = 0;
605 i2c_dev->msg_buf = NULL;
606 barrier();
607
608 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
609 }
610
611 return 0;
612}
613
614/*
615 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
616 * block. This block is identical to the rest of the I2C blocks, except that
617 * it only supports master mode, it has registers moved around, and it needs
618 * some extra init to get it into I2C mode. The register moves are handled
619 * by i2c_readl and i2c_writel
620 */
621static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
622{
623 u32 val;
624
625 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
626 val |= DVC_CTRL_REG3_SW_PROG;
627 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
628 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
629
630 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
631 val |= DVC_CTRL_REG1_INTR_EN;
632 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
633}
634
635static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
636{
637 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
638 int ret;
639
640 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
641 if (ret)
642 return ret;
643
644 if (!i2c_dev->hw->has_single_clk_source) {
645 ret = clk_enable(i2c_dev->fast_clk);
646 if (ret < 0) {
647 dev_err(i2c_dev->dev,
648 "Enabling fast clk failed, err %d\n", ret);
649 return ret;
650 }
651 }
652
653 ret = clk_enable(i2c_dev->div_clk);
654 if (ret < 0) {
655 dev_err(i2c_dev->dev,
656 "Enabling div clk failed, err %d\n", ret);
657 clk_disable(i2c_dev->fast_clk);
658 return ret;
659 }
660
661 return 0;
662}
663
664static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
665{
666 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
667
668 clk_disable(i2c_dev->div_clk);
669 if (!i2c_dev->hw->has_single_clk_source)
670 clk_disable(i2c_dev->fast_clk);
671
672 return pinctrl_pm_select_idle_state(i2c_dev->dev);
673}
674
675static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
676{
677 unsigned long reg_offset;
678 void __iomem *addr;
679 u32 val;
680 int err;
681
682 if (i2c_dev->hw->has_config_load_reg) {
683 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
684 addr = i2c_dev->base + reg_offset;
685 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
686 if (in_interrupt())
687 err = readl_poll_timeout_atomic(addr, val, val == 0,
688 1000,
689 I2C_CONFIG_LOAD_TIMEOUT);
690 else
691 err = readl_poll_timeout(addr, val, val == 0, 1000,
692 I2C_CONFIG_LOAD_TIMEOUT);
693
694 if (err) {
695 dev_warn(i2c_dev->dev,
696 "timeout waiting for config load\n");
697 return err;
698 }
699 }
700
701 return 0;
702}
703
704static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit)
705{
706 u32 val;
707 int err;
708 u32 clk_divisor, clk_multiplier;
709 u32 tsu_thd;
710 u8 tlow, thigh;
711
712 reset_control_assert(i2c_dev->rst);
713 udelay(2);
714 reset_control_deassert(i2c_dev->rst);
715
716 if (i2c_dev->is_dvc)
717 tegra_dvc_init(i2c_dev);
718
719 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
720 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
721
722 if (i2c_dev->hw->has_multi_master_mode)
723 val |= I2C_CNFG_MULTI_MASTER_MODE;
724
725 i2c_writel(i2c_dev, val, I2C_CNFG);
726 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
727
728 /* Make sure clock divisor programmed correctly */
729 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
730 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
731 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
732 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
733
734 if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
735 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE) {
736 tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
737 thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
738 tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
739 } else {
740 tlow = i2c_dev->hw->tlow_std_mode;
741 thigh = i2c_dev->hw->thigh_std_mode;
742 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
743 }
744
745 if (i2c_dev->hw->has_interface_timing_reg) {
746 val = (thigh << I2C_THIGH_SHIFT) | tlow;
747 i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
748 }
749
750 /*
751 * configure setup and hold times only when tsu_thd is non-zero.
752 * otherwise, preserve the chip default values
753 */
754 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
755 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
756
757 if (!clk_reinit) {
758 clk_multiplier = (tlow + thigh + 2);
759 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
760 err = clk_set_rate(i2c_dev->div_clk,
761 i2c_dev->bus_clk_rate * clk_multiplier);
762 if (err) {
763 dev_err(i2c_dev->dev,
764 "failed changing clock rate: %d\n", err);
765 return err;
766 }
767 }
768
769 if (!i2c_dev->is_dvc) {
770 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
771
772 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
773 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
774 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
775 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
776 }
777
778 err = tegra_i2c_flush_fifos(i2c_dev);
779 if (err)
780 return err;
781
782 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
783 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
784
785 err = tegra_i2c_wait_for_config_load(i2c_dev);
786 if (err)
787 return err;
788
789 return 0;
790}
791
792static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
793{
794 u32 cnfg;
795
796 /*
797 * NACK interrupt is generated before the I2C controller generates
798 * the STOP condition on the bus. So wait for 2 clock periods
799 * before disabling the controller so that the STOP condition has
800 * been delivered properly.
801 */
802 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
803
804 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
805 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
806 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
807
808 return tegra_i2c_wait_for_config_load(i2c_dev);
809}
810
811static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
812{
813 u32 status;
814 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
815 struct tegra_i2c_dev *i2c_dev = dev_id;
816
817 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
818
819 if (status == 0) {
820 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
821 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
822 i2c_readl(i2c_dev, I2C_STATUS),
823 i2c_readl(i2c_dev, I2C_CNFG));
824 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
825 goto err;
826 }
827
828 if (unlikely(status & status_err)) {
829 tegra_i2c_disable_packet_mode(i2c_dev);
830 if (status & I2C_INT_NO_ACK)
831 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
832 if (status & I2C_INT_ARBITRATION_LOST)
833 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
834 goto err;
835 }
836
837 /*
838 * I2C transfer is terminated during the bus clear so skip
839 * processing the other interrupts.
840 */
841 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
842 goto err;
843
844 if (!i2c_dev->is_curr_dma_xfer) {
845 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
846 if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
847 /*
848 * Overflow error condition: message fully sent,
849 * with no XFER_COMPLETE interrupt but hardware
850 * asks to transfer more.
851 */
852 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
853 goto err;
854 }
855 }
856
857 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
858 if (i2c_dev->msg_buf_remaining)
859 tegra_i2c_fill_tx_fifo(i2c_dev);
860 else
861 tegra_i2c_mask_irq(i2c_dev,
862 I2C_INT_TX_FIFO_DATA_REQ);
863 }
864 }
865
866 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
867 if (i2c_dev->is_dvc)
868 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
869
870 /*
871 * During message read XFER_COMPLETE interrupt is triggered prior to
872 * DMA completion and during message write XFER_COMPLETE interrupt is
873 * triggered after DMA completion.
874 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
875 * so forcing msg_buf_remaining to 0 in DMA mode.
876 */
877 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
878 if (i2c_dev->is_curr_dma_xfer)
879 i2c_dev->msg_buf_remaining = 0;
880 /*
881 * Underflow error condition: XFER_COMPLETE before message
882 * fully sent.
883 */
884 if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
885 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
886 goto err;
887 }
888 complete(&i2c_dev->msg_complete);
889 }
890 goto done;
891err:
892 /* An error occurred, mask all interrupts */
893 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
894 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
895 I2C_INT_RX_FIFO_DATA_REQ);
896 if (i2c_dev->hw->supports_bus_clear)
897 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
898 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
899 if (i2c_dev->is_dvc)
900 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
901
902 if (i2c_dev->is_curr_dma_xfer) {
903 if (i2c_dev->msg_read)
904 dmaengine_terminate_async(i2c_dev->rx_dma_chan);
905 else
906 dmaengine_terminate_async(i2c_dev->tx_dma_chan);
907
908 complete(&i2c_dev->dma_complete);
909 }
910
911 complete(&i2c_dev->msg_complete);
912done:
913 return IRQ_HANDLED;
914}
915
916static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
917 size_t len)
918{
919 u32 val, reg;
920 u8 dma_burst;
921 struct dma_slave_config slv_config = {0};
922 struct dma_chan *chan;
923 int ret;
924 unsigned long reg_offset;
925
926 if (i2c_dev->hw->has_mst_fifo)
927 reg = I2C_MST_FIFO_CONTROL;
928 else
929 reg = I2C_FIFO_CONTROL;
930
931 if (i2c_dev->is_curr_dma_xfer) {
932 if (len & 0xF)
933 dma_burst = 1;
934 else if (len & 0x10)
935 dma_burst = 4;
936 else
937 dma_burst = 8;
938
939 if (i2c_dev->msg_read) {
940 chan = i2c_dev->rx_dma_chan;
941 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
942 slv_config.src_addr = i2c_dev->base_phys + reg_offset;
943 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
944 slv_config.src_maxburst = dma_burst;
945
946 if (i2c_dev->hw->has_mst_fifo)
947 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
948 else
949 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
950 } else {
951 chan = i2c_dev->tx_dma_chan;
952 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
953 slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
954 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
955 slv_config.dst_maxburst = dma_burst;
956
957 if (i2c_dev->hw->has_mst_fifo)
958 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
959 else
960 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
961 }
962
963 slv_config.device_fc = true;
964 ret = dmaengine_slave_config(chan, &slv_config);
965 if (ret < 0) {
966 dev_err(i2c_dev->dev, "DMA slave config failed: %d\n",
967 ret);
968 dev_err(i2c_dev->dev, "falling back to PIO\n");
969 tegra_i2c_release_dma(i2c_dev);
970 i2c_dev->is_curr_dma_xfer = false;
971 } else {
972 goto out;
973 }
974 }
975
976 if (i2c_dev->hw->has_mst_fifo)
977 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
978 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
979 else
980 val = I2C_FIFO_CONTROL_TX_TRIG(8) |
981 I2C_FIFO_CONTROL_RX_TRIG(1);
982out:
983 i2c_writel(i2c_dev, val, reg);
984}
985
986static unsigned long
987tegra_i2c_wait_completion_timeout(struct tegra_i2c_dev *i2c_dev,
988 struct completion *complete,
989 unsigned int timeout_ms)
990{
991 unsigned long ret;
992
993 enable_irq(i2c_dev->irq);
994 ret = wait_for_completion_timeout(complete,
995 msecs_to_jiffies(timeout_ms));
996 disable_irq(i2c_dev->irq);
997
998 /*
999 * There is a chance that completion may happen after IRQ
1000 * synchronization, which is done by disable_irq().
1001 */
1002 if (ret == 0 && completion_done(complete)) {
1003 dev_warn(i2c_dev->dev, "completion done after timeout\n");
1004 ret = 1;
1005 }
1006
1007 return ret;
1008}
1009
1010static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
1011{
1012 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1013 int err;
1014 unsigned long time_left;
1015 u32 reg;
1016
1017 reinit_completion(&i2c_dev->msg_complete);
1018 reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) |
1019 I2C_BC_STOP_COND | I2C_BC_TERMINATE;
1020 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1021 if (i2c_dev->hw->has_config_load_reg) {
1022 err = tegra_i2c_wait_for_config_load(i2c_dev);
1023 if (err)
1024 return err;
1025 }
1026
1027 reg |= I2C_BC_ENABLE;
1028 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1029 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1030
1031 time_left = tegra_i2c_wait_completion_timeout(
1032 i2c_dev, &i2c_dev->msg_complete, 50);
1033 if (time_left == 0) {
1034 dev_err(i2c_dev->dev, "timed out for bus clear\n");
1035 return -ETIMEDOUT;
1036 }
1037
1038 reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1039 if (!(reg & I2C_BC_STATUS)) {
1040 dev_err(i2c_dev->dev,
1041 "un-recovered arbitration lost\n");
1042 return -EIO;
1043 }
1044
1045 return -EAGAIN;
1046}
1047
1048static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
1049 struct i2c_msg *msg,
1050 enum msg_end_type end_state)
1051{
1052 u32 packet_header;
1053 u32 int_mask;
1054 unsigned long time_left;
1055 size_t xfer_size;
1056 u32 *buffer = NULL;
1057 int err = 0;
1058 bool dma;
1059 u16 xfer_time = 100;
1060
1061 tegra_i2c_flush_fifos(i2c_dev);
1062
1063 i2c_dev->msg_buf = msg->buf;
1064 i2c_dev->msg_buf_remaining = msg->len;
1065 i2c_dev->msg_err = I2C_ERR_NONE;
1066 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
1067 reinit_completion(&i2c_dev->msg_complete);
1068
1069 if (i2c_dev->msg_read)
1070 xfer_size = msg->len;
1071 else
1072 xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1073
1074 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1075 i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_MAX_LEN) &&
1076 i2c_dev->dma_buf;
1077 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1078 dma = i2c_dev->is_curr_dma_xfer;
1079 /*
1080 * Transfer time in mSec = Total bits / transfer rate
1081 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1082 */
1083 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1084 i2c_dev->bus_clk_rate);
1085
1086 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1087 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1088 if (dma) {
1089 if (i2c_dev->msg_read) {
1090 dma_sync_single_for_device(i2c_dev->dev,
1091 i2c_dev->dma_phys,
1092 xfer_size,
1093 DMA_FROM_DEVICE);
1094 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1095 if (err < 0) {
1096 dev_err(i2c_dev->dev,
1097 "starting RX DMA failed, err %d\n",
1098 err);
1099 return err;
1100 }
1101
1102 } else {
1103 dma_sync_single_for_cpu(i2c_dev->dev,
1104 i2c_dev->dma_phys,
1105 xfer_size,
1106 DMA_TO_DEVICE);
1107 buffer = i2c_dev->dma_buf;
1108 }
1109 }
1110
1111 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
1112 PACKET_HEADER0_PROTOCOL_I2C |
1113 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
1114 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
1115 if (dma && !i2c_dev->msg_read)
1116 *buffer++ = packet_header;
1117 else
1118 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1119
1120 packet_header = msg->len - 1;
1121 if (dma && !i2c_dev->msg_read)
1122 *buffer++ = packet_header;
1123 else
1124 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1125
1126 packet_header = I2C_HEADER_IE_ENABLE;
1127 if (end_state == MSG_END_CONTINUE)
1128 packet_header |= I2C_HEADER_CONTINUE_XFER;
1129 else if (end_state == MSG_END_REPEAT_START)
1130 packet_header |= I2C_HEADER_REPEAT_START;
1131 if (msg->flags & I2C_M_TEN) {
1132 packet_header |= msg->addr;
1133 packet_header |= I2C_HEADER_10BIT_ADDR;
1134 } else {
1135 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1136 }
1137 if (msg->flags & I2C_M_IGNORE_NAK)
1138 packet_header |= I2C_HEADER_CONT_ON_NAK;
1139 if (msg->flags & I2C_M_RD)
1140 packet_header |= I2C_HEADER_READ;
1141 if (dma && !i2c_dev->msg_read)
1142 *buffer++ = packet_header;
1143 else
1144 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1145
1146 if (!i2c_dev->msg_read) {
1147 if (dma) {
1148 memcpy(buffer, msg->buf, msg->len);
1149 dma_sync_single_for_device(i2c_dev->dev,
1150 i2c_dev->dma_phys,
1151 xfer_size,
1152 DMA_TO_DEVICE);
1153 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1154 if (err < 0) {
1155 dev_err(i2c_dev->dev,
1156 "starting TX DMA failed, err %d\n",
1157 err);
1158 return err;
1159 }
1160 } else {
1161 tegra_i2c_fill_tx_fifo(i2c_dev);
1162 }
1163 }
1164
1165 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1166 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
1167 if (!dma) {
1168 if (msg->flags & I2C_M_RD)
1169 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1170 else if (i2c_dev->msg_buf_remaining)
1171 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1172 }
1173
1174 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1175 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
1176 i2c_readl(i2c_dev, I2C_INT_MASK));
1177
1178 if (dma) {
1179 time_left = tegra_i2c_wait_completion_timeout(
1180 i2c_dev, &i2c_dev->dma_complete, xfer_time);
1181
1182 if (time_left == 0) {
1183 dev_err(i2c_dev->dev, "DMA transfer timeout\n");
1184 dmaengine_terminate_sync(i2c_dev->msg_read ?
1185 i2c_dev->rx_dma_chan :
1186 i2c_dev->tx_dma_chan);
1187 tegra_i2c_init(i2c_dev, true);
1188 return -ETIMEDOUT;
1189 }
1190
1191 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1192 dma_sync_single_for_cpu(i2c_dev->dev,
1193 i2c_dev->dma_phys,
1194 xfer_size,
1195 DMA_FROM_DEVICE);
1196 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf,
1197 msg->len);
1198 }
1199
1200 if (i2c_dev->msg_err != I2C_ERR_NONE)
1201 dmaengine_synchronize(i2c_dev->msg_read ?
1202 i2c_dev->rx_dma_chan :
1203 i2c_dev->tx_dma_chan);
1204 }
1205
1206 time_left = tegra_i2c_wait_completion_timeout(
1207 i2c_dev, &i2c_dev->msg_complete, xfer_time);
1208
1209 tegra_i2c_mask_irq(i2c_dev, int_mask);
1210
1211 if (time_left == 0) {
1212 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
1213 tegra_i2c_init(i2c_dev, true);
1214 return -ETIMEDOUT;
1215 }
1216
1217 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1218 time_left, completion_done(&i2c_dev->msg_complete),
1219 i2c_dev->msg_err);
1220
1221 i2c_dev->is_curr_dma_xfer = false;
1222 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
1223 return 0;
1224
1225 tegra_i2c_init(i2c_dev, true);
1226 /* start recovery upon arbitration loss in single master mode */
1227 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1228 if (!i2c_dev->is_multimaster_mode)
1229 return i2c_recover_bus(&i2c_dev->adapter);
1230 return -EAGAIN;
1231 }
1232
1233 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1234 if (msg->flags & I2C_M_IGNORE_NAK)
1235 return 0;
1236 return -EREMOTEIO;
1237 }
1238
1239 return -EIO;
1240}
1241
1242static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1243 int num)
1244{
1245 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1246 int i;
1247 int ret;
1248
1249 ret = pm_runtime_get_sync(i2c_dev->dev);
1250 if (ret < 0) {
1251 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
1252 return ret;
1253 }
1254
1255 for (i = 0; i < num; i++) {
1256 enum msg_end_type end_type = MSG_END_STOP;
1257
1258 if (i < (num - 1)) {
1259 if (msgs[i + 1].flags & I2C_M_NOSTART)
1260 end_type = MSG_END_CONTINUE;
1261 else
1262 end_type = MSG_END_REPEAT_START;
1263 }
1264 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
1265 if (ret)
1266 break;
1267 }
1268
1269 pm_runtime_put(i2c_dev->dev);
1270
1271 return ret ?: i;
1272}
1273
1274static u32 tegra_i2c_func(struct i2c_adapter *adap)
1275{
1276 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1277 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1278 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
1279
1280 if (i2c_dev->hw->has_continue_xfer_support)
1281 ret |= I2C_FUNC_NOSTART;
1282 return ret;
1283}
1284
1285static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1286{
1287 struct device_node *np = i2c_dev->dev->of_node;
1288 int ret;
1289 bool multi_mode;
1290
1291 ret = of_property_read_u32(np, "clock-frequency",
1292 &i2c_dev->bus_clk_rate);
1293 if (ret)
1294 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
1295
1296 multi_mode = of_property_read_bool(np, "multi-master");
1297 i2c_dev->is_multimaster_mode = multi_mode;
1298}
1299
1300static const struct i2c_algorithm tegra_i2c_algo = {
1301 .master_xfer = tegra_i2c_xfer,
1302 .functionality = tegra_i2c_func,
1303};
1304
1305/* payload size is only 12 bit */
1306static const struct i2c_adapter_quirks tegra_i2c_quirks = {
1307 .flags = I2C_AQ_NO_ZERO_LEN,
1308 .max_read_len = SZ_4K,
1309 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
1310};
1311
1312static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1313 .flags = I2C_AQ_NO_ZERO_LEN,
1314 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
1315};
1316
1317static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1318 .recover_bus = tegra_i2c_issue_bus_clear,
1319};
1320
1321static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1322 .has_continue_xfer_support = false,
1323 .has_per_pkt_xfer_complete_irq = false,
1324 .has_single_clk_source = false,
1325 .clk_divisor_hs_mode = 3,
1326 .clk_divisor_std_mode = 0,
1327 .clk_divisor_fast_mode = 0,
1328 .clk_divisor_fast_plus_mode = 0,
1329 .has_config_load_reg = false,
1330 .has_multi_master_mode = false,
1331 .has_slcg_override_reg = false,
1332 .has_mst_fifo = false,
1333 .quirks = &tegra_i2c_quirks,
1334 .supports_bus_clear = false,
1335 .has_apb_dma = true,
1336 .tlow_std_mode = 0x4,
1337 .thigh_std_mode = 0x2,
1338 .tlow_fast_fastplus_mode = 0x4,
1339 .thigh_fast_fastplus_mode = 0x2,
1340 .setup_hold_time_std_mode = 0x0,
1341 .setup_hold_time_fast_fast_plus_mode = 0x0,
1342 .setup_hold_time_hs_mode = 0x0,
1343 .has_interface_timing_reg = false,
1344};
1345
1346static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1347 .has_continue_xfer_support = true,
1348 .has_per_pkt_xfer_complete_irq = false,
1349 .has_single_clk_source = false,
1350 .clk_divisor_hs_mode = 3,
1351 .clk_divisor_std_mode = 0,
1352 .clk_divisor_fast_mode = 0,
1353 .clk_divisor_fast_plus_mode = 0,
1354 .has_config_load_reg = false,
1355 .has_multi_master_mode = false,
1356 .has_slcg_override_reg = false,
1357 .has_mst_fifo = false,
1358 .quirks = &tegra_i2c_quirks,
1359 .supports_bus_clear = false,
1360 .has_apb_dma = true,
1361 .tlow_std_mode = 0x4,
1362 .thigh_std_mode = 0x2,
1363 .tlow_fast_fastplus_mode = 0x4,
1364 .thigh_fast_fastplus_mode = 0x2,
1365 .setup_hold_time_std_mode = 0x0,
1366 .setup_hold_time_fast_fast_plus_mode = 0x0,
1367 .setup_hold_time_hs_mode = 0x0,
1368 .has_interface_timing_reg = false,
1369};
1370
1371static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1372 .has_continue_xfer_support = true,
1373 .has_per_pkt_xfer_complete_irq = true,
1374 .has_single_clk_source = true,
1375 .clk_divisor_hs_mode = 1,
1376 .clk_divisor_std_mode = 0x19,
1377 .clk_divisor_fast_mode = 0x19,
1378 .clk_divisor_fast_plus_mode = 0x10,
1379 .has_config_load_reg = false,
1380 .has_multi_master_mode = false,
1381 .has_slcg_override_reg = false,
1382 .has_mst_fifo = false,
1383 .quirks = &tegra_i2c_quirks,
1384 .supports_bus_clear = true,
1385 .has_apb_dma = true,
1386 .tlow_std_mode = 0x4,
1387 .thigh_std_mode = 0x2,
1388 .tlow_fast_fastplus_mode = 0x4,
1389 .thigh_fast_fastplus_mode = 0x2,
1390 .setup_hold_time_std_mode = 0x0,
1391 .setup_hold_time_fast_fast_plus_mode = 0x0,
1392 .setup_hold_time_hs_mode = 0x0,
1393 .has_interface_timing_reg = false,
1394};
1395
1396static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1397 .has_continue_xfer_support = true,
1398 .has_per_pkt_xfer_complete_irq = true,
1399 .has_single_clk_source = true,
1400 .clk_divisor_hs_mode = 1,
1401 .clk_divisor_std_mode = 0x19,
1402 .clk_divisor_fast_mode = 0x19,
1403 .clk_divisor_fast_plus_mode = 0x10,
1404 .has_config_load_reg = true,
1405 .has_multi_master_mode = false,
1406 .has_slcg_override_reg = true,
1407 .has_mst_fifo = false,
1408 .quirks = &tegra_i2c_quirks,
1409 .supports_bus_clear = true,
1410 .has_apb_dma = true,
1411 .tlow_std_mode = 0x4,
1412 .thigh_std_mode = 0x2,
1413 .tlow_fast_fastplus_mode = 0x4,
1414 .thigh_fast_fastplus_mode = 0x2,
1415 .setup_hold_time_std_mode = 0x0,
1416 .setup_hold_time_fast_fast_plus_mode = 0x0,
1417 .setup_hold_time_hs_mode = 0x0,
1418 .has_interface_timing_reg = true,
1419};
1420
1421static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1422 .has_continue_xfer_support = true,
1423 .has_per_pkt_xfer_complete_irq = true,
1424 .has_single_clk_source = true,
1425 .clk_divisor_hs_mode = 1,
1426 .clk_divisor_std_mode = 0x19,
1427 .clk_divisor_fast_mode = 0x19,
1428 .clk_divisor_fast_plus_mode = 0x10,
1429 .has_config_load_reg = true,
1430 .has_multi_master_mode = false,
1431 .has_slcg_override_reg = true,
1432 .has_mst_fifo = false,
1433 .quirks = &tegra_i2c_quirks,
1434 .supports_bus_clear = true,
1435 .has_apb_dma = true,
1436 .tlow_std_mode = 0x4,
1437 .thigh_std_mode = 0x2,
1438 .tlow_fast_fastplus_mode = 0x4,
1439 .thigh_fast_fastplus_mode = 0x2,
1440 .setup_hold_time_std_mode = 0,
1441 .setup_hold_time_fast_fast_plus_mode = 0,
1442 .setup_hold_time_hs_mode = 0,
1443 .has_interface_timing_reg = true,
1444};
1445
1446static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1447 .has_continue_xfer_support = true,
1448 .has_per_pkt_xfer_complete_irq = true,
1449 .has_single_clk_source = true,
1450 .clk_divisor_hs_mode = 1,
1451 .clk_divisor_std_mode = 0x16,
1452 .clk_divisor_fast_mode = 0x19,
1453 .clk_divisor_fast_plus_mode = 0x10,
1454 .has_config_load_reg = true,
1455 .has_multi_master_mode = false,
1456 .has_slcg_override_reg = true,
1457 .has_mst_fifo = false,
1458 .quirks = &tegra_i2c_quirks,
1459 .supports_bus_clear = true,
1460 .has_apb_dma = false,
1461 .tlow_std_mode = 0x4,
1462 .thigh_std_mode = 0x3,
1463 .tlow_fast_fastplus_mode = 0x4,
1464 .thigh_fast_fastplus_mode = 0x2,
1465 .setup_hold_time_std_mode = 0,
1466 .setup_hold_time_fast_fast_plus_mode = 0,
1467 .setup_hold_time_hs_mode = 0,
1468 .has_interface_timing_reg = true,
1469};
1470
1471static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1472 .has_continue_xfer_support = true,
1473 .has_per_pkt_xfer_complete_irq = true,
1474 .has_single_clk_source = true,
1475 .clk_divisor_hs_mode = 1,
1476 .clk_divisor_std_mode = 0x4f,
1477 .clk_divisor_fast_mode = 0x3c,
1478 .clk_divisor_fast_plus_mode = 0x16,
1479 .has_config_load_reg = true,
1480 .has_multi_master_mode = true,
1481 .has_slcg_override_reg = true,
1482 .has_mst_fifo = true,
1483 .quirks = &tegra194_i2c_quirks,
1484 .supports_bus_clear = true,
1485 .has_apb_dma = false,
1486 .tlow_std_mode = 0x8,
1487 .thigh_std_mode = 0x7,
1488 .tlow_fast_fastplus_mode = 0x2,
1489 .thigh_fast_fastplus_mode = 0x2,
1490 .setup_hold_time_std_mode = 0x08080808,
1491 .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1492 .setup_hold_time_hs_mode = 0x090909,
1493 .has_interface_timing_reg = true,
1494};
1495
1496/* Match table for of_platform binding */
1497static const struct of_device_id tegra_i2c_of_match[] = {
1498 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
1499 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
1500 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
1501 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
1502 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
1503 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1504 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1505 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1506 {},
1507};
1508MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
1509
1510static int tegra_i2c_probe(struct platform_device *pdev)
1511{
1512 struct tegra_i2c_dev *i2c_dev;
1513 struct resource *res;
1514 struct clk *div_clk;
1515 struct clk *fast_clk;
1516 void __iomem *base;
1517 phys_addr_t base_phys;
1518 int irq;
1519 int ret;
1520
1521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1522 base_phys = res->start;
1523 base = devm_ioremap_resource(&pdev->dev, res);
1524 if (IS_ERR(base))
1525 return PTR_ERR(base);
1526
1527 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1528 if (!res) {
1529 dev_err(&pdev->dev, "no irq resource\n");
1530 return -EINVAL;
1531 }
1532 irq = res->start;
1533
1534 div_clk = devm_clk_get(&pdev->dev, "div-clk");
1535 if (IS_ERR(div_clk)) {
1536 if (PTR_ERR(div_clk) != -EPROBE_DEFER)
1537 dev_err(&pdev->dev, "missing controller clock\n");
1538
1539 return PTR_ERR(div_clk);
1540 }
1541
1542 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1543 if (!i2c_dev)
1544 return -ENOMEM;
1545
1546 i2c_dev->base = base;
1547 i2c_dev->base_phys = base_phys;
1548 i2c_dev->div_clk = div_clk;
1549 i2c_dev->adapter.algo = &tegra_i2c_algo;
1550 i2c_dev->adapter.retries = 1;
1551 i2c_dev->adapter.timeout = 6 * HZ;
1552 i2c_dev->irq = irq;
1553 i2c_dev->cont_id = pdev->id;
1554 i2c_dev->dev = &pdev->dev;
1555
1556 i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
1557 if (IS_ERR(i2c_dev->rst)) {
1558 dev_err(&pdev->dev, "missing controller reset\n");
1559 return PTR_ERR(i2c_dev->rst);
1560 }
1561
1562 tegra_i2c_parse_dt(i2c_dev);
1563
1564 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
1565 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
1566 "nvidia,tegra20-i2c-dvc");
1567 i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
1568 i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len +
1569 I2C_PACKET_HEADER_SIZE;
1570 init_completion(&i2c_dev->msg_complete);
1571 init_completion(&i2c_dev->dma_complete);
1572
1573 if (!i2c_dev->hw->has_single_clk_source) {
1574 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
1575 if (IS_ERR(fast_clk)) {
1576 dev_err(&pdev->dev, "missing fast clock\n");
1577 return PTR_ERR(fast_clk);
1578 }
1579 i2c_dev->fast_clk = fast_clk;
1580 }
1581
1582 platform_set_drvdata(pdev, i2c_dev);
1583
1584 if (!i2c_dev->hw->has_single_clk_source) {
1585 ret = clk_prepare(i2c_dev->fast_clk);
1586 if (ret < 0) {
1587 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1588 return ret;
1589 }
1590 }
1591
1592 if (i2c_dev->bus_clk_rate > I2C_FAST_MODE &&
1593 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE)
1594 i2c_dev->clk_divisor_non_hs_mode =
1595 i2c_dev->hw->clk_divisor_fast_plus_mode;
1596 else if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
1597 i2c_dev->bus_clk_rate <= I2C_FAST_MODE)
1598 i2c_dev->clk_divisor_non_hs_mode =
1599 i2c_dev->hw->clk_divisor_fast_mode;
1600 else
1601 i2c_dev->clk_divisor_non_hs_mode =
1602 i2c_dev->hw->clk_divisor_std_mode;
1603
1604 ret = clk_prepare(i2c_dev->div_clk);
1605 if (ret < 0) {
1606 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1607 goto unprepare_fast_clk;
1608 }
1609
1610 pm_runtime_enable(&pdev->dev);
1611 if (!pm_runtime_enabled(&pdev->dev)) {
1612 ret = tegra_i2c_runtime_resume(&pdev->dev);
1613 if (ret < 0) {
1614 dev_err(&pdev->dev, "runtime resume failed\n");
1615 goto unprepare_div_clk;
1616 }
1617 } else {
1618 ret = pm_runtime_get_sync(i2c_dev->dev);
1619 if (ret < 0) {
1620 dev_err(&pdev->dev, "runtime resume failed\n");
1621 goto disable_rpm;
1622 }
1623 }
1624
1625 if (i2c_dev->is_multimaster_mode) {
1626 ret = clk_enable(i2c_dev->div_clk);
1627 if (ret < 0) {
1628 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
1629 ret);
1630 goto put_rpm;
1631 }
1632 }
1633
1634 if (i2c_dev->hw->supports_bus_clear)
1635 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1636
1637 ret = tegra_i2c_init_dma(i2c_dev);
1638 if (ret < 0)
1639 goto disable_div_clk;
1640
1641 ret = tegra_i2c_init(i2c_dev, false);
1642 if (ret) {
1643 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
1644 goto release_dma;
1645 }
1646
1647 irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
1648
1649 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
1650 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
1651 if (ret) {
1652 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
1653 goto release_dma;
1654 }
1655
1656 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1657 i2c_dev->adapter.owner = THIS_MODULE;
1658 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
1659 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
1660 sizeof(i2c_dev->adapter.name));
1661 i2c_dev->adapter.dev.parent = &pdev->dev;
1662 i2c_dev->adapter.nr = pdev->id;
1663 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
1664
1665 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
1666 if (ret)
1667 goto release_dma;
1668
1669 pm_runtime_put(&pdev->dev);
1670
1671 return 0;
1672
1673release_dma:
1674 tegra_i2c_release_dma(i2c_dev);
1675
1676disable_div_clk:
1677 if (i2c_dev->is_multimaster_mode)
1678 clk_disable(i2c_dev->div_clk);
1679
1680put_rpm:
1681 if (pm_runtime_enabled(&pdev->dev))
1682 pm_runtime_put_sync(&pdev->dev);
1683 else
1684 tegra_i2c_runtime_suspend(&pdev->dev);
1685
1686disable_rpm:
1687 if (pm_runtime_enabled(&pdev->dev))
1688 pm_runtime_disable(&pdev->dev);
1689
1690unprepare_div_clk:
1691 clk_unprepare(i2c_dev->div_clk);
1692
1693unprepare_fast_clk:
1694 if (!i2c_dev->hw->has_single_clk_source)
1695 clk_unprepare(i2c_dev->fast_clk);
1696
1697 return ret;
1698}
1699
1700static int tegra_i2c_remove(struct platform_device *pdev)
1701{
1702 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1703
1704 i2c_del_adapter(&i2c_dev->adapter);
1705
1706 if (i2c_dev->is_multimaster_mode)
1707 clk_disable(i2c_dev->div_clk);
1708
1709 pm_runtime_disable(&pdev->dev);
1710 if (!pm_runtime_status_suspended(&pdev->dev))
1711 tegra_i2c_runtime_suspend(&pdev->dev);
1712
1713 clk_unprepare(i2c_dev->div_clk);
1714 if (!i2c_dev->hw->has_single_clk_source)
1715 clk_unprepare(i2c_dev->fast_clk);
1716
1717 tegra_i2c_release_dma(i2c_dev);
1718 return 0;
1719}
1720
1721static int __maybe_unused tegra_i2c_suspend(struct device *dev)
1722{
1723 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1724 int err = 0;
1725
1726 i2c_mark_adapter_suspended(&i2c_dev->adapter);
1727
1728 if (!pm_runtime_status_suspended(dev))
1729 err = tegra_i2c_runtime_suspend(dev);
1730
1731 return err;
1732}
1733
1734static int __maybe_unused tegra_i2c_resume(struct device *dev)
1735{
1736 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1737 int err;
1738
1739 /*
1740 * We need to ensure that clocks are enabled so that registers can be
1741 * restored in tegra_i2c_init().
1742 */
1743 err = tegra_i2c_runtime_resume(dev);
1744 if (err)
1745 return err;
1746
1747 err = tegra_i2c_init(i2c_dev, false);
1748 if (err)
1749 return err;
1750
1751 /*
1752 * In case we are runtime suspended, disable clocks again so that we
1753 * don't unbalance the clock reference counts during the next runtime
1754 * resume transition.
1755 */
1756 if (pm_runtime_status_suspended(dev)) {
1757 err = tegra_i2c_runtime_suspend(dev);
1758 if (err)
1759 return err;
1760 }
1761
1762 i2c_mark_adapter_resumed(&i2c_dev->adapter);
1763
1764 return 0;
1765}
1766
1767static const struct dev_pm_ops tegra_i2c_pm = {
1768 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1769 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1770 NULL)
1771};
1772
1773static struct platform_driver tegra_i2c_driver = {
1774 .probe = tegra_i2c_probe,
1775 .remove = tegra_i2c_remove,
1776 .driver = {
1777 .name = "tegra-i2c",
1778 .of_match_table = tegra_i2c_of_match,
1779 .pm = &tegra_i2c_pm,
1780 },
1781};
1782
1783module_platform_driver(tegra_i2c_driver);
1784
1785MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1786MODULE_AUTHOR("Colin Cross");
1787MODULE_LICENSE("GPL v2");