blob: 2ff73d72ca34aca4d332fa1c7967d35f24471de0 [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/*
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +000032 * ACT8600 Global Register Map.
33 */
34#define ACT8600_SYS_MODE 0x00
35#define ACT8600_SYS_CTRL 0x01
36#define ACT8600_DCDC1_VSET 0x10
37#define ACT8600_DCDC1_CTRL 0x12
38#define ACT8600_DCDC2_VSET 0x20
39#define ACT8600_DCDC2_CTRL 0x22
40#define ACT8600_DCDC3_VSET 0x30
41#define ACT8600_DCDC3_CTRL 0x32
42#define ACT8600_SUDCDC4_VSET 0x40
43#define ACT8600_SUDCDC4_CTRL 0x41
44#define ACT8600_LDO5_VSET 0x50
45#define ACT8600_LDO5_CTRL 0x51
46#define ACT8600_LDO6_VSET 0x60
47#define ACT8600_LDO6_CTRL 0x61
48#define ACT8600_LDO7_VSET 0x70
49#define ACT8600_LDO7_CTRL 0x71
50#define ACT8600_LDO8_VSET 0x80
51#define ACT8600_LDO8_CTRL 0x81
52#define ACT8600_LDO910_CTRL 0x91
53#define ACT8600_APCH0 0xA1
54#define ACT8600_APCH1 0xA8
55#define ACT8600_APCH2 0xA9
56#define ACT8600_APCH_STAT 0xAA
57#define ACT8600_OTG0 0xB0
58#define ACT8600_OTG1 0xB2
59
60/*
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020061 * ACT8846 Global Register Map.
62 */
63#define ACT8846_SYS0 0x00
64#define ACT8846_SYS1 0x01
65#define ACT8846_REG1_VSET 0x10
66#define ACT8846_REG1_CTRL 0x12
67#define ACT8846_REG2_VSET0 0x20
68#define ACT8846_REG2_VSET1 0x21
69#define ACT8846_REG2_CTRL 0x22
70#define ACT8846_REG3_VSET0 0x30
71#define ACT8846_REG3_VSET1 0x31
72#define ACT8846_REG3_CTRL 0x32
73#define ACT8846_REG4_VSET0 0x40
74#define ACT8846_REG4_VSET1 0x41
75#define ACT8846_REG4_CTRL 0x42
76#define ACT8846_REG5_VSET 0x50
77#define ACT8846_REG5_CTRL 0x51
78#define ACT8846_REG6_VSET 0x58
79#define ACT8846_REG6_CTRL 0x59
80#define ACT8846_REG7_VSET 0x60
81#define ACT8846_REG7_CTRL 0x61
82#define ACT8846_REG8_VSET 0x68
83#define ACT8846_REG8_CTRL 0x69
84#define ACT8846_REG9_VSET 0x70
85#define ACT8846_REG9_CTRL 0x71
86#define ACT8846_REG10_VSET 0x80
87#define ACT8846_REG10_CTRL 0x81
88#define ACT8846_REG11_VSET 0x90
89#define ACT8846_REG11_CTRL 0x91
90#define ACT8846_REG12_VSET 0xa0
91#define ACT8846_REG12_CTRL 0xa1
92#define ACT8846_REG13_CTRL 0xb1
Romain Perier2b17fa22014-10-14 06:31:10 +000093#define ACT8846_GLB_OFF_CTRL 0xc3
94#define ACT8846_OFF_SYSMASK 0x18
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020095
96/*
Wenyou Yang33036f42013-12-24 10:34:28 +080097 * ACT8865 Global Register Map.
98 */
99#define ACT8865_SYS_MODE 0x00
100#define ACT8865_SYS_CTRL 0x01
101#define ACT8865_DCDC1_VSET1 0x20
102#define ACT8865_DCDC1_VSET2 0x21
103#define ACT8865_DCDC1_CTRL 0x22
104#define ACT8865_DCDC2_VSET1 0x30
105#define ACT8865_DCDC2_VSET2 0x31
106#define ACT8865_DCDC2_CTRL 0x32
107#define ACT8865_DCDC3_VSET1 0x40
108#define ACT8865_DCDC3_VSET2 0x41
109#define ACT8865_DCDC3_CTRL 0x42
110#define ACT8865_LDO1_VSET 0x50
111#define ACT8865_LDO1_CTRL 0x51
112#define ACT8865_LDO2_VSET 0x54
113#define ACT8865_LDO2_CTRL 0x55
114#define ACT8865_LDO3_VSET 0x60
115#define ACT8865_LDO3_CTRL 0x61
116#define ACT8865_LDO4_VSET 0x64
117#define ACT8865_LDO4_CTRL 0x65
Romain Perier2b17fa22014-10-14 06:31:10 +0000118#define ACT8865_MSTROFF 0x20
Wenyou Yang33036f42013-12-24 10:34:28 +0800119
120/*
121 * Field Definitions.
122 */
123#define ACT8865_ENA 0x80 /* ON - [7] */
124#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
125
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000126
127#define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
128#define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
129
Wenyou Yang33036f42013-12-24 10:34:28 +0800130/*
131 * ACT8865 voltage number
132 */
133#define ACT8865_VOLTAGE_NUM 64
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000134#define ACT8600_SUDCDC_VOLTAGE_NUM 255
Wenyou Yang33036f42013-12-24 10:34:28 +0800135
136struct act8865 {
Wenyou Yang33036f42013-12-24 10:34:28 +0800137 struct regmap *regmap;
Romain Perier2b17fa22014-10-14 06:31:10 +0000138 int off_reg;
139 int off_mask;
Wenyou Yang33036f42013-12-24 10:34:28 +0800140};
141
142static const struct regmap_config act8865_regmap_config = {
143 .reg_bits = 8,
144 .val_bits = 8,
145};
146
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200147static const struct regulator_linear_range act8865_voltage_ranges[] = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800148 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
149 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
150 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
151};
152
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000153static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
154 REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
155 REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
156 REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
157 REGULATOR_LINEAR_RANGE(19000000, 191, 255, 400000),
158};
159
Wenyou Yang33036f42013-12-24 10:34:28 +0800160static struct regulator_ops act8865_ops = {
161 .list_voltage = regulator_list_voltage_linear_range,
162 .map_voltage = regulator_map_voltage_linear_range,
163 .get_voltage_sel = regulator_get_voltage_sel_regmap,
164 .set_voltage_sel = regulator_set_voltage_sel_regmap,
165 .enable = regulator_enable_regmap,
166 .disable = regulator_disable_regmap,
167 .is_enabled = regulator_is_enabled_regmap,
Wenyou Yang33036f42013-12-24 10:34:28 +0800168};
169
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000170static struct regulator_ops act8865_ldo_ops = {
171 .enable = regulator_enable_regmap,
172 .disable = regulator_disable_regmap,
173 .is_enabled = regulator_is_enabled_regmap,
174};
175
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100176#define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200177 [_family##_ID_##_id] = { \
178 .name = _name, \
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100179 .supply_name = _supply, \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200180 .id = _family##_ID_##_id, \
181 .type = REGULATOR_VOLTAGE, \
182 .ops = &act8865_ops, \
183 .n_voltages = ACT8865_VOLTAGE_NUM, \
184 .linear_ranges = act8865_voltage_ranges, \
185 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
186 .vsel_reg = _family##_##_id##_##_vsel_reg, \
187 .vsel_mask = ACT8865_VSEL_MASK, \
188 .enable_reg = _family##_##_id##_CTRL, \
189 .enable_mask = ACT8865_ENA, \
190 .owner = THIS_MODULE, \
191 }
192
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000193static const struct regulator_desc act8600_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100194 ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
195 ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
196 ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000197 {
198 .name = "SUDCDC_REG4",
199 .id = ACT8600_ID_SUDCDC4,
200 .ops = &act8865_ops,
201 .type = REGULATOR_VOLTAGE,
202 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
203 .linear_ranges = act8600_sudcdc_voltage_ranges,
204 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
205 .vsel_reg = ACT8600_SUDCDC4_VSET,
206 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
207 .enable_reg = ACT8600_SUDCDC4_CTRL,
208 .enable_mask = ACT8865_ENA,
209 .owner = THIS_MODULE,
210 },
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100211 ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
212 ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
213 ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
214 ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000215 {
216 .name = "LDO_REG9",
217 .id = ACT8600_ID_LDO9,
218 .ops = &act8865_ldo_ops,
219 .type = REGULATOR_VOLTAGE,
220 .n_voltages = 1,
221 .fixed_uV = 1800000,
222 .enable_reg = ACT8600_LDO910_CTRL,
223 .enable_mask = ACT8865_ENA,
224 .owner = THIS_MODULE,
225 },
226 {
227 .name = "LDO_REG10",
228 .id = ACT8600_ID_LDO10,
229 .ops = &act8865_ldo_ops,
230 .type = REGULATOR_VOLTAGE,
231 .n_voltages = 1,
232 .fixed_uV = 1200000,
233 .enable_reg = ACT8600_LDO910_CTRL,
234 .enable_mask = ACT8600_LDO10_ENA,
235 .owner = THIS_MODULE,
236 },
237};
238
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200239static const struct regulator_desc act8846_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100240 ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
241 ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
242 ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
243 ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
244 ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
245 ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
246 ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
247 ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
248 ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
249 ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
250 ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
251 ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200252};
253
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200254static const struct regulator_desc act8865_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100255 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
256 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
257 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
258 ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
259 ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
260 ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
261 ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
Wenyou Yang33036f42013-12-24 10:34:28 +0800262};
263
264#ifdef CONFIG_OF
265static const struct of_device_id act8865_dt_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000266 { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200267 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200268 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800269 { }
270};
271MODULE_DEVICE_TABLE(of, act8865_dt_ids);
272
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200273static struct of_regulator_match act8846_matches[] = {
274 [ACT8846_ID_REG1] = { .name = "REG1" },
275 [ACT8846_ID_REG2] = { .name = "REG2" },
276 [ACT8846_ID_REG3] = { .name = "REG3" },
277 [ACT8846_ID_REG4] = { .name = "REG4" },
278 [ACT8846_ID_REG5] = { .name = "REG5" },
279 [ACT8846_ID_REG6] = { .name = "REG6" },
280 [ACT8846_ID_REG7] = { .name = "REG7" },
281 [ACT8846_ID_REG8] = { .name = "REG8" },
282 [ACT8846_ID_REG9] = { .name = "REG9" },
283 [ACT8846_ID_REG10] = { .name = "REG10" },
284 [ACT8846_ID_REG11] = { .name = "REG11" },
285 [ACT8846_ID_REG12] = { .name = "REG12" },
286};
287
Wenyou Yang33036f42013-12-24 10:34:28 +0800288static struct of_regulator_match act8865_matches[] = {
289 [ACT8865_ID_DCDC1] = { .name = "DCDC_REG1"},
290 [ACT8865_ID_DCDC2] = { .name = "DCDC_REG2"},
291 [ACT8865_ID_DCDC3] = { .name = "DCDC_REG3"},
292 [ACT8865_ID_LDO1] = { .name = "LDO_REG1"},
293 [ACT8865_ID_LDO2] = { .name = "LDO_REG2"},
294 [ACT8865_ID_LDO3] = { .name = "LDO_REG3"},
295 [ACT8865_ID_LDO4] = { .name = "LDO_REG4"},
296};
297
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000298static struct of_regulator_match act8600_matches[] = {
299 [ACT8600_ID_DCDC1] = { .name = "DCDC_REG1"},
300 [ACT8600_ID_DCDC2] = { .name = "DCDC_REG2"},
301 [ACT8600_ID_DCDC3] = { .name = "DCDC_REG3"},
302 [ACT8600_ID_SUDCDC4] = { .name = "SUDCDC_REG4"},
303 [ACT8600_ID_LDO5] = { .name = "LDO_REG5"},
304 [ACT8600_ID_LDO6] = { .name = "LDO_REG6"},
305 [ACT8600_ID_LDO7] = { .name = "LDO_REG7"},
306 [ACT8600_ID_LDO8] = { .name = "LDO_REG8"},
307 [ACT8600_ID_LDO9] = { .name = "LDO_REG9"},
308 [ACT8600_ID_LDO10] = { .name = "LDO_REG10"},
309};
310
Wenyou Yang33036f42013-12-24 10:34:28 +0800311static int act8865_pdata_from_dt(struct device *dev,
312 struct device_node **of_node,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200313 struct act8865_platform_data *pdata,
Beniamino Galvani34e02472014-07-07 23:40:47 +0200314 unsigned long type)
Wenyou Yang33036f42013-12-24 10:34:28 +0800315{
Beniamino Galvani34e02472014-07-07 23:40:47 +0200316 int matched, i, num_matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800317 struct device_node *np;
318 struct act8865_regulator_data *regulator;
Beniamino Galvani34e02472014-07-07 23:40:47 +0200319 struct of_regulator_match *matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800320
Sachin Kamat6ea970a2014-02-14 17:19:54 +0530321 np = of_get_child_by_name(dev->of_node, "regulators");
Wenyou Yang33036f42013-12-24 10:34:28 +0800322 if (!np) {
323 dev_err(dev, "missing 'regulators' subnode in DT\n");
324 return -EINVAL;
325 }
326
Beniamino Galvani34e02472014-07-07 23:40:47 +0200327 switch (type) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000328 case ACT8600:
329 matches = act8600_matches;
330 num_matches = ARRAY_SIZE(act8600_matches);
331 break;
Beniamino Galvani34e02472014-07-07 23:40:47 +0200332 case ACT8846:
333 matches = act8846_matches;
334 num_matches = ARRAY_SIZE(act8846_matches);
335 break;
336 case ACT8865:
337 matches = act8865_matches;
338 num_matches = ARRAY_SIZE(act8865_matches);
339 break;
340 default:
341 dev_err(dev, "invalid device id %lu\n", type);
342 return -EINVAL;
343 }
344
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200345 matched = of_regulator_match(dev, np, matches, num_matches);
Sachin Kamat0079eb52014-02-17 14:33:29 +0530346 of_node_put(np);
Wenyou Yang33036f42013-12-24 10:34:28 +0800347 if (matched <= 0)
348 return matched;
349
350 pdata->regulators = devm_kzalloc(dev,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200351 sizeof(struct act8865_regulator_data) *
352 num_matches, GFP_KERNEL);
Sachin Kamat5ee77ef2014-02-18 16:11:08 +0530353 if (!pdata->regulators)
Wenyou Yang33036f42013-12-24 10:34:28 +0800354 return -ENOMEM;
Wenyou Yang33036f42013-12-24 10:34:28 +0800355
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200356 pdata->num_regulators = num_matches;
Wenyou Yang33036f42013-12-24 10:34:28 +0800357 regulator = pdata->regulators;
358
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200359 for (i = 0; i < num_matches; i++) {
Wenyou Yang33036f42013-12-24 10:34:28 +0800360 regulator->id = i;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200361 regulator->name = matches[i].name;
362 regulator->platform_data = matches[i].init_data;
363 of_node[i] = matches[i].of_node;
Wenyou Yang33036f42013-12-24 10:34:28 +0800364 regulator++;
365 }
366
367 return 0;
368}
369#else
370static inline int act8865_pdata_from_dt(struct device *dev,
371 struct device_node **of_node,
Beniamino Galvani34e02472014-07-07 23:40:47 +0200372 struct act8865_platform_data *pdata,
373 unsigned long type)
Wenyou Yang33036f42013-12-24 10:34:28 +0800374{
375 return 0;
376}
377#endif
378
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200379static struct regulator_init_data
380*act8865_get_init_data(int id, struct act8865_platform_data *pdata)
381{
382 int i;
383
384 if (!pdata)
385 return NULL;
386
387 for (i = 0; i < pdata->num_regulators; i++) {
388 if (pdata->regulators[i].id == id)
389 return pdata->regulators[i].platform_data;
390 }
391
392 return NULL;
393}
394
Romain Perier2b17fa22014-10-14 06:31:10 +0000395static struct i2c_client *act8865_i2c_client;
396static void act8865_power_off(void)
397{
398 struct act8865 *act8865;
399
400 act8865 = i2c_get_clientdata(act8865_i2c_client);
401 regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
402 while (1);
403}
404
Wenyou Yang33036f42013-12-24 10:34:28 +0800405static int act8865_pmic_probe(struct i2c_client *client,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200406 const struct i2c_device_id *i2c_id)
Wenyou Yang33036f42013-12-24 10:34:28 +0800407{
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200408 static const struct regulator_desc *regulators;
409 struct act8865_platform_data pdata_of, *pdata;
Wenyou Yang33036f42013-12-24 10:34:28 +0800410 struct device *dev = &client->dev;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200411 struct device_node **of_node;
412 int i, ret, num_regulators;
Wenyou Yang33036f42013-12-24 10:34:28 +0800413 struct act8865 *act8865;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200414 unsigned long type;
Romain Perier2b17fa22014-10-14 06:31:10 +0000415 int off_reg, off_mask;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200416
417 pdata = dev_get_platdata(dev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800418
419 if (dev->of_node && !pdata) {
420 const struct of_device_id *id;
Wenyou Yang33036f42013-12-24 10:34:28 +0800421
422 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
423 if (!id)
424 return -ENODEV;
425
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200426 type = (unsigned long) id->data;
427 } else {
428 type = i2c_id->driver_data;
429 }
430
431 switch (type) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000432 case ACT8600:
433 regulators = act8600_regulators;
434 num_regulators = ARRAY_SIZE(act8600_regulators);
435 off_reg = -1;
436 off_mask = -1;
437 break;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200438 case ACT8846:
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200439 regulators = act8846_regulators;
440 num_regulators = ARRAY_SIZE(act8846_regulators);
Romain Perier2b17fa22014-10-14 06:31:10 +0000441 off_reg = ACT8846_GLB_OFF_CTRL;
442 off_mask = ACT8846_OFF_SYSMASK;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200443 break;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200444 case ACT8865:
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200445 regulators = act8865_regulators;
446 num_regulators = ARRAY_SIZE(act8865_regulators);
Romain Perier2b17fa22014-10-14 06:31:10 +0000447 off_reg = ACT8865_SYS_CTRL;
448 off_mask = ACT8865_MSTROFF;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200449 break;
450 default:
451 dev_err(dev, "invalid device id %lu\n", type);
452 return -EINVAL;
453 }
454
455 of_node = devm_kzalloc(dev, sizeof(struct device_node *) *
456 num_regulators, GFP_KERNEL);
457 if (!of_node)
458 return -ENOMEM;
459
460 if (dev->of_node && !pdata) {
Beniamino Galvani34e02472014-07-07 23:40:47 +0200461 ret = act8865_pdata_from_dt(dev, of_node, &pdata_of, type);
Wenyou Yang33036f42013-12-24 10:34:28 +0800462 if (ret < 0)
463 return ret;
464
465 pdata = &pdata_of;
466 }
467
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200468 if (pdata->num_regulators > num_regulators) {
469 dev_err(dev, "too many regulators: %d\n",
470 pdata->num_regulators);
Wenyou Yang33036f42013-12-24 10:34:28 +0800471 return -EINVAL;
472 }
473
Wenyou Yangf1de2c22013-12-26 14:52:43 +0800474 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
Wenyou Yang33036f42013-12-24 10:34:28 +0800475 if (!act8865)
476 return -ENOMEM;
477
Wenyou Yang33036f42013-12-24 10:34:28 +0800478 act8865->regmap = devm_regmap_init_i2c(client, &act8865_regmap_config);
479 if (IS_ERR(act8865->regmap)) {
Axel Lin40d3bc12014-06-30 15:32:09 +0800480 ret = PTR_ERR(act8865->regmap);
Wenyou Yang33036f42013-12-24 10:34:28 +0800481 dev_err(&client->dev, "Failed to allocate register map: %d\n",
Axel Lin40d3bc12014-06-30 15:32:09 +0800482 ret);
483 return ret;
Wenyou Yang33036f42013-12-24 10:34:28 +0800484 }
485
Romain Perier8f731102014-11-25 12:28:25 +0000486 if (of_device_is_system_power_controller(dev->of_node)) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000487 if (!pm_power_off && (off_reg > 0)) {
Romain Perier2b17fa22014-10-14 06:31:10 +0000488 act8865_i2c_client = client;
489 act8865->off_reg = off_reg;
490 act8865->off_mask = off_mask;
491 pm_power_off = act8865_power_off;
492 } else {
493 dev_err(dev, "Failed to set poweroff capability, already defined\n");
494 }
495 }
496
Wenyou Yang33036f42013-12-24 10:34:28 +0800497 /* Finally register devices */
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200498 for (i = 0; i < num_regulators; i++) {
499 const struct regulator_desc *desc = &regulators[i];
500 struct regulator_config config = { };
501 struct regulator_dev *rdev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800502
503 config.dev = dev;
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200504 config.init_data = act8865_get_init_data(desc->id, pdata);
Wenyou Yang33036f42013-12-24 10:34:28 +0800505 config.of_node = of_node[i];
506 config.driver_data = act8865;
507 config.regmap = act8865->regmap;
508
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200509 rdev = devm_regulator_register(&client->dev, desc, &config);
Axel Linfb8eb452014-03-08 21:19:07 +0800510 if (IS_ERR(rdev)) {
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200511 dev_err(dev, "failed to register %s\n", desc->name);
Axel Linfb8eb452014-03-08 21:19:07 +0800512 return PTR_ERR(rdev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800513 }
514 }
515
516 i2c_set_clientdata(client, act8865);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200517 devm_kfree(dev, of_node);
Wenyou Yang33036f42013-12-24 10:34:28 +0800518
519 return 0;
520}
521
Wenyou Yang33036f42013-12-24 10:34:28 +0800522static const struct i2c_device_id act8865_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000523 { .name = "act8600", .driver_data = ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200524 { .name = "act8846", .driver_data = ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200525 { .name = "act8865", .driver_data = ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800526 { },
527};
528MODULE_DEVICE_TABLE(i2c, act8865_ids);
529
530static struct i2c_driver act8865_pmic_driver = {
531 .driver = {
532 .name = "act8865",
533 .owner = THIS_MODULE,
534 },
535 .probe = act8865_pmic_probe,
Wenyou Yang33036f42013-12-24 10:34:28 +0800536 .id_table = act8865_ids,
537};
538
539module_i2c_driver(act8865_pmic_driver);
540
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200541MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
Wenyou Yang33036f42013-12-24 10:34:28 +0800542MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
543MODULE_LICENSE("GPL v2");