]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/regulator/mc13xxx-regulator-core.c
regulator: mc13xxx: Fix voltage checking for mc13xxx_fixed_regulator_set_voltage
[mirror_ubuntu-artful-kernel.git] / drivers / regulator / mc13xxx-regulator-core.c
CommitLineData
167e3d8a
YS
1/*
2 * Regulator Driver for Freescale MC13xxx PMIC
3 *
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
5 *
6 * Based on mc13783 regulator driver :
7 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
15 * from freescale
16 */
17
18#include <linux/mfd/mc13xxx.h>
19#include <linux/regulator/machine.h>
20#include <linux/regulator/driver.h>
93bcb23b 21#include <linux/regulator/of_regulator.h>
167e3d8a
YS
22#include <linux/platform_device.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/err.h>
65602c32 27#include <linux/module.h>
93bcb23b 28#include <linux/of.h>
167e3d8a
YS
29#include "mc13xxx.h"
30
31static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
32{
33 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
34 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
35 int id = rdev_get_id(rdev);
36 int ret;
37
38 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
39
40 mc13xxx_lock(priv->mc13xxx);
41 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
42 mc13xxx_regulators[id].enable_bit,
43 mc13xxx_regulators[id].enable_bit);
44 mc13xxx_unlock(priv->mc13xxx);
45
46 return ret;
47}
48
49static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
50{
51 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
52 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
53 int id = rdev_get_id(rdev);
54 int ret;
55
56 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
57
58 mc13xxx_lock(priv->mc13xxx);
59 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
60 mc13xxx_regulators[id].enable_bit, 0);
61 mc13xxx_unlock(priv->mc13xxx);
62
63 return ret;
64}
65
66static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
67{
68 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
69 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
70 int ret, id = rdev_get_id(rdev);
71 unsigned int val;
72
73 mc13xxx_lock(priv->mc13xxx);
74 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
75 mc13xxx_unlock(priv->mc13xxx);
76
77 if (ret)
78 return ret;
79
80 return (val & mc13xxx_regulators[id].enable_bit) != 0;
81}
82
83int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
84 unsigned selector)
85{
86 int id = rdev_get_id(rdev);
87 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
88 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
89
90 if (selector >= mc13xxx_regulators[id].desc.n_voltages)
91 return -EINVAL;
92
93 return mc13xxx_regulators[id].voltages[selector];
94}
4d7071f1 95EXPORT_SYMBOL_GPL(mc13xxx_regulator_list_voltage);
167e3d8a 96
939b62d8
AL
97static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
98 unsigned selector)
167e3d8a
YS
99{
100 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
101 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
939b62d8 102 int id = rdev_get_id(rdev);
167e3d8a
YS
103 int ret;
104
167e3d8a
YS
105 mc13xxx_lock(priv->mc13xxx);
106 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
107 mc13xxx_regulators[id].vsel_mask,
939b62d8 108 selector << mc13xxx_regulators[id].vsel_shift);
167e3d8a
YS
109 mc13xxx_unlock(priv->mc13xxx);
110
111 return ret;
112}
113
114static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
115{
116 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
117 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
118 int ret, id = rdev_get_id(rdev);
119 unsigned int val;
120
121 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
122
123 mc13xxx_lock(priv->mc13xxx);
124 ret = mc13xxx_reg_read(priv->mc13xxx,
125 mc13xxx_regulators[id].vsel_reg, &val);
126 mc13xxx_unlock(priv->mc13xxx);
127
128 if (ret)
129 return ret;
130
131 val = (val & mc13xxx_regulators[id].vsel_mask)
132 >> mc13xxx_regulators[id].vsel_shift;
133
134 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
135
3c24019d 136 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
167e3d8a
YS
137
138 return mc13xxx_regulators[id].voltages[val];
139}
140
141struct regulator_ops mc13xxx_regulator_ops = {
142 .enable = mc13xxx_regulator_enable,
143 .disable = mc13xxx_regulator_disable,
144 .is_enabled = mc13xxx_regulator_is_enabled,
145 .list_voltage = mc13xxx_regulator_list_voltage,
939b62d8 146 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
167e3d8a
YS
147 .get_voltage = mc13xxx_regulator_get_voltage,
148};
4d7071f1 149EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
167e3d8a
YS
150
151int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
152 int max_uV, unsigned *selector)
153{
154 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
155 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
156 int id = rdev_get_id(rdev);
157
158 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
159 __func__, id, min_uV, max_uV);
160
ee5e6253
AL
161 if (min_uV <= mc13xxx_regulators[id].voltages[0] &&
162 mc13xxx_regulators[id].voltages[0] <= max_uV)
167e3d8a
YS
163 return 0;
164 else
165 return -EINVAL;
166}
4d7071f1 167EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
167e3d8a
YS
168
169int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
170{
171 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
172 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
173 int id = rdev_get_id(rdev);
174
175 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
176
177 return mc13xxx_regulators[id].voltages[0];
178}
4d7071f1 179EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_get_voltage);
167e3d8a
YS
180
181struct regulator_ops mc13xxx_fixed_regulator_ops = {
182 .enable = mc13xxx_regulator_enable,
183 .disable = mc13xxx_regulator_disable,
184 .is_enabled = mc13xxx_regulator_is_enabled,
185 .list_voltage = mc13xxx_regulator_list_voltage,
186 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
187 .get_voltage = mc13xxx_fixed_regulator_get_voltage,
188};
4d7071f1 189EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
167e3d8a
YS
190
191int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
192{
193 return 1;
194}
4d7071f1 195EXPORT_SYMBOL_GPL(mc13xxx_sw_regulator_is_enabled);
167e3d8a 196
93bcb23b
SG
197#ifdef CONFIG_OF
198int __devinit mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
199{
200 struct device_node *parent, *child;
201 int num = 0;
202
203 of_node_get(pdev->dev.parent->of_node);
204 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
205 if (!parent)
206 return -ENODEV;
207
208 for_each_child_of_node(parent, child)
209 num++;
210
211 return num;
212}
53269163 213EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
93bcb23b
SG
214
215struct mc13xxx_regulator_init_data * __devinit mc13xxx_parse_regulators_dt(
216 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
217 int num_regulators)
218{
219 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
220 struct mc13xxx_regulator_init_data *data, *p;
221 struct device_node *parent, *child;
222 int i;
223
224 of_node_get(pdev->dev.parent->of_node);
225 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
226 if (!parent)
227 return NULL;
228
229 data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
230 GFP_KERNEL);
231 if (!data)
232 return NULL;
233 p = data;
234
235 for_each_child_of_node(parent, child) {
236 for (i = 0; i < num_regulators; i++) {
237 if (!of_node_cmp(child->name,
238 regulators[i].desc.name)) {
239 p->id = i;
240 p->init_data = of_get_regulator_init_data(
241 &pdev->dev, child);
242 p->node = child;
243 p++;
244 break;
245 }
246 }
247 }
248
249 return data;
250}
53269163 251EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
93bcb23b
SG
252#endif
253
167e3d8a
YS
254MODULE_LICENSE("GPL v2");
255MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
256MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
257MODULE_ALIAS("mc13xxx-regulator-core");