blob: 656371b2351955cb35791ecdc3550a35309f9d03 [file] [log] [blame]
Przemyslaw Marczak3753f282015-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>
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020010#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
17DECLARE_GLOBAL_DATA_PTR;
18
19struct fixed_regulator_platdata {
20 struct gpio_desc gpio; /* GPIO for regulator enable control */
John Keepinga531aa52016-08-22 15:10:09 +010021 unsigned int startup_delay_us;
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020022};
23
24static 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;
Simon Glassb714b262017-05-18 20:09:35 -060029 int flags = GPIOD_IS_OUT;
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020030 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
Simon Glassb714b262017-05-18 20:09:35 -060040 if (dev_read_bool(dev, "enable-active-high"))
Vignesh Rdd242ab2016-12-07 16:55:06 +053041 flags |= GPIOD_IS_OUT_ACTIVE;
42
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020043 /* Get fixed regulator optional enable GPIO desc */
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020044 gpio = &dev_pdata->gpio;
Vignesh Rdd242ab2016-12-07 16:55:06 +053045 ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020046 if (ret) {
47 debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
48 ret);
49 if (ret != -ENOENT)
50 return ret;
51 }
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020052
John Keepinga531aa52016-08-22 15:10:09 +010053 /* Get optional ramp up delay */
Simon Glassb714b262017-05-18 20:09:35 -060054 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
55 "startup-delay-us", 0);
John Keepinga531aa52016-08-22 15:10:09 +010056
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020057 return 0;
58}
59
60static 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
76static 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
92static bool fixed_regulator_get_enable(struct udevice *dev)
93{
94 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
95
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020096 /* Enable GPIO is optional */
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020097 if (!dev_pdata->gpio.dev)
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020098 return true;
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020099
100 return dm_gpio_get_value(&dev_pdata->gpio);
101}
102
103static 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
Simon Glassb714b262017-05-18 20:09:35 -0600108 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));
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +0200111 /* Enable GPIO is optional */
Simon Glassb714b262017-05-18 20:09:35 -0600112 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +0200113 if (!enable)
114 return -ENOSYS;
115 return 0;
116 }
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200117
118 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
119 if (ret) {
120 error("Can't set regulator : %s gpio to: %d\n", dev->name,
121 enable);
122 return ret;
123 }
John Keepinga531aa52016-08-22 15:10:09 +0100124
125 if (enable && dev_pdata->startup_delay_us)
126 udelay(dev_pdata->startup_delay_us);
Simon Glassb714b262017-05-18 20:09:35 -0600127 debug("%s: done\n", __func__);
John Keepinga531aa52016-08-22 15:10:09 +0100128
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200129 return 0;
130}
131
132static 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
139static const struct udevice_id fixed_regulator_ids[] = {
140 { .compatible = "regulator-fixed" },
141 { },
142};
143
144U_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};