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->desc->id, reg, val;
91 
92 	switch (id) {
93 	case ACT8945A_ID_DCDC1:
94 		reg = ACT8945A_DCDC1_SUS;
95 		val = 0xa8;
96 		break;
97 	case ACT8945A_ID_DCDC2:
98 		reg = ACT8945A_DCDC2_SUS;
99 		val = 0xa8;
100 		break;
101 	case ACT8945A_ID_DCDC3:
102 		reg = ACT8945A_DCDC3_SUS;
103 		val = 0xa8;
104 		break;
105 	case ACT8945A_ID_LDO1:
106 		reg = ACT8945A_LDO1_SUS;
107 		val = 0xe8;
108 		break;
109 	case ACT8945A_ID_LDO2:
110 		reg = ACT8945A_LDO2_SUS;
111 		val = 0xe8;
112 		break;
113 	case ACT8945A_ID_LDO3:
114 		reg = ACT8945A_LDO3_SUS;
115 		val = 0xe8;
116 		break;
117 	case ACT8945A_ID_LDO4:
118 		reg = ACT8945A_LDO4_SUS;
119 		val = 0xe8;
120 		break;
121 	default:
122 		return -EINVAL;
123 	}
124 
125 	if (enable)
126 		val |= BIT(4);
127 
128 	/*
129 	 * Ask the PMIC to enable/disable this output when entering hibernate
130 	 * mode.
131 	 */
132 	return regmap_write(regmap, reg, val);
133 }
134 
135 static int act8945a_set_suspend_enable(struct regulator_dev *rdev)
136 {
137 	return act8945a_set_suspend_state(rdev, true);
138 }
139 
140 static int act8945a_set_suspend_disable(struct regulator_dev *rdev)
141 {
142 	return act8945a_set_suspend_state(rdev, false);
143 }
144 
145 static unsigned int act8945a_of_map_mode(unsigned int mode)
146 {
147 	switch (mode) {
148 	case ACT8945A_REGULATOR_MODE_FIXED:
149 	case ACT8945A_REGULATOR_MODE_NORMAL:
150 		return REGULATOR_MODE_NORMAL;
151 	case ACT8945A_REGULATOR_MODE_LOWPOWER:
152 		return REGULATOR_MODE_STANDBY;
153 	default:
154 		return REGULATOR_MODE_INVALID;
155 	}
156 }
157 
158 static int act8945a_set_mode(struct regulator_dev *rdev, unsigned int mode)
159 {
160 	struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
161 	struct regmap *regmap = rdev->regmap;
162 	int id = rdev->desc->id;
163 	int reg, ret, val = 0;
164 
165 	switch (id) {
166 	case ACT8945A_ID_DCDC1:
167 		reg = ACT8945A_DCDC1_CTRL;
168 		break;
169 	case ACT8945A_ID_DCDC2:
170 		reg = ACT8945A_DCDC2_CTRL;
171 		break;
172 	case ACT8945A_ID_DCDC3:
173 		reg = ACT8945A_DCDC3_CTRL;
174 		break;
175 	case ACT8945A_ID_LDO1:
176 		reg = ACT8945A_LDO1_SUS;
177 		break;
178 	case ACT8945A_ID_LDO2:
179 		reg = ACT8945A_LDO2_SUS;
180 		break;
181 	case ACT8945A_ID_LDO3:
182 		reg = ACT8945A_LDO3_SUS;
183 		break;
184 	case ACT8945A_ID_LDO4:
185 		reg = ACT8945A_LDO4_SUS;
186 		break;
187 	default:
188 		return -EINVAL;
189 	}
190 
191 	switch (mode) {
192 	case REGULATOR_MODE_STANDBY:
193 		if (rdev->desc->id > ACT8945A_ID_DCDC3)
194 			val = BIT(5);
195 		break;
196 	case REGULATOR_MODE_NORMAL:
197 		if (rdev->desc->id <= ACT8945A_ID_DCDC3)
198 			val = BIT(5);
199 		break;
200 	default:
201 		return -EINVAL;
202 	}
203 
204 	ret = regmap_update_bits(regmap, reg, BIT(5), val);
205 	if (ret)
206 		return ret;
207 
208 	act8945a->op_mode[id] = mode;
209 
210 	return 0;
211 }
212 
213 static unsigned int act8945a_get_mode(struct regulator_dev *rdev)
214 {
215 	struct act8945a_pmic *act8945a = rdev_get_drvdata(rdev);
216 	int id = rdev->desc->id;
217 
218 	if (id < ACT8945A_ID_DCDC1 || id >= ACT8945A_ID_MAX)
219 		return -EINVAL;
220 
221 	return act8945a->op_mode[id];
222 }
223 
224 static const struct regulator_ops act8945a_ops = {
225 	.list_voltage		= regulator_list_voltage_linear_range,
226 	.map_voltage		= regulator_map_voltage_linear_range,
227 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
228 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
229 	.enable			= regulator_enable_regmap,
230 	.disable		= regulator_disable_regmap,
231 	.set_mode		= act8945a_set_mode,
232 	.get_mode		= act8945a_get_mode,
233 	.is_enabled		= regulator_is_enabled_regmap,
234 	.set_suspend_enable	= act8945a_set_suspend_enable,
235 	.set_suspend_disable	= act8945a_set_suspend_disable,
236 };
237 
238 #define ACT89xx_REG(_name, _family, _id, _vsel_reg, _supply)		\
239 	[_family##_ID_##_id] = {					\
240 		.name			= _name,			\
241 		.supply_name		= _supply,			\
242 		.of_match		= of_match_ptr("REG_"#_id),	\
243 		.of_map_mode		= act8945a_of_map_mode,		\
244 		.regulators_node	= of_match_ptr("regulators"),	\
245 		.id			= _family##_ID_##_id,		\
246 		.type			= REGULATOR_VOLTAGE,		\
247 		.ops			= &act8945a_ops,		\
248 		.n_voltages		= ACT8945A_VOLTAGE_NUM,		\
249 		.linear_ranges		= act8945a_voltage_ranges,	\
250 		.n_linear_ranges	= ARRAY_SIZE(act8945a_voltage_ranges), \
251 		.vsel_reg		= _family##_##_id##_##_vsel_reg, \
252 		.vsel_mask		= ACT8945A_VSEL_MASK,		\
253 		.enable_reg		= _family##_##_id##_CTRL,	\
254 		.enable_mask		= ACT8945A_ENA,			\
255 		.owner			= THIS_MODULE,			\
256 	}
257 
258 static const struct regulator_desc act8945a_regulators[] = {
259 	ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET1, "vp1"),
260 	ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET1, "vp2"),
261 	ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET1, "vp3"),
262 	ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
263 	ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
264 	ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
265 	ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
266 };
267 
268 static const struct regulator_desc act8945a_alt_regulators[] = {
269 	ACT89xx_REG("DCDC_REG1", ACT8945A, DCDC1, VSET2, "vp1"),
270 	ACT89xx_REG("DCDC_REG2", ACT8945A, DCDC2, VSET2, "vp2"),
271 	ACT89xx_REG("DCDC_REG3", ACT8945A, DCDC3, VSET2, "vp3"),
272 	ACT89xx_REG("LDO_REG1", ACT8945A, LDO1, VSET, "inl45"),
273 	ACT89xx_REG("LDO_REG2", ACT8945A, LDO2, VSET, "inl45"),
274 	ACT89xx_REG("LDO_REG3", ACT8945A, LDO3, VSET, "inl67"),
275 	ACT89xx_REG("LDO_REG4", ACT8945A, LDO4, VSET, "inl67"),
276 };
277 
278 static int act8945a_pmic_probe(struct platform_device *pdev)
279 {
280 	struct regulator_config config = { };
281 	const struct regulator_desc *regulators;
282 	struct act8945a_pmic *act8945a;
283 	struct regulator_dev *rdev;
284 	int i, num_regulators;
285 	bool voltage_select;
286 
287 	act8945a = devm_kzalloc(&pdev->dev, sizeof(*act8945a), GFP_KERNEL);
288 	if (!act8945a)
289 		return -ENOMEM;
290 
291 	act8945a->regmap = dev_get_regmap(pdev->dev.parent, NULL);
292 	if (!act8945a->regmap) {
293 		dev_err(&pdev->dev,
294 			"could not retrieve regmap from parent device\n");
295 		return -EINVAL;
296 	}
297 
298 	voltage_select = of_property_read_bool(pdev->dev.parent->of_node,
299 					       "active-semi,vsel-high");
300 
301 	if (voltage_select) {
302 		regulators = act8945a_alt_regulators;
303 		num_regulators = ARRAY_SIZE(act8945a_alt_regulators);
304 	} else {
305 		regulators = act8945a_regulators;
306 		num_regulators = ARRAY_SIZE(act8945a_regulators);
307 	}
308 
309 	config.dev = &pdev->dev;
310 	config.dev->of_node = pdev->dev.parent->of_node;
311 	config.driver_data = act8945a;
312 	for (i = 0; i < num_regulators; i++) {
313 		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
314 					       &config);
315 		if (IS_ERR(rdev)) {
316 			dev_err(&pdev->dev,
317 				"failed to register %s regulator\n",
318 				regulators[i].name);
319 			return PTR_ERR(rdev);
320 		}
321 	}
322 
323 	platform_set_drvdata(pdev, act8945a);
324 
325 	/* Unlock expert registers. */
326 	return regmap_write(act8945a->regmap, ACT8945A_SYS_UNLK_REGS, 0xef);
327 }
328 
329 static int __maybe_unused act8945a_suspend(struct device *pdev)
330 {
331 	struct act8945a_pmic *act8945a = dev_get_drvdata(pdev);
332 
333 	/*
334 	 * Ask the PMIC to enter the suspend mode on the next PWRHLD
335 	 * transition.
336 	 */
337 	return regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x42);
338 }
339 
340 static SIMPLE_DEV_PM_OPS(act8945a_pm, act8945a_suspend, NULL);
341 
342 static void act8945a_pmic_shutdown(struct platform_device *pdev)
343 {
344 	struct act8945a_pmic *act8945a = platform_get_drvdata(pdev);
345 
346 	/*
347 	 * Ask the PMIC to shutdown everything on the next PWRHLD transition.
348 	 */
349 	regmap_write(act8945a->regmap, ACT8945A_SYS_CTRL, 0x0);
350 }
351 
352 static struct platform_driver act8945a_pmic_driver = {
353 	.driver = {
354 		.name = "act8945a-regulator",
355 		.pm = &act8945a_pm,
356 	},
357 	.probe = act8945a_pmic_probe,
358 	.shutdown = act8945a_pmic_shutdown,
359 };
360 module_platform_driver(act8945a_pmic_driver);
361 
362 MODULE_DESCRIPTION("Active-semi ACT8945A voltage regulator driver");
363 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
364 MODULE_LICENSE("GPL");
365