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