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/module.h> 20 #include <linux/i2c.h> 21 #include <linux/mfd/rk808.h> 22 #include <linux/of_device.h> 23 #include <linux/regulator/driver.h> 24 #include <linux/regulator/of_regulator.h> 25 26 /* Field Definitions */ 27 #define RK808_BUCK_VSEL_MASK 0x3f 28 #define RK808_BUCK4_VSEL_MASK 0xf 29 #define RK808_LDO_VSEL_MASK 0x1f 30 31 /* Ramp rate definitions for buck1 / buck2 only */ 32 #define RK808_RAMP_RATE_OFFSET 3 33 #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET) 34 #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET) 35 #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET) 36 #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET) 37 #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET) 38 39 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */ 40 #define RK808_SLP_REG_OFFSET 1 41 42 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */ 43 #define RK808_SLP_SET_OFF_REG_OFFSET 2 44 45 static const int rk808_buck_config_regs[] = { 46 RK808_BUCK1_CONFIG_REG, 47 RK808_BUCK2_CONFIG_REG, 48 RK808_BUCK3_CONFIG_REG, 49 RK808_BUCK4_CONFIG_REG, 50 }; 51 52 static const struct regulator_linear_range rk808_buck_voltage_ranges[] = { 53 REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500), 54 }; 55 56 static const struct regulator_linear_range rk808_buck4_voltage_ranges[] = { 57 REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000), 58 }; 59 60 static const struct regulator_linear_range rk808_ldo_voltage_ranges[] = { 61 REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000), 62 }; 63 64 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = { 65 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000), 66 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0), 67 }; 68 69 static const struct regulator_linear_range rk808_ldo6_voltage_ranges[] = { 70 REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000), 71 }; 72 73 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 74 { 75 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US; 76 unsigned int reg = rk808_buck_config_regs[rdev->desc->id - 77 RK808_ID_DCDC1]; 78 79 switch (ramp_delay) { 80 case 1 ... 2000: 81 ramp_value = RK808_RAMP_RATE_2MV_PER_US; 82 break; 83 case 2001 ... 4000: 84 ramp_value = RK808_RAMP_RATE_4MV_PER_US; 85 break; 86 case 4001 ... 6000: 87 ramp_value = RK808_RAMP_RATE_6MV_PER_US; 88 break; 89 case 6001 ... 10000: 90 break; 91 default: 92 pr_warn("%s ramp_delay: %d not supported, setting 10000\n", 93 rdev->desc->name, ramp_delay); 94 } 95 96 return regmap_update_bits(rdev->regmap, reg, 97 RK808_RAMP_RATE_MASK, ramp_value); 98 } 99 100 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv) 101 { 102 unsigned int reg; 103 int sel = regulator_map_voltage_linear_range(rdev, uv, uv); 104 105 if (sel < 0) 106 return -EINVAL; 107 108 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET; 109 110 return regmap_update_bits(rdev->regmap, reg, 111 rdev->desc->vsel_mask, 112 sel); 113 } 114 115 static int rk808_set_suspend_enable(struct regulator_dev *rdev) 116 { 117 unsigned int reg; 118 119 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET; 120 121 return regmap_update_bits(rdev->regmap, reg, 122 rdev->desc->enable_mask, 123 0); 124 } 125 126 static int rk808_set_suspend_disable(struct regulator_dev *rdev) 127 { 128 unsigned int reg; 129 130 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET; 131 132 return regmap_update_bits(rdev->regmap, reg, 133 rdev->desc->enable_mask, 134 rdev->desc->enable_mask); 135 } 136 137 static struct regulator_ops rk808_buck1_2_ops = { 138 .list_voltage = regulator_list_voltage_linear_range, 139 .map_voltage = regulator_map_voltage_linear_range, 140 .get_voltage_sel = regulator_get_voltage_sel_regmap, 141 .set_voltage_sel = regulator_set_voltage_sel_regmap, 142 .enable = regulator_enable_regmap, 143 .disable = regulator_disable_regmap, 144 .is_enabled = regulator_is_enabled_regmap, 145 .set_ramp_delay = rk808_set_ramp_delay, 146 .set_suspend_voltage = rk808_set_suspend_voltage, 147 .set_suspend_enable = rk808_set_suspend_enable, 148 .set_suspend_disable = rk808_set_suspend_disable, 149 }; 150 151 static struct regulator_ops rk808_reg_ops = { 152 .list_voltage = regulator_list_voltage_linear_range, 153 .map_voltage = regulator_map_voltage_linear_range, 154 .get_voltage_sel = regulator_get_voltage_sel_regmap, 155 .set_voltage_sel = regulator_set_voltage_sel_regmap, 156 .enable = regulator_enable_regmap, 157 .disable = regulator_disable_regmap, 158 .is_enabled = regulator_is_enabled_regmap, 159 .set_suspend_voltage = rk808_set_suspend_voltage, 160 .set_suspend_enable = rk808_set_suspend_enable, 161 .set_suspend_disable = rk808_set_suspend_disable, 162 }; 163 164 static struct regulator_ops rk808_switch_ops = { 165 .enable = regulator_enable_regmap, 166 .disable = regulator_disable_regmap, 167 .is_enabled = regulator_is_enabled_regmap, 168 .set_suspend_enable = rk808_set_suspend_enable, 169 .set_suspend_disable = rk808_set_suspend_disable, 170 }; 171 172 static const struct regulator_desc rk808_reg[] = { 173 { 174 .name = "DCDC_REG1", 175 .supply_name = "vcc1", 176 .id = RK808_ID_DCDC1, 177 .ops = &rk808_buck1_2_ops, 178 .type = REGULATOR_VOLTAGE, 179 .n_voltages = 64, 180 .linear_ranges = rk808_buck_voltage_ranges, 181 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges), 182 .vsel_reg = RK808_BUCK1_ON_VSEL_REG, 183 .vsel_mask = RK808_BUCK_VSEL_MASK, 184 .enable_reg = RK808_DCDC_EN_REG, 185 .enable_mask = BIT(0), 186 .owner = THIS_MODULE, 187 }, { 188 .name = "DCDC_REG2", 189 .supply_name = "vcc2", 190 .id = RK808_ID_DCDC2, 191 .ops = &rk808_buck1_2_ops, 192 .type = REGULATOR_VOLTAGE, 193 .n_voltages = 64, 194 .linear_ranges = rk808_buck_voltage_ranges, 195 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges), 196 .vsel_reg = RK808_BUCK2_ON_VSEL_REG, 197 .vsel_mask = RK808_BUCK_VSEL_MASK, 198 .enable_reg = RK808_DCDC_EN_REG, 199 .enable_mask = BIT(1), 200 .owner = THIS_MODULE, 201 }, { 202 .name = "DCDC_REG3", 203 .supply_name = "vcc3", 204 .id = RK808_ID_DCDC3, 205 .ops = &rk808_switch_ops, 206 .type = REGULATOR_VOLTAGE, 207 .n_voltages = 1, 208 .enable_reg = RK808_DCDC_EN_REG, 209 .enable_mask = BIT(2), 210 .owner = THIS_MODULE, 211 }, { 212 .name = "DCDC_REG4", 213 .supply_name = "vcc4", 214 .id = RK808_ID_DCDC4, 215 .ops = &rk808_reg_ops, 216 .type = REGULATOR_VOLTAGE, 217 .n_voltages = 16, 218 .linear_ranges = rk808_buck4_voltage_ranges, 219 .n_linear_ranges = ARRAY_SIZE(rk808_buck4_voltage_ranges), 220 .vsel_reg = RK808_BUCK4_ON_VSEL_REG, 221 .vsel_mask = RK808_BUCK4_VSEL_MASK, 222 .enable_reg = RK808_DCDC_EN_REG, 223 .enable_mask = BIT(3), 224 .owner = THIS_MODULE, 225 }, { 226 .name = "LDO_REG1", 227 .supply_name = "vcc6", 228 .id = RK808_ID_LDO1, 229 .ops = &rk808_reg_ops, 230 .type = REGULATOR_VOLTAGE, 231 .n_voltages = 17, 232 .linear_ranges = rk808_ldo_voltage_ranges, 233 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges), 234 .vsel_reg = RK808_LDO1_ON_VSEL_REG, 235 .vsel_mask = RK808_LDO_VSEL_MASK, 236 .enable_reg = RK808_LDO_EN_REG, 237 .enable_mask = BIT(0), 238 .owner = THIS_MODULE, 239 }, { 240 .name = "LDO_REG2", 241 .supply_name = "vcc6", 242 .id = RK808_ID_LDO2, 243 .ops = &rk808_reg_ops, 244 .type = REGULATOR_VOLTAGE, 245 .n_voltages = 17, 246 .linear_ranges = rk808_ldo_voltage_ranges, 247 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges), 248 .vsel_reg = RK808_LDO2_ON_VSEL_REG, 249 .vsel_mask = RK808_LDO_VSEL_MASK, 250 .enable_reg = RK808_LDO_EN_REG, 251 .enable_mask = BIT(1), 252 .owner = THIS_MODULE, 253 }, { 254 .name = "LDO_REG3", 255 .supply_name = "vcc7", 256 .id = RK808_ID_LDO3, 257 .ops = &rk808_reg_ops, 258 .type = REGULATOR_VOLTAGE, 259 .n_voltages = 16, 260 .linear_ranges = rk808_ldo3_voltage_ranges, 261 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges), 262 .vsel_reg = RK808_LDO3_ON_VSEL_REG, 263 .vsel_mask = RK808_BUCK4_VSEL_MASK, 264 .enable_reg = RK808_LDO_EN_REG, 265 .enable_mask = BIT(2), 266 .owner = THIS_MODULE, 267 }, { 268 .name = "LDO_REG4", 269 .supply_name = "vcc9", 270 .id = RK808_ID_LDO4, 271 .ops = &rk808_reg_ops, 272 .type = REGULATOR_VOLTAGE, 273 .n_voltages = 17, 274 .linear_ranges = rk808_ldo_voltage_ranges, 275 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges), 276 .vsel_reg = RK808_LDO4_ON_VSEL_REG, 277 .vsel_mask = RK808_LDO_VSEL_MASK, 278 .enable_reg = RK808_LDO_EN_REG, 279 .enable_mask = BIT(3), 280 .owner = THIS_MODULE, 281 }, { 282 .name = "LDO_REG5", 283 .supply_name = "vcc9", 284 .id = RK808_ID_LDO5, 285 .ops = &rk808_reg_ops, 286 .type = REGULATOR_VOLTAGE, 287 .n_voltages = 17, 288 .linear_ranges = rk808_ldo_voltage_ranges, 289 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges), 290 .vsel_reg = RK808_LDO5_ON_VSEL_REG, 291 .vsel_mask = RK808_LDO_VSEL_MASK, 292 .enable_reg = RK808_LDO_EN_REG, 293 .enable_mask = BIT(4), 294 .owner = THIS_MODULE, 295 }, { 296 .name = "LDO_REG6", 297 .supply_name = "vcc10", 298 .id = RK808_ID_LDO6, 299 .ops = &rk808_reg_ops, 300 .type = REGULATOR_VOLTAGE, 301 .n_voltages = 18, 302 .linear_ranges = rk808_ldo6_voltage_ranges, 303 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges), 304 .vsel_reg = RK808_LDO6_ON_VSEL_REG, 305 .vsel_mask = RK808_LDO_VSEL_MASK, 306 .enable_reg = RK808_LDO_EN_REG, 307 .enable_mask = BIT(5), 308 .owner = THIS_MODULE, 309 }, { 310 .name = "LDO_REG7", 311 .supply_name = "vcc7", 312 .id = RK808_ID_LDO7, 313 .ops = &rk808_reg_ops, 314 .type = REGULATOR_VOLTAGE, 315 .n_voltages = 18, 316 .linear_ranges = rk808_ldo6_voltage_ranges, 317 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges), 318 .vsel_reg = RK808_LDO7_ON_VSEL_REG, 319 .vsel_mask = RK808_LDO_VSEL_MASK, 320 .enable_reg = RK808_LDO_EN_REG, 321 .enable_mask = BIT(6), 322 .owner = THIS_MODULE, 323 }, { 324 .name = "LDO_REG8", 325 .supply_name = "vcc11", 326 .id = RK808_ID_LDO8, 327 .ops = &rk808_reg_ops, 328 .type = REGULATOR_VOLTAGE, 329 .n_voltages = 17, 330 .linear_ranges = rk808_ldo_voltage_ranges, 331 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges), 332 .vsel_reg = RK808_LDO8_ON_VSEL_REG, 333 .vsel_mask = RK808_LDO_VSEL_MASK, 334 .enable_reg = RK808_LDO_EN_REG, 335 .enable_mask = BIT(7), 336 .owner = THIS_MODULE, 337 }, { 338 .name = "SWITCH_REG1", 339 .supply_name = "vcc8", 340 .id = RK808_ID_SWITCH1, 341 .ops = &rk808_switch_ops, 342 .type = REGULATOR_VOLTAGE, 343 .enable_reg = RK808_DCDC_EN_REG, 344 .enable_mask = BIT(5), 345 .owner = THIS_MODULE, 346 }, { 347 .name = "SWITCH_REG2", 348 .supply_name = "vcc12", 349 .id = RK808_ID_SWITCH2, 350 .ops = &rk808_switch_ops, 351 .type = REGULATOR_VOLTAGE, 352 .enable_reg = RK808_DCDC_EN_REG, 353 .enable_mask = BIT(6), 354 .owner = THIS_MODULE, 355 }, 356 }; 357 358 static struct of_regulator_match rk808_reg_matches[] = { 359 [RK808_ID_DCDC1] = { .name = "DCDC_REG1" }, 360 [RK808_ID_DCDC2] = { .name = "DCDC_REG2" }, 361 [RK808_ID_DCDC3] = { .name = "DCDC_REG3" }, 362 [RK808_ID_DCDC4] = { .name = "DCDC_REG4" }, 363 [RK808_ID_LDO1] = { .name = "LDO_REG1" }, 364 [RK808_ID_LDO2] = { .name = "LDO_REG2" }, 365 [RK808_ID_LDO3] = { .name = "LDO_REG3" }, 366 [RK808_ID_LDO4] = { .name = "LDO_REG4" }, 367 [RK808_ID_LDO5] = { .name = "LDO_REG5" }, 368 [RK808_ID_LDO6] = { .name = "LDO_REG6" }, 369 [RK808_ID_LDO7] = { .name = "LDO_REG7" }, 370 [RK808_ID_LDO8] = { .name = "LDO_REG8" }, 371 [RK808_ID_SWITCH1] = { .name = "SWITCH_REG1" }, 372 [RK808_ID_SWITCH2] = { .name = "SWITCH_REG2" }, 373 }; 374 375 static int rk808_regulator_probe(struct platform_device *pdev) 376 { 377 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 378 struct i2c_client *client = rk808->i2c; 379 struct device_node *reg_np; 380 struct regulator_config config = {}; 381 struct regulator_dev *rk808_rdev; 382 int ret, i; 383 384 reg_np = of_get_child_by_name(client->dev.of_node, "regulators"); 385 if (!reg_np) 386 return -ENXIO; 387 388 ret = of_regulator_match(&pdev->dev, reg_np, rk808_reg_matches, 389 RK808_NUM_REGULATORS); 390 of_node_put(reg_np); 391 if (ret < 0) 392 return ret; 393 394 /* Instantiate the regulators */ 395 for (i = 0; i < RK808_NUM_REGULATORS; i++) { 396 if (!rk808_reg_matches[i].init_data || 397 !rk808_reg_matches[i].of_node) 398 continue; 399 400 config.dev = &client->dev; 401 config.driver_data = rk808; 402 config.regmap = rk808->regmap; 403 config.of_node = rk808_reg_matches[i].of_node; 404 config.init_data = rk808_reg_matches[i].init_data; 405 406 rk808_rdev = devm_regulator_register(&pdev->dev, 407 &rk808_reg[i], &config); 408 if (IS_ERR(rk808_rdev)) { 409 dev_err(&client->dev, 410 "failed to register %d regulator\n", i); 411 return PTR_ERR(rk808_rdev); 412 } 413 } 414 415 return 0; 416 } 417 418 static struct platform_driver rk808_regulator_driver = { 419 .probe = rk808_regulator_probe, 420 .driver = { 421 .name = "rk808-regulator", 422 }, 423 }; 424 425 module_platform_driver(rk808_regulator_driver); 426 427 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs"); 428 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>"); 429 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>"); 430 MODULE_LICENSE("GPL"); 431 MODULE_ALIAS("platform:rk808-regulator"); 432