blob: 6a62f946ccae44e5fbb3855a60eec9f9cdeb7a95 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wenyou Yang38c09962016-02-05 12:33:30 +08002/*
3 * Voltage regulation driver for active-semi ACT8945A PMIC
4 *
5 * Copyright (C) 2015 Atmel Corporation
6 *
7 * Author: Wenyou Yang <wenyou.yang@atmel.com>
Wenyou Yang38c09962016-02-05 12:33:30 +08008 */
9
10#include <linux/module.h>
11#include <linux/of_device.h>
12#include <linux/platform_device.h>
Claudiu Beznea90bc8ac22018-12-11 15:56:36 +000013#include <linux/regmap.h>
Wenyou Yang38c09962016-02-05 12:33:30 +080014#include <linux/regulator/driver.h>
15#include <linux/regulator/machine.h>
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000016#include <dt-bindings/regulator/active-semi,8945a-regulator.h>
Wenyou Yang38c09962016-02-05 12:33:30 +080017
18/**
19 * ACT8945A Global Register Map.
20 */
21#define ACT8945A_SYS_MODE 0x00
22#define ACT8945A_SYS_CTRL 0x01
Claudiu Beznea90bc8ac22018-12-11 15:56:36 +000023#define ACT8945A_SYS_UNLK_REGS 0x0b
Wenyou Yang38c09962016-02-05 12:33:30 +080024#define ACT8945A_DCDC1_VSET1 0x20
25#define ACT8945A_DCDC1_VSET2 0x21
26#define ACT8945A_DCDC1_CTRL 0x22
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000027#define ACT8945A_DCDC1_SUS 0x24
Wenyou Yang38c09962016-02-05 12:33:30 +080028#define ACT8945A_DCDC2_VSET1 0x30
29#define ACT8945A_DCDC2_VSET2 0x31
30#define ACT8945A_DCDC2_CTRL 0x32
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000031#define ACT8945A_DCDC2_SUS 0x34
Wenyou Yang38c09962016-02-05 12:33:30 +080032#define ACT8945A_DCDC3_VSET1 0x40
33#define ACT8945A_DCDC3_VSET2 0x41
34#define ACT8945A_DCDC3_CTRL 0x42
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000035#define ACT8945A_DCDC3_SUS 0x44
Wenyou Yang38c09962016-02-05 12:33:30 +080036#define ACT8945A_LDO1_VSET 0x50
37#define ACT8945A_LDO1_CTRL 0x51
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000038#define ACT8945A_LDO1_SUS 0x52
Wenyou Yang38c09962016-02-05 12:33:30 +080039#define ACT8945A_LDO2_VSET 0x54
40#define ACT8945A_LDO2_CTRL 0x55
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000041#define ACT8945A_LDO2_SUS 0x56
Wenyou Yang38c09962016-02-05 12:33:30 +080042#define ACT8945A_LDO3_VSET 0x60
43#define ACT8945A_LDO3_CTRL 0x61
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000044#define ACT8945A_LDO3_SUS 0x62
Wenyou Yang38c09962016-02-05 12:33:30 +080045#define ACT8945A_LDO4_VSET 0x64
46#define ACT8945A_LDO4_CTRL 0x65
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000047#define ACT8945A_LDO4_SUS 0x66
Wenyou Yang38c09962016-02-05 12:33:30 +080048
49/**
50 * Field Definitions.
51 */
52#define ACT8945A_ENA 0x80 /* ON - [7] */
53#define ACT8945A_VSEL_MASK 0x3F /* VSET - [5:0] */
54
55/**
56 * ACT8945A Voltage Number
57 */
58#define ACT8945A_VOLTAGE_NUM 64
59
60enum {
61 ACT8945A_ID_DCDC1,
62 ACT8945A_ID_DCDC2,
63 ACT8945A_ID_DCDC3,
64 ACT8945A_ID_LDO1,
65 ACT8945A_ID_LDO2,
66 ACT8945A_ID_LDO3,
67 ACT8945A_ID_LDO4,
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000068 ACT8945A_ID_MAX,
69};
70
71struct act8945a_pmic {
72 struct regmap *regmap;
73 u32 op_mode[ACT8945A_ID_MAX];
Wenyou Yang38c09962016-02-05 12:33:30 +080074};
75
Matti Vaittinen60ab7f42020-05-08 18:43:36 +030076static const struct linear_range act8945a_voltage_ranges[] = {
Wenyou Yang38c09962016-02-05 12:33:30 +080077 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
78 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
79 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
80};
81
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000082static int act8945a_set_suspend_state(struct regulator_dev *rdev, bool enable)
83{
84 struct regmap *regmap = rdev->regmap;
Axel Lin557ce9d2019-01-04 22:42:36 +080085 int id = rdev_get_id(rdev);
86 int reg, val;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +000087
88 switch (id) {
89 case ACT8945A_ID_DCDC1:
90 reg = ACT8945A_DCDC1_SUS;
91 val = 0xa8;
92 break;
93 case ACT8945A_ID_DCDC2:
94 reg = ACT8945A_DCDC2_SUS;
95 val = 0xa8;
96 break;
97 case ACT8945A_ID_DCDC3:
98 reg = ACT8945A_DCDC3_SUS;
99 val = 0xa8;
100 break;
101 case ACT8945A_ID_LDO1:
102 reg = ACT8945A_LDO1_SUS;
103 val = 0xe8;
104 break;
105 case ACT8945A_ID_LDO2:
106 reg = ACT8945A_LDO2_SUS;
107 val = 0xe8;
108 break;
109 case ACT8945A_ID_LDO3:
110 reg = ACT8945A_LDO3_SUS;
111 val = 0xe8;
112 break;
113 case ACT8945A_ID_LDO4:
114 reg = ACT8945A_LDO4_SUS;
115 val = 0xe8;
116 break;
117 default:
118 return -EINVAL;
119 }
120
121 if (enable)
122 val |= BIT(4);
123
124 /*
125 * Ask the PMIC to enable/disable this output when entering hibernate
126 * mode.
127 */
128 return regmap_write(regmap, reg, val);
129}
130
131static int act8945a_set_suspend_enable(struct regulator_dev *rdev)
132{
133 return act8945a_set_suspend_state(rdev, true);
134}
135
136static int act8945a_set_suspend_disable(struct regulator_dev *rdev)
137{
138 return act8945a_set_suspend_state(rdev, false);
139}
140
141static unsigned int act8945a_of_map_mode(unsigned int mode)
142{
143 switch (mode) {
144 case ACT8945A_REGULATOR_MODE_FIXED:
145 case ACT8945A_REGULATOR_MODE_NORMAL:
146 return REGULATOR_MODE_NORMAL;
147 case ACT8945A_REGULATOR_MODE_LOWPOWER:
148 return REGULATOR_MODE_STANDBY;
149 default:
150 return REGULATOR_MODE_INVALID;
151 }
152}
153
154static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode)
155{
156 struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
157 struct regmap *regmap = rdev->regmap;
Axel Lin557ce9d2019-01-04 22:42:36 +0800158 int id = rdev_get_id(rdev);
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000159 int reg, ret, val = 0;
160
161 switch (id) {
162 case ACT8945A_ID_DCDC1:
163 reg = ACT8945A_DCDC1_CTRL;
164 break;
165 case ACT8945A_ID_DCDC2:
166 reg = ACT8945A_DCDC2_CTRL;
167 break;
168 case ACT8945A_ID_DCDC3:
169 reg = ACT8945A_DCDC3_CTRL;
170 break;
171 case ACT8945A_ID_LDO1:
Raag Jadav5d2fc542019-08-13 00:09:54 +0530172 reg = ACT8945A_LDO1_CTRL;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000173 break;
174 case ACT8945A_ID_LDO2:
Raag Jadav5d2fc542019-08-13 00:09:54 +0530175 reg = ACT8945A_LDO2_CTRL;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000176 break;
177 case ACT8945A_ID_LDO3:
Raag Jadav5d2fc542019-08-13 00:09:54 +0530178 reg = ACT8945A_LDO3_CTRL;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000179 break;
180 case ACT8945A_ID_LDO4:
Raag Jadav5d2fc542019-08-13 00:09:54 +0530181 reg = ACT8945A_LDO4_CTRL;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000182 break;
183 default:
184 return -EINVAL;
185 }
186
187 switch (mode) {
188 case REGULATOR_MODE_STANDBY:
Axel Lin557ce9d2019-01-04 22:42:36 +0800189 if (id > ACT8945A_ID_DCDC3)
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000190 val = BIT(5);
191 break;
192 case REGULATOR_MODE_NORMAL:
Axel Lin557ce9d2019-01-04 22:42:36 +0800193 if (id <= ACT8945A_ID_DCDC3)
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000194 val = BIT(5);
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 ret = regmap_update_bits(regmap, reg, BIT(5), val);
201 if (ret)
202 return ret;
203
204 act8945a->op_mode[id] = mode;
205
206 return 0;
207}
208
209static unsigned int act8945a_get_mode(struct regulator_dev *rdev)
210{
211 struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
Axel Lin557ce9d2019-01-04 22:42:36 +0800212 int id = rdev_get_id(rdev);
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000213
214 if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX)
215 return -EINVAL;
216
217 return act8945a->op_mode[id];
218}
219
Bhumika Goyal1ed1da32017-01-28 19:03:28 +0530220static const struct regulator_ops act8945a_ops = {
Wenyou Yang38c09962016-02-05 12:33:30 +0800221 .list_voltage = regulator_list_voltage_linear_range,
222 .map_voltage = regulator_map_voltage_linear_range,
223 .get_voltage_sel = regulator_get_voltage_sel_regmap,
224 .set_voltage_sel = regulator_set_voltage_sel_regmap,
225 .enable = regulator_enable_regmap,
226 .disable = regulator_disable_regmap,
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000227 .set_mode = act8945a_set_mode,
228 .get_mode = act8945a_get_mode,
Wenyou Yang38c09962016-02-05 12:33:30 +0800229 .is_enabled = regulator_is_enabled_regmap,
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000230 .set_suspend_enable = act8945a_set_suspend_enable,
231 .set_suspend_disable = act8945a_set_suspend_disable,
Wenyou Yang38c09962016-02-05 12:33:30 +0800232};
233
234#define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply) \
235 [_family##_ID_##_id] = { \
236 .name = _name, \
237 .supply_name = _supply, \
238 .of_match = of_match_ptr("REG_"#_id), \
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000239 .of_map_mode = act8945a_of_map_mode, \
Wenyou Yang38c09962016-02-05 12:33:30 +0800240 .regulators_node = of_match_ptr("regulators"), \
241 .id = _family##_ID_##_id, \
242 .type = REGULATOR_VOLTAGE, \
243 .ops = &act8945a_ops, \
244 .n_voltages = ACT8945A_VOLTAGE_NUM, \
245 .linear_ranges = act8945a_voltage_ranges, \
246 .n_linear_ranges = ARRAY_SIZE(act8945a_voltage_ranges), \
247 .vsel_reg = _family##_##_id##_##_vsel_reg, \
248 .vsel_mask = ACT8945A_VSEL_MASK, \
249 .enable_reg = _family##_##_id##_CTRL, \
250 .enable_mask = ACT8945A_ENA, \
251 .owner = THIS_MODULE, \
252 }
253
254static const struct regulator_desc act8945a_regulators[] = {
255 ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET1, "vp1"),
256 ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET1, "vp2"),
257 ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET1, "vp3"),
258 ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
259 ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
260 ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
261 ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
262};
263
264static const struct regulator_desc act8945a_alt_regulators[] = {
265 ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET2, "vp1"),
266 ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET2, "vp2"),
267 ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET2, "vp3"),
268 ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
269 ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
270 ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
271 ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
272};
273
274static int act8945a_pmic_probe(struct platform_device *pdev)
275{
276 struct regulator_config config = { };
277 const struct regulator_desc *regulators;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000278 struct act8945a_pmic *act8945a;
Wenyou Yang38c09962016-02-05 12:33:30 +0800279 struct regulator_dev *rdev;
280 int i, num_regulators;
281 bool voltage_select;
282
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000283 act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL);
284 if (!act8945a)
285 return -ENOMEM;
286
287 act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL);
288 if (!act8945a->regmap) {
Claudiu Beznea90bc8ac22018-12-11 15:56:36 +0000289 dev_err(&pdev->dev,
290 "could not retrieve regmap from parent device\n");
291 return -EINVAL;
292 }
293
Wenyou Yang38c09962016-02-05 12:33:30 +0800294 voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
295 "active-semi,vsel-high");
296
297 if (voltage_select) {
298 regulators = act8945a_alt_regulators;
299 num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
300 } else {
301 regulators = act8945a_regulators;
302 num_regulators = ARRAY_SIZE(act8945a_regulators);
303 }
304
305 config.dev = &pdev->dev;
306 config.dev->of_node = pdev->dev.parent->of_node;
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000307 config.driver_data = act8945a;
Wenyou Yang38c09962016-02-05 12:33:30 +0800308 for (i = 0; i < num_regulators; i++) {
Claudiu Beznea03ffbf92018-12-11 15:56:45 +0000309 rdev = devm_regulator_register(&pdev->dev, &regulators[i],
310 &config);
Wenyou Yang38c09962016-02-05 12:33:30 +0800311 if (IS_ERR(rdev)) {
312 dev_err(&pdev->dev,
313 "failed to register %s regulator\n",
314 regulators[i].name);
315 return PTR_ERR(rdev);
316 }
317 }
318
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000319 platform_set_drvdata(pdev, act8945a);
320
Claudiu Beznea90bc8ac22018-12-11 15:56:36 +0000321 /* Unlock expert registers. */
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000322 return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
Wenyou Yang38c09962016-02-05 12:33:30 +0800323}
324
Claudiu Beznea22a5a4a2018-12-17 10:37:51 +0000325static int __maybe_unused act8945a_suspend(struct device *pdev)
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000326{
327 struct act8945a_pmic *act8945a = dev_get_drvdata(pdev);
328
329 /*
330 * Ask the PMIC to enter the suspend mode on the next PWRHLD
331 * transition.
332 */
333 return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42);
334}
335
Wei Yongjun78e6fbf2018-12-18 07:10:21 +0000336static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL);
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000337
Claudiu Bezneab5ebba42018-12-11 15:56:50 +0000338static void act8945a_pmic_shutdown(struct platform_device *pdev)
339{
340 struct act8945a_pmic *act8945a = platform_get_drvdata(pdev);
341
342 /*
343 * Ask the PMIC to shutdown everything on the next PWRHLD transition.
344 */
345 regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0);
346}
347
Wenyou Yang38c09962016-02-05 12:33:30 +0800348static struct platform_driver act8945a_pmic_driver = {
349 .driver = {
350 .name = "act8945a-regulator",
Boris Brezillon7482d6ec2018-12-11 15:56:41 +0000351 .pm = &act8945a_pm,
Wenyou Yang38c09962016-02-05 12:33:30 +0800352 },
353 .probe = act8945a_pmic_probe,
Claudiu Bezneab5ebba42018-12-11 15:56:50 +0000354 .shutdown = act8945a_pmic_shutdown,
Wenyou Yang38c09962016-02-05 12:33:30 +0800355};
356module_platform_driver(act8945a_pmic_driver);
357
358MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
359MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
360MODULE_LICENSE("GPL");