Commit | Line | Data |
---|---|---|
cfb61a41 | 1 | /* |
af7e9069 | 2 | * axp20x.c - MFD core driver for the X-Powers' Power Management ICs |
cfb61a41 | 3 | * |
af7e9069 JP |
4 | * AXP20x typically comprises an adaptive USB-Compatible PWM charger, BUCK DC-DC |
5 | * converters, LDOs, multiple 12-bit ADCs of voltage, current and temperature | |
6 | * as well as configurable GPIOs. | |
cfb61a41 | 7 | * |
e740235d CYT |
8 | * Copyright (C) 2014 Carlo Caione |
9 | * | |
cfb61a41 CC |
10 | * Author: Carlo Caione <carlo@caione.org> |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | */ | |
16 | ||
17 | #include <linux/err.h> | |
18 | #include <linux/i2c.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/pm_runtime.h> | |
23 | #include <linux/regmap.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/regulator/consumer.h> | |
26 | #include <linux/mfd/axp20x.h> | |
27 | #include <linux/mfd/core.h> | |
28 | #include <linux/of_device.h> | |
29 | #include <linux/of_irq.h> | |
af7e9069 | 30 | #include <linux/acpi.h> |
cfb61a41 CC |
31 | |
32 | #define AXP20X_OFF 0x80 | |
33 | ||
c31e858b | 34 | static const char * const axp20x_model_names[] = { |
d8d79f8f | 35 | "AXP152", |
af7e9069 JP |
36 | "AXP202", |
37 | "AXP209", | |
f05be589 | 38 | "AXP221", |
af7e9069 JP |
39 | "AXP288", |
40 | }; | |
41 | ||
d8d79f8f MS |
42 | static const struct regmap_range axp152_writeable_ranges[] = { |
43 | regmap_reg_range(AXP152_LDO3456_DC1234_CTRL, AXP152_IRQ3_STATE), | |
44 | regmap_reg_range(AXP152_DCDC_MODE, AXP152_PWM1_DUTY_CYCLE), | |
45 | }; | |
46 | ||
47 | static const struct regmap_range axp152_volatile_ranges[] = { | |
48 | regmap_reg_range(AXP152_PWR_OP_MODE, AXP152_PWR_OP_MODE), | |
49 | regmap_reg_range(AXP152_IRQ1_EN, AXP152_IRQ3_STATE), | |
50 | regmap_reg_range(AXP152_GPIO_INPUT, AXP152_GPIO_INPUT), | |
51 | }; | |
52 | ||
53 | static const struct regmap_access_table axp152_writeable_table = { | |
54 | .yes_ranges = axp152_writeable_ranges, | |
55 | .n_yes_ranges = ARRAY_SIZE(axp152_writeable_ranges), | |
56 | }; | |
57 | ||
58 | static const struct regmap_access_table axp152_volatile_table = { | |
59 | .yes_ranges = axp152_volatile_ranges, | |
60 | .n_yes_ranges = ARRAY_SIZE(axp152_volatile_ranges), | |
61 | }; | |
62 | ||
cfb61a41 CC |
63 | static const struct regmap_range axp20x_writeable_ranges[] = { |
64 | regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE), | |
65 | regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES), | |
553ed4b5 | 66 | regmap_reg_range(AXP20X_RDC_H, AXP20X_OCV(AXP20X_OCV_MAX)), |
cfb61a41 CC |
67 | }; |
68 | ||
69 | static const struct regmap_range axp20x_volatile_ranges[] = { | |
553ed4b5 BP |
70 | regmap_reg_range(AXP20X_PWR_INPUT_STATUS, AXP20X_USB_OTG_STATUS), |
71 | regmap_reg_range(AXP20X_CHRG_CTRL1, AXP20X_CHRG_CTRL2), | |
cfb61a41 | 72 | regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE), |
553ed4b5 BP |
73 | regmap_reg_range(AXP20X_ACIN_V_ADC_H, AXP20X_IPSOUT_V_HIGH_L), |
74 | regmap_reg_range(AXP20X_GPIO20_SS, AXP20X_GPIO3_CTRL), | |
75 | regmap_reg_range(AXP20X_FG_RES, AXP20X_RDC_L), | |
cfb61a41 CC |
76 | }; |
77 | ||
78 | static const struct regmap_access_table axp20x_writeable_table = { | |
79 | .yes_ranges = axp20x_writeable_ranges, | |
80 | .n_yes_ranges = ARRAY_SIZE(axp20x_writeable_ranges), | |
81 | }; | |
82 | ||
83 | static const struct regmap_access_table axp20x_volatile_table = { | |
84 | .yes_ranges = axp20x_volatile_ranges, | |
85 | .n_yes_ranges = ARRAY_SIZE(axp20x_volatile_ranges), | |
86 | }; | |
87 | ||
f05be589 BB |
88 | static const struct regmap_range axp22x_writeable_ranges[] = { |
89 | regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE), | |
90 | regmap_reg_range(AXP20X_DCDC_MODE, AXP22X_BATLOW_THRES1), | |
91 | }; | |
92 | ||
93 | static const struct regmap_range axp22x_volatile_ranges[] = { | |
94 | regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE), | |
95 | }; | |
96 | ||
97 | static const struct regmap_access_table axp22x_writeable_table = { | |
98 | .yes_ranges = axp22x_writeable_ranges, | |
99 | .n_yes_ranges = ARRAY_SIZE(axp22x_writeable_ranges), | |
100 | }; | |
101 | ||
102 | static const struct regmap_access_table axp22x_volatile_table = { | |
103 | .yes_ranges = axp22x_volatile_ranges, | |
104 | .n_yes_ranges = ARRAY_SIZE(axp22x_volatile_ranges), | |
105 | }; | |
106 | ||
af7e9069 JP |
107 | static const struct regmap_range axp288_writeable_ranges[] = { |
108 | regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ6_STATE), | |
109 | regmap_reg_range(AXP20X_DCDC_MODE, AXP288_FG_TUNE5), | |
110 | }; | |
111 | ||
112 | static const struct regmap_range axp288_volatile_ranges[] = { | |
113 | regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IPSOUT_V_HIGH_L), | |
114 | }; | |
115 | ||
116 | static const struct regmap_access_table axp288_writeable_table = { | |
117 | .yes_ranges = axp288_writeable_ranges, | |
118 | .n_yes_ranges = ARRAY_SIZE(axp288_writeable_ranges), | |
119 | }; | |
120 | ||
121 | static const struct regmap_access_table axp288_volatile_table = { | |
122 | .yes_ranges = axp288_volatile_ranges, | |
123 | .n_yes_ranges = ARRAY_SIZE(axp288_volatile_ranges), | |
124 | }; | |
125 | ||
d8d79f8f MS |
126 | static struct resource axp152_pek_resources[] = { |
127 | DEFINE_RES_IRQ_NAMED(AXP152_IRQ_PEK_RIS_EDGE, "PEK_DBR"), | |
128 | DEFINE_RES_IRQ_NAMED(AXP152_IRQ_PEK_FAL_EDGE, "PEK_DBF"), | |
129 | }; | |
130 | ||
cfb61a41 CC |
131 | static struct resource axp20x_pek_resources[] = { |
132 | { | |
133 | .name = "PEK_DBR", | |
134 | .start = AXP20X_IRQ_PEK_RIS_EDGE, | |
135 | .end = AXP20X_IRQ_PEK_RIS_EDGE, | |
136 | .flags = IORESOURCE_IRQ, | |
137 | }, { | |
138 | .name = "PEK_DBF", | |
139 | .start = AXP20X_IRQ_PEK_FAL_EDGE, | |
140 | .end = AXP20X_IRQ_PEK_FAL_EDGE, | |
141 | .flags = IORESOURCE_IRQ, | |
142 | }, | |
143 | }; | |
144 | ||
8de4efda HG |
145 | static struct resource axp20x_usb_power_supply_resources[] = { |
146 | DEFINE_RES_IRQ_NAMED(AXP20X_IRQ_VBUS_PLUGIN, "VBUS_PLUGIN"), | |
147 | DEFINE_RES_IRQ_NAMED(AXP20X_IRQ_VBUS_REMOVAL, "VBUS_REMOVAL"), | |
148 | DEFINE_RES_IRQ_NAMED(AXP20X_IRQ_VBUS_VALID, "VBUS_VALID"), | |
149 | DEFINE_RES_IRQ_NAMED(AXP20X_IRQ_VBUS_NOT_VALID, "VBUS_NOT_VALID"), | |
150 | }; | |
151 | ||
f05be589 BB |
152 | static struct resource axp22x_pek_resources[] = { |
153 | { | |
154 | .name = "PEK_DBR", | |
155 | .start = AXP22X_IRQ_PEK_RIS_EDGE, | |
156 | .end = AXP22X_IRQ_PEK_RIS_EDGE, | |
157 | .flags = IORESOURCE_IRQ, | |
158 | }, { | |
159 | .name = "PEK_DBF", | |
160 | .start = AXP22X_IRQ_PEK_FAL_EDGE, | |
161 | .end = AXP22X_IRQ_PEK_FAL_EDGE, | |
162 | .flags = IORESOURCE_IRQ, | |
163 | }, | |
164 | }; | |
165 | ||
e56e5ad6 BF |
166 | static struct resource axp288_power_button_resources[] = { |
167 | { | |
168 | .name = "PEK_DBR", | |
169 | .start = AXP288_IRQ_POKN, | |
170 | .end = AXP288_IRQ_POKN, | |
171 | .flags = IORESOURCE_IRQ, | |
172 | }, | |
173 | { | |
174 | .name = "PEK_DBF", | |
175 | .start = AXP288_IRQ_POKP, | |
176 | .end = AXP288_IRQ_POKP, | |
177 | .flags = IORESOURCE_IRQ, | |
178 | }, | |
179 | }; | |
180 | ||
d6387874 | 181 | static struct resource axp288_fuel_gauge_resources[] = { |
af7e9069 JP |
182 | { |
183 | .start = AXP288_IRQ_QWBTU, | |
184 | .end = AXP288_IRQ_QWBTU, | |
185 | .flags = IORESOURCE_IRQ, | |
186 | }, | |
187 | { | |
188 | .start = AXP288_IRQ_WBTU, | |
189 | .end = AXP288_IRQ_WBTU, | |
190 | .flags = IORESOURCE_IRQ, | |
191 | }, | |
192 | { | |
193 | .start = AXP288_IRQ_QWBTO, | |
194 | .end = AXP288_IRQ_QWBTO, | |
195 | .flags = IORESOURCE_IRQ, | |
196 | }, | |
197 | { | |
198 | .start = AXP288_IRQ_WBTO, | |
199 | .end = AXP288_IRQ_WBTO, | |
200 | .flags = IORESOURCE_IRQ, | |
201 | }, | |
202 | { | |
203 | .start = AXP288_IRQ_WL2, | |
204 | .end = AXP288_IRQ_WL2, | |
205 | .flags = IORESOURCE_IRQ, | |
206 | }, | |
207 | { | |
208 | .start = AXP288_IRQ_WL1, | |
209 | .end = AXP288_IRQ_WL1, | |
210 | .flags = IORESOURCE_IRQ, | |
211 | }, | |
212 | }; | |
213 | ||
d8d79f8f MS |
214 | static const struct regmap_config axp152_regmap_config = { |
215 | .reg_bits = 8, | |
216 | .val_bits = 8, | |
217 | .wr_table = &axp152_writeable_table, | |
218 | .volatile_table = &axp152_volatile_table, | |
219 | .max_register = AXP152_PWM1_DUTY_CYCLE, | |
220 | .cache_type = REGCACHE_RBTREE, | |
221 | }; | |
222 | ||
cfb61a41 CC |
223 | static const struct regmap_config axp20x_regmap_config = { |
224 | .reg_bits = 8, | |
225 | .val_bits = 8, | |
226 | .wr_table = &axp20x_writeable_table, | |
227 | .volatile_table = &axp20x_volatile_table, | |
553ed4b5 | 228 | .max_register = AXP20X_OCV(AXP20X_OCV_MAX), |
cfb61a41 CC |
229 | .cache_type = REGCACHE_RBTREE, |
230 | }; | |
231 | ||
f05be589 BB |
232 | static const struct regmap_config axp22x_regmap_config = { |
233 | .reg_bits = 8, | |
234 | .val_bits = 8, | |
235 | .wr_table = &axp22x_writeable_table, | |
236 | .volatile_table = &axp22x_volatile_table, | |
237 | .max_register = AXP22X_BATLOW_THRES1, | |
238 | .cache_type = REGCACHE_RBTREE, | |
239 | }; | |
240 | ||
af7e9069 JP |
241 | static const struct regmap_config axp288_regmap_config = { |
242 | .reg_bits = 8, | |
243 | .val_bits = 8, | |
244 | .wr_table = &axp288_writeable_table, | |
245 | .volatile_table = &axp288_volatile_table, | |
246 | .max_register = AXP288_FG_TUNE5, | |
247 | .cache_type = REGCACHE_RBTREE, | |
248 | }; | |
249 | ||
250 | #define INIT_REGMAP_IRQ(_variant, _irq, _off, _mask) \ | |
251 | [_variant##_IRQ_##_irq] = { .reg_offset = (_off), .mask = BIT(_mask) } | |
cfb61a41 | 252 | |
d8d79f8f MS |
253 | static const struct regmap_irq axp152_regmap_irqs[] = { |
254 | INIT_REGMAP_IRQ(AXP152, LDO0IN_CONNECT, 0, 6), | |
255 | INIT_REGMAP_IRQ(AXP152, LDO0IN_REMOVAL, 0, 5), | |
256 | INIT_REGMAP_IRQ(AXP152, ALDO0IN_CONNECT, 0, 3), | |
257 | INIT_REGMAP_IRQ(AXP152, ALDO0IN_REMOVAL, 0, 2), | |
258 | INIT_REGMAP_IRQ(AXP152, DCDC1_V_LOW, 1, 5), | |
259 | INIT_REGMAP_IRQ(AXP152, DCDC2_V_LOW, 1, 4), | |
260 | INIT_REGMAP_IRQ(AXP152, DCDC3_V_LOW, 1, 3), | |
261 | INIT_REGMAP_IRQ(AXP152, DCDC4_V_LOW, 1, 2), | |
262 | INIT_REGMAP_IRQ(AXP152, PEK_SHORT, 1, 1), | |
263 | INIT_REGMAP_IRQ(AXP152, PEK_LONG, 1, 0), | |
264 | INIT_REGMAP_IRQ(AXP152, TIMER, 2, 7), | |
265 | INIT_REGMAP_IRQ(AXP152, PEK_RIS_EDGE, 2, 6), | |
266 | INIT_REGMAP_IRQ(AXP152, PEK_FAL_EDGE, 2, 5), | |
267 | INIT_REGMAP_IRQ(AXP152, GPIO3_INPUT, 2, 3), | |
268 | INIT_REGMAP_IRQ(AXP152, GPIO2_INPUT, 2, 2), | |
269 | INIT_REGMAP_IRQ(AXP152, GPIO1_INPUT, 2, 1), | |
270 | INIT_REGMAP_IRQ(AXP152, GPIO0_INPUT, 2, 0), | |
271 | }; | |
272 | ||
cfb61a41 | 273 | static const struct regmap_irq axp20x_regmap_irqs[] = { |
af7e9069 JP |
274 | INIT_REGMAP_IRQ(AXP20X, ACIN_OVER_V, 0, 7), |
275 | INIT_REGMAP_IRQ(AXP20X, ACIN_PLUGIN, 0, 6), | |
276 | INIT_REGMAP_IRQ(AXP20X, ACIN_REMOVAL, 0, 5), | |
277 | INIT_REGMAP_IRQ(AXP20X, VBUS_OVER_V, 0, 4), | |
278 | INIT_REGMAP_IRQ(AXP20X, VBUS_PLUGIN, 0, 3), | |
279 | INIT_REGMAP_IRQ(AXP20X, VBUS_REMOVAL, 0, 2), | |
280 | INIT_REGMAP_IRQ(AXP20X, VBUS_V_LOW, 0, 1), | |
281 | INIT_REGMAP_IRQ(AXP20X, BATT_PLUGIN, 1, 7), | |
282 | INIT_REGMAP_IRQ(AXP20X, BATT_REMOVAL, 1, 6), | |
283 | INIT_REGMAP_IRQ(AXP20X, BATT_ENT_ACT_MODE, 1, 5), | |
284 | INIT_REGMAP_IRQ(AXP20X, BATT_EXIT_ACT_MODE, 1, 4), | |
285 | INIT_REGMAP_IRQ(AXP20X, CHARG, 1, 3), | |
286 | INIT_REGMAP_IRQ(AXP20X, CHARG_DONE, 1, 2), | |
287 | INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_HIGH, 1, 1), | |
288 | INIT_REGMAP_IRQ(AXP20X, BATT_TEMP_LOW, 1, 0), | |
289 | INIT_REGMAP_IRQ(AXP20X, DIE_TEMP_HIGH, 2, 7), | |
290 | INIT_REGMAP_IRQ(AXP20X, CHARG_I_LOW, 2, 6), | |
291 | INIT_REGMAP_IRQ(AXP20X, DCDC1_V_LONG, 2, 5), | |
292 | INIT_REGMAP_IRQ(AXP20X, DCDC2_V_LONG, 2, 4), | |
293 | INIT_REGMAP_IRQ(AXP20X, DCDC3_V_LONG, 2, 3), | |
294 | INIT_REGMAP_IRQ(AXP20X, PEK_SHORT, 2, 1), | |
295 | INIT_REGMAP_IRQ(AXP20X, PEK_LONG, 2, 0), | |
296 | INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_ON, 3, 7), | |
297 | INIT_REGMAP_IRQ(AXP20X, N_OE_PWR_OFF, 3, 6), | |
298 | INIT_REGMAP_IRQ(AXP20X, VBUS_VALID, 3, 5), | |
299 | INIT_REGMAP_IRQ(AXP20X, VBUS_NOT_VALID, 3, 4), | |
300 | INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_VALID, 3, 3), | |
301 | INIT_REGMAP_IRQ(AXP20X, VBUS_SESS_END, 3, 2), | |
302 | INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL1, 3, 1), | |
303 | INIT_REGMAP_IRQ(AXP20X, LOW_PWR_LVL2, 3, 0), | |
304 | INIT_REGMAP_IRQ(AXP20X, TIMER, 4, 7), | |
305 | INIT_REGMAP_IRQ(AXP20X, PEK_RIS_EDGE, 4, 6), | |
306 | INIT_REGMAP_IRQ(AXP20X, PEK_FAL_EDGE, 4, 5), | |
307 | INIT_REGMAP_IRQ(AXP20X, GPIO3_INPUT, 4, 3), | |
308 | INIT_REGMAP_IRQ(AXP20X, GPIO2_INPUT, 4, 2), | |
309 | INIT_REGMAP_IRQ(AXP20X, GPIO1_INPUT, 4, 1), | |
310 | INIT_REGMAP_IRQ(AXP20X, GPIO0_INPUT, 4, 0), | |
311 | }; | |
312 | ||
f05be589 BB |
313 | static const struct regmap_irq axp22x_regmap_irqs[] = { |
314 | INIT_REGMAP_IRQ(AXP22X, ACIN_OVER_V, 0, 7), | |
315 | INIT_REGMAP_IRQ(AXP22X, ACIN_PLUGIN, 0, 6), | |
316 | INIT_REGMAP_IRQ(AXP22X, ACIN_REMOVAL, 0, 5), | |
317 | INIT_REGMAP_IRQ(AXP22X, VBUS_OVER_V, 0, 4), | |
318 | INIT_REGMAP_IRQ(AXP22X, VBUS_PLUGIN, 0, 3), | |
319 | INIT_REGMAP_IRQ(AXP22X, VBUS_REMOVAL, 0, 2), | |
320 | INIT_REGMAP_IRQ(AXP22X, VBUS_V_LOW, 0, 1), | |
321 | INIT_REGMAP_IRQ(AXP22X, BATT_PLUGIN, 1, 7), | |
322 | INIT_REGMAP_IRQ(AXP22X, BATT_REMOVAL, 1, 6), | |
323 | INIT_REGMAP_IRQ(AXP22X, BATT_ENT_ACT_MODE, 1, 5), | |
324 | INIT_REGMAP_IRQ(AXP22X, BATT_EXIT_ACT_MODE, 1, 4), | |
325 | INIT_REGMAP_IRQ(AXP22X, CHARG, 1, 3), | |
326 | INIT_REGMAP_IRQ(AXP22X, CHARG_DONE, 1, 2), | |
327 | INIT_REGMAP_IRQ(AXP22X, BATT_TEMP_HIGH, 1, 1), | |
328 | INIT_REGMAP_IRQ(AXP22X, BATT_TEMP_LOW, 1, 0), | |
329 | INIT_REGMAP_IRQ(AXP22X, DIE_TEMP_HIGH, 2, 7), | |
330 | INIT_REGMAP_IRQ(AXP22X, PEK_SHORT, 2, 1), | |
331 | INIT_REGMAP_IRQ(AXP22X, PEK_LONG, 2, 0), | |
332 | INIT_REGMAP_IRQ(AXP22X, LOW_PWR_LVL1, 3, 1), | |
333 | INIT_REGMAP_IRQ(AXP22X, LOW_PWR_LVL2, 3, 0), | |
334 | INIT_REGMAP_IRQ(AXP22X, TIMER, 4, 7), | |
335 | INIT_REGMAP_IRQ(AXP22X, PEK_RIS_EDGE, 4, 6), | |
336 | INIT_REGMAP_IRQ(AXP22X, PEK_FAL_EDGE, 4, 5), | |
337 | INIT_REGMAP_IRQ(AXP22X, GPIO1_INPUT, 4, 1), | |
338 | INIT_REGMAP_IRQ(AXP22X, GPIO0_INPUT, 4, 0), | |
339 | }; | |
340 | ||
af7e9069 JP |
341 | /* some IRQs are compatible with axp20x models */ |
342 | static const struct regmap_irq axp288_regmap_irqs[] = { | |
ff3bbc5c JP |
343 | INIT_REGMAP_IRQ(AXP288, VBUS_FALL, 0, 2), |
344 | INIT_REGMAP_IRQ(AXP288, VBUS_RISE, 0, 3), | |
345 | INIT_REGMAP_IRQ(AXP288, OV, 0, 4), | |
af7e9069 | 346 | |
ff3bbc5c JP |
347 | INIT_REGMAP_IRQ(AXP288, DONE, 1, 2), |
348 | INIT_REGMAP_IRQ(AXP288, CHARGING, 1, 3), | |
af7e9069 JP |
349 | INIT_REGMAP_IRQ(AXP288, SAFE_QUIT, 1, 4), |
350 | INIT_REGMAP_IRQ(AXP288, SAFE_ENTER, 1, 5), | |
ff3bbc5c JP |
351 | INIT_REGMAP_IRQ(AXP288, ABSENT, 1, 6), |
352 | INIT_REGMAP_IRQ(AXP288, APPEND, 1, 7), | |
af7e9069 JP |
353 | |
354 | INIT_REGMAP_IRQ(AXP288, QWBTU, 2, 0), | |
355 | INIT_REGMAP_IRQ(AXP288, WBTU, 2, 1), | |
356 | INIT_REGMAP_IRQ(AXP288, QWBTO, 2, 2), | |
ff3bbc5c | 357 | INIT_REGMAP_IRQ(AXP288, WBTO, 2, 3), |
af7e9069 JP |
358 | INIT_REGMAP_IRQ(AXP288, QCBTU, 2, 4), |
359 | INIT_REGMAP_IRQ(AXP288, CBTU, 2, 5), | |
360 | INIT_REGMAP_IRQ(AXP288, QCBTO, 2, 6), | |
361 | INIT_REGMAP_IRQ(AXP288, CBTO, 2, 7), | |
362 | ||
363 | INIT_REGMAP_IRQ(AXP288, WL2, 3, 0), | |
364 | INIT_REGMAP_IRQ(AXP288, WL1, 3, 1), | |
365 | INIT_REGMAP_IRQ(AXP288, GPADC, 3, 2), | |
366 | INIT_REGMAP_IRQ(AXP288, OT, 3, 7), | |
367 | ||
368 | INIT_REGMAP_IRQ(AXP288, GPIO0, 4, 0), | |
369 | INIT_REGMAP_IRQ(AXP288, GPIO1, 4, 1), | |
370 | INIT_REGMAP_IRQ(AXP288, POKO, 4, 2), | |
371 | INIT_REGMAP_IRQ(AXP288, POKL, 4, 3), | |
372 | INIT_REGMAP_IRQ(AXP288, POKS, 4, 4), | |
373 | INIT_REGMAP_IRQ(AXP288, POKN, 4, 5), | |
374 | INIT_REGMAP_IRQ(AXP288, POKP, 4, 6), | |
ff3bbc5c | 375 | INIT_REGMAP_IRQ(AXP288, TIMER, 4, 7), |
af7e9069 JP |
376 | |
377 | INIT_REGMAP_IRQ(AXP288, MV_CHNG, 5, 0), | |
378 | INIT_REGMAP_IRQ(AXP288, BC_USB_CHNG, 5, 1), | |
cfb61a41 CC |
379 | }; |
380 | ||
381 | static const struct of_device_id axp20x_of_match[] = { | |
d8d79f8f | 382 | { .compatible = "x-powers,axp152", .data = (void *) AXP152_ID }, |
cfb61a41 CC |
383 | { .compatible = "x-powers,axp202", .data = (void *) AXP202_ID }, |
384 | { .compatible = "x-powers,axp209", .data = (void *) AXP209_ID }, | |
f05be589 | 385 | { .compatible = "x-powers,axp221", .data = (void *) AXP221_ID }, |
cfb61a41 CC |
386 | { }, |
387 | }; | |
388 | MODULE_DEVICE_TABLE(of, axp20x_of_match); | |
389 | ||
390 | /* | |
391 | * This is useless for OF-enabled devices, but it is needed by I2C subsystem | |
392 | */ | |
393 | static const struct i2c_device_id axp20x_i2c_id[] = { | |
394 | { }, | |
395 | }; | |
396 | MODULE_DEVICE_TABLE(i2c, axp20x_i2c_id); | |
397 | ||
0e50e926 | 398 | static const struct acpi_device_id axp20x_acpi_match[] = { |
af7e9069 JP |
399 | { |
400 | .id = "INT33F4", | |
401 | .driver_data = AXP288_ID, | |
402 | }, | |
403 | { }, | |
404 | }; | |
405 | MODULE_DEVICE_TABLE(acpi, axp20x_acpi_match); | |
406 | ||
d8d79f8f MS |
407 | static const struct regmap_irq_chip axp152_regmap_irq_chip = { |
408 | .name = "axp152_irq_chip", | |
409 | .status_base = AXP152_IRQ1_STATE, | |
410 | .ack_base = AXP152_IRQ1_STATE, | |
411 | .mask_base = AXP152_IRQ1_EN, | |
412 | .mask_invert = true, | |
413 | .init_ack_masked = true, | |
414 | .irqs = axp152_regmap_irqs, | |
415 | .num_irqs = ARRAY_SIZE(axp152_regmap_irqs), | |
416 | .num_regs = 3, | |
417 | }; | |
418 | ||
cfb61a41 CC |
419 | static const struct regmap_irq_chip axp20x_regmap_irq_chip = { |
420 | .name = "axp20x_irq_chip", | |
421 | .status_base = AXP20X_IRQ1_STATE, | |
422 | .ack_base = AXP20X_IRQ1_STATE, | |
423 | .mask_base = AXP20X_IRQ1_EN, | |
af7e9069 JP |
424 | .mask_invert = true, |
425 | .init_ack_masked = true, | |
cfb61a41 CC |
426 | .irqs = axp20x_regmap_irqs, |
427 | .num_irqs = ARRAY_SIZE(axp20x_regmap_irqs), | |
af7e9069 JP |
428 | .num_regs = 5, |
429 | ||
430 | }; | |
431 | ||
f05be589 BB |
432 | static const struct regmap_irq_chip axp22x_regmap_irq_chip = { |
433 | .name = "axp22x_irq_chip", | |
434 | .status_base = AXP20X_IRQ1_STATE, | |
435 | .ack_base = AXP20X_IRQ1_STATE, | |
436 | .mask_base = AXP20X_IRQ1_EN, | |
437 | .mask_invert = true, | |
438 | .init_ack_masked = true, | |
439 | .irqs = axp22x_regmap_irqs, | |
440 | .num_irqs = ARRAY_SIZE(axp22x_regmap_irqs), | |
441 | .num_regs = 5, | |
442 | }; | |
443 | ||
af7e9069 JP |
444 | static const struct regmap_irq_chip axp288_regmap_irq_chip = { |
445 | .name = "axp288_irq_chip", | |
446 | .status_base = AXP20X_IRQ1_STATE, | |
447 | .ack_base = AXP20X_IRQ1_STATE, | |
448 | .mask_base = AXP20X_IRQ1_EN, | |
cfb61a41 CC |
449 | .mask_invert = true, |
450 | .init_ack_masked = true, | |
af7e9069 JP |
451 | .irqs = axp288_regmap_irqs, |
452 | .num_irqs = ARRAY_SIZE(axp288_regmap_irqs), | |
453 | .num_regs = 6, | |
454 | ||
cfb61a41 CC |
455 | }; |
456 | ||
cfb61a41 CC |
457 | static struct mfd_cell axp20x_cells[] = { |
458 | { | |
8de4efda HG |
459 | .name = "axp20x-pek", |
460 | .num_resources = ARRAY_SIZE(axp20x_pek_resources), | |
461 | .resources = axp20x_pek_resources, | |
cfb61a41 | 462 | }, { |
8de4efda HG |
463 | .name = "axp20x-regulator", |
464 | }, { | |
465 | .name = "axp20x-usb-power-supply", | |
466 | .of_compatible = "x-powers,axp202-usb-power-supply", | |
467 | .num_resources = ARRAY_SIZE(axp20x_usb_power_supply_resources), | |
468 | .resources = axp20x_usb_power_supply_resources, | |
cfb61a41 CC |
469 | }, |
470 | }; | |
471 | ||
f05be589 BB |
472 | static struct mfd_cell axp22x_cells[] = { |
473 | { | |
474 | .name = "axp20x-pek", | |
475 | .num_resources = ARRAY_SIZE(axp22x_pek_resources), | |
476 | .resources = axp22x_pek_resources, | |
6d4fa89d CYT |
477 | }, { |
478 | .name = "axp20x-regulator", | |
f05be589 BB |
479 | }, |
480 | }; | |
481 | ||
d8d79f8f MS |
482 | static struct mfd_cell axp152_cells[] = { |
483 | { | |
484 | .name = "axp20x-pek", | |
485 | .num_resources = ARRAY_SIZE(axp152_pek_resources), | |
486 | .resources = axp152_pek_resources, | |
487 | }, | |
488 | }; | |
489 | ||
af7e9069 JP |
490 | static struct resource axp288_adc_resources[] = { |
491 | { | |
492 | .name = "GPADC", | |
493 | .start = AXP288_IRQ_GPADC, | |
494 | .end = AXP288_IRQ_GPADC, | |
495 | .flags = IORESOURCE_IRQ, | |
496 | }, | |
497 | }; | |
498 | ||
bdb01f78 RP |
499 | static struct resource axp288_extcon_resources[] = { |
500 | { | |
501 | .start = AXP288_IRQ_VBUS_FALL, | |
502 | .end = AXP288_IRQ_VBUS_FALL, | |
503 | .flags = IORESOURCE_IRQ, | |
504 | }, | |
505 | { | |
506 | .start = AXP288_IRQ_VBUS_RISE, | |
507 | .end = AXP288_IRQ_VBUS_RISE, | |
508 | .flags = IORESOURCE_IRQ, | |
509 | }, | |
510 | { | |
511 | .start = AXP288_IRQ_MV_CHNG, | |
512 | .end = AXP288_IRQ_MV_CHNG, | |
513 | .flags = IORESOURCE_IRQ, | |
514 | }, | |
515 | { | |
516 | .start = AXP288_IRQ_BC_USB_CHNG, | |
517 | .end = AXP288_IRQ_BC_USB_CHNG, | |
518 | .flags = IORESOURCE_IRQ, | |
519 | }, | |
520 | }; | |
521 | ||
af7e9069 JP |
522 | static struct resource axp288_charger_resources[] = { |
523 | { | |
524 | .start = AXP288_IRQ_OV, | |
525 | .end = AXP288_IRQ_OV, | |
526 | .flags = IORESOURCE_IRQ, | |
527 | }, | |
528 | { | |
529 | .start = AXP288_IRQ_DONE, | |
530 | .end = AXP288_IRQ_DONE, | |
531 | .flags = IORESOURCE_IRQ, | |
532 | }, | |
533 | { | |
534 | .start = AXP288_IRQ_CHARGING, | |
535 | .end = AXP288_IRQ_CHARGING, | |
536 | .flags = IORESOURCE_IRQ, | |
537 | }, | |
538 | { | |
539 | .start = AXP288_IRQ_SAFE_QUIT, | |
540 | .end = AXP288_IRQ_SAFE_QUIT, | |
541 | .flags = IORESOURCE_IRQ, | |
542 | }, | |
543 | { | |
544 | .start = AXP288_IRQ_SAFE_ENTER, | |
545 | .end = AXP288_IRQ_SAFE_ENTER, | |
546 | .flags = IORESOURCE_IRQ, | |
547 | }, | |
548 | { | |
549 | .start = AXP288_IRQ_QCBTU, | |
550 | .end = AXP288_IRQ_QCBTU, | |
551 | .flags = IORESOURCE_IRQ, | |
552 | }, | |
553 | { | |
554 | .start = AXP288_IRQ_CBTU, | |
555 | .end = AXP288_IRQ_CBTU, | |
556 | .flags = IORESOURCE_IRQ, | |
557 | }, | |
558 | { | |
559 | .start = AXP288_IRQ_QCBTO, | |
560 | .end = AXP288_IRQ_QCBTO, | |
561 | .flags = IORESOURCE_IRQ, | |
562 | }, | |
563 | { | |
564 | .start = AXP288_IRQ_CBTO, | |
565 | .end = AXP288_IRQ_CBTO, | |
566 | .flags = IORESOURCE_IRQ, | |
567 | }, | |
568 | }; | |
569 | ||
570 | static struct mfd_cell axp288_cells[] = { | |
571 | { | |
572 | .name = "axp288_adc", | |
573 | .num_resources = ARRAY_SIZE(axp288_adc_resources), | |
574 | .resources = axp288_adc_resources, | |
575 | }, | |
bdb01f78 RP |
576 | { |
577 | .name = "axp288_extcon", | |
578 | .num_resources = ARRAY_SIZE(axp288_extcon_resources), | |
579 | .resources = axp288_extcon_resources, | |
580 | }, | |
af7e9069 JP |
581 | { |
582 | .name = "axp288_charger", | |
583 | .num_resources = ARRAY_SIZE(axp288_charger_resources), | |
584 | .resources = axp288_charger_resources, | |
585 | }, | |
586 | { | |
d6387874 TB |
587 | .name = "axp288_fuel_gauge", |
588 | .num_resources = ARRAY_SIZE(axp288_fuel_gauge_resources), | |
589 | .resources = axp288_fuel_gauge_resources, | |
af7e9069 | 590 | }, |
e56e5ad6 BF |
591 | { |
592 | .name = "axp20x-pek", | |
593 | .num_resources = ARRAY_SIZE(axp288_power_button_resources), | |
594 | .resources = axp288_power_button_resources, | |
595 | }, | |
d8139f63 AL |
596 | { |
597 | .name = "axp288_pmic_acpi", | |
598 | }, | |
af7e9069 JP |
599 | }; |
600 | ||
cfb61a41 CC |
601 | static struct axp20x_dev *axp20x_pm_power_off; |
602 | static void axp20x_power_off(void) | |
603 | { | |
af7e9069 JP |
604 | if (axp20x_pm_power_off->variant == AXP288_ID) |
605 | return; | |
606 | ||
cfb61a41 CC |
607 | regmap_write(axp20x_pm_power_off->regmap, AXP20X_OFF_CTRL, |
608 | AXP20X_OFF); | |
609 | } | |
610 | ||
e47a3cf7 | 611 | static int axp20x_match_device(struct axp20x_dev *axp20x) |
af7e9069 | 612 | { |
e47a3cf7 | 613 | struct device *dev = axp20x->dev; |
af7e9069 JP |
614 | const struct acpi_device_id *acpi_id; |
615 | const struct of_device_id *of_id; | |
616 | ||
617 | if (dev->of_node) { | |
af7acc3d | 618 | of_id = of_match_device(dev->driver->of_match_table, dev); |
af7e9069 JP |
619 | if (!of_id) { |
620 | dev_err(dev, "Unable to match OF ID\n"); | |
621 | return -ENODEV; | |
622 | } | |
623 | axp20x->variant = (long) of_id->data; | |
624 | } else { | |
625 | acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); | |
626 | if (!acpi_id || !acpi_id->driver_data) { | |
627 | dev_err(dev, "Unable to match ACPI ID and data\n"); | |
628 | return -ENODEV; | |
629 | } | |
630 | axp20x->variant = (long) acpi_id->driver_data; | |
631 | } | |
632 | ||
633 | switch (axp20x->variant) { | |
d8d79f8f MS |
634 | case AXP152_ID: |
635 | axp20x->nr_cells = ARRAY_SIZE(axp152_cells); | |
636 | axp20x->cells = axp152_cells; | |
637 | axp20x->regmap_cfg = &axp152_regmap_config; | |
638 | axp20x->regmap_irq_chip = &axp152_regmap_irq_chip; | |
639 | break; | |
af7e9069 JP |
640 | case AXP202_ID: |
641 | case AXP209_ID: | |
642 | axp20x->nr_cells = ARRAY_SIZE(axp20x_cells); | |
643 | axp20x->cells = axp20x_cells; | |
644 | axp20x->regmap_cfg = &axp20x_regmap_config; | |
645 | axp20x->regmap_irq_chip = &axp20x_regmap_irq_chip; | |
646 | break; | |
f05be589 BB |
647 | case AXP221_ID: |
648 | axp20x->nr_cells = ARRAY_SIZE(axp22x_cells); | |
649 | axp20x->cells = axp22x_cells; | |
650 | axp20x->regmap_cfg = &axp22x_regmap_config; | |
651 | axp20x->regmap_irq_chip = &axp22x_regmap_irq_chip; | |
652 | break; | |
af7e9069 JP |
653 | case AXP288_ID: |
654 | axp20x->cells = axp288_cells; | |
655 | axp20x->nr_cells = ARRAY_SIZE(axp288_cells); | |
656 | axp20x->regmap_cfg = &axp288_regmap_config; | |
657 | axp20x->regmap_irq_chip = &axp288_regmap_irq_chip; | |
658 | break; | |
659 | default: | |
660 | dev_err(dev, "unsupported AXP20X ID %lu\n", axp20x->variant); | |
661 | return -EINVAL; | |
662 | } | |
663 | dev_info(dev, "AXP20x variant %s found\n", | |
664 | axp20x_model_names[axp20x->variant]); | |
665 | ||
666 | return 0; | |
667 | } | |
668 | ||
cfb61a41 CC |
669 | static int axp20x_i2c_probe(struct i2c_client *i2c, |
670 | const struct i2c_device_id *id) | |
671 | { | |
672 | struct axp20x_dev *axp20x; | |
cfb61a41 CC |
673 | int ret; |
674 | ||
675 | axp20x = devm_kzalloc(&i2c->dev, sizeof(*axp20x), GFP_KERNEL); | |
676 | if (!axp20x) | |
677 | return -ENOMEM; | |
678 | ||
cfb61a41 CC |
679 | axp20x->i2c_client = i2c; |
680 | axp20x->dev = &i2c->dev; | |
681 | dev_set_drvdata(axp20x->dev, axp20x); | |
682 | ||
e47a3cf7 CYT |
683 | ret = axp20x_match_device(axp20x); |
684 | if (ret) | |
685 | return ret; | |
686 | ||
af7e9069 | 687 | axp20x->regmap = devm_regmap_init_i2c(i2c, axp20x->regmap_cfg); |
cfb61a41 CC |
688 | if (IS_ERR(axp20x->regmap)) { |
689 | ret = PTR_ERR(axp20x->regmap); | |
690 | dev_err(&i2c->dev, "regmap init failed: %d\n", ret); | |
691 | return ret; | |
692 | } | |
693 | ||
694 | ret = regmap_add_irq_chip(axp20x->regmap, i2c->irq, | |
695 | IRQF_ONESHOT | IRQF_SHARED, -1, | |
af7e9069 | 696 | axp20x->regmap_irq_chip, |
cfb61a41 CC |
697 | &axp20x->regmap_irqc); |
698 | if (ret) { | |
699 | dev_err(&i2c->dev, "failed to add irq chip: %d\n", ret); | |
700 | return ret; | |
701 | } | |
702 | ||
af7e9069 JP |
703 | ret = mfd_add_devices(axp20x->dev, -1, axp20x->cells, |
704 | axp20x->nr_cells, NULL, 0, NULL); | |
cfb61a41 CC |
705 | |
706 | if (ret) { | |
707 | dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret); | |
708 | regmap_del_irq_chip(i2c->irq, axp20x->regmap_irqc); | |
709 | return ret; | |
710 | } | |
711 | ||
712 | if (!pm_power_off) { | |
713 | axp20x_pm_power_off = axp20x; | |
714 | pm_power_off = axp20x_power_off; | |
715 | } | |
716 | ||
717 | dev_info(&i2c->dev, "AXP20X driver loaded\n"); | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
722 | static int axp20x_i2c_remove(struct i2c_client *i2c) | |
723 | { | |
724 | struct axp20x_dev *axp20x = i2c_get_clientdata(i2c); | |
725 | ||
726 | if (axp20x == axp20x_pm_power_off) { | |
727 | axp20x_pm_power_off = NULL; | |
728 | pm_power_off = NULL; | |
729 | } | |
730 | ||
731 | mfd_remove_devices(axp20x->dev); | |
732 | regmap_del_irq_chip(axp20x->i2c_client->irq, axp20x->regmap_irqc); | |
733 | ||
734 | return 0; | |
735 | } | |
736 | ||
737 | static struct i2c_driver axp20x_i2c_driver = { | |
738 | .driver = { | |
739 | .name = "axp20x", | |
cfb61a41 | 740 | .of_match_table = of_match_ptr(axp20x_of_match), |
af7e9069 | 741 | .acpi_match_table = ACPI_PTR(axp20x_acpi_match), |
cfb61a41 CC |
742 | }, |
743 | .probe = axp20x_i2c_probe, | |
744 | .remove = axp20x_i2c_remove, | |
745 | .id_table = axp20x_i2c_id, | |
746 | }; | |
747 | ||
748 | module_i2c_driver(axp20x_i2c_driver); | |
749 | ||
750 | MODULE_DESCRIPTION("PMIC MFD core driver for AXP20X"); | |
751 | MODULE_AUTHOR("Carlo Caione <carlo@caione.org>"); | |
752 | MODULE_LICENSE("GPL"); |