1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
5 * http://www.active-semi.com/products/power-management-units/act88xx/
7 * Copyright (C) 2013 Atmel Corporation
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/act8865.h>
18 #include <linux/of_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/regulator/of_regulator.h>
21 #include <linux/regmap.h>
24 * ACT8600 Global Register Map.
26 #define ACT8600_SYS_MODE 0x00
27 #define ACT8600_SYS_CTRL 0x01
28 #define ACT8600_DCDC1_VSET 0x10
29 #define ACT8600_DCDC1_CTRL 0x12
30 #define ACT8600_DCDC2_VSET 0x20
31 #define ACT8600_DCDC2_CTRL 0x22
32 #define ACT8600_DCDC3_VSET 0x30
33 #define ACT8600_DCDC3_CTRL 0x32
34 #define ACT8600_SUDCDC4_VSET 0x40
35 #define ACT8600_SUDCDC4_CTRL 0x41
36 #define ACT8600_LDO5_VSET 0x50
37 #define ACT8600_LDO5_CTRL 0x51
38 #define ACT8600_LDO6_VSET 0x60
39 #define ACT8600_LDO6_CTRL 0x61
40 #define ACT8600_LDO7_VSET 0x70
41 #define ACT8600_LDO7_CTRL 0x71
42 #define ACT8600_LDO8_VSET 0x80
43 #define ACT8600_LDO8_CTRL 0x81
44 #define ACT8600_LDO910_CTRL 0x91
45 #define ACT8600_APCH0 0xA1
46 #define ACT8600_APCH1 0xA8
47 #define ACT8600_APCH2 0xA9
48 #define ACT8600_APCH_STAT 0xAA
49 #define ACT8600_OTG0 0xB0
50 #define ACT8600_OTG1 0xB2
53 * ACT8846 Global Register Map.
55 #define ACT8846_SYS0 0x00
56 #define ACT8846_SYS1 0x01
57 #define ACT8846_REG1_VSET 0x10
58 #define ACT8846_REG1_CTRL 0x12
59 #define ACT8846_REG2_VSET0 0x20
60 #define ACT8846_REG2_VSET1 0x21
61 #define ACT8846_REG2_CTRL 0x22
62 #define ACT8846_REG3_VSET0 0x30
63 #define ACT8846_REG3_VSET1 0x31
64 #define ACT8846_REG3_CTRL 0x32
65 #define ACT8846_REG4_VSET0 0x40
66 #define ACT8846_REG4_VSET1 0x41
67 #define ACT8846_REG4_CTRL 0x42
68 #define ACT8846_REG5_VSET 0x50
69 #define ACT8846_REG5_CTRL 0x51
70 #define ACT8846_REG6_VSET 0x58
71 #define ACT8846_REG6_CTRL 0x59
72 #define ACT8846_REG7_VSET 0x60
73 #define ACT8846_REG7_CTRL 0x61
74 #define ACT8846_REG8_VSET 0x68
75 #define ACT8846_REG8_CTRL 0x69
76 #define ACT8846_REG9_VSET 0x70
77 #define ACT8846_REG9_CTRL 0x71
78 #define ACT8846_REG10_VSET 0x80
79 #define ACT8846_REG10_CTRL 0x81
80 #define ACT8846_REG11_VSET 0x90
81 #define ACT8846_REG11_CTRL 0x91
82 #define ACT8846_REG12_VSET 0xa0
83 #define ACT8846_REG12_CTRL 0xa1
84 #define ACT8846_REG13_CTRL 0xb1
85 #define ACT8846_GLB_OFF_CTRL 0xc3
86 #define ACT8846_OFF_SYSMASK 0x18
89 * ACT8865 Global Register Map.
91 #define ACT8865_SYS_MODE 0x00
92 #define ACT8865_SYS_CTRL 0x01
93 #define ACT8865_DCDC1_VSET1 0x20
94 #define ACT8865_DCDC1_VSET2 0x21
95 #define ACT8865_DCDC1_CTRL 0x22
96 #define ACT8865_DCDC2_VSET1 0x30
97 #define ACT8865_DCDC2_VSET2 0x31
98 #define ACT8865_DCDC2_CTRL 0x32
99 #define ACT8865_DCDC3_VSET1 0x40
100 #define ACT8865_DCDC3_VSET2 0x41
101 #define ACT8865_DCDC3_CTRL 0x42
102 #define ACT8865_LDO1_VSET 0x50
103 #define ACT8865_LDO1_CTRL 0x51
104 #define ACT8865_LDO2_VSET 0x54
105 #define ACT8865_LDO2_CTRL 0x55
106 #define ACT8865_LDO3_VSET 0x60
107 #define ACT8865_LDO3_CTRL 0x61
108 #define ACT8865_LDO4_VSET 0x64
109 #define ACT8865_LDO4_CTRL 0x65
110 #define ACT8865_MSTROFF 0x20
115 #define ACT8865_ENA 0x80 /* ON - [7] */
116 #define ACT8865_DIS 0x40 /* DIS - [6] */
118 #define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
121 #define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
122 #define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
124 #define ACT8600_APCH_CHG_ACIN BIT(7)
125 #define ACT8600_APCH_CHG_USB BIT(6)
126 #define ACT8600_APCH_CSTATE0 BIT(5)
127 #define ACT8600_APCH_CSTATE1 BIT(4)
130 * ACT8865 voltage number
132 #define ACT8865_VOLTAGE_NUM 64
133 #define ACT8600_SUDCDC_VOLTAGE_NUM 256
136 struct regmap *regmap;
141 static const struct regmap_range act8600_reg_ranges[] = {
142 regmap_reg_range(0x00, 0x01),
143 regmap_reg_range(0x10, 0x10),
144 regmap_reg_range(0x12, 0x12),
145 regmap_reg_range(0x20, 0x20),
146 regmap_reg_range(0x22, 0x22),
147 regmap_reg_range(0x30, 0x30),
148 regmap_reg_range(0x32, 0x32),
149 regmap_reg_range(0x40, 0x41),
150 regmap_reg_range(0x50, 0x51),
151 regmap_reg_range(0x60, 0x61),
152 regmap_reg_range(0x70, 0x71),
153 regmap_reg_range(0x80, 0x81),
154 regmap_reg_range(0x91, 0x91),
155 regmap_reg_range(0xA1, 0xA1),
156 regmap_reg_range(0xA8, 0xAA),
157 regmap_reg_range(0xB0, 0xB0),
158 regmap_reg_range(0xB2, 0xB2),
159 regmap_reg_range(0xC1, 0xC1),
162 static const struct regmap_range act8600_reg_ro_ranges[] = {
163 regmap_reg_range(0xAA, 0xAA),
164 regmap_reg_range(0xC1, 0xC1),
167 static const struct regmap_range act8600_reg_volatile_ranges[] = {
168 regmap_reg_range(0x00, 0x01),
169 regmap_reg_range(0x12, 0x12),
170 regmap_reg_range(0x22, 0x22),
171 regmap_reg_range(0x32, 0x32),
172 regmap_reg_range(0x41, 0x41),
173 regmap_reg_range(0x51, 0x51),
174 regmap_reg_range(0x61, 0x61),
175 regmap_reg_range(0x71, 0x71),
176 regmap_reg_range(0x81, 0x81),
177 regmap_reg_range(0xA8, 0xA8),
178 regmap_reg_range(0xAA, 0xAA),
179 regmap_reg_range(0xB0, 0xB0),
180 regmap_reg_range(0xC1, 0xC1),
183 static const struct regmap_access_table act8600_write_ranges_table = {
184 .yes_ranges = act8600_reg_ranges,
185 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
186 .no_ranges = act8600_reg_ro_ranges,
187 .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges),
190 static const struct regmap_access_table act8600_read_ranges_table = {
191 .yes_ranges = act8600_reg_ranges,
192 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
195 static const struct regmap_access_table act8600_volatile_ranges_table = {
196 .yes_ranges = act8600_reg_volatile_ranges,
197 .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges),
200 static const struct regmap_config act8600_regmap_config = {
203 .max_register = 0xFF,
204 .wr_table = &act8600_write_ranges_table,
205 .rd_table = &act8600_read_ranges_table,
206 .volatile_table = &act8600_volatile_ranges_table,
209 static const struct regmap_config act8865_regmap_config = {
214 static const struct regulator_linear_range act8865_voltage_ranges[] = {
215 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
216 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
217 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
220 static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
221 REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
222 REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
223 REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
224 REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
225 REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
228 static const struct regulator_ops act8865_ops = {
229 .list_voltage = regulator_list_voltage_linear_range,
230 .map_voltage = regulator_map_voltage_linear_range,
231 .get_voltage_sel = regulator_get_voltage_sel_regmap,
232 .set_voltage_sel = regulator_set_voltage_sel_regmap,
233 .enable = regulator_enable_regmap,
234 .disable = regulator_disable_regmap,
235 .is_enabled = regulator_is_enabled_regmap,
238 static const struct regulator_ops act8865_ldo_ops = {
239 .list_voltage = regulator_list_voltage_linear_range,
240 .map_voltage = regulator_map_voltage_linear_range,
241 .get_voltage_sel = regulator_get_voltage_sel_regmap,
242 .set_voltage_sel = regulator_set_voltage_sel_regmap,
243 .enable = regulator_enable_regmap,
244 .disable = regulator_disable_regmap,
245 .is_enabled = regulator_is_enabled_regmap,
246 .set_pull_down = regulator_set_pull_down_regmap,
249 static const struct regulator_ops act8865_fixed_ldo_ops = {
250 .enable = regulator_enable_regmap,
251 .disable = regulator_disable_regmap,
252 .is_enabled = regulator_is_enabled_regmap,
255 #define ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, _ops) \
256 [_family##_ID_##_id] = { \
258 .of_match = of_match_ptr(_name), \
259 .regulators_node = of_match_ptr("regulators"), \
260 .supply_name = _supply, \
261 .id = _family##_ID_##_id, \
262 .type = REGULATOR_VOLTAGE, \
264 .n_voltages = ACT8865_VOLTAGE_NUM, \
265 .linear_ranges = act8865_voltage_ranges, \
266 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
267 .vsel_reg = _family##_##_id##_##_vsel_reg, \
268 .vsel_mask = ACT8865_VSEL_MASK, \
269 .enable_reg = _family##_##_id##_CTRL, \
270 .enable_mask = ACT8865_ENA, \
271 .pull_down_reg = _family##_##_id##_CTRL, \
272 .pull_down_mask = ACT8865_DIS, \
273 .owner = THIS_MODULE, \
276 #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
277 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ops)
279 #define ACT88xx_LDO(_name, _family, _id, _vsel_reg, _supply) \
280 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ldo_ops)
282 static const struct regulator_desc act8600_regulators[] = {
283 ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
284 ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
285 ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
287 .name = "SUDCDC_REG4",
288 .of_match = of_match_ptr("SUDCDC_REG4"),
289 .regulators_node = of_match_ptr("regulators"),
290 .id = ACT8600_ID_SUDCDC4,
292 .type = REGULATOR_VOLTAGE,
293 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
294 .linear_ranges = act8600_sudcdc_voltage_ranges,
295 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
296 .vsel_reg = ACT8600_SUDCDC4_VSET,
297 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
298 .enable_reg = ACT8600_SUDCDC4_CTRL,
299 .enable_mask = ACT8865_ENA,
300 .owner = THIS_MODULE,
302 ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
303 ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
304 ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
305 ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
308 .of_match = of_match_ptr("LDO_REG9"),
309 .regulators_node = of_match_ptr("regulators"),
310 .id = ACT8600_ID_LDO9,
311 .ops = &act8865_fixed_ldo_ops,
312 .type = REGULATOR_VOLTAGE,
315 .enable_reg = ACT8600_LDO910_CTRL,
316 .enable_mask = ACT8865_ENA,
317 .owner = THIS_MODULE,
321 .of_match = of_match_ptr("LDO_REG10"),
322 .regulators_node = of_match_ptr("regulators"),
323 .id = ACT8600_ID_LDO10,
324 .ops = &act8865_fixed_ldo_ops,
325 .type = REGULATOR_VOLTAGE,
328 .enable_reg = ACT8600_LDO910_CTRL,
329 .enable_mask = ACT8600_LDO10_ENA,
330 .owner = THIS_MODULE,
334 static const struct regulator_desc act8846_regulators[] = {
335 ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
336 ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
337 ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
338 ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
339 ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
340 ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
341 ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
342 ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
343 ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
344 ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
345 ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
346 ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
349 static const struct regulator_desc act8865_regulators[] = {
350 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
351 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
352 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
353 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
354 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
355 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
356 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
359 static const struct regulator_desc act8865_alt_regulators[] = {
360 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
361 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
362 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
363 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
364 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
365 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
366 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
370 static const struct of_device_id act8865_dt_ids[] = {
371 { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
372 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
373 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
376 MODULE_DEVICE_TABLE(of, act8865_dt_ids);
379 static struct act8865_regulator_data *act8865_get_regulator_data(
380 int id, struct act8865_platform_data *pdata)
384 for (i = 0; i < pdata->num_regulators; i++) {
385 if (pdata->regulators[i].id == id)
386 return &pdata->regulators[i];
392 static struct i2c_client *act8865_i2c_client;
393 static void act8865_power_off(void)
395 struct act8865 *act8865;
397 act8865 = i2c_get_clientdata(act8865_i2c_client);
398 regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
402 static int act8600_charger_get_status(struct regmap *map)
408 ret = regmap_read(map, ACT8600_APCH_STAT, &val);
412 state0 = val & ACT8600_APCH_CSTATE0;
413 state1 = val & ACT8600_APCH_CSTATE1;
415 if (state0 && !state1)
416 return POWER_SUPPLY_STATUS_CHARGING;
417 if (!state0 && state1)
418 return POWER_SUPPLY_STATUS_NOT_CHARGING;
419 if (!state0 && !state1)
420 return POWER_SUPPLY_STATUS_DISCHARGING;
422 return POWER_SUPPLY_STATUS_UNKNOWN;
425 static int act8600_charger_get_property(struct power_supply *psy,
426 enum power_supply_property psp, union power_supply_propval *val)
428 struct regmap *map = power_supply_get_drvdata(psy);
432 case POWER_SUPPLY_PROP_STATUS:
433 ret = act8600_charger_get_status(map);
446 static enum power_supply_property act8600_charger_properties[] = {
447 POWER_SUPPLY_PROP_STATUS,
450 static const struct power_supply_desc act8600_charger_desc = {
451 .name = "act8600-charger",
452 .type = POWER_SUPPLY_TYPE_BATTERY,
453 .properties = act8600_charger_properties,
454 .num_properties = ARRAY_SIZE(act8600_charger_properties),
455 .get_property = act8600_charger_get_property,
458 static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
460 struct power_supply *charger;
461 struct power_supply_config cfg = {
463 .of_node = dev->of_node,
466 charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
468 return PTR_ERR_OR_ZERO(charger);
471 static int act8865_pmic_probe(struct i2c_client *client,
472 const struct i2c_device_id *i2c_id)
474 const struct regulator_desc *regulators;
475 struct act8865_platform_data *pdata = NULL;
476 struct device *dev = &client->dev;
477 int i, ret, num_regulators;
478 struct act8865 *act8865;
479 const struct regmap_config *regmap_config;
481 int off_reg, off_mask;
482 int voltage_select = 0;
485 const struct of_device_id *id;
487 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
491 type = (unsigned long) id->data;
493 voltage_select = !!of_get_property(dev->of_node,
494 "active-semi,vsel-high",
497 type = i2c_id->driver_data;
498 pdata = dev_get_platdata(dev);
503 regulators = act8600_regulators;
504 num_regulators = ARRAY_SIZE(act8600_regulators);
505 regmap_config = &act8600_regmap_config;
510 regulators = act8846_regulators;
511 num_regulators = ARRAY_SIZE(act8846_regulators);
512 regmap_config = &act8865_regmap_config;
513 off_reg = ACT8846_GLB_OFF_CTRL;
514 off_mask = ACT8846_OFF_SYSMASK;
517 if (voltage_select) {
518 regulators = act8865_alt_regulators;
519 num_regulators = ARRAY_SIZE(act8865_alt_regulators);
521 regulators = act8865_regulators;
522 num_regulators = ARRAY_SIZE(act8865_regulators);
524 regmap_config = &act8865_regmap_config;
525 off_reg = ACT8865_SYS_CTRL;
526 off_mask = ACT8865_MSTROFF;
529 dev_err(dev, "invalid device id %lu\n", type);
533 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
537 act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
538 if (IS_ERR(act8865->regmap)) {
539 ret = PTR_ERR(act8865->regmap);
540 dev_err(dev, "Failed to allocate register map: %d\n", ret);
544 if (of_device_is_system_power_controller(dev->of_node)) {
545 if (!pm_power_off && (off_reg > 0)) {
546 act8865_i2c_client = client;
547 act8865->off_reg = off_reg;
548 act8865->off_mask = off_mask;
549 pm_power_off = act8865_power_off;
551 dev_err(dev, "Failed to set poweroff capability, already defined\n");
555 /* Finally register devices */
556 for (i = 0; i < num_regulators; i++) {
557 const struct regulator_desc *desc = ®ulators[i];
558 struct regulator_config config = { };
559 struct regulator_dev *rdev;
562 config.driver_data = act8865;
563 config.regmap = act8865->regmap;
566 struct act8865_regulator_data *rdata;
568 rdata = act8865_get_regulator_data(desc->id, pdata);
570 config.init_data = rdata->init_data;
571 config.of_node = rdata->of_node;
575 rdev = devm_regulator_register(dev, desc, &config);
577 dev_err(dev, "failed to register %s\n", desc->name);
578 return PTR_ERR(rdev);
582 if (type == ACT8600) {
583 ret = act8600_charger_probe(dev, act8865->regmap);
585 if (ret != -EPROBE_DEFER)
586 dev_err(dev, "Failed to probe charger");
591 i2c_set_clientdata(client, act8865);
596 static const struct i2c_device_id act8865_ids[] = {
597 { .name = "act8600", .driver_data = ACT8600 },
598 { .name = "act8846", .driver_data = ACT8846 },
599 { .name = "act8865", .driver_data = ACT8865 },
602 MODULE_DEVICE_TABLE(i2c, act8865_ids);
604 static struct i2c_driver act8865_pmic_driver = {
608 .probe = act8865_pmic_probe,
609 .id_table = act8865_ids,
612 module_i2c_driver(act8865_pmic_driver);
614 MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
615 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
616 MODULE_LICENSE("GPL v2");