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