Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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
939b62d8
AL
83static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
84 unsigned selector)
167e3d8a
YS
85{
86 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
87 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
939b62d8 88 int id = rdev_get_id(rdev);
167e3d8a
YS
89 int ret;
90
167e3d8a
YS
91 mc13xxx_lock(priv->mc13xxx);
92 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
93 mc13xxx_regulators[id].vsel_mask,
939b62d8 94 selector << mc13xxx_regulators[id].vsel_shift);
167e3d8a
YS
95 mc13xxx_unlock(priv->mc13xxx);
96
97 return ret;
98}
99
100static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
101{
102 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
103 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
104 int ret, id = rdev_get_id(rdev);
105 unsigned int val;
106
107 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
108
109 mc13xxx_lock(priv->mc13xxx);
110 ret = mc13xxx_reg_read(priv->mc13xxx,
111 mc13xxx_regulators[id].vsel_reg, &val);
112 mc13xxx_unlock(priv->mc13xxx);
113
114 if (ret)
115 return ret;
116
117 val = (val & mc13xxx_regulators[id].vsel_mask)
118 >> mc13xxx_regulators[id].vsel_shift;
119
120 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
121
3c24019d 122 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
167e3d8a 123
34e74f39 124 return rdev->desc->volt_table[val];
167e3d8a
YS
125}
126
127struct regulator_ops mc13xxx_regulator_ops = {
128 .enable = mc13xxx_regulator_enable,
129 .disable = mc13xxx_regulator_disable,
130 .is_enabled = mc13xxx_regulator_is_enabled,
34e74f39 131 .list_voltage = regulator_list_voltage_table,
939b62d8 132 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
167e3d8a
YS
133 .get_voltage = mc13xxx_regulator_get_voltage,
134};
4d7071f1 135EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
167e3d8a
YS
136
137int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
138 int max_uV, unsigned *selector)
139{
167e3d8a
YS
140 int id = rdev_get_id(rdev);
141
142 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
143 __func__, id, min_uV, max_uV);
144
34e74f39 145 if (min_uV <= rdev->desc->volt_table[0] &&
13407ea8
AL
146 rdev->desc->volt_table[0] <= max_uV) {
147 *selector = 0;
167e3d8a 148 return 0;
13407ea8 149 } else {
167e3d8a 150 return -EINVAL;
13407ea8 151 }
167e3d8a 152}
4d7071f1 153EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
167e3d8a 154
167e3d8a
YS
155struct regulator_ops mc13xxx_fixed_regulator_ops = {
156 .enable = mc13xxx_regulator_enable,
157 .disable = mc13xxx_regulator_disable,
158 .is_enabled = mc13xxx_regulator_is_enabled,
34e74f39 159 .list_voltage = regulator_list_voltage_table,
167e3d8a 160 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
167e3d8a 161};
4d7071f1 162EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
167e3d8a 163
93bcb23b 164#ifdef CONFIG_OF
a5023574 165int mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
93bcb23b 166{
86f66733
AL
167 struct device_node *parent;
168 int num;
93bcb23b
SG
169
170 of_node_get(pdev->dev.parent->of_node);
171 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
172 if (!parent)
173 return -ENODEV;
174
86f66733 175 num = of_get_child_count(parent);
c92f5dd2 176 of_node_put(parent);
93bcb23b
SG
177 return num;
178}
53269163 179EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
93bcb23b 180
a5023574 181struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt(
93bcb23b 182 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
eb0d8e7a 183 int num_regulators)
93bcb23b
SG
184{
185 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
186 struct mc13xxx_regulator_init_data *data, *p;
187 struct device_node *parent, *child;
2c8a5dca
MS
188 int i, parsed = 0;
189
93bcb23b
SG
190 of_node_get(pdev->dev.parent->of_node);
191 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
192 if (!parent)
193 return NULL;
194
195 data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
196 GFP_KERNEL);
c92f5dd2
AL
197 if (!data) {
198 of_node_put(parent);
93bcb23b 199 return NULL;
c92f5dd2
AL
200 }
201
93bcb23b
SG
202 p = data;
203
204 for_each_child_of_node(parent, child) {
eb0d8e7a
AS
205 int found = 0;
206
93bcb23b 207 for (i = 0; i < num_regulators; i++) {
eb0d8e7a
AS
208 if (!regulators[i].desc.name)
209 continue;
93bcb23b
SG
210 if (!of_node_cmp(child->name,
211 regulators[i].desc.name)) {
212 p->id = i;
213 p->init_data = of_get_regulator_init_data(
214 &pdev->dev, child);
215 p->node = child;
216 p++;
2c8a5dca
MS
217
218 parsed++;
eb0d8e7a 219 found = 1;
93bcb23b
SG
220 break;
221 }
222 }
eb0d8e7a
AS
223
224 if (!found)
225 dev_warn(&pdev->dev,
226 "Unknown regulator: %s\n", child->name);
93bcb23b 227 }
c92f5dd2 228 of_node_put(parent);
93bcb23b 229
eb0d8e7a
AS
230 priv->num_regulators = parsed;
231
93bcb23b
SG
232 return data;
233}
53269163 234EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
93bcb23b
SG
235#endif
236
167e3d8a
YS
237MODULE_LICENSE("GPL v2");
238MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
239MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
240MODULE_ALIAS("mc13xxx-regulator-core");