Blame view
drivers/regulator/lp872x.c
25.8 KB
af8b5fc31 regulator: add ne... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* * Copyright 2012 Texas Instruments * * Author: Milo(Woogyom) Kim <milo.kim@ti.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * */ #include <linux/module.h> #include <linux/slab.h> #include <linux/i2c.h> #include <linux/regmap.h> #include <linux/err.h> #include <linux/gpio.h> |
7e6213f43 regulator: lp872x... |
18 |
#include <linux/delay.h> |
af8b5fc31 regulator: add ne... |
19 20 21 |
#include <linux/regulator/lp872x.h> #include <linux/regulator/driver.h> #include <linux/platform_device.h> |
00fd6e615 regulator: lp872x... |
22 23 24 |
#include <linux/of.h> #include <linux/of_gpio.h> #include <linux/regulator/of_regulator.h> |
af8b5fc31 regulator: add ne... |
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
/* Registers : LP8720/8725 shared */ #define LP872X_GENERAL_CFG 0x00 #define LP872X_LDO1_VOUT 0x01 #define LP872X_LDO2_VOUT 0x02 #define LP872X_LDO3_VOUT 0x03 #define LP872X_LDO4_VOUT 0x04 #define LP872X_LDO5_VOUT 0x05 /* Registers : LP8720 */ #define LP8720_BUCK_VOUT1 0x06 #define LP8720_BUCK_VOUT2 0x07 #define LP8720_ENABLE 0x08 /* Registers : LP8725 */ #define LP8725_LILO1_VOUT 0x06 #define LP8725_LILO2_VOUT 0x07 #define LP8725_BUCK1_VOUT1 0x08 #define LP8725_BUCK1_VOUT2 0x09 #define LP8725_BUCK2_VOUT1 0x0A #define LP8725_BUCK2_VOUT2 0x0B #define LP8725_BUCK_CTRL 0x0C #define LP8725_LDO_CTRL 0x0D /* Mask/shift : LP8720/LP8725 shared */ #define LP872X_VOUT_M 0x1F #define LP872X_START_DELAY_M 0xE0 #define LP872X_START_DELAY_S 5 #define LP872X_EN_LDO1_M BIT(0) #define LP872X_EN_LDO2_M BIT(1) #define LP872X_EN_LDO3_M BIT(2) #define LP872X_EN_LDO4_M BIT(3) #define LP872X_EN_LDO5_M BIT(4) /* Mask/shift : LP8720 */ #define LP8720_TIMESTEP_S 0 /* Addr 00h */ #define LP8720_TIMESTEP_M BIT(0) #define LP8720_EXT_DVS_M BIT(2) #define LP8720_BUCK_FPWM_S 5 /* Addr 07h */ #define LP8720_BUCK_FPWM_M BIT(5) #define LP8720_EN_BUCK_M BIT(5) /* Addr 08h */ #define LP8720_DVS_SEL_M BIT(7) /* Mask/shift : LP8725 */ #define LP8725_TIMESTEP_M 0xC0 /* Addr 00h */ #define LP8725_TIMESTEP_S 6 #define LP8725_BUCK1_EN_M BIT(0) #define LP8725_DVS1_M BIT(2) #define LP8725_DVS2_M BIT(3) #define LP8725_BUCK2_EN_M BIT(4) #define LP8725_BUCK_CL_M 0xC0 /* Addr 09h, 0Bh */ #define LP8725_BUCK_CL_S 6 #define LP8725_BUCK1_FPWM_S 1 /* Addr 0Ch */ #define LP8725_BUCK1_FPWM_M BIT(1) #define LP8725_BUCK2_FPWM_S 5 #define LP8725_BUCK2_FPWM_M BIT(5) #define LP8725_EN_LILO1_M BIT(5) /* Addr 0Dh */ #define LP8725_EN_LILO2_M BIT(6) /* PWM mode */ #define LP872X_FORCE_PWM 1 #define LP872X_AUTO_PWM 0 #define LP8720_NUM_REGULATORS 6 #define LP8725_NUM_REGULATORS 9 #define EXTERN_DVS_USED 0 #define MAX_DELAY 6 |
b158fba60 regulator: lp872x... |
92 93 94 |
/* Default DVS Mode */ #define LP8720_DEFAULT_DVS 0 #define LP8725_DEFAULT_DVS BIT(2) |
af8b5fc31 regulator: add ne... |
95 96 97 98 99 100 101 102 103 104 105 106 107 |
/* dump registers in regmap-debugfs */ #define MAX_REGISTERS 0x0F enum lp872x_id { LP8720, LP8725, }; struct lp872x { struct regmap *regmap; struct device *dev; enum lp872x_id chipid; struct lp872x_platform_data *pdata; |
af8b5fc31 regulator: add ne... |
108 109 |
int num_regulators; enum lp872x_dvs_state dvs_pin; |
af8b5fc31 regulator: add ne... |
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
}; /* LP8720/LP8725 shared voltage table for LDOs */ static const unsigned int lp872x_ldo_vtbl[] = { 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000, }; /* LP8720 LDO4 voltage table */ static const unsigned int lp8720_ldo4_vtbl[] = { 800000, 850000, 900000, 1000000, 1100000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000, }; /* LP8725 LILO(Low Input Low Output) voltage table */ static const unsigned int lp8725_lilo_vtbl[] = { 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000, }; /* LP8720 BUCK voltage table */ #define EXT_R 0 /* external resistor divider */ static const unsigned int lp8720_buck_vtbl[] = { EXT_R, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000, }; /* LP8725 BUCK voltage table */ static const unsigned int lp8725_buck_vtbl[] = { 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000, }; /* LP8725 BUCK current limit */ static const unsigned int lp8725_buck_uA[] = { 460000, 780000, 1050000, 1370000, }; static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data) { int ret; unsigned int val; ret = regmap_read(lp->regmap, addr, &val); if (ret < 0) { dev_err(lp->dev, "failed to read 0x%.2x ", addr); return ret; } *data = (u8)val; return 0; } static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data) { return regmap_write(lp->regmap, addr, data); } static inline int lp872x_update_bits(struct lp872x *lp, u8 addr, unsigned int mask, u8 data) { return regmap_update_bits(lp->regmap, addr, mask, data); } |
af8b5fc31 regulator: add ne... |
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
static int lp872x_get_timestep_usec(struct lp872x *lp) { enum lp872x_id chip = lp->chipid; u8 val, mask, shift; int *time_usec, size, ret; int lp8720_time_usec[] = { 25, 50 }; int lp8725_time_usec[] = { 32, 64, 128, 256 }; switch (chip) { case LP8720: mask = LP8720_TIMESTEP_M; shift = LP8720_TIMESTEP_S; time_usec = &lp8720_time_usec[0]; size = ARRAY_SIZE(lp8720_time_usec); break; case LP8725: mask = LP8725_TIMESTEP_M; shift = LP8725_TIMESTEP_S; time_usec = &lp8725_time_usec[0]; size = ARRAY_SIZE(lp8725_time_usec); break; default: return -EINVAL; } ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val); if (ret) |
ad5ec6cdb regulator: lp872x... |
211 |
return ret; |
af8b5fc31 regulator: add ne... |
212 213 214 215 216 217 218 219 220 221 222 |
val = (val & mask) >> shift; if (val >= size) return -EINVAL; return *(time_usec + val); } static int lp872x_regulator_enable_time(struct regulator_dev *rdev) { struct lp872x *lp = rdev_get_drvdata(rdev); |
2c1299278 regulator: lp872x... |
223 |
enum lp872x_regulator_id rid = rdev_get_id(rdev); |
af8b5fc31 regulator: add ne... |
224 |
int time_step_us = lp872x_get_timestep_usec(lp); |
2c1299278 regulator: lp872x... |
225 |
int ret; |
af8b5fc31 regulator: add ne... |
226 227 228 |
u8 addr, val; if (time_step_us < 0) |
ad5ec6cdb regulator: lp872x... |
229 |
return time_step_us; |
af8b5fc31 regulator: add ne... |
230 |
|
2c1299278 regulator: lp872x... |
231 232 233 |
switch (rid) { case LP8720_ID_LDO1 ... LP8720_ID_BUCK: addr = LP872X_LDO1_VOUT + rid; |
af8b5fc31 regulator: add ne... |
234 |
break; |
2c1299278 regulator: lp872x... |
235 236 |
case LP8725_ID_LDO1 ... LP8725_ID_BUCK1: addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE; |
af8b5fc31 regulator: add ne... |
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
break; case LP8725_ID_BUCK2: addr = LP8725_BUCK2_VOUT1; break; default: return -EINVAL; } ret = lp872x_read_byte(lp, addr, &val); if (ret) return ret; val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S; return val > MAX_DELAY ? 0 : val * time_step_us; } |
9d6da6fcc regulator: lp872x... |
253 254 |
static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel, int gpio) |
af8b5fc31 regulator: add ne... |
255 |
{ |
af8b5fc31 regulator: add ne... |
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
enum lp872x_dvs_state state; state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW; gpio_set_value(gpio, state); lp->dvs_pin = state; } static u8 lp872x_select_buck_vout_addr(struct lp872x *lp, enum lp872x_regulator_id buck) { u8 val, addr; if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val)) return 0; switch (buck) { case LP8720_ID_BUCK: if (val & LP8720_EXT_DVS_M) { addr = (lp->dvs_pin == DVS_HIGH) ? LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2; } else { if (lp872x_read_byte(lp, LP8720_ENABLE, &val)) return 0; addr = val & LP8720_DVS_SEL_M ? LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2; } break; case LP8725_ID_BUCK1: if (val & LP8725_DVS1_M) addr = LP8725_BUCK1_VOUT1; else addr = (lp->dvs_pin == DVS_HIGH) ? LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2; break; case LP8725_ID_BUCK2: addr = val & LP8725_DVS2_M ? LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2; break; default: return 0; } return addr; } static bool lp872x_is_valid_buck_addr(u8 addr) { switch (addr) { case LP8720_BUCK_VOUT1: case LP8720_BUCK_VOUT2: case LP8725_BUCK1_VOUT1: case LP8725_BUCK1_VOUT2: case LP8725_BUCK2_VOUT1: case LP8725_BUCK2_VOUT2: return true; default: return false; } } static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev, unsigned selector) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); u8 addr, mask = LP872X_VOUT_M; |
9d6da6fcc regulator: lp872x... |
323 |
struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL; |
af8b5fc31 regulator: add ne... |
324 325 |
if (dvs && gpio_is_valid(dvs->gpio)) |
9d6da6fcc regulator: lp872x... |
326 |
lp872x_set_dvs(lp, dvs->vsel, dvs->gpio); |
af8b5fc31 regulator: add ne... |
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
addr = lp872x_select_buck_vout_addr(lp, buck); if (!lp872x_is_valid_buck_addr(addr)) return -EINVAL; return lp872x_update_bits(lp, addr, mask, selector); } static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); u8 addr, val; int ret; addr = lp872x_select_buck_vout_addr(lp, buck); if (!lp872x_is_valid_buck_addr(addr)) return -EINVAL; ret = lp872x_read_byte(lp, addr, &val); if (ret) return ret; return val & LP872X_VOUT_M; } static int lp8725_buck_set_current_limit(struct regulator_dev *rdev, int min_uA, int max_uA) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); |
4e1d67edd regulator: lp872x... |
358 359 |
int i; u8 addr; |
af8b5fc31 regulator: add ne... |
360 361 362 363 364 365 366 367 368 369 370 |
switch (buck) { case LP8725_ID_BUCK1: addr = LP8725_BUCK1_VOUT2; break; case LP8725_ID_BUCK2: addr = LP8725_BUCK2_VOUT2; break; default: return -EINVAL; } |
37a6f43dd regulator: lp872x... |
371 |
for (i = ARRAY_SIZE(lp8725_buck_uA) - 1; i >= 0; i--) { |
af8b5fc31 regulator: add ne... |
372 373 |
if (lp8725_buck_uA[i] >= min_uA && lp8725_buck_uA[i] <= max_uA) |
4e1d67edd regulator: lp872x... |
374 375 376 377 |
return lp872x_update_bits(lp, addr, LP8725_BUCK_CL_M, i << LP8725_BUCK_CL_S); } |
af8b5fc31 regulator: add ne... |
378 |
|
4e1d67edd regulator: lp872x... |
379 |
return -EINVAL; |
af8b5fc31 regulator: add ne... |
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
} static int lp8725_buck_get_current_limit(struct regulator_dev *rdev) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); u8 addr, val; int ret; switch (buck) { case LP8725_ID_BUCK1: addr = LP8725_BUCK1_VOUT2; break; case LP8725_ID_BUCK2: addr = LP8725_BUCK2_VOUT2; break; default: return -EINVAL; } ret = lp872x_read_byte(lp, addr, &val); if (ret) return ret; val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S; return (val < ARRAY_SIZE(lp8725_buck_uA)) ? lp8725_buck_uA[val] : -EINVAL; } static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); u8 addr, mask, shift, val; switch (buck) { case LP8720_ID_BUCK: addr = LP8720_BUCK_VOUT2; mask = LP8720_BUCK_FPWM_M; shift = LP8720_BUCK_FPWM_S; break; case LP8725_ID_BUCK1: addr = LP8725_BUCK_CTRL; mask = LP8725_BUCK1_FPWM_M; shift = LP8725_BUCK1_FPWM_S; break; case LP8725_ID_BUCK2: addr = LP8725_BUCK_CTRL; mask = LP8725_BUCK2_FPWM_M; shift = LP8725_BUCK2_FPWM_S; break; default: return -EINVAL; } if (mode == REGULATOR_MODE_FAST) val = LP872X_FORCE_PWM << shift; else if (mode == REGULATOR_MODE_NORMAL) val = LP872X_AUTO_PWM << shift; else return -EINVAL; return lp872x_update_bits(lp, addr, mask, val); } static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev) { struct lp872x *lp = rdev_get_drvdata(rdev); enum lp872x_regulator_id buck = rdev_get_id(rdev); u8 addr, mask, val; int ret; switch (buck) { case LP8720_ID_BUCK: addr = LP8720_BUCK_VOUT2; mask = LP8720_BUCK_FPWM_M; break; case LP8725_ID_BUCK1: addr = LP8725_BUCK_CTRL; mask = LP8725_BUCK1_FPWM_M; break; case LP8725_ID_BUCK2: addr = LP8725_BUCK_CTRL; mask = LP8725_BUCK2_FPWM_M; break; default: return -EINVAL; } ret = lp872x_read_byte(lp, addr, &val); if (ret) return ret; return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL; } static struct regulator_ops lp872x_ldo_ops = { .list_voltage = regulator_list_voltage_table, |
a32f9e020 regulator: lp872x... |
479 |
.map_voltage = regulator_map_voltage_ascend, |
af8b5fc31 regulator: add ne... |
480 481 482 483 484 485 486 487 488 489 |
.set_voltage_sel = regulator_set_voltage_sel_regmap, .get_voltage_sel = regulator_get_voltage_sel_regmap, .enable = regulator_enable_regmap, .disable = regulator_disable_regmap, .is_enabled = regulator_is_enabled_regmap, .enable_time = lp872x_regulator_enable_time, }; static struct regulator_ops lp8720_buck_ops = { .list_voltage = regulator_list_voltage_table, |
a32f9e020 regulator: lp872x... |
490 |
.map_voltage = regulator_map_voltage_ascend, |
af8b5fc31 regulator: add ne... |
491 492 493 494 495 496 497 498 499 500 501 502 |
.set_voltage_sel = lp872x_buck_set_voltage_sel, .get_voltage_sel = lp872x_buck_get_voltage_sel, .enable = regulator_enable_regmap, .disable = regulator_disable_regmap, .is_enabled = regulator_is_enabled_regmap, .enable_time = lp872x_regulator_enable_time, .set_mode = lp872x_buck_set_mode, .get_mode = lp872x_buck_get_mode, }; static struct regulator_ops lp8725_buck_ops = { .list_voltage = regulator_list_voltage_table, |
a32f9e020 regulator: lp872x... |
503 |
.map_voltage = regulator_map_voltage_ascend, |
af8b5fc31 regulator: add ne... |
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
.set_voltage_sel = lp872x_buck_set_voltage_sel, .get_voltage_sel = lp872x_buck_get_voltage_sel, .enable = regulator_enable_regmap, .disable = regulator_disable_regmap, .is_enabled = regulator_is_enabled_regmap, .enable_time = lp872x_regulator_enable_time, .set_mode = lp872x_buck_set_mode, .get_mode = lp872x_buck_get_mode, .set_current_limit = lp8725_buck_set_current_limit, .get_current_limit = lp8725_buck_get_current_limit, }; static struct regulator_desc lp8720_regulator_desc[] = { { .name = "ldo1", |
1f97fe477 regulator: lp872x... |
519 |
.of_match = of_match_ptr("ldo1"), |
af8b5fc31 regulator: add ne... |
520 521 522 523 524 525 526 527 528 529 530 531 532 |
.id = LP8720_ID_LDO1, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO1_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8720_ENABLE, .enable_mask = LP872X_EN_LDO1_M, }, { .name = "ldo2", |
1f97fe477 regulator: lp872x... |
533 |
.of_match = of_match_ptr("ldo2"), |
af8b5fc31 regulator: add ne... |
534 535 536 537 538 539 540 541 542 543 544 545 546 |
.id = LP8720_ID_LDO2, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO2_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8720_ENABLE, .enable_mask = LP872X_EN_LDO2_M, }, { .name = "ldo3", |
1f97fe477 regulator: lp872x... |
547 |
.of_match = of_match_ptr("ldo3"), |
af8b5fc31 regulator: add ne... |
548 549 550 551 552 553 554 555 556 557 558 559 560 |
.id = LP8720_ID_LDO3, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO3_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8720_ENABLE, .enable_mask = LP872X_EN_LDO3_M, }, { .name = "ldo4", |
1f97fe477 regulator: lp872x... |
561 |
.of_match = of_match_ptr("ldo4"), |
af8b5fc31 regulator: add ne... |
562 563 564 565 566 567 568 569 570 571 572 573 574 |
.id = LP8720_ID_LDO4, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl), .volt_table = lp8720_ldo4_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO4_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8720_ENABLE, .enable_mask = LP872X_EN_LDO4_M, }, { .name = "ldo5", |
1f97fe477 regulator: lp872x... |
575 |
.of_match = of_match_ptr("ldo5"), |
af8b5fc31 regulator: add ne... |
576 577 578 579 580 581 582 583 584 585 586 587 588 |
.id = LP8720_ID_LDO5, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO5_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8720_ENABLE, .enable_mask = LP872X_EN_LDO5_M, }, { .name = "buck", |
1f97fe477 regulator: lp872x... |
589 |
.of_match = of_match_ptr("buck"), |
af8b5fc31 regulator: add ne... |
590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
.id = LP8720_ID_BUCK, .ops = &lp8720_buck_ops, .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl), .volt_table = lp8720_buck_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .enable_reg = LP8720_ENABLE, .enable_mask = LP8720_EN_BUCK_M, }, }; static struct regulator_desc lp8725_regulator_desc[] = { { .name = "ldo1", |
1f97fe477 regulator: lp872x... |
604 |
.of_match = of_match_ptr("ldo1"), |
af8b5fc31 regulator: add ne... |
605 606 607 608 609 610 611 612 613 614 615 616 617 |
.id = LP8725_ID_LDO1, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO1_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP872X_EN_LDO1_M, }, { .name = "ldo2", |
1f97fe477 regulator: lp872x... |
618 |
.of_match = of_match_ptr("ldo2"), |
af8b5fc31 regulator: add ne... |
619 620 621 622 623 624 625 626 627 628 629 630 631 |
.id = LP8725_ID_LDO2, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO2_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP872X_EN_LDO2_M, }, { .name = "ldo3", |
1f97fe477 regulator: lp872x... |
632 |
.of_match = of_match_ptr("ldo3"), |
af8b5fc31 regulator: add ne... |
633 634 635 636 637 638 639 640 641 642 643 644 645 |
.id = LP8725_ID_LDO3, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO3_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP872X_EN_LDO3_M, }, { .name = "ldo4", |
1f97fe477 regulator: lp872x... |
646 |
.of_match = of_match_ptr("ldo4"), |
af8b5fc31 regulator: add ne... |
647 648 649 650 651 652 653 654 655 656 657 658 659 |
.id = LP8725_ID_LDO4, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO4_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP872X_EN_LDO4_M, }, { .name = "ldo5", |
1f97fe477 regulator: lp872x... |
660 |
.of_match = of_match_ptr("ldo5"), |
af8b5fc31 regulator: add ne... |
661 662 663 664 665 666 667 668 669 670 671 672 673 |
.id = LP8725_ID_LDO5, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl), .volt_table = lp872x_ldo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP872X_LDO5_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP872X_EN_LDO5_M, }, { .name = "lilo1", |
1f97fe477 regulator: lp872x... |
674 |
.of_match = of_match_ptr("lilo1"), |
af8b5fc31 regulator: add ne... |
675 676 677 678 679 680 681 682 683 684 685 686 687 |
.id = LP8725_ID_LILO1, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl), .volt_table = lp8725_lilo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP8725_LILO1_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP8725_EN_LILO1_M, }, { .name = "lilo2", |
1f97fe477 regulator: lp872x... |
688 |
.of_match = of_match_ptr("lilo2"), |
af8b5fc31 regulator: add ne... |
689 690 691 692 693 694 695 696 697 698 699 700 701 |
.id = LP8725_ID_LILO2, .ops = &lp872x_ldo_ops, .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl), .volt_table = lp8725_lilo_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .vsel_reg = LP8725_LILO2_VOUT, .vsel_mask = LP872X_VOUT_M, .enable_reg = LP8725_LDO_CTRL, .enable_mask = LP8725_EN_LILO2_M, }, { .name = "buck1", |
1f97fe477 regulator: lp872x... |
702 |
.of_match = of_match_ptr("buck1"), |
af8b5fc31 regulator: add ne... |
703 704 705 706 707 708 709 710 711 712 713 |
.id = LP8725_ID_BUCK1, .ops = &lp8725_buck_ops, .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl), .volt_table = lp8725_buck_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .enable_reg = LP872X_GENERAL_CFG, .enable_mask = LP8725_BUCK1_EN_M, }, { .name = "buck2", |
1f97fe477 regulator: lp872x... |
714 |
.of_match = of_match_ptr("buck2"), |
af8b5fc31 regulator: add ne... |
715 716 717 718 719 720 721 722 723 724 |
.id = LP8725_ID_BUCK2, .ops = &lp8725_buck_ops, .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl), .volt_table = lp8725_buck_vtbl, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .enable_reg = LP872X_GENERAL_CFG, .enable_mask = LP8725_BUCK2_EN_M, }, }; |
af8b5fc31 regulator: add ne... |
725 726 727 |
static int lp872x_init_dvs(struct lp872x *lp) { int ret, gpio; |
b158fba60 regulator: lp872x... |
728 |
struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL; |
af8b5fc31 regulator: add ne... |
729 |
enum lp872x_dvs_state pinstate; |
b158fba60 regulator: lp872x... |
730 731 732 733 734 |
u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M }; u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS }; if (!dvs) goto set_default_dvs_mode; |
af8b5fc31 regulator: add ne... |
735 |
|
af8b5fc31 regulator: add ne... |
736 |
gpio = dvs->gpio; |
690f44bad regulator: lp872x... |
737 |
if (!gpio_is_valid(gpio)) |
00fd6e615 regulator: lp872x... |
738 |
goto set_default_dvs_mode; |
af8b5fc31 regulator: add ne... |
739 740 741 742 743 744 745 746 747 748 |
pinstate = dvs->init_state; ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS"); if (ret) { dev_err(lp->dev, "gpio request err: %d ", ret); return ret; } lp->dvs_pin = pinstate; |
af8b5fc31 regulator: add ne... |
749 750 |
return 0; |
b158fba60 regulator: lp872x... |
751 752 753 754 |
set_default_dvs_mode: return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid], default_dvs_mode[lp->chipid]); |
af8b5fc31 regulator: add ne... |
755 |
} |
7e6213f43 regulator: lp872x... |
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
static int lp872x_hw_enable(struct lp872x *lp) { int ret, gpio; if (!lp->pdata) return -EINVAL; gpio = lp->pdata->enable_gpio; if (!gpio_is_valid(gpio)) return 0; /* Always set enable GPIO high. */ ret = devm_gpio_request_one(lp->dev, gpio, GPIOF_OUT_INIT_HIGH, "LP872X EN"); if (ret) { dev_err(lp->dev, "gpio request err: %d ", ret); return ret; } /* Each chip has a different enable delay. */ if (lp->chipid == LP8720) usleep_range(LP8720_ENABLE_DELAY, 1.5 * LP8720_ENABLE_DELAY); else usleep_range(LP8725_ENABLE_DELAY, 1.5 * LP8725_ENABLE_DELAY); return 0; } |
af8b5fc31 regulator: add ne... |
783 784 785 786 |
static int lp872x_config(struct lp872x *lp) { struct lp872x_platform_data *pdata = lp->pdata; int ret; |
86b3fef0c regulator: lp872x... |
787 788 |
if (!pdata || !pdata->update_config) goto init_dvs; |
af8b5fc31 regulator: add ne... |
789 790 791 792 |
ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config); if (ret) return ret; |
86b3fef0c regulator: lp872x... |
793 |
init_dvs: |
af8b5fc31 regulator: add ne... |
794 795 796 797 |
return lp872x_init_dvs(lp); } static struct regulator_init_data |
5bae06283 regulator: lp872x... |
798 |
*lp872x_find_regulator_init_data(int id, struct lp872x *lp) |
af8b5fc31 regulator: add ne... |
799 |
{ |
9ffaa8689 regulator: lp872x... |
800 |
struct lp872x_platform_data *pdata = lp->pdata; |
5bae06283 regulator: lp872x... |
801 |
int i; |
af8b5fc31 regulator: add ne... |
802 |
|
9ffaa8689 regulator: lp872x... |
803 804 |
if (!pdata) return NULL; |
5bae06283 regulator: lp872x... |
805 |
for (i = 0; i < lp->num_regulators; i++) { |
9ffaa8689 regulator: lp872x... |
806 807 |
if (pdata->regulator_data[i].id == id) return pdata->regulator_data[i].init_data; |
5bae06283 regulator: lp872x... |
808 |
} |
af8b5fc31 regulator: add ne... |
809 |
|
5bae06283 regulator: lp872x... |
810 |
return NULL; |
af8b5fc31 regulator: add ne... |
811 812 813 814 815 816 817 |
} static int lp872x_regulator_register(struct lp872x *lp) { struct regulator_desc *desc; struct regulator_config cfg = { }; struct regulator_dev *rdev; |
ed602534d regulator: lp872x... |
818 |
int i; |
af8b5fc31 regulator: add ne... |
819 |
|
37a6f43dd regulator: lp872x... |
820 |
for (i = 0; i < lp->num_regulators; i++) { |
af8b5fc31 regulator: add ne... |
821 822 823 824 |
desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] : &lp8725_regulator_desc[i]; cfg.dev = lp->dev; |
5bae06283 regulator: lp872x... |
825 |
cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp); |
af8b5fc31 regulator: add ne... |
826 827 |
cfg.driver_data = lp; cfg.regmap = lp->regmap; |
ed602534d regulator: lp872x... |
828 |
rdev = devm_regulator_register(lp->dev, desc, &cfg); |
af8b5fc31 regulator: add ne... |
829 830 |
if (IS_ERR(rdev)) { dev_err(lp->dev, "regulator register err"); |
ed602534d regulator: lp872x... |
831 |
return PTR_ERR(rdev); |
af8b5fc31 regulator: add ne... |
832 |
} |
af8b5fc31 regulator: add ne... |
833 834 835 |
} return 0; |
af8b5fc31 regulator: add ne... |
836 837 838 839 840 841 842 |
} static const struct regmap_config lp872x_regmap_config = { .reg_bits = 8, .val_bits = 8, .max_register = MAX_REGISTERS, }; |
00fd6e615 regulator: lp872x... |
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 |
#ifdef CONFIG_OF #define LP872X_VALID_OPMODE (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL) static struct of_regulator_match lp8720_matches[] = { { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, }, { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, }, { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, }, { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, }, { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, }, { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, }, }; static struct of_regulator_match lp8725_matches[] = { { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, }, { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, }, { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, }, { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, }, { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, }, { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, }, { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, }, { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, }, { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, }, }; static struct lp872x_platform_data *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which) { struct device_node *np = dev->of_node; struct lp872x_platform_data *pdata; struct of_regulator_match *match; |
00fd6e615 regulator: lp872x... |
874 875 876 877 878 879 880 |
int num_matches; int count; int i; u8 dvs_state; pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); if (!pdata) |
804486b19 regulator: lp872x... |
881 |
return ERR_PTR(-ENOMEM); |
00fd6e615 regulator: lp872x... |
882 883 884 885 886 887 888 |
of_property_read_u8(np, "ti,general-config", &pdata->general_config); if (of_find_property(np, "ti,update-config", NULL)) pdata->update_config = true; pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL); if (!pdata->dvs) |
804486b19 regulator: lp872x... |
889 |
return ERR_PTR(-ENOMEM); |
00fd6e615 regulator: lp872x... |
890 891 892 893 894 |
pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0); of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel); of_property_read_u8(np, "ti,dvs-state", &dvs_state); pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW; |
7e6213f43 regulator: lp872x... |
895 |
pdata->enable_gpio = of_get_named_gpio(np, "enable-gpios", 0); |
00fd6e615 regulator: lp872x... |
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 |
if (of_get_child_count(np) == 0) goto out; switch (which) { case LP8720: match = lp8720_matches; num_matches = ARRAY_SIZE(lp8720_matches); break; case LP8725: match = lp8725_matches; num_matches = ARRAY_SIZE(lp8725_matches); break; default: goto out; } count = of_regulator_match(dev, np, match, num_matches); if (count <= 0) goto out; for (i = 0; i < num_matches; i++) { |
37a6f43dd regulator: lp872x... |
917 918 |
pdata->regulator_data[i].id = (enum lp872x_regulator_id)match[i].driver_data; |
00fd6e615 regulator: lp872x... |
919 |
pdata->regulator_data[i].init_data = match[i].init_data; |
00fd6e615 regulator: lp872x... |
920 921 922 923 924 925 926 927 928 929 930 |
} out: return pdata; } #else static struct lp872x_platform_data *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which) { return NULL; } #endif |
af8b5fc31 regulator: add ne... |
931 932 933 |
static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id) { struct lp872x *lp; |
d9ffae15f regulator: lp872x... |
934 |
struct lp872x_platform_data *pdata; |
8538c4075 regulator: lp872x... |
935 |
int ret; |
af8b5fc31 regulator: add ne... |
936 937 938 939 |
const int lp872x_num_regulators[] = { [LP8720] = LP8720_NUM_REGULATORS, [LP8725] = LP8725_NUM_REGULATORS, }; |
804486b19 regulator: lp872x... |
940 |
if (cl->dev.of_node) { |
d9ffae15f regulator: lp872x... |
941 |
pdata = lp872x_populate_pdata_from_dt(&cl->dev, |
00fd6e615 regulator: lp872x... |
942 |
(enum lp872x_id)id->driver_data); |
804486b19 regulator: lp872x... |
943 944 945 |
if (IS_ERR(pdata)) return PTR_ERR(pdata); } else { |
d9ffae15f regulator: lp872x... |
946 |
pdata = dev_get_platdata(&cl->dev); |
804486b19 regulator: lp872x... |
947 |
} |
00fd6e615 regulator: lp872x... |
948 |
|
af8b5fc31 regulator: add ne... |
949 950 |
lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL); if (!lp) |
8538c4075 regulator: lp872x... |
951 |
return -ENOMEM; |
af8b5fc31 regulator: add ne... |
952 |
|
8538c4075 regulator: lp872x... |
953 |
lp->num_regulators = lp872x_num_regulators[id->driver_data]; |
af8b5fc31 regulator: add ne... |
954 955 956 957 958 959 |
lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config); if (IS_ERR(lp->regmap)) { ret = PTR_ERR(lp->regmap); dev_err(&cl->dev, "regmap init i2c err: %d ", ret); |
8538c4075 regulator: lp872x... |
960 |
return ret; |
af8b5fc31 regulator: add ne... |
961 962 963 |
} lp->dev = &cl->dev; |
d9ffae15f regulator: lp872x... |
964 |
lp->pdata = pdata; |
af8b5fc31 regulator: add ne... |
965 |
lp->chipid = id->driver_data; |
af8b5fc31 regulator: add ne... |
966 |
i2c_set_clientdata(cl, lp); |
7e6213f43 regulator: lp872x... |
967 968 969 |
ret = lp872x_hw_enable(lp); if (ret) return ret; |
af8b5fc31 regulator: add ne... |
970 971 |
ret = lp872x_config(lp); if (ret) |
8538c4075 regulator: lp872x... |
972 |
return ret; |
af8b5fc31 regulator: add ne... |
973 974 |
return lp872x_regulator_register(lp); |
af8b5fc31 regulator: add ne... |
975 |
} |
00fd6e615 regulator: lp872x... |
976 977 978 979 980 981 |
static const struct of_device_id lp872x_dt_ids[] = { { .compatible = "ti,lp8720", }, { .compatible = "ti,lp8725", }, { } }; MODULE_DEVICE_TABLE(of, lp872x_dt_ids); |
af8b5fc31 regulator: add ne... |
982 983 984 985 986 987 988 989 990 991 |
static const struct i2c_device_id lp872x_ids[] = { {"lp8720", LP8720}, {"lp8725", LP8725}, { } }; MODULE_DEVICE_TABLE(i2c, lp872x_ids); static struct i2c_driver lp872x_driver = { .driver = { .name = "lp872x", |
00fd6e615 regulator: lp872x... |
992 |
.of_match_table = of_match_ptr(lp872x_dt_ids), |
af8b5fc31 regulator: add ne... |
993 994 |
}, .probe = lp872x_probe, |
af8b5fc31 regulator: add ne... |
995 996 997 998 999 1000 1001 1002 |
.id_table = lp872x_ids, }; module_i2c_driver(lp872x_driver); MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver"); MODULE_AUTHOR("Milo Kim"); MODULE_LICENSE("GPL"); |