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