1 /* 2 * Regulator driver for Rockchip RK808 3 * 4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 5 * 6 * Author: Chris Zhong <zyw@rock-chips.com> 7 * Author: Zhang Qing <zhangqing@rock-chips.com> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms and conditions of the GNU General Public License, 11 * version 2, as published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 * more details. 17 */ 18 19 #include <linux/delay.h> 20 #include <linux/gpio.h> 21 #include <linux/i2c.h> 22 #include <linux/module.h> 23 #include <linux/of_device.h> 24 #include <linux/of_gpio.h> 25 #include <linux/mfd/rk808.h> 26 #include <linux/regulator/driver.h> 27 #include <linux/regulator/of_regulator.h> 28 #include <linux/gpio/consumer.h> 29 30 /* Field Definitions */ 31 #define RK808_BUCK_VSEL_MASK 0x3f 32 #define RK808_BUCK4_VSEL_MASK 0xf 33 #define RK808_LDO_VSEL_MASK 0x1f 34 35 /* Ramp rate definitions for buck1 / buck2 only */ 36 #define RK808_RAMP_RATE_OFFSET 3 37 #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET) 38 #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET) 39 #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET) 40 #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET) 41 #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET) 42 43 #define RK808_DVS2_POL BIT(2) 44 #define RK808_DVS1_POL BIT(1) 45 46 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */ 47 #define RK808_SLP_REG_OFFSET 1 48 49 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */ 50 #define RK808_DVS_REG_OFFSET 2 51 52 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */ 53 #define RK808_SLP_SET_OFF_REG_OFFSET 2 54 55 /* max steps for increase voltage of Buck1/2, equal 100mv*/ 56 #define MAX_STEPS_ONE_TIME 8 57 58 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \ 59 _vmask, _ereg, _emask, _etime) \ 60 [_id] = { \ 61 .name = (_match), \ 62 .supply_name = (_supply), \ 63 .of_match = of_match_ptr(_match), \ 64 .regulators_node = of_match_ptr("regulators"), \ 65 .type = REGULATOR_VOLTAGE, \ 66 .id = (_id), \ 67 .n_voltages = (((_max) - (_min)) / (_step) + 1), \ 68 .owner = THIS_MODULE, \ 69 .min_uV = (_min) * 1000, \ 70 .uV_step = (_step) * 1000, \ 71 .vsel_reg = (_vreg), \ 72 .vsel_mask = (_vmask), \ 73 .enable_reg = (_ereg), \ 74 .enable_mask = (_emask), \ 75 .enable_time = (_etime), \ 76 .ops = &rk808_reg_ops, \ 77 } 78 79 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \ 80 [_id] = { \ 81 .name = (_match), \ 82 .supply_name = (_supply), \ 83 .of_match = of_match_ptr(_match), \ 84 .regulators_node = of_match_ptr("regulators"), \ 85 .type = REGULATOR_VOLTAGE, \ 86 .id = (_id), \ 87 .enable_reg = (_ereg), \ 88 .enable_mask = (_emask), \ 89 .owner = THIS_MODULE, \ 90 .ops = &rk808_switch_ops \ 91 } 92 93 94 struct rk808_regulator_data { 95 struct gpio_desc *dvs_gpio[2]; 96 }; 97 98 static const int rk808_buck_config_regs[] = { 99 RK808_BUCK1_CONFIG_REG, 100 RK808_BUCK2_CONFIG_REG, 101 RK808_BUCK3_CONFIG_REG, 102 RK808_BUCK4_CONFIG_REG, 103 }; 104 105 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = { 106 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000), 107 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0), 108 }; 109 110 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev) 111 { 112 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev); 113 int id = rdev->desc->id - RK808_ID_DCDC1; 114 struct gpio_desc *gpio = pdata->dvs_gpio[id]; 115 unsigned int val; 116 int ret; 117 118 if (!gpio || gpiod_get_value(gpio) == 0) 119 return regulator_get_voltage_sel_regmap(rdev); 120 121 ret = regmap_read(rdev->regmap, 122 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET, 123 &val); 124 if (ret != 0) 125 return ret; 126 127 val &= rdev->desc->vsel_mask; 128 val >>= ffs(rdev->desc->vsel_mask) - 1; 129 130 return val; 131 } 132 133 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev, 134 unsigned sel) 135 { 136 int ret, delta_sel; 137 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask; 138 139 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); 140 if (ret != 0) 141 return ret; 142 143 tmp = val & ~mask; 144 old_sel = val & mask; 145 old_sel >>= ffs(mask) - 1; 146 delta_sel = sel - old_sel; 147 148 /* 149 * If directly modify the register to change the voltage, we will face 150 * the risk of overshoot. Put it into a multi-step, can effectively 151 * avoid this problem, a step is 100mv here. 152 */ 153 while (delta_sel > MAX_STEPS_ONE_TIME) { 154 old_sel += MAX_STEPS_ONE_TIME; 155 val = old_sel << (ffs(mask) - 1); 156 val |= tmp; 157 158 /* 159 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_ 160 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've 161 * got more than 65 us between each voltage change and thus 162 * won't ramp faster than ~1500 uV / us. 163 */ 164 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val); 165 delta_sel = sel - old_sel; 166 } 167 168 sel <<= ffs(mask) - 1; 169 val = tmp | sel; 170 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val); 171 172 /* 173 * When we change the voltage register directly, the ramp rate is about 174 * 100000uv/us, wait 1us to make sure the target voltage to be stable, 175 * so we needn't wait extra time after that. 176 */ 177 udelay(1); 178 179 return ret; 180 } 181 182 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev, 183 unsigned sel) 184 { 185 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev); 186 int id = rdev->desc->id - RK808_ID_DCDC1; 187 struct gpio_desc *gpio = pdata->dvs_gpio[id]; 188 unsigned int reg = rdev->desc->vsel_reg; 189 unsigned old_sel; 190 int ret, gpio_level; 191 192 if (!gpio) 193 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel); 194 195 gpio_level = gpiod_get_value(gpio); 196 if (gpio_level == 0) { 197 reg += RK808_DVS_REG_OFFSET; 198 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel); 199 } else { 200 ret = regmap_read(rdev->regmap, 201 reg + RK808_DVS_REG_OFFSET, 202 &old_sel); 203 } 204 205 if (ret != 0) 206 return ret; 207 208 sel <<= ffs(rdev->desc->vsel_mask) - 1; 209 sel |= old_sel & ~rdev->desc->vsel_mask; 210 211 ret = regmap_write(rdev->regmap, reg, sel); 212 if (ret) 213 return ret; 214 215 gpiod_set_value(gpio, !gpio_level); 216 217 return ret; 218 } 219 220 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev, 221 unsigned int old_selector, 222 unsigned int new_selector) 223 { 224 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev); 225 int id = rdev->desc->id - RK808_ID_DCDC1; 226 struct gpio_desc *gpio = pdata->dvs_gpio[id]; 227 228 /* if there is no dvs1/2 pin, we don't need wait extra time here. */ 229 if (!gpio) 230 return 0; 231 232 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector); 233 } 234 235 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 236 { 237 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US; 238 unsigned int reg = rk808_buck_config_regs[rdev->desc->id - 239 RK808_ID_DCDC1]; 240 241 switch (ramp_delay) { 242 case 1 ... 2000: 243 ramp_value = RK808_RAMP_RATE_2MV_PER_US; 244 break; 245 case 2001 ... 4000: 246 ramp_value = RK808_RAMP_RATE_4MV_PER_US; 247 break; 248 case 4001 ... 6000: 249 ramp_value = RK808_RAMP_RATE_6MV_PER_US; 250 break; 251 case 6001 ... 10000: 252 break; 253 default: 254 pr_warn("%s ramp_delay: %d not supported, setting 10000\n", 255 rdev->desc->name, ramp_delay); 256 } 257 258 return regmap_update_bits(rdev->regmap, reg, 259 RK808_RAMP_RATE_MASK, ramp_value); 260 } 261 262 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv) 263 { 264 unsigned int reg; 265 int sel = regulator_map_voltage_linear(rdev, uv, uv); 266 267 if (sel < 0) 268 return -EINVAL; 269 270 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET; 271 272 return regmap_update_bits(rdev->regmap, reg, 273 rdev->desc->vsel_mask, 274 sel); 275 } 276 277 static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv) 278 { 279 unsigned int reg; 280 int sel = regulator_map_voltage_linear_range(rdev, uv, uv); 281 282 if (sel < 0) 283 return -EINVAL; 284 285 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET; 286 287 return regmap_update_bits(rdev->regmap, reg, 288 rdev->desc->vsel_mask, 289 sel); 290 } 291 292 static int rk808_set_suspend_enable(struct regulator_dev *rdev) 293 { 294 unsigned int reg; 295 296 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET; 297 298 return regmap_update_bits(rdev->regmap, reg, 299 rdev->desc->enable_mask, 300 0); 301 } 302 303 static int rk808_set_suspend_disable(struct regulator_dev *rdev) 304 { 305 unsigned int reg; 306 307 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET; 308 309 return regmap_update_bits(rdev->regmap, reg, 310 rdev->desc->enable_mask, 311 rdev->desc->enable_mask); 312 } 313 314 static struct regulator_ops rk808_buck1_2_ops = { 315 .list_voltage = regulator_list_voltage_linear, 316 .map_voltage = regulator_map_voltage_linear, 317 .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap, 318 .set_voltage_sel = rk808_buck1_2_set_voltage_sel, 319 .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel, 320 .enable = regulator_enable_regmap, 321 .disable = regulator_disable_regmap, 322 .is_enabled = regulator_is_enabled_regmap, 323 .set_ramp_delay = rk808_set_ramp_delay, 324 .set_suspend_voltage = rk808_set_suspend_voltage, 325 .set_suspend_enable = rk808_set_suspend_enable, 326 .set_suspend_disable = rk808_set_suspend_disable, 327 }; 328 329 static struct regulator_ops rk808_reg_ops = { 330 .list_voltage = regulator_list_voltage_linear, 331 .map_voltage = regulator_map_voltage_linear, 332 .get_voltage_sel = regulator_get_voltage_sel_regmap, 333 .set_voltage_sel = regulator_set_voltage_sel_regmap, 334 .enable = regulator_enable_regmap, 335 .disable = regulator_disable_regmap, 336 .is_enabled = regulator_is_enabled_regmap, 337 .set_suspend_voltage = rk808_set_suspend_voltage, 338 .set_suspend_enable = rk808_set_suspend_enable, 339 .set_suspend_disable = rk808_set_suspend_disable, 340 }; 341 342 static struct regulator_ops rk808_reg_ops_ranges = { 343 .list_voltage = regulator_list_voltage_linear_range, 344 .map_voltage = regulator_map_voltage_linear_range, 345 .get_voltage_sel = regulator_get_voltage_sel_regmap, 346 .set_voltage_sel = regulator_set_voltage_sel_regmap, 347 .enable = regulator_enable_regmap, 348 .disable = regulator_disable_regmap, 349 .is_enabled = regulator_is_enabled_regmap, 350 .set_suspend_voltage = rk808_set_suspend_voltage_range, 351 .set_suspend_enable = rk808_set_suspend_enable, 352 .set_suspend_disable = rk808_set_suspend_disable, 353 }; 354 355 static struct regulator_ops rk808_switch_ops = { 356 .enable = regulator_enable_regmap, 357 .disable = regulator_disable_regmap, 358 .is_enabled = regulator_is_enabled_regmap, 359 .set_suspend_enable = rk808_set_suspend_enable, 360 .set_suspend_disable = rk808_set_suspend_disable, 361 }; 362 363 static const struct regulator_desc rk808_reg[] = { 364 { 365 .name = "DCDC_REG1", 366 .supply_name = "vcc1", 367 .of_match = of_match_ptr("DCDC_REG1"), 368 .regulators_node = of_match_ptr("regulators"), 369 .id = RK808_ID_DCDC1, 370 .ops = &rk808_buck1_2_ops, 371 .type = REGULATOR_VOLTAGE, 372 .min_uV = 712500, 373 .uV_step = 12500, 374 .n_voltages = 64, 375 .vsel_reg = RK808_BUCK1_ON_VSEL_REG, 376 .vsel_mask = RK808_BUCK_VSEL_MASK, 377 .enable_reg = RK808_DCDC_EN_REG, 378 .enable_mask = BIT(0), 379 .owner = THIS_MODULE, 380 }, { 381 .name = "DCDC_REG2", 382 .supply_name = "vcc2", 383 .of_match = of_match_ptr("DCDC_REG2"), 384 .regulators_node = of_match_ptr("regulators"), 385 .id = RK808_ID_DCDC2, 386 .ops = &rk808_buck1_2_ops, 387 .type = REGULATOR_VOLTAGE, 388 .min_uV = 712500, 389 .uV_step = 12500, 390 .n_voltages = 64, 391 .vsel_reg = RK808_BUCK2_ON_VSEL_REG, 392 .vsel_mask = RK808_BUCK_VSEL_MASK, 393 .enable_reg = RK808_DCDC_EN_REG, 394 .enable_mask = BIT(1), 395 .owner = THIS_MODULE, 396 }, { 397 .name = "DCDC_REG3", 398 .supply_name = "vcc3", 399 .of_match = of_match_ptr("DCDC_REG3"), 400 .regulators_node = of_match_ptr("regulators"), 401 .id = RK808_ID_DCDC3, 402 .ops = &rk808_switch_ops, 403 .type = REGULATOR_VOLTAGE, 404 .n_voltages = 1, 405 .enable_reg = RK808_DCDC_EN_REG, 406 .enable_mask = BIT(2), 407 .owner = THIS_MODULE, 408 }, 409 RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100, 410 RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK, 411 RK808_DCDC_EN_REG, BIT(3), 0), 412 RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100, 413 RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 414 BIT(0), 400), 415 RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100, 416 RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 417 BIT(1), 400), 418 { 419 .name = "LDO_REG3", 420 .supply_name = "vcc7", 421 .of_match = of_match_ptr("LDO_REG3"), 422 .regulators_node = of_match_ptr("regulators"), 423 .id = RK808_ID_LDO3, 424 .ops = &rk808_reg_ops_ranges, 425 .type = REGULATOR_VOLTAGE, 426 .n_voltages = 16, 427 .linear_ranges = rk808_ldo3_voltage_ranges, 428 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges), 429 .vsel_reg = RK808_LDO3_ON_VSEL_REG, 430 .vsel_mask = RK808_BUCK4_VSEL_MASK, 431 .enable_reg = RK808_LDO_EN_REG, 432 .enable_mask = BIT(2), 433 .enable_time = 400, 434 .owner = THIS_MODULE, 435 }, 436 RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100, 437 RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 438 BIT(3), 400), 439 RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100, 440 RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 441 BIT(4), 400), 442 RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100, 443 RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 444 BIT(5), 400), 445 RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100, 446 RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 447 BIT(6), 400), 448 RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100, 449 RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG, 450 BIT(7), 400), 451 RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8", 452 RK808_DCDC_EN_REG, BIT(5)), 453 RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12", 454 RK808_DCDC_EN_REG, BIT(6)), 455 }; 456 457 static int rk808_regulator_dt_parse_pdata(struct device *dev, 458 struct device *client_dev, 459 struct regmap *map, 460 struct rk808_regulator_data *pdata) 461 { 462 struct device_node *np; 463 int tmp, ret = 0, i; 464 465 np = of_get_child_by_name(client_dev->of_node, "regulators"); 466 if (!np) 467 return -ENXIO; 468 469 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) { 470 pdata->dvs_gpio[i] = 471 devm_gpiod_get_index_optional(client_dev, "dvs", i, 472 GPIOD_OUT_LOW); 473 if (IS_ERR(pdata->dvs_gpio[i])) { 474 ret = PTR_ERR(pdata->dvs_gpio[i]); 475 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret); 476 goto dt_parse_end; 477 } 478 479 if (!pdata->dvs_gpio[i]) { 480 dev_warn(dev, "there is no dvs%d gpio\n", i); 481 continue; 482 } 483 484 tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL; 485 ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp, 486 gpiod_is_active_low(pdata->dvs_gpio[i]) ? 487 0 : tmp); 488 } 489 490 dt_parse_end: 491 of_node_put(np); 492 return ret; 493 } 494 495 static int rk808_regulator_probe(struct platform_device *pdev) 496 { 497 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 498 struct i2c_client *client = rk808->i2c; 499 struct regulator_config config = {}; 500 struct regulator_dev *rk808_rdev; 501 struct rk808_regulator_data *pdata; 502 int ret, i; 503 504 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 505 if (!pdata) 506 return -ENOMEM; 507 508 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev, 509 rk808->regmap, pdata); 510 if (ret < 0) 511 return ret; 512 513 platform_set_drvdata(pdev, pdata); 514 515 config.dev = &client->dev; 516 config.driver_data = pdata; 517 config.regmap = rk808->regmap; 518 519 /* Instantiate the regulators */ 520 for (i = 0; i < RK808_NUM_REGULATORS; i++) { 521 rk808_rdev = devm_regulator_register(&pdev->dev, 522 &rk808_reg[i], &config); 523 if (IS_ERR(rk808_rdev)) { 524 dev_err(&client->dev, 525 "failed to register %d regulator\n", i); 526 return PTR_ERR(rk808_rdev); 527 } 528 } 529 530 return 0; 531 } 532 533 static struct platform_driver rk808_regulator_driver = { 534 .probe = rk808_regulator_probe, 535 .driver = { 536 .name = "rk808-regulator", 537 .owner = THIS_MODULE, 538 }, 539 }; 540 541 module_platform_driver(rk808_regulator_driver); 542 543 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs"); 544 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>"); 545 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>"); 546 MODULE_LICENSE("GPL"); 547 MODULE_ALIAS("platform:rk808-regulator"); 548