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