]>
Commit | Line | Data |
---|---|---|
9923a8b4 PM |
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> | |
9923a8b4 PM |
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 */ | |
7302fbb3 | 21 | unsigned int startup_delay_us; |
9923a8b4 PM |
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; | |
a5493f82 | 29 | int flags = GPIOD_IS_OUT; |
9923a8b4 PM |
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 | ||
a5493f82 | 40 | if (dev_read_bool(dev, "enable-active-high")) |
bd2e9714 V |
41 | flags |= GPIOD_IS_OUT_ACTIVE; |
42 | ||
dc06f63f | 43 | /* Get fixed regulator optional enable GPIO desc */ |
9923a8b4 | 44 | gpio = &dev_pdata->gpio; |
bd2e9714 | 45 | ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags); |
dc06f63f MZ |
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 | } | |
9923a8b4 | 52 | |
7302fbb3 | 53 | /* Get optional ramp up delay */ |
a5493f82 SG |
54 | dev_pdata->startup_delay_us = dev_read_u32_default(dev, |
55 | "startup-delay-us", 0); | |
7302fbb3 | 56 | |
9923a8b4 PM |
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 | ||
0f2194d8 | 92 | static int fixed_regulator_get_enable(struct udevice *dev) |
9923a8b4 PM |
93 | { |
94 | struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev); | |
95 | ||
dc06f63f | 96 | /* Enable GPIO is optional */ |
9923a8b4 | 97 | if (!dev_pdata->gpio.dev) |
dc06f63f | 98 | return true; |
9923a8b4 PM |
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 | ||
a5493f82 SG |
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)); | |
dc06f63f | 111 | /* Enable GPIO is optional */ |
a5493f82 | 112 | if (!dm_gpio_is_valid(&dev_pdata->gpio)) { |
dc06f63f MZ |
113 | if (!enable) |
114 | return -ENOSYS; | |
115 | return 0; | |
116 | } | |
9923a8b4 PM |
117 | |
118 | ret = dm_gpio_set_value(&dev_pdata->gpio, enable); | |
119 | if (ret) { | |
9b643e31 | 120 | pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, |
9923a8b4 PM |
121 | enable); |
122 | return ret; | |
123 | } | |
7302fbb3 JK |
124 | |
125 | if (enable && dev_pdata->startup_delay_us) | |
126 | udelay(dev_pdata->startup_delay_us); | |
a5493f82 | 127 | debug("%s: done\n", __func__); |
7302fbb3 | 128 | |
9923a8b4 PM |
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 | }; |