blob: 939efb2c0d0d9ace36e74b95e59da8479a71c183 [file] [log] [blame]
Sven Schwermer25957522019-06-24 13:03:33 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Disruptive Technologies Research AS
4 * Sven Schwermer <sven.svenschwermer@disruptive-technologies.com>
5 */
6
7#include "regulator_common.h"
8#include <common.h>
9#include <power/regulator.h>
10
11int regulator_common_ofdata_to_platdata(struct udevice *dev,
12 struct regulator_common_platdata *dev_pdata, const char *enable_gpio_name)
13{
14 struct gpio_desc *gpio;
Patrice Chotard6814ec62019-08-26 13:50:31 +020015 struct dm_regulator_uclass_platdata *uc_pdata;
Sven Schwermer25957522019-06-24 13:03:33 +020016 int flags = GPIOD_IS_OUT;
17 int ret;
18
Patrice Chotard6814ec62019-08-26 13:50:31 +020019 uc_pdata = dev_get_uclass_platdata(dev);
20
21 if (!dev_read_bool(dev, "enable-active-high"))
22 flags |= GPIOD_ACTIVE_LOW;
23 if (uc_pdata->boot_on)
Sven Schwermer25957522019-06-24 13:03:33 +020024 flags |= GPIOD_IS_OUT_ACTIVE;
25
26 /* Get optional enable GPIO desc */
27 gpio = &dev_pdata->gpio;
28 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
29 if (ret) {
30 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
31 dev->name, ret);
32 if (ret != -ENOENT)
33 return ret;
34 }
35
36 /* Get optional ramp up delay */
37 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
38 "startup-delay-us", 0);
39 dev_pdata->off_on_delay_us =
Peng Fane145c472019-11-04 09:27:23 +000040 dev_read_u32_default(dev, "off-on-delay-us", 0);
41 if (!dev_pdata->off_on_delay_us) {
42 dev_pdata->off_on_delay_us =
Sven Schwermer25957522019-06-24 13:03:33 +020043 dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
Peng Fane145c472019-11-04 09:27:23 +000044 }
Sven Schwermer25957522019-06-24 13:03:33 +020045
46 return 0;
47}
48
49int regulator_common_get_enable(const struct udevice *dev,
50 struct regulator_common_platdata *dev_pdata)
51{
52 /* Enable GPIO is optional */
53 if (!dev_pdata->gpio.dev)
54 return true;
55
56 return dm_gpio_get_value(&dev_pdata->gpio);
57}
58
59int regulator_common_set_enable(const struct udevice *dev,
60 struct regulator_common_platdata *dev_pdata, bool enable)
61{
62 int ret;
63
64 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
65 dev->name, enable, dev_pdata->startup_delay_us,
66 dm_gpio_is_valid(&dev_pdata->gpio));
67 /* Enable GPIO is optional */
68 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
69 if (!enable)
70 return -ENOSYS;
71 return 0;
72 }
73
74 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
75 if (ret) {
76 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
77 enable);
78 return ret;
79 }
80
81 if (enable && dev_pdata->startup_delay_us)
82 udelay(dev_pdata->startup_delay_us);
83 debug("%s: done\n", __func__);
84
85 if (!enable && dev_pdata->off_on_delay_us)
86 udelay(dev_pdata->off_on_delay_us);
87
88 return 0;
89}