xref: /freebsd/sys/arm/allwinner/axp209.c (revision 2c9a9dfc187d171de6b92654d71b977f067ed641)
1 /*-
2  * Copyright (c) 2015-2016 Emmanuel Vadot <manu@freebsd.org>
3  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31 * X-Power AXP209/AXP211 PMU for Allwinner SoCs
32 */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/clock.h>
40 #include <sys/time.h>
41 #include <sys/bus.h>
42 #include <sys/proc.h>
43 #include <sys/gpio.h>
44 #include <sys/reboot.h>
45 #include <sys/resource.h>
46 #include <sys/rman.h>
47 #include <sys/sysctl.h>
48 
49 #include <dev/iicbus/iiconf.h>
50 
51 #include <dev/gpio/gpiobusvar.h>
52 
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 
56 #include <dev/extres/regulator/regulator.h>
57 
58 #include <arm/allwinner/axp209reg.h>
59 
60 #include "gpio_if.h"
61 #include "regdev_if.h"
62 
63 MALLOC_DEFINE(M_AXP2XX_REG, "Axp2XX regulator", "Axp2XX power regulator");
64 
65 struct axp2xx_regdef {
66 	intptr_t		id;
67 	char			*name;
68 	uint8_t			enable_reg;
69 	uint8_t			enable_mask;
70 	uint8_t			voltage_reg;
71 	uint8_t			voltage_mask;
72 	uint8_t			voltage_shift;
73 	int			voltage_min;
74 	int			voltage_max;
75 	int			voltage_step;
76 	int			voltage_nstep;
77 };
78 
79 static struct axp2xx_regdef axp209_regdefs[] = {
80 	{
81 		.id = AXP209_REG_ID_DCDC2,
82 		.name = "dcdc2",
83 		.enable_reg = AXP209_POWERCTL,
84 		.enable_mask = AXP209_POWERCTL_DCDC2,
85 		.voltage_reg = AXP209_REG_DCDC2_VOLTAGE,
86 		.voltage_mask = 0x3f,
87 		.voltage_min = 700,
88 		.voltage_max = 2275,
89 		.voltage_step = 25,
90 		.voltage_nstep = 64,
91 	},
92 	{
93 		.id = AXP209_REG_ID_DCDC3,
94 		.name = "dcdc3",
95 		.enable_reg = AXP209_POWERCTL,
96 		.enable_mask = AXP209_POWERCTL_DCDC3,
97 		.voltage_reg = AXP209_REG_DCDC3_VOLTAGE,
98 		.voltage_mask = 0x7f,
99 		.voltage_min = 700,
100 		.voltage_max = 3500,
101 		.voltage_step = 25,
102 		.voltage_nstep = 128,
103 	},
104 	{
105 		.id = AXP209_REG_ID_LDO2,
106 		.name = "ldo2",
107 		.enable_reg = AXP209_POWERCTL,
108 		.enable_mask = AXP209_POWERCTL_LDO2,
109 		.voltage_reg = AXP209_REG_LDO24_VOLTAGE,
110 		.voltage_mask = 0xf0,
111 		.voltage_shift = 4,
112 		.voltage_min = 1800,
113 		.voltage_max = 3300,
114 		.voltage_step = 100,
115 		.voltage_nstep = 16,
116 	},
117 	{
118 		.id = AXP209_REG_ID_LDO3,
119 		.name = "ldo3",
120 		.enable_reg = AXP209_POWERCTL,
121 		.enable_mask = AXP209_POWERCTL_LDO3,
122 		.voltage_reg = AXP209_REG_LDO3_VOLTAGE,
123 		.voltage_mask = 0x7f,
124 		.voltage_min = 700,
125 		.voltage_max = 2275,
126 		.voltage_step = 25,
127 		.voltage_nstep = 128,
128 	},
129 };
130 
131 static struct axp2xx_regdef axp221_regdefs[] = {
132 	{
133 		.id = AXP221_REG_ID_DLDO1,
134 		.name = "dldo1",
135 		.enable_reg = AXP221_POWERCTL_2,
136 		.enable_mask = AXP221_POWERCTL2_DLDO1,
137 		.voltage_reg = AXP221_REG_DLDO1_VOLTAGE,
138 		.voltage_mask = 0x1f,
139 		.voltage_min = 700,
140 		.voltage_max = 3300,
141 		.voltage_step = 100,
142 		.voltage_nstep = 26,
143 	},
144 	{
145 		.id = AXP221_REG_ID_DLDO2,
146 		.name = "dldo2",
147 		.enable_reg = AXP221_POWERCTL_2,
148 		.enable_mask = AXP221_POWERCTL2_DLDO2,
149 		.voltage_reg = AXP221_REG_DLDO2_VOLTAGE,
150 		.voltage_mask = 0x1f,
151 		.voltage_min = 700,
152 		.voltage_max = 3300,
153 		.voltage_step = 100,
154 		.voltage_nstep = 26,
155 	},
156 	{
157 		.id = AXP221_REG_ID_DLDO3,
158 		.name = "dldo3",
159 		.enable_reg = AXP221_POWERCTL_2,
160 		.enable_mask = AXP221_POWERCTL2_DLDO3,
161 		.voltage_reg = AXP221_REG_DLDO3_VOLTAGE,
162 		.voltage_mask = 0x1f,
163 		.voltage_min = 700,
164 		.voltage_max = 3300,
165 		.voltage_step = 100,
166 		.voltage_nstep = 26,
167 	},
168 	{
169 		.id = AXP221_REG_ID_DLDO4,
170 		.name = "dldo4",
171 		.enable_reg = AXP221_POWERCTL_2,
172 		.enable_mask = AXP221_POWERCTL2_DLDO4,
173 		.voltage_reg = AXP221_REG_DLDO4_VOLTAGE,
174 		.voltage_mask = 0x1f,
175 		.voltage_min = 700,
176 		.voltage_max = 3300,
177 		.voltage_step = 100,
178 		.voltage_nstep = 26,
179 	},
180 	{
181 		.id = AXP221_REG_ID_ELDO1,
182 		.name = "eldo1",
183 		.enable_reg = AXP221_POWERCTL_2,
184 		.enable_mask = AXP221_POWERCTL2_ELDO1,
185 		.voltage_reg = AXP221_REG_ELDO1_VOLTAGE,
186 		.voltage_mask = 0x1f,
187 		.voltage_min = 700,
188 		.voltage_max = 3300,
189 		.voltage_step = 100,
190 		.voltage_nstep = 26,
191 	},
192 	{
193 		.id = AXP221_REG_ID_ELDO2,
194 		.name = "eldo2",
195 		.enable_reg = AXP221_POWERCTL_2,
196 		.enable_mask = AXP221_POWERCTL2_ELDO2,
197 		.voltage_reg = AXP221_REG_ELDO2_VOLTAGE,
198 		.voltage_mask = 0x1f,
199 		.voltage_min = 700,
200 		.voltage_max = 3300,
201 		.voltage_step = 100,
202 		.voltage_nstep = 26,
203 	},
204 	{
205 		.id = AXP221_REG_ID_ELDO3,
206 		.name = "eldo3",
207 		.enable_reg = AXP221_POWERCTL_2,
208 		.enable_mask = AXP221_POWERCTL2_ELDO3,
209 		.voltage_reg = AXP221_REG_ELDO3_VOLTAGE,
210 		.voltage_mask = 0x1f,
211 		.voltage_min = 700,
212 		.voltage_max = 3300,
213 		.voltage_step = 100,
214 		.voltage_nstep = 26,
215 	},
216 	{
217 		.id = AXP221_REG_ID_DC5LDO,
218 		.name = "dc5ldo",
219 		.enable_reg = AXP221_POWERCTL_1,
220 		.enable_mask = AXP221_POWERCTL1_DC5LDO,
221 		.voltage_reg = AXP221_REG_DC5LDO_VOLTAGE,
222 		.voltage_mask = 0x3,
223 		.voltage_min = 700,
224 		.voltage_max = 1400,
225 		.voltage_step = 100,
226 		.voltage_nstep = 7,
227 	},
228 	{
229 		.id = AXP221_REG_ID_DCDC1,
230 		.name = "dcdc1",
231 		.enable_reg = AXP221_POWERCTL_1,
232 		.enable_mask = AXP221_POWERCTL1_DCDC1,
233 		.voltage_reg = AXP221_REG_DCDC1_VOLTAGE,
234 		.voltage_mask = 0x1f,
235 		.voltage_min = 1600,
236 		.voltage_max = 3400,
237 		.voltage_step = 100,
238 		.voltage_nstep = 18,
239 	},
240 	{
241 		.id = AXP221_REG_ID_DCDC2,
242 		.name = "dcdc2",
243 		.enable_reg = AXP221_POWERCTL_1,
244 		.enable_mask = AXP221_POWERCTL1_DCDC2,
245 		.voltage_reg = AXP221_REG_DCDC2_VOLTAGE,
246 		.voltage_mask = 0x3f,
247 		.voltage_min = 600,
248 		.voltage_max = 1540,
249 		.voltage_step = 20,
250 		.voltage_nstep = 47,
251 	},
252 	{
253 		.id = AXP221_REG_ID_DCDC3,
254 		.name = "dcdc3",
255 		.enable_reg = AXP221_POWERCTL_1,
256 		.enable_mask = AXP221_POWERCTL1_DCDC3,
257 		.voltage_reg = AXP221_REG_DCDC3_VOLTAGE,
258 		.voltage_mask = 0x3f,
259 		.voltage_min = 600,
260 		.voltage_max = 1860,
261 		.voltage_step = 20,
262 		.voltage_nstep = 63,
263 	},
264 	{
265 		.id = AXP221_REG_ID_DCDC4,
266 		.name = "dcdc4",
267 		.enable_reg = AXP221_POWERCTL_1,
268 		.enable_mask = AXP221_POWERCTL1_DCDC4,
269 		.voltage_reg = AXP221_REG_DCDC4_VOLTAGE,
270 		.voltage_mask = 0x3f,
271 		.voltage_min = 600,
272 		.voltage_max = 1540,
273 		.voltage_step = 20,
274 		.voltage_nstep = 47,
275 	},
276 	{
277 		.id = AXP221_REG_ID_DCDC5,
278 		.name = "dcdc5",
279 		.enable_reg = AXP221_POWERCTL_1,
280 		.enable_mask = AXP221_POWERCTL1_DCDC5,
281 		.voltage_reg = AXP221_REG_DCDC5_VOLTAGE,
282 		.voltage_mask = 0x1f,
283 		.voltage_min = 1000,
284 		.voltage_max = 2550,
285 		.voltage_step = 50,
286 		.voltage_nstep = 31,
287 	},
288 	{
289 		.id = AXP221_REG_ID_ALDO1,
290 		.name = "aldo1",
291 		.enable_reg = AXP221_POWERCTL_1,
292 		.enable_mask = AXP221_POWERCTL1_ALDO1,
293 		.voltage_reg = AXP221_REG_ALDO1_VOLTAGE,
294 		.voltage_mask = 0x1f,
295 		.voltage_min = 700,
296 		.voltage_max = 3300,
297 		.voltage_step = 100,
298 		.voltage_nstep = 26,
299 	},
300 	{
301 		.id = AXP221_REG_ID_ALDO2,
302 		.name = "aldo2",
303 		.enable_reg = AXP221_POWERCTL_1,
304 		.enable_mask = AXP221_POWERCTL1_ALDO2,
305 		.voltage_reg = AXP221_REG_ALDO2_VOLTAGE,
306 		.voltage_mask = 0x1f,
307 		.voltage_min = 700,
308 		.voltage_max = 3300,
309 		.voltage_step = 100,
310 		.voltage_nstep = 26,
311 	},
312 	{
313 		.id = AXP221_REG_ID_ALDO3,
314 		.name = "aldo3",
315 		.enable_reg = AXP221_POWERCTL_3,
316 		.enable_mask = AXP221_POWERCTL3_ALDO3,
317 		.voltage_reg = AXP221_REG_ALDO3_VOLTAGE,
318 		.voltage_mask = 0x1f,
319 		.voltage_min = 700,
320 		.voltage_max = 3300,
321 		.voltage_step = 100,
322 		.voltage_nstep = 26,
323 	},
324 	{
325 		.id = AXP221_REG_ID_DC1SW,
326 		.name = "dc1sw",
327 		.enable_reg = AXP221_POWERCTL_2,
328 		.enable_mask = AXP221_POWERCTL2_DC1SW,
329 	},
330 };
331 
332 struct axp2xx_reg_sc {
333 	struct regnode		*regnode;
334 	device_t		base_dev;
335 	struct axp2xx_regdef	*def;
336 	phandle_t		xref;
337 	struct regnode_std_param *param;
338 };
339 
340 struct axp2xx_pins {
341 	const char	*name;
342 	uint8_t		ctrl_reg;
343 	uint8_t		status_reg;
344 	uint8_t		status_mask;
345 	uint8_t		status_shift;
346 };
347 
348 /* GPIO3 is different, don't expose it for now */
349 static const struct axp2xx_pins axp209_pins[] = {
350 	{
351 		.name = "GPIO0",
352 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
353 		.status_reg = AXP2XX_GPIO_STATUS,
354 		.status_mask = 0x10,
355 		.status_shift = 4,
356 	},
357 	{
358 		.name = "GPIO1",
359 		.ctrl_reg = AXP2XX_GPIO1_CTRL,
360 		.status_reg = AXP2XX_GPIO_STATUS,
361 		.status_mask = 0x20,
362 		.status_shift = 5,
363 	},
364 	{
365 		.name = "GPIO2",
366 		.ctrl_reg = AXP209_GPIO2_CTRL,
367 		.status_reg = AXP2XX_GPIO_STATUS,
368 		.status_mask = 0x40,
369 		.status_shift = 6,
370 	},
371 };
372 
373 static const struct axp2xx_pins axp221_pins[] = {
374 	{
375 		.name = "GPIO0",
376 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
377 		.status_reg = AXP2XX_GPIO_STATUS,
378 		.status_mask = 0x1,
379 		.status_shift = 0x0,
380 	},
381 	{
382 		.name = "GPIO1",
383 		.ctrl_reg = AXP2XX_GPIO0_CTRL,
384 		.status_reg = AXP2XX_GPIO_STATUS,
385 		.status_mask = 0x2,
386 		.status_shift = 0x1,
387 	},
388 };
389 
390 struct axp2xx_sensors {
391 	int		id;
392 	const char	*name;
393 	const char	*desc;
394 	const char	*format;
395 	uint8_t		enable_reg;
396 	uint8_t		enable_mask;
397 	uint8_t		value_reg;
398 	uint8_t		value_size;
399 	uint8_t		h_value_mask;
400 	uint8_t		h_value_shift;
401 	uint8_t		l_value_mask;
402 	uint8_t		l_value_shift;
403 	int		value_step;
404 	int		value_convert;
405 };
406 
407 static const struct axp2xx_sensors axp209_sensors[] = {
408 	{
409 		.id = AXP209_ACVOLT,
410 		.name = "acvolt",
411 		.desc = "AC Voltage (microvolt)",
412 		.format = "I",
413 		.enable_reg = AXP2XX_ADC_ENABLE1,
414 		.enable_mask = AXP209_ADC1_ACVOLT,
415 		.value_reg = AXP209_ACIN_VOLTAGE,
416 		.value_size = 2,
417 		.h_value_mask = 0xff,
418 		.h_value_shift = 4,
419 		.l_value_mask = 0xf,
420 		.l_value_shift = 0,
421 		.value_step = AXP209_VOLT_STEP,
422 	},
423 	{
424 		.id = AXP209_ACCURRENT,
425 		.name = "accurrent",
426 		.desc = "AC Current (microAmpere)",
427 		.format = "I",
428 		.enable_reg = AXP2XX_ADC_ENABLE1,
429 		.enable_mask = AXP209_ADC1_ACCURRENT,
430 		.value_reg = AXP209_ACIN_CURRENT,
431 		.value_size = 2,
432 		.h_value_mask = 0xff,
433 		.h_value_shift = 4,
434 		.l_value_mask = 0xf,
435 		.l_value_shift = 0,
436 		.value_step = AXP209_ACCURRENT_STEP,
437 	},
438 	{
439 		.id = AXP209_VBUSVOLT,
440 		.name = "vbusvolt",
441 		.desc = "VBUS Voltage (microVolt)",
442 		.format = "I",
443 		.enable_reg = AXP2XX_ADC_ENABLE1,
444 		.enable_mask = AXP209_ADC1_VBUSVOLT,
445 		.value_reg = AXP209_VBUS_VOLTAGE,
446 		.value_size = 2,
447 		.h_value_mask = 0xff,
448 		.h_value_shift = 4,
449 		.l_value_mask = 0xf,
450 		.l_value_shift = 0,
451 		.value_step = AXP209_VOLT_STEP,
452 	},
453 	{
454 		.id = AXP209_VBUSCURRENT,
455 		.name = "vbuscurrent",
456 		.desc = "VBUS Current (microAmpere)",
457 		.format = "I",
458 		.enable_reg = AXP2XX_ADC_ENABLE1,
459 		.enable_mask = AXP209_ADC1_VBUSCURRENT,
460 		.value_reg = AXP209_VBUS_CURRENT,
461 		.value_size = 2,
462 		.h_value_mask = 0xff,
463 		.h_value_shift = 4,
464 		.l_value_mask = 0xf,
465 		.l_value_shift = 0,
466 		.value_step = AXP209_VBUSCURRENT_STEP,
467 	},
468 	{
469 		.id = AXP2XX_BATVOLT,
470 		.name = "batvolt",
471 		.desc = "Battery Voltage (microVolt)",
472 		.format = "I",
473 		.enable_reg = AXP2XX_ADC_ENABLE1,
474 		.enable_mask = AXP2XX_ADC1_BATVOLT,
475 		.value_reg = AXP2XX_BAT_VOLTAGE,
476 		.value_size = 2,
477 		.h_value_mask = 0xff,
478 		.h_value_shift = 4,
479 		.l_value_mask = 0xf,
480 		.l_value_shift = 0,
481 		.value_step = AXP2XX_BATVOLT_STEP,
482 	},
483 	{
484 		.id = AXP2XX_BATCHARGECURRENT,
485 		.name = "batchargecurrent",
486 		.desc = "Battery Charging Current (microAmpere)",
487 		.format = "I",
488 		.enable_reg = AXP2XX_ADC_ENABLE1,
489 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
490 		.value_reg = AXP2XX_BAT_CHARGE_CURRENT,
491 		.value_size = 2,
492 		.h_value_mask = 0xff,
493 		.h_value_shift = 5,
494 		.l_value_mask = 0x1f,
495 		.l_value_shift = 0,
496 		.value_step = AXP2XX_BATCURRENT_STEP,
497 	},
498 	{
499 		.id = AXP2XX_BATDISCHARGECURRENT,
500 		.name = "batdischargecurrent",
501 		.desc = "Battery Discharging Current (microAmpere)",
502 		.format = "I",
503 		.enable_reg = AXP2XX_ADC_ENABLE1,
504 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
505 		.value_reg = AXP2XX_BAT_DISCHARGE_CURRENT,
506 		.value_size = 2,
507 		.h_value_mask = 0xff,
508 		.h_value_shift = 5,
509 		.l_value_mask = 0x1f,
510 		.l_value_shift = 0,
511 		.value_step = AXP2XX_BATCURRENT_STEP,
512 	},
513 	{
514 		.id = AXP2XX_TEMP,
515 		.name = "temp",
516 		.desc = "Internal Temperature",
517 		.format = "IK",
518 		.enable_reg = AXP209_ADC_ENABLE2,
519 		.enable_mask = AXP209_ADC2_TEMP,
520 		.value_reg = AXP209_TEMPMON,
521 		.value_size = 2,
522 		.h_value_mask = 0xff,
523 		.h_value_shift = 4,
524 		.l_value_mask = 0xf,
525 		.l_value_shift = 0,
526 		.value_step = 1,
527 		.value_convert = -(AXP209_TEMPMON_MIN - AXP209_0C_TO_K),
528 	},
529 };
530 
531 static const struct axp2xx_sensors axp221_sensors[] = {
532 	{
533 		.id = AXP2XX_BATVOLT,
534 		.name = "batvolt",
535 		.desc = "Battery Voltage (microVolt)",
536 		.format = "I",
537 		.enable_reg = AXP2XX_ADC_ENABLE1,
538 		.enable_mask = AXP2XX_ADC1_BATVOLT,
539 		.value_reg = AXP2XX_BAT_VOLTAGE,
540 		.value_size = 2,
541 		.h_value_mask = 0xff,
542 		.h_value_shift = 4,
543 		.l_value_mask = 0xf,
544 		.l_value_shift = 0,
545 		.value_step = AXP2XX_BATVOLT_STEP,
546 	},
547 	{
548 		.id = AXP2XX_BATCHARGECURRENT,
549 		.name = "batchargecurrent",
550 		.desc = "Battery Charging Current (microAmpere)",
551 		.format = "I",
552 		.enable_reg = AXP2XX_ADC_ENABLE1,
553 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
554 		.value_reg = AXP2XX_BAT_CHARGE_CURRENT,
555 		.value_size = 2,
556 		.h_value_mask = 0xff,
557 		.h_value_shift = 5,
558 		.l_value_mask = 0x1f,
559 		.l_value_shift = 0,
560 		.value_step = AXP2XX_BATCURRENT_STEP,
561 	},
562 	{
563 		.id = AXP2XX_BATDISCHARGECURRENT,
564 		.name = "batdischargecurrent",
565 		.desc = "Battery Discharging Current (microAmpere)",
566 		.format = "I",
567 		.enable_reg = AXP2XX_ADC_ENABLE1,
568 		.enable_mask = AXP2XX_ADC1_BATCURRENT,
569 		.value_reg = AXP2XX_BAT_DISCHARGE_CURRENT,
570 		.value_size = 2,
571 		.h_value_mask = 0xff,
572 		.h_value_shift = 5,
573 		.l_value_mask = 0x1f,
574 		.l_value_shift = 0,
575 		.value_step = AXP2XX_BATCURRENT_STEP,
576 	},
577 	{
578 		.id = AXP2XX_TEMP,
579 		.name = "temp",
580 		.desc = "Internal Temperature",
581 		.format = "IK",
582 		.enable_reg = AXP2XX_ADC_ENABLE1,
583 		.enable_mask = AXP221_ADC1_TEMP,
584 		.value_reg = AXP221_TEMPMON,
585 		.value_size = 2,
586 		.h_value_mask = 0xff,
587 		.h_value_shift = 4,
588 		.l_value_mask = 0xf,
589 		.l_value_shift = 0,
590 		.value_step = 1,
591 		.value_convert = -(AXP221_TEMPMON_MIN - AXP209_0C_TO_K),
592 	},
593 };
594 
595 enum AXP2XX_TYPE {
596 	AXP209 = 1,
597 	AXP221,
598 };
599 
600 struct axp2xx_softc {
601 	device_t		dev;
602 	struct resource *	res[1];
603 	void *			intrcookie;
604 	struct intr_config_hook	intr_hook;
605 	struct mtx		mtx;
606 	uint8_t			type;
607 
608 	/* GPIO */
609 	device_t		gpiodev;
610 	int			npins;
611 	const struct axp2xx_pins	*pins;
612 
613 	/* Sensors */
614 	const struct axp2xx_sensors	*sensors;
615 	int				nsensors;
616 
617 	/* Regulators */
618 	struct axp2xx_reg_sc	**regs;
619 	int			nregs;
620 	struct axp2xx_regdef	*regdefs;
621 };
622 
623 static struct ofw_compat_data compat_data[] = {
624 	{ "x-powers,axp209",		AXP209 },
625 	{ "x-powers,axp221",		AXP221 },
626 	{ NULL,				0 }
627 };
628 
629 static struct resource_spec axp_res_spec[] = {
630 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
631 	{ -1,			0,	0 }
632 };
633 
634 #define	AXP_LOCK(sc)	mtx_lock(&(sc)->mtx)
635 #define	AXP_UNLOCK(sc)	mtx_unlock(&(sc)->mtx)
636 
637 static int
638 axp2xx_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
639 {
640 
641 	return (iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT));
642 }
643 
644 static int
645 axp2xx_write(device_t dev, uint8_t reg, uint8_t data)
646 {
647 
648 	return (iicdev_writeto(dev, reg, &data, sizeof(data), IIC_INTRWAIT));
649 }
650 
651 static int
652 axp2xx_regnode_init(struct regnode *regnode)
653 {
654 	return (0);
655 }
656 
657 static int
658 axp2xx_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
659 {
660 	struct axp2xx_reg_sc *sc;
661 	uint8_t val;
662 
663 	sc = regnode_get_softc(regnode);
664 
665 	axp2xx_read(sc->base_dev, sc->def->enable_reg, &val, 1);
666 	if (enable)
667 		val |= sc->def->enable_mask;
668 	else
669 		val &= ~sc->def->enable_mask;
670 	axp2xx_write(sc->base_dev, sc->def->enable_reg, val);
671 
672 	*udelay = 0;
673 
674 	return (0);
675 }
676 
677 static void
678 axp2xx_regnode_reg_to_voltage(struct axp2xx_reg_sc *sc, uint8_t val, int *uv)
679 {
680 	if (val < sc->def->voltage_nstep)
681 		*uv = sc->def->voltage_min + val * sc->def->voltage_step;
682 	else
683 		*uv = sc->def->voltage_min +
684 		       (sc->def->voltage_nstep * sc->def->voltage_step);
685 	*uv *= 1000;
686 }
687 
688 static int
689 axp2xx_regnode_voltage_to_reg(struct axp2xx_reg_sc *sc, int min_uvolt,
690     int max_uvolt, uint8_t *val)
691 {
692 	uint8_t nval;
693 	int nstep, uvolt;
694 
695 	nval = 0;
696 	uvolt = sc->def->voltage_min * 1000;
697 
698 	for (nstep = 0; nstep < sc->def->voltage_nstep && uvolt < min_uvolt;
699 	     nstep++) {
700 		++nval;
701 		uvolt += (sc->def->voltage_step * 1000);
702 	}
703 	if (uvolt > max_uvolt)
704 		return (EINVAL);
705 
706 	*val = nval;
707 	return (0);
708 }
709 
710 static int
711 axp2xx_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
712     int max_uvolt, int *udelay)
713 {
714 	struct axp2xx_reg_sc *sc;
715 	uint8_t val;
716 
717 	sc = regnode_get_softc(regnode);
718 
719 	if (!sc->def->voltage_step)
720 		return (ENXIO);
721 
722 	if (axp2xx_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0)
723 		return (ERANGE);
724 
725 	axp2xx_write(sc->base_dev, sc->def->voltage_reg, val);
726 
727 	*udelay = 0;
728 
729 	return (0);
730 }
731 
732 static int
733 axp2xx_regnode_get_voltage(struct regnode *regnode, int *uvolt)
734 {
735 	struct axp2xx_reg_sc *sc;
736 	uint8_t val;
737 
738 	sc = regnode_get_softc(regnode);
739 
740 	if (!sc->def->voltage_step)
741 		return (ENXIO);
742 
743 	axp2xx_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
744 	axp2xx_regnode_reg_to_voltage(sc, val & sc->def->voltage_mask, uvolt);
745 
746 	return (0);
747 }
748 
749 static regnode_method_t axp2xx_regnode_methods[] = {
750 	/* Regulator interface */
751 	REGNODEMETHOD(regnode_init,		axp2xx_regnode_init),
752 	REGNODEMETHOD(regnode_enable,		axp2xx_regnode_enable),
753 	REGNODEMETHOD(regnode_set_voltage,	axp2xx_regnode_set_voltage),
754 	REGNODEMETHOD(regnode_get_voltage,	axp2xx_regnode_get_voltage),
755 	REGNODEMETHOD(regnode_check_voltage,	regnode_method_check_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