linux/drivers/regulator/da9052-regulator.c
<<
>>
Prefs
   1/*
   2* da9052-regulator.c: Regulator driver for DA9052
   3*
   4* Copyright(c) 2011 Dialog Semiconductor Ltd.
   5*
   6* Author: David Dajun Chen <dchen@diasemi.com>
   7*
   8* This program is free software; you can redistribute it and/or modify
   9* it under the terms of the GNU General Public License as published by
  10* the Free Software Foundation; either version 2 of the License, or
  11* (at your option) any later version.
  12*
  13*/
  14
  15#include <linux/module.h>
  16#include <linux/moduleparam.h>
  17#include <linux/init.h>
  18#include <linux/err.h>
  19#include <linux/platform_device.h>
  20#include <linux/regulator/driver.h>
  21#include <linux/regulator/machine.h>
  22#ifdef CONFIG_OF
  23#include <linux/of.h>
  24#include <linux/regulator/of_regulator.h>
  25#endif
  26
  27#include <linux/mfd/da9052/da9052.h>
  28#include <linux/mfd/da9052/reg.h>
  29#include <linux/mfd/da9052/pdata.h>
  30
  31/* Buck step size */
  32#define DA9052_BUCK_PERI_3uV_STEP               100000
  33#define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV       24
  34#define DA9052_CONST_3uV                        3000000
  35
  36#define DA9052_MIN_UA           0
  37#define DA9052_MAX_UA           3
  38#define DA9052_CURRENT_RANGE    4
  39
  40/* Bit masks */
  41#define DA9052_BUCK_ILIM_MASK_EVEN      0x0c
  42#define DA9052_BUCK_ILIM_MASK_ODD       0xc0
  43
  44/* DA9052 REGULATOR IDs */
  45#define DA9052_ID_BUCK1         0
  46#define DA9052_ID_BUCK2         1
  47#define DA9052_ID_BUCK3         2
  48#define DA9052_ID_BUCK4         3
  49#define DA9052_ID_LDO1          4
  50#define DA9052_ID_LDO2          5
  51#define DA9052_ID_LDO3          6
  52#define DA9052_ID_LDO4          7
  53#define DA9052_ID_LDO5          8
  54#define DA9052_ID_LDO6          9
  55#define DA9052_ID_LDO7          10
  56#define DA9052_ID_LDO8          11
  57#define DA9052_ID_LDO9          12
  58#define DA9052_ID_LDO10         13
  59
  60static const u32 da9052_current_limits[3][4] = {
  61        {700000, 800000, 1000000, 1200000},     /* DA9052-BC BUCKs */
  62        {1600000, 2000000, 2400000, 3000000},   /* DA9053-AA/Bx BUCK-CORE */
  63        {800000, 1000000, 1200000, 1500000},    /* DA9053-AA/Bx BUCK-PRO,
  64                                                 * BUCK-MEM and BUCK-PERI
  65                                                */
  66};
  67
  68struct da9052_regulator_info {
  69        struct regulator_desc reg_desc;
  70        int step_uV;
  71        int min_uV;
  72        int max_uV;
  73};
  74
  75struct da9052_regulator {
  76        struct da9052 *da9052;
  77        struct da9052_regulator_info *info;
  78        struct regulator_dev *rdev;
  79};
  80
  81static int verify_range(struct da9052_regulator_info *info,
  82                         int min_uV, int max_uV)
  83{
  84        if (min_uV > info->max_uV || max_uV < info->min_uV)
  85                return -EINVAL;
  86
  87        return 0;
  88}
  89
  90static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
  91{
  92        struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
  93        int offset = rdev_get_id(rdev);
  94        int ret, row = 2;
  95
  96        ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
  97        if (ret < 0)
  98                return ret;
  99
 100        /* Determine the even or odd position of the buck current limit
 101         * register field
 102        */
 103        if (offset % 2 == 0)
 104                ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
 105        else
 106                ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
 107
 108        /* Select the appropriate current limit range */
 109        if (regulator->da9052->chip_id == DA9052)
 110                row = 0;
 111        else if (offset == 0)
 112                row = 1;
 113
 114        return da9052_current_limits[row][ret];
 115}
 116
 117static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
 118                                          int max_uA)
 119{
 120        struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
 121        int offset = rdev_get_id(rdev);
 122        int reg_val = 0;
 123        int i, row = 2;
 124
 125        /* Select the appropriate current limit range */
 126        if (regulator->da9052->chip_id == DA9052)
 127                row = 0;
 128        else if (offset == 0)
 129                row = 1;
 130
 131        for (i = DA9052_CURRENT_RANGE - 1; i >= 0; i--) {
 132                if ((min_uA <= da9052_current_limits[row][i]) &&
 133                    (da9052_current_limits[row][i] <= max_uA)) {
 134                        reg_val = i;
 135                        break;
 136                }
 137        }
 138
 139        if (i < 0)
 140                return -EINVAL;
 141
 142        /* Determine the even or odd position of the buck current limit
 143         * register field
 144        */
 145        if (offset % 2 == 0)
 146                return da9052_reg_update(regulator->da9052,
 147                                         DA9052_BUCKA_REG + offset/2,
 148                                         DA9052_BUCK_ILIM_MASK_EVEN,
 149                                         reg_val << 2);
 150        else
 151                return da9052_reg_update(regulator->da9052,
 152                                         DA9052_BUCKA_REG + offset/2,
 153                                         DA9052_BUCK_ILIM_MASK_ODD,
 154                                         reg_val << 6);
 155}
 156
 157static int da9052_list_voltage(struct regulator_dev *rdev,
 158                                unsigned int selector)
 159{
 160        struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
 161        struct da9052_regulator_info *info = regulator->info;
 162        int id = rdev_get_id(rdev);
 163        int volt_uV;
 164
 165        if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
 166                && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
 167                volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
 168                          + info->min_uV);
 169                volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
 170                                    * (DA9052_BUCK_PERI_3uV_STEP);
 171        } else {
 172                volt_uV = (selector * info->step_uV) + info->min_uV;
 173        }
 174
 175        if (volt_uV > info->max_uV)
 176                return -EINVAL;
 177
 178        return volt_uV;
 179}
 180
 181static int da9052_map_voltage(struct regulator_dev *rdev,
 182                              int min_uV, int max_uV)
 183{
 184        struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
 185        struct da9052_regulator_info *info = regulator->info;
 186        int id = rdev_get_id(rdev);
 187        int ret, sel;
 188
 189        ret = verify_range(info, min_uV, max_uV);
 190        if (ret < 0)
 191                return ret;
 192
 193        if (min_uV < info->min_uV)
 194                min_uV = info->min_uV;
 195
 196        if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
 197                && (min_uV >= DA9052_CONST_3uV)) {
 198                        sel = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
 199                              DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
 200                                           DA9052_BUCK_PERI_3uV_STEP);
 201        } else {
 202                sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
 203        }
 204
 205        ret = da9052_list_voltage(rdev, sel);
 206        if (ret < 0)
 207                return ret;
 208
 209        return sel;
 210}
 211
 212static struct regulator_ops da9052_dcdc_ops = {
 213        .get_current_limit = da9052_dcdc_get_current_limit,
 214        .set_current_limit = da9052_dcdc_set_current_limit,
 215
 216        .list_voltage = da9052_list_voltage,
 217        .map_voltage = da9052_map_voltage,
 218        .get_voltage_sel = regulator_get_voltage_sel_regmap,
 219        .set_voltage_sel = regulator_set_voltage_sel_regmap,
 220        .is_enabled = regulator_is_enabled_regmap,
 221        .enable = regulator_enable_regmap,
 222        .disable = regulator_disable_regmap,
 223};
 224
 225static struct regulator_ops da9052_ldo_ops = {
 226        .list_voltage = da9052_list_voltage,
 227        .map_voltage = da9052_map_voltage,
 228        .get_voltage_sel = regulator_get_voltage_sel_regmap,
 229        .set_voltage_sel = regulator_set_voltage_sel_regmap,
 230        .is_enabled = regulator_is_enabled_regmap,
 231        .enable = regulator_enable_regmap,
 232        .disable = regulator_disable_regmap,
 233};
 234
 235#define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
 236{\
 237        .reg_desc = {\
 238                .name = #_id,\
 239                .ops = &da9052_ldo_ops,\
 240                .type = REGULATOR_VOLTAGE,\
 241                .id = DA9052_ID_##_id,\
 242                .n_voltages = (max - min) / step + 1, \
 243                .owner = THIS_MODULE,\
 244                .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
 245                .vsel_mask = (1 << (sbits)) - 1,\
 246                .apply_reg = DA9052_SUPPLY_REG, \
 247                .apply_bit = (abits), \
 248                .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
 249                .enable_mask = 1 << (ebits),\
 250        },\
 251        .min_uV = (min) * 1000,\
 252        .max_uV = (max) * 1000,\
 253        .step_uV = (step) * 1000,\
 254}
 255
 256#define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
 257{\
 258        .reg_desc = {\
 259                .name = #_id,\
 260                .ops = &da9052_dcdc_ops,\
 261                .type = REGULATOR_VOLTAGE,\
 262                .id = DA9052_ID_##_id,\
 263                .n_voltages = (max - min) / step + 1, \
 264                .owner = THIS_MODULE,\
 265                .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
 266                .vsel_mask = (1 << (sbits)) - 1,\
 267                .apply_reg = DA9052_SUPPLY_REG, \
 268                .apply_bit = (abits), \
 269                .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
 270                .enable_mask = 1 << (ebits),\
 271        },\
 272        .min_uV = (min) * 1000,\
 273        .max_uV = (max) * 1000,\
 274        .step_uV = (step) * 1000,\
 275}
 276
 277static struct da9052_regulator_info da9052_regulator_info[] = {
 278        DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
 279        DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
 280        DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
 281        DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
 282        DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
 283        DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
 284        DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
 285        DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
 286        DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
 287        DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
 288        DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
 289        DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
 290        DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
 291        DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
 292};
 293
 294static struct da9052_regulator_info da9053_regulator_info[] = {
 295        DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
 296        DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
 297        DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
 298        DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
 299        DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
 300        DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
 301        DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
 302        DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
 303        DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
 304        DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
 305        DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
 306        DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
 307        DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
 308        DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
 309};
 310
 311static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
 312                                                                 int id)
 313{
 314        struct da9052_regulator_info *info;
 315        int i;
 316
 317        switch (chip_id) {
 318        case DA9052:
 319                for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
 320                        info = &da9052_regulator_info[i];
 321                        if (info->reg_desc.id == id)
 322                                return info;
 323                }
 324                break;
 325        case DA9053_AA:
 326        case DA9053_BA:
 327        case DA9053_BB:
 328                for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
 329                        info = &da9053_regulator_info[i];
 330                        if (info->reg_desc.id == id)
 331                                return info;
 332                }
 333                break;
 334        }
 335
 336        return NULL;
 337}
 338
 339static int da9052_regulator_probe(struct platform_device *pdev)
 340{
 341        struct regulator_config config = { };
 342        struct da9052_regulator *regulator;
 343        struct da9052 *da9052;
 344        struct da9052_pdata *pdata;
 345
 346        regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
 347                                 GFP_KERNEL);
 348        if (!regulator)
 349                return -ENOMEM;
 350
 351        da9052 = dev_get_drvdata(pdev->dev.parent);
 352        pdata = da9052->dev->platform_data;
 353        regulator->da9052 = da9052;
 354
 355        regulator->info = find_regulator_info(regulator->da9052->chip_id,
 356                                              pdev->id);
 357        if (regulator->info == NULL) {
 358                dev_err(&pdev->dev, "invalid regulator ID specified\n");
 359                return -EINVAL;
 360        }
 361
 362        config.dev = &pdev->dev;
 363        config.driver_data = regulator;
 364        config.regmap = da9052->regmap;
 365        if (pdata && pdata->regulators) {
 366                config.init_data = pdata->regulators[pdev->id];
 367        } else {
 368#ifdef CONFIG_OF
 369                struct device_node *nproot, *np;
 370
 371                nproot = of_node_get(da9052->dev->of_node);
 372                if (!nproot)
 373                        return -ENODEV;
 374
 375                nproot = of_find_node_by_name(nproot, "regulators");
 376                if (!nproot)
 377                        return -ENODEV;
 378
 379                for_each_child_of_node(nproot, np) {
 380                        if (!of_node_cmp(np->name,
 381                                         regulator->info->reg_desc.name)) {
 382                                config.init_data = of_get_regulator_init_data(
 383                                        &pdev->dev, np);
 384                                config.of_node = np;
 385                                break;
 386                        }
 387                }
 388                of_node_put(nproot);
 389#endif
 390        }
 391
 392        regulator->rdev = regulator_register(&regulator->info->reg_desc,
 393                                             &config);
 394        if (IS_ERR(regulator->rdev)) {
 395                dev_err(&pdev->dev, "failed to register regulator %s\n",
 396                        regulator->info->reg_desc.name);
 397                return PTR_ERR(regulator->rdev);
 398        }
 399
 400        platform_set_drvdata(pdev, regulator);
 401
 402        return 0;
 403}
 404
 405static int da9052_regulator_remove(struct platform_device *pdev)
 406{
 407        struct da9052_regulator *regulator = platform_get_drvdata(pdev);
 408
 409        regulator_unregister(regulator->rdev);
 410        return 0;
 411}
 412
 413static struct platform_driver da9052_regulator_driver = {
 414        .probe = da9052_regulator_probe,
 415        .remove = da9052_regulator_remove,
 416        .driver = {
 417                .name = "da9052-regulator",
 418                .owner = THIS_MODULE,
 419        },
 420};
 421
 422static int __init da9052_regulator_init(void)
 423{
 424        return platform_driver_register(&da9052_regulator_driver);
 425}
 426subsys_initcall(da9052_regulator_init);
 427
 428static void __exit da9052_regulator_exit(void)
 429{
 430        platform_driver_unregister(&da9052_regulator_driver);
 431}
 432module_exit(da9052_regulator_exit);
 433
 434MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
 435MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
 436MODULE_LICENSE("GPL");
 437MODULE_ALIAS("platform:da9052-regulator");
 438
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.