Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
0004  *
0005  * Author: Andrew F. Davis <afd@ti.com>
0006  *
0007  * Based on the TPS65912 driver
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regulator/driver.h>
0014 
0015 #include <linux/mfd/tps65086.h>
0016 
0017 enum tps65086_regulators { BUCK1, BUCK2, BUCK3, BUCK4, BUCK5, BUCK6, LDOA1,
0018     LDOA2, LDOA3, SWA1, SWB1, SWB2, VTT };
0019 
0020 #define TPS65086_REGULATOR(_name, _of, _id, _nv, _vr, _vm, _er, _em, _lr, _dr, _dm) \
0021     [_id] = {                           \
0022         .desc = {                       \
0023             .name           = _name,        \
0024             .of_match       = of_match_ptr(_of),    \
0025             .regulators_node    = "regulators",     \
0026             .of_parse_cb        = tps65086_of_parse_cb, \
0027             .id         = _id,          \
0028             .ops            = &reg_ops,     \
0029             .n_voltages     = _nv,          \
0030             .type           = REGULATOR_VOLTAGE,    \
0031             .owner          = THIS_MODULE,      \
0032             .vsel_reg       = _vr,          \
0033             .vsel_mask      = _vm,          \
0034             .enable_reg     = _er,          \
0035             .enable_mask        = _em,          \
0036             .volt_table     = NULL,         \
0037             .linear_ranges      = _lr,          \
0038             .n_linear_ranges    = ARRAY_SIZE(_lr),  \
0039         },                          \
0040         .decay_reg = _dr,                   \
0041         .decay_mask = _dm,                  \
0042     }
0043 
0044 #define TPS65086_SWITCH(_name, _of, _id, _er, _em)          \
0045     [_id] = {                           \
0046         .desc = {                       \
0047             .name           = _name,        \
0048             .of_match       = of_match_ptr(_of),    \
0049             .regulators_node    = "regulators",     \
0050             .of_parse_cb        = tps65086_of_parse_cb, \
0051             .id         = _id,          \
0052             .ops            = &switch_ops,      \
0053             .type           = REGULATOR_VOLTAGE,    \
0054             .owner          = THIS_MODULE,      \
0055             .enable_reg     = _er,          \
0056             .enable_mask        = _em,          \
0057         },                          \
0058     }
0059 
0060 struct tps65086_regulator {
0061     struct regulator_desc desc;
0062     unsigned int decay_reg;
0063     unsigned int decay_mask;
0064 };
0065 
0066 static const struct linear_range tps65086_10mv_ranges[] = {
0067     REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
0068     REGULATOR_LINEAR_RANGE(410000, 0x1, 0x7F, 10000),
0069 };
0070 
0071 static const struct linear_range tps65086_buck126_25mv_ranges[] = {
0072     REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
0073     REGULATOR_LINEAR_RANGE(1000000, 0x1, 0x18, 0),
0074     REGULATOR_LINEAR_RANGE(1025000, 0x19, 0x7F, 25000),
0075 };
0076 
0077 static const struct linear_range tps65086_buck345_25mv_ranges[] = {
0078     REGULATOR_LINEAR_RANGE(0, 0x0, 0x0, 0),
0079     REGULATOR_LINEAR_RANGE(425000, 0x1, 0x7F, 25000),
0080 };
0081 
0082 static const struct linear_range tps65086_ldoa1_ranges[] = {
0083     REGULATOR_LINEAR_RANGE(1350000, 0x0, 0x0, 0),
0084     REGULATOR_LINEAR_RANGE(1500000, 0x1, 0x7, 100000),
0085     REGULATOR_LINEAR_RANGE(2300000, 0x8, 0xB, 100000),
0086     REGULATOR_LINEAR_RANGE(2850000, 0xC, 0xD, 150000),
0087     REGULATOR_LINEAR_RANGE(3300000, 0xE, 0xE, 0),
0088 };
0089 
0090 static const struct linear_range tps65086_ldoa23_ranges[] = {
0091     REGULATOR_LINEAR_RANGE(700000, 0x0, 0xD, 50000),
0092     REGULATOR_LINEAR_RANGE(1400000, 0xE, 0xF, 100000),
0093 };
0094 
0095 /* Operations permitted on regulators */
0096 static const struct regulator_ops reg_ops = {
0097     .enable         = regulator_enable_regmap,
0098     .disable        = regulator_disable_regmap,
0099     .is_enabled     = regulator_is_enabled_regmap,
0100     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0101     .map_voltage        = regulator_map_voltage_linear_range,
0102     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0103     .list_voltage       = regulator_list_voltage_linear_range,
0104 };
0105 
0106 /* Operations permitted on load switches */
0107 static const struct regulator_ops switch_ops = {
0108     .enable         = regulator_enable_regmap,
0109     .disable        = regulator_disable_regmap,
0110     .is_enabled     = regulator_is_enabled_regmap,
0111 };
0112 
0113 static int tps65086_of_parse_cb(struct device_node *dev,
0114                 const struct regulator_desc *desc,
0115                 struct regulator_config *config);
0116 
0117 static struct tps65086_regulator regulators[] = {
0118     TPS65086_REGULATOR("BUCK1", "buck1", BUCK1, 0x80, TPS65086_BUCK1CTRL,
0119                BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(0),
0120                tps65086_10mv_ranges, TPS65086_BUCK1CTRL,
0121                BIT(0)),
0122     TPS65086_REGULATOR("BUCK2", "buck2", BUCK2, 0x80, TPS65086_BUCK2CTRL,
0123                BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(1),
0124                tps65086_10mv_ranges, TPS65086_BUCK2CTRL,
0125                BIT(0)),
0126     TPS65086_REGULATOR("BUCK3", "buck3", BUCK3, 0x80, TPS65086_BUCK3VID,
0127                BUCK_VID_MASK, TPS65086_BUCK123CTRL, BIT(2),
0128                tps65086_10mv_ranges, TPS65086_BUCK3DECAY,
0129                BIT(0)),
0130     TPS65086_REGULATOR("BUCK4", "buck4", BUCK4, 0x80, TPS65086_BUCK4VID,
0131                BUCK_VID_MASK, TPS65086_BUCK4CTRL, BIT(0),
0132                tps65086_10mv_ranges, TPS65086_BUCK4VID,
0133                BIT(0)),
0134     TPS65086_REGULATOR("BUCK5", "buck5", BUCK5, 0x80, TPS65086_BUCK5VID,
0135                BUCK_VID_MASK, TPS65086_BUCK5CTRL, BIT(0),
0136                tps65086_10mv_ranges, TPS65086_BUCK5CTRL,
0137                BIT(0)),
0138     TPS65086_REGULATOR("BUCK6", "buck6", BUCK6, 0x80, TPS65086_BUCK6VID,
0139                BUCK_VID_MASK, TPS65086_BUCK6CTRL, BIT(0),
0140                tps65086_10mv_ranges, TPS65086_BUCK6CTRL,
0141                BIT(0)),
0142     TPS65086_REGULATOR("LDOA1", "ldoa1", LDOA1, 0xF, TPS65086_LDOA1CTRL,
0143                VDOA1_VID_MASK, TPS65086_LDOA1CTRL, BIT(0),
0144                tps65086_ldoa1_ranges, 0, 0),
0145     TPS65086_REGULATOR("LDOA2", "ldoa2", LDOA2, 0x10, TPS65086_LDOA2VID,
0146                VDOA23_VID_MASK, TPS65086_LDOA2CTRL, BIT(0),
0147                tps65086_ldoa23_ranges, 0, 0),
0148     TPS65086_REGULATOR("LDOA3", "ldoa3", LDOA3, 0x10, TPS65086_LDOA3VID,
0149                VDOA23_VID_MASK, TPS65086_LDOA3CTRL, BIT(0),
0150                tps65086_ldoa23_ranges, 0, 0),
0151     TPS65086_SWITCH("SWA1", "swa1", SWA1, TPS65086_SWVTT_EN, BIT(5)),
0152     TPS65086_SWITCH("SWB1", "swb1", SWB1, TPS65086_SWVTT_EN, BIT(6)),
0153     TPS65086_SWITCH("SWB2", "swb2", SWB2, TPS65086_SWVTT_EN, BIT(7)),
0154     TPS65086_SWITCH("VTT", "vtt", VTT, TPS65086_SWVTT_EN, BIT(4)),
0155 };
0156 
0157 static int tps65086_of_parse_cb(struct device_node *node,
0158                 const struct regulator_desc *desc,
0159                 struct regulator_config *config)
0160 {
0161     int ret;
0162 
0163     /* Check for 25mV step mode */
0164     if (of_property_read_bool(node, "ti,regulator-step-size-25mv")) {
0165         switch (desc->id) {
0166         case BUCK1:
0167         case BUCK2:
0168         case BUCK6:
0169             regulators[desc->id].desc.linear_ranges =
0170                 tps65086_buck126_25mv_ranges;
0171             regulators[desc->id].desc.n_linear_ranges =
0172                 ARRAY_SIZE(tps65086_buck126_25mv_ranges);
0173             break;
0174         case BUCK3:
0175         case BUCK4:
0176         case BUCK5:
0177             regulators[desc->id].desc.linear_ranges =
0178                 tps65086_buck345_25mv_ranges;
0179             regulators[desc->id].desc.n_linear_ranges =
0180                 ARRAY_SIZE(tps65086_buck345_25mv_ranges);
0181             break;
0182         default:
0183             dev_warn(config->dev, "25mV step mode only valid for BUCK regulators\n");
0184         }
0185     }
0186 
0187     /* Check for decay mode */
0188     if (desc->id <= BUCK6 && of_property_read_bool(node, "ti,regulator-decay")) {
0189         ret = regmap_write_bits(config->regmap,
0190                     regulators[desc->id].decay_reg,
0191                     regulators[desc->id].decay_mask,
0192                     regulators[desc->id].decay_mask);
0193         if (ret) {
0194             dev_err(config->dev, "Error setting decay\n");
0195             return ret;
0196         }
0197     }
0198 
0199     return 0;
0200 }
0201 
0202 static int tps65086_regulator_probe(struct platform_device *pdev)
0203 {
0204     struct tps65086 *tps = dev_get_drvdata(pdev->dev.parent);
0205     struct regulator_config config = { };
0206     struct regulator_dev *rdev;
0207     int i;
0208 
0209     platform_set_drvdata(pdev, tps);
0210 
0211     config.dev = &pdev->dev;
0212     config.dev->of_node = tps->dev->of_node;
0213     config.driver_data = tps;
0214     config.regmap = tps->regmap;
0215 
0216     for (i = 0; i < ARRAY_SIZE(regulators); i++) {
0217         rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
0218                            &config);
0219         if (IS_ERR(rdev)) {
0220             dev_err(tps->dev, "failed to register %s regulator\n",
0221                 pdev->name);
0222             return PTR_ERR(rdev);
0223         }
0224     }
0225 
0226     return 0;
0227 }
0228 
0229 static const struct platform_device_id tps65086_regulator_id_table[] = {
0230     { "tps65086-regulator", },
0231     { /* sentinel */ }
0232 };
0233 MODULE_DEVICE_TABLE(platform, tps65086_regulator_id_table);
0234 
0235 static struct platform_driver tps65086_regulator_driver = {
0236     .driver = {
0237         .name = "tps65086-regulator",
0238     },
0239     .probe = tps65086_regulator_probe,
0240     .id_table = tps65086_regulator_id_table,
0241 };
0242 module_platform_driver(tps65086_regulator_driver);
0243 
0244 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
0245 MODULE_DESCRIPTION("TPS65086 Regulator driver");
0246 MODULE_LICENSE("GPL v2");