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