Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Samsung Electronics |
| 4 | * |
| 5 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 9 | #include <clk.h> |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <dm.h> |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 12 | #include <linux/delay.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 15 | #include <power/pmic.h> |
| 16 | #include <power/regulator.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 17 | #include "regulator_common.h" |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 18 | |
Simon Glass | ad2f4ac | 2020-07-19 10:15:44 -0600 | [diff] [blame] | 19 | #include "regulator_common.h" |
| 20 | |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 21 | struct fixed_clock_regulator_plat { |
| 22 | struct clk *enable_clock; |
| 23 | unsigned int clk_enable_counter; |
| 24 | }; |
| 25 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 26 | static int fixed_regulator_of_to_plat(struct udevice *dev) |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 27 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 28 | struct dm_regulator_uclass_plat *uc_pdata; |
Eugen Hristev | 29fca9f | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 29 | struct regulator_common_plat *plat; |
Jonas Karlman | f7b8a84 | 2023-07-22 13:30:21 +0000 | [diff] [blame] | 30 | bool gpios; |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 31 | |
Eugen Hristev | 29fca9f | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 32 | plat = dev_get_plat(dev); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 33 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 34 | if (!uc_pdata) |
| 35 | return -ENXIO; |
| 36 | |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 37 | uc_pdata->type = REGULATOR_TYPE_FIXED; |
| 38 | |
Jonas Karlman | f7b8a84 | 2023-07-22 13:30:21 +0000 | [diff] [blame] | 39 | gpios = dev_read_bool(dev, "gpios"); |
| 40 | return regulator_common_of_to_plat(dev, plat, gpios ? "gpios" : "gpio"); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int fixed_regulator_get_value(struct udevice *dev) |
| 44 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 45 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 46 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 47 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 48 | if (!uc_pdata) |
| 49 | return -ENXIO; |
| 50 | |
| 51 | if (uc_pdata->min_uV != uc_pdata->max_uV) { |
| 52 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | |
| 56 | return uc_pdata->min_uV; |
| 57 | } |
| 58 | |
| 59 | static int fixed_regulator_get_current(struct udevice *dev) |
| 60 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 61 | struct dm_regulator_uclass_plat *uc_pdata; |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 62 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 63 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 64 | if (!uc_pdata) |
| 65 | return -ENXIO; |
| 66 | |
| 67 | if (uc_pdata->min_uA != uc_pdata->max_uA) { |
| 68 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | return uc_pdata->min_uA; |
| 73 | } |
| 74 | |
Keerthy | 0f2194d | 2017-06-13 09:53:46 +0530 | [diff] [blame] | 75 | static int fixed_regulator_get_enable(struct udevice *dev) |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 76 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 77 | return regulator_common_get_enable(dev, dev_get_plat(dev)); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int fixed_regulator_set_enable(struct udevice *dev, bool enable) |
| 81 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 82 | return regulator_common_set_enable(dev, dev_get_plat(dev), enable); |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 85 | static int fixed_clock_regulator_get_enable(struct udevice *dev) |
| 86 | { |
| 87 | struct fixed_clock_regulator_plat *priv = dev_get_priv(dev); |
| 88 | |
| 89 | return priv->clk_enable_counter > 0; |
| 90 | } |
| 91 | |
| 92 | static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable) |
| 93 | { |
| 94 | struct fixed_clock_regulator_plat *priv = dev_get_priv(dev); |
Eugen Hristev | 29fca9f | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 95 | struct regulator_common_plat *plat = dev_get_plat(dev); |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 96 | int ret = 0; |
| 97 | |
| 98 | if (enable) { |
| 99 | ret = clk_enable(priv->enable_clock); |
| 100 | priv->clk_enable_counter++; |
| 101 | } else { |
| 102 | ret = clk_disable(priv->enable_clock); |
| 103 | priv->clk_enable_counter--; |
| 104 | } |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
Eugen Hristev | 29fca9f | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 108 | if (enable && plat->startup_delay_us) |
| 109 | udelay(plat->startup_delay_us); |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 110 | |
Eugen Hristev | 29fca9f | 2023-04-19 16:45:25 +0300 | [diff] [blame] | 111 | if (!enable && plat->off_on_delay_us) |
| 112 | udelay(plat->off_on_delay_us); |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 117 | static const struct dm_regulator_ops fixed_regulator_ops = { |
| 118 | .get_value = fixed_regulator_get_value, |
| 119 | .get_current = fixed_regulator_get_current, |
| 120 | .get_enable = fixed_regulator_get_enable, |
| 121 | .set_enable = fixed_regulator_set_enable, |
| 122 | }; |
| 123 | |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 124 | static const struct dm_regulator_ops fixed_clock_regulator_ops = { |
| 125 | .get_enable = fixed_clock_regulator_get_enable, |
| 126 | .set_enable = fixed_clock_regulator_set_enable, |
| 127 | }; |
| 128 | |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 129 | static const struct udevice_id fixed_regulator_ids[] = { |
| 130 | { .compatible = "regulator-fixed" }, |
| 131 | { }, |
| 132 | }; |
| 133 | |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 134 | static const struct udevice_id fixed_clock_regulator_ids[] = { |
| 135 | { .compatible = "regulator-fixed-clock" }, |
| 136 | { }, |
| 137 | }; |
| 138 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 139 | U_BOOT_DRIVER(regulator_fixed) = { |
| 140 | .name = "regulator_fixed", |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 141 | .id = UCLASS_REGULATOR, |
| 142 | .ops = &fixed_regulator_ops, |
| 143 | .of_match = fixed_regulator_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 144 | .of_to_plat = fixed_regulator_of_to_plat, |
Philippe Schenker | f3b5100 | 2022-04-08 10:07:10 +0200 | [diff] [blame] | 145 | .plat_auto = sizeof(struct regulator_common_plat), |
| 146 | }; |
| 147 | |
| 148 | U_BOOT_DRIVER(regulator_fixed_clock) = { |
| 149 | .name = "regulator_fixed_clk", |
| 150 | .id = UCLASS_REGULATOR, |
| 151 | .ops = &fixed_clock_regulator_ops, |
| 152 | .of_match = fixed_clock_regulator_ids, |
| 153 | .of_to_plat = fixed_regulator_of_to_plat, |
| 154 | .plat_auto = sizeof(struct fixed_clock_regulator_plat), |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 155 | }; |