1/*
2 * Regulator driver for Rockchip RK808
3 *
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5 *
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16 * more details.
17 */
18
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/mfd/rk808.h>
22#include <linux/of_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/of_regulator.h>
25
26/* Field Definitions */
27#define RK808_BUCK_VSEL_MASK	0x3f
28#define RK808_BUCK4_VSEL_MASK	0xf
29#define RK808_LDO_VSEL_MASK	0x1f
30
31/* Ramp rate definitions for buck1 / buck2 only */
32#define RK808_RAMP_RATE_OFFSET		3
33#define RK808_RAMP_RATE_MASK		(3 << RK808_RAMP_RATE_OFFSET)
34#define RK808_RAMP_RATE_2MV_PER_US	(0 << RK808_RAMP_RATE_OFFSET)
35#define RK808_RAMP_RATE_4MV_PER_US	(1 << RK808_RAMP_RATE_OFFSET)
36#define RK808_RAMP_RATE_6MV_PER_US	(2 << RK808_RAMP_RATE_OFFSET)
37#define RK808_RAMP_RATE_10MV_PER_US	(3 << RK808_RAMP_RATE_OFFSET)
38
39/* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
40#define RK808_SLP_REG_OFFSET 1
41
42/* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
43#define RK808_SLP_SET_OFF_REG_OFFSET 2
44
45static const int rk808_buck_config_regs[] = {
46	RK808_BUCK1_CONFIG_REG,
47	RK808_BUCK2_CONFIG_REG,
48	RK808_BUCK3_CONFIG_REG,
49	RK808_BUCK4_CONFIG_REG,
50};
51
52static const struct regulator_linear_range rk808_buck_voltage_ranges[] = {
53	REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
54};
55
56static const struct regulator_linear_range rk808_buck4_voltage_ranges[] = {
57	REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
58};
59
60static const struct regulator_linear_range rk808_ldo_voltage_ranges[] = {
61	REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
62};
63
64static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
65	REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
66	REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
67};
68
69static const struct regulator_linear_range rk808_ldo6_voltage_ranges[] = {
70	REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
71};
72
73static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
74{
75	unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
76	unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
77						  RK808_ID_DCDC1];
78
79	switch (ramp_delay) {
80	case 1 ... 2000:
81		ramp_value = RK808_RAMP_RATE_2MV_PER_US;
82		break;
83	case 2001 ... 4000:
84		ramp_value = RK808_RAMP_RATE_4MV_PER_US;
85		break;
86	case 4001 ... 6000:
87		ramp_value = RK808_RAMP_RATE_6MV_PER_US;
88		break;
89	case 6001 ... 10000:
90		break;
91	default:
92		pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
93			rdev->desc->name, ramp_delay);
94	}
95
96	return regmap_update_bits(rdev->regmap, reg,
97				  RK808_RAMP_RATE_MASK, ramp_value);
98}
99
100static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
101{
102	unsigned int reg;
103	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
104
105	if (sel < 0)
106		return -EINVAL;
107
108	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
109
110	return regmap_update_bits(rdev->regmap, reg,
111				  rdev->desc->vsel_mask,
112				  sel);
113}
114
115static int rk808_set_suspend_enable(struct regulator_dev *rdev)
116{
117	unsigned int reg;
118
119	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
120
121	return regmap_update_bits(rdev->regmap, reg,
122				  rdev->desc->enable_mask,
123				  0);
124}
125
126static int rk808_set_suspend_disable(struct regulator_dev *rdev)
127{
128	unsigned int reg;
129
130	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
131
132	return regmap_update_bits(rdev->regmap, reg,
133				  rdev->desc->enable_mask,
134				  rdev->desc->enable_mask);
135}
136
137static struct regulator_ops rk808_buck1_2_ops = {
138	.list_voltage		= regulator_list_voltage_linear_range,
139	.map_voltage		= regulator_map_voltage_linear_range,
140	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
141	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
142	.enable			= regulator_enable_regmap,
143	.disable		= regulator_disable_regmap,
144	.is_enabled		= regulator_is_enabled_regmap,
145	.set_ramp_delay		= rk808_set_ramp_delay,
146	.set_suspend_voltage	= rk808_set_suspend_voltage,
147	.set_suspend_enable	= rk808_set_suspend_enable,
148	.set_suspend_disable	= rk808_set_suspend_disable,
149};
150
151static struct regulator_ops rk808_reg_ops = {
152	.list_voltage		= regulator_list_voltage_linear_range,
153	.map_voltage		= regulator_map_voltage_linear_range,
154	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
155	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
156	.enable			= regulator_enable_regmap,
157	.disable		= regulator_disable_regmap,
158	.is_enabled		= regulator_is_enabled_regmap,
159	.set_suspend_voltage	= rk808_set_suspend_voltage,
160	.set_suspend_enable	= rk808_set_suspend_enable,
161	.set_suspend_disable	= rk808_set_suspend_disable,
162};
163
164static struct regulator_ops rk808_switch_ops = {
165	.enable			= regulator_enable_regmap,
166	.disable		= regulator_disable_regmap,
167	.is_enabled		= regulator_is_enabled_regmap,
168	.set_suspend_enable	= rk808_set_suspend_enable,
169	.set_suspend_disable	= rk808_set_suspend_disable,
170};
171
172static const struct regulator_desc rk808_reg[] = {
173	{
174		.name = "DCDC_REG1",
175		.supply_name = "vcc1",
176		.id = RK808_ID_DCDC1,
177		.ops = &rk808_buck1_2_ops,
178		.type = REGULATOR_VOLTAGE,
179		.n_voltages = 64,
180		.linear_ranges = rk808_buck_voltage_ranges,
181		.n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
182		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
183		.vsel_mask = RK808_BUCK_VSEL_MASK,
184		.enable_reg = RK808_DCDC_EN_REG,
185		.enable_mask = BIT(0),
186		.owner = THIS_MODULE,
187	}, {
188		.name = "DCDC_REG2",
189		.supply_name = "vcc2",
190		.id = RK808_ID_DCDC2,
191		.ops = &rk808_buck1_2_ops,
192		.type = REGULATOR_VOLTAGE,
193		.n_voltages = 64,
194		.linear_ranges = rk808_buck_voltage_ranges,
195		.n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
196		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
197		.vsel_mask = RK808_BUCK_VSEL_MASK,
198		.enable_reg = RK808_DCDC_EN_REG,
199		.enable_mask = BIT(1),
200		.owner = THIS_MODULE,
201	}, {
202		.name = "DCDC_REG3",
203		.supply_name = "vcc3",
204		.id = RK808_ID_DCDC3,
205		.ops = &rk808_switch_ops,
206		.type = REGULATOR_VOLTAGE,
207		.n_voltages = 1,
208		.enable_reg = RK808_DCDC_EN_REG,
209		.enable_mask = BIT(2),
210		.owner = THIS_MODULE,
211	}, {
212		.name = "DCDC_REG4",
213		.supply_name = "vcc4",
214		.id = RK808_ID_DCDC4,
215		.ops = &rk808_reg_ops,
216		.type = REGULATOR_VOLTAGE,
217		.n_voltages = 16,
218		.linear_ranges = rk808_buck4_voltage_ranges,
219		.n_linear_ranges = ARRAY_SIZE(rk808_buck4_voltage_ranges),
220		.vsel_reg = RK808_BUCK4_ON_VSEL_REG,
221		.vsel_mask = RK808_BUCK4_VSEL_MASK,
222		.enable_reg = RK808_DCDC_EN_REG,
223		.enable_mask = BIT(3),
224		.owner = THIS_MODULE,
225	}, {
226		.name = "LDO_REG1",
227		.supply_name = "vcc6",
228		.id = RK808_ID_LDO1,
229		.ops = &rk808_reg_ops,
230		.type = REGULATOR_VOLTAGE,
231		.n_voltages = 17,
232		.linear_ranges = rk808_ldo_voltage_ranges,
233		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
234		.vsel_reg = RK808_LDO1_ON_VSEL_REG,
235		.vsel_mask = RK808_LDO_VSEL_MASK,
236		.enable_reg = RK808_LDO_EN_REG,
237		.enable_mask = BIT(0),
238		.enable_time = 400,
239		.owner = THIS_MODULE,
240	}, {
241		.name = "LDO_REG2",
242		.supply_name = "vcc6",
243		.id = RK808_ID_LDO2,
244		.ops = &rk808_reg_ops,
245		.type = REGULATOR_VOLTAGE,
246		.n_voltages = 17,
247		.linear_ranges = rk808_ldo_voltage_ranges,
248		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
249		.vsel_reg = RK808_LDO2_ON_VSEL_REG,
250		.vsel_mask = RK808_LDO_VSEL_MASK,
251		.enable_reg = RK808_LDO_EN_REG,
252		.enable_mask = BIT(1),
253		.enable_time = 400,
254		.owner = THIS_MODULE,
255	}, {
256		.name = "LDO_REG3",
257		.supply_name = "vcc7",
258		.id = RK808_ID_LDO3,
259		.ops = &rk808_reg_ops,
260		.type = REGULATOR_VOLTAGE,
261		.n_voltages = 16,
262		.linear_ranges = rk808_ldo3_voltage_ranges,
263		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
264		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
265		.vsel_mask = RK808_BUCK4_VSEL_MASK,
266		.enable_reg = RK808_LDO_EN_REG,
267		.enable_mask = BIT(2),
268		.enable_time = 400,
269		.owner = THIS_MODULE,
270	}, {
271		.name = "LDO_REG4",
272		.supply_name = "vcc9",
273		.id = RK808_ID_LDO4,
274		.ops = &rk808_reg_ops,
275		.type = REGULATOR_VOLTAGE,
276		.n_voltages = 17,
277		.linear_ranges = rk808_ldo_voltage_ranges,
278		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
279		.vsel_reg = RK808_LDO4_ON_VSEL_REG,
280		.vsel_mask = RK808_LDO_VSEL_MASK,
281		.enable_reg = RK808_LDO_EN_REG,
282		.enable_mask = BIT(3),
283		.enable_time = 400,
284		.owner = THIS_MODULE,
285	}, {
286		.name = "LDO_REG5",
287		.supply_name = "vcc9",
288		.id = RK808_ID_LDO5,
289		.ops = &rk808_reg_ops,
290		.type = REGULATOR_VOLTAGE,
291		.n_voltages = 17,
292		.linear_ranges = rk808_ldo_voltage_ranges,
293		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
294		.vsel_reg = RK808_LDO5_ON_VSEL_REG,
295		.vsel_mask = RK808_LDO_VSEL_MASK,
296		.enable_reg = RK808_LDO_EN_REG,
297		.enable_mask = BIT(4),
298		.enable_time = 400,
299		.owner = THIS_MODULE,
300	}, {
301		.name = "LDO_REG6",
302		.supply_name = "vcc10",
303		.id = RK808_ID_LDO6,
304		.ops = &rk808_reg_ops,
305		.type = REGULATOR_VOLTAGE,
306		.n_voltages = 18,
307		.linear_ranges = rk808_ldo6_voltage_ranges,
308		.n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
309		.vsel_reg = RK808_LDO6_ON_VSEL_REG,
310		.vsel_mask = RK808_LDO_VSEL_MASK,
311		.enable_reg = RK808_LDO_EN_REG,
312		.enable_mask = BIT(5),
313		.enable_time = 400,
314		.owner = THIS_MODULE,
315	}, {
316		.name = "LDO_REG7",
317		.supply_name = "vcc7",
318		.id = RK808_ID_LDO7,
319		.ops = &rk808_reg_ops,
320		.type = REGULATOR_VOLTAGE,
321		.n_voltages = 18,
322		.linear_ranges = rk808_ldo6_voltage_ranges,
323		.n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
324		.vsel_reg = RK808_LDO7_ON_VSEL_REG,
325		.vsel_mask = RK808_LDO_VSEL_MASK,
326		.enable_reg = RK808_LDO_EN_REG,
327		.enable_mask = BIT(6),
328		.enable_time = 400,
329		.owner = THIS_MODULE,
330	}, {
331		.name = "LDO_REG8",
332		.supply_name = "vcc11",
333		.id = RK808_ID_LDO8,
334		.ops = &rk808_reg_ops,
335		.type = REGULATOR_VOLTAGE,
336		.n_voltages = 17,
337		.linear_ranges = rk808_ldo_voltage_ranges,
338		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
339		.vsel_reg = RK808_LDO8_ON_VSEL_REG,
340		.vsel_mask = RK808_LDO_VSEL_MASK,
341		.enable_reg = RK808_LDO_EN_REG,
342		.enable_mask = BIT(7),
343		.enable_time = 400,
344		.owner = THIS_MODULE,
345	}, {
346		.name = "SWITCH_REG1",
347		.supply_name = "vcc8",
348		.id = RK808_ID_SWITCH1,
349		.ops = &rk808_switch_ops,
350		.type = REGULATOR_VOLTAGE,
351		.enable_reg = RK808_DCDC_EN_REG,
352		.enable_mask = BIT(5),
353		.owner = THIS_MODULE,
354	}, {
355		.name = "SWITCH_REG2",
356		.supply_name = "vcc12",
357		.id = RK808_ID_SWITCH2,
358		.ops = &rk808_switch_ops,
359		.type = REGULATOR_VOLTAGE,
360		.enable_reg = RK808_DCDC_EN_REG,
361		.enable_mask = BIT(6),
362		.owner = THIS_MODULE,
363	},
364};
365
366static struct of_regulator_match rk808_reg_matches[] = {
367	[RK808_ID_DCDC1]	= { .name = "DCDC_REG1" },
368	[RK808_ID_DCDC2]	= { .name = "DCDC_REG2" },
369	[RK808_ID_DCDC3]	= { .name = "DCDC_REG3" },
370	[RK808_ID_DCDC4]	= { .name = "DCDC_REG4" },
371	[RK808_ID_LDO1]		= { .name = "LDO_REG1" },
372	[RK808_ID_LDO2]		= { .name = "LDO_REG2" },
373	[RK808_ID_LDO3]		= { .name = "LDO_REG3" },
374	[RK808_ID_LDO4]		= { .name = "LDO_REG4" },
375	[RK808_ID_LDO5]		= { .name = "LDO_REG5" },
376	[RK808_ID_LDO6]		= { .name = "LDO_REG6" },
377	[RK808_ID_LDO7]		= { .name = "LDO_REG7" },
378	[RK808_ID_LDO8]		= { .name = "LDO_REG8" },
379	[RK808_ID_SWITCH1]	= { .name = "SWITCH_REG1" },
380	[RK808_ID_SWITCH2]	= { .name = "SWITCH_REG2" },
381};
382
383static int rk808_regulator_probe(struct platform_device *pdev)
384{
385	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
386	struct i2c_client *client = rk808->i2c;
387	struct device_node *reg_np;
388	struct regulator_config config = {};
389	struct regulator_dev *rk808_rdev;
390	int ret, i;
391
392	reg_np = of_get_child_by_name(client->dev.of_node, "regulators");
393	if (!reg_np)
394		return -ENXIO;
395
396	ret = of_regulator_match(&pdev->dev, reg_np, rk808_reg_matches,
397				 RK808_NUM_REGULATORS);
398	of_node_put(reg_np);
399	if (ret < 0)
400		return ret;
401
402	/* Instantiate the regulators */
403	for (i = 0; i < RK808_NUM_REGULATORS; i++) {
404		if (!rk808_reg_matches[i].init_data ||
405		    !rk808_reg_matches[i].of_node)
406			continue;
407
408		config.dev = &client->dev;
409		config.driver_data = rk808;
410		config.regmap = rk808->regmap;
411		config.of_node = rk808_reg_matches[i].of_node;
412		config.init_data = rk808_reg_matches[i].init_data;
413
414		rk808_rdev = devm_regulator_register(&pdev->dev,
415						     &rk808_reg[i], &config);
416		if (IS_ERR(rk808_rdev)) {
417			dev_err(&client->dev,
418				"failed to register %d regulator\n", i);
419			return PTR_ERR(rk808_rdev);
420		}
421	}
422
423	return 0;
424}
425
426static struct platform_driver rk808_regulator_driver = {
427	.probe = rk808_regulator_probe,
428	.driver = {
429		.name = "rk808-regulator",
430	},
431};
432
433module_platform_driver(rk808_regulator_driver);
434
435MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
436MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
437MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
438MODULE_LICENSE("GPL");
439MODULE_ALIAS("platform:rk808-regulator");
440