linux/drivers/regulator/max77686.c
<<
>>
Prefs
   1/*
   2 * max77686.c - Regulator driver for the Maxim 77686
   3 *
   4 * Copyright (C) 2012 Samsung Electronics
   5 * Chiwoong Byun <woong.byun@smasung.com>
   6 * Jonghwa Lee <jonghwa3.lee@samsung.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 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21 *
  22 * This driver is based on max8997.c
  23 */
  24
  25#include <linux/kernel.h>
  26#include <linux/bug.h>
  27#include <linux/delay.h>
  28#include <linux/err.h>
  29#include <linux/gpio.h>
  30#include <linux/slab.h>
  31#include <linux/platform_device.h>
  32#include <linux/regulator/driver.h>
  33#include <linux/regulator/machine.h>
  34#include <linux/regulator/of_regulator.h>
  35#include <linux/mfd/max77686.h>
  36#include <linux/mfd/max77686-private.h>
  37
  38#define MAX77686_LDO_MINUV      800000
  39#define MAX77686_LDO_UVSTEP     50000
  40#define MAX77686_LDO_LOW_MINUV  800000
  41#define MAX77686_LDO_LOW_UVSTEP 25000
  42#define MAX77686_BUCK_MINUV     750000
  43#define MAX77686_BUCK_UVSTEP    50000
  44#define MAX77686_RAMP_DELAY     100000                  /* uV/us */
  45#define MAX77686_DVS_RAMP_DELAY 27500                   /* uV/us */
  46#define MAX77686_DVS_MINUV      600000
  47#define MAX77686_DVS_UVSTEP     12500
  48
  49#define MAX77686_OPMODE_SHIFT   6
  50#define MAX77686_OPMODE_BUCK234_SHIFT   4
  51#define MAX77686_OPMODE_MASK    0x3
  52
  53#define MAX77686_VSEL_MASK      0x3F
  54#define MAX77686_DVS_VSEL_MASK  0xFF
  55
  56#define MAX77686_RAMP_RATE_MASK 0xC0
  57
  58#define MAX77686_REGULATORS     MAX77686_REG_MAX
  59#define MAX77686_LDOS           26
  60
  61enum max77686_ramp_rate {
  62        RAMP_RATE_13P75MV,
  63        RAMP_RATE_27P5MV,
  64        RAMP_RATE_55MV,
  65        RAMP_RATE_NO_CTRL,      /* 100mV/us */
  66};
  67
  68struct max77686_data {
  69        struct regulator_dev **rdev;
  70};
  71
  72static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  73{
  74        unsigned int ramp_value = RAMP_RATE_NO_CTRL;
  75
  76        switch (ramp_delay) {
  77        case 1 ... 13750:
  78                ramp_value = RAMP_RATE_13P75MV;
  79                break;
  80        case 13751 ... 27500:
  81                ramp_value = RAMP_RATE_27P5MV;
  82                break;
  83        case 27501 ... 55000:
  84                ramp_value = RAMP_RATE_55MV;
  85                break;
  86        case 55001 ... 100000:
  87                break;
  88        default:
  89                pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
  90                        rdev->desc->name, ramp_delay);
  91        }
  92
  93        return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  94                                  MAX77686_RAMP_RATE_MASK, ramp_value << 6);
  95}
  96
  97static struct regulator_ops max77686_ops = {
  98        .list_voltage           = regulator_list_voltage_linear,
  99        .map_voltage            = regulator_map_voltage_linear,
 100        .is_enabled             = regulator_is_enabled_regmap,
 101        .enable                 = regulator_enable_regmap,
 102        .disable                = regulator_disable_regmap,
 103        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 104        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 105        .set_voltage_time_sel   = regulator_set_voltage_time_sel,
 106};
 107
 108static struct regulator_ops max77686_buck_dvs_ops = {
 109        .list_voltage           = regulator_list_voltage_linear,
 110        .map_voltage            = regulator_map_voltage_linear,
 111        .is_enabled             = regulator_is_enabled_regmap,
 112        .enable                 = regulator_enable_regmap,
 113        .disable                = regulator_disable_regmap,
 114        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 115        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 116        .set_voltage_time_sel   = regulator_set_voltage_time_sel,
 117        .set_ramp_delay         = max77686_set_ramp_delay,
 118};
 119
 120#define regulator_desc_ldo(num)         {                               \
 121        .name           = "LDO"#num,                                    \
 122        .id             = MAX77686_LDO##num,                            \
 123        .ops            = &max77686_ops,                                \
 124        .type           = REGULATOR_VOLTAGE,                            \
 125        .owner          = THIS_MODULE,                                  \
 126        .min_uV         = MAX77686_LDO_MINUV,                           \
 127        .uV_step        = MAX77686_LDO_UVSTEP,                          \
 128        .ramp_delay     = MAX77686_RAMP_DELAY,                          \
 129        .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
 130        .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
 131        .vsel_mask      = MAX77686_VSEL_MASK,                           \
 132        .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
 133        .enable_mask    = MAX77686_OPMODE_MASK                          \
 134                        << MAX77686_OPMODE_SHIFT,                       \
 135}
 136#define regulator_desc_ldo_low(num)             {                       \
 137        .name           = "LDO"#num,                                    \
 138        .id             = MAX77686_LDO##num,                            \
 139        .ops            = &max77686_ops,                                \
 140        .type           = REGULATOR_VOLTAGE,                            \
 141        .owner          = THIS_MODULE,                                  \
 142        .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
 143        .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
 144        .ramp_delay     = MAX77686_RAMP_DELAY,                          \
 145        .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
 146        .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
 147        .vsel_mask      = MAX77686_VSEL_MASK,                           \
 148        .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
 149        .enable_mask    = MAX77686_OPMODE_MASK                          \
 150                        << MAX77686_OPMODE_SHIFT,                       \
 151}
 152#define regulator_desc_buck(num)                {                       \
 153        .name           = "BUCK"#num,                                   \
 154        .id             = MAX77686_BUCK##num,                           \
 155        .ops            = &max77686_ops,                                \
 156        .type           = REGULATOR_VOLTAGE,                            \
 157        .owner          = THIS_MODULE,                                  \
 158        .min_uV         = MAX77686_BUCK_MINUV,                          \
 159        .uV_step        = MAX77686_BUCK_UVSTEP,                         \
 160        .ramp_delay     = MAX77686_RAMP_DELAY,                          \
 161        .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
 162        .vsel_reg       = MAX77686_REG_BUCK5OUT + (num - 5) * 2,        \
 163        .vsel_mask      = MAX77686_VSEL_MASK,                           \
 164        .enable_reg     = MAX77686_REG_BUCK5CTRL + (num - 5) * 2,       \
 165        .enable_mask    = MAX77686_OPMODE_MASK,                         \
 166}
 167#define regulator_desc_buck1(num)               {                       \
 168        .name           = "BUCK"#num,                                   \
 169        .id             = MAX77686_BUCK##num,                           \
 170        .ops            = &max77686_ops,                                \
 171        .type           = REGULATOR_VOLTAGE,                            \
 172        .owner          = THIS_MODULE,                                  \
 173        .min_uV         = MAX77686_BUCK_MINUV,                          \
 174        .uV_step        = MAX77686_BUCK_UVSTEP,                         \
 175        .ramp_delay     = MAX77686_RAMP_DELAY,                          \
 176        .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
 177        .vsel_reg       = MAX77686_REG_BUCK1OUT,                        \
 178        .vsel_mask      = MAX77686_VSEL_MASK,                           \
 179        .enable_reg     = MAX77686_REG_BUCK1CTRL,                       \
 180        .enable_mask    = MAX77686_OPMODE_MASK,                         \
 181}
 182#define regulator_desc_buck_dvs(num)            {                       \
 183        .name           = "BUCK"#num,                                   \
 184        .id             = MAX77686_BUCK##num,                           \
 185        .ops            = &max77686_buck_dvs_ops,                       \
 186        .type           = REGULATOR_VOLTAGE,                            \
 187        .owner          = THIS_MODULE,                                  \
 188        .min_uV         = MAX77686_DVS_MINUV,                           \
 189        .uV_step        = MAX77686_DVS_UVSTEP,                          \
 190        .ramp_delay     = MAX77686_DVS_RAMP_DELAY,                      \
 191        .n_voltages     = MAX77686_DVS_VSEL_MASK + 1,                   \
 192        .vsel_reg       = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10,      \
 193        .vsel_mask      = MAX77686_DVS_VSEL_MASK,                       \
 194        .enable_reg     = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10,     \
 195        .enable_mask    = MAX77686_OPMODE_MASK                          \
 196                        << MAX77686_OPMODE_BUCK234_SHIFT,               \
 197}
 198
 199static struct regulator_desc regulators[] = {
 200        regulator_desc_ldo_low(1),
 201        regulator_desc_ldo_low(2),
 202        regulator_desc_ldo(3),
 203        regulator_desc_ldo(4),
 204        regulator_desc_ldo(5),
 205        regulator_desc_ldo_low(6),
 206        regulator_desc_ldo_low(7),
 207        regulator_desc_ldo_low(8),
 208        regulator_desc_ldo(9),
 209        regulator_desc_ldo(10),
 210        regulator_desc_ldo(11),
 211        regulator_desc_ldo(12),
 212        regulator_desc_ldo(13),
 213        regulator_desc_ldo(14),
 214        regulator_desc_ldo_low(15),
 215        regulator_desc_ldo(16),
 216        regulator_desc_ldo(17),
 217        regulator_desc_ldo(18),
 218        regulator_desc_ldo(19),
 219        regulator_desc_ldo(20),
 220        regulator_desc_ldo(21),
 221        regulator_desc_ldo(22),
 222        regulator_desc_ldo(23),
 223        regulator_desc_ldo(24),
 224        regulator_desc_ldo(25),
 225        regulator_desc_ldo(26),
 226        regulator_desc_buck1(1),
 227        regulator_desc_buck_dvs(2),
 228        regulator_desc_buck_dvs(3),
 229        regulator_desc_buck_dvs(4),
 230        regulator_desc_buck(5),
 231        regulator_desc_buck(6),
 232        regulator_desc_buck(7),
 233        regulator_desc_buck(8),
 234        regulator_desc_buck(9),
 235};
 236
 237#ifdef CONFIG_OF
 238static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
 239                                        struct max77686_platform_data *pdata)
 240{
 241        struct device_node *pmic_np, *regulators_np;
 242        struct max77686_regulator_data *rdata;
 243        struct of_regulator_match rmatch;
 244        unsigned int i;
 245
 246        pmic_np = iodev->dev->of_node;
 247        regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
 248        if (!regulators_np) {
 249                dev_err(iodev->dev, "could not find regulators sub-node\n");
 250                return -EINVAL;
 251        }
 252
 253        pdata->num_regulators = ARRAY_SIZE(regulators);
 254        rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
 255                             pdata->num_regulators, GFP_KERNEL);
 256        if (!rdata) {
 257                dev_err(iodev->dev,
 258                        "could not allocate memory for regulator data\n");
 259                return -ENOMEM;
 260        }
 261
 262        for (i = 0; i < pdata->num_regulators; i++) {
 263                rmatch.name = regulators[i].name;
 264                rmatch.init_data = NULL;
 265                rmatch.of_node = NULL;
 266                of_regulator_match(iodev->dev, regulators_np, &rmatch, 1);
 267                rdata[i].initdata = rmatch.init_data;
 268        }
 269
 270        pdata->regulators = rdata;
 271
 272        return 0;
 273}
 274#else
 275static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
 276                                        struct max77686_platform_data *pdata)
 277{
 278        return 0;
 279}
 280#endif /* CONFIG_OF */
 281
 282static __devinit int max77686_pmic_probe(struct platform_device *pdev)
 283{
 284        struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 285        struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
 286        struct regulator_dev **rdev;
 287        struct max77686_data *max77686;
 288        int i,  size;
 289        int ret = 0;
 290        struct regulator_config config = { };
 291
 292        dev_dbg(&pdev->dev, "%s\n", __func__);
 293
 294        if (!pdata) {
 295                dev_err(&pdev->dev, "no platform data found for regulator\n");
 296                return -ENODEV;
 297        }
 298
 299        if (iodev->dev->of_node) {
 300                ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
 301                if (ret)
 302                        return ret;
 303        }
 304
 305        if (pdata->num_regulators != MAX77686_REGULATORS) {
 306                dev_err(&pdev->dev,
 307                        "Invalid initial data for regulator's initialiation\n");
 308                return -EINVAL;
 309        }
 310
 311        max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
 312                                GFP_KERNEL);
 313        if (!max77686)
 314                return -ENOMEM;
 315
 316        size = sizeof(struct regulator_dev *) * MAX77686_REGULATORS;
 317        max77686->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
 318        if (!max77686->rdev)
 319                return -ENOMEM;
 320
 321        rdev = max77686->rdev;
 322        config.dev = &pdev->dev;
 323        config.regmap = iodev->regmap;
 324        platform_set_drvdata(pdev, max77686);
 325
 326        for (i = 0; i < MAX77686_REGULATORS; i++) {
 327                config.init_data = pdata->regulators[i].initdata;
 328
 329                rdev[i] = regulator_register(&regulators[i], &config);
 330                if (IS_ERR(rdev[i])) {
 331                        ret = PTR_ERR(rdev[i]);
 332                        dev_err(&pdev->dev,
 333                                "regulator init failed for %d\n", i);
 334                                rdev[i] = NULL;
 335                                goto err;
 336                }
 337        }
 338
 339        return 0;
 340err:
 341        while (--i >= 0)
 342                regulator_unregister(rdev[i]);
 343        return ret;
 344}
 345
 346static int __devexit max77686_pmic_remove(struct platform_device *pdev)
 347{
 348        struct max77686_data *max77686 = platform_get_drvdata(pdev);
 349        struct regulator_dev **rdev = max77686->rdev;
 350        int i;
 351
 352        for (i = 0; i < MAX77686_REGULATORS; i++)
 353                if (rdev[i])
 354                        regulator_unregister(rdev[i]);
 355
 356        return 0;
 357}
 358
 359static const struct platform_device_id max77686_pmic_id[] = {
 360        {"max77686-pmic", 0},
 361        { },
 362};
 363MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
 364
 365static struct platform_driver max77686_pmic_driver = {
 366        .driver = {
 367                .name = "max77686-pmic",
 368                .owner = THIS_MODULE,
 369        },
 370        .probe = max77686_pmic_probe,
 371        .remove = __devexit_p(max77686_pmic_remove),
 372        .id_table = max77686_pmic_id,
 373};
 374
 375static int __init max77686_pmic_init(void)
 376{
 377        return platform_driver_register(&max77686_pmic_driver);
 378}
 379subsys_initcall(max77686_pmic_init);
 380
 381static void __exit max77686_pmic_cleanup(void)
 382{
 383        platform_driver_unregister(&max77686_pmic_driver);
 384}
 385module_exit(max77686_pmic_cleanup);
 386
 387MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
 388MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
 389MODULE_LICENSE("GPL");
 390
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.