regulator: tps65090: Add missing of_node_put
[deliverable/linux.git] / drivers / regulator / tps65090-regulator.c
CommitLineData
452534e5
VB
1/*
2 * Regulator driver for tps65090 power management chip.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 */
18
19#include <linux/module.h>
452534e5 20#include <linux/init.h>
f329b175 21#include <linux/gpio.h>
6c7a7a0e 22#include <linux/of_gpio.h>
452534e5
VB
23#include <linux/slab.h>
24#include <linux/err.h>
25#include <linux/platform_device.h>
26#include <linux/regulator/driver.h>
27#include <linux/regulator/machine.h>
6c7a7a0e 28#include <linux/regulator/of_regulator.h>
452534e5 29#include <linux/mfd/tps65090.h>
452534e5
VB
30
31struct tps65090_regulator {
452534e5 32 struct device *dev;
24282a1c
LD
33 struct regulator_desc *desc;
34 struct regulator_dev *rdev;
452534e5
VB
35};
36
f329b175
LD
37static struct regulator_ops tps65090_ext_control_ops = {
38};
39
40static struct regulator_ops tps65090_reg_contol_ops = {
41 .enable = regulator_enable_regmap,
42 .disable = regulator_disable_regmap,
43 .is_enabled = regulator_is_enabled_regmap,
452534e5
VB
44};
45
3a81ef8c
LD
46static struct regulator_ops tps65090_ldo_ops = {
47};
48
24282a1c 49#define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
452534e5 50{ \
24282a1c
LD
51 .name = "TPS65090_RAILS"#_id, \
52 .supply_name = _sname, \
8620ca9f 53 .id = TPS65090_REGULATOR_##_id, \
24282a1c
LD
54 .ops = &_ops, \
55 .enable_reg = _en_reg, \
56 .enable_mask = BIT(0), \
57 .type = REGULATOR_VOLTAGE, \
58 .owner = THIS_MODULE, \
452534e5
VB
59}
60
24282a1c 61static struct regulator_desc tps65090_regulator_desc[] = {
f329b175
LD
62 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops),
63 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops),
64 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops),
65 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops),
66 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops),
67 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops),
68 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops),
69 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops),
70 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops),
71 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops),
6c7a7a0e
LD
72 tps65090_REG_DESC(LDO1, "vsys-l1", 0, tps65090_ldo_ops),
73 tps65090_REG_DESC(LDO2, "vsys-l2", 0, tps65090_ldo_ops),
452534e5
VB
74};
75
24282a1c 76static inline bool is_dcdc(int id)
452534e5 77{
24282a1c 78 switch (id) {
8620ca9f
LD
79 case TPS65090_REGULATOR_DCDC1:
80 case TPS65090_REGULATOR_DCDC2:
81 case TPS65090_REGULATOR_DCDC3:
24282a1c
LD
82 return true;
83 default:
84 return false;
85 }
86}
452534e5 87
a5023574 88static int tps65090_config_ext_control(
24282a1c
LD
89 struct tps65090_regulator *ri, bool enable)
90{
91 int ret;
92 struct device *parent = ri->dev->parent;
93 unsigned int reg_en_reg = ri->desc->enable_reg;
94
95 if (enable)
96 ret = tps65090_set_bits(parent, reg_en_reg, 1);
97 else
98 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
99 if (ret < 0)
100 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
101 return ret;
102}
103
a5023574 104static int tps65090_regulator_disable_ext_control(
24282a1c
LD
105 struct tps65090_regulator *ri,
106 struct tps65090_regulator_plat_data *tps_pdata)
107{
108 int ret = 0;
109 struct device *parent = ri->dev->parent;
110 unsigned int reg_en_reg = ri->desc->enable_reg;
111
112 /*
113 * First enable output for internal control if require.
114 * And then disable external control.
115 */
116 if (tps_pdata->reg_init_data->constraints.always_on ||
117 tps_pdata->reg_init_data->constraints.boot_on) {
118 ret = tps65090_set_bits(parent, reg_en_reg, 0);
119 if (ret < 0) {
120 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
121 return ret;
122 }
452534e5 123 }
24282a1c 124 return tps65090_config_ext_control(ri, false);
452534e5
VB
125}
126
a5023574 127static void tps65090_configure_regulator_config(
f329b175
LD
128 struct tps65090_regulator_plat_data *tps_pdata,
129 struct regulator_config *config)
130{
131 if (gpio_is_valid(tps_pdata->gpio)) {
132 int gpio_flag = GPIOF_OUT_INIT_LOW;
133
134 if (tps_pdata->reg_init_data->constraints.always_on ||
135 tps_pdata->reg_init_data->constraints.boot_on)
136 gpio_flag = GPIOF_OUT_INIT_HIGH;
137
138 config->ena_gpio = tps_pdata->gpio;
139 config->ena_gpio_flags = gpio_flag;
140 }
141}
142
6c7a7a0e
LD
143#ifdef CONFIG_OF
144static struct of_regulator_match tps65090_matches[] = {
145 { .name = "dcdc1", },
146 { .name = "dcdc2", },
147 { .name = "dcdc3", },
148 { .name = "fet1", },
149 { .name = "fet2", },
150 { .name = "fet3", },
151 { .name = "fet4", },
152 { .name = "fet5", },
153 { .name = "fet6", },
154 { .name = "fet7", },
155 { .name = "ldo1", },
156 { .name = "ldo2", },
157};
158
159static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
160 struct platform_device *pdev,
161 struct of_regulator_match **tps65090_reg_matches)
162{
163 struct tps65090_platform_data *tps65090_pdata;
164 struct device_node *np = pdev->dev.parent->of_node;
165 struct device_node *regulators;
166 int idx = 0, ret;
167 struct tps65090_regulator_plat_data *reg_pdata;
168
169 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
170 GFP_KERNEL);
171 if (!tps65090_pdata) {
172 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
173 return ERR_PTR(-ENOMEM);
174 }
175
176 reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
177 sizeof(*reg_pdata), GFP_KERNEL);
178 if (!reg_pdata) {
179 dev_err(&pdev->dev, "Memory alloc for reg_pdata failed\n");
180 return ERR_PTR(-ENOMEM);
181 }
182
4c850ead 183 regulators = of_get_child_by_name(np, "regulators");
6c7a7a0e
LD
184 if (!regulators) {
185 dev_err(&pdev->dev, "regulator node not found\n");
186 return ERR_PTR(-ENODEV);
187 }
188
09a228e7 189 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
6c7a7a0e 190 ARRAY_SIZE(tps65090_matches));
026cdfe6 191 of_node_put(regulators);
6c7a7a0e
LD
192 if (ret < 0) {
193 dev_err(&pdev->dev,
194 "Error parsing regulator init data: %d\n", ret);
195 return ERR_PTR(ret);
196 }
197
198 *tps65090_reg_matches = tps65090_matches;
199 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
200 struct regulator_init_data *ri_data;
201 struct tps65090_regulator_plat_data *rpdata;
202
203 rpdata = &reg_pdata[idx];
204 ri_data = tps65090_matches[idx].init_data;
205 if (!ri_data || !tps65090_matches[idx].of_node)
206 continue;
207
208 rpdata->reg_init_data = ri_data;
209 rpdata->enable_ext_control = of_property_read_bool(
210 tps65090_matches[idx].of_node,
211 "ti,enable-ext-control");
212 if (rpdata->enable_ext_control)
213 rpdata->gpio = of_get_named_gpio(np,
214 "dcdc-ext-control-gpios", 0);
215
216 tps65090_pdata->reg_pdata[idx] = rpdata;
217 }
218 return tps65090_pdata;
219}
220#else
221static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
222 struct platform_device *pdev,
223 struct of_regulator_match **tps65090_reg_matches)
224{
225 *tps65090_reg_matches = NULL;
226 return NULL;
227}
228#endif
229
a5023574 230static int tps65090_regulator_probe(struct platform_device *pdev)
452534e5 231{
06c4998b 232 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
452534e5 233 struct tps65090_regulator *ri = NULL;
c172708d 234 struct regulator_config config = { };
452534e5 235 struct regulator_dev *rdev;
24282a1c
LD
236 struct tps65090_regulator_plat_data *tps_pdata;
237 struct tps65090_regulator *pmic;
238 struct tps65090_platform_data *tps65090_pdata;
6c7a7a0e 239 struct of_regulator_match *tps65090_reg_matches = NULL;
24282a1c
LD
240 int num;
241 int ret;
452534e5 242
24282a1c 243 dev_dbg(&pdev->dev, "Probing regulator\n");
452534e5 244
24282a1c 245 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
6c7a7a0e
LD
246 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
247 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
248 &tps65090_reg_matches);
249 if (IS_ERR_OR_NULL(tps65090_pdata)) {
24282a1c 250 dev_err(&pdev->dev, "Platform data missing\n");
6c7a7a0e 251 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
452534e5 252 }
24282a1c 253
8620ca9f 254 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
24282a1c
LD
255 GFP_KERNEL);
256 if (!pmic) {
257 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
258 return -ENOMEM;
259 }
260
8620ca9f 261 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
24282a1c
LD
262 tps_pdata = tps65090_pdata->reg_pdata[num];
263
264 ri = &pmic[num];
265 ri->dev = &pdev->dev;
266 ri->desc = &tps65090_regulator_desc[num];
267
268 /*
269 * TPS5090 DCDC support the control from external digital input.
f329b175 270 * Configure it as per platform data.
24282a1c
LD
271 */
272 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
f329b175
LD
273 if (tps_pdata->enable_ext_control) {
274 tps65090_configure_regulator_config(
275 tps_pdata, &config);
276 ri->desc->ops = &tps65090_ext_control_ops;
277 } else {
278 ret = tps65090_regulator_disable_ext_control(
24282a1c 279 ri, tps_pdata);
f329b175
LD
280 if (ret < 0) {
281 dev_err(&pdev->dev,
24282a1c 282 "failed disable ext control\n");
9738efae 283 return ret;
f329b175 284 }
24282a1c
LD
285 }
286 }
f329b175 287
6c7a7a0e 288 config.dev = pdev->dev.parent;
24282a1c
LD
289 config.driver_data = ri;
290 config.regmap = tps65090_mfd->rmap;
291 if (tps_pdata)
292 config.init_data = tps_pdata->reg_init_data;
293 else
294 config.init_data = NULL;
6c7a7a0e
LD
295 if (tps65090_reg_matches)
296 config.of_node = tps65090_reg_matches[num].of_node;
297 else
298 config.of_node = NULL;
24282a1c 299
9738efae 300 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
24282a1c
LD
301 if (IS_ERR(rdev)) {
302 dev_err(&pdev->dev, "failed to register regulator %s\n",
303 ri->desc->name);
9738efae 304 return PTR_ERR(rdev);
24282a1c
LD
305 }
306 ri->rdev = rdev;
f329b175
LD
307
308 /* Enable external control if it is require */
309 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
310 tps_pdata->enable_ext_control) {
311 ret = tps65090_config_ext_control(ri, true);
9738efae
SK
312 if (ret < 0)
313 return ret;
f329b175 314 }
452534e5
VB
315 }
316
24282a1c 317 platform_set_drvdata(pdev, pmic);
452534e5 318 return 0;
452534e5
VB
319}
320
321static struct platform_driver tps65090_regulator_driver = {
322 .driver = {
8620ca9f 323 .name = "tps65090-pmic",
452534e5
VB
324 .owner = THIS_MODULE,
325 },
326 .probe = tps65090_regulator_probe,
452534e5
VB
327};
328
329static int __init tps65090_regulator_init(void)
330{
331 return platform_driver_register(&tps65090_regulator_driver);
332}
333subsys_initcall(tps65090_regulator_init);
334
335static void __exit tps65090_regulator_exit(void)
336{
337 platform_driver_unregister(&tps65090_regulator_driver);
338}
339module_exit(tps65090_regulator_exit);
340
341MODULE_DESCRIPTION("tps65090 regulator driver");
342MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
343MODULE_LICENSE("GPL v2");
d95420b8 344MODULE_ALIAS("platform:tps65090-pmic");
This page took 0.136849 seconds and 5 git commands to generate.