Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/spinlock.h> |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 19 | #include <linux/interrupt.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/mfd/pm8xxx/core.h> |
| 22 | #include <linux/mfd/pm8xxx/misc.h> |
| 23 | |
| 24 | /* PON CTRL 1 register */ |
| 25 | #define REG_PM8058_PON_CTRL_1 0x01C |
| 26 | #define REG_PM8921_PON_CTRL_1 0x01C |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 27 | #define REG_PM8018_PON_CTRL_1 0x01C |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
| 29 | #define PON_CTRL_1_PULL_UP_MASK 0xE0 |
| 30 | #define PON_CTRL_1_USB_PWR_EN 0x10 |
| 31 | |
| 32 | #define PON_CTRL_1_WD_EN_MASK 0x08 |
| 33 | #define PON_CTRL_1_WD_EN_RESET 0x08 |
| 34 | #define PON_CTRL_1_WD_EN_PWR_OFF 0x00 |
| 35 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 36 | /* Regulator master enable addresses */ |
| 37 | #define REG_PM8058_VREG_EN_MSM 0x018 |
| 38 | #define REG_PM8058_VREG_EN_GRP_5_4 0x1C8 |
| 39 | |
| 40 | /* Regulator control registers for shutdown/reset */ |
| 41 | #define REG_PM8058_S0_CTRL 0x004 |
| 42 | #define REG_PM8058_S1_CTRL 0x005 |
| 43 | #define REG_PM8058_S3_CTRL 0x111 |
| 44 | #define REG_PM8058_L21_CTRL 0x120 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | #define REG_PM8058_L22_CTRL 0x121 |
| 46 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 47 | #define PM8058_REGULATOR_ENABLE_MASK 0x80 |
| 48 | #define PM8058_REGULATOR_ENABLE 0x80 |
| 49 | #define PM8058_REGULATOR_DISABLE 0x00 |
| 50 | #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40 |
| 51 | #define PM8058_REGULATOR_PULL_DOWN_EN 0x40 |
| 52 | |
| 53 | /* Buck CTRL register */ |
| 54 | #define PM8058_SMPS_LEGACY_VREF_SEL 0x20 |
| 55 | #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1F |
| 56 | #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0 |
| 57 | #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6 |
| 58 | #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3F |
| 59 | |
| 60 | /* Buck TEST2 registers for shutdown/reset */ |
| 61 | #define REG_PM8058_S0_TEST2 0x084 |
| 62 | #define REG_PM8058_S1_TEST2 0x085 |
| 63 | #define REG_PM8058_S3_TEST2 0x11A |
| 64 | |
| 65 | #define PM8058_REGULATOR_BANK_WRITE 0x80 |
| 66 | #define PM8058_REGULATOR_BANK_MASK 0x70 |
| 67 | #define PM8058_REGULATOR_BANK_SHIFT 4 |
| 68 | #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT) |
| 69 | |
| 70 | /* Buck TEST2 register bank 1 */ |
| 71 | #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01 |
| 72 | |
| 73 | /* Buck TEST2 register bank 7 */ |
| 74 | #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02 |
| 75 | #define PM8058_SMPS_ADVANCED_MODE 0x02 |
| 76 | #define PM8058_SMPS_LEGACY_MODE 0x00 |
| 77 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 78 | /* SLEEP CTRL register */ |
| 79 | #define REG_PM8058_SLEEP_CTRL 0x02B |
| 80 | #define REG_PM8921_SLEEP_CTRL 0x10A |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 81 | #define REG_PM8018_SLEEP_CTRL 0x10A |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 82 | |
| 83 | #define SLEEP_CTRL_SMPL_EN_MASK 0x04 |
| 84 | #define SLEEP_CTRL_SMPL_EN_RESET 0x04 |
| 85 | #define SLEEP_CTRL_SMPL_EN_PWR_OFF 0x00 |
| 86 | |
| 87 | /* FTS regulator PMR registers */ |
| 88 | #define REG_PM8901_REGULATOR_S1_PMR 0xA7 |
| 89 | #define REG_PM8901_REGULATOR_S2_PMR 0xA8 |
| 90 | #define REG_PM8901_REGULATOR_S3_PMR 0xA9 |
| 91 | #define REG_PM8901_REGULATOR_S4_PMR 0xAA |
| 92 | |
| 93 | #define PM8901_REGULATOR_PMR_STATE_MASK 0x60 |
| 94 | #define PM8901_REGULATOR_PMR_STATE_OFF 0x20 |
| 95 | |
Anirudh Ghayal | 5213eb8 | 2011-10-24 14:44:58 +0530 | [diff] [blame] | 96 | /* GPIO UART MUX CTRL registers */ |
| 97 | #define REG_PM8XXX_GPIO_MUX_CTRL 0x1CC |
| 98 | |
| 99 | #define UART_PATH_SEL_MASK 0x60 |
| 100 | #define UART_PATH_SEL_SHIFT 0x5 |
| 101 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | struct pm8xxx_misc_chip { |
| 103 | struct list_head link; |
| 104 | struct pm8xxx_misc_platform_data pdata; |
| 105 | struct device *dev; |
| 106 | enum pm8xxx_version version; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 107 | u64 osc_halt_count; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static LIST_HEAD(pm8xxx_misc_chips); |
| 111 | static DEFINE_SPINLOCK(pm8xxx_misc_chips_lock); |
| 112 | |
| 113 | static int pm8xxx_misc_masked_write(struct pm8xxx_misc_chip *chip, u16 addr, |
| 114 | u8 mask, u8 val) |
| 115 | { |
| 116 | int rc; |
| 117 | u8 reg; |
| 118 | |
| 119 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 120 | if (rc) { |
| 121 | pr_err("pm8xxx_readb(0x%03X) failed, rc=%d\n", addr, rc); |
| 122 | return rc; |
| 123 | } |
| 124 | reg &= ~mask; |
| 125 | reg |= val & mask; |
| 126 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 127 | if (rc) |
| 128 | pr_err("pm8xxx_writeb(0x%03X)=0x%02X failed, rc=%d\n", addr, |
| 129 | reg, rc); |
| 130 | return rc; |
| 131 | } |
| 132 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 133 | /* |
| 134 | * Set an SMPS regulator to be disabled in its CTRL register, but enabled |
| 135 | * in the master enable register. Also set it's pull down enable bit. |
| 136 | * Take care to make sure that the output voltage doesn't change if switching |
| 137 | * from advanced mode to legacy mode. |
| 138 | */ |
| 139 | static int |
| 140 | __pm8058_disable_smps_locally_set_pull_down(struct pm8xxx_misc_chip *chip, |
| 141 | u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr, |
| 142 | u8 master_enable_bit) |
| 143 | { |
| 144 | int rc = 0; |
| 145 | u8 vref_sel, vlow_sel, band, vprog, bank, reg; |
| 146 | |
| 147 | bank = PM8058_REGULATOR_BANK_SEL(7); |
| 148 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 149 | if (rc) { |
| 150 | pr_err("%s: pm8xxx_writeb(0x%03X) failed: rc=%d\n", __func__, |
| 151 | test2_addr, rc); |
| 152 | goto done; |
| 153 | } |
| 154 | |
| 155 | rc = pm8xxx_readb(chip->dev->parent, test2_addr, ®); |
| 156 | if (rc) { |
| 157 | pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n", |
| 158 | __func__, test2_addr, rc); |
| 159 | goto done; |
| 160 | } |
| 161 | |
| 162 | /* Check if in advanced mode. */ |
| 163 | if ((reg & PM8058_SMPS_ADVANCED_MODE_MASK) == |
| 164 | PM8058_SMPS_ADVANCED_MODE) { |
| 165 | /* Determine current output voltage. */ |
| 166 | rc = pm8xxx_readb(chip->dev->parent, ctrl_addr, ®); |
| 167 | if (rc) { |
| 168 | pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n", |
| 169 | __func__, ctrl_addr, rc); |
| 170 | goto done; |
| 171 | } |
| 172 | |
| 173 | band = (reg & PM8058_SMPS_ADVANCED_BAND_MASK) |
| 174 | >> PM8058_SMPS_ADVANCED_BAND_SHIFT; |
| 175 | switch (band) { |
| 176 | case 3: |
| 177 | vref_sel = 0; |
| 178 | vlow_sel = 0; |
| 179 | break; |
| 180 | case 2: |
| 181 | vref_sel = PM8058_SMPS_LEGACY_VREF_SEL; |
| 182 | vlow_sel = 0; |
| 183 | break; |
| 184 | case 1: |
| 185 | vref_sel = PM8058_SMPS_LEGACY_VREF_SEL; |
| 186 | vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL; |
| 187 | break; |
| 188 | default: |
| 189 | pr_err("%s: regulator already disabled\n", __func__); |
| 190 | return -EPERM; |
| 191 | } |
| 192 | vprog = (reg & PM8058_SMPS_ADVANCED_VPROG_MASK); |
| 193 | /* Round up if fine step is in use. */ |
| 194 | vprog = (vprog + 1) >> 1; |
| 195 | if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK) |
| 196 | vprog = PM8058_SMPS_LEGACY_VPROG_MASK; |
| 197 | |
| 198 | /* Set VLOW_SEL bit. */ |
| 199 | bank = PM8058_REGULATOR_BANK_SEL(1); |
| 200 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 201 | if (rc) { |
| 202 | pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n", |
| 203 | __func__, test2_addr, rc); |
| 204 | goto done; |
| 205 | } |
| 206 | |
| 207 | rc = pm8xxx_misc_masked_write(chip, test2_addr, |
| 208 | PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK |
| 209 | | PM8058_SMPS_LEGACY_VLOW_SEL, |
| 210 | PM8058_REGULATOR_BANK_WRITE | |
| 211 | PM8058_REGULATOR_BANK_SEL(1) | vlow_sel); |
| 212 | if (rc) |
| 213 | goto done; |
| 214 | |
| 215 | /* Switch to legacy mode */ |
| 216 | bank = PM8058_REGULATOR_BANK_SEL(7); |
| 217 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 218 | if (rc) { |
| 219 | pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n", |
| 220 | __func__, test2_addr, rc); |
| 221 | goto done; |
| 222 | } |
| 223 | rc = pm8xxx_misc_masked_write(chip, test2_addr, |
| 224 | PM8058_REGULATOR_BANK_WRITE | |
| 225 | PM8058_REGULATOR_BANK_MASK | |
| 226 | PM8058_SMPS_ADVANCED_MODE_MASK, |
| 227 | PM8058_REGULATOR_BANK_WRITE | |
| 228 | PM8058_REGULATOR_BANK_SEL(7) | |
| 229 | PM8058_SMPS_LEGACY_MODE); |
| 230 | if (rc) |
| 231 | goto done; |
| 232 | |
| 233 | /* Enable locally, enable pull down, keep voltage the same. */ |
| 234 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 235 | PM8058_REGULATOR_ENABLE_MASK | |
| 236 | PM8058_REGULATOR_PULL_DOWN_MASK | |
| 237 | PM8058_SMPS_LEGACY_VREF_SEL | |
| 238 | PM8058_SMPS_LEGACY_VPROG_MASK, |
| 239 | PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN |
| 240 | | vref_sel | vprog); |
| 241 | if (rc) |
| 242 | goto done; |
| 243 | } |
| 244 | |
| 245 | /* Enable in master control register. */ |
| 246 | rc = pm8xxx_misc_masked_write(chip, master_enable_addr, |
| 247 | master_enable_bit, master_enable_bit); |
| 248 | if (rc) |
| 249 | goto done; |
| 250 | |
| 251 | /* Disable locally and enable pull down. */ |
| 252 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 253 | PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK, |
| 254 | PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN); |
| 255 | |
| 256 | done: |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | __pm8058_disable_ldo_locally_set_pull_down(struct pm8xxx_misc_chip *chip, |
| 262 | u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit) |
| 263 | { |
| 264 | int rc; |
| 265 | |
| 266 | /* Enable LDO in master control register. */ |
| 267 | rc = pm8xxx_misc_masked_write(chip, master_enable_addr, |
| 268 | master_enable_bit, master_enable_bit); |
| 269 | if (rc) |
| 270 | goto done; |
| 271 | |
| 272 | /* Disable LDO in CTRL register and set pull down */ |
| 273 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 274 | PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK, |
| 275 | PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN); |
| 276 | |
| 277 | done: |
| 278 | return rc; |
| 279 | } |
| 280 | |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 281 | static int __pm8018_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 282 | { |
| 283 | int rc; |
| 284 | |
| 285 | /* Enable SMPL if resetting is desired. */ |
| 286 | rc = pm8xxx_misc_masked_write(chip, REG_PM8018_SLEEP_CTRL, |
| 287 | SLEEP_CTRL_SMPL_EN_MASK, |
| 288 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 289 | if (rc) { |
| 290 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 291 | return rc; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 296 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 297 | * USB charging is enabled. |
| 298 | */ |
| 299 | rc = pm8xxx_misc_masked_write(chip, REG_PM8018_PON_CTRL_1, |
| 300 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 301 | | PON_CTRL_1_WD_EN_MASK, |
| 302 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 303 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 304 | if (rc) |
| 305 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 306 | |
| 307 | return rc; |
| 308 | } |
| 309 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 310 | static int __pm8058_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 311 | { |
| 312 | int rc; |
| 313 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 314 | /* When shutting down, enable active pulldowns on important rails. */ |
| 315 | if (!reset) { |
| 316 | /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */ |
| 317 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 318 | REG_PM8058_S0_CTRL, REG_PM8058_S0_TEST2, |
| 319 | REG_PM8058_VREG_EN_MSM, BIT(7)); |
| 320 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 321 | REG_PM8058_S1_CTRL, REG_PM8058_S1_TEST2, |
| 322 | REG_PM8058_VREG_EN_MSM, BIT(6)); |
| 323 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 324 | REG_PM8058_S3_CTRL, REG_PM8058_S3_TEST2, |
| 325 | REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4)); |
| 326 | /* Disable LDO 21 locally and set pulldown enable bit. */ |
| 327 | __pm8058_disable_ldo_locally_set_pull_down(chip, |
| 328 | REG_PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4, |
| 329 | BIT(1)); |
| 330 | } |
| 331 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its |
| 334 | * pull-down state intact. This ensures a safe shutdown. |
| 335 | */ |
| 336 | rc = pm8xxx_misc_masked_write(chip, REG_PM8058_L22_CTRL, 0xBF, 0x93); |
| 337 | if (rc) { |
| 338 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 339 | goto read_write_err; |
| 340 | } |
| 341 | |
| 342 | /* Enable SMPL if resetting is desired. */ |
| 343 | rc = pm8xxx_misc_masked_write(chip, REG_PM8058_SLEEP_CTRL, |
| 344 | SLEEP_CTRL_SMPL_EN_MASK, |
| 345 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 346 | if (rc) { |
| 347 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 348 | goto read_write_err; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 353 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 354 | * USB charging is enabled. |
| 355 | */ |
| 356 | rc = pm8xxx_misc_masked_write(chip, REG_PM8058_PON_CTRL_1, |
| 357 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 358 | | PON_CTRL_1_WD_EN_MASK, |
| 359 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 360 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 361 | if (rc) { |
| 362 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 363 | goto read_write_err; |
| 364 | } |
| 365 | |
| 366 | read_write_err: |
| 367 | return rc; |
| 368 | } |
| 369 | |
| 370 | static int __pm8901_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 371 | { |
| 372 | int rc = 0, i; |
| 373 | u8 pmr_addr[4] = { |
| 374 | REG_PM8901_REGULATOR_S2_PMR, |
| 375 | REG_PM8901_REGULATOR_S3_PMR, |
| 376 | REG_PM8901_REGULATOR_S4_PMR, |
| 377 | REG_PM8901_REGULATOR_S1_PMR, |
| 378 | }; |
| 379 | |
| 380 | /* Fix-up: Turn off regulators S1, S2, S3, S4 when shutting down. */ |
| 381 | if (!reset) { |
| 382 | for (i = 0; i < 4; i++) { |
| 383 | rc = pm8xxx_misc_masked_write(chip, pmr_addr[i], |
| 384 | PM8901_REGULATOR_PMR_STATE_MASK, |
| 385 | PM8901_REGULATOR_PMR_STATE_OFF); |
| 386 | if (rc) { |
| 387 | pr_err("pm8xxx_misc_masked_write failed, " |
| 388 | "rc=%d\n", rc); |
| 389 | goto read_write_err; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | read_write_err: |
| 395 | return rc; |
| 396 | } |
| 397 | |
| 398 | static int __pm8921_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 399 | { |
| 400 | int rc; |
| 401 | |
| 402 | /* Enable SMPL if resetting is desired. */ |
| 403 | rc = pm8xxx_misc_masked_write(chip, REG_PM8921_SLEEP_CTRL, |
| 404 | SLEEP_CTRL_SMPL_EN_MASK, |
| 405 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 406 | if (rc) { |
| 407 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 408 | goto read_write_err; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 413 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 414 | * USB charging is enabled. |
| 415 | */ |
| 416 | rc = pm8xxx_misc_masked_write(chip, REG_PM8921_PON_CTRL_1, |
| 417 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 418 | | PON_CTRL_1_WD_EN_MASK, |
| 419 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 420 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 421 | if (rc) { |
| 422 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 423 | goto read_write_err; |
| 424 | } |
| 425 | |
| 426 | read_write_err: |
| 427 | return rc; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * pm8xxx_reset_pwr_off - switch all PM8XXX PMIC chips attached to the system to |
| 432 | * either reset or shutdown when they are turned off |
| 433 | * @reset: 0 = shudown the PMICs, 1 = shutdown and then restart the PMICs |
| 434 | * |
| 435 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 436 | */ |
| 437 | int pm8xxx_reset_pwr_off(int reset) |
| 438 | { |
| 439 | struct pm8xxx_misc_chip *chip; |
| 440 | unsigned long flags; |
| 441 | int rc = 0; |
| 442 | |
| 443 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 444 | |
| 445 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 446 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 447 | switch (chip->version) { |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 448 | case PM8XXX_VERSION_8018: |
| 449 | rc = __pm8018_reset_pwr_off(chip, reset); |
| 450 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 451 | case PM8XXX_VERSION_8058: |
| 452 | rc = __pm8058_reset_pwr_off(chip, reset); |
| 453 | break; |
| 454 | case PM8XXX_VERSION_8901: |
| 455 | rc = __pm8901_reset_pwr_off(chip, reset); |
| 456 | break; |
| 457 | case PM8XXX_VERSION_8921: |
| 458 | rc = __pm8921_reset_pwr_off(chip, reset); |
| 459 | break; |
| 460 | default: |
| 461 | /* PMIC doesn't have reset_pwr_off; do nothing. */ |
| 462 | break; |
| 463 | } |
| 464 | if (rc) { |
| 465 | pr_err("reset_pwr_off failed, rc=%d\n", rc); |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | EXPORT_SYMBOL_GPL(pm8xxx_reset_pwr_off); |
| 475 | |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 476 | /* Handle the OSC_HALT interrupt: 32 kHz XTAL oscillator has stopped. */ |
| 477 | static irqreturn_t pm8xxx_osc_halt_isr(int irq, void *data) |
| 478 | { |
| 479 | struct pm8xxx_misc_chip *chip = data; |
| 480 | u64 count = 0; |
| 481 | |
| 482 | if (chip) { |
| 483 | chip->osc_halt_count++; |
| 484 | count = chip->osc_halt_count; |
| 485 | } |
| 486 | |
| 487 | pr_crit("%s: OSC_HALT interrupt has triggered, 32 kHz XTAL oscillator" |
| 488 | " has halted (%llu)!\n", __func__, count); |
| 489 | |
| 490 | return IRQ_HANDLED; |
| 491 | } |
| 492 | |
Anirudh Ghayal | 5213eb8 | 2011-10-24 14:44:58 +0530 | [diff] [blame] | 493 | /** |
| 494 | * pm8xxx_uart_gpio_mux_ctrl - Mux configuration to select the UART |
| 495 | * |
| 496 | * @uart_path_sel: Input argument to select either UART1/2/3 |
| 497 | * |
| 498 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 499 | */ |
| 500 | int pm8xxx_uart_gpio_mux_ctrl(enum pm8xxx_uart_path_sel uart_path_sel) |
| 501 | { |
| 502 | struct pm8xxx_misc_chip *chip; |
| 503 | unsigned long flags; |
| 504 | int rc = 0; |
| 505 | |
| 506 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 507 | |
| 508 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 509 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 510 | switch (chip->version) { |
| 511 | case PM8XXX_VERSION_8018: |
| 512 | case PM8XXX_VERSION_8058: |
| 513 | case PM8XXX_VERSION_8921: |
| 514 | rc = pm8xxx_misc_masked_write(chip, |
| 515 | REG_PM8XXX_GPIO_MUX_CTRL, UART_PATH_SEL_MASK, |
| 516 | uart_path_sel << UART_PATH_SEL_SHIFT); |
| 517 | break; |
| 518 | default: |
| 519 | /* Functionality not supported */ |
| 520 | break; |
| 521 | } |
| 522 | if (rc) { |
| 523 | pr_err("uart_gpio_mux_ctrl failed, rc=%d\n", rc); |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 529 | |
| 530 | return rc; |
| 531 | } |
| 532 | EXPORT_SYMBOL(pm8xxx_uart_gpio_mux_ctrl); |
| 533 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 534 | static int __devinit pm8xxx_misc_probe(struct platform_device *pdev) |
| 535 | { |
| 536 | const struct pm8xxx_misc_platform_data *pdata = pdev->dev.platform_data; |
| 537 | struct pm8xxx_misc_chip *chip; |
| 538 | struct pm8xxx_misc_chip *sibling; |
| 539 | struct list_head *prev; |
| 540 | unsigned long flags; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 541 | int rc = 0, irq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 542 | |
| 543 | if (!pdata) { |
| 544 | pr_err("missing platform data\n"); |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | |
| 548 | chip = kzalloc(sizeof(struct pm8xxx_misc_chip), GFP_KERNEL); |
| 549 | if (!chip) { |
| 550 | pr_err("Cannot allocate %d bytes\n", |
| 551 | sizeof(struct pm8xxx_misc_chip)); |
| 552 | return -ENOMEM; |
| 553 | } |
| 554 | |
| 555 | chip->dev = &pdev->dev; |
| 556 | chip->version = pm8xxx_get_version(chip->dev->parent); |
| 557 | memcpy(&(chip->pdata), pdata, sizeof(struct pm8xxx_misc_platform_data)); |
| 558 | |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 559 | irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq"); |
| 560 | if (irq > 0) { |
| 561 | rc = request_any_context_irq(irq, pm8xxx_osc_halt_isr, |
| 562 | IRQF_TRIGGER_RISING | IRQF_DISABLED, |
| 563 | "pm8xxx_osc_halt_irq", chip); |
| 564 | if (rc < 0) { |
| 565 | pr_err("%s: request_any_context_irq(%d) FAIL: %d\n", |
| 566 | __func__, irq, rc); |
| 567 | goto fail_irq; |
| 568 | } |
| 569 | } |
| 570 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | /* Insert PMICs in priority order (lowest value first). */ |
| 572 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 573 | prev = &pm8xxx_misc_chips; |
| 574 | list_for_each_entry(sibling, &pm8xxx_misc_chips, link) { |
| 575 | if (chip->pdata.priority < sibling->pdata.priority) |
| 576 | break; |
| 577 | else |
| 578 | prev = &sibling->link; |
| 579 | } |
| 580 | list_add(&chip->link, prev); |
| 581 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 582 | |
| 583 | platform_set_drvdata(pdev, chip); |
| 584 | |
| 585 | return rc; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 586 | |
| 587 | fail_irq: |
| 588 | platform_set_drvdata(pdev, NULL); |
| 589 | kfree(chip); |
| 590 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static int __devexit pm8xxx_misc_remove(struct platform_device *pdev) |
| 594 | { |
| 595 | struct pm8xxx_misc_chip *chip = platform_get_drvdata(pdev); |
| 596 | unsigned long flags; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame^] | 597 | int irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq"); |
| 598 | if (irq > 0) |
| 599 | free_irq(irq, chip); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | |
| 601 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 602 | list_del(&chip->link); |
| 603 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 604 | |
| 605 | platform_set_drvdata(pdev, NULL); |
| 606 | kfree(chip); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static struct platform_driver pm8xxx_misc_driver = { |
| 612 | .probe = pm8xxx_misc_probe, |
| 613 | .remove = __devexit_p(pm8xxx_misc_remove), |
| 614 | .driver = { |
| 615 | .name = PM8XXX_MISC_DEV_NAME, |
| 616 | .owner = THIS_MODULE, |
| 617 | }, |
| 618 | }; |
| 619 | |
| 620 | static int __init pm8xxx_misc_init(void) |
| 621 | { |
| 622 | return platform_driver_register(&pm8xxx_misc_driver); |
| 623 | } |
| 624 | postcore_initcall(pm8xxx_misc_init); |
| 625 | |
| 626 | static void __exit pm8xxx_misc_exit(void) |
| 627 | { |
| 628 | platform_driver_unregister(&pm8xxx_misc_driver); |
| 629 | } |
| 630 | module_exit(pm8xxx_misc_exit); |
| 631 | |
| 632 | MODULE_LICENSE("GPL v2"); |
| 633 | MODULE_DESCRIPTION("PMIC 8XXX misc driver"); |
| 634 | MODULE_VERSION("1.0"); |
| 635 | MODULE_ALIAS("platform:" PM8XXX_MISC_DEV_NAME); |