xref: /openbmc/u-boot/drivers/power/regulator/fixed.c (revision 08592136)
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 <fdtdec.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <i2c.h>
14 #include <asm/gpio.h>
15 #include <power/pmic.h>
16 #include <power/regulator.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 struct fixed_regulator_platdata {
21 	struct gpio_desc gpio; /* GPIO for regulator enable control */
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 ret;
30 
31 	dev_pdata = dev_get_platdata(dev);
32 	uc_pdata = dev_get_uclass_platdata(dev);
33 	if (!uc_pdata)
34 		return -ENXIO;
35 
36 	/* Set type to fixed */
37 	uc_pdata->type = REGULATOR_TYPE_FIXED;
38 
39 	/* Get fixed regulator gpio desc */
40 	gpio = &dev_pdata->gpio;
41 	ret = gpio_request_by_name(dev, "gpio", 0, gpio, GPIOD_IS_OUT);
42 	if (ret)
43 		debug("Fixed regulator gpio - not found! Error: %d", ret);
44 
45 	return 0;
46 }
47 
48 static int fixed_regulator_get_value(struct udevice *dev)
49 {
50 	struct dm_regulator_uclass_platdata *uc_pdata;
51 
52 	uc_pdata = dev_get_uclass_platdata(dev);
53 	if (!uc_pdata)
54 		return -ENXIO;
55 
56 	if (uc_pdata->min_uV != uc_pdata->max_uV) {
57 		debug("Invalid constraints for: %s\n", uc_pdata->name);
58 		return -EINVAL;
59 	}
60 
61 	return uc_pdata->min_uV;
62 }
63 
64 static int fixed_regulator_get_current(struct udevice *dev)
65 {
66 	struct dm_regulator_uclass_platdata *uc_pdata;
67 
68 	uc_pdata = dev_get_uclass_platdata(dev);
69 	if (!uc_pdata)
70 		return -ENXIO;
71 
72 	if (uc_pdata->min_uA != uc_pdata->max_uA) {
73 		debug("Invalid constraints for: %s\n", uc_pdata->name);
74 		return -EINVAL;
75 	}
76 
77 	return uc_pdata->min_uA;
78 }
79 
80 static bool fixed_regulator_get_enable(struct udevice *dev)
81 {
82 	struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
83 
84 	if (!dev_pdata->gpio.dev)
85 		return false;
86 
87 	return dm_gpio_get_value(&dev_pdata->gpio);
88 }
89 
90 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
91 {
92 	struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
93 	int ret;
94 
95 	if (!dev_pdata->gpio.dev)
96 		return -ENOSYS;
97 
98 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
99 	if (ret) {
100 		error("Can't set regulator : %s gpio to: %d\n", dev->name,
101 		      enable);
102 		return ret;
103 	}
104 	return 0;
105 }
106 
107 static const struct dm_regulator_ops fixed_regulator_ops = {
108 	.get_value	= fixed_regulator_get_value,
109 	.get_current	= fixed_regulator_get_current,
110 	.get_enable	= fixed_regulator_get_enable,
111 	.set_enable	= fixed_regulator_set_enable,
112 };
113 
114 static const struct udevice_id fixed_regulator_ids[] = {
115 	{ .compatible = "regulator-fixed" },
116 	{ },
117 };
118 
119 U_BOOT_DRIVER(fixed_regulator) = {
120 	.name = "fixed regulator",
121 	.id = UCLASS_REGULATOR,
122 	.ops = &fixed_regulator_ops,
123 	.of_match = fixed_regulator_ids,
124 	.ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
125 	.platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
126 };
127