1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies
4  *
5  * Copyright (C) 2012 Renesas Electronics Corporation
6  * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
7  */
8 
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/mfd/as3711.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/slab.h>
19 
20 struct as3711_regulator_info {
21 	struct regulator_desc	desc;
22 };
23 
24 struct as3711_regulator {
25 	struct as3711_regulator_info *reg_info;
26 };
27 
28 /*
29  * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and
30  * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes:
31  * FAST:	sdX_fast=1
32  * NORMAL:	low_noise=1
33  * IDLE:	low_noise=0
34  */
35 
36 static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode)
37 {
38 	unsigned int fast_bit = rdev->desc->enable_mask,
39 		low_noise_bit = fast_bit << 4;
40 	u8 val;
41 
42 	switch (mode) {
43 	case REGULATOR_MODE_FAST:
44 		val = fast_bit | low_noise_bit;
45 		break;
46 	case REGULATOR_MODE_NORMAL:
47 		val = low_noise_bit;
48 		break;
49 	case REGULATOR_MODE_IDLE:
50 		val = 0;
51 		break;
52 	default:
53 		return -EINVAL;
54 	}
55 
56 	return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1,
57 				  low_noise_bit | fast_bit, val);
58 }
59 
60 static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev)
61 {
62 	unsigned int fast_bit = rdev->desc->enable_mask,
63 		low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit;
64 	unsigned int val;
65 	int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val);
66 
67 	if (ret < 0)
68 		return ret;
69 
70 	if ((val & mask) == mask)
71 		return REGULATOR_MODE_FAST;
72 
73 	if ((val & mask) == low_noise_bit)
74 		return REGULATOR_MODE_NORMAL;
75 
76 	if (!(val & mask))
77 		return REGULATOR_MODE_IDLE;
78 
79 	return -EINVAL;
80 }
81 
82 static const struct regulator_ops as3711_sd_ops = {
83 	.is_enabled		= regulator_is_enabled_regmap,
84 	.enable			= regulator_enable_regmap,
85 	.disable		= regulator_disable_regmap,
86 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
87 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
88 	.list_voltage		= regulator_list_voltage_linear_range,
89 	.map_voltage		= regulator_map_voltage_linear_range,
90 	.get_mode		= as3711_get_mode_sd,
91 	.set_mode		= as3711_set_mode_sd,
92 };
93 
94 static const struct regulator_ops as3711_aldo_ops = {
95 	.is_enabled		= regulator_is_enabled_regmap,
96 	.enable			= regulator_enable_regmap,
97 	.disable		= regulator_disable_regmap,
98 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
99 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
100 	.list_voltage		= regulator_list_voltage_linear_range,
101 	.map_voltage		= regulator_map_voltage_linear_range,
102 };
103 
104 static const struct regulator_ops as3711_dldo_ops = {
105 	.is_enabled		= regulator_is_enabled_regmap,
106 	.enable			= regulator_enable_regmap,
107 	.disable		= regulator_disable_regmap,
108 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
109 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
110 	.list_voltage		= regulator_list_voltage_linear_range,
111 	.map_voltage		= regulator_map_voltage_linear_range,
112 };
113 
114 static const struct regulator_linear_range as3711_sd_ranges[] = {
115 	REGULATOR_LINEAR_RANGE(612500, 0x1, 0x40, 12500),
116 	REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
117 	REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7f, 50000),
118 };
119 
120 static const struct regulator_linear_range as3711_aldo_ranges[] = {
121 	REGULATOR_LINEAR_RANGE(1200000, 0, 0xf, 50000),
122 	REGULATOR_LINEAR_RANGE(1800000, 0x10, 0x1f, 100000),
123 };
124 
125 static const struct regulator_linear_range as3711_dldo_ranges[] = {
126 	REGULATOR_LINEAR_RANGE(900000, 0, 0x10, 50000),
127 	REGULATOR_LINEAR_RANGE(1750000, 0x20, 0x3f, 50000),
128 };
129 
130 #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _sfx)			   \
131 	[AS3711_REGULATOR_ ## _id] = {					   \
132 	.desc = {							   \
133 		.name = "as3711-regulator-" # _id,			   \
134 		.id = AS3711_REGULATOR_ ## _id,				   \
135 		.n_voltages = (_vmask + 1),				   \
136 		.ops = &as3711_ ## _sfx ## _ops,			   \
137 		.type = REGULATOR_VOLTAGE,				   \
138 		.owner = THIS_MODULE,					   \
139 		.vsel_reg = AS3711_ ## _id ## _VOLTAGE,			   \
140 		.vsel_mask = _vmask,					   \
141 		.enable_reg = AS3711_ ## _en_reg,			   \
142 		.enable_mask = BIT(_en_bit),				   \
143 		.linear_ranges = as3711_ ## _sfx ## _ranges,		   \
144 		.n_linear_ranges = ARRAY_SIZE(as3711_ ## _sfx ## _ranges), \
145 	},								   \
146 }
147 
148 static struct as3711_regulator_info as3711_reg_info[] = {
149 	AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, sd),
150 	AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, sd),
151 	AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, sd),
152 	AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, sd),
153 	AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, aldo),
154 	AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, aldo),
155 	AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, dldo),
156 	AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, dldo),
157 	AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, dldo),
158 	AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, dldo),
159 	AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, dldo),
160 	AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, dldo),
161 	/* StepUp output voltage depends on supplying regulator */
162 };
163 
164 #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_info)
165 
166 static struct of_regulator_match
167 as3711_regulator_matches[AS3711_REGULATOR_NUM] = {
168 	[AS3711_REGULATOR_SD_1] = { .name = "sd1" },
169 	[AS3711_REGULATOR_SD_2] = { .name = "sd2" },
170 	[AS3711_REGULATOR_SD_3] = { .name = "sd3" },
171 	[AS3711_REGULATOR_SD_4] = { .name = "sd4" },
172 	[AS3711_REGULATOR_LDO_1] = { .name = "ldo1" },
173 	[AS3711_REGULATOR_LDO_2] = { .name = "ldo2" },
174 	[AS3711_REGULATOR_LDO_3] = { .name = "ldo3" },
175 	[AS3711_REGULATOR_LDO_4] = { .name = "ldo4" },
176 	[AS3711_REGULATOR_LDO_5] = { .name = "ldo5" },
177 	[AS3711_REGULATOR_LDO_6] = { .name = "ldo6" },
178 	[AS3711_REGULATOR_LDO_7] = { .name = "ldo7" },
179 	[AS3711_REGULATOR_LDO_8] = { .name = "ldo8" },
180 };
181 
182 static int as3711_regulator_parse_dt(struct device *dev,
183 				struct device_node **of_node, const int count)
184 {
185 	struct as3711_regulator_pdata *pdata = dev_get_platdata(dev);
186 	struct device_node *regulators =
187 		of_get_child_by_name(dev->parent->of_node, "regulators");
188 	struct of_regulator_match *match;
189 	int ret, i;
190 
191 	if (!regulators) {
192 		dev_err(dev, "regulator node not found\n");
193 		return -ENODEV;
194 	}
195 
196 	ret = of_regulator_match(dev->parent, regulators,
197 				 as3711_regulator_matches, count);
198 	of_node_put(regulators);
199 	if (ret < 0) {
200 		dev_err(dev, "Error parsing regulator init data: %d\n", ret);
201 		return ret;
202 	}
203 
204 	for (i = 0, match = as3711_regulator_matches; i < count; i++, match++)
205 		if (match->of_node) {
206 			pdata->init_data[i] = match->init_data;
207 			of_node[i] = match->of_node;
208 		}
209 
210 	return 0;
211 }
212 
213 static int as3711_regulator_probe(struct platform_device *pdev)
214 {
215 	struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev);
216 	struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
217 	struct regulator_config config = {.dev = &pdev->dev,};
218 	struct as3711_regulator *reg = NULL;
219 	struct as3711_regulator *regs;
220 	struct device_node *of_node[AS3711_REGULATOR_NUM] = {};
221 	struct regulator_dev *rdev;
222 	struct as3711_regulator_info *ri;
223 	int ret;
224 	int id;
225 
226 	if (!pdata) {
227 		dev_err(&pdev->dev, "No platform data...\n");
228 		return -ENODEV;
229 	}
230 
231 	if (pdev->dev.parent->of_node) {
232 		ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM);
233 		if (ret < 0) {
234 			dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
235 			return ret;
236 		}
237 	}
238 
239 	regs = devm_kcalloc(&pdev->dev,
240 			    AS3711_REGULATOR_NUM,
241 			    sizeof(struct as3711_regulator),
242 			    GFP_KERNEL);
243 	if (!regs)
244 		return -ENOMEM;
245 
246 	for (id = 0, ri = as3711_reg_info; id < AS3711_REGULATOR_NUM; ++id, ri++) {
247 		reg = &regs[id];
248 		reg->reg_info = ri;
249 
250 		config.init_data = pdata->init_data[id];
251 		config.driver_data = reg;
252 		config.regmap = as3711->regmap;
253 		config.of_node = of_node[id];
254 
255 		rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
256 		if (IS_ERR(rdev)) {
257 			dev_err(&pdev->dev, "Failed to register regulator %s\n",
258 				ri->desc.name);
259 			return PTR_ERR(rdev);
260 		}
261 	}
262 	platform_set_drvdata(pdev, regs);
263 	return 0;
264 }
265 
266 static struct platform_driver as3711_regulator_driver = {
267 	.driver	= {
268 		.name	= "as3711-regulator",
269 	},
270 	.probe		= as3711_regulator_probe,
271 };
272 
273 static int __init as3711_regulator_init(void)
274 {
275 	return platform_driver_register(&as3711_regulator_driver);
276 }
277 subsys_initcall(as3711_regulator_init);
278 
279 static void __exit as3711_regulator_exit(void)
280 {
281 	platform_driver_unregister(&as3711_regulator_driver);
282 }
283 module_exit(as3711_regulator_exit);
284 
285 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
286 MODULE_DESCRIPTION("AS3711 regulator driver");
287 MODULE_ALIAS("platform:as3711-regulator");
288 MODULE_LICENSE("GPL v2");
289