Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <linux/mfd/sm5703.h>
0004 #include <linux/module.h>
0005 #include <linux/mod_devicetable.h>
0006 #include <linux/platform_device.h>
0007 #include <linux/regmap.h>
0008 #include <linux/regulator/driver.h>
0009 #include <linux/regulator/of_regulator.h>
0010 
0011 enum sm5703_regulators {
0012     SM5703_BUCK,
0013     SM5703_LDO1,
0014     SM5703_LDO2,
0015     SM5703_LDO3,
0016     SM5703_USBLDO1,
0017     SM5703_USBLDO2,
0018     SM5703_VBUS,
0019     SM5703_MAX_REGULATORS,
0020 };
0021 
0022 static const int sm5703_ldo_voltagemap[] = {
0023     1500000, 1800000, 2600000, 2800000, 3000000, 3300000,
0024 };
0025 
0026 static const int sm5703_buck_voltagemap[] = {
0027     1000000, 1000000, 1000000, 1000000,
0028     1000000, 1000000, 1000000, 1000000,
0029     1000000, 1000000, 1000000, 1100000,
0030     1200000, 1300000, 1400000, 1500000,
0031     1600000, 1700000, 1800000, 1900000,
0032     2000000, 2100000, 2200000, 2300000,
0033     2400000, 2500000, 2600000, 2700000,
0034     2800000, 2900000, 3000000, 3000000,
0035 };
0036 
0037 #define SM5703USBLDO(_name, _id)                    \
0038     [SM5703_USBLDO ## _id] = {                  \
0039         .name = _name,                      \
0040         .of_match = _name,                  \
0041         .regulators_node = "regulators",            \
0042         .type = REGULATOR_VOLTAGE,              \
0043         .id = SM5703_USBLDO ## _id,             \
0044         .ops = &sm5703_regulator_ops_fixed,         \
0045         .fixed_uV = SM5703_USBLDO_MICROVOLT,            \
0046         .enable_reg = SM5703_REG_USBLDO12,          \
0047         .enable_mask = SM5703_REG_EN_USBLDO ##_id,      \
0048         .owner          = THIS_MODULE,          \
0049     }
0050 
0051 #define SM5703VBUS(_name)                       \
0052     [SM5703_VBUS] = {                       \
0053         .name = _name,                      \
0054         .of_match = _name,                  \
0055         .regulators_node = "regulators",            \
0056         .type = REGULATOR_VOLTAGE,              \
0057         .id = SM5703_VBUS,                  \
0058         .ops = &sm5703_regulator_ops_fixed,         \
0059         .fixed_uV = SM5703_VBUS_MICROVOLT,          \
0060         .enable_reg = SM5703_REG_CNTL,              \
0061         .enable_mask = SM5703_OPERATION_MODE_MASK,      \
0062         .enable_val = SM5703_OPERATION_MODE_USB_OTG_MODE,   \
0063         .disable_val = SM5703_OPERATION_MODE_CHARGING_ON,   \
0064         .owner          = THIS_MODULE,          \
0065     }
0066 
0067 #define SM5703BUCK(_name)                       \
0068     [SM5703_BUCK] = {                       \
0069         .name = _name,                      \
0070         .of_match = _name,                  \
0071         .regulators_node = "regulators",            \
0072         .type = REGULATOR_VOLTAGE,              \
0073         .id = SM5703_BUCK,                  \
0074         .ops = &sm5703_regulator_ops,               \
0075         .n_voltages = ARRAY_SIZE(sm5703_buck_voltagemap),   \
0076         .volt_table = sm5703_buck_voltagemap,           \
0077         .vsel_reg = SM5703_REG_BUCK,                \
0078         .vsel_mask = SM5703_BUCK_VOLT_MASK,         \
0079         .enable_reg = SM5703_REG_BUCK,              \
0080         .enable_mask = SM5703_REG_EN_BUCK,          \
0081         .owner          = THIS_MODULE,          \
0082     }
0083 
0084 #define SM5703LDO(_name, _id)                       \
0085     [SM5703_LDO ## _id] = {                     \
0086         .name = _name,                      \
0087         .of_match = _name,                  \
0088         .regulators_node = "regulators",            \
0089         .type = REGULATOR_VOLTAGE,              \
0090         .id = SM5703_LDO ## _id,                \
0091         .ops = &sm5703_regulator_ops,               \
0092         .n_voltages = ARRAY_SIZE(sm5703_ldo_voltagemap),    \
0093         .volt_table = sm5703_ldo_voltagemap,            \
0094         .vsel_reg = SM5703_REG_LDO ##_id,           \
0095         .vsel_mask = SM5703_LDO_VOLT_MASK,          \
0096         .enable_reg = SM5703_REG_LDO ##_id,         \
0097         .enable_mask = SM5703_LDO_EN,               \
0098         .owner          = THIS_MODULE,          \
0099     }
0100 
0101 static const struct regulator_ops sm5703_regulator_ops = {
0102     .enable         = regulator_enable_regmap,
0103     .disable        = regulator_disable_regmap,
0104     .is_enabled     = regulator_is_enabled_regmap,
0105     .list_voltage       = regulator_list_voltage_table,
0106     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0107     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0108 };
0109 
0110 static const struct regulator_ops sm5703_regulator_ops_fixed = {
0111     .enable         = regulator_enable_regmap,
0112     .disable        = regulator_disable_regmap,
0113     .is_enabled     = regulator_is_enabled_regmap,
0114 };
0115 
0116 static struct regulator_desc sm5703_regulators_desc[SM5703_MAX_REGULATORS] = {
0117     SM5703BUCK("buck"),
0118     SM5703LDO("ldo1", 1),
0119     SM5703LDO("ldo2", 2),
0120     SM5703LDO("ldo3", 3),
0121     SM5703USBLDO("usbldo1", 1),
0122     SM5703USBLDO("usbldo2", 2),
0123     SM5703VBUS("vbus"),
0124 };
0125 
0126 static int sm5703_regulator_probe(struct platform_device *pdev)
0127 {
0128     struct device *dev = &pdev->dev;
0129     struct regulator_config config = { NULL, };
0130     struct regulator_dev *rdev;
0131     struct sm5703_dev *sm5703 = dev_get_drvdata(pdev->dev.parent);
0132     int i;
0133 
0134     config.dev = dev->parent;
0135     config.regmap = sm5703->regmap;
0136 
0137     for (i = 0; i < SM5703_MAX_REGULATORS; i++) {
0138         rdev = devm_regulator_register(dev,
0139                            &sm5703_regulators_desc[i],
0140                            &config);
0141         if (IS_ERR(rdev))
0142             return dev_err_probe(dev, PTR_ERR(rdev),
0143                          "Failed to register a regulator\n");
0144     }
0145 
0146     return 0;
0147 }
0148 
0149 static const struct platform_device_id sm5703_regulator_id[] = {
0150     { "sm5703-regulator", 0 },
0151     {}
0152 };
0153 MODULE_DEVICE_TABLE(platform, sm5703_regulator_id);
0154 
0155 static struct platform_driver sm5703_regulator_driver = {
0156     .driver = {
0157         .name = "sm5703-regulator",
0158     },
0159     .probe  = sm5703_regulator_probe,
0160     .id_table   = sm5703_regulator_id,
0161 };
0162 
0163 module_platform_driver(sm5703_regulator_driver);
0164 
0165 MODULE_DESCRIPTION("Silicon Mitus SM5703 LDO/Buck/USB regulator driver");
0166 MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>");
0167 MODULE_LICENSE("GPL");