regulator: aat2870: Convert to regulator_list_voltage_table
[deliverable/linux.git] / drivers / regulator / aat2870-regulator.c
CommitLineData
f7eb6c5e
JP
1/*
2 * linux/drivers/regulator/aat2870-regulator.c
3 *
4 * Copyright (c) 2011, NVIDIA Corporation.
5 * Author: Jin Park <jinyoungp@nvidia.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/err.h>
3a7d021b 25#include <linux/module.h>
f7eb6c5e 26#include <linux/slab.h>
f7eb6c5e
JP
27#include <linux/platform_device.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/machine.h>
30#include <linux/mfd/aat2870.h>
31
32struct aat2870_regulator {
b21bcd1a 33 struct aat2870_data *aat2870;
f7eb6c5e
JP
34 struct regulator_desc desc;
35
f7eb6c5e
JP
36 int min_uV;
37 int max_uV;
38
39 u8 enable_addr;
40 u8 enable_shift;
41 u8 enable_mask;
42
43 u8 voltage_addr;
44 u8 voltage_shift;
45 u8 voltage_mask;
46};
47
f7eb6c5e
JP
48static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev,
49 unsigned selector)
50{
51 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
b21bcd1a 52 struct aat2870_data *aat2870 = ri->aat2870;
f7eb6c5e
JP
53
54 return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask,
a5228d2e 55 selector << ri->voltage_shift);
f7eb6c5e
JP
56}
57
58static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev)
59{
60 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
b21bcd1a 61 struct aat2870_data *aat2870 = ri->aat2870;
f7eb6c5e
JP
62 u8 val;
63 int ret;
64
65 ret = aat2870->read(aat2870, ri->voltage_addr, &val);
66 if (ret)
67 return ret;
68
69 return (val & ri->voltage_mask) >> ri->voltage_shift;
70}
71
72static int aat2870_ldo_enable(struct regulator_dev *rdev)
73{
74 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
b21bcd1a 75 struct aat2870_data *aat2870 = ri->aat2870;
f7eb6c5e
JP
76
77 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask,
78 ri->enable_mask);
79}
80
81static int aat2870_ldo_disable(struct regulator_dev *rdev)
82{
83 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
b21bcd1a 84 struct aat2870_data *aat2870 = ri->aat2870;
f7eb6c5e
JP
85
86 return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0);
87}
88
89static int aat2870_ldo_is_enabled(struct regulator_dev *rdev)
90{
91 struct aat2870_regulator *ri = rdev_get_drvdata(rdev);
b21bcd1a 92 struct aat2870_data *aat2870 = ri->aat2870;
f7eb6c5e
JP
93 u8 val;
94 int ret;
95
96 ret = aat2870->read(aat2870, ri->enable_addr, &val);
97 if (ret)
98 return ret;
99
100 return val & ri->enable_mask ? 1 : 0;
101}
102
103static struct regulator_ops aat2870_ldo_ops = {
4506c6d5 104 .list_voltage = regulator_list_voltage_table,
f7eb6c5e
JP
105 .set_voltage_sel = aat2870_ldo_set_voltage_sel,
106 .get_voltage_sel = aat2870_ldo_get_voltage_sel,
107 .enable = aat2870_ldo_enable,
108 .disable = aat2870_ldo_disable,
109 .is_enabled = aat2870_ldo_is_enabled,
110};
111
4506c6d5 112static const unsigned int aat2870_ldo_voltages[] = {
f7eb6c5e
JP
113 1200000, 1300000, 1500000, 1600000,
114 1800000, 2000000, 2200000, 2500000,
115 2600000, 2700000, 2800000, 2900000,
116 3000000, 3100000, 3200000, 3300000,
117};
118
119#define AAT2870_LDO(ids) \
120 { \
121 .desc = { \
122 .name = #ids, \
123 .id = AAT2870_ID_##ids, \
124 .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \
4506c6d5 125 .volt_table = aat2870_ldo_voltages, \
f7eb6c5e
JP
126 .ops = &aat2870_ldo_ops, \
127 .type = REGULATOR_VOLTAGE, \
128 .owner = THIS_MODULE, \
129 }, \
f7eb6c5e
JP
130 .min_uV = 1200000, \
131 .max_uV = 3300000, \
132 }
133
134static struct aat2870_regulator aat2870_regulators[] = {
135 AAT2870_LDO(LDOA),
136 AAT2870_LDO(LDOB),
137 AAT2870_LDO(LDOC),
138 AAT2870_LDO(LDOD),
139};
140
141static struct aat2870_regulator *aat2870_get_regulator(int id)
142{
143 struct aat2870_regulator *ri = NULL;
144 int i;
145
146 for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
147 ri = &aat2870_regulators[i];
148 if (ri->desc.id == id)
149 break;
150 }
151
d4d6373c 152 if (i == ARRAY_SIZE(aat2870_regulators))
f7eb6c5e
JP
153 return NULL;
154
155 ri->enable_addr = AAT2870_LDO_EN;
156 ri->enable_shift = id - AAT2870_ID_LDOA;
157 ri->enable_mask = 0x1 << ri->enable_shift;
158
159 ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
160 AAT2870_LDO_CD : AAT2870_LDO_AB;
161 ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
162 ri->voltage_mask = 0xF << ri->voltage_shift;
163
164 return ri;
165}
166
167static int aat2870_regulator_probe(struct platform_device *pdev)
168{
169 struct aat2870_regulator *ri;
c172708d 170 struct regulator_config config = { 0 };
f7eb6c5e
JP
171 struct regulator_dev *rdev;
172
173 ri = aat2870_get_regulator(pdev->id);
174 if (!ri) {
175 dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
176 return -EINVAL;
177 }
b21bcd1a 178 ri->aat2870 = dev_get_drvdata(pdev->dev.parent);
f7eb6c5e 179
c172708d
MB
180 config.dev = &pdev->dev;
181 config.driver_data = ri;
182 config.init_data = pdev->dev.platform_data;
183
184 rdev = regulator_register(&ri->desc, &config);
f7eb6c5e
JP
185 if (IS_ERR(rdev)) {
186 dev_err(&pdev->dev, "Failed to register regulator %s\n",
187 ri->desc.name);
188 return PTR_ERR(rdev);
189 }
190 platform_set_drvdata(pdev, rdev);
191
192 return 0;
193}
194
195static int __devexit aat2870_regulator_remove(struct platform_device *pdev)
196{
197 struct regulator_dev *rdev = platform_get_drvdata(pdev);
198
199 regulator_unregister(rdev);
200 return 0;
201}
202
203static struct platform_driver aat2870_regulator_driver = {
204 .driver = {
205 .name = "aat2870-regulator",
206 .owner = THIS_MODULE,
207 },
208 .probe = aat2870_regulator_probe,
209 .remove = __devexit_p(aat2870_regulator_remove),
210};
211
212static int __init aat2870_regulator_init(void)
213{
214 return platform_driver_register(&aat2870_regulator_driver);
215}
216subsys_initcall(aat2870_regulator_init);
217
218static void __exit aat2870_regulator_exit(void)
219{
220 platform_driver_unregister(&aat2870_regulator_driver);
221}
222module_exit(aat2870_regulator_exit);
223
224MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator");
225MODULE_LICENSE("GPL");
226MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");
ad9c5ffe 227MODULE_ALIAS("platform:aat2870-regulator");
This page took 0.07263 seconds and 5 git commands to generate.