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