xref: /openbmc/u-boot/drivers/power/regulator/fixed.c (revision 5994e8b6)
1 /*
2  *  Copyright (C) 2015 Samsung Electronics
3  *
4  *  Przemyslaw Marczak <p.marczak@samsung.com>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <errno.h>
11 #include <dm.h>
12 #include <i2c.h>
13 #include <asm/gpio.h>
14 #include <power/pmic.h>
15 #include <power/regulator.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 struct fixed_regulator_platdata {
20 	struct gpio_desc gpio; /* GPIO for regulator enable control */
21 	unsigned int startup_delay_us;
22 };
23 
24 static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
25 {
26 	struct dm_regulator_uclass_platdata *uc_pdata;
27 	struct fixed_regulator_platdata *dev_pdata;
28 	struct gpio_desc *gpio;
29 	int flags = GPIOD_IS_OUT;
30 	int ret;
31 
32 	dev_pdata = dev_get_platdata(dev);
33 	uc_pdata = dev_get_uclass_platdata(dev);
34 	if (!uc_pdata)
35 		return -ENXIO;
36 
37 	/* Set type to fixed */
38 	uc_pdata->type = REGULATOR_TYPE_FIXED;
39 
40 	if (dev_read_bool(dev, "enable-active-high"))
41 		flags |= GPIOD_IS_OUT_ACTIVE;
42 
43 	/* Get fixed regulator optional enable GPIO desc */
44 	gpio = &dev_pdata->gpio;
45 	ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
46 	if (ret) {
47 		debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
48 		      ret);
49 		if (ret != -ENOENT)
50 			return ret;
51 	}
52 
53 	/* Get optional ramp up delay */
54 	dev_pdata->startup_delay_us = dev_read_u32_default(dev,
55 							"startup-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 		error("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 	return 0;
130 }
131 
132 static const struct dm_regulator_ops fixed_regulator_ops = {
133 	.get_value	= fixed_regulator_get_value,
134 	.get_current	= fixed_regulator_get_current,
135 	.get_enable	= fixed_regulator_get_enable,
136 	.set_enable	= fixed_regulator_set_enable,
137 };
138 
139 static const struct udevice_id fixed_regulator_ids[] = {
140 	{ .compatible = "regulator-fixed" },
141 	{ },
142 };
143 
144 U_BOOT_DRIVER(fixed_regulator) = {
145 	.name = "fixed regulator",
146 	.id = UCLASS_REGULATOR,
147 	.ops = &fixed_regulator_ops,
148 	.of_match = fixed_regulator_ids,
149 	.ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
150 	.platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
151 };
152