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 | |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 1c1ddf6 | 2020-07-19 10:15:44 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame^] | 10 | #include <asm/gpio.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 11 | #include <linux/delay.h> |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 12 | #include <power/regulator.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame^] | 13 | #include "regulator_common.h" |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 14 | |
Simon Glass | 1c1ddf6 | 2020-07-19 10:15:44 -0600 | [diff] [blame] | 15 | #include "regulator_common.h" |
| 16 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 17 | int regulator_common_of_to_plat(struct udevice *dev, |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 18 | struct regulator_common_plat *plat, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 19 | const char *enable_gpio_name) |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 20 | { |
| 21 | struct gpio_desc *gpio; |
| 22 | int flags = GPIOD_IS_OUT; |
| 23 | int ret; |
| 24 | |
Patrice Chotard | 6814ec6 | 2019-08-26 13:50:31 +0200 | [diff] [blame] | 25 | if (!dev_read_bool(dev, "enable-active-high")) |
| 26 | flags |= GPIOD_ACTIVE_LOW; |
Mark Kettenis | 91ccefc | 2020-04-27 11:09:47 +0200 | [diff] [blame] | 27 | if (dev_read_bool(dev, "regulator-boot-on")) |
| 28 | flags |= GPIOD_IS_OUT_ACTIVE; |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 29 | |
| 30 | /* Get optional enable GPIO desc */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 31 | gpio = &plat->gpio; |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 32 | ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags); |
| 33 | if (ret) { |
| 34 | debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n", |
| 35 | dev->name, ret); |
| 36 | if (ret != -ENOENT) |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | /* Get optional ramp up delay */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 41 | plat->startup_delay_us = dev_read_u32_default(dev, |
| 42 | "startup-delay-us", 0); |
| 43 | plat->off_on_delay_us = dev_read_u32_default(dev, "off-on-delay-us", 0); |
| 44 | if (!plat->off_on_delay_us) { |
| 45 | plat->off_on_delay_us = |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 46 | dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0); |
Peng Fan | e145c47 | 2019-11-04 09:27:23 +0000 | [diff] [blame] | 47 | } |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int regulator_common_get_enable(const struct udevice *dev, |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 53 | struct regulator_common_plat *plat) |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 54 | { |
| 55 | /* Enable GPIO is optional */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 56 | if (!plat->gpio.dev) |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 57 | return true; |
| 58 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 59 | return dm_gpio_get_value(&plat->gpio); |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int regulator_common_set_enable(const struct udevice *dev, |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 63 | struct regulator_common_plat *plat, bool enable) |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 64 | { |
| 65 | int ret; |
| 66 | |
| 67 | debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__, |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 68 | dev->name, enable, plat->startup_delay_us, |
| 69 | dm_gpio_is_valid(&plat->gpio)); |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 70 | /* Enable GPIO is optional */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 71 | if (!dm_gpio_is_valid(&plat->gpio)) { |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 72 | if (!enable) |
| 73 | return -ENOSYS; |
| 74 | return 0; |
| 75 | } |
| 76 | |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 77 | /* If previously enabled, increase count */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 78 | if (enable && plat->enable_count > 0) { |
| 79 | plat->enable_count++; |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 80 | return -EALREADY; |
| 81 | } |
| 82 | |
| 83 | if (!enable) { |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 84 | if (plat->enable_count > 1) { |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 85 | /* If enabled multiple times, decrease count */ |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 86 | plat->enable_count--; |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 87 | return -EBUSY; |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 88 | } else if (!plat->enable_count) { |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 89 | /* If already disabled, do nothing */ |
| 90 | return -EALREADY; |
| 91 | } |
| 92 | } |
| 93 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 94 | ret = dm_gpio_set_value(&plat->gpio, enable); |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 95 | if (ret) { |
| 96 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
| 97 | enable); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 101 | if (enable && plat->startup_delay_us) |
| 102 | udelay(plat->startup_delay_us); |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 103 | debug("%s: done\n", __func__); |
| 104 | |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 105 | if (!enable && plat->off_on_delay_us) |
| 106 | udelay(plat->off_on_delay_us); |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 107 | |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 108 | if (enable) |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 109 | plat->enable_count++; |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 110 | else |
Eugen Hristev | 81aa192 | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 111 | plat->enable_count--; |
Eugen Hristev | 405512e | 2023-04-19 16:45:24 +0300 | [diff] [blame] | 112 | |
Sven Schwermer | 2595752 | 2019-06-24 13:03:33 +0200 | [diff] [blame] | 113 | return 0; |
| 114 | } |