linux/drivers/regulator/lp872x.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 Texas Instruments
   3 *
   4 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 */
  11
  12#include <linux/module.h>
  13#include <linux/slab.h>
  14#include <linux/i2c.h>
  15#include <linux/regmap.h>
  16#include <linux/err.h>
  17#include <linux/gpio.h>
  18#include <linux/regulator/lp872x.h>
  19#include <linux/regulator/driver.h>
  20#include <linux/platform_device.h>
  21
  22/* Registers : LP8720/8725 shared */
  23#define LP872X_GENERAL_CFG              0x00
  24#define LP872X_LDO1_VOUT                0x01
  25#define LP872X_LDO2_VOUT                0x02
  26#define LP872X_LDO3_VOUT                0x03
  27#define LP872X_LDO4_VOUT                0x04
  28#define LP872X_LDO5_VOUT                0x05
  29
  30/* Registers : LP8720 */
  31#define LP8720_BUCK_VOUT1               0x06
  32#define LP8720_BUCK_VOUT2               0x07
  33#define LP8720_ENABLE                   0x08
  34
  35/* Registers : LP8725 */
  36#define LP8725_LILO1_VOUT               0x06
  37#define LP8725_LILO2_VOUT               0x07
  38#define LP8725_BUCK1_VOUT1              0x08
  39#define LP8725_BUCK1_VOUT2              0x09
  40#define LP8725_BUCK2_VOUT1              0x0A
  41#define LP8725_BUCK2_VOUT2              0x0B
  42#define LP8725_BUCK_CTRL                0x0C
  43#define LP8725_LDO_CTRL                 0x0D
  44
  45/* Mask/shift : LP8720/LP8725 shared */
  46#define LP872X_VOUT_M                   0x1F
  47#define LP872X_START_DELAY_M            0xE0
  48#define LP872X_START_DELAY_S            5
  49#define LP872X_EN_LDO1_M                BIT(0)
  50#define LP872X_EN_LDO2_M                BIT(1)
  51#define LP872X_EN_LDO3_M                BIT(2)
  52#define LP872X_EN_LDO4_M                BIT(3)
  53#define LP872X_EN_LDO5_M                BIT(4)
  54
  55/* Mask/shift : LP8720 */
  56#define LP8720_TIMESTEP_S               0               /* Addr 00h */
  57#define LP8720_TIMESTEP_M               BIT(0)
  58#define LP8720_EXT_DVS_M                BIT(2)
  59#define LP8720_BUCK_FPWM_S              5               /* Addr 07h */
  60#define LP8720_BUCK_FPWM_M              BIT(5)
  61#define LP8720_EN_BUCK_M                BIT(5)          /* Addr 08h */
  62#define LP8720_DVS_SEL_M                BIT(7)
  63
  64/* Mask/shift : LP8725 */
  65#define LP8725_TIMESTEP_M               0xC0            /* Addr 00h */
  66#define LP8725_TIMESTEP_S               6
  67#define LP8725_BUCK1_EN_M               BIT(0)
  68#define LP8725_DVS1_M                   BIT(2)
  69#define LP8725_DVS2_M                   BIT(3)
  70#define LP8725_BUCK2_EN_M               BIT(4)
  71#define LP8725_BUCK_CL_M                0xC0            /* Addr 09h, 0Bh */
  72#define LP8725_BUCK_CL_S                6
  73#define LP8725_BUCK1_FPWM_S             1               /* Addr 0Ch */
  74#define LP8725_BUCK1_FPWM_M             BIT(1)
  75#define LP8725_BUCK2_FPWM_S             5
  76#define LP8725_BUCK2_FPWM_M             BIT(5)
  77#define LP8725_EN_LILO1_M               BIT(5)          /* Addr 0Dh */
  78#define LP8725_EN_LILO2_M               BIT(6)
  79
  80/* PWM mode */
  81#define LP872X_FORCE_PWM                1
  82#define LP872X_AUTO_PWM                 0
  83
  84#define LP8720_NUM_REGULATORS           6
  85#define LP8725_NUM_REGULATORS           9
  86#define EXTERN_DVS_USED                 0
  87#define MAX_DELAY                       6
  88
  89/* Default DVS Mode */
  90#define LP8720_DEFAULT_DVS              0
  91#define LP8725_DEFAULT_DVS              BIT(2)
  92
  93/* dump registers in regmap-debugfs */
  94#define MAX_REGISTERS                   0x0F
  95
  96enum lp872x_id {
  97        LP8720,
  98        LP8725,
  99};
 100
 101struct lp872x {
 102        struct regmap *regmap;
 103        struct device *dev;
 104        enum lp872x_id chipid;
 105        struct lp872x_platform_data *pdata;
 106        struct regulator_dev **regulators;
 107        int num_regulators;
 108        enum lp872x_dvs_state dvs_pin;
 109        int dvs_gpio;
 110};
 111
 112/* LP8720/LP8725 shared voltage table for LDOs */
 113static const unsigned int lp872x_ldo_vtbl[] = {
 114        1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
 115        1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
 116        2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
 117        2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
 118};
 119
 120/* LP8720 LDO4 voltage table */
 121static const unsigned int lp8720_ldo4_vtbl[] = {
 122         800000,  850000,  900000, 1000000, 1100000, 1200000, 1250000, 1300000,
 123        1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
 124        1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
 125        2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
 126};
 127
 128/* LP8725 LILO(Low Input Low Output) voltage table */
 129static const unsigned int lp8725_lilo_vtbl[] = {
 130         800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
 131        1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
 132        1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
 133        2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
 134};
 135
 136/* LP8720 BUCK voltage table */
 137#define EXT_R           0       /* external resistor divider */
 138static const unsigned int lp8720_buck_vtbl[] = {
 139          EXT_R,  800000,  850000,  900000,  950000, 1000000, 1050000, 1100000,
 140        1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
 141        1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
 142        1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
 143};
 144
 145/* LP8725 BUCK voltage table */
 146static const unsigned int lp8725_buck_vtbl[] = {
 147         800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
 148        1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
 149        1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
 150        2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
 151};
 152
 153/* LP8725 BUCK current limit */
 154static const unsigned int lp8725_buck_uA[] = {
 155        460000, 780000, 1050000, 1370000,
 156};
 157
 158static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
 159{
 160        int ret;
 161        unsigned int val;
 162
 163        ret = regmap_read(lp->regmap, addr, &val);
 164        if (ret < 0) {
 165                dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
 166                return ret;
 167        }
 168
 169        *data = (u8)val;
 170        return 0;
 171}
 172
 173static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
 174{
 175        return regmap_write(lp->regmap, addr, data);
 176}
 177
 178static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
 179                                unsigned int mask, u8 data)
 180{
 181        return regmap_update_bits(lp->regmap, addr, mask, data);
 182}
 183
 184static int lp872x_get_timestep_usec(struct lp872x *lp)
 185{
 186        enum lp872x_id chip = lp->chipid;
 187        u8 val, mask, shift;
 188        int *time_usec, size, ret;
 189        int lp8720_time_usec[] = { 25, 50 };
 190        int lp8725_time_usec[] = { 32, 64, 128, 256 };
 191
 192        switch (chip) {
 193        case LP8720:
 194                mask = LP8720_TIMESTEP_M;
 195                shift = LP8720_TIMESTEP_S;
 196                time_usec = &lp8720_time_usec[0];
 197                size = ARRAY_SIZE(lp8720_time_usec);
 198                break;
 199        case LP8725:
 200                mask = LP8725_TIMESTEP_M;
 201                shift = LP8725_TIMESTEP_S;
 202                time_usec = &lp8725_time_usec[0];
 203                size = ARRAY_SIZE(lp8725_time_usec);
 204                break;
 205        default:
 206                return -EINVAL;
 207        }
 208
 209        ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
 210        if (ret)
 211                return -EINVAL;
 212
 213        val = (val & mask) >> shift;
 214        if (val >= size)
 215                return -EINVAL;
 216
 217        return *(time_usec + val);
 218}
 219
 220static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
 221{
 222        struct lp872x *lp = rdev_get_drvdata(rdev);
 223        enum lp872x_regulator_id rid = rdev_get_id(rdev);
 224        int time_step_us = lp872x_get_timestep_usec(lp);
 225        int ret;
 226        u8 addr, val;
 227
 228        if (time_step_us < 0)
 229                return -EINVAL;
 230
 231        switch (rid) {
 232        case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
 233                addr = LP872X_LDO1_VOUT + rid;
 234                break;
 235        case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
 236                addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
 237                break;
 238        case LP8725_ID_BUCK2:
 239                addr = LP8725_BUCK2_VOUT1;
 240                break;
 241        default:
 242                return -EINVAL;
 243        }
 244
 245        ret = lp872x_read_byte(lp, addr, &val);
 246        if (ret)
 247                return ret;
 248
 249        val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
 250
 251        return val > MAX_DELAY ? 0 : val * time_step_us;
 252}
 253
 254static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
 255                        int gpio)
 256{
 257        enum lp872x_dvs_state state;
 258
 259        state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
 260        gpio_set_value(gpio, state);
 261        lp->dvs_pin = state;
 262}
 263
 264static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
 265                                enum lp872x_regulator_id buck)
 266{
 267        u8 val, addr;
 268
 269        if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
 270                return 0;
 271
 272        switch (buck) {
 273        case LP8720_ID_BUCK:
 274                if (val & LP8720_EXT_DVS_M) {
 275                        addr = (lp->dvs_pin == DVS_HIGH) ?
 276                                LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
 277                } else {
 278                        if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
 279                                return 0;
 280
 281                        addr = val & LP8720_DVS_SEL_M ?
 282                                LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
 283                }
 284                break;
 285        case LP8725_ID_BUCK1:
 286                if (val & LP8725_DVS1_M)
 287                        addr = LP8725_BUCK1_VOUT1;
 288                else
 289                        addr = (lp->dvs_pin == DVS_HIGH) ?
 290                                LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
 291                break;
 292        case LP8725_ID_BUCK2:
 293                addr =  val & LP8725_DVS2_M ?
 294                        LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
 295                break;
 296        default:
 297                return 0;
 298        }
 299
 300        return addr;
 301}
 302
 303static bool lp872x_is_valid_buck_addr(u8 addr)
 304{
 305        switch (addr) {
 306        case LP8720_BUCK_VOUT1:
 307        case LP8720_BUCK_VOUT2:
 308        case LP8725_BUCK1_VOUT1:
 309        case LP8725_BUCK1_VOUT2:
 310        case LP8725_BUCK2_VOUT1:
 311        case LP8725_BUCK2_VOUT2:
 312                return true;
 313        default:
 314                return false;
 315        }
 316}
 317
 318static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
 319                                        unsigned selector)
 320{
 321        struct lp872x *lp = rdev_get_drvdata(rdev);
 322        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 323        u8 addr, mask = LP872X_VOUT_M;
 324        struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
 325
 326        if (dvs && gpio_is_valid(dvs->gpio))
 327                lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
 328
 329        addr = lp872x_select_buck_vout_addr(lp, buck);
 330        if (!lp872x_is_valid_buck_addr(addr))
 331                return -EINVAL;
 332
 333        return lp872x_update_bits(lp, addr, mask, selector);
 334}
 335
 336static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
 337{
 338        struct lp872x *lp = rdev_get_drvdata(rdev);
 339        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 340        u8 addr, val;
 341        int ret;
 342
 343        addr = lp872x_select_buck_vout_addr(lp, buck);
 344        if (!lp872x_is_valid_buck_addr(addr))
 345                return -EINVAL;
 346
 347        ret = lp872x_read_byte(lp, addr, &val);
 348        if (ret)
 349                return ret;
 350
 351        return val & LP872X_VOUT_M;
 352}
 353
 354static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
 355                                        int min_uA, int max_uA)
 356{
 357        struct lp872x *lp = rdev_get_drvdata(rdev);
 358        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 359        int i;
 360        u8 addr;
 361
 362        switch (buck) {
 363        case LP8725_ID_BUCK1:
 364                addr = LP8725_BUCK1_VOUT2;
 365                break;
 366        case LP8725_ID_BUCK2:
 367                addr = LP8725_BUCK2_VOUT2;
 368                break;
 369        default:
 370                return -EINVAL;
 371        }
 372
 373        for (i = ARRAY_SIZE(lp8725_buck_uA) - 1 ; i >= 0; i--) {
 374                if (lp8725_buck_uA[i] >= min_uA &&
 375                        lp8725_buck_uA[i] <= max_uA)
 376                        return lp872x_update_bits(lp, addr,
 377                                                  LP8725_BUCK_CL_M,
 378                                                  i << LP8725_BUCK_CL_S);
 379        }
 380
 381        return -EINVAL;
 382}
 383
 384static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
 385{
 386        struct lp872x *lp = rdev_get_drvdata(rdev);
 387        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 388        u8 addr, val;
 389        int ret;
 390
 391        switch (buck) {
 392        case LP8725_ID_BUCK1:
 393                addr = LP8725_BUCK1_VOUT2;
 394                break;
 395        case LP8725_ID_BUCK2:
 396                addr = LP8725_BUCK2_VOUT2;
 397                break;
 398        default:
 399                return -EINVAL;
 400        }
 401
 402        ret = lp872x_read_byte(lp, addr, &val);
 403        if (ret)
 404                return ret;
 405
 406        val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
 407
 408        return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
 409                        lp8725_buck_uA[val] : -EINVAL;
 410}
 411
 412static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
 413{
 414        struct lp872x *lp = rdev_get_drvdata(rdev);
 415        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 416        u8 addr, mask, shift, val;
 417
 418        switch (buck) {
 419        case LP8720_ID_BUCK:
 420                addr = LP8720_BUCK_VOUT2;
 421                mask = LP8720_BUCK_FPWM_M;
 422                shift = LP8720_BUCK_FPWM_S;
 423                break;
 424        case LP8725_ID_BUCK1:
 425                addr = LP8725_BUCK_CTRL;
 426                mask = LP8725_BUCK1_FPWM_M;
 427                shift = LP8725_BUCK1_FPWM_S;
 428                break;
 429        case LP8725_ID_BUCK2:
 430                addr = LP8725_BUCK_CTRL;
 431                mask = LP8725_BUCK2_FPWM_M;
 432                shift = LP8725_BUCK2_FPWM_S;
 433                break;
 434        default:
 435                return -EINVAL;
 436        }
 437
 438        if (mode == REGULATOR_MODE_FAST)
 439                val = LP872X_FORCE_PWM << shift;
 440        else if (mode == REGULATOR_MODE_NORMAL)
 441                val = LP872X_AUTO_PWM << shift;
 442        else
 443                return -EINVAL;
 444
 445        return lp872x_update_bits(lp, addr, mask, val);
 446}
 447
 448static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
 449{
 450        struct lp872x *lp = rdev_get_drvdata(rdev);
 451        enum lp872x_regulator_id buck = rdev_get_id(rdev);
 452        u8 addr, mask, val;
 453        int ret;
 454
 455        switch (buck) {
 456        case LP8720_ID_BUCK:
 457                addr = LP8720_BUCK_VOUT2;
 458                mask = LP8720_BUCK_FPWM_M;
 459                break;
 460        case LP8725_ID_BUCK1:
 461                addr = LP8725_BUCK_CTRL;
 462                mask = LP8725_BUCK1_FPWM_M;
 463                break;
 464        case LP8725_ID_BUCK2:
 465                addr = LP8725_BUCK_CTRL;
 466                mask = LP8725_BUCK2_FPWM_M;
 467                break;
 468        default:
 469                return -EINVAL;
 470        }
 471
 472        ret = lp872x_read_byte(lp, addr, &val);
 473        if (ret)
 474                return ret;
 475
 476        return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
 477}
 478
 479static struct regulator_ops lp872x_ldo_ops = {
 480        .list_voltage = regulator_list_voltage_table,
 481        .map_voltage = regulator_map_voltage_ascend,
 482        .set_voltage_sel = regulator_set_voltage_sel_regmap,
 483        .get_voltage_sel = regulator_get_voltage_sel_regmap,
 484        .enable = regulator_enable_regmap,
 485        .disable = regulator_disable_regmap,
 486        .is_enabled = regulator_is_enabled_regmap,
 487        .enable_time = lp872x_regulator_enable_time,
 488};
 489
 490static struct regulator_ops lp8720_buck_ops = {
 491        .list_voltage = regulator_list_voltage_table,
 492        .map_voltage = regulator_map_voltage_ascend,
 493        .set_voltage_sel = lp872x_buck_set_voltage_sel,
 494        .get_voltage_sel = lp872x_buck_get_voltage_sel,
 495        .enable = regulator_enable_regmap,
 496        .disable = regulator_disable_regmap,
 497        .is_enabled = regulator_is_enabled_regmap,
 498        .enable_time = lp872x_regulator_enable_time,
 499        .set_mode = lp872x_buck_set_mode,
 500        .get_mode = lp872x_buck_get_mode,
 501};
 502
 503static struct regulator_ops lp8725_buck_ops = {
 504        .list_voltage = regulator_list_voltage_table,
 505        .map_voltage = regulator_map_voltage_ascend,
 506        .set_voltage_sel = lp872x_buck_set_voltage_sel,
 507        .get_voltage_sel = lp872x_buck_get_voltage_sel,
 508        .enable = regulator_enable_regmap,
 509        .disable = regulator_disable_regmap,
 510        .is_enabled = regulator_is_enabled_regmap,
 511        .enable_time = lp872x_regulator_enable_time,
 512        .set_mode = lp872x_buck_set_mode,
 513        .get_mode = lp872x_buck_get_mode,
 514        .set_current_limit = lp8725_buck_set_current_limit,
 515        .get_current_limit = lp8725_buck_get_current_limit,
 516};
 517
 518static struct regulator_desc lp8720_regulator_desc[] = {
 519        {
 520                .name = "ldo1",
 521                .id = LP8720_ID_LDO1,
 522                .ops = &lp872x_ldo_ops,
 523                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 524                .volt_table = lp872x_ldo_vtbl,
 525                .type = REGULATOR_VOLTAGE,
 526                .owner = THIS_MODULE,
 527                .vsel_reg = LP872X_LDO1_VOUT,
 528                .vsel_mask = LP872X_VOUT_M,
 529                .enable_reg = LP8720_ENABLE,
 530                .enable_mask = LP872X_EN_LDO1_M,
 531        },
 532        {
 533                .name = "ldo2",
 534                .id = LP8720_ID_LDO2,
 535                .ops = &lp872x_ldo_ops,
 536                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 537                .volt_table = lp872x_ldo_vtbl,
 538                .type = REGULATOR_VOLTAGE,
 539                .owner = THIS_MODULE,
 540                .vsel_reg = LP872X_LDO2_VOUT,
 541                .vsel_mask = LP872X_VOUT_M,
 542                .enable_reg = LP8720_ENABLE,
 543                .enable_mask = LP872X_EN_LDO2_M,
 544        },
 545        {
 546                .name = "ldo3",
 547                .id = LP8720_ID_LDO3,
 548                .ops = &lp872x_ldo_ops,
 549                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 550                .volt_table = lp872x_ldo_vtbl,
 551                .type = REGULATOR_VOLTAGE,
 552                .owner = THIS_MODULE,
 553                .vsel_reg = LP872X_LDO3_VOUT,
 554                .vsel_mask = LP872X_VOUT_M,
 555                .enable_reg = LP8720_ENABLE,
 556                .enable_mask = LP872X_EN_LDO3_M,
 557        },
 558        {
 559                .name = "ldo4",
 560                .id = LP8720_ID_LDO4,
 561                .ops = &lp872x_ldo_ops,
 562                .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
 563                .volt_table = lp8720_ldo4_vtbl,
 564                .type = REGULATOR_VOLTAGE,
 565                .owner = THIS_MODULE,
 566                .vsel_reg = LP872X_LDO4_VOUT,
 567                .vsel_mask = LP872X_VOUT_M,
 568                .enable_reg = LP8720_ENABLE,
 569                .enable_mask = LP872X_EN_LDO4_M,
 570        },
 571        {
 572                .name = "ldo5",
 573                .id = LP8720_ID_LDO5,
 574                .ops = &lp872x_ldo_ops,
 575                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 576                .volt_table = lp872x_ldo_vtbl,
 577                .type = REGULATOR_VOLTAGE,
 578                .owner = THIS_MODULE,
 579                .vsel_reg = LP872X_LDO5_VOUT,
 580                .vsel_mask = LP872X_VOUT_M,
 581                .enable_reg = LP8720_ENABLE,
 582                .enable_mask = LP872X_EN_LDO5_M,
 583        },
 584        {
 585                .name = "buck",
 586                .id = LP8720_ID_BUCK,
 587                .ops = &lp8720_buck_ops,
 588                .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
 589                .volt_table = lp8720_buck_vtbl,
 590                .type = REGULATOR_VOLTAGE,
 591                .owner = THIS_MODULE,
 592                .enable_reg = LP8720_ENABLE,
 593                .enable_mask = LP8720_EN_BUCK_M,
 594        },
 595};
 596
 597static struct regulator_desc lp8725_regulator_desc[] = {
 598        {
 599                .name = "ldo1",
 600                .id = LP8725_ID_LDO1,
 601                .ops = &lp872x_ldo_ops,
 602                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 603                .volt_table = lp872x_ldo_vtbl,
 604                .type = REGULATOR_VOLTAGE,
 605                .owner = THIS_MODULE,
 606                .vsel_reg = LP872X_LDO1_VOUT,
 607                .vsel_mask = LP872X_VOUT_M,
 608                .enable_reg = LP8725_LDO_CTRL,
 609                .enable_mask = LP872X_EN_LDO1_M,
 610        },
 611        {
 612                .name = "ldo2",
 613                .id = LP8725_ID_LDO2,
 614                .ops = &lp872x_ldo_ops,
 615                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 616                .volt_table = lp872x_ldo_vtbl,
 617                .type = REGULATOR_VOLTAGE,
 618                .owner = THIS_MODULE,
 619                .vsel_reg = LP872X_LDO2_VOUT,
 620                .vsel_mask = LP872X_VOUT_M,
 621                .enable_reg = LP8725_LDO_CTRL,
 622                .enable_mask = LP872X_EN_LDO2_M,
 623        },
 624        {
 625                .name = "ldo3",
 626                .id = LP8725_ID_LDO3,
 627                .ops = &lp872x_ldo_ops,
 628                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 629                .volt_table = lp872x_ldo_vtbl,
 630                .type = REGULATOR_VOLTAGE,
 631                .owner = THIS_MODULE,
 632                .vsel_reg = LP872X_LDO3_VOUT,
 633                .vsel_mask = LP872X_VOUT_M,
 634                .enable_reg = LP8725_LDO_CTRL,
 635                .enable_mask = LP872X_EN_LDO3_M,
 636        },
 637        {
 638                .name = "ldo4",
 639                .id = LP8725_ID_LDO4,
 640                .ops = &lp872x_ldo_ops,
 641                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 642                .volt_table = lp872x_ldo_vtbl,
 643                .type = REGULATOR_VOLTAGE,
 644                .owner = THIS_MODULE,
 645                .vsel_reg = LP872X_LDO4_VOUT,
 646                .vsel_mask = LP872X_VOUT_M,
 647                .enable_reg = LP8725_LDO_CTRL,
 648                .enable_mask = LP872X_EN_LDO4_M,
 649        },
 650        {
 651                .name = "ldo5",
 652                .id = LP8725_ID_LDO5,
 653                .ops = &lp872x_ldo_ops,
 654                .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
 655                .volt_table = lp872x_ldo_vtbl,
 656                .type = REGULATOR_VOLTAGE,
 657                .owner = THIS_MODULE,
 658                .vsel_reg = LP872X_LDO5_VOUT,
 659                .vsel_mask = LP872X_VOUT_M,
 660                .enable_reg = LP8725_LDO_CTRL,
 661                .enable_mask = LP872X_EN_LDO5_M,
 662        },
 663        {
 664                .name = "lilo1",
 665                .id = LP8725_ID_LILO1,
 666                .ops = &lp872x_ldo_ops,
 667                .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
 668                .volt_table = lp8725_lilo_vtbl,
 669                .type = REGULATOR_VOLTAGE,
 670                .owner = THIS_MODULE,
 671                .vsel_reg = LP8725_LILO1_VOUT,
 672                .vsel_mask = LP872X_VOUT_M,
 673                .enable_reg = LP8725_LDO_CTRL,
 674                .enable_mask = LP8725_EN_LILO1_M,
 675        },
 676        {
 677                .name = "lilo2",
 678                .id = LP8725_ID_LILO2,
 679                .ops = &lp872x_ldo_ops,
 680                .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
 681                .volt_table = lp8725_lilo_vtbl,
 682                .type = REGULATOR_VOLTAGE,
 683                .owner = THIS_MODULE,
 684                .vsel_reg = LP8725_LILO2_VOUT,
 685                .vsel_mask = LP872X_VOUT_M,
 686                .enable_reg = LP8725_LDO_CTRL,
 687                .enable_mask = LP8725_EN_LILO2_M,
 688        },
 689        {
 690                .name = "buck1",
 691                .id = LP8725_ID_BUCK1,
 692                .ops = &lp8725_buck_ops,
 693                .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
 694                .volt_table = lp8725_buck_vtbl,
 695                .type = REGULATOR_VOLTAGE,
 696                .owner = THIS_MODULE,
 697                .enable_reg = LP872X_GENERAL_CFG,
 698                .enable_mask = LP8725_BUCK1_EN_M,
 699        },
 700        {
 701                .name = "buck2",
 702                .id = LP8725_ID_BUCK2,
 703                .ops = &lp8725_buck_ops,
 704                .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
 705                .volt_table = lp8725_buck_vtbl,
 706                .type = REGULATOR_VOLTAGE,
 707                .owner = THIS_MODULE,
 708                .enable_reg = LP872X_GENERAL_CFG,
 709                .enable_mask = LP8725_BUCK2_EN_M,
 710        },
 711};
 712
 713static int lp872x_init_dvs(struct lp872x *lp)
 714{
 715        int ret, gpio;
 716        struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
 717        enum lp872x_dvs_state pinstate;
 718        u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
 719        u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
 720
 721        if (!dvs)
 722                goto set_default_dvs_mode;
 723
 724        gpio = dvs->gpio;
 725        if (!gpio_is_valid(gpio)) {
 726                dev_err(lp->dev, "invalid gpio: %d\n", gpio);
 727                return -EINVAL;
 728        }
 729
 730        pinstate = dvs->init_state;
 731        ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
 732        if (ret) {
 733                dev_err(lp->dev, "gpio request err: %d\n", ret);
 734                return ret;
 735        }
 736
 737        lp->dvs_pin = pinstate;
 738        lp->dvs_gpio = gpio;
 739
 740        return 0;
 741
 742set_default_dvs_mode:
 743        return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
 744                                default_dvs_mode[lp->chipid]);
 745}
 746
 747static int lp872x_config(struct lp872x *lp)
 748{
 749        struct lp872x_platform_data *pdata = lp->pdata;
 750        int ret;
 751
 752        if (!pdata || !pdata->update_config)
 753                goto init_dvs;
 754
 755        ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
 756        if (ret)
 757                return ret;
 758
 759init_dvs:
 760        return lp872x_init_dvs(lp);
 761}
 762
 763static struct regulator_init_data
 764*lp872x_find_regulator_init_data(int id, struct lp872x *lp)
 765{
 766        struct lp872x_platform_data *pdata = lp->pdata;
 767        int i;
 768
 769        if (!pdata)
 770                return NULL;
 771
 772        for (i = 0; i < lp->num_regulators; i++) {
 773                if (pdata->regulator_data[i].id == id)
 774                        return pdata->regulator_data[i].init_data;
 775        }
 776
 777        return NULL;
 778}
 779
 780static int lp872x_regulator_register(struct lp872x *lp)
 781{
 782        struct regulator_desc *desc;
 783        struct regulator_config cfg = { };
 784        struct regulator_dev *rdev;
 785        int i, ret;
 786
 787        for (i = 0 ; i < lp->num_regulators ; i++) {
 788                desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
 789                                                &lp8725_regulator_desc[i];
 790
 791                cfg.dev = lp->dev;
 792                cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
 793                cfg.driver_data = lp;
 794                cfg.regmap = lp->regmap;
 795
 796                rdev = regulator_register(desc, &cfg);
 797                if (IS_ERR(rdev)) {
 798                        dev_err(lp->dev, "regulator register err");
 799                        ret =  PTR_ERR(rdev);
 800                        goto err;
 801                }
 802
 803                *(lp->regulators + i) = rdev;
 804        }
 805
 806        return 0;
 807err:
 808        while (--i >= 0) {
 809                rdev = *(lp->regulators + i);
 810                regulator_unregister(rdev);
 811        }
 812        return ret;
 813}
 814
 815static void lp872x_regulator_unregister(struct lp872x *lp)
 816{
 817        struct regulator_dev *rdev;
 818        int i;
 819
 820        for (i = 0 ; i < lp->num_regulators ; i++) {
 821                rdev = *(lp->regulators + i);
 822                regulator_unregister(rdev);
 823        }
 824}
 825
 826static const struct regmap_config lp872x_regmap_config = {
 827        .reg_bits = 8,
 828        .val_bits = 8,
 829        .max_register = MAX_REGISTERS,
 830};
 831
 832static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
 833{
 834        struct lp872x *lp;
 835        int ret, size, num_regulators;
 836        const int lp872x_num_regulators[] = {
 837                [LP8720] = LP8720_NUM_REGULATORS,
 838                [LP8725] = LP8725_NUM_REGULATORS,
 839        };
 840
 841        lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
 842        if (!lp)
 843                goto err_mem;
 844
 845        num_regulators = lp872x_num_regulators[id->driver_data];
 846        size = sizeof(struct regulator_dev *) * num_regulators;
 847
 848        lp->regulators = devm_kzalloc(&cl->dev, size, GFP_KERNEL);
 849        if (!lp->regulators)
 850                goto err_mem;
 851
 852        lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
 853        if (IS_ERR(lp->regmap)) {
 854                ret = PTR_ERR(lp->regmap);
 855                dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
 856                goto err_dev;
 857        }
 858
 859        lp->dev = &cl->dev;
 860        lp->pdata = cl->dev.platform_data;
 861        lp->chipid = id->driver_data;
 862        lp->num_regulators = num_regulators;
 863        i2c_set_clientdata(cl, lp);
 864
 865        ret = lp872x_config(lp);
 866        if (ret)
 867                goto err_dev;
 868
 869        return lp872x_regulator_register(lp);
 870
 871err_mem:
 872        return -ENOMEM;
 873err_dev:
 874        return ret;
 875}
 876
 877static int lp872x_remove(struct i2c_client *cl)
 878{
 879        struct lp872x *lp = i2c_get_clientdata(cl);
 880
 881        lp872x_regulator_unregister(lp);
 882        return 0;
 883}
 884
 885static const struct i2c_device_id lp872x_ids[] = {
 886        {"lp8720", LP8720},
 887        {"lp8725", LP8725},
 888        { }
 889};
 890MODULE_DEVICE_TABLE(i2c, lp872x_ids);
 891
 892static struct i2c_driver lp872x_driver = {
 893        .driver = {
 894                .name = "lp872x",
 895                .owner = THIS_MODULE,
 896        },
 897        .probe = lp872x_probe,
 898        .remove = lp872x_remove,
 899        .id_table = lp872x_ids,
 900};
 901
 902module_i2c_driver(lp872x_driver);
 903
 904MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
 905MODULE_AUTHOR("Milo Kim");
 906MODULE_LICENSE("GPL");
 907
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.