blob: e63285de485dcec508e3cc80e2d0076e49851c85 [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>
22
23/*
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +000024 * 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
52/*
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020053 * 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
Romain Perier2b17fa22014-10-14 06:31:10 +000085#define ACT8846_GLB_OFF_CTRL 0xc3
86#define ACT8846_OFF_SYSMASK 0x18
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +020087
88/*
Wenyou Yang33036f42013-12-24 10:34:28 +080089 * 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
Romain Perier2b17fa22014-10-14 06:31:10 +0000110#define ACT8865_MSTROFF 0x20
Wenyou Yang33036f42013-12-24 10:34:28 +0800111
112/*
113 * Field Definitions.
114 */
115#define ACT8865_ENA 0x80 /* ON - [7] */
116#define ACT8865_VSEL_MASK 0x3F /* VSET - [5:0] */
117
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000118
119#define ACT8600_LDO10_ENA 0x40 /* ON - [6] */
120#define ACT8600_SUDCDC_VSEL_MASK 0xFF /* SUDCDC VSET - [7:0] */
121
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400122#define ACT8600_APCH_CHG_ACIN BIT(7)
123#define ACT8600_APCH_CHG_USB BIT(6)
124#define ACT8600_APCH_CSTATE0 BIT(5)
125#define ACT8600_APCH_CSTATE1 BIT(4)
126
Wenyou Yang33036f42013-12-24 10:34:28 +0800127/*
128 * ACT8865 voltage number
129 */
130#define ACT8865_VOLTAGE_NUM 64
Axel Linf01a7be2019-01-10 17:26:16 +0800131#define ACT8600_SUDCDC_VOLTAGE_NUM 256
Wenyou Yang33036f42013-12-24 10:34:28 +0800132
133struct act8865 {
Wenyou Yang33036f42013-12-24 10:34:28 +0800134 struct regmap *regmap;
Romain Perier2b17fa22014-10-14 06:31:10 +0000135 int off_reg;
136 int off_mask;
Wenyou Yang33036f42013-12-24 10:34:28 +0800137};
138
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100139static const struct regmap_range act8600_reg_ranges[] = {
140 regmap_reg_range(0x00, 0x01),
141 regmap_reg_range(0x10, 0x10),
142 regmap_reg_range(0x12, 0x12),
143 regmap_reg_range(0x20, 0x20),
144 regmap_reg_range(0x22, 0x22),
145 regmap_reg_range(0x30, 0x30),
146 regmap_reg_range(0x32, 0x32),
147 regmap_reg_range(0x40, 0x41),
148 regmap_reg_range(0x50, 0x51),
149 regmap_reg_range(0x60, 0x61),
150 regmap_reg_range(0x70, 0x71),
151 regmap_reg_range(0x80, 0x81),
152 regmap_reg_range(0x91, 0x91),
153 regmap_reg_range(0xA1, 0xA1),
154 regmap_reg_range(0xA8, 0xAA),
155 regmap_reg_range(0xB0, 0xB0),
156 regmap_reg_range(0xB2, 0xB2),
157 regmap_reg_range(0xC1, 0xC1),
158};
159
160static const struct regmap_range act8600_reg_ro_ranges[] = {
161 regmap_reg_range(0xAA, 0xAA),
162 regmap_reg_range(0xC1, 0xC1),
163};
164
165static const struct regmap_range act8600_reg_volatile_ranges[] = {
166 regmap_reg_range(0x00, 0x01),
167 regmap_reg_range(0x12, 0x12),
168 regmap_reg_range(0x22, 0x22),
169 regmap_reg_range(0x32, 0x32),
170 regmap_reg_range(0x41, 0x41),
171 regmap_reg_range(0x51, 0x51),
172 regmap_reg_range(0x61, 0x61),
173 regmap_reg_range(0x71, 0x71),
174 regmap_reg_range(0x81, 0x81),
175 regmap_reg_range(0xA8, 0xA8),
176 regmap_reg_range(0xAA, 0xAA),
177 regmap_reg_range(0xB0, 0xB0),
178 regmap_reg_range(0xC1, 0xC1),
179};
180
181static const struct regmap_access_table act8600_write_ranges_table = {
182 .yes_ranges = act8600_reg_ranges,
183 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
184 .no_ranges = act8600_reg_ro_ranges,
185 .n_no_ranges = ARRAY_SIZE(act8600_reg_ro_ranges),
186};
187
188static const struct regmap_access_table act8600_read_ranges_table = {
189 .yes_ranges = act8600_reg_ranges,
190 .n_yes_ranges = ARRAY_SIZE(act8600_reg_ranges),
191};
192
193static const struct regmap_access_table act8600_volatile_ranges_table = {
194 .yes_ranges = act8600_reg_volatile_ranges,
195 .n_yes_ranges = ARRAY_SIZE(act8600_reg_volatile_ranges),
196};
197
198static const struct regmap_config act8600_regmap_config = {
199 .reg_bits = 8,
200 .val_bits = 8,
201 .max_register = 0xFF,
202 .wr_table = &act8600_write_ranges_table,
203 .rd_table = &act8600_read_ranges_table,
204 .volatile_table = &act8600_volatile_ranges_table,
205};
206
Wenyou Yang33036f42013-12-24 10:34:28 +0800207static const struct regmap_config act8865_regmap_config = {
208 .reg_bits = 8,
209 .val_bits = 8,
210};
211
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200212static const struct regulator_linear_range act8865_voltage_ranges[] = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800213 REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
214 REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
215 REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
216};
217
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000218static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
219 REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
220 REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
221 REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
Axel Linf01a7be2019-01-10 17:26:16 +0800222 REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
223 REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000224};
225
Axel Lin7cae2552019-03-22 14:38:20 +0800226static const struct regulator_ops act8865_ops = {
Wenyou Yang33036f42013-12-24 10:34:28 +0800227 .list_voltage = regulator_list_voltage_linear_range,
228 .map_voltage = regulator_map_voltage_linear_range,
229 .get_voltage_sel = regulator_get_voltage_sel_regmap,
230 .set_voltage_sel = regulator_set_voltage_sel_regmap,
231 .enable = regulator_enable_regmap,
232 .disable = regulator_disable_regmap,
233 .is_enabled = regulator_is_enabled_regmap,
Wenyou Yang33036f42013-12-24 10:34:28 +0800234};
235
Axel Lin7cae2552019-03-22 14:38:20 +0800236static const struct regulator_ops act8865_ldo_ops = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000237 .enable = regulator_enable_regmap,
238 .disable = regulator_disable_regmap,
239 .is_enabled = regulator_is_enabled_regmap,
240};
241
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100242#define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply) \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200243 [_family##_ID_##_id] = { \
244 .name = _name, \
Axel Lin253c9c72019-03-22 14:38:19 +0800245 .of_match = of_match_ptr(_name), \
246 .regulators_node = of_match_ptr("regulators"), \
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100247 .supply_name = _supply, \
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200248 .id = _family##_ID_##_id, \
249 .type = REGULATOR_VOLTAGE, \
250 .ops = &act8865_ops, \
251 .n_voltages = ACT8865_VOLTAGE_NUM, \
252 .linear_ranges = act8865_voltage_ranges, \
253 .n_linear_ranges = ARRAY_SIZE(act8865_voltage_ranges), \
254 .vsel_reg = _family##_##_id##_##_vsel_reg, \
255 .vsel_mask = ACT8865_VSEL_MASK, \
256 .enable_reg = _family##_##_id##_CTRL, \
257 .enable_mask = ACT8865_ENA, \
258 .owner = THIS_MODULE, \
259 }
260
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000261static const struct regulator_desc act8600_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100262 ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
263 ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
264 ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000265 {
266 .name = "SUDCDC_REG4",
Axel Lin253c9c72019-03-22 14:38:19 +0800267 .of_match = of_match_ptr("SUDCDC_REG4"),
268 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000269 .id = ACT8600_ID_SUDCDC4,
270 .ops = &act8865_ops,
271 .type = REGULATOR_VOLTAGE,
272 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
273 .linear_ranges = act8600_sudcdc_voltage_ranges,
274 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
275 .vsel_reg = ACT8600_SUDCDC4_VSET,
276 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
277 .enable_reg = ACT8600_SUDCDC4_CTRL,
278 .enable_mask = ACT8865_ENA,
279 .owner = THIS_MODULE,
280 },
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100281 ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
282 ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
283 ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
284 ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000285 {
286 .name = "LDO_REG9",
Axel Lin253c9c72019-03-22 14:38:19 +0800287 .of_match = of_match_ptr("LDO_REG9"),
288 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000289 .id = ACT8600_ID_LDO9,
290 .ops = &act8865_ldo_ops,
291 .type = REGULATOR_VOLTAGE,
292 .n_voltages = 1,
Maarten ter Huurne79b5d022016-02-28 16:53:28 +0100293 .fixed_uV = 3300000,
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000294 .enable_reg = ACT8600_LDO910_CTRL,
295 .enable_mask = ACT8865_ENA,
296 .owner = THIS_MODULE,
297 },
298 {
299 .name = "LDO_REG10",
Axel Lin253c9c72019-03-22 14:38:19 +0800300 .of_match = of_match_ptr("LDO_REG10"),
301 .regulators_node = of_match_ptr("regulators"),
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000302 .id = ACT8600_ID_LDO10,
303 .ops = &act8865_ldo_ops,
304 .type = REGULATOR_VOLTAGE,
305 .n_voltages = 1,
306 .fixed_uV = 1200000,
307 .enable_reg = ACT8600_LDO910_CTRL,
308 .enable_mask = ACT8600_LDO10_ENA,
309 .owner = THIS_MODULE,
310 },
311};
312
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200313static const struct regulator_desc act8846_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100314 ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
315 ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
316 ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
317 ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
318 ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
319 ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
320 ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
321 ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
322 ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
323 ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
324 ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
325 ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200326};
327
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200328static const struct regulator_desc act8865_regulators[] = {
Heiko Stuebnerc8b263c2015-03-07 16:33:53 +0100329 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
330 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
331 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
332 ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
333 ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
334 ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
335 ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
Wenyou Yang33036f42013-12-24 10:34:28 +0800336};
337
Wenyou Yangc86dc032015-09-30 15:25:49 +0800338static const struct regulator_desc act8865_alt_regulators[] = {
339 ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
340 ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
341 ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
342 ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
343 ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
344 ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
345 ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
346};
347
Wenyou Yang33036f42013-12-24 10:34:28 +0800348#ifdef CONFIG_OF
349static const struct of_device_id act8865_dt_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000350 { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200351 { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200352 { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800353 { }
354};
355MODULE_DEVICE_TABLE(of, act8865_dt_ids);
Wenyou Yang33036f42013-12-24 10:34:28 +0800356#endif
357
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100358static struct act8865_regulator_data *act8865_get_regulator_data(
359 int id, struct act8865_platform_data *pdata)
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200360{
361 int i;
362
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200363 for (i = 0; i < pdata->num_regulators; i++) {
364 if (pdata->regulators[i].id == id)
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100365 return &pdata->regulators[i];
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200366 }
367
368 return NULL;
369}
370
Romain Perier2b17fa22014-10-14 06:31:10 +0000371static struct i2c_client *act8865_i2c_client;
372static void act8865_power_off(void)
373{
374 struct act8865 *act8865;
375
376 act8865 = i2c_get_clientdata(act8865_i2c_client);
377 regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
378 while (1);
379}
380
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400381static int act8600_charger_get_status(struct regmap *map)
382{
383 unsigned int val;
384 int ret;
385 u8 state0, state1;
386
387 ret = regmap_read(map, ACT8600_APCH_STAT, &val);
388 if (ret < 0)
389 return ret;
390
391 state0 = val & ACT8600_APCH_CSTATE0;
392 state1 = val & ACT8600_APCH_CSTATE1;
393
394 if (state0 && !state1)
395 return POWER_SUPPLY_STATUS_CHARGING;
396 if (!state0 && state1)
397 return POWER_SUPPLY_STATUS_NOT_CHARGING;
398 if (!state0 && !state1)
399 return POWER_SUPPLY_STATUS_DISCHARGING;
400
401 return POWER_SUPPLY_STATUS_UNKNOWN;
402}
403
404static int act8600_charger_get_property(struct power_supply *psy,
405 enum power_supply_property psp, union power_supply_propval *val)
406{
407 struct regmap *map = power_supply_get_drvdata(psy);
408 int ret;
409
410 switch (psp) {
411 case POWER_SUPPLY_PROP_STATUS:
412 ret = act8600_charger_get_status(map);
413 if (ret < 0)
414 return ret;
415
416 val->intval = ret;
417 break;
418 default:
419 return -EINVAL;
420 }
421
422 return 0;
423}
424
425static enum power_supply_property act8600_charger_properties[] = {
426 POWER_SUPPLY_PROP_STATUS,
427};
428
429static const struct power_supply_desc act8600_charger_desc = {
430 .name = "act8600-charger",
431 .type = POWER_SUPPLY_TYPE_BATTERY,
432 .properties = act8600_charger_properties,
433 .num_properties = ARRAY_SIZE(act8600_charger_properties),
434 .get_property = act8600_charger_get_property,
435};
436
437static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
438{
439 struct power_supply *charger;
440 struct power_supply_config cfg = {
441 .drv_data = regmap,
442 .of_node = dev->of_node,
443 };
444
445 charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
446
447 return IS_ERR(charger) ? PTR_ERR(charger) : 0;
448}
449
Wenyou Yang33036f42013-12-24 10:34:28 +0800450static int act8865_pmic_probe(struct i2c_client *client,
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200451 const struct i2c_device_id *i2c_id)
Wenyou Yang33036f42013-12-24 10:34:28 +0800452{
Maarten ter Huurneabea1bb2016-02-28 16:53:25 +0100453 const struct regulator_desc *regulators;
Axel Lin253c9c72019-03-22 14:38:19 +0800454 struct act8865_platform_data *pdata = NULL;
Wenyou Yang33036f42013-12-24 10:34:28 +0800455 struct device *dev = &client->dev;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200456 int i, ret, num_regulators;
Wenyou Yang33036f42013-12-24 10:34:28 +0800457 struct act8865 *act8865;
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100458 const struct regmap_config *regmap_config;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200459 unsigned long type;
Romain Perier2b17fa22014-10-14 06:31:10 +0000460 int off_reg, off_mask;
Wenyou Yangc86dc032015-09-30 15:25:49 +0800461 int voltage_select = 0;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200462
Axel Lin253c9c72019-03-22 14:38:19 +0800463 if (dev->of_node) {
Wenyou Yang33036f42013-12-24 10:34:28 +0800464 const struct of_device_id *id;
Wenyou Yang33036f42013-12-24 10:34:28 +0800465
466 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
467 if (!id)
468 return -ENODEV;
469
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200470 type = (unsigned long) id->data;
Wenyou Yangc86dc032015-09-30 15:25:49 +0800471
472 voltage_select = !!of_get_property(dev->of_node,
473 "active-semi,vsel-high",
474 NULL);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200475 } else {
476 type = i2c_id->driver_data;
Axel Lin253c9c72019-03-22 14:38:19 +0800477 pdata = dev_get_platdata(dev);
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200478 }
479
480 switch (type) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000481 case ACT8600:
482 regulators = act8600_regulators;
483 num_regulators = ARRAY_SIZE(act8600_regulators);
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100484 regmap_config = &act8600_regmap_config;
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000485 off_reg = -1;
486 off_mask = -1;
487 break;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200488 case ACT8846:
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200489 regulators = act8846_regulators;
490 num_regulators = ARRAY_SIZE(act8846_regulators);
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100491 regmap_config = &act8865_regmap_config;
Romain Perier2b17fa22014-10-14 06:31:10 +0000492 off_reg = ACT8846_GLB_OFF_CTRL;
493 off_mask = ACT8846_OFF_SYSMASK;
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200494 break;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200495 case ACT8865:
Wenyou Yangc86dc032015-09-30 15:25:49 +0800496 if (voltage_select) {
497 regulators = act8865_alt_regulators;
498 num_regulators = ARRAY_SIZE(act8865_alt_regulators);
499 } else {
500 regulators = act8865_regulators;
501 num_regulators = ARRAY_SIZE(act8865_regulators);
502 }
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100503 regmap_config = &act8865_regmap_config;
Romain Perier2b17fa22014-10-14 06:31:10 +0000504 off_reg = ACT8865_SYS_CTRL;
505 off_mask = ACT8865_MSTROFF;
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200506 break;
507 default:
508 dev_err(dev, "invalid device id %lu\n", type);
509 return -EINVAL;
510 }
511
Wenyou Yangf1de2c22013-12-26 14:52:43 +0800512 act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
Wenyou Yang33036f42013-12-24 10:34:28 +0800513 if (!act8865)
514 return -ENOMEM;
515
Maarten ter Huurnec5c9c2d2016-03-17 15:05:08 +0100516 act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
Wenyou Yang33036f42013-12-24 10:34:28 +0800517 if (IS_ERR(act8865->regmap)) {
Axel Lin40d3bc12014-06-30 15:32:09 +0800518 ret = PTR_ERR(act8865->regmap);
Maarten ter Huurnede14ba62016-03-17 15:05:05 +0100519 dev_err(dev, "Failed to allocate register map: %d\n", ret);
Axel Lin40d3bc12014-06-30 15:32:09 +0800520 return ret;
Wenyou Yang33036f42013-12-24 10:34:28 +0800521 }
522
Romain Perier8f731102014-11-25 12:28:25 +0000523 if (of_device_is_system_power_controller(dev->of_node)) {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000524 if (!pm_power_off && (off_reg > 0)) {
Romain Perier2b17fa22014-10-14 06:31:10 +0000525 act8865_i2c_client = client;
526 act8865->off_reg = off_reg;
527 act8865->off_mask = off_mask;
528 pm_power_off = act8865_power_off;
529 } else {
530 dev_err(dev, "Failed to set poweroff capability, already defined\n");
531 }
532 }
533
Wenyou Yang33036f42013-12-24 10:34:28 +0800534 /* Finally register devices */
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200535 for (i = 0; i < num_regulators; i++) {
536 const struct regulator_desc *desc = &regulators[i];
537 struct regulator_config config = { };
538 struct regulator_dev *rdev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800539
540 config.dev = dev;
Wenyou Yang33036f42013-12-24 10:34:28 +0800541 config.driver_data = act8865;
542 config.regmap = act8865->regmap;
543
Axel Lin253c9c72019-03-22 14:38:19 +0800544 if (pdata) {
545 struct act8865_regulator_data *rdata;
546
547 rdata = act8865_get_regulator_data(desc->id, pdata);
548 if (rdata) {
549 config.init_data = rdata->init_data;
550 config.of_node = rdata->of_node;
551 }
Maarten ter Huurne005e46852016-03-17 15:05:07 +0100552 }
553
Maarten ter Huurnede14ba62016-03-17 15:05:05 +0100554 rdev = devm_regulator_register(dev, desc, &config);
Axel Linfb8eb452014-03-08 21:19:07 +0800555 if (IS_ERR(rdev)) {
Beniamino Galvani48a1e1b2014-06-22 17:31:41 +0200556 dev_err(dev, "failed to register %s\n", desc->name);
Axel Linfb8eb452014-03-08 21:19:07 +0800557 return PTR_ERR(rdev);
Wenyou Yang33036f42013-12-24 10:34:28 +0800558 }
559 }
560
Maarten ter Huurne2d09a792019-07-22 21:14:18 -0400561 if (type == ACT8600) {
562 ret = act8600_charger_probe(dev, act8865->regmap);
563 if (ret < 0) {
564 if (ret != -EPROBE_DEFER)
565 dev_err(dev, "Failed to probe charger");
566 return ret;
567 }
568 }
569
Wenyou Yang33036f42013-12-24 10:34:28 +0800570 i2c_set_clientdata(client, act8865);
571
572 return 0;
573}
574
Wenyou Yang33036f42013-12-24 10:34:28 +0800575static const struct i2c_device_id act8865_ids[] = {
Zubair Lutfullah Kakakheldf3a9502015-02-27 17:04:04 +0000576 { .name = "act8600", .driver_data = ACT8600 },
Beniamino Galvaniac0c0ea2014-07-05 15:20:55 +0200577 { .name = "act8846", .driver_data = ACT8846 },
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200578 { .name = "act8865", .driver_data = ACT8865 },
Wenyou Yang33036f42013-12-24 10:34:28 +0800579 { },
580};
581MODULE_DEVICE_TABLE(i2c, act8865_ids);
582
583static struct i2c_driver act8865_pmic_driver = {
584 .driver = {
585 .name = "act8865",
Wenyou Yang33036f42013-12-24 10:34:28 +0800586 },
587 .probe = act8865_pmic_probe,
Wenyou Yang33036f42013-12-24 10:34:28 +0800588 .id_table = act8865_ids,
589};
590
591module_i2c_driver(act8865_pmic_driver);
592
Beniamino Galvani50a03e32014-07-05 15:20:54 +0200593MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
Wenyou Yang33036f42013-12-24 10:34:28 +0800594MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
595MODULE_LICENSE("GPL v2");