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