Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // wm8994-regulator.c  --  Regulator driver for the WM8994
0004 //
0005 // Copyright 2009 Wolfson Microelectronics PLC.
0006 //
0007 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008 
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/init.h>
0012 #include <linux/bitops.h>
0013 #include <linux/err.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regulator/driver.h>
0016 #include <linux/regulator/machine.h>
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/slab.h>
0019 
0020 #include <linux/mfd/wm8994/core.h>
0021 #include <linux/mfd/wm8994/registers.h>
0022 #include <linux/mfd/wm8994/pdata.h>
0023 
0024 struct wm8994_ldo {
0025     struct regulator_dev *regulator;
0026     struct wm8994 *wm8994;
0027     struct regulator_consumer_supply supply;
0028     struct regulator_init_data init_data;
0029 };
0030 
0031 #define WM8994_LDO1_MAX_SELECTOR 0x7
0032 #define WM8994_LDO2_MAX_SELECTOR 0x3
0033 
0034 static const struct regulator_ops wm8994_ldo1_ops = {
0035     .list_voltage = regulator_list_voltage_linear,
0036     .map_voltage = regulator_map_voltage_linear,
0037     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0038     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0039 };
0040 
0041 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
0042                     unsigned int selector)
0043 {
0044     struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
0045 
0046     if (selector > WM8994_LDO2_MAX_SELECTOR)
0047         return -EINVAL;
0048 
0049     switch (ldo->wm8994->type) {
0050     case WM8994:
0051         return (selector * 100000) + 900000;
0052     case WM8958:
0053         return (selector * 100000) + 1000000;
0054     case WM1811:
0055         switch (selector) {
0056         case 0:
0057             return -EINVAL;
0058         default:
0059             return (selector * 100000) + 950000;
0060         }
0061         break;
0062     default:
0063         return -EINVAL;
0064     }
0065 }
0066 
0067 static const struct regulator_ops wm8994_ldo2_ops = {
0068     .list_voltage = wm8994_ldo2_list_voltage,
0069     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0070     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0071 };
0072 
0073 static const struct regulator_desc wm8994_ldo_desc[] = {
0074     {
0075         .name = "LDO1",
0076         .id = 1,
0077         .type = REGULATOR_VOLTAGE,
0078         .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
0079         .vsel_reg = WM8994_LDO_1,
0080         .vsel_mask = WM8994_LDO1_VSEL_MASK,
0081         .ops = &wm8994_ldo1_ops,
0082         .min_uV = 2400000,
0083         .uV_step = 100000,
0084         .enable_time = 3000,
0085         .off_on_delay = 36000,
0086         .owner = THIS_MODULE,
0087     },
0088     {
0089         .name = "LDO2",
0090         .id = 2,
0091         .type = REGULATOR_VOLTAGE,
0092         .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
0093         .vsel_reg = WM8994_LDO_2,
0094         .vsel_mask = WM8994_LDO2_VSEL_MASK,
0095         .ops = &wm8994_ldo2_ops,
0096         .enable_time = 3000,
0097         .off_on_delay = 36000,
0098         .owner = THIS_MODULE,
0099     },
0100 };
0101 
0102 static const struct regulator_desc wm8958_ldo_desc[] = {
0103     {
0104         .name = "LDO1",
0105         .id = 1,
0106         .type = REGULATOR_VOLTAGE,
0107         .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
0108         .vsel_reg = WM8994_LDO_1,
0109         .vsel_mask = WM8994_LDO1_VSEL_MASK,
0110         .ops = &wm8994_ldo1_ops,
0111         .min_uV = 2400000,
0112         .uV_step = 100000,
0113         .enable_time = 3000,
0114         .owner = THIS_MODULE,
0115     },
0116     {
0117         .name = "LDO2",
0118         .id = 2,
0119         .type = REGULATOR_VOLTAGE,
0120         .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
0121         .vsel_reg = WM8994_LDO_2,
0122         .vsel_mask = WM8994_LDO2_VSEL_MASK,
0123         .ops = &wm8994_ldo2_ops,
0124         .enable_time = 3000,
0125         .owner = THIS_MODULE,
0126     },
0127 };
0128 
0129 static const struct regulator_consumer_supply wm8994_ldo_consumer[] = {
0130     { .supply = "AVDD1" },
0131     { .supply = "DCVDD" },
0132 };
0133 
0134 static const struct regulator_init_data wm8994_ldo_default[] = {
0135     {
0136         .constraints = {
0137             .valid_ops_mask = REGULATOR_CHANGE_STATUS,
0138         },
0139         .num_consumer_supplies = 1,
0140     },
0141     {
0142         .constraints = {
0143             .valid_ops_mask = REGULATOR_CHANGE_STATUS,
0144         },
0145         .num_consumer_supplies = 1,
0146     },
0147 };
0148 
0149 static int wm8994_ldo_probe(struct platform_device *pdev)
0150 {
0151     struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
0152     struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev);
0153     int id = pdev->id % ARRAY_SIZE(pdata->ldo);
0154     struct regulator_config config = { };
0155     struct wm8994_ldo *ldo;
0156     struct gpio_desc *gpiod;
0157     int ret;
0158 
0159     dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
0160 
0161     ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
0162     if (!ldo)
0163         return -ENOMEM;
0164 
0165     ldo->wm8994 = wm8994;
0166     ldo->supply = wm8994_ldo_consumer[id];
0167     ldo->supply.dev_name = dev_name(wm8994->dev);
0168 
0169     config.dev = wm8994->dev;
0170     config.driver_data = ldo;
0171     config.regmap = wm8994->regmap;
0172     config.init_data = &ldo->init_data;
0173 
0174     /*
0175      * Look up LDO enable GPIO from the parent device node, we don't
0176      * use devm because the regulator core will free the GPIO
0177      */
0178     gpiod = gpiod_get_optional(pdev->dev.parent,
0179                    id ? "wlf,ldo2ena" : "wlf,ldo1ena",
0180                    GPIOD_OUT_LOW |
0181                    GPIOD_FLAGS_BIT_NONEXCLUSIVE);
0182     if (IS_ERR(gpiod))
0183         return PTR_ERR(gpiod);
0184     config.ena_gpiod = gpiod;
0185 
0186     /* Use default constraints if none set up */
0187     if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) {
0188         dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n",
0189             ldo->supply.dev_name, ldo->supply.supply);
0190 
0191         ldo->init_data = wm8994_ldo_default[id];
0192         ldo->init_data.consumer_supplies = &ldo->supply;
0193         if (!gpiod)
0194             ldo->init_data.constraints.valid_ops_mask = 0;
0195     } else {
0196         ldo->init_data = *pdata->ldo[id].init_data;
0197     }
0198 
0199     /*
0200      * At this point the GPIO descriptor is handled over to the
0201      * regulator core and we need not worry about it on the
0202      * error path.
0203      */
0204     if (ldo->wm8994->type == WM8994) {
0205         ldo->regulator = devm_regulator_register(&pdev->dev,
0206                              &wm8994_ldo_desc[id],
0207                              &config);
0208     } else {
0209         ldo->regulator = devm_regulator_register(&pdev->dev,
0210                              &wm8958_ldo_desc[id],
0211                              &config);
0212     }
0213 
0214     if (IS_ERR(ldo->regulator)) {
0215         ret = PTR_ERR(ldo->regulator);
0216         dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
0217             id + 1, ret);
0218         return ret;
0219     }
0220 
0221     platform_set_drvdata(pdev, ldo);
0222 
0223     return 0;
0224 }
0225 
0226 static struct platform_driver wm8994_ldo_driver = {
0227     .probe = wm8994_ldo_probe,
0228     .driver     = {
0229         .name   = "wm8994-ldo",
0230     },
0231 };
0232 
0233 module_platform_driver(wm8994_ldo_driver);
0234 
0235 /* Module information */
0236 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0237 MODULE_DESCRIPTION("WM8994 LDO driver");
0238 MODULE_LICENSE("GPL");
0239 MODULE_ALIAS("platform:wm8994-ldo");