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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 11 | #include <power/regulator.h> |
| 12 | |
| 13 | int regulator_common_ofdata_to_platdata(struct udevice *dev, |
| 14 | struct regulator_common_platdata *dev_pdata, const char *enable_gpio_name) |
| 15 | { |
| 16 | struct gpio_desc *gpio; |
| 17 | int flags = GPIOD_IS_OUT; |
| 18 | int ret; |
| 19 | |
Patrice Chotard | 6814ec6 | 2019-08-26 13:50:31 +0200 | [diff] [blame] | 20 | if (!dev_read_bool(dev, "enable-active-high")) |
| 21 | flags |= GPIOD_ACTIVE_LOW; |
Mark Kettenis | 91ccefc | 2020-04-27 11:09:47 +0200 | [diff] [blame] | 22 | if (dev_read_bool(dev, "regulator-boot-on")) |
| 23 | flags |= GPIOD_IS_OUT_ACTIVE; |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 24 | |
| 25 | /* Get optional enable GPIO desc */ |
| 26 | gpio = &dev_pdata->gpio; |
| 27 | ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags); |
| 28 | if (ret) { |
| 29 | debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n", |
| 30 | dev->name, ret); |
| 31 | if (ret != -ENOENT) |
| 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | /* Get optional ramp up delay */ |
| 36 | dev_pdata->startup_delay_us = dev_read_u32_default(dev, |
| 37 | "startup-delay-us", 0); |
| 38 | dev_pdata->off_on_delay_us = |
Peng Fan | e145c47 | 2019-11-04 09:27:23 +0000 | [diff] [blame] | 39 | dev_read_u32_default(dev, "off-on-delay-us", 0); |
| 40 | if (!dev_pdata->off_on_delay_us) { |
| 41 | dev_pdata->off_on_delay_us = |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 42 | dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0); |
Peng Fan | e145c47 | 2019-11-04 09:27:23 +0000 | [diff] [blame] | 43 | } |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int regulator_common_get_enable(const struct udevice *dev, |
| 49 | struct regulator_common_platdata *dev_pdata) |
| 50 | { |
| 51 | /* Enable GPIO is optional */ |
| 52 | if (!dev_pdata->gpio.dev) |
| 53 | return true; |
| 54 | |
| 55 | return dm_gpio_get_value(&dev_pdata->gpio); |
| 56 | } |
| 57 | |
| 58 | int regulator_common_set_enable(const struct udevice *dev, |
| 59 | struct regulator_common_platdata *dev_pdata, bool enable) |
| 60 | { |
| 61 | int ret; |
| 62 | |
| 63 | debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__, |
| 64 | dev->name, enable, dev_pdata->startup_delay_us, |
| 65 | dm_gpio_is_valid(&dev_pdata->gpio)); |
| 66 | /* Enable GPIO is optional */ |
| 67 | if (!dm_gpio_is_valid(&dev_pdata->gpio)) { |
| 68 | if (!enable) |
| 69 | return -ENOSYS; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); |
| 74 | if (ret) { |
| 75 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
| 76 | enable); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | if (enable && dev_pdata->startup_delay_us) |
| 81 | udelay(dev_pdata->startup_delay_us); |
| 82 | debug("%s: done\n", __func__); |
| 83 | |
| 84 | if (!enable && dev_pdata->off_on_delay_us) |
| 85 | udelay(dev_pdata->off_on_delay_us); |
| 86 | |
| 87 | return 0; |
| 88 | } |