blob: 637db02434884a8654f33fe5f9feff736ab4b1f6 [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>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Sven Schwermer25957522019-06-24 13:03:33 +020010#include <power/regulator.h>
11
12int regulator_common_ofdata_to_platdata(struct udevice *dev,
13 struct regulator_common_platdata *dev_pdata, const char *enable_gpio_name)
14{
15 struct gpio_desc *gpio;
16 int flags = GPIOD_IS_OUT;
17 int ret;
18
Patrice Chotard6814ec62019-08-26 13:50:31 +020019 if (!dev_read_bool(dev, "enable-active-high"))
20 flags |= GPIOD_ACTIVE_LOW;
Sven Schwermer25957522019-06-24 13:03:33 +020021
22 /* Get optional enable GPIO desc */
23 gpio = &dev_pdata->gpio;
24 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
25 if (ret) {
26 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
27 dev->name, ret);
28 if (ret != -ENOENT)
29 return ret;
30 }
31
32 /* Get optional ramp up delay */
33 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
34 "startup-delay-us", 0);
35 dev_pdata->off_on_delay_us =
Peng Fane145c472019-11-04 09:27:23 +000036 dev_read_u32_default(dev, "off-on-delay-us", 0);
37 if (!dev_pdata->off_on_delay_us) {
38 dev_pdata->off_on_delay_us =
Sven Schwermer25957522019-06-24 13:03:33 +020039 dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
Peng Fane145c472019-11-04 09:27:23 +000040 }
Sven Schwermer25957522019-06-24 13:03:33 +020041
42 return 0;
43}
44
45int regulator_common_get_enable(const struct udevice *dev,
46 struct regulator_common_platdata *dev_pdata)
47{
48 /* Enable GPIO is optional */
49 if (!dev_pdata->gpio.dev)
50 return true;
51
52 return dm_gpio_get_value(&dev_pdata->gpio);
53}
54
55int regulator_common_set_enable(const struct udevice *dev,
56 struct regulator_common_platdata *dev_pdata, bool enable)
57{
58 int ret;
59
60 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
61 dev->name, enable, dev_pdata->startup_delay_us,
62 dm_gpio_is_valid(&dev_pdata->gpio));
63 /* Enable GPIO is optional */
64 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
65 if (!enable)
66 return -ENOSYS;
67 return 0;
68 }
69
70 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
71 if (ret) {
72 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
73 enable);
74 return ret;
75 }
76
77 if (enable && dev_pdata->startup_delay_us)
78 udelay(dev_pdata->startup_delay_us);
79 debug("%s: done\n", __func__);
80
81 if (!enable && dev_pdata->off_on_delay_us)
82 udelay(dev_pdata->off_on_delay_us);
83
84 return 0;
85}