]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/regulator/act8865-regulator.c
regulator: act8865: support regulator-pull-down property
[mirror_ubuntu-jammy-kernel.git] / drivers / regulator / act8865-regulator.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
33036f48 2/*
50a03e35
BG
3 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
4 *
5 * http://www.active-semi.com/products/power-management-units/act88xx/
33036f48
WY
6 *
7 * Copyright (C) 2013 Atmel Corporation
33036f48
WY
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/i2c.h>
13#include <linux/err.h>
14#include <linux/platform_device.h>
15#include <linux/regulator/driver.h>
16#include <linux/regulator/act8865.h>
17#include <linux/of.h>
18#include <linux/of_device.h>
2d09a79b 19#include <linux/power_supply.h>
33036f48
WY
20#include <linux/regulator/of_regulator.h>
21#include <linux/regmap.h>
22
df3a950e
ZLK
23/*
24 * ACT8600 Global Register Map.
25 */
26#define ACT8600_SYS_MODE 0x00
27#define ACT8600_SYS_CTRL 0x01
28#define ACT8600_DCDC1_VSET 0x10
29#define ACT8600_DCDC1_CTRL 0x12
30#define ACT8600_DCDC2_VSET 0x20
31#define ACT8600_DCDC2_CTRL 0x22
32#define ACT8600_DCDC3_VSET 0x30
33#define ACT8600_DCDC3_CTRL 0x32
34#define ACT8600_SUDCDC4_VSET 0x40
35#define ACT8600_SUDCDC4_CTRL 0x41
36#define ACT8600_LDO5_VSET 0x50
37#define ACT8600_LDO5_CTRL 0x51
38#define ACT8600_LDO6_VSET 0x60
39#define ACT8600_LDO6_CTRL 0x61
40#define ACT8600_LDO7_VSET 0x70
41#define ACT8600_LDO7_CTRL 0x71
42#define ACT8600_LDO8_VSET 0x80
43#define ACT8600_LDO8_CTRL 0x81
44#define ACT8600_LDO910_CTRL 0x91
45#define ACT8600_APCH0 0xA1
46#define ACT8600_APCH1 0xA8
47#define ACT8600_APCH2 0xA9
48#define ACT8600_APCH_STAT 0xAA
49#define ACT8600_OTG0 0xB0
50#define ACT8600_OTG1 0xB2
51
ac0c0ea8
BG
52/*
53 * ACT8846 Global Register Map.
54 */
55#define ACT8846_SYS0 0x00
56#define ACT8846_SYS1 0x01
57#define ACT8846_REG1_VSET 0x10
58#define ACT8846_REG1_CTRL 0x12
59#define ACT8846_REG2_VSET0 0x20
60#define ACT8846_REG2_VSET1 0x21
61#define ACT8846_REG2_CTRL 0x22
62#define ACT8846_REG3_VSET0 0x30
63#define ACT8846_REG3_VSET1 0x31
64#define ACT8846_REG3_CTRL 0x32
65#define ACT8846_REG4_VSET0 0x40
66#define ACT8846_REG4_VSET1 0x41
67#define ACT8846_REG4_CTRL 0x42
68#define ACT8846_REG5_VSET 0x50
69#define ACT8846_REG5_CTRL 0x51
70#define ACT8846_REG6_VSET 0x58
71#define ACT8846_REG6_CTRL 0x59
72#define ACT8846_REG7_VSET 0x60
73#define ACT8846_REG7_CTRL 0x61
74#define ACT8846_REG8_VSET 0x68
75#define ACT8846_REG8_CTRL 0x69
76#define ACT8846_REG9_VSET 0x70
77#define ACT8846_REG9_CTRL 0x71
78#define ACT8846_REG10_VSET 0x80
79#define ACT8846_REG10_CTRL 0x81
80#define ACT8846_REG11_VSET 0x90
81#define ACT8846_REG11_CTRL 0x91
82#define ACT8846_REG12_VSET 0xa0
83#define ACT8846_REG12_CTRL 0xa1
84#define ACT8846_REG13_CTRL 0xb1
2b17fa28
RP
85#define ACT8846_GLB_OFF_CTRL 0xc3
86#define ACT8846_OFF_SYSMASK 0x18
ac0c0ea8 87
33036f48
WY
88/*
89 * ACT8865 Global Register Map.
90 */
91#define ACT8865_SYS_MODE 0x00
92#define ACT8865_SYS_CTRL 0x01
93#define ACT8865_DCDC1_VSET1 0x20
94#define ACT8865_DCDC1_VSET2 0x21
95#define ACT8865_DCDC1_CTRL 0x22
96#define ACT8865_DCDC2_VSET1 0x30
97#define ACT8865_DCDC2_VSET2 0x31
98#define ACT8865_DCDC2_CTRL 0x32
99#define ACT8865_DCDC3_VSET1 0x40
100#define ACT8865_DCDC3_VSET2 0x41
101#define ACT8865_DCDC3_CTRL 0x42
102#define ACT8865_LDO1_VSET 0x50
103#define ACT8865_LDO1_CTRL 0x51
104#define ACT8865_LDO2_VSET 0x54
105#define ACT8865_LDO2_CTRL 0x55
106#define ACT8865_LDO3_VSET 0x60
107#define ACT8865_LDO3_CTRL 0x61
108#define ACT8865_LDO4_VSET 0x64
109#define ACT8865_LDO4_CTRL 0x65
2b17fa28 110#define ACT8865_MSTROFF 0x20
33036f48
WY
111
112/*
113 * Field Definitions.
114 */
115#define ACT8865_ENA 0x80 /* ON - [7] */
a5f888c5
MM
116#define ACT8865_DIS 0x40 /* DIS - [6] */
117
33036f48
WY
118#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
119
df3a950e
ZLK
120
121#define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
122#define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
123
2d09a79b
MH
124#define ACT8600_APCH_CHG_ACIN BIT(7)
125#define ACT8600_APCH_CHG_USB BIT(6)
126#define ACT8600_APCH_CSTATE0 BIT(5)
127#define ACT8600_APCH_CSTATE1 BIT(4)
128
33036f48
WY
129/*
130 * ACT8865 voltage number
131 */
132#define ACT8865_VOLTAGE_NUM 64
f01a7beb 133#define ACT8600_SUDCDC_VOLTAGE_NUM 256
33036f48
WY
134
135struct act8865 {
33036f48 136 struct regmap *regmap;
2b17fa28
RP
137 int off_reg;
138 int off_mask;
33036f48
WY
139};
140
c5c9c2df
MH
141static const struct regmap_range act8600_reg_ranges[] = {
142 regmap_reg_range(0x00, 0x01),
143 regmap_reg_range(0x10, 0x10),
144 regmap_reg_range(0x12, 0x12),
145 regmap_reg_range(0x20, 0x20),
146 regmap_reg_range(0x22, 0x22),
147 regmap_reg_range(0x30, 0x30),
148 regmap_reg_range(0x32, 0x32),
149 regmap_reg_range(0x40, 0x41),
150 regmap_reg_range(0x50, 0x51),
151 regmap_reg_range(0x60, 0x61),
152 regmap_reg_range(0x70, 0x71),
153 regmap_reg_range(0x80, 0x81),
154 regmap_reg_range(0x91, 0x91),
155 regmap_reg_range(0xA1, 0xA1),
156 regmap_reg_range(0xA8, 0xAA),
157 regmap_reg_range(0xB0, 0xB0),
158 regmap_reg_range(0xB2, 0xB2),
159 regmap_reg_range(0xC1, 0xC1),
160};
161
162static const struct regmap_range act8600_reg_ro_ranges[] = {
163 regmap_reg_range(0xAA, 0xAA),
164 regmap_reg_range(0xC1, 0xC1),
165};
166
167static const struct regmap_range act8600_reg_volatile_ranges[] = {
168 regmap_reg_range(0x00, 0x01),
169 regmap_reg_range(0x12, 0x12),
170 regmap_reg_range(0x22, 0x22),
171 regmap_reg_range(0x32, 0x32),
172 regmap_reg_range(0x41, 0x41),
173 regmap_reg_range(0x51, 0x51),
174 regmap_reg_range(0x61, 0x61),
175 regmap_reg_range(0x71, 0x71),
176 regmap_reg_range(0x81, 0x81),
177 regmap_reg_range(0xA8, 0xA8),
178 regmap_reg_range(0xAA, 0xAA),
179 regmap_reg_range(0xB0, 0xB0),
180 regmap_reg_range(0xC1, 0xC1),
181};
182
183static const struct regmap_access_table act8600_write_ranges_table = {
184 .yes_ranges = act8600_reg_ranges,
185 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
186 .no_ranges = act8600_reg_ro_ranges,
187 .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges),
188};
189
190static const struct regmap_access_table act8600_read_ranges_table = {
191 .yes_ranges = act8600_reg_ranges,
192 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
193};
194
195static const struct regmap_access_table act8600_volatile_ranges_table = {
196 .yes_ranges = act8600_reg_volatile_ranges,
197 .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges),
198};
199
200static const struct regmap_config act8600_regmap_config = {
201 .reg_bits = 8,
202 .val_bits = 8,
203 .max_register = 0xFF,
204 .wr_table = &act8600_write_ranges_table,
205 .rd_table = &act8600_read_ranges_table,
206 .volatile_table = &act8600_volatile_ranges_table,
207};
208
33036f48
WY
209static const struct regmap_config act8865_regmap_config = {
210 .reg_bits = 8,
211 .val_bits = 8,
212};
213
50a03e35 214static const struct regulator_linear_range act8865_voltage_ranges[] = {
33036f48
WY
215 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
216 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
217 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
218};
219
df3a950e
ZLK
220static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
221 REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
222 REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
223 REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
f01a7beb
AL
224 REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
225 REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
df3a950e
ZLK
226};
227
7cae255d 228static const struct regulator_ops act8865_ops = {
33036f48
WY
229 .list_voltage = regulator_list_voltage_linear_range,
230 .map_voltage = regulator_map_voltage_linear_range,
231 .get_voltage_sel = regulator_get_voltage_sel_regmap,
232 .set_voltage_sel = regulator_set_voltage_sel_regmap,
233 .enable = regulator_enable_regmap,
234 .disable = regulator_disable_regmap,
235 .is_enabled = regulator_is_enabled_regmap,
33036f48
WY
236};
237
a5f888c5
MM
238static const struct regulator_ops act8865_ldo_ops = {
239 .list_voltage = regulator_list_voltage_linear_range,
240 .map_voltage = regulator_map_voltage_linear_range,
241 .get_voltage_sel = regulator_get_voltage_sel_regmap,
242 .set_voltage_sel = regulator_set_voltage_sel_regmap,
243 .enable = regulator_enable_regmap,
244 .disable = regulator_disable_regmap,
245 .is_enabled = regulator_is_enabled_regmap,
246 .set_pull_down = regulator_set_pull_down_regmap,
247};
248
cd81a91d 249static const struct regulator_ops act8865_fixed_ldo_ops = {
df3a950e
ZLK
250 .enable = regulator_enable_regmap,
251 .disable = regulator_disable_regmap,
252 .is_enabled = regulator_is_enabled_regmap,
253};
254
a5f888c5 255#define ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, _ops) \
50a03e35
BG
256 [_family##_ID_##_id] = { \
257 .name = _name, \
253c9c79
AL
258 .of_match = of_match_ptr(_name), \
259 .regulators_node = of_match_ptr("regulators"), \
c8b263cc 260 .supply_name = _supply, \
50a03e35
BG
261 .id = _family##_ID_##_id, \
262 .type = REGULATOR_VOLTAGE, \
a5f888c5 263 .ops = _ops, \
50a03e35
BG
264 .n_voltages = ACT8865_VOLTAGE_NUM, \
265 .linear_ranges = act8865_voltage_ranges, \
266 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
267 .vsel_reg = _family##_##_id##_##_vsel_reg, \
268 .vsel_mask = ACT8865_VSEL_MASK, \
269 .enable_reg = _family##_##_id##_CTRL, \
270 .enable_mask = ACT8865_ENA, \
a5f888c5
MM
271 .pull_down_reg = _family##_##_id##_CTRL, \
272 .pull_down_mask = ACT8865_DIS, \
50a03e35
BG
273 .owner = THIS_MODULE, \
274 }
275
a5f888c5
MM
276#define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
277 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ops)
278
279#define ACT88xx_LDO(_name, _family, _id, _vsel_reg, _supply) \
280 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ldo_ops)
281
df3a950e 282static const struct regulator_desc act8600_regulators[] = {
c8b263cc
HS
283 ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
284 ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
285 ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
df3a950e
ZLK
286 {
287 .name = "SUDCDC_REG4",
253c9c79
AL
288 .of_match = of_match_ptr("SUDCDC_REG4"),
289 .regulators_node = of_match_ptr("regulators"),
df3a950e
ZLK
290 .id = ACT8600_ID_SUDCDC4,
291 .ops = &act8865_ops,
292 .type = REGULATOR_VOLTAGE,
293 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
294 .linear_ranges = act8600_sudcdc_voltage_ranges,
295 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
296 .vsel_reg = ACT8600_SUDCDC4_VSET,
297 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
298 .enable_reg = ACT8600_SUDCDC4_CTRL,
299 .enable_mask = ACT8865_ENA,
300 .owner = THIS_MODULE,
301 },
c8b263cc
HS
302 ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
303 ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
304 ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
305 ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
df3a950e
ZLK
306 {
307 .name = "LDO_REG9",
253c9c79
AL
308 .of_match = of_match_ptr("LDO_REG9"),
309 .regulators_node = of_match_ptr("regulators"),
df3a950e 310 .id = ACT8600_ID_LDO9,
cd81a91d 311 .ops = &act8865_fixed_ldo_ops,
df3a950e
ZLK
312 .type = REGULATOR_VOLTAGE,
313 .n_voltages = 1,
79b5d026 314 .fixed_uV = 3300000,
df3a950e
ZLK
315 .enable_reg = ACT8600_LDO910_CTRL,
316 .enable_mask = ACT8865_ENA,
317 .owner = THIS_MODULE,
318 },
319 {
320 .name = "LDO_REG10",
253c9c79
AL
321 .of_match = of_match_ptr("LDO_REG10"),
322 .regulators_node = of_match_ptr("regulators"),
df3a950e 323 .id = ACT8600_ID_LDO10,
cd81a91d 324 .ops = &act8865_fixed_ldo_ops,
df3a950e
ZLK
325 .type = REGULATOR_VOLTAGE,
326 .n_voltages = 1,
327 .fixed_uV = 1200000,
328 .enable_reg = ACT8600_LDO910_CTRL,
329 .enable_mask = ACT8600_LDO10_ENA,
330 .owner = THIS_MODULE,
331 },
332};
333
ac0c0ea8 334static const struct regulator_desc act8846_regulators[] = {
c8b263cc
HS
335 ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
336 ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
337 ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
338 ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
339 ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
340 ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
341 ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
342 ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
343 ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
344 ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
345 ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
346 ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
ac0c0ea8
BG
347};
348
50a03e35 349static const struct regulator_desc act8865_regulators[] = {
c8b263cc
HS
350 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
351 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
352 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
a5f888c5
MM
353 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
354 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
355 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
356 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
33036f48
WY
357};
358
c86dc03e
WY
359static const struct regulator_desc act8865_alt_regulators[] = {
360 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
361 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
362 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
a5f888c5
MM
363 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
364 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
365 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
366 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
c86dc03e
WY
367};
368
33036f48
WY
369#ifdef CONFIG_OF
370static const struct of_device_id act8865_dt_ids[] = {
df3a950e 371 { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
ac0c0ea8 372 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
50a03e35 373 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
33036f48
WY
374 { }
375};
376MODULE_DEVICE_TABLE(of, act8865_dt_ids);
33036f48
WY
377#endif
378
005e4685
MH
379static struct act8865_regulator_data *act8865_get_regulator_data(
380 int id, struct act8865_platform_data *pdata)
48a1e1b5
BG
381{
382 int i;
383
48a1e1b5
BG
384 for (i = 0; i < pdata->num_regulators; i++) {
385 if (pdata->regulators[i].id == id)
005e4685 386 return &pdata->regulators[i];
48a1e1b5
BG
387 }
388
389 return NULL;
390}
391
2b17fa28
RP
392static struct i2c_client *act8865_i2c_client;
393static void act8865_power_off(void)
394{
395 struct act8865 *act8865;
396
397 act8865 = i2c_get_clientdata(act8865_i2c_client);
398 regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
399 while (1);
400}
401
2d09a79b
MH
402static int act8600_charger_get_status(struct regmap *map)
403{
404 unsigned int val;
405 int ret;
406 u8 state0, state1;
407
408 ret = regmap_read(map, ACT8600_APCH_STAT, &val);
409 if (ret < 0)
410 return ret;
411
412 state0 = val & ACT8600_APCH_CSTATE0;
413 state1 = val & ACT8600_APCH_CSTATE1;
414
415 if (state0 && !state1)
416 return POWER_SUPPLY_STATUS_CHARGING;
417 if (!state0 && state1)
418 return POWER_SUPPLY_STATUS_NOT_CHARGING;
419 if (!state0 && !state1)
420 return POWER_SUPPLY_STATUS_DISCHARGING;
421
422 return POWER_SUPPLY_STATUS_UNKNOWN;
423}
424
425static int act8600_charger_get_property(struct power_supply *psy,
426 enum power_supply_property psp, union power_supply_propval *val)
427{
428 struct regmap *map = power_supply_get_drvdata(psy);
429 int ret;
430
431 switch (psp) {
432 case POWER_SUPPLY_PROP_STATUS:
433 ret = act8600_charger_get_status(map);
434 if (ret < 0)
435 return ret;
436
437 val->intval = ret;
438 break;
439 default:
440 return -EINVAL;
441 }
442
443 return 0;
444}
445
446static enum power_supply_property act8600_charger_properties[] = {
447 POWER_SUPPLY_PROP_STATUS,
448};
449
450static const struct power_supply_desc act8600_charger_desc = {
451 .name = "act8600-charger",
452 .type = POWER_SUPPLY_TYPE_BATTERY,
453 .properties = act8600_charger_properties,
454 .num_properties = ARRAY_SIZE(act8600_charger_properties),
455 .get_property = act8600_charger_get_property,
456};
457
458static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
459{
460 struct power_supply *charger;
461 struct power_supply_config cfg = {
462 .drv_data = regmap,
463 .of_node = dev->of_node,
464 };
465
466 charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
467
468 return IS_ERR(charger) ? PTR_ERR(charger) : 0;
469}
470
33036f48 471static int act8865_pmic_probe(struct i2c_client *client,
50a03e35 472 const struct i2c_device_id *i2c_id)
33036f48 473{
abea1bb0 474 const struct regulator_desc *regulators;
253c9c79 475 struct act8865_platform_data *pdata = NULL;
33036f48 476 struct device *dev = &client->dev;
50a03e35 477 int i, ret, num_regulators;
33036f48 478 struct act8865 *act8865;
c5c9c2df 479 const struct regmap_config *regmap_config;
50a03e35 480 unsigned long type;
2b17fa28 481 int off_reg, off_mask;
c86dc03e 482 int voltage_select = 0;
50a03e35 483
253c9c79 484 if (dev->of_node) {
33036f48 485 const struct of_device_id *id;
33036f48
WY
486
487 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
488 if (!id)
489 return -ENODEV;
490
50a03e35 491 type = (unsigned long) id->data;
c86dc03e
WY
492
493 voltage_select = !!of_get_property(dev->of_node,
494 "active-semi,vsel-high",
495 NULL);
50a03e35
BG
496 } else {
497 type = i2c_id->driver_data;
253c9c79 498 pdata = dev_get_platdata(dev);
50a03e35
BG
499 }
500
501 switch (type) {
df3a950e
ZLK
502 case ACT8600:
503 regulators = act8600_regulators;
504 num_regulators = ARRAY_SIZE(act8600_regulators);
c5c9c2df 505 regmap_config = &act8600_regmap_config;
df3a950e
ZLK
506 off_reg = -1;
507 off_mask = -1;
508 break;
ac0c0ea8 509 case ACT8846:
ac0c0ea8
BG
510 regulators = act8846_regulators;
511 num_regulators = ARRAY_SIZE(act8846_regulators);
c5c9c2df 512 regmap_config = &act8865_regmap_config;
2b17fa28
RP
513 off_reg = ACT8846_GLB_OFF_CTRL;
514 off_mask = ACT8846_OFF_SYSMASK;
ac0c0ea8 515 break;
50a03e35 516 case ACT8865:
c86dc03e
WY
517 if (voltage_select) {
518 regulators = act8865_alt_regulators;
519 num_regulators = ARRAY_SIZE(act8865_alt_regulators);
520 } else {
521 regulators = act8865_regulators;
522 num_regulators = ARRAY_SIZE(act8865_regulators);
523 }
c5c9c2df 524 regmap_config = &act8865_regmap_config;
2b17fa28
RP
525 off_reg = ACT8865_SYS_CTRL;
526 off_mask = ACT8865_MSTROFF;
50a03e35
BG
527 break;
528 default:
529 dev_err(dev, "invalid device id %lu\n", type);
530 return -EINVAL;
531 }
532
f1de2c2f 533 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
33036f48
WY
534 if (!act8865)
535 return -ENOMEM;
536
c5c9c2df 537 act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
33036f48 538 if (IS_ERR(act8865->regmap)) {
40d3bc19 539 ret = PTR_ERR(act8865->regmap);
de14ba67 540 dev_err(dev, "Failed to allocate register map: %d\n", ret);
40d3bc19 541 return ret;
33036f48
WY
542 }
543
8f73110f 544 if (of_device_is_system_power_controller(dev->of_node)) {
df3a950e 545 if (!pm_power_off && (off_reg > 0)) {
2b17fa28
RP
546 act8865_i2c_client = client;
547 act8865->off_reg = off_reg;
548 act8865->off_mask = off_mask;
549 pm_power_off = act8865_power_off;
550 } else {
551 dev_err(dev, "Failed to set poweroff capability, already defined\n");
552 }
553 }
554
33036f48 555 /* Finally register devices */
50a03e35
BG
556 for (i = 0; i < num_regulators; i++) {
557 const struct regulator_desc *desc = &regulators[i];
558 struct regulator_config config = { };
559 struct regulator_dev *rdev;
33036f48
WY
560
561 config.dev = dev;
33036f48
WY
562 config.driver_data = act8865;
563 config.regmap = act8865->regmap;
564
253c9c79
AL
565 if (pdata) {
566 struct act8865_regulator_data *rdata;
567
568 rdata = act8865_get_regulator_data(desc->id, pdata);
569 if (rdata) {
570 config.init_data = rdata->init_data;
571 config.of_node = rdata->of_node;
572 }
005e4685
MH
573 }
574
de14ba67 575 rdev = devm_regulator_register(dev, desc, &config);
fb8eb454 576 if (IS_ERR(rdev)) {
48a1e1b5 577 dev_err(dev, "failed to register %s\n", desc->name);
fb8eb454 578 return PTR_ERR(rdev);
33036f48
WY
579 }
580 }
581
2d09a79b
MH
582 if (type == ACT8600) {
583 ret = act8600_charger_probe(dev, act8865->regmap);
584 if (ret < 0) {
585 if (ret != -EPROBE_DEFER)
586 dev_err(dev, "Failed to probe charger");
587 return ret;
588 }
589 }
590
33036f48
WY
591 i2c_set_clientdata(client, act8865);
592
593 return 0;
594}
595
33036f48 596static const struct i2c_device_id act8865_ids[] = {
df3a950e 597 { .name = "act8600", .driver_data = ACT8600 },
ac0c0ea8 598 { .name = "act8846", .driver_data = ACT8846 },
50a03e35 599 { .name = "act8865", .driver_data = ACT8865 },
33036f48
WY
600 { },
601};
602MODULE_DEVICE_TABLE(i2c, act8865_ids);
603
604static struct i2c_driver act8865_pmic_driver = {
605 .driver = {
606 .name = "act8865",
33036f48
WY
607 },
608 .probe = act8865_pmic_probe,
33036f48
WY
609 .id_table = act8865_ids,
610};
611
612module_i2c_driver(act8865_pmic_driver);
613
50a03e35 614MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
33036f48
WY
615MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
616MODULE_LICENSE("GPL v2");