1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2015 Samsung Electronics
4  *
5  *  Przemyslaw Marczak <p.marczak@samsung.com>
6  */
7 
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
14 
15 #include "regulator_common.h"
16 
fixed_regulator_of_to_plat(struct udevice * dev)17 static int fixed_regulator_of_to_plat(struct udevice *dev)
18 {
19 	struct dm_regulator_uclass_plat *uc_pdata;
20 	struct regulator_common_plat *dev_pdata;
21 
22 	dev_pdata = dev_get_plat(dev);
23 	uc_pdata = dev_get_uclass_plat(dev);
24 	if (!uc_pdata)
25 		return -ENXIO;
26 
27 	uc_pdata->type = REGULATOR_TYPE_FIXED;
28 
29 	return regulator_common_of_to_plat(dev, dev_pdata, "gpio");
30 }
31 
fixed_regulator_get_value(struct udevice * dev)32 static int fixed_regulator_get_value(struct udevice *dev)
33 {
34 	struct dm_regulator_uclass_plat *uc_pdata;
35 
36 	uc_pdata = dev_get_uclass_plat(dev);
37 	if (!uc_pdata)
38 		return -ENXIO;
39 
40 	if (uc_pdata->min_uV != uc_pdata->max_uV) {
41 		debug("Invalid constraints for: %s\n", uc_pdata->name);
42 		return -EINVAL;
43 	}
44 
45 	return uc_pdata->min_uV;
46 }
47 
fixed_regulator_get_current(struct udevice * dev)48 static int fixed_regulator_get_current(struct udevice *dev)
49 {
50 	struct dm_regulator_uclass_plat *uc_pdata;
51 
52 	uc_pdata = dev_get_uclass_plat(dev);
53 	if (!uc_pdata)
54 		return -ENXIO;
55 
56 	if (uc_pdata->min_uA != uc_pdata->max_uA) {
57 		debug("Invalid constraints for: %s\n", uc_pdata->name);
58 		return -EINVAL;
59 	}
60 
61 	return uc_pdata->min_uA;
62 }
63 
fixed_regulator_get_enable(struct udevice * dev)64 static int fixed_regulator_get_enable(struct udevice *dev)
65 {
66 	return regulator_common_get_enable(dev, dev_get_plat(dev));
67 }
68 
fixed_regulator_set_enable(struct udevice * dev,bool enable)69 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
70 {
71 	return regulator_common_set_enable(dev, dev_get_plat(dev), enable);
72 }
73 
74 static const struct dm_regulator_ops fixed_regulator_ops = {
75 	.get_value	= fixed_regulator_get_value,
76 	.get_current	= fixed_regulator_get_current,
77 	.get_enable	= fixed_regulator_get_enable,
78 	.set_enable	= fixed_regulator_set_enable,
79 };
80 
81 static const struct udevice_id fixed_regulator_ids[] = {
82 	{ .compatible = "regulator-fixed" },
83 	{ },
84 };
85 
86 U_BOOT_DRIVER(regulator_fixed) = {
87 	.name = "regulator_fixed",
88 	.id = UCLASS_REGULATOR,
89 	.ops = &fixed_regulator_ops,
90 	.of_match = fixed_regulator_ids,
91 	.of_to_plat = fixed_regulator_of_to_plat,
92 	.plat_auto	= sizeof(struct regulator_common_plat),
93 };
94