Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2009-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | /* |
| 14 | * QUP driver for Qualcomm MSM platforms |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | /* #define DEBUG */ |
| 19 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 21 | #include <linux/clk.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/timer.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <mach/board.h> |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 33 | #include <mach/gpiomux.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | #include <linux/pm_runtime.h> |
| 36 | #include <linux/gpio.h> |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 37 | #include <linux/of.h> |
| 38 | #include <linux/of_i2c.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | |
| 40 | MODULE_LICENSE("GPL v2"); |
| 41 | MODULE_VERSION("0.2"); |
| 42 | MODULE_ALIAS("platform:i2c_qup"); |
| 43 | |
| 44 | /* QUP Registers */ |
| 45 | enum { |
| 46 | QUP_CONFIG = 0x0, |
| 47 | QUP_STATE = 0x4, |
| 48 | QUP_IO_MODE = 0x8, |
| 49 | QUP_SW_RESET = 0xC, |
| 50 | QUP_OPERATIONAL = 0x18, |
| 51 | QUP_ERROR_FLAGS = 0x1C, |
| 52 | QUP_ERROR_FLAGS_EN = 0x20, |
| 53 | QUP_MX_READ_CNT = 0x208, |
| 54 | QUP_MX_INPUT_CNT = 0x200, |
| 55 | QUP_MX_WR_CNT = 0x100, |
| 56 | QUP_OUT_DEBUG = 0x108, |
| 57 | QUP_OUT_FIFO_CNT = 0x10C, |
| 58 | QUP_OUT_FIFO_BASE = 0x110, |
| 59 | QUP_IN_READ_CUR = 0x20C, |
| 60 | QUP_IN_DEBUG = 0x210, |
| 61 | QUP_IN_FIFO_CNT = 0x214, |
| 62 | QUP_IN_FIFO_BASE = 0x218, |
| 63 | QUP_I2C_CLK_CTL = 0x400, |
| 64 | QUP_I2C_STATUS = 0x404, |
| 65 | }; |
| 66 | |
| 67 | /* QUP States and reset values */ |
| 68 | enum { |
| 69 | QUP_RESET_STATE = 0, |
| 70 | QUP_RUN_STATE = 1U, |
| 71 | QUP_STATE_MASK = 3U, |
| 72 | QUP_PAUSE_STATE = 3U, |
| 73 | QUP_STATE_VALID = 1U << 2, |
| 74 | QUP_I2C_MAST_GEN = 1U << 4, |
| 75 | QUP_OPERATIONAL_RESET = 0xFF0, |
| 76 | QUP_I2C_STATUS_RESET = 0xFFFFFC, |
| 77 | }; |
| 78 | |
| 79 | /* QUP OPERATIONAL FLAGS */ |
| 80 | enum { |
| 81 | QUP_OUT_SVC_FLAG = 1U << 8, |
| 82 | QUP_IN_SVC_FLAG = 1U << 9, |
| 83 | QUP_MX_INPUT_DONE = 1U << 11, |
| 84 | }; |
| 85 | |
| 86 | /* I2C mini core related values */ |
| 87 | enum { |
| 88 | I2C_MINI_CORE = 2U << 8, |
| 89 | I2C_N_VAL = 0xF, |
| 90 | |
| 91 | }; |
| 92 | |
| 93 | /* Packing Unpacking words in FIFOs , and IO modes*/ |
| 94 | enum { |
| 95 | QUP_WR_BLK_MODE = 1U << 10, |
| 96 | QUP_RD_BLK_MODE = 1U << 12, |
| 97 | QUP_UNPACK_EN = 1U << 14, |
| 98 | QUP_PACK_EN = 1U << 15, |
| 99 | }; |
| 100 | |
| 101 | /* QUP tags */ |
| 102 | enum { |
| 103 | QUP_OUT_NOP = 0, |
| 104 | QUP_OUT_START = 1U << 8, |
| 105 | QUP_OUT_DATA = 2U << 8, |
| 106 | QUP_OUT_STOP = 3U << 8, |
| 107 | QUP_OUT_REC = 4U << 8, |
| 108 | QUP_IN_DATA = 5U << 8, |
| 109 | QUP_IN_STOP = 6U << 8, |
| 110 | QUP_IN_NACK = 7U << 8, |
| 111 | }; |
| 112 | |
| 113 | /* Status, Error flags */ |
| 114 | enum { |
| 115 | I2C_STATUS_WR_BUFFER_FULL = 1U << 0, |
| 116 | I2C_STATUS_BUS_ACTIVE = 1U << 8, |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 117 | I2C_STATUS_BUS_MASTER = 1U << 9, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | I2C_STATUS_ERROR_MASK = 0x38000FC, |
| 119 | QUP_I2C_NACK_FLAG = 1U << 3, |
| 120 | QUP_IN_NOT_EMPTY = 1U << 5, |
| 121 | QUP_STATUS_ERROR_FLAGS = 0x7C, |
| 122 | }; |
| 123 | |
| 124 | /* Master status clock states */ |
| 125 | enum { |
| 126 | I2C_CLK_RESET_BUSIDLE_STATE = 0, |
| 127 | I2C_CLK_FORCED_LOW_STATE = 5, |
| 128 | }; |
| 129 | |
| 130 | #define QUP_MAX_CLK_STATE_RETRIES 300 |
| 131 | |
| 132 | static char const * const i2c_rsrcs[] = {"i2c_clk", "i2c_sda"}; |
| 133 | |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 134 | static struct gpiomux_setting recovery_config = { |
| 135 | .func = GPIOMUX_FUNC_GPIO, |
| 136 | .drv = GPIOMUX_DRV_8MA, |
| 137 | .pull = GPIOMUX_PULL_NONE, |
| 138 | }; |
| 139 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | struct qup_i2c_dev { |
| 141 | struct device *dev; |
| 142 | void __iomem *base; /* virtual */ |
| 143 | void __iomem *gsbi; /* virtual */ |
| 144 | int in_irq; |
| 145 | int out_irq; |
| 146 | int err_irq; |
| 147 | int num_irqs; |
| 148 | struct clk *clk; |
| 149 | struct clk *pclk; |
| 150 | struct i2c_adapter adapter; |
| 151 | |
| 152 | struct i2c_msg *msg; |
| 153 | int pos; |
| 154 | int cnt; |
| 155 | int err; |
| 156 | int mode; |
| 157 | int clk_ctl; |
| 158 | int one_bit_t; |
| 159 | int out_fifo_sz; |
| 160 | int in_fifo_sz; |
| 161 | int out_blk_sz; |
| 162 | int in_blk_sz; |
| 163 | int wr_sz; |
| 164 | struct msm_i2c_platform_data *pdata; |
| 165 | int suspended; |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 166 | int pwr_state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | struct mutex mlock; |
| 168 | void *complete; |
| 169 | int i2c_gpios[ARRAY_SIZE(i2c_rsrcs)]; |
| 170 | }; |
| 171 | |
| 172 | #ifdef DEBUG |
| 173 | static void |
| 174 | qup_print_status(struct qup_i2c_dev *dev) |
| 175 | { |
| 176 | uint32_t val; |
| 177 | val = readl_relaxed(dev->base+QUP_CONFIG); |
| 178 | dev_dbg(dev->dev, "Qup config is :0x%x\n", val); |
| 179 | val = readl_relaxed(dev->base+QUP_STATE); |
| 180 | dev_dbg(dev->dev, "Qup state is :0x%x\n", val); |
| 181 | val = readl_relaxed(dev->base+QUP_IO_MODE); |
| 182 | dev_dbg(dev->dev, "Qup mode is :0x%x\n", val); |
| 183 | } |
| 184 | #else |
| 185 | static inline void qup_print_status(struct qup_i2c_dev *dev) |
| 186 | { |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | static irqreturn_t |
| 191 | qup_i2c_interrupt(int irq, void *devid) |
| 192 | { |
| 193 | struct qup_i2c_dev *dev = devid; |
Alok Chauhan | 809e064 | 2013-02-21 20:14:54 +0530 | [diff] [blame^] | 194 | uint32_t status = 0; |
| 195 | uint32_t status1 = 0; |
| 196 | uint32_t op_flgs = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | int err = 0; |
| 198 | |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 199 | if (pm_runtime_suspended(dev->dev)) |
| 200 | return IRQ_NONE; |
| 201 | |
Alok Chauhan | 809e064 | 2013-02-21 20:14:54 +0530 | [diff] [blame^] | 202 | status = readl_relaxed(dev->base + QUP_I2C_STATUS); |
| 203 | status1 = readl_relaxed(dev->base + QUP_ERROR_FLAGS); |
| 204 | op_flgs = readl_relaxed(dev->base + QUP_OPERATIONAL); |
| 205 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | if (!dev->msg || !dev->complete) { |
| 207 | /* Clear Error interrupt if it's a level triggered interrupt*/ |
| 208 | if (dev->num_irqs == 1) { |
| 209 | writel_relaxed(QUP_RESET_STATE, dev->base+QUP_STATE); |
| 210 | /* Ensure that state is written before ISR exits */ |
| 211 | mb(); |
| 212 | } |
| 213 | return IRQ_HANDLED; |
| 214 | } |
| 215 | |
| 216 | if (status & I2C_STATUS_ERROR_MASK) { |
| 217 | dev_err(dev->dev, "QUP: I2C status flags :0x%x, irq:%d\n", |
| 218 | status, irq); |
| 219 | err = status; |
| 220 | /* Clear Error interrupt if it's a level triggered interrupt*/ |
| 221 | if (dev->num_irqs == 1) { |
| 222 | writel_relaxed(QUP_RESET_STATE, dev->base+QUP_STATE); |
| 223 | /* Ensure that state is written before ISR exits */ |
| 224 | mb(); |
| 225 | } |
| 226 | goto intr_done; |
| 227 | } |
| 228 | |
| 229 | if (status1 & 0x7F) { |
| 230 | dev_err(dev->dev, "QUP: QUP status flags :0x%x\n", status1); |
| 231 | err = -status1; |
| 232 | /* Clear Error interrupt if it's a level triggered interrupt*/ |
| 233 | if (dev->num_irqs == 1) { |
| 234 | writel_relaxed((status1 & QUP_STATUS_ERROR_FLAGS), |
| 235 | dev->base + QUP_ERROR_FLAGS); |
| 236 | /* Ensure that error flags are cleared before ISR |
| 237 | * exits |
| 238 | */ |
| 239 | mb(); |
| 240 | } |
| 241 | goto intr_done; |
| 242 | } |
| 243 | |
| 244 | if ((dev->num_irqs == 3) && (dev->msg->flags == I2C_M_RD) |
| 245 | && (irq == dev->out_irq)) |
| 246 | return IRQ_HANDLED; |
| 247 | if (op_flgs & QUP_OUT_SVC_FLAG) { |
| 248 | writel_relaxed(QUP_OUT_SVC_FLAG, dev->base + QUP_OPERATIONAL); |
| 249 | /* Ensure that service flag is acknowledged before ISR exits */ |
| 250 | mb(); |
| 251 | } |
| 252 | if (dev->msg->flags == I2C_M_RD) { |
| 253 | if ((op_flgs & QUP_MX_INPUT_DONE) || |
| 254 | (op_flgs & QUP_IN_SVC_FLAG)) { |
| 255 | writel_relaxed(QUP_IN_SVC_FLAG, dev->base |
| 256 | + QUP_OPERATIONAL); |
| 257 | /* Ensure that service flag is acknowledged before ISR |
| 258 | * exits |
| 259 | */ |
| 260 | mb(); |
| 261 | } else |
| 262 | return IRQ_HANDLED; |
| 263 | } |
| 264 | |
| 265 | intr_done: |
| 266 | dev_dbg(dev->dev, "QUP intr= %d, i2c status=0x%x, qup status = 0x%x\n", |
| 267 | irq, status, status1); |
| 268 | qup_print_status(dev); |
| 269 | dev->err = err; |
| 270 | complete(dev->complete); |
| 271 | return IRQ_HANDLED; |
| 272 | } |
| 273 | |
Sagar Dharia | 57ac1ac | 2011-08-06 15:12:44 -0600 | [diff] [blame] | 274 | static int |
| 275 | qup_i2c_poll_state(struct qup_i2c_dev *dev, uint32_t req_state, bool only_valid) |
| 276 | { |
| 277 | uint32_t retries = 0; |
| 278 | |
| 279 | dev_dbg(dev->dev, "Polling for state:0x%x, or valid-only:%d\n", |
| 280 | req_state, only_valid); |
| 281 | |
| 282 | while (retries != 2000) { |
| 283 | uint32_t status = readl_relaxed(dev->base + QUP_STATE); |
| 284 | |
| 285 | /* |
| 286 | * If only valid bit needs to be checked, requested state is |
| 287 | * 'don't care' |
| 288 | */ |
| 289 | if (status & QUP_STATE_VALID) { |
| 290 | if (only_valid) |
| 291 | return 0; |
| 292 | else if ((req_state & QUP_I2C_MAST_GEN) && |
| 293 | (status & QUP_I2C_MAST_GEN)) |
| 294 | return 0; |
| 295 | else if ((status & QUP_STATE_MASK) == req_state) |
| 296 | return 0; |
| 297 | } |
| 298 | if (retries++ == 1000) |
| 299 | udelay(100); |
| 300 | } |
| 301 | return -ETIMEDOUT; |
| 302 | } |
| 303 | |
| 304 | static int |
| 305 | qup_update_state(struct qup_i2c_dev *dev, uint32_t state) |
| 306 | { |
| 307 | if (qup_i2c_poll_state(dev, 0, true) != 0) |
| 308 | return -EIO; |
| 309 | writel_relaxed(state, dev->base + QUP_STATE); |
| 310 | if (qup_i2c_poll_state(dev, state, false) != 0) |
| 311 | return -EIO; |
| 312 | return 0; |
| 313 | } |
| 314 | |
Trilok Soni | f0274f1 | 2011-08-19 12:26:13 +0530 | [diff] [blame] | 315 | /* |
| 316 | * Before calling qup_config_core_on_en(), please make |
| 317 | * sure that QuPE core is in RESET state. |
Trilok Soni | f0274f1 | 2011-08-19 12:26:13 +0530 | [diff] [blame] | 318 | */ |
| 319 | static void |
| 320 | qup_config_core_on_en(struct qup_i2c_dev *dev) |
| 321 | { |
| 322 | uint32_t status; |
| 323 | |
Trilok Soni | f0274f1 | 2011-08-19 12:26:13 +0530 | [diff] [blame] | 324 | status = readl_relaxed(dev->base + QUP_CONFIG); |
| 325 | status |= BIT(13); |
| 326 | writel_relaxed(status, dev->base + QUP_CONFIG); |
| 327 | /* making sure that write has really gone through */ |
| 328 | mb(); |
| 329 | } |
| 330 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 331 | static void |
| 332 | qup_i2c_pwr_mgmt(struct qup_i2c_dev *dev, unsigned int state) |
| 333 | { |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 334 | dev->pwr_state = state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | if (state != 0) { |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 336 | clk_prepare_enable(dev->clk); |
Kiran Gunda | 9aa5e91 | 2012-08-16 18:37:46 +0530 | [diff] [blame] | 337 | if (!dev->pdata->keep_ahb_clk_on) |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 338 | clk_prepare_enable(dev->pclk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | } else { |
Sagar Dharia | 57ac1ac | 2011-08-06 15:12:44 -0600 | [diff] [blame] | 340 | qup_update_state(dev, QUP_RESET_STATE); |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 341 | clk_disable_unprepare(dev->clk); |
Trilok Soni | f0274f1 | 2011-08-19 12:26:13 +0530 | [diff] [blame] | 342 | qup_config_core_on_en(dev); |
Kiran Gunda | 9aa5e91 | 2012-08-16 18:37:46 +0530 | [diff] [blame] | 343 | if (!dev->pdata->keep_ahb_clk_on) |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 344 | clk_disable_unprepare(dev->pclk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 348 | static int |
| 349 | qup_i2c_poll_writeready(struct qup_i2c_dev *dev, int rem) |
| 350 | { |
| 351 | uint32_t retries = 0; |
| 352 | |
| 353 | while (retries != 2000) { |
| 354 | uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS); |
| 355 | |
| 356 | if (!(status & I2C_STATUS_WR_BUFFER_FULL)) { |
| 357 | if (((dev->msg->flags & I2C_M_RD) || (rem == 0)) && |
| 358 | !(status & I2C_STATUS_BUS_ACTIVE)) |
| 359 | return 0; |
| 360 | else if ((dev->msg->flags == 0) && (rem > 0)) |
| 361 | return 0; |
| 362 | else /* 1-bit delay before we check for bus busy */ |
| 363 | udelay(dev->one_bit_t); |
| 364 | } |
Harini Jayaraman | d997b3b | 2011-10-11 14:25:29 -0600 | [diff] [blame] | 365 | if (retries++ == 1000) { |
| 366 | /* |
| 367 | * Wait for FIFO number of bytes to be absolutely sure |
| 368 | * that I2C write state machine is not idle. Each byte |
| 369 | * takes 9 clock cycles. (8 bits + 1 ack) |
| 370 | */ |
| 371 | usleep_range((dev->one_bit_t * (dev->out_fifo_sz * 9)), |
| 372 | (dev->one_bit_t * (dev->out_fifo_sz * 9))); |
| 373 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 374 | } |
| 375 | qup_print_status(dev); |
| 376 | return -ETIMEDOUT; |
| 377 | } |
| 378 | |
| 379 | static int qup_i2c_poll_clock_ready(struct qup_i2c_dev *dev) |
| 380 | { |
| 381 | uint32_t retries = 0; |
| 382 | |
| 383 | /* |
| 384 | * Wait for the clock state to transition to either IDLE or FORCED |
| 385 | * LOW. This will usually happen within one cycle of the i2c clock. |
| 386 | */ |
| 387 | |
| 388 | while (retries++ < QUP_MAX_CLK_STATE_RETRIES) { |
| 389 | uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS); |
| 390 | uint32_t clk_state = (status >> 13) & 0x7; |
| 391 | |
| 392 | if (clk_state == I2C_CLK_RESET_BUSIDLE_STATE || |
| 393 | clk_state == I2C_CLK_FORCED_LOW_STATE) |
| 394 | return 0; |
| 395 | /* 1-bit delay before we check again */ |
| 396 | udelay(dev->one_bit_t); |
| 397 | } |
| 398 | |
| 399 | dev_err(dev->dev, "Error waiting for clk ready\n"); |
| 400 | return -ETIMEDOUT; |
| 401 | } |
| 402 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 403 | static inline int qup_i2c_request_gpios(struct qup_i2c_dev *dev) |
| 404 | { |
| 405 | int i; |
| 406 | int result = 0; |
| 407 | |
| 408 | for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) { |
| 409 | if (dev->i2c_gpios[i] >= 0) { |
| 410 | result = gpio_request(dev->i2c_gpios[i], i2c_rsrcs[i]); |
| 411 | if (result) { |
| 412 | dev_err(dev->dev, |
| 413 | "gpio_request for pin %d failed\ |
| 414 | with error %d\n", dev->i2c_gpios[i], |
| 415 | result); |
| 416 | goto error; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return 0; |
| 421 | |
| 422 | error: |
| 423 | for (; --i >= 0;) { |
| 424 | if (dev->i2c_gpios[i] >= 0) |
| 425 | gpio_free(dev->i2c_gpios[i]); |
| 426 | } |
| 427 | return result; |
| 428 | } |
| 429 | |
| 430 | static inline void qup_i2c_free_gpios(struct qup_i2c_dev *dev) |
| 431 | { |
| 432 | int i; |
| 433 | |
| 434 | for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) { |
| 435 | if (dev->i2c_gpios[i] >= 0) |
| 436 | gpio_free(dev->i2c_gpios[i]); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | #ifdef DEBUG |
| 441 | static void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val, |
| 442 | uint32_t addr, int rdwr) |
| 443 | { |
| 444 | if (rdwr) |
| 445 | dev_dbg(dev->dev, "RD:Wrote 0x%x to out_ff:0x%x\n", val, addr); |
| 446 | else |
| 447 | dev_dbg(dev->dev, "WR:Wrote 0x%x to out_ff:0x%x\n", val, addr); |
| 448 | } |
| 449 | #else |
| 450 | static inline void qup_verify_fifo(struct qup_i2c_dev *dev, uint32_t val, |
| 451 | uint32_t addr, int rdwr) |
| 452 | { |
| 453 | } |
| 454 | #endif |
| 455 | |
| 456 | static void |
| 457 | qup_issue_read(struct qup_i2c_dev *dev, struct i2c_msg *msg, int *idx, |
| 458 | uint32_t carry_over) |
| 459 | { |
| 460 | uint16_t addr = (msg->addr << 1) | 1; |
| 461 | /* QUP limit 256 bytes per read. By HW design, 0 in the 8-bit field |
| 462 | * is treated as 256 byte read. |
| 463 | */ |
| 464 | uint16_t rd_len = ((dev->cnt == 256) ? 0 : dev->cnt); |
| 465 | |
| 466 | if (*idx % 4) { |
| 467 | writel_relaxed(carry_over | ((QUP_OUT_START | addr) << 16), |
| 468 | dev->base + QUP_OUT_FIFO_BASE);/* + (*idx-2)); */ |
| 469 | |
| 470 | qup_verify_fifo(dev, carry_over | |
| 471 | ((QUP_OUT_START | addr) << 16), (uint32_t)dev->base |
| 472 | + QUP_OUT_FIFO_BASE + (*idx - 2), 1); |
| 473 | writel_relaxed((QUP_OUT_REC | rd_len), |
| 474 | dev->base + QUP_OUT_FIFO_BASE);/* + (*idx+2)); */ |
| 475 | |
| 476 | qup_verify_fifo(dev, (QUP_OUT_REC | rd_len), |
| 477 | (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx + 2), 1); |
| 478 | } else { |
| 479 | writel_relaxed(((QUP_OUT_REC | rd_len) << 16) |
| 480 | | QUP_OUT_START | addr, |
| 481 | dev->base + QUP_OUT_FIFO_BASE);/* + (*idx)); */ |
| 482 | |
| 483 | qup_verify_fifo(dev, QUP_OUT_REC << 16 | rd_len << 16 | |
| 484 | QUP_OUT_START | addr, |
| 485 | (uint32_t)dev->base + QUP_OUT_FIFO_BASE + (*idx), 1); |
| 486 | } |
| 487 | *idx += 4; |
| 488 | } |
| 489 | |
| 490 | static void |
| 491 | qup_issue_write(struct qup_i2c_dev *dev, struct i2c_msg *msg, int rem, |
| 492 | int *idx, uint32_t *carry_over) |
| 493 | { |
| 494 | int entries = dev->cnt; |
| 495 | int empty_sl = dev->wr_sz - ((*idx) >> 1); |
| 496 | int i = 0; |
| 497 | uint32_t val = 0; |
| 498 | uint32_t last_entry = 0; |
| 499 | uint16_t addr = msg->addr << 1; |
| 500 | |
| 501 | if (dev->pos == 0) { |
| 502 | if (*idx % 4) { |
| 503 | writel_relaxed(*carry_over | ((QUP_OUT_START | |
| 504 | addr) << 16), |
| 505 | dev->base + QUP_OUT_FIFO_BASE); |
| 506 | |
Harini Jayaraman | be7e45d | 2011-11-02 12:12:03 -0600 | [diff] [blame] | 507 | qup_verify_fifo(dev, *carry_over | QUP_OUT_START << 16 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 508 | addr << 16, (uint32_t)dev->base + |
| 509 | QUP_OUT_FIFO_BASE + (*idx) - 2, 0); |
| 510 | } else |
| 511 | val = QUP_OUT_START | addr; |
| 512 | *idx += 2; |
| 513 | i++; |
| 514 | entries++; |
| 515 | } else { |
| 516 | /* Avoid setp time issue by adding 1 NOP when number of bytes |
| 517 | * are more than FIFO/BLOCK size. setup time issue can't appear |
| 518 | * otherwise since next byte to be written will always be ready |
| 519 | */ |
| 520 | val = (QUP_OUT_NOP | 1); |
| 521 | *idx += 2; |
| 522 | i++; |
| 523 | entries++; |
| 524 | } |
| 525 | if (entries > empty_sl) |
| 526 | entries = empty_sl; |
| 527 | |
| 528 | for (; i < (entries - 1); i++) { |
| 529 | if (*idx % 4) { |
| 530 | writel_relaxed(val | ((QUP_OUT_DATA | |
| 531 | msg->buf[dev->pos]) << 16), |
| 532 | dev->base + QUP_OUT_FIFO_BASE); |
| 533 | |
| 534 | qup_verify_fifo(dev, val | QUP_OUT_DATA << 16 | |
| 535 | msg->buf[dev->pos] << 16, (uint32_t)dev->base + |
| 536 | QUP_OUT_FIFO_BASE + (*idx) - 2, 0); |
| 537 | } else |
| 538 | val = QUP_OUT_DATA | msg->buf[dev->pos]; |
| 539 | (*idx) += 2; |
| 540 | dev->pos++; |
| 541 | } |
| 542 | if (dev->pos < (msg->len - 1)) |
| 543 | last_entry = QUP_OUT_DATA; |
| 544 | else if (rem > 1) /* not last array entry */ |
| 545 | last_entry = QUP_OUT_DATA; |
| 546 | else |
| 547 | last_entry = QUP_OUT_STOP; |
| 548 | if ((*idx % 4) == 0) { |
| 549 | /* |
| 550 | * If read-start and read-command end up in different fifos, it |
| 551 | * may result in extra-byte being read due to extra-read cycle. |
| 552 | * Avoid that by inserting NOP as the last entry of fifo only |
| 553 | * if write command(s) leave 1 space in fifo. |
| 554 | */ |
| 555 | if (rem > 1) { |
| 556 | struct i2c_msg *next = msg + 1; |
Harini Jayaraman | 24bea43 | 2011-10-11 16:06:28 -0600 | [diff] [blame] | 557 | if (next->addr == msg->addr && (next->flags & I2C_M_RD) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 558 | && *idx == ((dev->wr_sz*2) - 4)) { |
| 559 | writel_relaxed(((last_entry | |
| 560 | msg->buf[dev->pos]) | |
| 561 | ((1 | QUP_OUT_NOP) << 16)), dev->base + |
| 562 | QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */ |
| 563 | |
| 564 | qup_verify_fifo(dev, |
| 565 | ((last_entry | msg->buf[dev->pos]) | |
| 566 | ((1 | QUP_OUT_NOP) << 16)), |
| 567 | (uint32_t)dev->base + |
| 568 | QUP_OUT_FIFO_BASE + (*idx), 0); |
| 569 | *idx += 2; |
| 570 | } else if (next->flags == 0 && dev->pos == msg->len - 1 |
Harini Jayaraman | be7e45d | 2011-11-02 12:12:03 -0600 | [diff] [blame] | 571 | && *idx < (dev->wr_sz*2) && |
| 572 | (next->addr != msg->addr)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 573 | /* Last byte of an intermittent write */ |
Harini Jayaraman | be7e45d | 2011-11-02 12:12:03 -0600 | [diff] [blame] | 574 | writel_relaxed((QUP_OUT_STOP | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 575 | msg->buf[dev->pos]), |
| 576 | dev->base + QUP_OUT_FIFO_BASE); |
| 577 | |
| 578 | qup_verify_fifo(dev, |
Harini Jayaraman | be7e45d | 2011-11-02 12:12:03 -0600 | [diff] [blame] | 579 | QUP_OUT_STOP | msg->buf[dev->pos], |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | (uint32_t)dev->base + |
| 581 | QUP_OUT_FIFO_BASE + (*idx), 0); |
| 582 | *idx += 2; |
| 583 | } else |
| 584 | *carry_over = (last_entry | msg->buf[dev->pos]); |
| 585 | } else { |
| 586 | writel_relaxed((last_entry | msg->buf[dev->pos]), |
| 587 | dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */ |
| 588 | |
| 589 | qup_verify_fifo(dev, last_entry | msg->buf[dev->pos], |
| 590 | (uint32_t)dev->base + QUP_OUT_FIFO_BASE + |
| 591 | (*idx), 0); |
| 592 | } |
| 593 | } else { |
| 594 | writel_relaxed(val | ((last_entry | msg->buf[dev->pos]) << 16), |
| 595 | dev->base + QUP_OUT_FIFO_BASE);/* + (*idx) - 2); */ |
| 596 | |
| 597 | qup_verify_fifo(dev, val | (last_entry << 16) | |
| 598 | (msg->buf[dev->pos] << 16), (uint32_t)dev->base + |
| 599 | QUP_OUT_FIFO_BASE + (*idx) - 2, 0); |
| 600 | } |
| 601 | |
| 602 | *idx += 2; |
| 603 | dev->pos++; |
| 604 | dev->cnt = msg->len - dev->pos; |
| 605 | } |
| 606 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 607 | static void |
| 608 | qup_set_read_mode(struct qup_i2c_dev *dev, int rd_len) |
| 609 | { |
| 610 | uint32_t wr_mode = (dev->wr_sz < dev->out_fifo_sz) ? |
| 611 | QUP_WR_BLK_MODE : 0; |
| 612 | if (rd_len > 256) { |
| 613 | dev_dbg(dev->dev, "HW limit: Breaking reads in chunk of 256\n"); |
| 614 | rd_len = 256; |
| 615 | } |
| 616 | if (rd_len <= dev->in_fifo_sz) { |
| 617 | writel_relaxed(wr_mode | QUP_PACK_EN | QUP_UNPACK_EN, |
| 618 | dev->base + QUP_IO_MODE); |
| 619 | writel_relaxed(rd_len, dev->base + QUP_MX_READ_CNT); |
| 620 | } else { |
| 621 | writel_relaxed(wr_mode | QUP_RD_BLK_MODE | |
| 622 | QUP_PACK_EN | QUP_UNPACK_EN, dev->base + QUP_IO_MODE); |
| 623 | writel_relaxed(rd_len, dev->base + QUP_MX_INPUT_CNT); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | static int |
| 628 | qup_set_wr_mode(struct qup_i2c_dev *dev, int rem) |
| 629 | { |
| 630 | int total_len = 0; |
| 631 | int ret = 0; |
Kenneth Heitke | 6a852e9 | 2011-10-20 17:56:03 -0600 | [diff] [blame] | 632 | int len = dev->msg->len; |
| 633 | struct i2c_msg *next = NULL; |
| 634 | if (rem > 1) |
| 635 | next = dev->msg + 1; |
Harini Jayaraman | be7e45d | 2011-11-02 12:12:03 -0600 | [diff] [blame] | 636 | while (rem > 1 && next->flags == 0 && (next->addr == dev->msg->addr)) { |
Kenneth Heitke | 6a852e9 | 2011-10-20 17:56:03 -0600 | [diff] [blame] | 637 | len += next->len + 1; |
| 638 | next = next + 1; |
| 639 | rem--; |
| 640 | } |
| 641 | if (len >= (dev->out_fifo_sz - 1)) { |
| 642 | total_len = len + 1 + (len/(dev->out_blk_sz-1)); |
| 643 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 644 | writel_relaxed(QUP_WR_BLK_MODE | QUP_PACK_EN | QUP_UNPACK_EN, |
| 645 | dev->base + QUP_IO_MODE); |
| 646 | dev->wr_sz = dev->out_blk_sz; |
| 647 | } else |
| 648 | writel_relaxed(QUP_PACK_EN | QUP_UNPACK_EN, |
| 649 | dev->base + QUP_IO_MODE); |
| 650 | |
| 651 | if (rem > 1) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | if (next->addr == dev->msg->addr && |
| 653 | next->flags == I2C_M_RD) { |
| 654 | qup_set_read_mode(dev, next->len); |
| 655 | /* make sure read start & read command are in 1 blk */ |
| 656 | if ((total_len % dev->out_blk_sz) == |
| 657 | (dev->out_blk_sz - 1)) |
| 658 | total_len += 3; |
| 659 | else |
| 660 | total_len += 2; |
| 661 | } |
| 662 | } |
| 663 | /* WRITE COUNT register valid/used only in block mode */ |
| 664 | if (dev->wr_sz == dev->out_blk_sz) |
| 665 | writel_relaxed(total_len, dev->base + QUP_MX_WR_CNT); |
| 666 | return ret; |
| 667 | } |
| 668 | |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 669 | |
| 670 | static void qup_i2c_recover_bus_busy(struct qup_i2c_dev *dev) |
| 671 | { |
| 672 | int i; |
| 673 | int gpio_clk; |
| 674 | int gpio_dat; |
| 675 | bool gpio_clk_status = false; |
| 676 | uint32_t status = readl_relaxed(dev->base + QUP_I2C_STATUS); |
| 677 | struct gpiomux_setting old_gpio_setting; |
| 678 | |
| 679 | if (dev->pdata->msm_i2c_config_gpio) |
| 680 | return; |
| 681 | |
| 682 | if (!(status & (I2C_STATUS_BUS_ACTIVE)) || |
| 683 | (status & (I2C_STATUS_BUS_MASTER))) |
| 684 | return; |
| 685 | |
| 686 | gpio_clk = dev->i2c_gpios[0]; |
| 687 | gpio_dat = dev->i2c_gpios[1]; |
| 688 | |
| 689 | if ((gpio_clk == -1) && (gpio_dat == -1)) { |
| 690 | dev_err(dev->dev, "Recovery failed due to undefined GPIO's\n"); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | disable_irq(dev->err_irq); |
| 695 | for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) { |
| 696 | if (msm_gpiomux_write(dev->i2c_gpios[i], GPIOMUX_ACTIVE, |
| 697 | &recovery_config, &old_gpio_setting)) { |
| 698 | dev_err(dev->dev, "GPIO pins have no active setting\n"); |
| 699 | goto recovery_end; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | dev_warn(dev->dev, "i2c_scl: %d, i2c_sda: %d\n", |
| 704 | gpio_get_value(gpio_clk), gpio_get_value(gpio_dat)); |
| 705 | |
| 706 | for (i = 0; i < 9; i++) { |
| 707 | if (gpio_get_value(gpio_dat) && gpio_clk_status) |
| 708 | break; |
| 709 | gpio_direction_output(gpio_clk, 0); |
| 710 | udelay(5); |
| 711 | gpio_direction_output(gpio_dat, 0); |
| 712 | udelay(5); |
| 713 | gpio_direction_input(gpio_clk); |
| 714 | udelay(5); |
| 715 | if (!gpio_get_value(gpio_clk)) |
| 716 | udelay(20); |
| 717 | if (!gpio_get_value(gpio_clk)) |
| 718 | usleep_range(10000, 10000); |
| 719 | gpio_clk_status = gpio_get_value(gpio_clk); |
| 720 | gpio_direction_input(gpio_dat); |
| 721 | udelay(5); |
| 722 | } |
| 723 | |
| 724 | /* Configure ALT funciton to QUP I2C*/ |
| 725 | for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) { |
| 726 | msm_gpiomux_write(dev->i2c_gpios[i], GPIOMUX_ACTIVE, |
| 727 | &old_gpio_setting, NULL); |
| 728 | } |
| 729 | |
| 730 | udelay(10); |
| 731 | |
| 732 | status = readl_relaxed(dev->base + QUP_I2C_STATUS); |
| 733 | if (!(status & I2C_STATUS_BUS_ACTIVE)) { |
| 734 | dev_info(dev->dev, "Bus busy cleared after %d clock cycles, " |
| 735 | "status %x\n", |
| 736 | i, status); |
| 737 | goto recovery_end; |
| 738 | } |
| 739 | |
| 740 | dev_warn(dev->dev, "Bus still busy, status %x\n", status); |
| 741 | |
| 742 | recovery_end: |
| 743 | enable_irq(dev->err_irq); |
| 744 | } |
| 745 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 746 | static int |
| 747 | qup_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 748 | { |
| 749 | DECLARE_COMPLETION_ONSTACK(complete); |
| 750 | struct qup_i2c_dev *dev = i2c_get_adapdata(adap); |
| 751 | int ret; |
| 752 | int rem = num; |
| 753 | long timeout; |
| 754 | int err; |
| 755 | |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 756 | pm_runtime_get_sync(dev->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 757 | mutex_lock(&dev->mlock); |
| 758 | |
| 759 | if (dev->suspended) { |
| 760 | mutex_unlock(&dev->mlock); |
| 761 | return -EIO; |
| 762 | } |
| 763 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 764 | /* Initialize QUP registers during first transfer */ |
| 765 | if (dev->clk_ctl == 0) { |
| 766 | int fs_div; |
| 767 | int hs_div; |
| 768 | uint32_t fifo_reg; |
| 769 | |
| 770 | if (dev->gsbi) { |
| 771 | writel_relaxed(0x2 << 4, dev->gsbi); |
| 772 | /* GSBI memory is not in the same 1K region as other |
| 773 | * QUP registers. mb() here ensures that the GSBI |
| 774 | * register is updated in correct order and that the |
| 775 | * write has gone through before programming QUP core |
| 776 | * registers |
| 777 | */ |
| 778 | mb(); |
| 779 | } |
| 780 | |
| 781 | fs_div = ((dev->pdata->src_clk_rate |
| 782 | / dev->pdata->clk_freq) / 2) - 3; |
| 783 | hs_div = 3; |
| 784 | dev->clk_ctl = ((hs_div & 0x7) << 8) | (fs_div & 0xff); |
| 785 | fifo_reg = readl_relaxed(dev->base + QUP_IO_MODE); |
| 786 | if (fifo_reg & 0x3) |
| 787 | dev->out_blk_sz = (fifo_reg & 0x3) * 16; |
| 788 | else |
| 789 | dev->out_blk_sz = 16; |
| 790 | if (fifo_reg & 0x60) |
| 791 | dev->in_blk_sz = ((fifo_reg & 0x60) >> 5) * 16; |
| 792 | else |
| 793 | dev->in_blk_sz = 16; |
| 794 | /* |
| 795 | * The block/fifo size w.r.t. 'actual data' is 1/2 due to 'tag' |
| 796 | * associated with each byte written/received |
| 797 | */ |
| 798 | dev->out_blk_sz /= 2; |
| 799 | dev->in_blk_sz /= 2; |
| 800 | dev->out_fifo_sz = dev->out_blk_sz * |
| 801 | (2 << ((fifo_reg & 0x1C) >> 2)); |
| 802 | dev->in_fifo_sz = dev->in_blk_sz * |
| 803 | (2 << ((fifo_reg & 0x380) >> 7)); |
| 804 | dev_dbg(dev->dev, "QUP IN:bl:%d, ff:%d, OUT:bl:%d, ff:%d\n", |
| 805 | dev->in_blk_sz, dev->in_fifo_sz, |
| 806 | dev->out_blk_sz, dev->out_fifo_sz); |
| 807 | } |
| 808 | |
| 809 | writel_relaxed(1, dev->base + QUP_SW_RESET); |
Sagar Dharia | 518e230 | 2011-08-05 11:03:03 -0600 | [diff] [blame] | 810 | ret = qup_i2c_poll_state(dev, QUP_RESET_STATE, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 811 | if (ret) { |
| 812 | dev_err(dev->dev, "QUP Busy:Trying to recover\n"); |
| 813 | goto out_err; |
| 814 | } |
| 815 | |
| 816 | if (dev->num_irqs == 3) { |
| 817 | enable_irq(dev->in_irq); |
| 818 | enable_irq(dev->out_irq); |
| 819 | } |
| 820 | enable_irq(dev->err_irq); |
| 821 | |
| 822 | /* Initialize QUP registers */ |
| 823 | writel_relaxed(0, dev->base + QUP_CONFIG); |
| 824 | writel_relaxed(QUP_OPERATIONAL_RESET, dev->base + QUP_OPERATIONAL); |
| 825 | writel_relaxed(QUP_STATUS_ERROR_FLAGS, dev->base + QUP_ERROR_FLAGS_EN); |
| 826 | |
| 827 | writel_relaxed(I2C_MINI_CORE | I2C_N_VAL, dev->base + QUP_CONFIG); |
| 828 | |
| 829 | /* Initialize I2C mini core registers */ |
| 830 | writel_relaxed(0, dev->base + QUP_I2C_CLK_CTL); |
| 831 | writel_relaxed(QUP_I2C_STATUS_RESET, dev->base + QUP_I2C_STATUS); |
| 832 | |
| 833 | while (rem) { |
| 834 | bool filled = false; |
| 835 | |
| 836 | dev->cnt = msgs->len - dev->pos; |
| 837 | dev->msg = msgs; |
| 838 | |
| 839 | dev->wr_sz = dev->out_fifo_sz; |
| 840 | dev->err = 0; |
| 841 | dev->complete = &complete; |
| 842 | |
Sagar Dharia | 518e230 | 2011-08-05 11:03:03 -0600 | [diff] [blame] | 843 | if (qup_i2c_poll_state(dev, QUP_I2C_MAST_GEN, false) != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 844 | ret = -EIO; |
| 845 | goto out_err; |
| 846 | } |
| 847 | |
| 848 | qup_print_status(dev); |
| 849 | /* HW limits Read upto 256 bytes in 1 read without stop */ |
| 850 | if (dev->msg->flags & I2C_M_RD) { |
| 851 | qup_set_read_mode(dev, dev->cnt); |
| 852 | if (dev->cnt > 256) |
| 853 | dev->cnt = 256; |
| 854 | } else { |
| 855 | ret = qup_set_wr_mode(dev, rem); |
| 856 | if (ret != 0) |
| 857 | goto out_err; |
| 858 | /* Don't fill block till we get interrupt */ |
| 859 | if (dev->wr_sz == dev->out_blk_sz) |
| 860 | filled = true; |
| 861 | } |
| 862 | |
| 863 | err = qup_update_state(dev, QUP_RUN_STATE); |
| 864 | if (err < 0) { |
| 865 | ret = err; |
| 866 | goto out_err; |
| 867 | } |
| 868 | |
| 869 | qup_print_status(dev); |
| 870 | writel_relaxed(dev->clk_ctl, dev->base + QUP_I2C_CLK_CTL); |
| 871 | /* CLK_CTL register is not in the same 1K region as other QUP |
| 872 | * registers. Ensure that clock control is written before |
| 873 | * programming other QUP registers |
| 874 | */ |
| 875 | mb(); |
| 876 | |
| 877 | do { |
| 878 | int idx = 0; |
| 879 | uint32_t carry_over = 0; |
| 880 | |
| 881 | /* Transition to PAUSE state only possible from RUN */ |
| 882 | err = qup_update_state(dev, QUP_PAUSE_STATE); |
| 883 | if (err < 0) { |
| 884 | ret = err; |
| 885 | goto out_err; |
| 886 | } |
| 887 | |
| 888 | qup_print_status(dev); |
| 889 | /* This operation is Write, check the next operation |
| 890 | * and decide mode |
| 891 | */ |
| 892 | while (filled == false) { |
| 893 | if ((msgs->flags & I2C_M_RD)) |
| 894 | qup_issue_read(dev, msgs, &idx, |
| 895 | carry_over); |
| 896 | else if (!(msgs->flags & I2C_M_RD)) |
| 897 | qup_issue_write(dev, msgs, rem, &idx, |
| 898 | &carry_over); |
| 899 | if (idx >= (dev->wr_sz << 1)) |
| 900 | filled = true; |
| 901 | /* Start new message */ |
| 902 | if (filled == false) { |
| 903 | if (msgs->flags & I2C_M_RD) |
| 904 | filled = true; |
| 905 | else if (rem > 1) { |
| 906 | /* Only combine operations with |
| 907 | * same address |
| 908 | */ |
| 909 | struct i2c_msg *next = msgs + 1; |
| 910 | if (next->addr != msgs->addr) |
| 911 | filled = true; |
| 912 | else { |
| 913 | rem--; |
| 914 | msgs++; |
| 915 | dev->msg = msgs; |
| 916 | dev->pos = 0; |
| 917 | dev->cnt = msgs->len; |
| 918 | if (msgs->len > 256) |
| 919 | dev->cnt = 256; |
| 920 | } |
| 921 | } else |
| 922 | filled = true; |
| 923 | } |
| 924 | } |
| 925 | err = qup_update_state(dev, QUP_RUN_STATE); |
| 926 | if (err < 0) { |
| 927 | ret = err; |
| 928 | goto out_err; |
| 929 | } |
| 930 | dev_dbg(dev->dev, "idx:%d, rem:%d, num:%d, mode:%d\n", |
| 931 | idx, rem, num, dev->mode); |
| 932 | |
| 933 | qup_print_status(dev); |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 934 | timeout = wait_for_completion_timeout(&complete, |
| 935 | msecs_to_jiffies(dev->out_fifo_sz)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 936 | if (!timeout) { |
| 937 | uint32_t istatus = readl_relaxed(dev->base + |
| 938 | QUP_I2C_STATUS); |
| 939 | uint32_t qstatus = readl_relaxed(dev->base + |
| 940 | QUP_ERROR_FLAGS); |
| 941 | uint32_t op_flgs = readl_relaxed(dev->base + |
| 942 | QUP_OPERATIONAL); |
| 943 | |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 944 | /* |
| 945 | * Dont wait for 1 sec if i2c sees the bus |
| 946 | * active and controller is not master. |
| 947 | * A slave has pulled line low. Try to recover |
| 948 | */ |
| 949 | if (!(istatus & I2C_STATUS_BUS_ACTIVE) || |
| 950 | (istatus & I2C_STATUS_BUS_MASTER)) { |
| 951 | timeout = |
| 952 | wait_for_completion_timeout(&complete, |
| 953 | HZ); |
| 954 | if (timeout) |
| 955 | goto timeout_err; |
| 956 | } |
| 957 | qup_i2c_recover_bus_busy(dev); |
| 958 | dev_err(dev->dev, |
| 959 | "Transaction timed out, SL-AD = 0x%x\n", |
| 960 | dev->msg->addr); |
| 961 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 962 | dev_err(dev->dev, "I2C Status: %x\n", istatus); |
| 963 | dev_err(dev->dev, "QUP Status: %x\n", qstatus); |
| 964 | dev_err(dev->dev, "OP Flags: %x\n", op_flgs); |
| 965 | writel_relaxed(1, dev->base + QUP_SW_RESET); |
| 966 | /* Make sure that the write has gone through |
| 967 | * before returning from the function |
| 968 | */ |
| 969 | mb(); |
| 970 | ret = -ETIMEDOUT; |
| 971 | goto out_err; |
| 972 | } |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 973 | timeout_err: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 974 | if (dev->err) { |
| 975 | if (dev->err > 0 && |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 976 | dev->err & QUP_I2C_NACK_FLAG) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 977 | dev_err(dev->dev, |
| 978 | "I2C slave addr:0x%x not connected\n", |
| 979 | dev->msg->addr); |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 980 | dev->err = ENOTCONN; |
| 981 | } else if (dev->err < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 982 | dev_err(dev->dev, |
| 983 | "QUP data xfer error %d\n", dev->err); |
| 984 | ret = dev->err; |
| 985 | goto out_err; |
Harini Jayaraman | 17f8e0e | 2011-12-01 18:01:43 -0700 | [diff] [blame] | 986 | } else if (dev->err > 0) { |
| 987 | /* |
| 988 | * ISR returns +ve error if error code |
| 989 | * is I2C related, e.g. unexpected start |
| 990 | * So you may call recover-bus-busy when |
| 991 | * this error happens |
| 992 | */ |
| 993 | qup_i2c_recover_bus_busy(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 994 | } |
| 995 | ret = -dev->err; |
| 996 | goto out_err; |
| 997 | } |
| 998 | if (dev->msg->flags & I2C_M_RD) { |
| 999 | int i; |
| 1000 | uint32_t dval = 0; |
| 1001 | for (i = 0; dev->pos < dev->msg->len; i++, |
| 1002 | dev->pos++) { |
| 1003 | uint32_t rd_status = |
| 1004 | readl_relaxed(dev->base |
| 1005 | + QUP_OPERATIONAL); |
| 1006 | if (i % 2 == 0) { |
| 1007 | if ((rd_status & |
| 1008 | QUP_IN_NOT_EMPTY) == 0) |
| 1009 | break; |
| 1010 | dval = readl_relaxed(dev->base + |
| 1011 | QUP_IN_FIFO_BASE); |
| 1012 | dev->msg->buf[dev->pos] = |
| 1013 | dval & 0xFF; |
| 1014 | } else |
| 1015 | dev->msg->buf[dev->pos] = |
| 1016 | ((dval & 0xFF0000) >> |
| 1017 | 16); |
| 1018 | } |
| 1019 | dev->cnt -= i; |
| 1020 | } else |
| 1021 | filled = false; /* refill output FIFO */ |
| 1022 | dev_dbg(dev->dev, "pos:%d, len:%d, cnt:%d\n", |
| 1023 | dev->pos, msgs->len, dev->cnt); |
| 1024 | } while (dev->cnt > 0); |
| 1025 | if (dev->cnt == 0) { |
| 1026 | if (msgs->len == dev->pos) { |
| 1027 | rem--; |
| 1028 | msgs++; |
| 1029 | dev->pos = 0; |
| 1030 | } |
| 1031 | if (rem) { |
| 1032 | err = qup_i2c_poll_clock_ready(dev); |
| 1033 | if (err < 0) { |
| 1034 | ret = err; |
| 1035 | goto out_err; |
| 1036 | } |
| 1037 | err = qup_update_state(dev, QUP_RESET_STATE); |
| 1038 | if (err < 0) { |
| 1039 | ret = err; |
| 1040 | goto out_err; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | /* Wait for I2C bus to be idle */ |
| 1045 | ret = qup_i2c_poll_writeready(dev, rem); |
| 1046 | if (ret) { |
| 1047 | dev_err(dev->dev, |
| 1048 | "Error waiting for write ready\n"); |
| 1049 | goto out_err; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | ret = num; |
| 1054 | out_err: |
| 1055 | disable_irq(dev->err_irq); |
| 1056 | if (dev->num_irqs == 3) { |
| 1057 | disable_irq(dev->in_irq); |
| 1058 | disable_irq(dev->out_irq); |
| 1059 | } |
| 1060 | dev->complete = NULL; |
| 1061 | dev->msg = NULL; |
| 1062 | dev->pos = 0; |
| 1063 | dev->err = 0; |
| 1064 | dev->cnt = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1065 | mutex_unlock(&dev->mlock); |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1066 | pm_runtime_mark_last_busy(dev->dev); |
| 1067 | pm_runtime_put_autosuspend(dev->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1068 | return ret; |
| 1069 | } |
| 1070 | |
| 1071 | static u32 |
| 1072 | qup_i2c_func(struct i2c_adapter *adap) |
| 1073 | { |
| 1074 | return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); |
| 1075 | } |
| 1076 | |
| 1077 | static const struct i2c_algorithm qup_i2c_algo = { |
| 1078 | .master_xfer = qup_i2c_xfer, |
| 1079 | .functionality = qup_i2c_func, |
| 1080 | }; |
| 1081 | |
| 1082 | static int __devinit |
| 1083 | qup_i2c_probe(struct platform_device *pdev) |
| 1084 | { |
| 1085 | struct qup_i2c_dev *dev; |
| 1086 | struct resource *qup_mem, *gsbi_mem, *qup_io, *gsbi_io, *res; |
| 1087 | struct resource *in_irq, *out_irq, *err_irq; |
| 1088 | struct clk *clk, *pclk; |
| 1089 | int ret = 0; |
| 1090 | int i; |
| 1091 | struct msm_i2c_platform_data *pdata; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1092 | |
| 1093 | gsbi_mem = NULL; |
| 1094 | dev_dbg(&pdev->dev, "qup_i2c_probe\n"); |
| 1095 | |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1096 | if (pdev->dev.of_node) { |
| 1097 | struct device_node *node = pdev->dev.of_node; |
| 1098 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 1099 | if (!pdata) |
| 1100 | return -ENOMEM; |
| 1101 | ret = of_property_read_u32(node, "qcom,i2c-bus-freq", |
| 1102 | &pdata->clk_freq); |
| 1103 | if (ret) |
| 1104 | goto get_res_failed; |
| 1105 | ret = of_property_read_u32(node, "cell-index", &pdev->id); |
| 1106 | if (ret) |
| 1107 | goto get_res_failed; |
| 1108 | /* Optional property */ |
| 1109 | of_property_read_u32(node, "qcom,i2c-src-freq", |
| 1110 | &pdata->src_clk_rate); |
| 1111 | } else |
| 1112 | pdata = pdev->dev.platform_data; |
| 1113 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | if (!pdata) { |
| 1115 | dev_err(&pdev->dev, "platform data not initialized\n"); |
| 1116 | return -ENOSYS; |
| 1117 | } |
| 1118 | qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1119 | "qup_phys_addr"); |
| 1120 | if (!qup_mem) { |
| 1121 | dev_err(&pdev->dev, "no qup mem resource?\n"); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1122 | ret = -ENODEV; |
| 1123 | goto get_res_failed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * We only have 1 interrupt for new hardware targets and in_irq, |
| 1128 | * out_irq will be NULL for those platforms |
| 1129 | */ |
| 1130 | in_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1131 | "qup_in_intr"); |
| 1132 | |
| 1133 | out_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1134 | "qup_out_intr"); |
| 1135 | |
| 1136 | err_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1137 | "qup_err_intr"); |
| 1138 | if (!err_irq) { |
| 1139 | dev_err(&pdev->dev, "no error irq resource?\n"); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1140 | ret = -ENODEV; |
| 1141 | goto get_res_failed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | qup_io = request_mem_region(qup_mem->start, resource_size(qup_mem), |
| 1145 | pdev->name); |
| 1146 | if (!qup_io) { |
| 1147 | dev_err(&pdev->dev, "QUP region already claimed\n"); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1148 | ret = -EBUSY; |
| 1149 | goto get_res_failed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1150 | } |
| 1151 | if (!pdata->use_gsbi_shared_mode) { |
| 1152 | gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1153 | "gsbi_qup_i2c_addr"); |
| 1154 | if (!gsbi_mem) { |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1155 | dev_dbg(&pdev->dev, "Assume BLSP\n"); |
| 1156 | /* |
| 1157 | * BLSP core does not need protocol programming so this |
| 1158 | * resource is not expected |
| 1159 | */ |
| 1160 | goto blsp_core_init; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1161 | } |
| 1162 | gsbi_io = request_mem_region(gsbi_mem->start, |
| 1163 | resource_size(gsbi_mem), |
| 1164 | pdev->name); |
| 1165 | if (!gsbi_io) { |
| 1166 | dev_err(&pdev->dev, "GSBI region already claimed\n"); |
Harini Jayaraman | ee31ae9 | 2011-09-20 18:32:34 -0600 | [diff] [blame] | 1167 | ret = -EBUSY; |
| 1168 | goto err_res_failed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1172 | blsp_core_init: |
Matt Wagantall | ac29485 | 2011-08-17 15:44:58 -0700 | [diff] [blame] | 1173 | clk = clk_get(&pdev->dev, "core_clk"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | if (IS_ERR(clk)) { |
Matt Wagantall | ac29485 | 2011-08-17 15:44:58 -0700 | [diff] [blame] | 1175 | dev_err(&pdev->dev, "Could not get core_clk\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1176 | ret = PTR_ERR(clk); |
| 1177 | goto err_clk_get_failed; |
| 1178 | } |
| 1179 | |
Matt Wagantall | ac29485 | 2011-08-17 15:44:58 -0700 | [diff] [blame] | 1180 | pclk = clk_get(&pdev->dev, "iface_clk"); |
| 1181 | if (IS_ERR(pclk)) { |
| 1182 | dev_err(&pdev->dev, "Could not get iface_clk\n"); |
| 1183 | ret = PTR_ERR(pclk); |
| 1184 | clk_put(clk); |
| 1185 | goto err_clk_get_failed; |
| 1186 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1187 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1188 | /* We support frequencies upto FAST Mode(400KHz) */ |
| 1189 | if (pdata->clk_freq <= 0 || |
| 1190 | pdata->clk_freq > 400000) { |
| 1191 | dev_err(&pdev->dev, "clock frequency not supported\n"); |
| 1192 | ret = -EIO; |
| 1193 | goto err_config_failed; |
| 1194 | } |
| 1195 | |
| 1196 | dev = kzalloc(sizeof(struct qup_i2c_dev), GFP_KERNEL); |
| 1197 | if (!dev) { |
| 1198 | ret = -ENOMEM; |
| 1199 | goto err_alloc_dev_failed; |
| 1200 | } |
| 1201 | |
| 1202 | dev->dev = &pdev->dev; |
| 1203 | if (in_irq) |
| 1204 | dev->in_irq = in_irq->start; |
| 1205 | if (out_irq) |
| 1206 | dev->out_irq = out_irq->start; |
| 1207 | dev->err_irq = err_irq->start; |
| 1208 | if (in_irq && out_irq) |
| 1209 | dev->num_irqs = 3; |
| 1210 | else |
| 1211 | dev->num_irqs = 1; |
| 1212 | dev->clk = clk; |
| 1213 | dev->pclk = pclk; |
| 1214 | dev->base = ioremap(qup_mem->start, resource_size(qup_mem)); |
| 1215 | if (!dev->base) { |
| 1216 | ret = -ENOMEM; |
| 1217 | goto err_ioremap_failed; |
| 1218 | } |
| 1219 | |
| 1220 | /* Configure GSBI block to use I2C functionality */ |
| 1221 | if (gsbi_mem) { |
| 1222 | dev->gsbi = ioremap(gsbi_mem->start, resource_size(gsbi_mem)); |
| 1223 | if (!dev->gsbi) { |
| 1224 | ret = -ENOMEM; |
| 1225 | goto err_gsbi_failed; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | for (i = 0; i < ARRAY_SIZE(i2c_rsrcs); ++i) { |
| 1230 | res = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 1231 | i2c_rsrcs[i]); |
| 1232 | dev->i2c_gpios[i] = res ? res->start : -1; |
| 1233 | } |
| 1234 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1235 | platform_set_drvdata(pdev, dev); |
| 1236 | |
Harini Jayaraman | d997b3b | 2011-10-11 14:25:29 -0600 | [diff] [blame] | 1237 | dev->one_bit_t = (USEC_PER_SEC/pdata->clk_freq) + 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1238 | dev->pdata = pdata; |
| 1239 | dev->clk_ctl = 0; |
| 1240 | dev->pos = 0; |
| 1241 | |
| 1242 | /* |
Harini Jayaraman | 65c6cbe | 2012-03-27 17:06:32 -0600 | [diff] [blame] | 1243 | * If bootloaders leave a pending interrupt on certain GSBI's, |
| 1244 | * then we reset the core before registering for interrupts. |
| 1245 | */ |
Harini Jayaraman | d59ee0a | 2012-04-06 10:43:44 -0600 | [diff] [blame] | 1246 | |
| 1247 | if (dev->pdata->src_clk_rate > 0) |
| 1248 | clk_set_rate(dev->clk, dev->pdata->src_clk_rate); |
| 1249 | else |
| 1250 | dev->pdata->src_clk_rate = 19200000; |
| 1251 | |
Harini Jayaraman | 65c6cbe | 2012-03-27 17:06:32 -0600 | [diff] [blame] | 1252 | clk_prepare_enable(dev->clk); |
| 1253 | clk_prepare_enable(dev->pclk); |
| 1254 | writel_relaxed(1, dev->base + QUP_SW_RESET); |
| 1255 | if (qup_i2c_poll_state(dev, 0, true) != 0) |
| 1256 | goto err_reset_failed; |
| 1257 | clk_disable_unprepare(dev->clk); |
| 1258 | clk_disable_unprepare(dev->pclk); |
| 1259 | |
| 1260 | /* |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1261 | * We use num_irqs to also indicate if we got 3 interrupts or just 1. |
| 1262 | * If we have just 1, we use err_irq as the general purpose irq |
| 1263 | * and handle the changes in ISR accordingly |
| 1264 | * Per Hardware guidelines, if we have 3 interrupts, they are always |
| 1265 | * edge triggering, and if we have 1, it's always level-triggering |
| 1266 | */ |
| 1267 | if (dev->num_irqs == 3) { |
| 1268 | ret = request_irq(dev->in_irq, qup_i2c_interrupt, |
| 1269 | IRQF_TRIGGER_RISING, "qup_in_intr", dev); |
| 1270 | if (ret) { |
| 1271 | dev_err(&pdev->dev, "request_in_irq failed\n"); |
| 1272 | goto err_request_irq_failed; |
| 1273 | } |
| 1274 | /* |
| 1275 | * We assume out_irq exists if in_irq does since platform |
| 1276 | * configuration either has 3 interrupts assigned to QUP or 1 |
| 1277 | */ |
| 1278 | ret = request_irq(dev->out_irq, qup_i2c_interrupt, |
| 1279 | IRQF_TRIGGER_RISING, "qup_out_intr", dev); |
| 1280 | if (ret) { |
| 1281 | dev_err(&pdev->dev, "request_out_irq failed\n"); |
| 1282 | free_irq(dev->in_irq, dev); |
| 1283 | goto err_request_irq_failed; |
| 1284 | } |
| 1285 | ret = request_irq(dev->err_irq, qup_i2c_interrupt, |
| 1286 | IRQF_TRIGGER_RISING, "qup_err_intr", dev); |
| 1287 | if (ret) { |
| 1288 | dev_err(&pdev->dev, "request_err_irq failed\n"); |
| 1289 | free_irq(dev->out_irq, dev); |
| 1290 | free_irq(dev->in_irq, dev); |
| 1291 | goto err_request_irq_failed; |
| 1292 | } |
| 1293 | } else { |
| 1294 | ret = request_irq(dev->err_irq, qup_i2c_interrupt, |
| 1295 | IRQF_TRIGGER_HIGH, "qup_err_intr", dev); |
| 1296 | if (ret) { |
| 1297 | dev_err(&pdev->dev, "request_err_irq failed\n"); |
| 1298 | goto err_request_irq_failed; |
| 1299 | } |
| 1300 | } |
| 1301 | disable_irq(dev->err_irq); |
| 1302 | if (dev->num_irqs == 3) { |
| 1303 | disable_irq(dev->in_irq); |
| 1304 | disable_irq(dev->out_irq); |
| 1305 | } |
| 1306 | i2c_set_adapdata(&dev->adapter, dev); |
| 1307 | dev->adapter.algo = &qup_i2c_algo; |
| 1308 | strlcpy(dev->adapter.name, |
| 1309 | "QUP I2C adapter", |
| 1310 | sizeof(dev->adapter.name)); |
| 1311 | dev->adapter.nr = pdev->id; |
Kenneth Heitke | 9b70286 | 2013-01-30 13:39:13 -0700 | [diff] [blame] | 1312 | dev->adapter.dev.parent = &pdev->dev; |
Harini Jayaraman | ce67cf8 | 2011-08-05 09:26:06 -0600 | [diff] [blame] | 1313 | if (pdata->msm_i2c_config_gpio) |
| 1314 | pdata->msm_i2c_config_gpio(dev->adapter.nr, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1315 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1316 | mutex_init(&dev->mlock); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1317 | dev->pwr_state = 0; |
Kiran Gunda | 9aa5e91 | 2012-08-16 18:37:46 +0530 | [diff] [blame] | 1318 | /* If the same AHB clock is used on Modem side |
| 1319 | * switch it on here itself and don't switch it |
| 1320 | * on and off during suspend and resume. |
| 1321 | */ |
| 1322 | if (dev->pdata->keep_ahb_clk_on) |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1323 | clk_prepare_enable(dev->pclk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1324 | |
| 1325 | ret = i2c_add_numbered_adapter(&dev->adapter); |
| 1326 | if (ret) { |
| 1327 | dev_err(&pdev->dev, "i2c_add_adapter failed\n"); |
| 1328 | if (dev->num_irqs == 3) { |
| 1329 | free_irq(dev->out_irq, dev); |
| 1330 | free_irq(dev->in_irq, dev); |
| 1331 | } |
| 1332 | free_irq(dev->err_irq, dev); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1333 | } else { |
Amy Maloche | f8f1a46 | 2012-08-13 22:55:26 -0700 | [diff] [blame] | 1334 | if (dev->dev->of_node) { |
| 1335 | dev->adapter.dev.of_node = pdev->dev.of_node; |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1336 | of_i2c_register_devices(&dev->adapter); |
Amy Maloche | f8f1a46 | 2012-08-13 22:55:26 -0700 | [diff] [blame] | 1337 | } |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1338 | |
| 1339 | pm_runtime_set_autosuspend_delay(&pdev->dev, MSEC_PER_SEC); |
| 1340 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1341 | pm_runtime_enable(&pdev->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1342 | return 0; |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1343 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1344 | |
| 1345 | |
| 1346 | err_request_irq_failed: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1347 | if (dev->gsbi) |
| 1348 | iounmap(dev->gsbi); |
Harini Jayaraman | 65c6cbe | 2012-03-27 17:06:32 -0600 | [diff] [blame] | 1349 | err_reset_failed: |
| 1350 | clk_disable_unprepare(dev->clk); |
| 1351 | clk_disable_unprepare(dev->pclk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1352 | err_gsbi_failed: |
| 1353 | iounmap(dev->base); |
| 1354 | err_ioremap_failed: |
| 1355 | kfree(dev); |
| 1356 | err_alloc_dev_failed: |
| 1357 | err_config_failed: |
| 1358 | clk_put(clk); |
Matt Wagantall | ac29485 | 2011-08-17 15:44:58 -0700 | [diff] [blame] | 1359 | clk_put(pclk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1360 | err_clk_get_failed: |
| 1361 | if (gsbi_mem) |
| 1362 | release_mem_region(gsbi_mem->start, resource_size(gsbi_mem)); |
Harini Jayaraman | ee31ae9 | 2011-09-20 18:32:34 -0600 | [diff] [blame] | 1363 | err_res_failed: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1364 | release_mem_region(qup_mem->start, resource_size(qup_mem)); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1365 | get_res_failed: |
| 1366 | if (pdev->dev.of_node) |
| 1367 | kfree(pdata); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | static int __devexit |
| 1372 | qup_i2c_remove(struct platform_device *pdev) |
| 1373 | { |
| 1374 | struct qup_i2c_dev *dev = platform_get_drvdata(pdev); |
| 1375 | struct resource *qup_mem, *gsbi_mem; |
| 1376 | |
| 1377 | /* Grab mutex to ensure ongoing transaction is over */ |
| 1378 | mutex_lock(&dev->mlock); |
| 1379 | dev->suspended = 1; |
| 1380 | mutex_unlock(&dev->mlock); |
| 1381 | mutex_destroy(&dev->mlock); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1382 | if (dev->pwr_state != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1383 | qup_i2c_pwr_mgmt(dev, 0); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1384 | qup_i2c_free_gpios(dev); |
| 1385 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1386 | platform_set_drvdata(pdev, NULL); |
| 1387 | if (dev->num_irqs == 3) { |
| 1388 | free_irq(dev->out_irq, dev); |
| 1389 | free_irq(dev->in_irq, dev); |
| 1390 | } |
| 1391 | free_irq(dev->err_irq, dev); |
| 1392 | i2c_del_adapter(&dev->adapter); |
Kiran Gunda | 9aa5e91 | 2012-08-16 18:37:46 +0530 | [diff] [blame] | 1393 | if (!dev->pdata->keep_ahb_clk_on) { |
Kiran Gunda | 9aa5e91 | 2012-08-16 18:37:46 +0530 | [diff] [blame] | 1394 | clk_put(dev->pclk); |
| 1395 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1396 | clk_put(dev->clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1397 | if (dev->gsbi) |
| 1398 | iounmap(dev->gsbi); |
| 1399 | iounmap(dev->base); |
| 1400 | |
| 1401 | pm_runtime_disable(&pdev->dev); |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1402 | pm_runtime_set_suspended(&pdev->dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1403 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1404 | if (!(dev->pdata->use_gsbi_shared_mode)) { |
| 1405 | gsbi_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1406 | "gsbi_qup_i2c_addr"); |
| 1407 | release_mem_region(gsbi_mem->start, resource_size(gsbi_mem)); |
| 1408 | } |
| 1409 | qup_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1410 | "qup_phys_addr"); |
| 1411 | release_mem_region(qup_mem->start, resource_size(qup_mem)); |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1412 | if (dev->dev->of_node) |
| 1413 | kfree(dev->pdata); |
Harini Jayaraman | ee31ae9 | 2011-09-20 18:32:34 -0600 | [diff] [blame] | 1414 | kfree(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | #ifdef CONFIG_PM |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1419 | static int i2c_qup_pm_suspend_runtime(struct device *device) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1420 | { |
| 1421 | struct platform_device *pdev = to_platform_device(device); |
| 1422 | struct qup_i2c_dev *dev = platform_get_drvdata(pdev); |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1423 | dev_dbg(device, "pm_runtime: suspending...\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1424 | /* Grab mutex to ensure ongoing transaction is over */ |
| 1425 | mutex_lock(&dev->mlock); |
| 1426 | dev->suspended = 1; |
| 1427 | mutex_unlock(&dev->mlock); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1428 | if (dev->pwr_state != 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1429 | qup_i2c_pwr_mgmt(dev, 0); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1430 | qup_i2c_free_gpios(dev); |
| 1431 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1432 | return 0; |
| 1433 | } |
| 1434 | |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1435 | static int i2c_qup_pm_resume_runtime(struct device *device) |
| 1436 | { |
| 1437 | struct platform_device *pdev = to_platform_device(device); |
| 1438 | struct qup_i2c_dev *dev = platform_get_drvdata(pdev); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1439 | int ret = 0; |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1440 | dev_dbg(device, "pm_runtime: resuming...\n"); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1441 | if (dev->pwr_state == 0) { |
| 1442 | ret = qup_i2c_request_gpios(dev); |
| 1443 | if (ret != 0) |
| 1444 | return ret; |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1445 | qup_i2c_pwr_mgmt(dev, 1); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1446 | } |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1447 | dev->suspended = 0; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static int qup_i2c_suspend(struct device *device) |
| 1452 | { |
| 1453 | if (!pm_runtime_enabled(device) || !pm_runtime_suspended(device)) { |
| 1454 | dev_dbg(device, "system suspend"); |
| 1455 | i2c_qup_pm_suspend_runtime(device); |
| 1456 | } |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1460 | static int qup_i2c_resume(struct device *device) |
| 1461 | { |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1462 | int ret = 0; |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1463 | if (!pm_runtime_enabled(device) || !pm_runtime_suspended(device)) { |
| 1464 | dev_dbg(device, "system resume"); |
Alok Chauhan | 3acb67e | 2012-12-17 12:32:14 +0530 | [diff] [blame] | 1465 | ret = i2c_qup_pm_resume_runtime(device); |
| 1466 | if (!ret) { |
| 1467 | pm_runtime_mark_last_busy(device); |
| 1468 | pm_request_autosuspend(device); |
| 1469 | } |
| 1470 | return ret; |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1471 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | #endif /* CONFIG_PM */ |
| 1475 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1476 | static const struct dev_pm_ops i2c_qup_dev_pm_ops = { |
| 1477 | SET_SYSTEM_SLEEP_PM_OPS( |
| 1478 | qup_i2c_suspend, |
| 1479 | qup_i2c_resume |
| 1480 | ) |
| 1481 | SET_RUNTIME_PM_OPS( |
Alok Chauhan | ebe211d | 2012-11-19 21:53:12 +0530 | [diff] [blame] | 1482 | i2c_qup_pm_suspend_runtime, |
| 1483 | i2c_qup_pm_resume_runtime, |
| 1484 | NULL |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1485 | ) |
| 1486 | }; |
| 1487 | |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1488 | static struct of_device_id i2c_qup_dt_match[] = { |
| 1489 | { |
| 1490 | .compatible = "qcom,i2c-qup", |
| 1491 | }, |
| 1492 | {} |
| 1493 | }; |
| 1494 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1495 | static struct platform_driver qup_i2c_driver = { |
| 1496 | .probe = qup_i2c_probe, |
| 1497 | .remove = __devexit_p(qup_i2c_remove), |
| 1498 | .driver = { |
| 1499 | .name = "qup_i2c", |
| 1500 | .owner = THIS_MODULE, |
| 1501 | .pm = &i2c_qup_dev_pm_ops, |
Sagar Dharia | 4c5bef3 | 2012-03-14 17:00:29 -0600 | [diff] [blame] | 1502 | .of_match_table = i2c_qup_dt_match, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1503 | }, |
| 1504 | }; |
| 1505 | |
| 1506 | /* QUP may be needed to bring up other drivers */ |
| 1507 | static int __init |
| 1508 | qup_i2c_init_driver(void) |
| 1509 | { |
| 1510 | return platform_driver_register(&qup_i2c_driver); |
| 1511 | } |
| 1512 | arch_initcall(qup_i2c_init_driver); |
| 1513 | |
| 1514 | static void __exit qup_i2c_exit_driver(void) |
| 1515 | { |
| 1516 | platform_driver_unregister(&qup_i2c_driver); |
| 1517 | } |
| 1518 | module_exit(qup_i2c_exit_driver); |
| 1519 | |