Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/regulator/ab3100.c |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 ST-Ericsson AB |
| 5 | * License terms: GNU General Public License (GPL) version 2 |
| 6 | * Low-level control of the AB3100 IC Low Dropout (LDO) |
| 7 | * regulators, external regulator and buck converter |
| 8 | * Author: Mattias Wallin <mattias.wallin@stericsson.com> |
| 9 | * Author: Linus Walleij <linus.walleij@stericsson.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/regulator/driver.h> |
Linus Walleij | 812f9e9 | 2010-05-01 18:26:07 +0200 | [diff] [blame] | 19 | #include <linux/mfd/abx500.h> |
Andres Salomon | 5528e40 | 2011-02-17 19:07:12 -0800 | [diff] [blame] | 20 | #include <linux/mfd/core.h> |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 21 | |
| 22 | /* LDO registers and some handy masking definitions for AB3100 */ |
| 23 | #define AB3100_LDO_A 0x40 |
| 24 | #define AB3100_LDO_C 0x41 |
| 25 | #define AB3100_LDO_D 0x42 |
| 26 | #define AB3100_LDO_E 0x43 |
| 27 | #define AB3100_LDO_E_SLEEP 0x44 |
| 28 | #define AB3100_LDO_F 0x45 |
| 29 | #define AB3100_LDO_G 0x46 |
| 30 | #define AB3100_LDO_H 0x47 |
| 31 | #define AB3100_LDO_H_SLEEP_MODE 0 |
| 32 | #define AB3100_LDO_H_SLEEP_EN 2 |
| 33 | #define AB3100_LDO_ON 4 |
| 34 | #define AB3100_LDO_H_VSEL_AC 5 |
| 35 | #define AB3100_LDO_K 0x48 |
| 36 | #define AB3100_LDO_EXT 0x49 |
| 37 | #define AB3100_BUCK 0x4A |
| 38 | #define AB3100_BUCK_SLEEP 0x4B |
| 39 | #define AB3100_REG_ON_MASK 0x10 |
| 40 | |
| 41 | /** |
| 42 | * struct ab3100_regulator |
| 43 | * A struct passed around the individual regulator functions |
| 44 | * @platform_device: platform device holding this regulator |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 45 | * @dev: handle to the device |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 46 | * @plfdata: AB3100 platform data passed in at probe time |
| 47 | * @regreg: regulator register number in the AB3100 |
| 48 | * @fixed_voltage: a fixed voltage for this regulator, if this |
| 49 | * 0 the voltages array is used instead. |
| 50 | * @typ_voltages: an array of available typical voltages for |
| 51 | * this regulator |
| 52 | * @voltages_len: length of the array of available voltages |
| 53 | */ |
| 54 | struct ab3100_regulator { |
| 55 | struct regulator_dev *rdev; |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 56 | struct device *dev; |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 57 | struct ab3100_platform_data *plfdata; |
| 58 | u8 regreg; |
| 59 | int fixed_voltage; |
| 60 | int const *typ_voltages; |
| 61 | u8 voltages_len; |
| 62 | }; |
| 63 | |
| 64 | /* The order in which registers are initialized */ |
| 65 | static const u8 ab3100_reg_init_order[AB3100_NUM_REGULATORS+2] = { |
| 66 | AB3100_LDO_A, |
| 67 | AB3100_LDO_C, |
| 68 | AB3100_LDO_E, |
| 69 | AB3100_LDO_E_SLEEP, |
| 70 | AB3100_LDO_F, |
| 71 | AB3100_LDO_G, |
| 72 | AB3100_LDO_H, |
| 73 | AB3100_LDO_K, |
| 74 | AB3100_LDO_EXT, |
| 75 | AB3100_BUCK, |
| 76 | AB3100_BUCK_SLEEP, |
| 77 | AB3100_LDO_D, |
| 78 | }; |
| 79 | |
| 80 | /* Preset (hardware defined) voltages for these regulators */ |
| 81 | #define LDO_A_VOLTAGE 2750000 |
| 82 | #define LDO_C_VOLTAGE 2650000 |
| 83 | #define LDO_D_VOLTAGE 2650000 |
| 84 | |
Mark Brown | 9992ef4 | 2009-10-22 16:31:34 +0100 | [diff] [blame] | 85 | static const int ldo_e_buck_typ_voltages[] = { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 86 | 1800000, |
| 87 | 1400000, |
| 88 | 1300000, |
| 89 | 1200000, |
| 90 | 1100000, |
| 91 | 1050000, |
| 92 | 900000, |
| 93 | }; |
| 94 | |
Mark Brown | 9992ef4 | 2009-10-22 16:31:34 +0100 | [diff] [blame] | 95 | static const int ldo_f_typ_voltages[] = { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 96 | 1800000, |
| 97 | 1400000, |
| 98 | 1300000, |
| 99 | 1200000, |
| 100 | 1100000, |
| 101 | 1050000, |
| 102 | 2500000, |
| 103 | 2650000, |
| 104 | }; |
| 105 | |
Mark Brown | 9992ef4 | 2009-10-22 16:31:34 +0100 | [diff] [blame] | 106 | static const int ldo_g_typ_voltages[] = { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 107 | 2850000, |
| 108 | 2750000, |
| 109 | 1800000, |
| 110 | 1500000, |
| 111 | }; |
| 112 | |
Mark Brown | 9992ef4 | 2009-10-22 16:31:34 +0100 | [diff] [blame] | 113 | static const int ldo_h_typ_voltages[] = { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 114 | 2750000, |
| 115 | 1800000, |
| 116 | 1500000, |
| 117 | 1200000, |
| 118 | }; |
| 119 | |
Mark Brown | 9992ef4 | 2009-10-22 16:31:34 +0100 | [diff] [blame] | 120 | static const int ldo_k_typ_voltages[] = { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 121 | 2750000, |
| 122 | 1800000, |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /* The regulator devices */ |
| 127 | static struct ab3100_regulator |
| 128 | ab3100_regulators[AB3100_NUM_REGULATORS] = { |
| 129 | { |
| 130 | .regreg = AB3100_LDO_A, |
| 131 | .fixed_voltage = LDO_A_VOLTAGE, |
| 132 | }, |
| 133 | { |
| 134 | .regreg = AB3100_LDO_C, |
| 135 | .fixed_voltage = LDO_C_VOLTAGE, |
| 136 | }, |
| 137 | { |
| 138 | .regreg = AB3100_LDO_D, |
| 139 | .fixed_voltage = LDO_D_VOLTAGE, |
| 140 | }, |
| 141 | { |
| 142 | .regreg = AB3100_LDO_E, |
| 143 | .typ_voltages = ldo_e_buck_typ_voltages, |
| 144 | .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages), |
| 145 | }, |
| 146 | { |
| 147 | .regreg = AB3100_LDO_F, |
| 148 | .typ_voltages = ldo_f_typ_voltages, |
| 149 | .voltages_len = ARRAY_SIZE(ldo_f_typ_voltages), |
| 150 | }, |
| 151 | { |
| 152 | .regreg = AB3100_LDO_G, |
| 153 | .typ_voltages = ldo_g_typ_voltages, |
| 154 | .voltages_len = ARRAY_SIZE(ldo_g_typ_voltages), |
| 155 | }, |
| 156 | { |
| 157 | .regreg = AB3100_LDO_H, |
| 158 | .typ_voltages = ldo_h_typ_voltages, |
| 159 | .voltages_len = ARRAY_SIZE(ldo_h_typ_voltages), |
| 160 | }, |
| 161 | { |
| 162 | .regreg = AB3100_LDO_K, |
| 163 | .typ_voltages = ldo_k_typ_voltages, |
| 164 | .voltages_len = ARRAY_SIZE(ldo_k_typ_voltages), |
| 165 | }, |
| 166 | { |
| 167 | .regreg = AB3100_LDO_EXT, |
| 168 | /* No voltages for the external regulator */ |
| 169 | }, |
| 170 | { |
| 171 | .regreg = AB3100_BUCK, |
| 172 | .typ_voltages = ldo_e_buck_typ_voltages, |
| 173 | .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages), |
| 174 | }, |
| 175 | }; |
| 176 | |
| 177 | /* |
| 178 | * General functions for enable, disable and is_enabled used for |
| 179 | * LDO: A,C,E,F,G,H,K,EXT and BUCK |
| 180 | */ |
| 181 | static int ab3100_enable_regulator(struct regulator_dev *reg) |
| 182 | { |
| 183 | struct ab3100_regulator *abreg = reg->reg_data; |
| 184 | int err; |
| 185 | u8 regval; |
| 186 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 187 | err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 188 | ®val); |
| 189 | if (err) { |
| 190 | dev_warn(®->dev, "failed to get regid %d value\n", |
| 191 | abreg->regreg); |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | /* The regulator is already on, no reason to go further */ |
| 196 | if (regval & AB3100_REG_ON_MASK) |
| 197 | return 0; |
| 198 | |
| 199 | regval |= AB3100_REG_ON_MASK; |
| 200 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 201 | err = abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 202 | regval); |
| 203 | if (err) { |
| 204 | dev_warn(®->dev, "failed to set regid %d value\n", |
| 205 | abreg->regreg); |
| 206 | return err; |
| 207 | } |
| 208 | |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int ab3100_disable_regulator(struct regulator_dev *reg) |
| 213 | { |
| 214 | struct ab3100_regulator *abreg = reg->reg_data; |
| 215 | int err; |
| 216 | u8 regval; |
| 217 | |
| 218 | /* |
| 219 | * LDO D is a special regulator. When it is disabled, the entire |
| 220 | * system is shut down. So this is handled specially. |
| 221 | */ |
Linus Walleij | 176f45b | 2009-10-28 17:30:15 +0100 | [diff] [blame] | 222 | pr_info("Called ab3100_disable_regulator\n"); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 223 | if (abreg->regreg == AB3100_LDO_D) { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 224 | dev_info(®->dev, "disabling LDO D - shut down system\n"); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 225 | /* Setting LDO D to 0x00 cuts the power to the SoC */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 226 | return abx500_set_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 227 | AB3100_LDO_D, 0x00U); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * All other regulators are handled here |
| 232 | */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 233 | err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 234 | ®val); |
| 235 | if (err) { |
| 236 | dev_err(®->dev, "unable to get register 0x%x\n", |
| 237 | abreg->regreg); |
| 238 | return err; |
| 239 | } |
| 240 | regval &= ~AB3100_REG_ON_MASK; |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 241 | return abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 242 | regval); |
| 243 | } |
| 244 | |
| 245 | static int ab3100_is_enabled_regulator(struct regulator_dev *reg) |
| 246 | { |
| 247 | struct ab3100_regulator *abreg = reg->reg_data; |
| 248 | u8 regval; |
| 249 | int err; |
| 250 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 251 | err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 252 | ®val); |
| 253 | if (err) { |
| 254 | dev_err(®->dev, "unable to get register 0x%x\n", |
| 255 | abreg->regreg); |
| 256 | return err; |
| 257 | } |
| 258 | |
| 259 | return regval & AB3100_REG_ON_MASK; |
| 260 | } |
| 261 | |
| 262 | static int ab3100_list_voltage_regulator(struct regulator_dev *reg, |
| 263 | unsigned selector) |
| 264 | { |
| 265 | struct ab3100_regulator *abreg = reg->reg_data; |
| 266 | |
Axel Lin | 979da89 | 2010-07-26 15:34:14 +0800 | [diff] [blame] | 267 | if (selector >= abreg->voltages_len) |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | return abreg->typ_voltages[selector]; |
| 270 | } |
| 271 | |
| 272 | static int ab3100_get_voltage_regulator(struct regulator_dev *reg) |
| 273 | { |
| 274 | struct ab3100_regulator *abreg = reg->reg_data; |
| 275 | u8 regval; |
| 276 | int err; |
| 277 | |
| 278 | /* Return the voltage for fixed regulators immediately */ |
| 279 | if (abreg->fixed_voltage) |
| 280 | return abreg->fixed_voltage; |
| 281 | |
| 282 | /* |
| 283 | * For variable types, read out setting and index into |
| 284 | * supplied voltage list. |
| 285 | */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 286 | err = abx500_get_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 287 | abreg->regreg, ®val); |
| 288 | if (err) { |
| 289 | dev_warn(®->dev, |
| 290 | "failed to get regulator value in register %02x\n", |
| 291 | abreg->regreg); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | /* The 3 highest bits index voltages */ |
| 296 | regval &= 0xE0; |
| 297 | regval >>= 5; |
| 298 | |
Axel Lin | 979da89 | 2010-07-26 15:34:14 +0800 | [diff] [blame] | 299 | if (regval >= abreg->voltages_len) { |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 300 | dev_err(®->dev, |
| 301 | "regulator register %02x contains an illegal voltage setting\n", |
| 302 | abreg->regreg); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
| 306 | return abreg->typ_voltages[regval]; |
| 307 | } |
| 308 | |
| 309 | static int ab3100_get_best_voltage_index(struct regulator_dev *reg, |
| 310 | int min_uV, int max_uV) |
| 311 | { |
| 312 | struct ab3100_regulator *abreg = reg->reg_data; |
| 313 | int i; |
| 314 | int bestmatch; |
| 315 | int bestindex; |
| 316 | |
| 317 | /* |
| 318 | * Locate the minimum voltage fitting the criteria on |
| 319 | * this regulator. The switchable voltages are not |
| 320 | * in strict falling order so we need to check them |
| 321 | * all for the best match. |
| 322 | */ |
| 323 | bestmatch = INT_MAX; |
| 324 | bestindex = -1; |
| 325 | for (i = 0; i < abreg->voltages_len; i++) { |
| 326 | if (abreg->typ_voltages[i] <= max_uV && |
| 327 | abreg->typ_voltages[i] >= min_uV && |
| 328 | abreg->typ_voltages[i] < bestmatch) { |
| 329 | bestmatch = abreg->typ_voltages[i]; |
| 330 | bestindex = i; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (bestindex < 0) { |
| 335 | dev_warn(®->dev, "requested %d<=x<=%d uV, out of range!\n", |
| 336 | min_uV, max_uV); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | return bestindex; |
| 340 | } |
| 341 | |
| 342 | static int ab3100_set_voltage_regulator(struct regulator_dev *reg, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 343 | int min_uV, int max_uV, |
| 344 | unsigned *selector) |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 345 | { |
| 346 | struct ab3100_regulator *abreg = reg->reg_data; |
| 347 | u8 regval; |
| 348 | int err; |
| 349 | int bestindex; |
| 350 | |
| 351 | bestindex = ab3100_get_best_voltage_index(reg, min_uV, max_uV); |
| 352 | if (bestindex < 0) |
| 353 | return bestindex; |
| 354 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 355 | *selector = bestindex; |
| 356 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 357 | err = abx500_get_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 358 | abreg->regreg, ®val); |
| 359 | if (err) { |
| 360 | dev_warn(®->dev, |
| 361 | "failed to get regulator register %02x\n", |
| 362 | abreg->regreg); |
| 363 | return err; |
| 364 | } |
| 365 | |
| 366 | /* The highest three bits control the variable regulators */ |
| 367 | regval &= ~0xE0; |
| 368 | regval |= (bestindex << 5); |
| 369 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 370 | err = abx500_set_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 371 | abreg->regreg, regval); |
| 372 | if (err) |
| 373 | dev_warn(®->dev, "failed to set regulator register %02x\n", |
| 374 | abreg->regreg); |
| 375 | |
| 376 | return err; |
| 377 | } |
| 378 | |
| 379 | static int ab3100_set_suspend_voltage_regulator(struct regulator_dev *reg, |
| 380 | int uV) |
| 381 | { |
| 382 | struct ab3100_regulator *abreg = reg->reg_data; |
| 383 | u8 regval; |
| 384 | int err; |
| 385 | int bestindex; |
| 386 | u8 targetreg; |
| 387 | |
| 388 | if (abreg->regreg == AB3100_LDO_E) |
| 389 | targetreg = AB3100_LDO_E_SLEEP; |
| 390 | else if (abreg->regreg == AB3100_BUCK) |
| 391 | targetreg = AB3100_BUCK_SLEEP; |
| 392 | else |
| 393 | return -EINVAL; |
| 394 | |
| 395 | /* LDO E and BUCK have special suspend voltages you can set */ |
| 396 | bestindex = ab3100_get_best_voltage_index(reg, uV, uV); |
| 397 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 398 | err = abx500_get_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 399 | targetreg, ®val); |
| 400 | if (err) { |
| 401 | dev_warn(®->dev, |
| 402 | "failed to get regulator register %02x\n", |
| 403 | targetreg); |
| 404 | return err; |
| 405 | } |
| 406 | |
| 407 | /* The highest three bits control the variable regulators */ |
| 408 | regval &= ~0xE0; |
| 409 | regval |= (bestindex << 5); |
| 410 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 411 | err = abx500_set_register_interruptible(abreg->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 412 | targetreg, regval); |
| 413 | if (err) |
| 414 | dev_warn(®->dev, "failed to set regulator register %02x\n", |
| 415 | abreg->regreg); |
| 416 | |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * The external regulator can just define a fixed voltage. |
| 422 | */ |
| 423 | static int ab3100_get_voltage_regulator_external(struct regulator_dev *reg) |
| 424 | { |
| 425 | struct ab3100_regulator *abreg = reg->reg_data; |
| 426 | |
| 427 | return abreg->plfdata->external_voltage; |
| 428 | } |
| 429 | |
Linus Walleij | 19c9882 | 2011-03-11 16:26:18 +0100 | [diff] [blame^] | 430 | static int ab3100_enable_time_regulator(struct regulator_dev *reg) |
| 431 | { |
| 432 | struct ab3100_regulator *abreg = reg->reg_data; |
| 433 | |
| 434 | /* Per-regulator power on delay from spec */ |
| 435 | switch (abreg->regreg) { |
| 436 | case AB3100_LDO_A: /* Fallthrough */ |
| 437 | case AB3100_LDO_C: /* Fallthrough */ |
| 438 | case AB3100_LDO_D: /* Fallthrough */ |
| 439 | case AB3100_LDO_E: /* Fallthrough */ |
| 440 | case AB3100_LDO_H: /* Fallthrough */ |
| 441 | case AB3100_LDO_K: |
| 442 | return 200; |
| 443 | case AB3100_LDO_F: |
| 444 | return 600; |
| 445 | case AB3100_LDO_G: |
| 446 | return 400; |
| 447 | case AB3100_BUCK: |
| 448 | return 1000; |
| 449 | default: |
| 450 | break; |
| 451 | } |
| 452 | return 0; |
| 453 | } |
| 454 | |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 455 | static struct regulator_ops regulator_ops_fixed = { |
| 456 | .enable = ab3100_enable_regulator, |
| 457 | .disable = ab3100_disable_regulator, |
| 458 | .is_enabled = ab3100_is_enabled_regulator, |
| 459 | .get_voltage = ab3100_get_voltage_regulator, |
Linus Walleij | 19c9882 | 2011-03-11 16:26:18 +0100 | [diff] [blame^] | 460 | .enable_time = ab3100_enable_time_regulator, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | static struct regulator_ops regulator_ops_variable = { |
| 464 | .enable = ab3100_enable_regulator, |
| 465 | .disable = ab3100_disable_regulator, |
| 466 | .is_enabled = ab3100_is_enabled_regulator, |
| 467 | .get_voltage = ab3100_get_voltage_regulator, |
| 468 | .set_voltage = ab3100_set_voltage_regulator, |
| 469 | .list_voltage = ab3100_list_voltage_regulator, |
Linus Walleij | 19c9882 | 2011-03-11 16:26:18 +0100 | [diff] [blame^] | 470 | .enable_time = ab3100_enable_time_regulator, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static struct regulator_ops regulator_ops_variable_sleepable = { |
| 474 | .enable = ab3100_enable_regulator, |
| 475 | .disable = ab3100_disable_regulator, |
| 476 | .is_enabled = ab3100_is_enabled_regulator, |
| 477 | .get_voltage = ab3100_get_voltage_regulator, |
| 478 | .set_voltage = ab3100_set_voltage_regulator, |
| 479 | .set_suspend_voltage = ab3100_set_suspend_voltage_regulator, |
| 480 | .list_voltage = ab3100_list_voltage_regulator, |
Linus Walleij | 19c9882 | 2011-03-11 16:26:18 +0100 | [diff] [blame^] | 481 | .enable_time = ab3100_enable_time_regulator, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | /* |
| 485 | * LDO EXT is an external regulator so it is really |
| 486 | * not possible to set any voltage locally here, AB3100 |
| 487 | * is an on/off switch plain an simple. The external |
| 488 | * voltage is defined in the board set-up if any. |
| 489 | */ |
| 490 | static struct regulator_ops regulator_ops_external = { |
| 491 | .enable = ab3100_enable_regulator, |
| 492 | .disable = ab3100_disable_regulator, |
| 493 | .is_enabled = ab3100_is_enabled_regulator, |
| 494 | .get_voltage = ab3100_get_voltage_regulator_external, |
| 495 | }; |
| 496 | |
| 497 | static struct regulator_desc |
| 498 | ab3100_regulator_desc[AB3100_NUM_REGULATORS] = { |
| 499 | { |
| 500 | .name = "LDO_A", |
| 501 | .id = AB3100_LDO_A, |
| 502 | .ops = ®ulator_ops_fixed, |
| 503 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 504 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 505 | }, |
| 506 | { |
| 507 | .name = "LDO_C", |
| 508 | .id = AB3100_LDO_C, |
| 509 | .ops = ®ulator_ops_fixed, |
| 510 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 511 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 512 | }, |
| 513 | { |
| 514 | .name = "LDO_D", |
| 515 | .id = AB3100_LDO_D, |
| 516 | .ops = ®ulator_ops_fixed, |
| 517 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 518 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 519 | }, |
| 520 | { |
| 521 | .name = "LDO_E", |
| 522 | .id = AB3100_LDO_E, |
| 523 | .ops = ®ulator_ops_variable_sleepable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 524 | .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 525 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 526 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 527 | }, |
| 528 | { |
| 529 | .name = "LDO_F", |
| 530 | .id = AB3100_LDO_F, |
| 531 | .ops = ®ulator_ops_variable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 532 | .n_voltages = ARRAY_SIZE(ldo_f_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 533 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 534 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 535 | }, |
| 536 | { |
| 537 | .name = "LDO_G", |
| 538 | .id = AB3100_LDO_G, |
| 539 | .ops = ®ulator_ops_variable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 540 | .n_voltages = ARRAY_SIZE(ldo_g_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 541 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 542 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 543 | }, |
| 544 | { |
| 545 | .name = "LDO_H", |
| 546 | .id = AB3100_LDO_H, |
| 547 | .ops = ®ulator_ops_variable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 548 | .n_voltages = ARRAY_SIZE(ldo_h_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 549 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 550 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 551 | }, |
| 552 | { |
| 553 | .name = "LDO_K", |
| 554 | .id = AB3100_LDO_K, |
| 555 | .ops = ®ulator_ops_variable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 556 | .n_voltages = ARRAY_SIZE(ldo_k_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 557 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 558 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 559 | }, |
| 560 | { |
| 561 | .name = "LDO_EXT", |
| 562 | .id = AB3100_LDO_EXT, |
| 563 | .ops = ®ulator_ops_external, |
| 564 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 565 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 566 | }, |
| 567 | { |
| 568 | .name = "BUCK", |
| 569 | .id = AB3100_BUCK, |
| 570 | .ops = ®ulator_ops_variable_sleepable, |
Linus Walleij | 75f2ba8 | 2009-09-17 09:17:33 +0200 | [diff] [blame] | 571 | .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 572 | .type = REGULATOR_VOLTAGE, |
Axel Lin | 6471435 | 2010-05-13 17:33:01 +0800 | [diff] [blame] | 573 | .owner = THIS_MODULE, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 574 | }, |
| 575 | }; |
| 576 | |
| 577 | /* |
| 578 | * NOTE: the following functions are regulators pluralis - it is the |
| 579 | * binding to the AB3100 core driver and the parent platform device |
| 580 | * for all the different regulators. |
| 581 | */ |
| 582 | |
Dmitry Torokhov | 98bf7c0 | 2010-02-23 23:37:50 -0800 | [diff] [blame] | 583 | static int __devinit ab3100_regulators_probe(struct platform_device *pdev) |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 584 | { |
Andres Salomon | 5528e40 | 2011-02-17 19:07:12 -0800 | [diff] [blame] | 585 | struct ab3100_platform_data *plfdata = mfd_get_data(pdev); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 586 | int err = 0; |
| 587 | u8 data; |
| 588 | int i; |
| 589 | |
| 590 | /* Check chip state */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 591 | err = abx500_get_register_interruptible(&pdev->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 592 | AB3100_LDO_D, &data); |
| 593 | if (err) { |
| 594 | dev_err(&pdev->dev, "could not read initial status of LDO_D\n"); |
| 595 | return err; |
| 596 | } |
| 597 | if (data & 0x10) |
| 598 | dev_notice(&pdev->dev, |
| 599 | "chip is already in active mode (Warm start)\n"); |
| 600 | else |
| 601 | dev_notice(&pdev->dev, |
| 602 | "chip is in inactive mode (Cold start)\n"); |
| 603 | |
| 604 | /* Set up regulators */ |
| 605 | for (i = 0; i < ARRAY_SIZE(ab3100_reg_init_order); i++) { |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 606 | err = abx500_set_register_interruptible(&pdev->dev, 0, |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 607 | ab3100_reg_init_order[i], |
| 608 | plfdata->reg_initvals[i]); |
| 609 | if (err) { |
| 610 | dev_err(&pdev->dev, "regulator initialization failed with error %d\n", |
| 611 | err); |
| 612 | return err; |
| 613 | } |
| 614 | } |
| 615 | |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 616 | /* Register the regulators */ |
| 617 | for (i = 0; i < AB3100_NUM_REGULATORS; i++) { |
| 618 | struct ab3100_regulator *reg = &ab3100_regulators[i]; |
| 619 | struct regulator_dev *rdev; |
| 620 | |
| 621 | /* |
| 622 | * Initialize per-regulator struct. |
| 623 | * Inherit platform data, this comes down from the |
| 624 | * i2c boarddata, from the machine. So if you want to |
| 625 | * see what it looks like for a certain machine, go |
| 626 | * into the machine I2C setup. |
| 627 | */ |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 628 | reg->dev = &pdev->dev; |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 629 | reg->plfdata = plfdata; |
| 630 | |
| 631 | /* |
| 632 | * Register the regulator, pass around |
| 633 | * the ab3100_regulator struct |
| 634 | */ |
| 635 | rdev = regulator_register(&ab3100_regulator_desc[i], |
| 636 | &pdev->dev, |
| 637 | &plfdata->reg_constraints[i], |
| 638 | reg); |
| 639 | |
| 640 | if (IS_ERR(rdev)) { |
| 641 | err = PTR_ERR(rdev); |
| 642 | dev_err(&pdev->dev, |
| 643 | "%s: failed to register regulator %s err %d\n", |
| 644 | __func__, ab3100_regulator_desc[i].name, |
| 645 | err); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 646 | /* remove the already registered regulators */ |
Axel Lin | b3fcf3e | 2010-08-14 21:31:01 +0800 | [diff] [blame] | 647 | while (--i >= 0) |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 648 | regulator_unregister(ab3100_regulators[i].rdev); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 649 | return err; |
| 650 | } |
| 651 | |
| 652 | /* Then set a pointer back to the registered regulator */ |
| 653 | reg->rdev = rdev; |
| 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Dmitry Torokhov | 98bf7c0 | 2010-02-23 23:37:50 -0800 | [diff] [blame] | 659 | static int __devexit ab3100_regulators_remove(struct platform_device *pdev) |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 660 | { |
| 661 | int i; |
| 662 | |
| 663 | for (i = 0; i < AB3100_NUM_REGULATORS; i++) { |
| 664 | struct ab3100_regulator *reg = &ab3100_regulators[i]; |
| 665 | |
| 666 | regulator_unregister(reg->rdev); |
| 667 | } |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static struct platform_driver ab3100_regulators_driver = { |
| 672 | .driver = { |
| 673 | .name = "ab3100-regulators", |
| 674 | .owner = THIS_MODULE, |
| 675 | }, |
| 676 | .probe = ab3100_regulators_probe, |
Dmitry Torokhov | 98bf7c0 | 2010-02-23 23:37:50 -0800 | [diff] [blame] | 677 | .remove = __devexit_p(ab3100_regulators_remove), |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 678 | }; |
| 679 | |
| 680 | static __init int ab3100_regulators_init(void) |
| 681 | { |
| 682 | return platform_driver_register(&ab3100_regulators_driver); |
| 683 | } |
| 684 | |
| 685 | static __exit void ab3100_regulators_exit(void) |
| 686 | { |
Linus Walleij | 176f45b | 2009-10-28 17:30:15 +0100 | [diff] [blame] | 687 | platform_driver_unregister(&ab3100_regulators_driver); |
Linus Walleij | d619bc1 | 2009-09-09 11:31:00 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | subsys_initcall(ab3100_regulators_init); |
| 691 | module_exit(ab3100_regulators_exit); |
| 692 | |
| 693 | MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>"); |
| 694 | MODULE_DESCRIPTION("AB3100 Regulator driver"); |
| 695 | MODULE_LICENSE("GPL"); |
| 696 | MODULE_ALIAS("platform:ab3100-regulators"); |