1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
4  *
5  * http://www.active-semi.com/products/power-management-units/act88xx/
6  *
7  * Copyright (C) 2013 Atmel Corporation
8  */
9 
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>
17 #include <linux/of.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>
22 #include <dt-bindings/regulator/active-semi,8865-regulator.h>
23 
24 /*
25  * ACT8600 Global Register Map.
26  */
27 #define ACT8600_SYS_MODE	0x00
28 #define ACT8600_SYS_CTRL	0x01
29 #define ACT8600_DCDC1_VSET	0x10
30 #define ACT8600_DCDC1_CTRL	0x12
31 #define ACT8600_DCDC2_VSET	0x20
32 #define ACT8600_DCDC2_CTRL	0x22
33 #define ACT8600_DCDC3_VSET	0x30
34 #define ACT8600_DCDC3_CTRL	0x32
35 #define ACT8600_SUDCDC4_VSET	0x40
36 #define ACT8600_SUDCDC4_CTRL	0x41
37 #define ACT8600_LDO5_VSET	0x50
38 #define ACT8600_LDO5_CTRL	0x51
39 #define ACT8600_LDO6_VSET	0x60
40 #define ACT8600_LDO6_CTRL	0x61
41 #define ACT8600_LDO7_VSET	0x70
42 #define ACT8600_LDO7_CTRL	0x71
43 #define ACT8600_LDO8_VSET	0x80
44 #define ACT8600_LDO8_CTRL	0x81
45 #define ACT8600_LDO910_CTRL	0x91
46 #define ACT8600_APCH0		0xA1
47 #define ACT8600_APCH1		0xA8
48 #define ACT8600_APCH2		0xA9
49 #define ACT8600_APCH_STAT	0xAA
50 #define ACT8600_OTG0		0xB0
51 #define ACT8600_OTG1		0xB2
52 
53 /*
54  * ACT8846 Global Register Map.
55  */
56 #define	ACT8846_SYS0		0x00
57 #define	ACT8846_SYS1		0x01
58 #define	ACT8846_REG1_VSET	0x10
59 #define	ACT8846_REG1_CTRL	0x12
60 #define	ACT8846_REG2_VSET0	0x20
61 #define	ACT8846_REG2_VSET1	0x21
62 #define	ACT8846_REG2_CTRL	0x22
63 #define	ACT8846_REG3_VSET0	0x30
64 #define	ACT8846_REG3_VSET1	0x31
65 #define	ACT8846_REG3_CTRL	0x32
66 #define	ACT8846_REG4_VSET0	0x40
67 #define	ACT8846_REG4_VSET1	0x41
68 #define	ACT8846_REG4_CTRL	0x42
69 #define	ACT8846_REG5_VSET	0x50
70 #define	ACT8846_REG5_CTRL	0x51
71 #define	ACT8846_REG6_VSET	0x58
72 #define	ACT8846_REG6_CTRL	0x59
73 #define	ACT8846_REG7_VSET	0x60
74 #define	ACT8846_REG7_CTRL	0x61
75 #define	ACT8846_REG8_VSET	0x68
76 #define	ACT8846_REG8_CTRL	0x69
77 #define	ACT8846_REG9_VSET	0x70
78 #define	ACT8846_REG9_CTRL	0x71
79 #define	ACT8846_REG10_VSET	0x80
80 #define	ACT8846_REG10_CTRL	0x81
81 #define	ACT8846_REG11_VSET	0x90
82 #define	ACT8846_REG11_CTRL	0x91
83 #define	ACT8846_REG12_VSET	0xa0
84 #define	ACT8846_REG12_CTRL	0xa1
85 #define	ACT8846_REG13_CTRL	0xb1
86 #define	ACT8846_GLB_OFF_CTRL	0xc3
87 #define	ACT8846_OFF_SYSMASK	0x18
88 
89 /*
90  * ACT8865 Global Register Map.
91  */
92 #define	ACT8865_SYS_MODE	0x00
93 #define	ACT8865_SYS_CTRL	0x01
94 #define	ACT8865_SYS_UNLK_REGS	0x0b
95 #define	ACT8865_DCDC1_VSET1	0x20
96 #define	ACT8865_DCDC1_VSET2	0x21
97 #define	ACT8865_DCDC1_CTRL	0x22
98 #define	ACT8865_DCDC1_SUS	0x24
99 #define	ACT8865_DCDC2_VSET1	0x30
100 #define	ACT8865_DCDC2_VSET2	0x31
101 #define	ACT8865_DCDC2_CTRL	0x32
102 #define	ACT8865_DCDC2_SUS	0x34
103 #define	ACT8865_DCDC3_VSET1	0x40
104 #define	ACT8865_DCDC3_VSET2	0x41
105 #define	ACT8865_DCDC3_CTRL	0x42
106 #define	ACT8865_DCDC3_SUS	0x44
107 #define	ACT8865_LDO1_VSET	0x50
108 #define	ACT8865_LDO1_CTRL	0x51
109 #define	ACT8865_LDO1_SUS	0x52
110 #define	ACT8865_LDO2_VSET	0x54
111 #define	ACT8865_LDO2_CTRL	0x55
112 #define	ACT8865_LDO2_SUS	0x56
113 #define	ACT8865_LDO3_VSET	0x60
114 #define	ACT8865_LDO3_CTRL	0x61
115 #define	ACT8865_LDO3_SUS	0x62
116 #define	ACT8865_LDO4_VSET	0x64
117 #define	ACT8865_LDO4_CTRL	0x65
118 #define	ACT8865_LDO4_SUS	0x66
119 #define	ACT8865_MSTROFF		0x20
120 
121 /*
122  * Field Definitions.
123  */
124 #define	ACT8865_ENA		0x80	/* ON - [7] */
125 #define	ACT8865_DIS		0x40	/* DIS - [6] */
126 
127 #define	ACT8865_VSEL_MASK	0x3F	/* VSET - [5:0] */
128 
129 
130 #define ACT8600_LDO10_ENA		0x40	/* ON - [6] */
131 #define ACT8600_SUDCDC_VSEL_MASK	0xFF	/* SUDCDC VSET - [7:0] */
132 
133 #define ACT8600_APCH_CHG_ACIN		BIT(7)
134 #define ACT8600_APCH_CHG_USB		BIT(6)
135 #define ACT8600_APCH_CSTATE0		BIT(5)
136 #define ACT8600_APCH_CSTATE1		BIT(4)
137 
138 /*
139  * ACT8865 voltage number
140  */
141 #define	ACT8865_VOLTAGE_NUM	64
142 #define ACT8600_SUDCDC_VOLTAGE_NUM	256
143 
144 struct act8865 {
145 	struct regmap *regmap;
146 	int off_reg;
147 	int off_mask;
148 };
149 
150 static const struct regmap_range act8600_reg_ranges[] = {
151 	regmap_reg_range(0x00, 0x01),
152 	regmap_reg_range(0x10, 0x10),
153 	regmap_reg_range(0x12, 0x12),
154 	regmap_reg_range(0x20, 0x20),
155 	regmap_reg_range(0x22, 0x22),
156 	regmap_reg_range(0x30, 0x30),
157 	regmap_reg_range(0x32, 0x32),
158 	regmap_reg_range(0x40, 0x41),
159 	regmap_reg_range(0x50, 0x51),
160 	regmap_reg_range(0x60, 0x61),
161 	regmap_reg_range(0x70, 0x71),
162 	regmap_reg_range(0x80, 0x81),
163 	regmap_reg_range(0x91, 0x91),
164 	regmap_reg_range(0xA1, 0xA1),
165 	regmap_reg_range(0xA8, 0xAA),
166 	regmap_reg_range(0xB0, 0xB0),
167 	regmap_reg_range(0xB2, 0xB2),
168 	regmap_reg_range(0xC1, 0xC1),
169 };
170 
171 static const struct regmap_range act8600_reg_ro_ranges[] = {
172 	regmap_reg_range(0xAA, 0xAA),
173 	regmap_reg_range(0xC1, 0xC1),
174 };
175 
176 static const struct regmap_range act8600_reg_volatile_ranges[] = {
177 	regmap_reg_range(0x00, 0x01),
178 	regmap_reg_range(0x12, 0x12),
179 	regmap_reg_range(0x22, 0x22),
180 	regmap_reg_range(0x32, 0x32),
181 	regmap_reg_range(0x41, 0x41),
182 	regmap_reg_range(0x51, 0x51),
183 	regmap_reg_range(0x61, 0x61),
184 	regmap_reg_range(0x71, 0x71),
185 	regmap_reg_range(0x81, 0x81),
186 	regmap_reg_range(0xA8, 0xA8),
187 	regmap_reg_range(0xAA, 0xAA),
188 	regmap_reg_range(0xB0, 0xB0),
189 	regmap_reg_range(0xC1, 0xC1),
190 };
191 
192 static const struct regmap_access_table act8600_write_ranges_table = {
193 	.yes_ranges	= act8600_reg_ranges,
194 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_ranges),
195 	.no_ranges	= act8600_reg_ro_ranges,
196 	.n_no_ranges	= ARRAY_SIZE(act8600_reg_ro_ranges),
197 };
198 
199 static const struct regmap_access_table act8600_read_ranges_table = {
200 	.yes_ranges	= act8600_reg_ranges,
201 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_ranges),
202 };
203 
204 static const struct regmap_access_table act8600_volatile_ranges_table = {
205 	.yes_ranges	= act8600_reg_volatile_ranges,
206 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_volatile_ranges),
207 };
208 
209 static const struct regmap_config act8600_regmap_config = {
210 	.reg_bits = 8,
211 	.val_bits = 8,
212 	.max_register = 0xFF,
213 	.wr_table = &act8600_write_ranges_table,
214 	.rd_table = &act8600_read_ranges_table,
215 	.volatile_table = &act8600_volatile_ranges_table,
216 };
217 
218 static const struct regmap_config act8865_regmap_config = {
219 	.reg_bits = 8,
220 	.val_bits = 8,
221 };
222 
223 static const struct linear_range act8865_voltage_ranges[] = {
224 	REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
225 	REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
226 	REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
227 };
228 
229 static const struct linear_range act8600_sudcdc_voltage_ranges[] = {
230 	REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
231 	REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
232 	REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
233 	REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
234 	REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
235 };
236 
237 static int act8865_set_suspend_state(struct regulator_dev *rdev, bool enable)
238 {
239 	struct regmap *regmap = rdev->regmap;
240 	int id = rdev->desc->id, reg, val;
241 
242 	switch (id) {
243 	case ACT8865_ID_DCDC1:
244 		reg = ACT8865_DCDC1_SUS;
245 		val = 0xa8;
246 		break;
247 	case ACT8865_ID_DCDC2:
248 		reg = ACT8865_DCDC2_SUS;
249 		val = 0xa8;
250 		break;
251 	case ACT8865_ID_DCDC3:
252 		reg = ACT8865_DCDC3_SUS;
253 		val = 0xa8;
254 		break;
255 	case ACT8865_ID_LDO1:
256 		reg = ACT8865_LDO1_SUS;
257 		val = 0xe8;
258 		break;
259 	case ACT8865_ID_LDO2:
260 		reg = ACT8865_LDO2_SUS;
261 		val = 0xe8;
262 		break;
263 	case ACT8865_ID_LDO3:
264 		reg = ACT8865_LDO3_SUS;
265 		val = 0xe8;
266 		break;
267 	case ACT8865_ID_LDO4:
268 		reg = ACT8865_LDO4_SUS;
269 		val = 0xe8;
270 		break;
271 	default:
272 		return -EINVAL;
273 	}
274 
275 	if (enable)
276 		val |= BIT(4);
277 
278 	/*
279 	 * Ask the PMIC to enable/disable this output when entering hibernate
280 	 * mode.
281 	 */
282 	return regmap_write(regmap, reg, val);
283 }
284 
285 static int act8865_set_suspend_enable(struct regulator_dev *rdev)
286 {
287 	return act8865_set_suspend_state(rdev, true);
288 }
289 
290 static int act8865_set_suspend_disable(struct regulator_dev *rdev)
291 {
292 	return act8865_set_suspend_state(rdev, false);
293 }
294 
295 static unsigned int act8865_of_map_mode(unsigned int mode)
296 {
297 	switch (mode) {
298 	case ACT8865_REGULATOR_MODE_FIXED:
299 		return REGULATOR_MODE_FAST;
300 	case ACT8865_REGULATOR_MODE_NORMAL:
301 		return REGULATOR_MODE_NORMAL;
302 	case ACT8865_REGULATOR_MODE_LOWPOWER:
303 		return REGULATOR_MODE_STANDBY;
304 	default:
305 		return REGULATOR_MODE_INVALID;
306 	}
307 }
308 
309 static int act8865_set_mode(struct regulator_dev *rdev, unsigned int mode)
310 {
311 	struct regmap *regmap = rdev->regmap;
312 	int id = rdev_get_id(rdev);
313 	int reg, val = 0;
314 
315 	switch (id) {
316 	case ACT8865_ID_DCDC1:
317 		reg = ACT8865_DCDC1_CTRL;
318 		break;
319 	case ACT8865_ID_DCDC2:
320 		reg = ACT8865_DCDC2_CTRL;
321 		break;
322 	case ACT8865_ID_DCDC3:
323 		reg = ACT8865_DCDC3_CTRL;
324 		break;
325 	case ACT8865_ID_LDO1:
326 		reg = ACT8865_LDO1_CTRL;
327 		break;
328 	case ACT8865_ID_LDO2:
329 		reg = ACT8865_LDO2_CTRL;
330 		break;
331 	case ACT8865_ID_LDO3:
332 		reg = ACT8865_LDO3_CTRL;
333 		break;
334 	case ACT8865_ID_LDO4:
335 		reg = ACT8865_LDO4_CTRL;
336 		break;
337 	default:
338 		return -EINVAL;
339 	}
340 
341 	switch (mode) {
342 	case REGULATOR_MODE_FAST:
343 	case REGULATOR_MODE_NORMAL:
344 		if (id <= ACT8865_ID_DCDC3)
345 			val = BIT(5);
346 		break;
347 	case REGULATOR_MODE_STANDBY:
348 		if (id > ACT8865_ID_DCDC3)
349 			val = BIT(5);
350 		break;
351 	default:
352 		return -EINVAL;
353 	}
354 
355 	return regmap_update_bits(regmap, reg, BIT(5), val);
356 }
357 
358 static unsigned int act8865_get_mode(struct regulator_dev *rdev)
359 {
360 	struct regmap *regmap = rdev->regmap;
361 	int id = rdev_get_id(rdev);
362 	int reg, ret, val = 0;
363 
364 	switch (id) {
365 	case ACT8865_ID_DCDC1:
366 		reg = ACT8865_DCDC1_CTRL;
367 		break;
368 	case ACT8865_ID_DCDC2:
369 		reg = ACT8865_DCDC2_CTRL;
370 		break;
371 	case ACT8865_ID_DCDC3:
372 		reg = ACT8865_DCDC3_CTRL;
373 		break;
374 	case ACT8865_ID_LDO1:
375 		reg = ACT8865_LDO1_CTRL;
376 		break;
377 	case ACT8865_ID_LDO2:
378 		reg = ACT8865_LDO2_CTRL;
379 		break;
380 	case ACT8865_ID_LDO3:
381 		reg = ACT8865_LDO3_CTRL;
382 		break;
383 	case ACT8865_ID_LDO4:
384 		reg = ACT8865_LDO4_CTRL;
385 		break;
386 	default:
387 		return -EINVAL;
388 	}
389 
390 	ret = regmap_read(regmap, reg, &val);
391 	if (ret)
392 		return ret;
393 
394 	if (id <= ACT8865_ID_DCDC3 && (val & BIT(5)))
395 		return REGULATOR_MODE_FAST;
396 	else if	(id > ACT8865_ID_DCDC3 && !(val & BIT(5)))
397 		return REGULATOR_MODE_NORMAL;
398 	else
399 		return REGULATOR_MODE_STANDBY;
400 }
401 
402 static const struct regulator_ops act8865_ops = {
403 	.list_voltage		= regulator_list_voltage_linear_range,
404 	.map_voltage		= regulator_map_voltage_linear_range,
405 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
406 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
407 	.enable			= regulator_enable_regmap,
408 	.disable		= regulator_disable_regmap,
409 	.set_mode		= act8865_set_mode,
410 	.get_mode		= act8865_get_mode,
411 	.is_enabled		= regulator_is_enabled_regmap,
412 	.set_suspend_enable	= act8865_set_suspend_enable,
413 	.set_suspend_disable	= act8865_set_suspend_disable,
414 };
415 
416 static const struct regulator_ops act8865_ldo_ops = {
417 	.list_voltage		= regulator_list_voltage_linear_range,
418 	.map_voltage		= regulator_map_voltage_linear_range,
419 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
420 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
421 	.enable			= regulator_enable_regmap,
422 	.disable		= regulator_disable_regmap,
423 	.set_mode		= act8865_set_mode,
424 	.get_mode		= act8865_get_mode,
425 	.is_enabled		= regulator_is_enabled_regmap,
426 	.set_suspend_enable	= act8865_set_suspend_enable,
427 	.set_suspend_disable	= act8865_set_suspend_disable,
428 	.set_pull_down		= regulator_set_pull_down_regmap,
429 };
430 
431 static const struct regulator_ops act8865_fixed_ldo_ops = {
432 	.enable			= regulator_enable_regmap,
433 	.disable		= regulator_disable_regmap,
434 	.is_enabled		= regulator_is_enabled_regmap,
435 };
436 
437 #define ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, _ops)	\
438 	[_family##_ID_##_id] = {					\
439 		.name			= _name,			\
440 		.of_match		= of_match_ptr(_name),		\
441 		.of_map_mode		= act8865_of_map_mode,		\
442 		.regulators_node	= of_match_ptr("regulators"),	\
443 		.supply_name		= _supply,			\
444 		.id			= _family##_ID_##_id,		\
445 		.type			= REGULATOR_VOLTAGE,		\
446 		.ops			= _ops,				\
447 		.n_voltages		= ACT8865_VOLTAGE_NUM,		\
448 		.linear_ranges		= act8865_voltage_ranges,	\
449 		.n_linear_ranges	= ARRAY_SIZE(act8865_voltage_ranges), \
450 		.vsel_reg		= _family##_##_id##_##_vsel_reg, \
451 		.vsel_mask		= ACT8865_VSEL_MASK,		\
452 		.enable_reg		= _family##_##_id##_CTRL,	\
453 		.enable_mask		= ACT8865_ENA,			\
454 		.pull_down_reg		= _family##_##_id##_CTRL,	\
455 		.pull_down_mask		= ACT8865_DIS,			\
456 		.owner			= THIS_MODULE,			\
457 	}
458 
459 #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
460 	ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ops)
461 
462 #define ACT88xx_LDO(_name, _family, _id, _vsel_reg, _supply) \
463 	ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ldo_ops)
464 
465 static const struct regulator_desc act8600_regulators[] = {
466 	ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
467 	ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
468 	ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
469 	{
470 		.name = "SUDCDC_REG4",
471 		.of_match = of_match_ptr("SUDCDC_REG4"),
472 		.regulators_node = of_match_ptr("regulators"),
473 		.id = ACT8600_ID_SUDCDC4,
474 		.ops = &act8865_ops,
475 		.type = REGULATOR_VOLTAGE,
476 		.n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
477 		.linear_ranges = act8600_sudcdc_voltage_ranges,
478 		.n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
479 		.vsel_reg = ACT8600_SUDCDC4_VSET,
480 		.vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
481 		.enable_reg = ACT8600_SUDCDC4_CTRL,
482 		.enable_mask = ACT8865_ENA,
483 		.owner = THIS_MODULE,
484 	},
485 	ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
486 	ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
487 	ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
488 	ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
489 	{
490 		.name = "LDO_REG9",
491 		.of_match = of_match_ptr("LDO_REG9"),
492 		.regulators_node = of_match_ptr("regulators"),
493 		.id = ACT8600_ID_LDO9,
494 		.ops = &act8865_fixed_ldo_ops,
495 		.type = REGULATOR_VOLTAGE,
496 		.n_voltages = 1,
497 		.fixed_uV = 3300000,
498 		.enable_reg = ACT8600_LDO910_CTRL,
499 		.enable_mask = ACT8865_ENA,
500 		.owner = THIS_MODULE,
501 	},
502 	{
503 		.name = "LDO_REG10",
504 		.of_match = of_match_ptr("LDO_REG10"),
505 		.regulators_node = of_match_ptr("regulators"),
506 		.id = ACT8600_ID_LDO10,
507 		.ops = &act8865_fixed_ldo_ops,
508 		.type = REGULATOR_VOLTAGE,
509 		.n_voltages = 1,
510 		.fixed_uV = 1200000,
511 		.enable_reg = ACT8600_LDO910_CTRL,
512 		.enable_mask = ACT8600_LDO10_ENA,
513 		.owner = THIS_MODULE,
514 	},
515 };
516 
517 static const struct regulator_desc act8846_regulators[] = {
518 	ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
519 	ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
520 	ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
521 	ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
522 	ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
523 	ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
524 	ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
525 	ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
526 	ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
527 	ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
528 	ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
529 	ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
530 };
531 
532 static const struct regulator_desc act8865_regulators[] = {
533 	ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
534 	ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
535 	ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
536 	ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
537 	ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
538 	ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
539 	ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
540 };
541 
542 static const struct regulator_desc act8865_alt_regulators[] = {
543 	ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
544 	ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
545 	ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
546 	ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
547 	ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
548 	ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
549 	ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
550 };
551 
552 #ifdef CONFIG_OF
553 static const struct of_device_id act8865_dt_ids[] = {
554 	{ .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
555 	{ .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
556 	{ .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
557 	{ }
558 };
559 MODULE_DEVICE_TABLE(of, act8865_dt_ids);
560 #endif
561 
562 static struct act8865_regulator_data *act8865_get_regulator_data(
563 		int id, struct act8865_platform_data *pdata)
564 {
565 	int i;
566 
567 	for (i = 0; i < pdata->num_regulators; i++) {
568 		if (pdata->regulators[i].id == id)
569 			return &pdata->regulators[i];
570 	}
571 
572 	return NULL;
573 }
574 
575 static struct i2c_client *act8865_i2c_client;
576 static void act8865_power_off(void)
577 {
578 	struct act8865 *act8865;
579 
580 	act8865 = i2c_get_clientdata(act8865_i2c_client);
581 	regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
582 	while (1);
583 }
584 
585 static int act8600_charger_get_status(struct regmap *map)
586 {
587 	unsigned int val;
588 	int ret;
589 	u8 state0, state1;
590 
591 	ret = regmap_read(map, ACT8600_APCH_STAT, &val);
592 	if (ret < 0)
593 		return ret;
594 
595 	state0 = val & ACT8600_APCH_CSTATE0;
596 	state1 = val & ACT8600_APCH_CSTATE1;
597 
598 	if (state0 && !state1)
599 		return POWER_SUPPLY_STATUS_CHARGING;
600 	if (!state0 && state1)
601 		return POWER_SUPPLY_STATUS_NOT_CHARGING;
602 	if (!state0 && !state1)
603 		return POWER_SUPPLY_STATUS_DISCHARGING;
604 
605 	return POWER_SUPPLY_STATUS_UNKNOWN;
606 }
607 
608 static int act8600_charger_get_property(struct power_supply *psy,
609 		enum power_supply_property psp, union power_supply_propval *val)
610 {
611 	struct regmap *map = power_supply_get_drvdata(psy);
612 	int ret;
613 
614 	switch (psp) {
615 	case POWER_SUPPLY_PROP_STATUS:
616 		ret = act8600_charger_get_status(map);
617 		if (ret < 0)
618 			return ret;
619 
620 		val->intval = ret;
621 		break;
622 	default:
623 		return -EINVAL;
624 	}
625 
626 	return 0;
627 }
628 
629 static enum power_supply_property act8600_charger_properties[] = {
630 	POWER_SUPPLY_PROP_STATUS,
631 };
632 
633 static const struct power_supply_desc act8600_charger_desc = {
634 	.name = "act8600-charger",
635 	.type = POWER_SUPPLY_TYPE_BATTERY,
636 	.properties = act8600_charger_properties,
637 	.num_properties = ARRAY_SIZE(act8600_charger_properties),
638 	.get_property = act8600_charger_get_property,
639 };
640 
641 static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
642 {
643 	struct power_supply *charger;
644 	struct power_supply_config cfg = {
645 		.drv_data = regmap,
646 		.of_node = dev->of_node,
647 	};
648 
649 	charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
650 
651 	return PTR_ERR_OR_ZERO(charger);
652 }
653 
654 static int act8865_pmic_probe(struct i2c_client *client,
655 			      const struct i2c_device_id *i2c_id)
656 {
657 	const struct regulator_desc *regulators;
658 	struct act8865_platform_data *pdata = NULL;
659 	struct device *dev = &client->dev;
660 	int i, ret, num_regulators;
661 	struct act8865 *act8865;
662 	const struct regmap_config *regmap_config;
663 	unsigned long type;
664 	int off_reg, off_mask;
665 	int voltage_select = 0;
666 
667 	if (dev->of_node) {
668 		const struct of_device_id *id;
669 
670 		id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
671 		if (!id)
672 			return -ENODEV;
673 
674 		type = (unsigned long) id->data;
675 
676 		voltage_select = !!of_get_property(dev->of_node,
677 						   "active-semi,vsel-high",
678 						   NULL);
679 	} else {
680 		type = i2c_id->driver_data;
681 		pdata = dev_get_platdata(dev);
682 	}
683 
684 	switch (type) {
685 	case ACT8600:
686 		regulators = act8600_regulators;
687 		num_regulators = ARRAY_SIZE(act8600_regulators);
688 		regmap_config = &act8600_regmap_config;
689 		off_reg = -1;
690 		off_mask = -1;
691 		break;
692 	case ACT8846:
693 		regulators = act8846_regulators;
694 		num_regulators = ARRAY_SIZE(act8846_regulators);
695 		regmap_config = &act8865_regmap_config;
696 		off_reg = ACT8846_GLB_OFF_CTRL;
697 		off_mask = ACT8846_OFF_SYSMASK;
698 		break;
699 	case ACT8865:
700 		if (voltage_select) {
701 			regulators = act8865_alt_regulators;
702 			num_regulators = ARRAY_SIZE(act8865_alt_regulators);
703 		} else {
704 			regulators = act8865_regulators;
705 			num_regulators = ARRAY_SIZE(act8865_regulators);
706 		}
707 		regmap_config = &act8865_regmap_config;
708 		off_reg = ACT8865_SYS_CTRL;
709 		off_mask = ACT8865_MSTROFF;
710 		break;
711 	default:
712 		dev_err(dev, "invalid device id %lu\n", type);
713 		return -EINVAL;
714 	}
715 
716 	act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
717 	if (!act8865)
718 		return -ENOMEM;
719 
720 	act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
721 	if (IS_ERR(act8865->regmap)) {
722 		ret = PTR_ERR(act8865->regmap);
723 		dev_err(dev, "Failed to allocate register map: %d\n", ret);
724 		return ret;
725 	}
726 
727 	if (of_device_is_system_power_controller(dev->of_node)) {
728 		if (!pm_power_off && (off_reg > 0)) {
729 			act8865_i2c_client = client;
730 			act8865->off_reg = off_reg;
731 			act8865->off_mask = off_mask;
732 			pm_power_off = act8865_power_off;
733 		} else {
734 			dev_err(dev, "Failed to set poweroff capability, already defined\n");
735 		}
736 	}
737 
738 	/* Finally register devices */
739 	for (i = 0; i < num_regulators; i++) {
740 		const struct regulator_desc *desc = &regulators[i];
741 		struct regulator_config config = { };
742 		struct regulator_dev *rdev;
743 
744 		config.dev = dev;
745 		config.driver_data = act8865;
746 		config.regmap = act8865->regmap;
747 
748 		if (pdata) {
749 			struct act8865_regulator_data *rdata;
750 
751 			rdata = act8865_get_regulator_data(desc->id, pdata);
752 			if (rdata) {
753 				config.init_data = rdata->init_data;
754 				config.of_node = rdata->of_node;
755 			}
756 		}
757 
758 		rdev = devm_regulator_register(dev, desc, &config);
759 		if (IS_ERR(rdev)) {
760 			dev_err(dev, "failed to register %s\n", desc->name);
761 			return PTR_ERR(rdev);
762 		}
763 	}
764 
765 	if (type == ACT8600) {
766 		ret = act8600_charger_probe(dev, act8865->regmap);
767 		if (ret < 0) {
768 			if (ret != -EPROBE_DEFER)
769 				dev_err(dev, "Failed to probe charger");
770 			return ret;
771 		}
772 	}
773 
774 	i2c_set_clientdata(client, act8865);
775 
776 	/* Unlock expert registers for ACT8865. */
777 	return type != ACT8865 ? 0 : regmap_write(act8865->regmap,
778 						  ACT8865_SYS_UNLK_REGS, 0xef);
779 }
780 
781 static const struct i2c_device_id act8865_ids[] = {
782 	{ .name = "act8600", .driver_data = ACT8600 },
783 	{ .name = "act8846", .driver_data = ACT8846 },
784 	{ .name = "act8865", .driver_data = ACT8865 },
785 	{ },
786 };
787 MODULE_DEVICE_TABLE(i2c, act8865_ids);
788 
789 static struct i2c_driver act8865_pmic_driver = {
790 	.driver	= {
791 		.name	= "act8865",
792 	},
793 	.probe		= act8865_pmic_probe,
794 	.id_table	= act8865_ids,
795 };
796 
797 module_i2c_driver(act8865_pmic_driver);
798 
799 MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
800 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
801 MODULE_LICENSE("GPL v2");
802