Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/i2c/busses/i2c-tegra.c |
| 3 | * |
| 4 | * Copyright (C) 2010 Google, Inc. |
| 5 | * Author: Colin Cross <ccross@android.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/i2c.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/slab.h> |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Paul Gortmaker | 93cf5d7 | 2011-07-29 21:14:30 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 30 | #include <linux/reset.h> |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/unaligned.h> |
| 33 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 34 | #define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000)) |
| 35 | #define BYTES_PER_FIFO_WORD 4 |
| 36 | |
| 37 | #define I2C_CNFG 0x000 |
Jay Cheng | 40abcf7 | 2011-04-25 15:32:27 -0600 | [diff] [blame] | 38 | #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 39 | #define I2C_CNFG_PACKET_MODE_EN (1<<10) |
| 40 | #define I2C_CNFG_NEW_MASTER_FSM (1<<11) |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 41 | #define I2C_STATUS 0x01C |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 42 | #define I2C_SL_CNFG 0x020 |
Stephen Warren | 5afa9d3 | 2011-06-06 11:25:19 -0600 | [diff] [blame] | 43 | #define I2C_SL_CNFG_NACK (1<<1) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 44 | #define I2C_SL_CNFG_NEWSL (1<<2) |
| 45 | #define I2C_SL_ADDR1 0x02c |
Stephen Warren | 5afa9d3 | 2011-06-06 11:25:19 -0600 | [diff] [blame] | 46 | #define I2C_SL_ADDR2 0x030 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 47 | #define I2C_TX_FIFO 0x050 |
| 48 | #define I2C_RX_FIFO 0x054 |
| 49 | #define I2C_PACKET_TRANSFER_STATUS 0x058 |
| 50 | #define I2C_FIFO_CONTROL 0x05c |
| 51 | #define I2C_FIFO_CONTROL_TX_FLUSH (1<<1) |
| 52 | #define I2C_FIFO_CONTROL_RX_FLUSH (1<<0) |
| 53 | #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5 |
| 54 | #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2 |
| 55 | #define I2C_FIFO_STATUS 0x060 |
| 56 | #define I2C_FIFO_STATUS_TX_MASK 0xF0 |
| 57 | #define I2C_FIFO_STATUS_TX_SHIFT 4 |
| 58 | #define I2C_FIFO_STATUS_RX_MASK 0x0F |
| 59 | #define I2C_FIFO_STATUS_RX_SHIFT 0 |
| 60 | #define I2C_INT_MASK 0x064 |
| 61 | #define I2C_INT_STATUS 0x068 |
| 62 | #define I2C_INT_PACKET_XFER_COMPLETE (1<<7) |
| 63 | #define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6) |
| 64 | #define I2C_INT_TX_FIFO_OVERFLOW (1<<5) |
| 65 | #define I2C_INT_RX_FIFO_UNDERFLOW (1<<4) |
| 66 | #define I2C_INT_NO_ACK (1<<3) |
| 67 | #define I2C_INT_ARBITRATION_LOST (1<<2) |
| 68 | #define I2C_INT_TX_FIFO_DATA_REQ (1<<1) |
| 69 | #define I2C_INT_RX_FIFO_DATA_REQ (1<<0) |
| 70 | #define I2C_CLK_DIVISOR 0x06c |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 71 | #define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16 |
| 72 | #define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 73 | |
| 74 | #define DVC_CTRL_REG1 0x000 |
| 75 | #define DVC_CTRL_REG1_INTR_EN (1<<10) |
| 76 | #define DVC_CTRL_REG2 0x004 |
| 77 | #define DVC_CTRL_REG3 0x008 |
| 78 | #define DVC_CTRL_REG3_SW_PROG (1<<26) |
| 79 | #define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30) |
| 80 | #define DVC_STATUS 0x00c |
| 81 | #define DVC_STATUS_I2C_DONE_INTR (1<<30) |
| 82 | |
| 83 | #define I2C_ERR_NONE 0x00 |
| 84 | #define I2C_ERR_NO_ACK 0x01 |
| 85 | #define I2C_ERR_ARBITRATION_LOST 0x02 |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 86 | #define I2C_ERR_UNKNOWN_INTERRUPT 0x04 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 87 | |
| 88 | #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28 |
| 89 | #define PACKET_HEADER0_PACKET_ID_SHIFT 16 |
| 90 | #define PACKET_HEADER0_CONT_ID_SHIFT 12 |
| 91 | #define PACKET_HEADER0_PROTOCOL_I2C (1<<4) |
| 92 | |
| 93 | #define I2C_HEADER_HIGHSPEED_MODE (1<<22) |
| 94 | #define I2C_HEADER_CONT_ON_NAK (1<<21) |
| 95 | #define I2C_HEADER_SEND_START_BYTE (1<<20) |
| 96 | #define I2C_HEADER_READ (1<<19) |
| 97 | #define I2C_HEADER_10BIT_ADDR (1<<18) |
| 98 | #define I2C_HEADER_IE_ENABLE (1<<17) |
| 99 | #define I2C_HEADER_REPEAT_START (1<<16) |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 100 | #define I2C_HEADER_CONTINUE_XFER (1<<15) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 101 | #define I2C_HEADER_MASTER_ADDR_SHIFT 12 |
| 102 | #define I2C_HEADER_SLAVE_ADDR_SHIFT 1 |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 103 | |
| 104 | #define I2C_CONFIG_LOAD 0x08C |
| 105 | #define I2C_MSTR_CONFIG_LOAD (1 << 0) |
| 106 | #define I2C_SLV_CONFIG_LOAD (1 << 1) |
| 107 | #define I2C_TIMEOUT_CONFIG_LOAD (1 << 2) |
| 108 | |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 109 | /* |
| 110 | * msg_end_type: The bus control which need to be send at end of transfer. |
| 111 | * @MSG_END_STOP: Send stop pulse at end of transfer. |
| 112 | * @MSG_END_REPEAT_START: Send repeat start at end of transfer. |
| 113 | * @MSG_END_CONTINUE: The following on message is coming and so do not send |
| 114 | * stop or repeat start. |
| 115 | */ |
| 116 | enum msg_end_type { |
| 117 | MSG_END_STOP, |
| 118 | MSG_END_REPEAT_START, |
| 119 | MSG_END_CONTINUE, |
| 120 | }; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 121 | |
| 122 | /** |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 123 | * struct tegra_i2c_hw_feature : Different HW support on Tegra |
| 124 | * @has_continue_xfer_support: Continue transfer supports. |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 125 | * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer |
| 126 | * complete interrupt per packet basis. |
| 127 | * @has_single_clk_source: The i2c controller has single clock source. Tegra30 |
| 128 | * and earlier Socs has two clock sources i.e. div-clk and |
| 129 | * fast-clk. |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 130 | * @has_config_load_reg: Has the config load register to load the new |
| 131 | * configuration. |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 132 | * @clk_divisor_hs_mode: Clock divisor in HS mode. |
| 133 | * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is |
| 134 | * applicable if there is no fast clock source i.e. single clock |
| 135 | * source. |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 136 | */ |
| 137 | |
| 138 | struct tegra_i2c_hw_feature { |
| 139 | bool has_continue_xfer_support; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 140 | bool has_per_pkt_xfer_complete_irq; |
| 141 | bool has_single_clk_source; |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 142 | bool has_config_load_reg; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 143 | int clk_divisor_hs_mode; |
| 144 | int clk_divisor_std_fast_mode; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 145 | u16 clk_divisor_fast_plus_mode; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /** |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 149 | * struct tegra_i2c_dev - per device i2c context |
| 150 | * @dev: device reference for power management |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 151 | * @hw: Tegra i2c hw feature. |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 152 | * @adapter: core i2c layer adapter information |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 153 | * @div_clk: clock reference for div clock of i2c controller. |
| 154 | * @fast_clk: clock reference for fast clock of i2c controller. |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 155 | * @base: ioremapped registers cookie |
| 156 | * @cont_id: i2c controller id, used for for packet header |
| 157 | * @irq: irq number of transfer complete interrupt |
| 158 | * @is_dvc: identifies the DVC i2c controller, has a different register layout |
| 159 | * @msg_complete: transfer completion notifier |
| 160 | * @msg_err: error code for completed message |
| 161 | * @msg_buf: pointer to current message data |
| 162 | * @msg_buf_remaining: size of unsent data in the message buffer |
| 163 | * @msg_read: identifies read transfers |
| 164 | * @bus_clk_rate: current i2c bus clock rate |
| 165 | * @is_suspended: prevents i2c controller accesses after suspend is called |
| 166 | */ |
| 167 | struct tegra_i2c_dev { |
| 168 | struct device *dev; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 169 | const struct tegra_i2c_hw_feature *hw; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 170 | struct i2c_adapter adapter; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 171 | struct clk *div_clk; |
| 172 | struct clk *fast_clk; |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 173 | struct reset_control *rst; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 174 | void __iomem *base; |
| 175 | int cont_id; |
| 176 | int irq; |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 177 | bool irq_disabled; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 178 | int is_dvc; |
| 179 | struct completion msg_complete; |
| 180 | int msg_err; |
| 181 | u8 *msg_buf; |
| 182 | size_t msg_buf_remaining; |
| 183 | int msg_read; |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 184 | u32 bus_clk_rate; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 185 | u16 clk_divisor_non_hs_mode; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 186 | bool is_suspended; |
| 187 | }; |
| 188 | |
| 189 | static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned long reg) |
| 190 | { |
| 191 | writel(val, i2c_dev->base + reg); |
| 192 | } |
| 193 | |
| 194 | static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg) |
| 195 | { |
| 196 | return readl(i2c_dev->base + reg); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * i2c_writel and i2c_readl will offset the register if necessary to talk |
| 201 | * to the I2C block inside the DVC block |
| 202 | */ |
| 203 | static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, |
| 204 | unsigned long reg) |
| 205 | { |
| 206 | if (i2c_dev->is_dvc) |
| 207 | reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40; |
| 208 | return reg; |
| 209 | } |
| 210 | |
| 211 | static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, |
| 212 | unsigned long reg) |
| 213 | { |
| 214 | writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
Laxman Dewangan | ec7aaca | 2012-06-13 15:42:36 +0530 | [diff] [blame] | 215 | |
| 216 | /* Read back register to make sure that register writes completed */ |
| 217 | if (reg != I2C_TX_FIFO) |
| 218 | readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg) |
| 222 | { |
| 223 | return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
| 224 | } |
| 225 | |
| 226 | static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data, |
| 227 | unsigned long reg, int len) |
| 228 | { |
| 229 | writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); |
| 230 | } |
| 231 | |
| 232 | static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data, |
| 233 | unsigned long reg, int len) |
| 234 | { |
| 235 | readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); |
| 236 | } |
| 237 | |
| 238 | static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) |
| 239 | { |
| 240 | u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK); |
| 241 | int_mask &= ~mask; |
| 242 | i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); |
| 243 | } |
| 244 | |
| 245 | static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) |
| 246 | { |
| 247 | u32 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK); |
| 248 | int_mask |= mask; |
| 249 | i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); |
| 250 | } |
| 251 | |
| 252 | static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev) |
| 253 | { |
| 254 | unsigned long timeout = jiffies + HZ; |
| 255 | u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL); |
| 256 | val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH; |
| 257 | i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL); |
| 258 | |
| 259 | while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) & |
| 260 | (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) { |
| 261 | if (time_after(jiffies, timeout)) { |
| 262 | dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n"); |
| 263 | return -ETIMEDOUT; |
| 264 | } |
| 265 | msleep(1); |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev) |
| 271 | { |
| 272 | u32 val; |
| 273 | int rx_fifo_avail; |
| 274 | u8 *buf = i2c_dev->msg_buf; |
| 275 | size_t buf_remaining = i2c_dev->msg_buf_remaining; |
| 276 | int words_to_transfer; |
| 277 | |
| 278 | val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); |
| 279 | rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >> |
| 280 | I2C_FIFO_STATUS_RX_SHIFT; |
| 281 | |
| 282 | /* Rounds down to not include partial word at the end of buf */ |
| 283 | words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; |
| 284 | if (words_to_transfer > rx_fifo_avail) |
| 285 | words_to_transfer = rx_fifo_avail; |
| 286 | |
| 287 | i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer); |
| 288 | |
| 289 | buf += words_to_transfer * BYTES_PER_FIFO_WORD; |
| 290 | buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; |
| 291 | rx_fifo_avail -= words_to_transfer; |
| 292 | |
| 293 | /* |
| 294 | * If there is a partial word at the end of buf, handle it manually to |
| 295 | * prevent overwriting past the end of buf |
| 296 | */ |
| 297 | if (rx_fifo_avail > 0 && buf_remaining > 0) { |
| 298 | BUG_ON(buf_remaining > 3); |
| 299 | val = i2c_readl(i2c_dev, I2C_RX_FIFO); |
Dmitry Osipenko | 8c340f6 | 2015-01-26 19:55:02 +0300 | [diff] [blame] | 300 | val = cpu_to_le32(val); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 301 | memcpy(buf, &val, buf_remaining); |
| 302 | buf_remaining = 0; |
| 303 | rx_fifo_avail--; |
| 304 | } |
| 305 | |
| 306 | BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0); |
| 307 | i2c_dev->msg_buf_remaining = buf_remaining; |
| 308 | i2c_dev->msg_buf = buf; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev) |
| 313 | { |
| 314 | u32 val; |
| 315 | int tx_fifo_avail; |
| 316 | u8 *buf = i2c_dev->msg_buf; |
| 317 | size_t buf_remaining = i2c_dev->msg_buf_remaining; |
| 318 | int words_to_transfer; |
| 319 | |
| 320 | val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); |
| 321 | tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >> |
| 322 | I2C_FIFO_STATUS_TX_SHIFT; |
| 323 | |
| 324 | /* Rounds down to not include partial word at the end of buf */ |
| 325 | words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 326 | |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 327 | /* It's very common to have < 4 bytes, so optimize that case. */ |
| 328 | if (words_to_transfer) { |
| 329 | if (words_to_transfer > tx_fifo_avail) |
| 330 | words_to_transfer = tx_fifo_avail; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 331 | |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 332 | /* |
| 333 | * Update state before writing to FIFO. If this casues us |
| 334 | * to finish writing all bytes (AKA buf_remaining goes to 0) we |
| 335 | * have a potential for an interrupt (PACKET_XFER_COMPLETE is |
| 336 | * not maskable). We need to make sure that the isr sees |
| 337 | * buf_remaining as 0 and doesn't call us back re-entrantly. |
| 338 | */ |
| 339 | buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; |
| 340 | tx_fifo_avail -= words_to_transfer; |
| 341 | i2c_dev->msg_buf_remaining = buf_remaining; |
| 342 | i2c_dev->msg_buf = buf + |
| 343 | words_to_transfer * BYTES_PER_FIFO_WORD; |
| 344 | barrier(); |
| 345 | |
| 346 | i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer); |
| 347 | |
| 348 | buf += words_to_transfer * BYTES_PER_FIFO_WORD; |
| 349 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * If there is a partial word at the end of buf, handle it manually to |
| 353 | * prevent reading past the end of buf, which could cross a page |
| 354 | * boundary and fault. |
| 355 | */ |
| 356 | if (tx_fifo_avail > 0 && buf_remaining > 0) { |
| 357 | BUG_ON(buf_remaining > 3); |
| 358 | memcpy(&val, buf, buf_remaining); |
Dmitry Osipenko | 8c340f6 | 2015-01-26 19:55:02 +0300 | [diff] [blame] | 359 | val = le32_to_cpu(val); |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 360 | |
| 361 | /* Again update before writing to FIFO to make sure isr sees. */ |
| 362 | i2c_dev->msg_buf_remaining = 0; |
| 363 | i2c_dev->msg_buf = NULL; |
| 364 | barrier(); |
| 365 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 366 | i2c_writel(i2c_dev, val, I2C_TX_FIFO); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller) |
| 374 | * block. This block is identical to the rest of the I2C blocks, except that |
| 375 | * it only supports master mode, it has registers moved around, and it needs |
| 376 | * some extra init to get it into I2C mode. The register moves are handled |
| 377 | * by i2c_readl and i2c_writel |
| 378 | */ |
| 379 | static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev) |
| 380 | { |
| 381 | u32 val = 0; |
| 382 | val = dvc_readl(i2c_dev, DVC_CTRL_REG3); |
| 383 | val |= DVC_CTRL_REG3_SW_PROG; |
| 384 | val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN; |
| 385 | dvc_writel(i2c_dev, val, DVC_CTRL_REG3); |
| 386 | |
| 387 | val = dvc_readl(i2c_dev, DVC_CTRL_REG1); |
| 388 | val |= DVC_CTRL_REG1_INTR_EN; |
| 389 | dvc_writel(i2c_dev, val, DVC_CTRL_REG1); |
| 390 | } |
| 391 | |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 392 | static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev) |
| 393 | { |
| 394 | int ret; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 395 | if (!i2c_dev->hw->has_single_clk_source) { |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 396 | ret = clk_enable(i2c_dev->fast_clk); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 397 | if (ret < 0) { |
| 398 | dev_err(i2c_dev->dev, |
| 399 | "Enabling fast clk failed, err %d\n", ret); |
| 400 | return ret; |
| 401 | } |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 402 | } |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 403 | ret = clk_enable(i2c_dev->div_clk); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 404 | if (ret < 0) { |
| 405 | dev_err(i2c_dev->dev, |
| 406 | "Enabling div clk failed, err %d\n", ret); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 407 | clk_disable(i2c_dev->fast_clk); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 408 | } |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev) |
| 413 | { |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 414 | clk_disable(i2c_dev->div_clk); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 415 | if (!i2c_dev->hw->has_single_clk_source) |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 416 | clk_disable(i2c_dev->fast_clk); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 417 | } |
| 418 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 419 | static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev) |
| 420 | { |
| 421 | u32 val; |
| 422 | int err = 0; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 423 | u32 clk_divisor; |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 424 | unsigned long timeout = jiffies + HZ; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 425 | |
Laxman Dewangan | 132c803 | 2013-03-15 05:34:08 +0000 | [diff] [blame] | 426 | err = tegra_i2c_clock_enable(i2c_dev); |
| 427 | if (err < 0) { |
| 428 | dev_err(i2c_dev->dev, "Clock enable failed %d\n", err); |
| 429 | return err; |
| 430 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 431 | |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 432 | reset_control_assert(i2c_dev->rst); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 433 | udelay(2); |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 434 | reset_control_deassert(i2c_dev->rst); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 435 | |
| 436 | if (i2c_dev->is_dvc) |
| 437 | tegra_dvc_init(i2c_dev); |
| 438 | |
Jay Cheng | 40abcf7 | 2011-04-25 15:32:27 -0600 | [diff] [blame] | 439 | val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN | |
| 440 | (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 441 | i2c_writel(i2c_dev, val, I2C_CNFG); |
| 442 | i2c_writel(i2c_dev, 0, I2C_INT_MASK); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 443 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 444 | /* Make sure clock divisor programmed correctly */ |
| 445 | clk_divisor = i2c_dev->hw->clk_divisor_hs_mode; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 446 | clk_divisor |= i2c_dev->clk_divisor_non_hs_mode << |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 447 | I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT; |
| 448 | i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 449 | |
Kenneth Waters | 65a1a0a | 2011-04-25 12:29:54 -0600 | [diff] [blame] | 450 | if (!i2c_dev->is_dvc) { |
| 451 | u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG); |
Stephen Warren | 5afa9d3 | 2011-06-06 11:25:19 -0600 | [diff] [blame] | 452 | sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL; |
| 453 | i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG); |
| 454 | i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1); |
| 455 | i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2); |
| 456 | |
Kenneth Waters | 65a1a0a | 2011-04-25 12:29:54 -0600 | [diff] [blame] | 457 | } |
| 458 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 459 | val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT | |
| 460 | 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT; |
| 461 | i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL); |
| 462 | |
| 463 | if (tegra_i2c_flush_fifos(i2c_dev)) |
| 464 | err = -ETIMEDOUT; |
| 465 | |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 466 | if (i2c_dev->hw->has_config_load_reg) { |
| 467 | i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD); |
| 468 | while (i2c_readl(i2c_dev, I2C_CONFIG_LOAD) != 0) { |
| 469 | if (time_after(jiffies, timeout)) { |
| 470 | dev_warn(i2c_dev->dev, |
| 471 | "timeout waiting for config load\n"); |
| 472 | return -ETIMEDOUT; |
| 473 | } |
| 474 | msleep(1); |
| 475 | } |
| 476 | } |
| 477 | |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 478 | tegra_i2c_clock_disable(i2c_dev); |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 479 | |
| 480 | if (i2c_dev->irq_disabled) { |
| 481 | i2c_dev->irq_disabled = 0; |
| 482 | enable_irq(i2c_dev->irq); |
| 483 | } |
| 484 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 485 | return err; |
| 486 | } |
| 487 | |
| 488 | static irqreturn_t tegra_i2c_isr(int irq, void *dev_id) |
| 489 | { |
| 490 | u32 status; |
| 491 | const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; |
| 492 | struct tegra_i2c_dev *i2c_dev = dev_id; |
| 493 | |
| 494 | status = i2c_readl(i2c_dev, I2C_INT_STATUS); |
| 495 | |
| 496 | if (status == 0) { |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 497 | dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n", |
| 498 | i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS), |
| 499 | i2c_readl(i2c_dev, I2C_STATUS), |
| 500 | i2c_readl(i2c_dev, I2C_CNFG)); |
| 501 | i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; |
| 502 | |
| 503 | if (!i2c_dev->irq_disabled) { |
| 504 | disable_irq_nosync(i2c_dev->irq); |
| 505 | i2c_dev->irq_disabled = 1; |
| 506 | } |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 507 | goto err; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | if (unlikely(status & status_err)) { |
| 511 | if (status & I2C_INT_NO_ACK) |
| 512 | i2c_dev->msg_err |= I2C_ERR_NO_ACK; |
| 513 | if (status & I2C_INT_ARBITRATION_LOST) |
| 514 | i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 515 | goto err; |
| 516 | } |
| 517 | |
| 518 | if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) { |
| 519 | if (i2c_dev->msg_buf_remaining) |
| 520 | tegra_i2c_empty_rx_fifo(i2c_dev); |
| 521 | else |
| 522 | BUG(); |
| 523 | } |
| 524 | |
| 525 | if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) { |
| 526 | if (i2c_dev->msg_buf_remaining) |
| 527 | tegra_i2c_fill_tx_fifo(i2c_dev); |
| 528 | else |
| 529 | tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ); |
| 530 | } |
| 531 | |
Laxman Dewangan | c889e91 | 2012-05-07 12:16:19 +0530 | [diff] [blame] | 532 | i2c_writel(i2c_dev, status, I2C_INT_STATUS); |
| 533 | if (i2c_dev->is_dvc) |
| 534 | dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); |
| 535 | |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 536 | if (status & I2C_INT_PACKET_XFER_COMPLETE) { |
| 537 | BUG_ON(i2c_dev->msg_buf_remaining); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 538 | complete(&i2c_dev->msg_complete); |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 539 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 540 | return IRQ_HANDLED; |
| 541 | err: |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 542 | /* An error occurred, mask all interrupts */ |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 543 | tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST | |
| 544 | I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ | |
| 545 | I2C_INT_RX_FIFO_DATA_REQ); |
| 546 | i2c_writel(i2c_dev, status, I2C_INT_STATUS); |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 547 | if (i2c_dev->is_dvc) |
| 548 | dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); |
Laxman Dewangan | c889e91 | 2012-05-07 12:16:19 +0530 | [diff] [blame] | 549 | |
| 550 | complete(&i2c_dev->msg_complete); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 551 | return IRQ_HANDLED; |
| 552 | } |
| 553 | |
| 554 | static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev, |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 555 | struct i2c_msg *msg, enum msg_end_type end_state) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 556 | { |
| 557 | u32 packet_header; |
| 558 | u32 int_mask; |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 559 | unsigned long time_left; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 560 | |
| 561 | tegra_i2c_flush_fifos(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 562 | |
| 563 | if (msg->len == 0) |
| 564 | return -EINVAL; |
| 565 | |
| 566 | i2c_dev->msg_buf = msg->buf; |
| 567 | i2c_dev->msg_buf_remaining = msg->len; |
| 568 | i2c_dev->msg_err = I2C_ERR_NONE; |
| 569 | i2c_dev->msg_read = (msg->flags & I2C_M_RD); |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 570 | reinit_completion(&i2c_dev->msg_complete); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 571 | |
| 572 | packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) | |
| 573 | PACKET_HEADER0_PROTOCOL_I2C | |
| 574 | (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) | |
| 575 | (1 << PACKET_HEADER0_PACKET_ID_SHIFT); |
| 576 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
| 577 | |
| 578 | packet_header = msg->len - 1; |
| 579 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
| 580 | |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 581 | packet_header = I2C_HEADER_IE_ENABLE; |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 582 | if (end_state == MSG_END_CONTINUE) |
| 583 | packet_header |= I2C_HEADER_CONTINUE_XFER; |
| 584 | else if (end_state == MSG_END_REPEAT_START) |
Erik Gilling | 2078cf3 | 2011-04-25 15:32:26 -0600 | [diff] [blame] | 585 | packet_header |= I2C_HEADER_REPEAT_START; |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 586 | if (msg->flags & I2C_M_TEN) { |
| 587 | packet_header |= msg->addr; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 588 | packet_header |= I2C_HEADER_10BIT_ADDR; |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 589 | } else { |
| 590 | packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT; |
| 591 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 592 | if (msg->flags & I2C_M_IGNORE_NAK) |
| 593 | packet_header |= I2C_HEADER_CONT_ON_NAK; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 594 | if (msg->flags & I2C_M_RD) |
| 595 | packet_header |= I2C_HEADER_READ; |
| 596 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
| 597 | |
| 598 | if (!(msg->flags & I2C_M_RD)) |
| 599 | tegra_i2c_fill_tx_fifo(i2c_dev); |
| 600 | |
| 601 | int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 602 | if (i2c_dev->hw->has_per_pkt_xfer_complete_irq) |
| 603 | int_mask |= I2C_INT_PACKET_XFER_COMPLETE; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 604 | if (msg->flags & I2C_M_RD) |
| 605 | int_mask |= I2C_INT_RX_FIFO_DATA_REQ; |
| 606 | else if (i2c_dev->msg_buf_remaining) |
| 607 | int_mask |= I2C_INT_TX_FIFO_DATA_REQ; |
| 608 | tegra_i2c_unmask_irq(i2c_dev, int_mask); |
| 609 | dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n", |
| 610 | i2c_readl(i2c_dev, I2C_INT_MASK)); |
| 611 | |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 612 | time_left = wait_for_completion_timeout(&i2c_dev->msg_complete, |
| 613 | TEGRA_I2C_TIMEOUT); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 614 | tegra_i2c_mask_irq(i2c_dev, int_mask); |
| 615 | |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 616 | if (time_left == 0) { |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 617 | dev_err(i2c_dev->dev, "i2c transfer timed out\n"); |
| 618 | |
| 619 | tegra_i2c_init(i2c_dev); |
| 620 | return -ETIMEDOUT; |
| 621 | } |
| 622 | |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 623 | dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n", |
| 624 | time_left, completion_done(&i2c_dev->msg_complete), |
| 625 | i2c_dev->msg_err); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 626 | |
| 627 | if (likely(i2c_dev->msg_err == I2C_ERR_NONE)) |
| 628 | return 0; |
| 629 | |
Alok Chauhan | f70893d0 | 2012-04-02 11:23:02 +0530 | [diff] [blame] | 630 | /* |
| 631 | * NACK interrupt is generated before the I2C controller generates the |
| 632 | * STOP condition on the bus. So wait for 2 clock periods before resetting |
| 633 | * the controller so that STOP condition has been delivered properly. |
| 634 | */ |
| 635 | if (i2c_dev->msg_err == I2C_ERR_NO_ACK) |
| 636 | udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate)); |
| 637 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 638 | tegra_i2c_init(i2c_dev); |
| 639 | if (i2c_dev->msg_err == I2C_ERR_NO_ACK) { |
| 640 | if (msg->flags & I2C_M_IGNORE_NAK) |
| 641 | return 0; |
| 642 | return -EREMOTEIO; |
| 643 | } |
| 644 | |
| 645 | return -EIO; |
| 646 | } |
| 647 | |
| 648 | static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], |
| 649 | int num) |
| 650 | { |
| 651 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
| 652 | int i; |
| 653 | int ret = 0; |
| 654 | |
| 655 | if (i2c_dev->is_suspended) |
| 656 | return -EBUSY; |
| 657 | |
Laxman Dewangan | 132c803 | 2013-03-15 05:34:08 +0000 | [diff] [blame] | 658 | ret = tegra_i2c_clock_enable(i2c_dev); |
| 659 | if (ret < 0) { |
| 660 | dev_err(i2c_dev->dev, "Clock enable failed %d\n", ret); |
| 661 | return ret; |
| 662 | } |
| 663 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 664 | for (i = 0; i < num; i++) { |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 665 | enum msg_end_type end_type = MSG_END_STOP; |
| 666 | if (i < (num - 1)) { |
| 667 | if (msgs[i + 1].flags & I2C_M_NOSTART) |
| 668 | end_type = MSG_END_CONTINUE; |
| 669 | else |
| 670 | end_type = MSG_END_REPEAT_START; |
| 671 | } |
| 672 | ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 673 | if (ret) |
| 674 | break; |
| 675 | } |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 676 | tegra_i2c_clock_disable(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 677 | return ret ?: i; |
| 678 | } |
| 679 | |
| 680 | static u32 tegra_i2c_func(struct i2c_adapter *adap) |
| 681 | { |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 682 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
Wolfram Sang | 4bb28e3 | 2015-06-16 19:47:21 +0200 | [diff] [blame] | 683 | u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | |
| 684 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 685 | |
| 686 | if (i2c_dev->hw->has_continue_xfer_support) |
| 687 | ret |= I2C_FUNC_NOSTART; |
| 688 | return ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static const struct i2c_algorithm tegra_i2c_algo = { |
| 692 | .master_xfer = tegra_i2c_xfer, |
| 693 | .functionality = tegra_i2c_func, |
| 694 | }; |
| 695 | |
Wolfram Sang | 3aaa34b | 2015-06-16 19:57:29 +0200 | [diff] [blame] | 696 | /* payload size is only 12 bit */ |
| 697 | static struct i2c_adapter_quirks tegra_i2c_quirks = { |
| 698 | .max_read_len = 4096, |
| 699 | .max_write_len = 4096, |
| 700 | }; |
| 701 | |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 702 | static const struct tegra_i2c_hw_feature tegra20_i2c_hw = { |
| 703 | .has_continue_xfer_support = false, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 704 | .has_per_pkt_xfer_complete_irq = false, |
| 705 | .has_single_clk_source = false, |
| 706 | .clk_divisor_hs_mode = 3, |
| 707 | .clk_divisor_std_fast_mode = 0, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 708 | .clk_divisor_fast_plus_mode = 0, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 709 | .has_config_load_reg = false, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | static const struct tegra_i2c_hw_feature tegra30_i2c_hw = { |
| 713 | .has_continue_xfer_support = true, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 714 | .has_per_pkt_xfer_complete_irq = false, |
| 715 | .has_single_clk_source = false, |
| 716 | .clk_divisor_hs_mode = 3, |
| 717 | .clk_divisor_std_fast_mode = 0, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 718 | .clk_divisor_fast_plus_mode = 0, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 719 | .has_config_load_reg = false, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | static const struct tegra_i2c_hw_feature tegra114_i2c_hw = { |
| 723 | .has_continue_xfer_support = true, |
| 724 | .has_per_pkt_xfer_complete_irq = true, |
| 725 | .has_single_clk_source = true, |
| 726 | .clk_divisor_hs_mode = 1, |
| 727 | .clk_divisor_std_fast_mode = 0x19, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 728 | .clk_divisor_fast_plus_mode = 0x10, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 729 | .has_config_load_reg = false, |
| 730 | }; |
| 731 | |
| 732 | static const struct tegra_i2c_hw_feature tegra124_i2c_hw = { |
| 733 | .has_continue_xfer_support = true, |
| 734 | .has_per_pkt_xfer_complete_irq = true, |
| 735 | .has_single_clk_source = true, |
| 736 | .clk_divisor_hs_mode = 1, |
| 737 | .clk_divisor_std_fast_mode = 0x19, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 738 | .clk_divisor_fast_plus_mode = 0x10, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 739 | .has_config_load_reg = true, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 740 | }; |
| 741 | |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 742 | /* Match table for of_platform binding */ |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 743 | static const struct of_device_id tegra_i2c_of_match[] = { |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 744 | { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, }, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 745 | { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, }, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 746 | { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, }, |
| 747 | { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, }, |
| 748 | { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, }, |
| 749 | {}, |
| 750 | }; |
| 751 | MODULE_DEVICE_TABLE(of, tegra_i2c_of_match); |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 752 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 753 | static int tegra_i2c_probe(struct platform_device *pdev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 754 | { |
| 755 | struct tegra_i2c_dev *i2c_dev; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 756 | struct resource *res; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 757 | struct clk *div_clk; |
| 758 | struct clk *fast_clk; |
Olof Johansson | f533c61 | 2011-10-12 17:33:00 -0700 | [diff] [blame] | 759 | void __iomem *base; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 760 | int irq; |
| 761 | int ret = 0; |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 762 | int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 763 | |
| 764 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 765 | base = devm_ioremap_resource(&pdev->dev, res); |
| 766 | if (IS_ERR(base)) |
| 767 | return PTR_ERR(base); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 768 | |
| 769 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 770 | if (!res) { |
| 771 | dev_err(&pdev->dev, "no irq resource\n"); |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 772 | return -EINVAL; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 773 | } |
| 774 | irq = res->start; |
| 775 | |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 776 | div_clk = devm_clk_get(&pdev->dev, "div-clk"); |
| 777 | if (IS_ERR(div_clk)) { |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 778 | dev_err(&pdev->dev, "missing controller clock"); |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 779 | return PTR_ERR(div_clk); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 782 | i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 783 | if (!i2c_dev) |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 784 | return -ENOMEM; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 785 | |
| 786 | i2c_dev->base = base; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 787 | i2c_dev->div_clk = div_clk; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 788 | i2c_dev->adapter.algo = &tegra_i2c_algo; |
Wolfram Sang | 3aaa34b | 2015-06-16 19:57:29 +0200 | [diff] [blame] | 789 | i2c_dev->adapter.quirks = &tegra_i2c_quirks; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 790 | i2c_dev->irq = irq; |
| 791 | i2c_dev->cont_id = pdev->id; |
| 792 | i2c_dev->dev = &pdev->dev; |
John Bonesio | 5c470f3 | 2011-06-22 09:16:56 -0700 | [diff] [blame] | 793 | |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 794 | i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c"); |
| 795 | if (IS_ERR(i2c_dev->rst)) { |
| 796 | dev_err(&pdev->dev, "missing controller reset"); |
| 797 | return PTR_ERR(i2c_dev->rst); |
| 798 | } |
| 799 | |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 800 | ret = of_property_read_u32(i2c_dev->dev->of_node, "clock-frequency", |
| 801 | &i2c_dev->bus_clk_rate); |
| 802 | if (ret) |
| 803 | i2c_dev->bus_clk_rate = 100000; /* default clock rate */ |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 804 | |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 805 | i2c_dev->hw = &tegra20_i2c_hw; |
| 806 | |
| 807 | if (pdev->dev.of_node) { |
| 808 | const struct of_device_id *match; |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 809 | match = of_match_device(tegra_i2c_of_match, &pdev->dev); |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 810 | i2c_dev->hw = match->data; |
Stephen Warren | 68fb669 | 2011-12-17 23:29:30 -0700 | [diff] [blame] | 811 | i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node, |
| 812 | "nvidia,tegra20-i2c-dvc"); |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 813 | } else if (pdev->id == 3) { |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 814 | i2c_dev->is_dvc = 1; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 815 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 816 | init_completion(&i2c_dev->msg_complete); |
| 817 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 818 | if (!i2c_dev->hw->has_single_clk_source) { |
| 819 | fast_clk = devm_clk_get(&pdev->dev, "fast-clk"); |
| 820 | if (IS_ERR(fast_clk)) { |
| 821 | dev_err(&pdev->dev, "missing fast clock"); |
| 822 | return PTR_ERR(fast_clk); |
| 823 | } |
| 824 | i2c_dev->fast_clk = fast_clk; |
| 825 | } |
| 826 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 827 | platform_set_drvdata(pdev, i2c_dev); |
| 828 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 829 | if (!i2c_dev->hw->has_single_clk_source) { |
| 830 | ret = clk_prepare(i2c_dev->fast_clk); |
| 831 | if (ret < 0) { |
| 832 | dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret); |
| 833 | return ret; |
| 834 | } |
| 835 | } |
| 836 | |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame^] | 837 | i2c_dev->clk_divisor_non_hs_mode = |
| 838 | i2c_dev->hw->clk_divisor_std_fast_mode; |
| 839 | if (i2c_dev->hw->clk_divisor_fast_plus_mode && |
| 840 | (i2c_dev->bus_clk_rate == 1000000)) |
| 841 | i2c_dev->clk_divisor_non_hs_mode = |
| 842 | i2c_dev->hw->clk_divisor_fast_plus_mode; |
| 843 | |
| 844 | clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 845 | ret = clk_set_rate(i2c_dev->div_clk, |
| 846 | i2c_dev->bus_clk_rate * clk_multiplier); |
| 847 | if (ret) { |
| 848 | dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret); |
| 849 | goto unprepare_fast_clk; |
| 850 | } |
| 851 | |
| 852 | ret = clk_prepare(i2c_dev->div_clk); |
| 853 | if (ret < 0) { |
| 854 | dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret); |
| 855 | goto unprepare_fast_clk; |
| 856 | } |
| 857 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 858 | ret = tegra_i2c_init(i2c_dev); |
| 859 | if (ret) { |
| 860 | dev_err(&pdev->dev, "Failed to initialize i2c controller"); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 861 | goto unprepare_div_clk; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 864 | ret = devm_request_irq(&pdev->dev, i2c_dev->irq, |
Laxman Dewangan | 91b370a | 2012-11-01 22:08:14 +0530 | [diff] [blame] | 865 | tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 866 | if (ret) { |
| 867 | dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 868 | goto unprepare_div_clk; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 871 | i2c_set_adapdata(&i2c_dev->adapter, i2c_dev); |
| 872 | i2c_dev->adapter.owner = THIS_MODULE; |
Wolfram Sang | 6025189 | 2014-07-10 13:46:35 +0200 | [diff] [blame] | 873 | i2c_dev->adapter.class = I2C_CLASS_DEPRECATED; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 874 | strlcpy(i2c_dev->adapter.name, "Tegra I2C adapter", |
| 875 | sizeof(i2c_dev->adapter.name)); |
| 876 | i2c_dev->adapter.algo = &tegra_i2c_algo; |
| 877 | i2c_dev->adapter.dev.parent = &pdev->dev; |
| 878 | i2c_dev->adapter.nr = pdev->id; |
John Bonesio | 5c470f3 | 2011-06-22 09:16:56 -0700 | [diff] [blame] | 879 | i2c_dev->adapter.dev.of_node = pdev->dev.of_node; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 880 | |
| 881 | ret = i2c_add_numbered_adapter(&i2c_dev->adapter); |
| 882 | if (ret) { |
| 883 | dev_err(&pdev->dev, "Failed to add I2C adapter\n"); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 884 | goto unprepare_div_clk; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 887 | return 0; |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 888 | |
| 889 | unprepare_div_clk: |
| 890 | clk_unprepare(i2c_dev->div_clk); |
| 891 | |
| 892 | unprepare_fast_clk: |
| 893 | if (!i2c_dev->hw->has_single_clk_source) |
| 894 | clk_unprepare(i2c_dev->fast_clk); |
| 895 | |
| 896 | return ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 899 | static int tegra_i2c_remove(struct platform_device *pdev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 900 | { |
| 901 | struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
| 902 | i2c_del_adapter(&i2c_dev->adapter); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 903 | |
| 904 | clk_unprepare(i2c_dev->div_clk); |
| 905 | if (!i2c_dev->hw->has_single_clk_source) |
| 906 | clk_unprepare(i2c_dev->fast_clk); |
| 907 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | |
Laxman Dewangan | 371e67c | 2012-08-18 17:49:58 +0530 | [diff] [blame] | 911 | #ifdef CONFIG_PM_SLEEP |
Wolfram Sang | 5db20c4 | 2012-07-24 17:32:45 +0200 | [diff] [blame] | 912 | static int tegra_i2c_suspend(struct device *dev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 913 | { |
Rafael J. Wysocki | 6a7b3c3 | 2012-07-11 21:27:30 +0200 | [diff] [blame] | 914 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 915 | |
| 916 | i2c_lock_adapter(&i2c_dev->adapter); |
| 917 | i2c_dev->is_suspended = true; |
| 918 | i2c_unlock_adapter(&i2c_dev->adapter); |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
Wolfram Sang | 5db20c4 | 2012-07-24 17:32:45 +0200 | [diff] [blame] | 923 | static int tegra_i2c_resume(struct device *dev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 924 | { |
Rafael J. Wysocki | 6a7b3c3 | 2012-07-11 21:27:30 +0200 | [diff] [blame] | 925 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 926 | int ret; |
| 927 | |
| 928 | i2c_lock_adapter(&i2c_dev->adapter); |
| 929 | |
| 930 | ret = tegra_i2c_init(i2c_dev); |
| 931 | |
| 932 | if (ret) { |
| 933 | i2c_unlock_adapter(&i2c_dev->adapter); |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | i2c_dev->is_suspended = false; |
| 938 | |
| 939 | i2c_unlock_adapter(&i2c_dev->adapter); |
| 940 | |
| 941 | return 0; |
| 942 | } |
Rafael J. Wysocki | 6a7b3c3 | 2012-07-11 21:27:30 +0200 | [diff] [blame] | 943 | |
Wolfram Sang | 5db20c4 | 2012-07-24 17:32:45 +0200 | [diff] [blame] | 944 | static SIMPLE_DEV_PM_OPS(tegra_i2c_pm, tegra_i2c_suspend, tegra_i2c_resume); |
Rafael J. Wysocki | 6a7b3c3 | 2012-07-11 21:27:30 +0200 | [diff] [blame] | 945 | #define TEGRA_I2C_PM (&tegra_i2c_pm) |
| 946 | #else |
| 947 | #define TEGRA_I2C_PM NULL |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 948 | #endif |
| 949 | |
| 950 | static struct platform_driver tegra_i2c_driver = { |
| 951 | .probe = tegra_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 952 | .remove = tegra_i2c_remove, |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 953 | .driver = { |
| 954 | .name = "tegra-i2c", |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 955 | .of_match_table = tegra_i2c_of_match, |
Rafael J. Wysocki | 6a7b3c3 | 2012-07-11 21:27:30 +0200 | [diff] [blame] | 956 | .pm = TEGRA_I2C_PM, |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 957 | }, |
| 958 | }; |
| 959 | |
| 960 | static int __init tegra_i2c_init_driver(void) |
| 961 | { |
| 962 | return platform_driver_register(&tegra_i2c_driver); |
| 963 | } |
| 964 | |
| 965 | static void __exit tegra_i2c_exit_driver(void) |
| 966 | { |
| 967 | platform_driver_unregister(&tegra_i2c_driver); |
| 968 | } |
| 969 | |
| 970 | subsys_initcall(tegra_i2c_init_driver); |
| 971 | module_exit(tegra_i2c_exit_driver); |
| 972 | |
| 973 | MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver"); |
| 974 | MODULE_AUTHOR("Colin Cross"); |
| 975 | MODULE_LICENSE("GPL v2"); |