blob: afd06f92dfdf006cba0cc282d5e87e0b757bdf9e [file] [log] [blame]
Wenyou Yang33036f42013-12-24 10:34:28 +08001/*
Beniamino Galvani50a03e32014-07-05 15:20:54 +02002 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
3 *
4 * http://www.active-semi.com/products/power-management-units/act88xx/
Wenyou Yang33036f42013-12-24 10:34:28 +08005 *
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/*
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020032 * ACT8846 Global Register Map.
33 */
34#define ACT8846_SYS0 0x00
35#define ACT8846_SYS1 0x01
36#define ACT8846_REG1_VSET 0x10
37#define ACT8846_REG1_CTRL 0x12
38#define ACT8846_REG2_VSET0 0x20
39#define ACT8846_REG2_VSET1 0x21
40#define ACT8846_REG2_CTRL 0x22
41#define ACT8846_REG3_VSET0 0x30
42#define ACT8846_REG3_VSET1 0x31
43#define ACT8846_REG3_CTRL 0x32
44#define ACT8846_REG4_VSET0 0x40
45#define ACT8846_REG4_VSET1 0x41
46#define ACT8846_REG4_CTRL 0x42
47#define ACT8846_REG5_VSET 0x50
48#define ACT8846_REG5_CTRL 0x51
49#define ACT8846_REG6_VSET 0x58
50#define ACT8846_REG6_CTRL 0x59
51#define ACT8846_REG7_VSET 0x60
52#define ACT8846_REG7_CTRL 0x61
53#define ACT8846_REG8_VSET 0x68
54#define ACT8846_REG8_CTRL 0x69
55#define ACT8846_REG9_VSET 0x70
56#define ACT8846_REG9_CTRL 0x71
57#define ACT8846_REG10_VSET 0x80
58#define ACT8846_REG10_CTRL 0x81
59#define ACT8846_REG11_VSET 0x90
60#define ACT8846_REG11_CTRL 0x91
61#define ACT8846_REG12_VSET 0xa0
62#define ACT8846_REG12_CTRL 0xa1
63#define ACT8846_REG13_CTRL 0xb1
64
65/*
Wenyou Yang33036f42013-12-24 10:34:28 +080066 * ACT8865 Global Register Map.
67 */
68#define ACT8865_SYS_MODE 0x00
69#define ACT8865_SYS_CTRL 0x01
70#define ACT8865_DCDC1_VSET1 0x20
71#define ACT8865_DCDC1_VSET2 0x21
72#define ACT8865_DCDC1_CTRL 0x22
73#define ACT8865_DCDC2_VSET1 0x30
74#define ACT8865_DCDC2_VSET2 0x31
75#define ACT8865_DCDC2_CTRL 0x32
76#define ACT8865_DCDC3_VSET1 0x40
77#define ACT8865_DCDC3_VSET2 0x41
78#define ACT8865_DCDC3_CTRL 0x42
79#define ACT8865_LDO1_VSET 0x50
80#define ACT8865_LDO1_CTRL 0x51
81#define ACT8865_LDO2_VSET 0x54
82#define ACT8865_LDO2_CTRL 0x55
83#define ACT8865_LDO3_VSET 0x60
84#define ACT8865_LDO3_CTRL 0x61
85#define ACT8865_LDO4_VSET 0x64
86#define ACT8865_LDO4_CTRL 0x65
87
88/*
89 * Field Definitions.
90 */
91#define ACT8865_ENA 0x80 /* ON - [7] */
92#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
93
94/*
95 * ACT8865 voltage number
96 */
97#define ACT8865_VOLTAGE_NUM 64
98
99struct act8865 {
Wenyou Yang33036f42013-12-24 10:34:28 +0800100 struct regmap *regmap;
101};
102
103static const struct regmap_config act8865_regmap_config = {
104 .reg_bits = 8,
105 .val_bits = 8,
106};
107
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200108static const struct regulator_linear_range act8865_voltage_ranges[] = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800109 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
110 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
111 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
112};
113
114static struct regulator_ops act8865_ops = {
115 .list_voltage = regulator_list_voltage_linear_range,
116 .map_voltage = regulator_map_voltage_linear_range,
117 .get_voltage_sel = regulator_get_voltage_sel_regmap,
118 .set_voltage_sel = regulator_set_voltage_sel_regmap,
119 .enable = regulator_enable_regmap,
120 .disable = regulator_disable_regmap,
121 .is_enabled = regulator_is_enabled_regmap,
Wenyou Yang33036f42013-12-24 10:34:28 +0800122};
123
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200124#define ACT88xx_REG(_name, _family, _id, _vsel_reg) \
125 [_family##_ID_##_id] = { \
126 .name = _name, \
127 .id = _family##_ID_##_id, \
128 .type = REGULATOR_VOLTAGE, \
129 .ops = &act8865_ops, \
130 .n_voltages = ACT8865_VOLTAGE_NUM, \
131 .linear_ranges = act8865_voltage_ranges, \
132 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
133 .vsel_reg = _family##_##_id##_##_vsel_reg, \
134 .vsel_mask = ACT8865_VSEL_MASK, \
135 .enable_reg = _family##_##_id##_CTRL, \
136 .enable_mask = ACT8865_ENA, \
137 .owner = THIS_MODULE, \
138 }
139
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200140static const struct regulator_desc act8846_regulators[] = {
141 ACT88xx_REG("REG1", ACT8846, REG1, VSET),
142 ACT88xx_REG("REG2", ACT8846, REG2, VSET0),
143 ACT88xx_REG("REG3", ACT8846, REG3, VSET0),
144 ACT88xx_REG("REG4", ACT8846, REG4, VSET0),
145 ACT88xx_REG("REG5", ACT8846, REG5, VSET),
146 ACT88xx_REG("REG6", ACT8846, REG6, VSET),
147 ACT88xx_REG("REG7", ACT8846, REG7, VSET),
148 ACT88xx_REG("REG8", ACT8846, REG8, VSET),
149 ACT88xx_REG("REG9", ACT8846, REG9, VSET),
150 ACT88xx_REG("REG10", ACT8846, REG10, VSET),
151 ACT88xx_REG("REG11", ACT8846, REG11, VSET),
152 ACT88xx_REG("REG12", ACT8846, REG12, VSET),
153};
154
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200155static const struct regulator_desc act8865_regulators[] = {
156 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1),
157 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1),
158 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1),
159 ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET),
160 ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET),
161 ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET),
162 ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET),
Wenyou Yang33036f42013-12-24 10:34:28 +0800163};
164
165#ifdef CONFIG_OF
166static const struct of_device_id act8865_dt_ids[] = {
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200167 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200168 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800169 { }
170};
171MODULE_DEVICE_TABLE(of, act8865_dt_ids);
172
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200173static struct of_regulator_match act8846_matches[] = {
174 [ACT8846_ID_REG1] = { .name = "REG1" },
175 [ACT8846_ID_REG2] = { .name = "REG2" },
176 [ACT8846_ID_REG3] = { .name = "REG3" },
177 [ACT8846_ID_REG4] = { .name = "REG4" },
178 [ACT8846_ID_REG5] = { .name = "REG5" },
179 [ACT8846_ID_REG6] = { .name = "REG6" },
180 [ACT8846_ID_REG7] = { .name = "REG7" },
181 [ACT8846_ID_REG8] = { .name = "REG8" },
182 [ACT8846_ID_REG9] = { .name = "REG9" },
183 [ACT8846_ID_REG10] = { .name = "REG10" },
184 [ACT8846_ID_REG11] = { .name = "REG11" },
185 [ACT8846_ID_REG12] = { .name = "REG12" },
186};
187
Wenyou Yang33036f42013-12-24 10:34:28 +0800188static struct of_regulator_match act8865_matches[] = {
189 [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"},
190 [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"},
191 [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"},
192 [ACT8865_ID_LDO1] = { .name = "LDO_REG1"},
193 [ACT8865_ID_LDO2] = { .name = "LDO_REG2"},
194 [ACT8865_ID_LDO3] = { .name = "LDO_REG3"},
195 [ACT8865_ID_LDO4] = { .name = "LDO_REG4"},
196};
197
198static int act8865_pdata_from_dt(struct device *dev,
199 struct device_node **of_node,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200200 struct act8865_platform_data *pdata,
Beniamino Galvani34e02472014-07-07 23:40:47 +0200201 unsigned long type)
Wenyou Yang33036f42013-12-24 10:34:28 +0800202{
Beniamino Galvani34e02472014-07-07 23:40:47 +0200203 int matched, i, num_matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800204 struct device_node *np;
205 struct act8865_regulator_data *regulator;
Beniamino Galvani34e02472014-07-07 23:40:47 +0200206 struct of_regulator_match *matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800207
Sachin Kamat6ea970a2014-02-14 17:19:54 +0530208 np = of_get_child_by_name(dev->of_node, "regulators");
Wenyou Yang33036f42013-12-24 10:34:28 +0800209 if (!np) {
210 dev_err(dev, "missing 'regulators' subnode in DT\n");
211 return -EINVAL;
212 }
213
Beniamino Galvani34e02472014-07-07 23:40:47 +0200214 switch (type) {
215 case ACT8846:
216 matches = act8846_matches;
217 num_matches = ARRAY_SIZE(act8846_matches);
218 break;
219 case ACT8865:
220 matches = act8865_matches;
221 num_matches = ARRAY_SIZE(act8865_matches);
222 break;
223 default:
224 dev_err(dev, "invalid device id %lu\n", type);
225 return -EINVAL;
226 }
227
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200228 matched = of_regulator_match(dev, np, matches, num_matches);
Sachin Kamat0079eb52014-02-17 14:33:29 +0530229 of_node_put(np);
Wenyou Yang33036f42013-12-24 10:34:28 +0800230 if (matched <= 0)
231 return matched;
232
233 pdata->regulators = devm_kzalloc(dev,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200234 sizeof(struct act8865_regulator_data) *
235 num_matches, GFP_KERNEL);
Sachin Kamat5ee77ef2014-02-18 16:11:08 +0530236 if (!pdata->regulators)
Wenyou Yang33036f42013-12-24 10:34:28 +0800237 return -ENOMEM;
Wenyou Yang33036f42013-12-24 10:34:28 +0800238
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200239 pdata->num_regulators = num_matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800240 regulator = pdata->regulators;
241
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200242 for (i = 0; i < num_matches; i++) {
Wenyou Yang33036f42013-12-24 10:34:28 +0800243 regulator->id = i;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200244 regulator->name = matches[i].name;
245 regulator->platform_data = matches[i].init_data;
246 of_node[i] = matches[i].of_node;
Wenyou Yang33036f42013-12-24 10:34:28 +0800247 regulator++;
248 }
249
250 return 0;
251}
252#else
253static inline int act8865_pdata_from_dt(struct device *dev,
254 struct device_node **of_node,
Beniamino Galvani34e02472014-07-07 23:40:47 +0200255 struct act8865_platform_data *pdata,
256 unsigned long type)
Wenyou Yang33036f42013-12-24 10:34:28 +0800257{
258 return 0;
259}
260#endif
261
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200262static struct regulator_init_data
263*act8865_get_init_data(int id, struct act8865_platform_data *pdata)
264{
265 int i;
266
267 if (!pdata)
268 return NULL;
269
270 for (i = 0; i < pdata->num_regulators; i++) {
271 if (pdata->regulators[i].id == id)
272 return pdata->regulators[i].platform_data;
273 }
274
275 return NULL;
276}
277
Wenyou Yang33036f42013-12-24 10:34:28 +0800278static int act8865_pmic_probe(struct i2c_client *client,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200279 const struct i2c_device_id *i2c_id)
Wenyou Yang33036f42013-12-24 10:34:28 +0800280{
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200281 static const struct regulator_desc *regulators;
282 struct act8865_platform_data pdata_of, *pdata;
Wenyou Yang33036f42013-12-24 10:34:28 +0800283 struct device *dev = &client->dev;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200284 struct device_node **of_node;
285 int i, ret, num_regulators;
Wenyou Yang33036f42013-12-24 10:34:28 +0800286 struct act8865 *act8865;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200287 unsigned long type;
288
289 pdata = dev_get_platdata(dev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800290
291 if (dev->of_node && !pdata) {
292 const struct of_device_id *id;
Wenyou Yang33036f42013-12-24 10:34:28 +0800293
294 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
295 if (!id)
296 return -ENODEV;
297
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200298 type = (unsigned long) id->data;
299 } else {
300 type = i2c_id->driver_data;
301 }
302
303 switch (type) {
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200304 case ACT8846:
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200305 regulators = act8846_regulators;
306 num_regulators = ARRAY_SIZE(act8846_regulators);
307 break;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200308 case ACT8865:
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200309 regulators = act8865_regulators;
310 num_regulators = ARRAY_SIZE(act8865_regulators);
311 break;
312 default:
313 dev_err(dev, "invalid device id %lu\n", type);
314 return -EINVAL;
315 }
316
317 of_node = devm_kzalloc(dev, sizeof(struct device_node *) *
318 num_regulators, GFP_KERNEL);
319 if (!of_node)
320 return -ENOMEM;
321
322 if (dev->of_node && !pdata) {
Beniamino Galvani34e02472014-07-07 23:40:47 +0200323 ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, type);
Wenyou Yang33036f42013-12-24 10:34:28 +0800324 if (ret < 0)
325 return ret;
326
327 pdata = &pdata_of;
328 }
329
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200330 if (pdata->num_regulators > num_regulators) {
331 dev_err(dev, "too many regulators: %d\n",
332 pdata->num_regulators);
Wenyou Yang33036f42013-12-24 10:34:28 +0800333 return -EINVAL;
334 }
335
Wenyou Yangf1de2c22013-12-26 14:52:43 +0800336 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
Wenyou Yang33036f42013-12-24 10:34:28 +0800337 if (!act8865)
338 return -ENOMEM;
339
Wenyou Yang33036f42013-12-24 10:34:28 +0800340 act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
341 if (IS_ERR(act8865->regmap)) {
Axel Lin40d3bc12014-06-30 15:32:09 +0800342 ret = PTR_ERR(act8865->regmap);
Wenyou Yang33036f42013-12-24 10:34:28 +0800343 dev_err(&client->dev, "Failed to allocate register map: %d\n",
Axel Lin40d3bc12014-06-30 15:32:09 +0800344 ret);
345 return ret;
Wenyou Yang33036f42013-12-24 10:34:28 +0800346 }
347
348 /* Finally register devices */
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200349 for (i = 0; i < num_regulators; i++) {
350 const struct regulator_desc *desc = &regulators[i];
351 struct regulator_config config = { };
352 struct regulator_dev *rdev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800353
354 config.dev = dev;
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200355 config.init_data = act8865_get_init_data(desc->id, pdata);
Wenyou Yang33036f42013-12-24 10:34:28 +0800356 config.of_node = of_node[i];
357 config.driver_data = act8865;
358 config.regmap = act8865->regmap;
359
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200360 rdev = devm_regulator_register(&client->dev, desc, &config);
Axel Linfb8eb452014-03-08 21:19:07 +0800361 if (IS_ERR(rdev)) {
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200362 dev_err(dev, "failed to register %s\n", desc->name);
Axel Linfb8eb452014-03-08 21:19:07 +0800363 return PTR_ERR(rdev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800364 }
365 }
366
367 i2c_set_clientdata(client, act8865);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200368 devm_kfree(dev, of_node);
Wenyou Yang33036f42013-12-24 10:34:28 +0800369
370 return 0;
371}
372
Wenyou Yang33036f42013-12-24 10:34:28 +0800373static const struct i2c_device_id act8865_ids[] = {
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200374 { .name = "act8846", .driver_data = ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200375 { .name = "act8865", .driver_data = ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800376 { },
377};
378MODULE_DEVICE_TABLE(i2c, act8865_ids);
379
380static struct i2c_driver act8865_pmic_driver = {
381 .driver = {
382 .name = "act8865",
383 .owner = THIS_MODULE,
384 },
385 .probe = act8865_pmic_probe,
Wenyou Yang33036f42013-12-24 10:34:28 +0800386 .id_table = act8865_ids,
387};
388
389module_i2c_driver(act8865_pmic_driver);
390
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200391MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
Wenyou Yang33036f42013-12-24 10:34:28 +0800392MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
393MODULE_LICENSE("GPL v2");