2 * Copyright (C) 2015 Samsung Electronics
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <power/pmic.h>
15 #include <power/regulator.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 struct fixed_regulator_platdata {
20 struct gpio_desc gpio; /* GPIO for regulator enable control */
21 unsigned int startup_delay_us;
24 static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
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;
32 dev_pdata = dev_get_platdata(dev);
33 uc_pdata = dev_get_uclass_platdata(dev);
37 /* Set type to fixed */
38 uc_pdata->type = REGULATOR_TYPE_FIXED;
40 if (dev_read_bool(dev, "enable-active-high"))
41 flags |= GPIOD_IS_OUT_ACTIVE;
43 /* Get fixed regulator optional enable GPIO desc */
44 gpio = &dev_pdata->gpio;
45 ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
47 debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
53 /* Get optional ramp up delay */
54 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
55 "startup-delay-us", 0);
60 static int fixed_regulator_get_value(struct udevice *dev)
62 struct dm_regulator_uclass_platdata *uc_pdata;
64 uc_pdata = dev_get_uclass_platdata(dev);
68 if (uc_pdata->min_uV != uc_pdata->max_uV) {
69 debug("Invalid constraints for: %s\n", uc_pdata->name);
73 return uc_pdata->min_uV;
76 static int fixed_regulator_get_current(struct udevice *dev)
78 struct dm_regulator_uclass_platdata *uc_pdata;
80 uc_pdata = dev_get_uclass_platdata(dev);
84 if (uc_pdata->min_uA != uc_pdata->max_uA) {
85 debug("Invalid constraints for: %s\n", uc_pdata->name);
89 return uc_pdata->min_uA;
92 static int fixed_regulator_get_enable(struct udevice *dev)
94 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
96 /* Enable GPIO is optional */
97 if (!dev_pdata->gpio.dev)
100 return dm_gpio_get_value(&dev_pdata->gpio);
103 static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
105 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
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)) {
118 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
120 error("Can't set regulator : %s gpio to: %d\n", dev->name,
125 if (enable && dev_pdata->startup_delay_us)
126 udelay(dev_pdata->startup_delay_us);
127 debug("%s: done\n", __func__);
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,
139 static const struct udevice_id fixed_regulator_ids[] = {
140 { .compatible = "regulator-fixed" },
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),