1 // SPDX-License-Identifier: GPL-2.0-only
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.
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>
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
65 MAX8660_N_REGS, /* not a real register */
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
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);
88 max8660->shadow_regs[reg] = reg_val;
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];
132 static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev,
133 unsigned int selector)
135 struct max8660 *max8660 = rdev_get_drvdata(rdev);
139 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
140 ret = max8660_write(max8660, reg, 0, selector);
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,
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];
170 static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev,
171 unsigned int selector)
173 struct max8660 *max8660 = rdev_get_drvdata(rdev);
176 ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
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,
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;
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);
238 return max8660_write(max8660, MAX8660_L12VCR, 0x0f,
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[] = {
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,
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,
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,
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,
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,
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)
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");
329 dev_err(dev, "missing 'regulators' subnode in DT\n");
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));
341 pdata->subdevs = devm_kcalloc(dev,
343 sizeof(struct max8660_subdev_data),
348 pdata->num_subdevs = matched;
349 sub = pdata->subdevs;
351 for (i = 0; i < matched; i++) {
353 sub->name = rmatch[i].name;
354 sub->platform_data = rmatch[i].init_data;
355 of_node[i] = rmatch[i].of_node;
362 static inline int max8660_pdata_from_dt(struct device *dev,
363 struct device_node **of_node,
364 struct max8660_platform_data *pdata)
370 static int max8660_probe(struct i2c_client *client)
372 const struct i2c_device_id *i2c_id = i2c_client_get_device_id(client);
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];
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);
388 ret = max8660_pdata_from_dt(dev, of_node, &pdata_of);
393 type = (unsigned long) id->data;
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");
404 max8660 = devm_kzalloc(dev, sizeof(struct max8660), GFP_KERNEL);
408 max8660->client = client;
410 if (pdata->en34_is_high) {
411 /* Simulate always on */
412 max8660->shadow_regs[MAX8660_OVER1] = 5;
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)
436 boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
438 switch (pdata->subdevs[i].id) {
441 max8660->shadow_regs[MAX8660_OVER1] |= 1;
446 max8660->shadow_regs[MAX8660_OVER1] |= 4;
454 max8660->shadow_regs[MAX8660_OVER2] |= 2;
458 if (type == MAX8661) {
459 dev_err(dev, "Regulator not on this chip!\n");
464 max8660->shadow_regs[MAX8660_OVER2] |= 4;
468 dev_err(dev, "invalid regulator %s\n",
469 pdata->subdevs[i].name);
474 /* Finally register devices */
475 for (i = 0; i < pdata->num_subdevs; i++) {
476 struct regulator_dev *rdev;
478 id = pdata->subdevs[i].id;
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);
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);
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,
509 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
511 .id_table = max8660_id,
514 static int __init max8660_init(void)
516 return i2c_add_driver(&max8660_driver);
518 subsys_initcall(max8660_init);
520 static void __exit max8660_exit(void)
522 i2c_del_driver(&max8660_driver);
524 module_exit(max8660_exit);
526 /* Module information */
527 MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
528 MODULE_AUTHOR("Wolfram Sang");
529 MODULE_LICENSE("GPL v2");