Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Samsung Electronics |
| 3 | * |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <dm.h> |
| 12 | #include <i2c.h> |
| 13 | #include <asm/gpio.h> |
| 14 | #include <power/pmic.h> |
| 15 | #include <power/regulator.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct fixed_regulator_platdata { |
| 20 | struct gpio_desc gpio; /* GPIO for regulator enable control */ |
John Keeping | a531aa5 | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 21 | unsigned int startup_delay_us; |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static int fixed_regulator_ofdata_to_platdata(struct udevice *dev) |
| 25 | { |
| 26 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 27 | struct fixed_regulator_platdata *dev_pdata; |
| 28 | struct gpio_desc *gpio; |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 29 | int flags = GPIOD_IS_OUT; |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 30 | int ret; |
| 31 | |
| 32 | dev_pdata = dev_get_platdata(dev); |
| 33 | uc_pdata = dev_get_uclass_platdata(dev); |
| 34 | if (!uc_pdata) |
| 35 | return -ENXIO; |
| 36 | |
| 37 | /* Set type to fixed */ |
| 38 | uc_pdata->type = REGULATOR_TYPE_FIXED; |
| 39 | |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 40 | if (dev_read_bool(dev, "enable-active-high")) |
Vignesh R | dd242ab | 2016-12-07 16:55:06 +0530 | [diff] [blame] | 41 | flags |= GPIOD_IS_OUT_ACTIVE; |
| 42 | |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 43 | /* Get fixed regulator optional enable GPIO desc */ |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 44 | gpio = &dev_pdata->gpio; |
Vignesh R | dd242ab | 2016-12-07 16:55:06 +0530 | [diff] [blame] | 45 | ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags); |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 46 | if (ret) { |
| 47 | debug("Fixed regulator optional enable GPIO - not found! Error: %d\n", |
| 48 | ret); |
| 49 | if (ret != -ENOENT) |
| 50 | return ret; |
| 51 | } |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 52 | |
John Keeping | a531aa5 | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 53 | /* Get optional ramp up delay */ |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 54 | dev_pdata->startup_delay_us = dev_read_u32_default(dev, |
| 55 | "startup-delay-us", 0); |
John Keeping | a531aa5 | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 56 | |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int fixed_regulator_get_value(struct udevice *dev) |
| 61 | { |
| 62 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 63 | |
| 64 | uc_pdata = dev_get_uclass_platdata(dev); |
| 65 | if (!uc_pdata) |
| 66 | return -ENXIO; |
| 67 | |
| 68 | if (uc_pdata->min_uV != uc_pdata->max_uV) { |
| 69 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | return uc_pdata->min_uV; |
| 74 | } |
| 75 | |
| 76 | static int fixed_regulator_get_current(struct udevice *dev) |
| 77 | { |
| 78 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 79 | |
| 80 | uc_pdata = dev_get_uclass_platdata(dev); |
| 81 | if (!uc_pdata) |
| 82 | return -ENXIO; |
| 83 | |
| 84 | if (uc_pdata->min_uA != uc_pdata->max_uA) { |
| 85 | debug("Invalid constraints for: %s\n", uc_pdata->name); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | return uc_pdata->min_uA; |
| 90 | } |
| 91 | |
Keerthy | 8690d6a | 2017-06-13 09:53:46 +0530 | [diff] [blame] | 92 | static int fixed_regulator_get_enable(struct udevice *dev) |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 93 | { |
| 94 | struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 95 | |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 96 | /* Enable GPIO is optional */ |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 97 | if (!dev_pdata->gpio.dev) |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 98 | return true; |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 99 | |
| 100 | return dm_gpio_get_value(&dev_pdata->gpio); |
| 101 | } |
| 102 | |
| 103 | static int fixed_regulator_set_enable(struct udevice *dev, bool enable) |
| 104 | { |
| 105 | struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 106 | int ret; |
| 107 | |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 108 | debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__, |
| 109 | dev->name, enable, dev_pdata->startup_delay_us, |
| 110 | dm_gpio_is_valid(&dev_pdata->gpio)); |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 111 | /* Enable GPIO is optional */ |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 112 | if (!dm_gpio_is_valid(&dev_pdata->gpio)) { |
Marcel Ziswiler | 4c41ee4 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 113 | if (!enable) |
| 114 | return -ENOSYS; |
| 115 | return 0; |
| 116 | } |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 117 | |
| 118 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); |
| 119 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame^] | 120 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 121 | enable); |
| 122 | return ret; |
| 123 | } |
John Keeping | a531aa5 | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 124 | |
| 125 | if (enable && dev_pdata->startup_delay_us) |
| 126 | udelay(dev_pdata->startup_delay_us); |
Simon Glass | b714b26 | 2017-05-18 20:09:35 -0600 | [diff] [blame] | 127 | debug("%s: done\n", __func__); |
John Keeping | a531aa5 | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 128 | |
Przemyslaw Marczak | 3753f28 | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static const struct dm_regulator_ops fixed_regulator_ops = { |
| 133 | .get_value = fixed_regulator_get_value, |
| 134 | .get_current = fixed_regulator_get_current, |
| 135 | .get_enable = fixed_regulator_get_enable, |
| 136 | .set_enable = fixed_regulator_set_enable, |
| 137 | }; |
| 138 | |
| 139 | static const struct udevice_id fixed_regulator_ids[] = { |
| 140 | { .compatible = "regulator-fixed" }, |
| 141 | { }, |
| 142 | }; |
| 143 | |
| 144 | U_BOOT_DRIVER(fixed_regulator) = { |
| 145 | .name = "fixed regulator", |
| 146 | .id = UCLASS_REGULATOR, |
| 147 | .ops = &fixed_regulator_ops, |
| 148 | .of_match = fixed_regulator_ids, |
| 149 | .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata, |
| 150 | .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata), |
| 151 | }; |