blob: 62dc47f769859210be08d823a6f889055301cc5e [file] [log] [blame]
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +02001/*
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
18DECLARE_GLOBAL_DATA_PTR;
19
20struct fixed_regulator_platdata {
21 struct gpio_desc gpio; /* GPIO for regulator enable control */
John Keeping7302fbb2016-08-22 15:10:09 +010022 unsigned int startup_delay_us;
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +020023};
24
25static 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 Ziswilerdc06f632016-09-28 11:24:07 +020040 /* Get fixed regulator optional enable GPIO desc */
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +020041 gpio = &dev_pdata->gpio;
42 ret = gpio_request_by_name(dev, "gpio", 0, gpio, GPIOD_IS_OUT);
Marcel Ziswilerdc06f632016-09-28 11:24:07 +020043 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 Marczak9923a8b2015-04-20 20:07:48 +020049
John Keeping7302fbb2016-08-22 15:10:09 +010050 /* 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 Marczak9923a8b2015-04-20 20:07:48 +020055 return 0;
56}
57
58static 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
74static 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
90static bool fixed_regulator_get_enable(struct udevice *dev)
91{
92 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
93
Marcel Ziswilerdc06f632016-09-28 11:24:07 +020094 /* Enable GPIO is optional */
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +020095 if (!dev_pdata->gpio.dev)
Marcel Ziswilerdc06f632016-09-28 11:24:07 +020096 return true;
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +020097
98 return dm_gpio_get_value(&dev_pdata->gpio);
99}
100
101static 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 Ziswilerdc06f632016-09-28 11:24:07 +0200106 /* Enable GPIO is optional */
107 if (!dev_pdata->gpio.dev) {
108 if (!enable)
109 return -ENOSYS;
110 return 0;
111 }
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +0200112
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 Keeping7302fbb2016-08-22 15:10:09 +0100119
120 if (enable && dev_pdata->startup_delay_us)
121 udelay(dev_pdata->startup_delay_us);
122
Przemyslaw Marczak9923a8b2015-04-20 20:07:48 +0200123 return 0;
124}
125
126static 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
133static const struct udevice_id fixed_regulator_ids[] = {
134 { .compatible = "regulator-fixed" },
135 { },
136};
137
138U_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};