2 * drivers/i2c/busses/i2c-tegra.c
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
7 * Copyright (C) 2010-2016 NVIDIA Corporation. All rights reserved.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/clk.h>
24 #include <linux/err.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-algo-bit.h>
27 #include <linux/i2c-gpio.h>
29 #include <linux/interrupt.h>
30 #include <linux/gpio.h>
31 #include <linux/delay.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/slab.h>
34 #include <linux/i2c-tegra.h>
35 #include <linux/of_device.h>
36 #include <linux/of_gpio.h>
37 #include <linux/of_i2c.h>
38 #include <linux/module.h>
39 #include <linux/clk/tegra.h>
40 #include <linux/spinlock.h>
41 #include <linux/clk/tegra.h>
42 #include <linux/tegra-pm.h>
43 #include <linux/pinctrl/consumer.h>
44 #include <linux/pinctrl/pinconf-tegra.h>
45 #include <linux/tegra_prod.h>
47 #include <asm/unaligned.h>
49 #define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
50 #define TEGRA_I2C_RETRIES 3
51 #define BYTES_PER_FIFO_WORD 4
53 #define I2C_CNFG 0x000
54 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
55 #define I2C_CNFG_PACKET_MODE_EN (1<<10)
56 #define I2C_CNFG_NEW_MASTER_FSM (1<<11)
57 #define I2C_CNFG_MULTI_MASTER_MODE (1<<17)
58 #define I2C_STATUS 0x01C
59 #define I2C_STATUS_BUSY (1<<8)
60 #define I2C_SL_CNFG 0x020
61 #define I2C_SL_CNFG_NACK (1<<1)
62 #define I2C_SL_CNFG_NEWSL (1<<2)
63 #define I2C_SL_ADDR1 0x02c
64 #define I2C_SL_ADDR2 0x030
65 #define I2C_TX_FIFO 0x050
66 #define I2C_RX_FIFO 0x054
67 #define I2C_PACKET_TRANSFER_STATUS 0x058
68 #define I2C_FIFO_CONTROL 0x05c
69 #define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
70 #define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
71 #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
72 #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
73 #define I2C_FIFO_STATUS 0x060
74 #define I2C_FIFO_STATUS_TX_MASK 0xF0
75 #define I2C_FIFO_STATUS_TX_SHIFT 4
76 #define I2C_FIFO_STATUS_RX_MASK 0x0F
77 #define I2C_FIFO_STATUS_RX_SHIFT 0
78 #define I2C_INT_MASK 0x064
79 #define I2C_INT_STATUS 0x068
80 #define I2C_INT_BUS_CLEAR_DONE (1<<11)
81 #define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
82 #define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
83 #define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
84 #define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
85 #define I2C_INT_NO_ACK (1<<3)
86 #define I2C_INT_ARBITRATION_LOST (1<<2)
87 #define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
88 #define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
90 #define I2C_CLK_DIVISOR 0x06c
91 #define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
92 #define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
94 #define DVC_CTRL_REG1 0x000
95 #define DVC_CTRL_REG1_INTR_EN (1<<10)
96 #define DVC_CTRL_REG2 0x004
97 #define DVC_CTRL_REG3 0x008
98 #define DVC_CTRL_REG3_SW_PROG (1<<26)
99 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
100 #define DVC_STATUS 0x00c
101 #define DVC_STATUS_I2C_DONE_INTR (1<<30)
103 #define I2C_ERR_NONE 0x00
104 #define I2C_ERR_NO_ACK 0x01
105 #define I2C_ERR_ARBITRATION_LOST 0x02
106 #define I2C_ERR_UNKNOWN_INTERRUPT 0x04
107 #define I2C_ERR_UNEXPECTED_STATUS 0x08
109 #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
110 #define PACKET_HEADER0_PACKET_ID_SHIFT 16
111 #define PACKET_HEADER0_CONT_ID_SHIFT 12
112 #define PACKET_HEADER0_CONT_ID_MASK 0xF
113 #define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
115 #define I2C_HEADER_HIGHSPEED_MODE (1<<22)
116 #define I2C_HEADER_CONT_ON_NAK (1<<21)
117 #define I2C_HEADER_SEND_START_BYTE (1<<20)
118 #define I2C_HEADER_READ (1<<19)
119 #define I2C_HEADER_10BIT_ADDR (1<<18)
120 #define I2C_HEADER_IE_ENABLE (1<<17)
121 #define I2C_HEADER_REPEAT_START (1<<16)
122 #define I2C_HEADER_CONTINUE_XFER (1<<15)
123 #define I2C_HEADER_MASTER_ADDR_SHIFT 12
124 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
126 #define I2C_BUS_CLEAR_CNFG 0x084
127 #define I2C_BC_SCLK_THRESHOLD (9<<16)
128 #define I2C_BC_STOP_COND (1<<2)
129 #define I2C_BC_TERMINATE (1<<1)
130 #define I2C_BC_ENABLE (1<<0)
132 #define I2C_BUS_CLEAR_STATUS 0x088
133 #define I2C_BC_STATUS (1<<0)
135 #define I2C_CONFIG_LOAD 0x08C
136 #define I2C_MSTR_CONFIG_LOAD (1 << 0)
137 #define I2C_SLV_CONFIG_LOAD (1 << 1)
138 #define I2C_TIMEOUT_CONFIG_LOAD (1 << 2)
140 #define I2C_CLKEN_OVERRIDE 0x090
141 #define I2C_MST_CORE_CLKEN_OVR (1 << 0)
143 #define SL_ADDR1(addr) (addr & 0xff)
144 #define SL_ADDR2(addr) ((addr >> 8) & 0xff)
146 #define MAX_BUSCLEAR_CLOCK (9 * 8 + 1)
147 #define I2C_MAX_TRANSFER_LEN 4096
149 * msg_end_type: The bus control which need to be send at end of transfer.
150 * @MSG_END_STOP: Send stop pulse at end of transfer.
151 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
152 * @MSG_END_CONTINUE: The following on message is coming and so do not send
153 * stop or repeat start.
157 MSG_END_REPEAT_START,
161 struct tegra_i2c_chipdata {
162 bool timeout_irq_occurs_before_bus_inactive;
163 bool has_xfer_complete_interrupt;
164 bool has_hw_arb_support;
166 bool has_clk_divisor_std_fast_mode;
167 bool has_continue_xfer_support;
168 u16 clk_divisor_std_fast_mode;
169 u16 clk_divisor_fast_plus_mode;
170 u16 clk_divisor_hs_mode;
171 int clk_multiplier_hs_mode;
172 bool has_config_load_reg;
173 bool has_multi_master_en_bit;
174 bool has_clk_override_reg;
178 * struct tegra_i2c_dev - per device i2c context
179 * @dev: device reference for power management
180 * @adapter: core i2c layer adapter information
181 * @div_clk: clock reference for div clock of i2c controller.
182 * @fast_clk: clock reference for fast clock of i2c controller.
183 * @base: ioremapped registers cookie
184 * @cont_id: i2c controller id, used for for packet header
185 * @irq: irq number of transfer complete interrupt
186 * @is_dvc: identifies the DVC i2c controller, has a different register layout
187 * @msg_complete: transfer completion notifier
188 * @msg_err: error code for completed message
189 * @msg_buf: pointer to current message data
190 * @msg_buf_remaining: size of unsent data in the message buffer
191 * @msg_read: identifies read transfers
192 * @bus_clk_rate: current i2c bus clock rate
193 * @is_suspended: prevents i2c controller accesses after suspend is called
195 struct tegra_i2c_dev {
197 struct i2c_adapter adapter;
199 struct clk *fast_clk;
200 bool needs_cl_dvfs_clock;
201 struct clk *dvfs_ref_clk;
202 struct clk *dvfs_soc_clk;
203 spinlock_t fifo_lock;
210 struct completion msg_complete;
216 u32 next_packet_header;
218 u32 next_payload_size;
221 size_t msg_buf_remaining;
222 size_t next_msg_buf_remaining;
225 struct i2c_msg *msgs;
229 unsigned long bus_clk_rate;
232 bool is_clkon_always;
233 bool is_interruptable_xfer;
234 bool is_high_speed_enable;
236 u16 clk_divisor_non_hs_mode;
237 bool use_single_xfer_complete;
238 const struct tegra_i2c_chipdata *chipdata;
241 struct i2c_algo_bit_data bit_data;
242 const struct i2c_algorithm *bit_algo;
243 bool bit_banging_xfer_after_shutdown;
245 struct notifier_block pm_nb;
246 struct tegra_prod_list *prod_list;
247 bool is_multimaster_mode;
250 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg)
252 writel(val, i2c_dev->base + reg);
255 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
257 return readl(i2c_dev->base + reg);
260 static void dvc_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
262 u32 int_mask = dvc_readl(i2c_dev, DVC_CTRL_REG3);
264 dvc_writel(i2c_dev, int_mask, DVC_CTRL_REG3);
267 static void dvc_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
269 u32 int_mask = dvc_readl(i2c_dev, DVC_CTRL_REG3);
271 dvc_writel(i2c_dev, int_mask, DVC_CTRL_REG3);
275 * i2c_writel and i2c_readl will offset the register if necessary to talk
276 * to the I2C block inside the DVC block
278 static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
282 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
286 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
289 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
291 /* Read back register to make sure that register writes completed */
292 if (reg != I2C_TX_FIFO)
293 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
296 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
298 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
301 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
302 unsigned long reg, int len)
305 unsigned long flags = 0;
307 if (!i2c_dev->is_interruptable_xfer)
308 spin_lock_irqsave(&i2c_dev->mem_lock, flags);
311 writel(*buf++, i2c_dev->base +
312 tegra_i2c_reg_addr(i2c_dev, reg));
314 if (!i2c_dev->is_interruptable_xfer)
315 spin_unlock_irqrestore(&i2c_dev->mem_lock, flags);
318 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
319 unsigned long reg, int len)
322 unsigned long flags = 0;
324 if (!i2c_dev->is_interruptable_xfer)
325 spin_lock_irqsave(&i2c_dev->mem_lock, flags);
328 *buf++ = readl(i2c_dev->base +
329 tegra_i2c_reg_addr(i2c_dev, reg));
331 if (!i2c_dev->is_interruptable_xfer)
332 spin_unlock_irqrestore(&i2c_dev->mem_lock, flags);
335 static inline void tegra_i2c_gpio_setscl(void *data, int state)
337 struct tegra_i2c_dev *i2c_dev = data;
339 gpio_set_value(i2c_dev->scl_gpio, state);
342 static inline int tegra_i2c_gpio_getscl(void *data)
344 struct tegra_i2c_dev *i2c_dev = data;
346 return gpio_get_value(i2c_dev->scl_gpio);
349 static inline void tegra_i2c_gpio_setsda(void *data, int state)
351 struct tegra_i2c_dev *i2c_dev = data;
353 gpio_set_value(i2c_dev->sda_gpio, state);
356 static inline int tegra_i2c_gpio_getsda(void *data)
358 struct tegra_i2c_dev *i2c_dev = data;
360 return gpio_get_value(i2c_dev->sda_gpio);
363 static int tegra_i2c_gpio_request(struct tegra_i2c_dev *i2c_dev)
367 ret = gpio_request_one(i2c_dev->scl_gpio,
368 GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN,
371 dev_err(i2c_dev->dev, "GPIO request for gpio %d failed %d\n",
372 i2c_dev->scl_gpio, ret);
376 ret = gpio_request_one(i2c_dev->sda_gpio,
377 GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN,
380 dev_err(i2c_dev->dev, "GPIO request for gpio %d failed %d\n",
381 i2c_dev->sda_gpio, ret);
382 gpio_free(i2c_dev->scl_gpio);
388 static void tegra_i2c_gpio_free(struct tegra_i2c_dev *i2c_dev)
390 gpio_free(i2c_dev->scl_gpio);
391 gpio_free(i2c_dev->sda_gpio);
394 static int tegra_i2c_gpio_xfer(struct i2c_adapter *adap,
395 struct i2c_msg msgs[], int num)
397 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
400 ret = tegra_i2c_gpio_request(i2c_dev);
404 ret = i2c_dev->bit_algo->master_xfer(adap, msgs, num);
406 dev_err(i2c_dev->dev, "i2c-bit-algo xfer failed %d\n", ret);
408 tegra_i2c_gpio_free(i2c_dev);
412 static int tegra_i2c_gpio_init(struct tegra_i2c_dev *i2c_dev)
414 struct i2c_algo_bit_data *bit_data = &i2c_dev->bit_data;
416 bit_data->setsda = tegra_i2c_gpio_setsda;
417 bit_data->getsda = tegra_i2c_gpio_getsda;
418 bit_data->setscl = tegra_i2c_gpio_setscl;
419 bit_data->getscl = tegra_i2c_gpio_getscl;
420 bit_data->data = i2c_dev;
421 bit_data->udelay = 20; /* 50KHz */
422 bit_data->timeout = HZ; /* 10 ms*/
423 i2c_dev->bit_algo = &i2c_bit_algo;
424 i2c_dev->adapter.algo_data = bit_data;
428 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
430 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
432 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
435 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
437 u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK);
439 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
442 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
444 unsigned long timeout = jiffies + HZ;
445 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
446 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
447 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
449 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
450 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
451 if (time_after(jiffies, timeout)) {
452 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
460 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
464 u8 *buf = i2c_dev->msg_buf;
465 size_t buf_remaining = i2c_dev->msg_buf_remaining;
466 int words_to_transfer;
468 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
469 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
470 I2C_FIFO_STATUS_RX_SHIFT;
472 /* Rounds down to not include partial word at the end of buf */
473 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
474 if (words_to_transfer > rx_fifo_avail)
475 words_to_transfer = rx_fifo_avail;
477 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
479 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
480 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
481 rx_fifo_avail -= words_to_transfer;
484 * If there is a partial word at the end of buf, handle it manually to
485 * prevent overwriting past the end of buf
487 if (rx_fifo_avail > 0 && buf_remaining > 0) {
488 BUG_ON(buf_remaining > 3);
489 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
490 memcpy(buf, &val, buf_remaining);
495 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
496 i2c_dev->msg_buf_remaining = buf_remaining;
497 i2c_dev->msg_buf = buf;
501 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
506 size_t buf_remaining;
507 int words_to_transfer;
509 if (!i2c_dev->msg_buf_remaining)
511 buf = i2c_dev->msg_buf;
512 buf_remaining = i2c_dev->msg_buf_remaining;
514 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
515 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
516 I2C_FIFO_STATUS_TX_SHIFT;
518 /* Rounds down to not include partial word at the end of buf */
519 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
521 /* It's very common to have < 4 bytes, so optimize that case. */
522 if (words_to_transfer) {
523 if (words_to_transfer > tx_fifo_avail)
524 words_to_transfer = tx_fifo_avail;
527 * Update state before writing to FIFO. If this casues us
528 * to finish writing all bytes (AKA buf_remaining goes to 0) we
529 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
530 * not maskable). We need to make sure that the isr sees
531 * buf_remaining as 0 and doesn't call us back re-entrantly.
533 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
534 tx_fifo_avail -= words_to_transfer;
535 i2c_dev->msg_buf_remaining = buf_remaining;
536 i2c_dev->msg_buf = buf +
537 words_to_transfer * BYTES_PER_FIFO_WORD;
540 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
542 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
546 * If there is a partial word at the end of buf, handle it manually to
547 * prevent reading past the end of buf, which could cross a page
548 * boundary and fault.
550 if (tx_fifo_avail > 0 && buf_remaining > 0) {
551 if (buf_remaining > 3) {
552 dev_err(i2c_dev->dev,
553 "Remaining buffer more than 3 %zd\n",
557 memcpy(&val, buf, buf_remaining);
559 /* Again update before writing to FIFO to make sure isr sees. */
560 i2c_dev->msg_buf_remaining = 0;
561 i2c_dev->msg_buf = NULL;
564 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
571 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
572 * block. This block is identical to the rest of the I2C blocks, except that
573 * it only supports master mode, it has registers moved around, and it needs
574 * some extra init to get it into I2C mode. The register moves are handled
575 * by i2c_readl and i2c_writel
577 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
580 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
581 val |= DVC_CTRL_REG3_SW_PROG;
582 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
584 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
585 val |= DVC_CTRL_REG1_INTR_EN;
586 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
589 static void tegra_i2c_slave_init(struct tegra_i2c_dev *i2c_dev)
591 u32 val = I2C_SL_CNFG_NEWSL | I2C_SL_CNFG_NACK;
593 i2c_writel(i2c_dev, val, I2C_SL_CNFG);
595 if (i2c_dev->slave_addr) {
596 u16 addr = i2c_dev->slave_addr;
598 i2c_writel(i2c_dev, SL_ADDR1(addr), I2C_SL_ADDR1);
599 i2c_writel(i2c_dev, SL_ADDR2(addr), I2C_SL_ADDR2);
603 static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
607 if (i2c_dev->needs_cl_dvfs_clock) {
608 ret = clk_prepare_enable(i2c_dev->dvfs_soc_clk);
610 dev_err(i2c_dev->dev,
611 "Error in enabling dvfs soc clock %d\n", ret);
614 ret = clk_prepare_enable(i2c_dev->dvfs_ref_clk);
616 dev_err(i2c_dev->dev,
617 "Error in enabling dvfs ref clock %d\n", ret);
621 if (i2c_dev->chipdata->has_fast_clock) {
622 ret = clk_prepare_enable(i2c_dev->fast_clk);
624 dev_err(i2c_dev->dev,
625 "Enabling fast clk failed, err %d\n", ret);
629 ret = clk_prepare_enable(i2c_dev->div_clk);
631 dev_err(i2c_dev->dev,
632 "Enabling div clk failed, err %d\n", ret);
638 if (i2c_dev->chipdata->has_fast_clock)
639 clk_disable_unprepare(i2c_dev->fast_clk);
641 if (i2c_dev->needs_cl_dvfs_clock)
642 clk_disable_unprepare(i2c_dev->dvfs_ref_clk);
644 if (i2c_dev->needs_cl_dvfs_clock)
645 clk_disable_unprepare(i2c_dev->dvfs_soc_clk);
649 static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
651 clk_disable_unprepare(i2c_dev->div_clk);
652 if (i2c_dev->chipdata->has_fast_clock)
653 clk_disable_unprepare(i2c_dev->fast_clk);
654 if (i2c_dev->needs_cl_dvfs_clock) {
655 clk_disable_unprepare(i2c_dev->dvfs_soc_clk);
656 clk_disable_unprepare(i2c_dev->dvfs_ref_clk);
660 static int tegra_i2c_set_clk_rate(struct tegra_i2c_dev *i2c_dev)
665 if (i2c_dev->is_high_speed_enable)
666 clk_multiplier = i2c_dev->chipdata->clk_multiplier_hs_mode
667 * (i2c_dev->chipdata->clk_divisor_hs_mode + 1);
669 clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE
670 * (i2c_dev->clk_divisor_non_hs_mode + 1);
672 ret = clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate
675 dev_err(i2c_dev->dev, "set clock rate %lu failed: %d\n",
676 i2c_dev->bus_clk_rate * clk_multiplier, ret);
682 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
687 unsigned long timeout;
689 err = tegra_i2c_clock_enable(i2c_dev);
691 dev_err(i2c_dev->dev, "Clock enable failed %d\n", err);
695 tegra_periph_reset_assert(i2c_dev->div_clk);
697 tegra_periph_reset_deassert(i2c_dev->div_clk);
700 tegra_dvc_init(i2c_dev);
702 val = I2C_CNFG_NEW_MASTER_FSM;
703 if (!i2c_dev->is_high_speed_enable)
704 val |= (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
706 i2c_writel(i2c_dev, val, I2C_CNFG);
707 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
709 err = tegra_i2c_set_clk_rate(i2c_dev);
713 clk_divisor |= i2c_dev->chipdata->clk_divisor_hs_mode;
714 if (i2c_dev->chipdata->has_clk_divisor_std_fast_mode)
715 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode
716 << I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
717 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
719 if (!i2c_dev->is_dvc) {
720 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
721 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
722 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
723 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
724 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
728 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
729 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
730 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
732 if (!i2c_dev->is_dvc)
733 tegra_i2c_slave_init(i2c_dev);
735 if (tegra_i2c_flush_fifos(i2c_dev))
738 if (i2c_dev->is_multimaster_mode && i2c_dev->chipdata->has_clk_override_reg)
739 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
741 if (i2c_dev->chipdata->has_config_load_reg) {
742 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
743 timeout = jiffies + msecs_to_jiffies(1000);
744 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
745 if (time_after(jiffies, timeout)) {
746 dev_warn(i2c_dev->dev, "timeout waiting for config load\n");
753 tegra_prod_set_list(&i2c_dev->base, i2c_dev->prod_list);
755 tegra_i2c_clock_disable(i2c_dev);
757 if (i2c_dev->irq_disabled) {
758 i2c_dev->irq_disabled = 0;
759 enable_irq(i2c_dev->irq);
764 static int tegra_i2c_copy_next_to_current(struct tegra_i2c_dev *i2c_dev)
766 i2c_dev->msg_buf = i2c_dev->next_msg_buf;
767 i2c_dev->msg_buf_remaining = i2c_dev->next_msg_buf_remaining;
768 i2c_dev->msg_err = i2c_dev->next_msg_err;
769 i2c_dev->msg_read = i2c_dev->next_msg_read;
770 i2c_dev->msg_add = i2c_dev->next_msg_add;
771 i2c_dev->packet_header = i2c_dev->next_packet_header;
772 i2c_dev->io_header = i2c_dev->next_io_header;
773 i2c_dev->payload_size = i2c_dev->next_payload_size;
778 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
781 unsigned long flags = 0;
783 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST
784 | I2C_INT_TX_FIFO_OVERFLOW;
785 struct tegra_i2c_dev *i2c_dev = dev_id;
788 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
791 dev_dbg(i2c_dev->dev, "unknown interrupt Add 0x%02x\n",
793 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
795 if (!i2c_dev->irq_disabled) {
796 disable_irq_nosync(i2c_dev->irq);
797 i2c_dev->irq_disabled = 1;
799 /* Clear all interrupts */
800 status = 0xFFFFFFFFU;
804 if (unlikely(status & status_err)) {
805 dev_dbg(i2c_dev->dev, "I2c error status 0x%08x\n", status);
806 if (status & I2C_INT_NO_ACK) {
807 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
808 dev_dbg(i2c_dev->dev, "no acknowledge from address"
809 " 0x%x\n", i2c_dev->msg_add);
810 dev_dbg(i2c_dev->dev, "Packet status 0x%08x\n",
811 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
814 if (status & I2C_INT_ARBITRATION_LOST) {
815 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
816 dev_dbg(i2c_dev->dev, "arbitration lost during "
817 " communicate to add 0x%x\n", i2c_dev->msg_add);
818 dev_dbg(i2c_dev->dev, "Packet status 0x%08x\n",
819 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
822 if (status & I2C_INT_TX_FIFO_OVERFLOW) {
823 i2c_dev->msg_err |= I2C_INT_TX_FIFO_OVERFLOW;
824 dev_dbg(i2c_dev->dev, "Tx fifo overflow during "
825 " communicate to add 0x%x\n", i2c_dev->msg_add);
826 dev_dbg(i2c_dev->dev, "Packet status 0x%08x\n",
827 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
832 if (i2c_dev->chipdata->has_hw_arb_support &&
833 (status & I2C_INT_BUS_CLEAR_DONE))
836 if (unlikely((i2c_readl(i2c_dev, I2C_STATUS) & I2C_STATUS_BUSY)
837 && (status == I2C_INT_TX_FIFO_DATA_REQ)
839 && i2c_dev->msg_buf_remaining)) {
840 dev_dbg(i2c_dev->dev, "unexpected status\n");
841 i2c_dev->msg_err |= I2C_ERR_UNEXPECTED_STATUS;
843 if (!i2c_dev->irq_disabled) {
844 disable_irq_nosync(i2c_dev->irq);
845 i2c_dev->irq_disabled = 1;
851 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
852 if (i2c_dev->msg_buf_remaining)
853 tegra_i2c_empty_rx_fifo(i2c_dev);
855 dev_err(i2c_dev->dev, "--- register dump for buffer remain == 0----\n");
856 dev_err(i2c_dev->dev, "I2C_CNFG - 0x%x\n",
857 i2c_readl(i2c_dev, I2C_CNFG));
858 dev_err(i2c_dev->dev, "I2C_PACKET_TRANSFER_STATUS - 0x%x\n",
859 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
860 dev_err(i2c_dev->dev, "I2C_FIFO_CONTROL - 0x%x\n",
861 i2c_readl(i2c_dev, I2C_FIFO_CONTROL));
862 dev_err(i2c_dev->dev, "I2C_FIFO_STATUS - 0x%x\n",
863 i2c_readl(i2c_dev, I2C_FIFO_STATUS));
864 dev_err(i2c_dev->dev, "I2C_INT_MASK - 0x%x\n",
865 i2c_readl(i2c_dev, I2C_INT_MASK));
866 dev_err(i2c_dev->dev, "I2C_INT_STATUS - 0x%x\n",
867 i2c_readl(i2c_dev, I2C_INT_STATUS));
868 dev_err(i2c_dev->dev, "msg_err - 0x%x, msg_read - 0x%x, msg_add - 0x%x\n",
869 i2c_dev->msg_err, i2c_dev->msg_read, i2c_dev->msg_add);
871 struct i2c_msg *msgs = i2c_dev->msgs;
873 for (i = 0; i < i2c_dev->msgs_num; i++)
874 dev_err(i2c_dev->dev,
875 "msgs[%d] %c, addr=0x%04x, len=%d\n",
876 i, (msgs[i].flags & I2C_M_RD) ? 'R' : 'W',
877 msgs[i].addr, msgs[i].len);
883 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
884 if (i2c_dev->msg_buf_remaining) {
886 if (!i2c_dev->is_interruptable_xfer)
887 spin_lock_irqsave(&i2c_dev->fifo_lock, flags);
889 tegra_i2c_fill_tx_fifo(i2c_dev);
891 if (!i2c_dev->is_interruptable_xfer)
892 spin_unlock_irqrestore(&i2c_dev->fifo_lock, flags);
896 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
899 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
902 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
904 if (status & I2C_INT_ALL_PACKETS_XFER_COMPLETE) {
905 BUG_ON(i2c_dev->msg_buf_remaining);
906 complete(&i2c_dev->msg_complete);
907 } else if ((status & I2C_INT_PACKET_XFER_COMPLETE)
908 && i2c_dev->use_single_xfer_complete) {
909 BUG_ON(i2c_dev->msg_buf_remaining);
910 complete(&i2c_dev->msg_complete);
916 dev_dbg(i2c_dev->dev, "reg: 0x%08x 0x%08x 0x%08x 0x%08x\n",
917 i2c_readl(i2c_dev, I2C_CNFG), i2c_readl(i2c_dev, I2C_STATUS),
918 i2c_readl(i2c_dev, I2C_INT_STATUS),
919 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
921 dev_dbg(i2c_dev->dev, "packet: 0x%08x %u 0x%08x\n",
922 i2c_dev->packet_header, i2c_dev->payload_size,
926 struct i2c_msg *msgs = i2c_dev->msgs;
929 for (i = 0; i < i2c_dev->msgs_num; i++)
930 dev_dbg(i2c_dev->dev,
931 "msgs[%d] %c, addr=0x%04x, len=%d\n",
932 i, (msgs[i].flags & I2C_M_RD) ? 'R' : 'W',
933 msgs[i].addr, msgs[i].len);
936 mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
937 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
938 I2C_INT_RX_FIFO_DATA_REQ | I2C_INT_TX_FIFO_OVERFLOW;
940 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
942 if (i2c_dev->chipdata->has_xfer_complete_interrupt)
943 mask |= I2C_INT_ALL_PACKETS_XFER_COMPLETE;
945 if (!(i2c_dev->use_single_xfer_complete &&
946 i2c_dev->chipdata->has_xfer_complete_interrupt))
947 mask |= I2C_INT_ALL_PACKETS_XFER_COMPLETE;
949 if (i2c_dev->chipdata->has_hw_arb_support)
950 mask |= I2C_INT_BUS_CLEAR_DONE;
952 /* An error occurred, mask all interrupts */
953 tegra_i2c_mask_irq(i2c_dev, mask);
955 /* An error occured, mask dvc interrupt */
956 if (i2c_dev->is_dvc) {
957 dvc_i2c_mask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
958 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
961 complete(&i2c_dev->msg_complete);
966 static int tegra_i2c_send_next_read_msg_pkt_header(struct tegra_i2c_dev *i2c_dev, struct i2c_msg *next_msg, enum msg_end_type end_state)
968 i2c_dev->next_msg_buf = next_msg->buf;
969 i2c_dev->next_msg_buf_remaining = next_msg->len;
970 i2c_dev->next_msg_err = I2C_ERR_NONE;
971 i2c_dev->next_msg_read = 1;
972 i2c_dev->next_msg_add = next_msg->addr;
973 i2c_dev->next_packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
974 PACKET_HEADER0_PROTOCOL_I2C |
975 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
976 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
978 i2c_writel(i2c_dev, i2c_dev->next_packet_header, I2C_TX_FIFO);
980 i2c_dev->next_payload_size = next_msg->len - 1;
981 i2c_writel(i2c_dev, i2c_dev->next_payload_size, I2C_TX_FIFO);
983 i2c_dev->next_io_header = I2C_HEADER_IE_ENABLE;
985 if (end_state == MSG_END_CONTINUE)
986 i2c_dev->next_io_header |= I2C_HEADER_CONTINUE_XFER;
987 else if (end_state == MSG_END_REPEAT_START)
988 i2c_dev->next_io_header |= I2C_HEADER_REPEAT_START;
990 if (next_msg->flags & I2C_M_TEN) {
991 i2c_dev->next_io_header |= next_msg->addr;
992 i2c_dev->next_io_header |= I2C_HEADER_10BIT_ADDR;
994 i2c_dev->next_io_header |= (next_msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT);
996 if (next_msg->flags & I2C_M_IGNORE_NAK)
997 i2c_dev->next_io_header |= I2C_HEADER_CONT_ON_NAK;
999 i2c_dev->next_io_header |= I2C_HEADER_READ;
1001 if (i2c_dev->is_high_speed_enable) {
1002 i2c_dev->next_io_header |= I2C_HEADER_HIGHSPEED_MODE;
1003 i2c_dev->next_io_header |= ((i2c_dev->hs_master_code & 0x7)
1004 << I2C_HEADER_MASTER_ADDR_SHIFT);
1006 i2c_writel(i2c_dev, i2c_dev->next_io_header, I2C_TX_FIFO);
1011 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
1012 struct i2c_msg *msg, enum msg_end_type end_state,
1013 struct i2c_msg *next_msg, enum msg_end_type next_msg_end_state)
1017 unsigned long flags = 0;
1018 unsigned long timeout;
1025 tegra_i2c_flush_fifos(i2c_dev);
1028 i2c_dev->msg_buf = msg->buf;
1029 i2c_dev->msg_buf_remaining = msg->len;
1030 i2c_dev->msg_err = I2C_ERR_NONE;
1031 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
1032 INIT_COMPLETION(i2c_dev->msg_complete);
1034 if (!i2c_dev->is_interruptable_xfer)
1035 spin_lock_irqsave(&i2c_dev->fifo_lock, flags);
1037 cnfg = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN;
1038 if (i2c_dev->chipdata->has_multi_master_en_bit)
1039 cnfg |= I2C_CNFG_MULTI_MASTER_MODE;
1041 if (!i2c_dev->is_high_speed_enable)
1042 cnfg |= (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
1044 i2c_writel(i2c_dev, cnfg, I2C_CNFG);
1046 if (i2c_dev->chipdata->has_config_load_reg) {
1047 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD,
1049 timeout = jiffies + msecs_to_jiffies(1000);
1050 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
1051 if (time_after(jiffies, timeout)) {
1052 dev_warn(i2c_dev->dev,
1053 "timeout config_load");
1054 if (!i2c_dev->is_interruptable_xfer)
1055 spin_unlock_irqrestore(&i2c_dev->fifo_lock,
1062 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
1064 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
1065 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
1066 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
1068 i2c_dev->msg_add = msg->addr;
1070 i2c_dev->packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
1071 PACKET_HEADER0_PROTOCOL_I2C |
1072 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
1073 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
1074 i2c_writel(i2c_dev, i2c_dev->packet_header, I2C_TX_FIFO);
1076 i2c_dev->payload_size = msg->len - 1;
1077 i2c_writel(i2c_dev, i2c_dev->payload_size, I2C_TX_FIFO);
1079 i2c_dev->use_single_xfer_complete = true;
1080 i2c_dev->io_header = 0;
1081 if (next_msg == NULL)
1082 i2c_dev->io_header = I2C_HEADER_IE_ENABLE;
1084 if (end_state == MSG_END_CONTINUE)
1085 i2c_dev->io_header |= I2C_HEADER_CONTINUE_XFER;
1086 else if (end_state == MSG_END_REPEAT_START)
1087 i2c_dev->io_header |= I2C_HEADER_REPEAT_START;
1088 if (msg->flags & I2C_M_TEN) {
1089 i2c_dev->io_header |= msg->addr;
1090 i2c_dev->io_header |= I2C_HEADER_10BIT_ADDR;
1092 i2c_dev->io_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1094 if (msg->flags & I2C_M_IGNORE_NAK)
1095 i2c_dev->io_header |= I2C_HEADER_CONT_ON_NAK;
1096 if (msg->flags & I2C_M_RD)
1097 i2c_dev->io_header |= I2C_HEADER_READ;
1098 if (i2c_dev->is_high_speed_enable) {
1099 i2c_dev->io_header |= I2C_HEADER_HIGHSPEED_MODE;
1100 i2c_dev->io_header |= ((i2c_dev->hs_master_code & 0x7)
1101 << I2C_HEADER_MASTER_ADDR_SHIFT);
1103 i2c_writel(i2c_dev, i2c_dev->io_header, I2C_TX_FIFO);
1105 if (!(msg->flags & I2C_M_RD))
1106 tegra_i2c_fill_tx_fifo(i2c_dev);
1108 if (i2c_dev->is_dvc)
1109 dvc_i2c_unmask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
1111 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST
1112 | I2C_INT_TX_FIFO_OVERFLOW;
1113 if (i2c_dev->chipdata->has_xfer_complete_interrupt)
1114 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
1116 if (i2c_dev->chipdata->has_xfer_complete_interrupt)
1117 int_mask |= I2C_INT_ALL_PACKETS_XFER_COMPLETE;
1119 if (msg->flags & I2C_M_RD)
1120 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1121 else if (i2c_dev->msg_buf_remaining)
1122 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1124 if (next_msg != NULL) {
1125 tegra_i2c_send_next_read_msg_pkt_header(i2c_dev, next_msg,
1126 next_msg_end_state);
1127 tegra_i2c_copy_next_to_current(i2c_dev);
1128 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1129 i2c_dev->use_single_xfer_complete = false;
1132 if (!(i2c_dev->use_single_xfer_complete &&
1133 i2c_dev->chipdata->has_xfer_complete_interrupt))
1134 int_mask |= I2C_INT_ALL_PACKETS_XFER_COMPLETE;
1136 if (!i2c_dev->is_interruptable_xfer)
1137 spin_unlock_irqrestore(&i2c_dev->fifo_lock, flags);
1139 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1141 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
1142 i2c_readl(i2c_dev, I2C_INT_MASK));
1144 ret = wait_for_completion_timeout(&i2c_dev->msg_complete,
1147 dev_err(i2c_dev->dev, "--- register dump for debugging ----\n");
1148 dev_err(i2c_dev->dev, "I2C_CNFG - 0x%x\n",
1149 i2c_readl(i2c_dev, I2C_CNFG));
1150 dev_err(i2c_dev->dev, "I2C_PACKET_TRANSFER_STATUS - 0x%x\n",
1151 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
1152 dev_err(i2c_dev->dev, "I2C_FIFO_CONTROL - 0x%x\n",
1153 i2c_readl(i2c_dev, I2C_FIFO_CONTROL));
1154 dev_err(i2c_dev->dev, "I2C_FIFO_STATUS - 0x%x\n",
1155 i2c_readl(i2c_dev, I2C_FIFO_STATUS));
1156 dev_err(i2c_dev->dev, "I2C_INT_MASK - 0x%x\n",
1157 i2c_readl(i2c_dev, I2C_INT_MASK));
1158 dev_err(i2c_dev->dev, "I2C_INT_STATUS - 0x%x\n",
1159 i2c_readl(i2c_dev, I2C_INT_STATUS));
1161 dev_err(i2c_dev->dev, "msg->len - %d\n", msg->len);
1162 dev_err(i2c_dev->dev, "is_msg_write - %d\n",
1163 !(msg->flags & I2C_M_RD));
1164 if (next_msg != NULL) {
1165 dev_err(i2c_dev->dev, "next_msg->len - %d\n",
1167 dev_err(i2c_dev->dev, "is_next_msg_write - %d\n",
1168 !(next_msg->flags & I2C_M_RD));
1171 dev_err(i2c_dev->dev, "buf_remaining - %zd\n",
1172 i2c_dev->msg_buf_remaining);
1175 tegra_i2c_mask_irq(i2c_dev, int_mask);
1177 if (i2c_dev->is_dvc)
1178 dvc_i2c_mask_irq(i2c_dev, DVC_CTRL_REG3_I2C_DONE_INTR_EN);
1180 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
1181 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
1182 i2c_writel(i2c_dev, cnfg & (~I2C_CNFG_PACKET_MODE_EN),
1185 dev_err(i2c_dev->dev, "i2c_cnfg PACKET_MODE_EN not set\n");
1187 if (i2c_dev->chipdata->has_config_load_reg) {
1188 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD,
1190 timeout = jiffies + msecs_to_jiffies(1000);
1191 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
1192 if (time_after(jiffies, timeout)) {
1193 dev_warn(i2c_dev->dev,
1194 "timeout config_load");
1202 dev_err(i2c_dev->dev,
1203 "i2c transfer timed out, addr 0x%04x, data 0x%02x\n",
1204 msg->addr, msg->buf[0]);
1206 ret = tegra_i2c_init(i2c_dev);
1214 dev_dbg(i2c_dev->dev, "transfer complete: %d %d %d\n",
1215 ret, completion_done(&i2c_dev->msg_complete), i2c_dev->msg_err);
1217 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
1221 if (i2c_dev->msg_err & I2C_ERR_UNKNOWN_INTERRUPT)
1222 dev_warn(i2c_dev->dev, "unknown interrupt Add 0x%02x\n",
1224 if (i2c_dev->msg_err & I2C_ERR_NO_ACK)
1225 dev_warn(i2c_dev->dev, "no acknowledge from address 0x%x\n",
1227 if (i2c_dev->msg_err & I2C_ERR_ARBITRATION_LOST)
1228 dev_warn(i2c_dev->dev, "arb lost in communicate to add 0x%x\n",
1230 if (i2c_dev->msg_err & I2C_INT_TX_FIFO_OVERFLOW)
1231 dev_warn(i2c_dev->dev, "Tx fifo overflow to add 0x%x\n",
1233 if (i2c_dev->msg_err & I2C_ERR_UNEXPECTED_STATUS)
1234 dev_warn(i2c_dev->dev, "unexpected status to add 0x%x\n",
1237 if ((i2c_dev->chipdata->timeout_irq_occurs_before_bus_inactive) &&
1238 (i2c_dev->msg_err == I2C_ERR_NO_ACK)) {
1240 * In NACK error condition resetting of I2C controller happens
1241 * before STOP condition is properly completed by I2C controller,
1242 * so wait for 2 clock cycle to complete STOP condition.
1244 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
1248 * NACK interrupt is generated before the I2C controller generates the
1249 * STOP condition on the bus. So wait for 2 clock periods before resetting
1250 * the controller so that STOP condition has been delivered properly.
1252 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
1253 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
1255 ret = tegra_i2c_init(i2c_dev);
1261 /* Arbitration Lost occurs, Start recovery */
1262 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1263 if (i2c_dev->chipdata->has_hw_arb_support) {
1264 INIT_COMPLETION(i2c_dev->msg_complete);
1265 i2c_writel(i2c_dev, I2C_BC_ENABLE
1266 | I2C_BC_SCLK_THRESHOLD
1269 , I2C_BUS_CLEAR_CNFG);
1271 if (i2c_dev->chipdata->has_config_load_reg) {
1272 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD,
1274 timeout = jiffies + msecs_to_jiffies(1000);
1275 while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) {
1276 if (time_after(jiffies, timeout)) {
1277 dev_warn(i2c_dev->dev,
1278 "timeout config_load");
1285 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLEAR_DONE);
1287 wait_for_completion_timeout(&i2c_dev->msg_complete,
1290 if (!(i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS) & I2C_BC_STATUS))
1291 dev_warn(i2c_dev->dev, "Un-recovered Arbitration lost\n");
1293 i2c_algo_busclear_gpio(i2c_dev->dev,
1294 i2c_dev->scl_gpio, GPIOF_OPEN_DRAIN,
1295 i2c_dev->sda_gpio,GPIOF_OPEN_DRAIN,
1296 MAX_BUSCLEAR_CLOCK, 100000);
1301 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
1304 if (i2c_dev->msg_err & I2C_ERR_UNEXPECTED_STATUS)
1310 static int tegra_i2c_split_i2c_msg_xfer(struct tegra_i2c_dev *i2c_dev,
1311 struct i2c_msg *msg, enum msg_end_type end_type,
1312 enum msg_end_type next_msg_end_type)
1315 struct i2c_msg temp_msg;
1317 enum msg_end_type temp_end_type;
1320 temp_msg.flags = msg->flags;
1321 temp_msg.addr = msg->addr;
1322 temp_end_type = end_type;
1325 len = min(size, I2C_MAX_TRANSFER_LEN);
1328 if ((len == I2C_MAX_TRANSFER_LEN) && size)
1329 end_type = MSG_END_CONTINUE;
1331 end_type = temp_end_type;
1332 ret = tegra_i2c_xfer_msg(i2c_dev, &temp_msg, end_type,
1333 NULL, next_msg_end_type);
1337 } while (size != 0);
1342 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1345 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1348 BUG_ON(!rt_mutex_is_locked(&(adap->bus_lock)));
1349 if (i2c_dev->is_suspended)
1352 if ((i2c_dev->is_shutdown || adap->atomic_xfer_only)
1353 && i2c_dev->bit_banging_xfer_after_shutdown)
1354 return tegra_i2c_gpio_xfer(adap, msgs, num);
1356 if (adap->atomic_xfer_only)
1359 if (adap->bus_clk_rate != i2c_dev->bus_clk_rate) {
1360 i2c_dev->bus_clk_rate = adap->bus_clk_rate;
1361 ret = tegra_i2c_set_clk_rate(i2c_dev);
1366 i2c_dev->msgs = msgs;
1367 i2c_dev->msgs_num = num;
1369 pm_runtime_get_sync(&adap->dev);
1370 ret = tegra_i2c_clock_enable(i2c_dev);
1372 dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret);
1373 pm_runtime_put(&adap->dev);
1377 for (i = 0; i < num; i++) {
1378 enum msg_end_type end_type = MSG_END_STOP;
1379 enum msg_end_type next_msg_end_type = MSG_END_STOP;
1381 if (i < (num - 1)) {
1382 if (msgs[i + 1].flags & I2C_M_NOSTART)
1383 end_type = MSG_END_CONTINUE;
1385 end_type = MSG_END_REPEAT_START;
1387 if (msgs[i + 2].flags & I2C_M_NOSTART)
1388 next_msg_end_type = MSG_END_CONTINUE;
1390 next_msg_end_type = MSG_END_REPEAT_START;
1392 if ((!(msgs[i].flags & I2C_M_RD)) && (msgs[i].len <= 8)
1393 && (msgs[i+1].flags & I2C_M_RD)
1394 && (msgs[i+1].len <= I2C_MAX_TRANSFER_LEN)
1395 && (next_msg_end_type != MSG_END_CONTINUE)
1396 && (end_type == MSG_END_REPEAT_START)) {
1397 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type, &msgs[i+1], next_msg_end_type);
1402 if (msgs[i].len > I2C_MAX_TRANSFER_LEN) {
1403 ret = tegra_i2c_split_i2c_msg_xfer(i2c_dev, &msgs[i],
1404 end_type, next_msg_end_type);
1408 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type,
1409 NULL, next_msg_end_type);
1415 if (msgs[i].len > I2C_MAX_TRANSFER_LEN) {
1416 ret = tegra_i2c_split_i2c_msg_xfer(i2c_dev,
1422 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type, NULL, next_msg_end_type);
1429 tegra_i2c_clock_disable(i2c_dev);
1430 pm_runtime_put(&adap->dev);
1432 i2c_dev->msgs = NULL;
1433 i2c_dev->msgs_num = 0;
1438 static u32 tegra_i2c_func(struct i2c_adapter *adap)
1440 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1441 u32 ret = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
1442 I2C_FUNC_PROTOCOL_MANGLING;
1444 if (i2c_dev->chipdata->has_continue_xfer_support)
1445 ret |= I2C_FUNC_NOSTART;
1449 static const struct i2c_algorithm tegra_i2c_algo = {
1450 .master_xfer = tegra_i2c_xfer,
1451 .functionality = tegra_i2c_func,
1454 static int tegra_i2c_pm_notifier(struct notifier_block *nb,
1455 unsigned long event, void *data);
1457 static struct tegra_i2c_platform_data *parse_i2c_tegra_dt(
1458 struct platform_device *pdev)
1460 struct tegra_i2c_platform_data *pdata;
1461 struct device_node *np = pdev->dev.of_node;
1464 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1466 return ERR_PTR(-ENOMEM);
1468 if (!of_property_read_u32(np, "clock-frequency", &prop))
1469 pdata->bus_clk_rate = prop;
1471 pdata->is_clkon_always = of_property_read_bool(np,
1472 "nvidia,clock-always-on");
1474 if (!of_property_read_u32(np, "nvidia,hs-master-code", &prop)) {
1475 pdata->hs_master_code = prop;
1476 pdata->is_high_speed_enable = true;
1479 pdata->is_interruptable_xfer = of_property_read_bool(np,
1480 "nvidia,interruptable-transfer");
1481 pdata->needs_cl_dvfs_clock = of_property_read_bool(np,
1482 "nvidia,require-cldvfs-clock");
1484 pdata->bit_banging_xfer_after_shutdown = of_property_read_bool(np,
1485 "nvidia,bit-banging-xfer-after-shutdown");
1487 pdata->scl_gpio = of_get_named_gpio(np, "scl-gpio", 0);
1488 pdata->sda_gpio = of_get_named_gpio(np, "sda-gpio", 0);
1489 pdata->is_dvc = of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc");
1491 pdata->is_multimaster_mode = of_property_read_bool(np,
1492 "nvidia,multimaster-mode");
1494 if (pdata->is_multimaster_mode)
1495 pdata->is_clkon_always = true;
1497 /* Default configuration for device tree initiated driver */
1498 pdata->slave_addr = 0xFC;
1502 static struct tegra_i2c_chipdata tegra20_i2c_chipdata = {
1503 .timeout_irq_occurs_before_bus_inactive = true,
1504 .has_xfer_complete_interrupt = false,
1505 .has_continue_xfer_support = false,
1506 .has_hw_arb_support = false,
1507 .has_fast_clock = true,
1508 .has_clk_divisor_std_fast_mode = false,
1509 .clk_divisor_std_fast_mode = 0,
1510 .clk_divisor_hs_mode = 3,
1511 .clk_multiplier_hs_mode = 12,
1512 .has_config_load_reg = false,
1513 .has_multi_master_en_bit = false,
1514 .has_clk_override_reg = false,
1517 static struct tegra_i2c_chipdata tegra30_i2c_chipdata = {
1518 .timeout_irq_occurs_before_bus_inactive = true,
1519 .has_xfer_complete_interrupt = false,
1520 .has_continue_xfer_support = true,
1521 .has_hw_arb_support = false,
1522 .has_fast_clock = true,
1523 .has_clk_divisor_std_fast_mode = false,
1524 .clk_divisor_std_fast_mode = 0,
1525 .clk_divisor_hs_mode = 3,
1526 .clk_multiplier_hs_mode = 12,
1527 .has_config_load_reg = false,
1528 .has_multi_master_en_bit = false,
1529 .has_clk_override_reg = false,
1532 static struct tegra_i2c_chipdata tegra114_i2c_chipdata = {
1533 .timeout_irq_occurs_before_bus_inactive = false,
1534 .has_xfer_complete_interrupt = true,
1535 .has_continue_xfer_support = true,
1536 .has_hw_arb_support = true,
1537 .has_fast_clock = false,
1538 .has_clk_divisor_std_fast_mode = true,
1539 .clk_divisor_std_fast_mode = 0x19,
1540 .clk_divisor_fast_plus_mode = 0x10,
1541 .clk_divisor_hs_mode = 1,
1542 .clk_multiplier_hs_mode = 3,
1543 .has_config_load_reg = false,
1544 .has_multi_master_en_bit = false,
1545 .has_clk_override_reg = false,
1548 static struct tegra_i2c_chipdata tegra148_i2c_chipdata = {
1549 .timeout_irq_occurs_before_bus_inactive = false,
1550 .has_xfer_complete_interrupt = true,
1551 .has_continue_xfer_support = true,
1552 .has_hw_arb_support = true,
1553 .has_fast_clock = false,
1554 .has_clk_divisor_std_fast_mode = true,
1555 .clk_divisor_std_fast_mode = 0x19,
1556 .clk_divisor_fast_plus_mode = 0x19,
1557 .clk_divisor_hs_mode = 2,
1558 .clk_multiplier_hs_mode = 13,
1559 .has_config_load_reg = true,
1560 .has_multi_master_en_bit = false,
1561 .has_clk_override_reg = true,
1564 static struct tegra_i2c_chipdata tegra124_i2c_chipdata = {
1565 .timeout_irq_occurs_before_bus_inactive = false,
1566 .has_xfer_complete_interrupt = true,
1567 .has_continue_xfer_support = true,
1568 .has_hw_arb_support = true,
1569 .has_fast_clock = false,
1570 .has_clk_divisor_std_fast_mode = true,
1571 .clk_divisor_std_fast_mode = 0x19,
1572 .clk_divisor_fast_plus_mode = 0x10,
1573 .clk_divisor_hs_mode = 2,
1574 .clk_multiplier_hs_mode = 13,
1575 .has_config_load_reg = true,
1576 .has_multi_master_en_bit = false,
1577 .has_clk_override_reg = true,
1580 static struct tegra_i2c_chipdata tegra210_i2c_chipdata = {
1581 .timeout_irq_occurs_before_bus_inactive = false,
1582 .has_xfer_complete_interrupt = true,
1583 .has_continue_xfer_support = true,
1584 .has_hw_arb_support = true,
1585 .has_fast_clock = false,
1586 .has_clk_divisor_std_fast_mode = true,
1587 .clk_divisor_std_fast_mode = 0x19,
1588 .clk_divisor_fast_plus_mode = 0x10,
1589 .clk_divisor_hs_mode = 2,
1590 .clk_multiplier_hs_mode = 13,
1591 .has_config_load_reg = true,
1592 .has_multi_master_en_bit = true,
1593 .has_clk_override_reg = true,
1596 /* Match table for of_platform binding */
1597 static const struct of_device_id tegra_i2c_of_match[] = {
1598 { .compatible = "nvidia,tegra210-i2c",
1599 .data = &tegra210_i2c_chipdata, },
1600 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_chipdata, },
1601 { .compatible = "nvidia,tegra148-i2c", .data = &tegra148_i2c_chipdata, },
1602 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_chipdata, },
1603 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_chipdata, },
1604 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_chipdata, },
1605 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_chipdata, },
1608 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
1610 static struct platform_device_id tegra_i2c_devtype[] = {
1612 .name = "tegra-i2c",
1613 .driver_data = (unsigned long)&tegra30_i2c_chipdata,
1616 .name = "tegra20-i2c",
1617 .driver_data = (unsigned long)&tegra20_i2c_chipdata,
1620 .name = "tegra30-i2c",
1621 .driver_data = (unsigned long)&tegra30_i2c_chipdata,
1624 .name = "tegra11-i2c",
1625 .driver_data = (unsigned long)&tegra114_i2c_chipdata,
1628 .name = "tegra14-i2c",
1629 .driver_data = (unsigned long)&tegra148_i2c_chipdata,
1632 .name = "tegra12-i2c",
1633 .driver_data = (unsigned long)&tegra124_i2c_chipdata,
1636 .name = "tegra21-i2c",
1637 .driver_data = (unsigned long)&tegra210_i2c_chipdata,
1641 static int tegra_i2c_probe(struct platform_device *pdev)
1643 struct tegra_i2c_dev *i2c_dev;
1644 struct tegra_i2c_platform_data *pdata = pdev->dev.platform_data;
1645 struct resource *res;
1646 struct clk *div_clk;
1647 struct clk *fast_clk = NULL;
1648 struct clk *dvfs_ref_clk = NULL;
1649 struct clk *dvfs_soc_clk = NULL;
1653 const struct tegra_i2c_chipdata *chip_data = NULL;
1654 const struct of_device_id *match;
1656 struct pinctrl *pin;
1657 struct pinctrl_state *s;
1660 if (pdev->dev.of_node) {
1661 match = of_match_device(of_match_ptr(tegra_i2c_of_match), &pdev->dev);
1663 dev_err(&pdev->dev, "Device Not matching\n");
1666 chip_data = match->data;
1668 pdata = parse_i2c_tegra_dt(pdev);
1670 chip_data = (struct tegra_i2c_chipdata *)pdev->id_entry->driver_data;
1674 if (IS_ERR(pdata) || !pdata || !chip_data) {
1675 dev_err(&pdev->dev, "no platform/chip data?\n");
1676 return IS_ERR(pdata) ? PTR_ERR(pdata) : -ENODEV;
1679 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1681 dev_err(&pdev->dev, "no mem resource\n");
1685 base = devm_ioremap_resource(&pdev->dev, res);
1687 return PTR_ERR(base);
1689 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1691 dev_err(&pdev->dev, "no irq resource\n");
1696 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1698 dev_err(&pdev->dev, "Could not allocate struct tegra_i2c_dev");
1702 i2c_dev->chipdata = chip_data;
1703 i2c_dev->needs_cl_dvfs_clock = pdata->needs_cl_dvfs_clock;
1704 i2c_dev->is_interruptable_xfer = pdata->is_interruptable_xfer;
1706 div_clk = devm_clk_get(&pdev->dev, "div-clk");
1707 if (IS_ERR(div_clk)) {
1708 dev_err(&pdev->dev, "missing controller clock");
1709 return PTR_ERR(div_clk);
1712 if (i2c_dev->chipdata->has_fast_clock) {
1713 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
1714 if (IS_ERR(fast_clk)) {
1715 dev_err(&pdev->dev, "missing bus clock");
1716 return PTR_ERR(fast_clk);
1720 if (i2c_dev->needs_cl_dvfs_clock) {
1721 dvfs_ref_clk = devm_clk_get(&pdev->dev, "cl_dvfs_ref");
1722 if (IS_ERR(dvfs_ref_clk)) {
1723 dev_err(&pdev->dev, "missing cl_dvfs_ref clock");
1724 return PTR_ERR(dvfs_ref_clk);
1726 i2c_dev->dvfs_ref_clk = dvfs_ref_clk;
1727 dvfs_soc_clk = devm_clk_get(&pdev->dev, "cl_dvfs_soc");
1728 if (IS_ERR(dvfs_soc_clk)) {
1729 dev_err(&pdev->dev, "missing cl_dvfs_soc clock");
1730 return PTR_ERR(dvfs_soc_clk);
1732 i2c_dev->dvfs_soc_clk = dvfs_soc_clk;
1735 if (pdata->is_high_speed_enable) {
1736 pin = devm_pinctrl_get(&pdev->dev);
1738 dev_warn(&pdev->dev, "Missing pinctrl device\n");
1742 s = pinctrl_lookup_state(pin, "hs_mode");
1744 dev_warn(&pdev->dev, "Missing hs_mode state\n");
1748 ret = pinctrl_select_state(pin, s);
1750 dev_err(&pdev->dev, "setting state failed\n");
1755 i2c_dev->base = base;
1756 i2c_dev->div_clk = div_clk;
1757 if (i2c_dev->chipdata->has_fast_clock)
1758 i2c_dev->fast_clk = fast_clk;
1760 i2c_dev->dev = &pdev->dev;
1761 i2c_dev->is_clkon_always = pdata->is_clkon_always;
1762 i2c_dev->is_multimaster_mode = pdata->is_multimaster_mode;
1763 i2c_dev->bus_clk_rate = pdata->bus_clk_rate ? pdata->bus_clk_rate: 100000;
1764 i2c_dev->is_high_speed_enable = pdata->is_high_speed_enable;
1765 i2c_dev->clk_divisor_non_hs_mode =
1766 i2c_dev->chipdata->clk_divisor_std_fast_mode;
1767 if (i2c_dev->bus_clk_rate == 1000000)
1768 i2c_dev->clk_divisor_non_hs_mode =
1769 i2c_dev->chipdata->clk_divisor_fast_plus_mode;
1770 i2c_dev->msgs = NULL;
1771 i2c_dev->msgs_num = 0;
1772 i2c_dev->is_dvc = pdata->is_dvc;
1773 i2c_dev->slave_addr = pdata->slave_addr;
1774 i2c_dev->hs_master_code = pdata->hs_master_code;
1775 i2c_dev->bit_banging_xfer_after_shutdown =
1776 pdata->bit_banging_xfer_after_shutdown;
1777 init_completion(&i2c_dev->msg_complete);
1779 spin_lock_init(&i2c_dev->fifo_lock);
1781 spin_lock_init(&i2c_dev->mem_lock);
1783 if (pdev->dev.of_node)
1784 i2c_dev->prod_list = tegra_prod_init(pdev->dev.of_node);
1785 if (IS_ERR(i2c_dev->prod_list))
1786 i2c_dev->prod_list = NULL;
1788 platform_set_drvdata(pdev, i2c_dev);
1790 if (i2c_dev->is_clkon_always)
1791 tegra_i2c_clock_enable(i2c_dev);
1793 ret = tegra_i2c_init(i2c_dev);
1795 dev_err(&pdev->dev, "Failed to initialize i2c controller");
1799 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
1800 tegra_i2c_isr, IRQF_NO_SUSPEND,
1801 dev_name(&pdev->dev), i2c_dev);
1803 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
1807 pm_runtime_enable(&pdev->dev);
1809 i2c_dev->scl_gpio = pdata->scl_gpio;
1810 i2c_dev->sda_gpio = pdata->sda_gpio;
1811 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1812 i2c_dev->adapter.owner = THIS_MODULE;
1813 i2c_dev->adapter.class = I2C_CLASS_HWMON;
1814 strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter",
1815 sizeof(i2c_dev->adapter.name));
1816 i2c_dev->adapter.bus_clk_rate = i2c_dev->bus_clk_rate;
1817 i2c_dev->adapter.algo = &tegra_i2c_algo;
1818 i2c_dev->adapter.dev.parent = &pdev->dev;
1819 i2c_dev->adapter.nr = bus_num;
1820 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
1823 i2c_dev->adapter.retries = pdata->retries;
1825 i2c_dev->adapter.retries = TEGRA_I2C_RETRIES;
1828 i2c_dev->adapter.timeout = pdata->timeout;
1830 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
1832 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
1835 i2c_dev->cont_id = i2c_dev->adapter.nr & PACKET_HEADER0_CONT_ID_MASK;
1836 if (pdata->is_high_speed_enable) {
1837 sprintf(prod_name, "i2c%d_hs_prod", i2c_dev->cont_id);
1838 ret = tegra_pinctrl_config_prod(&pdev->dev, prod_name);
1840 dev_warn(&pdev->dev, "Failed to set %s setting\n",
1844 i2c_dev->pm_nb.notifier_call = tegra_i2c_pm_notifier;
1846 tegra_register_pm_notifier(&i2c_dev->pm_nb);
1848 of_i2c_register_devices(&i2c_dev->adapter);
1849 pm_runtime_enable(&i2c_dev->adapter.dev);
1850 tegra_i2c_gpio_init(i2c_dev);
1854 tegra_prod_release(&i2c_dev->prod_list);
1858 static int tegra_i2c_remove(struct platform_device *pdev)
1860 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1862 tegra_unregister_pm_notifier(&i2c_dev->pm_nb);
1863 i2c_del_adapter(&i2c_dev->adapter);
1864 pm_runtime_disable(&i2c_dev->adapter.dev);
1866 if (i2c_dev->is_clkon_always)
1867 tegra_i2c_clock_disable(i2c_dev);
1869 pm_runtime_disable(&pdev->dev);
1870 tegra_prod_release(&i2c_dev->prod_list);
1874 static void tegra_i2c_shutdown(struct platform_device *pdev)
1876 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1878 dev_info(i2c_dev->dev, "Shutting down\n");
1879 i2c_shutdown_adapter(&i2c_dev->adapter);
1880 i2c_dev->is_shutdown = true;
1883 #ifdef CONFIG_PM_SLEEP
1884 static int __tegra_i2c_suspend_noirq_late(struct tegra_i2c_dev *i2c_dev)
1886 i2c_dev->is_suspended = true;
1887 if (i2c_dev->is_clkon_always)
1888 tegra_i2c_clock_disable(i2c_dev);
1893 static int tegra_i2c_suspend_noirq_late(struct device *dev)
1895 struct platform_device *pdev = to_platform_device(dev);
1896 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1898 i2c_lock_adapter(&i2c_dev->adapter);
1900 __tegra_i2c_suspend_noirq_late(i2c_dev);
1902 i2c_unlock_adapter(&i2c_dev->adapter);
1907 static int __tegra_i2c_resume_noirq_early(struct tegra_i2c_dev *i2c_dev)
1911 if (i2c_dev->is_clkon_always)
1912 tegra_i2c_clock_enable(i2c_dev);
1914 ret = tegra_i2c_init(i2c_dev);
1918 i2c_dev->is_suspended = false;
1923 static int tegra_i2c_resume_noirq_early(struct device *dev)
1925 struct platform_device *pdev = to_platform_device(dev);
1926 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1928 i2c_lock_adapter(&i2c_dev->adapter);
1930 __tegra_i2c_resume_noirq_early(i2c_dev);
1932 i2c_unlock_adapter(&i2c_dev->adapter);
1937 static int tegra_i2c_pm_notifier(struct notifier_block *nb,
1938 unsigned long event, void *data)
1940 struct tegra_i2c_dev *i2c_dev = container_of(nb, struct tegra_i2c_dev, pm_nb);
1942 if (event == TEGRA_PM_SUSPEND)
1943 __tegra_i2c_suspend_noirq_late(i2c_dev);
1944 else if (event == TEGRA_PM_RESUME)
1945 __tegra_i2c_resume_noirq_early(i2c_dev);
1950 static const struct dev_pm_ops tegra_i2c_pm = {
1951 .suspend_noirq_late = tegra_i2c_suspend_noirq_late,
1952 .resume_noirq_early = tegra_i2c_resume_noirq_early,
1954 #define TEGRA_I2C_PM (&tegra_i2c_pm)
1956 #define TEGRA_I2C_PM NULL
1957 static int tegra_i2c_pm_notifier(struct notifier_block *nb,
1958 unsigned long event, void *data)
1964 static struct platform_driver tegra_i2c_driver = {
1965 .probe = tegra_i2c_probe,
1966 .remove = tegra_i2c_remove,
1967 .late_shutdown = tegra_i2c_shutdown,
1968 .id_table = tegra_i2c_devtype,
1970 .name = "tegra-i2c",
1971 .owner = THIS_MODULE,
1972 .of_match_table = of_match_ptr(tegra_i2c_of_match),
1977 static int __init tegra_i2c_init_driver(void)
1979 return platform_driver_register(&tegra_i2c_driver);
1982 static void __exit tegra_i2c_exit_driver(void)
1984 platform_driver_unregister(&tegra_i2c_driver);
1987 subsys_initcall(tegra_i2c_init_driver);
1988 module_exit(tegra_i2c_exit_driver);
1990 MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1991 MODULE_AUTHOR("Colin Cross");
1992 MODULE_LICENSE("GPL v2");