2 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
3 * Keerthy <j-keerthy@ti.com>
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <power/pmic.h>
15 #include <power/regulator.h>
17 #define GPIO_REGULATOR_MAX_STATES 2
19 DECLARE_GLOBAL_DATA_PTR
;
21 struct gpio_regulator_platdata
{
22 struct gpio_desc gpio
; /* GPIO for regulator voltage control */
23 int states
[GPIO_REGULATOR_MAX_STATES
];
24 int voltages
[GPIO_REGULATOR_MAX_STATES
];
27 static int gpio_regulator_ofdata_to_platdata(struct udevice
*dev
)
29 struct dm_regulator_uclass_platdata
*uc_pdata
;
30 struct gpio_regulator_platdata
*dev_pdata
;
31 struct gpio_desc
*gpio
;
32 const void *blob
= gd
->fdt_blob
;
33 int node
= dev
->of_offset
;
37 dev_pdata
= dev_get_platdata(dev
);
38 uc_pdata
= dev_get_uclass_platdata(dev
);
42 /* Set type to gpio */
43 uc_pdata
->type
= REGULATOR_TYPE_GPIO
;
46 * Get gpio regulator gpio desc
47 * Assuming one GPIO per regulator.
48 * Can be extended later to multiple GPIOs
49 * per gpio-regulator. As of now no instance with multiple
52 gpio
= &dev_pdata
->gpio
;
53 ret
= gpio_request_by_name(dev
, "gpios", 0, gpio
, GPIOD_IS_OUT
);
55 debug("regulator gpio - not found! Error: %d", ret
);
57 count
= fdtdec_get_int_array_count(blob
, node
, "states",
63 for (i
= 0, j
= 0; i
< count
; i
+= 2) {
64 dev_pdata
->voltages
[j
] = states_array
[i
];
65 dev_pdata
->states
[j
] = states_array
[i
+ 1];
72 static int gpio_regulator_get_value(struct udevice
*dev
)
74 struct dm_regulator_uclass_platdata
*uc_pdata
;
75 struct gpio_regulator_platdata
*dev_pdata
= dev_get_platdata(dev
);
78 if (!dev_pdata
->gpio
.dev
)
81 uc_pdata
= dev_get_uclass_platdata(dev
);
82 if (uc_pdata
->min_uV
> uc_pdata
->max_uV
) {
83 debug("Invalid constraints for: %s\n", uc_pdata
->name
);
87 enable
= dm_gpio_get_value(&dev_pdata
->gpio
);
88 if (enable
== dev_pdata
->states
[0])
89 return dev_pdata
->voltages
[0];
91 return dev_pdata
->voltages
[1];
94 static int gpio_regulator_set_value(struct udevice
*dev
, int uV
)
96 struct gpio_regulator_platdata
*dev_pdata
= dev_get_platdata(dev
);
100 if (!dev_pdata
->gpio
.dev
)
103 if (uV
== dev_pdata
->voltages
[0])
104 enable
= dev_pdata
->states
[0];
105 else if (uV
== dev_pdata
->voltages
[1])
106 enable
= dev_pdata
->states
[1];
110 ret
= dm_gpio_set_value(&dev_pdata
->gpio
, enable
);
112 error("Can't set regulator : %s gpio to: %d\n", dev
->name
,
120 static const struct dm_regulator_ops gpio_regulator_ops
= {
121 .get_value
= gpio_regulator_get_value
,
122 .set_value
= gpio_regulator_set_value
,
125 static const struct udevice_id gpio_regulator_ids
[] = {
126 { .compatible
= "regulator-gpio" },
130 U_BOOT_DRIVER(gpio_regulator
) = {
131 .name
= "gpio regulator",
132 .id
= UCLASS_REGULATOR
,
133 .ops
= &gpio_regulator_ops
,
134 .of_match
= gpio_regulator_ids
,
135 .ofdata_to_platdata
= gpio_regulator_ofdata_to_platdata
,
136 .platdata_auto_alloc_size
= sizeof(struct gpio_regulator_platdata
),