blob: b79fe93c8edb3748ba60f8bdcf74541967e6695a [file] [log] [blame]
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +01001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 BayLibre SAS
4// Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
5//
6// Regulator driver for MAXIM 77650/77651 charger/power-supply.
7
Axel Lin5358db52019-01-30 17:07:21 +08008#include <linux/of.h>
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +01009#include <linux/mfd/max77650.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/regmap.h>
13#include <linux/regulator/driver.h>
14
15#define MAX77650_REGULATOR_EN_CTRL_MASK GENMASK(3, 0)
16#define MAX77650_REGULATOR_EN_CTRL_BITS(_reg) \
17 ((_reg) & MAX77650_REGULATOR_EN_CTRL_MASK)
18#define MAX77650_REGULATOR_ENABLED GENMASK(2, 1)
19#define MAX77650_REGULATOR_DISABLED BIT(2)
20
21#define MAX77650_REGULATOR_V_LDO_MASK GENMASK(6, 0)
22#define MAX77650_REGULATOR_V_SBB_MASK GENMASK(5, 0)
Axel Lin3df42352019-03-25 21:55:57 +080023#define MAX77651_REGULATOR_V_SBB1_MASK GENMASK(5, 2)
24#define MAX77651_REGULATOR_V_SBB1_RANGE_MASK GENMASK(1, 0)
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +010025
26#define MAX77650_REGULATOR_AD_MASK BIT(3)
27#define MAX77650_REGULATOR_AD_DISABLED 0x00
28#define MAX77650_REGULATOR_AD_ENABLED BIT(3)
29
30#define MAX77650_REGULATOR_CURR_LIM_MASK GENMASK(7, 6)
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +010031
32enum {
33 MAX77650_REGULATOR_ID_LDO = 0,
34 MAX77650_REGULATOR_ID_SBB0,
35 MAX77650_REGULATOR_ID_SBB1,
36 MAX77650_REGULATOR_ID_SBB2,
37 MAX77650_REGULATOR_NUM_REGULATORS,
38};
39
40struct max77650_regulator_desc {
41 struct regulator_desc desc;
42 unsigned int regA;
43 unsigned int regB;
44};
45
Nathan Chancellor59dec1f2019-05-22 18:26:29 -070046static struct max77650_regulator_desc max77651_SBB1_desc;
47
Axel Lin3df42352019-03-25 21:55:57 +080048static const unsigned int max77651_sbb1_volt_range_sel[] = {
49 0x0, 0x1, 0x2, 0x3
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +010050};
51
Axel Lin3df42352019-03-25 21:55:57 +080052static const struct regulator_linear_range max77651_sbb1_volt_ranges[] = {
53 /* range index 0 */
54 REGULATOR_LINEAR_RANGE(2400000, 0x00, 0x0f, 50000),
55 /* range index 1 */
56 REGULATOR_LINEAR_RANGE(3200000, 0x00, 0x0f, 50000),
57 /* range index 2 */
58 REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x0f, 50000),
59 /* range index 3 */
60 REGULATOR_LINEAR_RANGE(4800000, 0x00, 0x09, 50000),
61};
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +010062
Axel Lin6c98ac2a2019-02-28 21:40:18 +080063static const unsigned int max77650_current_limit_table[] = {
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +010064 1000000, 866000, 707000, 500000,
65};
66
67static int max77650_regulator_is_enabled(struct regulator_dev *rdev)
68{
69 struct max77650_regulator_desc *rdesc;
70 struct regmap *map;
71 int val, rv, en;
72
73 rdesc = rdev_get_drvdata(rdev);
74 map = rdev_get_regmap(rdev);
75
76 rv = regmap_read(map, rdesc->regB, &val);
77 if (rv)
78 return rv;
79
80 en = MAX77650_REGULATOR_EN_CTRL_BITS(val);
81
82 return en != MAX77650_REGULATOR_DISABLED;
83}
84
85static int max77650_regulator_enable(struct regulator_dev *rdev)
86{
87 struct max77650_regulator_desc *rdesc;
88 struct regmap *map;
89
90 rdesc = rdev_get_drvdata(rdev);
91 map = rdev_get_regmap(rdev);
92
93 return regmap_update_bits(map, rdesc->regB,
94 MAX77650_REGULATOR_EN_CTRL_MASK,
95 MAX77650_REGULATOR_ENABLED);
96}
97
98static int max77650_regulator_disable(struct regulator_dev *rdev)
99{
100 struct max77650_regulator_desc *rdesc;
101 struct regmap *map;
102
103 rdesc = rdev_get_drvdata(rdev);
104 map = rdev_get_regmap(rdev);
105
106 return regmap_update_bits(map, rdesc->regB,
107 MAX77650_REGULATOR_EN_CTRL_MASK,
108 MAX77650_REGULATOR_DISABLED);
109}
110
111static int max77650_regulator_set_voltage_sel(struct regulator_dev *rdev,
112 unsigned int sel)
113{
Axel Lin3df42352019-03-25 21:55:57 +0800114 struct max77650_regulator_desc *rdesc = rdev_get_drvdata(rdev);
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100115 int rv = 0, curr, diff;
116 bool ascending;
117
118 /*
119 * If the regulator is disabled, we can program the desired
120 * voltage right away.
121 */
Axel Lin3df42352019-03-25 21:55:57 +0800122 if (!max77650_regulator_is_enabled(rdev)) {
123 if (rdesc == &max77651_SBB1_desc)
124 return regulator_set_voltage_sel_pickable_regmap(rdev,
125 sel);
126 else
127 return regulator_set_voltage_sel_regmap(rdev, sel);
128 }
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100129
130 /*
131 * Otherwise we need to manually ramp the output voltage up/down
132 * one step at a time.
133 */
134
Axel Lin3df42352019-03-25 21:55:57 +0800135 if (rdesc == &max77651_SBB1_desc)
136 curr = regulator_get_voltage_sel_pickable_regmap(rdev);
137 else
138 curr = regulator_get_voltage_sel_regmap(rdev);
139
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100140 if (curr < 0)
141 return curr;
142
143 diff = curr - sel;
144 if (diff == 0)
145 return 0; /* Already there. */
146 else if (diff > 0)
147 ascending = false;
148 else
149 ascending = true;
150
151 /*
152 * Make sure we'll get to the right voltage and break the loop even if
153 * the selector equals 0.
154 */
155 for (ascending ? curr++ : curr--;; ascending ? curr++ : curr--) {
Axel Lin3df42352019-03-25 21:55:57 +0800156 if (rdesc == &max77651_SBB1_desc)
157 rv = regulator_set_voltage_sel_pickable_regmap(rdev,
158 curr);
159 else
160 rv = regulator_set_voltage_sel_regmap(rdev, curr);
161
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100162 if (rv)
163 return rv;
164
165 if (curr == sel)
166 break;
167 }
168
169 return 0;
170}
171
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100172static const struct regulator_ops max77650_regulator_LDO_ops = {
173 .is_enabled = max77650_regulator_is_enabled,
174 .enable = max77650_regulator_enable,
175 .disable = max77650_regulator_disable,
176 .list_voltage = regulator_list_voltage_linear,
177 .map_voltage = regulator_map_voltage_linear,
178 .get_voltage_sel = regulator_get_voltage_sel_regmap,
179 .set_voltage_sel = max77650_regulator_set_voltage_sel,
180 .set_active_discharge = regulator_set_active_discharge_regmap,
181};
182
183static const struct regulator_ops max77650_regulator_SBB_ops = {
184 .is_enabled = max77650_regulator_is_enabled,
185 .enable = max77650_regulator_enable,
186 .disable = max77650_regulator_disable,
187 .list_voltage = regulator_list_voltage_linear,
188 .map_voltage = regulator_map_voltage_linear,
189 .get_voltage_sel = regulator_get_voltage_sel_regmap,
190 .set_voltage_sel = max77650_regulator_set_voltage_sel,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800191 .get_current_limit = regulator_get_current_limit_regmap,
192 .set_current_limit = regulator_set_current_limit_regmap,
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100193 .set_active_discharge = regulator_set_active_discharge_regmap,
194};
195
Axel Lin3df42352019-03-25 21:55:57 +0800196/* Special case for max77651 SBB1 - pickable linear-range voltage mapping. */
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100197static const struct regulator_ops max77651_SBB1_regulator_ops = {
198 .is_enabled = max77650_regulator_is_enabled,
199 .enable = max77650_regulator_enable,
200 .disable = max77650_regulator_disable,
Axel Lin3df42352019-03-25 21:55:57 +0800201 .list_voltage = regulator_list_voltage_pickable_linear_range,
202 .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
203 .set_voltage_sel = max77650_regulator_set_voltage_sel,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800204 .get_current_limit = regulator_get_current_limit_regmap,
205 .set_current_limit = regulator_set_current_limit_regmap,
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100206 .set_active_discharge = regulator_set_active_discharge_regmap,
207};
208
209static struct max77650_regulator_desc max77650_LDO_desc = {
210 .desc = {
211 .name = "ldo",
212 .of_match = of_match_ptr("ldo"),
213 .regulators_node = of_match_ptr("regulators"),
214 .supply_name = "in-ldo",
215 .id = MAX77650_REGULATOR_ID_LDO,
216 .ops = &max77650_regulator_LDO_ops,
217 .min_uV = 1350000,
218 .uV_step = 12500,
219 .n_voltages = 128,
220 .vsel_mask = MAX77650_REGULATOR_V_LDO_MASK,
221 .vsel_reg = MAX77650_REG_CNFG_LDO_A,
222 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
223 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
224 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
225 .active_discharge_reg = MAX77650_REG_CNFG_LDO_B,
226 .enable_time = 100,
227 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800228 .owner = THIS_MODULE,
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100229 },
230 .regA = MAX77650_REG_CNFG_LDO_A,
231 .regB = MAX77650_REG_CNFG_LDO_B,
232};
233
234static struct max77650_regulator_desc max77650_SBB0_desc = {
235 .desc = {
236 .name = "sbb0",
237 .of_match = of_match_ptr("sbb0"),
238 .regulators_node = of_match_ptr("regulators"),
239 .supply_name = "in-sbb0",
240 .id = MAX77650_REGULATOR_ID_SBB0,
241 .ops = &max77650_regulator_SBB_ops,
242 .min_uV = 800000,
243 .uV_step = 25000,
244 .n_voltages = 64,
245 .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
246 .vsel_reg = MAX77650_REG_CNFG_SBB0_A,
247 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
248 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
249 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
250 .active_discharge_reg = MAX77650_REG_CNFG_SBB0_B,
251 .enable_time = 100,
252 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800253 .owner = THIS_MODULE,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800254 .csel_reg = MAX77650_REG_CNFG_SBB0_A,
255 .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
256 .curr_table = max77650_current_limit_table,
257 .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100258 },
259 .regA = MAX77650_REG_CNFG_SBB0_A,
260 .regB = MAX77650_REG_CNFG_SBB0_B,
261};
262
263static struct max77650_regulator_desc max77650_SBB1_desc = {
264 .desc = {
265 .name = "sbb1",
266 .of_match = of_match_ptr("sbb1"),
267 .regulators_node = of_match_ptr("regulators"),
268 .supply_name = "in-sbb1",
269 .id = MAX77650_REGULATOR_ID_SBB1,
270 .ops = &max77650_regulator_SBB_ops,
271 .min_uV = 800000,
272 .uV_step = 12500,
273 .n_voltages = 64,
274 .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
275 .vsel_reg = MAX77650_REG_CNFG_SBB1_A,
276 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
277 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
278 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
279 .active_discharge_reg = MAX77650_REG_CNFG_SBB1_B,
280 .enable_time = 100,
281 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800282 .owner = THIS_MODULE,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800283 .csel_reg = MAX77650_REG_CNFG_SBB1_A,
284 .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
285 .curr_table = max77650_current_limit_table,
286 .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100287 },
288 .regA = MAX77650_REG_CNFG_SBB1_A,
289 .regB = MAX77650_REG_CNFG_SBB1_B,
290};
291
292static struct max77650_regulator_desc max77651_SBB1_desc = {
293 .desc = {
294 .name = "sbb1",
295 .of_match = of_match_ptr("sbb1"),
296 .regulators_node = of_match_ptr("regulators"),
297 .supply_name = "in-sbb1",
298 .id = MAX77650_REGULATOR_ID_SBB1,
299 .ops = &max77651_SBB1_regulator_ops,
Axel Lin3df42352019-03-25 21:55:57 +0800300 .linear_range_selectors = max77651_sbb1_volt_range_sel,
301 .linear_ranges = max77651_sbb1_volt_ranges,
302 .n_linear_ranges = ARRAY_SIZE(max77651_sbb1_volt_ranges),
303 .n_voltages = 58,
304 .vsel_range_mask = MAX77651_REGULATOR_V_SBB1_RANGE_MASK,
305 .vsel_range_reg = MAX77650_REG_CNFG_SBB1_A,
306 .vsel_mask = MAX77651_REGULATOR_V_SBB1_MASK,
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100307 .vsel_reg = MAX77650_REG_CNFG_SBB1_A,
308 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
309 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
310 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
311 .active_discharge_reg = MAX77650_REG_CNFG_SBB1_B,
312 .enable_time = 100,
313 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800314 .owner = THIS_MODULE,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800315 .csel_reg = MAX77650_REG_CNFG_SBB1_A,
316 .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
317 .curr_table = max77650_current_limit_table,
318 .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100319 },
320 .regA = MAX77650_REG_CNFG_SBB1_A,
321 .regB = MAX77650_REG_CNFG_SBB1_B,
322};
323
324static struct max77650_regulator_desc max77650_SBB2_desc = {
325 .desc = {
326 .name = "sbb2",
327 .of_match = of_match_ptr("sbb2"),
328 .regulators_node = of_match_ptr("regulators"),
329 .supply_name = "in-sbb0",
330 .id = MAX77650_REGULATOR_ID_SBB2,
331 .ops = &max77650_regulator_SBB_ops,
332 .min_uV = 800000,
333 .uV_step = 50000,
334 .n_voltages = 64,
335 .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
336 .vsel_reg = MAX77650_REG_CNFG_SBB2_A,
337 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
338 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
339 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
340 .active_discharge_reg = MAX77650_REG_CNFG_SBB2_B,
341 .enable_time = 100,
342 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800343 .owner = THIS_MODULE,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800344 .csel_reg = MAX77650_REG_CNFG_SBB2_A,
345 .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
346 .curr_table = max77650_current_limit_table,
347 .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100348 },
349 .regA = MAX77650_REG_CNFG_SBB2_A,
350 .regB = MAX77650_REG_CNFG_SBB2_B,
351};
352
353static struct max77650_regulator_desc max77651_SBB2_desc = {
354 .desc = {
355 .name = "sbb2",
356 .of_match = of_match_ptr("sbb2"),
357 .regulators_node = of_match_ptr("regulators"),
358 .supply_name = "in-sbb0",
359 .id = MAX77650_REGULATOR_ID_SBB2,
360 .ops = &max77650_regulator_SBB_ops,
361 .min_uV = 2400000,
362 .uV_step = 50000,
363 .n_voltages = 64,
364 .vsel_mask = MAX77650_REGULATOR_V_SBB_MASK,
365 .vsel_reg = MAX77650_REG_CNFG_SBB2_A,
366 .active_discharge_off = MAX77650_REGULATOR_AD_DISABLED,
367 .active_discharge_on = MAX77650_REGULATOR_AD_ENABLED,
368 .active_discharge_mask = MAX77650_REGULATOR_AD_MASK,
369 .active_discharge_reg = MAX77650_REG_CNFG_SBB2_B,
370 .enable_time = 100,
371 .type = REGULATOR_VOLTAGE,
Axel Lin721efb52019-02-20 09:53:28 +0800372 .owner = THIS_MODULE,
Axel Lin6c98ac2a2019-02-28 21:40:18 +0800373 .csel_reg = MAX77650_REG_CNFG_SBB2_A,
374 .csel_mask = MAX77650_REGULATOR_CURR_LIM_MASK,
375 .curr_table = max77650_current_limit_table,
376 .n_current_limits = ARRAY_SIZE(max77650_current_limit_table),
Bartosz Golaszewskibcc61f12019-01-29 14:35:40 +0100377 },
378 .regA = MAX77650_REG_CNFG_SBB2_A,
379 .regB = MAX77650_REG_CNFG_SBB2_B,
380};
381
382static int max77650_regulator_probe(struct platform_device *pdev)
383{
384 struct max77650_regulator_desc **rdescs;
385 struct max77650_regulator_desc *rdesc;
386 struct regulator_config config = { };
387 struct device *dev, *parent;
388 struct regulator_dev *rdev;
389 struct regmap *map;
390 unsigned int val;
391 int i, rv;
392
393 dev = &pdev->dev;
394 parent = dev->parent;
395
396 if (!dev->of_node)
397 dev->of_node = parent->of_node;
398
399 rdescs = devm_kcalloc(dev, MAX77650_REGULATOR_NUM_REGULATORS,
400 sizeof(*rdescs), GFP_KERNEL);
401 if (!rdescs)
402 return -ENOMEM;
403
404 map = dev_get_regmap(parent, NULL);
405 if (!map)
406 return -ENODEV;
407
408 rv = regmap_read(map, MAX77650_REG_CID, &val);
409 if (rv)
410 return rv;
411
412 rdescs[MAX77650_REGULATOR_ID_LDO] = &max77650_LDO_desc;
413 rdescs[MAX77650_REGULATOR_ID_SBB0] = &max77650_SBB0_desc;
414
415 switch (MAX77650_CID_BITS(val)) {
416 case MAX77650_CID_77650A:
417 case MAX77650_CID_77650C:
418 rdescs[MAX77650_REGULATOR_ID_SBB1] = &max77650_SBB1_desc;
419 rdescs[MAX77650_REGULATOR_ID_SBB2] = &max77650_SBB2_desc;
420 break;
421 case MAX77650_CID_77651A:
422 case MAX77650_CID_77651B:
423 rdescs[MAX77650_REGULATOR_ID_SBB1] = &max77651_SBB1_desc;
424 rdescs[MAX77650_REGULATOR_ID_SBB2] = &max77651_SBB2_desc;
425 break;
426 default:
427 return -ENODEV;
428 }
429
430 config.dev = parent;
431
432 for (i = 0; i < MAX77650_REGULATOR_NUM_REGULATORS; i++) {
433 rdesc = rdescs[i];
434 config.driver_data = rdesc;
435
436 rdev = devm_regulator_register(dev, &rdesc->desc, &config);
437 if (IS_ERR(rdev))
438 return PTR_ERR(rdev);
439 }
440
441 return 0;
442}
443
444static struct platform_driver max77650_regulator_driver = {
445 .driver = {
446 .name = "max77650-regulator",
447 },
448 .probe = max77650_regulator_probe,
449};
450module_platform_driver(max77650_regulator_driver);
451
452MODULE_DESCRIPTION("MAXIM 77650/77651 regulator driver");
453MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
454MODULE_LICENSE("GPL v2");
Bartosz Golaszewskiba2bf342019-07-03 10:48:49 +0200455MODULE_ALIAS("platform:max77650-regulator");