Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
| 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 | * Qualcomm PMIC8058 driver |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/i2c.h> |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/ratelimit.h> |
| 22 | #include <linux/kthread.h> |
| 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/mfd/pmic8058.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/ratelimit.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/irq.h> |
Abhijeet Dharmapurikar | a3c0d94 | 2011-07-25 17:53:45 -0700 | [diff] [blame] | 30 | #include <linux/syscore_ops.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
| 32 | /* PMIC8058 Revision */ |
| 33 | #define SSBI_REG_REV 0x002 /* PMIC4 revision */ |
| 34 | |
| 35 | /* PMIC8058 IRQ */ |
| 36 | #define SSBI_REG_ADDR_IRQ_BASE 0x1BB |
| 37 | |
| 38 | #define SSBI_REG_ADDR_IRQ_ROOT (SSBI_REG_ADDR_IRQ_BASE + 0) |
| 39 | #define SSBI_REG_ADDR_IRQ_M_STATUS1 (SSBI_REG_ADDR_IRQ_BASE + 1) |
| 40 | #define SSBI_REG_ADDR_IRQ_M_STATUS2 (SSBI_REG_ADDR_IRQ_BASE + 2) |
| 41 | #define SSBI_REG_ADDR_IRQ_M_STATUS3 (SSBI_REG_ADDR_IRQ_BASE + 3) |
| 42 | #define SSBI_REG_ADDR_IRQ_M_STATUS4 (SSBI_REG_ADDR_IRQ_BASE + 4) |
| 43 | #define SSBI_REG_ADDR_IRQ_BLK_SEL (SSBI_REG_ADDR_IRQ_BASE + 5) |
| 44 | #define SSBI_REG_ADDR_IRQ_IT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 6) |
| 45 | #define SSBI_REG_ADDR_IRQ_CONFIG (SSBI_REG_ADDR_IRQ_BASE + 7) |
| 46 | #define SSBI_REG_ADDR_IRQ_RT_STATUS (SSBI_REG_ADDR_IRQ_BASE + 8) |
| 47 | |
| 48 | #define PM8058_IRQF_LVL_SEL 0x01 /* level select */ |
| 49 | #define PM8058_IRQF_MASK_FE 0x02 /* mask falling edge */ |
| 50 | #define PM8058_IRQF_MASK_RE 0x04 /* mask rising edge */ |
| 51 | #define PM8058_IRQF_CLR 0x08 /* clear interrupt */ |
| 52 | #define PM8058_IRQF_BITS_MASK 0x70 |
| 53 | #define PM8058_IRQF_BITS_SHIFT 4 |
| 54 | #define PM8058_IRQF_WRITE 0x80 |
| 55 | |
| 56 | #define PM8058_IRQF_MASK_ALL (PM8058_IRQF_MASK_FE | \ |
| 57 | PM8058_IRQF_MASK_RE) |
| 58 | #define PM8058_IRQF_W_C_M (PM8058_IRQF_WRITE | \ |
| 59 | PM8058_IRQF_CLR | \ |
| 60 | PM8058_IRQF_MASK_ALL) |
| 61 | |
| 62 | /* MISC register */ |
| 63 | #define SSBI_REG_ADDR_MISC 0x1CC |
| 64 | |
| 65 | /* PON CNTL 1 register */ |
| 66 | #define SSBI_REG_ADDR_PON_CNTL_1 0x01C |
| 67 | |
| 68 | #define PM8058_PON_PUP_MASK 0xF0 |
| 69 | |
| 70 | #define PM8058_PON_WD_EN_MASK 0x08 |
| 71 | #define PM8058_PON_WD_EN_RESET 0x08 |
| 72 | #define PM8058_PON_WD_EN_PWR_OFF 0x00 |
| 73 | |
| 74 | /* PON CNTL 4 register */ |
| 75 | #define SSBI_REG_ADDR_PON_CNTL_4 0x98 |
| 76 | #define PM8058_PON_RESET_EN_MASK 0x01 |
| 77 | |
| 78 | /* PON CNTL 5 register */ |
| 79 | #define SSBI_REG_ADDR_PON_CNTL_5 0x7B |
| 80 | #define PM8058_HARD_RESET_EN_MASK 0x08 |
| 81 | |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 82 | /* Regulator master enable addresses */ |
| 83 | #define SSBI_REG_ADDR_VREG_EN_MSM 0x018 |
| 84 | #define SSBI_REG_ADDR_VREG_EN_GRP_5_4 0x1C8 |
| 85 | |
| 86 | /* Regulator control registers for shutdown/reset */ |
| 87 | #define SSBI_REG_ADDR_S0_CTRL 0x004 |
| 88 | #define SSBI_REG_ADDR_S1_CTRL 0x005 |
| 89 | #define SSBI_REG_ADDR_S3_CTRL 0x111 |
| 90 | #define SSBI_REG_ADDR_L21_CTRL 0x120 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | #define SSBI_REG_ADDR_L22_CTRL 0x121 |
| 92 | |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 93 | #define REGULATOR_ENABLE_MASK 0x80 |
| 94 | #define REGULATOR_ENABLE 0x80 |
| 95 | #define REGULATOR_DISABLE 0x00 |
| 96 | #define REGULATOR_PULL_DOWN_MASK 0x40 |
| 97 | #define REGULATOR_PULL_DOWN_EN 0x40 |
| 98 | #define REGULATOR_PULL_DOWN_DIS 0x00 |
| 99 | |
| 100 | /* Buck CTRL register */ |
| 101 | #define SMPS_LEGACY_VREF_SEL 0x20 |
| 102 | #define SMPS_LEGACY_VPROG_MASK 0x1F |
| 103 | #define SMPS_ADVANCED_BAND_MASK 0xC0 |
| 104 | #define SMPS_ADVANCED_BAND_SHIFT 6 |
| 105 | #define SMPS_ADVANCED_VPROG_MASK 0x3F |
| 106 | |
| 107 | /* Buck TEST2 registers for shutdown/reset */ |
| 108 | #define SSBI_REG_ADDR_S0_TEST2 0x084 |
| 109 | #define SSBI_REG_ADDR_S1_TEST2 0x085 |
| 110 | #define SSBI_REG_ADDR_S3_TEST2 0x11A |
| 111 | |
| 112 | #define REGULATOR_BANK_WRITE 0x80 |
| 113 | #define REGULATOR_BANK_MASK 0x70 |
| 114 | #define REGULATOR_BANK_SHIFT 4 |
| 115 | #define REGULATOR_BANK_SEL(n) ((n) << REGULATOR_BANK_SHIFT) |
| 116 | |
| 117 | /* Buck TEST2 register bank 1 */ |
| 118 | #define SMPS_LEGACY_VLOW_SEL 0x01 |
| 119 | |
| 120 | /* Buck TEST2 register bank 7 */ |
| 121 | #define SMPS_ADVANCED_MODE_MASK 0x02 |
| 122 | #define SMPS_ADVANCED_MODE 0x02 |
| 123 | #define SMPS_LEGACY_MODE 0x00 |
| 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | /* SLEEP CNTL register */ |
| 126 | #define SSBI_REG_ADDR_SLEEP_CNTL 0x02B |
| 127 | |
| 128 | #define PM8058_SLEEP_SMPL_EN_MASK 0x04 |
| 129 | #define PM8058_SLEEP_SMPL_EN_RESET 0x04 |
| 130 | #define PM8058_SLEEP_SMPL_EN_PWR_OFF 0x00 |
| 131 | |
| 132 | #define PM8058_SLEEP_SMPL_SEL_MASK 0x03 |
| 133 | #define PM8058_SLEEP_SMPL_SEL_MIN 0 |
| 134 | #define PM8058_SLEEP_SMPL_SEL_MAX 3 |
| 135 | |
Willie Ruan | 6a3c914 | 2011-07-14 16:52:41 -0700 | [diff] [blame] | 136 | /* GP_TEST1 register */ |
| 137 | #define SSBI_REG_ADDR_GP_TEST_1 0x07A |
| 138 | |
| 139 | /* IRQ */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | #define MAX_PM_IRQ 256 |
| 141 | #define MAX_PM_BLOCKS (MAX_PM_IRQ / 8 + 1) |
| 142 | #define MAX_PM_MASTERS (MAX_PM_BLOCKS / 8 + 1) |
| 143 | |
| 144 | struct pm8058_chip { |
| 145 | struct pm8058_platform_data pdata; |
| 146 | |
| 147 | struct i2c_client *dev; |
| 148 | |
| 149 | u8 irqs_allowed[MAX_PM_BLOCKS]; |
| 150 | u8 blocks_allowed[MAX_PM_MASTERS]; |
| 151 | u8 masters_allowed; |
| 152 | int pm_max_irq; |
| 153 | int pm_max_blocks; |
| 154 | int pm_max_masters; |
| 155 | |
| 156 | u8 config[MAX_PM_IRQ]; |
| 157 | u8 bus_unlock_config[MAX_PM_IRQ]; |
| 158 | u8 wake_enable[MAX_PM_IRQ]; |
| 159 | u16 count_wakeable; |
| 160 | |
| 161 | u8 revision; |
| 162 | |
| 163 | struct mutex pm_lock; |
| 164 | }; |
| 165 | |
| 166 | #if defined(CONFIG_DEBUG_FS) |
| 167 | struct pm8058_dbg_device { |
| 168 | struct mutex dbg_mutex; |
| 169 | struct pm8058_chip *pm_chip; |
| 170 | struct dentry *dent; |
| 171 | int addr; |
| 172 | }; |
| 173 | |
| 174 | static struct pm8058_dbg_device *pmic_dbg_device; |
| 175 | #endif |
| 176 | |
| 177 | static struct pm8058_chip *pmic_chip; |
| 178 | |
| 179 | /* Helper Functions */ |
| 180 | DEFINE_RATELIMIT_STATE(pm8058_msg_ratelimit, 60 * HZ, 10); |
| 181 | |
| 182 | static inline int pm8058_can_print(void) |
| 183 | { |
| 184 | return __ratelimit(&pm8058_msg_ratelimit); |
| 185 | } |
| 186 | |
| 187 | static inline int |
| 188 | ssbi_write(struct i2c_client *client, u16 addr, const u8 *buf, size_t len) |
| 189 | { |
| 190 | int rc; |
| 191 | struct i2c_msg msg = { |
| 192 | .addr = addr, |
| 193 | .flags = 0x0, |
| 194 | .buf = (u8 *)buf, |
| 195 | .len = len, |
| 196 | }; |
| 197 | |
| 198 | rc = i2c_transfer(client->adapter, &msg, 1); |
| 199 | return (rc == 1) ? 0 : rc; |
| 200 | } |
| 201 | |
| 202 | static inline int |
| 203 | ssbi_read(struct i2c_client *client, u16 addr, u8 *buf, size_t len) |
| 204 | { |
| 205 | int rc; |
| 206 | struct i2c_msg msg = { |
| 207 | .addr = addr, |
| 208 | .flags = I2C_M_RD, |
| 209 | .buf = buf, |
| 210 | .len = len, |
| 211 | }; |
| 212 | |
| 213 | rc = i2c_transfer(client->adapter, &msg, 1); |
| 214 | return (rc == 1) ? 0 : rc; |
| 215 | } |
| 216 | |
| 217 | static int pm8058_masked_write(u16 addr, u8 val, u8 mask) |
| 218 | { |
| 219 | int rc; |
| 220 | u8 reg; |
| 221 | |
| 222 | if (pmic_chip == NULL) |
| 223 | return -ENODEV; |
| 224 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | rc = ssbi_read(pmic_chip->dev, addr, ®, 1); |
| 226 | if (rc) { |
| 227 | pr_err("%s: ssbi_read(0x%03X) failed: rc=%d\n", __func__, addr, |
| 228 | rc); |
| 229 | goto done; |
| 230 | } |
| 231 | |
| 232 | reg &= ~mask; |
| 233 | reg |= val & mask; |
| 234 | |
| 235 | rc = ssbi_write(pmic_chip->dev, addr, ®, 1); |
| 236 | if (rc) |
| 237 | pr_err("%s: ssbi_write(0x%03X)=0x%02X failed: rc=%d\n", |
| 238 | __func__, addr, reg, rc); |
| 239 | done: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | /* External APIs */ |
| 244 | int pm8058_rev(struct pm8058_chip *chip) |
| 245 | { |
| 246 | if (chip == NULL) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | return chip->revision; |
| 250 | } |
| 251 | EXPORT_SYMBOL(pm8058_rev); |
| 252 | |
| 253 | int pm8058_irq_get_rt_status(struct pm8058_chip *chip, int irq) |
| 254 | { |
| 255 | int rc; |
| 256 | u8 block, bits, bit; |
| 257 | |
| 258 | if (chip == NULL || irq < chip->pdata.irq_base || |
| 259 | irq >= chip->pdata.irq_base + MAX_PM_IRQ) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | irq -= chip->pdata.irq_base; |
| 263 | |
| 264 | block = irq / 8; |
| 265 | bit = irq % 8; |
| 266 | |
| 267 | mutex_lock(&chip->pm_lock); |
| 268 | |
| 269 | rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, &block, 1); |
| 270 | if (rc) { |
| 271 | pr_err("%s: FAIL ssbi_write(): rc=%d (Select Block)\n", |
| 272 | __func__, rc); |
| 273 | goto bail_out; |
| 274 | } |
| 275 | |
| 276 | rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_RT_STATUS, &bits, 1); |
| 277 | if (rc) { |
| 278 | pr_err("%s: FAIL ssbi_read(): rc=%d (Read RT Status)\n", |
| 279 | __func__, rc); |
| 280 | goto bail_out; |
| 281 | } |
| 282 | |
| 283 | rc = (bits & (1 << bit)) ? 1 : 0; |
| 284 | |
| 285 | bail_out: |
| 286 | mutex_unlock(&chip->pm_lock); |
| 287 | |
| 288 | return rc; |
| 289 | } |
| 290 | EXPORT_SYMBOL(pm8058_irq_get_rt_status); |
| 291 | |
| 292 | int pm8058_read(struct pm8058_chip *chip, u16 addr, u8 *values, |
| 293 | unsigned int len) |
| 294 | { |
| 295 | if (chip == NULL) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | return ssbi_read(chip->dev, addr, values, len); |
| 299 | } |
| 300 | EXPORT_SYMBOL(pm8058_read); |
| 301 | |
| 302 | int pm8058_write(struct pm8058_chip *chip, u16 addr, u8 *values, |
| 303 | unsigned int len) |
| 304 | { |
| 305 | if (chip == NULL) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | return ssbi_write(chip->dev, addr, values, len); |
| 309 | } |
| 310 | EXPORT_SYMBOL(pm8058_write); |
| 311 | |
| 312 | int pm8058_misc_control(struct pm8058_chip *chip, int mask, int flag) |
| 313 | { |
| 314 | int rc; |
| 315 | u8 misc; |
| 316 | |
| 317 | if (chip == NULL) |
| 318 | chip = pmic_chip; /* for calls from non child */ |
| 319 | if (chip == NULL) |
| 320 | return -ENODEV; |
| 321 | |
| 322 | mutex_lock(&chip->pm_lock); |
| 323 | |
| 324 | rc = ssbi_read(chip->dev, SSBI_REG_ADDR_MISC, &misc, 1); |
| 325 | if (rc) { |
| 326 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", |
| 327 | __func__, SSBI_REG_ADDR_MISC, rc); |
| 328 | goto get_out; |
| 329 | } |
| 330 | |
| 331 | misc &= ~mask; |
| 332 | misc |= flag; |
| 333 | |
| 334 | rc = ssbi_write(chip->dev, SSBI_REG_ADDR_MISC, &misc, 1); |
| 335 | if (rc) { |
| 336 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 337 | __func__, SSBI_REG_ADDR_MISC, misc, rc); |
| 338 | goto get_out; |
| 339 | } |
| 340 | |
| 341 | get_out: |
| 342 | mutex_unlock(&chip->pm_lock); |
| 343 | |
| 344 | return rc; |
| 345 | } |
| 346 | EXPORT_SYMBOL(pm8058_misc_control); |
| 347 | |
| 348 | /** |
| 349 | * pm8058_smpl_control - enables/disables SMPL detection |
| 350 | * @enable: 0 = shutdown PMIC on power loss, 1 = reset PMIC on power loss |
| 351 | * |
| 352 | * This function enables or disables the Sudden Momentary Power Loss detection |
| 353 | * module. If SMPL detection is enabled, then when a sufficiently long power |
| 354 | * loss event occurs, the PMIC will automatically reset itself. If SMPL |
| 355 | * detection is disabled, then the PMIC will shutdown when power loss occurs. |
| 356 | * |
| 357 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 358 | */ |
| 359 | int pm8058_smpl_control(int enable) |
| 360 | { |
| 361 | return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL, |
| 362 | (enable ? PM8058_SLEEP_SMPL_EN_RESET |
| 363 | : PM8058_SLEEP_SMPL_EN_PWR_OFF), |
| 364 | PM8058_SLEEP_SMPL_EN_MASK); |
| 365 | } |
| 366 | EXPORT_SYMBOL(pm8058_smpl_control); |
| 367 | |
| 368 | /** |
| 369 | * pm8058_smpl_set_delay - sets the SMPL detection time delay |
| 370 | * @delay: enum value corresponding to delay time |
| 371 | * |
| 372 | * This function sets the time delay of the SMPL detection module. If power |
| 373 | * is reapplied within this interval, then the PMIC reset automatically. The |
| 374 | * SMPL detection module must be enabled for this delay time to take effect. |
| 375 | * |
| 376 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 377 | */ |
| 378 | int pm8058_smpl_set_delay(enum pm8058_smpl_delay delay) |
| 379 | { |
| 380 | if (delay < PM8058_SLEEP_SMPL_SEL_MIN |
| 381 | || delay > PM8058_SLEEP_SMPL_SEL_MAX) { |
| 382 | pr_err("%s: invalid delay specified: %d\n", __func__, delay); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | return pm8058_masked_write(SSBI_REG_ADDR_SLEEP_CNTL, delay, |
| 387 | PM8058_SLEEP_SMPL_SEL_MASK); |
| 388 | } |
| 389 | EXPORT_SYMBOL(pm8058_smpl_set_delay); |
| 390 | |
| 391 | /** |
| 392 | * pm8058_watchdog_reset_control - enables/disables watchdog reset detection |
| 393 | * @enable: 0 = shutdown when PS_HOLD goes low, 1 = reset when PS_HOLD goes low |
| 394 | * |
| 395 | * This function enables or disables the PMIC watchdog reset detection feature. |
| 396 | * If watchdog reset detection is enabled, then the PMIC will reset itself |
| 397 | * when PS_HOLD goes low. If it is not enabled, then the PMIC will shutdown |
| 398 | * when PS_HOLD goes low. |
| 399 | * |
| 400 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 401 | */ |
| 402 | int pm8058_watchdog_reset_control(int enable) |
| 403 | { |
| 404 | return pm8058_masked_write(SSBI_REG_ADDR_PON_CNTL_1, |
| 405 | (enable ? PM8058_PON_WD_EN_RESET |
| 406 | : PM8058_PON_WD_EN_PWR_OFF), |
| 407 | PM8058_PON_WD_EN_MASK); |
| 408 | } |
| 409 | EXPORT_SYMBOL(pm8058_watchdog_reset_control); |
| 410 | |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Set an SMPS regulator to be disabled in its CTRL register, but enabled |
| 413 | * in the master enable register. Also set it's pull down enable bit. |
| 414 | * Take care to make sure that the output voltage doesn't change if switching |
| 415 | * from advanced mode to legacy mode. |
| 416 | */ |
| 417 | static int disable_smps_locally_set_pull_down(u16 ctrl_addr, u16 test2_addr, |
| 418 | u16 master_enable_addr, u8 master_enable_bit) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 419 | { |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 420 | int rc = 0; |
| 421 | u8 vref_sel, vlow_sel, band, vprog, bank, reg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | |
| 423 | if (pmic_chip == NULL) |
| 424 | return -ENODEV; |
| 425 | |
David Collins | 0a91160 | 2011-06-15 15:03:13 -0700 | [diff] [blame] | 426 | bank = REGULATOR_BANK_SEL(7); |
| 427 | rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1); |
| 428 | if (rc) { |
| 429 | pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__, |
| 430 | test2_addr, rc); |
| 431 | goto done; |
| 432 | } |
| 433 | |
| 434 | rc = ssbi_read(pmic_chip->dev, test2_addr, ®, 1); |
| 435 | if (rc) { |
| 436 | pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n", |
| 437 | __func__, test2_addr, rc); |
| 438 | goto done; |
| 439 | } |
| 440 | |
| 441 | /* Check if in advanced mode. */ |
| 442 | if ((reg & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE) { |
| 443 | /* Determine current output voltage. */ |
| 444 | rc = ssbi_read(pmic_chip->dev, ctrl_addr, ®, 1); |
| 445 | if (rc) { |
| 446 | pr_err("%s: FAIL pm8058_read(0x%03X): rc=%d\n", |
| 447 | __func__, ctrl_addr, rc); |
| 448 | goto done; |
| 449 | } |
| 450 | |
| 451 | band = (reg & SMPS_ADVANCED_BAND_MASK) |
| 452 | >> SMPS_ADVANCED_BAND_SHIFT; |
| 453 | switch (band) { |
| 454 | case 3: |
| 455 | vref_sel = 0; |
| 456 | vlow_sel = 0; |
| 457 | break; |
| 458 | case 2: |
| 459 | vref_sel = SMPS_LEGACY_VREF_SEL; |
| 460 | vlow_sel = 0; |
| 461 | break; |
| 462 | case 1: |
| 463 | vref_sel = SMPS_LEGACY_VREF_SEL; |
| 464 | vlow_sel = SMPS_LEGACY_VLOW_SEL; |
| 465 | break; |
| 466 | default: |
| 467 | pr_err("%s: regulator already disabled\n", __func__); |
| 468 | return -EPERM; |
| 469 | } |
| 470 | vprog = (reg & SMPS_ADVANCED_VPROG_MASK); |
| 471 | /* Round up if fine step is in use. */ |
| 472 | vprog = (vprog + 1) >> 1; |
| 473 | if (vprog > SMPS_LEGACY_VPROG_MASK) |
| 474 | vprog = SMPS_LEGACY_VPROG_MASK; |
| 475 | |
| 476 | /* Set VLOW_SEL bit. */ |
| 477 | bank = REGULATOR_BANK_SEL(1); |
| 478 | rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1); |
| 479 | if (rc) { |
| 480 | pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", |
| 481 | __func__, test2_addr, rc); |
| 482 | goto done; |
| 483 | } |
| 484 | rc = pm8058_masked_write(test2_addr, |
| 485 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1) |
| 486 | | vlow_sel, |
| 487 | REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK |
| 488 | | SMPS_LEGACY_VLOW_SEL); |
| 489 | if (rc) |
| 490 | goto done; |
| 491 | |
| 492 | /* Switch to legacy mode */ |
| 493 | bank = REGULATOR_BANK_SEL(7); |
| 494 | rc = ssbi_write(pmic_chip->dev, test2_addr, &bank, 1); |
| 495 | if (rc) { |
| 496 | pr_err("%s: FAIL ssbi_write(0x%03X): rc=%d\n", __func__, |
| 497 | test2_addr, rc); |
| 498 | goto done; |
| 499 | } |
| 500 | rc = pm8058_masked_write(test2_addr, |
| 501 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7) |
| 502 | | SMPS_LEGACY_MODE, |
| 503 | REGULATOR_BANK_WRITE | REGULATOR_BANK_MASK |
| 504 | | SMPS_ADVANCED_MODE_MASK); |
| 505 | if (rc) |
| 506 | goto done; |
| 507 | |
| 508 | /* Enable locally, enable pull down, keep voltage the same. */ |
| 509 | rc = pm8058_masked_write(ctrl_addr, |
| 510 | REGULATOR_ENABLE | REGULATOR_PULL_DOWN_EN |
| 511 | | vref_sel | vprog, |
| 512 | REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK |
| 513 | | SMPS_LEGACY_VREF_SEL | SMPS_LEGACY_VPROG_MASK); |
| 514 | if (rc) |
| 515 | goto done; |
| 516 | } |
| 517 | |
| 518 | /* Enable in master control register. */ |
| 519 | rc = pm8058_masked_write(master_enable_addr, master_enable_bit, |
| 520 | master_enable_bit); |
| 521 | if (rc) |
| 522 | goto done; |
| 523 | |
| 524 | /* Disable locally and enable pull down. */ |
| 525 | rc = pm8058_masked_write(ctrl_addr, |
| 526 | REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN, |
| 527 | REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK); |
| 528 | |
| 529 | done: |
| 530 | return rc; |
| 531 | } |
| 532 | |
| 533 | static int disable_ldo_locally_set_pull_down(u16 ctrl_addr, |
| 534 | u16 master_enable_addr, u8 master_enable_bit) |
| 535 | { |
| 536 | int rc; |
| 537 | |
| 538 | /* Enable LDO in master control register. */ |
| 539 | rc = pm8058_masked_write(master_enable_addr, master_enable_bit, |
| 540 | master_enable_bit); |
| 541 | if (rc) |
| 542 | goto done; |
| 543 | |
| 544 | /* Disable LDO in CTRL register and set pull down */ |
| 545 | rc = pm8058_masked_write(ctrl_addr, |
| 546 | REGULATOR_DISABLE | REGULATOR_PULL_DOWN_EN, |
| 547 | REGULATOR_ENABLE_MASK | REGULATOR_PULL_DOWN_MASK); |
| 548 | |
| 549 | done: |
| 550 | return rc; |
| 551 | } |
| 552 | |
| 553 | int pm8058_reset_pwr_off(int reset) |
| 554 | { |
| 555 | int rc; |
| 556 | u8 pon, ctrl, smpl; |
| 557 | |
| 558 | if (pmic_chip == NULL) |
| 559 | return -ENODEV; |
| 560 | |
| 561 | /* When shutting down, enable active pulldowns on important rails. */ |
| 562 | if (!reset) { |
| 563 | /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */ |
| 564 | disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S0_CTRL, |
| 565 | SSBI_REG_ADDR_S0_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(7)); |
| 566 | disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S1_CTRL, |
| 567 | SSBI_REG_ADDR_S1_TEST2, SSBI_REG_ADDR_VREG_EN_MSM, BIT(6)); |
| 568 | disable_smps_locally_set_pull_down(SSBI_REG_ADDR_S3_CTRL, |
| 569 | SSBI_REG_ADDR_S3_TEST2, SSBI_REG_ADDR_VREG_EN_GRP_5_4, |
| 570 | BIT(7) | BIT(4)); |
| 571 | /* Disable LDO 21 locally and set pulldown enable bit. */ |
| 572 | disable_ldo_locally_set_pull_down(SSBI_REG_ADDR_L21_CTRL, |
| 573 | SSBI_REG_ADDR_VREG_EN_GRP_5_4, BIT(1)); |
| 574 | } |
| 575 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | /* Set regulator L22 to 1.225V in high power mode. */ |
| 577 | rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1); |
| 578 | if (rc) { |
| 579 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", __func__, |
| 580 | SSBI_REG_ADDR_L22_CTRL, rc); |
| 581 | goto get_out3; |
| 582 | } |
| 583 | /* Leave pull-down state intact. */ |
| 584 | ctrl &= 0x40; |
| 585 | ctrl |= 0x93; |
| 586 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_L22_CTRL, &ctrl, 1); |
| 587 | if (rc) |
| 588 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", __func__, |
| 589 | SSBI_REG_ADDR_L22_CTRL, ctrl, rc); |
| 590 | |
| 591 | get_out3: |
| 592 | if (!reset) { |
| 593 | /* Only modify the SLEEP_CNTL reg if shutdown is desired. */ |
| 594 | rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL, |
| 595 | &smpl, 1); |
| 596 | if (rc) { |
| 597 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", |
| 598 | __func__, SSBI_REG_ADDR_SLEEP_CNTL, rc); |
| 599 | goto get_out2; |
| 600 | } |
| 601 | |
| 602 | smpl &= ~PM8058_SLEEP_SMPL_EN_MASK; |
| 603 | smpl |= PM8058_SLEEP_SMPL_EN_PWR_OFF; |
| 604 | |
| 605 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_SLEEP_CNTL, |
| 606 | &smpl, 1); |
| 607 | if (rc) |
| 608 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 609 | __func__, SSBI_REG_ADDR_SLEEP_CNTL, smpl, rc); |
| 610 | } |
| 611 | |
| 612 | get_out2: |
| 613 | rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1); |
| 614 | if (rc) { |
| 615 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", |
| 616 | __func__, SSBI_REG_ADDR_PON_CNTL_1, rc); |
| 617 | goto get_out; |
| 618 | } |
| 619 | |
| 620 | pon &= ~PM8058_PON_WD_EN_MASK; |
| 621 | pon |= reset ? PM8058_PON_WD_EN_RESET : PM8058_PON_WD_EN_PWR_OFF; |
| 622 | |
| 623 | /* Enable all pullups */ |
| 624 | pon |= PM8058_PON_PUP_MASK; |
| 625 | |
| 626 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_1, &pon, 1); |
| 627 | if (rc) { |
| 628 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 629 | __func__, SSBI_REG_ADDR_PON_CNTL_1, pon, rc); |
| 630 | goto get_out; |
| 631 | } |
| 632 | |
| 633 | get_out: |
| 634 | return rc; |
| 635 | } |
| 636 | EXPORT_SYMBOL(pm8058_reset_pwr_off); |
| 637 | |
Willie Ruan | 6a3c914 | 2011-07-14 16:52:41 -0700 | [diff] [blame] | 638 | /** |
| 639 | * pm8058_stay_on - enables stay_on feature |
| 640 | * |
| 641 | * PMIC stay-on feature allows PMIC to ignore MSM PS_HOLD=low |
| 642 | * signal so that some special functions like debugging could be |
| 643 | * performed. |
| 644 | * |
| 645 | * This feature should not be used in any product release. |
| 646 | * |
| 647 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 648 | */ |
| 649 | int pm8058_stay_on(void) |
| 650 | { |
| 651 | u8 ctrl = 0x92; |
| 652 | int rc; |
| 653 | |
| 654 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_GP_TEST_1, &ctrl, 1); |
| 655 | pr_info("%s: set stay-on: rc = %d\n", __func__, rc); |
| 656 | return rc; |
| 657 | } |
| 658 | EXPORT_SYMBOL(pm8058_stay_on); |
| 659 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 660 | /* |
| 661 | power on hard reset configuration |
| 662 | config = DISABLE_HARD_RESET to disable hard reset |
| 663 | = SHUTDOWN_ON_HARD_RESET to turn off the system on hard reset |
| 664 | = RESTART_ON_HARD_RESET to restart the system on hard reset |
| 665 | */ |
| 666 | int pm8058_hard_reset_config(enum pon_config config) |
| 667 | { |
| 668 | int rc, ret; |
| 669 | u8 pon, pon_5; |
| 670 | |
| 671 | if (config >= MAX_PON_CONFIG) |
| 672 | return -EINVAL; |
| 673 | |
| 674 | if (pmic_chip == NULL) |
| 675 | return -ENODEV; |
| 676 | |
| 677 | mutex_lock(&pmic_chip->pm_lock); |
| 678 | |
| 679 | rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1); |
| 680 | if (rc) { |
| 681 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", |
| 682 | __func__, SSBI_REG_ADDR_PON_CNTL_5, rc); |
| 683 | mutex_unlock(&pmic_chip->pm_lock); |
| 684 | return rc; |
| 685 | } |
| 686 | |
| 687 | pon_5 = pon; |
| 688 | (config != DISABLE_HARD_RESET) ? (pon |= PM8058_HARD_RESET_EN_MASK) : |
| 689 | (pon &= ~PM8058_HARD_RESET_EN_MASK); |
| 690 | |
| 691 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon, 1); |
| 692 | if (rc) { |
| 693 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 694 | __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, rc); |
| 695 | mutex_unlock(&pmic_chip->pm_lock); |
| 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | if (config == DISABLE_HARD_RESET) { |
| 700 | mutex_unlock(&pmic_chip->pm_lock); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | rc = ssbi_read(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1); |
| 705 | if (rc) { |
| 706 | pr_err("%s: FAIL ssbi_read(0x%x): rc=%d\n", |
| 707 | __func__, SSBI_REG_ADDR_PON_CNTL_4, rc); |
| 708 | goto err_restore_pon_5; |
| 709 | } |
| 710 | |
| 711 | (config == RESTART_ON_HARD_RESET) ? (pon |= PM8058_PON_RESET_EN_MASK) : |
| 712 | (pon &= ~PM8058_PON_RESET_EN_MASK); |
| 713 | |
| 714 | rc = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_4, &pon, 1); |
| 715 | if (rc) { |
| 716 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 717 | __func__, SSBI_REG_ADDR_PON_CNTL_4, pon, rc); |
| 718 | goto err_restore_pon_5; |
| 719 | } |
| 720 | mutex_unlock(&pmic_chip->pm_lock); |
| 721 | return 0; |
| 722 | |
| 723 | err_restore_pon_5: |
| 724 | ret = ssbi_write(pmic_chip->dev, SSBI_REG_ADDR_PON_CNTL_5, &pon_5, 1); |
| 725 | if (ret) |
| 726 | pr_err("%s: FAIL ssbi_write(0x%x)=0x%x: rc=%d\n", |
| 727 | __func__, SSBI_REG_ADDR_PON_CNTL_5, pon, ret); |
| 728 | mutex_unlock(&pmic_chip->pm_lock); |
| 729 | return rc; |
| 730 | } |
| 731 | EXPORT_SYMBOL(pm8058_hard_reset_config); |
| 732 | |
| 733 | /* Internal functions */ |
| 734 | static inline int |
| 735 | pm8058_config_irq(struct pm8058_chip *chip, u8 *bp, u8 *cp) |
| 736 | { |
| 737 | int rc; |
| 738 | |
| 739 | rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp, 1); |
| 740 | if (rc) { |
| 741 | pr_err("%s: ssbi_write: rc=%d (Select block)\n", |
| 742 | __func__, rc); |
| 743 | goto bail_out; |
| 744 | } |
| 745 | |
| 746 | rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_CONFIG, cp, 1); |
| 747 | if (rc) |
| 748 | pr_err("%s: ssbi_write: rc=%d (Configure IRQ)\n", |
| 749 | __func__, rc); |
| 750 | |
| 751 | bail_out: |
| 752 | return rc; |
| 753 | } |
| 754 | |
| 755 | static void pm8058_irq_mask(struct irq_data *data) |
| 756 | { |
| 757 | int master, irq_bit; |
| 758 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 759 | u8 block, config; |
| 760 | unsigned int irq = data->irq; |
| 761 | |
| 762 | irq -= chip->pdata.irq_base; |
| 763 | block = irq / 8; |
| 764 | master = block / 8; |
| 765 | irq_bit = irq % 8; |
| 766 | |
| 767 | chip->irqs_allowed[block] &= ~(1 << irq_bit); |
| 768 | if (!chip->irqs_allowed[block]) { |
| 769 | chip->blocks_allowed[master] &= ~(1 << (block % 8)); |
| 770 | |
| 771 | if (!chip->blocks_allowed[master]) |
| 772 | chip->masters_allowed &= ~(1 << master); |
| 773 | } |
| 774 | |
| 775 | config = PM8058_IRQF_WRITE | chip->config[irq] | |
| 776 | PM8058_IRQF_MASK_FE | PM8058_IRQF_MASK_RE; |
| 777 | chip->bus_unlock_config[irq] = config; |
| 778 | } |
| 779 | |
| 780 | static void pm8058_irq_unmask(struct irq_data *data) |
| 781 | { |
| 782 | int master, irq_bit; |
| 783 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 784 | u8 block, config, old_irqs_allowed, old_blocks_allowed; |
| 785 | unsigned int irq = data->irq; |
| 786 | |
| 787 | irq -= chip->pdata.irq_base; |
| 788 | block = irq / 8; |
| 789 | master = block / 8; |
| 790 | irq_bit = irq % 8; |
| 791 | |
| 792 | old_irqs_allowed = chip->irqs_allowed[block]; |
Abhijeet Dharmapurikar | a346e2c | 2011-08-30 18:17:51 -0700 | [diff] [blame] | 793 | if (old_irqs_allowed & (1 << irq_bit)) { |
| 794 | pr_debug("%s: no need to enable an already enabled irq=%d\n", |
| 795 | __func__, irq + chip->pdata.irq_base); |
| 796 | return; |
| 797 | } |
| 798 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 799 | chip->irqs_allowed[block] |= 1 << irq_bit; |
| 800 | if (!old_irqs_allowed) { |
| 801 | master = block / 8; |
| 802 | |
| 803 | old_blocks_allowed = chip->blocks_allowed[master]; |
| 804 | chip->blocks_allowed[master] |= 1 << (block % 8); |
| 805 | |
| 806 | if (!old_blocks_allowed) |
| 807 | chip->masters_allowed |= 1 << master; |
| 808 | } |
| 809 | |
| 810 | config = PM8058_IRQF_WRITE | chip->config[irq]; |
| 811 | chip->bus_unlock_config[irq] = config; |
| 812 | } |
| 813 | |
| 814 | static void pm8058_irq_ack(struct irq_data *data) |
| 815 | { |
| 816 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 817 | u8 block, config; |
| 818 | unsigned int irq = data->irq; |
| 819 | |
| 820 | irq -= chip->pdata.irq_base; |
| 821 | block = irq / 8; |
| 822 | |
| 823 | config = PM8058_IRQF_WRITE | chip->config[irq] | PM8058_IRQF_CLR; |
| 824 | /* Keep the mask */ |
| 825 | if (!(chip->irqs_allowed[block] & (1 << (irq % 8)))) |
| 826 | config |= PM8058_IRQF_MASK_FE | PM8058_IRQF_MASK_RE; |
| 827 | chip->bus_unlock_config[irq] = config; |
| 828 | } |
| 829 | |
| 830 | static int pm8058_irq_set_type(struct irq_data *data, unsigned int flow_type) |
| 831 | { |
| 832 | int master, irq_bit; |
| 833 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 834 | u8 block, config; |
| 835 | unsigned int irq = data->irq; |
| 836 | |
| 837 | irq -= chip->pdata.irq_base; |
| 838 | if (irq > chip->pm_max_irq) { |
| 839 | chip->pm_max_irq = irq; |
| 840 | chip->pm_max_blocks = |
| 841 | chip->pm_max_irq / 8 + 1; |
| 842 | chip->pm_max_masters = |
| 843 | chip->pm_max_blocks / 8 + 1; |
| 844 | } |
| 845 | block = irq / 8; |
| 846 | master = block / 8; |
| 847 | irq_bit = irq % 8; |
| 848 | |
| 849 | chip->config[irq] = (irq_bit << PM8058_IRQF_BITS_SHIFT) | |
| 850 | PM8058_IRQF_MASK_RE | PM8058_IRQF_MASK_FE; |
| 851 | if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { |
| 852 | if (flow_type & IRQF_TRIGGER_RISING) |
| 853 | chip->config[irq] &= ~PM8058_IRQF_MASK_RE; |
| 854 | if (flow_type & IRQF_TRIGGER_FALLING) |
| 855 | chip->config[irq] &= ~PM8058_IRQF_MASK_FE; |
| 856 | } else { |
| 857 | chip->config[irq] |= PM8058_IRQF_LVL_SEL; |
| 858 | |
| 859 | if (flow_type & IRQF_TRIGGER_HIGH) |
| 860 | chip->config[irq] &= ~PM8058_IRQF_MASK_RE; |
| 861 | else |
| 862 | chip->config[irq] &= ~PM8058_IRQF_MASK_FE; |
| 863 | } |
| 864 | |
| 865 | config = PM8058_IRQF_WRITE | chip->config[irq] | PM8058_IRQF_CLR; |
| 866 | chip->bus_unlock_config[irq] = config; |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static int pm8058_irq_set_wake(struct irq_data *data, unsigned int on) |
| 871 | { |
| 872 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 873 | unsigned int irq = data->irq; |
| 874 | |
| 875 | irq -= chip->pdata.irq_base; |
| 876 | if (on) { |
| 877 | if (!chip->wake_enable[irq]) { |
| 878 | chip->wake_enable[irq] = 1; |
| 879 | chip->count_wakeable++; |
| 880 | } |
| 881 | } else { |
| 882 | if (chip->wake_enable[irq]) { |
| 883 | chip->wake_enable[irq] = 0; |
| 884 | chip->count_wakeable--; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static void pm8058_irq_bus_lock(struct irq_data *data) |
| 892 | { |
| 893 | u8 block; |
| 894 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 895 | unsigned int irq = data->irq; |
| 896 | |
| 897 | irq -= chip->pdata.irq_base; |
| 898 | block = irq / 8; |
| 899 | chip->bus_unlock_config[irq] = 0; |
| 900 | |
| 901 | mutex_lock(&chip->pm_lock); |
| 902 | } |
| 903 | |
| 904 | static void pm8058_irq_bus_sync_unlock(struct irq_data *data) |
| 905 | { |
| 906 | u8 block, config; |
| 907 | struct pm8058_chip *chip = irq_data_get_irq_chip_data(data); |
| 908 | unsigned int irq = data->irq; |
| 909 | |
| 910 | irq -= chip->pdata.irq_base; |
| 911 | block = irq / 8; |
| 912 | config = chip->bus_unlock_config[irq]; |
| 913 | /* dont waste cpu cycles if we dont have data to write */ |
| 914 | if (config) |
| 915 | pm8058_config_irq(chip, &block, &config); |
| 916 | mutex_unlock(&chip->pm_lock); |
| 917 | } |
| 918 | |
| 919 | static inline int |
| 920 | pm8058_read_root(struct pm8058_chip *chip, u8 *rp) |
| 921 | { |
| 922 | int rc; |
| 923 | |
| 924 | rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_ROOT, rp, 1); |
| 925 | if (rc) { |
| 926 | pr_err("%s: FAIL ssbi_read(): rc=%d (Read Root)\n", |
| 927 | __func__, rc); |
| 928 | *rp = 0; |
| 929 | } |
| 930 | |
| 931 | return rc; |
| 932 | } |
| 933 | |
| 934 | static inline int |
| 935 | pm8058_read_master(struct pm8058_chip *chip, u8 m, u8 *bp) |
| 936 | { |
| 937 | int rc; |
| 938 | |
| 939 | rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_M_STATUS1 + m, bp, 1); |
| 940 | if (rc) { |
| 941 | pr_err("%s: FAIL ssbi_read(): rc=%d (Read Master)\n", |
| 942 | __func__, rc); |
| 943 | *bp = 0; |
| 944 | } |
| 945 | |
| 946 | return rc; |
| 947 | } |
| 948 | |
| 949 | static inline int |
| 950 | pm8058_read_block(struct pm8058_chip *chip, u8 *bp, u8 *ip) |
| 951 | { |
| 952 | int rc; |
| 953 | |
| 954 | rc = ssbi_write(chip->dev, SSBI_REG_ADDR_IRQ_BLK_SEL, bp, 1); |
| 955 | if (rc) { |
| 956 | pr_err("%s: FAIL ssbi_write(): rc=%d (Select Block)\n", |
| 957 | __func__, rc); |
| 958 | *bp = 0; |
| 959 | goto bail_out; |
| 960 | } |
| 961 | |
| 962 | rc = ssbi_read(chip->dev, SSBI_REG_ADDR_IRQ_IT_STATUS, ip, 1); |
| 963 | if (rc) |
| 964 | pr_err("%s: FAIL ssbi_read(): rc=%d (Read Status)\n", |
| 965 | __func__, rc); |
| 966 | |
| 967 | bail_out: |
| 968 | return rc; |
| 969 | } |
| 970 | |
| 971 | static irqreturn_t pm8058_isr_thread(int irq_requested, void *data) |
| 972 | { |
| 973 | struct pm8058_chip *chip = data; |
| 974 | int i, j, k; |
| 975 | u8 root, block, config, bits; |
| 976 | u8 blocks[MAX_PM_MASTERS]; |
| 977 | int masters = 0, irq, handled = 0, spurious = 0; |
| 978 | u16 irqs_to_handle[MAX_PM_IRQ]; |
| 979 | |
| 980 | mutex_lock(&chip->pm_lock); |
| 981 | |
| 982 | /* Read root for masters */ |
| 983 | if (pm8058_read_root(chip, &root)) |
| 984 | goto bail_out; |
| 985 | |
| 986 | masters = root >> 1; |
| 987 | |
| 988 | if (!(masters & chip->masters_allowed) || |
| 989 | (masters & ~chip->masters_allowed)) { |
| 990 | spurious = 1000000; |
| 991 | } |
| 992 | |
| 993 | /* Read allowed masters for blocks. */ |
| 994 | for (i = 0; i < chip->pm_max_masters; i++) { |
| 995 | if (masters & (1 << i)) { |
| 996 | if (pm8058_read_master(chip, i, &blocks[i])) |
| 997 | goto bail_out; |
| 998 | |
| 999 | if (!blocks[i]) { |
| 1000 | if (pm8058_can_print()) |
| 1001 | pr_err("%s: Spurious master: %d " |
| 1002 | "(blocks=0)", __func__, i); |
| 1003 | spurious += 10000; |
| 1004 | } |
| 1005 | } else |
| 1006 | blocks[i] = 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* Select block, read status and call isr */ |
| 1010 | for (i = 0; i < chip->pm_max_masters; i++) { |
| 1011 | if (!blocks[i]) |
| 1012 | continue; |
| 1013 | |
| 1014 | for (j = 0; j < 8; j++) { |
| 1015 | if (!(blocks[i] & (1 << j))) |
| 1016 | continue; |
| 1017 | |
| 1018 | block = i * 8 + j; /* block # */ |
| 1019 | if (pm8058_read_block(chip, &block, &bits)) |
| 1020 | goto bail_out; |
| 1021 | |
| 1022 | if (!bits) { |
| 1023 | if (pm8058_can_print()) |
| 1024 | pr_err("%s: Spurious block: " |
| 1025 | "[master, block]=[%d, %d] " |
| 1026 | "(bits=0)\n", __func__, i, j); |
| 1027 | spurious += 100; |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | /* Check IRQ bits */ |
| 1032 | for (k = 0; k < 8; k++) { |
| 1033 | if (!(bits & (1 << k))) |
| 1034 | continue; |
| 1035 | |
| 1036 | /* Check spurious interrupts */ |
| 1037 | if (((1 << i) & chip->masters_allowed) && |
| 1038 | (blocks[i] & chip->blocks_allowed[i]) && |
| 1039 | (bits & chip->irqs_allowed[block])) { |
| 1040 | |
| 1041 | /* Found one */ |
| 1042 | irq = block * 8 + k; |
| 1043 | irqs_to_handle[handled] = irq + |
| 1044 | chip->pdata.irq_base; |
| 1045 | handled++; |
| 1046 | } else { |
| 1047 | /* Clear and mask wrong one */ |
| 1048 | config = PM8058_IRQF_W_C_M | |
| 1049 | (k << PM8058_IRQF_BITS_SHIFT); |
| 1050 | |
| 1051 | pm8058_config_irq(chip, |
| 1052 | &block, &config); |
| 1053 | |
| 1054 | if (pm8058_can_print()) |
| 1055 | pr_err("%s: Spurious IRQ: " |
| 1056 | "[master, block, bit]=" |
| 1057 | "[%d, %d (%d), %d]\n", |
| 1058 | __func__, |
| 1059 | i, j, block, k); |
| 1060 | spurious++; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | } |
| 1066 | |
| 1067 | bail_out: |
| 1068 | |
| 1069 | mutex_unlock(&chip->pm_lock); |
| 1070 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1071 | for (i = 0; i < handled; i++) { |
Abhijeet Dharmapurikar | bb23a05 | 2011-09-19 10:44:53 -0700 | [diff] [blame] | 1072 | int pmic_irq = irqs_to_handle[i] - chip->pdata.irq_base; |
| 1073 | |
| 1074 | /* ack the interrupt first */ |
| 1075 | block = pmic_irq / 8 ; |
| 1076 | config = PM8058_IRQF_WRITE | chip->config[pmic_irq] |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1077 | | PM8058_IRQF_CLR; |
| 1078 | pm8058_config_irq(chip, &block, &config); |
Abhijeet Dharmapurikar | bb23a05 | 2011-09-19 10:44:53 -0700 | [diff] [blame] | 1079 | |
| 1080 | /* calle the action handler */ |
| 1081 | handle_nested_irq(irqs_to_handle[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | if (spurious) { |
| 1085 | if (!pm8058_can_print()) |
| 1086 | return IRQ_HANDLED; |
| 1087 | |
| 1088 | pr_err("%s: spurious = %d (handled = %d)\n", |
| 1089 | __func__, spurious, handled); |
| 1090 | pr_err(" root = 0x%x (masters_allowed<<1 = 0x%x)\n", |
| 1091 | root, chip->masters_allowed << 1); |
| 1092 | for (i = 0; i < chip->pm_max_masters; i++) { |
| 1093 | if (masters & (1 << i)) |
| 1094 | pr_err(" blocks[%d]=0x%x, " |
| 1095 | "allowed[%d]=0x%x\n", |
| 1096 | i, blocks[i], |
| 1097 | i, chip->blocks_allowed[i]); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | return IRQ_HANDLED; |
| 1102 | } |
| 1103 | |
| 1104 | #if defined(CONFIG_DEBUG_FS) |
| 1105 | |
| 1106 | static int check_addr(int addr, const char *func_name) |
| 1107 | { |
| 1108 | if (addr < 0 || addr > 0x3FF) { |
| 1109 | pr_err("%s: PMIC 8058 register address is invalid: %d\n", |
| 1110 | func_name, addr); |
| 1111 | return -EINVAL; |
| 1112 | } |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | static int data_set(void *data, u64 val) |
| 1117 | { |
| 1118 | struct pm8058_dbg_device *dbgdev = data; |
| 1119 | u8 reg = val; |
| 1120 | int rc; |
| 1121 | |
| 1122 | mutex_lock(&dbgdev->dbg_mutex); |
| 1123 | |
| 1124 | rc = check_addr(dbgdev->addr, __func__); |
| 1125 | if (rc) |
| 1126 | goto done; |
| 1127 | |
| 1128 | rc = pm8058_write(dbgdev->pm_chip, dbgdev->addr, ®, 1); |
| 1129 | |
| 1130 | if (rc) |
| 1131 | pr_err("%s: FAIL pm8058_write(0x%03X)=0x%02X: rc=%d\n", |
| 1132 | __func__, dbgdev->addr, reg, rc); |
| 1133 | done: |
| 1134 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
| 1138 | static int data_get(void *data, u64 *val) |
| 1139 | { |
| 1140 | struct pm8058_dbg_device *dbgdev = data; |
| 1141 | int rc; |
| 1142 | u8 reg; |
| 1143 | |
| 1144 | mutex_lock(&dbgdev->dbg_mutex); |
| 1145 | |
| 1146 | rc = check_addr(dbgdev->addr, __func__); |
| 1147 | if (rc) |
| 1148 | goto done; |
| 1149 | |
| 1150 | rc = pm8058_read(dbgdev->pm_chip, dbgdev->addr, ®, 1); |
| 1151 | |
| 1152 | if (rc) { |
| 1153 | pr_err("%s: FAIL pm8058_read(0x%03X)=0x%02X: rc=%d\n", |
| 1154 | __func__, dbgdev->addr, reg, rc); |
| 1155 | goto done; |
| 1156 | } |
| 1157 | |
| 1158 | *val = reg; |
| 1159 | done: |
| 1160 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1161 | return rc; |
| 1162 | } |
| 1163 | |
| 1164 | DEFINE_SIMPLE_ATTRIBUTE(dbg_data_fops, data_get, data_set, "0x%02llX\n"); |
| 1165 | |
| 1166 | static int addr_set(void *data, u64 val) |
| 1167 | { |
| 1168 | struct pm8058_dbg_device *dbgdev = data; |
| 1169 | int rc; |
| 1170 | |
| 1171 | rc = check_addr(val, __func__); |
| 1172 | if (rc) |
| 1173 | return rc; |
| 1174 | |
| 1175 | mutex_lock(&dbgdev->dbg_mutex); |
| 1176 | dbgdev->addr = val; |
| 1177 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | static int addr_get(void *data, u64 *val) |
| 1183 | { |
| 1184 | struct pm8058_dbg_device *dbgdev = data; |
| 1185 | int rc; |
| 1186 | |
| 1187 | mutex_lock(&dbgdev->dbg_mutex); |
| 1188 | |
| 1189 | rc = check_addr(dbgdev->addr, __func__); |
| 1190 | if (rc) { |
| 1191 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1192 | return rc; |
| 1193 | } |
| 1194 | *val = dbgdev->addr; |
| 1195 | |
| 1196 | mutex_unlock(&dbgdev->dbg_mutex); |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | DEFINE_SIMPLE_ATTRIBUTE(dbg_addr_fops, addr_get, addr_set, "0x%03llX\n"); |
| 1202 | |
| 1203 | static int __devinit pmic8058_dbg_probe(struct pm8058_chip *chip) |
| 1204 | { |
| 1205 | struct pm8058_dbg_device *dbgdev; |
| 1206 | struct dentry *dent; |
| 1207 | struct dentry *temp; |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1208 | int rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1209 | |
| 1210 | if (chip == NULL) { |
| 1211 | pr_err("%s: no parent data passed in.\n", __func__); |
| 1212 | return -EINVAL; |
| 1213 | } |
| 1214 | |
| 1215 | dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL); |
| 1216 | if (dbgdev == NULL) { |
| 1217 | pr_err("%s: kzalloc() failed.\n", __func__); |
| 1218 | return -ENOMEM; |
| 1219 | } |
| 1220 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1221 | dbgdev->pm_chip = chip; |
| 1222 | dbgdev->addr = -1; |
| 1223 | |
| 1224 | dent = debugfs_create_dir("pm8058-dbg", NULL); |
| 1225 | if (dent == NULL || IS_ERR(dent)) { |
| 1226 | pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n", |
| 1227 | __func__, (unsigned)dent); |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1228 | rc = PTR_ERR(dent); |
| 1229 | goto dir_error; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dent, |
| 1233 | dbgdev, &dbg_addr_fops); |
| 1234 | if (temp == NULL || IS_ERR(temp)) { |
| 1235 | pr_err("%s: ERR debugfs_create_file: dent=0x%X\n", |
| 1236 | __func__, (unsigned)temp); |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1237 | rc = PTR_ERR(temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1238 | goto debug_error; |
| 1239 | } |
| 1240 | |
| 1241 | temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dent, |
| 1242 | dbgdev, &dbg_data_fops); |
| 1243 | if (temp == NULL || IS_ERR(temp)) { |
| 1244 | pr_err("%s: ERR debugfs_create_file: dent=0x%X\n", |
| 1245 | __func__, (unsigned)temp); |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1246 | rc = PTR_ERR(temp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1247 | goto debug_error; |
| 1248 | } |
| 1249 | |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1250 | mutex_init(&dbgdev->dbg_mutex); |
| 1251 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1252 | dbgdev->dent = dent; |
| 1253 | |
| 1254 | pmic_dbg_device = dbgdev; |
| 1255 | |
| 1256 | return 0; |
| 1257 | |
| 1258 | debug_error: |
| 1259 | debugfs_remove_recursive(dent); |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1260 | dir_error: |
| 1261 | kfree(dbgdev); |
| 1262 | |
| 1263 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static int __devexit pmic8058_dbg_remove(void) |
| 1267 | { |
| 1268 | if (pmic_dbg_device) { |
| 1269 | debugfs_remove_recursive(pmic_dbg_device->dent); |
Jay Chokshi | 898340f | 2011-09-29 17:19:31 -0700 | [diff] [blame^] | 1270 | mutex_destroy(&pmic_dbg_device->dbg_mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1271 | kfree(pmic_dbg_device); |
| 1272 | } |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | #else |
| 1277 | |
| 1278 | static int __devinit pmic8058_dbg_probe(struct pm8058_chip *chip) |
| 1279 | { |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int __devexit pmic8058_dbg_remove(void) |
| 1284 | { |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | #endif |
| 1289 | |
| 1290 | static struct irq_chip pm8058_irq_chip = { |
| 1291 | .name = "pm8058", |
| 1292 | .irq_ack = pm8058_irq_ack, |
| 1293 | .irq_mask = pm8058_irq_mask, |
| 1294 | .irq_unmask = pm8058_irq_unmask, |
| 1295 | .irq_set_type = pm8058_irq_set_type, |
| 1296 | .irq_set_wake = pm8058_irq_set_wake, |
| 1297 | .irq_bus_lock = pm8058_irq_bus_lock, |
| 1298 | .irq_bus_sync_unlock = pm8058_irq_bus_sync_unlock, |
| 1299 | }; |
| 1300 | |
Abhijeet Dharmapurikar | a3c0d94 | 2011-07-25 17:53:45 -0700 | [diff] [blame] | 1301 | static int pm8058_suspend(void) |
| 1302 | { |
| 1303 | struct pm8058_chip *chip = pmic_chip; |
| 1304 | struct irq_data *data; |
| 1305 | int i; |
| 1306 | |
| 1307 | for (i = 0; i < MAX_PM_IRQ; i++) { |
| 1308 | if (chip->config[i] && !chip->wake_enable[i]) { |
| 1309 | if (!((chip->config[i] & PM8058_IRQF_MASK_ALL) |
| 1310 | == PM8058_IRQF_MASK_ALL)) { |
| 1311 | data = irq_get_irq_data(i + |
| 1312 | chip->pdata.irq_base); |
| 1313 | pm8058_irq_bus_lock(data); |
| 1314 | pm8058_irq_mask(data); |
| 1315 | pm8058_irq_bus_sync_unlock(data); |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | if (!chip->count_wakeable) |
| 1321 | disable_irq(chip->dev->irq); |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | extern int msm_show_resume_irq_mask; |
| 1327 | |
| 1328 | static void pm8058_show_resume_irq(void) |
| 1329 | { |
| 1330 | u8 block, bits; |
| 1331 | int i; |
| 1332 | struct pm8058_chip *chip = pmic_chip; |
| 1333 | |
| 1334 | if (!msm_show_resume_irq_mask) |
| 1335 | return; |
| 1336 | |
| 1337 | for (i = 0; i < MAX_PM_IRQ; i++) { |
| 1338 | if (chip->wake_enable[i]) { |
| 1339 | block = i / 8; |
| 1340 | if (!pm8058_read_block(chip, &block, &bits)) { |
| 1341 | if (bits & (1 << (i & 0x7))) |
| 1342 | pr_warning("%s:%d triggered\n", |
| 1343 | __func__, i + chip->pdata.irq_base); |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | static void pm8058_resume(void) |
| 1350 | { |
| 1351 | struct pm8058_chip *chip = pmic_chip; |
| 1352 | struct irq_data *data; |
| 1353 | int i; |
| 1354 | |
| 1355 | pm8058_show_resume_irq(); |
| 1356 | |
| 1357 | for (i = 0; i < MAX_PM_IRQ; i++) { |
| 1358 | if (chip->config[i] && !chip->wake_enable[i]) { |
| 1359 | if (!((chip->config[i] & PM8058_IRQF_MASK_ALL) |
| 1360 | == PM8058_IRQF_MASK_ALL)) { |
| 1361 | data = irq_get_irq_data(i + |
| 1362 | chip->pdata.irq_base); |
| 1363 | pm8058_irq_bus_lock(data); |
| 1364 | pm8058_irq_unmask(data); |
| 1365 | pm8058_irq_bus_sync_unlock(data); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | if (!chip->count_wakeable) |
| 1371 | enable_irq(chip->dev->irq); |
| 1372 | } |
| 1373 | |
| 1374 | static struct syscore_ops pm8058_pm = { |
| 1375 | .suspend = pm8058_suspend, |
| 1376 | .resume = pm8058_resume, |
| 1377 | }; |
| 1378 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1379 | static int pm8058_probe(struct i2c_client *client, |
| 1380 | const struct i2c_device_id *id) |
| 1381 | { |
| 1382 | int i, rc; |
| 1383 | struct pm8058_platform_data *pdata = client->dev.platform_data; |
| 1384 | struct pm8058_chip *chip; |
| 1385 | |
| 1386 | if (pdata == NULL || !client->irq) { |
| 1387 | pr_err("%s: No platform_data or IRQ.\n", __func__); |
| 1388 | return -ENODEV; |
| 1389 | } |
| 1390 | |
| 1391 | if (pdata->num_subdevs == 0) { |
| 1392 | pr_err("%s: No sub devices to support.\n", __func__); |
| 1393 | return -ENODEV; |
| 1394 | } |
| 1395 | |
| 1396 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) { |
| 1397 | pr_err("%s: i2c_check_functionality failed.\n", __func__); |
| 1398 | return -ENODEV; |
| 1399 | } |
| 1400 | |
| 1401 | chip = kzalloc(sizeof *chip, GFP_KERNEL); |
| 1402 | if (chip == NULL) { |
| 1403 | pr_err("%s: kzalloc() failed.\n", __func__); |
| 1404 | return -ENOMEM; |
| 1405 | } |
| 1406 | |
| 1407 | chip->dev = client; |
| 1408 | |
| 1409 | /* Read PMIC chip revision */ |
| 1410 | rc = ssbi_read(chip->dev, SSBI_REG_REV, &chip->revision, 1); |
| 1411 | if (rc) |
| 1412 | pr_err("%s: Failed on ssbi_read for revision: rc=%d.\n", |
| 1413 | __func__, rc); |
| 1414 | pr_info("%s: PMIC revision: %X\n", __func__, chip->revision); |
| 1415 | |
| 1416 | (void) memcpy((void *)&chip->pdata, (const void *)pdata, |
| 1417 | sizeof(chip->pdata)); |
| 1418 | |
| 1419 | mutex_init(&chip->pm_lock); |
| 1420 | irq_set_handler_data(chip->dev->irq, (void *)chip); |
| 1421 | irq_set_irq_wake(chip->dev->irq, 1); |
| 1422 | |
| 1423 | chip->pm_max_irq = 0; |
| 1424 | chip->pm_max_blocks = 0; |
| 1425 | chip->pm_max_masters = 0; |
| 1426 | |
| 1427 | i2c_set_clientdata(client, chip); |
| 1428 | |
| 1429 | pmic_chip = chip; |
| 1430 | |
| 1431 | /* Register for all reserved IRQs */ |
| 1432 | for (i = pdata->irq_base; i < (pdata->irq_base + MAX_PM_IRQ); i++) { |
| 1433 | irq_set_chip(i, &pm8058_irq_chip); |
| 1434 | irq_set_chip_data(i, (void *)chip); |
| 1435 | irq_set_handler(i, handle_edge_irq); |
| 1436 | set_irq_flags(i, IRQF_VALID); |
| 1437 | irq_set_nested_thread(i, 1); |
| 1438 | } |
| 1439 | |
| 1440 | rc = mfd_add_devices(&chip->dev->dev, 0, pdata->sub_devices, |
| 1441 | pdata->num_subdevs, NULL, 0); |
| 1442 | |
| 1443 | /* Add charger sub device with the chip parameter as driver data */ |
| 1444 | if (pdata->charger_sub_device) { |
| 1445 | rc = mfd_add_devices(&chip->dev->dev, 0, |
| 1446 | pdata->charger_sub_device, |
| 1447 | 1, NULL, 0); |
| 1448 | } |
| 1449 | |
| 1450 | if (pdata->init) { |
| 1451 | rc = pdata->init(chip); |
| 1452 | if (rc != 0) { |
| 1453 | pr_err("%s: board init failed\n", __func__); |
| 1454 | chip->dev = NULL; |
| 1455 | kfree(chip); |
| 1456 | return -ENODEV; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | rc = request_threaded_irq(chip->dev->irq, NULL, pm8058_isr_thread, |
| 1461 | IRQF_ONESHOT | IRQF_DISABLED | pdata->irq_trigger_flags, |
| 1462 | "pm8058-irq", chip); |
| 1463 | if (rc < 0) |
| 1464 | pr_err("%s: could not request irq %d: %d\n", __func__, |
| 1465 | chip->dev->irq, rc); |
| 1466 | |
| 1467 | rc = pmic8058_dbg_probe(chip); |
| 1468 | if (rc < 0) |
| 1469 | pr_err("%s: could not set up debugfs: %d\n", __func__, rc); |
| 1470 | |
Abhijeet Dharmapurikar | a4a3eaf | 2011-09-22 15:27:28 -0700 | [diff] [blame] | 1471 | rc = pm8058_hard_reset_config(SHUTDOWN_ON_HARD_RESET); |
| 1472 | if (rc < 0) |
| 1473 | pr_err("%s: failed to config shutdown on hard reset: %d\n", |
| 1474 | __func__, rc); |
| 1475 | |
Abhijeet Dharmapurikar | a3c0d94 | 2011-07-25 17:53:45 -0700 | [diff] [blame] | 1476 | register_syscore_ops(&pm8058_pm); |
| 1477 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | static int __devexit pm8058_remove(struct i2c_client *client) |
| 1482 | { |
| 1483 | struct pm8058_chip *chip; |
| 1484 | |
| 1485 | chip = i2c_get_clientdata(client); |
| 1486 | if (chip) { |
| 1487 | if (chip->pm_max_irq) { |
| 1488 | irq_set_irq_wake(chip->dev->irq, 0); |
| 1489 | free_irq(chip->dev->irq, chip); |
| 1490 | } |
| 1491 | mutex_destroy(&chip->pm_lock); |
| 1492 | chip->dev = NULL; |
| 1493 | |
| 1494 | kfree(chip); |
| 1495 | } |
| 1496 | |
| 1497 | pmic8058_dbg_remove(); |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1502 | static const struct i2c_device_id pm8058_ids[] = { |
| 1503 | { "pm8058-core", 0 }, |
| 1504 | { }, |
| 1505 | }; |
| 1506 | MODULE_DEVICE_TABLE(i2c, pm8058_ids); |
| 1507 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1508 | static struct i2c_driver pm8058_driver = { |
| 1509 | .driver.name = "pm8058-core", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1510 | .id_table = pm8058_ids, |
| 1511 | .probe = pm8058_probe, |
| 1512 | .remove = __devexit_p(pm8058_remove), |
| 1513 | }; |
| 1514 | |
| 1515 | static int __init pm8058_init(void) |
| 1516 | { |
| 1517 | int rc = i2c_add_driver(&pm8058_driver); |
| 1518 | pr_notice("%s: i2c_add_driver: rc = %d\n", __func__, rc); |
| 1519 | |
| 1520 | return rc; |
| 1521 | } |
| 1522 | |
| 1523 | static void __exit pm8058_exit(void) |
| 1524 | { |
| 1525 | i2c_del_driver(&pm8058_driver); |
| 1526 | } |
| 1527 | |
| 1528 | arch_initcall(pm8058_init); |
| 1529 | module_exit(pm8058_exit); |
| 1530 | |
| 1531 | MODULE_LICENSE("GPL v2"); |
| 1532 | MODULE_DESCRIPTION("PMIC8058 core driver"); |
| 1533 | MODULE_VERSION("1.0"); |
| 1534 | MODULE_ALIAS("platform:pmic8058-core"); |