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