Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Regulator driver for TPS68470 PMIC
0004 //
0005 // Copyright (c) 2021 Red Hat Inc.
0006 // Copyright (C) 2018 Intel Corporation
0007 //
0008 // Authors:
0009 //  Hans de Goede <hdegoede@redhat.com>
0010 //  Zaikuo Wang <zaikuo.wang@intel.com>
0011 //  Tianshu Qiu <tian.shu.qiu@intel.com>
0012 //  Jian Xu Zheng <jian.xu.zheng@intel.com>
0013 //  Yuning Pu <yuning.pu@intel.com>
0014 //  Rajmohan Mani <rajmohan.mani@intel.com>
0015 
0016 #include <linux/clk.h>
0017 #include <linux/device.h>
0018 #include <linux/err.h>
0019 #include <linux/init.h>
0020 #include <linux/kernel.h>
0021 #include <linux/mfd/tps68470.h>
0022 #include <linux/module.h>
0023 #include <linux/platform_data/tps68470.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/regulator/driver.h>
0026 #include <linux/regulator/machine.h>
0027 
0028 struct tps68470_regulator_data {
0029     struct clk *clk;
0030 };
0031 
0032 #define TPS68470_REGULATOR(_name, _id, _ops, _n,            \
0033                _vr, _vm, _er, _em, _lr, _nlr)       \
0034     [TPS68470_ ## _name] = {                    \
0035         .name           = # _name,          \
0036         .id         = _id,              \
0037         .ops            = &_ops,            \
0038         .n_voltages     = _n,               \
0039         .type           = REGULATOR_VOLTAGE,        \
0040         .owner          = THIS_MODULE,          \
0041         .vsel_reg       = _vr,              \
0042         .vsel_mask      = _vm,              \
0043         .enable_reg     = _er,              \
0044         .enable_mask        = _em,              \
0045         .linear_ranges      = _lr,              \
0046         .n_linear_ranges    = _nlr,             \
0047     }
0048 
0049 static const struct linear_range tps68470_ldo_ranges[] = {
0050     REGULATOR_LINEAR_RANGE(875000, 0, 125, 17800),
0051 };
0052 
0053 static const struct linear_range tps68470_core_ranges[] = {
0054     REGULATOR_LINEAR_RANGE(900000, 0, 42, 25000),
0055 };
0056 
0057 static int tps68470_regulator_enable(struct regulator_dev *rdev)
0058 {
0059     struct tps68470_regulator_data *data = rdev->reg_data;
0060     int ret;
0061 
0062     /* The Core buck regulator needs the PMIC's PLL to be enabled */
0063     if (rdev->desc->id == TPS68470_CORE) {
0064         ret = clk_prepare_enable(data->clk);
0065         if (ret) {
0066             dev_err(&rdev->dev, "Error enabling TPS68470 clock\n");
0067             return ret;
0068         }
0069     }
0070 
0071     return regulator_enable_regmap(rdev);
0072 }
0073 
0074 static int tps68470_regulator_disable(struct regulator_dev *rdev)
0075 {
0076     struct tps68470_regulator_data *data = rdev->reg_data;
0077 
0078     if (rdev->desc->id == TPS68470_CORE)
0079         clk_disable_unprepare(data->clk);
0080 
0081     return regulator_disable_regmap(rdev);
0082 }
0083 
0084 /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
0085 static const struct regulator_ops tps68470_regulator_ops = {
0086     .is_enabled     = regulator_is_enabled_regmap,
0087     .enable         = tps68470_regulator_enable,
0088     .disable        = tps68470_regulator_disable,
0089     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0090     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0091     .list_voltage       = regulator_list_voltage_linear_range,
0092     .map_voltage        = regulator_map_voltage_linear_range,
0093 };
0094 
0095 static const struct regulator_ops tps68470_always_on_reg_ops = {
0096     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0097     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0098     .list_voltage       = regulator_list_voltage_linear_range,
0099     .map_voltage        = regulator_map_voltage_linear_range,
0100 };
0101 
0102 static const struct regulator_desc regulators[] = {
0103     TPS68470_REGULATOR(CORE, TPS68470_CORE, tps68470_regulator_ops, 43,
0104                TPS68470_REG_VDVAL, TPS68470_VDVAL_DVOLT_MASK,
0105                TPS68470_REG_VDCTL, TPS68470_VDCTL_EN_MASK,
0106                tps68470_core_ranges, ARRAY_SIZE(tps68470_core_ranges)),
0107     TPS68470_REGULATOR(ANA, TPS68470_ANA, tps68470_regulator_ops, 126,
0108                TPS68470_REG_VAVAL, TPS68470_VAVAL_AVOLT_MASK,
0109                TPS68470_REG_VACTL, TPS68470_VACTL_EN_MASK,
0110                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0111     TPS68470_REGULATOR(VCM, TPS68470_VCM, tps68470_regulator_ops, 126,
0112                TPS68470_REG_VCMVAL, TPS68470_VCMVAL_VCVOLT_MASK,
0113                TPS68470_REG_VCMCTL, TPS68470_VCMCTL_EN_MASK,
0114                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0115     TPS68470_REGULATOR(VIO, TPS68470_VIO, tps68470_always_on_reg_ops, 126,
0116                TPS68470_REG_VIOVAL, TPS68470_VIOVAL_IOVOLT_MASK,
0117                0, 0,
0118                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0119 /*
0120  * (1) This regulator must have the same voltage as VIO if S_IO LDO is used to
0121  *     power a sensor/VCM which I2C is daisy chained behind the PMIC.
0122  * (2) If there is no I2C daisy chain it can be set freely.
0123  */
0124     TPS68470_REGULATOR(VSIO, TPS68470_VSIO, tps68470_regulator_ops, 126,
0125                TPS68470_REG_VSIOVAL, TPS68470_VSIOVAL_IOVOLT_MASK,
0126                TPS68470_REG_S_I2C_CTL, TPS68470_S_I2C_CTL_EN_MASK,
0127                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0128     TPS68470_REGULATOR(AUX1, TPS68470_AUX1, tps68470_regulator_ops, 126,
0129                TPS68470_REG_VAUX1VAL, TPS68470_VAUX1VAL_AUX1VOLT_MASK,
0130                TPS68470_REG_VAUX1CTL, TPS68470_VAUX1CTL_EN_MASK,
0131                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0132     TPS68470_REGULATOR(AUX2, TPS68470_AUX2, tps68470_regulator_ops, 126,
0133                TPS68470_REG_VAUX2VAL, TPS68470_VAUX2VAL_AUX2VOLT_MASK,
0134                TPS68470_REG_VAUX2CTL, TPS68470_VAUX2CTL_EN_MASK,
0135                tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
0136 };
0137 
0138 static int tps68470_regulator_probe(struct platform_device *pdev)
0139 {
0140     struct device *dev = &pdev->dev;
0141     struct tps68470_regulator_platform_data *pdata = dev_get_platdata(dev);
0142     struct tps68470_regulator_data *data;
0143     struct regulator_config config = { };
0144     struct regulator_dev *rdev;
0145     int i;
0146 
0147     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0148     if (!data)
0149         return -ENOMEM;
0150 
0151     data->clk = devm_clk_get(dev, "tps68470-clk");
0152     if (IS_ERR(data->clk))
0153         return dev_err_probe(dev, PTR_ERR(data->clk), "getting tps68470-clk\n");
0154 
0155     config.dev = dev->parent;
0156     config.regmap = dev_get_drvdata(dev->parent);
0157     config.driver_data = data;
0158 
0159     for (i = 0; i < TPS68470_NUM_REGULATORS; i++) {
0160         if (pdata)
0161             config.init_data = pdata->reg_init_data[i];
0162         else
0163             config.init_data = NULL;
0164 
0165         rdev = devm_regulator_register(dev, &regulators[i], &config);
0166         if (IS_ERR(rdev))
0167             return dev_err_probe(dev, PTR_ERR(rdev),
0168                          "registering %s regulator\n",
0169                          regulators[i].name);
0170     }
0171 
0172     return 0;
0173 }
0174 
0175 static struct platform_driver tps68470_regulator_driver = {
0176     .driver = {
0177         .name = "tps68470-regulator",
0178     },
0179     .probe = tps68470_regulator_probe,
0180 };
0181 
0182 /*
0183  * The ACPI tps68470 probe-ordering depends on the clk/gpio/regulator drivers
0184  * registering before the drivers for the camera-sensors which use them bind.
0185  * subsys_initcall() ensures this when the drivers are builtin.
0186  */
0187 static int __init tps68470_regulator_init(void)
0188 {
0189     return platform_driver_register(&tps68470_regulator_driver);
0190 }
0191 subsys_initcall(tps68470_regulator_init);
0192 
0193 static void __exit tps68470_regulator_exit(void)
0194 {
0195     platform_driver_unregister(&tps68470_regulator_driver);
0196 }
0197 module_exit(tps68470_regulator_exit);
0198 
0199 MODULE_ALIAS("platform:tps68470-regulator");
0200 MODULE_DESCRIPTION("TPS68470 voltage regulator driver");
0201 MODULE_LICENSE("GPL v2");