Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Split TWL6030 logic from twl-regulator.c:
0004  * Copyright (C) 2008 David Brownell
0005  *
0006  * Copyright (C) 2016 Nicolae Rosia <nicolae.rosia@gmail.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/string.h>
0011 #include <linux/slab.h>
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 #include <linux/regulator/driver.h>
0018 #include <linux/regulator/machine.h>
0019 #include <linux/regulator/of_regulator.h>
0020 #include <linux/mfd/twl.h>
0021 #include <linux/delay.h>
0022 
0023 struct twlreg_info {
0024     /* start of regulator's PM_RECEIVER control register bank */
0025     u8          base;
0026 
0027     /* twl resource ID, for resource control state machine */
0028     u8          id;
0029 
0030     u8          flags;
0031 
0032     /* used by regulator core */
0033     struct regulator_desc   desc;
0034 
0035     /* chip specific features */
0036     unsigned long       features;
0037 
0038     /* data passed from board for external get/set voltage */
0039     void            *data;
0040 };
0041 
0042 
0043 /* LDO control registers ... offset is from the base of its register bank.
0044  * The first three registers of all power resource banks help hardware to
0045  * manage the various resource groups.
0046  */
0047 /* Common offset in TWL4030/6030 */
0048 #define VREG_GRP        0
0049 /* TWL6030 register offsets */
0050 #define VREG_TRANS      1
0051 #define VREG_STATE      2
0052 #define VREG_VOLTAGE        3
0053 #define VREG_VOLTAGE_SMPS   4
0054 /* TWL6030 Misc register offsets */
0055 #define VREG_BC_ALL     1
0056 #define VREG_BC_REF     2
0057 #define VREG_BC_PROC        3
0058 #define VREG_BC_CLK_RST     4
0059 
0060 /* TWL6030 LDO register values for VREG_VOLTAGE */
0061 #define TWL6030_VREG_VOLTAGE_WR_S   BIT(7)
0062 
0063 /* TWL6030 LDO register values for CFG_STATE */
0064 #define TWL6030_CFG_STATE_OFF   0x00
0065 #define TWL6030_CFG_STATE_ON    0x01
0066 #define TWL6030_CFG_STATE_OFF2  0x02
0067 #define TWL6030_CFG_STATE_SLEEP 0x03
0068 #define TWL6030_CFG_STATE_GRP_SHIFT 5
0069 #define TWL6030_CFG_STATE_APP_SHIFT 2
0070 #define TWL6030_CFG_STATE_APP_MASK  (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
0071 #define TWL6030_CFG_STATE_APP(v)    (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
0072                         TWL6030_CFG_STATE_APP_SHIFT)
0073 
0074 /* Flags for SMPS Voltage reading and LDO reading*/
0075 #define SMPS_OFFSET_EN      BIT(0)
0076 #define SMPS_EXTENDED_EN    BIT(1)
0077 #define TWL_6030_WARM_RESET BIT(3)
0078 
0079 /* twl6032 SMPS EPROM values */
0080 #define TWL6030_SMPS_OFFSET     0xB0
0081 #define TWL6030_SMPS_MULT       0xB3
0082 #define SMPS_MULTOFFSET_SMPS4   BIT(0)
0083 #define SMPS_MULTOFFSET_VIO BIT(1)
0084 #define SMPS_MULTOFFSET_SMPS3   BIT(6)
0085 
0086 static inline int
0087 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
0088 {
0089     u8 value;
0090     int status;
0091 
0092     status = twl_i2c_read_u8(slave_subgp,
0093             &value, info->base + offset);
0094     return (status < 0) ? status : value;
0095 }
0096 
0097 static inline int
0098 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
0099                          u8 value)
0100 {
0101     return twl_i2c_write_u8(slave_subgp,
0102             value, info->base + offset);
0103 }
0104 
0105 /* generic power resource operations, which work on all regulators */
0106 static int twlreg_grp(struct regulator_dev *rdev)
0107 {
0108     return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
0109                                  VREG_GRP);
0110 }
0111 
0112 /*
0113  * Enable/disable regulators by joining/leaving the P1 (processor) group.
0114  * We assume nobody else is updating the DEV_GRP registers.
0115  */
0116 /* definition for 6030 family */
0117 #define P3_GRP_6030 BIT(2)      /* secondary processor, modem, etc */
0118 #define P2_GRP_6030 BIT(1)      /* "peripherals" */
0119 #define P1_GRP_6030 BIT(0)      /* CPU/Linux */
0120 
0121 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
0122 {
0123     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0124     int         grp = 0, val;
0125 
0126     if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
0127         grp = twlreg_grp(rdev);
0128         if (grp < 0)
0129             return grp;
0130         grp &= P1_GRP_6030;
0131     } else {
0132         grp = 1;
0133     }
0134 
0135     val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
0136     val = TWL6030_CFG_STATE_APP(val);
0137 
0138     return grp && (val == TWL6030_CFG_STATE_ON);
0139 }
0140 
0141 #define PB_I2C_BUSY BIT(0)
0142 #define PB_I2C_BWEN BIT(1)
0143 
0144 
0145 static int twl6030reg_enable(struct regulator_dev *rdev)
0146 {
0147     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0148     int         grp = 0;
0149     int         ret;
0150 
0151     if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
0152         grp = twlreg_grp(rdev);
0153     if (grp < 0)
0154         return grp;
0155 
0156     ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
0157             grp << TWL6030_CFG_STATE_GRP_SHIFT |
0158             TWL6030_CFG_STATE_ON);
0159     return ret;
0160 }
0161 
0162 static int twl6030reg_disable(struct regulator_dev *rdev)
0163 {
0164     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0165     int         grp = 0;
0166     int         ret;
0167 
0168     if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
0169         grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
0170 
0171     /* For 6030, set the off state for all grps enabled */
0172     ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
0173             (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
0174             TWL6030_CFG_STATE_OFF);
0175 
0176     return ret;
0177 }
0178 
0179 static int twl6030reg_get_status(struct regulator_dev *rdev)
0180 {
0181     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0182     int         val;
0183 
0184     val = twlreg_grp(rdev);
0185     if (val < 0)
0186         return val;
0187 
0188     val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
0189 
0190     switch (TWL6030_CFG_STATE_APP(val)) {
0191     case TWL6030_CFG_STATE_ON:
0192         return REGULATOR_STATUS_NORMAL;
0193 
0194     case TWL6030_CFG_STATE_SLEEP:
0195         return REGULATOR_STATUS_STANDBY;
0196 
0197     case TWL6030_CFG_STATE_OFF:
0198     case TWL6030_CFG_STATE_OFF2:
0199     default:
0200         break;
0201     }
0202 
0203     return REGULATOR_STATUS_OFF;
0204 }
0205 
0206 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
0207 {
0208     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0209     int grp = 0;
0210     int val;
0211 
0212     if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
0213         grp = twlreg_grp(rdev);
0214 
0215     if (grp < 0)
0216         return grp;
0217 
0218     /* Compose the state register settings */
0219     val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
0220     /* We can only set the mode through state machine commands... */
0221     switch (mode) {
0222     case REGULATOR_MODE_NORMAL:
0223         val |= TWL6030_CFG_STATE_ON;
0224         break;
0225     case REGULATOR_MODE_STANDBY:
0226         val |= TWL6030_CFG_STATE_SLEEP;
0227         break;
0228 
0229     default:
0230         return -EINVAL;
0231     }
0232 
0233     return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
0234 }
0235 
0236 static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
0237     int max_uV, unsigned *selector)
0238 {
0239     return -ENODEV;
0240 }
0241 
0242 static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
0243 {
0244     return -ENODEV;
0245 }
0246 
0247 static const struct regulator_ops twl6030coresmps_ops = {
0248     .set_voltage    = twl6030coresmps_set_voltage,
0249     .get_voltage    = twl6030coresmps_get_voltage,
0250 };
0251 
0252 static int
0253 twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
0254 {
0255     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0256 
0257     if (info->flags & TWL_6030_WARM_RESET)
0258         selector |= TWL6030_VREG_VOLTAGE_WR_S;
0259 
0260     return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
0261                 selector);
0262 }
0263 
0264 static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
0265 {
0266     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0267     int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
0268 
0269     if (info->flags & TWL_6030_WARM_RESET)
0270         vsel &= ~TWL6030_VREG_VOLTAGE_WR_S;
0271 
0272     return vsel;
0273 }
0274 
0275 static const struct regulator_ops twl6030ldo_ops = {
0276     .list_voltage   = regulator_list_voltage_linear_range,
0277 
0278     .set_voltage_sel = twl6030ldo_set_voltage_sel,
0279     .get_voltage_sel = twl6030ldo_get_voltage_sel,
0280 
0281     .enable     = twl6030reg_enable,
0282     .disable    = twl6030reg_disable,
0283     .is_enabled = twl6030reg_is_enabled,
0284 
0285     .set_mode   = twl6030reg_set_mode,
0286 
0287     .get_status = twl6030reg_get_status,
0288 };
0289 
0290 static const struct regulator_ops twl6030fixed_ops = {
0291     .list_voltage   = regulator_list_voltage_linear,
0292 
0293     .enable     = twl6030reg_enable,
0294     .disable    = twl6030reg_disable,
0295     .is_enabled = twl6030reg_is_enabled,
0296 
0297     .set_mode   = twl6030reg_set_mode,
0298 
0299     .get_status = twl6030reg_get_status,
0300 };
0301 
0302 /*
0303  * SMPS status and control
0304  */
0305 
0306 static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
0307 {
0308     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0309 
0310     int voltage = 0;
0311 
0312     switch (info->flags) {
0313     case SMPS_OFFSET_EN:
0314         voltage = 100000;
0315         fallthrough;
0316     case 0:
0317         switch (index) {
0318         case 0:
0319             voltage = 0;
0320             break;
0321         case 58:
0322             voltage = 1350 * 1000;
0323             break;
0324         case 59:
0325             voltage = 1500 * 1000;
0326             break;
0327         case 60:
0328             voltage = 1800 * 1000;
0329             break;
0330         case 61:
0331             voltage = 1900 * 1000;
0332             break;
0333         case 62:
0334             voltage = 2100 * 1000;
0335             break;
0336         default:
0337             voltage += (600000 + (12500 * (index - 1)));
0338         }
0339         break;
0340     case SMPS_EXTENDED_EN:
0341         switch (index) {
0342         case 0:
0343             voltage = 0;
0344             break;
0345         case 58:
0346             voltage = 2084 * 1000;
0347             break;
0348         case 59:
0349             voltage = 2315 * 1000;
0350             break;
0351         case 60:
0352             voltage = 2778 * 1000;
0353             break;
0354         case 61:
0355             voltage = 2932 * 1000;
0356             break;
0357         case 62:
0358             voltage = 3241 * 1000;
0359             break;
0360         default:
0361             voltage = (1852000 + (38600 * (index - 1)));
0362         }
0363         break;
0364     case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
0365         switch (index) {
0366         case 0:
0367             voltage = 0;
0368             break;
0369         case 58:
0370             voltage = 4167 * 1000;
0371             break;
0372         case 59:
0373             voltage = 2315 * 1000;
0374             break;
0375         case 60:
0376             voltage = 2778 * 1000;
0377             break;
0378         case 61:
0379             voltage = 2932 * 1000;
0380             break;
0381         case 62:
0382             voltage = 3241 * 1000;
0383             break;
0384         default:
0385             voltage = (2161000 + (38600 * (index - 1)));
0386         }
0387         break;
0388     }
0389 
0390     return voltage;
0391 }
0392 
0393 static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
0394                    int max_uV)
0395 {
0396     struct twlreg_info *info = rdev_get_drvdata(rdev);
0397     int vsel = 0;
0398 
0399     switch (info->flags) {
0400     case 0:
0401         if (min_uV == 0)
0402             vsel = 0;
0403         else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
0404             vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
0405             vsel++;
0406         }
0407         /* Values 1..57 for vsel are linear and can be calculated
0408          * values 58..62 are non linear.
0409          */
0410         else if ((min_uV > 1900000) && (min_uV <= 2100000))
0411             vsel = 62;
0412         else if ((min_uV > 1800000) && (min_uV <= 1900000))
0413             vsel = 61;
0414         else if ((min_uV > 1500000) && (min_uV <= 1800000))
0415             vsel = 60;
0416         else if ((min_uV > 1350000) && (min_uV <= 1500000))
0417             vsel = 59;
0418         else if ((min_uV > 1300000) && (min_uV <= 1350000))
0419             vsel = 58;
0420         else
0421             return -EINVAL;
0422         break;
0423     case SMPS_OFFSET_EN:
0424         if (min_uV == 0)
0425             vsel = 0;
0426         else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
0427             vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
0428             vsel++;
0429         }
0430         /* Values 1..57 for vsel are linear and can be calculated
0431          * values 58..62 are non linear.
0432          */
0433         else if ((min_uV > 1900000) && (min_uV <= 2100000))
0434             vsel = 62;
0435         else if ((min_uV > 1800000) && (min_uV <= 1900000))
0436             vsel = 61;
0437         else if ((min_uV > 1500000) && (min_uV <= 1800000))
0438             vsel = 60;
0439         else if ((min_uV > 1350000) && (min_uV <= 1500000))
0440             vsel = 59;
0441         else
0442             return -EINVAL;
0443         break;
0444     case SMPS_EXTENDED_EN:
0445         if (min_uV == 0) {
0446             vsel = 0;
0447         } else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
0448             vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
0449             vsel++;
0450         }
0451         break;
0452     case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
0453         if (min_uV == 0) {
0454             vsel = 0;
0455         } else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
0456             vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
0457             vsel++;
0458         }
0459         break;
0460     }
0461 
0462     return vsel;
0463 }
0464 
0465 static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
0466                        unsigned int selector)
0467 {
0468     struct twlreg_info *info = rdev_get_drvdata(rdev);
0469 
0470     return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
0471                 selector);
0472 }
0473 
0474 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
0475 {
0476     struct twlreg_info  *info = rdev_get_drvdata(rdev);
0477 
0478     return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
0479 }
0480 
0481 static const struct regulator_ops twlsmps_ops = {
0482     .list_voltage       = twl6030smps_list_voltage,
0483     .map_voltage        = twl6030smps_map_voltage,
0484 
0485     .set_voltage_sel    = twl6030smps_set_voltage_sel,
0486     .get_voltage_sel    = twl6030smps_get_voltage_sel,
0487 
0488     .enable         = twl6030reg_enable,
0489     .disable        = twl6030reg_disable,
0490     .is_enabled     = twl6030reg_is_enabled,
0491 
0492     .set_mode       = twl6030reg_set_mode,
0493 
0494     .get_status     = twl6030reg_get_status,
0495 };
0496 
0497 /*----------------------------------------------------------------------*/
0498 static const struct linear_range twl6030ldo_linear_range[] = {
0499     REGULATOR_LINEAR_RANGE(0, 0, 0, 0),
0500     REGULATOR_LINEAR_RANGE(1000000, 1, 24, 100000),
0501     REGULATOR_LINEAR_RANGE(2750000, 31, 31, 0),
0502 };
0503 
0504 #define TWL6030_ADJUSTABLE_SMPS(label) \
0505 static const struct twlreg_info TWL6030_INFO_##label = { \
0506     .desc = { \
0507         .name = #label, \
0508         .id = TWL6030_REG_##label, \
0509         .ops = &twl6030coresmps_ops, \
0510         .type = REGULATOR_VOLTAGE, \
0511         .owner = THIS_MODULE, \
0512         }, \
0513     }
0514 
0515 #define TWL6030_ADJUSTABLE_LDO(label, offset) \
0516 static const struct twlreg_info TWL6030_INFO_##label = { \
0517     .base = offset, \
0518     .desc = { \
0519         .name = #label, \
0520         .id = TWL6030_REG_##label, \
0521         .n_voltages = 32, \
0522         .linear_ranges = twl6030ldo_linear_range, \
0523         .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
0524         .ops = &twl6030ldo_ops, \
0525         .type = REGULATOR_VOLTAGE, \
0526         .owner = THIS_MODULE, \
0527         }, \
0528     }
0529 
0530 #define TWL6032_ADJUSTABLE_LDO(label, offset) \
0531 static const struct twlreg_info TWL6032_INFO_##label = { \
0532     .base = offset, \
0533     .desc = { \
0534         .name = #label, \
0535         .id = TWL6032_REG_##label, \
0536         .n_voltages = 32, \
0537         .linear_ranges = twl6030ldo_linear_range, \
0538         .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
0539         .ops = &twl6030ldo_ops, \
0540         .type = REGULATOR_VOLTAGE, \
0541         .owner = THIS_MODULE, \
0542         }, \
0543     }
0544 
0545 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
0546 static const struct twlreg_info TWLFIXED_INFO_##label = { \
0547     .base = offset, \
0548     .id = 0, \
0549     .desc = { \
0550         .name = #label, \
0551         .id = TWL6030##_REG_##label, \
0552         .n_voltages = 1, \
0553         .ops = &twl6030fixed_ops, \
0554         .type = REGULATOR_VOLTAGE, \
0555         .owner = THIS_MODULE, \
0556         .min_uV = mVolts * 1000, \
0557         .enable_time = turnon_delay, \
0558         .of_map_mode = NULL, \
0559         }, \
0560     }
0561 
0562 #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
0563 static const struct twlreg_info TWLSMPS_INFO_##label = { \
0564     .base = offset, \
0565     .desc = { \
0566         .name = #label, \
0567         .id = TWL6032_REG_##label, \
0568         .n_voltages = 63, \
0569         .ops = &twlsmps_ops, \
0570         .type = REGULATOR_VOLTAGE, \
0571         .owner = THIS_MODULE, \
0572         }, \
0573     }
0574 
0575 /* VUSBCP is managed *only* by the USB subchip */
0576 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
0577 /* Turnon-delay and remap configuration values for 6030 are not
0578    verified since the specification is not public */
0579 TWL6030_ADJUSTABLE_SMPS(VDD1);
0580 TWL6030_ADJUSTABLE_SMPS(VDD2);
0581 TWL6030_ADJUSTABLE_SMPS(VDD3);
0582 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54);
0583 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58);
0584 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c);
0585 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68);
0586 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c);
0587 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74);
0588 /* 6025 are renamed compared to 6030 versions */
0589 TWL6032_ADJUSTABLE_LDO(LDO2, 0x54);
0590 TWL6032_ADJUSTABLE_LDO(LDO4, 0x58);
0591 TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c);
0592 TWL6032_ADJUSTABLE_LDO(LDO5, 0x68);
0593 TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c);
0594 TWL6032_ADJUSTABLE_LDO(LDO7, 0x74);
0595 TWL6032_ADJUSTABLE_LDO(LDO6, 0x60);
0596 TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64);
0597 TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70);
0598 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
0599 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
0600 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
0601 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
0602 TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
0603 TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
0604 TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
0605 TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
0606 TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
0607 
0608 static u8 twl_get_smps_offset(void)
0609 {
0610     u8 value;
0611 
0612     twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
0613             TWL6030_SMPS_OFFSET);
0614     return value;
0615 }
0616 
0617 static u8 twl_get_smps_mult(void)
0618 {
0619     u8 value;
0620 
0621     twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
0622             TWL6030_SMPS_MULT);
0623     return value;
0624 }
0625 
0626 #define TWL_OF_MATCH(comp, family, label) \
0627     { \
0628         .compatible = comp, \
0629         .data = &family##_INFO_##label, \
0630     }
0631 
0632 #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
0633 #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
0634 #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
0635 #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
0636 
0637 static const struct of_device_id twl_of_match[] = {
0638     TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
0639     TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
0640     TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
0641     TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
0642     TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
0643     TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
0644     TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
0645     TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
0646     TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
0647     TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
0648     TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
0649     TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
0650     TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
0651     TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
0652     TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
0653     TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
0654     TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
0655     TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
0656     TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
0657     TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
0658     TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
0659     TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
0660     TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
0661     TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
0662     TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
0663     TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
0664     TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
0665     {},
0666 };
0667 MODULE_DEVICE_TABLE(of, twl_of_match);
0668 
0669 static int twlreg_probe(struct platform_device *pdev)
0670 {
0671     int id;
0672     struct twlreg_info      *info;
0673     const struct twlreg_info    *template;
0674     struct regulator_init_data  *initdata;
0675     struct regulation_constraints   *c;
0676     struct regulator_dev        *rdev;
0677     struct regulator_config     config = { };
0678     struct device_node      *np = pdev->dev.of_node;
0679 
0680     template = of_device_get_match_data(&pdev->dev);
0681     if (!template)
0682         return -ENODEV;
0683 
0684     id = template->desc.id;
0685     initdata = of_get_regulator_init_data(&pdev->dev, np, &template->desc);
0686     if (!initdata)
0687         return -EINVAL;
0688 
0689     info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
0690     if (!info)
0691         return -ENOMEM;
0692 
0693     /* Constrain board-specific capabilities according to what
0694      * this driver and the chip itself can actually do.
0695      */
0696     c = &initdata->constraints;
0697     c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
0698     c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
0699                 | REGULATOR_CHANGE_MODE
0700                 | REGULATOR_CHANGE_STATUS;
0701 
0702     switch (id) {
0703     case TWL6032_REG_SMPS3:
0704         if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
0705             info->flags |= SMPS_EXTENDED_EN;
0706         if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
0707             info->flags |= SMPS_OFFSET_EN;
0708         break;
0709     case TWL6032_REG_SMPS4:
0710         if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
0711             info->flags |= SMPS_EXTENDED_EN;
0712         if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
0713             info->flags |= SMPS_OFFSET_EN;
0714         break;
0715     case TWL6032_REG_VIO:
0716         if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
0717             info->flags |= SMPS_EXTENDED_EN;
0718         if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
0719             info->flags |= SMPS_OFFSET_EN;
0720         break;
0721     }
0722 
0723     if (of_get_property(np, "ti,retain-on-reset", NULL))
0724         info->flags |= TWL_6030_WARM_RESET;
0725 
0726     config.dev = &pdev->dev;
0727     config.init_data = initdata;
0728     config.driver_data = info;
0729     config.of_node = np;
0730 
0731     rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
0732     if (IS_ERR(rdev)) {
0733         dev_err(&pdev->dev, "can't register %s, %ld\n",
0734                 info->desc.name, PTR_ERR(rdev));
0735         return PTR_ERR(rdev);
0736     }
0737     platform_set_drvdata(pdev, rdev);
0738 
0739     /* NOTE:  many regulators support short-circuit IRQs (presentable
0740      * as REGULATOR_OVER_CURRENT notifications?) configured via:
0741      *  - SC_CONFIG
0742      *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
0743      *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
0744      *  - IT_CONFIG
0745      */
0746 
0747     return 0;
0748 }
0749 
0750 MODULE_ALIAS("platform:twl6030_reg");
0751 
0752 static struct platform_driver twlreg_driver = {
0753     .probe      = twlreg_probe,
0754     /* NOTE: short name, to work around driver model truncation of
0755      * "twl_regulator.12" (and friends) to "twl_regulator.1".
0756      */
0757     .driver  = {
0758         .name  = "twl6030_reg",
0759         .of_match_table = of_match_ptr(twl_of_match),
0760     },
0761 };
0762 
0763 static int __init twlreg_init(void)
0764 {
0765     return platform_driver_register(&twlreg_driver);
0766 }
0767 subsys_initcall(twlreg_init);
0768 
0769 static void __exit twlreg_exit(void)
0770 {
0771     platform_driver_unregister(&twlreg_driver);
0772 }
0773 module_exit(twlreg_exit)
0774 
0775 MODULE_DESCRIPTION("TWL6030 regulator driver");
0776 MODULE_LICENSE("GPL");