xref: /freebsd/sys/arm/allwinner/axp209.c (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
1 /*-
2  * Copyright (c) 2015-2016 Emmanuel Vadot <manu@freebsd.org>
3  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 /*
29 * X-Power AXP209/AXP211 PMU for Allwinner SoCs
30 */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/clock.h>
38 #include <sys/time.h>
39 #include <sys/bus.h>
40 #include <sys/proc.h>
41 #include <sys/gpio.h>
42 #include <sys/reboot.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 
47 #include <dev/iicbus/iiconf.h>
48 
49 #include <dev/gpio/gpiobusvar.h>
50 
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 
54 #include <dev/regulator/regulator.h>
55 
56 #include <arm/allwinner/axp209reg.h>
57 
58 #include "gpio_if.h"
59 #include "regdev_if.h"
60 
61 MALLOC_DEFINE(M_AXP2XX_REG, "Axp2XX regulator", "Axp2XX power regulator");
62 
63 struct axp2xx_regdef {
64 	intptr_t		id;
65 	char			*name;
66 	uint8_t			enable_reg;
67 	uint8_t			enable_mask;
68 	uint8_t			voltage_reg;
69 	uint8_t			voltage_mask;
70 	uint8_t			voltage_shift;
71 	int			voltage_min;
72 	int			voltage_max;
73 	int			voltage_step;
74 	int			voltage_nstep;
75 };
76 
77 static struct axp2xx_regdef axp209_regdefs[] = {
78 	{
79 		.id = AXP209_REG_ID_DCDC2,
80 		.name = "dcdc2",
81 		.enable_reg = AXP209_POWERCTL,
82 		.enable_mask = AXP209_POWERCTL_DCDC2,
83 		.voltage_reg = AXP209_REG_DCDC2_VOLTAGE,
84 		.voltage_mask = 0x3f,
85 		.voltage_min = 700,
86 		.voltage_max = 2275,
87 		.voltage_step = 25,
88 		.voltage_nstep = 64,
89 	},
90 	{
91 		.id = AXP209_REG_ID_DCDC3,
92 		.name = "dcdc3",
93 		.enable_reg = AXP209_POWERCTL,
94 		.enable_mask = AXP209_POWERCTL_DCDC3,
95 		.voltage_reg = AXP209_REG_DCDC3_VOLTAGE,
96 		.voltage_mask = 0x7f,
97 		.voltage_min = 700,
98 		.voltage_max = 3500,
99 		.voltage_step = 25,
100 		.voltage_nstep = 128,
101 	},
102 	{
103 		.id = AXP209_REG_ID_LDO2,
104 		.name = "ldo2",
105 		.enable_reg = AXP209_POWERCTL,
106 		.enable_mask = AXP209_POWERCTL_LDO2,
107 		.voltage_reg = AXP209_REG_LDO24_VOLTAGE,
108 		.voltage_mask = 0xf0,
109 		.voltage_shift = 4,
110 		.voltage_min = 1800,
111 		.voltage_max = 3300,
112 		.voltage_step = 100,
113 		.voltage_nstep = 16,
114 	},
115 	{
116 		.id = AXP209_REG_ID_LDO3,
117 		.name = "ldo3",
118 		.enable_reg = AXP209_POWERCTL,
119 		.enable_mask = AXP209_POWERCTL_LDO3,
120 		.voltage_reg = AXP209_REG_LDO3_VOLTAGE,
121 		.voltage_mask = 0x7f,
122 		.voltage_min = 700,
123 		.voltage_max = 2275,
124 		.voltage_step = 25,
125 		.voltage_nstep = 128,
126 	},
127 };
128 
129 static struct axp2xx_regdef axp221_regdefs[] = {
130 	{
131 		.id = AXP221_REG_ID_DLDO1,
132 		.name = "dldo1",
133 		.enable_reg = AXP221_POWERCTL_2,
134 		.enable_mask = AXP221_POWERCTL2_DLDO1,
135 		.voltage_reg = AXP221_REG_DLDO1_VOLTAGE,
136 		.voltage_mask = 0x1f,
137 		.voltage_min = 700,
138 		.voltage_max = 3300,
139 		.voltage_step = 100,
140 		.voltage_nstep = 26,
141 	},
142 	{
143 		.id = AXP221_REG_ID_DLDO2,
144 		.name = "dldo2",
145 		.enable_reg = AXP221_POWERCTL_2,
146 		.enable_mask = AXP221_POWERCTL2_DLDO2,
147 		.voltage_reg = AXP221_REG_DLDO2_VOLTAGE,
148 		.voltage_mask = 0x1f,
149 		.voltage_min = 700,
150 		.voltage_max = 3300,
151 		.voltage_step = 100,
152 		.voltage_nstep = 26,
153 	},
154 	{
155 		.id = AXP221_REG_ID_DLDO3,
156 		.name = "dldo3",
157 		.enable_reg = AXP221_POWERCTL_2,
158 		.enable_mask = AXP221_POWERCTL2_DLDO3,
159 		.voltage_reg = AXP221_REG_DLDO3_VOLTAGE,
160 		.voltage_mask = 0x1f,
161 		.voltage_min = 700,
162 		.voltage_max = 3300,
163 		.voltage_step = 100,
164 		.voltage_nstep = 26,
165 	},
166 	{
167 		.id = AXP221_REG_ID_DLDO4,
168 		.name = "dldo4",
169 		.enable_reg = AXP221_POWERCTL_2,
170 		.enable_mask = AXP221_POWERCTL2_DLDO4,
171 		.voltage_reg = AXP221_REG_DLDO4_VOLTAGE,
172 		.voltage_mask = 0x1f,
173 		.voltage_min = 700,
174 		.voltage_max = 3300,
175 		.voltage_step = 100,
176 		.voltage_nstep = 26,
177 	},
178 	{
179 		.id = AXP221_REG_ID_ELDO1,
180 		.name = "eldo1",
181 		.enable_reg = AXP221_POWERCTL_2,
182 		.enable_mask = AXP221_POWERCTL2_ELDO1,
183 		.voltage_reg = AXP221_REG_ELDO1_VOLTAGE,
184 		.voltage_mask = 0x1f,
185 		.voltage_min = 700,
186 		.voltage_max = 3300,
187 		.voltage_step = 100,
188 		.voltage_nstep = 26,
189 	},
190 	{
191 		.id = AXP221_REG_ID_ELDO2,
192 		.name = "eldo2",
193 		.enable_reg = AXP221_POWERCTL_2,
194 		.enable_mask = AXP221_POWERCTL2_ELDO2,
195 		.voltage_reg = AXP221_REG_ELDO2_VOLTAGE,
196 		.voltage_mask = 0x1f,
197 		.voltage_min = 700,
198 		.voltage_max = 3300,
199 		.voltage_step = 100,
200 		.voltage_nstep = 26,
201 	},
202 	{
203 		.id = AXP221_REG_ID_ELDO3,
204 		.name = "eldo3",
205 		.enable_reg = AXP221_POWERCTL_2,
206 		.enable_mask = AXP221_POWERCTL2_ELDO3,
207 		.voltage_reg = AXP221_REG_ELDO3_VOLTAGE,
208 		.voltage_mask = 0x1f,
209 		.voltage_min = 700,
210 		.voltage_max = 3300,
211 		.voltage_step = 100,
212 		.voltage_nstep = 26,
213 	},
214 	{
215 		.id = AXP221_REG_ID_DC5LDO,
216 		.name = "dc5ldo",
217 		.enable_reg = AXP221_POWERCTL_1,
218 		.enable_mask = AXP221_POWERCTL1_DC5LDO,
219 		.voltage_reg = AXP221_REG_DC5LDO_VOLTAGE,
220 		.voltage_mask = 0x3,
221 		.voltage_min = 700,
222 		.voltage_max = 1400,
223 		.voltage_step = 100,
224 		.voltage_nstep = 7,
225 	},
226 	{
227 		.id = AXP221_REG_ID_DCDC1,
228 		.name = "dcdc1",
229 		.enable_reg = AXP221_POWERCTL_1,
230 		.enable_mask = AXP221_POWERCTL1_DCDC1,
231 		.voltage_reg = AXP221_REG_DCDC1_VOLTAGE,
232 		.voltage_mask = 0x1f,
233 		.voltage_min = 1600,
234 		.voltage_max = 3400,
235 		.voltage_step = 100,
236 		.voltage_nstep = 18,
237 	},
238 	{
239 		.id = AXP221_REG_ID_DCDC2,
240 		.name = "dcdc2",
241 		.enable_reg = AXP221_POWERCTL_1,
242 		.enable_mask = AXP221_POWERCTL1_DCDC2,
243 		.voltage_reg = AXP221_REG_DCDC2_VOLTAGE,
244 		.voltage_mask = 0x3f,
245 		.voltage_min = 600,
246 		.voltage_max = 1540,
247 		.voltage_step = 20,
248 		.voltage_nstep = 47,
249 	},
250 	{
251 		.id = AXP221_REG_ID_DCDC3,
252 		.name = "dcdc3",
253 		.enable_reg = AXP221_POWERCTL_1,
254 		.enable_mask = AXP221_POWERCTL1_DCDC3,
255 		.voltage_reg = AXP221_REG_DCDC3_VOLTAGE,
256 		.voltage_mask = 0x3f,
257 		.voltage_min = 600,
258 		.voltage_max = 1860,
259 		.voltage_step = 20,
260 		.voltage_nstep = 63,
261 	},
262 	{
263 		.id = AXP221_REG_ID_DCDC4,
264 		.name = "dcdc4",
265 		.enable_reg = AXP221_POWERCTL_1,
266 		.enable_mask = AXP221_POWERCTL1_DCDC4,
267 		.voltage_reg = AXP221_REG_DCDC4_VOLTAGE,
268 		.voltage_mask = 0x3f,
269 		.voltage_min = 600,
270 		.voltage_max = 1540,
271 		.voltage_step = 20,
272 		.voltage_nstep = 47,
273 	},
274 	{
275 		.id = AXP221_REG_ID_DCDC5,
276 		.name = "dcdc5",
277 		.enable_reg = AXP221_POWERCTL_1,
278 		.enable_mask = AXP221_POWERCTL1_DCDC5,
279 		.voltage_reg = AXP221_REG_DCDC5_VOLTAGE,
280 		.voltage_mask = 0x1f,
281 		.voltage_min = 1000,
282 		.voltage_max = 2550,
283 		.voltage_step = 50,
284 		.voltage_nstep = 31,
285 	},
286 	{
287 		.id = AXP221_REG_ID_ALDO1,
288 		.name = "aldo1",
289 		.enable_reg = AXP221_POWERCTL_1,
290 		.enable_mask = AXP221_POWERCTL1_ALDO1,
291 		.voltage_reg = AXP221_REG_ALDO1_VOLTAGE,
292 		.voltage_mask = 0x1f,
293 		.voltage_min = 700,
294 		.voltage_max = 3300,
295 		.voltage_step = 100,
296 		.voltage_nstep = 26,
297 	},
298 	{
299 		.id = AXP221_REG_ID_ALDO2,
300 		.name = "aldo2",
301 		.enable_reg = AXP221_POWERCTL_1,
302 		.enable_mask = AXP221_POWERCTL1_ALDO2,
303 		.voltage_reg = AXP221_REG_ALDO2_VOLTAGE,
304 		.voltage_mask = 0x1f,
305 		.voltage_min = 700,
306 		.voltage_max = 3300,
307 		.voltage_step = 100,
308 		.voltage_nstep = 26,
309 	},
310 	{
311 		.id = AXP221_REG_ID_ALDO3,
312 		.name = "aldo3",
313 		.enable_reg = AXP221_POWERCTL_3,
314 		.enable_mask = AXP221_POWERCTL3_ALDO3,
315 		.voltage_reg = AXP221_REG_ALDO3_VOLTAGE,
316 		.voltage_mask = 0x1f,
317 		.voltage_min = 700,
318 		.voltage_max = 3300,
319 		.voltage_step = 100,
320 		.voltage_nstep = 26,
321 	},
322 	{
323 		.id = AXP221_REG_ID_DC1SW,
324 		.name = "dc1sw",
325 		.enable_reg = AXP221_POWERCTL_2,
326 		.enable_mask = AXP221_POWERCTL2_DC1SW,
327 	},
328 };
329 
330 struct axp2xx_reg_sc {
331 	struct regnode		*regnode;
332 	device_t		base_dev;
333 	struct axp2xx_regdef	*def;
334 	phandle_t		xref;
335 	struct regnode_std_param *param;
336 };
337 
338 struct axp2xx_pins {
339 	const char	*name;
340 	uint8_t		ctrl_reg;
341 	uint8_t		status_reg;
342 	uint8_t		status_mask;
343 	uint8_t		status_shift;
344 };
345 
346 /* GPIO3 is different, don't expose it for now */
347 static const struct axp2xx_pins axp209_pins[] = {
348 	{
349 		.name = "GPIO0",
350 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
351 		.status_reg = AXP2XX_GPIO_STATUS,
352 		.status_mask = 0x10,
353 		.status_shift = 4,
354 	},
355 	{
356 		.name = "GPIO1",
357 		.ctrl_reg = AXP2XX_GPIO1_CTRL,
358 		.status_reg = AXP2XX_GPIO_STATUS,
359 		.status_mask = 0x20,
360 		.status_shift = 5,
361 	},
362 	{
363 		.name = "GPIO2",
364 		.ctrl_reg = AXP209_GPIO2_CTRL,
365 		.status_reg = AXP2XX_GPIO_STATUS,
366 		.status_mask = 0x40,
367 		.status_shift = 6,
368 	},
369 };
370 
371 static const struct axp2xx_pins axp221_pins[] = {
372 	{
373 		.name = "GPIO0",
374 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
375 		.status_reg = AXP2XX_GPIO_STATUS,
376 		.status_mask = 0x1,
377 		.status_shift = 0x0,
378 	},
379 	{
380 		.name = "GPIO1",
381 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
382 		.status_reg = AXP2XX_GPIO_STATUS,
383 		.status_mask = 0x2,
384 		.status_shift = 0x1,
385 	},
386 };
387 
388 struct axp2xx_sensors {
389 	int		id;
390 	const char	*name;
391 	const char	*desc;
392 	const char	*format;
393 	uint8_t		enable_reg;
394 	uint8_t		enable_mask;
395 	uint8_t		value_reg;
396 	uint8_t		value_size;
397 	uint8_t		h_value_mask;
398 	uint8_t		h_value_shift;
399 	uint8_t		l_value_mask;
400 	uint8_t		l_value_shift;
401 	int		value_step;
402 	int		value_convert;
403 };
404 
405 static const struct axp2xx_sensors axp209_sensors[] = {
406 	{
407 		.id = AXP209_ACVOLT,
408 		.name = "acvolt",
409 		.desc = "AC Voltage (microvolt)",
410 		.format = "I",
411 		.enable_reg = AXP2XX_ADC_ENABLE1,
412 		.enable_mask = AXP209_ADC1_ACVOLT,
413 		.value_reg = AXP209_ACIN_VOLTAGE,
414 		.value_size = 2,
415 		.h_value_mask = 0xff,
416 		.h_value_shift = 4,
417 		.l_value_mask = 0xf,
418 		.l_value_shift = 0,
419 		.value_step = AXP209_VOLT_STEP,
420 	},
421 	{
422 		.id = AXP209_ACCURRENT,
423 		.name = "accurrent",
424 		.desc = "AC Current (microAmpere)",
425 		.format = "I",
426 		.enable_reg = AXP2XX_ADC_ENABLE1,
427 		.enable_mask = AXP209_ADC1_ACCURRENT,
428 		.value_reg = AXP209_ACIN_CURRENT,
429 		.value_size = 2,
430 		.h_value_mask = 0xff,
431 		.h_value_shift = 4,
432 		.l_value_mask = 0xf,
433 		.l_value_shift = 0,
434 		.value_step = AXP209_ACCURRENT_STEP,
435 	},
436 	{
437 		.id = AXP209_VBUSVOLT,
438 		.name = "vbusvolt",
439 		.desc = "VBUS Voltage (microVolt)",
440 		.format = "I",
441 		.enable_reg = AXP2XX_ADC_ENABLE1,
442 		.enable_mask = AXP209_ADC1_VBUSVOLT,
443 		.value_reg = AXP209_VBUS_VOLTAGE,
444 		.value_size = 2,
445 		.h_value_mask = 0xff,
446 		.h_value_shift = 4,
447 		.l_value_mask = 0xf,
448 		.l_value_shift = 0,
449 		.value_step = AXP209_VOLT_STEP,
450 	},
451 	{
452 		.id = AXP209_VBUSCURRENT,
453 		.name = "vbuscurrent",
454 		.desc = "VBUS Current (microAmpere)",
455 		.format = "I",
456 		.enable_reg = AXP2XX_ADC_ENABLE1,
457 		.enable_mask = AXP209_ADC1_VBUSCURRENT,
458 		.value_reg = AXP209_VBUS_CURRENT,
459 		.value_size = 2,
460 		.h_value_mask = 0xff,
461 		.h_value_shift = 4,
462 		.l_value_mask = 0xf,
463 		.l_value_shift = 0,
464 		.value_step = AXP209_VBUSCURRENT_STEP,
465 	},
466 	{
467 		.id = AXP2XX_BATVOLT,
468 		.name = "batvolt",
469 		.desc = "Battery Voltage (microVolt)",
470 		.format = "I",
471 		.enable_reg = AXP2XX_ADC_ENABLE1,
472 		.enable_mask = AXP2XX_ADC1_BATVOLT,
473 		.value_reg = AXP2XX_BAT_VOLTAGE,
474 		.value_size = 2,
475 		.h_value_mask = 0xff,
476 		.h_value_shift = 4,
477 		.l_value_mask = 0xf,
478 		.l_value_shift = 0,
479 		.value_step = AXP2XX_BATVOLT_STEP,
480 	},
481 	{
482 		.id = AXP2XX_BATCHARGECURRENT,
483 		.name = "batchargecurrent",
484 		.desc = "Battery Charging Current (microAmpere)",
485 		.format = "I",
486 		.enable_reg = AXP2XX_ADC_ENABLE1,
487 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
488 		.value_reg = AXP2XX_BAT_CHARGE_CURRENT,
489 		.value_size = 2,
490 		.h_value_mask = 0xff,
491 		.h_value_shift = 5,
492 		.l_value_mask = 0x1f,
493 		.l_value_shift = 0,
494 		.value_step = AXP2XX_BATCURRENT_STEP,
495 	},
496 	{
497 		.id = AXP2XX_BATDISCHARGECURRENT,
498 		.name = "batdischargecurrent",
499 		.desc = "Battery Discharging Current (microAmpere)",
500 		.format = "I",
501 		.enable_reg = AXP2XX_ADC_ENABLE1,
502 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
503 		.value_reg = AXP2XX_BAT_DISCHARGE_CURRENT,
504 		.value_size = 2,
505 		.h_value_mask = 0xff,
506 		.h_value_shift = 5,
507 		.l_value_mask = 0x1f,
508 		.l_value_shift = 0,
509 		.value_step = AXP2XX_BATCURRENT_STEP,
510 	},
511 	{
512 		.id = AXP2XX_TEMP,
513 		.name = "temp",
514 		.desc = "Internal Temperature",
515 		.format = "IK",
516 		.enable_reg = AXP209_ADC_ENABLE2,
517 		.enable_mask = AXP209_ADC2_TEMP,
518 		.value_reg = AXP209_TEMPMON,
519 		.value_size = 2,
520 		.h_value_mask = 0xff,
521 		.h_value_shift = 4,
522 		.l_value_mask = 0xf,
523 		.l_value_shift = 0,
524 		.value_step = 1,
525 		.value_convert = -(AXP209_TEMPMON_MIN - AXP209_0C_TO_K),
526 	},
527 };
528 
529 static const struct axp2xx_sensors axp221_sensors[] = {
530 	{
531 		.id = AXP2XX_BATVOLT,
532 		.name = "batvolt",
533 		.desc = "Battery Voltage (microVolt)",
534 		.format = "I",
535 		.enable_reg = AXP2XX_ADC_ENABLE1,
536 		.enable_mask = AXP2XX_ADC1_BATVOLT,
537 		.value_reg = AXP2XX_BAT_VOLTAGE,
538 		.value_size = 2,
539 		.h_value_mask = 0xff,
540 		.h_value_shift = 4,
541 		.l_value_mask = 0xf,
542 		.l_value_shift = 0,
543 		.value_step = AXP2XX_BATVOLT_STEP,
544 	},
545 	{
546 		.id = AXP2XX_BATCHARGECURRENT,
547 		.name = "batchargecurrent",
548 		.desc = "Battery Charging Current (microAmpere)",
549 		.format = "I",
550 		.enable_reg = AXP2XX_ADC_ENABLE1,
551 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
552 		.value_reg = AXP2XX_BAT_CHARGE_CURRENT,
553 		.value_size = 2,
554 		.h_value_mask = 0xff,
555 		.h_value_shift = 5,
556 		.l_value_mask = 0x1f,
557 		.l_value_shift = 0,
558 		.value_step = AXP2XX_BATCURRENT_STEP,
559 	},
560 	{
561 		.id = AXP2XX_BATDISCHARGECURRENT,
562 		.name = "batdischargecurrent",
563 		.desc = "Battery Discharging Current (microAmpere)",
564 		.format = "I",
565 		.enable_reg = AXP2XX_ADC_ENABLE1,
566 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
567 		.value_reg = AXP2XX_BAT_DISCHARGE_CURRENT,
568 		.value_size = 2,
569 		.h_value_mask = 0xff,
570 		.h_value_shift = 5,
571 		.l_value_mask = 0x1f,
572 		.l_value_shift = 0,
573 		.value_step = AXP2XX_BATCURRENT_STEP,
574 	},
575 	{
576 		.id = AXP2XX_TEMP,
577 		.name = "temp",
578 		.desc = "Internal Temperature",
579 		.format = "IK",
580 		.enable_reg = AXP2XX_ADC_ENABLE1,
581 		.enable_mask = AXP221_ADC1_TEMP,
582 		.value_reg = AXP221_TEMPMON,
583 		.value_size = 2,
584 		.h_value_mask = 0xff,
585 		.h_value_shift = 4,
586 		.l_value_mask = 0xf,
587 		.l_value_shift = 0,
588 		.value_step = 1,
589 		.value_convert = -(AXP221_TEMPMON_MIN - AXP209_0C_TO_K),
590 	},
591 };
592 
593 enum AXP2XX_TYPE {
594 	AXP209 = 1,
595 	AXP221,
596 };
597 
598 struct axp2xx_softc {
599 	device_t		dev;
600 	struct resource *	res[1];
601 	void *			intrcookie;
602 	struct intr_config_hook	intr_hook;
603 	struct mtx		mtx;
604 	uint8_t			type;
605 
606 	/* GPIO */
607 	device_t		gpiodev;
608 	int			npins;
609 	const struct axp2xx_pins	*pins;
610 
611 	/* Sensors */
612 	const struct axp2xx_sensors	*sensors;
613 	int				nsensors;
614 
615 	/* Regulators */
616 	struct axp2xx_reg_sc	**regs;
617 	int			nregs;
618 	struct axp2xx_regdef	*regdefs;
619 };
620 
621 static struct ofw_compat_data compat_data[] = {
622 	{ "x-powers,axp209",		AXP209 },
623 	{ "x-powers,axp221",		AXP221 },
624 	{ NULL,				0 }
625 };
626 
627 static struct resource_spec axp_res_spec[] = {
628 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
629 	{ -1,			0,	0 }
630 };
631 
632 #define	AXP_LOCK(sc)	mtx_lock(&(sc)->mtx)
633 #define	AXP_UNLOCK(sc)	mtx_unlock(&(sc)->mtx)
634 
635 static int
636 axp2xx_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
637 {
638 
639 	return (iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT));
640 }
641 
642 static int
643 axp2xx_write(device_t dev, uint8_t reg, uint8_t data)
644 {
645 
646 	return (iicdev_writeto(dev, reg, &data, sizeof(data), IIC_INTRWAIT));
647 }
648 
649 static int
650 axp2xx_regnode_init(struct regnode *regnode)
651 {
652 	return (0);
653 }
654 
655 static int
656 axp2xx_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
657 {
658 	struct axp2xx_reg_sc *sc;
659 	uint8_t val;
660 
661 	sc = regnode_get_softc(regnode);
662 
663 	axp2xx_read(sc->base_dev, sc->def->enable_reg, &val, 1);
664 	if (enable)
665 		val |= sc->def->enable_mask;
666 	else
667 		val &= ~sc->def->enable_mask;
668 	axp2xx_write(sc->base_dev, sc->def->enable_reg, val);
669 
670 	*udelay = 0;
671 
672 	return (0);
673 }
674 
675 static void
676 axp2xx_regnode_reg_to_voltage(struct axp2xx_reg_sc *sc, uint8_t val, int *uv)
677 {
678 	if (val < sc->def->voltage_nstep)
679 		*uv = sc->def->voltage_min + val * sc->def->voltage_step;
680 	else
681 		*uv = sc->def->voltage_min +
682 		       (sc->def->voltage_nstep * sc->def->voltage_step);
683 	*uv *= 1000;
684 }
685 
686 static int
687 axp2xx_regnode_voltage_to_reg(struct axp2xx_reg_sc *sc, int min_uvolt,
688     int max_uvolt, uint8_t *val)
689 {
690 	uint8_t nval;
691 	int nstep, uvolt;
692 
693 	nval = 0;
694 	uvolt = sc->def->voltage_min * 1000;
695 
696 	for (nstep = 0; nstep < sc->def->voltage_nstep && uvolt < min_uvolt;
697 	     nstep++) {
698 		++nval;
699 		uvolt += (sc->def->voltage_step * 1000);
700 	}
701 	if (uvolt > max_uvolt)
702 		return (EINVAL);
703 
704 	*val = nval;
705 	return (0);
706 }
707 
708 static int
709 axp2xx_regnode_status(struct regnode *regnode, int *status)
710 {
711 	struct axp2xx_reg_sc *sc;
712 	uint8_t val;
713 
714 	sc = regnode_get_softc(regnode);
715 
716 	*status = 0;
717 	axp2xx_read(sc->base_dev, sc->def->enable_reg, &val, 1);
718 	if (val & sc->def->enable_mask)
719 		*status = REGULATOR_STATUS_ENABLED;
720 
721 	return (0);
722 }
723 
724 static int
725 axp2xx_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
726     int max_uvolt, int *udelay)
727 {
728 	struct axp2xx_reg_sc *sc;
729 	uint8_t val;
730 
731 	sc = regnode_get_softc(regnode);
732 
733 	if (!sc->def->voltage_step)
734 		return (ENXIO);
735 
736 	if (axp2xx_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0)
737 		return (ERANGE);
738 
739 	axp2xx_write(sc->base_dev, sc->def->voltage_reg, val);
740 
741 	*udelay = 0;
742 
743 	return (0);
744 }
745 
746 static int
747 axp2xx_regnode_get_voltage(struct regnode *regnode, int *uvolt)
748 {
749 	struct axp2xx_reg_sc *sc;
750 	uint8_t val;
751 
752 	sc = regnode_get_softc(regnode);
753 
754 	if (!sc->def->voltage_step)
755 		return (ENXIO);
756 
757 	axp2xx_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
758 	axp2xx_regnode_reg_to_voltage(sc, val & sc->def->voltage_mask, uvolt);
759 
760 	return (0);
761 }
762 
763 static regnode_method_t axp2xx_regnode_methods[] = {
764 	/* Regulator interface */
765 	REGNODEMETHOD(regnode_init,		axp2xx_regnode_init),
766 	REGNODEMETHOD(regnode_enable,		axp2xx_regnode_enable),
767 	REGNODEMETHOD(regnode_status,		axp2xx_regnode_status),
768 	REGNODEMETHOD(regnode_set_voltage,	axp2xx_regnode_set_voltage),
769 	REGNODEMETHOD(regnode_get_voltage,	axp2xx_regnode_get_voltage),
770 	REGNODEMETHOD(regnode_check_voltage,	regnode_method_check_voltage),
771 	REGNODEMETHOD_END
772 };
773 DEFINE_CLASS_1(axp2xx_regnode, axp2xx_regnode_class, axp2xx_regnode_methods,
774     sizeof(struct axp2xx_reg_sc), regnode_class);
775 
776 static int
777 axp2xx_sysctl(SYSCTL_HANDLER_ARGS)
778 {
779 	struct axp2xx_softc *sc;
780 	device_t dev = arg1;
781 	enum axp2xx_sensor sensor = arg2;
782 	uint8_t data[2];
783 	int val, error, i, found;
784 
785 	sc = device_get_softc(dev);
786 
787 	for (found = 0, i = 0; i < sc->nsensors; i++) {
788 		if (sc->sensors[i].id == sensor) {
789 			found = 1;
790 			break;
791 		}
792 	}
793 
794 	if (found == 0)
795 		return (ENOENT);
796 
797 	error = axp2xx_read(dev, sc->sensors[i].value_reg, data, 2);
798 	if (error != 0)
799 		return (error);
800 
801 	val = ((data[0] & sc->sensors[i].h_value_mask) <<
802 	    sc->sensors[i].h_value_shift);
803 	val |= ((data[1] & sc->sensors[i].l_value_mask) <<
804 	    sc->sensors[i].l_value_shift);
805 	val *= sc->sensors[i].value_step;
806 	val += sc->sensors[i].value_convert;
807 
808 	return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
809 }
810 
811 static void
812 axp2xx_shutdown(void *devp, int howto)
813 {
814 	device_t dev;
815 
816 	if (!(howto & RB_POWEROFF))
817 		return;
818 	dev = (device_t)devp;
819 
820 	if (bootverbose)
821 		device_printf(dev, "Shutdown AXP2xx\n");
822 
823 	axp2xx_write(dev, AXP2XX_SHUTBAT, AXP2XX_SHUTBAT_SHUTDOWN);
824 }
825 
826 static void
827 axp2xx_intr(void *arg)
828 {
829 	struct axp2xx_softc *sc;
830 	uint8_t reg;
831 
832 	sc = arg;
833 
834 	axp2xx_read(sc->dev, AXP2XX_IRQ1_STATUS, &reg, 1);
835 	if (reg) {
836 		if (reg & AXP2XX_IRQ1_AC_OVERVOLT)
837 			devctl_notify("PMU", "AC", "overvoltage", NULL);
838 		if (reg & AXP2XX_IRQ1_VBUS_OVERVOLT)
839 			devctl_notify("PMU", "USB", "overvoltage", NULL);
840 		if (reg & AXP2XX_IRQ1_VBUS_LOW)
841 			devctl_notify("PMU", "USB", "undervoltage", NULL);
842 		if (reg & AXP2XX_IRQ1_AC_CONN)
843 			devctl_notify("PMU", "AC", "plugged", NULL);
844 		if (reg & AXP2XX_IRQ1_AC_DISCONN)
845 			devctl_notify("PMU", "AC", "unplugged", NULL);
846 		if (reg & AXP2XX_IRQ1_VBUS_CONN)
847 			devctl_notify("PMU", "USB", "plugged", NULL);
848 		if (reg & AXP2XX_IRQ1_VBUS_DISCONN)
849 			devctl_notify("PMU", "USB", "unplugged", NULL);
850 		axp2xx_write(sc->dev, AXP2XX_IRQ1_STATUS, AXP2XX_IRQ_ACK);
851 	}
852 
853 	axp2xx_read(sc->dev, AXP2XX_IRQ2_STATUS, &reg, 1);
854 	if (reg) {
855 		if (reg & AXP2XX_IRQ2_BATT_CHARGED)
856 			devctl_notify("PMU", "Battery", "charged", NULL);
857 		if (reg & AXP2XX_IRQ2_BATT_CHARGING)
858 			devctl_notify("PMU", "Battery", "charging", NULL);
859 		if (reg & AXP2XX_IRQ2_BATT_CONN)
860 			devctl_notify("PMU", "Battery", "connected", NULL);
861 		if (reg & AXP2XX_IRQ2_BATT_DISCONN)
862 			devctl_notify("PMU", "Battery", "disconnected", NULL);
863 		if (reg & AXP2XX_IRQ2_BATT_TEMP_LOW)
864 			devctl_notify("PMU", "Battery", "low-temp", NULL);
865 		if (reg & AXP2XX_IRQ2_BATT_TEMP_OVER)
866 			devctl_notify("PMU", "Battery", "high-temp", NULL);
867 		axp2xx_write(sc->dev, AXP2XX_IRQ2_STATUS, AXP2XX_IRQ_ACK);
868 	}
869 
870 	axp2xx_read(sc->dev, AXP2XX_IRQ3_STATUS, &reg, 1);
871 	if (reg) {
872 		if (reg & AXP2XX_IRQ3_PEK_SHORT)
873 			shutdown_nice(RB_POWEROFF);
874 		axp2xx_write(sc->dev, AXP2XX_IRQ3_STATUS, AXP2XX_IRQ_ACK);
875 	}
876 
877 	axp2xx_read(sc->dev, AXP2XX_IRQ4_STATUS, &reg, 1);
878 	if (reg) {
879 		axp2xx_write(sc->dev, AXP2XX_IRQ4_STATUS, AXP2XX_IRQ_ACK);
880 	}
881 
882 	axp2xx_read(sc->dev, AXP2XX_IRQ5_STATUS, &reg, 1);
883 	if (reg) {
884 		axp2xx_write(sc->dev, AXP2XX_IRQ5_STATUS, AXP2XX_IRQ_ACK);
885 	}
886 }
887 
888 static device_t
889 axp2xx_gpio_get_bus(device_t dev)
890 {
891 	struct axp2xx_softc *sc;
892 
893 	sc = device_get_softc(dev);
894 
895 	return (sc->gpiodev);
896 }
897 
898 static int
899 axp2xx_gpio_pin_max(device_t dev, int *maxpin)
900 {
901 	struct axp2xx_softc *sc;
902 
903 	sc = device_get_softc(dev);
904 
905 	*maxpin = sc->npins - 1;
906 
907 	return (0);
908 }
909 
910 static int
911 axp2xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
912 {
913 	struct axp2xx_softc *sc;
914 
915 	sc = device_get_softc(dev);
916 
917 	if (pin >= sc->npins)
918 		return (EINVAL);
919 
920 	snprintf(name, GPIOMAXNAME, "%s", axp209_pins[pin].name);
921 
922 	return (0);
923 }
924 
925 static int
926 axp2xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
927 {
928 	struct axp2xx_softc *sc;
929 
930 	sc = device_get_softc(dev);
931 
932 	if (pin >= sc->npins)
933 		return (EINVAL);
934 
935 	*caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
936 
937 	return (0);
938 }
939 
940 static int
941 axp2xx_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
942 {
943 	struct axp2xx_softc *sc;
944 	uint8_t data, func;
945 	int error;
946 
947 	sc = device_get_softc(dev);
948 
949 	if (pin >= sc->npins)
950 		return (EINVAL);
951 
952 	AXP_LOCK(sc);
953 	error = axp2xx_read(dev, sc->pins[pin].ctrl_reg, &data, 1);
954 	if (error == 0) {
955 		func = data & AXP2XX_GPIO_FUNC_MASK;
956 		if (func == AXP2XX_GPIO_FUNC_INPUT)
957 			*flags = GPIO_PIN_INPUT;
958 		else if (func == AXP2XX_GPIO_FUNC_DRVLO ||
959 		    func == AXP2XX_GPIO_FUNC_DRVHI)
960 			*flags = GPIO_PIN_OUTPUT;
961 		else
962 			*flags = 0;
963 	}
964 	AXP_UNLOCK(sc);
965 
966 	return (error);
967 }
968 
969 static int
970 axp2xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
971 {
972 	struct axp2xx_softc *sc;
973 	uint8_t data;
974 	int error;
975 
976 	sc = device_get_softc(dev);
977 
978 	if (pin >= sc->npins)
979 		return (EINVAL);
980 
981 	AXP_LOCK(sc);
982 	error = axp2xx_read(dev, sc->pins[pin].ctrl_reg, &data, 1);
983 	if (error == 0) {
984 		data &= ~AXP2XX_GPIO_FUNC_MASK;
985 		if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) {
986 			if ((flags & GPIO_PIN_OUTPUT) == 0)
987 				data |= AXP2XX_GPIO_FUNC_INPUT;
988 		}
989 		error = axp2xx_write(dev, sc->pins[pin].ctrl_reg, data);
990 	}
991 	AXP_UNLOCK(sc);
992 
993 	return (error);
994 }
995 
996 static int
997 axp2xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
998 {
999 	struct axp2xx_softc *sc;
1000 	uint8_t data, func;
1001 	int error;
1002 
1003 	sc = device_get_softc(dev);
1004 
1005 	if (pin >= sc->npins)
1006 		return (EINVAL);
1007 
1008 	AXP_LOCK(sc);
1009 	error = axp2xx_read(dev, sc->pins[pin].ctrl_reg, &data, 1);
1010 	if (error == 0) {
1011 		func = data & AXP2XX_GPIO_FUNC_MASK;
1012 		switch (func) {
1013 		case AXP2XX_GPIO_FUNC_DRVLO:
1014 			*val = 0;
1015 			break;
1016 		case AXP2XX_GPIO_FUNC_DRVHI:
1017 			*val = 1;
1018 			break;
1019 		case AXP2XX_GPIO_FUNC_INPUT:
1020 			error = axp2xx_read(dev, sc->pins[pin].status_reg,
1021 			    &data, 1);
1022 			if (error == 0) {
1023 				*val = (data & sc->pins[pin].status_mask);
1024 				*val >>= sc->pins[pin].status_shift;
1025 			}
1026 			break;
1027 		default:
1028 			error = EIO;
1029 			break;
1030 		}
1031 	}
1032 	AXP_UNLOCK(sc);
1033 
1034 	return (error);
1035 }
1036 
1037 static int
1038 axp2xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
1039 {
1040 	struct axp2xx_softc *sc;
1041 	uint8_t data, func;
1042 	int error;
1043 
1044 	sc = device_get_softc(dev);
1045 
1046 	if (pin >= sc->npins)
1047 		return (EINVAL);
1048 
1049 	AXP_LOCK(sc);
1050 	error = axp2xx_read(dev, sc->pins[pin].ctrl_reg, &data, 1);
1051 	if (error == 0) {
1052 		func = data & AXP2XX_GPIO_FUNC_MASK;
1053 		switch (func) {
1054 		case AXP2XX_GPIO_FUNC_DRVLO:
1055 		case AXP2XX_GPIO_FUNC_DRVHI:
1056 			/* GPIO2 can't be set to 1 */
1057 			if (pin == 2 && val == 1) {
1058 				error = EINVAL;
1059 				break;
1060 			}
1061 			data &= ~AXP2XX_GPIO_FUNC_MASK;
1062 			data |= val;
1063 			break;
1064 		default:
1065 			error = EIO;
1066 			break;
1067 		}
1068 	}
1069 	if (error == 0)
1070 		error = axp2xx_write(dev, sc->pins[pin].ctrl_reg, data);
1071 	AXP_UNLOCK(sc);
1072 
1073 	return (error);
1074 }
1075 
1076 static int
1077 axp2xx_gpio_pin_toggle(device_t dev, uint32_t pin)
1078 {
1079 	struct axp2xx_softc *sc;
1080 	uint8_t data, func;
1081 	int error;
1082 
1083 	sc = device_get_softc(dev);
1084 
1085 	if (pin >= sc->npins)
1086 		return (EINVAL);
1087 
1088 	AXP_LOCK(sc);
1089 	error = axp2xx_read(dev, sc->pins[pin].ctrl_reg, &data, 1);
1090 	if (error == 0) {
1091 		func = data & AXP2XX_GPIO_FUNC_MASK;
1092 		switch (func) {
1093 		case AXP2XX_GPIO_FUNC_DRVLO:
1094 			/* Pin 2 can't be set to 1*/
1095 			if (pin == 2) {
1096 				error = EINVAL;
1097 				break;
1098 			}
1099 			data &= ~AXP2XX_GPIO_FUNC_MASK;
1100 			data |= AXP2XX_GPIO_FUNC_DRVHI;
1101 			break;
1102 		case AXP2XX_GPIO_FUNC_DRVHI:
1103 			data &= ~AXP2XX_GPIO_FUNC_MASK;
1104 			data |= AXP2XX_GPIO_FUNC_DRVLO;
1105 			break;
1106 		default:
1107 			error = EIO;
1108 			break;
1109 		}
1110 	}
1111 	if (error == 0)
1112 		error = axp2xx_write(dev, sc->pins[pin].ctrl_reg, data);
1113 	AXP_UNLOCK(sc);
1114 
1115 	return (error);
1116 }
1117 
1118 static int
1119 axp2xx_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent,
1120     int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
1121 {
1122 	struct axp2xx_softc *sc;
1123 
1124 	sc = device_get_softc(bus);
1125 
1126 	if (gpios[0] >= sc->npins)
1127 		return (EINVAL);
1128 
1129 	*pin = gpios[0];
1130 	*flags = gpios[1];
1131 
1132 	return (0);
1133 }
1134 
1135 static phandle_t
1136 axp2xx_get_node(device_t dev, device_t bus)
1137 {
1138 	return (ofw_bus_get_node(dev));
1139 }
1140 
1141 static struct axp2xx_reg_sc *
1142 axp2xx_reg_attach(device_t dev, phandle_t node,
1143     struct axp2xx_regdef *def)
1144 {
1145 	struct axp2xx_reg_sc *reg_sc;
1146 	struct regnode_init_def initdef;
1147 	struct regnode *regnode;
1148 
1149 	memset(&initdef, 0, sizeof(initdef));
1150 	if (regulator_parse_ofw_stdparam(dev, node, &initdef) != 0) {
1151 		device_printf(dev, "cannot create regulator\n");
1152 		return (NULL);
1153 	}
1154 	if (initdef.std_param.min_uvolt == 0)
1155 		initdef.std_param.min_uvolt = def->voltage_min * 1000;
1156 	if (initdef.std_param.max_uvolt == 0)
1157 		initdef.std_param.max_uvolt = def->voltage_max * 1000;
1158 	initdef.id = def->id;
1159 	initdef.ofw_node = node;
1160 	regnode = regnode_create(dev, &axp2xx_regnode_class, &initdef);
1161 	if (regnode == NULL) {
1162 		device_printf(dev, "cannot create regulator\n");
1163 		return (NULL);
1164 	}
1165 
1166 	reg_sc = regnode_get_softc(regnode);
1167 	reg_sc->regnode = regnode;
1168 	reg_sc->base_dev = dev;
1169 	reg_sc->def = def;
1170 	reg_sc->xref = OF_xref_from_node(node);
1171 	reg_sc->param = regnode_get_stdparam(regnode);
1172 
1173 	regnode_register(regnode);
1174 
1175 	return (reg_sc);
1176 }
1177 
1178 static int
1179 axp2xx_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
1180     intptr_t *num)
1181 {
1182 	struct axp2xx_softc *sc;
1183 	int i;
1184 
1185 	sc = device_get_softc(dev);
1186 	for (i = 0; i < sc->nregs; i++) {
1187 		if (sc->regs[i] == NULL)
1188 			continue;
1189 		if (sc->regs[i]->xref == xref) {
1190 			*num = sc->regs[i]->def->id;
1191 			return (0);
1192 		}
1193 	}
1194 
1195 	return (ENXIO);
1196 }
1197 
1198 static void
1199 axp2xx_start(void *pdev)
1200 {
1201 	device_t dev;
1202 	struct axp2xx_softc *sc;
1203 	const char *pwr_name[] = {"Battery", "AC", "USB", "AC and USB"};
1204 	int i;
1205 	uint8_t reg, data;
1206 	uint8_t pwr_src;
1207 
1208 	dev = pdev;
1209 
1210 	sc = device_get_softc(dev);
1211 	sc->dev = dev;
1212 
1213 	if (bootverbose) {
1214 		/*
1215 		 * Read the Power State register.
1216 		 * Shift the AC presence into bit 0.
1217 		 * Shift the Battery presence into bit 1.
1218 		 */
1219 		axp2xx_read(dev, AXP2XX_PSR, &data, 1);
1220 		pwr_src = ((data & AXP2XX_PSR_ACIN) >> AXP2XX_PSR_ACIN_SHIFT) |
1221 		    ((data & AXP2XX_PSR_VBUS) >> (AXP2XX_PSR_VBUS_SHIFT - 1));
1222 
1223 		device_printf(dev, "Powered by %s\n",
1224 		    pwr_name[pwr_src]);
1225 	}
1226 
1227 	/* Only enable interrupts that we are interested in */
1228 	axp2xx_write(dev, AXP2XX_IRQ1_ENABLE,
1229 	    AXP2XX_IRQ1_AC_OVERVOLT |
1230 	    AXP2XX_IRQ1_AC_DISCONN |
1231 	    AXP2XX_IRQ1_AC_CONN |
1232 	    AXP2XX_IRQ1_VBUS_OVERVOLT |
1233 	    AXP2XX_IRQ1_VBUS_DISCONN |
1234 	    AXP2XX_IRQ1_VBUS_CONN);
1235 	axp2xx_write(dev, AXP2XX_IRQ2_ENABLE,
1236 	    AXP2XX_IRQ2_BATT_CONN |
1237 	    AXP2XX_IRQ2_BATT_DISCONN |
1238 	    AXP2XX_IRQ2_BATT_CHARGE_ACCT_ON |
1239 	    AXP2XX_IRQ2_BATT_CHARGE_ACCT_OFF |
1240 	    AXP2XX_IRQ2_BATT_CHARGING |
1241 	    AXP2XX_IRQ2_BATT_CHARGED |
1242 	    AXP2XX_IRQ2_BATT_TEMP_OVER |
1243 	    AXP2XX_IRQ2_BATT_TEMP_LOW);
1244 	axp2xx_write(dev, AXP2XX_IRQ3_ENABLE,
1245 	    AXP2XX_IRQ3_PEK_SHORT | AXP2XX_IRQ3_PEK_LONG);
1246 	axp2xx_write(dev, AXP2XX_IRQ4_ENABLE, AXP2XX_IRQ4_APS_LOW_2);
1247 	axp2xx_write(dev, AXP2XX_IRQ5_ENABLE, 0x0);
1248 
1249 	EVENTHANDLER_REGISTER(shutdown_final, axp2xx_shutdown, dev,
1250 	    SHUTDOWN_PRI_LAST);
1251 
1252 	/* Enable ADC sensors */
1253 	for (i = 0; i < sc->nsensors; i++) {
1254 		if (axp2xx_read(dev, sc->sensors[i].enable_reg, &reg, 1) == -1) {
1255 			device_printf(dev, "Cannot enable sensor '%s'\n",
1256 			    sc->sensors[i].name);
1257 			continue;
1258 		}
1259 		reg |= sc->sensors[i].enable_mask;
1260 		if (axp2xx_write(dev, sc->sensors[i].enable_reg, reg) == -1) {
1261 			device_printf(dev, "Cannot enable sensor '%s'\n",
1262 			    sc->sensors[i].name);
1263 			continue;
1264 		}
1265 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1266 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1267 		    OID_AUTO, sc->sensors[i].name,
1268 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
1269 		    dev, sc->sensors[i].id, axp2xx_sysctl,
1270 		    sc->sensors[i].format,
1271 		    sc->sensors[i].desc);
1272 	}
1273 
1274 	if ((bus_setup_intr(dev, sc->res[0], INTR_TYPE_MISC | INTR_MPSAFE,
1275 	      NULL, axp2xx_intr, sc, &sc->intrcookie)))
1276 		device_printf(dev, "unable to register interrupt handler\n");
1277 
1278 	config_intrhook_disestablish(&sc->intr_hook);
1279 }
1280 
1281 static int
1282 axp2xx_probe(device_t dev)
1283 {
1284 
1285 	if (!ofw_bus_status_okay(dev))
1286 		return (ENXIO);
1287 
1288 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1289 	{
1290 	case AXP209:
1291 		device_set_desc(dev, "X-Powers AXP209 Power Management Unit");
1292 		break;
1293 	case AXP221:
1294 		device_set_desc(dev, "X-Powers AXP221 Power Management Unit");
1295 		break;
1296 	default:
1297 		return (ENXIO);
1298 	}
1299 
1300 	return (BUS_PROBE_DEFAULT);
1301 }
1302 
1303 static int
1304 axp2xx_attach(device_t dev)
1305 {
1306 	struct axp2xx_softc *sc;
1307 	struct axp2xx_reg_sc *reg;
1308 	struct axp2xx_regdef *regdefs;
1309 	phandle_t rnode, child;
1310 	int i;
1311 
1312 	sc = device_get_softc(dev);
1313 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1314 
1315 	if (bus_alloc_resources(dev, axp_res_spec, sc->res) != 0) {
1316 		device_printf(dev, "can't allocate device resources\n");
1317 		return (ENXIO);
1318 	}
1319 
1320 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1321 	switch (sc->type) {
1322 	case AXP209:
1323 		sc->pins = axp209_pins;
1324 		sc->npins = nitems(axp209_pins);
1325 		sc->gpiodev = gpiobus_attach_bus(dev);
1326 
1327 		sc->sensors = axp209_sensors;
1328 		sc->nsensors = nitems(axp209_sensors);
1329 
1330 		regdefs = axp209_regdefs;
1331 		sc->nregs = nitems(axp209_regdefs);
1332 		break;
1333 	case AXP221:
1334 		sc->pins = axp221_pins;
1335 		sc->npins = nitems(axp221_pins);
1336 		sc->gpiodev = gpiobus_attach_bus(dev);
1337 
1338 		sc->sensors = axp221_sensors;
1339 		sc->nsensors = nitems(axp221_sensors);
1340 
1341 		regdefs = axp221_regdefs;
1342 		sc->nregs = nitems(axp221_regdefs);
1343 		break;
1344 	}
1345 
1346 	sc->regs = malloc(sizeof(struct axp2xx_reg_sc *) * sc->nregs,
1347 	    M_AXP2XX_REG, M_WAITOK | M_ZERO);
1348 
1349 	sc->intr_hook.ich_func = axp2xx_start;
1350 	sc->intr_hook.ich_arg = dev;
1351 
1352 	if (config_intrhook_establish(&sc->intr_hook) != 0)
1353 		return (ENOMEM);
1354 
1355 	/* Attach known regulators that exist in the DT */
1356 	rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators");
1357 	if (rnode > 0) {
1358 		for (i = 0; i < sc->nregs; i++) {
1359 			child = ofw_bus_find_child(rnode,
1360 			    regdefs[i].name);
1361 			if (child == 0)
1362 				continue;
1363 			reg = axp2xx_reg_attach(dev, child, &regdefs[i]);
1364 			if (reg == NULL) {
1365 				device_printf(dev,
1366 				    "cannot attach regulator %s\n",
1367 				    regdefs[i].name);
1368 				continue;
1369 			}
1370 			sc->regs[i] = reg;
1371 			if (bootverbose)
1372 				device_printf(dev, "Regulator %s attached\n",
1373 				    regdefs[i].name);
1374 		}
1375 	}
1376 
1377 	return (0);
1378 }
1379 
1380 static device_method_t axp2xx_methods[] = {
1381 	DEVMETHOD(device_probe,		axp2xx_probe),
1382 	DEVMETHOD(device_attach,	axp2xx_attach),
1383 
1384 	/* GPIO interface */
1385 	DEVMETHOD(gpio_get_bus,		axp2xx_gpio_get_bus),
1386 	DEVMETHOD(gpio_pin_max,		axp2xx_gpio_pin_max),
1387 	DEVMETHOD(gpio_pin_getname,	axp2xx_gpio_pin_getname),
1388 	DEVMETHOD(gpio_pin_getcaps,	axp2xx_gpio_pin_getcaps),
1389 	DEVMETHOD(gpio_pin_getflags,	axp2xx_gpio_pin_getflags),
1390 	DEVMETHOD(gpio_pin_setflags,	axp2xx_gpio_pin_setflags),
1391 	DEVMETHOD(gpio_pin_get,		axp2xx_gpio_pin_get),
1392 	DEVMETHOD(gpio_pin_set,		axp2xx_gpio_pin_set),
1393 	DEVMETHOD(gpio_pin_toggle,	axp2xx_gpio_pin_toggle),
1394 	DEVMETHOD(gpio_map_gpios,	axp2xx_gpio_map_gpios),
1395 
1396 	/* Regdev interface */
1397 	DEVMETHOD(regdev_map,		axp2xx_regdev_map),
1398 
1399 	/* OFW bus interface */
1400 	DEVMETHOD(ofw_bus_get_node,	axp2xx_get_node),
1401 
1402 	DEVMETHOD_END
1403 };
1404 
1405 static driver_t axp2xx_driver = {
1406 	"axp2xx_pmu",
1407 	axp2xx_methods,
1408 	sizeof(struct axp2xx_softc),
1409 };
1410 
1411 extern driver_t ofw_gpiobus_driver, gpioc_driver;
1412 
1413 EARLY_DRIVER_MODULE(axp2xx, iicbus, axp2xx_driver, 0, 0,
1414     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
1415 EARLY_DRIVER_MODULE(ofw_gpiobus, axp2xx_pmu, ofw_gpiobus_driver, 0, 0,
1416     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
1417 DRIVER_MODULE(gpioc, axp2xx_pmu, gpioc_driver, 0, 0);
1418 MODULE_VERSION(axp2xx, 1);
1419 MODULE_DEPEND(axp2xx, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1420