linux/drivers/regulator/palmas-regulator.c
<<
>>
Prefs
   1/*
   2 * Driver for Regulator part of Palmas PMIC Chips
   3 *
   4 * Copyright 2011-2012 Texas Instruments Inc.
   5 *
   6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
   7 * Author: Ian Lartey <ian@slimlogic.co.uk>
   8 *
   9 *  This program is free software; you can redistribute it and/or modify it
  10 *  under  the terms of the GNU General  Public License as published by the
  11 *  Free Software Foundation;  either version 2 of the License, or (at your
  12 *  option) any later version.
  13 *
  14 */
  15
  16#include <linux/kernel.h>
  17#include <linux/module.h>
  18#include <linux/init.h>
  19#include <linux/err.h>
  20#include <linux/platform_device.h>
  21#include <linux/regulator/driver.h>
  22#include <linux/regulator/machine.h>
  23#include <linux/slab.h>
  24#include <linux/regmap.h>
  25#include <linux/mfd/palmas.h>
  26#include <linux/of.h>
  27#include <linux/of_platform.h>
  28#include <linux/regulator/of_regulator.h>
  29
  30struct regs_info {
  31        char    *name;
  32        u8      vsel_addr;
  33        u8      ctrl_addr;
  34        u8      tstep_addr;
  35};
  36
  37static const struct regs_info palmas_regs_info[] = {
  38        {
  39                .name           = "SMPS12",
  40                .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
  41                .ctrl_addr      = PALMAS_SMPS12_CTRL,
  42                .tstep_addr     = PALMAS_SMPS12_TSTEP,
  43        },
  44        {
  45                .name           = "SMPS123",
  46                .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
  47                .ctrl_addr      = PALMAS_SMPS12_CTRL,
  48                .tstep_addr     = PALMAS_SMPS12_TSTEP,
  49        },
  50        {
  51                .name           = "SMPS3",
  52                .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
  53                .ctrl_addr      = PALMAS_SMPS3_CTRL,
  54        },
  55        {
  56                .name           = "SMPS45",
  57                .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
  58                .ctrl_addr      = PALMAS_SMPS45_CTRL,
  59                .tstep_addr     = PALMAS_SMPS45_TSTEP,
  60        },
  61        {
  62                .name           = "SMPS457",
  63                .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
  64                .ctrl_addr      = PALMAS_SMPS45_CTRL,
  65                .tstep_addr     = PALMAS_SMPS45_TSTEP,
  66        },
  67        {
  68                .name           = "SMPS6",
  69                .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
  70                .ctrl_addr      = PALMAS_SMPS6_CTRL,
  71                .tstep_addr     = PALMAS_SMPS6_TSTEP,
  72        },
  73        {
  74                .name           = "SMPS7",
  75                .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
  76                .ctrl_addr      = PALMAS_SMPS7_CTRL,
  77        },
  78        {
  79                .name           = "SMPS8",
  80                .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
  81                .ctrl_addr      = PALMAS_SMPS8_CTRL,
  82                .tstep_addr     = PALMAS_SMPS8_TSTEP,
  83        },
  84        {
  85                .name           = "SMPS9",
  86                .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
  87                .ctrl_addr      = PALMAS_SMPS9_CTRL,
  88        },
  89        {
  90                .name           = "SMPS10",
  91        },
  92        {
  93                .name           = "LDO1",
  94                .vsel_addr      = PALMAS_LDO1_VOLTAGE,
  95                .ctrl_addr      = PALMAS_LDO1_CTRL,
  96        },
  97        {
  98                .name           = "LDO2",
  99                .vsel_addr      = PALMAS_LDO2_VOLTAGE,
 100                .ctrl_addr      = PALMAS_LDO2_CTRL,
 101        },
 102        {
 103                .name           = "LDO3",
 104                .vsel_addr      = PALMAS_LDO3_VOLTAGE,
 105                .ctrl_addr      = PALMAS_LDO3_CTRL,
 106        },
 107        {
 108                .name           = "LDO4",
 109                .vsel_addr      = PALMAS_LDO4_VOLTAGE,
 110                .ctrl_addr      = PALMAS_LDO4_CTRL,
 111        },
 112        {
 113                .name           = "LDO5",
 114                .vsel_addr      = PALMAS_LDO5_VOLTAGE,
 115                .ctrl_addr      = PALMAS_LDO5_CTRL,
 116        },
 117        {
 118                .name           = "LDO6",
 119                .vsel_addr      = PALMAS_LDO6_VOLTAGE,
 120                .ctrl_addr      = PALMAS_LDO6_CTRL,
 121        },
 122        {
 123                .name           = "LDO7",
 124                .vsel_addr      = PALMAS_LDO7_VOLTAGE,
 125                .ctrl_addr      = PALMAS_LDO7_CTRL,
 126        },
 127        {
 128                .name           = "LDO8",
 129                .vsel_addr      = PALMAS_LDO8_VOLTAGE,
 130                .ctrl_addr      = PALMAS_LDO8_CTRL,
 131        },
 132        {
 133                .name           = "LDO9",
 134                .vsel_addr      = PALMAS_LDO9_VOLTAGE,
 135                .ctrl_addr      = PALMAS_LDO9_CTRL,
 136        },
 137        {
 138                .name           = "LDOLN",
 139                .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
 140                .ctrl_addr      = PALMAS_LDOLN_CTRL,
 141        },
 142        {
 143                .name           = "LDOUSB",
 144                .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
 145                .ctrl_addr      = PALMAS_LDOUSB_CTRL,
 146        },
 147};
 148
 149#define SMPS_CTRL_MODE_OFF              0x00
 150#define SMPS_CTRL_MODE_ON               0x01
 151#define SMPS_CTRL_MODE_ECO              0x02
 152#define SMPS_CTRL_MODE_PWM              0x03
 153
 154/* These values are derived from the data sheet. And are the number of steps
 155 * where there is a voltage change, the ranges at beginning and end of register
 156 * max/min values where there are no change are ommitted.
 157 *
 158 * So they are basically (maxV-minV)/stepV
 159 */
 160#define PALMAS_SMPS_NUM_VOLTAGES        117
 161#define PALMAS_SMPS10_NUM_VOLTAGES      2
 162#define PALMAS_LDO_NUM_VOLTAGES         50
 163
 164#define SMPS10_VSEL                     (1<<3)
 165#define SMPS10_BOOST_EN                 (1<<2)
 166#define SMPS10_BYPASS_EN                (1<<1)
 167#define SMPS10_SWITCH_EN                (1<<0)
 168
 169#define REGULATOR_SLAVE                 0
 170
 171static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
 172                unsigned int *dest)
 173{
 174        unsigned int addr;
 175
 176        addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
 177
 178        return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
 179}
 180
 181static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
 182                unsigned int value)
 183{
 184        unsigned int addr;
 185
 186        addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
 187
 188        return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
 189}
 190
 191static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
 192                unsigned int *dest)
 193{
 194        unsigned int addr;
 195
 196        addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
 197
 198        return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
 199}
 200
 201static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
 202                unsigned int value)
 203{
 204        unsigned int addr;
 205
 206        addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
 207
 208        return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
 209}
 210
 211static int palmas_is_enabled_smps(struct regulator_dev *dev)
 212{
 213        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 214        int id = rdev_get_id(dev);
 215        unsigned int reg;
 216
 217        palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 218
 219        reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
 220        reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
 221
 222        return !!(reg);
 223}
 224
 225static int palmas_enable_smps(struct regulator_dev *dev)
 226{
 227        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 228        int id = rdev_get_id(dev);
 229        unsigned int reg;
 230
 231        palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 232
 233        reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
 234        reg |= SMPS_CTRL_MODE_ON;
 235
 236        palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
 237
 238        return 0;
 239}
 240
 241static int palmas_disable_smps(struct regulator_dev *dev)
 242{
 243        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 244        int id = rdev_get_id(dev);
 245        unsigned int reg;
 246
 247        palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 248
 249        reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
 250
 251        palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
 252
 253        return 0;
 254}
 255
 256
 257static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
 258{
 259        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 260        int id = rdev_get_id(dev);
 261        unsigned int reg;
 262
 263        palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 264        reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
 265
 266        switch (mode) {
 267        case REGULATOR_MODE_NORMAL:
 268                reg |= SMPS_CTRL_MODE_ON;
 269                break;
 270        case REGULATOR_MODE_IDLE:
 271                reg |= SMPS_CTRL_MODE_ECO;
 272                break;
 273        case REGULATOR_MODE_FAST:
 274                reg |= SMPS_CTRL_MODE_PWM;
 275                break;
 276        default:
 277                return -EINVAL;
 278        }
 279        palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
 280
 281        return 0;
 282}
 283
 284static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
 285{
 286        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 287        int id = rdev_get_id(dev);
 288        unsigned int reg;
 289
 290        palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 291        reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
 292        reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
 293
 294        switch (reg) {
 295        case SMPS_CTRL_MODE_ON:
 296                return REGULATOR_MODE_NORMAL;
 297        case SMPS_CTRL_MODE_ECO:
 298                return REGULATOR_MODE_IDLE;
 299        case SMPS_CTRL_MODE_PWM:
 300                return REGULATOR_MODE_FAST;
 301        }
 302
 303        return 0;
 304}
 305
 306static int palmas_list_voltage_smps(struct regulator_dev *dev,
 307                                        unsigned selector)
 308{
 309        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 310        int id = rdev_get_id(dev);
 311        int mult = 1;
 312
 313        /* Read the multiplier set in VSEL register to return
 314         * the correct voltage.
 315         */
 316        if (pmic->range[id])
 317                mult = 2;
 318
 319        if (selector == 0)
 320                return 0;
 321        else if (selector < 6)
 322                return 500000 * mult;
 323        else
 324                /* Voltage is linear mapping starting from selector 6,
 325                 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
 326                 * RANGE is either x1 or x2
 327                 */
 328                return (490000 + ((selector - 5) * 10000)) * mult;
 329}
 330
 331static int palmas_map_voltage_smps(struct regulator_dev *rdev,
 332                int min_uV, int max_uV)
 333{
 334        struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
 335        int id = rdev_get_id(rdev);
 336        int ret, voltage;
 337
 338        if (min_uV == 0)
 339                return 0;
 340
 341        if (pmic->range[id]) { /* RANGE is x2 */
 342                if (min_uV < 1000000)
 343                        min_uV = 1000000;
 344                ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
 345        } else {                /* RANGE is x1 */
 346                if (min_uV < 500000)
 347                        min_uV = 500000;
 348                ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
 349        }
 350
 351        /* Map back into a voltage to verify we're still in bounds */
 352        voltage = palmas_list_voltage_smps(rdev, ret);
 353        if (voltage < min_uV || voltage > max_uV)
 354                return -EINVAL;
 355
 356        return ret;
 357}
 358
 359static struct regulator_ops palmas_ops_smps = {
 360        .is_enabled             = palmas_is_enabled_smps,
 361        .enable                 = palmas_enable_smps,
 362        .disable                = palmas_disable_smps,
 363        .set_mode               = palmas_set_mode_smps,
 364        .get_mode               = palmas_get_mode_smps,
 365        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 366        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 367        .list_voltage           = palmas_list_voltage_smps,
 368        .map_voltage            = palmas_map_voltage_smps,
 369};
 370
 371static struct regulator_ops palmas_ops_smps10 = {
 372        .is_enabled             = regulator_is_enabled_regmap,
 373        .enable                 = regulator_enable_regmap,
 374        .disable                = regulator_disable_regmap,
 375        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 376        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 377        .list_voltage           = regulator_list_voltage_linear,
 378        .map_voltage            = regulator_map_voltage_linear,
 379};
 380
 381static int palmas_is_enabled_ldo(struct regulator_dev *dev)
 382{
 383        struct palmas_pmic *pmic = rdev_get_drvdata(dev);
 384        int id = rdev_get_id(dev);
 385        unsigned int reg;
 386
 387        palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
 388
 389        reg &= PALMAS_LDO1_CTRL_STATUS;
 390
 391        return !!(reg);
 392}
 393
 394static struct regulator_ops palmas_ops_ldo = {
 395        .is_enabled             = palmas_is_enabled_ldo,
 396        .enable                 = regulator_enable_regmap,
 397        .disable                = regulator_disable_regmap,
 398        .get_voltage_sel        = regulator_get_voltage_sel_regmap,
 399        .set_voltage_sel        = regulator_set_voltage_sel_regmap,
 400        .list_voltage           = regulator_list_voltage_linear,
 401        .map_voltage            = regulator_map_voltage_linear,
 402};
 403
 404/*
 405 * setup the hardware based sleep configuration of the SMPS/LDO regulators
 406 * from the platform data. This is different to the software based control
 407 * supported by the regulator framework as it is controlled by toggling
 408 * pins on the PMIC such as PREQ, SYSEN, ...
 409 */
 410static int palmas_smps_init(struct palmas *palmas, int id,
 411                struct palmas_reg_init *reg_init)
 412{
 413        unsigned int reg;
 414        unsigned int addr;
 415        int ret;
 416
 417        addr = palmas_regs_info[id].ctrl_addr;
 418
 419        ret = palmas_smps_read(palmas, addr, &reg);
 420        if (ret)
 421                return ret;
 422
 423        switch (id) {
 424        case PALMAS_REG_SMPS10:
 425                if (reg_init->mode_sleep) {
 426                        reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
 427                        reg |= reg_init->mode_sleep <<
 428                                        PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
 429                }
 430                break;
 431        default:
 432                if (reg_init->warm_reset)
 433                        reg |= PALMAS_SMPS12_CTRL_WR_S;
 434
 435                if (reg_init->roof_floor)
 436                        reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
 437
 438                if (reg_init->mode_sleep) {
 439                        reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
 440                        reg |= reg_init->mode_sleep <<
 441                                        PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
 442                }
 443        }
 444
 445        ret = palmas_smps_write(palmas, addr, reg);
 446        if (ret)
 447                return ret;
 448
 449        if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
 450                addr = palmas_regs_info[id].tstep_addr;
 451
 452                reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
 453
 454                ret = palmas_smps_write(palmas, addr, reg);
 455                if (ret)
 456                        return ret;
 457        }
 458
 459        if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
 460                addr = palmas_regs_info[id].vsel_addr;
 461
 462                reg = reg_init->vsel;
 463
 464                ret = palmas_smps_write(palmas, addr, reg);
 465                if (ret)
 466                        return ret;
 467        }
 468
 469
 470        return 0;
 471}
 472
 473static int palmas_ldo_init(struct palmas *palmas, int id,
 474                struct palmas_reg_init *reg_init)
 475{
 476        unsigned int reg;
 477        unsigned int addr;
 478        int ret;
 479
 480        addr = palmas_regs_info[id].ctrl_addr;
 481
 482        ret = palmas_ldo_read(palmas, addr, &reg);
 483        if (ret)
 484                return ret;
 485
 486        if (reg_init->warm_reset)
 487                reg |= PALMAS_LDO1_CTRL_WR_S;
 488
 489        if (reg_init->mode_sleep)
 490                reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
 491
 492        ret = palmas_ldo_write(palmas, addr, reg);
 493        if (ret)
 494                return ret;
 495
 496        return 0;
 497}
 498
 499static struct of_regulator_match palmas_matches[] = {
 500        { .name = "smps12", },
 501        { .name = "smps123", },
 502        { .name = "smps3", },
 503        { .name = "smps45", },
 504        { .name = "smps457", },
 505        { .name = "smps6", },
 506        { .name = "smps7", },
 507        { .name = "smps8", },
 508        { .name = "smps9", },
 509        { .name = "smps10", },
 510        { .name = "ldo1", },
 511        { .name = "ldo2", },
 512        { .name = "ldo3", },
 513        { .name = "ldo4", },
 514        { .name = "ldo5", },
 515        { .name = "ldo6", },
 516        { .name = "ldo7", },
 517        { .name = "ldo8", },
 518        { .name = "ldo9", },
 519        { .name = "ldoln", },
 520        { .name = "ldousb", },
 521};
 522
 523static void palmas_dt_to_pdata(struct device *dev,
 524                struct device_node *node,
 525                struct palmas_pmic_platform_data *pdata)
 526{
 527        struct device_node *regulators;
 528        u32 prop;
 529        int idx, ret;
 530
 531        node = of_node_get(node);
 532        regulators = of_find_node_by_name(node, "regulators");
 533        if (!regulators) {
 534                dev_info(dev, "regulator node not found\n");
 535                return;
 536        }
 537
 538        ret = of_regulator_match(dev, regulators, palmas_matches,
 539                        PALMAS_NUM_REGS);
 540        of_node_put(regulators);
 541        if (ret < 0) {
 542                dev_err(dev, "Error parsing regulator init data: %d\n", ret);
 543                return;
 544        }
 545
 546        for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
 547                if (!palmas_matches[idx].init_data ||
 548                                !palmas_matches[idx].of_node)
 549                        continue;
 550
 551                pdata->reg_data[idx] = palmas_matches[idx].init_data;
 552
 553                pdata->reg_init[idx] = devm_kzalloc(dev,
 554                                sizeof(struct palmas_reg_init), GFP_KERNEL);
 555
 556                ret = of_property_read_u32(palmas_matches[idx].of_node,
 557                                "ti,warm_reset", &prop);
 558                if (!ret)
 559                        pdata->reg_init[idx]->warm_reset = prop;
 560
 561                ret = of_property_read_u32(palmas_matches[idx].of_node,
 562                                "ti,roof_floor", &prop);
 563                if (!ret)
 564                        pdata->reg_init[idx]->roof_floor = prop;
 565
 566                ret = of_property_read_u32(palmas_matches[idx].of_node,
 567                                "ti,mode_sleep", &prop);
 568                if (!ret)
 569                        pdata->reg_init[idx]->mode_sleep = prop;
 570
 571                ret = of_property_read_u32(palmas_matches[idx].of_node,
 572                                "ti,tstep", &prop);
 573                if (!ret)
 574                        pdata->reg_init[idx]->tstep = prop;
 575
 576                ret = of_property_read_u32(palmas_matches[idx].of_node,
 577                                "ti,vsel", &prop);
 578                if (!ret)
 579                        pdata->reg_init[idx]->vsel = prop;
 580        }
 581
 582        ret = of_property_read_u32(node, "ti,ldo6_vibrator", &prop);
 583        if (!ret)
 584                pdata->ldo6_vibrator = prop;
 585}
 586
 587
 588static int palmas_probe(struct platform_device *pdev)
 589{
 590        struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
 591        struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
 592        struct device_node *node = pdev->dev.of_node;
 593        struct regulator_dev *rdev;
 594        struct regulator_config config = { };
 595        struct palmas_pmic *pmic;
 596        struct palmas_reg_init *reg_init;
 597        int id = 0, ret;
 598        unsigned int addr, reg;
 599
 600        if (node && !pdata) {
 601                pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 602
 603                if (!pdata)
 604                        return -ENOMEM;
 605
 606                palmas_dt_to_pdata(&pdev->dev, node, pdata);
 607        }
 608
 609        pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
 610        if (!pmic)
 611                return -ENOMEM;
 612
 613        pmic->dev = &pdev->dev;
 614        pmic->palmas = palmas;
 615        palmas->pmic = pmic;
 616        platform_set_drvdata(pdev, pmic);
 617
 618        ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
 619        if (ret)
 620                return ret;
 621
 622        if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
 623                pmic->smps123 = 1;
 624
 625        if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
 626                pmic->smps457 = 1;
 627
 628        config.regmap = palmas->regmap[REGULATOR_SLAVE];
 629        config.dev = &pdev->dev;
 630        config.driver_data = pmic;
 631
 632        for (id = 0; id < PALMAS_REG_LDO1; id++) {
 633
 634                /*
 635                 * Miss out regulators which are not available due
 636                 * to slaving configurations.
 637                 */
 638                switch (id) {
 639                case PALMAS_REG_SMPS12:
 640                case PALMAS_REG_SMPS3:
 641                        if (pmic->smps123)
 642                                continue;
 643                        break;
 644                case PALMAS_REG_SMPS123:
 645                        if (!pmic->smps123)
 646                                continue;
 647                        break;
 648                case PALMAS_REG_SMPS45:
 649                case PALMAS_REG_SMPS7:
 650                        if (pmic->smps457)
 651                                continue;
 652                        break;
 653                case PALMAS_REG_SMPS457:
 654                        if (!pmic->smps457)
 655                                continue;
 656                }
 657
 658                /* Initialise sleep/init values from platform data */
 659                if (pdata && pdata->reg_init[id]) {
 660                        reg_init = pdata->reg_init[id];
 661                        ret = palmas_smps_init(palmas, id, reg_init);
 662                        if (ret)
 663                                goto err_unregister_regulator;
 664                }
 665
 666                /* Register the regulators */
 667                pmic->desc[id].name = palmas_regs_info[id].name;
 668                pmic->desc[id].id = id;
 669
 670                switch (id) {
 671                case PALMAS_REG_SMPS10:
 672                        pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
 673                        pmic->desc[id].ops = &palmas_ops_smps10;
 674                        pmic->desc[id].vsel_reg =
 675                                        PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
 676                                                        PALMAS_SMPS10_CTRL);
 677                        pmic->desc[id].vsel_mask = SMPS10_VSEL;
 678                        pmic->desc[id].enable_reg =
 679                                        PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
 680                                                        PALMAS_SMPS10_STATUS);
 681                        pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
 682                        pmic->desc[id].min_uV = 3750000;
 683                        pmic->desc[id].uV_step = 1250000;
 684                        break;
 685                default:
 686                        /*
 687                         * Read and store the RANGE bit for later use
 688                         * This must be done before regulator is probed,
 689                         * otherwise we error in probe with unsupportable ranges.
 690                         */
 691                        addr = palmas_regs_info[id].vsel_addr;
 692
 693                        ret = palmas_smps_read(pmic->palmas, addr, &reg);
 694                        if (ret)
 695                                goto err_unregister_regulator;
 696                        if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
 697                                pmic->range[id] = 1;
 698
 699                        pmic->desc[id].ops = &palmas_ops_smps;
 700                        pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
 701                        pmic->desc[id].vsel_reg =
 702                                        PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
 703                                                palmas_regs_info[id].vsel_addr);
 704                        pmic->desc[id].vsel_mask =
 705                                        PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
 706                }
 707
 708                pmic->desc[id].type = REGULATOR_VOLTAGE;
 709                pmic->desc[id].owner = THIS_MODULE;
 710
 711                if (pdata)
 712                        config.init_data = pdata->reg_data[id];
 713                else
 714                        config.init_data = NULL;
 715
 716                config.of_node = palmas_matches[id].of_node;
 717
 718                rdev = regulator_register(&pmic->desc[id], &config);
 719                if (IS_ERR(rdev)) {
 720                        dev_err(&pdev->dev,
 721                                "failed to register %s regulator\n",
 722                                pdev->name);
 723                        ret = PTR_ERR(rdev);
 724                        goto err_unregister_regulator;
 725                }
 726
 727                /* Save regulator for cleanup */
 728                pmic->rdev[id] = rdev;
 729        }
 730
 731        /* Start this loop from the id left from previous loop */
 732        for (; id < PALMAS_NUM_REGS; id++) {
 733
 734                /* Miss out regulators which are not available due
 735                 * to alternate functions.
 736                 */
 737
 738                /* Register the regulators */
 739                pmic->desc[id].name = palmas_regs_info[id].name;
 740                pmic->desc[id].id = id;
 741                pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
 742
 743                pmic->desc[id].ops = &palmas_ops_ldo;
 744
 745                pmic->desc[id].type = REGULATOR_VOLTAGE;
 746                pmic->desc[id].owner = THIS_MODULE;
 747                pmic->desc[id].min_uV = 900000;
 748                pmic->desc[id].uV_step = 50000;
 749                pmic->desc[id].linear_min_sel = 1;
 750                pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
 751                                                palmas_regs_info[id].vsel_addr);
 752                pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
 753                pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
 754                                                palmas_regs_info[id].ctrl_addr);
 755                pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
 756
 757                if (pdata)
 758                        config.init_data = pdata->reg_data[id];
 759                else
 760                        config.init_data = NULL;
 761
 762                config.of_node = palmas_matches[id].of_node;
 763
 764                rdev = regulator_register(&pmic->desc[id], &config);
 765                if (IS_ERR(rdev)) {
 766                        dev_err(&pdev->dev,
 767                                "failed to register %s regulator\n",
 768                                pdev->name);
 769                        ret = PTR_ERR(rdev);
 770                        goto err_unregister_regulator;
 771                }
 772
 773                /* Save regulator for cleanup */
 774                pmic->rdev[id] = rdev;
 775
 776                /* Initialise sleep/init values from platform data */
 777                if (pdata) {
 778                        reg_init = pdata->reg_init[id];
 779                        if (reg_init) {
 780                                ret = palmas_ldo_init(palmas, id, reg_init);
 781                                if (ret) {
 782                                        regulator_unregister(pmic->rdev[id]);
 783                                        goto err_unregister_regulator;
 784                                }
 785                        }
 786                }
 787        }
 788
 789        return 0;
 790
 791err_unregister_regulator:
 792        while (--id >= 0)
 793                regulator_unregister(pmic->rdev[id]);
 794        return ret;
 795}
 796
 797static int palmas_remove(struct platform_device *pdev)
 798{
 799        struct palmas_pmic *pmic = platform_get_drvdata(pdev);
 800        int id;
 801
 802        for (id = 0; id < PALMAS_NUM_REGS; id++)
 803                regulator_unregister(pmic->rdev[id]);
 804        return 0;
 805}
 806
 807static struct of_device_id of_palmas_match_tbl[] = {
 808        { .compatible = "ti,palmas-pmic", },
 809        { /* end */ }
 810};
 811
 812static struct platform_driver palmas_driver = {
 813        .driver = {
 814                .name = "palmas-pmic",
 815                .of_match_table = of_palmas_match_tbl,
 816                .owner = THIS_MODULE,
 817        },
 818        .probe = palmas_probe,
 819        .remove = palmas_remove,
 820};
 821
 822static int __init palmas_init(void)
 823{
 824        return platform_driver_register(&palmas_driver);
 825}
 826subsys_initcall(palmas_init);
 827
 828static void __exit palmas_exit(void)
 829{
 830        platform_driver_unregister(&palmas_driver);
 831}
 832module_exit(palmas_exit);
 833
 834MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
 835MODULE_DESCRIPTION("Palmas voltage regulator driver");
 836MODULE_LICENSE("GPL");
 837MODULE_ALIAS("platform:palmas-pmic");
 838MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
 839
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.