Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 1 | // 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 | |
| 11 | int 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; |
| 15 | int flags = GPIOD_IS_OUT; |
| 16 | int ret; |
| 17 | |
| 18 | if (dev_read_bool(dev, "enable-active-high")) |
| 19 | flags |= GPIOD_IS_OUT_ACTIVE; |
| 20 | |
| 21 | /* Get optional enable GPIO desc */ |
| 22 | gpio = &dev_pdata->gpio; |
| 23 | ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags); |
| 24 | if (ret) { |
| 25 | debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n", |
| 26 | dev->name, ret); |
| 27 | if (ret != -ENOENT) |
| 28 | return ret; |
| 29 | } |
| 30 | |
| 31 | /* Get optional ramp up delay */ |
| 32 | dev_pdata->startup_delay_us = dev_read_u32_default(dev, |
| 33 | "startup-delay-us", 0); |
| 34 | dev_pdata->off_on_delay_us = |
| 35 | dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | int regulator_common_get_enable(const struct udevice *dev, |
| 41 | struct regulator_common_platdata *dev_pdata) |
| 42 | { |
| 43 | /* Enable GPIO is optional */ |
| 44 | if (!dev_pdata->gpio.dev) |
| 45 | return true; |
| 46 | |
| 47 | return dm_gpio_get_value(&dev_pdata->gpio); |
| 48 | } |
| 49 | |
| 50 | int regulator_common_set_enable(const struct udevice *dev, |
| 51 | struct regulator_common_platdata *dev_pdata, bool enable) |
| 52 | { |
| 53 | int ret; |
| 54 | |
| 55 | debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__, |
| 56 | dev->name, enable, dev_pdata->startup_delay_us, |
| 57 | dm_gpio_is_valid(&dev_pdata->gpio)); |
| 58 | /* Enable GPIO is optional */ |
| 59 | if (!dm_gpio_is_valid(&dev_pdata->gpio)) { |
| 60 | if (!enable) |
| 61 | return -ENOSYS; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); |
| 66 | if (ret) { |
| 67 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
| 68 | enable); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | if (enable && dev_pdata->startup_delay_us) |
| 73 | udelay(dev_pdata->startup_delay_us); |
| 74 | debug("%s: done\n", __func__); |
| 75 | |
| 76 | if (!enable && dev_pdata->off_on_delay_us) |
| 77 | udelay(dev_pdata->off_on_delay_us); |
| 78 | |
| 79 | return 0; |
| 80 | } |