Przemyslaw Marczak | 9923a8b | 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> |
| 10 | #include <fdtdec.h> |
| 11 | #include <errno.h> |
| 12 | #include <dm.h> |
| 13 | #include <i2c.h> |
| 14 | #include <asm/gpio.h> |
| 15 | #include <power/pmic.h> |
| 16 | #include <power/regulator.h> |
| 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | struct fixed_regulator_platdata { |
| 21 | struct gpio_desc gpio; /* GPIO for regulator enable control */ |
John Keeping | 7302fbb | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 22 | unsigned int startup_delay_us; |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static int fixed_regulator_ofdata_to_platdata(struct udevice *dev) |
| 26 | { |
| 27 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 28 | struct fixed_regulator_platdata *dev_pdata; |
| 29 | struct gpio_desc *gpio; |
| 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 | |
Marcel Ziswiler | dc06f63 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 40 | /* Get fixed regulator optional enable GPIO desc */ |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 41 | gpio = &dev_pdata->gpio; |
| 42 | ret = gpio_request_by_name(dev, "gpio", 0, gpio, GPIOD_IS_OUT); |
Marcel Ziswiler | dc06f63 | 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 | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 49 | |
John Keeping | 7302fbb | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 50 | /* Get optional ramp up delay */ |
| 51 | dev_pdata->startup_delay_us = fdtdec_get_uint(gd->fdt_blob, |
| 52 | dev->of_offset, |
| 53 | "startup-delay-us", 0); |
| 54 | |
Przemyslaw Marczak | 9923a8b | 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 | |
| 90 | static bool fixed_regulator_get_enable(struct udevice *dev) |
| 91 | { |
| 92 | struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev); |
| 93 | |
Marcel Ziswiler | dc06f63 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 94 | /* Enable GPIO is optional */ |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 95 | if (!dev_pdata->gpio.dev) |
Marcel Ziswiler | dc06f63 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 96 | return true; |
Przemyslaw Marczak | 9923a8b | 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 | |
Marcel Ziswiler | dc06f63 | 2016-09-28 11:24:07 +0200 | [diff] [blame] | 106 | /* Enable GPIO is optional */ |
| 107 | if (!dev_pdata->gpio.dev) { |
| 108 | if (!enable) |
| 109 | return -ENOSYS; |
| 110 | return 0; |
| 111 | } |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 112 | |
| 113 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); |
| 114 | if (ret) { |
| 115 | error("Can't set regulator : %s gpio to: %d\n", dev->name, |
| 116 | enable); |
| 117 | return ret; |
| 118 | } |
John Keeping | 7302fbb | 2016-08-22 15:10:09 +0100 | [diff] [blame] | 119 | |
| 120 | if (enable && dev_pdata->startup_delay_us) |
| 121 | udelay(dev_pdata->startup_delay_us); |
| 122 | |
Przemyslaw Marczak | 9923a8b | 2015-04-20 20:07:48 +0200 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static const struct dm_regulator_ops fixed_regulator_ops = { |
| 127 | .get_value = fixed_regulator_get_value, |
| 128 | .get_current = fixed_regulator_get_current, |
| 129 | .get_enable = fixed_regulator_get_enable, |
| 130 | .set_enable = fixed_regulator_set_enable, |
| 131 | }; |
| 132 | |
| 133 | static const struct udevice_id fixed_regulator_ids[] = { |
| 134 | { .compatible = "regulator-fixed" }, |
| 135 | { }, |
| 136 | }; |
| 137 | |
| 138 | U_BOOT_DRIVER(fixed_regulator) = { |
| 139 | .name = "fixed regulator", |
| 140 | .id = UCLASS_REGULATOR, |
| 141 | .ops = &fixed_regulator_ops, |
| 142 | .of_match = fixed_regulator_ids, |
| 143 | .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata, |
| 144 | .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata), |
| 145 | }; |