xref: /freebsd/sys/arm/allwinner/axp81x.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * 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  * $FreeBSD$
27  */
28 
29 /*
30  * X-Powers AXP813/818 PMU for Allwinner SoCs
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/kernel.h>
42 #include <sys/reboot.h>
43 #include <sys/gpio.h>
44 #include <sys/module.h>
45 #include <machine/bus.h>
46 
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
49 
50 #include <dev/gpio/gpiobusvar.h>
51 
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <dev/extres/regulator/regulator.h>
56 
57 #include "gpio_if.h"
58 #include "iicbus_if.h"
59 #include "regdev_if.h"
60 
61 MALLOC_DEFINE(M_AXP81X_REG, "AXP81x regulator", "AXP81x power regulator");
62 
63 #define	AXP_ICTYPE		0x03
64 #define	AXP_POWERCTL1		0x10
65 #define	 AXP_POWERCTL1_DCDC2	(1 << 1)
66 #define	AXP_POWERCTL2		0x12
67 #define	 AXP_POWERCTL2_DC1SW	(1 << 7)
68 #define	AXP_VOLTCTL_DCDC2	0x21
69 #define	 AXP_VOLTCTL_STATUS	(1 << 7)
70 #define	 AXP_VOLTCTL_MASK	0x7f
71 #define	AXP_POWERBAT		0x32
72 #define	 AXP_POWERBAT_SHUTDOWN	(1 << 7)
73 #define	AXP_IRQEN1		0x40
74 #define	AXP_IRQEN2		0x41
75 #define	AXP_IRQEN3		0x42
76 #define	AXP_IRQEN4		0x43
77 #define	AXP_IRQEN5		0x44
78 #define	 AXP_IRQEN5_POKSIRQ	(1 << 4)
79 #define	AXP_IRQEN6		0x45
80 #define	AXP_IRQSTAT5		0x4c
81 #define	 AXP_IRQSTAT5_POKSIRQ	(1 << 4)
82 #define	AXP_GPIO0_CTRL		0x90
83 #define	AXP_GPIO1_CTRL		0x92
84 #define	 AXP_GPIO_FUNC		(0x7 << 0)
85 #define	 AXP_GPIO_FUNC_SHIFT	0
86 #define	 AXP_GPIO_FUNC_DRVLO	0
87 #define	 AXP_GPIO_FUNC_DRVHI	1
88 #define	 AXP_GPIO_FUNC_INPUT	2
89 #define	AXP_GPIO_SIGBIT		0x94
90 #define	AXP_GPIO_PD		0x97
91 
92 static const struct {
93 	const char *name;
94 	uint8_t	ctrl_reg;
95 } axp81x_pins[] = {
96 	{ "GPIO0", AXP_GPIO0_CTRL },
97 	{ "GPIO1", AXP_GPIO1_CTRL },
98 };
99 
100 static struct ofw_compat_data compat_data[] = {
101 	{ "x-powers,axp813",			1 },
102 	{ "x-powers,axp818",			1 },
103 	{ NULL,					0 }
104 };
105 
106 static struct resource_spec axp81x_spec[] = {
107 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
108 	{ -1, 0 }
109 };
110 
111 struct axp81x_regdef {
112 	intptr_t		id;
113 	char			*name;
114 	char			*supply_name;
115 	uint8_t			enable_reg;
116 	uint8_t			enable_mask;
117 	uint8_t			voltage_reg;
118 	int			voltage_min;
119 	int			voltage_max;
120 	int			voltage_step1;
121 	int			voltage_nstep1;
122 	int			voltage_step2;
123 	int			voltage_nstep2;
124 };
125 
126 enum axp81x_reg_id {
127 	AXP81X_REG_ID_DC1SW,
128 	AXP81X_REG_ID_DCDC2,
129 };
130 
131 static struct axp81x_regdef axp81x_regdefs[] = {
132 	{
133 		.id = AXP81X_REG_ID_DC1SW,
134 		.name = "dc1sw",
135 		.enable_reg = AXP_POWERCTL2,
136 		.enable_mask = AXP_POWERCTL2_DC1SW,
137 	},
138 	{
139 		.id = AXP81X_REG_ID_DCDC2,
140 		.name = "dcdc2",
141 		.enable_reg = AXP_POWERCTL1,
142 		.enable_mask = AXP_POWERCTL1_DCDC2,
143 		.voltage_reg = AXP_VOLTCTL_DCDC2,
144 		.voltage_min = 500,
145 		.voltage_max = 1300,
146 		.voltage_step1 = 10,
147 		.voltage_nstep1 = 70,
148 		.voltage_step2 = 20,
149 		.voltage_nstep2 = 5,
150 	},
151 };
152 
153 struct axp81x_softc;
154 
155 struct axp81x_reg_sc {
156 	struct regnode		*regnode;
157 	device_t		base_dev;
158 	struct axp81x_regdef	*def;
159 	phandle_t		xref;
160 	struct regnode_std_param *param;
161 };
162 
163 struct axp81x_softc {
164 	struct resource		*res;
165 	uint16_t		addr;
166 	void			*ih;
167 	device_t		gpiodev;
168 	struct mtx		mtx;
169 	int			busy;
170 
171 	/* Regulators */
172 	struct axp81x_reg_sc	**regs;
173 	int			nregs;
174 };
175 
176 #define	AXP_LOCK(sc)	mtx_lock(&(sc)->mtx)
177 #define	AXP_UNLOCK(sc)	mtx_unlock(&(sc)->mtx)
178 
179 static int
180 axp81x_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
181 {
182 	struct axp81x_softc *sc;
183 	struct iic_msg msg[2];
184 
185 	sc = device_get_softc(dev);
186 
187 	msg[0].slave = sc->addr;
188 	msg[0].flags = IIC_M_WR;
189 	msg[0].len = 1;
190 	msg[0].buf = &reg;
191 
192 	msg[1].slave = sc->addr;
193 	msg[1].flags = IIC_M_RD;
194 	msg[1].len = size;
195 	msg[1].buf = data;
196 
197 	return (iicbus_transfer(dev, msg, 2));
198 }
199 
200 static int
201 axp81x_write(device_t dev, uint8_t reg, uint8_t val)
202 {
203 	struct axp81x_softc *sc;
204 	struct iic_msg msg[2];
205 
206 	sc = device_get_softc(dev);
207 
208 	msg[0].slave = sc->addr;
209 	msg[0].flags = IIC_M_WR;
210 	msg[0].len = 1;
211 	msg[0].buf = &reg;
212 
213 	msg[1].slave = sc->addr;
214 	msg[1].flags = IIC_M_WR;
215 	msg[1].len = 1;
216 	msg[1].buf = &val;
217 
218 	return (iicbus_transfer(dev, msg, 2));
219 }
220 
221 static int
222 axp81x_regnode_init(struct regnode *regnode)
223 {
224 	return (0);
225 }
226 
227 static int
228 axp81x_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
229 {
230 	struct axp81x_reg_sc *sc;
231 	uint8_t val;
232 
233 	sc = regnode_get_softc(regnode);
234 
235 	axp81x_read(sc->base_dev, sc->def->enable_reg, &val, 1);
236 	if (enable)
237 		val |= sc->def->enable_mask;
238 	else
239 		val &= ~sc->def->enable_mask;
240 	axp81x_write(sc->base_dev, sc->def->enable_reg, val);
241 
242 	*udelay = 0;
243 
244 	return (0);
245 }
246 
247 static void
248 axp81x_regnode_reg_to_voltage(struct axp81x_reg_sc *sc, uint8_t val, int *uv)
249 {
250 	if (val < sc->def->voltage_nstep1)
251 		*uv = sc->def->voltage_min + val * sc->def->voltage_step1;
252 	else
253 		*uv = sc->def->voltage_min +
254 		    (sc->def->voltage_nstep1 * sc->def->voltage_step1) +
255 		    ((val - sc->def->voltage_nstep1) * sc->def->voltage_step2);
256 	*uv *= 1000;
257 }
258 
259 static int
260 axp81x_regnode_voltage_to_reg(struct axp81x_reg_sc *sc, int min_uvolt,
261     int max_uvolt, uint8_t *val)
262 {
263 	uint8_t nval;
264 	int nstep, uvolt;
265 
266 	nval = 0;
267 	uvolt = sc->def->voltage_min * 1000;
268 
269 	for (nstep = 0; nstep < sc->def->voltage_nstep1 && uvolt < min_uvolt;
270 	     nstep++) {
271 		++nval;
272 		uvolt += (sc->def->voltage_step1 * 1000);
273 	}
274 	for (nstep = 0; nstep < sc->def->voltage_nstep2 && uvolt < min_uvolt;
275 	     nstep++) {
276 		++nval;
277 		uvolt += (sc->def->voltage_step2 * 1000);
278 	}
279 	if (uvolt > max_uvolt)
280 		return (EINVAL);
281 
282 	*val = nval;
283 	return (0);
284 }
285 
286 static int
287 axp81x_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
288     int max_uvolt, int *udelay)
289 {
290 	struct axp81x_reg_sc *sc;
291 	uint8_t val;
292 
293 	sc = regnode_get_softc(regnode);
294 
295 	if (!sc->def->voltage_step1 || !sc->def->voltage_step2)
296 		return (ENXIO);
297 
298 	if (axp81x_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0)
299 		return (ERANGE);
300 
301 	axp81x_write(sc->base_dev, sc->def->voltage_reg, val);
302 
303 	*udelay = 0;
304 
305 	return (0);
306 }
307 
308 static int
309 axp81x_regnode_get_voltage(struct regnode *regnode, int *uvolt)
310 {
311 	struct axp81x_reg_sc *sc;
312 	uint8_t val;
313 
314 	sc = regnode_get_softc(regnode);
315 
316 	if (!sc->def->voltage_step1 || !sc->def->voltage_step2)
317 		return (ENXIO);
318 
319 	axp81x_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
320 	axp81x_regnode_reg_to_voltage(sc, val & AXP_VOLTCTL_MASK, uvolt);
321 
322 	return (0);
323 }
324 
325 static regnode_method_t axp81x_regnode_methods[] = {
326 	/* Regulator interface */
327 	REGNODEMETHOD(regnode_init,		axp81x_regnode_init),
328 	REGNODEMETHOD(regnode_enable,		axp81x_regnode_enable),
329 	REGNODEMETHOD(regnode_set_voltage,	axp81x_regnode_set_voltage),
330 	REGNODEMETHOD(regnode_get_voltage,	axp81x_regnode_get_voltage),
331 	REGNODEMETHOD_END
332 };
333 DEFINE_CLASS_1(axp81x_regnode, axp81x_regnode_class, axp81x_regnode_methods,
334     sizeof(struct axp81x_reg_sc), regnode_class);
335 
336 static void
337 axp81x_shutdown(void *devp, int howto)
338 {
339 	device_t dev;
340 
341 	if ((howto & RB_POWEROFF) == 0)
342 		return;
343 
344 	dev = devp;
345 
346 	if (bootverbose)
347 		device_printf(dev, "Shutdown AXP81x\n");
348 
349 	axp81x_write(dev, AXP_POWERBAT, AXP_POWERBAT_SHUTDOWN);
350 }
351 
352 static void
353 axp81x_intr(void *arg)
354 {
355 	struct axp81x_softc *sc;
356 	device_t dev;
357 	uint8_t val;
358 	int error;
359 
360 	dev = arg;
361 	sc = device_get_softc(dev);
362 
363 	error = axp81x_read(dev, AXP_IRQSTAT5, &val, 1);
364 	if (error != 0)
365 		return;
366 
367 	if (val != 0) {
368 		if ((val & AXP_IRQSTAT5_POKSIRQ) != 0) {
369 			if (bootverbose)
370 				device_printf(dev, "Power button pressed\n");
371 			shutdown_nice(RB_POWEROFF);
372 		}
373 		/* Acknowledge */
374 		axp81x_write(dev, AXP_IRQSTAT5, val);
375 	}
376 }
377 
378 static device_t
379 axp81x_gpio_get_bus(device_t dev)
380 {
381 	struct axp81x_softc *sc;
382 
383 	sc = device_get_softc(dev);
384 
385 	return (sc->gpiodev);
386 }
387 
388 static int
389 axp81x_gpio_pin_max(device_t dev, int *maxpin)
390 {
391 	*maxpin = nitems(axp81x_pins) - 1;
392 
393 	return (0);
394 }
395 
396 static int
397 axp81x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
398 {
399 	if (pin >= nitems(axp81x_pins))
400 		return (EINVAL);
401 
402 	snprintf(name, GPIOMAXNAME, "%s", axp81x_pins[pin].name);
403 
404 	return (0);
405 }
406 
407 static int
408 axp81x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
409 {
410 	if (pin >= nitems(axp81x_pins))
411 		return (EINVAL);
412 
413 	*caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
414 
415 	return (0);
416 }
417 
418 static int
419 axp81x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
420 {
421 	struct axp81x_softc *sc;
422 	uint8_t data, func;
423 	int error;
424 
425 	if (pin >= nitems(axp81x_pins))
426 		return (EINVAL);
427 
428 	sc = device_get_softc(dev);
429 
430 	AXP_LOCK(sc);
431 	error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
432 	if (error == 0) {
433 		func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
434 		if (func == AXP_GPIO_FUNC_INPUT)
435 			*flags = GPIO_PIN_INPUT;
436 		else if (func == AXP_GPIO_FUNC_DRVLO ||
437 		    func == AXP_GPIO_FUNC_DRVHI)
438 			*flags = GPIO_PIN_OUTPUT;
439 		else
440 			*flags = 0;
441 	}
442 	AXP_UNLOCK(sc);
443 
444 	return (error);
445 }
446 
447 static int
448 axp81x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
449 {
450 	struct axp81x_softc *sc;
451 	uint8_t data;
452 	int error;
453 
454 	if (pin >= nitems(axp81x_pins))
455 		return (EINVAL);
456 
457 	sc = device_get_softc(dev);
458 
459 	AXP_LOCK(sc);
460 	error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
461 	if (error == 0) {
462 		data &= ~AXP_GPIO_FUNC;
463 		if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) {
464 			if ((flags & GPIO_PIN_OUTPUT) == 0)
465 				data |= AXP_GPIO_FUNC_INPUT;
466 		}
467 		error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
468 	}
469 	AXP_UNLOCK(sc);
470 
471 	return (error);
472 }
473 
474 static int
475 axp81x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
476 {
477 	struct axp81x_softc *sc;
478 	uint8_t data, func;
479 	int error;
480 
481 	if (pin >= nitems(axp81x_pins))
482 		return (EINVAL);
483 
484 	sc = device_get_softc(dev);
485 
486 	AXP_LOCK(sc);
487 	error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
488 	if (error == 0) {
489 		func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
490 		switch (func) {
491 		case AXP_GPIO_FUNC_DRVLO:
492 			*val = 0;
493 			break;
494 		case AXP_GPIO_FUNC_DRVHI:
495 			*val = 1;
496 			break;
497 		case AXP_GPIO_FUNC_INPUT:
498 			error = axp81x_read(dev, AXP_GPIO_SIGBIT, &data, 1);
499 			if (error == 0)
500 				*val = (data & (1 << pin)) ? 1 : 0;
501 			break;
502 		default:
503 			error = EIO;
504 			break;
505 		}
506 	}
507 	AXP_UNLOCK(sc);
508 
509 	return (error);
510 }
511 
512 static int
513 axp81x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
514 {
515 	struct axp81x_softc *sc;
516 	uint8_t data, func;
517 	int error;
518 
519 	if (pin >= nitems(axp81x_pins))
520 		return (EINVAL);
521 
522 	sc = device_get_softc(dev);
523 
524 	AXP_LOCK(sc);
525 	error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
526 	if (error == 0) {
527 		func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
528 		switch (func) {
529 		case AXP_GPIO_FUNC_DRVLO:
530 		case AXP_GPIO_FUNC_DRVHI:
531 			data &= ~AXP_GPIO_FUNC;
532 			data |= (val << AXP_GPIO_FUNC_SHIFT);
533 			break;
534 		default:
535 			error = EIO;
536 			break;
537 		}
538 	}
539 	if (error == 0)
540 		error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
541 	AXP_UNLOCK(sc);
542 
543 	return (error);
544 }
545 
546 
547 static int
548 axp81x_gpio_pin_toggle(device_t dev, uint32_t pin)
549 {
550 	struct axp81x_softc *sc;
551 	uint8_t data, func;
552 	int error;
553 
554 	if (pin >= nitems(axp81x_pins))
555 		return (EINVAL);
556 
557 	sc = device_get_softc(dev);
558 
559 	AXP_LOCK(sc);
560 	error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
561 	if (error == 0) {
562 		func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
563 		switch (func) {
564 		case AXP_GPIO_FUNC_DRVLO:
565 			data &= ~AXP_GPIO_FUNC;
566 			data |= (AXP_GPIO_FUNC_DRVHI << AXP_GPIO_FUNC_SHIFT);
567 			break;
568 		case AXP_GPIO_FUNC_DRVHI:
569 			data &= ~AXP_GPIO_FUNC;
570 			data |= (AXP_GPIO_FUNC_DRVLO << AXP_GPIO_FUNC_SHIFT);
571 			break;
572 		default:
573 			error = EIO;
574 			break;
575 		}
576 	}
577 	if (error == 0)
578 		error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
579 	AXP_UNLOCK(sc);
580 
581 	return (error);
582 }
583 
584 static int
585 axp81x_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent,
586     int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
587 {
588 	if (gpios[0] >= nitems(axp81x_pins))
589 		return (EINVAL);
590 
591 	*pin = gpios[0];
592 	*flags = gpios[1];
593 
594 	return (0);
595 }
596 
597 static phandle_t
598 axp81x_get_node(device_t dev, device_t bus)
599 {
600 	return (ofw_bus_get_node(dev));
601 }
602 
603 static struct axp81x_reg_sc *
604 axp81x_reg_attach(device_t dev, phandle_t node,
605     struct axp81x_regdef *def)
606 {
607 	struct axp81x_reg_sc *reg_sc;
608 	struct regnode_init_def initdef;
609 	struct regnode *regnode;
610 
611 	memset(&initdef, 0, sizeof(initdef));
612 	regulator_parse_ofw_stdparam(dev, node, &initdef);
613 	if (initdef.std_param.min_uvolt == 0)
614 		initdef.std_param.min_uvolt = def->voltage_min * 1000;
615 	if (initdef.std_param.max_uvolt == 0)
616 		initdef.std_param.max_uvolt = def->voltage_max * 1000;
617 	initdef.id = def->id;
618 	initdef.ofw_node = node;
619 	regnode = regnode_create(dev, &axp81x_regnode_class, &initdef);
620 	if (regnode == NULL) {
621 		device_printf(dev, "cannot create regulator\n");
622 		return (NULL);
623 	}
624 
625 	reg_sc = regnode_get_softc(regnode);
626 	reg_sc->regnode = regnode;
627 	reg_sc->base_dev = dev;
628 	reg_sc->def = def;
629 	reg_sc->xref = OF_xref_from_node(node);
630 	reg_sc->param = regnode_get_stdparam(regnode);
631 
632 	regnode_register(regnode);
633 
634 	return (reg_sc);
635 }
636 
637 static int
638 axp81x_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
639     intptr_t *num)
640 {
641 	struct axp81x_softc *sc;
642 	int i;
643 
644 	sc = device_get_softc(dev);
645 	for (i = 0; i < sc->nregs; i++) {
646 		if (sc->regs[i] == NULL)
647 			continue;
648 		if (sc->regs[i]->xref == xref) {
649 			*num = sc->regs[i]->def->id;
650 			return (0);
651 		}
652 	}
653 
654 	return (ENXIO);
655 }
656 
657 static int
658 axp81x_probe(device_t dev)
659 {
660 	if (!ofw_bus_status_okay(dev))
661 		return (ENXIO);
662 
663 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
664 		return (ENXIO);
665 
666 	device_set_desc(dev, "X-Powers AXP81x Power Management Unit");
667 
668 	return (BUS_PROBE_DEFAULT);
669 }
670 
671 static int
672 axp81x_attach(device_t dev)
673 {
674 	struct axp81x_softc *sc;
675 	struct axp81x_reg_sc *reg;
676 	uint8_t chip_id;
677 	phandle_t rnode, child;
678 	int error, i;
679 
680 	sc = device_get_softc(dev);
681 
682 	sc->addr = iicbus_get_addr(dev);
683 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
684 
685 	error = bus_alloc_resources(dev, axp81x_spec, &sc->res);
686 	if (error != 0) {
687 		device_printf(dev, "cannot allocate resources for device\n");
688 		return (error);
689 	}
690 
691 	if (bootverbose) {
692 		axp81x_read(dev, AXP_ICTYPE, &chip_id, 1);
693 		device_printf(dev, "chip ID 0x%02x\n", chip_id);
694 	}
695 
696 	sc->nregs = nitems(axp81x_regdefs);
697 	sc->regs = malloc(sizeof(struct axp81x_reg_sc *) * sc->nregs,
698 	    M_AXP81X_REG, M_WAITOK | M_ZERO);
699 
700 	/* Attach known regulators that exist in the DT */
701 	rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators");
702 	if (rnode > 0) {
703 		for (i = 0; i < sc->nregs; i++) {
704 			child = ofw_bus_find_child(rnode,
705 			    axp81x_regdefs[i].name);
706 			if (child == 0)
707 				continue;
708 			reg = axp81x_reg_attach(dev, child, &axp81x_regdefs[i]);
709 			if (reg == NULL) {
710 				device_printf(dev,
711 				    "cannot attach regulator %s\n",
712 				    axp81x_regdefs[i].name);
713 				return (ENXIO);
714 			}
715 			sc->regs[i] = reg;
716 		}
717 	}
718 
719 	/* Enable IRQ on short power key press */
720 	axp81x_write(dev, AXP_IRQEN1, 0);
721 	axp81x_write(dev, AXP_IRQEN2, 0);
722 	axp81x_write(dev, AXP_IRQEN3, 0);
723 	axp81x_write(dev, AXP_IRQEN4, 0);
724 	axp81x_write(dev, AXP_IRQEN5, AXP_IRQEN5_POKSIRQ);
725 	axp81x_write(dev, AXP_IRQEN6, 0);
726 
727 	/* Install interrupt handler */
728 	error = bus_setup_intr(dev, sc->res, INTR_TYPE_MISC | INTR_MPSAFE,
729 	    NULL, axp81x_intr, dev, &sc->ih);
730 	if (error != 0) {
731 		device_printf(dev, "cannot setup interrupt handler\n");
732 		return (error);
733 	}
734 
735 	EVENTHANDLER_REGISTER(shutdown_final, axp81x_shutdown, dev,
736 	    SHUTDOWN_PRI_LAST);
737 
738 	sc->gpiodev = gpiobus_attach_bus(dev);
739 
740 	return (0);
741 }
742 
743 static device_method_t axp81x_methods[] = {
744 	/* Device interface */
745 	DEVMETHOD(device_probe,		axp81x_probe),
746 	DEVMETHOD(device_attach,	axp81x_attach),
747 
748 	/* GPIO interface */
749 	DEVMETHOD(gpio_get_bus,		axp81x_gpio_get_bus),
750 	DEVMETHOD(gpio_pin_max,		axp81x_gpio_pin_max),
751 	DEVMETHOD(gpio_pin_getname,	axp81x_gpio_pin_getname),
752 	DEVMETHOD(gpio_pin_getcaps,	axp81x_gpio_pin_getcaps),
753 	DEVMETHOD(gpio_pin_getflags,	axp81x_gpio_pin_getflags),
754 	DEVMETHOD(gpio_pin_setflags,	axp81x_gpio_pin_setflags),
755 	DEVMETHOD(gpio_pin_get,		axp81x_gpio_pin_get),
756 	DEVMETHOD(gpio_pin_set,		axp81x_gpio_pin_set),
757 	DEVMETHOD(gpio_pin_toggle,	axp81x_gpio_pin_toggle),
758 	DEVMETHOD(gpio_map_gpios,	axp81x_gpio_map_gpios),
759 
760 	/* Regdev interface */
761 	DEVMETHOD(regdev_map,		axp81x_regdev_map),
762 
763 	/* OFW bus interface */
764 	DEVMETHOD(ofw_bus_get_node,	axp81x_get_node),
765 
766 	DEVMETHOD_END
767 };
768 
769 static driver_t axp81x_driver = {
770 	"axp81x_pmu",
771 	axp81x_methods,
772 	sizeof(struct axp81x_softc),
773 };
774 
775 static devclass_t axp81x_devclass;
776 extern devclass_t ofwgpiobus_devclass, gpioc_devclass;
777 extern driver_t ofw_gpiobus_driver, gpioc_driver;
778 
779 EARLY_DRIVER_MODULE(axp81x, iicbus, axp81x_driver, axp81x_devclass, 0, 0,
780     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
781 EARLY_DRIVER_MODULE(ofw_gpiobus, axp81x_pmu, ofw_gpiobus_driver,
782     ofwgpiobus_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
783 DRIVER_MODULE(gpioc, axp81x_pmu, gpioc_driver, gpioc_devclass, 0, 0);
784 MODULE_VERSION(axp81x, 1);
785 MODULE_DEPEND(axp81x, iicbus, 1, 1, 1);
786