2 * Voltage regulation driver for active-semi ACT8945A PMIC
4 * Copyright (C) 2015 Atmel Corporation
6 * Author: Wenyou Yang <wenyou.yang@atmel.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <dt-bindings/regulator/active-semi,8945a-regulator.h>
24 * ACT8945A Global Register Map.
26 #define ACT8945A_SYS_MODE 0x00
27 #define ACT8945A_SYS_CTRL 0x01
28 #define ACT8945A_SYS_UNLK_REGS 0x0b
29 #define ACT8945A_DCDC1_VSET1 0x20
30 #define ACT8945A_DCDC1_VSET2 0x21
31 #define ACT8945A_DCDC1_CTRL 0x22
32 #define ACT8945A_DCDC1_SUS 0x24
33 #define ACT8945A_DCDC2_VSET1 0x30
34 #define ACT8945A_DCDC2_VSET2 0x31
35 #define ACT8945A_DCDC2_CTRL 0x32
36 #define ACT8945A_DCDC2_SUS 0x34
37 #define ACT8945A_DCDC3_VSET1 0x40
38 #define ACT8945A_DCDC3_VSET2 0x41
39 #define ACT8945A_DCDC3_CTRL 0x42
40 #define ACT8945A_DCDC3_SUS 0x44
41 #define ACT8945A_LDO1_VSET 0x50
42 #define ACT8945A_LDO1_CTRL 0x51
43 #define ACT8945A_LDO1_SUS 0x52
44 #define ACT8945A_LDO2_VSET 0x54
45 #define ACT8945A_LDO2_CTRL 0x55
46 #define ACT8945A_LDO2_SUS 0x56
47 #define ACT8945A_LDO3_VSET 0x60
48 #define ACT8945A_LDO3_CTRL 0x61
49 #define ACT8945A_LDO3_SUS 0x62
50 #define ACT8945A_LDO4_VSET 0x64
51 #define ACT8945A_LDO4_CTRL 0x65
52 #define ACT8945A_LDO4_SUS 0x66
57 #define ACT8945A_ENA 0x80 /* ON - [7] */
58 #define ACT8945A_VSEL_MASK 0x3F /* VSET - [5:0] */
61 * ACT8945A Voltage Number
63 #define ACT8945A_VOLTAGE_NUM 64
76 struct act8945a_pmic {
77 struct regmap *regmap;
78 u32 op_mode[ACT8945A_ID_MAX];
81 static const struct regulator_linear_range act8945a_voltage_ranges[] = {
82 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
83 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
84 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
87 static int act8945a_set_suspend_state(struct regulator_dev *rdev, bool enable)
89 struct regmap *regmap = rdev->regmap;
90 int id = rdev_get_id(rdev);
94 case ACT8945A_ID_DCDC1:
95 reg = ACT8945A_DCDC1_SUS;
98 case ACT8945A_ID_DCDC2:
99 reg = ACT8945A_DCDC2_SUS;
102 case ACT8945A_ID_DCDC3:
103 reg = ACT8945A_DCDC3_SUS;
106 case ACT8945A_ID_LDO1:
107 reg = ACT8945A_LDO1_SUS;
110 case ACT8945A_ID_LDO2:
111 reg = ACT8945A_LDO2_SUS;
114 case ACT8945A_ID_LDO3:
115 reg = ACT8945A_LDO3_SUS;
118 case ACT8945A_ID_LDO4:
119 reg = ACT8945A_LDO4_SUS;
130 * Ask the PMIC to enable/disable this output when entering hibernate
133 return regmap_write(regmap, reg, val);
136 static int act8945a_set_suspend_enable(struct regulator_dev *rdev)
138 return act8945a_set_suspend_state(rdev, true);
141 static int act8945a_set_suspend_disable(struct regulator_dev *rdev)
143 return act8945a_set_suspend_state(rdev, false);
146 static unsigned int act8945a_of_map_mode(unsigned int mode)
149 case ACT8945A_REGULATOR_MODE_FIXED:
150 case ACT8945A_REGULATOR_MODE_NORMAL:
151 return REGULATOR_MODE_NORMAL;
152 case ACT8945A_REGULATOR_MODE_LOWPOWER:
153 return REGULATOR_MODE_STANDBY;
155 return REGULATOR_MODE_INVALID;
159 static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode)
161 struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
162 struct regmap *regmap = rdev->regmap;
163 int id = rdev_get_id(rdev);
164 int reg, ret, val = 0;
167 case ACT8945A_ID_DCDC1:
168 reg = ACT8945A_DCDC1_CTRL;
170 case ACT8945A_ID_DCDC2:
171 reg = ACT8945A_DCDC2_CTRL;
173 case ACT8945A_ID_DCDC3:
174 reg = ACT8945A_DCDC3_CTRL;
176 case ACT8945A_ID_LDO1:
177 reg = ACT8945A_LDO1_SUS;
179 case ACT8945A_ID_LDO2:
180 reg = ACT8945A_LDO2_SUS;
182 case ACT8945A_ID_LDO3:
183 reg = ACT8945A_LDO3_SUS;
185 case ACT8945A_ID_LDO4:
186 reg = ACT8945A_LDO4_SUS;
193 case REGULATOR_MODE_STANDBY:
194 if (id > ACT8945A_ID_DCDC3)
197 case REGULATOR_MODE_NORMAL:
198 if (id <= ACT8945A_ID_DCDC3)
205 ret = regmap_update_bits(regmap, reg, BIT(5), val);
209 act8945a->op_mode[id] = mode;
214 static unsigned int act8945a_get_mode(struct regulator_dev *rdev)
216 struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
217 int id = rdev_get_id(rdev);
219 if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX)
222 return act8945a->op_mode[id];
225 static const struct regulator_ops act8945a_ops = {
226 .list_voltage = regulator_list_voltage_linear_range,
227 .map_voltage = regulator_map_voltage_linear_range,
228 .get_voltage_sel = regulator_get_voltage_sel_regmap,
229 .set_voltage_sel = regulator_set_voltage_sel_regmap,
230 .enable = regulator_enable_regmap,
231 .disable = regulator_disable_regmap,
232 .set_mode = act8945a_set_mode,
233 .get_mode = act8945a_get_mode,
234 .is_enabled = regulator_is_enabled_regmap,
235 .set_suspend_enable = act8945a_set_suspend_enable,
236 .set_suspend_disable = act8945a_set_suspend_disable,
239 #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply) \
240 [_family##_ID_##_id] = { \
242 .supply_name = _supply, \
243 .of_match = of_match_ptr("REG_"#_id), \
244 .of_map_mode = act8945a_of_map_mode, \
245 .regulators_node = of_match_ptr("regulators"), \
246 .id = _family##_ID_##_id, \
247 .type = REGULATOR_VOLTAGE, \
248 .ops = &act8945a_ops, \
249 .n_voltages = ACT8945A_VOLTAGE_NUM, \
250 .linear_ranges = act8945a_voltage_ranges, \
251 .n_linear_ranges = ARRAY_SIZE(act8945a_voltage_ranges), \
252 .vsel_reg = _family##_##_id##_##_vsel_reg, \
253 .vsel_mask = ACT8945A_VSEL_MASK, \
254 .enable_reg = _family##_##_id##_CTRL, \
255 .enable_mask = ACT8945A_ENA, \
256 .owner = THIS_MODULE, \
259 static const struct regulator_desc act8945a_regulators[] = {
260 ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET1, "vp1"),
261 ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET1, "vp2"),
262 ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET1, "vp3"),
263 ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
264 ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
265 ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
266 ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
269 static const struct regulator_desc act8945a_alt_regulators[] = {
270 ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET2, "vp1"),
271 ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET2, "vp2"),
272 ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET2, "vp3"),
273 ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
274 ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
275 ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
276 ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
279 static int act8945a_pmic_probe(struct platform_device *pdev)
281 struct regulator_config config = { };
282 const struct regulator_desc *regulators;
283 struct act8945a_pmic *act8945a;
284 struct regulator_dev *rdev;
285 int i, num_regulators;
288 act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL);
292 act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL);
293 if (!act8945a->regmap) {
295 "could not retrieve regmap from parent device\n");
299 voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
300 "active-semi,vsel-high");
302 if (voltage_select) {
303 regulators = act8945a_alt_regulators;
304 num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
306 regulators = act8945a_regulators;
307 num_regulators = ARRAY_SIZE(act8945a_regulators);
310 config.dev = &pdev->dev;
311 config.dev->of_node = pdev->dev.parent->of_node;
312 config.driver_data = act8945a;
313 for (i = 0; i < num_regulators; i++) {
314 rdev = devm_regulator_register(&pdev->dev, ®ulators[i],
318 "failed to register %s regulator\n",
320 return PTR_ERR(rdev);
324 platform_set_drvdata(pdev, act8945a);
326 /* Unlock expert registers. */
327 return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
330 static int __maybe_unused act8945a_suspend(struct device *pdev)
332 struct act8945a_pmic *act8945a = dev_get_drvdata(pdev);
335 * Ask the PMIC to enter the suspend mode on the next PWRHLD
338 return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42);
341 static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL);
343 static void act8945a_pmic_shutdown(struct platform_device *pdev)
345 struct act8945a_pmic *act8945a = platform_get_drvdata(pdev);
348 * Ask the PMIC to shutdown everything on the next PWRHLD transition.
350 regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0);
353 static struct platform_driver act8945a_pmic_driver = {
355 .name = "act8945a-regulator",
358 .probe = act8945a_pmic_probe,
359 .shutdown = act8945a_pmic_shutdown,
361 module_platform_driver(act8945a_pmic_driver);
363 MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
364 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
365 MODULE_LICENSE("GPL");