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