1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
7 * This file is based on drivers/regulator/ab8500.c
9 * AB8500 external regulators
11 * ab8500-ext supports the following regulators:
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/err.h>
17 #include <linux/module.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>
26 /* AB8500 external regulators */
27 enum ab8500_ext_regulator_id
{
31 AB8500_NUM_EXT_REGULATORS
,
34 struct ab8500_ext_regulator_cfg
{
35 bool hwreq
; /* requires hw mode or high power mode */
38 /* supply for VextSupply3 */
39 static struct regulator_consumer_supply ab8500_ext_supply3_consumers
[] = {
40 /* SIM supply for 3 V SIM cards */
41 REGULATOR_SUPPLY("vinvsim", "sim-detect.0"),
45 * AB8500 external regulators
47 static struct regulator_init_data ab8500_ext_regulators
[] = {
48 /* fixed Vbat supplies VSMPS1_EXT_1V8 */
49 [AB8500_EXT_SUPPLY1
] = {
51 .name
= "ab8500-ext-supply1",
54 .initial_mode
= REGULATOR_MODE_IDLE
,
59 /* fixed Vbat supplies VSMPS2_EXT_1V36 and VSMPS5_EXT_1V15 */
60 [AB8500_EXT_SUPPLY2
] = {
62 .name
= "ab8500-ext-supply2",
67 /* fixed Vbat supplies VSMPS3_EXT_3V4 and VSMPS4_EXT_3V4 */
68 [AB8500_EXT_SUPPLY3
] = {
70 .name
= "ab8500-ext-supply3",
73 .valid_ops_mask
= REGULATOR_CHANGE_STATUS
,
76 .num_consumer_supplies
=
77 ARRAY_SIZE(ab8500_ext_supply3_consumers
),
78 .consumer_supplies
= ab8500_ext_supply3_consumers
,
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
98 struct ab8500_ext_regulator_info
{
100 struct regulator_desc desc
;
101 struct ab8500_ext_regulator_cfg
*cfg
;
111 static int ab8500_ext_regulator_enable(struct regulator_dev
*rdev
)
114 struct ab8500_ext_regulator_info
*info
= rdev_get_drvdata(rdev
);
118 dev_err(rdev_get_dev(rdev
), "regulator info null pointer\n");
123 * To satisfy both HW high power request and SW request, the regulator
124 * must be on in high power.
126 if (info
->cfg
&& info
->cfg
->hwreq
)
127 regval
= info
->update_val_hp
;
129 regval
= info
->update_val
;
131 ret
= abx500_mask_and_set_register_interruptible(info
->dev
,
132 info
->update_bank
, info
->update_reg
,
133 info
->update_mask
, regval
);
135 dev_err(rdev_get_dev(rdev
),
136 "couldn't set enable bits for regulator\n");
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
);
148 static int ab8500_ext_regulator_disable(struct regulator_dev
*rdev
)
151 struct ab8500_ext_regulator_info
*info
= rdev_get_drvdata(rdev
);
155 dev_err(rdev_get_dev(rdev
), "regulator info null pointer\n");
160 * Set the regulator in HW request mode if configured
162 if (info
->cfg
&& info
->cfg
->hwreq
)
163 regval
= info
->update_val_hw
;
167 ret
= abx500_mask_and_set_register_interruptible(info
->dev
,
168 info
->update_bank
, info
->update_reg
,
169 info
->update_mask
, regval
);
171 dev_err(rdev_get_dev(rdev
),
172 "couldn't set disable bits for regulator\n");
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
);
184 static int ab8500_ext_regulator_is_enabled(struct regulator_dev
*rdev
)
187 struct ab8500_ext_regulator_info
*info
= rdev_get_drvdata(rdev
);
191 dev_err(rdev_get_dev(rdev
), "regulator info null pointer\n");
195 ret
= abx500_get_register_interruptible(info
->dev
,
196 info
->update_bank
, info
->update_reg
, ®val
);
198 dev_err(rdev_get_dev(rdev
),
199 "couldn't read 0x%x register\n", info
->update_reg
);
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
);
208 if (((regval
& info
->update_mask
) == info
->update_val_lp
) ||
209 ((regval
& info
->update_mask
) == info
->update_val_hp
))
215 static int ab8500_ext_regulator_set_mode(struct regulator_dev
*rdev
,
219 struct ab8500_ext_regulator_info
*info
= rdev_get_drvdata(rdev
);
223 dev_err(rdev_get_dev(rdev
), "regulator info null pointer\n");
228 case REGULATOR_MODE_NORMAL
:
229 regval
= info
->update_val_hp
;
231 case REGULATOR_MODE_IDLE
:
232 regval
= info
->update_val_lp
;
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
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
);
249 dev_err(rdev_get_dev(rdev
),
250 "Could not set regulator mode.\n");
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
);
261 info
->update_val
= regval
;
266 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev
*rdev
)
268 struct ab8500_ext_regulator_info
*info
= rdev_get_drvdata(rdev
);
272 dev_err(rdev_get_dev(rdev
), "regulator info null pointer\n");
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
;
286 static int ab8500_ext_set_voltage(struct regulator_dev
*rdev
, int min_uV
,
287 int max_uV
, unsigned *selector
)
289 struct regulation_constraints
*regu_constraints
= rdev
->constraints
;
291 if (!regu_constraints
) {
292 dev_err(rdev_get_dev(rdev
), "No regulator constraints\n");
296 if (regu_constraints
->min_uV
== min_uV
&&
297 regu_constraints
->max_uV
== max_uV
)
300 dev_err(rdev_get_dev(rdev
),
301 "Requested min %duV max %duV != constrained min %duV max %duV\n",
303 regu_constraints
->min_uV
, regu_constraints
->max_uV
);
308 static int ab8500_ext_list_voltage(struct regulator_dev
*rdev
,
311 struct regulation_constraints
*regu_constraints
= rdev
->constraints
;
313 if (regu_constraints
== NULL
) {
314 dev_err(rdev_get_dev(rdev
), "regulator constraints null pointer\n");
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
;
325 static 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
,
335 static struct ab8500_ext_regulator_info
336 ab8500_ext_regulator_info
[AB8500_NUM_EXT_REGULATORS
] = {
337 [AB8500_EXT_SUPPLY1
] = {
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
,
351 .update_val_hp
= 0x01,
352 .update_val_lp
= 0x03,
353 .update_val_hw
= 0x02,
355 [AB8500_EXT_SUPPLY2
] = {
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
,
369 .update_val_hp
= 0x04,
370 .update_val_lp
= 0x0c,
371 .update_val_hw
= 0x08,
373 [AB8500_EXT_SUPPLY3
] = {
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
,
387 .update_val_hp
= 0x10,
388 .update_val_lp
= 0x30,
389 .update_val_hw
= 0x20,
393 static int ab8500_ext_regulator_probe(struct platform_device
*pdev
)
395 struct ab8500
*ab8500
= dev_get_drvdata(pdev
->dev
.parent
);
396 struct regulator_config config
= { };
397 struct regulator_dev
*rdev
;
401 dev_err(&pdev
->dev
, "null mfd parent\n");
405 /* check for AB8500 2.x */
406 if (is_ab8500_2p0_or_earlier(ab8500
)) {
407 struct ab8500_ext_regulator_info
*info
;
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;
416 /* register all regulators */
417 for (i
= 0; i
< ARRAY_SIZE(ab8500_ext_regulator_info
); i
++) {
418 struct ab8500_ext_regulator_info
*info
= NULL
;
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
;
426 config
.dev
= &pdev
->dev
;
427 config
.driver_data
= info
;
428 config
.init_data
= &ab8500_ext_regulators
[i
];
430 /* register regulator with framework */
431 rdev
= devm_regulator_register(&pdev
->dev
, &info
->desc
,
434 dev_err(&pdev
->dev
, "failed to register regulator %s\n",
436 return PTR_ERR(rdev
);
439 dev_dbg(&pdev
->dev
, "%s-probed\n", info
->desc
.name
);
445 static struct platform_driver ab8500_ext_regulator_driver
= {
446 .probe
= ab8500_ext_regulator_probe
,
448 .name
= "ab8500-ext-regulator",
449 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
453 static int __init
ab8500_ext_regulator_init(void)
457 ret
= platform_driver_register(&ab8500_ext_regulator_driver
);
459 pr_err("Failed to register ab8500 ext regulator: %d\n", ret
);
463 subsys_initcall(ab8500_ext_regulator_init
);
465 static void __exit
ab8500_ext_regulator_exit(void)
467 platform_driver_unregister(&ab8500_ext_regulator_driver
);
469 module_exit(ab8500_ext_regulator_exit
);
471 MODULE_LICENSE("GPL v2");
472 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
473 MODULE_DESCRIPTION("AB8500 external regulator driver");
474 MODULE_ALIAS("platform:ab8500-ext-regulator");