Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
Amy Maloche | 4c994c9 | 2012-02-15 09:56:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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> |
Anirudh Ghayal | 1fd48c6 | 2011-12-13 12:39:43 +0530 | [diff] [blame] | 21 | #include <linux/delay.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <linux/mfd/pm8xxx/core.h> |
| 23 | #include <linux/mfd/pm8xxx/misc.h> |
| 24 | |
| 25 | /* PON CTRL 1 register */ |
Anirudh Ghayal | a23c1ca | 2011-11-01 14:36:24 +0530 | [diff] [blame] | 26 | #define REG_PM8XXX_PON_CTRL_1 0x01C |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 28 | #define PON_CTRL_1_PULL_UP_MASK 0xE0 |
| 29 | #define PON_CTRL_1_USB_PWR_EN 0x10 |
| 30 | |
| 31 | #define PON_CTRL_1_WD_EN_MASK 0x08 |
| 32 | #define PON_CTRL_1_WD_EN_RESET 0x08 |
| 33 | #define PON_CTRL_1_WD_EN_PWR_OFF 0x00 |
| 34 | |
Anirudh Ghayal | a4262a3 | 2011-11-10 00:02:18 +0530 | [diff] [blame] | 35 | /* PON CNTL registers */ |
| 36 | #define REG_PM8058_PON_CNTL_4 0x098 |
| 37 | #define REG_PM8901_PON_CNTL_4 0x099 |
| 38 | #define REG_PM8018_PON_CNTL_4 0x01E |
| 39 | #define REG_PM8921_PON_CNTL_4 0x01E |
| 40 | #define REG_PM8058_PON_CNTL_5 0x07B |
| 41 | #define REG_PM8901_PON_CNTL_5 0x09A |
| 42 | #define REG_PM8018_PON_CNTL_5 0x01F |
| 43 | #define REG_PM8921_PON_CNTL_5 0x01F |
| 44 | |
| 45 | #define PON_CTRL_4_RESET_EN_MASK 0x01 |
| 46 | #define PON_CTRL_4_SHUTDOWN_ON_RESET 0x0 |
| 47 | #define PON_CTRL_4_RESTART_ON_RESET 0x1 |
| 48 | #define PON_CTRL_5_HARD_RESET_EN_MASK 0x08 |
| 49 | #define PON_CTRL_5_HARD_RESET_EN 0x08 |
| 50 | #define PON_CTRL_5_HARD_RESET_DIS 0x00 |
| 51 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 52 | /* Regulator master enable addresses */ |
| 53 | #define REG_PM8058_VREG_EN_MSM 0x018 |
| 54 | #define REG_PM8058_VREG_EN_GRP_5_4 0x1C8 |
| 55 | |
| 56 | /* Regulator control registers for shutdown/reset */ |
| 57 | #define REG_PM8058_S0_CTRL 0x004 |
| 58 | #define REG_PM8058_S1_CTRL 0x005 |
| 59 | #define REG_PM8058_S3_CTRL 0x111 |
| 60 | #define REG_PM8058_L21_CTRL 0x120 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 61 | #define REG_PM8058_L22_CTRL 0x121 |
| 62 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 63 | #define PM8058_REGULATOR_ENABLE_MASK 0x80 |
| 64 | #define PM8058_REGULATOR_ENABLE 0x80 |
| 65 | #define PM8058_REGULATOR_DISABLE 0x00 |
| 66 | #define PM8058_REGULATOR_PULL_DOWN_MASK 0x40 |
| 67 | #define PM8058_REGULATOR_PULL_DOWN_EN 0x40 |
| 68 | |
| 69 | /* Buck CTRL register */ |
| 70 | #define PM8058_SMPS_LEGACY_VREF_SEL 0x20 |
| 71 | #define PM8058_SMPS_LEGACY_VPROG_MASK 0x1F |
| 72 | #define PM8058_SMPS_ADVANCED_BAND_MASK 0xC0 |
| 73 | #define PM8058_SMPS_ADVANCED_BAND_SHIFT 6 |
| 74 | #define PM8058_SMPS_ADVANCED_VPROG_MASK 0x3F |
| 75 | |
| 76 | /* Buck TEST2 registers for shutdown/reset */ |
| 77 | #define REG_PM8058_S0_TEST2 0x084 |
| 78 | #define REG_PM8058_S1_TEST2 0x085 |
| 79 | #define REG_PM8058_S3_TEST2 0x11A |
| 80 | |
| 81 | #define PM8058_REGULATOR_BANK_WRITE 0x80 |
| 82 | #define PM8058_REGULATOR_BANK_MASK 0x70 |
| 83 | #define PM8058_REGULATOR_BANK_SHIFT 4 |
| 84 | #define PM8058_REGULATOR_BANK_SEL(n) ((n) << PM8058_REGULATOR_BANK_SHIFT) |
| 85 | |
| 86 | /* Buck TEST2 register bank 1 */ |
| 87 | #define PM8058_SMPS_LEGACY_VLOW_SEL 0x01 |
| 88 | |
| 89 | /* Buck TEST2 register bank 7 */ |
| 90 | #define PM8058_SMPS_ADVANCED_MODE_MASK 0x02 |
| 91 | #define PM8058_SMPS_ADVANCED_MODE 0x02 |
| 92 | #define PM8058_SMPS_LEGACY_MODE 0x00 |
| 93 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 94 | /* SLEEP CTRL register */ |
| 95 | #define REG_PM8058_SLEEP_CTRL 0x02B |
| 96 | #define REG_PM8921_SLEEP_CTRL 0x10A |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 97 | #define REG_PM8018_SLEEP_CTRL 0x10A |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | |
| 99 | #define SLEEP_CTRL_SMPL_EN_MASK 0x04 |
| 100 | #define SLEEP_CTRL_SMPL_EN_RESET 0x04 |
| 101 | #define SLEEP_CTRL_SMPL_EN_PWR_OFF 0x00 |
| 102 | |
Anirudh Ghayal | bfbaf82 | 2011-11-01 14:28:34 +0530 | [diff] [blame] | 103 | #define SLEEP_CTRL_SMPL_SEL_MASK 0x03 |
| 104 | #define SLEEP_CTRL_SMPL_SEL_MIN 0 |
| 105 | #define SLEEP_CTRL_SMPL_SEL_MAX 3 |
| 106 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | /* FTS regulator PMR registers */ |
| 108 | #define REG_PM8901_REGULATOR_S1_PMR 0xA7 |
| 109 | #define REG_PM8901_REGULATOR_S2_PMR 0xA8 |
| 110 | #define REG_PM8901_REGULATOR_S3_PMR 0xA9 |
| 111 | #define REG_PM8901_REGULATOR_S4_PMR 0xAA |
| 112 | |
| 113 | #define PM8901_REGULATOR_PMR_STATE_MASK 0x60 |
| 114 | #define PM8901_REGULATOR_PMR_STATE_OFF 0x20 |
| 115 | |
Anirudh Ghayal | 7b38229 | 2011-11-01 14:08:34 +0530 | [diff] [blame] | 116 | /* COINCELL CHG registers */ |
| 117 | #define REG_PM8058_COIN_CHG 0x02F |
| 118 | #define REG_PM8921_COIN_CHG 0x09C |
| 119 | #define REG_PM8018_COIN_CHG 0x09C |
| 120 | |
| 121 | #define COINCELL_RESISTOR_SHIFT 0x2 |
| 122 | |
Anirudh Ghayal | 51e947f | 2011-11-01 14:49:45 +0530 | [diff] [blame] | 123 | /* GP TEST register */ |
| 124 | #define REG_PM8XXX_GP_TEST_1 0x07A |
| 125 | |
| 126 | /* Stay on configuration */ |
| 127 | #define PM8XXX_STAY_ON_CFG 0x92 |
| 128 | |
Anirudh Ghayal | 5213eb8 | 2011-10-24 14:44:58 +0530 | [diff] [blame] | 129 | /* GPIO UART MUX CTRL registers */ |
| 130 | #define REG_PM8XXX_GPIO_MUX_CTRL 0x1CC |
| 131 | |
| 132 | #define UART_PATH_SEL_MASK 0x60 |
| 133 | #define UART_PATH_SEL_SHIFT 0x5 |
| 134 | |
Willie Ruan | 5db1f24 | 2012-01-30 22:08:04 -0800 | [diff] [blame] | 135 | #define USB_ID_PU_EN_MASK 0x10 /* PM8921 family only */ |
| 136 | #define USB_ID_PU_EN_SHIFT 4 |
| 137 | |
Anirudh Ghayal | 1fd48c6 | 2011-12-13 12:39:43 +0530 | [diff] [blame] | 138 | /* Shutdown/restart delays to allow for LDO 7/dVdd regulator load settling. */ |
| 139 | #define PM8901_DELAY_AFTER_REG_DISABLE_MS 4 |
| 140 | #define PM8901_DELAY_BEFORE_SHUTDOWN_MS 8 |
| 141 | |
Amy Maloche | 4c994c9 | 2012-02-15 09:56:15 -0800 | [diff] [blame] | 142 | #define REG_PM8XXX_XO_CNTRL_2 0x114 |
| 143 | #define MP3_1_MASK 0xE0 |
| 144 | #define MP3_2_MASK 0x1C |
| 145 | #define MP3_1_SHIFT 5 |
| 146 | #define MP3_2_SHIFT 2 |
| 147 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 148 | struct pm8xxx_misc_chip { |
| 149 | struct list_head link; |
| 150 | struct pm8xxx_misc_platform_data pdata; |
| 151 | struct device *dev; |
| 152 | enum pm8xxx_version version; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 153 | u64 osc_halt_count; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static LIST_HEAD(pm8xxx_misc_chips); |
| 157 | static DEFINE_SPINLOCK(pm8xxx_misc_chips_lock); |
| 158 | |
| 159 | static int pm8xxx_misc_masked_write(struct pm8xxx_misc_chip *chip, u16 addr, |
| 160 | u8 mask, u8 val) |
| 161 | { |
| 162 | int rc; |
| 163 | u8 reg; |
| 164 | |
| 165 | rc = pm8xxx_readb(chip->dev->parent, addr, ®); |
| 166 | if (rc) { |
| 167 | pr_err("pm8xxx_readb(0x%03X) failed, rc=%d\n", addr, rc); |
| 168 | return rc; |
| 169 | } |
| 170 | reg &= ~mask; |
| 171 | reg |= val & mask; |
| 172 | rc = pm8xxx_writeb(chip->dev->parent, addr, reg); |
| 173 | if (rc) |
| 174 | pr_err("pm8xxx_writeb(0x%03X)=0x%02X failed, rc=%d\n", addr, |
| 175 | reg, rc); |
| 176 | return rc; |
| 177 | } |
| 178 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 179 | /* |
| 180 | * Set an SMPS regulator to be disabled in its CTRL register, but enabled |
| 181 | * in the master enable register. Also set it's pull down enable bit. |
| 182 | * Take care to make sure that the output voltage doesn't change if switching |
| 183 | * from advanced mode to legacy mode. |
| 184 | */ |
| 185 | static int |
| 186 | __pm8058_disable_smps_locally_set_pull_down(struct pm8xxx_misc_chip *chip, |
| 187 | u16 ctrl_addr, u16 test2_addr, u16 master_enable_addr, |
| 188 | u8 master_enable_bit) |
| 189 | { |
| 190 | int rc = 0; |
| 191 | u8 vref_sel, vlow_sel, band, vprog, bank, reg; |
| 192 | |
| 193 | bank = PM8058_REGULATOR_BANK_SEL(7); |
| 194 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 195 | if (rc) { |
| 196 | pr_err("%s: pm8xxx_writeb(0x%03X) failed: rc=%d\n", __func__, |
| 197 | test2_addr, rc); |
| 198 | goto done; |
| 199 | } |
| 200 | |
| 201 | rc = pm8xxx_readb(chip->dev->parent, test2_addr, ®); |
| 202 | if (rc) { |
| 203 | pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n", |
| 204 | __func__, test2_addr, rc); |
| 205 | goto done; |
| 206 | } |
| 207 | |
| 208 | /* Check if in advanced mode. */ |
| 209 | if ((reg & PM8058_SMPS_ADVANCED_MODE_MASK) == |
| 210 | PM8058_SMPS_ADVANCED_MODE) { |
| 211 | /* Determine current output voltage. */ |
| 212 | rc = pm8xxx_readb(chip->dev->parent, ctrl_addr, ®); |
| 213 | if (rc) { |
| 214 | pr_err("%s: FAIL pm8xxx_readb(0x%03X): rc=%d\n", |
| 215 | __func__, ctrl_addr, rc); |
| 216 | goto done; |
| 217 | } |
| 218 | |
| 219 | band = (reg & PM8058_SMPS_ADVANCED_BAND_MASK) |
| 220 | >> PM8058_SMPS_ADVANCED_BAND_SHIFT; |
| 221 | switch (band) { |
| 222 | case 3: |
| 223 | vref_sel = 0; |
| 224 | vlow_sel = 0; |
| 225 | break; |
| 226 | case 2: |
| 227 | vref_sel = PM8058_SMPS_LEGACY_VREF_SEL; |
| 228 | vlow_sel = 0; |
| 229 | break; |
| 230 | case 1: |
| 231 | vref_sel = PM8058_SMPS_LEGACY_VREF_SEL; |
| 232 | vlow_sel = PM8058_SMPS_LEGACY_VLOW_SEL; |
| 233 | break; |
| 234 | default: |
| 235 | pr_err("%s: regulator already disabled\n", __func__); |
| 236 | return -EPERM; |
| 237 | } |
| 238 | vprog = (reg & PM8058_SMPS_ADVANCED_VPROG_MASK); |
| 239 | /* Round up if fine step is in use. */ |
| 240 | vprog = (vprog + 1) >> 1; |
| 241 | if (vprog > PM8058_SMPS_LEGACY_VPROG_MASK) |
| 242 | vprog = PM8058_SMPS_LEGACY_VPROG_MASK; |
| 243 | |
| 244 | /* Set VLOW_SEL bit. */ |
| 245 | bank = PM8058_REGULATOR_BANK_SEL(1); |
| 246 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 247 | if (rc) { |
| 248 | pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n", |
| 249 | __func__, test2_addr, rc); |
| 250 | goto done; |
| 251 | } |
| 252 | |
| 253 | rc = pm8xxx_misc_masked_write(chip, test2_addr, |
| 254 | PM8058_REGULATOR_BANK_WRITE | PM8058_REGULATOR_BANK_MASK |
| 255 | | PM8058_SMPS_LEGACY_VLOW_SEL, |
| 256 | PM8058_REGULATOR_BANK_WRITE | |
| 257 | PM8058_REGULATOR_BANK_SEL(1) | vlow_sel); |
| 258 | if (rc) |
| 259 | goto done; |
| 260 | |
| 261 | /* Switch to legacy mode */ |
| 262 | bank = PM8058_REGULATOR_BANK_SEL(7); |
| 263 | rc = pm8xxx_writeb(chip->dev->parent, test2_addr, bank); |
| 264 | if (rc) { |
| 265 | pr_err("%s: FAIL pm8xxx_writeb(0x%03X): rc=%d\n", |
| 266 | __func__, test2_addr, rc); |
| 267 | goto done; |
| 268 | } |
| 269 | rc = pm8xxx_misc_masked_write(chip, test2_addr, |
| 270 | PM8058_REGULATOR_BANK_WRITE | |
| 271 | PM8058_REGULATOR_BANK_MASK | |
| 272 | PM8058_SMPS_ADVANCED_MODE_MASK, |
| 273 | PM8058_REGULATOR_BANK_WRITE | |
| 274 | PM8058_REGULATOR_BANK_SEL(7) | |
| 275 | PM8058_SMPS_LEGACY_MODE); |
| 276 | if (rc) |
| 277 | goto done; |
| 278 | |
| 279 | /* Enable locally, enable pull down, keep voltage the same. */ |
| 280 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 281 | PM8058_REGULATOR_ENABLE_MASK | |
| 282 | PM8058_REGULATOR_PULL_DOWN_MASK | |
| 283 | PM8058_SMPS_LEGACY_VREF_SEL | |
| 284 | PM8058_SMPS_LEGACY_VPROG_MASK, |
| 285 | PM8058_REGULATOR_ENABLE | PM8058_REGULATOR_PULL_DOWN_EN |
| 286 | | vref_sel | vprog); |
| 287 | if (rc) |
| 288 | goto done; |
| 289 | } |
| 290 | |
| 291 | /* Enable in master control register. */ |
| 292 | rc = pm8xxx_misc_masked_write(chip, master_enable_addr, |
| 293 | master_enable_bit, master_enable_bit); |
| 294 | if (rc) |
| 295 | goto done; |
| 296 | |
| 297 | /* Disable locally and enable pull down. */ |
| 298 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 299 | PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK, |
| 300 | PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN); |
| 301 | |
| 302 | done: |
| 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | static int |
| 307 | __pm8058_disable_ldo_locally_set_pull_down(struct pm8xxx_misc_chip *chip, |
| 308 | u16 ctrl_addr, u16 master_enable_addr, u8 master_enable_bit) |
| 309 | { |
| 310 | int rc; |
| 311 | |
| 312 | /* Enable LDO in master control register. */ |
| 313 | rc = pm8xxx_misc_masked_write(chip, master_enable_addr, |
| 314 | master_enable_bit, master_enable_bit); |
| 315 | if (rc) |
| 316 | goto done; |
| 317 | |
| 318 | /* Disable LDO in CTRL register and set pull down */ |
| 319 | rc = pm8xxx_misc_masked_write(chip, ctrl_addr, |
| 320 | PM8058_REGULATOR_ENABLE_MASK | PM8058_REGULATOR_PULL_DOWN_MASK, |
| 321 | PM8058_REGULATOR_DISABLE | PM8058_REGULATOR_PULL_DOWN_EN); |
| 322 | |
| 323 | done: |
| 324 | return rc; |
| 325 | } |
| 326 | |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 327 | static int __pm8018_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 328 | { |
| 329 | int rc; |
| 330 | |
| 331 | /* Enable SMPL if resetting is desired. */ |
| 332 | rc = pm8xxx_misc_masked_write(chip, REG_PM8018_SLEEP_CTRL, |
| 333 | SLEEP_CTRL_SMPL_EN_MASK, |
| 334 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 335 | if (rc) { |
| 336 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 342 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 343 | * USB charging is enabled. |
| 344 | */ |
Anirudh Ghayal | a23c1ca | 2011-11-01 14:36:24 +0530 | [diff] [blame] | 345 | rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1, |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 346 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 347 | | PON_CTRL_1_WD_EN_MASK, |
| 348 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 349 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 350 | if (rc) |
| 351 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 352 | |
| 353 | return rc; |
| 354 | } |
| 355 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 356 | static int __pm8058_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 357 | { |
| 358 | int rc; |
| 359 | |
Anirudh Ghayal | 9e1bd64 | 2011-11-01 13:57:40 +0530 | [diff] [blame] | 360 | /* When shutting down, enable active pulldowns on important rails. */ |
| 361 | if (!reset) { |
| 362 | /* Disable SMPS's 0,1,3 locally and set pulldown enable bits. */ |
| 363 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 364 | REG_PM8058_S0_CTRL, REG_PM8058_S0_TEST2, |
| 365 | REG_PM8058_VREG_EN_MSM, BIT(7)); |
| 366 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 367 | REG_PM8058_S1_CTRL, REG_PM8058_S1_TEST2, |
| 368 | REG_PM8058_VREG_EN_MSM, BIT(6)); |
| 369 | __pm8058_disable_smps_locally_set_pull_down(chip, |
| 370 | REG_PM8058_S3_CTRL, REG_PM8058_S3_TEST2, |
| 371 | REG_PM8058_VREG_EN_GRP_5_4, BIT(7) | BIT(4)); |
| 372 | /* Disable LDO 21 locally and set pulldown enable bit. */ |
| 373 | __pm8058_disable_ldo_locally_set_pull_down(chip, |
| 374 | REG_PM8058_L21_CTRL, REG_PM8058_VREG_EN_GRP_5_4, |
| 375 | BIT(1)); |
| 376 | } |
| 377 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 378 | /* |
| 379 | * Fix-up: Set regulator LDO22 to 1.225 V in high power mode. Leave its |
| 380 | * pull-down state intact. This ensures a safe shutdown. |
| 381 | */ |
| 382 | rc = pm8xxx_misc_masked_write(chip, REG_PM8058_L22_CTRL, 0xBF, 0x93); |
| 383 | if (rc) { |
| 384 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 385 | goto read_write_err; |
| 386 | } |
| 387 | |
| 388 | /* Enable SMPL if resetting is desired. */ |
| 389 | rc = pm8xxx_misc_masked_write(chip, REG_PM8058_SLEEP_CTRL, |
| 390 | SLEEP_CTRL_SMPL_EN_MASK, |
| 391 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 392 | if (rc) { |
| 393 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 394 | goto read_write_err; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 399 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 400 | * USB charging is enabled. |
| 401 | */ |
Anirudh Ghayal | a23c1ca | 2011-11-01 14:36:24 +0530 | [diff] [blame] | 402 | rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 403 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 404 | | PON_CTRL_1_WD_EN_MASK, |
| 405 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 406 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 407 | if (rc) { |
| 408 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 409 | goto read_write_err; |
| 410 | } |
| 411 | |
| 412 | read_write_err: |
| 413 | return rc; |
| 414 | } |
| 415 | |
| 416 | static int __pm8901_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 417 | { |
| 418 | int rc = 0, i; |
| 419 | u8 pmr_addr[4] = { |
| 420 | REG_PM8901_REGULATOR_S2_PMR, |
| 421 | REG_PM8901_REGULATOR_S3_PMR, |
| 422 | REG_PM8901_REGULATOR_S4_PMR, |
| 423 | REG_PM8901_REGULATOR_S1_PMR, |
| 424 | }; |
| 425 | |
| 426 | /* Fix-up: Turn off regulators S1, S2, S3, S4 when shutting down. */ |
| 427 | if (!reset) { |
| 428 | for (i = 0; i < 4; i++) { |
| 429 | rc = pm8xxx_misc_masked_write(chip, pmr_addr[i], |
| 430 | PM8901_REGULATOR_PMR_STATE_MASK, |
| 431 | PM8901_REGULATOR_PMR_STATE_OFF); |
| 432 | if (rc) { |
| 433 | pr_err("pm8xxx_misc_masked_write failed, " |
| 434 | "rc=%d\n", rc); |
| 435 | goto read_write_err; |
| 436 | } |
Anirudh Ghayal | 1fd48c6 | 2011-12-13 12:39:43 +0530 | [diff] [blame] | 437 | mdelay(PM8901_DELAY_AFTER_REG_DISABLE_MS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | read_write_err: |
Anirudh Ghayal | 1fd48c6 | 2011-12-13 12:39:43 +0530 | [diff] [blame] | 442 | mdelay(PM8901_DELAY_BEFORE_SHUTDOWN_MS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | return rc; |
| 444 | } |
| 445 | |
| 446 | static int __pm8921_reset_pwr_off(struct pm8xxx_misc_chip *chip, int reset) |
| 447 | { |
| 448 | int rc; |
| 449 | |
| 450 | /* Enable SMPL if resetting is desired. */ |
| 451 | rc = pm8xxx_misc_masked_write(chip, REG_PM8921_SLEEP_CTRL, |
| 452 | SLEEP_CTRL_SMPL_EN_MASK, |
| 453 | (reset ? SLEEP_CTRL_SMPL_EN_RESET : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 454 | if (rc) { |
| 455 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 456 | goto read_write_err; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Select action to perform (reset or shutdown) when PS_HOLD goes low. |
| 461 | * Also ensure that KPD, CBL0, and CBL1 pull ups are enabled and that |
| 462 | * USB charging is enabled. |
| 463 | */ |
Anirudh Ghayal | a23c1ca | 2011-11-01 14:36:24 +0530 | [diff] [blame] | 464 | rc = pm8xxx_misc_masked_write(chip, REG_PM8XXX_PON_CTRL_1, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 465 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 466 | | PON_CTRL_1_WD_EN_MASK, |
| 467 | PON_CTRL_1_PULL_UP_MASK | PON_CTRL_1_USB_PWR_EN |
| 468 | | (reset ? PON_CTRL_1_WD_EN_RESET : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 469 | if (rc) { |
| 470 | pr_err("pm8xxx_misc_masked_write failed, rc=%d\n", rc); |
| 471 | goto read_write_err; |
| 472 | } |
| 473 | |
| 474 | read_write_err: |
| 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * pm8xxx_reset_pwr_off - switch all PM8XXX PMIC chips attached to the system to |
| 480 | * either reset or shutdown when they are turned off |
| 481 | * @reset: 0 = shudown the PMICs, 1 = shutdown and then restart the PMICs |
| 482 | * |
| 483 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 484 | */ |
| 485 | int pm8xxx_reset_pwr_off(int reset) |
| 486 | { |
| 487 | struct pm8xxx_misc_chip *chip; |
| 488 | unsigned long flags; |
| 489 | int rc = 0; |
| 490 | |
| 491 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 492 | |
| 493 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 494 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 495 | switch (chip->version) { |
Jay Chokshi | 86580f2 | 2011-10-17 12:27:52 -0700 | [diff] [blame] | 496 | case PM8XXX_VERSION_8018: |
| 497 | rc = __pm8018_reset_pwr_off(chip, reset); |
| 498 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 499 | case PM8XXX_VERSION_8058: |
| 500 | rc = __pm8058_reset_pwr_off(chip, reset); |
| 501 | break; |
| 502 | case PM8XXX_VERSION_8901: |
| 503 | rc = __pm8901_reset_pwr_off(chip, reset); |
| 504 | break; |
| 505 | case PM8XXX_VERSION_8921: |
| 506 | rc = __pm8921_reset_pwr_off(chip, reset); |
| 507 | break; |
| 508 | default: |
| 509 | /* PMIC doesn't have reset_pwr_off; do nothing. */ |
| 510 | break; |
| 511 | } |
| 512 | if (rc) { |
| 513 | pr_err("reset_pwr_off failed, rc=%d\n", rc); |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 519 | |
| 520 | return rc; |
| 521 | } |
| 522 | EXPORT_SYMBOL_GPL(pm8xxx_reset_pwr_off); |
| 523 | |
Anirudh Ghayal | 7b38229 | 2011-11-01 14:08:34 +0530 | [diff] [blame] | 524 | /** |
Anirudh Ghayal | bfbaf82 | 2011-11-01 14:28:34 +0530 | [diff] [blame] | 525 | * pm8xxx_smpl_control - enables/disables SMPL detection |
| 526 | * @enable: 0 = shutdown PMIC on power loss, 1 = reset PMIC on power loss |
| 527 | * |
| 528 | * This function enables or disables the Sudden Momentary Power Loss detection |
| 529 | * module. If SMPL detection is enabled, then when a sufficiently long power |
| 530 | * loss event occurs, the PMIC will automatically reset itself. If SMPL |
| 531 | * detection is disabled, then the PMIC will shutdown when power loss occurs. |
| 532 | * |
| 533 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 534 | */ |
| 535 | int pm8xxx_smpl_control(int enable) |
| 536 | { |
| 537 | struct pm8xxx_misc_chip *chip; |
| 538 | unsigned long flags; |
| 539 | int rc = 0; |
| 540 | |
| 541 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 542 | |
| 543 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 544 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 545 | switch (chip->version) { |
| 546 | case PM8XXX_VERSION_8018: |
| 547 | rc = pm8xxx_misc_masked_write(chip, |
| 548 | REG_PM8018_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK, |
David Collins | c06e0d6 | 2012-02-13 14:42:09 -0800 | [diff] [blame] | 549 | (enable ? SLEEP_CTRL_SMPL_EN_RESET |
Anirudh Ghayal | bfbaf82 | 2011-11-01 14:28:34 +0530 | [diff] [blame] | 550 | : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 551 | break; |
| 552 | case PM8XXX_VERSION_8058: |
| 553 | rc = pm8xxx_misc_masked_write(chip, |
| 554 | REG_PM8058_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK, |
| 555 | (enable ? SLEEP_CTRL_SMPL_EN_RESET |
| 556 | : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 557 | break; |
| 558 | case PM8XXX_VERSION_8921: |
| 559 | rc = pm8xxx_misc_masked_write(chip, |
| 560 | REG_PM8921_SLEEP_CTRL, SLEEP_CTRL_SMPL_EN_MASK, |
David Collins | c06e0d6 | 2012-02-13 14:42:09 -0800 | [diff] [blame] | 561 | (enable ? SLEEP_CTRL_SMPL_EN_RESET |
Anirudh Ghayal | bfbaf82 | 2011-11-01 14:28:34 +0530 | [diff] [blame] | 562 | : SLEEP_CTRL_SMPL_EN_PWR_OFF)); |
| 563 | break; |
| 564 | default: |
| 565 | /* PMIC doesn't have reset_pwr_off; do nothing. */ |
| 566 | break; |
| 567 | } |
| 568 | if (rc) { |
| 569 | pr_err("setting smpl control failed, rc=%d\n", rc); |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 575 | |
| 576 | return rc; |
| 577 | } |
| 578 | EXPORT_SYMBOL(pm8xxx_smpl_control); |
| 579 | |
| 580 | |
| 581 | /** |
| 582 | * pm8xxx_smpl_set_delay - sets the SMPL detection time delay |
| 583 | * @delay: enum value corresponding to delay time |
| 584 | * |
| 585 | * This function sets the time delay of the SMPL detection module. If power |
| 586 | * is reapplied within this interval, then the PMIC reset automatically. The |
| 587 | * SMPL detection module must be enabled for this delay time to take effect. |
| 588 | * |
| 589 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 590 | */ |
| 591 | int pm8xxx_smpl_set_delay(enum pm8xxx_smpl_delay delay) |
| 592 | { |
| 593 | struct pm8xxx_misc_chip *chip; |
| 594 | unsigned long flags; |
| 595 | int rc = 0; |
| 596 | |
| 597 | if (delay < SLEEP_CTRL_SMPL_SEL_MIN |
| 598 | || delay > SLEEP_CTRL_SMPL_SEL_MAX) { |
| 599 | pr_err("%s: invalid delay specified: %d\n", __func__, delay); |
| 600 | return -EINVAL; |
| 601 | } |
| 602 | |
| 603 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 604 | |
| 605 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 606 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 607 | switch (chip->version) { |
| 608 | case PM8XXX_VERSION_8018: |
| 609 | rc = pm8xxx_misc_masked_write(chip, |
| 610 | REG_PM8018_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK, |
| 611 | delay); |
| 612 | break; |
| 613 | case PM8XXX_VERSION_8058: |
| 614 | rc = pm8xxx_misc_masked_write(chip, |
| 615 | REG_PM8058_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK, |
| 616 | delay); |
| 617 | break; |
| 618 | case PM8XXX_VERSION_8921: |
| 619 | rc = pm8xxx_misc_masked_write(chip, |
| 620 | REG_PM8921_SLEEP_CTRL, SLEEP_CTRL_SMPL_SEL_MASK, |
| 621 | delay); |
| 622 | break; |
| 623 | default: |
| 624 | /* PMIC doesn't have reset_pwr_off; do nothing. */ |
| 625 | break; |
| 626 | } |
| 627 | if (rc) { |
| 628 | pr_err("setting smpl delay failed, rc=%d\n", rc); |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 634 | |
| 635 | return rc; |
| 636 | } |
| 637 | EXPORT_SYMBOL(pm8xxx_smpl_set_delay); |
| 638 | |
| 639 | /** |
Anirudh Ghayal | 7b38229 | 2011-11-01 14:08:34 +0530 | [diff] [blame] | 640 | * pm8xxx_coincell_chg_config - Disables or enables the coincell charger, and |
| 641 | * configures its voltage and resistor settings. |
| 642 | * @chg_config: Holds both voltage and resistor values, and a |
| 643 | * switch to change the state of charger. |
| 644 | * If state is to disable the charger then |
| 645 | * both voltage and resistor are disregarded. |
| 646 | * |
| 647 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 648 | */ |
| 649 | int pm8xxx_coincell_chg_config(struct pm8xxx_coincell_chg *chg_config) |
| 650 | { |
| 651 | struct pm8xxx_misc_chip *chip; |
| 652 | unsigned long flags; |
| 653 | u8 reg = 0, voltage, resistor; |
| 654 | int rc = 0; |
| 655 | |
| 656 | if (chg_config == NULL) { |
| 657 | pr_err("chg_config is NULL\n"); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | |
| 661 | voltage = chg_config->voltage; |
| 662 | resistor = chg_config->resistor; |
| 663 | |
| 664 | if (resistor < PM8XXX_COINCELL_RESISTOR_2100_OHMS || |
| 665 | resistor > PM8XXX_COINCELL_RESISTOR_800_OHMS) { |
| 666 | pr_err("Invalid resistor value provided\n"); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if (voltage < PM8XXX_COINCELL_VOLTAGE_3p2V || |
| 671 | (voltage > PM8XXX_COINCELL_VOLTAGE_3p0V && |
| 672 | voltage != PM8XXX_COINCELL_VOLTAGE_2p5V)) { |
| 673 | pr_err("Invalid voltage value provided\n"); |
| 674 | return -EINVAL; |
| 675 | } |
| 676 | |
| 677 | if (chg_config->state == PM8XXX_COINCELL_CHG_DISABLE) { |
| 678 | reg = 0; |
| 679 | } else { |
| 680 | reg |= voltage; |
| 681 | reg |= (resistor << COINCELL_RESISTOR_SHIFT); |
| 682 | } |
| 683 | |
| 684 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 685 | |
| 686 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 687 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 688 | switch (chip->version) { |
| 689 | case PM8XXX_VERSION_8018: |
| 690 | rc = pm8xxx_writeb(chip->dev->parent, |
| 691 | REG_PM8018_COIN_CHG, reg); |
| 692 | break; |
| 693 | case PM8XXX_VERSION_8058: |
| 694 | rc = pm8xxx_writeb(chip->dev->parent, |
| 695 | REG_PM8058_COIN_CHG, reg); |
| 696 | break; |
| 697 | case PM8XXX_VERSION_8921: |
| 698 | rc = pm8xxx_writeb(chip->dev->parent, |
| 699 | REG_PM8921_COIN_CHG, reg); |
| 700 | break; |
| 701 | default: |
| 702 | /* PMIC doesn't have reset_pwr_off; do nothing. */ |
| 703 | break; |
| 704 | } |
| 705 | if (rc) { |
| 706 | pr_err("coincell chg. config failed, rc=%d\n", rc); |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 712 | |
| 713 | return rc; |
| 714 | } |
| 715 | EXPORT_SYMBOL(pm8xxx_coincell_chg_config); |
| 716 | |
Anirudh Ghayal | a23c1ca | 2011-11-01 14:36:24 +0530 | [diff] [blame] | 717 | /** |
| 718 | * pm8xxx_watchdog_reset_control - enables/disables watchdog reset detection |
| 719 | * @enable: 0 = shutdown when PS_HOLD goes low, 1 = reset when PS_HOLD goes low |
| 720 | * |
| 721 | * This function enables or disables the PMIC watchdog reset detection feature. |
| 722 | * If watchdog reset detection is enabled, then the PMIC will reset itself |
| 723 | * when PS_HOLD goes low. If it is not enabled, then the PMIC will shutdown |
| 724 | * when PS_HOLD goes low. |
| 725 | * |
| 726 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 727 | */ |
| 728 | int pm8xxx_watchdog_reset_control(int enable) |
| 729 | { |
| 730 | struct pm8xxx_misc_chip *chip; |
| 731 | unsigned long flags; |
| 732 | int rc = 0; |
| 733 | |
| 734 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 735 | |
| 736 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 737 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 738 | switch (chip->version) { |
| 739 | case PM8XXX_VERSION_8018: |
| 740 | case PM8XXX_VERSION_8058: |
| 741 | case PM8XXX_VERSION_8921: |
| 742 | rc = pm8xxx_misc_masked_write(chip, |
| 743 | REG_PM8XXX_PON_CTRL_1, PON_CTRL_1_WD_EN_MASK, |
| 744 | (enable ? PON_CTRL_1_WD_EN_RESET |
| 745 | : PON_CTRL_1_WD_EN_PWR_OFF)); |
| 746 | break; |
| 747 | default: |
| 748 | /* WD reset control not supported */ |
| 749 | break; |
| 750 | } |
| 751 | if (rc) { |
| 752 | pr_err("setting WD reset control failed, rc=%d\n", rc); |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 758 | |
| 759 | return rc; |
| 760 | } |
| 761 | EXPORT_SYMBOL(pm8xxx_watchdog_reset_control); |
| 762 | |
Anirudh Ghayal | 51e947f | 2011-11-01 14:49:45 +0530 | [diff] [blame] | 763 | /** |
| 764 | * pm8xxx_stay_on - enables stay_on feature |
| 765 | * |
| 766 | * PMIC stay-on feature allows PMIC to ignore MSM PS_HOLD=low |
| 767 | * signal so that some special functions like debugging could be |
| 768 | * performed. |
| 769 | * |
| 770 | * This feature should not be used in any product release. |
| 771 | * |
| 772 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 773 | */ |
| 774 | int pm8xxx_stay_on(void) |
| 775 | { |
| 776 | struct pm8xxx_misc_chip *chip; |
| 777 | unsigned long flags; |
| 778 | int rc = 0; |
| 779 | |
| 780 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 781 | |
| 782 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 783 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 784 | switch (chip->version) { |
| 785 | case PM8XXX_VERSION_8018: |
| 786 | case PM8XXX_VERSION_8058: |
| 787 | case PM8XXX_VERSION_8921: |
| 788 | rc = pm8xxx_writeb(chip->dev->parent, |
| 789 | REG_PM8XXX_GP_TEST_1, PM8XXX_STAY_ON_CFG); |
| 790 | break; |
| 791 | default: |
| 792 | /* stay on not supported */ |
| 793 | break; |
| 794 | } |
| 795 | if (rc) { |
| 796 | pr_err("stay_on failed failed, rc=%d\n", rc); |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 802 | |
| 803 | return rc; |
| 804 | } |
| 805 | EXPORT_SYMBOL(pm8xxx_stay_on); |
| 806 | |
Anirudh Ghayal | a4262a3 | 2011-11-10 00:02:18 +0530 | [diff] [blame] | 807 | static int |
| 808 | __pm8xxx_hard_reset_config(struct pm8xxx_misc_chip *chip, |
| 809 | enum pm8xxx_pon_config config, u16 pon4_addr, u16 pon5_addr) |
| 810 | { |
| 811 | int rc = 0; |
| 812 | |
| 813 | switch (config) { |
| 814 | case PM8XXX_DISABLE_HARD_RESET: |
| 815 | rc = pm8xxx_misc_masked_write(chip, pon5_addr, |
| 816 | PON_CTRL_5_HARD_RESET_EN_MASK, |
| 817 | PON_CTRL_5_HARD_RESET_DIS); |
| 818 | break; |
| 819 | case PM8XXX_SHUTDOWN_ON_HARD_RESET: |
| 820 | rc = pm8xxx_misc_masked_write(chip, pon5_addr, |
| 821 | PON_CTRL_5_HARD_RESET_EN_MASK, |
| 822 | PON_CTRL_5_HARD_RESET_EN); |
| 823 | if (!rc) { |
| 824 | rc = pm8xxx_misc_masked_write(chip, pon4_addr, |
| 825 | PON_CTRL_4_RESET_EN_MASK, |
| 826 | PON_CTRL_4_SHUTDOWN_ON_RESET); |
| 827 | } |
| 828 | break; |
| 829 | case PM8XXX_RESTART_ON_HARD_RESET: |
| 830 | rc = pm8xxx_misc_masked_write(chip, pon5_addr, |
| 831 | PON_CTRL_5_HARD_RESET_EN_MASK, |
| 832 | PON_CTRL_5_HARD_RESET_EN); |
| 833 | if (!rc) { |
| 834 | rc = pm8xxx_misc_masked_write(chip, pon4_addr, |
| 835 | PON_CTRL_4_RESET_EN_MASK, |
| 836 | PON_CTRL_4_RESTART_ON_RESET); |
| 837 | } |
| 838 | break; |
| 839 | default: |
| 840 | rc = -EINVAL; |
| 841 | break; |
| 842 | } |
| 843 | return rc; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * pm8xxx_hard_reset_config - Allows different reset configurations |
| 848 | * |
| 849 | * config = PM8XXX_DISABLE_HARD_RESET to disable hard reset |
| 850 | * = PM8XXX_SHUTDOWN_ON_HARD_RESET to turn off the system on hard reset |
| 851 | * = PM8XXX_RESTART_ON_HARD_RESET to restart the system on hard reset |
| 852 | * |
| 853 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 854 | */ |
| 855 | int pm8xxx_hard_reset_config(enum pm8xxx_pon_config config) |
| 856 | { |
| 857 | struct pm8xxx_misc_chip *chip; |
| 858 | unsigned long flags; |
| 859 | int rc = 0; |
| 860 | |
| 861 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 862 | |
| 863 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 864 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 865 | switch (chip->version) { |
| 866 | case PM8XXX_VERSION_8018: |
| 867 | __pm8xxx_hard_reset_config(chip, config, |
| 868 | REG_PM8018_PON_CNTL_4, REG_PM8018_PON_CNTL_5); |
| 869 | break; |
| 870 | case PM8XXX_VERSION_8058: |
| 871 | __pm8xxx_hard_reset_config(chip, config, |
| 872 | REG_PM8058_PON_CNTL_4, REG_PM8058_PON_CNTL_5); |
| 873 | break; |
| 874 | case PM8XXX_VERSION_8901: |
| 875 | __pm8xxx_hard_reset_config(chip, config, |
| 876 | REG_PM8901_PON_CNTL_4, REG_PM8901_PON_CNTL_5); |
| 877 | break; |
| 878 | case PM8XXX_VERSION_8921: |
| 879 | __pm8xxx_hard_reset_config(chip, config, |
| 880 | REG_PM8921_PON_CNTL_4, REG_PM8921_PON_CNTL_5); |
| 881 | break; |
| 882 | default: |
| 883 | /* hard reset config. no supported */ |
| 884 | break; |
| 885 | } |
| 886 | if (rc) { |
| 887 | pr_err("hard reset config. failed, rc=%d\n", rc); |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 893 | |
| 894 | return rc; |
| 895 | } |
| 896 | EXPORT_SYMBOL(pm8xxx_hard_reset_config); |
| 897 | |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 898 | /* Handle the OSC_HALT interrupt: 32 kHz XTAL oscillator has stopped. */ |
| 899 | static irqreturn_t pm8xxx_osc_halt_isr(int irq, void *data) |
| 900 | { |
| 901 | struct pm8xxx_misc_chip *chip = data; |
| 902 | u64 count = 0; |
| 903 | |
| 904 | if (chip) { |
| 905 | chip->osc_halt_count++; |
| 906 | count = chip->osc_halt_count; |
| 907 | } |
| 908 | |
| 909 | pr_crit("%s: OSC_HALT interrupt has triggered, 32 kHz XTAL oscillator" |
| 910 | " has halted (%llu)!\n", __func__, count); |
| 911 | |
| 912 | return IRQ_HANDLED; |
| 913 | } |
| 914 | |
Anirudh Ghayal | 5213eb8 | 2011-10-24 14:44:58 +0530 | [diff] [blame] | 915 | /** |
| 916 | * pm8xxx_uart_gpio_mux_ctrl - Mux configuration to select the UART |
| 917 | * |
| 918 | * @uart_path_sel: Input argument to select either UART1/2/3 |
| 919 | * |
| 920 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 921 | */ |
| 922 | int pm8xxx_uart_gpio_mux_ctrl(enum pm8xxx_uart_path_sel uart_path_sel) |
| 923 | { |
| 924 | struct pm8xxx_misc_chip *chip; |
| 925 | unsigned long flags; |
| 926 | int rc = 0; |
| 927 | |
| 928 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 929 | |
| 930 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 931 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 932 | switch (chip->version) { |
| 933 | case PM8XXX_VERSION_8018: |
| 934 | case PM8XXX_VERSION_8058: |
| 935 | case PM8XXX_VERSION_8921: |
| 936 | rc = pm8xxx_misc_masked_write(chip, |
| 937 | REG_PM8XXX_GPIO_MUX_CTRL, UART_PATH_SEL_MASK, |
| 938 | uart_path_sel << UART_PATH_SEL_SHIFT); |
| 939 | break; |
| 940 | default: |
| 941 | /* Functionality not supported */ |
| 942 | break; |
| 943 | } |
| 944 | if (rc) { |
| 945 | pr_err("uart_gpio_mux_ctrl failed, rc=%d\n", rc); |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 951 | |
| 952 | return rc; |
| 953 | } |
| 954 | EXPORT_SYMBOL(pm8xxx_uart_gpio_mux_ctrl); |
| 955 | |
Willie Ruan | 5db1f24 | 2012-01-30 22:08:04 -0800 | [diff] [blame] | 956 | /** |
| 957 | * pm8xxx_usb_id_pullup - Control a pullup for USB ID |
| 958 | * |
| 959 | * @enable: enable (1) or disable (0) the pullup |
| 960 | * |
| 961 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 962 | */ |
| 963 | int pm8xxx_usb_id_pullup(int enable) |
| 964 | { |
| 965 | struct pm8xxx_misc_chip *chip; |
| 966 | unsigned long flags; |
| 967 | int rc = -ENXIO; |
| 968 | |
| 969 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 970 | |
| 971 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 972 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 973 | switch (chip->version) { |
| 974 | case PM8XXX_VERSION_8921: |
| 975 | case PM8XXX_VERSION_8922: |
| 976 | case PM8XXX_VERSION_8917: |
| 977 | case PM8XXX_VERSION_8038: |
| 978 | rc = pm8xxx_misc_masked_write(chip, |
| 979 | REG_PM8XXX_GPIO_MUX_CTRL, USB_ID_PU_EN_MASK, |
| 980 | enable << USB_ID_PU_EN_SHIFT); |
| 981 | |
| 982 | if (rc) |
| 983 | pr_err("Fail: reg=%x, rc=%d\n", |
| 984 | REG_PM8XXX_GPIO_MUX_CTRL, rc); |
| 985 | break; |
| 986 | default: |
| 987 | /* Functionality not supported */ |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 993 | |
| 994 | return rc; |
| 995 | } |
| 996 | EXPORT_SYMBOL(pm8xxx_usb_id_pullup); |
| 997 | |
David Collins | 4724272 | 2012-01-20 11:34:58 -0800 | [diff] [blame] | 998 | static int __pm8901_preload_dVdd(struct pm8xxx_misc_chip *chip) |
| 999 | { |
| 1000 | int rc; |
| 1001 | |
David Collins | 135f3e0 | 2012-04-05 10:15:23 -0700 | [diff] [blame^] | 1002 | /* dVdd preloading is not needed for PMIC PM8901 rev 2.3 and beyond. */ |
| 1003 | if (pm8xxx_get_revision(chip->dev->parent) >= PM8XXX_REVISION_8901_2p3) |
| 1004 | return 0; |
| 1005 | |
David Collins | 4724272 | 2012-01-20 11:34:58 -0800 | [diff] [blame] | 1006 | rc = pm8xxx_writeb(chip->dev->parent, 0x0BD, 0x0F); |
| 1007 | if (rc) |
| 1008 | pr_err("pm8xxx_writeb failed for 0x0BD, rc=%d\n", rc); |
| 1009 | |
| 1010 | rc = pm8xxx_writeb(chip->dev->parent, 0x001, 0xB4); |
| 1011 | if (rc) |
| 1012 | pr_err("pm8xxx_writeb failed for 0x001, rc=%d\n", rc); |
| 1013 | |
| 1014 | pr_info("dVdd preloaded\n"); |
| 1015 | |
| 1016 | return rc; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * pm8xxx_preload_dVdd - preload the dVdd regulator during off state. |
| 1021 | * |
| 1022 | * This can help to reduce fluctuations in the dVdd voltage during startup |
| 1023 | * at the cost of additional off state current draw. |
| 1024 | * |
| 1025 | * This API should only be called if dVdd startup issues are suspected. |
| 1026 | * |
| 1027 | * RETURNS: an appropriate -ERRNO error value on error, or zero for success. |
| 1028 | */ |
| 1029 | int pm8xxx_preload_dVdd(void) |
| 1030 | { |
| 1031 | struct pm8xxx_misc_chip *chip; |
| 1032 | unsigned long flags; |
| 1033 | int rc = 0; |
| 1034 | |
| 1035 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 1036 | |
| 1037 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 1038 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 1039 | switch (chip->version) { |
| 1040 | case PM8XXX_VERSION_8901: |
| 1041 | rc = __pm8901_preload_dVdd(chip); |
| 1042 | break; |
| 1043 | default: |
| 1044 | /* PMIC doesn't have preload_dVdd; do nothing. */ |
| 1045 | break; |
| 1046 | } |
| 1047 | if (rc) { |
| 1048 | pr_err("preload_dVdd failed, rc=%d\n", rc); |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 1054 | |
| 1055 | return rc; |
| 1056 | } |
| 1057 | EXPORT_SYMBOL_GPL(pm8xxx_preload_dVdd); |
| 1058 | |
Amy Maloche | 4c994c9 | 2012-02-15 09:56:15 -0800 | [diff] [blame] | 1059 | int pm8xxx_aux_clk_control(enum pm8xxx_aux_clk_id clk_id, |
| 1060 | enum pm8xxx_aux_clk_div divider, bool enable) |
| 1061 | { |
| 1062 | struct pm8xxx_misc_chip *chip; |
| 1063 | unsigned long flags; |
| 1064 | u8 clk_mask = 0, value = 0; |
| 1065 | |
| 1066 | if (clk_id == CLK_MP3_1) { |
| 1067 | clk_mask = MP3_1_MASK; |
| 1068 | value = divider << MP3_1_SHIFT; |
| 1069 | } else if (clk_id == CLK_MP3_2) { |
| 1070 | clk_mask = MP3_2_MASK; |
| 1071 | value = divider << MP3_2_SHIFT; |
| 1072 | } else { |
| 1073 | pr_err("Invalid clock id of %d\n", clk_id); |
| 1074 | return -EINVAL; |
| 1075 | } |
| 1076 | if (!enable) |
| 1077 | value = 0; |
| 1078 | |
| 1079 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 1080 | |
| 1081 | /* Loop over all attached PMICs and call specific functions for them. */ |
| 1082 | list_for_each_entry(chip, &pm8xxx_misc_chips, link) { |
| 1083 | switch (chip->version) { |
| 1084 | case PM8XXX_VERSION_8038: |
| 1085 | case PM8XXX_VERSION_8921: |
| 1086 | pm8xxx_misc_masked_write(chip, |
| 1087 | REG_PM8XXX_XO_CNTRL_2, clk_mask, value); |
| 1088 | break; |
| 1089 | default: |
| 1090 | /* Functionality not supported */ |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | EXPORT_SYMBOL_GPL(pm8xxx_aux_clk_control); |
| 1100 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1101 | static int __devinit pm8xxx_misc_probe(struct platform_device *pdev) |
| 1102 | { |
| 1103 | const struct pm8xxx_misc_platform_data *pdata = pdev->dev.platform_data; |
| 1104 | struct pm8xxx_misc_chip *chip; |
| 1105 | struct pm8xxx_misc_chip *sibling; |
| 1106 | struct list_head *prev; |
| 1107 | unsigned long flags; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 1108 | int rc = 0, irq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1109 | |
| 1110 | if (!pdata) { |
| 1111 | pr_err("missing platform data\n"); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
| 1115 | chip = kzalloc(sizeof(struct pm8xxx_misc_chip), GFP_KERNEL); |
| 1116 | if (!chip) { |
| 1117 | pr_err("Cannot allocate %d bytes\n", |
| 1118 | sizeof(struct pm8xxx_misc_chip)); |
| 1119 | return -ENOMEM; |
| 1120 | } |
| 1121 | |
| 1122 | chip->dev = &pdev->dev; |
| 1123 | chip->version = pm8xxx_get_version(chip->dev->parent); |
| 1124 | memcpy(&(chip->pdata), pdata, sizeof(struct pm8xxx_misc_platform_data)); |
| 1125 | |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 1126 | irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq"); |
| 1127 | if (irq > 0) { |
| 1128 | rc = request_any_context_irq(irq, pm8xxx_osc_halt_isr, |
| 1129 | IRQF_TRIGGER_RISING | IRQF_DISABLED, |
| 1130 | "pm8xxx_osc_halt_irq", chip); |
| 1131 | if (rc < 0) { |
| 1132 | pr_err("%s: request_any_context_irq(%d) FAIL: %d\n", |
| 1133 | __func__, irq, rc); |
| 1134 | goto fail_irq; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1138 | /* Insert PMICs in priority order (lowest value first). */ |
| 1139 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 1140 | prev = &pm8xxx_misc_chips; |
| 1141 | list_for_each_entry(sibling, &pm8xxx_misc_chips, link) { |
| 1142 | if (chip->pdata.priority < sibling->pdata.priority) |
| 1143 | break; |
| 1144 | else |
| 1145 | prev = &sibling->link; |
| 1146 | } |
| 1147 | list_add(&chip->link, prev); |
| 1148 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 1149 | |
| 1150 | platform_set_drvdata(pdev, chip); |
| 1151 | |
| 1152 | return rc; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 1153 | |
| 1154 | fail_irq: |
| 1155 | platform_set_drvdata(pdev, NULL); |
| 1156 | kfree(chip); |
| 1157 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static int __devexit pm8xxx_misc_remove(struct platform_device *pdev) |
| 1161 | { |
| 1162 | struct pm8xxx_misc_chip *chip = platform_get_drvdata(pdev); |
| 1163 | unsigned long flags; |
Anirudh Ghayal | 8b8f189 | 2011-11-11 10:48:41 +0530 | [diff] [blame] | 1164 | int irq = platform_get_irq_byname(pdev, "pm8xxx_osc_halt_irq"); |
| 1165 | if (irq > 0) |
| 1166 | free_irq(irq, chip); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1167 | |
| 1168 | spin_lock_irqsave(&pm8xxx_misc_chips_lock, flags); |
| 1169 | list_del(&chip->link); |
| 1170 | spin_unlock_irqrestore(&pm8xxx_misc_chips_lock, flags); |
| 1171 | |
| 1172 | platform_set_drvdata(pdev, NULL); |
| 1173 | kfree(chip); |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static struct platform_driver pm8xxx_misc_driver = { |
| 1179 | .probe = pm8xxx_misc_probe, |
| 1180 | .remove = __devexit_p(pm8xxx_misc_remove), |
| 1181 | .driver = { |
| 1182 | .name = PM8XXX_MISC_DEV_NAME, |
| 1183 | .owner = THIS_MODULE, |
| 1184 | }, |
| 1185 | }; |
| 1186 | |
| 1187 | static int __init pm8xxx_misc_init(void) |
| 1188 | { |
| 1189 | return platform_driver_register(&pm8xxx_misc_driver); |
| 1190 | } |
| 1191 | postcore_initcall(pm8xxx_misc_init); |
| 1192 | |
| 1193 | static void __exit pm8xxx_misc_exit(void) |
| 1194 | { |
| 1195 | platform_driver_unregister(&pm8xxx_misc_driver); |
| 1196 | } |
| 1197 | module_exit(pm8xxx_misc_exit); |
| 1198 | |
| 1199 | MODULE_LICENSE("GPL v2"); |
| 1200 | MODULE_DESCRIPTION("PMIC 8XXX misc driver"); |
| 1201 | MODULE_VERSION("1.0"); |
| 1202 | MODULE_ALIAS("platform:" PM8XXX_MISC_DEV_NAME); |