Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Regulators driver for Marvell 88PM800
0004  *
0005  * Copyright (C) 2012 Marvell International Ltd.
0006  * Joseph(Yossi) Hanin <yhanin@marvell.com>
0007  * Yi Zhang <yizhang@marvell.com>
0008  */
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/init.h>
0012 #include <linux/err.h>
0013 #include <linux/regmap.h>
0014 #include <linux/regulator/driver.h>
0015 #include <linux/regulator/machine.h>
0016 #include <linux/mfd/88pm80x.h>
0017 #include <linux/delay.h>
0018 #include <linux/io.h>
0019 #include <linux/of.h>
0020 #include <linux/regulator/of_regulator.h>
0021 
0022 /* LDO1 with DVC[0..3] */
0023 #define PM800_LDO1_VOUT     (0x08) /* VOUT1 */
0024 #define PM800_LDO1_VOUT_2   (0x09)
0025 #define PM800_LDO1_VOUT_3   (0x0A)
0026 #define PM800_LDO2_VOUT     (0x0B)
0027 #define PM800_LDO3_VOUT     (0x0C)
0028 #define PM800_LDO4_VOUT     (0x0D)
0029 #define PM800_LDO5_VOUT     (0x0E)
0030 #define PM800_LDO6_VOUT     (0x0F)
0031 #define PM800_LDO7_VOUT     (0x10)
0032 #define PM800_LDO8_VOUT     (0x11)
0033 #define PM800_LDO9_VOUT     (0x12)
0034 #define PM800_LDO10_VOUT    (0x13)
0035 #define PM800_LDO11_VOUT    (0x14)
0036 #define PM800_LDO12_VOUT    (0x15)
0037 #define PM800_LDO13_VOUT    (0x16)
0038 #define PM800_LDO14_VOUT    (0x17)
0039 #define PM800_LDO15_VOUT    (0x18)
0040 #define PM800_LDO16_VOUT    (0x19)
0041 #define PM800_LDO17_VOUT    (0x1A)
0042 #define PM800_LDO18_VOUT    (0x1B)
0043 #define PM800_LDO19_VOUT    (0x1C)
0044 
0045 /* BUCK1 with DVC[0..3] */
0046 #define PM800_BUCK1     (0x3C)
0047 #define PM800_BUCK1_1       (0x3D)
0048 #define PM800_BUCK1_2       (0x3E)
0049 #define PM800_BUCK1_3       (0x3F)
0050 #define PM800_BUCK2     (0x40)
0051 #define PM800_BUCK3     (0x41)
0052 #define PM800_BUCK4     (0x42)
0053 #define PM800_BUCK4_1       (0x43)
0054 #define PM800_BUCK4_2       (0x44)
0055 #define PM800_BUCK4_3       (0x45)
0056 #define PM800_BUCK5     (0x46)
0057 
0058 #define PM800_BUCK_ENA      (0x50)
0059 #define PM800_LDO_ENA1_1    (0x51)
0060 #define PM800_LDO_ENA1_2    (0x52)
0061 #define PM800_LDO_ENA1_3    (0x53)
0062 
0063 #define PM800_LDO_ENA2_1    (0x56)
0064 #define PM800_LDO_ENA2_2    (0x57)
0065 #define PM800_LDO_ENA2_3    (0x58)
0066 
0067 #define PM800_BUCK1_MISC1   (0x78)
0068 #define PM800_BUCK3_MISC1   (0x7E)
0069 #define PM800_BUCK4_MISC1   (0x81)
0070 #define PM800_BUCK5_MISC1   (0x84)
0071 
0072 struct pm800_regulator_info {
0073     struct regulator_desc desc;
0074     int max_ua;
0075 };
0076 
0077 /*
0078  * vreg - the buck regs string.
0079  * ereg - the string for the enable register.
0080  * ebit - the bit number in the enable register.
0081  * amax - the current
0082  * Buck has 2 kinds of voltage steps. It is easy to find voltage by ranges,
0083  * not the constant voltage table.
0084  * n_volt - Number of available selectors
0085  */
0086 #define PM800_BUCK(match, vreg, ereg, ebit, amax, volt_ranges, n_volt)  \
0087 {                                   \
0088     .desc   = {                         \
0089         .name           = #vreg,            \
0090         .of_match       = of_match_ptr(#match),     \
0091         .regulators_node    = of_match_ptr("regulators"),   \
0092         .ops            = &pm800_volt_range_ops,    \
0093         .type           = REGULATOR_VOLTAGE,        \
0094         .id         = PM800_ID_##vreg,      \
0095         .owner          = THIS_MODULE,          \
0096         .n_voltages     = n_volt,           \
0097         .linear_ranges      = volt_ranges,          \
0098         .n_linear_ranges    = ARRAY_SIZE(volt_ranges),  \
0099         .vsel_reg       = PM800_##vreg,         \
0100         .vsel_mask      = 0x7f,             \
0101         .enable_reg     = PM800_##ereg,         \
0102         .enable_mask        = 1 << (ebit),          \
0103     },                              \
0104     .max_ua = (amax),                       \
0105 }
0106 
0107 /*
0108  * vreg - the LDO regs string
0109  * ereg -  the string for the enable register.
0110  * ebit - the bit number in the enable register.
0111  * amax - the current
0112  * volt_table - the LDO voltage table
0113  * For all the LDOes, there are too many ranges. Using volt_table will be
0114  * simpler and faster.
0115  */
0116 #define PM800_LDO(match, vreg, ereg, ebit, amax, ldo_volt_table)    \
0117 {                                   \
0118     .desc   = {                         \
0119         .name           = #vreg,            \
0120         .of_match       = of_match_ptr(#match),     \
0121         .regulators_node    = of_match_ptr("regulators"),   \
0122         .ops            = &pm800_volt_table_ops,    \
0123         .type           = REGULATOR_VOLTAGE,        \
0124         .id         = PM800_ID_##vreg,      \
0125         .owner          = THIS_MODULE,          \
0126         .n_voltages     = ARRAY_SIZE(ldo_volt_table),   \
0127         .vsel_reg       = PM800_##vreg##_VOUT,      \
0128         .vsel_mask      = 0xf,              \
0129         .enable_reg     = PM800_##ereg,         \
0130         .enable_mask        = 1 << (ebit),          \
0131         .volt_table     = ldo_volt_table,       \
0132     },                              \
0133     .max_ua = (amax),                       \
0134 }
0135 
0136 /* Ranges are sorted in ascending order. */
0137 static const struct linear_range buck1_volt_range[] = {
0138     REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
0139     REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x54, 50000),
0140 };
0141 
0142 /* BUCK 2~5 have same ranges. */
0143 static const struct linear_range buck2_5_volt_range[] = {
0144     REGULATOR_LINEAR_RANGE(600000, 0, 0x4f, 12500),
0145     REGULATOR_LINEAR_RANGE(1600000, 0x50, 0x72, 50000),
0146 };
0147 
0148 static const unsigned int ldo1_volt_table[] = {
0149     600000,  650000,  700000,  750000,  800000,  850000,  900000,  950000,
0150     1000000, 1050000, 1100000, 1150000, 1200000, 1300000, 1400000, 1500000,
0151 };
0152 
0153 static const unsigned int ldo2_volt_table[] = {
0154     1700000, 1800000, 1900000, 2000000, 2100000, 2500000, 2700000, 2800000,
0155 };
0156 
0157 /* LDO 3~17 have same voltage table. */
0158 static const unsigned int ldo3_17_volt_table[] = {
0159     1200000, 1250000, 1700000, 1800000, 1850000, 1900000, 2500000, 2600000,
0160     2700000, 2750000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
0161 };
0162 
0163 /* LDO 18~19 have same voltage table. */
0164 static const unsigned int ldo18_19_volt_table[] = {
0165     1700000, 1800000, 1900000, 2500000, 2800000, 2900000, 3100000, 3300000,
0166 };
0167 
0168 static int pm800_get_current_limit(struct regulator_dev *rdev)
0169 {
0170     struct pm800_regulator_info *info = rdev_get_drvdata(rdev);
0171 
0172     return info->max_ua;
0173 }
0174 
0175 static const struct regulator_ops pm800_volt_range_ops = {
0176     .list_voltage       = regulator_list_voltage_linear_range,
0177     .map_voltage        = regulator_map_voltage_linear_range,
0178     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0179     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0180     .enable         = regulator_enable_regmap,
0181     .disable        = regulator_disable_regmap,
0182     .is_enabled     = regulator_is_enabled_regmap,
0183     .get_current_limit  = pm800_get_current_limit,
0184 };
0185 
0186 static const struct regulator_ops pm800_volt_table_ops = {
0187     .list_voltage       = regulator_list_voltage_table,
0188     .map_voltage        = regulator_map_voltage_iterate,
0189     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0190     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0191     .enable         = regulator_enable_regmap,
0192     .disable        = regulator_disable_regmap,
0193     .is_enabled     = regulator_is_enabled_regmap,
0194     .get_current_limit  = pm800_get_current_limit,
0195 };
0196 
0197 /* The array is indexed by id(PM800_ID_XXX) */
0198 static struct pm800_regulator_info pm800_regulator_info[] = {
0199     PM800_BUCK(buck1, BUCK1, BUCK_ENA, 0, 3000000, buck1_volt_range, 0x55),
0200     PM800_BUCK(buck2, BUCK2, BUCK_ENA, 1, 1200000, buck2_5_volt_range, 0x73),
0201     PM800_BUCK(buck3, BUCK3, BUCK_ENA, 2, 1200000, buck2_5_volt_range, 0x73),
0202     PM800_BUCK(buck4, BUCK4, BUCK_ENA, 3, 1200000, buck2_5_volt_range, 0x73),
0203     PM800_BUCK(buck5, BUCK5, BUCK_ENA, 4, 1200000, buck2_5_volt_range, 0x73),
0204 
0205     PM800_LDO(ldo1, LDO1, LDO_ENA1_1, 0, 200000, ldo1_volt_table),
0206     PM800_LDO(ldo2, LDO2, LDO_ENA1_1, 1, 10000, ldo2_volt_table),
0207     PM800_LDO(ldo3, LDO3, LDO_ENA1_1, 2, 300000, ldo3_17_volt_table),
0208     PM800_LDO(ldo4, LDO4, LDO_ENA1_1, 3, 300000, ldo3_17_volt_table),
0209     PM800_LDO(ldo5, LDO5, LDO_ENA1_1, 4, 300000, ldo3_17_volt_table),
0210     PM800_LDO(ldo6, LDO6, LDO_ENA1_1, 5, 300000, ldo3_17_volt_table),
0211     PM800_LDO(ldo7, LDO7, LDO_ENA1_1, 6, 300000, ldo3_17_volt_table),
0212     PM800_LDO(ldo8, LDO8, LDO_ENA1_1, 7, 300000, ldo3_17_volt_table),
0213     PM800_LDO(ldo9, LDO9, LDO_ENA1_2, 0, 300000, ldo3_17_volt_table),
0214     PM800_LDO(ldo10, LDO10, LDO_ENA1_2, 1, 300000, ldo3_17_volt_table),
0215     PM800_LDO(ldo11, LDO11, LDO_ENA1_2, 2, 300000, ldo3_17_volt_table),
0216     PM800_LDO(ldo12, LDO12, LDO_ENA1_2, 3, 300000, ldo3_17_volt_table),
0217     PM800_LDO(ldo13, LDO13, LDO_ENA1_2, 4, 300000, ldo3_17_volt_table),
0218     PM800_LDO(ldo14, LDO14, LDO_ENA1_2, 5, 300000, ldo3_17_volt_table),
0219     PM800_LDO(ldo15, LDO15, LDO_ENA1_2, 6, 300000, ldo3_17_volt_table),
0220     PM800_LDO(ldo16, LDO16, LDO_ENA1_2, 7, 300000, ldo3_17_volt_table),
0221     PM800_LDO(ldo17, LDO17, LDO_ENA1_3, 0, 300000, ldo3_17_volt_table),
0222     PM800_LDO(ldo18, LDO18, LDO_ENA1_3, 1, 200000, ldo18_19_volt_table),
0223     PM800_LDO(ldo19, LDO19, LDO_ENA1_3, 2, 200000, ldo18_19_volt_table),
0224 };
0225 
0226 static int pm800_regulator_probe(struct platform_device *pdev)
0227 {
0228     struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0229     struct pm80x_platform_data *pdata = dev_get_platdata(pdev->dev.parent);
0230     struct regulator_config config = { };
0231     struct regulator_init_data *init_data;
0232     int i, ret;
0233 
0234     if (pdata && pdata->num_regulators) {
0235         unsigned int count = 0;
0236 
0237         /* Check whether num_regulator is valid. */
0238         for (i = 0; i < ARRAY_SIZE(pdata->regulators); i++) {
0239             if (pdata->regulators[i])
0240                 count++;
0241         }
0242         if (count != pdata->num_regulators)
0243             return -EINVAL;
0244     }
0245 
0246     config.dev = chip->dev;
0247     config.regmap = chip->subchip->regmap_power;
0248     for (i = 0; i < PM800_ID_RG_MAX; i++) {
0249         struct regulator_dev *regulator;
0250 
0251         if (pdata && pdata->num_regulators) {
0252             init_data = pdata->regulators[i];
0253             if (!init_data)
0254                 continue;
0255 
0256             config.init_data = init_data;
0257         }
0258 
0259         config.driver_data = &pm800_regulator_info[i];
0260 
0261         regulator = devm_regulator_register(&pdev->dev,
0262                 &pm800_regulator_info[i].desc, &config);
0263         if (IS_ERR(regulator)) {
0264             ret = PTR_ERR(regulator);
0265             dev_err(&pdev->dev, "Failed to register %s\n",
0266                     pm800_regulator_info[i].desc.name);
0267             return ret;
0268         }
0269     }
0270 
0271     return 0;
0272 }
0273 
0274 static struct platform_driver pm800_regulator_driver = {
0275     .driver     = {
0276         .name   = "88pm80x-regulator",
0277     },
0278     .probe      = pm800_regulator_probe,
0279 };
0280 
0281 module_platform_driver(pm800_regulator_driver);
0282 
0283 MODULE_LICENSE("GPL");
0284 MODULE_AUTHOR("Joseph(Yossi) Hanin <yhanin@marvell.com>");
0285 MODULE_DESCRIPTION("Regulator Driver for Marvell 88PM800 PMIC");
0286 MODULE_ALIAS("platform:88pm800-regulator");