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