gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / regulator / mc13xxx-regulator-core.c
blob8ff19150ca9230811c88d8284375505e8d28ff47
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Regulator Driver for Freescale MC13xxx PMIC
4 //
5 // Copyright 2010 Yong Shen <yong.shen@linaro.org>
6 //
7 // Based on mc13783 regulator driver :
8 // Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
9 // Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
11 // Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
12 // from freescale
14 #include <linux/mfd/mc13xxx.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/platform_device.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include "mc13xxx.h"
27 static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
29 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
30 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
31 int id = rdev_get_id(rdev);
33 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
35 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
36 mc13xxx_regulators[id].enable_bit,
37 mc13xxx_regulators[id].enable_bit);
40 static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
42 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
43 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
44 int id = rdev_get_id(rdev);
46 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
48 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
49 mc13xxx_regulators[id].enable_bit, 0);
52 static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
54 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
55 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
56 int ret, id = rdev_get_id(rdev);
57 unsigned int val;
59 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
60 if (ret)
61 return ret;
63 return (val & mc13xxx_regulators[id].enable_bit) != 0;
66 static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
67 unsigned selector)
69 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
70 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
71 int id = rdev_get_id(rdev);
73 return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
74 mc13xxx_regulators[id].vsel_mask,
75 selector << mc13xxx_regulators[id].vsel_shift);
78 static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
80 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
81 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
82 int ret, id = rdev_get_id(rdev);
83 unsigned int val;
85 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
87 ret = mc13xxx_reg_read(priv->mc13xxx,
88 mc13xxx_regulators[id].vsel_reg, &val);
89 if (ret)
90 return ret;
92 val = (val & mc13xxx_regulators[id].vsel_mask)
93 >> mc13xxx_regulators[id].vsel_shift;
95 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
97 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
99 return rdev->desc->volt_table[val];
102 const struct regulator_ops mc13xxx_regulator_ops = {
103 .enable = mc13xxx_regulator_enable,
104 .disable = mc13xxx_regulator_disable,
105 .is_enabled = mc13xxx_regulator_is_enabled,
106 .list_voltage = regulator_list_voltage_table,
107 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
108 .get_voltage = mc13xxx_regulator_get_voltage,
110 EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
112 int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
113 int max_uV, unsigned *selector)
115 int id = rdev_get_id(rdev);
117 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
118 __func__, id, min_uV, max_uV);
120 if (min_uV <= rdev->desc->volt_table[0] &&
121 rdev->desc->volt_table[0] <= max_uV) {
122 *selector = 0;
123 return 0;
124 } else {
125 return -EINVAL;
128 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
130 const struct regulator_ops mc13xxx_fixed_regulator_ops = {
131 .enable = mc13xxx_regulator_enable,
132 .disable = mc13xxx_regulator_disable,
133 .is_enabled = mc13xxx_regulator_is_enabled,
134 .list_voltage = regulator_list_voltage_table,
135 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
137 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
139 #ifdef CONFIG_OF
140 int mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
142 struct device_node *parent;
143 int num;
145 if (!pdev->dev.parent->of_node)
146 return -ENODEV;
148 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
149 if (!parent)
150 return -ENODEV;
152 num = of_get_child_count(parent);
153 of_node_put(parent);
154 return num;
156 EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
158 struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt(
159 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
160 int num_regulators)
162 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
163 struct mc13xxx_regulator_init_data *data, *p;
164 struct device_node *parent, *child;
165 int i, parsed = 0;
167 if (!pdev->dev.parent->of_node)
168 return NULL;
170 parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
171 if (!parent)
172 return NULL;
174 data = devm_kcalloc(&pdev->dev, priv->num_regulators, sizeof(*data),
175 GFP_KERNEL);
176 if (!data) {
177 of_node_put(parent);
178 return NULL;
181 p = data;
183 for_each_child_of_node(parent, child) {
184 int found = 0;
186 for (i = 0; i < num_regulators; i++) {
187 if (!regulators[i].desc.name)
188 continue;
189 if (of_node_name_eq(child,
190 regulators[i].desc.name)) {
191 p->id = i;
192 p->init_data = of_get_regulator_init_data(
193 &pdev->dev, child,
194 &regulators[i].desc);
195 p->node = child;
196 p++;
198 parsed++;
199 found = 1;
200 break;
204 if (!found)
205 dev_warn(&pdev->dev,
206 "Unknown regulator: %pOFn\n", child);
208 of_node_put(parent);
210 priv->num_regulators = parsed;
212 return data;
214 EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
215 #endif
217 MODULE_LICENSE("GPL v2");
218 MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
219 MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
220 MODULE_ALIAS("mc13xxx-regulator-core");