blob: 19b9742c9eccaa21a5a43c9a90be60306a346fda [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wenyou Yang33036f42013-12-24 10:34:28 +08002/*
Beniamino Galvani50a03e32014-07-05 15:20:54 +02003 * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
4 *
5 * http://www.active-semi.com/products/power-management-units/act88xx/
Wenyou Yang33036f42013-12-24 10:34:28 +08006 *
7 * Copyright (C) 2013 Atmel Corporation
Wenyou Yang33036f42013-12-24 10:34:28 +08008 */
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>
Maarten ter Huurne2d09a792019-07-22 21:14:18 -040019#include <linux/power_supply.h>
Wenyou Yang33036f42013-12-24 10:34:28 +080020#include <linux/regulator/of_regulator.h>
21#include <linux/regmap.h>
Raag Jadav955741a2019-08-14 00:02:55 +053022#include <dt-bindings/regulator/active-semi,8865-regulator.h>
Wenyou Yang33036f42013-12-24 10:34:28 +080023
24/*
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +000025 * ACT8600 Global Register Map.
26 */
27#define ACT8600_SYS_MODE 0x00
28#define ACT8600_SYS_CTRL 0x01
29#define ACT8600_DCDC1_VSET 0x10
30#define ACT8600_DCDC1_CTRL 0x12
31#define ACT8600_DCDC2_VSET 0x20
32#define ACT8600_DCDC2_CTRL 0x22
33#define ACT8600_DCDC3_VSET 0x30
34#define ACT8600_DCDC3_CTRL 0x32
35#define ACT8600_SUDCDC4_VSET 0x40
36#define ACT8600_SUDCDC4_CTRL 0x41
37#define ACT8600_LDO5_VSET 0x50
38#define ACT8600_LDO5_CTRL 0x51
39#define ACT8600_LDO6_VSET 0x60
40#define ACT8600_LDO6_CTRL 0x61
41#define ACT8600_LDO7_VSET 0x70
42#define ACT8600_LDO7_CTRL 0x71
43#define ACT8600_LDO8_VSET 0x80
44#define ACT8600_LDO8_CTRL 0x81
45#define ACT8600_LDO910_CTRL 0x91
46#define ACT8600_APCH0 0xA1
47#define ACT8600_APCH1 0xA8
48#define ACT8600_APCH2 0xA9
49#define ACT8600_APCH_STAT 0xAA
50#define ACT8600_OTG0 0xB0
51#define ACT8600_OTG1 0xB2
52
53/*
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020054 * ACT8846 Global Register Map.
55 */
56#define ACT8846_SYS0 0x00
57#define ACT8846_SYS1 0x01
58#define ACT8846_REG1_VSET 0x10
59#define ACT8846_REG1_CTRL 0x12
60#define ACT8846_REG2_VSET0 0x20
61#define ACT8846_REG2_VSET1 0x21
62#define ACT8846_REG2_CTRL 0x22
63#define ACT8846_REG3_VSET0 0x30
64#define ACT8846_REG3_VSET1 0x31
65#define ACT8846_REG3_CTRL 0x32
66#define ACT8846_REG4_VSET0 0x40
67#define ACT8846_REG4_VSET1 0x41
68#define ACT8846_REG4_CTRL 0x42
69#define ACT8846_REG5_VSET 0x50
70#define ACT8846_REG5_CTRL 0x51
71#define ACT8846_REG6_VSET 0x58
72#define ACT8846_REG6_CTRL 0x59
73#define ACT8846_REG7_VSET 0x60
74#define ACT8846_REG7_CTRL 0x61
75#define ACT8846_REG8_VSET 0x68
76#define ACT8846_REG8_CTRL 0x69
77#define ACT8846_REG9_VSET 0x70
78#define ACT8846_REG9_CTRL 0x71
79#define ACT8846_REG10_VSET 0x80
80#define ACT8846_REG10_CTRL 0x81
81#define ACT8846_REG11_VSET 0x90
82#define ACT8846_REG11_CTRL 0x91
83#define ACT8846_REG12_VSET 0xa0
84#define ACT8846_REG12_CTRL 0xa1
85#define ACT8846_REG13_CTRL 0xb1
Romain Perier2b17fa22014-10-14 06:31:10 +000086#define ACT8846_GLB_OFF_CTRL 0xc3
87#define ACT8846_OFF_SYSMASK 0x18
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020088
89/*
Wenyou Yang33036f42013-12-24 10:34:28 +080090 * ACT8865 Global Register Map.
91 */
92#define ACT8865_SYS_MODE 0x00
93#define ACT8865_SYS_CTRL 0x01
Raag Jadav955741a2019-08-14 00:02:55 +053094#define ACT8865_SYS_UNLK_REGS 0x0b
Wenyou Yang33036f42013-12-24 10:34:28 +080095#define ACT8865_DCDC1_VSET1 0x20
96#define ACT8865_DCDC1_VSET2 0x21
97#define ACT8865_DCDC1_CTRL 0x22
Raag Jadav955741a2019-08-14 00:02:55 +053098#define ACT8865_DCDC1_SUS 0x24
Wenyou Yang33036f42013-12-24 10:34:28 +080099#define ACT8865_DCDC2_VSET1 0x30
100#define ACT8865_DCDC2_VSET2 0x31
101#define ACT8865_DCDC2_CTRL 0x32
Raag Jadav955741a2019-08-14 00:02:55 +0530102#define ACT8865_DCDC2_SUS 0x34
Wenyou Yang33036f42013-12-24 10:34:28 +0800103#define ACT8865_DCDC3_VSET1 0x40
104#define ACT8865_DCDC3_VSET2 0x41
105#define ACT8865_DCDC3_CTRL 0x42
Raag Jadav955741a2019-08-14 00:02:55 +0530106#define ACT8865_DCDC3_SUS 0x44
Wenyou Yang33036f42013-12-24 10:34:28 +0800107#define ACT8865_LDO1_VSET 0x50
108#define ACT8865_LDO1_CTRL 0x51
Raag Jadav955741a2019-08-14 00:02:55 +0530109#define ACT8865_LDO1_SUS 0x52
Wenyou Yang33036f42013-12-24 10:34:28 +0800110#define ACT8865_LDO2_VSET 0x54
111#define ACT8865_LDO2_CTRL 0x55
Raag Jadav955741a2019-08-14 00:02:55 +0530112#define ACT8865_LDO2_SUS 0x56
Wenyou Yang33036f42013-12-24 10:34:28 +0800113#define ACT8865_LDO3_VSET 0x60
114#define ACT8865_LDO3_CTRL 0x61
Raag Jadav955741a2019-08-14 00:02:55 +0530115#define ACT8865_LDO3_SUS 0x62
Wenyou Yang33036f42013-12-24 10:34:28 +0800116#define ACT8865_LDO4_VSET 0x64
117#define ACT8865_LDO4_CTRL 0x65
Raag Jadav955741a2019-08-14 00:02:55 +0530118#define ACT8865_LDO4_SUS 0x66
Romain Perier2b17fa22014-10-14 06:31:10 +0000119#define ACT8865_MSTROFF 0x20
Wenyou Yang33036f42013-12-24 10:34:28 +0800120
121/*
122 * Field Definitions.
123 */
124#define ACT8865_ENA 0x80 /* ON - [7] */
Michał Mirosława5f888c2019-07-23 18:32:07 +0200125#define ACT8865_DIS 0x40 /* DIS - [6] */
126
Wenyou Yang33036f42013-12-24 10:34:28 +0800127#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
128
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000129
130#define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
131#define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
132
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400133#define ACT8600_APCH_CHG_ACIN BIT(7)
134#define ACT8600_APCH_CHG_USB BIT(6)
135#define ACT8600_APCH_CSTATE0 BIT(5)
136#define ACT8600_APCH_CSTATE1 BIT(4)
137
Wenyou Yang33036f42013-12-24 10:34:28 +0800138/*
139 * ACT8865 voltage number
140 */
141#define ACT8865_VOLTAGE_NUM 64
Axel Linf01a7be2019-01-10 17:26:16 +0800142#define ACT8600_SUDCDC_VOLTAGE_NUM 256
Wenyou Yang33036f42013-12-24 10:34:28 +0800143
144struct act8865 {
Wenyou Yang33036f42013-12-24 10:34:28 +0800145 struct regmap *regmap;
Romain Perier2b17fa22014-10-14 06:31:10 +0000146 int off_reg;
147 int off_mask;
Wenyou Yang33036f42013-12-24 10:34:28 +0800148};
149
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100150static const struct regmap_range act8600_reg_ranges[] = {
151 regmap_reg_range(0x00, 0x01),
152 regmap_reg_range(0x10, 0x10),
153 regmap_reg_range(0x12, 0x12),
154 regmap_reg_range(0x20, 0x20),
155 regmap_reg_range(0x22, 0x22),
156 regmap_reg_range(0x30, 0x30),
157 regmap_reg_range(0x32, 0x32),
158 regmap_reg_range(0x40, 0x41),
159 regmap_reg_range(0x50, 0x51),
160 regmap_reg_range(0x60, 0x61),
161 regmap_reg_range(0x70, 0x71),
162 regmap_reg_range(0x80, 0x81),
163 regmap_reg_range(0x91, 0x91),
164 regmap_reg_range(0xA1, 0xA1),
165 regmap_reg_range(0xA8, 0xAA),
166 regmap_reg_range(0xB0, 0xB0),
167 regmap_reg_range(0xB2, 0xB2),
168 regmap_reg_range(0xC1, 0xC1),
169};
170
171static const struct regmap_range act8600_reg_ro_ranges[] = {
172 regmap_reg_range(0xAA, 0xAA),
173 regmap_reg_range(0xC1, 0xC1),
174};
175
176static const struct regmap_range act8600_reg_volatile_ranges[] = {
177 regmap_reg_range(0x00, 0x01),
178 regmap_reg_range(0x12, 0x12),
179 regmap_reg_range(0x22, 0x22),
180 regmap_reg_range(0x32, 0x32),
181 regmap_reg_range(0x41, 0x41),
182 regmap_reg_range(0x51, 0x51),
183 regmap_reg_range(0x61, 0x61),
184 regmap_reg_range(0x71, 0x71),
185 regmap_reg_range(0x81, 0x81),
186 regmap_reg_range(0xA8, 0xA8),
187 regmap_reg_range(0xAA, 0xAA),
188 regmap_reg_range(0xB0, 0xB0),
189 regmap_reg_range(0xC1, 0xC1),
190};
191
192static const struct regmap_access_table act8600_write_ranges_table = {
193 .yes_ranges = act8600_reg_ranges,
194 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
195 .no_ranges = act8600_reg_ro_ranges,
196 .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges),
197};
198
199static const struct regmap_access_table act8600_read_ranges_table = {
200 .yes_ranges = act8600_reg_ranges,
201 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
202};
203
204static const struct regmap_access_table act8600_volatile_ranges_table = {
205 .yes_ranges = act8600_reg_volatile_ranges,
206 .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges),
207};
208
209static const struct regmap_config act8600_regmap_config = {
210 .reg_bits = 8,
211 .val_bits = 8,
212 .max_register = 0xFF,
213 .wr_table = &act8600_write_ranges_table,
214 .rd_table = &act8600_read_ranges_table,
215 .volatile_table = &act8600_volatile_ranges_table,
216};
217
Wenyou Yang33036f42013-12-24 10:34:28 +0800218static const struct regmap_config act8865_regmap_config = {
219 .reg_bits = 8,
220 .val_bits = 8,
221};
222
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300223static const struct linear_range act8865_voltage_ranges[] = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800224 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
225 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
226 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
227};
228
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300229static const struct linear_range act8600_sudcdc_voltage_ranges[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000230 REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
231 REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
232 REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
Axel Linf01a7be2019-01-10 17:26:16 +0800233 REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
234 REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000235};
236
Raag Jadav955741a2019-08-14 00:02:55 +0530237static int act8865_set_suspend_state(struct regulator_dev *rdev, bool enable)
238{
239 struct regmap *regmap = rdev->regmap;
240 int id = rdev->desc->id, reg, val;
241
242 switch (id) {
243 case ACT8865_ID_DCDC1:
244 reg = ACT8865_DCDC1_SUS;
245 val = 0xa8;
246 break;
247 case ACT8865_ID_DCDC2:
248 reg = ACT8865_DCDC2_SUS;
249 val = 0xa8;
250 break;
251 case ACT8865_ID_DCDC3:
252 reg = ACT8865_DCDC3_SUS;
253 val = 0xa8;
254 break;
255 case ACT8865_ID_LDO1:
256 reg = ACT8865_LDO1_SUS;
257 val = 0xe8;
258 break;
259 case ACT8865_ID_LDO2:
260 reg = ACT8865_LDO2_SUS;
261 val = 0xe8;
262 break;
263 case ACT8865_ID_LDO3:
264 reg = ACT8865_LDO3_SUS;
265 val = 0xe8;
266 break;
267 case ACT8865_ID_LDO4:
268 reg = ACT8865_LDO4_SUS;
269 val = 0xe8;
270 break;
271 default:
272 return -EINVAL;
273 }
274
275 if (enable)
276 val |= BIT(4);
277
278 /*
279 * Ask the PMIC to enable/disable this output when entering hibernate
280 * mode.
281 */
282 return regmap_write(regmap, reg, val);
283}
284
285static int act8865_set_suspend_enable(struct regulator_dev *rdev)
286{
287 return act8865_set_suspend_state(rdev, true);
288}
289
290static int act8865_set_suspend_disable(struct regulator_dev *rdev)
291{
292 return act8865_set_suspend_state(rdev, false);
293}
294
295static unsigned int act8865_of_map_mode(unsigned int mode)
296{
297 switch (mode) {
298 case ACT8865_REGULATOR_MODE_FIXED:
299 return REGULATOR_MODE_FAST;
300 case ACT8865_REGULATOR_MODE_NORMAL:
301 return REGULATOR_MODE_NORMAL;
302 case ACT8865_REGULATOR_MODE_LOWPOWER:
303 return REGULATOR_MODE_STANDBY;
304 default:
305 return REGULATOR_MODE_INVALID;
306 }
307}
308
309static int act8865_set_mode(struct regulator_dev *rdev, unsigned int mode)
310{
311 struct regmap *regmap = rdev->regmap;
312 int id = rdev_get_id(rdev);
313 int reg, val = 0;
314
315 switch (id) {
316 case ACT8865_ID_DCDC1:
317 reg = ACT8865_DCDC1_CTRL;
318 break;
319 case ACT8865_ID_DCDC2:
320 reg = ACT8865_DCDC2_CTRL;
321 break;
322 case ACT8865_ID_DCDC3:
323 reg = ACT8865_DCDC3_CTRL;
324 break;
325 case ACT8865_ID_LDO1:
326 reg = ACT8865_LDO1_CTRL;
327 break;
328 case ACT8865_ID_LDO2:
329 reg = ACT8865_LDO2_CTRL;
330 break;
331 case ACT8865_ID_LDO3:
332 reg = ACT8865_LDO3_CTRL;
333 break;
334 case ACT8865_ID_LDO4:
335 reg = ACT8865_LDO4_CTRL;
336 break;
337 default:
338 return -EINVAL;
339 }
340
341 switch (mode) {
342 case REGULATOR_MODE_FAST:
343 case REGULATOR_MODE_NORMAL:
344 if (id <= ACT8865_ID_DCDC3)
345 val = BIT(5);
346 break;
347 case REGULATOR_MODE_STANDBY:
348 if (id > ACT8865_ID_DCDC3)
349 val = BIT(5);
350 break;
351 default:
352 return -EINVAL;
353 }
354
355 return regmap_update_bits(regmap, reg, BIT(5), val);
356}
357
358static unsigned int act8865_get_mode(struct regulator_dev *rdev)
359{
360 struct regmap *regmap = rdev->regmap;
361 int id = rdev_get_id(rdev);
362 int reg, ret, val = 0;
363
364 switch (id) {
365 case ACT8865_ID_DCDC1:
366 reg = ACT8865_DCDC1_CTRL;
367 break;
368 case ACT8865_ID_DCDC2:
369 reg = ACT8865_DCDC2_CTRL;
370 break;
371 case ACT8865_ID_DCDC3:
372 reg = ACT8865_DCDC3_CTRL;
373 break;
374 case ACT8865_ID_LDO1:
375 reg = ACT8865_LDO1_CTRL;
376 break;
377 case ACT8865_ID_LDO2:
378 reg = ACT8865_LDO2_CTRL;
379 break;
380 case ACT8865_ID_LDO3:
381 reg = ACT8865_LDO3_CTRL;
382 break;
383 case ACT8865_ID_LDO4:
384 reg = ACT8865_LDO4_CTRL;
385 break;
386 default:
387 return -EINVAL;
388 }
389
390 ret = regmap_read(regmap, reg, &val);
391 if (ret)
392 return ret;
393
394 if (id <= ACT8865_ID_DCDC3 && (val & BIT(5)))
395 return REGULATOR_MODE_FAST;
396 else if (id > ACT8865_ID_DCDC3 && !(val & BIT(5)))
397 return REGULATOR_MODE_NORMAL;
398 else
399 return REGULATOR_MODE_STANDBY;
400}
401
Axel Lin7cae2552019-03-22 14:38:20 +0800402static const struct regulator_ops act8865_ops = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800403 .list_voltage = regulator_list_voltage_linear_range,
404 .map_voltage = regulator_map_voltage_linear_range,
405 .get_voltage_sel = regulator_get_voltage_sel_regmap,
406 .set_voltage_sel = regulator_set_voltage_sel_regmap,
407 .enable = regulator_enable_regmap,
408 .disable = regulator_disable_regmap,
Raag Jadav955741a2019-08-14 00:02:55 +0530409 .set_mode = act8865_set_mode,
410 .get_mode = act8865_get_mode,
Wenyou Yang33036f42013-12-24 10:34:28 +0800411 .is_enabled = regulator_is_enabled_regmap,
Raag Jadav955741a2019-08-14 00:02:55 +0530412 .set_suspend_enable = act8865_set_suspend_enable,
413 .set_suspend_disable = act8865_set_suspend_disable,
Wenyou Yang33036f42013-12-24 10:34:28 +0800414};
415
Michał Mirosława5f888c2019-07-23 18:32:07 +0200416static const struct regulator_ops act8865_ldo_ops = {
417 .list_voltage = regulator_list_voltage_linear_range,
418 .map_voltage = regulator_map_voltage_linear_range,
419 .get_voltage_sel = regulator_get_voltage_sel_regmap,
420 .set_voltage_sel = regulator_set_voltage_sel_regmap,
421 .enable = regulator_enable_regmap,
422 .disable = regulator_disable_regmap,
Raag Jadav955741a2019-08-14 00:02:55 +0530423 .set_mode = act8865_set_mode,
424 .get_mode = act8865_get_mode,
Michał Mirosława5f888c2019-07-23 18:32:07 +0200425 .is_enabled = regulator_is_enabled_regmap,
Raag Jadav955741a2019-08-14 00:02:55 +0530426 .set_suspend_enable = act8865_set_suspend_enable,
427 .set_suspend_disable = act8865_set_suspend_disable,
Michał Mirosława5f888c2019-07-23 18:32:07 +0200428 .set_pull_down = regulator_set_pull_down_regmap,
429};
430
Michał Mirosławcd81a91d2019-07-23 18:32:06 +0200431static const struct regulator_ops act8865_fixed_ldo_ops = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000432 .enable = regulator_enable_regmap,
433 .disable = regulator_disable_regmap,
434 .is_enabled = regulator_is_enabled_regmap,
435};
436
Michał Mirosława5f888c2019-07-23 18:32:07 +0200437#define ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, _ops) \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200438 [_family##_ID_##_id] = { \
439 .name = _name, \
Axel Lin253c9c72019-03-22 14:38:19 +0800440 .of_match = of_match_ptr(_name), \
Raag Jadav955741a2019-08-14 00:02:55 +0530441 .of_map_mode = act8865_of_map_mode, \
Axel Lin253c9c72019-03-22 14:38:19 +0800442 .regulators_node = of_match_ptr("regulators"), \
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100443 .supply_name = _supply, \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200444 .id = _family##_ID_##_id, \
445 .type = REGULATOR_VOLTAGE, \
Michał Mirosława5f888c2019-07-23 18:32:07 +0200446 .ops = _ops, \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200447 .n_voltages = ACT8865_VOLTAGE_NUM, \
448 .linear_ranges = act8865_voltage_ranges, \
449 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
450 .vsel_reg = _family##_##_id##_##_vsel_reg, \
451 .vsel_mask = ACT8865_VSEL_MASK, \
452 .enable_reg = _family##_##_id##_CTRL, \
453 .enable_mask = ACT8865_ENA, \
Michał Mirosława5f888c2019-07-23 18:32:07 +0200454 .pull_down_reg = _family##_##_id##_CTRL, \
455 .pull_down_mask = ACT8865_DIS, \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200456 .owner = THIS_MODULE, \
457 }
458
Michał Mirosława5f888c2019-07-23 18:32:07 +0200459#define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
460 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ops)
461
462#define ACT88xx_LDO(_name, _family, _id, _vsel_reg, _supply) \
463 ACT88xx_REG_(_name, _family, _id, _vsel_reg, _supply, &act8865_ldo_ops)
464
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000465static const struct regulator_desc act8600_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100466 ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
467 ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
468 ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000469 {
470 .name = "SUDCDC_REG4",
Axel Lin253c9c72019-03-22 14:38:19 +0800471 .of_match = of_match_ptr("SUDCDC_REG4"),
472 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000473 .id = ACT8600_ID_SUDCDC4,
474 .ops = &act8865_ops,
475 .type = REGULATOR_VOLTAGE,
476 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
477 .linear_ranges = act8600_sudcdc_voltage_ranges,
478 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
479 .vsel_reg = ACT8600_SUDCDC4_VSET,
480 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
481 .enable_reg = ACT8600_SUDCDC4_CTRL,
482 .enable_mask = ACT8865_ENA,
483 .owner = THIS_MODULE,
484 },
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100485 ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
486 ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
487 ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
488 ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000489 {
490 .name = "LDO_REG9",
Axel Lin253c9c72019-03-22 14:38:19 +0800491 .of_match = of_match_ptr("LDO_REG9"),
492 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000493 .id = ACT8600_ID_LDO9,
Michał Mirosławcd81a91d2019-07-23 18:32:06 +0200494 .ops = &act8865_fixed_ldo_ops,
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000495 .type = REGULATOR_VOLTAGE,
496 .n_voltages = 1,
Maarten ter Huurne79b5d022016-02-28 16:53:28 +0100497 .fixed_uV = 3300000,
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000498 .enable_reg = ACT8600_LDO910_CTRL,
499 .enable_mask = ACT8865_ENA,
500 .owner = THIS_MODULE,
501 },
502 {
503 .name = "LDO_REG10",
Axel Lin253c9c72019-03-22 14:38:19 +0800504 .of_match = of_match_ptr("LDO_REG10"),
505 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000506 .id = ACT8600_ID_LDO10,
Michał Mirosławcd81a91d2019-07-23 18:32:06 +0200507 .ops = &act8865_fixed_ldo_ops,
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000508 .type = REGULATOR_VOLTAGE,
509 .n_voltages = 1,
510 .fixed_uV = 1200000,
511 .enable_reg = ACT8600_LDO910_CTRL,
512 .enable_mask = ACT8600_LDO10_ENA,
513 .owner = THIS_MODULE,
514 },
515};
516
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200517static const struct regulator_desc act8846_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100518 ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
519 ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
520 ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
521 ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
522 ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
523 ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
524 ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
525 ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
526 ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
527 ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
528 ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
529 ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200530};
531
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200532static const struct regulator_desc act8865_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100533 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
534 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
535 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
Michał Mirosława5f888c2019-07-23 18:32:07 +0200536 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
537 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
538 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
539 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
Wenyou Yang33036f42013-12-24 10:34:28 +0800540};
541
Wenyou Yangc86dc032015-09-30 15:25:49 +0800542static const struct regulator_desc act8865_alt_regulators[] = {
543 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
544 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
545 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
Michał Mirosława5f888c2019-07-23 18:32:07 +0200546 ACT88xx_LDO("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
547 ACT88xx_LDO("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
548 ACT88xx_LDO("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
549 ACT88xx_LDO("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
Wenyou Yangc86dc032015-09-30 15:25:49 +0800550};
551
Wenyou Yang33036f42013-12-24 10:34:28 +0800552#ifdef CONFIG_OF
553static const struct of_device_id act8865_dt_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000554 { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200555 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200556 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800557 { }
558};
559MODULE_DEVICE_TABLE(of, act8865_dt_ids);
Wenyou Yang33036f42013-12-24 10:34:28 +0800560#endif
561
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100562static struct act8865_regulator_data *act8865_get_regulator_data(
563 int id, struct act8865_platform_data *pdata)
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200564{
565 int i;
566
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200567 for (i = 0; i < pdata->num_regulators; i++) {
568 if (pdata->regulators[i].id == id)
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100569 return &pdata->regulators[i];
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200570 }
571
572 return NULL;
573}
574
Romain Perier2b17fa22014-10-14 06:31:10 +0000575static struct i2c_client *act8865_i2c_client;
576static void act8865_power_off(void)
577{
578 struct act8865 *act8865;
579
580 act8865 = i2c_get_clientdata(act8865_i2c_client);
581 regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
582 while (1);
583}
584
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400585static int act8600_charger_get_status(struct regmap *map)
586{
587 unsigned int val;
588 int ret;
589 u8 state0, state1;
590
591 ret = regmap_read(map, ACT8600_APCH_STAT, &val);
592 if (ret < 0)
593 return ret;
594
595 state0 = val & ACT8600_APCH_CSTATE0;
596 state1 = val & ACT8600_APCH_CSTATE1;
597
598 if (state0 && !state1)
599 return POWER_SUPPLY_STATUS_CHARGING;
600 if (!state0 && state1)
601 return POWER_SUPPLY_STATUS_NOT_CHARGING;
602 if (!state0 && !state1)
603 return POWER_SUPPLY_STATUS_DISCHARGING;
604
605 return POWER_SUPPLY_STATUS_UNKNOWN;
606}
607
608static int act8600_charger_get_property(struct power_supply *psy,
609 enum power_supply_property psp, union power_supply_propval *val)
610{
611 struct regmap *map = power_supply_get_drvdata(psy);
612 int ret;
613
614 switch (psp) {
615 case POWER_SUPPLY_PROP_STATUS:
616 ret = act8600_charger_get_status(map);
617 if (ret < 0)
618 return ret;
619
620 val->intval = ret;
621 break;
622 default:
623 return -EINVAL;
624 }
625
626 return 0;
627}
628
629static enum power_supply_property act8600_charger_properties[] = {
630 POWER_SUPPLY_PROP_STATUS,
631};
632
633static const struct power_supply_desc act8600_charger_desc = {
634 .name = "act8600-charger",
635 .type = POWER_SUPPLY_TYPE_BATTERY,
636 .properties = act8600_charger_properties,
637 .num_properties = ARRAY_SIZE(act8600_charger_properties),
638 .get_property = act8600_charger_get_property,
639};
640
641static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
642{
643 struct power_supply *charger;
644 struct power_supply_config cfg = {
645 .drv_data = regmap,
646 .of_node = dev->of_node,
647 };
648
649 charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
650
kbuild test robot1889c6e2019-07-24 17:22:36 +0800651 return PTR_ERR_OR_ZERO(charger);
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400652}
653
Wenyou Yang33036f42013-12-24 10:34:28 +0800654static int act8865_pmic_probe(struct i2c_client *client,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200655 const struct i2c_device_id *i2c_id)
Wenyou Yang33036f42013-12-24 10:34:28 +0800656{
Maarten ter Huurneabea1bb2016-02-28 16:53:25 +0100657 const struct regulator_desc *regulators;
Axel Lin253c9c72019-03-22 14:38:19 +0800658 struct act8865_platform_data *pdata = NULL;
Wenyou Yang33036f42013-12-24 10:34:28 +0800659 struct device *dev = &client->dev;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200660 int i, ret, num_regulators;
Wenyou Yang33036f42013-12-24 10:34:28 +0800661 struct act8865 *act8865;
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100662 const struct regmap_config *regmap_config;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200663 unsigned long type;
Romain Perier2b17fa22014-10-14 06:31:10 +0000664 int off_reg, off_mask;
Wenyou Yangc86dc032015-09-30 15:25:49 +0800665 int voltage_select = 0;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200666
Axel Lin253c9c72019-03-22 14:38:19 +0800667 if (dev->of_node) {
Wenyou Yang33036f42013-12-24 10:34:28 +0800668 const struct of_device_id *id;
Wenyou Yang33036f42013-12-24 10:34:28 +0800669
670 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
671 if (!id)
672 return -ENODEV;
673
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200674 type = (unsigned long) id->data;
Wenyou Yangc86dc032015-09-30 15:25:49 +0800675
676 voltage_select = !!of_get_property(dev->of_node,
677 "active-semi,vsel-high",
678 NULL);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200679 } else {
680 type = i2c_id->driver_data;
Axel Lin253c9c72019-03-22 14:38:19 +0800681 pdata = dev_get_platdata(dev);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200682 }
683
684 switch (type) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000685 case ACT8600:
686 regulators = act8600_regulators;
687 num_regulators = ARRAY_SIZE(act8600_regulators);
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100688 regmap_config = &act8600_regmap_config;
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000689 off_reg = -1;
690 off_mask = -1;
691 break;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200692 case ACT8846:
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200693 regulators = act8846_regulators;
694 num_regulators = ARRAY_SIZE(act8846_regulators);
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100695 regmap_config = &act8865_regmap_config;
Romain Perier2b17fa22014-10-14 06:31:10 +0000696 off_reg = ACT8846_GLB_OFF_CTRL;
697 off_mask = ACT8846_OFF_SYSMASK;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200698 break;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200699 case ACT8865:
Wenyou Yangc86dc032015-09-30 15:25:49 +0800700 if (voltage_select) {
701 regulators = act8865_alt_regulators;
702 num_regulators = ARRAY_SIZE(act8865_alt_regulators);
703 } else {
704 regulators = act8865_regulators;
705 num_regulators = ARRAY_SIZE(act8865_regulators);
706 }
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100707 regmap_config = &act8865_regmap_config;
Romain Perier2b17fa22014-10-14 06:31:10 +0000708 off_reg = ACT8865_SYS_CTRL;
709 off_mask = ACT8865_MSTROFF;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200710 break;
711 default:
712 dev_err(dev, "invalid device id %lu\n", type);
713 return -EINVAL;
714 }
715
Wenyou Yangf1de2c22013-12-26 14:52:43 +0800716 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
Wenyou Yang33036f42013-12-24 10:34:28 +0800717 if (!act8865)
718 return -ENOMEM;
719
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100720 act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
Wenyou Yang33036f42013-12-24 10:34:28 +0800721 if (IS_ERR(act8865->regmap)) {
Axel Lin40d3bc12014-06-30 15:32:09 +0800722 ret = PTR_ERR(act8865->regmap);
Maarten ter Huurnede14ba62016-03-17 15:05:05 +0100723 dev_err(dev, "Failed to allocate register map: %d\n", ret);
Axel Lin40d3bc12014-06-30 15:32:09 +0800724 return ret;
Wenyou Yang33036f42013-12-24 10:34:28 +0800725 }
726
Romain Perier8f731102014-11-25 12:28:25 +0000727 if (of_device_is_system_power_controller(dev->of_node)) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000728 if (!pm_power_off && (off_reg > 0)) {
Romain Perier2b17fa22014-10-14 06:31:10 +0000729 act8865_i2c_client = client;
730 act8865->off_reg = off_reg;
731 act8865->off_mask = off_mask;
732 pm_power_off = act8865_power_off;
733 } else {
734 dev_err(dev, "Failed to set poweroff capability, already defined\n");
735 }
736 }
737
Wenyou Yang33036f42013-12-24 10:34:28 +0800738 /* Finally register devices */
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200739 for (i = 0; i < num_regulators; i++) {
740 const struct regulator_desc *desc = &regulators[i];
741 struct regulator_config config = { };
742 struct regulator_dev *rdev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800743
744 config.dev = dev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800745 config.driver_data = act8865;
746 config.regmap = act8865->regmap;
747
Axel Lin253c9c72019-03-22 14:38:19 +0800748 if (pdata) {
749 struct act8865_regulator_data *rdata;
750
751 rdata = act8865_get_regulator_data(desc->id, pdata);
752 if (rdata) {
753 config.init_data = rdata->init_data;
754 config.of_node = rdata->of_node;
755 }
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100756 }
757
Maarten ter Huurnede14ba62016-03-17 15:05:05 +0100758 rdev = devm_regulator_register(dev, desc, &config);
Axel Linfb8eb452014-03-08 21:19:07 +0800759 if (IS_ERR(rdev)) {
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200760 dev_err(dev, "failed to register %s\n", desc->name);
Axel Linfb8eb452014-03-08 21:19:07 +0800761 return PTR_ERR(rdev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800762 }
763 }
764
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400765 if (type == ACT8600) {
766 ret = act8600_charger_probe(dev, act8865->regmap);
767 if (ret < 0) {
768 if (ret != -EPROBE_DEFER)
769 dev_err(dev, "Failed to probe charger");
770 return ret;
771 }
772 }
773
Wenyou Yang33036f42013-12-24 10:34:28 +0800774 i2c_set_clientdata(client, act8865);
775
Raag Jadav955741a2019-08-14 00:02:55 +0530776 /* Unlock expert registers for ACT8865. */
777 return type != ACT8865 ? 0 : regmap_write(act8865->regmap,
778 ACT8865_SYS_UNLK_REGS, 0xef);
Wenyou Yang33036f42013-12-24 10:34:28 +0800779}
780
Wenyou Yang33036f42013-12-24 10:34:28 +0800781static const struct i2c_device_id act8865_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000782 { .name = "act8600", .driver_data = ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200783 { .name = "act8846", .driver_data = ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200784 { .name = "act8865", .driver_data = ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800785 { },
786};
787MODULE_DEVICE_TABLE(i2c, act8865_ids);
788
789static struct i2c_driver act8865_pmic_driver = {
790 .driver = {
791 .name = "act8865",
Wenyou Yang33036f42013-12-24 10:34:28 +0800792 },
793 .probe = act8865_pmic_probe,
Wenyou Yang33036f42013-12-24 10:34:28 +0800794 .id_table = act8865_ids,
795};
796
797module_i2c_driver(act8865_pmic_driver);
798
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200799MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
Wenyou Yang33036f42013-12-24 10:34:28 +0800800MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
801MODULE_LICENSE("GPL v2");