xref: /openbmc/u-boot/drivers/power/regulator/fixed.c (revision 5c8fd32b)
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 <i2c.h>
12 #include <asm/gpio.h>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
15 
16 struct fixed_regulator_platdata {
17 	struct gpio_desc gpio; /* GPIO for regulator enable control */
18 	unsigned int startup_delay_us;
19 	unsigned int off_on_delay_us;
20 };
21 
22 static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
23 {
24 	struct dm_regulator_uclass_platdata *uc_pdata;
25 	struct fixed_regulator_platdata *dev_pdata;
26 	struct gpio_desc *gpio;
27 	int flags = GPIOD_IS_OUT;
28 	int ret;
29 
30 	dev_pdata = dev_get_platdata(dev);
31 	uc_pdata = dev_get_uclass_platdata(dev);
32 	if (!uc_pdata)
33 		return -ENXIO;
34 
35 	/* Set type to fixed */
36 	uc_pdata->type = REGULATOR_TYPE_FIXED;
37 
38 	if (dev_read_bool(dev, "enable-active-high"))
39 		flags |= GPIOD_IS_OUT_ACTIVE;
40 
41 	/* Get fixed regulator optional enable GPIO desc */
42 	gpio = &dev_pdata->gpio;
43 	ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
44 	if (ret) {
45 		debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
46 		      ret);
47 		if (ret != -ENOENT)
48 			return ret;
49 	}
50 
51 	/* Get optional ramp up delay */
52 	dev_pdata->startup_delay_us = dev_read_u32_default(dev,
53 							"startup-delay-us", 0);
54 	dev_pdata->off_on_delay_us =
55 			dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
56 
57 	return 0;
58 }
59 
60 static int fixed_regulator_get_value(struct udevice *dev)
61 {
62 	struct dm_regulator_uclass_platdata *uc_pdata;
63 
64 	uc_pdata = dev_get_uclass_platdata(dev);
65 	if (!uc_pdata)
66 		return -ENXIO;
67 
68 	if (uc_pdata->min_uV != uc_pdata->max_uV) {
69 		debug("Invalid constraints for: %s\n", uc_pdata->name);
70 		return -EINVAL;
71 	}
72 
73 	return uc_pdata->min_uV;
74 }
75 
76 static int fixed_regulator_get_current(struct udevice *dev)
77 {
78 	struct dm_regulator_uclass_platdata *uc_pdata;
79 
80 	uc_pdata = dev_get_uclass_platdata(dev);
81 	if (!uc_pdata)
82 		return -ENXIO;
83 
84 	if (uc_pdata->min_uA != uc_pdata->max_uA) {
85 		debug("Invalid constraints for: %s\n", uc_pdata->name);
86 		return -EINVAL;
87 	}
88 
89 	return uc_pdata->min_uA;
90 }
91 
92 static int fixed_regulator_get_enable(struct udevice *dev)
93 {
94 	struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
95 
96 	/* Enable GPIO is optional */
97 	if (!dev_pdata->gpio.dev)
98 		return true;
99 
100 	return dm_gpio_get_value(&dev_pdata->gpio);
101 }
102 
103 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
104 {
105 	struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
106 	int ret;
107 
108 	debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
109 	      dev->name, enable, dev_pdata->startup_delay_us,
110 	      dm_gpio_is_valid(&dev_pdata->gpio));
111 	/* Enable GPIO is optional */
112 	if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
113 		if (!enable)
114 			return -ENOSYS;
115 		return 0;
116 	}
117 
118 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
119 	if (ret) {
120 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
121 		      enable);
122 		return ret;
123 	}
124 
125 	if (enable && dev_pdata->startup_delay_us)
126 		udelay(dev_pdata->startup_delay_us);
127 	debug("%s: done\n", __func__);
128 
129 	if (!enable && dev_pdata->off_on_delay_us)
130 		udelay(dev_pdata->off_on_delay_us);
131 
132 	return 0;
133 }
134 
135 static const struct dm_regulator_ops fixed_regulator_ops = {
136 	.get_value	= fixed_regulator_get_value,
137 	.get_current	= fixed_regulator_get_current,
138 	.get_enable	= fixed_regulator_get_enable,
139 	.set_enable	= fixed_regulator_set_enable,
140 };
141 
142 static const struct udevice_id fixed_regulator_ids[] = {
143 	{ .compatible = "regulator-fixed" },
144 	{ },
145 };
146 
147 U_BOOT_DRIVER(fixed_regulator) = {
148 	.name = "fixed regulator",
149 	.id = UCLASS_REGULATOR,
150 	.ops = &fixed_regulator_ops,
151 	.of_match = fixed_regulator_ids,
152 	.ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
153 	.platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
154 };
155