linux/drivers/regulator/s2mpa01.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// Copyright (c) 2013 Samsung Electronics Co., Ltd
   4//              http://www.samsung.com
   5
   6#include <linux/bug.h>
   7#include <linux/err.h>
   8#include <linux/gpio.h>
   9#include <linux/slab.h>
  10#include <linux/module.h>
  11#include <linux/of.h>
  12#include <linux/regmap.h>
  13#include <linux/platform_device.h>
  14#include <linux/regulator/driver.h>
  15#include <linux/regulator/machine.h>
  16#include <linux/regulator/of_regulator.h>
  17#include <linux/mfd/samsung/core.h>
  18#include <linux/mfd/samsung/s2mpa01.h>
  19
  20#define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
  21
  22struct s2mpa01_info {
  23        struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX];
  24        int ramp_delay24;
  25        int ramp_delay3;
  26        int ramp_delay5;
  27        int ramp_delay16;
  28        int ramp_delay7;
  29        int ramp_delay8910;
  30};
  31
  32static int get_ramp_delay(int ramp_delay)
  33{
  34        unsigned char cnt = 0;
  35
  36        ramp_delay /= 6250;
  37
  38        while (true) {
  39                ramp_delay = ramp_delay >> 1;
  40                if (ramp_delay == 0)
  41                        break;
  42                cnt++;
  43        }
  44
  45        if (cnt > 3)
  46                cnt = 3;
  47
  48        return cnt;
  49}
  50
  51static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  52                                   unsigned int old_selector,
  53                                   unsigned int new_selector)
  54{
  55        struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
  56        unsigned int ramp_delay = 0;
  57        int old_volt, new_volt;
  58
  59        switch (rdev_get_id(rdev)) {
  60        case S2MPA01_BUCK2:
  61        case S2MPA01_BUCK4:
  62                ramp_delay = s2mpa01->ramp_delay24;
  63                break;
  64        case S2MPA01_BUCK3:
  65                ramp_delay = s2mpa01->ramp_delay3;
  66                break;
  67        case S2MPA01_BUCK5:
  68                ramp_delay = s2mpa01->ramp_delay5;
  69                break;
  70        case S2MPA01_BUCK1:
  71        case S2MPA01_BUCK6:
  72                ramp_delay = s2mpa01->ramp_delay16;
  73                break;
  74        case S2MPA01_BUCK7:
  75                ramp_delay = s2mpa01->ramp_delay7;
  76                break;
  77        case S2MPA01_BUCK8:
  78        case S2MPA01_BUCK9:
  79        case S2MPA01_BUCK10:
  80                ramp_delay = s2mpa01->ramp_delay8910;
  81                break;
  82        }
  83
  84        if (ramp_delay == 0)
  85                ramp_delay = rdev->desc->ramp_delay;
  86
  87        old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
  88        new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
  89
  90        return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
  91}
  92
  93static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  94{
  95        struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
  96        unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
  97        unsigned int ramp_enable = 1, enable_shift = 0;
  98        int ret;
  99
 100        switch (rdev_get_id(rdev)) {
 101        case S2MPA01_BUCK1:
 102                enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
 103                if (!ramp_delay) {
 104                        ramp_enable = 0;
 105                        break;
 106                }
 107
 108                if (ramp_delay > s2mpa01->ramp_delay16)
 109                        s2mpa01->ramp_delay16 = ramp_delay;
 110                else
 111                        ramp_delay = s2mpa01->ramp_delay16;
 112
 113                ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
 114                break;
 115        case S2MPA01_BUCK2:
 116                enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
 117                if (!ramp_delay) {
 118                        ramp_enable = 0;
 119                        break;
 120                }
 121
 122                if (ramp_delay > s2mpa01->ramp_delay24)
 123                        s2mpa01->ramp_delay24 = ramp_delay;
 124                else
 125                        ramp_delay = s2mpa01->ramp_delay24;
 126
 127                ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
 128                ramp_reg = S2MPA01_REG_RAMP1;
 129                break;
 130        case S2MPA01_BUCK3:
 131                enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
 132                if (!ramp_delay) {
 133                        ramp_enable = 0;
 134                        break;
 135                }
 136
 137                s2mpa01->ramp_delay3 = ramp_delay;
 138                ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
 139                ramp_reg = S2MPA01_REG_RAMP1;
 140                break;
 141        case S2MPA01_BUCK4:
 142                enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
 143                if (!ramp_delay) {
 144                        ramp_enable = 0;
 145                        break;
 146                }
 147
 148                if (ramp_delay > s2mpa01->ramp_delay24)
 149                        s2mpa01->ramp_delay24 = ramp_delay;
 150                else
 151                        ramp_delay = s2mpa01->ramp_delay24;
 152
 153                ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
 154                ramp_reg = S2MPA01_REG_RAMP1;
 155                break;
 156        case S2MPA01_BUCK5:
 157                s2mpa01->ramp_delay5 = ramp_delay;
 158                ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
 159                break;
 160        case S2MPA01_BUCK6:
 161                if (ramp_delay > s2mpa01->ramp_delay16)
 162                        s2mpa01->ramp_delay16 = ramp_delay;
 163                else
 164                        ramp_delay = s2mpa01->ramp_delay16;
 165
 166                ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
 167                break;
 168        case S2MPA01_BUCK7:
 169                s2mpa01->ramp_delay7 = ramp_delay;
 170                ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
 171                break;
 172        case S2MPA01_BUCK8:
 173        case S2MPA01_BUCK9:
 174        case S2MPA01_BUCK10:
 175                if (ramp_delay > s2mpa01->ramp_delay8910)
 176                        s2mpa01->ramp_delay8910 = ramp_delay;
 177                else
 178                        ramp_delay = s2mpa01->ramp_delay8910;
 179
 180                ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
 181                break;
 182        default:
 183                return 0;
 184        }
 185
 186        if (!ramp_enable)
 187                goto ramp_disable;
 188
 189        /* Ramp delay can be enabled/disabled only for buck[1234] */
 190        if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
 191                        rdev_get_id(rdev) <= S2MPA01_BUCK4) {
 192                ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
 193                                         1 << enable_shift, 1 << enable_shift);
 194                if (ret) {
 195                        dev_err(&rdev->dev, "failed to enable ramp rate\n");
 196                        return ret;
 197                }
 198        }
 199
 200        ramp_val = get_ramp_delay(ramp_delay);
 201
 202        return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
 203                                  ramp_val << ramp_shift);
 204
 205ramp_disable:
 206        return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
 207                                  1 << enable_shift, 0);
 208}
 209
 210static const struct regulator_ops s2mpa01_ldo_ops = {
 211        .list_voltage           = regulator_list_voltage_linear,
 212        .map_voltage            = regulator_map_voltage_linear,
 213        .is_enabled             = regulator_is_enabled_regmap,
 214        .enable                 = regulator_enable_regmap,
 215        .disable                = regulator_disable_regmap,
 216        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 217        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 218        .set_voltage_time_sel   = regulator_set_voltage_time_sel,
 219};
 220
 221static const struct regulator_ops s2mpa01_buck_ops = {
 222        .list_voltage           = regulator_list_voltage_linear,
 223        .map_voltage            = regulator_map_voltage_linear,
 224        .is_enabled             = regulator_is_enabled_regmap,
 225        .enable                 = regulator_enable_regmap,
 226        .disable                = regulator_disable_regmap,
 227        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 228        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 229        .set_voltage_time_sel   = s2mpa01_regulator_set_voltage_time_sel,
 230        .set_ramp_delay         = s2mpa01_set_ramp_delay,
 231};
 232
 233#define regulator_desc_ldo(num, step) {                 \
 234        .name           = "LDO"#num,                    \
 235        .id             = S2MPA01_LDO##num,             \
 236        .ops            = &s2mpa01_ldo_ops,             \
 237        .type           = REGULATOR_VOLTAGE,            \
 238        .owner          = THIS_MODULE,                  \
 239        .min_uV         = MIN_800_MV,                   \
 240        .uV_step        = step,                         \
 241        .n_voltages     = S2MPA01_LDO_N_VOLTAGES,       \
 242        .vsel_reg       = S2MPA01_REG_L1CTRL + num - 1, \
 243        .vsel_mask      = S2MPA01_LDO_VSEL_MASK,        \
 244        .enable_reg     = S2MPA01_REG_L1CTRL + num - 1, \
 245        .enable_mask    = S2MPA01_ENABLE_MASK           \
 246}
 247
 248#define regulator_desc_buck1_4(num)     {                       \
 249        .name           = "BUCK"#num,                           \
 250        .id             = S2MPA01_BUCK##num,                    \
 251        .ops            = &s2mpa01_buck_ops,                    \
 252        .type           = REGULATOR_VOLTAGE,                    \
 253        .owner          = THIS_MODULE,                          \
 254        .min_uV         = MIN_600_MV,                           \
 255        .uV_step        = STEP_6_25_MV,                         \
 256        .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
 257        .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
 258        .vsel_reg       = S2MPA01_REG_B1CTRL2 + (num - 1) * 2,  \
 259        .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
 260        .enable_reg     = S2MPA01_REG_B1CTRL1 + (num - 1) * 2,  \
 261        .enable_mask    = S2MPA01_ENABLE_MASK                   \
 262}
 263
 264#define regulator_desc_buck5    {                               \
 265        .name           = "BUCK5",                              \
 266        .id             = S2MPA01_BUCK5,                        \
 267        .ops            = &s2mpa01_buck_ops,                    \
 268        .type           = REGULATOR_VOLTAGE,                    \
 269        .owner          = THIS_MODULE,                          \
 270        .min_uV         = MIN_800_MV,                           \
 271        .uV_step        = STEP_6_25_MV,                         \
 272        .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
 273        .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
 274        .vsel_reg       = S2MPA01_REG_B5CTRL2,                  \
 275        .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
 276        .enable_reg     = S2MPA01_REG_B5CTRL1,                  \
 277        .enable_mask    = S2MPA01_ENABLE_MASK                   \
 278}
 279
 280#define regulator_desc_buck6_10(num, min, step) {                       \
 281        .name           = "BUCK"#num,                           \
 282        .id             = S2MPA01_BUCK##num,                    \
 283        .ops            = &s2mpa01_buck_ops,                    \
 284        .type           = REGULATOR_VOLTAGE,                    \
 285        .owner          = THIS_MODULE,                          \
 286        .min_uV         = min,                                  \
 287        .uV_step        = step,                                 \
 288        .n_voltages     = S2MPA01_BUCK_N_VOLTAGES,              \
 289        .ramp_delay     = S2MPA01_RAMP_DELAY,                   \
 290        .vsel_reg       = S2MPA01_REG_B6CTRL2 + (num - 6) * 2,  \
 291        .vsel_mask      = S2MPA01_BUCK_VSEL_MASK,               \
 292        .enable_reg     = S2MPA01_REG_B6CTRL1 + (num - 6) * 2,  \
 293        .enable_mask    = S2MPA01_ENABLE_MASK                   \
 294}
 295
 296static const struct regulator_desc regulators[] = {
 297        regulator_desc_ldo(1, STEP_25_MV),
 298        regulator_desc_ldo(2, STEP_50_MV),
 299        regulator_desc_ldo(3, STEP_50_MV),
 300        regulator_desc_ldo(4, STEP_50_MV),
 301        regulator_desc_ldo(5, STEP_25_MV),
 302        regulator_desc_ldo(6, STEP_25_MV),
 303        regulator_desc_ldo(7, STEP_50_MV),
 304        regulator_desc_ldo(8, STEP_50_MV),
 305        regulator_desc_ldo(9, STEP_50_MV),
 306        regulator_desc_ldo(10, STEP_50_MV),
 307        regulator_desc_ldo(11, STEP_50_MV),
 308        regulator_desc_ldo(12, STEP_50_MV),
 309        regulator_desc_ldo(13, STEP_50_MV),
 310        regulator_desc_ldo(14, STEP_50_MV),
 311        regulator_desc_ldo(15, STEP_50_MV),
 312        regulator_desc_ldo(16, STEP_50_MV),
 313        regulator_desc_ldo(17, STEP_50_MV),
 314        regulator_desc_ldo(18, STEP_50_MV),
 315        regulator_desc_ldo(19, STEP_50_MV),
 316        regulator_desc_ldo(20, STEP_50_MV),
 317        regulator_desc_ldo(21, STEP_50_MV),
 318        regulator_desc_ldo(22, STEP_50_MV),
 319        regulator_desc_ldo(23, STEP_50_MV),
 320        regulator_desc_ldo(24, STEP_50_MV),
 321        regulator_desc_ldo(25, STEP_50_MV),
 322        regulator_desc_ldo(26, STEP_25_MV),
 323        regulator_desc_buck1_4(1),
 324        regulator_desc_buck1_4(2),
 325        regulator_desc_buck1_4(3),
 326        regulator_desc_buck1_4(4),
 327        regulator_desc_buck5,
 328        regulator_desc_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
 329        regulator_desc_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
 330        regulator_desc_buck6_10(8, MIN_800_MV, STEP_12_5_MV),
 331        regulator_desc_buck6_10(9, MIN_1500_MV, STEP_12_5_MV),
 332        regulator_desc_buck6_10(10, MIN_1000_MV, STEP_12_5_MV),
 333};
 334
 335static int s2mpa01_pmic_probe(struct platform_device *pdev)
 336{
 337        struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 338        struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
 339        struct device_node *reg_np = NULL;
 340        struct regulator_config config = { };
 341        struct of_regulator_match *rdata;
 342        struct s2mpa01_info *s2mpa01;
 343        int i;
 344
 345        s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
 346        if (!s2mpa01)
 347                return -ENOMEM;
 348
 349        rdata = s2mpa01->rdata;
 350        for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
 351                rdata[i].name = regulators[i].name;
 352
 353        if (iodev->dev->of_node) {
 354                reg_np = of_get_child_by_name(iodev->dev->of_node,
 355                                                        "regulators");
 356                if (!reg_np) {
 357                        dev_err(&pdev->dev,
 358                                "could not find regulators sub-node\n");
 359                        return -EINVAL;
 360                }
 361
 362                of_regulator_match(&pdev->dev, reg_np, rdata,
 363                                                S2MPA01_REGULATOR_MAX);
 364                of_node_put(reg_np);
 365        }
 366
 367        platform_set_drvdata(pdev, s2mpa01);
 368
 369        config.dev = &pdev->dev;
 370        config.regmap = iodev->regmap_pmic;
 371        config.driver_data = s2mpa01;
 372
 373        for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
 374                struct regulator_dev *rdev;
 375                if (pdata)
 376                        config.init_data = pdata->regulators[i].initdata;
 377                else
 378                        config.init_data = rdata[i].init_data;
 379
 380                if (reg_np)
 381                        config.of_node = rdata[i].of_node;
 382
 383                rdev = devm_regulator_register(&pdev->dev,
 384                                                &regulators[i], &config);
 385                if (IS_ERR(rdev)) {
 386                        dev_err(&pdev->dev, "regulator init failed for %d\n",
 387                                i);
 388                        return PTR_ERR(rdev);
 389                }
 390        }
 391
 392        return 0;
 393}
 394
 395static const struct platform_device_id s2mpa01_pmic_id[] = {
 396        { "s2mpa01-pmic", 0},
 397        { },
 398};
 399MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
 400
 401static struct platform_driver s2mpa01_pmic_driver = {
 402        .driver = {
 403                .name = "s2mpa01-pmic",
 404        },
 405        .probe = s2mpa01_pmic_probe,
 406        .id_table = s2mpa01_pmic_id,
 407};
 408
 409module_platform_driver(s2mpa01_pmic_driver);
 410
 411/* Module information */
 412MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
 413MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
 414MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
 415MODULE_LICENSE("GPL");
 416