blob: 448e8f4cf41bd9ac894b767c3a79f53be91155ef [file] [log] [blame]
Yong Shen167e3d82010-12-14 14:00:54 +08001/*
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>
21#include <linux/platform_device.h>
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/err.h>
26#include "mc13xxx.h"
27
28static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
29{
30 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
31 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
32 int id = rdev_get_id(rdev);
33 int ret;
34
35 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
36
37 mc13xxx_lock(priv->mc13xxx);
38 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
39 mc13xxx_regulators[id].enable_bit,
40 mc13xxx_regulators[id].enable_bit);
41 mc13xxx_unlock(priv->mc13xxx);
42
43 return ret;
44}
45
46static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
47{
48 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
49 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
50 int id = rdev_get_id(rdev);
51 int ret;
52
53 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
54
55 mc13xxx_lock(priv->mc13xxx);
56 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
57 mc13xxx_regulators[id].enable_bit, 0);
58 mc13xxx_unlock(priv->mc13xxx);
59
60 return ret;
61}
62
63static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
64{
65 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
66 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
67 int ret, id = rdev_get_id(rdev);
68 unsigned int val;
69
70 mc13xxx_lock(priv->mc13xxx);
71 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
72 mc13xxx_unlock(priv->mc13xxx);
73
74 if (ret)
75 return ret;
76
77 return (val & mc13xxx_regulators[id].enable_bit) != 0;
78}
79
80int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
81 unsigned selector)
82{
83 int id = rdev_get_id(rdev);
84 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
85 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
86
87 if (selector >= mc13xxx_regulators[id].desc.n_voltages)
88 return -EINVAL;
89
90 return mc13xxx_regulators[id].voltages[selector];
91}
92
93int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
94 int min_uV, int max_uV)
95{
96 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
97 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
98 int reg_id = rdev_get_id(rdev);
99 int i;
100 int bestmatch;
101 int bestindex;
102
103 /*
104 * Locate the minimum voltage fitting the criteria on
105 * this regulator. The switchable voltages are not
106 * in strict falling order so we need to check them
107 * all for the best match.
108 */
109 bestmatch = INT_MAX;
110 bestindex = -1;
111 for (i = 0; i < mc13xxx_regulators[reg_id].desc.n_voltages; i++) {
112 if (mc13xxx_regulators[reg_id].voltages[i] >= min_uV &&
113 mc13xxx_regulators[reg_id].voltages[i] < bestmatch) {
114 bestmatch = mc13xxx_regulators[reg_id].voltages[i];
115 bestindex = i;
116 }
117 }
118
119 if (bestindex < 0 || bestmatch > max_uV) {
120 dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
121 min_uV, max_uV);
122 return -EINVAL;
123 }
124 return bestindex;
125}
126
127static int mc13xxx_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
128 int max_uV, unsigned *selector)
129{
130 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
131 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
132 int value, id = rdev_get_id(rdev);
133 int ret;
134
135 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
136 __func__, id, min_uV, max_uV);
137
138 /* Find the best index */
139 value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
140 dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
141 if (value < 0)
142 return value;
143
144 mc13xxx_lock(priv->mc13xxx);
145 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
146 mc13xxx_regulators[id].vsel_mask,
147 value << mc13xxx_regulators[id].vsel_shift);
148 mc13xxx_unlock(priv->mc13xxx);
149
150 return ret;
151}
152
153static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
154{
155 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
156 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
157 int ret, id = rdev_get_id(rdev);
158 unsigned int val;
159
160 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
161
162 mc13xxx_lock(priv->mc13xxx);
163 ret = mc13xxx_reg_read(priv->mc13xxx,
164 mc13xxx_regulators[id].vsel_reg, &val);
165 mc13xxx_unlock(priv->mc13xxx);
166
167 if (ret)
168 return ret;
169
170 val = (val & mc13xxx_regulators[id].vsel_mask)
171 >> mc13xxx_regulators[id].vsel_shift;
172
173 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
174
175 BUG_ON(val < 0 || val > mc13xxx_regulators[id].desc.n_voltages);
176
177 return mc13xxx_regulators[id].voltages[val];
178}
179
180struct regulator_ops mc13xxx_regulator_ops = {
181 .enable = mc13xxx_regulator_enable,
182 .disable = mc13xxx_regulator_disable,
183 .is_enabled = mc13xxx_regulator_is_enabled,
184 .list_voltage = mc13xxx_regulator_list_voltage,
185 .set_voltage = mc13xxx_regulator_set_voltage,
186 .get_voltage = mc13xxx_regulator_get_voltage,
187};
188
189int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
190 int max_uV, unsigned *selector)
191{
192 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
193 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
194 int id = rdev_get_id(rdev);
195
196 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
197 __func__, id, min_uV, max_uV);
198
199 if (min_uV >= mc13xxx_regulators[id].voltages[0] &&
200 max_uV <= mc13xxx_regulators[id].voltages[0])
201 return 0;
202 else
203 return -EINVAL;
204}
205
206int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
207{
208 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
209 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
210 int id = rdev_get_id(rdev);
211
212 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
213
214 return mc13xxx_regulators[id].voltages[0];
215}
216
217struct regulator_ops mc13xxx_fixed_regulator_ops = {
218 .enable = mc13xxx_regulator_enable,
219 .disable = mc13xxx_regulator_disable,
220 .is_enabled = mc13xxx_regulator_is_enabled,
221 .list_voltage = mc13xxx_regulator_list_voltage,
222 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
223 .get_voltage = mc13xxx_fixed_regulator_get_voltage,
224};
225
226int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
227{
228 return 1;
229}
230
231MODULE_LICENSE("GPL v2");
232MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
233MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
234MODULE_ALIAS("mc13xxx-regulator-core");