blob: eec1914c1da4946604100a90e87ad22332832c25 [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
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020017struct fixed_regulator_platdata {
18 struct gpio_desc gpio; /* GPIO for regulator enable control */
John Keepinga531aa52016-08-22 15:10:09 +010019 unsigned int startup_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);
John Keepinga531aa52016-08-22 15:10:09 +010054
Przemyslaw Marczak3753f282015-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
Keerthy8690d6a2017-06-13 09:53:46 +053090static int fixed_regulator_get_enable(struct udevice *dev)
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020091{
92 struct fixed_regulator_platdata *dev_pdata = dev_get_platdata(dev);
93
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020094 /* Enable GPIO is optional */
Przemyslaw Marczak3753f282015-04-20 20:07:48 +020095 if (!dev_pdata->gpio.dev)
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +020096 return true;
Przemyslaw Marczak3753f282015-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
Simon Glassb714b262017-05-18 20:09:35 -0600106 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
107 dev->name, enable, dev_pdata->startup_delay_us,
108 dm_gpio_is_valid(&dev_pdata->gpio));
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +0200109 /* Enable GPIO is optional */
Simon Glassb714b262017-05-18 20:09:35 -0600110 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
Marcel Ziswiler4c41ee42016-09-28 11:24:07 +0200111 if (!enable)
112 return -ENOSYS;
113 return 0;
114 }
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200115
116 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
117 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900118 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200119 enable);
120 return ret;
121 }
John Keepinga531aa52016-08-22 15:10:09 +0100122
123 if (enable && dev_pdata->startup_delay_us)
124 udelay(dev_pdata->startup_delay_us);
Simon Glassb714b262017-05-18 20:09:35 -0600125 debug("%s: done\n", __func__);
John Keepinga531aa52016-08-22 15:10:09 +0100126
Przemyslaw Marczak3753f282015-04-20 20:07:48 +0200127 return 0;
128}
129
130static const struct dm_regulator_ops fixed_regulator_ops = {
131 .get_value = fixed_regulator_get_value,
132 .get_current = fixed_regulator_get_current,
133 .get_enable = fixed_regulator_get_enable,
134 .set_enable = fixed_regulator_set_enable,
135};
136
137static const struct udevice_id fixed_regulator_ids[] = {
138 { .compatible = "regulator-fixed" },
139 { },
140};
141
142U_BOOT_DRIVER(fixed_regulator) = {
143 .name = "fixed regulator",
144 .id = UCLASS_REGULATOR,
145 .ops = &fixed_regulator_ops,
146 .of_match = fixed_regulator_ids,
147 .ofdata_to_platdata = fixed_regulator_ofdata_to_platdata,
148 .platdata_auto_alloc_size = sizeof(struct fixed_regulator_platdata),
149};