Linux 3.6-rc6
[linux/fpc-iii.git] / drivers / regulator / mc13xxx-regulator-core.c
blobd6eda28ca5d02dbf4f06498a7787d177304d03f0
1 /*
2 * Regulator Driver for Freescale MC13xxx PMIC
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
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>
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.
14 * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
15 * from freescale
18 #include <linux/mfd/mc13xxx.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/of_regulator.h>
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>
27 #include <linux/module.h>
28 #include <linux/of.h>
29 #include "mc13xxx.h"
31 static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
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;
38 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
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);
46 return ret;
49 static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
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;
56 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
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);
63 return ret;
66 static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
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;
73 mc13xxx_lock(priv->mc13xxx);
74 ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
75 mc13xxx_unlock(priv->mc13xxx);
77 if (ret)
78 return ret;
80 return (val & mc13xxx_regulators[id].enable_bit) != 0;
83 static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
84 unsigned selector)
86 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
87 struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
88 int id = rdev_get_id(rdev);
89 int ret;
91 mc13xxx_lock(priv->mc13xxx);
92 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
93 mc13xxx_regulators[id].vsel_mask,
94 selector << mc13xxx_regulators[id].vsel_shift);
95 mc13xxx_unlock(priv->mc13xxx);
97 return ret;
100 static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
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;
107 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
109 mc13xxx_lock(priv->mc13xxx);
110 ret = mc13xxx_reg_read(priv->mc13xxx,
111 mc13xxx_regulators[id].vsel_reg, &val);
112 mc13xxx_unlock(priv->mc13xxx);
114 if (ret)
115 return ret;
117 val = (val & mc13xxx_regulators[id].vsel_mask)
118 >> mc13xxx_regulators[id].vsel_shift;
120 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
122 BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
124 return rdev->desc->volt_table[val];
127 struct regulator_ops mc13xxx_regulator_ops = {
128 .enable = mc13xxx_regulator_enable,
129 .disable = mc13xxx_regulator_disable,
130 .is_enabled = mc13xxx_regulator_is_enabled,
131 .list_voltage = regulator_list_voltage_table,
132 .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
133 .get_voltage = mc13xxx_regulator_get_voltage,
135 EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
137 int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
138 int max_uV, unsigned *selector)
140 int id = rdev_get_id(rdev);
142 dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
143 __func__, id, min_uV, max_uV);
145 if (min_uV <= rdev->desc->volt_table[0] &&
146 rdev->desc->volt_table[0] <= max_uV)
147 return 0;
148 else
149 return -EINVAL;
151 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
153 int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
155 int id = rdev_get_id(rdev);
157 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
159 return rdev->desc->volt_table[0];
161 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_get_voltage);
163 struct regulator_ops mc13xxx_fixed_regulator_ops = {
164 .enable = mc13xxx_regulator_enable,
165 .disable = mc13xxx_regulator_disable,
166 .is_enabled = mc13xxx_regulator_is_enabled,
167 .list_voltage = regulator_list_voltage_table,
168 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
169 .get_voltage = mc13xxx_fixed_regulator_get_voltage,
171 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
173 #ifdef CONFIG_OF
174 int __devinit mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
176 struct device_node *parent, *child;
177 int num = 0;
179 of_node_get(pdev->dev.parent->of_node);
180 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
181 if (!parent)
182 return -ENODEV;
184 for_each_child_of_node(parent, child)
185 num++;
187 return num;
189 EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
191 struct mc13xxx_regulator_init_data * __devinit mc13xxx_parse_regulators_dt(
192 struct platform_device *pdev, struct mc13xxx_regulator *regulators,
193 int num_regulators)
195 struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
196 struct mc13xxx_regulator_init_data *data, *p;
197 struct device_node *parent, *child;
198 int i;
200 of_node_get(pdev->dev.parent->of_node);
201 parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
202 if (!parent)
203 return NULL;
205 data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
206 GFP_KERNEL);
207 if (!data)
208 return NULL;
209 p = data;
211 for_each_child_of_node(parent, child) {
212 for (i = 0; i < num_regulators; i++) {
213 if (!of_node_cmp(child->name,
214 regulators[i].desc.name)) {
215 p->id = i;
216 p->init_data = of_get_regulator_init_data(
217 &pdev->dev, child);
218 p->node = child;
219 p++;
220 break;
225 return data;
227 EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
228 #endif
230 MODULE_LICENSE("GPL v2");
231 MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
232 MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
233 MODULE_ALIAS("mc13xxx-regulator-core");