1 /*
2  * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
3  *
4  * http://www.active-semi.com/products/power-management-units/act88xx/
5  *
6  * Copyright (C) 2013 Atmel Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/act8865.h>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
28 #include <linux/regulator/of_regulator.h>
29 #include <linux/regmap.h>
30 
31 /*
32  * ACT8600 Global Register Map.
33  */
34 #define ACT8600_SYS_MODE	0x00
35 #define ACT8600_SYS_CTRL	0x01
36 #define ACT8600_DCDC1_VSET	0x10
37 #define ACT8600_DCDC1_CTRL	0x12
38 #define ACT8600_DCDC2_VSET	0x20
39 #define ACT8600_DCDC2_CTRL	0x22
40 #define ACT8600_DCDC3_VSET	0x30
41 #define ACT8600_DCDC3_CTRL	0x32
42 #define ACT8600_SUDCDC4_VSET	0x40
43 #define ACT8600_SUDCDC4_CTRL	0x41
44 #define ACT8600_LDO5_VSET	0x50
45 #define ACT8600_LDO5_CTRL	0x51
46 #define ACT8600_LDO6_VSET	0x60
47 #define ACT8600_LDO6_CTRL	0x61
48 #define ACT8600_LDO7_VSET	0x70
49 #define ACT8600_LDO7_CTRL	0x71
50 #define ACT8600_LDO8_VSET	0x80
51 #define ACT8600_LDO8_CTRL	0x81
52 #define ACT8600_LDO910_CTRL	0x91
53 #define ACT8600_APCH0		0xA1
54 #define ACT8600_APCH1		0xA8
55 #define ACT8600_APCH2		0xA9
56 #define ACT8600_APCH_STAT	0xAA
57 #define ACT8600_OTG0		0xB0
58 #define ACT8600_OTG1		0xB2
59 
60 /*
61  * ACT8846 Global Register Map.
62  */
63 #define	ACT8846_SYS0		0x00
64 #define	ACT8846_SYS1		0x01
65 #define	ACT8846_REG1_VSET	0x10
66 #define	ACT8846_REG1_CTRL	0x12
67 #define	ACT8846_REG2_VSET0	0x20
68 #define	ACT8846_REG2_VSET1	0x21
69 #define	ACT8846_REG2_CTRL	0x22
70 #define	ACT8846_REG3_VSET0	0x30
71 #define	ACT8846_REG3_VSET1	0x31
72 #define	ACT8846_REG3_CTRL	0x32
73 #define	ACT8846_REG4_VSET0	0x40
74 #define	ACT8846_REG4_VSET1	0x41
75 #define	ACT8846_REG4_CTRL	0x42
76 #define	ACT8846_REG5_VSET	0x50
77 #define	ACT8846_REG5_CTRL	0x51
78 #define	ACT8846_REG6_VSET	0x58
79 #define	ACT8846_REG6_CTRL	0x59
80 #define	ACT8846_REG7_VSET	0x60
81 #define	ACT8846_REG7_CTRL	0x61
82 #define	ACT8846_REG8_VSET	0x68
83 #define	ACT8846_REG8_CTRL	0x69
84 #define	ACT8846_REG9_VSET	0x70
85 #define	ACT8846_REG9_CTRL	0x71
86 #define	ACT8846_REG10_VSET	0x80
87 #define	ACT8846_REG10_CTRL	0x81
88 #define	ACT8846_REG11_VSET	0x90
89 #define	ACT8846_REG11_CTRL	0x91
90 #define	ACT8846_REG12_VSET	0xa0
91 #define	ACT8846_REG12_CTRL	0xa1
92 #define	ACT8846_REG13_CTRL	0xb1
93 #define	ACT8846_GLB_OFF_CTRL	0xc3
94 #define	ACT8846_OFF_SYSMASK	0x18
95 
96 /*
97  * ACT8865 Global Register Map.
98  */
99 #define	ACT8865_SYS_MODE	0x00
100 #define	ACT8865_SYS_CTRL	0x01
101 #define	ACT8865_DCDC1_VSET1	0x20
102 #define	ACT8865_DCDC1_VSET2	0x21
103 #define	ACT8865_DCDC1_CTRL	0x22
104 #define	ACT8865_DCDC2_VSET1	0x30
105 #define	ACT8865_DCDC2_VSET2	0x31
106 #define	ACT8865_DCDC2_CTRL	0x32
107 #define	ACT8865_DCDC3_VSET1	0x40
108 #define	ACT8865_DCDC3_VSET2	0x41
109 #define	ACT8865_DCDC3_CTRL	0x42
110 #define	ACT8865_LDO1_VSET	0x50
111 #define	ACT8865_LDO1_CTRL	0x51
112 #define	ACT8865_LDO2_VSET	0x54
113 #define	ACT8865_LDO2_CTRL	0x55
114 #define	ACT8865_LDO3_VSET	0x60
115 #define	ACT8865_LDO3_CTRL	0x61
116 #define	ACT8865_LDO4_VSET	0x64
117 #define	ACT8865_LDO4_CTRL	0x65
118 #define	ACT8865_MSTROFF		0x20
119 
120 /*
121  * Field Definitions.
122  */
123 #define	ACT8865_ENA		0x80	/* ON - [7] */
124 #define	ACT8865_VSEL_MASK	0x3F	/* VSET - [5:0] */
125 
126 
127 #define ACT8600_LDO10_ENA		0x40	/* ON - [6] */
128 #define ACT8600_SUDCDC_VSEL_MASK	0xFF	/* SUDCDC VSET - [7:0] */
129 
130 /*
131  * ACT8865 voltage number
132  */
133 #define	ACT8865_VOLTAGE_NUM	64
134 #define ACT8600_SUDCDC_VOLTAGE_NUM	256
135 
136 struct act8865 {
137 	struct regmap *regmap;
138 	int off_reg;
139 	int off_mask;
140 };
141 
142 static const struct regmap_range act8600_reg_ranges[] = {
143 	regmap_reg_range(0x00, 0x01),
144 	regmap_reg_range(0x10, 0x10),
145 	regmap_reg_range(0x12, 0x12),
146 	regmap_reg_range(0x20, 0x20),
147 	regmap_reg_range(0x22, 0x22),
148 	regmap_reg_range(0x30, 0x30),
149 	regmap_reg_range(0x32, 0x32),
150 	regmap_reg_range(0x40, 0x41),
151 	regmap_reg_range(0x50, 0x51),
152 	regmap_reg_range(0x60, 0x61),
153 	regmap_reg_range(0x70, 0x71),
154 	regmap_reg_range(0x80, 0x81),
155 	regmap_reg_range(0x91, 0x91),
156 	regmap_reg_range(0xA1, 0xA1),
157 	regmap_reg_range(0xA8, 0xAA),
158 	regmap_reg_range(0xB0, 0xB0),
159 	regmap_reg_range(0xB2, 0xB2),
160 	regmap_reg_range(0xC1, 0xC1),
161 };
162 
163 static const struct regmap_range act8600_reg_ro_ranges[] = {
164 	regmap_reg_range(0xAA, 0xAA),
165 	regmap_reg_range(0xC1, 0xC1),
166 };
167 
168 static const struct regmap_range act8600_reg_volatile_ranges[] = {
169 	regmap_reg_range(0x00, 0x01),
170 	regmap_reg_range(0x12, 0x12),
171 	regmap_reg_range(0x22, 0x22),
172 	regmap_reg_range(0x32, 0x32),
173 	regmap_reg_range(0x41, 0x41),
174 	regmap_reg_range(0x51, 0x51),
175 	regmap_reg_range(0x61, 0x61),
176 	regmap_reg_range(0x71, 0x71),
177 	regmap_reg_range(0x81, 0x81),
178 	regmap_reg_range(0xA8, 0xA8),
179 	regmap_reg_range(0xAA, 0xAA),
180 	regmap_reg_range(0xB0, 0xB0),
181 	regmap_reg_range(0xC1, 0xC1),
182 };
183 
184 static const struct regmap_access_table act8600_write_ranges_table = {
185 	.yes_ranges	= act8600_reg_ranges,
186 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_ranges),
187 	.no_ranges	= act8600_reg_ro_ranges,
188 	.n_no_ranges	= ARRAY_SIZE(act8600_reg_ro_ranges),
189 };
190 
191 static const struct regmap_access_table act8600_read_ranges_table = {
192 	.yes_ranges	= act8600_reg_ranges,
193 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_ranges),
194 };
195 
196 static const struct regmap_access_table act8600_volatile_ranges_table = {
197 	.yes_ranges	= act8600_reg_volatile_ranges,
198 	.n_yes_ranges	= ARRAY_SIZE(act8600_reg_volatile_ranges),
199 };
200 
201 static const struct regmap_config act8600_regmap_config = {
202 	.reg_bits = 8,
203 	.val_bits = 8,
204 	.max_register = 0xFF,
205 	.wr_table = &act8600_write_ranges_table,
206 	.rd_table = &act8600_read_ranges_table,
207 	.volatile_table = &act8600_volatile_ranges_table,
208 };
209 
210 static const struct regmap_config act8865_regmap_config = {
211 	.reg_bits = 8,
212 	.val_bits = 8,
213 };
214 
215 static const struct regulator_linear_range act8865_voltage_ranges[] = {
216 	REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
217 	REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
218 	REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
219 };
220 
221 static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
222 	REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
223 	REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
224 	REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
225 	REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
226 	REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
227 };
228 
229 static struct regulator_ops act8865_ops = {
230 	.list_voltage		= regulator_list_voltage_linear_range,
231 	.map_voltage		= regulator_map_voltage_linear_range,
232 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
233 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
234 	.enable			= regulator_enable_regmap,
235 	.disable		= regulator_disable_regmap,
236 	.is_enabled		= regulator_is_enabled_regmap,
237 };
238 
239 static struct regulator_ops act8865_ldo_ops = {
240 	.enable			= regulator_enable_regmap,
241 	.disable		= regulator_disable_regmap,
242 	.is_enabled		= regulator_is_enabled_regmap,
243 };
244 
245 #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply)		\
246 	[_family##_ID_##_id] = {					\
247 		.name			= _name,			\
248 		.of_match		= of_match_ptr(_name),		\
249 		.regulators_node	= of_match_ptr("regulators"),	\
250 		.supply_name		= _supply,			\
251 		.id			= _family##_ID_##_id,		\
252 		.type			= REGULATOR_VOLTAGE,		\
253 		.ops			= &act8865_ops,			\
254 		.n_voltages		= ACT8865_VOLTAGE_NUM,		\
255 		.linear_ranges		= act8865_voltage_ranges,	\
256 		.n_linear_ranges	= ARRAY_SIZE(act8865_voltage_ranges), \
257 		.vsel_reg		= _family##_##_id##_##_vsel_reg, \
258 		.vsel_mask		= ACT8865_VSEL_MASK,		\
259 		.enable_reg		= _family##_##_id##_CTRL,	\
260 		.enable_mask		= ACT8865_ENA,			\
261 		.owner			= THIS_MODULE,			\
262 	}
263 
264 static const struct regulator_desc act8600_regulators[] = {
265 	ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
266 	ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
267 	ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
268 	{
269 		.name = "SUDCDC_REG4",
270 		.of_match = of_match_ptr("SUDCDC_REG4"),
271 		.regulators_node = of_match_ptr("regulators"),
272 		.id = ACT8600_ID_SUDCDC4,
273 		.ops = &act8865_ops,
274 		.type = REGULATOR_VOLTAGE,
275 		.n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
276 		.linear_ranges = act8600_sudcdc_voltage_ranges,
277 		.n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
278 		.vsel_reg = ACT8600_SUDCDC4_VSET,
279 		.vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
280 		.enable_reg = ACT8600_SUDCDC4_CTRL,
281 		.enable_mask = ACT8865_ENA,
282 		.owner = THIS_MODULE,
283 	},
284 	ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
285 	ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
286 	ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
287 	ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
288 	{
289 		.name = "LDO_REG9",
290 		.of_match = of_match_ptr("LDO_REG9"),
291 		.regulators_node = of_match_ptr("regulators"),
292 		.id = ACT8600_ID_LDO9,
293 		.ops = &act8865_ldo_ops,
294 		.type = REGULATOR_VOLTAGE,
295 		.n_voltages = 1,
296 		.fixed_uV = 3300000,
297 		.enable_reg = ACT8600_LDO910_CTRL,
298 		.enable_mask = ACT8865_ENA,
299 		.owner = THIS_MODULE,
300 	},
301 	{
302 		.name = "LDO_REG10",
303 		.of_match = of_match_ptr("LDO_REG10"),
304 		.regulators_node = of_match_ptr("regulators"),
305 		.id = ACT8600_ID_LDO10,
306 		.ops = &act8865_ldo_ops,
307 		.type = REGULATOR_VOLTAGE,
308 		.n_voltages = 1,
309 		.fixed_uV = 1200000,
310 		.enable_reg = ACT8600_LDO910_CTRL,
311 		.enable_mask = ACT8600_LDO10_ENA,
312 		.owner = THIS_MODULE,
313 	},
314 };
315 
316 static const struct regulator_desc act8846_regulators[] = {
317 	ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
318 	ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
319 	ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
320 	ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
321 	ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
322 	ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
323 	ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
324 	ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
325 	ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
326 	ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
327 	ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
328 	ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
329 };
330 
331 static const struct regulator_desc act8865_regulators[] = {
332 	ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
333 	ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
334 	ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
335 	ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
336 	ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
337 	ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
338 	ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
339 };
340 
341 static const struct regulator_desc act8865_alt_regulators[] = {
342 	ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
343 	ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
344 	ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
345 	ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
346 	ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
347 	ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
348 	ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
349 };
350 
351 #ifdef CONFIG_OF
352 static const struct of_device_id act8865_dt_ids[] = {
353 	{ .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
354 	{ .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
355 	{ .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
356 	{ }
357 };
358 MODULE_DEVICE_TABLE(of, act8865_dt_ids);
359 #endif
360 
361 static struct act8865_regulator_data *act8865_get_regulator_data(
362 		int id, struct act8865_platform_data *pdata)
363 {
364 	int i;
365 
366 	for (i = 0; i < pdata->num_regulators; i++) {
367 		if (pdata->regulators[i].id == id)
368 			return &pdata->regulators[i];
369 	}
370 
371 	return NULL;
372 }
373 
374 static struct i2c_client *act8865_i2c_client;
375 static void act8865_power_off(void)
376 {
377 	struct act8865 *act8865;
378 
379 	act8865 = i2c_get_clientdata(act8865_i2c_client);
380 	regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
381 	while (1);
382 }
383 
384 static int act8865_pmic_probe(struct i2c_client *client,
385 			      const struct i2c_device_id *i2c_id)
386 {
387 	const struct regulator_desc *regulators;
388 	struct act8865_platform_data *pdata = NULL;
389 	struct device *dev = &client->dev;
390 	int i, ret, num_regulators;
391 	struct act8865 *act8865;
392 	const struct regmap_config *regmap_config;
393 	unsigned long type;
394 	int off_reg, off_mask;
395 	int voltage_select = 0;
396 
397 	if (dev->of_node) {
398 		const struct of_device_id *id;
399 
400 		id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
401 		if (!id)
402 			return -ENODEV;
403 
404 		type = (unsigned long) id->data;
405 
406 		voltage_select = !!of_get_property(dev->of_node,
407 						   "active-semi,vsel-high",
408 						   NULL);
409 	} else {
410 		type = i2c_id->driver_data;
411 		pdata = dev_get_platdata(dev);
412 	}
413 
414 	switch (type) {
415 	case ACT8600:
416 		regulators = act8600_regulators;
417 		num_regulators = ARRAY_SIZE(act8600_regulators);
418 		regmap_config = &act8600_regmap_config;
419 		off_reg = -1;
420 		off_mask = -1;
421 		break;
422 	case ACT8846:
423 		regulators = act8846_regulators;
424 		num_regulators = ARRAY_SIZE(act8846_regulators);
425 		regmap_config = &act8865_regmap_config;
426 		off_reg = ACT8846_GLB_OFF_CTRL;
427 		off_mask = ACT8846_OFF_SYSMASK;
428 		break;
429 	case ACT8865:
430 		if (voltage_select) {
431 			regulators = act8865_alt_regulators;
432 			num_regulators = ARRAY_SIZE(act8865_alt_regulators);
433 		} else {
434 			regulators = act8865_regulators;
435 			num_regulators = ARRAY_SIZE(act8865_regulators);
436 		}
437 		regmap_config = &act8865_regmap_config;
438 		off_reg = ACT8865_SYS_CTRL;
439 		off_mask = ACT8865_MSTROFF;
440 		break;
441 	default:
442 		dev_err(dev, "invalid device id %lu\n", type);
443 		return -EINVAL;
444 	}
445 
446 	act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
447 	if (!act8865)
448 		return -ENOMEM;
449 
450 	act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
451 	if (IS_ERR(act8865->regmap)) {
452 		ret = PTR_ERR(act8865->regmap);
453 		dev_err(dev, "Failed to allocate register map: %d\n", ret);
454 		return ret;
455 	}
456 
457 	if (of_device_is_system_power_controller(dev->of_node)) {
458 		if (!pm_power_off && (off_reg > 0)) {
459 			act8865_i2c_client = client;
460 			act8865->off_reg = off_reg;
461 			act8865->off_mask = off_mask;
462 			pm_power_off = act8865_power_off;
463 		} else {
464 			dev_err(dev, "Failed to set poweroff capability, already defined\n");
465 		}
466 	}
467 
468 	/* Finally register devices */
469 	for (i = 0; i < num_regulators; i++) {
470 		const struct regulator_desc *desc = &regulators[i];
471 		struct regulator_config config = { };
472 		struct regulator_dev *rdev;
473 
474 		config.dev = dev;
475 		config.driver_data = act8865;
476 		config.regmap = act8865->regmap;
477 
478 		if (pdata) {
479 			struct act8865_regulator_data *rdata;
480 
481 			rdata = act8865_get_regulator_data(desc->id, pdata);
482 			if (rdata) {
483 				config.init_data = rdata->init_data;
484 				config.of_node = rdata->of_node;
485 			}
486 		}
487 
488 		rdev = devm_regulator_register(dev, desc, &config);
489 		if (IS_ERR(rdev)) {
490 			dev_err(dev, "failed to register %s\n", desc->name);
491 			return PTR_ERR(rdev);
492 		}
493 	}
494 
495 	i2c_set_clientdata(client, act8865);
496 
497 	return 0;
498 }
499 
500 static const struct i2c_device_id act8865_ids[] = {
501 	{ .name = "act8600", .driver_data = ACT8600 },
502 	{ .name = "act8846", .driver_data = ACT8846 },
503 	{ .name = "act8865", .driver_data = ACT8865 },
504 	{ },
505 };
506 MODULE_DEVICE_TABLE(i2c, act8865_ids);
507 
508 static struct i2c_driver act8865_pmic_driver = {
509 	.driver	= {
510 		.name	= "act8865",
511 	},
512 	.probe		= act8865_pmic_probe,
513 	.id_table	= act8865_ids,
514 };
515 
516 module_i2c_driver(act8865_pmic_driver);
517 
518 MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
519 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
520 MODULE_LICENSE("GPL v2");
521