0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/regmap.h>
0011
0012 #include <linux/mfd/lp87565.h>
0013
0014 enum LP87565_regulator_id {
0015
0016 LP87565_BUCK_0,
0017 LP87565_BUCK_1,
0018 LP87565_BUCK_2,
0019 LP87565_BUCK_3,
0020 LP87565_BUCK_10,
0021 LP87565_BUCK_23,
0022 LP87565_BUCK_3210,
0023 };
0024
0025 #define LP87565_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, \
0026 _er, _em, _ev, _delay, _lr, _cr) \
0027 [_id] = { \
0028 .desc = { \
0029 .name = _name, \
0030 .supply_name = _of "-in", \
0031 .id = _id, \
0032 .of_match = of_match_ptr(_of), \
0033 .regulators_node = of_match_ptr("regulators"),\
0034 .ops = &_ops, \
0035 .n_voltages = _n, \
0036 .type = REGULATOR_VOLTAGE, \
0037 .owner = THIS_MODULE, \
0038 .vsel_reg = _vr, \
0039 .vsel_mask = _vm, \
0040 .enable_reg = _er, \
0041 .enable_mask = _em, \
0042 .enable_val = _ev, \
0043 .ramp_delay = _delay, \
0044 .linear_ranges = _lr, \
0045 .n_linear_ranges = ARRAY_SIZE(_lr), \
0046 .curr_table = lp87565_buck_uA, \
0047 .n_current_limits = ARRAY_SIZE(lp87565_buck_uA),\
0048 .csel_reg = (_cr), \
0049 .csel_mask = LP87565_BUCK_CTRL_2_ILIM, \
0050 }, \
0051 .ctrl2_reg = _cr, \
0052 }
0053
0054 struct lp87565_regulator {
0055 struct regulator_desc desc;
0056 unsigned int ctrl2_reg;
0057 };
0058
0059 static const struct lp87565_regulator regulators[];
0060
0061 static const struct linear_range buck0_1_2_3_ranges[] = {
0062 REGULATOR_LINEAR_RANGE(600000, 0xA, 0x17, 10000),
0063 REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
0064 REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
0065 };
0066
0067 static const unsigned int lp87565_buck_ramp_delay[] = {
0068 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
0069 };
0070
0071
0072 static const unsigned int lp87565_buck_uA[] = {
0073 1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000,
0074 };
0075
0076 static int lp87565_buck_set_ramp_delay(struct regulator_dev *rdev,
0077 int ramp_delay)
0078 {
0079 int id = rdev_get_id(rdev);
0080 unsigned int reg;
0081 int ret;
0082
0083 if (ramp_delay <= 470)
0084 reg = 7;
0085 else if (ramp_delay <= 940)
0086 reg = 6;
0087 else if (ramp_delay <= 1900)
0088 reg = 5;
0089 else if (ramp_delay <= 3800)
0090 reg = 4;
0091 else if (ramp_delay <= 7500)
0092 reg = 3;
0093 else if (ramp_delay <= 10000)
0094 reg = 2;
0095 else if (ramp_delay <= 15000)
0096 reg = 1;
0097 else
0098 reg = 0;
0099
0100 ret = regmap_update_bits(rdev->regmap, regulators[id].ctrl2_reg,
0101 LP87565_BUCK_CTRL_2_SLEW_RATE,
0102 reg << __ffs(LP87565_BUCK_CTRL_2_SLEW_RATE));
0103 if (ret) {
0104 dev_err(&rdev->dev, "SLEW RATE write failed: %d\n", ret);
0105 return ret;
0106 }
0107
0108 rdev->constraints->ramp_delay = lp87565_buck_ramp_delay[reg];
0109
0110
0111 rdev->constraints->ramp_delay =
0112 rdev->constraints->ramp_delay * 85 / 100;
0113
0114 return 0;
0115 }
0116
0117
0118 static const struct regulator_ops lp87565_buck_ops = {
0119 .is_enabled = regulator_is_enabled_regmap,
0120 .enable = regulator_enable_regmap,
0121 .disable = regulator_disable_regmap,
0122 .get_voltage_sel = regulator_get_voltage_sel_regmap,
0123 .set_voltage_sel = regulator_set_voltage_sel_regmap,
0124 .list_voltage = regulator_list_voltage_linear_range,
0125 .map_voltage = regulator_map_voltage_linear_range,
0126 .set_voltage_time_sel = regulator_set_voltage_time_sel,
0127 .set_ramp_delay = lp87565_buck_set_ramp_delay,
0128 .set_current_limit = regulator_set_current_limit_regmap,
0129 .get_current_limit = regulator_get_current_limit_regmap,
0130 };
0131
0132 static const struct lp87565_regulator regulators[] = {
0133 LP87565_REGULATOR("BUCK0", LP87565_BUCK_0, "buck0", lp87565_buck_ops,
0134 256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
0135 LP87565_REG_BUCK0_CTRL_1,
0136 LP87565_BUCK_CTRL_1_EN |
0137 LP87565_BUCK_CTRL_1_EN_PIN_CTRL,
0138 LP87565_BUCK_CTRL_1_EN, 3230,
0139 buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
0140 LP87565_REGULATOR("BUCK1", LP87565_BUCK_1, "buck1", lp87565_buck_ops,
0141 256, LP87565_REG_BUCK1_VOUT, LP87565_BUCK_VSET,
0142 LP87565_REG_BUCK1_CTRL_1,
0143 LP87565_BUCK_CTRL_1_EN |
0144 LP87565_BUCK_CTRL_1_EN_PIN_CTRL,
0145 LP87565_BUCK_CTRL_1_EN, 3230,
0146 buck0_1_2_3_ranges, LP87565_REG_BUCK1_CTRL_2),
0147 LP87565_REGULATOR("BUCK2", LP87565_BUCK_2, "buck2", lp87565_buck_ops,
0148 256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
0149 LP87565_REG_BUCK2_CTRL_1,
0150 LP87565_BUCK_CTRL_1_EN |
0151 LP87565_BUCK_CTRL_1_EN_PIN_CTRL,
0152 LP87565_BUCK_CTRL_1_EN, 3230,
0153 buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
0154 LP87565_REGULATOR("BUCK3", LP87565_BUCK_3, "buck3", lp87565_buck_ops,
0155 256, LP87565_REG_BUCK3_VOUT, LP87565_BUCK_VSET,
0156 LP87565_REG_BUCK3_CTRL_1,
0157 LP87565_BUCK_CTRL_1_EN |
0158 LP87565_BUCK_CTRL_1_EN_PIN_CTRL,
0159 LP87565_BUCK_CTRL_1_EN, 3230,
0160 buck0_1_2_3_ranges, LP87565_REG_BUCK3_CTRL_2),
0161 LP87565_REGULATOR("BUCK10", LP87565_BUCK_10, "buck10", lp87565_buck_ops,
0162 256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
0163 LP87565_REG_BUCK0_CTRL_1,
0164 LP87565_BUCK_CTRL_1_EN |
0165 LP87565_BUCK_CTRL_1_EN_PIN_CTRL |
0166 LP87565_BUCK_CTRL_1_FPWM_MP_0_2,
0167 LP87565_BUCK_CTRL_1_EN |
0168 LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
0169 buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
0170 LP87565_REGULATOR("BUCK23", LP87565_BUCK_23, "buck23", lp87565_buck_ops,
0171 256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
0172 LP87565_REG_BUCK2_CTRL_1,
0173 LP87565_BUCK_CTRL_1_EN |
0174 LP87565_BUCK_CTRL_1_EN_PIN_CTRL,
0175 LP87565_BUCK_CTRL_1_EN, 3230,
0176 buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
0177 LP87565_REGULATOR("BUCK3210", LP87565_BUCK_3210, "buck3210",
0178 lp87565_buck_ops, 256, LP87565_REG_BUCK0_VOUT,
0179 LP87565_BUCK_VSET, LP87565_REG_BUCK0_CTRL_1,
0180 LP87565_BUCK_CTRL_1_EN |
0181 LP87565_BUCK_CTRL_1_EN_PIN_CTRL |
0182 LP87565_BUCK_CTRL_1_FPWM_MP_0_2,
0183 LP87565_BUCK_CTRL_1_EN |
0184 LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
0185 buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
0186 };
0187
0188 static int lp87565_regulator_probe(struct platform_device *pdev)
0189 {
0190 struct lp87565 *lp87565 = dev_get_drvdata(pdev->dev.parent);
0191 struct regulator_config config = { };
0192 struct regulator_dev *rdev;
0193 int i, min_idx, max_idx;
0194
0195 platform_set_drvdata(pdev, lp87565);
0196
0197 config.dev = &pdev->dev;
0198 config.dev->of_node = lp87565->dev->of_node;
0199 config.driver_data = lp87565;
0200 config.regmap = lp87565->regmap;
0201
0202 switch (lp87565->dev_type) {
0203 case LP87565_DEVICE_TYPE_LP87565_Q1:
0204 min_idx = LP87565_BUCK_10;
0205 max_idx = LP87565_BUCK_23;
0206 break;
0207 case LP87565_DEVICE_TYPE_LP87561_Q1:
0208 min_idx = LP87565_BUCK_3210;
0209 max_idx = LP87565_BUCK_3210;
0210 break;
0211 default:
0212 min_idx = LP87565_BUCK_0;
0213 max_idx = LP87565_BUCK_3;
0214 break;
0215 }
0216
0217 for (i = min_idx; i <= max_idx; i++) {
0218 rdev = devm_regulator_register(&pdev->dev, ®ulators[i].desc,
0219 &config);
0220 if (IS_ERR(rdev)) {
0221 dev_err(lp87565->dev, "failed to register %s regulator\n",
0222 pdev->name);
0223 return PTR_ERR(rdev);
0224 }
0225 }
0226
0227 return 0;
0228 }
0229
0230 static const struct platform_device_id lp87565_regulator_id_table[] = {
0231 { "lp87565-regulator", },
0232 { "lp87565-q1-regulator", },
0233 { }
0234 };
0235 MODULE_DEVICE_TABLE(platform, lp87565_regulator_id_table);
0236
0237 static struct platform_driver lp87565_regulator_driver = {
0238 .driver = {
0239 .name = "lp87565-pmic",
0240 },
0241 .probe = lp87565_regulator_probe,
0242 .id_table = lp87565_regulator_id_table,
0243 };
0244 module_platform_driver(lp87565_regulator_driver);
0245
0246 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
0247 MODULE_DESCRIPTION("LP87565 voltage regulator driver");
0248 MODULE_LICENSE("GPL v2");