]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/regulator/lp873x-regulator.c
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[mirror_ubuntu-bionic-kernel.git] / drivers / regulator / lp873x-regulator.c
1 /*
2 * Regulator driver for LP873X PMIC
3 *
4 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether expressed or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details.
14 */
15
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19
20 #include <linux/mfd/lp873x.h>
21
22 #define LP873X_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
23 _delay, _lr, _cr) \
24 [_id] = { \
25 .desc = { \
26 .name = _name, \
27 .id = _id, \
28 .of_match = of_match_ptr(_of), \
29 .regulators_node = of_match_ptr("regulators"),\
30 .ops = &_ops, \
31 .n_voltages = _n, \
32 .type = REGULATOR_VOLTAGE, \
33 .owner = THIS_MODULE, \
34 .vsel_reg = _vr, \
35 .vsel_mask = _vm, \
36 .enable_reg = _er, \
37 .enable_mask = _em, \
38 .ramp_delay = _delay, \
39 .linear_ranges = _lr, \
40 .n_linear_ranges = ARRAY_SIZE(_lr), \
41 }, \
42 .ctrl2_reg = _cr, \
43 }
44
45 struct lp873x_regulator {
46 struct regulator_desc desc;
47 unsigned int ctrl2_reg;
48 };
49
50 static const struct lp873x_regulator regulators[];
51
52 static const struct regulator_linear_range buck0_buck1_ranges[] = {
53 REGULATOR_LINEAR_RANGE(0, 0x0, 0x13, 0),
54 REGULATOR_LINEAR_RANGE(700000, 0x14, 0x17, 10000),
55 REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
56 REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
57 };
58
59 static const struct regulator_linear_range ldo0_ldo1_ranges[] = {
60 REGULATOR_LINEAR_RANGE(800000, 0x0, 0x19, 100000),
61 };
62
63 static unsigned int lp873x_buck_ramp_delay[] = {
64 30000, 15000, 10000, 7500, 3800, 1900, 940, 470
65 };
66
67 /* LP873X BUCK current limit */
68 static const unsigned int lp873x_buck_uA[] = {
69 1500000, 2000000, 2500000, 3000000, 3500000, 4000000,
70 };
71
72 static int lp873x_buck_set_ramp_delay(struct regulator_dev *rdev,
73 int ramp_delay)
74 {
75 int id = rdev_get_id(rdev);
76 struct lp873x *lp873 = rdev_get_drvdata(rdev);
77 unsigned int reg;
78 int ret;
79
80 if (ramp_delay <= 470)
81 reg = 7;
82 else if (ramp_delay <= 940)
83 reg = 6;
84 else if (ramp_delay <= 1900)
85 reg = 5;
86 else if (ramp_delay <= 3800)
87 reg = 4;
88 else if (ramp_delay <= 7500)
89 reg = 3;
90 else if (ramp_delay <= 10000)
91 reg = 2;
92 else if (ramp_delay <= 15000)
93 reg = 1;
94 else
95 reg = 0;
96
97 ret = regmap_update_bits(lp873->regmap, regulators[id].ctrl2_reg,
98 LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE,
99 reg << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_SLEW_RATE));
100 if (ret) {
101 dev_err(lp873->dev, "SLEW RATE write failed: %d\n", ret);
102 return ret;
103 }
104
105 rdev->constraints->ramp_delay = lp873x_buck_ramp_delay[reg];
106
107 return 0;
108 }
109
110 static int lp873x_buck_set_current_limit(struct regulator_dev *rdev,
111 int min_uA, int max_uA)
112 {
113 int id = rdev_get_id(rdev);
114 struct lp873x *lp873 = rdev_get_drvdata(rdev);
115 int i;
116
117 for (i = ARRAY_SIZE(lp873x_buck_uA) - 1; i >= 0; i--) {
118 if (lp873x_buck_uA[i] >= min_uA &&
119 lp873x_buck_uA[i] <= max_uA)
120 return regmap_update_bits(lp873->regmap,
121 regulators[id].ctrl2_reg,
122 LP873X_BUCK0_CTRL_2_BUCK0_ILIM,
123 i << __ffs(LP873X_BUCK0_CTRL_2_BUCK0_ILIM));
124 }
125
126 return -EINVAL;
127 }
128
129 static int lp873x_buck_get_current_limit(struct regulator_dev *rdev)
130 {
131 int id = rdev_get_id(rdev);
132 struct lp873x *lp873 = rdev_get_drvdata(rdev);
133 int ret;
134 unsigned int val;
135
136 ret = regmap_read(lp873->regmap, regulators[id].ctrl2_reg, &val);
137 if (ret)
138 return ret;
139
140 val = (val & LP873X_BUCK0_CTRL_2_BUCK0_ILIM) >>
141 __ffs(LP873X_BUCK0_CTRL_2_BUCK0_ILIM);
142
143 return (val < ARRAY_SIZE(lp873x_buck_uA)) ?
144 lp873x_buck_uA[val] : -EINVAL;
145 }
146
147 /* Operations permitted on BUCK0, BUCK1 */
148 static struct regulator_ops lp873x_buck01_ops = {
149 .is_enabled = regulator_is_enabled_regmap,
150 .enable = regulator_enable_regmap,
151 .disable = regulator_disable_regmap,
152 .get_voltage_sel = regulator_get_voltage_sel_regmap,
153 .set_voltage_sel = regulator_set_voltage_sel_regmap,
154 .list_voltage = regulator_list_voltage_linear_range,
155 .map_voltage = regulator_map_voltage_linear_range,
156 .set_voltage_time_sel = regulator_set_voltage_time_sel,
157 .set_ramp_delay = lp873x_buck_set_ramp_delay,
158 .set_current_limit = lp873x_buck_set_current_limit,
159 .get_current_limit = lp873x_buck_get_current_limit,
160 };
161
162 /* Operations permitted on LDO0 and LDO1 */
163 static struct regulator_ops lp873x_ldo01_ops = {
164 .is_enabled = regulator_is_enabled_regmap,
165 .enable = regulator_enable_regmap,
166 .disable = regulator_disable_regmap,
167 .get_voltage_sel = regulator_get_voltage_sel_regmap,
168 .set_voltage_sel = regulator_set_voltage_sel_regmap,
169 .list_voltage = regulator_list_voltage_linear_range,
170 .map_voltage = regulator_map_voltage_linear_range,
171 };
172
173 static const struct lp873x_regulator regulators[] = {
174 LP873X_REGULATOR("BUCK0", LP873X_BUCK_0, "buck0", lp873x_buck01_ops,
175 256, LP873X_REG_BUCK0_VOUT,
176 LP873X_BUCK0_VOUT_BUCK0_VSET, LP873X_REG_BUCK0_CTRL_1,
177 LP873X_BUCK0_CTRL_1_BUCK0_EN, 10000,
178 buck0_buck1_ranges, LP873X_REG_BUCK0_CTRL_2),
179 LP873X_REGULATOR("BUCK1", LP873X_BUCK_1, "buck1", lp873x_buck01_ops,
180 256, LP873X_REG_BUCK1_VOUT,
181 LP873X_BUCK1_VOUT_BUCK1_VSET, LP873X_REG_BUCK1_CTRL_1,
182 LP873X_BUCK1_CTRL_1_BUCK1_EN, 10000,
183 buck0_buck1_ranges, LP873X_REG_BUCK1_CTRL_2),
184 LP873X_REGULATOR("LDO0", LP873X_LDO_0, "ldo0", lp873x_ldo01_ops, 26,
185 LP873X_REG_LDO0_VOUT, LP873X_LDO0_VOUT_LDO0_VSET,
186 LP873X_REG_LDO0_CTRL,
187 LP873X_LDO0_CTRL_LDO0_EN, 0, ldo0_ldo1_ranges, 0xFF),
188 LP873X_REGULATOR("LDO1", LP873X_LDO_1, "ldo1", lp873x_ldo01_ops, 26,
189 LP873X_REG_LDO1_VOUT, LP873X_LDO1_VOUT_LDO1_VSET,
190 LP873X_REG_LDO1_CTRL,
191 LP873X_LDO1_CTRL_LDO1_EN, 0, ldo0_ldo1_ranges, 0xFF),
192 };
193
194 static int lp873x_regulator_probe(struct platform_device *pdev)
195 {
196 struct lp873x *lp873 = dev_get_drvdata(pdev->dev.parent);
197 struct regulator_config config = { };
198 struct regulator_dev *rdev;
199 int i;
200
201 platform_set_drvdata(pdev, lp873);
202
203 config.dev = &pdev->dev;
204 config.dev->of_node = lp873->dev->of_node;
205 config.driver_data = lp873;
206 config.regmap = lp873->regmap;
207
208 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
209 rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
210 &config);
211 if (IS_ERR(rdev)) {
212 dev_err(lp873->dev, "failed to register %s regulator\n",
213 pdev->name);
214 return PTR_ERR(rdev);
215 }
216 }
217
218 return 0;
219 }
220
221 static const struct platform_device_id lp873x_regulator_id_table[] = {
222 { "lp873x-regulator", },
223 { /* sentinel */ }
224 };
225 MODULE_DEVICE_TABLE(platform, lp873x_regulator_id_table);
226
227 static struct platform_driver lp873x_regulator_driver = {
228 .driver = {
229 .name = "lp873x-pmic",
230 },
231 .probe = lp873x_regulator_probe,
232 .id_table = lp873x_regulator_id_table,
233 };
234 module_platform_driver(lp873x_regulator_driver);
235
236 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
237 MODULE_DESCRIPTION("LP873X voltage regulator driver");
238 MODULE_ALIAS("platform:lp873x-pmic");
239 MODULE_LICENSE("GPL v2");