blob: b26eaf7580fc83fda883ee5d96e0be7e141a0d0b [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/*
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 {
Wenyou Yang33036f42013-12-24 10:34:28 +080066 struct regmap *regmap;
67};
68
69static const struct regmap_config act8865_regmap_config = {
70 .reg_bits = 8,
71 .val_bits = 8,
72};
73
Beniamino Galvani50a03e32014-07-05 15:20:54 +020074static const struct regulator_linear_range act8865_voltage_ranges[] = {
Wenyou Yang33036f42013-12-24 10:34:28 +080075 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,
Wenyou Yang33036f42013-12-24 10:34:28 +080088};
89
Beniamino Galvani50a03e32014-07-05 15:20:54 +020090#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),
Wenyou Yang33036f42013-12-24 10:34:28 +0800114};
115
116#ifdef CONFIG_OF
117static const struct of_device_id act8865_dt_ids[] = {
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200118 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800119 { }
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,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200135 struct act8865_platform_data *pdata,
136 struct of_regulator_match *matches,
137 int num_matches)
Wenyou Yang33036f42013-12-24 10:34:28 +0800138{
139 int matched, i;
140 struct device_node *np;
141 struct act8865_regulator_data *regulator;
142
Sachin Kamat6ea970a2014-02-14 17:19:54 +0530143 np = of_get_child_by_name(dev->of_node, "regulators");
Wenyou Yang33036f42013-12-24 10:34:28 +0800144 if (!np) {
145 dev_err(dev, "missing 'regulators' subnode in DT\n");
146 return -EINVAL;
147 }
148
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200149 matched = of_regulator_match(dev, np, matches, num_matches);
Sachin Kamat0079eb52014-02-17 14:33:29 +0530150 of_node_put(np);
Wenyou Yang33036f42013-12-24 10:34:28 +0800151 if (matched <= 0)
152 return matched;
153
154 pdata->regulators = devm_kzalloc(dev,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200155 sizeof(struct act8865_regulator_data) *
156 num_matches, GFP_KERNEL);
Sachin Kamat5ee77ef2014-02-18 16:11:08 +0530157 if (!pdata->regulators)
Wenyou Yang33036f42013-12-24 10:34:28 +0800158 return -ENOMEM;
Wenyou Yang33036f42013-12-24 10:34:28 +0800159
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200160 pdata->num_regulators = num_matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800161 regulator = pdata->regulators;
162
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200163 for (i = 0; i < num_matches; i++) {
Wenyou Yang33036f42013-12-24 10:34:28 +0800164 regulator->id = i;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200165 regulator->name = matches[i].name;
166 regulator->platform_data = matches[i].init_data;
167 of_node[i] = matches[i].of_node;
Wenyou Yang33036f42013-12-24 10:34:28 +0800168 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
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200182static 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
Wenyou Yang33036f42013-12-24 10:34:28 +0800198static int act8865_pmic_probe(struct i2c_client *client,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200199 const struct i2c_device_id *i2c_id)
Wenyou Yang33036f42013-12-24 10:34:28 +0800200{
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200201 static const struct regulator_desc *regulators;
202 struct act8865_platform_data pdata_of, *pdata;
203 struct of_regulator_match *matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800204 struct device *dev = &client->dev;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200205 struct device_node **of_node;
206 int i, ret, num_regulators;
Wenyou Yang33036f42013-12-24 10:34:28 +0800207 struct act8865 *act8865;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200208 unsigned long type;
209
210 pdata = dev_get_platdata(dev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800211
212 if (dev->of_node && !pdata) {
213 const struct of_device_id *id;
Wenyou Yang33036f42013-12-24 10:34:28 +0800214
215 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
216 if (!id)
217 return -ENODEV;
218
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200219 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);
Wenyou Yang33036f42013-12-24 10:34:28 +0800243 if (ret < 0)
244 return ret;
245
246 pdata = &pdata_of;
247 }
248
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200249 if (pdata->num_regulators > num_regulators) {
250 dev_err(dev, "too many regulators: %d\n",
251 pdata->num_regulators);
Wenyou Yang33036f42013-12-24 10:34:28 +0800252 return -EINVAL;
253 }
254
Wenyou Yangf1de2c22013-12-26 14:52:43 +0800255 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
Wenyou Yang33036f42013-12-24 10:34:28 +0800256 if (!act8865)
257 return -ENOMEM;
258
Wenyou Yang33036f42013-12-24 10:34:28 +0800259 act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
260 if (IS_ERR(act8865->regmap)) {
Axel Lin40d3bc12014-06-30 15:32:09 +0800261 ret = PTR_ERR(act8865->regmap);
Wenyou Yang33036f42013-12-24 10:34:28 +0800262 dev_err(&client->dev, "Failed to allocate register map: %d\n",
Axel Lin40d3bc12014-06-30 15:32:09 +0800263 ret);
264 return ret;
Wenyou Yang33036f42013-12-24 10:34:28 +0800265 }
266
267 /* Finally register devices */
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200268 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;
Wenyou Yang33036f42013-12-24 10:34:28 +0800272
273 config.dev = dev;
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200274 config.init_data = act8865_get_init_data(desc->id, pdata);
Wenyou Yang33036f42013-12-24 10:34:28 +0800275 config.of_node = of_node[i];
276 config.driver_data = act8865;
277 config.regmap = act8865->regmap;
278
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200279 rdev = devm_regulator_register(&client->dev, desc, &config);
Axel Linfb8eb452014-03-08 21:19:07 +0800280 if (IS_ERR(rdev)) {
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200281 dev_err(dev, "failed to register %s\n", desc->name);
Axel Linfb8eb452014-03-08 21:19:07 +0800282 return PTR_ERR(rdev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800283 }
284 }
285
286 i2c_set_clientdata(client, act8865);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200287 devm_kfree(dev, of_node);
Wenyou Yang33036f42013-12-24 10:34:28 +0800288
289 return 0;
290}
291
Wenyou Yang33036f42013-12-24 10:34:28 +0800292static const struct i2c_device_id act8865_ids[] = {
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200293 { .name = "act8865", .driver_data = ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800294 { },
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,
Wenyou Yang33036f42013-12-24 10:34:28 +0800304 .id_table = act8865_ids,
305};
306
307module_i2c_driver(act8865_pmic_driver);
308
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200309MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
Wenyou Yang33036f42013-12-24 10:34:28 +0800310MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
311MODULE_LICENSE("GPL v2");