linux/drivers/regulator/rk808-regulator.c
<<
>>
Prefs
   1/*
   2 * Regulator driver for Rockchip RK808/RK818
   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 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
  10 *
  11 * Author: Wadim Egorov <w.egorov@phytec.de>
  12 *
  13 * This program is free software; you can redistribute it and/or modify it
  14 * under the terms and conditions of the GNU General Public License,
  15 * version 2, as published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope it will be useful, but WITHOUT
  18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  20 * more details.
  21 */
  22
  23#include <linux/delay.h>
  24#include <linux/gpio.h>
  25#include <linux/i2c.h>
  26#include <linux/module.h>
  27#include <linux/of_device.h>
  28#include <linux/of_gpio.h>
  29#include <linux/mfd/rk808.h>
  30#include <linux/regulator/driver.h>
  31#include <linux/regulator/of_regulator.h>
  32#include <linux/gpio/consumer.h>
  33
  34/* Field Definitions */
  35#define RK808_BUCK_VSEL_MASK    0x3f
  36#define RK808_BUCK4_VSEL_MASK   0xf
  37#define RK808_LDO_VSEL_MASK     0x1f
  38
  39#define RK818_BUCK_VSEL_MASK            0x3f
  40#define RK818_BUCK4_VSEL_MASK           0x1f
  41#define RK818_LDO_VSEL_MASK             0x1f
  42#define RK818_LDO3_ON_VSEL_MASK         0xf
  43#define RK818_BOOST_ON_VSEL_MASK        0xe0
  44
  45/* Ramp rate definitions for buck1 / buck2 only */
  46#define RK808_RAMP_RATE_OFFSET          3
  47#define RK808_RAMP_RATE_MASK            (3 << RK808_RAMP_RATE_OFFSET)
  48#define RK808_RAMP_RATE_2MV_PER_US      (0 << RK808_RAMP_RATE_OFFSET)
  49#define RK808_RAMP_RATE_4MV_PER_US      (1 << RK808_RAMP_RATE_OFFSET)
  50#define RK808_RAMP_RATE_6MV_PER_US      (2 << RK808_RAMP_RATE_OFFSET)
  51#define RK808_RAMP_RATE_10MV_PER_US     (3 << RK808_RAMP_RATE_OFFSET)
  52
  53#define RK808_DVS2_POL          BIT(2)
  54#define RK808_DVS1_POL          BIT(1)
  55
  56/* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
  57#define RK808_SLP_REG_OFFSET 1
  58
  59/* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
  60#define RK808_DVS_REG_OFFSET 2
  61
  62/* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
  63#define RK808_SLP_SET_OFF_REG_OFFSET 2
  64
  65/* max steps for increase voltage of Buck1/2, equal 100mv*/
  66#define MAX_STEPS_ONE_TIME 8
  67
  68#define RK805_DESC(_id, _match, _supply, _min, _max, _step, _vreg,      \
  69        _vmask, _ereg, _emask, _etime)                                  \
  70        [_id] = {                                                       \
  71                .name           = (_match),                             \
  72                .supply_name    = (_supply),                            \
  73                .of_match       = of_match_ptr(_match),                 \
  74                .regulators_node = of_match_ptr("regulators"),          \
  75                .type           = REGULATOR_VOLTAGE,                    \
  76                .id             = (_id),                                \
  77                .n_voltages     = (((_max) - (_min)) / (_step) + 1),    \
  78                .owner          = THIS_MODULE,                          \
  79                .min_uV         = (_min) * 1000,                        \
  80                .uV_step        = (_step) * 1000,                       \
  81                .vsel_reg       = (_vreg),                              \
  82                .vsel_mask      = (_vmask),                             \
  83                .enable_reg     = (_ereg),                              \
  84                .enable_mask    = (_emask),                             \
  85                .enable_time    = (_etime),                             \
  86                .ops            = &rk805_reg_ops,                       \
  87        }
  88
  89#define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg,      \
  90        _vmask, _ereg, _emask, _etime)                                  \
  91        [_id] = {                                                       \
  92                .name           = (_match),                             \
  93                .supply_name    = (_supply),                            \
  94                .of_match       = of_match_ptr(_match),                 \
  95                .regulators_node = of_match_ptr("regulators"),          \
  96                .type           = REGULATOR_VOLTAGE,                    \
  97                .id             = (_id),                                \
  98                .n_voltages     = (((_max) - (_min)) / (_step) + 1),    \
  99                .owner          = THIS_MODULE,                          \
 100                .min_uV         = (_min) * 1000,                        \
 101                .uV_step        = (_step) * 1000,                       \
 102                .vsel_reg       = (_vreg),                              \
 103                .vsel_mask      = (_vmask),                             \
 104                .enable_reg     = (_ereg),                              \
 105                .enable_mask    = (_emask),                             \
 106                .enable_time    = (_etime),                             \
 107                .ops            = &rk808_reg_ops,                       \
 108        }
 109
 110#define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask)          \
 111        [_id] = {                                                       \
 112                .name           = (_match),                             \
 113                .supply_name    = (_supply),                            \
 114                .of_match       = of_match_ptr(_match),                 \
 115                .regulators_node = of_match_ptr("regulators"),          \
 116                .type           = REGULATOR_VOLTAGE,                    \
 117                .id             = (_id),                                \
 118                .enable_reg     = (_ereg),                              \
 119                .enable_mask    = (_emask),                             \
 120                .owner          = THIS_MODULE,                          \
 121                .ops            = &rk808_switch_ops                     \
 122        }
 123
 124
 125struct rk808_regulator_data {
 126        struct gpio_desc *dvs_gpio[2];
 127};
 128
 129static const int rk808_buck_config_regs[] = {
 130        RK808_BUCK1_CONFIG_REG,
 131        RK808_BUCK2_CONFIG_REG,
 132        RK808_BUCK3_CONFIG_REG,
 133        RK808_BUCK4_CONFIG_REG,
 134};
 135
 136static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
 137        REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
 138        REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
 139};
 140
 141static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
 142{
 143        struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
 144        int id = rdev_get_id(rdev);
 145        struct gpio_desc *gpio = pdata->dvs_gpio[id];
 146        unsigned int val;
 147        int ret;
 148
 149        if (!gpio || gpiod_get_value(gpio) == 0)
 150                return regulator_get_voltage_sel_regmap(rdev);
 151
 152        ret = regmap_read(rdev->regmap,
 153                          rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
 154                          &val);
 155        if (ret != 0)
 156                return ret;
 157
 158        val &= rdev->desc->vsel_mask;
 159        val >>= ffs(rdev->desc->vsel_mask) - 1;
 160
 161        return val;
 162}
 163
 164static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
 165                                             unsigned sel)
 166{
 167        int ret, delta_sel;
 168        unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
 169
 170        ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
 171        if (ret != 0)
 172                return ret;
 173
 174        tmp = val & ~mask;
 175        old_sel = val & mask;
 176        old_sel >>= ffs(mask) - 1;
 177        delta_sel = sel - old_sel;
 178
 179        /*
 180         * If directly modify the register to change the voltage, we will face
 181         * the risk of overshoot. Put it into a multi-step, can effectively
 182         * avoid this problem, a step is 100mv here.
 183         */
 184        while (delta_sel > MAX_STEPS_ONE_TIME) {
 185                old_sel += MAX_STEPS_ONE_TIME;
 186                val = old_sel << (ffs(mask) - 1);
 187                val |= tmp;
 188
 189                /*
 190                 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
 191                 * 3 bytes (24 bits) plus start and stop so 26 bits.  So we've
 192                 * got more than 65 us between each voltage change and thus
 193                 * won't ramp faster than ~1500 uV / us.
 194                 */
 195                ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
 196                delta_sel = sel - old_sel;
 197        }
 198
 199        sel <<= ffs(mask) - 1;
 200        val = tmp | sel;
 201        ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
 202
 203        /*
 204         * When we change the voltage register directly, the ramp rate is about
 205         * 100000uv/us, wait 1us to make sure the target voltage to be stable,
 206         * so we needn't wait extra time after that.
 207         */
 208        udelay(1);
 209
 210        return ret;
 211}
 212
 213static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
 214                                         unsigned sel)
 215{
 216        struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
 217        int id = rdev_get_id(rdev);
 218        struct gpio_desc *gpio = pdata->dvs_gpio[id];
 219        unsigned int reg = rdev->desc->vsel_reg;
 220        unsigned old_sel;
 221        int ret, gpio_level;
 222
 223        if (!gpio)
 224                return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
 225
 226        gpio_level = gpiod_get_value(gpio);
 227        if (gpio_level == 0) {
 228                reg += RK808_DVS_REG_OFFSET;
 229                ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
 230        } else {
 231                ret = regmap_read(rdev->regmap,
 232                                  reg + RK808_DVS_REG_OFFSET,
 233                                  &old_sel);
 234        }
 235
 236        if (ret != 0)
 237                return ret;
 238
 239        sel <<= ffs(rdev->desc->vsel_mask) - 1;
 240        sel |= old_sel & ~rdev->desc->vsel_mask;
 241
 242        ret = regmap_write(rdev->regmap, reg, sel);
 243        if (ret)
 244                return ret;
 245
 246        gpiod_set_value(gpio, !gpio_level);
 247
 248        return ret;
 249}
 250
 251static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
 252                                       unsigned int old_selector,
 253                                       unsigned int new_selector)
 254{
 255        struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
 256        int id = rdev_get_id(rdev);
 257        struct gpio_desc *gpio = pdata->dvs_gpio[id];
 258
 259        /* if there is no dvs1/2 pin, we don't need wait extra time here. */
 260        if (!gpio)
 261                return 0;
 262
 263        return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
 264}
 265
 266static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
 267{
 268        unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
 269        unsigned int reg = rk808_buck_config_regs[rdev_get_id(rdev)];
 270
 271        switch (ramp_delay) {
 272        case 1 ... 2000:
 273                ramp_value = RK808_RAMP_RATE_2MV_PER_US;
 274                break;
 275        case 2001 ... 4000:
 276                ramp_value = RK808_RAMP_RATE_4MV_PER_US;
 277                break;
 278        case 4001 ... 6000:
 279                ramp_value = RK808_RAMP_RATE_6MV_PER_US;
 280                break;
 281        case 6001 ... 10000:
 282                break;
 283        default:
 284                pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
 285                        rdev->desc->name, ramp_delay);
 286        }
 287
 288        return regmap_update_bits(rdev->regmap, reg,
 289                                  RK808_RAMP_RATE_MASK, ramp_value);
 290}
 291
 292static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
 293{
 294        unsigned int reg;
 295        int sel = regulator_map_voltage_linear(rdev, uv, uv);
 296
 297        if (sel < 0)
 298                return -EINVAL;
 299
 300        reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
 301
 302        return regmap_update_bits(rdev->regmap, reg,
 303                                  rdev->desc->vsel_mask,
 304                                  sel);
 305}
 306
 307static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
 308{
 309        unsigned int reg;
 310        int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
 311
 312        if (sel < 0)
 313                return -EINVAL;
 314
 315        reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
 316
 317        return regmap_update_bits(rdev->regmap, reg,
 318                                  rdev->desc->vsel_mask,
 319                                  sel);
 320}
 321
 322static int rk805_set_suspend_enable(struct regulator_dev *rdev)
 323{
 324        unsigned int reg;
 325
 326        reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
 327
 328        return regmap_update_bits(rdev->regmap, reg,
 329                                  rdev->desc->enable_mask,
 330                                  rdev->desc->enable_mask);
 331}
 332
 333static int rk805_set_suspend_disable(struct regulator_dev *rdev)
 334{
 335        unsigned int reg;
 336
 337        reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
 338
 339        return regmap_update_bits(rdev->regmap, reg,
 340                                  rdev->desc->enable_mask,
 341                                  0);
 342}
 343
 344static int rk808_set_suspend_enable(struct regulator_dev *rdev)
 345{
 346        unsigned int reg;
 347
 348        reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
 349
 350        return regmap_update_bits(rdev->regmap, reg,
 351                                  rdev->desc->enable_mask,
 352                                  0);
 353}
 354
 355static int rk808_set_suspend_disable(struct regulator_dev *rdev)
 356{
 357        unsigned int reg;
 358
 359        reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
 360
 361        return regmap_update_bits(rdev->regmap, reg,
 362                                  rdev->desc->enable_mask,
 363                                  rdev->desc->enable_mask);
 364}
 365
 366static struct regulator_ops rk805_reg_ops = {
 367                .list_voltage           = regulator_list_voltage_linear,
 368                .map_voltage            = regulator_map_voltage_linear,
 369                .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 370                .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 371                .enable                 = regulator_enable_regmap,
 372                .disable                = regulator_disable_regmap,
 373                .is_enabled             = regulator_is_enabled_regmap,
 374                .set_suspend_voltage    = rk808_set_suspend_voltage,
 375                .set_suspend_enable     = rk805_set_suspend_enable,
 376                .set_suspend_disable    = rk805_set_suspend_disable,
 377};
 378
 379static struct regulator_ops rk805_switch_ops = {
 380                .enable                 = regulator_enable_regmap,
 381                .disable                = regulator_disable_regmap,
 382                .is_enabled             = regulator_is_enabled_regmap,
 383                .set_suspend_enable     = rk805_set_suspend_enable,
 384                .set_suspend_disable    = rk805_set_suspend_disable,
 385};
 386
 387static struct regulator_ops rk808_buck1_2_ops = {
 388        .list_voltage           = regulator_list_voltage_linear,
 389        .map_voltage            = regulator_map_voltage_linear,
 390        .get_voltage_sel        = rk808_buck1_2_get_voltage_sel_regmap,
 391        .set_voltage_sel        = rk808_buck1_2_set_voltage_sel,
 392        .set_voltage_time_sel   = rk808_buck1_2_set_voltage_time_sel,
 393        .enable                 = regulator_enable_regmap,
 394        .disable                = regulator_disable_regmap,
 395        .is_enabled             = regulator_is_enabled_regmap,
 396        .set_ramp_delay         = rk808_set_ramp_delay,
 397        .set_suspend_voltage    = rk808_set_suspend_voltage,
 398        .set_suspend_enable     = rk808_set_suspend_enable,
 399        .set_suspend_disable    = rk808_set_suspend_disable,
 400};
 401
 402static struct regulator_ops rk808_reg_ops = {
 403        .list_voltage           = regulator_list_voltage_linear,
 404        .map_voltage            = regulator_map_voltage_linear,
 405        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 406        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 407        .enable                 = regulator_enable_regmap,
 408        .disable                = regulator_disable_regmap,
 409        .is_enabled             = regulator_is_enabled_regmap,
 410        .set_suspend_voltage    = rk808_set_suspend_voltage,
 411        .set_suspend_enable     = rk808_set_suspend_enable,
 412        .set_suspend_disable    = rk808_set_suspend_disable,
 413};
 414
 415static struct regulator_ops rk808_reg_ops_ranges = {
 416        .list_voltage           = regulator_list_voltage_linear_range,
 417        .map_voltage            = regulator_map_voltage_linear_range,
 418        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 419        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 420        .enable                 = regulator_enable_regmap,
 421        .disable                = regulator_disable_regmap,
 422        .is_enabled             = regulator_is_enabled_regmap,
 423        .set_suspend_voltage    = rk808_set_suspend_voltage_range,
 424        .set_suspend_enable     = rk808_set_suspend_enable,
 425        .set_suspend_disable    = rk808_set_suspend_disable,
 426};
 427
 428static struct regulator_ops rk808_switch_ops = {
 429        .enable                 = regulator_enable_regmap,
 430        .disable                = regulator_disable_regmap,
 431        .is_enabled             = regulator_is_enabled_regmap,
 432        .set_suspend_enable     = rk808_set_suspend_enable,
 433        .set_suspend_disable    = rk808_set_suspend_disable,
 434};
 435
 436static const struct regulator_desc rk805_reg[] = {
 437        {
 438                .name = "DCDC_REG1",
 439                .supply_name = "vcc1",
 440                .of_match = of_match_ptr("DCDC_REG1"),
 441                .regulators_node = of_match_ptr("regulators"),
 442                .id = RK805_ID_DCDC1,
 443                .ops = &rk805_reg_ops,
 444                .type = REGULATOR_VOLTAGE,
 445                .min_uV = 712500,
 446                .uV_step = 12500,
 447                .n_voltages = 64,
 448                .vsel_reg = RK805_BUCK1_ON_VSEL_REG,
 449                .vsel_mask = RK818_BUCK_VSEL_MASK,
 450                .enable_reg = RK805_DCDC_EN_REG,
 451                .enable_mask = BIT(0),
 452                .owner = THIS_MODULE,
 453        }, {
 454                .name = "DCDC_REG2",
 455                .supply_name = "vcc2",
 456                .of_match = of_match_ptr("DCDC_REG2"),
 457                .regulators_node = of_match_ptr("regulators"),
 458                .id = RK805_ID_DCDC2,
 459                .ops = &rk805_reg_ops,
 460                .type = REGULATOR_VOLTAGE,
 461                .min_uV = 712500,
 462                .uV_step = 12500,
 463                .n_voltages = 64,
 464                .vsel_reg = RK805_BUCK2_ON_VSEL_REG,
 465                .vsel_mask = RK818_BUCK_VSEL_MASK,
 466                .enable_reg = RK805_DCDC_EN_REG,
 467                .enable_mask = BIT(1),
 468                .owner = THIS_MODULE,
 469        }, {
 470                .name = "DCDC_REG3",
 471                .supply_name = "vcc3",
 472                .of_match = of_match_ptr("DCDC_REG3"),
 473                .regulators_node = of_match_ptr("regulators"),
 474                .id = RK805_ID_DCDC3,
 475                .ops = &rk805_switch_ops,
 476                .type = REGULATOR_VOLTAGE,
 477                .n_voltages = 1,
 478                .enable_reg = RK805_DCDC_EN_REG,
 479                .enable_mask = BIT(2),
 480                .owner = THIS_MODULE,
 481        },
 482
 483        RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
 484                RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
 485                RK805_DCDC_EN_REG, BIT(3), 0),
 486
 487        RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
 488                RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
 489                BIT(0), 400),
 490        RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
 491                RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
 492                BIT(1), 400),
 493        RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
 494                RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
 495                BIT(2), 400),
 496};
 497
 498static const struct regulator_desc rk808_reg[] = {
 499        {
 500                .name = "DCDC_REG1",
 501                .supply_name = "vcc1",
 502                .of_match = of_match_ptr("DCDC_REG1"),
 503                .regulators_node = of_match_ptr("regulators"),
 504                .id = RK808_ID_DCDC1,
 505                .ops = &rk808_buck1_2_ops,
 506                .type = REGULATOR_VOLTAGE,
 507                .min_uV = 712500,
 508                .uV_step = 12500,
 509                .n_voltages = 64,
 510                .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
 511                .vsel_mask = RK808_BUCK_VSEL_MASK,
 512                .enable_reg = RK808_DCDC_EN_REG,
 513                .enable_mask = BIT(0),
 514                .owner = THIS_MODULE,
 515        }, {
 516                .name = "DCDC_REG2",
 517                .supply_name = "vcc2",
 518                .of_match = of_match_ptr("DCDC_REG2"),
 519                .regulators_node = of_match_ptr("regulators"),
 520                .id = RK808_ID_DCDC2,
 521                .ops = &rk808_buck1_2_ops,
 522                .type = REGULATOR_VOLTAGE,
 523                .min_uV = 712500,
 524                .uV_step = 12500,
 525                .n_voltages = 64,
 526                .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
 527                .vsel_mask = RK808_BUCK_VSEL_MASK,
 528                .enable_reg = RK808_DCDC_EN_REG,
 529                .enable_mask = BIT(1),
 530                .owner = THIS_MODULE,
 531        }, {
 532                .name = "DCDC_REG3",
 533                .supply_name = "vcc3",
 534                .of_match = of_match_ptr("DCDC_REG3"),
 535                .regulators_node = of_match_ptr("regulators"),
 536                .id = RK808_ID_DCDC3,
 537                .ops = &rk808_switch_ops,
 538                .type = REGULATOR_VOLTAGE,
 539                .n_voltages = 1,
 540                .enable_reg = RK808_DCDC_EN_REG,
 541                .enable_mask = BIT(2),
 542                .owner = THIS_MODULE,
 543        },
 544        RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
 545                RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
 546                RK808_DCDC_EN_REG, BIT(3), 0),
 547        RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
 548                RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 549                BIT(0), 400),
 550        RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
 551                RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 552                BIT(1), 400),
 553        {
 554                .name = "LDO_REG3",
 555                .supply_name = "vcc7",
 556                .of_match = of_match_ptr("LDO_REG3"),
 557                .regulators_node = of_match_ptr("regulators"),
 558                .id = RK808_ID_LDO3,
 559                .ops = &rk808_reg_ops_ranges,
 560                .type = REGULATOR_VOLTAGE,
 561                .n_voltages = 16,
 562                .linear_ranges = rk808_ldo3_voltage_ranges,
 563                .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
 564                .vsel_reg = RK808_LDO3_ON_VSEL_REG,
 565                .vsel_mask = RK808_BUCK4_VSEL_MASK,
 566                .enable_reg = RK808_LDO_EN_REG,
 567                .enable_mask = BIT(2),
 568                .enable_time = 400,
 569                .owner = THIS_MODULE,
 570        },
 571        RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
 572                RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 573                BIT(3), 400),
 574        RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
 575                RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 576                BIT(4), 400),
 577        RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
 578                RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 579                BIT(5), 400),
 580        RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
 581                RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 582                BIT(6), 400),
 583        RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
 584                RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
 585                BIT(7), 400),
 586        RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
 587                RK808_DCDC_EN_REG, BIT(5)),
 588        RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
 589                RK808_DCDC_EN_REG, BIT(6)),
 590};
 591
 592static const struct regulator_desc rk818_reg[] = {
 593        {
 594                .name = "DCDC_REG1",
 595                .supply_name = "vcc1",
 596                .of_match = of_match_ptr("DCDC_REG1"),
 597                .regulators_node = of_match_ptr("regulators"),
 598                .id = RK818_ID_DCDC1,
 599                .ops = &rk808_reg_ops,
 600                .type = REGULATOR_VOLTAGE,
 601                .min_uV = 712500,
 602                .uV_step = 12500,
 603                .n_voltages = 64,
 604                .vsel_reg = RK818_BUCK1_ON_VSEL_REG,
 605                .vsel_mask = RK818_BUCK_VSEL_MASK,
 606                .enable_reg = RK818_DCDC_EN_REG,
 607                .enable_mask = BIT(0),
 608                .owner = THIS_MODULE,
 609        }, {
 610                .name = "DCDC_REG2",
 611                .supply_name = "vcc2",
 612                .of_match = of_match_ptr("DCDC_REG2"),
 613                .regulators_node = of_match_ptr("regulators"),
 614                .id = RK818_ID_DCDC2,
 615                .ops = &rk808_reg_ops,
 616                .type = REGULATOR_VOLTAGE,
 617                .min_uV = 712500,
 618                .uV_step = 12500,
 619                .n_voltages = 64,
 620                .vsel_reg = RK818_BUCK2_ON_VSEL_REG,
 621                .vsel_mask = RK818_BUCK_VSEL_MASK,
 622                .enable_reg = RK818_DCDC_EN_REG,
 623                .enable_mask = BIT(1),
 624                .owner = THIS_MODULE,
 625        }, {
 626                .name = "DCDC_REG3",
 627                .supply_name = "vcc3",
 628                .of_match = of_match_ptr("DCDC_REG3"),
 629                .regulators_node = of_match_ptr("regulators"),
 630                .id = RK818_ID_DCDC3,
 631                .ops = &rk808_switch_ops,
 632                .type = REGULATOR_VOLTAGE,
 633                .n_voltages = 1,
 634                .enable_reg = RK818_DCDC_EN_REG,
 635                .enable_mask = BIT(2),
 636                .owner = THIS_MODULE,
 637        },
 638        RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
 639                RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
 640                RK818_DCDC_EN_REG, BIT(3), 0),
 641        RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
 642                RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
 643                RK818_DCDC_EN_REG, BIT(4), 0),
 644        RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
 645                RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 646                BIT(0), 400),
 647        RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
 648                RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 649                BIT(1), 400),
 650        {
 651                .name = "LDO_REG3",
 652                .supply_name = "vcc7",
 653                .of_match = of_match_ptr("LDO_REG3"),
 654                .regulators_node = of_match_ptr("regulators"),
 655                .id = RK818_ID_LDO3,
 656                .ops = &rk808_reg_ops_ranges,
 657                .type = REGULATOR_VOLTAGE,
 658                .n_voltages = 16,
 659                .linear_ranges = rk808_ldo3_voltage_ranges,
 660                .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
 661                .vsel_reg = RK818_LDO3_ON_VSEL_REG,
 662                .vsel_mask = RK818_LDO3_ON_VSEL_MASK,
 663                .enable_reg = RK818_LDO_EN_REG,
 664                .enable_mask = BIT(2),
 665                .enable_time = 400,
 666                .owner = THIS_MODULE,
 667        },
 668        RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
 669                RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 670                BIT(3), 400),
 671        RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
 672                RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 673                BIT(4), 400),
 674        RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
 675                RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 676                BIT(5), 400),
 677        RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
 678                RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 679                BIT(6), 400),
 680        RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
 681                RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
 682                BIT(7), 400),
 683        RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
 684                RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
 685                RK818_DCDC_EN_REG, BIT(5), 400),
 686        RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
 687                RK818_DCDC_EN_REG, BIT(6)),
 688        RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
 689                RK818_H5V_EN_REG, BIT(0)),
 690        RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
 691                RK818_DCDC_EN_REG, BIT(7)),
 692};
 693
 694static int rk808_regulator_dt_parse_pdata(struct device *dev,
 695                                   struct device *client_dev,
 696                                   struct regmap *map,
 697                                   struct rk808_regulator_data *pdata)
 698{
 699        struct device_node *np;
 700        int tmp, ret = 0, i;
 701
 702        np = of_get_child_by_name(client_dev->of_node, "regulators");
 703        if (!np)
 704                return -ENXIO;
 705
 706        for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
 707                pdata->dvs_gpio[i] =
 708                        devm_gpiod_get_index_optional(client_dev, "dvs", i,
 709                                                      GPIOD_OUT_LOW);
 710                if (IS_ERR(pdata->dvs_gpio[i])) {
 711                        ret = PTR_ERR(pdata->dvs_gpio[i]);
 712                        dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
 713                        goto dt_parse_end;
 714                }
 715
 716                if (!pdata->dvs_gpio[i]) {
 717                        dev_warn(dev, "there is no dvs%d gpio\n", i);
 718                        continue;
 719                }
 720
 721                tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
 722                ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
 723                                gpiod_is_active_low(pdata->dvs_gpio[i]) ?
 724                                0 : tmp);
 725        }
 726
 727dt_parse_end:
 728        of_node_put(np);
 729        return ret;
 730}
 731
 732static int rk808_regulator_probe(struct platform_device *pdev)
 733{
 734        struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
 735        struct i2c_client *client = rk808->i2c;
 736        struct regulator_config config = {};
 737        struct regulator_dev *rk808_rdev;
 738        struct rk808_regulator_data *pdata;
 739        const struct regulator_desc *regulators;
 740        int ret, i, nregulators;
 741
 742        pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 743        if (!pdata)
 744                return -ENOMEM;
 745
 746        ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
 747                                             rk808->regmap, pdata);
 748        if (ret < 0)
 749                return ret;
 750
 751        platform_set_drvdata(pdev, pdata);
 752
 753        switch (rk808->variant) {
 754        case RK805_ID:
 755                regulators = rk805_reg;
 756                nregulators = RK805_NUM_REGULATORS;
 757                break;
 758        case RK808_ID:
 759                regulators = rk808_reg;
 760                nregulators = RK808_NUM_REGULATORS;
 761                break;
 762        case RK818_ID:
 763                regulators = rk818_reg;
 764                nregulators = RK818_NUM_REGULATORS;
 765                break;
 766        default:
 767                dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
 768                        rk808->variant);
 769                return -EINVAL;
 770        }
 771
 772        config.dev = &client->dev;
 773        config.driver_data = pdata;
 774        config.regmap = rk808->regmap;
 775
 776        /* Instantiate the regulators */
 777        for (i = 0; i < nregulators; i++) {
 778                rk808_rdev = devm_regulator_register(&pdev->dev,
 779                                                     &regulators[i], &config);
 780                if (IS_ERR(rk808_rdev)) {
 781                        dev_err(&client->dev,
 782                                "failed to register %d regulator\n", i);
 783                        return PTR_ERR(rk808_rdev);
 784                }
 785        }
 786
 787        return 0;
 788}
 789
 790static struct platform_driver rk808_regulator_driver = {
 791        .probe = rk808_regulator_probe,
 792        .driver = {
 793                .name = "rk808-regulator"
 794        },
 795};
 796
 797module_platform_driver(rk808_regulator_driver);
 798
 799MODULE_DESCRIPTION("regulator driver for the RK808/RK818 series PMICs");
 800MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
 801MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
 802MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
 803MODULE_LICENSE("GPL");
 804MODULE_ALIAS("platform:rk808-regulator");
 805