regulator: act8865: prepare support for other act88xx devices
[deliverable/linux.git] / drivers / regulator / act8865-regulator.c
CommitLineData
33036f48 1/*
50a03e35
BG
2 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
3 *
4 * http://www.active-semi.com/products/power-management-units/act88xx/
33036f48
WY
5 *
6 * Copyright (C) 2013 Atmel Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/i2c.h>
22#include <linux/err.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/act8865.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/regulator/of_regulator.h>
29#include <linux/regmap.h>
30
31/*
32 * ACT8865 Global Register Map.
33 */
34#define ACT8865_SYS_MODE 0x00
35#define ACT8865_SYS_CTRL 0x01
36#define ACT8865_DCDC1_VSET1 0x20
37#define ACT8865_DCDC1_VSET2 0x21
38#define ACT8865_DCDC1_CTRL 0x22
39#define ACT8865_DCDC2_VSET1 0x30
40#define ACT8865_DCDC2_VSET2 0x31
41#define ACT8865_DCDC2_CTRL 0x32
42#define ACT8865_DCDC3_VSET1 0x40
43#define ACT8865_DCDC3_VSET2 0x41
44#define ACT8865_DCDC3_CTRL 0x42
45#define ACT8865_LDO1_VSET 0x50
46#define ACT8865_LDO1_CTRL 0x51
47#define ACT8865_LDO2_VSET 0x54
48#define ACT8865_LDO2_CTRL 0x55
49#define ACT8865_LDO3_VSET 0x60
50#define ACT8865_LDO3_CTRL 0x61
51#define ACT8865_LDO4_VSET 0x64
52#define ACT8865_LDO4_CTRL 0x65
53
54/*
55 * Field Definitions.
56 */
57#define ACT8865_ENA 0x80 /* ON - [7] */
58#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
59
60/*
61 * ACT8865 voltage number
62 */
63#define ACT8865_VOLTAGE_NUM 64
64
65struct act8865 {
33036f48
WY
66 struct regmap *regmap;
67};
68
69static const struct regmap_config act8865_regmap_config = {
70 .reg_bits = 8,
71 .val_bits = 8,
72};
73
50a03e35 74static const struct regulator_linear_range act8865_voltage_ranges[] = {
33036f48
WY
75 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
76 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
77 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
78};
79
80static struct regulator_ops act8865_ops = {
81 .list_voltage = regulator_list_voltage_linear_range,
82 .map_voltage = regulator_map_voltage_linear_range,
83 .get_voltage_sel = regulator_get_voltage_sel_regmap,
84 .set_voltage_sel = regulator_set_voltage_sel_regmap,
85 .enable = regulator_enable_regmap,
86 .disable = regulator_disable_regmap,
87 .is_enabled = regulator_is_enabled_regmap,
33036f48
WY
88};
89
50a03e35
BG
90#define ACT88xx_REG(_name, _family, _id, _vsel_reg) \
91 [_family##_ID_##_id] = { \
92 .name = _name, \
93 .id = _family##_ID_##_id, \
94 .type = REGULATOR_VOLTAGE, \
95 .ops = &act8865_ops, \
96 .n_voltages = ACT8865_VOLTAGE_NUM, \
97 .linear_ranges = act8865_voltage_ranges, \
98 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
99 .vsel_reg = _family##_##_id##_##_vsel_reg, \
100 .vsel_mask = ACT8865_VSEL_MASK, \
101 .enable_reg = _family##_##_id##_CTRL, \
102 .enable_mask = ACT8865_ENA, \
103 .owner = THIS_MODULE, \
104 }
105
106static const struct regulator_desc act8865_regulators[] = {
107 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1),
108 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1),
109 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1),
110 ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET),
111 ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET),
112 ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET),
113 ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET),
33036f48
WY
114};
115
116#ifdef CONFIG_OF
117static const struct of_device_id act8865_dt_ids[] = {
50a03e35 118 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
33036f48
WY
119 { }
120};
121MODULE_DEVICE_TABLE(of, act8865_dt_ids);
122
123static struct of_regulator_match act8865_matches[] = {
124 [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"},
125 [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"},
126 [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"},
127 [ACT8865_ID_LDO1] = { .name = "LDO_REG1"},
128 [ACT8865_ID_LDO2] = { .name = "LDO_REG2"},
129 [ACT8865_ID_LDO3] = { .name = "LDO_REG3"},
130 [ACT8865_ID_LDO4] = { .name = "LDO_REG4"},
131};
132
133static int act8865_pdata_from_dt(struct device *dev,
134 struct device_node **of_node,
50a03e35
BG
135 struct act8865_platform_data *pdata,
136 struct of_regulator_match *matches,
137 int num_matches)
33036f48
WY
138{
139 int matched, i;
140 struct device_node *np;
141 struct act8865_regulator_data *regulator;
142
6ea970a9 143 np = of_get_child_by_name(dev->of_node, "regulators");
33036f48
WY
144 if (!np) {
145 dev_err(dev, "missing 'regulators' subnode in DT\n");
146 return -EINVAL;
147 }
148
50a03e35 149 matched = of_regulator_match(dev, np, matches, num_matches);
0079eb53 150 of_node_put(np);
33036f48
WY
151 if (matched <= 0)
152 return matched;
153
154 pdata->regulators = devm_kzalloc(dev,
50a03e35
BG
155 sizeof(struct act8865_regulator_data) *
156 num_matches, GFP_KERNEL);
5ee77ef2 157 if (!pdata->regulators)
33036f48 158 return -ENOMEM;
33036f48 159
50a03e35 160 pdata->num_regulators = num_matches;
33036f48
WY
161 regulator = pdata->regulators;
162
50a03e35 163 for (i = 0; i < num_matches; i++) {
33036f48 164 regulator->id = i;
50a03e35
BG
165 regulator->name = matches[i].name;
166 regulator->platform_data = matches[i].init_data;
167 of_node[i] = matches[i].of_node;
33036f48
WY
168 regulator++;
169 }
170
171 return 0;
172}
173#else
174static inline int act8865_pdata_from_dt(struct device *dev,
175 struct device_node **of_node,
176 struct act8865_platform_data *pdata)
177{
178 return 0;
179}
180#endif
181
48a1e1b5
BG
182static struct regulator_init_data
183*act8865_get_init_data(int id, struct act8865_platform_data *pdata)
184{
185 int i;
186
187 if (!pdata)
188 return NULL;
189
190 for (i = 0; i < pdata->num_regulators; i++) {
191 if (pdata->regulators[i].id == id)
192 return pdata->regulators[i].platform_data;
193 }
194
195 return NULL;
196}
197
33036f48 198static int act8865_pmic_probe(struct i2c_client *client,
50a03e35 199 const struct i2c_device_id *i2c_id)
33036f48 200{
50a03e35
BG
201 static const struct regulator_desc *regulators;
202 struct act8865_platform_data pdata_of, *pdata;
203 struct of_regulator_match *matches;
33036f48 204 struct device *dev = &client->dev;
50a03e35
BG
205 struct device_node **of_node;
206 int i, ret, num_regulators;
33036f48 207 struct act8865 *act8865;
50a03e35
BG
208 unsigned long type;
209
210 pdata = dev_get_platdata(dev);
33036f48
WY
211
212 if (dev->of_node && !pdata) {
213 const struct of_device_id *id;
33036f48
WY
214
215 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
216 if (!id)
217 return -ENODEV;
218
50a03e35
BG
219 type = (unsigned long) id->data;
220 } else {
221 type = i2c_id->driver_data;
222 }
223
224 switch (type) {
225 case ACT8865:
226 matches = act8865_matches;
227 regulators = act8865_regulators;
228 num_regulators = ARRAY_SIZE(act8865_regulators);
229 break;
230 default:
231 dev_err(dev, "invalid device id %lu\n", type);
232 return -EINVAL;
233 }
234
235 of_node = devm_kzalloc(dev, sizeof(struct device_node *) *
236 num_regulators, GFP_KERNEL);
237 if (!of_node)
238 return -ENOMEM;
239
240 if (dev->of_node && !pdata) {
241 ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, matches,
242 num_regulators);
33036f48
WY
243 if (ret < 0)
244 return ret;
245
246 pdata = &pdata_of;
247 }
248
50a03e35
BG
249 if (pdata->num_regulators > num_regulators) {
250 dev_err(dev, "too many regulators: %d\n",
251 pdata->num_regulators);
33036f48
WY
252 return -EINVAL;
253 }
254
f1de2c2f 255 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
33036f48
WY
256 if (!act8865)
257 return -ENOMEM;
258
33036f48
WY
259 act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
260 if (IS_ERR(act8865->regmap)) {
40d3bc19 261 ret = PTR_ERR(act8865->regmap);
33036f48 262 dev_err(&client->dev, "Failed to allocate register map: %d\n",
40d3bc19
AL
263 ret);
264 return ret;
33036f48
WY
265 }
266
267 /* Finally register devices */
50a03e35
BG
268 for (i = 0; i < num_regulators; i++) {
269 const struct regulator_desc *desc = &regulators[i];
270 struct regulator_config config = { };
271 struct regulator_dev *rdev;
33036f48
WY
272
273 config.dev = dev;
48a1e1b5 274 config.init_data = act8865_get_init_data(desc->id, pdata);
33036f48
WY
275 config.of_node = of_node[i];
276 config.driver_data = act8865;
277 config.regmap = act8865->regmap;
278
48a1e1b5 279 rdev = devm_regulator_register(&client->dev, desc, &config);
fb8eb454 280 if (IS_ERR(rdev)) {
48a1e1b5 281 dev_err(dev, "failed to register %s\n", desc->name);
fb8eb454 282 return PTR_ERR(rdev);
33036f48
WY
283 }
284 }
285
286 i2c_set_clientdata(client, act8865);
50a03e35 287 devm_kfree(dev, of_node);
33036f48
WY
288
289 return 0;
290}
291
33036f48 292static const struct i2c_device_id act8865_ids[] = {
50a03e35 293 { .name = "act8865", .driver_data = ACT8865 },
33036f48
WY
294 { },
295};
296MODULE_DEVICE_TABLE(i2c, act8865_ids);
297
298static struct i2c_driver act8865_pmic_driver = {
299 .driver = {
300 .name = "act8865",
301 .owner = THIS_MODULE,
302 },
303 .probe = act8865_pmic_probe,
33036f48
WY
304 .id_table = act8865_ids,
305};
306
307module_i2c_driver(act8865_pmic_driver);
308
50a03e35 309MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
33036f48
WY
310MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
311MODULE_LICENSE("GPL v2");
This page took 0.05816 seconds and 5 git commands to generate.