blob: a99aa783107e7e1279c8b4a98b682c829d759aea [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak3753f282015-04-20 20:07:48 +02002/*
3 * Copyright (C) 2015 Samsung Electronics
4 *
5 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak3753f282015-04-20 20:07:48 +02006 */
7
8#include <common.h>
Przemyslaw Marczak3753f282015-04-20 20:07:48 +02009#include <errno.h>
10#include <dm.h>
11#include <i2c.h>
12#include <asm/gpio.h>
13#include <power/pmic.h>
14#include <power/regulator.h>
15
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020016struct fixed_regulator_platdata {
17 struct gpio_desc gpio; /* GPIO for regulator enable control */
John Keepinga531aa52016-08-22 15:10:09 +010018 unsigned int startup_delay_us;
Peng Fan2a7b06c2018-07-26 19:47:24 +080019 unsigned int off_on_delay_us;
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020020};
21
22static int fixed_regulator_ofdata_to_platdata(struct udevice *dev)
23{
24 struct dm_regulator_uclass_platdata *uc_pdata;
25 struct fixed_regulator_platdata *dev_pdata;
26 struct gpio_desc *gpio;
Simon Glassb714b262017-05-18 20:09:35 -060027 int flags = GPIOD_IS_OUT;
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020028 int ret;
29
30 dev_pdata = dev_get_platdata(dev);
31 uc_pdata = dev_get_uclass_platdata(dev);
32 if (!uc_pdata)
33 return -ENXIO;
34
35 /* Set type to fixed */
36 uc_pdata->type = REGULATOR_TYPE_FIXED;
37
Simon Glassb714b262017-05-18 20:09:35 -060038 if (dev_read_bool(dev, "enable-active-high"))
Vignesh Rdd242ab2016-12-07 16:55:06 +053039 flags |= GPIOD_IS_OUT_ACTIVE;
40
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020041 /* Get fixed regulator optional enable GPIO desc */
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020042 gpio = &dev_pdata->gpio;
Vignesh Rdd242ab2016-12-07 16:55:06 +053043 ret = gpio_request_by_name(dev, "gpio", 0, gpio, flags);
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020044 if (ret) {
45 debug("Fixed regulator optional enable GPIO - not found! Error: %d\n",
46 ret);
47 if (ret != -ENOENT)
48 return ret;
49 }
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020050
John Keepinga531aa52016-08-22 15:10:09 +010051 /* Get optional ramp up delay */
Simon Glassb714b262017-05-18 20:09:35 -060052 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
53 "startup-delay-us", 0);
Peng Fan2a7b06c2018-07-26 19:47:24 +080054 dev_pdata->off_on_delay_us =
55 dev_read_u32_default(dev, "u-boot,off-on-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
Keerthy8690d6a2017-06-13 09:53:46 +053092static int fixed_regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020093{
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) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900120 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200121 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
Peng Fan2a7b06c2018-07-26 19:47:24 +0800129 if (!enable && dev_pdata->off_on_delay_us)
130 udelay(dev_pdata->off_on_delay_us);
131
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200132 return 0;
133}
134
135static const struct dm_regulator_ops fixed_regulator_ops = {
136 .get_value = fixed_regulator_get_value,
137 .get_current = fixed_regulator_get_current,
138 .get_enable = fixed_regulator_get_enable,
139 .set_enable = fixed_regulator_set_enable,
140};
141
142static const struct udevice_id fixed_regulator_ids[] = {
143 { .compatible = "regulator-fixed" },
144 { },
145};
146
147U_BOOT_DRIVER(fixed_regulator) = {
148 .name = "fixed regulator",
149 .id = UCLASS_REGULATOR,
150 .ops = &fixed_regulator_ops,
151 .of_match = fixed_regulator_ids,
152 .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
153 .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
154};