Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies
0004  *
0005  * Copyright (C) 2012 Renesas Electronics Corporation
0006  * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
0007  */
0008 
0009 #include <linux/err.h>
0010 #include <linux/init.h>
0011 #include <linux/mfd/as3711.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regmap.h>
0016 #include <linux/regulator/driver.h>
0017 #include <linux/regulator/of_regulator.h>
0018 #include <linux/slab.h>
0019 
0020 /*
0021  * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and
0022  * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes:
0023  * FAST:    sdX_fast=1
0024  * NORMAL:  low_noise=1
0025  * IDLE:    low_noise=0
0026  */
0027 
0028 static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode)
0029 {
0030     unsigned int fast_bit = rdev->desc->enable_mask,
0031         low_noise_bit = fast_bit << 4;
0032     u8 val;
0033 
0034     switch (mode) {
0035     case REGULATOR_MODE_FAST:
0036         val = fast_bit | low_noise_bit;
0037         break;
0038     case REGULATOR_MODE_NORMAL:
0039         val = low_noise_bit;
0040         break;
0041     case REGULATOR_MODE_IDLE:
0042         val = 0;
0043         break;
0044     default:
0045         return -EINVAL;
0046     }
0047 
0048     return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1,
0049                   low_noise_bit | fast_bit, val);
0050 }
0051 
0052 static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev)
0053 {
0054     unsigned int fast_bit = rdev->desc->enable_mask,
0055         low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit;
0056     unsigned int val;
0057     int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val);
0058 
0059     if (ret < 0)
0060         return ret;
0061 
0062     if ((val & mask) == mask)
0063         return REGULATOR_MODE_FAST;
0064 
0065     if ((val & mask) == low_noise_bit)
0066         return REGULATOR_MODE_NORMAL;
0067 
0068     if (!(val & mask))
0069         return REGULATOR_MODE_IDLE;
0070 
0071     return -EINVAL;
0072 }
0073 
0074 static const struct regulator_ops as3711_sd_ops = {
0075     .is_enabled     = regulator_is_enabled_regmap,
0076     .enable         = regulator_enable_regmap,
0077     .disable        = regulator_disable_regmap,
0078     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0079     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0080     .list_voltage       = regulator_list_voltage_linear_range,
0081     .map_voltage        = regulator_map_voltage_linear_range,
0082     .get_mode       = as3711_get_mode_sd,
0083     .set_mode       = as3711_set_mode_sd,
0084 };
0085 
0086 static const struct regulator_ops as3711_aldo_ops = {
0087     .is_enabled     = regulator_is_enabled_regmap,
0088     .enable         = regulator_enable_regmap,
0089     .disable        = regulator_disable_regmap,
0090     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0091     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0092     .list_voltage       = regulator_list_voltage_linear_range,
0093     .map_voltage        = regulator_map_voltage_linear_range,
0094 };
0095 
0096 static const struct regulator_ops as3711_dldo_ops = {
0097     .is_enabled     = regulator_is_enabled_regmap,
0098     .enable         = regulator_enable_regmap,
0099     .disable        = regulator_disable_regmap,
0100     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0101     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0102     .list_voltage       = regulator_list_voltage_linear_range,
0103     .map_voltage        = regulator_map_voltage_linear_range,
0104 };
0105 
0106 static const struct linear_range as3711_sd_ranges[] = {
0107     REGULATOR_LINEAR_RANGE(612500, 0x1, 0x40, 12500),
0108     REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
0109     REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7f, 50000),
0110 };
0111 
0112 static const struct linear_range as3711_aldo_ranges[] = {
0113     REGULATOR_LINEAR_RANGE(1200000, 0, 0xf, 50000),
0114     REGULATOR_LINEAR_RANGE(1800000, 0x10, 0x1f, 100000),
0115 };
0116 
0117 static const struct linear_range as3711_dldo_ranges[] = {
0118     REGULATOR_LINEAR_RANGE(900000, 0, 0x10, 50000),
0119     REGULATOR_LINEAR_RANGE(1750000, 0x20, 0x3f, 50000),
0120 };
0121 
0122 #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _sfx)            \
0123     [AS3711_REGULATOR_ ## _id] = {                     \
0124         .name = "as3711-regulator-" # _id,             \
0125         .id = AS3711_REGULATOR_ ## _id,                \
0126         .n_voltages = (_vmask + 1),                \
0127         .ops = &as3711_ ## _sfx ## _ops,               \
0128         .type = REGULATOR_VOLTAGE,                 \
0129         .owner = THIS_MODULE,                      \
0130         .vsel_reg = AS3711_ ## _id ## _VOLTAGE,            \
0131         .vsel_mask = _vmask,                       \
0132         .enable_reg = AS3711_ ## _en_reg,              \
0133         .enable_mask = BIT(_en_bit),                   \
0134         .linear_ranges = as3711_ ## _sfx ## _ranges,           \
0135         .n_linear_ranges = ARRAY_SIZE(as3711_ ## _sfx ## _ranges), \
0136 }
0137 
0138 static const struct regulator_desc as3711_reg_desc[] = {
0139     AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, sd),
0140     AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, sd),
0141     AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, sd),
0142     AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, sd),
0143     AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, aldo),
0144     AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, aldo),
0145     AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, dldo),
0146     AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, dldo),
0147     AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, dldo),
0148     AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, dldo),
0149     AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, dldo),
0150     AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, dldo),
0151     /* StepUp output voltage depends on supplying regulator */
0152 };
0153 
0154 #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_desc)
0155 
0156 static struct of_regulator_match
0157 as3711_regulator_matches[AS3711_REGULATOR_NUM] = {
0158     [AS3711_REGULATOR_SD_1] = { .name = "sd1" },
0159     [AS3711_REGULATOR_SD_2] = { .name = "sd2" },
0160     [AS3711_REGULATOR_SD_3] = { .name = "sd3" },
0161     [AS3711_REGULATOR_SD_4] = { .name = "sd4" },
0162     [AS3711_REGULATOR_LDO_1] = { .name = "ldo1" },
0163     [AS3711_REGULATOR_LDO_2] = { .name = "ldo2" },
0164     [AS3711_REGULATOR_LDO_3] = { .name = "ldo3" },
0165     [AS3711_REGULATOR_LDO_4] = { .name = "ldo4" },
0166     [AS3711_REGULATOR_LDO_5] = { .name = "ldo5" },
0167     [AS3711_REGULATOR_LDO_6] = { .name = "ldo6" },
0168     [AS3711_REGULATOR_LDO_7] = { .name = "ldo7" },
0169     [AS3711_REGULATOR_LDO_8] = { .name = "ldo8" },
0170 };
0171 
0172 static int as3711_regulator_parse_dt(struct device *dev,
0173                 struct device_node **of_node, const int count)
0174 {
0175     struct as3711_regulator_pdata *pdata = dev_get_platdata(dev);
0176     struct device_node *regulators =
0177         of_get_child_by_name(dev->parent->of_node, "regulators");
0178     struct of_regulator_match *match;
0179     int ret, i;
0180 
0181     if (!regulators) {
0182         dev_err(dev, "regulator node not found\n");
0183         return -ENODEV;
0184     }
0185 
0186     ret = of_regulator_match(dev->parent, regulators,
0187                  as3711_regulator_matches, count);
0188     of_node_put(regulators);
0189     if (ret < 0) {
0190         dev_err(dev, "Error parsing regulator init data: %d\n", ret);
0191         return ret;
0192     }
0193 
0194     for (i = 0, match = as3711_regulator_matches; i < count; i++, match++)
0195         if (match->of_node) {
0196             pdata->init_data[i] = match->init_data;
0197             of_node[i] = match->of_node;
0198         }
0199 
0200     return 0;
0201 }
0202 
0203 static int as3711_regulator_probe(struct platform_device *pdev)
0204 {
0205     struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev);
0206     struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
0207     struct regulator_config config = {.dev = &pdev->dev,};
0208     struct device_node *of_node[AS3711_REGULATOR_NUM] = {};
0209     struct regulator_dev *rdev;
0210     int ret;
0211     int id;
0212 
0213     if (!pdata) {
0214         dev_err(&pdev->dev, "No platform data...\n");
0215         return -ENODEV;
0216     }
0217 
0218     if (pdev->dev.parent->of_node) {
0219         ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM);
0220         if (ret < 0) {
0221             dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
0222             return ret;
0223         }
0224     }
0225 
0226     for (id = 0; id < AS3711_REGULATOR_NUM; id++) {
0227         config.init_data = pdata->init_data[id];
0228         config.regmap = as3711->regmap;
0229         config.of_node = of_node[id];
0230 
0231         rdev = devm_regulator_register(&pdev->dev, &as3711_reg_desc[id],
0232                            &config);
0233         if (IS_ERR(rdev)) {
0234             dev_err(&pdev->dev, "Failed to register regulator %s\n",
0235                 as3711_reg_desc[id].name);
0236             return PTR_ERR(rdev);
0237         }
0238     }
0239 
0240     return 0;
0241 }
0242 
0243 static struct platform_driver as3711_regulator_driver = {
0244     .driver = {
0245         .name   = "as3711-regulator",
0246     },
0247     .probe      = as3711_regulator_probe,
0248 };
0249 
0250 static int __init as3711_regulator_init(void)
0251 {
0252     return platform_driver_register(&as3711_regulator_driver);
0253 }
0254 subsys_initcall(as3711_regulator_init);
0255 
0256 static void __exit as3711_regulator_exit(void)
0257 {
0258     platform_driver_unregister(&as3711_regulator_driver);
0259 }
0260 module_exit(as3711_regulator_exit);
0261 
0262 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
0263 MODULE_DESCRIPTION("AS3711 regulator driver");
0264 MODULE_ALIAS("platform:as3711-regulator");
0265 MODULE_LICENSE("GPL v2");