Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / regulator / max8660.c
blob347043a5a9a7df999722fd73870cda070123abbd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * max8660.c -- Voltage regulation for the Maxim 8660/8661
5 * based on max1586.c and wm8400-regulator.c
7 * Copyright (C) 2009 Wolfram Sang, Pengutronix e.K.
9 * Some info:
11 * Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX8660-MAX8661.pdf
13 * This chip is a bit nasty because it is a write-only device. Thus, the driver
14 * uses shadow registers to keep track of its values. The main problem appears
15 * to be the initialization: When Linux boots up, we cannot know if the chip is
16 * in the default state or not, so we would have to pass such information in
17 * platform_data. As this adds a bit of complexity to the driver, this is left
18 * out for now until it is really needed.
20 * [A|S|M]DTV1 registers are currently not used, but [A|S|M]DTV2.
22 * If the driver is feature complete, it might be worth to check if one set of
23 * functions for V3-V7 is sufficient. For maximum flexibility during
24 * development, they are separated for now.
27 #include <linux/module.h>
28 #include <linux/err.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/slab.h>
33 #include <linux/regulator/max8660.h>
34 #include <linux/of.h>
35 #include <linux/of_device.h>
36 #include <linux/regulator/of_regulator.h>
38 #define MAX8660_DCDC_MIN_UV 725000
39 #define MAX8660_DCDC_MAX_UV 1800000
40 #define MAX8660_DCDC_STEP 25000
41 #define MAX8660_DCDC_MAX_SEL 0x2b
43 #define MAX8660_LDO5_MIN_UV 1700000
44 #define MAX8660_LDO5_MAX_UV 2000000
45 #define MAX8660_LDO5_STEP 25000
46 #define MAX8660_LDO5_MAX_SEL 0x0c
48 #define MAX8660_LDO67_MIN_UV 1800000
49 #define MAX8660_LDO67_MAX_UV 3300000
50 #define MAX8660_LDO67_STEP 100000
51 #define MAX8660_LDO67_MAX_SEL 0x0f
53 enum {
54 MAX8660_OVER1,
55 MAX8660_OVER2,
56 MAX8660_VCC1,
57 MAX8660_ADTV1,
58 MAX8660_ADTV2,
59 MAX8660_SDTV1,
60 MAX8660_SDTV2,
61 MAX8660_MDTV1,
62 MAX8660_MDTV2,
63 MAX8660_L12VCR,
64 MAX8660_FPWM,
65 MAX8660_N_REGS, /* not a real register */
68 struct max8660 {
69 struct i2c_client *client;
70 u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
73 static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
75 static const u8 max8660_addresses[MAX8660_N_REGS] = {
76 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80
79 int ret;
80 u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
82 dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
83 max8660_addresses[reg], reg_val);
85 ret = i2c_smbus_write_byte_data(max8660->client,
86 max8660_addresses[reg], reg_val);
87 if (ret == 0)
88 max8660->shadow_regs[reg] = reg_val;
90 return ret;
95 * DCDC functions
98 static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
100 struct max8660 *max8660 = rdev_get_drvdata(rdev);
101 u8 val = max8660->shadow_regs[MAX8660_OVER1];
102 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
104 return !!(val & mask);
107 static int max8660_dcdc_enable(struct regulator_dev *rdev)
109 struct max8660 *max8660 = rdev_get_drvdata(rdev);
110 u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
112 return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
115 static int max8660_dcdc_disable(struct regulator_dev *rdev)
117 struct max8660 *max8660 = rdev_get_drvdata(rdev);
118 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
120 return max8660_write(max8660, MAX8660_OVER1, mask, 0);
123 static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev)
125 struct max8660 *max8660 = rdev_get_drvdata(rdev);
126 u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
127 u8 selector = max8660->shadow_regs[reg];
129 return selector;
132 static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev,
133 unsigned int selector)
135 struct max8660 *max8660 = rdev_get_drvdata(rdev);
136 u8 reg, bits;
137 int ret;
139 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
140 ret = max8660_write(max8660, reg, 0, selector);
141 if (ret)
142 return ret;
144 /* Select target voltage register and activate regulation */
145 bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
146 return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
149 static struct regulator_ops max8660_dcdc_ops = {
150 .is_enabled = max8660_dcdc_is_enabled,
151 .list_voltage = regulator_list_voltage_linear,
152 .map_voltage = regulator_map_voltage_linear,
153 .set_voltage_sel = max8660_dcdc_set_voltage_sel,
154 .get_voltage_sel = max8660_dcdc_get_voltage_sel,
159 * LDO5 functions
162 static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev)
164 struct max8660 *max8660 = rdev_get_drvdata(rdev);
166 u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
167 return selector;
170 static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev,
171 unsigned int selector)
173 struct max8660 *max8660 = rdev_get_drvdata(rdev);
174 int ret;
176 ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
177 if (ret)
178 return ret;
180 /* Select target voltage register and activate regulation */
181 return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
184 static const struct regulator_ops max8660_ldo5_ops = {
185 .list_voltage = regulator_list_voltage_linear,
186 .map_voltage = regulator_map_voltage_linear,
187 .set_voltage_sel = max8660_ldo5_set_voltage_sel,
188 .get_voltage_sel = max8660_ldo5_get_voltage_sel,
193 * LDO67 functions
196 static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
198 struct max8660 *max8660 = rdev_get_drvdata(rdev);
199 u8 val = max8660->shadow_regs[MAX8660_OVER2];
200 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
202 return !!(val & mask);
205 static int max8660_ldo67_enable(struct regulator_dev *rdev)
207 struct max8660 *max8660 = rdev_get_drvdata(rdev);
208 u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
210 return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
213 static int max8660_ldo67_disable(struct regulator_dev *rdev)
215 struct max8660 *max8660 = rdev_get_drvdata(rdev);
216 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
218 return max8660_write(max8660, MAX8660_OVER2, mask, 0);
221 static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev)
223 struct max8660 *max8660 = rdev_get_drvdata(rdev);
224 u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
225 u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
227 return selector;
230 static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev,
231 unsigned int selector)
233 struct max8660 *max8660 = rdev_get_drvdata(rdev);
235 if (rdev_get_id(rdev) == MAX8660_V6)
236 return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
237 else
238 return max8660_write(max8660, MAX8660_L12VCR, 0x0f,
239 selector << 4);
242 static const struct regulator_ops max8660_ldo67_ops = {
243 .is_enabled = max8660_ldo67_is_enabled,
244 .enable = max8660_ldo67_enable,
245 .disable = max8660_ldo67_disable,
246 .list_voltage = regulator_list_voltage_linear,
247 .map_voltage = regulator_map_voltage_linear,
248 .get_voltage_sel = max8660_ldo67_get_voltage_sel,
249 .set_voltage_sel = max8660_ldo67_set_voltage_sel,
252 static const struct regulator_desc max8660_reg[] = {
254 .name = "V3(DCDC)",
255 .id = MAX8660_V3,
256 .ops = &max8660_dcdc_ops,
257 .type = REGULATOR_VOLTAGE,
258 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
259 .owner = THIS_MODULE,
260 .min_uV = MAX8660_DCDC_MIN_UV,
261 .uV_step = MAX8660_DCDC_STEP,
264 .name = "V4(DCDC)",
265 .id = MAX8660_V4,
266 .ops = &max8660_dcdc_ops,
267 .type = REGULATOR_VOLTAGE,
268 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
269 .owner = THIS_MODULE,
270 .min_uV = MAX8660_DCDC_MIN_UV,
271 .uV_step = MAX8660_DCDC_STEP,
274 .name = "V5(LDO)",
275 .id = MAX8660_V5,
276 .ops = &max8660_ldo5_ops,
277 .type = REGULATOR_VOLTAGE,
278 .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
279 .owner = THIS_MODULE,
280 .min_uV = MAX8660_LDO5_MIN_UV,
281 .uV_step = MAX8660_LDO5_STEP,
284 .name = "V6(LDO)",
285 .id = MAX8660_V6,
286 .ops = &max8660_ldo67_ops,
287 .type = REGULATOR_VOLTAGE,
288 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
289 .owner = THIS_MODULE,
290 .min_uV = MAX8660_LDO67_MIN_UV,
291 .uV_step = MAX8660_LDO67_STEP,
294 .name = "V7(LDO)",
295 .id = MAX8660_V7,
296 .ops = &max8660_ldo67_ops,
297 .type = REGULATOR_VOLTAGE,
298 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
299 .owner = THIS_MODULE,
300 .min_uV = MAX8660_LDO67_MIN_UV,
301 .uV_step = MAX8660_LDO67_STEP,
305 enum {
306 MAX8660 = 0,
307 MAX8661 = 1,
310 #ifdef CONFIG_OF
311 static const struct of_device_id max8660_dt_ids[] = {
312 { .compatible = "maxim,max8660", .data = (void *) MAX8660 },
313 { .compatible = "maxim,max8661", .data = (void *) MAX8661 },
316 MODULE_DEVICE_TABLE(of, max8660_dt_ids);
318 static int max8660_pdata_from_dt(struct device *dev,
319 struct device_node **of_node,
320 struct max8660_platform_data *pdata)
322 int matched, i;
323 struct device_node *np;
324 struct max8660_subdev_data *sub;
325 struct of_regulator_match rmatch[ARRAY_SIZE(max8660_reg)] = { };
327 np = of_get_child_by_name(dev->of_node, "regulators");
328 if (!np) {
329 dev_err(dev, "missing 'regulators' subnode in DT\n");
330 return -EINVAL;
333 for (i = 0; i < ARRAY_SIZE(rmatch); i++)
334 rmatch[i].name = max8660_reg[i].name;
336 matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
337 of_node_put(np);
338 if (matched <= 0)
339 return matched;
341 pdata->subdevs = devm_kcalloc(dev,
342 matched,
343 sizeof(struct max8660_subdev_data),
344 GFP_KERNEL);
345 if (!pdata->subdevs)
346 return -ENOMEM;
348 pdata->num_subdevs = matched;
349 sub = pdata->subdevs;
351 for (i = 0; i < matched; i++) {
352 sub->id = i;
353 sub->name = rmatch[i].name;
354 sub->platform_data = rmatch[i].init_data;
355 of_node[i] = rmatch[i].of_node;
356 sub++;
359 return 0;
361 #else
362 static inline int max8660_pdata_from_dt(struct device *dev,
363 struct device_node **of_node,
364 struct max8660_platform_data *pdata)
366 return 0;
368 #endif
370 static int max8660_probe(struct i2c_client *client,
371 const struct i2c_device_id *i2c_id)
373 struct device *dev = &client->dev;
374 struct max8660_platform_data pdata_of, *pdata = dev_get_platdata(dev);
375 struct regulator_config config = { };
376 struct max8660 *max8660;
377 int boot_on, i, id, ret = -EINVAL;
378 struct device_node *of_node[MAX8660_V_END];
379 unsigned long type;
381 if (dev->of_node && !pdata) {
382 const struct of_device_id *id;
384 id = of_match_device(of_match_ptr(max8660_dt_ids), dev);
385 if (!id)
386 return -ENODEV;
388 ret = max8660_pdata_from_dt(dev, of_node, &pdata_of);
389 if (ret < 0)
390 return ret;
392 pdata = &pdata_of;
393 type = (unsigned long) id->data;
394 } else {
395 type = i2c_id->driver_data;
396 memset(of_node, 0, sizeof(of_node));
399 if (pdata->num_subdevs > MAX8660_V_END) {
400 dev_err(dev, "Too many regulators found!\n");
401 return -EINVAL;
404 max8660 = devm_kzalloc(dev, sizeof(struct max8660), GFP_KERNEL);
405 if (!max8660)
406 return -ENOMEM;
408 max8660->client = client;
410 if (pdata->en34_is_high) {
411 /* Simulate always on */
412 max8660->shadow_regs[MAX8660_OVER1] = 5;
413 } else {
414 /* Otherwise devices can be toggled via software */
415 max8660_dcdc_ops.enable = max8660_dcdc_enable;
416 max8660_dcdc_ops.disable = max8660_dcdc_disable;
420 * First, set up shadow registers to prevent glitches. As some
421 * registers are shared between regulators, everything must be properly
422 * set up for all regulators in advance.
424 max8660->shadow_regs[MAX8660_ADTV1] =
425 max8660->shadow_regs[MAX8660_ADTV2] =
426 max8660->shadow_regs[MAX8660_SDTV1] =
427 max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
428 max8660->shadow_regs[MAX8660_MDTV1] =
429 max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
431 for (i = 0; i < pdata->num_subdevs; i++) {
433 if (!pdata->subdevs[i].platform_data)
434 boot_on = false;
435 else
436 boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
438 switch (pdata->subdevs[i].id) {
439 case MAX8660_V3:
440 if (boot_on)
441 max8660->shadow_regs[MAX8660_OVER1] |= 1;
442 break;
444 case MAX8660_V4:
445 if (boot_on)
446 max8660->shadow_regs[MAX8660_OVER1] |= 4;
447 break;
449 case MAX8660_V5:
450 break;
452 case MAX8660_V6:
453 if (boot_on)
454 max8660->shadow_regs[MAX8660_OVER2] |= 2;
455 break;
457 case MAX8660_V7:
458 if (type == MAX8661) {
459 dev_err(dev, "Regulator not on this chip!\n");
460 return -EINVAL;
463 if (boot_on)
464 max8660->shadow_regs[MAX8660_OVER2] |= 4;
465 break;
467 default:
468 dev_err(dev, "invalid regulator %s\n",
469 pdata->subdevs[i].name);
470 return ret;
474 /* Finally register devices */
475 for (i = 0; i < pdata->num_subdevs; i++) {
476 struct regulator_dev *rdev;
478 id = pdata->subdevs[i].id;
480 config.dev = dev;
481 config.init_data = pdata->subdevs[i].platform_data;
482 config.of_node = of_node[i];
483 config.driver_data = max8660;
485 rdev = devm_regulator_register(&client->dev,
486 &max8660_reg[id], &config);
487 if (IS_ERR(rdev)) {
488 dev_err(&client->dev, "failed to register %s\n",
489 max8660_reg[id].name);
490 return PTR_ERR(rdev);
494 i2c_set_clientdata(client, max8660);
495 return 0;
498 static const struct i2c_device_id max8660_id[] = {
499 { .name = "max8660", .driver_data = MAX8660 },
500 { .name = "max8661", .driver_data = MAX8661 },
503 MODULE_DEVICE_TABLE(i2c, max8660_id);
505 static struct i2c_driver max8660_driver = {
506 .probe = max8660_probe,
507 .driver = {
508 .name = "max8660",
510 .id_table = max8660_id,
513 static int __init max8660_init(void)
515 return i2c_add_driver(&max8660_driver);
517 subsys_initcall(max8660_init);
519 static void __exit max8660_exit(void)
521 i2c_del_driver(&max8660_driver);
523 module_exit(max8660_exit);
525 /* Module information */
526 MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
527 MODULE_AUTHOR("Wolfram Sang");
528 MODULE_LICENSE("GPL v2");