Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) ST-Ericsson SA 2010
4 *
5 * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
6 *
7 * This file is based on drivers/regulator/ab8500.c
8 *
9 * AB8500 external regulators
10 *
11 * ab8500-ext supports the following regulators:
12 * - VextSupply3
13 */
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/err.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/of_regulator.h>
23#include <linux/mfd/abx500.h>
24#include <linux/mfd/abx500/ab8500.h>
25
26/* AB8500 external regulators */
27enum ab8500_ext_regulator_id {
28 AB8500_EXT_SUPPLY1,
29 AB8500_EXT_SUPPLY2,
30 AB8500_EXT_SUPPLY3,
31 AB8500_NUM_EXT_REGULATORS,
32};
33
34struct ab8500_ext_regulator_cfg {
35 bool hwreq; /* requires hw mode or high power mode */
36};
37
38/* supply for VextSupply3 */
39static struct regulator_consumer_supply ab8500_ext_supply3_consumers[] = {
40 /* SIM supply for 3 V SIM cards */
41 REGULATOR_SUPPLY("vinvsim", "sim-detect.0"),
42};
43
44/*
45 * AB8500 external regulators
46 */
47static struct regulator_init_data ab8500_ext_regulators[] = {
48 /* fixed Vbat supplies VSMPS1_EXT_1V8 */
49 [AB8500_EXT_SUPPLY1] = {
50 .constraints = {
51 .name = "ab8500-ext-supply1",
52 .min_uV = 1800000,
53 .max_uV = 1800000,
54 .initial_mode = REGULATOR_MODE_IDLE,
55 .boot_on = 1,
56 .always_on = 1,
57 },
58 },
59 /* fixed Vbat supplies VSMPS2_EXT_1V36 and VSMPS5_EXT_1V15 */
60 [AB8500_EXT_SUPPLY2] = {
61 .constraints = {
62 .name = "ab8500-ext-supply2",
63 .min_uV = 1360000,
64 .max_uV = 1360000,
65 },
66 },
67 /* fixed Vbat supplies VSMPS3_EXT_3V4 and VSMPS4_EXT_3V4 */
68 [AB8500_EXT_SUPPLY3] = {
69 .constraints = {
70 .name = "ab8500-ext-supply3",
71 .min_uV = 3400000,
72 .max_uV = 3400000,
73 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
74 .boot_on = 1,
75 },
76 .num_consumer_supplies =
77 ARRAY_SIZE(ab8500_ext_supply3_consumers),
78 .consumer_supplies = ab8500_ext_supply3_consumers,
79 },
80};
81
82/**
83 * struct ab8500_ext_regulator_info - ab8500 regulator information
84 * @dev: device pointer
85 * @desc: regulator description
86 * @cfg: regulator configuration (extension of regulator FW configuration)
87 * @update_bank: bank to control on/off
88 * @update_reg: register to control on/off
89 * @update_mask: mask to enable/disable and set mode of regulator
90 * @update_val: bits holding the regulator current mode
91 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
92 * normally this means high power mode
93 * @update_val_lp: bits to set EN pin active and LPn pin active
94 * normally this means low power mode
95 * @update_val_hw: bits to set regulator pins in HW control
96 * SysClkReq pins and logic will choose mode
97 */
98struct ab8500_ext_regulator_info {
99 struct device *dev;
100 struct regulator_desc desc;
101 struct ab8500_ext_regulator_cfg *cfg;
102 u8 update_bank;
103 u8 update_reg;
104 u8 update_mask;
105 u8 update_val;
106 u8 update_val_hp;
107 u8 update_val_lp;
108 u8 update_val_hw;
109};
110
111static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
112{
113 int ret;
114 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
115 u8 regval;
116
117 if (info == NULL) {
118 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
119 return -EINVAL;
120 }
121
122 /*
123 * To satisfy both HW high power request and SW request, the regulator
124 * must be on in high power.
125 */
126 if (info->cfg && info->cfg->hwreq)
127 regval = info->update_val_hp;
128 else
129 regval = info->update_val;
130
131 ret = abx500_mask_and_set_register_interruptible(info->dev,
132 info->update_bank, info->update_reg,
133 info->update_mask, regval);
134 if (ret < 0) {
135 dev_err(rdev_get_dev(rdev),
136 "couldn't set enable bits for regulator\n");
137 return ret;
138 }
139
140 dev_dbg(rdev_get_dev(rdev),
141 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
142 info->desc.name, info->update_bank, info->update_reg,
143 info->update_mask, regval);
144
145 return 0;
146}
147
148static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
149{
150 int ret;
151 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
152 u8 regval;
153
154 if (info == NULL) {
155 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
156 return -EINVAL;
157 }
158
159 /*
160 * Set the regulator in HW request mode if configured
161 */
162 if (info->cfg && info->cfg->hwreq)
163 regval = info->update_val_hw;
164 else
165 regval = 0;
166
167 ret = abx500_mask_and_set_register_interruptible(info->dev,
168 info->update_bank, info->update_reg,
169 info->update_mask, regval);
170 if (ret < 0) {
171 dev_err(rdev_get_dev(rdev),
172 "couldn't set disable bits for regulator\n");
173 return ret;
174 }
175
176 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
177 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
178 info->desc.name, info->update_bank, info->update_reg,
179 info->update_mask, regval);
180
181 return 0;
182}
183
184static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
185{
186 int ret;
187 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
188 u8 regval;
189
190 if (info == NULL) {
191 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
192 return -EINVAL;
193 }
194
195 ret = abx500_get_register_interruptible(info->dev,
196 info->update_bank, info->update_reg, ®val);
197 if (ret < 0) {
198 dev_err(rdev_get_dev(rdev),
199 "couldn't read 0x%x register\n", info->update_reg);
200 return ret;
201 }
202
203 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
204 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
205 info->desc.name, info->update_bank, info->update_reg,
206 info->update_mask, regval);
207
208 if (((regval & info->update_mask) == info->update_val_lp) ||
209 ((regval & info->update_mask) == info->update_val_hp))
210 return 1;
211 else
212 return 0;
213}
214
215static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
216 unsigned int mode)
217{
218 int ret = 0;
219 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
220 u8 regval;
221
222 if (info == NULL) {
223 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
224 return -EINVAL;
225 }
226
227 switch (mode) {
228 case REGULATOR_MODE_NORMAL:
229 regval = info->update_val_hp;
230 break;
231 case REGULATOR_MODE_IDLE:
232 regval = info->update_val_lp;
233 break;
234
235 default:
236 return -EINVAL;
237 }
238
239 /* If regulator is enabled and info->cfg->hwreq is set, the regulator
240 must be on in high power, so we don't need to write the register with
241 the same value.
242 */
243 if (ab8500_ext_regulator_is_enabled(rdev) &&
244 !(info->cfg && info->cfg->hwreq)) {
245 ret = abx500_mask_and_set_register_interruptible(info->dev,
246 info->update_bank, info->update_reg,
247 info->update_mask, regval);
248 if (ret < 0) {
249 dev_err(rdev_get_dev(rdev),
250 "Could not set regulator mode.\n");
251 return ret;
252 }
253
254 dev_dbg(rdev_get_dev(rdev),
255 "%s-set_mode (bank, reg, mask, value): "
256 "0x%x, 0x%x, 0x%x, 0x%x\n",
257 info->desc.name, info->update_bank, info->update_reg,
258 info->update_mask, regval);
259 }
260
261 info->update_val = regval;
262
263 return 0;
264}
265
266static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
267{
268 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
269 int ret;
270
271 if (info == NULL) {
272 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
273 return -EINVAL;
274 }
275
276 if (info->update_val == info->update_val_hp)
277 ret = REGULATOR_MODE_NORMAL;
278 else if (info->update_val == info->update_val_lp)
279 ret = REGULATOR_MODE_IDLE;
280 else
281 ret = -EINVAL;
282
283 return ret;
284}
285
286static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
287 int max_uV, unsigned *selector)
288{
289 struct regulation_constraints *regu_constraints = rdev->constraints;
290
291 if (!regu_constraints) {
292 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
293 return -EINVAL;
294 }
295
296 if (regu_constraints->min_uV == min_uV &&
297 regu_constraints->max_uV == max_uV)
298 return 0;
299
300 dev_err(rdev_get_dev(rdev),
301 "Requested min %duV max %duV != constrained min %duV max %duV\n",
302 min_uV, max_uV,
303 regu_constraints->min_uV, regu_constraints->max_uV);
304
305 return -EINVAL;
306}
307
308static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
309 unsigned selector)
310{
311 struct regulation_constraints *regu_constraints = rdev->constraints;
312
313 if (regu_constraints == NULL) {
314 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
315 return -EINVAL;
316 }
317 /* return the uV for the fixed regulators */
318 if (regu_constraints->min_uV && regu_constraints->max_uV) {
319 if (regu_constraints->min_uV == regu_constraints->max_uV)
320 return regu_constraints->min_uV;
321 }
322 return -EINVAL;
323}
324
325static const struct regulator_ops ab8500_ext_regulator_ops = {
326 .enable = ab8500_ext_regulator_enable,
327 .disable = ab8500_ext_regulator_disable,
328 .is_enabled = ab8500_ext_regulator_is_enabled,
329 .set_mode = ab8500_ext_regulator_set_mode,
330 .get_mode = ab8500_ext_regulator_get_mode,
331 .set_voltage = ab8500_ext_set_voltage,
332 .list_voltage = ab8500_ext_list_voltage,
333};
334
335static struct ab8500_ext_regulator_info
336 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
337 [AB8500_EXT_SUPPLY1] = {
338 .desc = {
339 .name = "VEXTSUPPLY1",
340 .of_match = of_match_ptr("ab8500_ext1"),
341 .ops = &ab8500_ext_regulator_ops,
342 .type = REGULATOR_VOLTAGE,
343 .id = AB8500_EXT_SUPPLY1,
344 .owner = THIS_MODULE,
345 .n_voltages = 1,
346 },
347 .update_bank = 0x04,
348 .update_reg = 0x08,
349 .update_mask = 0x03,
350 .update_val = 0x01,
351 .update_val_hp = 0x01,
352 .update_val_lp = 0x03,
353 .update_val_hw = 0x02,
354 },
355 [AB8500_EXT_SUPPLY2] = {
356 .desc = {
357 .name = "VEXTSUPPLY2",
358 .of_match = of_match_ptr("ab8500_ext2"),
359 .ops = &ab8500_ext_regulator_ops,
360 .type = REGULATOR_VOLTAGE,
361 .id = AB8500_EXT_SUPPLY2,
362 .owner = THIS_MODULE,
363 .n_voltages = 1,
364 },
365 .update_bank = 0x04,
366 .update_reg = 0x08,
367 .update_mask = 0x0c,
368 .update_val = 0x04,
369 .update_val_hp = 0x04,
370 .update_val_lp = 0x0c,
371 .update_val_hw = 0x08,
372 },
373 [AB8500_EXT_SUPPLY3] = {
374 .desc = {
375 .name = "VEXTSUPPLY3",
376 .of_match = of_match_ptr("ab8500_ext3"),
377 .ops = &ab8500_ext_regulator_ops,
378 .type = REGULATOR_VOLTAGE,
379 .id = AB8500_EXT_SUPPLY3,
380 .owner = THIS_MODULE,
381 .n_voltages = 1,
382 },
383 .update_bank = 0x04,
384 .update_reg = 0x08,
385 .update_mask = 0x30,
386 .update_val = 0x10,
387 .update_val_hp = 0x10,
388 .update_val_lp = 0x30,
389 .update_val_hw = 0x20,
390 },
391};
392
393static int ab8500_ext_regulator_probe(struct platform_device *pdev)
394{
395 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
396 struct regulator_config config = { };
397 struct regulator_dev *rdev;
398 int i;
399
400 if (!ab8500) {
401 dev_err(&pdev->dev, "null mfd parent\n");
402 return -EINVAL;
403 }
404
405 /* check for AB8500 2.x */
406 if (is_ab8500_2p0_or_earlier(ab8500)) {
407 struct ab8500_ext_regulator_info *info;
408
409 /* VextSupply3LPn is inverted on AB8500 2.x */
410 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
411 info->update_val = 0x30;
412 info->update_val_hp = 0x30;
413 info->update_val_lp = 0x10;
414 }
415
416 /* register all regulators */
417 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
418 struct ab8500_ext_regulator_info *info = NULL;
419
420 /* assign per-regulator data */
421 info = &ab8500_ext_regulator_info[i];
422 info->dev = &pdev->dev;
423 info->cfg = (struct ab8500_ext_regulator_cfg *)
424 ab8500_ext_regulators[i].driver_data;
425
426 config.dev = &pdev->dev;
427 config.driver_data = info;
428 config.init_data = &ab8500_ext_regulators[i];
429
430 /* register regulator with framework */
431 rdev = devm_regulator_register(&pdev->dev, &info->desc,
432 &config);
433 if (IS_ERR(rdev)) {
434 dev_err(&pdev->dev, "failed to register regulator %s\n",
435 info->desc.name);
436 return PTR_ERR(rdev);
437 }
438
439 dev_dbg(&pdev->dev, "%s-probed\n", info->desc.name);
440 }
441
442 return 0;
443}
444
445static struct platform_driver ab8500_ext_regulator_driver = {
446 .probe = ab8500_ext_regulator_probe,
447 .driver = {
448 .name = "ab8500-ext-regulator",
449 },
450};
451
452static int __init ab8500_ext_regulator_init(void)
453{
454 int ret;
455
456 ret = platform_driver_register(&ab8500_ext_regulator_driver);
457 if (ret)
458 pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
459
460 return ret;
461}
462subsys_initcall(ab8500_ext_regulator_init);
463
464static void __exit ab8500_ext_regulator_exit(void)
465{
466 platform_driver_unregister(&ab8500_ext_regulator_driver);
467}
468module_exit(ab8500_ext_regulator_exit);
469
470MODULE_LICENSE("GPL v2");
471MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
472MODULE_DESCRIPTION("AB8500 external regulator driver");
473MODULE_ALIAS("platform:ab8500-ext-regulator");
1/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 *
6 * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
7 *
8 * This file is based on drivers/regulator/ab8500.c
9 *
10 * AB8500 external regulators
11 *
12 * ab8500-ext supports the following regulators:
13 * - VextSupply3
14 */
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
23#include <linux/regulator/of_regulator.h>
24#include <linux/mfd/abx500.h>
25#include <linux/mfd/abx500/ab8500.h>
26#include <linux/regulator/ab8500.h>
27
28/**
29 * struct ab8500_ext_regulator_info - ab8500 regulator information
30 * @dev: device pointer
31 * @desc: regulator description
32 * @rdev: regulator device
33 * @cfg: regulator configuration (extension of regulator FW configuration)
34 * @update_bank: bank to control on/off
35 * @update_reg: register to control on/off
36 * @update_mask: mask to enable/disable and set mode of regulator
37 * @update_val: bits holding the regulator current mode
38 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
39 * normally this means high power mode
40 * @update_val_lp: bits to set EN pin active and LPn pin active
41 * normally this means low power mode
42 * @update_val_hw: bits to set regulator pins in HW control
43 * SysClkReq pins and logic will choose mode
44 */
45struct ab8500_ext_regulator_info {
46 struct device *dev;
47 struct regulator_desc desc;
48 struct regulator_dev *rdev;
49 struct ab8500_ext_regulator_cfg *cfg;
50 u8 update_bank;
51 u8 update_reg;
52 u8 update_mask;
53 u8 update_val;
54 u8 update_val_hp;
55 u8 update_val_lp;
56 u8 update_val_hw;
57};
58
59static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
60{
61 int ret;
62 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
63 u8 regval;
64
65 if (info == NULL) {
66 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
67 return -EINVAL;
68 }
69
70 /*
71 * To satisfy both HW high power request and SW request, the regulator
72 * must be on in high power.
73 */
74 if (info->cfg && info->cfg->hwreq)
75 regval = info->update_val_hp;
76 else
77 regval = info->update_val;
78
79 ret = abx500_mask_and_set_register_interruptible(info->dev,
80 info->update_bank, info->update_reg,
81 info->update_mask, regval);
82 if (ret < 0) {
83 dev_err(rdev_get_dev(info->rdev),
84 "couldn't set enable bits for regulator\n");
85 return ret;
86 }
87
88 dev_dbg(rdev_get_dev(rdev),
89 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
90 info->desc.name, info->update_bank, info->update_reg,
91 info->update_mask, regval);
92
93 return 0;
94}
95
96static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
97{
98 int ret;
99 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
100 u8 regval;
101
102 if (info == NULL) {
103 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
104 return -EINVAL;
105 }
106
107 /*
108 * Set the regulator in HW request mode if configured
109 */
110 if (info->cfg && info->cfg->hwreq)
111 regval = info->update_val_hw;
112 else
113 regval = 0;
114
115 ret = abx500_mask_and_set_register_interruptible(info->dev,
116 info->update_bank, info->update_reg,
117 info->update_mask, regval);
118 if (ret < 0) {
119 dev_err(rdev_get_dev(info->rdev),
120 "couldn't set disable bits for regulator\n");
121 return ret;
122 }
123
124 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
125 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
126 info->desc.name, info->update_bank, info->update_reg,
127 info->update_mask, regval);
128
129 return 0;
130}
131
132static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
133{
134 int ret;
135 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
136 u8 regval;
137
138 if (info == NULL) {
139 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
140 return -EINVAL;
141 }
142
143 ret = abx500_get_register_interruptible(info->dev,
144 info->update_bank, info->update_reg, ®val);
145 if (ret < 0) {
146 dev_err(rdev_get_dev(rdev),
147 "couldn't read 0x%x register\n", info->update_reg);
148 return ret;
149 }
150
151 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
152 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
153 info->desc.name, info->update_bank, info->update_reg,
154 info->update_mask, regval);
155
156 if (((regval & info->update_mask) == info->update_val_lp) ||
157 ((regval & info->update_mask) == info->update_val_hp))
158 return 1;
159 else
160 return 0;
161}
162
163static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
164 unsigned int mode)
165{
166 int ret = 0;
167 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
168 u8 regval;
169
170 if (info == NULL) {
171 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
172 return -EINVAL;
173 }
174
175 switch (mode) {
176 case REGULATOR_MODE_NORMAL:
177 regval = info->update_val_hp;
178 break;
179 case REGULATOR_MODE_IDLE:
180 regval = info->update_val_lp;
181 break;
182
183 default:
184 return -EINVAL;
185 }
186
187 /* If regulator is enabled and info->cfg->hwreq is set, the regulator
188 must be on in high power, so we don't need to write the register with
189 the same value.
190 */
191 if (ab8500_ext_regulator_is_enabled(rdev) &&
192 !(info->cfg && info->cfg->hwreq)) {
193 ret = abx500_mask_and_set_register_interruptible(info->dev,
194 info->update_bank, info->update_reg,
195 info->update_mask, regval);
196 if (ret < 0) {
197 dev_err(rdev_get_dev(rdev),
198 "Could not set regulator mode.\n");
199 return ret;
200 }
201
202 dev_dbg(rdev_get_dev(rdev),
203 "%s-set_mode (bank, reg, mask, value): "
204 "0x%x, 0x%x, 0x%x, 0x%x\n",
205 info->desc.name, info->update_bank, info->update_reg,
206 info->update_mask, regval);
207 }
208
209 info->update_val = regval;
210
211 return 0;
212}
213
214static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
215{
216 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
217 int ret;
218
219 if (info == NULL) {
220 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
221 return -EINVAL;
222 }
223
224 if (info->update_val == info->update_val_hp)
225 ret = REGULATOR_MODE_NORMAL;
226 else if (info->update_val == info->update_val_lp)
227 ret = REGULATOR_MODE_IDLE;
228 else
229 ret = -EINVAL;
230
231 return ret;
232}
233
234static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
235 int max_uV, unsigned *selector)
236{
237 struct regulation_constraints *regu_constraints = rdev->constraints;
238
239 if (!regu_constraints) {
240 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
241 return -EINVAL;
242 }
243
244 if (regu_constraints->min_uV == min_uV &&
245 regu_constraints->max_uV == max_uV)
246 return 0;
247
248 dev_err(rdev_get_dev(rdev),
249 "Requested min %duV max %duV != constrained min %duV max %duV\n",
250 min_uV, max_uV,
251 regu_constraints->min_uV, regu_constraints->max_uV);
252
253 return -EINVAL;
254}
255
256static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
257 unsigned selector)
258{
259 struct regulation_constraints *regu_constraints = rdev->constraints;
260
261 if (regu_constraints == NULL) {
262 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
263 return -EINVAL;
264 }
265 /* return the uV for the fixed regulators */
266 if (regu_constraints->min_uV && regu_constraints->max_uV) {
267 if (regu_constraints->min_uV == regu_constraints->max_uV)
268 return regu_constraints->min_uV;
269 }
270 return -EINVAL;
271}
272
273static struct regulator_ops ab8500_ext_regulator_ops = {
274 .enable = ab8500_ext_regulator_enable,
275 .disable = ab8500_ext_regulator_disable,
276 .is_enabled = ab8500_ext_regulator_is_enabled,
277 .set_mode = ab8500_ext_regulator_set_mode,
278 .get_mode = ab8500_ext_regulator_get_mode,
279 .set_voltage = ab8500_ext_set_voltage,
280 .list_voltage = ab8500_ext_list_voltage,
281};
282
283static struct ab8500_ext_regulator_info
284 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
285 [AB8500_EXT_SUPPLY1] = {
286 .desc = {
287 .name = "VEXTSUPPLY1",
288 .ops = &ab8500_ext_regulator_ops,
289 .type = REGULATOR_VOLTAGE,
290 .id = AB8500_EXT_SUPPLY1,
291 .owner = THIS_MODULE,
292 .n_voltages = 1,
293 },
294 .update_bank = 0x04,
295 .update_reg = 0x08,
296 .update_mask = 0x03,
297 .update_val = 0x01,
298 .update_val_hp = 0x01,
299 .update_val_lp = 0x03,
300 .update_val_hw = 0x02,
301 },
302 [AB8500_EXT_SUPPLY2] = {
303 .desc = {
304 .name = "VEXTSUPPLY2",
305 .ops = &ab8500_ext_regulator_ops,
306 .type = REGULATOR_VOLTAGE,
307 .id = AB8500_EXT_SUPPLY2,
308 .owner = THIS_MODULE,
309 .n_voltages = 1,
310 },
311 .update_bank = 0x04,
312 .update_reg = 0x08,
313 .update_mask = 0x0c,
314 .update_val = 0x04,
315 .update_val_hp = 0x04,
316 .update_val_lp = 0x0c,
317 .update_val_hw = 0x08,
318 },
319 [AB8500_EXT_SUPPLY3] = {
320 .desc = {
321 .name = "VEXTSUPPLY3",
322 .ops = &ab8500_ext_regulator_ops,
323 .type = REGULATOR_VOLTAGE,
324 .id = AB8500_EXT_SUPPLY3,
325 .owner = THIS_MODULE,
326 .n_voltages = 1,
327 },
328 .update_bank = 0x04,
329 .update_reg = 0x08,
330 .update_mask = 0x30,
331 .update_val = 0x10,
332 .update_val_hp = 0x10,
333 .update_val_lp = 0x30,
334 .update_val_hw = 0x20,
335 },
336};
337
338static struct of_regulator_match ab8500_ext_regulator_match[] = {
339 { .name = "ab8500_ext1", .driver_data = (void *) AB8500_EXT_SUPPLY1, },
340 { .name = "ab8500_ext2", .driver_data = (void *) AB8500_EXT_SUPPLY2, },
341 { .name = "ab8500_ext3", .driver_data = (void *) AB8500_EXT_SUPPLY3, },
342};
343
344static int ab8500_ext_regulator_probe(struct platform_device *pdev)
345{
346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
347 struct ab8500_platform_data *ppdata;
348 struct ab8500_regulator_platform_data *pdata;
349 struct device_node *np = pdev->dev.of_node;
350 struct regulator_config config = { };
351 int i, err;
352
353 if (np) {
354 err = of_regulator_match(&pdev->dev, np,
355 ab8500_ext_regulator_match,
356 ARRAY_SIZE(ab8500_ext_regulator_match));
357 if (err < 0) {
358 dev_err(&pdev->dev,
359 "Error parsing regulator init data: %d\n", err);
360 return err;
361 }
362 }
363
364 if (!ab8500) {
365 dev_err(&pdev->dev, "null mfd parent\n");
366 return -EINVAL;
367 }
368
369 ppdata = dev_get_platdata(ab8500->dev);
370 if (!ppdata) {
371 dev_err(&pdev->dev, "null parent pdata\n");
372 return -EINVAL;
373 }
374
375 pdata = ppdata->regulator;
376 if (!pdata) {
377 dev_err(&pdev->dev, "null pdata\n");
378 return -EINVAL;
379 }
380
381 /* make sure the platform data has the correct size */
382 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
383 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
384 return -EINVAL;
385 }
386
387 /* check for AB8500 2.x */
388 if (is_ab8500_2p0_or_earlier(ab8500)) {
389 struct ab8500_ext_regulator_info *info;
390
391 /* VextSupply3LPn is inverted on AB8500 2.x */
392 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
393 info->update_val = 0x30;
394 info->update_val_hp = 0x30;
395 info->update_val_lp = 0x10;
396 }
397
398 /* register all regulators */
399 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
400 struct ab8500_ext_regulator_info *info = NULL;
401
402 /* assign per-regulator data */
403 info = &ab8500_ext_regulator_info[i];
404 info->dev = &pdev->dev;
405 info->cfg = (struct ab8500_ext_regulator_cfg *)
406 pdata->ext_regulator[i].driver_data;
407
408 config.dev = &pdev->dev;
409 config.driver_data = info;
410 config.of_node = ab8500_ext_regulator_match[i].of_node;
411 config.init_data = (np) ?
412 ab8500_ext_regulator_match[i].init_data :
413 &pdata->ext_regulator[i];
414
415 /* register regulator with framework */
416 info->rdev = devm_regulator_register(&pdev->dev, &info->desc,
417 &config);
418 if (IS_ERR(info->rdev)) {
419 err = PTR_ERR(info->rdev);
420 dev_err(&pdev->dev, "failed to register regulator %s\n",
421 info->desc.name);
422 return err;
423 }
424
425 dev_dbg(rdev_get_dev(info->rdev),
426 "%s-probed\n", info->desc.name);
427 }
428
429 return 0;
430}
431
432static struct platform_driver ab8500_ext_regulator_driver = {
433 .probe = ab8500_ext_regulator_probe,
434 .driver = {
435 .name = "ab8500-ext-regulator",
436 },
437};
438
439static int __init ab8500_ext_regulator_init(void)
440{
441 int ret;
442
443 ret = platform_driver_register(&ab8500_ext_regulator_driver);
444 if (ret)
445 pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
446
447 return ret;
448}
449subsys_initcall(ab8500_ext_regulator_init);
450
451static void __exit ab8500_ext_regulator_exit(void)
452{
453 platform_driver_unregister(&ab8500_ext_regulator_driver);
454}
455module_exit(ab8500_ext_regulator_exit);
456
457MODULE_LICENSE("GPL v2");
458MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
459MODULE_DESCRIPTION("AB8500 external regulator driver");
460MODULE_ALIAS("platform:ab8500-ext-regulator");