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