1 /*
2  * Voltage regulation driver for active-semi ACT8945A PMIC
3  *
4  * Copyright (C) 2015 Atmel Corporation
5  *
6  * Author: Wenyou Yang <wenyou.yang@atmel.com>
7  *
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.
12  *
13  */
14 
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>
22 
23 /**
24  * ACT8945A Global Register Map.
25  */
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
53 
54 /**
55  * Field Definitions.
56  */
57 #define ACT8945A_ENA		0x80	/* ON - [7] */
58 #define ACT8945A_VSEL_MASK	0x3F	/* VSET - [5:0] */
59 
60 /**
61  * ACT8945A Voltage Number
62  */
63 #define ACT8945A_VOLTAGE_NUM	64
64 
65 enum {
66 	ACT8945A_ID_DCDC1,
67 	ACT8945A_ID_DCDC2,
68 	ACT8945A_ID_DCDC3,
69 	ACT8945A_ID_LDO1,
70 	ACT8945A_ID_LDO2,
71 	ACT8945A_ID_LDO3,
72 	ACT8945A_ID_LDO4,
73 	ACT8945A_ID_MAX,
74 };
75 
76 struct act8945a_pmic {
77 	struct regmap *regmap;
78 	u32 op_mode[ACT8945A_ID_MAX];
79 };
80 
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),
85 };
86 
87 static int act8945a_set_suspend_state(struct regulator_dev *rdev, bool enable)
88 {
89 	struct regmap *regmap = rdev->regmap;
90 	int id = rdev_get_id(rdev);
91 	int reg, val;
92 
93 	switch (id) {
94 	case ACT8945A_ID_DCDC1:
95 		reg = ACT8945A_DCDC1_SUS;
96 		val = 0xa8;
97 		break;
98 	case ACT8945A_ID_DCDC2:
99 		reg = ACT8945A_DCDC2_SUS;
100 		val = 0xa8;
101 		break;
102 	case ACT8945A_ID_DCDC3:
103 		reg = ACT8945A_DCDC3_SUS;
104 		val = 0xa8;
105 		break;
106 	case ACT8945A_ID_LDO1:
107 		reg = ACT8945A_LDO1_SUS;
108 		val = 0xe8;
109 		break;
110 	case ACT8945A_ID_LDO2:
111 		reg = ACT8945A_LDO2_SUS;
112 		val = 0xe8;
113 		break;
114 	case ACT8945A_ID_LDO3:
115 		reg = ACT8945A_LDO3_SUS;
116 		val = 0xe8;
117 		break;
118 	case ACT8945A_ID_LDO4:
119 		reg = ACT8945A_LDO4_SUS;
120 		val = 0xe8;
121 		break;
122 	default:
123 		return -EINVAL;
124 	}
125 
126 	if (enable)
127 		val |= BIT(4);
128 
129 	/*
130 	 * Ask the PMIC to enable/disable this output when entering hibernate
131 	 * mode.
132 	 */
133 	return regmap_write(regmap, reg, val);
134 }
135 
136 static int act8945a_set_suspend_enable(struct regulator_dev *rdev)
137 {
138 	return act8945a_set_suspend_state(rdev, true);
139 }
140 
141 static int act8945a_set_suspend_disable(struct regulator_dev *rdev)
142 {
143 	return act8945a_set_suspend_state(rdev, false);
144 }
145 
146 static unsigned int act8945a_of_map_mode(unsigned int mode)
147 {
148 	switch (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;
154 	default:
155 		return REGULATOR_MODE_INVALID;
156 	}
157 }
158 
159 static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode)
160 {
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;
165 
166 	switch (id) {
167 	case ACT8945A_ID_DCDC1:
168 		reg = ACT8945A_DCDC1_CTRL;
169 		break;
170 	case ACT8945A_ID_DCDC2:
171 		reg = ACT8945A_DCDC2_CTRL;
172 		break;
173 	case ACT8945A_ID_DCDC3:
174 		reg = ACT8945A_DCDC3_CTRL;
175 		break;
176 	case ACT8945A_ID_LDO1:
177 		reg = ACT8945A_LDO1_SUS;
178 		break;
179 	case ACT8945A_ID_LDO2:
180 		reg = ACT8945A_LDO2_SUS;
181 		break;
182 	case ACT8945A_ID_LDO3:
183 		reg = ACT8945A_LDO3_SUS;
184 		break;
185 	case ACT8945A_ID_LDO4:
186 		reg = ACT8945A_LDO4_SUS;
187 		break;
188 	default:
189 		return -EINVAL;
190 	}
191 
192 	switch (mode) {
193 	case REGULATOR_MODE_STANDBY:
194 		if (id > ACT8945A_ID_DCDC3)
195 			val = BIT(5);
196 		break;
197 	case REGULATOR_MODE_NORMAL:
198 		if (id <= ACT8945A_ID_DCDC3)
199 			val = BIT(5);
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	ret = regmap_update_bits(regmap, reg, BIT(5), val);
206 	if (ret)
207 		return ret;
208 
209 	act8945a->op_mode[id] = mode;
210 
211 	return 0;
212 }
213 
214 static unsigned int act8945a_get_mode(struct regulator_dev *rdev)
215 {
216 	struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
217 	int id = rdev_get_id(rdev);
218 
219 	if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX)
220 		return -EINVAL;
221 
222 	return act8945a->op_mode[id];
223 }
224 
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,
237 };
238 
239 #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply)		\
240 	[_family##_ID_##_id] = {					\
241 		.name			= _name,			\
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,			\
257 	}
258 
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"),
267 };
268 
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"),
277 };
278 
279 static int act8945a_pmic_probe(struct platform_device *pdev)
280 {
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;
286 	bool voltage_select;
287 
288 	act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL);
289 	if (!act8945a)
290 		return -ENOMEM;
291 
292 	act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL);
293 	if (!act8945a->regmap) {
294 		dev_err(&pdev->dev,
295 			"could not retrieve regmap from parent device\n");
296 		return -EINVAL;
297 	}
298 
299 	voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
300 					       "active-semi,vsel-high");
301 
302 	if (voltage_select) {
303 		regulators = act8945a_alt_regulators;
304 		num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
305 	} else {
306 		regulators = act8945a_regulators;
307 		num_regulators = ARRAY_SIZE(act8945a_regulators);
308 	}
309 
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, &regulators[i],
315 					       &config);
316 		if (IS_ERR(rdev)) {
317 			dev_err(&pdev->dev,
318 				"failed to register %s regulator\n",
319 				regulators[i].name);
320 			return PTR_ERR(rdev);
321 		}
322 	}
323 
324 	platform_set_drvdata(pdev, act8945a);
325 
326 	/* Unlock expert registers. */
327 	return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
328 }
329 
330 static int __maybe_unused act8945a_suspend(struct device *pdev)
331 {
332 	struct act8945a_pmic *act8945a = dev_get_drvdata(pdev);
333 
334 	/*
335 	 * Ask the PMIC to enter the suspend mode on the next PWRHLD
336 	 * transition.
337 	 */
338 	return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42);
339 }
340 
341 static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL);
342 
343 static void act8945a_pmic_shutdown(struct platform_device *pdev)
344 {
345 	struct act8945a_pmic *act8945a = platform_get_drvdata(pdev);
346 
347 	/*
348 	 * Ask the PMIC to shutdown everything on the next PWRHLD transition.
349 	 */
350 	regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0);
351 }
352 
353 static struct platform_driver act8945a_pmic_driver = {
354 	.driver = {
355 		.name = "act8945a-regulator",
356 		.pm = &act8945a_pm,
357 	},
358 	.probe = act8945a_pmic_probe,
359 	.shutdown = act8945a_pmic_shutdown,
360 };
361 module_platform_driver(act8945a_pmic_driver);
362 
363 MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
364 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
365 MODULE_LICENSE("GPL");
366