Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Regulators driver for Dialog Semiconductor DA903x
0004 //
0005 // Copyright (C) 2006-2008 Marvell International Ltd.
0006 // Copyright (C) 2008 Compulab Ltd.
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/init.h>
0010 #include <linux/err.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regulator/driver.h>
0014 #include <linux/regulator/machine.h>
0015 #include <linux/mfd/da903x.h>
0016 
0017 /* DA9030 Registers */
0018 #define DA9030_INVAL        (-1)
0019 #define DA9030_LDO1011      (0x10)
0020 #define DA9030_LDO15        (0x11)
0021 #define DA9030_LDO1416      (0x12)
0022 #define DA9030_LDO1819      (0x13)
0023 #define DA9030_LDO17        (0x14)
0024 #define DA9030_BUCK2DVM1    (0x15)
0025 #define DA9030_BUCK2DVM2    (0x16)
0026 #define DA9030_RCTL11       (0x17)
0027 #define DA9030_RCTL21       (0x18)
0028 #define DA9030_LDO1     (0x90)
0029 #define DA9030_LDO23        (0x91)
0030 #define DA9030_LDO45        (0x92)
0031 #define DA9030_LDO6     (0x93)
0032 #define DA9030_LDO78        (0x94)
0033 #define DA9030_LDO912       (0x95)
0034 #define DA9030_BUCK     (0x96)
0035 #define DA9030_RCTL12       (0x97)
0036 #define DA9030_RCTL22       (0x98)
0037 #define DA9030_LDO_UNLOCK   (0xa0)
0038 #define DA9030_LDO_UNLOCK_MASK  (0xe0)
0039 #define DA9034_OVER1        (0x10)
0040 
0041 /* DA9034 Registers */
0042 #define DA9034_INVAL        (-1)
0043 #define DA9034_OVER2        (0x11)
0044 #define DA9034_OVER3        (0x12)
0045 #define DA9034_LDO643       (0x13)
0046 #define DA9034_LDO987       (0x14)
0047 #define DA9034_LDO1110      (0x15)
0048 #define DA9034_LDO1312      (0x16)
0049 #define DA9034_LDO1514      (0x17)
0050 #define DA9034_VCC1     (0x20)
0051 #define DA9034_ADTV1        (0x23)
0052 #define DA9034_ADTV2        (0x24)
0053 #define DA9034_AVRC     (0x25)
0054 #define DA9034_CDTV1        (0x26)
0055 #define DA9034_CDTV2        (0x27)
0056 #define DA9034_CVRC     (0x28)
0057 #define DA9034_SDTV1        (0x29)
0058 #define DA9034_SDTV2        (0x2a)
0059 #define DA9034_SVRC     (0x2b)
0060 #define DA9034_MDTV1        (0x32)
0061 #define DA9034_MDTV2        (0x33)
0062 #define DA9034_MVRC     (0x34)
0063 
0064 /* DA9035 Registers. DA9034 Registers are comptabile to DA9035. */
0065 #define DA9035_OVER3        (0x12)
0066 #define DA9035_VCC2     (0x1f)
0067 #define DA9035_3DTV1        (0x2c)
0068 #define DA9035_3DTV2        (0x2d)
0069 #define DA9035_3VRC     (0x2e)
0070 #define DA9035_AUTOSKIP     (0x2f)
0071 
0072 struct da903x_regulator_info {
0073     struct regulator_desc desc;
0074 
0075     int max_uV;
0076     int vol_reg;
0077     int vol_shift;
0078     int vol_nbits;
0079     int update_reg;
0080     int update_bit;
0081     int enable_reg;
0082     int enable_bit;
0083 };
0084 
0085 static inline struct device *to_da903x_dev(struct regulator_dev *rdev)
0086 {
0087     return rdev_get_dev(rdev)->parent->parent;
0088 }
0089 
0090 static inline int check_range(struct da903x_regulator_info *info,
0091                 int min_uV, int max_uV)
0092 {
0093     if (min_uV < info->desc.min_uV || min_uV > info->max_uV)
0094         return -EINVAL;
0095 
0096     return 0;
0097 }
0098 
0099 /* DA9030/DA9034 common operations */
0100 static int da903x_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
0101 {
0102     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0103     struct device *da9034_dev = to_da903x_dev(rdev);
0104     uint8_t val, mask;
0105 
0106     if (rdev->desc->n_voltages == 1)
0107         return -EINVAL;
0108 
0109     val = selector << info->vol_shift;
0110     mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
0111 
0112     return da903x_update(da9034_dev, info->vol_reg, val, mask);
0113 }
0114 
0115 static int da903x_get_voltage_sel(struct regulator_dev *rdev)
0116 {
0117     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0118     struct device *da9034_dev = to_da903x_dev(rdev);
0119     uint8_t val, mask;
0120     int ret;
0121 
0122     if (rdev->desc->n_voltages == 1)
0123         return 0;
0124 
0125     ret = da903x_read(da9034_dev, info->vol_reg, &val);
0126     if (ret)
0127         return ret;
0128 
0129     mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
0130     val = (val & mask) >> info->vol_shift;
0131 
0132     return val;
0133 }
0134 
0135 static int da903x_enable(struct regulator_dev *rdev)
0136 {
0137     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0138     struct device *da9034_dev = to_da903x_dev(rdev);
0139 
0140     return da903x_set_bits(da9034_dev, info->enable_reg,
0141                     1 << info->enable_bit);
0142 }
0143 
0144 static int da903x_disable(struct regulator_dev *rdev)
0145 {
0146     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0147     struct device *da9034_dev = to_da903x_dev(rdev);
0148 
0149     return da903x_clr_bits(da9034_dev, info->enable_reg,
0150                     1 << info->enable_bit);
0151 }
0152 
0153 static int da903x_is_enabled(struct regulator_dev *rdev)
0154 {
0155     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0156     struct device *da9034_dev = to_da903x_dev(rdev);
0157     uint8_t reg_val;
0158     int ret;
0159 
0160     ret = da903x_read(da9034_dev, info->enable_reg, &reg_val);
0161     if (ret)
0162         return ret;
0163 
0164     return !!(reg_val & (1 << info->enable_bit));
0165 }
0166 
0167 /* DA9030 specific operations */
0168 static int da9030_set_ldo1_15_voltage_sel(struct regulator_dev *rdev,
0169                       unsigned selector)
0170 {
0171     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0172     struct device *da903x_dev = to_da903x_dev(rdev);
0173     uint8_t val, mask;
0174     int ret;
0175 
0176     val = selector << info->vol_shift;
0177     mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
0178     val |= DA9030_LDO_UNLOCK; /* have to set UNLOCK bits */
0179     mask |= DA9030_LDO_UNLOCK_MASK;
0180 
0181     /* write twice */
0182     ret = da903x_update(da903x_dev, info->vol_reg, val, mask);
0183     if (ret)
0184         return ret;
0185 
0186     return da903x_update(da903x_dev, info->vol_reg, val, mask);
0187 }
0188 
0189 static int da9030_map_ldo14_voltage(struct regulator_dev *rdev,
0190                     int min_uV, int max_uV)
0191 {
0192     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0193     int thresh, sel;
0194 
0195     if (check_range(info, min_uV, max_uV)) {
0196         pr_err("invalid voltage range (%d, %d) uV\n", min_uV, max_uV);
0197         return -EINVAL;
0198     }
0199 
0200     thresh = (info->max_uV + info->desc.min_uV) / 2;
0201     if (min_uV < thresh) {
0202         sel = DIV_ROUND_UP(thresh - min_uV, info->desc.uV_step);
0203         sel |= 0x4;
0204     } else {
0205         sel = DIV_ROUND_UP(min_uV - thresh, info->desc.uV_step);
0206     }
0207 
0208     return sel;
0209 }
0210 
0211 static int da9030_list_ldo14_voltage(struct regulator_dev *rdev,
0212                      unsigned selector)
0213 {
0214     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0215     int volt;
0216 
0217     if (selector & 0x4)
0218         volt = rdev->desc->min_uV +
0219                rdev->desc->uV_step * (3 - (selector & ~0x4));
0220     else
0221         volt = (info->max_uV + rdev->desc->min_uV) / 2 +
0222                rdev->desc->uV_step * (selector & ~0x4);
0223 
0224     if (volt > info->max_uV)
0225         return -EINVAL;
0226 
0227     return volt;
0228 }
0229 
0230 /* DA9034 specific operations */
0231 static int da9034_set_dvc_voltage_sel(struct regulator_dev *rdev,
0232                       unsigned selector)
0233 {
0234     struct da903x_regulator_info *info = rdev_get_drvdata(rdev);
0235     struct device *da9034_dev = to_da903x_dev(rdev);
0236     uint8_t val, mask;
0237     int ret;
0238 
0239     val = selector << info->vol_shift;
0240     mask = ((1 << info->vol_nbits) - 1)  << info->vol_shift;
0241 
0242     ret = da903x_update(da9034_dev, info->vol_reg, val, mask);
0243     if (ret)
0244         return ret;
0245 
0246     ret = da903x_set_bits(da9034_dev, info->update_reg,
0247                     1 << info->update_bit);
0248     return ret;
0249 }
0250 
0251 static const struct linear_range da9034_ldo12_ranges[] = {
0252     REGULATOR_LINEAR_RANGE(1700000, 0, 7, 50000),
0253     REGULATOR_LINEAR_RANGE(2700000, 8, 15, 50000),
0254 };
0255 
0256 static const struct regulator_ops da903x_regulator_ldo_ops = {
0257     .set_voltage_sel = da903x_set_voltage_sel,
0258     .get_voltage_sel = da903x_get_voltage_sel,
0259     .list_voltage   = regulator_list_voltage_linear,
0260     .map_voltage    = regulator_map_voltage_linear,
0261     .enable     = da903x_enable,
0262     .disable    = da903x_disable,
0263     .is_enabled = da903x_is_enabled,
0264 };
0265 
0266 /* NOTE: this is dedicated for the insane DA9030 LDO14 */
0267 static const struct regulator_ops da9030_regulator_ldo14_ops = {
0268     .set_voltage_sel = da903x_set_voltage_sel,
0269     .get_voltage_sel = da903x_get_voltage_sel,
0270     .list_voltage   = da9030_list_ldo14_voltage,
0271     .map_voltage    = da9030_map_ldo14_voltage,
0272     .enable     = da903x_enable,
0273     .disable    = da903x_disable,
0274     .is_enabled = da903x_is_enabled,
0275 };
0276 
0277 /* NOTE: this is dedicated for the DA9030 LDO1 and LDO15 that have locks  */
0278 static const struct regulator_ops da9030_regulator_ldo1_15_ops = {
0279     .set_voltage_sel = da9030_set_ldo1_15_voltage_sel,
0280     .get_voltage_sel = da903x_get_voltage_sel,
0281     .list_voltage   = regulator_list_voltage_linear,
0282     .map_voltage    = regulator_map_voltage_linear,
0283     .enable     = da903x_enable,
0284     .disable    = da903x_disable,
0285     .is_enabled = da903x_is_enabled,
0286 };
0287 
0288 static const struct regulator_ops da9034_regulator_dvc_ops = {
0289     .set_voltage_sel = da9034_set_dvc_voltage_sel,
0290     .get_voltage_sel = da903x_get_voltage_sel,
0291     .list_voltage   = regulator_list_voltage_linear,
0292     .map_voltage    = regulator_map_voltage_linear,
0293     .enable     = da903x_enable,
0294     .disable    = da903x_disable,
0295     .is_enabled = da903x_is_enabled,
0296 };
0297 
0298 /* NOTE: this is dedicated for the insane LDO12 */
0299 static const struct regulator_ops da9034_regulator_ldo12_ops = {
0300     .set_voltage_sel = da903x_set_voltage_sel,
0301     .get_voltage_sel = da903x_get_voltage_sel,
0302     .list_voltage   = regulator_list_voltage_linear_range,
0303     .map_voltage    = regulator_map_voltage_linear_range,
0304     .enable     = da903x_enable,
0305     .disable    = da903x_disable,
0306     .is_enabled = da903x_is_enabled,
0307 };
0308 
0309 #define DA903x_LDO(_pmic, _id, min, max, step, vreg, shift, nbits, ereg, ebit)  \
0310 {                                   \
0311     .desc   = {                         \
0312         .name   = "LDO" #_id,                   \
0313         .ops    = &da903x_regulator_ldo_ops,            \
0314         .type   = REGULATOR_VOLTAGE,                \
0315         .id = _pmic##_ID_LDO##_id,              \
0316         .n_voltages = (step) ? ((max - min) / step + 1) : 1,    \
0317         .owner  = THIS_MODULE,                  \
0318         .min_uV  = (min) * 1000,                \
0319         .uV_step = (step) * 1000,               \
0320     },                              \
0321     .max_uV     = (max) * 1000,                 \
0322     .vol_reg    = _pmic##_##vreg,               \
0323     .vol_shift  = (shift),                  \
0324     .vol_nbits  = (nbits),                  \
0325     .enable_reg = _pmic##_##ereg,               \
0326     .enable_bit = (ebit),                   \
0327 }
0328 
0329 #define DA903x_DVC(_pmic, _id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
0330 {                                   \
0331     .desc   = {                         \
0332         .name   = #_id,                     \
0333         .ops    = &da9034_regulator_dvc_ops,            \
0334         .type   = REGULATOR_VOLTAGE,                \
0335         .id = _pmic##_ID_##_id,             \
0336         .n_voltages = (step) ? ((max - min) / step + 1) : 1,    \
0337         .owner  = THIS_MODULE,                  \
0338         .min_uV = (min) * 1000,                 \
0339         .uV_step = (step) * 1000,               \
0340     },                              \
0341     .max_uV     = (max) * 1000,                 \
0342     .vol_reg    = _pmic##_##vreg,               \
0343     .vol_shift  = (0),                      \
0344     .vol_nbits  = (nbits),                  \
0345     .update_reg = _pmic##_##ureg,               \
0346     .update_bit = (ubit),                   \
0347     .enable_reg = _pmic##_##ereg,               \
0348     .enable_bit = (ebit),                   \
0349 }
0350 
0351 #define DA9034_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit) \
0352     DA903x_LDO(DA9034, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
0353 
0354 #define DA9030_LDO(_id, min, max, step, vreg, shift, nbits, ereg, ebit) \
0355     DA903x_LDO(DA9030, _id, min, max, step, vreg, shift, nbits, ereg, ebit)
0356 
0357 #define DA9030_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
0358     DA903x_DVC(DA9030, _id, min, max, step, vreg, nbits, ureg, ubit, \
0359            ereg, ebit)
0360 
0361 #define DA9034_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
0362     DA903x_DVC(DA9034, _id, min, max, step, vreg, nbits, ureg, ubit, \
0363            ereg, ebit)
0364 
0365 #define DA9035_DVC(_id, min, max, step, vreg, nbits, ureg, ubit, ereg, ebit) \
0366     DA903x_DVC(DA9035, _id, min, max, step, vreg, nbits, ureg, ubit, \
0367            ereg, ebit)
0368 
0369 static struct da903x_regulator_info da903x_regulator_info[] = {
0370     /* DA9030 */
0371     DA9030_DVC(BUCK2, 850, 1625, 25, BUCK2DVM1, 5, BUCK2DVM1, 7, RCTL11, 0),
0372 
0373     DA9030_LDO( 1, 1200, 3200, 100,    LDO1, 0, 5, RCTL12, 1),
0374     DA9030_LDO( 2, 1800, 3200, 100,   LDO23, 0, 4, RCTL12, 2),
0375     DA9030_LDO( 3, 1800, 3200, 100,   LDO23, 4, 4, RCTL12, 3),
0376     DA9030_LDO( 4, 1800, 3200, 100,   LDO45, 0, 4, RCTL12, 4),
0377     DA9030_LDO( 5, 1800, 3200, 100,   LDO45, 4, 4, RCTL12, 5),
0378     DA9030_LDO( 6, 1800, 3200, 100,    LDO6, 0, 4, RCTL12, 6),
0379     DA9030_LDO( 7, 1800, 3200, 100,   LDO78, 0, 4, RCTL12, 7),
0380     DA9030_LDO( 8, 1800, 3200, 100,   LDO78, 4, 4, RCTL22, 0),
0381     DA9030_LDO( 9, 1800, 3200, 100,  LDO912, 0, 4, RCTL22, 1),
0382     DA9030_LDO(10, 1800, 3200, 100, LDO1011, 0, 4, RCTL22, 2),
0383     DA9030_LDO(11, 1800, 3200, 100, LDO1011, 4, 4, RCTL22, 3),
0384     DA9030_LDO(12, 1800, 3200, 100,  LDO912, 4, 4, RCTL22, 4),
0385     DA9030_LDO(14, 2760, 2940,  30, LDO1416, 0, 3, RCTL11, 4),
0386     DA9030_LDO(15, 1100, 2650,  50,   LDO15, 0, 5, RCTL11, 5),
0387     DA9030_LDO(16, 1100, 2650,  50, LDO1416, 3, 5, RCTL11, 6),
0388     DA9030_LDO(17, 1800, 3200, 100,   LDO17, 0, 4, RCTL11, 7),
0389     DA9030_LDO(18, 1800, 3200, 100, LDO1819, 0, 4, RCTL21, 2),
0390     DA9030_LDO(19, 1800, 3200, 100, LDO1819, 4, 4, RCTL21, 1),
0391     DA9030_LDO(13, 2100, 2100, 0, INVAL, 0, 0, RCTL11, 3), /* fixed @2.1V */
0392 
0393     /* DA9034 */
0394     DA9034_DVC(BUCK1, 725, 1500, 25, ADTV2, 5, VCC1, 0, OVER1, 0),
0395     DA9034_DVC(BUCK2, 725, 1500, 25, CDTV2, 5, VCC1, 2, OVER1, 1),
0396     DA9034_DVC(LDO2,  725, 1500, 25, SDTV2, 5, VCC1, 4, OVER1, 2),
0397     DA9034_DVC(LDO1, 1700, 2075, 25, MDTV1, 4, VCC1, 6, OVER3, 4),
0398 
0399     DA9034_LDO( 3, 1800, 3300, 100,  LDO643, 0, 4, OVER3, 5),
0400     DA9034_LDO( 4, 1800, 2900,1100,  LDO643, 4, 1, OVER3, 6),
0401     DA9034_LDO( 6, 2500, 2850,  50,  LDO643, 5, 3, OVER2, 0),
0402     DA9034_LDO( 7, 2700, 3050,  50,  LDO987, 0, 3, OVER2, 1),
0403     DA9034_LDO( 8, 2700, 2850,  50,  LDO987, 3, 2, OVER2, 2),
0404     DA9034_LDO( 9, 2700, 3050,  50,  LDO987, 5, 3, OVER2, 3),
0405     DA9034_LDO(10, 2700, 3050,  50, LDO1110, 0, 3, OVER2, 4),
0406     DA9034_LDO(11, 1800, 3300, 100, LDO1110, 4, 4, OVER2, 5),
0407     DA9034_LDO(12, 1700, 3050,  50, LDO1312, 0, 4, OVER3, 6),
0408     DA9034_LDO(13, 1800, 3300, 100, LDO1312, 4, 4, OVER2, 7),
0409     DA9034_LDO(14, 1800, 3300, 100, LDO1514, 0, 4, OVER3, 0),
0410     DA9034_LDO(15, 1800, 3300, 100, LDO1514, 4, 4, OVER3, 1),
0411     DA9034_LDO(5, 3100, 3100, 0, INVAL, 0, 0, OVER3, 7), /* fixed @3.1V */
0412 
0413     /* DA9035 */
0414     DA9035_DVC(BUCK3, 1800, 2200, 100, 3DTV1, 3, VCC2, 0, OVER3, 3),
0415 };
0416 
0417 static inline struct da903x_regulator_info *find_regulator_info(int id)
0418 {
0419     struct da903x_regulator_info *ri;
0420     int i;
0421 
0422     for (i = 0; i < ARRAY_SIZE(da903x_regulator_info); i++) {
0423         ri = &da903x_regulator_info[i];
0424         if (ri->desc.id == id)
0425             return ri;
0426     }
0427     return NULL;
0428 }
0429 
0430 static int da903x_regulator_probe(struct platform_device *pdev)
0431 {
0432     struct da903x_regulator_info *ri = NULL;
0433     struct regulator_dev *rdev;
0434     struct regulator_config config = { };
0435 
0436     ri = find_regulator_info(pdev->id);
0437     if (ri == NULL) {
0438         dev_err(&pdev->dev, "invalid regulator ID specified\n");
0439         return -EINVAL;
0440     }
0441 
0442     /* Workaround for the weird LDO12 voltage setting */
0443     if (ri->desc.id == DA9034_ID_LDO12) {
0444         ri->desc.ops = &da9034_regulator_ldo12_ops;
0445         ri->desc.n_voltages = 16;
0446         ri->desc.linear_ranges = da9034_ldo12_ranges;
0447         ri->desc.n_linear_ranges = ARRAY_SIZE(da9034_ldo12_ranges);
0448     }
0449 
0450     if (ri->desc.id == DA9030_ID_LDO14)
0451         ri->desc.ops = &da9030_regulator_ldo14_ops;
0452 
0453     if (ri->desc.id == DA9030_ID_LDO1 || ri->desc.id == DA9030_ID_LDO15)
0454         ri->desc.ops = &da9030_regulator_ldo1_15_ops;
0455 
0456     config.dev = &pdev->dev;
0457     config.init_data = dev_get_platdata(&pdev->dev);
0458     config.driver_data = ri;
0459 
0460     rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
0461     if (IS_ERR(rdev)) {
0462         dev_err(&pdev->dev, "failed to register regulator %s\n",
0463                 ri->desc.name);
0464         return PTR_ERR(rdev);
0465     }
0466 
0467     platform_set_drvdata(pdev, rdev);
0468     return 0;
0469 }
0470 
0471 static struct platform_driver da903x_regulator_driver = {
0472     .driver = {
0473         .name   = "da903x-regulator",
0474     },
0475     .probe      = da903x_regulator_probe,
0476 };
0477 
0478 static int __init da903x_regulator_init(void)
0479 {
0480     return platform_driver_register(&da903x_regulator_driver);
0481 }
0482 subsys_initcall(da903x_regulator_init);
0483 
0484 static void __exit da903x_regulator_exit(void)
0485 {
0486     platform_driver_unregister(&da903x_regulator_driver);
0487 }
0488 module_exit(da903x_regulator_exit);
0489 
0490 MODULE_LICENSE("GPL");
0491 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
0492           "Mike Rapoport <mike@compulab.co.il>");
0493 MODULE_DESCRIPTION("Regulator Driver for Dialog Semiconductor DA903X PMIC");
0494 MODULE_ALIAS("platform:da903x-regulator");