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 <clk.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <linux/delay.h>
12 #include <log.h>
13 #include <asm/gpio.h>
14 #include <power/pmic.h>
15 #include <power/regulator.h>
16 #include "regulator_common.h"
17 
18 #include "regulator_common.h"
19 
20 struct fixed_clock_regulator_priv {
21 	struct clk *enable_clock;
22 	unsigned int clk_enable_counter;
23 };
24 
fixed_regulator_of_to_plat(struct udevice * dev)25 static int fixed_regulator_of_to_plat(struct udevice *dev)
26 {
27 	struct dm_regulator_uclass_plat *uc_pdata;
28 	struct regulator_common_plat *plat;
29 	bool gpios;
30 
31 	plat = dev_get_plat(dev);
32 	uc_pdata = dev_get_uclass_plat(dev);
33 	if (!uc_pdata)
34 		return -ENXIO;
35 
36 	uc_pdata->type = REGULATOR_TYPE_FIXED;
37 
38 	gpios = dev_read_bool(dev, "gpios");
39 	return regulator_common_of_to_plat(dev, plat, gpios ? "gpios" : "gpio");
40 }
41 
fixed_regulator_get_value(struct udevice * dev)42 static int fixed_regulator_get_value(struct udevice *dev)
43 {
44 	struct dm_regulator_uclass_plat *uc_pdata;
45 
46 	uc_pdata = dev_get_uclass_plat(dev);
47 	if (!uc_pdata)
48 		return -ENXIO;
49 
50 	if (uc_pdata->min_uV != uc_pdata->max_uV) {
51 		debug("Invalid constraints for: %s\n", uc_pdata->name);
52 		return -EINVAL;
53 	}
54 
55 	return uc_pdata->min_uV;
56 }
57 
fixed_regulator_get_current(struct udevice * dev)58 static int fixed_regulator_get_current(struct udevice *dev)
59 {
60 	struct dm_regulator_uclass_plat *uc_pdata;
61 
62 	uc_pdata = dev_get_uclass_plat(dev);
63 	if (!uc_pdata)
64 		return -ENXIO;
65 
66 	if (uc_pdata->min_uA != uc_pdata->max_uA) {
67 		debug("Invalid constraints for: %s\n", uc_pdata->name);
68 		return -EINVAL;
69 	}
70 
71 	return uc_pdata->min_uA;
72 }
73 
fixed_regulator_get_enable(struct udevice * dev)74 static int fixed_regulator_get_enable(struct udevice *dev)
75 {
76 	return regulator_common_get_enable(dev, dev_get_plat(dev));
77 }
78 
fixed_regulator_set_enable(struct udevice * dev,bool enable)79 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
80 {
81 	return regulator_common_set_enable(dev, dev_get_plat(dev), enable);
82 }
83 
fixed_clock_regulator_get_enable(struct udevice * dev)84 static int fixed_clock_regulator_get_enable(struct udevice *dev)
85 {
86 	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);
87 
88 	return priv->clk_enable_counter > 0;
89 }
90 
fixed_clock_regulator_set_enable(struct udevice * dev,bool enable)91 static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
92 {
93 	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);
94 	struct regulator_common_plat *plat = dev_get_plat(dev);
95 	int ret = 0;
96 
97 	if (enable) {
98 		ret = clk_enable(priv->enable_clock);
99 		priv->clk_enable_counter++;
100 	} else {
101 		ret = clk_disable(priv->enable_clock);
102 		priv->clk_enable_counter--;
103 	}
104 	if (ret)
105 		return ret;
106 
107 	if (enable && plat->startup_delay_us)
108 		udelay(plat->startup_delay_us);
109 
110 	if (!enable && plat->off_on_delay_us)
111 		udelay(plat->off_on_delay_us);
112 
113 	return ret;
114 }
115 
fixed_clock_regulator_probe(struct udevice * dev)116 static int fixed_clock_regulator_probe(struct udevice *dev)
117 {
118 	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);
119 
120 	priv->enable_clock = devm_clk_get(dev, NULL);
121 	if (IS_ERR(priv->enable_clock))
122 		return PTR_ERR(priv->enable_clock);
123 
124 	return 0;
125 }
126 
127 static const struct dm_regulator_ops fixed_regulator_ops = {
128 	.get_value	= fixed_regulator_get_value,
129 	.get_current	= fixed_regulator_get_current,
130 	.get_enable	= fixed_regulator_get_enable,
131 	.set_enable	= fixed_regulator_set_enable,
132 };
133 
134 static const struct dm_regulator_ops fixed_clock_regulator_ops = {
135 	.get_enable	= fixed_clock_regulator_get_enable,
136 	.set_enable	= fixed_clock_regulator_set_enable,
137 };
138 
139 static const struct udevice_id fixed_regulator_ids[] = {
140 	{ .compatible = "regulator-fixed" },
141 	{ },
142 };
143 
144 static const struct udevice_id fixed_clock_regulator_ids[] = {
145 	{ .compatible = "regulator-fixed-clock" },
146 	{ },
147 };
148 
149 U_BOOT_DRIVER(regulator_fixed) = {
150 	.name = "regulator_fixed",
151 	.id = UCLASS_REGULATOR,
152 	.ops = &fixed_regulator_ops,
153 	.of_match = fixed_regulator_ids,
154 	.of_to_plat = fixed_regulator_of_to_plat,
155 	.plat_auto = sizeof(struct regulator_common_plat),
156 };
157 
158 U_BOOT_DRIVER(regulator_fixed_clock) = {
159 	.name = "regulator_fixed_clk",
160 	.id = UCLASS_REGULATOR,
161 	.ops = &fixed_clock_regulator_ops,
162 	.of_match = fixed_clock_regulator_ids,
163 	.probe = fixed_clock_regulator_probe,
164 	.of_to_plat = fixed_regulator_of_to_plat,
165 	.plat_auto = sizeof(struct regulator_common_plat),
166 	.priv_auto = sizeof(struct fixed_clock_regulator_priv),
167 };
168