1abd46274SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2994aae32SKeerthy /*
3994aae32SKeerthy * Regulator driver for LP873X PMIC
4994aae32SKeerthy *
52ca76b3eSAlexander A. Klimov * Copyright (C) 2016 Texas Instruments Incorporated - https://www.ti.com/
6994aae32SKeerthy */
7994aae32SKeerthy
8994aae32SKeerthy #include <linux/module.h>
9994aae32SKeerthy #include <linux/platform_device.h>
10994aae32SKeerthy #include <linux/regmap.h>
11994aae32SKeerthy
12994aae32SKeerthy #include <linux/mfd/lp873x.h>
13994aae32SKeerthy
14994aae32SKeerthy #define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
154335f653SAxel Lin _delay, _lr, _cr) \
16994aae32SKeerthy [_id] = { \
17994aae32SKeerthy .desc = { \
18994aae32SKeerthy .name = _name, \
19205321f0SLokesh Vutla .supply_name = _of "-in", \
20994aae32SKeerthy .id = _id, \
21994aae32SKeerthy .of_match = of_match_ptr(_of), \
22994aae32SKeerthy .regulators_node = of_match_ptr("regulators"),\
23994aae32SKeerthy .ops = &_ops, \
24994aae32SKeerthy .n_voltages = _n, \
25994aae32SKeerthy .type = REGULATOR_VOLTAGE, \
26994aae32SKeerthy .owner = THIS_MODULE, \
27994aae32SKeerthy .vsel_reg = _vr, \
28994aae32SKeerthy .vsel_mask = _vm, \
29994aae32SKeerthy .enable_reg = _er, \
30994aae32SKeerthy .enable_mask = _em, \
31994aae32SKeerthy .ramp_delay = _delay, \
32994aae32SKeerthy .linear_ranges = _lr, \
334335f653SAxel Lin .n_linear_ranges = ARRAY_SIZE(_lr), \
34be6230c3SAxel Lin .curr_table = lp873x_buck_uA, \
35be6230c3SAxel Lin .n_current_limits = ARRAY_SIZE(lp873x_buck_uA), \
36be6230c3SAxel Lin .csel_reg = (_cr), \
37be6230c3SAxel Lin .csel_mask = LP873X_BUCK0_CTRL_2_BUCK0_ILIM,\
38994aae32SKeerthy }, \
39994aae32SKeerthy .ctrl2_reg = _cr, \
40994aae32SKeerthy }
41994aae32SKeerthy
42994aae32SKeerthy struct lp873x_regulator {
43994aae32SKeerthy struct regulator_desc desc;
44994aae32SKeerthy unsigned int ctrl2_reg;
45994aae32SKeerthy };
46994aae32SKeerthy
47994aae32SKeerthy static const struct lp873x_regulator regulators[];
48994aae32SKeerthy
4960ab7f41SMatti Vaittinen static const struct linear_range buck0_buck1_ranges[] = {
50994aae32SKeerthy REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0),
51994aae32SKeerthy REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000),
52994aae32SKeerthy REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
53994aae32SKeerthy REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
54994aae32SKeerthy };
55994aae32SKeerthy
5660ab7f41SMatti Vaittinen static const struct linear_range ldo0_ldo1_ranges[] = {
57994aae32SKeerthy REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000),
58994aae32SKeerthy };
59994aae32SKeerthy
60921b2b3aSAxel Lin static const unsigned int lp873x_buck_ramp_delay[] = {
61994aae32SKeerthy 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
62994aae32SKeerthy };
63994aae32SKeerthy
64994aae32SKeerthy /* LP873X BUCK current limit */
65994aae32SKeerthy static const unsigned int lp873x_buck_uA[] = {
66994aae32SKeerthy 1500000, 2000000, 2500000, 3000000, 3500000, 4000000,
67994aae32SKeerthy };
68994aae32SKeerthy
lp873x_buck_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)69994aae32SKeerthy static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev,
70994aae32SKeerthy int ramp_delay)
71994aae32SKeerthy {
72994aae32SKeerthy int id = rdev_get_id(rdev);
73994aae32SKeerthy struct lp873x *lp873 = rdev_get_drvdata(rdev);
74994aae32SKeerthy unsigned int reg;
75994aae32SKeerthy int ret;
76994aae32SKeerthy
77994aae32SKeerthy if (ramp_delay <= 470)
78994aae32SKeerthy reg = 7;
79994aae32SKeerthy else if (ramp_delay <= 940)
80994aae32SKeerthy reg = 6;
81994aae32SKeerthy else if (ramp_delay <= 1900)
82994aae32SKeerthy reg = 5;
83994aae32SKeerthy else if (ramp_delay <= 3800)
84994aae32SKeerthy reg = 4;
85994aae32SKeerthy else if (ramp_delay <= 7500)
86994aae32SKeerthy reg = 3;
87994aae32SKeerthy else if (ramp_delay <= 10000)
88994aae32SKeerthy reg = 2;
89994aae32SKeerthy else if (ramp_delay <= 15000)
90994aae32SKeerthy reg = 1;
91994aae32SKeerthy else
92994aae32SKeerthy reg = 0;
93994aae32SKeerthy
94994aae32SKeerthy ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg,
95994aae32SKeerthy LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE,
96994aae32SKeerthy reg << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE));
97994aae32SKeerthy if (ret) {
98994aae32SKeerthy dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret);
99994aae32SKeerthy return ret;
100994aae32SKeerthy }
101994aae32SKeerthy
102994aae32SKeerthy rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg];
103994aae32SKeerthy
104994aae32SKeerthy return 0;
105994aae32SKeerthy }
106994aae32SKeerthy
107994aae32SKeerthy /* Operations permitted on BUCK0, BUCK1 */
108699bdc23SAxel Lin static const struct regulator_ops lp873x_buck01_ops = {
109994aae32SKeerthy .is_enabled = regulator_is_enabled_regmap,
110994aae32SKeerthy .enable = regulator_enable_regmap,
111994aae32SKeerthy .disable = regulator_disable_regmap,
112994aae32SKeerthy .get_voltage_sel = regulator_get_voltage_sel_regmap,
113994aae32SKeerthy .set_voltage_sel = regulator_set_voltage_sel_regmap,
114994aae32SKeerthy .list_voltage = regulator_list_voltage_linear_range,
115994aae32SKeerthy .map_voltage = regulator_map_voltage_linear_range,
116994aae32SKeerthy .set_voltage_time_sel = regulator_set_voltage_time_sel,
117994aae32SKeerthy .set_ramp_delay = lp873x_buck_set_ramp_delay,
118be6230c3SAxel Lin .set_current_limit = regulator_set_current_limit_regmap,
119be6230c3SAxel Lin .get_current_limit = regulator_get_current_limit_regmap,
120994aae32SKeerthy };
121994aae32SKeerthy
122994aae32SKeerthy /* Operations permitted on LDO0 and LDO1 */
123699bdc23SAxel Lin static const struct regulator_ops lp873x_ldo01_ops = {
124994aae32SKeerthy .is_enabled = regulator_is_enabled_regmap,
125994aae32SKeerthy .enable = regulator_enable_regmap,
126994aae32SKeerthy .disable = regulator_disable_regmap,
127994aae32SKeerthy .get_voltage_sel = regulator_get_voltage_sel_regmap,
128994aae32SKeerthy .set_voltage_sel = regulator_set_voltage_sel_regmap,
129994aae32SKeerthy .list_voltage = regulator_list_voltage_linear_range,
130994aae32SKeerthy .map_voltage = regulator_map_voltage_linear_range,
131994aae32SKeerthy };
132994aae32SKeerthy
133994aae32SKeerthy static const struct lp873x_regulator regulators[] = {
134994aae32SKeerthy LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops,
135994aae32SKeerthy 256, LP873X_REG_BUCK0_VOUT,
136994aae32SKeerthy LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1,
137994aae32SKeerthy LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000,
1384335f653SAxel Lin buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2),
139994aae32SKeerthy LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops,
140994aae32SKeerthy 256, LP873X_REG_BUCK1_VOUT,
141994aae32SKeerthy LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1,
142994aae32SKeerthy LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000,
1434335f653SAxel Lin buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2),
144994aae32SKeerthy LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26,
145994aae32SKeerthy LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET,
146994aae32SKeerthy LP873X_REG_LDO0_CTRL,
1474335f653SAxel Lin LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF),
148994aae32SKeerthy LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26,
149994aae32SKeerthy LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET,
150994aae32SKeerthy LP873X_REG_LDO1_CTRL,
1514335f653SAxel Lin LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF),
152994aae32SKeerthy };
153994aae32SKeerthy
lp873x_regulator_probe(struct platform_device * pdev)154994aae32SKeerthy static int lp873x_regulator_probe(struct platform_device *pdev)
155994aae32SKeerthy {
156994aae32SKeerthy struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent);
157994aae32SKeerthy struct regulator_config config = { };
158994aae32SKeerthy struct regulator_dev *rdev;
159994aae32SKeerthy int i;
160994aae32SKeerthy
161994aae32SKeerthy platform_set_drvdata(pdev, lp873);
162994aae32SKeerthy
163994aae32SKeerthy config.dev = &pdev->dev;
164994aae32SKeerthy config.dev->of_node = lp873->dev->of_node;
165994aae32SKeerthy config.driver_data = lp873;
166994aae32SKeerthy config.regmap = lp873->regmap;
167994aae32SKeerthy
168994aae32SKeerthy for (i = 0; i < ARRAY_SIZE(regulators); i++) {
169994aae32SKeerthy rdev = devm_regulator_register(&pdev->dev, ®ulators[i].desc,
170994aae32SKeerthy &config);
171994aae32SKeerthy if (IS_ERR(rdev)) {
172994aae32SKeerthy dev_err(lp873->dev, "failed to register %s regulator\n",
173994aae32SKeerthy pdev->name);
174994aae32SKeerthy return PTR_ERR(rdev);
175994aae32SKeerthy }
176994aae32SKeerthy }
177994aae32SKeerthy
178994aae32SKeerthy return 0;
179994aae32SKeerthy }
180994aae32SKeerthy
181994aae32SKeerthy static const struct platform_device_id lp873x_regulator_id_table[] = {
182994aae32SKeerthy { "lp873x-regulator", },
183994aae32SKeerthy { /* sentinel */ }
184994aae32SKeerthy };
185994aae32SKeerthy MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table);
186994aae32SKeerthy
187994aae32SKeerthy static struct platform_driver lp873x_regulator_driver = {
188994aae32SKeerthy .driver = {
189994aae32SKeerthy .name = "lp873x-pmic",
190*259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
191994aae32SKeerthy },
192994aae32SKeerthy .probe = lp873x_regulator_probe,
193994aae32SKeerthy .id_table = lp873x_regulator_id_table,
194994aae32SKeerthy };
195994aae32SKeerthy module_platform_driver(lp873x_regulator_driver);
196994aae32SKeerthy
197994aae32SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
198994aae32SKeerthy MODULE_DESCRIPTION("LP873X voltage regulator driver");
199994aae32SKeerthy MODULE_ALIAS("platform:lp873x-pmic");
200994aae32SKeerthy MODULE_LICENSE("GPL v2");
201