1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Samsung Electronics
5 * Przemyslaw Marczak <p.marczak@samsung.com>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
16 struct fixed_regulator_platdata {
17 struct gpio_desc gpio; /* GPIO for regulator enable control */
18 unsigned int startup_delay_us;
21 static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
23 struct dm_regulator_uclass_platdata *uc_pdata;
24 struct fixed_regulator_platdata *dev_pdata;
25 struct gpio_desc *gpio;
26 int flags = GPIOD_IS_OUT;
29 dev_pdata = dev_get_platdata(dev);
30 uc_pdata = dev_get_uclass_platdata(dev);
34 /* Set type to fixed */
35 uc_pdata->type = REGULATOR_TYPE_FIXED;
37 if (dev_read_bool(dev, "enable-active-high"))
38 flags |= GPIOD_IS_OUT_ACTIVE;
40 /* Get fixed regulator optional enable GPIO desc */
41 gpio = &dev_pdata->gpio;
42 ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
44 debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
50 /* Get optional ramp up delay */
51 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
52 "startup-delay-us", 0);
57 static int fixed_regulator_get_value(struct udevice *dev)
59 struct dm_regulator_uclass_platdata *uc_pdata;
61 uc_pdata = dev_get_uclass_platdata(dev);
65 if (uc_pdata->min_uV != uc_pdata->max_uV) {
66 debug("Invalid constraints for: %s\n", uc_pdata->name);
70 return uc_pdata->min_uV;
73 static int fixed_regulator_get_current(struct udevice *dev)
75 struct dm_regulator_uclass_platdata *uc_pdata;
77 uc_pdata = dev_get_uclass_platdata(dev);
81 if (uc_pdata->min_uA != uc_pdata->max_uA) {
82 debug("Invalid constraints for: %s\n", uc_pdata->name);
86 return uc_pdata->min_uA;
89 static int fixed_regulator_get_enable(struct udevice *dev)
91 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
93 /* Enable GPIO is optional */
94 if (!dev_pdata->gpio.dev)
97 return dm_gpio_get_value(&dev_pdata->gpio);
100 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
102 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
105 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
106 dev->name, enable, dev_pdata->startup_delay_us,
107 dm_gpio_is_valid(&dev_pdata->gpio));
108 /* Enable GPIO is optional */
109 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
115 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
117 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
122 if (enable && dev_pdata->startup_delay_us)
123 udelay(dev_pdata->startup_delay_us);
124 debug("%s: done\n", __func__);
129 static const struct dm_regulator_ops fixed_regulator_ops = {
130 .get_value = fixed_regulator_get_value,
131 .get_current = fixed_regulator_get_current,
132 .get_enable = fixed_regulator_get_enable,
133 .set_enable = fixed_regulator_set_enable,
136 static const struct udevice_id fixed_regulator_ids[] = {
137 { .compatible = "regulator-fixed" },
141 U_BOOT_DRIVER(fixed_regulator) = {
142 .name = "fixed regulator",
143 .id = UCLASS_REGULATOR,
144 .ops = &fixed_regulator_ops,
145 .of_match = fixed_regulator_ids,
146 .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
147 .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),