Home | History | Annotate | Line # | Download | only in i2c
axppmic.c revision 1.9.2.6
      1  1.9.2.6  pgoyette /* $NetBSD: axppmic.c,v 1.9.2.6 2019/01/18 08:50:26 pgoyette Exp $ */
      2  1.9.2.2  pgoyette 
      3  1.9.2.2  pgoyette /*-
      4  1.9.2.2  pgoyette  * Copyright (c) 2014-2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.9.2.2  pgoyette  * All rights reserved.
      6  1.9.2.2  pgoyette  *
      7  1.9.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.9.2.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.9.2.2  pgoyette  * are met:
     10  1.9.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.9.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.9.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.9.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.9.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.9.2.2  pgoyette  *
     16  1.9.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.9.2.2  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.9.2.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.9.2.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.9.2.2  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.9.2.2  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.9.2.2  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.9.2.2  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.9.2.2  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.9.2.2  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.9.2.2  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.9.2.2  pgoyette  */
     28  1.9.2.2  pgoyette 
     29  1.9.2.2  pgoyette #include <sys/cdefs.h>
     30  1.9.2.6  pgoyette __KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.9.2.6 2019/01/18 08:50:26 pgoyette Exp $");
     31  1.9.2.2  pgoyette 
     32  1.9.2.2  pgoyette #include <sys/param.h>
     33  1.9.2.2  pgoyette #include <sys/systm.h>
     34  1.9.2.2  pgoyette #include <sys/kernel.h>
     35  1.9.2.2  pgoyette #include <sys/device.h>
     36  1.9.2.2  pgoyette #include <sys/conf.h>
     37  1.9.2.2  pgoyette #include <sys/bus.h>
     38  1.9.2.2  pgoyette #include <sys/kmem.h>
     39  1.9.2.2  pgoyette 
     40  1.9.2.2  pgoyette #include <dev/i2c/i2cvar.h>
     41  1.9.2.2  pgoyette 
     42  1.9.2.2  pgoyette #include <dev/sysmon/sysmonvar.h>
     43  1.9.2.2  pgoyette #include <dev/sysmon/sysmon_taskq.h>
     44  1.9.2.2  pgoyette 
     45  1.9.2.2  pgoyette #include <dev/fdt/fdtvar.h>
     46  1.9.2.2  pgoyette 
     47  1.9.2.2  pgoyette #define	AXP_POWER_SOURCE_REG	0x00
     48  1.9.2.2  pgoyette #define	 AXP_POWER_SOURCE_ACIN_PRESENT	__BIT(7)
     49  1.9.2.2  pgoyette #define	 AXP_POWER_SOURCE_VBUS_PRESENT	__BIT(5)
     50  1.9.2.3  pgoyette #define	 AXP_POWER_SOURCE_CHARGE_DIRECTION __BIT(2)
     51  1.9.2.2  pgoyette 
     52  1.9.2.2  pgoyette #define	AXP_POWER_MODE_REG	0x01
     53  1.9.2.2  pgoyette #define	 AXP_POWER_MODE_BATT_VALID	__BIT(4)
     54  1.9.2.2  pgoyette #define	 AXP_POWER_MODE_BATT_PRESENT	__BIT(5)
     55  1.9.2.2  pgoyette #define	 AXP_POWER_MODE_BATT_CHARGING	__BIT(6)
     56  1.9.2.2  pgoyette 
     57  1.9.2.2  pgoyette #define AXP_POWER_DISABLE_REG	0x32
     58  1.9.2.2  pgoyette #define	 AXP_POWER_DISABLE_CTRL	__BIT(7)
     59  1.9.2.2  pgoyette 
     60  1.9.2.2  pgoyette #define AXP_IRQ_ENABLE_REG(n)	(0x40 + (n) - 1)
     61  1.9.2.2  pgoyette #define	 AXP_IRQ1_ACIN_RAISE	__BIT(6)
     62  1.9.2.2  pgoyette #define	 AXP_IRQ1_ACIN_LOWER	__BIT(5)
     63  1.9.2.2  pgoyette #define	 AXP_IRQ1_VBUS_RAISE	__BIT(3)
     64  1.9.2.2  pgoyette #define	 AXP_IRQ1_VBUS_LOWER	__BIT(2)
     65  1.9.2.2  pgoyette #define AXP_IRQ_STATUS_REG(n)	(0x48 + (n) - 1)
     66  1.9.2.2  pgoyette 
     67  1.9.2.3  pgoyette #define	AXP_BATSENSE_HI_REG	0x78
     68  1.9.2.3  pgoyette #define	AXP_BATSENSE_LO_REG	0x79
     69  1.9.2.3  pgoyette 
     70  1.9.2.3  pgoyette #define	AXP_BATTCHG_HI_REG	0x7a
     71  1.9.2.3  pgoyette #define	AXP_BATTCHG_LO_REG	0x7b
     72  1.9.2.3  pgoyette 
     73  1.9.2.3  pgoyette #define	AXP_BATTDISCHG_HI_REG	0x7c
     74  1.9.2.3  pgoyette #define	AXP_BATTDISCHG_LO_REG	0x7d
     75  1.9.2.3  pgoyette 
     76  1.9.2.3  pgoyette #define	AXP_ADC_RAW(_hi, _lo)	\
     77  1.9.2.5  pgoyette 	(((u_int)(_hi) << 4) | ((_lo) & 0xf))
     78  1.9.2.3  pgoyette 
     79  1.9.2.2  pgoyette #define	AXP_FUEL_GAUGE_CTRL_REG	0xb8
     80  1.9.2.2  pgoyette #define	 AXP_FUEL_GAUGE_CTRL_EN	__BIT(7)
     81  1.9.2.3  pgoyette 
     82  1.9.2.2  pgoyette #define	AXP_BATT_CAP_REG	0xb9
     83  1.9.2.2  pgoyette #define	 AXP_BATT_CAP_VALID	__BIT(7)
     84  1.9.2.2  pgoyette #define	 AXP_BATT_CAP_PERCENT	__BITS(6,0)
     85  1.9.2.2  pgoyette 
     86  1.9.2.5  pgoyette #define	AXP_BATT_MAX_CAP_HI_REG	0xe0
     87  1.9.2.5  pgoyette #define	 AXP_BATT_MAX_CAP_VALID	__BIT(7)
     88  1.9.2.5  pgoyette #define	AXP_BATT_MAX_CAP_LO_REG	0xe1
     89  1.9.2.5  pgoyette 
     90  1.9.2.5  pgoyette #define	AXP_BATT_COULOMB_HI_REG	0xe2
     91  1.9.2.5  pgoyette #define	 AXP_BATT_COULOMB_VALID	__BIT(7)
     92  1.9.2.5  pgoyette #define	AXP_BATT_COULOMB_LO_REG	0xe3
     93  1.9.2.5  pgoyette 
     94  1.9.2.5  pgoyette #define	AXP_COULOMB_RAW(_hi, _lo)	\
     95  1.9.2.5  pgoyette 	(((u_int)(_hi & ~__BIT(7)) << 8) | (_lo))
     96  1.9.2.5  pgoyette 
     97  1.9.2.2  pgoyette #define	AXP_BATT_CAP_WARN_REG	0xe6
     98  1.9.2.2  pgoyette #define	 AXP_BATT_CAP_WARN_LV1	__BITS(7,4)
     99  1.9.2.2  pgoyette #define	 AXP_BATT_CAP_WARN_LV2	__BITS(3,0)
    100  1.9.2.2  pgoyette 
    101  1.9.2.2  pgoyette struct axppmic_ctrl {
    102  1.9.2.2  pgoyette 	device_t	c_dev;
    103  1.9.2.2  pgoyette 
    104  1.9.2.2  pgoyette 	const char *	c_name;
    105  1.9.2.2  pgoyette 	u_int		c_min;
    106  1.9.2.2  pgoyette 	u_int		c_max;
    107  1.9.2.2  pgoyette 	u_int		c_step1;
    108  1.9.2.2  pgoyette 	u_int		c_step1cnt;
    109  1.9.2.2  pgoyette 	u_int		c_step2;
    110  1.9.2.2  pgoyette 	u_int		c_step2cnt;
    111  1.9.2.2  pgoyette 
    112  1.9.2.2  pgoyette 	uint8_t		c_enable_reg;
    113  1.9.2.2  pgoyette 	uint8_t		c_enable_mask;
    114  1.9.2.2  pgoyette 
    115  1.9.2.2  pgoyette 	uint8_t		c_voltage_reg;
    116  1.9.2.2  pgoyette 	uint8_t		c_voltage_mask;
    117  1.9.2.2  pgoyette };
    118  1.9.2.2  pgoyette 
    119  1.9.2.2  pgoyette #define AXP_CTRL(name, min, max, step, ereg, emask, vreg, vmask)	\
    120  1.9.2.2  pgoyette 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    121  1.9.2.2  pgoyette 	  .c_step1 = (step), .c_step1cnt = (((max) - (min)) / (step)) + 1, \
    122  1.9.2.2  pgoyette 	  .c_step2 = 0, .c_step2cnt = 0,				\
    123  1.9.2.2  pgoyette 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    124  1.9.2.2  pgoyette 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    125  1.9.2.2  pgoyette 
    126  1.9.2.2  pgoyette #define AXP_CTRL2(name, min, max, step1, step1cnt, step2, step2cnt, ereg, emask, vreg, vmask) \
    127  1.9.2.2  pgoyette 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    128  1.9.2.2  pgoyette 	  .c_step1 = (step1), .c_step1cnt = (step1cnt),			\
    129  1.9.2.2  pgoyette 	  .c_step2 = (step2), .c_step2cnt = (step2cnt),			\
    130  1.9.2.2  pgoyette 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    131  1.9.2.2  pgoyette 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    132  1.9.2.2  pgoyette 
    133  1.9.2.2  pgoyette static const struct axppmic_ctrl axp803_ctrls[] = {
    134  1.9.2.2  pgoyette 	AXP_CTRL("dldo1", 700, 3300, 100,
    135  1.9.2.2  pgoyette 		0x12, __BIT(3), 0x15, __BITS(4,0)),
    136  1.9.2.2  pgoyette 	AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
    137  1.9.2.2  pgoyette 		0x12, __BIT(4), 0x16, __BITS(4,0)),
    138  1.9.2.2  pgoyette 	AXP_CTRL("dldo3", 700, 3300, 100,
    139  1.9.2.2  pgoyette 	 	0x12, __BIT(5), 0x17, __BITS(4,0)),
    140  1.9.2.2  pgoyette 	AXP_CTRL("dldo4", 700, 3300, 100,
    141  1.9.2.2  pgoyette 		0x12, __BIT(6), 0x18, __BITS(4,0)),
    142  1.9.2.2  pgoyette 	AXP_CTRL("eldo1", 700, 1900, 50,
    143  1.9.2.2  pgoyette 		0x12, __BIT(0), 0x19, __BITS(4,0)),
    144  1.9.2.2  pgoyette 	AXP_CTRL("eldo2", 700, 1900, 50,
    145  1.9.2.2  pgoyette 		0x12, __BIT(1), 0x1a, __BITS(4,0)),
    146  1.9.2.2  pgoyette 	AXP_CTRL("eldo3", 700, 1900, 50,
    147  1.9.2.2  pgoyette 		0x12, __BIT(2), 0x1b, __BITS(4,0)),
    148  1.9.2.2  pgoyette 	AXP_CTRL("fldo1", 700, 1450, 50,
    149  1.9.2.2  pgoyette 		0x13, __BIT(2), 0x1c, __BITS(3,0)),
    150  1.9.2.2  pgoyette 	AXP_CTRL("fldo2", 700, 1450, 50,
    151  1.9.2.2  pgoyette 		0x13, __BIT(3), 0x1d, __BITS(3,0)),
    152  1.9.2.2  pgoyette 	AXP_CTRL("dcdc1", 1600, 3400, 100,
    153  1.9.2.2  pgoyette 		0x10, __BIT(0), 0x20, __BITS(4,0)),
    154  1.9.2.2  pgoyette 	AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
    155  1.9.2.2  pgoyette 		0x10, __BIT(1), 0x21, __BITS(6,0)),
    156  1.9.2.2  pgoyette 	AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
    157  1.9.2.2  pgoyette 		0x10, __BIT(2), 0x22, __BITS(6,0)),
    158  1.9.2.2  pgoyette 	AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
    159  1.9.2.2  pgoyette 		0x10, __BIT(3), 0x23, __BITS(6,0)),
    160  1.9.2.2  pgoyette 	AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
    161  1.9.2.2  pgoyette 		0x10, __BIT(4), 0x24, __BITS(6,0)),
    162  1.9.2.2  pgoyette 	AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
    163  1.9.2.2  pgoyette 		0x10, __BIT(5), 0x25, __BITS(6,0)),
    164  1.9.2.2  pgoyette 	AXP_CTRL("aldo1", 700, 3300, 100,
    165  1.9.2.2  pgoyette 		0x13, __BIT(5), 0x28, __BITS(4,0)),
    166  1.9.2.2  pgoyette 	AXP_CTRL("aldo2", 700, 3300, 100,
    167  1.9.2.2  pgoyette 		0x13, __BIT(6), 0x29, __BITS(4,0)),
    168  1.9.2.2  pgoyette 	AXP_CTRL("aldo3", 700, 3300, 100,
    169  1.9.2.2  pgoyette 		0x13, __BIT(7), 0x2a, __BITS(4,0)),
    170  1.9.2.2  pgoyette };
    171  1.9.2.2  pgoyette 
    172  1.9.2.2  pgoyette static const struct axppmic_ctrl axp805_ctrls[] = {
    173  1.9.2.2  pgoyette 	AXP_CTRL2("dcdca", 600, 1520, 10, 51, 20, 21,
    174  1.9.2.2  pgoyette 		0x10, __BIT(0), 0x12, __BITS(6,0)),
    175  1.9.2.2  pgoyette 	AXP_CTRL("dcdcb", 1000, 2550, 50,
    176  1.9.2.2  pgoyette 		0x10, __BIT(1), 0x13, __BITS(4,0)),
    177  1.9.2.2  pgoyette 	AXP_CTRL2("dcdcc", 600, 1520, 10, 51, 20, 21,
    178  1.9.2.2  pgoyette 		0x10, __BIT(2), 0x14, __BITS(6,0)),
    179  1.9.2.2  pgoyette 	AXP_CTRL2("dcdcd", 600, 3300, 20, 46, 100, 18,
    180  1.9.2.2  pgoyette 		0x10, __BIT(3), 0x15, __BITS(5,0)),
    181  1.9.2.2  pgoyette 	AXP_CTRL("dcdce", 1100, 3400, 100,
    182  1.9.2.2  pgoyette 		0x10, __BIT(4), 0x16, __BITS(4,0)),
    183  1.9.2.2  pgoyette 	AXP_CTRL("aldo1", 700, 3300, 100,
    184  1.9.2.2  pgoyette 		0x10, __BIT(5), 0x17, __BITS(4,0)),
    185  1.9.2.2  pgoyette 	AXP_CTRL("aldo2", 700, 3400, 100,
    186  1.9.2.2  pgoyette 		0x10, __BIT(6), 0x18, __BITS(4,0)),
    187  1.9.2.2  pgoyette 	AXP_CTRL("aldo3", 700, 3300, 100,
    188  1.9.2.2  pgoyette 		0x10, __BIT(7), 0x19, __BITS(4,0)),
    189  1.9.2.2  pgoyette 	AXP_CTRL("bldo1", 700, 1900, 100,
    190  1.9.2.2  pgoyette 		0x11, __BIT(0), 0x20, __BITS(3,0)),
    191  1.9.2.2  pgoyette 	AXP_CTRL("bldo2", 700, 1900, 100,
    192  1.9.2.2  pgoyette 		0x11, __BIT(1), 0x21, __BITS(3,0)),
    193  1.9.2.2  pgoyette 	AXP_CTRL("bldo3", 700, 1900, 100,
    194  1.9.2.2  pgoyette 		0x11, __BIT(2), 0x22, __BITS(3,0)),
    195  1.9.2.2  pgoyette 	AXP_CTRL("bldo4", 700, 1900, 100,
    196  1.9.2.2  pgoyette 		0x11, __BIT(3), 0x23, __BITS(3,0)),
    197  1.9.2.2  pgoyette 	AXP_CTRL("cldo1", 700, 3300, 100,
    198  1.9.2.2  pgoyette 		0x11, __BIT(4), 0x24, __BITS(4,0)),
    199  1.9.2.2  pgoyette 	AXP_CTRL2("cldo2", 700, 4200, 100, 28, 200, 4,
    200  1.9.2.2  pgoyette 		0x11, __BIT(5), 0x25, __BITS(4,0)),
    201  1.9.2.2  pgoyette 	AXP_CTRL("cldo3", 700, 3300, 100,
    202  1.9.2.2  pgoyette 		0x11, __BIT(6), 0x26, __BITS(4,0)),
    203  1.9.2.2  pgoyette };
    204  1.9.2.2  pgoyette 
    205  1.9.2.6  pgoyette static const struct axppmic_ctrl axp813_ctrls[] = {
    206  1.9.2.6  pgoyette 	AXP_CTRL("dldo1", 700, 3300, 100,
    207  1.9.2.6  pgoyette 		0x12, __BIT(3), 0x15, __BITS(4,0)),
    208  1.9.2.6  pgoyette 	AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
    209  1.9.2.6  pgoyette 		0x12, __BIT(4), 0x16, __BITS(4,0)),
    210  1.9.2.6  pgoyette 	AXP_CTRL("dldo3", 700, 3300, 100,
    211  1.9.2.6  pgoyette 	 	0x12, __BIT(5), 0x17, __BITS(4,0)),
    212  1.9.2.6  pgoyette 	AXP_CTRL("dldo4", 700, 3300, 100,
    213  1.9.2.6  pgoyette 		0x12, __BIT(6), 0x18, __BITS(4,0)),
    214  1.9.2.6  pgoyette 	AXP_CTRL("eldo1", 700, 1900, 50,
    215  1.9.2.6  pgoyette 		0x12, __BIT(0), 0x19, __BITS(4,0)),
    216  1.9.2.6  pgoyette 	AXP_CTRL("eldo2", 700, 1900, 50,
    217  1.9.2.6  pgoyette 		0x12, __BIT(1), 0x1a, __BITS(4,0)),
    218  1.9.2.6  pgoyette 	AXP_CTRL("eldo3", 700, 1900, 50,
    219  1.9.2.6  pgoyette 		0x12, __BIT(2), 0x1b, __BITS(4,0)),
    220  1.9.2.6  pgoyette 	AXP_CTRL("fldo1", 700, 1450, 50,
    221  1.9.2.6  pgoyette 		0x13, __BIT(2), 0x1c, __BITS(3,0)),
    222  1.9.2.6  pgoyette 	AXP_CTRL("fldo2", 700, 1450, 50,
    223  1.9.2.6  pgoyette 		0x13, __BIT(3), 0x1d, __BITS(3,0)),
    224  1.9.2.6  pgoyette 	AXP_CTRL("dcdc1", 1600, 3400, 100,
    225  1.9.2.6  pgoyette 		0x10, __BIT(0), 0x20, __BITS(4,0)),
    226  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
    227  1.9.2.6  pgoyette 		0x10, __BIT(1), 0x21, __BITS(6,0)),
    228  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
    229  1.9.2.6  pgoyette 		0x10, __BIT(2), 0x22, __BITS(6,0)),
    230  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
    231  1.9.2.6  pgoyette 		0x10, __BIT(3), 0x23, __BITS(6,0)),
    232  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
    233  1.9.2.6  pgoyette 		0x10, __BIT(4), 0x24, __BITS(6,0)),
    234  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
    235  1.9.2.6  pgoyette 		0x10, __BIT(5), 0x25, __BITS(6,0)),
    236  1.9.2.6  pgoyette 	AXP_CTRL2("dcdc7", 600, 1520, 10, 51, 20, 21,
    237  1.9.2.6  pgoyette 		0x10, __BIT(6), 0x26, __BITS(6,0)),
    238  1.9.2.6  pgoyette 	AXP_CTRL("aldo1", 700, 3300, 100,
    239  1.9.2.6  pgoyette 		0x13, __BIT(5), 0x28, __BITS(4,0)),
    240  1.9.2.6  pgoyette 	AXP_CTRL("aldo2", 700, 3300, 100,
    241  1.9.2.6  pgoyette 		0x13, __BIT(6), 0x29, __BITS(4,0)),
    242  1.9.2.6  pgoyette 	AXP_CTRL("aldo3", 700, 3300, 100,
    243  1.9.2.6  pgoyette 		0x13, __BIT(7), 0x2a, __BITS(4,0)),
    244  1.9.2.6  pgoyette };
    245  1.9.2.6  pgoyette 
    246  1.9.2.2  pgoyette struct axppmic_irq {
    247  1.9.2.2  pgoyette 	u_int reg;
    248  1.9.2.2  pgoyette 	uint8_t mask;
    249  1.9.2.2  pgoyette };
    250  1.9.2.2  pgoyette 
    251  1.9.2.2  pgoyette #define	AXPPMIC_IRQ(_reg, _mask)	\
    252  1.9.2.2  pgoyette 	{ .reg = (_reg), .mask = (_mask) }
    253  1.9.2.2  pgoyette 
    254  1.9.2.2  pgoyette struct axppmic_config {
    255  1.9.2.2  pgoyette 	const char *name;
    256  1.9.2.2  pgoyette 	const struct axppmic_ctrl *controls;
    257  1.9.2.2  pgoyette 	u_int ncontrols;
    258  1.9.2.2  pgoyette 	u_int irq_regs;
    259  1.9.2.2  pgoyette 	bool has_battery;
    260  1.9.2.2  pgoyette 	bool has_fuel_gauge;
    261  1.9.2.2  pgoyette 	struct axppmic_irq poklirq;
    262  1.9.2.2  pgoyette 	struct axppmic_irq acinirq;
    263  1.9.2.2  pgoyette 	struct axppmic_irq vbusirq;
    264  1.9.2.2  pgoyette 	struct axppmic_irq battirq;
    265  1.9.2.2  pgoyette 	struct axppmic_irq chargeirq;
    266  1.9.2.2  pgoyette 	struct axppmic_irq chargestirq;
    267  1.9.2.3  pgoyette 	u_int batsense_step;	/* uV */
    268  1.9.2.3  pgoyette 	u_int charge_step;	/* uA */
    269  1.9.2.3  pgoyette 	u_int discharge_step;	/* uA */
    270  1.9.2.3  pgoyette 	u_int maxcap_step;	/* uAh */
    271  1.9.2.3  pgoyette 	u_int coulomb_step;	/* uAh */
    272  1.9.2.2  pgoyette };
    273  1.9.2.2  pgoyette 
    274  1.9.2.2  pgoyette enum axppmic_sensor {
    275  1.9.2.2  pgoyette 	AXP_SENSOR_ACIN_PRESENT,
    276  1.9.2.2  pgoyette 	AXP_SENSOR_VBUS_PRESENT,
    277  1.9.2.2  pgoyette 	AXP_SENSOR_BATT_PRESENT,
    278  1.9.2.2  pgoyette 	AXP_SENSOR_BATT_CHARGING,
    279  1.9.2.2  pgoyette 	AXP_SENSOR_BATT_CHARGE_STATE,
    280  1.9.2.3  pgoyette 	AXP_SENSOR_BATT_VOLTAGE,
    281  1.9.2.3  pgoyette 	AXP_SENSOR_BATT_CHARGE_CURRENT,
    282  1.9.2.3  pgoyette 	AXP_SENSOR_BATT_DISCHARGE_CURRENT,
    283  1.9.2.3  pgoyette 	AXP_SENSOR_BATT_CAPACITY_PERCENT,
    284  1.9.2.5  pgoyette 	AXP_SENSOR_BATT_MAXIMUM_CAPACITY,
    285  1.9.2.5  pgoyette 	AXP_SENSOR_BATT_CURRENT_CAPACITY,
    286  1.9.2.2  pgoyette 	AXP_NSENSORS
    287  1.9.2.2  pgoyette };
    288  1.9.2.2  pgoyette 
    289  1.9.2.2  pgoyette struct axppmic_softc {
    290  1.9.2.2  pgoyette 	device_t	sc_dev;
    291  1.9.2.2  pgoyette 	i2c_tag_t	sc_i2c;
    292  1.9.2.2  pgoyette 	i2c_addr_t	sc_addr;
    293  1.9.2.2  pgoyette 	int		sc_phandle;
    294  1.9.2.2  pgoyette 
    295  1.9.2.2  pgoyette 	const struct axppmic_config *sc_conf;
    296  1.9.2.2  pgoyette 
    297  1.9.2.2  pgoyette 	struct sysmon_pswitch sc_smpsw;
    298  1.9.2.2  pgoyette 
    299  1.9.2.2  pgoyette 	struct sysmon_envsys *sc_sme;
    300  1.9.2.2  pgoyette 
    301  1.9.2.2  pgoyette 	envsys_data_t	sc_sensor[AXP_NSENSORS];
    302  1.9.2.2  pgoyette 
    303  1.9.2.2  pgoyette 	u_int		sc_warn_thres;
    304  1.9.2.2  pgoyette 	u_int		sc_shut_thres;
    305  1.9.2.2  pgoyette };
    306  1.9.2.2  pgoyette 
    307  1.9.2.2  pgoyette struct axpreg_softc {
    308  1.9.2.2  pgoyette 	device_t	sc_dev;
    309  1.9.2.2  pgoyette 	i2c_tag_t	sc_i2c;
    310  1.9.2.2  pgoyette 	i2c_addr_t	sc_addr;
    311  1.9.2.2  pgoyette 	const struct axppmic_ctrl *sc_ctrl;
    312  1.9.2.2  pgoyette };
    313  1.9.2.2  pgoyette 
    314  1.9.2.2  pgoyette struct axpreg_attach_args {
    315  1.9.2.2  pgoyette 	const struct axppmic_ctrl *reg_ctrl;
    316  1.9.2.2  pgoyette 	int		reg_phandle;
    317  1.9.2.2  pgoyette 	i2c_tag_t	reg_i2c;
    318  1.9.2.2  pgoyette 	i2c_addr_t	reg_addr;
    319  1.9.2.2  pgoyette };
    320  1.9.2.2  pgoyette 
    321  1.9.2.2  pgoyette static const struct axppmic_config axp803_config = {
    322  1.9.2.2  pgoyette 	.name = "AXP803",
    323  1.9.2.2  pgoyette 	.controls = axp803_ctrls,
    324  1.9.2.2  pgoyette 	.ncontrols = __arraycount(axp803_ctrls),
    325  1.9.2.2  pgoyette 	.irq_regs = 6,
    326  1.9.2.2  pgoyette 	.has_battery = true,
    327  1.9.2.2  pgoyette 	.has_fuel_gauge = true,
    328  1.9.2.3  pgoyette 	.batsense_step = 1100,
    329  1.9.2.3  pgoyette 	.charge_step = 1000,
    330  1.9.2.3  pgoyette 	.discharge_step = 1000,
    331  1.9.2.5  pgoyette 	.maxcap_step = 1456,
    332  1.9.2.5  pgoyette 	.coulomb_step = 1456,
    333  1.9.2.2  pgoyette 	.poklirq = AXPPMIC_IRQ(5, __BIT(3)),
    334  1.9.2.2  pgoyette 	.acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
    335  1.9.2.2  pgoyette 	.vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
    336  1.9.2.2  pgoyette 	.battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
    337  1.9.2.2  pgoyette 	.chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
    338  1.9.2.2  pgoyette 	.chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
    339  1.9.2.2  pgoyette };
    340  1.9.2.2  pgoyette 
    341  1.9.2.2  pgoyette static const struct axppmic_config axp805_config = {
    342  1.9.2.2  pgoyette 	.name = "AXP805/806",
    343  1.9.2.2  pgoyette 	.controls = axp805_ctrls,
    344  1.9.2.2  pgoyette 	.ncontrols = __arraycount(axp805_ctrls),
    345  1.9.2.2  pgoyette 	.irq_regs = 2,
    346  1.9.2.2  pgoyette 	.poklirq = AXPPMIC_IRQ(2, __BIT(0)),
    347  1.9.2.2  pgoyette };
    348  1.9.2.2  pgoyette 
    349  1.9.2.6  pgoyette static const struct axppmic_config axp813_config = {
    350  1.9.2.6  pgoyette 	.name = "AXP813",
    351  1.9.2.6  pgoyette 	.controls = axp813_ctrls,
    352  1.9.2.6  pgoyette 	.ncontrols = __arraycount(axp813_ctrls),
    353  1.9.2.6  pgoyette 	.irq_regs = 6,
    354  1.9.2.6  pgoyette 	.has_battery = true,
    355  1.9.2.6  pgoyette 	.has_fuel_gauge = true,
    356  1.9.2.6  pgoyette 	.batsense_step = 1100,
    357  1.9.2.6  pgoyette 	.charge_step = 1000,
    358  1.9.2.6  pgoyette 	.discharge_step = 1000,
    359  1.9.2.6  pgoyette 	.maxcap_step = 1456,
    360  1.9.2.6  pgoyette 	.coulomb_step = 1456,
    361  1.9.2.6  pgoyette 	.poklirq = AXPPMIC_IRQ(5, __BIT(3)),
    362  1.9.2.6  pgoyette 	.acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
    363  1.9.2.6  pgoyette 	.vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
    364  1.9.2.6  pgoyette 	.battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
    365  1.9.2.6  pgoyette 	.chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
    366  1.9.2.6  pgoyette 	.chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
    367  1.9.2.6  pgoyette };
    368  1.9.2.6  pgoyette 
    369  1.9.2.4  pgoyette static const struct device_compatible_entry compat_data[] = {
    370  1.9.2.4  pgoyette 	{ "x-powers,axp803",		(uintptr_t)&axp803_config },
    371  1.9.2.4  pgoyette 	{ "x-powers,axp805",		(uintptr_t)&axp805_config },
    372  1.9.2.4  pgoyette 	{ "x-powers,axp806",		(uintptr_t)&axp805_config },
    373  1.9.2.6  pgoyette 	{ "x-powers,axp813",		(uintptr_t)&axp813_config },
    374  1.9.2.4  pgoyette 	{ NULL,				0 }
    375  1.9.2.2  pgoyette };
    376  1.9.2.2  pgoyette 
    377  1.9.2.2  pgoyette static int
    378  1.9.2.2  pgoyette axppmic_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t *val, int flags)
    379  1.9.2.2  pgoyette {
    380  1.9.2.2  pgoyette 	return iic_smbus_read_byte(tag, addr, reg, val, flags);
    381  1.9.2.2  pgoyette }
    382  1.9.2.2  pgoyette 
    383  1.9.2.2  pgoyette static int
    384  1.9.2.2  pgoyette axppmic_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t val, int flags)
    385  1.9.2.2  pgoyette {
    386  1.9.2.2  pgoyette 	return iic_smbus_write_byte(tag, addr, reg, val, flags);
    387  1.9.2.2  pgoyette }
    388  1.9.2.2  pgoyette 
    389  1.9.2.2  pgoyette static int
    390  1.9.2.2  pgoyette axppmic_set_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int min, u_int max)
    391  1.9.2.2  pgoyette {
    392  1.9.2.2  pgoyette 	const int flags = (cold ? I2C_F_POLL : 0);
    393  1.9.2.2  pgoyette 	u_int vol, reg_val;
    394  1.9.2.2  pgoyette 	int nstep, error;
    395  1.9.2.2  pgoyette 	uint8_t val;
    396  1.9.2.2  pgoyette 
    397  1.9.2.2  pgoyette 	if (!c->c_voltage_mask)
    398  1.9.2.2  pgoyette 		return EINVAL;
    399  1.9.2.2  pgoyette 
    400  1.9.2.2  pgoyette 	if (min < c->c_min || min > c->c_max)
    401  1.9.2.2  pgoyette 		return EINVAL;
    402  1.9.2.2  pgoyette 
    403  1.9.2.2  pgoyette 	reg_val = 0;
    404  1.9.2.2  pgoyette 	nstep = 1;
    405  1.9.2.2  pgoyette 	vol = c->c_min;
    406  1.9.2.2  pgoyette 
    407  1.9.2.2  pgoyette 	for (nstep = 0; nstep < c->c_step1cnt && vol < min; nstep++) {
    408  1.9.2.2  pgoyette 		++reg_val;
    409  1.9.2.2  pgoyette 		vol += c->c_step1;
    410  1.9.2.2  pgoyette 	}
    411  1.9.2.2  pgoyette 	for (nstep = 0; nstep < c->c_step2cnt && vol < min; nstep++) {
    412  1.9.2.2  pgoyette 		++reg_val;
    413  1.9.2.2  pgoyette 		vol += c->c_step2;
    414  1.9.2.2  pgoyette 	}
    415  1.9.2.2  pgoyette 
    416  1.9.2.2  pgoyette 	if (vol > max)
    417  1.9.2.2  pgoyette 		return EINVAL;
    418  1.9.2.2  pgoyette 
    419  1.9.2.2  pgoyette 	iic_acquire_bus(tag, flags);
    420  1.9.2.2  pgoyette 	if ((error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags)) == 0) {
    421  1.9.2.2  pgoyette 		val &= ~c->c_voltage_mask;
    422  1.9.2.2  pgoyette 		val |= __SHIFTIN(reg_val, c->c_voltage_mask);
    423  1.9.2.2  pgoyette 		error = axppmic_write(tag, addr, c->c_voltage_reg, val, flags);
    424  1.9.2.2  pgoyette 	}
    425  1.9.2.2  pgoyette 	iic_release_bus(tag, flags);
    426  1.9.2.2  pgoyette 
    427  1.9.2.2  pgoyette 	return error;
    428  1.9.2.2  pgoyette }
    429  1.9.2.2  pgoyette 
    430  1.9.2.2  pgoyette static int
    431  1.9.2.2  pgoyette axppmic_get_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int *pvol)
    432  1.9.2.2  pgoyette {
    433  1.9.2.2  pgoyette 	const int flags = (cold ? I2C_F_POLL : 0);
    434  1.9.2.2  pgoyette 	int reg_val, error;
    435  1.9.2.2  pgoyette 	uint8_t val;
    436  1.9.2.2  pgoyette 
    437  1.9.2.2  pgoyette 	if (!c->c_voltage_mask)
    438  1.9.2.2  pgoyette 		return EINVAL;
    439  1.9.2.2  pgoyette 
    440  1.9.2.2  pgoyette 	iic_acquire_bus(tag, flags);
    441  1.9.2.2  pgoyette 	error = axppmic_read(tag, addr, c->c_voltage_reg, &val, flags);
    442  1.9.2.2  pgoyette 	iic_release_bus(tag, flags);
    443  1.9.2.2  pgoyette 	if (error)
    444  1.9.2.2  pgoyette 		return error;
    445  1.9.2.2  pgoyette 
    446  1.9.2.2  pgoyette 	reg_val = __SHIFTOUT(val, c->c_voltage_mask);
    447  1.9.2.2  pgoyette 	if (reg_val < c->c_step1cnt) {
    448  1.9.2.2  pgoyette 		*pvol = c->c_min + reg_val * c->c_step1;
    449  1.9.2.2  pgoyette 	} else {
    450  1.9.2.2  pgoyette 		*pvol = c->c_min + (c->c_step1cnt * c->c_step1) +
    451  1.9.2.2  pgoyette 		    ((reg_val - c->c_step1cnt) * c->c_step2);
    452  1.9.2.2  pgoyette 	}
    453  1.9.2.2  pgoyette 
    454  1.9.2.2  pgoyette 	return 0;
    455  1.9.2.2  pgoyette }
    456  1.9.2.2  pgoyette 
    457  1.9.2.2  pgoyette static void
    458  1.9.2.2  pgoyette axppmic_power_poweroff(device_t dev)
    459  1.9.2.2  pgoyette {
    460  1.9.2.2  pgoyette 	struct axppmic_softc *sc = device_private(dev);
    461  1.9.2.2  pgoyette 
    462  1.9.2.2  pgoyette 	delay(1000000);
    463  1.9.2.2  pgoyette 
    464  1.9.2.2  pgoyette 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    465  1.9.2.2  pgoyette 	axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_POWER_DISABLE_REG, AXP_POWER_DISABLE_CTRL, I2C_F_POLL);
    466  1.9.2.2  pgoyette 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    467  1.9.2.2  pgoyette }
    468  1.9.2.2  pgoyette 
    469  1.9.2.2  pgoyette static struct fdtbus_power_controller_func axppmic_power_funcs = {
    470  1.9.2.2  pgoyette 	.poweroff = axppmic_power_poweroff,
    471  1.9.2.2  pgoyette };
    472  1.9.2.2  pgoyette 
    473  1.9.2.2  pgoyette static void
    474  1.9.2.2  pgoyette axppmic_task_shut(void *priv)
    475  1.9.2.2  pgoyette {
    476  1.9.2.2  pgoyette 	struct axppmic_softc *sc = priv;
    477  1.9.2.2  pgoyette 
    478  1.9.2.2  pgoyette 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
    479  1.9.2.2  pgoyette }
    480  1.9.2.2  pgoyette 
    481  1.9.2.2  pgoyette static void
    482  1.9.2.2  pgoyette axppmic_sensor_update(struct sysmon_envsys *sme, envsys_data_t *e)
    483  1.9.2.2  pgoyette {
    484  1.9.2.2  pgoyette 	struct axppmic_softc *sc = sme->sme_cookie;
    485  1.9.2.3  pgoyette 	const struct axppmic_config *c = sc->sc_conf;
    486  1.9.2.2  pgoyette 	const int flags = I2C_F_POLL;
    487  1.9.2.3  pgoyette 	uint8_t val, lo, hi;
    488  1.9.2.2  pgoyette 
    489  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    490  1.9.2.2  pgoyette 
    491  1.9.2.3  pgoyette 	const bool battery_present =
    492  1.9.2.3  pgoyette 	    sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].state == ENVSYS_SVALID &&
    493  1.9.2.3  pgoyette 	    sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].value_cur == 1;
    494  1.9.2.3  pgoyette 
    495  1.9.2.2  pgoyette 	switch (e->private) {
    496  1.9.2.2  pgoyette 	case AXP_SENSOR_ACIN_PRESENT:
    497  1.9.2.2  pgoyette 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
    498  1.9.2.2  pgoyette 			e->state = ENVSYS_SVALID;
    499  1.9.2.2  pgoyette 			e->value_cur = !!(val & AXP_POWER_SOURCE_ACIN_PRESENT);
    500  1.9.2.2  pgoyette 		}
    501  1.9.2.2  pgoyette 		break;
    502  1.9.2.2  pgoyette 	case AXP_SENSOR_VBUS_PRESENT:
    503  1.9.2.2  pgoyette 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0) {
    504  1.9.2.2  pgoyette 			e->state = ENVSYS_SVALID;
    505  1.9.2.2  pgoyette 			e->value_cur = !!(val & AXP_POWER_SOURCE_VBUS_PRESENT);
    506  1.9.2.2  pgoyette 		}
    507  1.9.2.2  pgoyette 		break;
    508  1.9.2.2  pgoyette 	case AXP_SENSOR_BATT_PRESENT:
    509  1.9.2.2  pgoyette 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
    510  1.9.2.2  pgoyette 			if (val & AXP_POWER_MODE_BATT_VALID) {
    511  1.9.2.2  pgoyette 				e->state = ENVSYS_SVALID;
    512  1.9.2.2  pgoyette 				e->value_cur = !!(val & AXP_POWER_MODE_BATT_PRESENT);
    513  1.9.2.2  pgoyette 			}
    514  1.9.2.2  pgoyette 		}
    515  1.9.2.2  pgoyette 		break;
    516  1.9.2.2  pgoyette 	case AXP_SENSOR_BATT_CHARGING:
    517  1.9.2.2  pgoyette 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, flags) == 0) {
    518  1.9.2.2  pgoyette 			e->state = ENVSYS_SVALID;
    519  1.9.2.2  pgoyette 			e->value_cur = !!(val & AXP_POWER_MODE_BATT_CHARGING);
    520  1.9.2.2  pgoyette 		}
    521  1.9.2.2  pgoyette 		break;
    522  1.9.2.2  pgoyette 	case AXP_SENSOR_BATT_CHARGE_STATE:
    523  1.9.2.3  pgoyette 		if (battery_present &&
    524  1.9.2.2  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
    525  1.9.2.2  pgoyette 		    (val & AXP_BATT_CAP_VALID) != 0) {
    526  1.9.2.2  pgoyette 			const u_int batt_val = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
    527  1.9.2.2  pgoyette 			if (batt_val <= sc->sc_shut_thres) {
    528  1.9.2.2  pgoyette 				e->state = ENVSYS_SCRITICAL;
    529  1.9.2.2  pgoyette 				e->value_cur = ENVSYS_BATTERY_CAPACITY_CRITICAL;
    530  1.9.2.2  pgoyette 			} else if (batt_val <= sc->sc_warn_thres) {
    531  1.9.2.2  pgoyette 				e->state = ENVSYS_SWARNUNDER;
    532  1.9.2.2  pgoyette 				e->value_cur = ENVSYS_BATTERY_CAPACITY_WARNING;
    533  1.9.2.2  pgoyette 			} else {
    534  1.9.2.2  pgoyette 				e->state = ENVSYS_SVALID;
    535  1.9.2.2  pgoyette 				e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
    536  1.9.2.2  pgoyette 			}
    537  1.9.2.2  pgoyette 		}
    538  1.9.2.2  pgoyette 		break;
    539  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_CAPACITY_PERCENT:
    540  1.9.2.3  pgoyette 		if (battery_present &&
    541  1.9.2.2  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, flags) == 0 &&
    542  1.9.2.2  pgoyette 		    (val & AXP_BATT_CAP_VALID) != 0) {
    543  1.9.2.2  pgoyette 			e->state = ENVSYS_SVALID;
    544  1.9.2.2  pgoyette 			e->value_cur = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
    545  1.9.2.2  pgoyette 		}
    546  1.9.2.2  pgoyette 		break;
    547  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_VOLTAGE:
    548  1.9.2.3  pgoyette 		if (battery_present &&
    549  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_HI_REG, &hi, flags) == 0 &&
    550  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_LO_REG, &lo, flags) == 0) {
    551  1.9.2.3  pgoyette 			e->state = ENVSYS_SVALID;
    552  1.9.2.3  pgoyette 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->batsense_step;
    553  1.9.2.3  pgoyette 		}
    554  1.9.2.3  pgoyette 		break;
    555  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_CHARGE_CURRENT:
    556  1.9.2.3  pgoyette 		if (battery_present &&
    557  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0 &&
    558  1.9.2.3  pgoyette 		    (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) != 0 &&
    559  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_HI_REG, &hi, flags) == 0 &&
    560  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_LO_REG, &lo, flags) == 0) {
    561  1.9.2.3  pgoyette 			e->state = ENVSYS_SVALID;
    562  1.9.2.3  pgoyette 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->charge_step;
    563  1.9.2.3  pgoyette 		}
    564  1.9.2.3  pgoyette 		break;
    565  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
    566  1.9.2.3  pgoyette 		if (battery_present &&
    567  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, flags) == 0 &&
    568  1.9.2.3  pgoyette 		    (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) == 0 &&
    569  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_HI_REG, &hi, flags) == 0 &&
    570  1.9.2.3  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_LO_REG, &lo, flags) == 0) {
    571  1.9.2.3  pgoyette 			e->state = ENVSYS_SVALID;
    572  1.9.2.3  pgoyette 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->discharge_step;
    573  1.9.2.3  pgoyette 		}
    574  1.9.2.3  pgoyette 		break;
    575  1.9.2.5  pgoyette 	case AXP_SENSOR_BATT_MAXIMUM_CAPACITY:
    576  1.9.2.5  pgoyette 		if (battery_present &&
    577  1.9.2.5  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_HI_REG, &hi, flags) == 0 &&
    578  1.9.2.5  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_LO_REG, &lo, flags) == 0) {
    579  1.9.2.5  pgoyette 			e->state = (hi & AXP_BATT_MAX_CAP_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
    580  1.9.2.5  pgoyette 			e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->maxcap_step;
    581  1.9.2.5  pgoyette 		}
    582  1.9.2.5  pgoyette 		break;
    583  1.9.2.5  pgoyette 	case AXP_SENSOR_BATT_CURRENT_CAPACITY:
    584  1.9.2.5  pgoyette 		if (battery_present &&
    585  1.9.2.5  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_HI_REG, &hi, flags) == 0 &&
    586  1.9.2.5  pgoyette 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_LO_REG, &lo, flags) == 0) {
    587  1.9.2.5  pgoyette 			e->state = (hi & AXP_BATT_COULOMB_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
    588  1.9.2.5  pgoyette 			e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->coulomb_step;
    589  1.9.2.5  pgoyette 		}
    590  1.9.2.5  pgoyette 		break;
    591  1.9.2.2  pgoyette 	}
    592  1.9.2.2  pgoyette }
    593  1.9.2.2  pgoyette 
    594  1.9.2.2  pgoyette static void
    595  1.9.2.2  pgoyette axppmic_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
    596  1.9.2.2  pgoyette {
    597  1.9.2.2  pgoyette 	struct axppmic_softc *sc = sme->sme_cookie;
    598  1.9.2.2  pgoyette 	const int flags = I2C_F_POLL;
    599  1.9.2.2  pgoyette 
    600  1.9.2.2  pgoyette 	switch (e->private) {
    601  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_CAPACITY_PERCENT:
    602  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_VOLTAGE:
    603  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_CHARGE_CURRENT:
    604  1.9.2.3  pgoyette 	case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
    605  1.9.2.3  pgoyette 		/* Always update battery capacity and ADCs */
    606  1.9.2.2  pgoyette 		iic_acquire_bus(sc->sc_i2c, flags);
    607  1.9.2.2  pgoyette 		axppmic_sensor_update(sme, e);
    608  1.9.2.2  pgoyette 		iic_release_bus(sc->sc_i2c, flags);
    609  1.9.2.2  pgoyette 		break;
    610  1.9.2.2  pgoyette 	default:
    611  1.9.2.2  pgoyette 		/* Refresh if the sensor is not in valid state */
    612  1.9.2.2  pgoyette 		if (e->state != ENVSYS_SVALID) {
    613  1.9.2.2  pgoyette 			iic_acquire_bus(sc->sc_i2c, flags);
    614  1.9.2.2  pgoyette 			axppmic_sensor_update(sme, e);
    615  1.9.2.2  pgoyette 			iic_release_bus(sc->sc_i2c, flags);
    616  1.9.2.2  pgoyette 		}
    617  1.9.2.2  pgoyette 		break;
    618  1.9.2.2  pgoyette 	}
    619  1.9.2.2  pgoyette }
    620  1.9.2.2  pgoyette 
    621  1.9.2.2  pgoyette static int
    622  1.9.2.2  pgoyette axppmic_intr(void *priv)
    623  1.9.2.2  pgoyette {
    624  1.9.2.2  pgoyette 	struct axppmic_softc *sc = priv;
    625  1.9.2.2  pgoyette 	const struct axppmic_config *c = sc->sc_conf;
    626  1.9.2.2  pgoyette 	const int flags = I2C_F_POLL;
    627  1.9.2.2  pgoyette 	uint8_t stat;
    628  1.9.2.2  pgoyette 	u_int n;
    629  1.9.2.2  pgoyette 
    630  1.9.2.2  pgoyette 	iic_acquire_bus(sc->sc_i2c, flags);
    631  1.9.2.2  pgoyette 	for (n = 1; n <= c->irq_regs; n++) {
    632  1.9.2.2  pgoyette 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_IRQ_STATUS_REG(n), &stat, flags) == 0) {
    633  1.9.2.2  pgoyette 			if (n == c->poklirq.reg && (stat & c->poklirq.mask) != 0)
    634  1.9.2.2  pgoyette 				sysmon_task_queue_sched(0, axppmic_task_shut, sc);
    635  1.9.2.2  pgoyette 			if (n == c->acinirq.reg && (stat & c->acinirq.mask) != 0)
    636  1.9.2.2  pgoyette 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT]);
    637  1.9.2.2  pgoyette 			if (n == c->vbusirq.reg && (stat & c->vbusirq.mask) != 0)
    638  1.9.2.2  pgoyette 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT]);
    639  1.9.2.2  pgoyette 			if (n == c->battirq.reg && (stat & c->battirq.mask) != 0)
    640  1.9.2.2  pgoyette 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT]);
    641  1.9.2.2  pgoyette 			if (n == c->chargeirq.reg && (stat & c->chargeirq.mask) != 0)
    642  1.9.2.2  pgoyette 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING]);
    643  1.9.2.2  pgoyette 			if (n == c->chargestirq.reg && (stat & c->chargestirq.mask) != 0)
    644  1.9.2.2  pgoyette 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE]);
    645  1.9.2.2  pgoyette 
    646  1.9.2.2  pgoyette 			if (stat != 0)
    647  1.9.2.2  pgoyette 				axppmic_write(sc->sc_i2c, sc->sc_addr,
    648  1.9.2.2  pgoyette 				    AXP_IRQ_STATUS_REG(n), stat, flags);
    649  1.9.2.2  pgoyette 		}
    650  1.9.2.2  pgoyette 	}
    651  1.9.2.2  pgoyette 	iic_release_bus(sc->sc_i2c, flags);
    652  1.9.2.2  pgoyette 
    653  1.9.2.2  pgoyette 	return 1;
    654  1.9.2.2  pgoyette }
    655  1.9.2.2  pgoyette 
    656  1.9.2.2  pgoyette static void
    657  1.9.2.2  pgoyette axppmic_attach_acadapter(struct axppmic_softc *sc)
    658  1.9.2.2  pgoyette {
    659  1.9.2.2  pgoyette 	envsys_data_t *e;
    660  1.9.2.2  pgoyette 
    661  1.9.2.2  pgoyette 	e = &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT];
    662  1.9.2.2  pgoyette 	e->private = AXP_SENSOR_ACIN_PRESENT;
    663  1.9.2.2  pgoyette 	e->units = ENVSYS_INDICATOR;
    664  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    665  1.9.2.2  pgoyette 	strlcpy(e->desc, "ACIN present", sizeof(e->desc));
    666  1.9.2.2  pgoyette 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    667  1.9.2.2  pgoyette 
    668  1.9.2.2  pgoyette 	e = &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT];
    669  1.9.2.2  pgoyette 	e->private = AXP_SENSOR_VBUS_PRESENT;
    670  1.9.2.2  pgoyette 	e->units = ENVSYS_INDICATOR;
    671  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    672  1.9.2.2  pgoyette 	strlcpy(e->desc, "VBUS present", sizeof(e->desc));
    673  1.9.2.2  pgoyette 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    674  1.9.2.2  pgoyette }
    675  1.9.2.2  pgoyette 
    676  1.9.2.2  pgoyette static void
    677  1.9.2.2  pgoyette axppmic_attach_battery(struct axppmic_softc *sc)
    678  1.9.2.2  pgoyette {
    679  1.9.2.3  pgoyette 	const struct axppmic_config *c = sc->sc_conf;
    680  1.9.2.2  pgoyette 	envsys_data_t *e;
    681  1.9.2.2  pgoyette 	uint8_t val;
    682  1.9.2.2  pgoyette 
    683  1.9.2.2  pgoyette 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    684  1.9.2.2  pgoyette 	if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_WARN_REG, &val, I2C_F_POLL) == 0) {
    685  1.9.2.2  pgoyette 		sc->sc_warn_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV1) + 5;
    686  1.9.2.2  pgoyette 		sc->sc_shut_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV2);
    687  1.9.2.2  pgoyette 	}
    688  1.9.2.2  pgoyette 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    689  1.9.2.2  pgoyette 
    690  1.9.2.2  pgoyette 	e = &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT];
    691  1.9.2.2  pgoyette 	e->private = AXP_SENSOR_BATT_PRESENT;
    692  1.9.2.2  pgoyette 	e->units = ENVSYS_INDICATOR;
    693  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    694  1.9.2.2  pgoyette 	strlcpy(e->desc, "battery present", sizeof(e->desc));
    695  1.9.2.2  pgoyette 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    696  1.9.2.2  pgoyette 
    697  1.9.2.2  pgoyette 	e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING];
    698  1.9.2.2  pgoyette 	e->private = AXP_SENSOR_BATT_CHARGING;
    699  1.9.2.2  pgoyette 	e->units = ENVSYS_BATTERY_CHARGE;
    700  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    701  1.9.2.2  pgoyette 	strlcpy(e->desc, "charging", sizeof(e->desc));
    702  1.9.2.2  pgoyette 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    703  1.9.2.2  pgoyette 
    704  1.9.2.2  pgoyette 	e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE];
    705  1.9.2.2  pgoyette 	e->private = AXP_SENSOR_BATT_CHARGE_STATE;
    706  1.9.2.2  pgoyette 	e->units = ENVSYS_BATTERY_CAPACITY;
    707  1.9.2.2  pgoyette 	e->flags = ENVSYS_FMONSTCHANGED;
    708  1.9.2.2  pgoyette 	e->state = ENVSYS_SINVALID;
    709  1.9.2.2  pgoyette 	e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
    710  1.9.2.2  pgoyette 	strlcpy(e->desc, "charge state", sizeof(e->desc));
    711  1.9.2.2  pgoyette 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
    712  1.9.2.2  pgoyette 
    713  1.9.2.3  pgoyette 	if (c->batsense_step) {
    714  1.9.2.3  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_VOLTAGE];
    715  1.9.2.3  pgoyette 		e->private = AXP_SENSOR_BATT_VOLTAGE;
    716  1.9.2.3  pgoyette 		e->units = ENVSYS_SVOLTS_DC;
    717  1.9.2.3  pgoyette 		e->state = ENVSYS_SINVALID;
    718  1.9.2.3  pgoyette 		strlcpy(e->desc, "battery voltage", sizeof(e->desc));
    719  1.9.2.3  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    720  1.9.2.3  pgoyette 	}
    721  1.9.2.3  pgoyette 
    722  1.9.2.3  pgoyette 	if (c->charge_step) {
    723  1.9.2.3  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_CURRENT];
    724  1.9.2.3  pgoyette 		e->private = AXP_SENSOR_BATT_CHARGE_CURRENT;
    725  1.9.2.3  pgoyette 		e->units = ENVSYS_SAMPS;
    726  1.9.2.3  pgoyette 		e->state = ENVSYS_SINVALID;
    727  1.9.2.3  pgoyette 		strlcpy(e->desc, "battery charge current", sizeof(e->desc));
    728  1.9.2.3  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    729  1.9.2.3  pgoyette 	}
    730  1.9.2.3  pgoyette 
    731  1.9.2.3  pgoyette 	if (c->discharge_step) {
    732  1.9.2.3  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_DISCHARGE_CURRENT];
    733  1.9.2.3  pgoyette 		e->private = AXP_SENSOR_BATT_DISCHARGE_CURRENT;
    734  1.9.2.3  pgoyette 		e->units = ENVSYS_SAMPS;
    735  1.9.2.3  pgoyette 		e->state = ENVSYS_SINVALID;
    736  1.9.2.3  pgoyette 		strlcpy(e->desc, "battery discharge current", sizeof(e->desc));
    737  1.9.2.3  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    738  1.9.2.3  pgoyette 	}
    739  1.9.2.3  pgoyette 
    740  1.9.2.3  pgoyette 	if (c->has_fuel_gauge) {
    741  1.9.2.3  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CAPACITY_PERCENT];
    742  1.9.2.3  pgoyette 		e->private = AXP_SENSOR_BATT_CAPACITY_PERCENT;
    743  1.9.2.2  pgoyette 		e->units = ENVSYS_INTEGER;
    744  1.9.2.2  pgoyette 		e->state = ENVSYS_SINVALID;
    745  1.9.2.2  pgoyette 		e->flags = ENVSYS_FPERCENT;
    746  1.9.2.2  pgoyette 		strlcpy(e->desc, "battery percent", sizeof(e->desc));
    747  1.9.2.2  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    748  1.9.2.2  pgoyette 	}
    749  1.9.2.5  pgoyette 
    750  1.9.2.5  pgoyette 	if (c->maxcap_step) {
    751  1.9.2.5  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_MAXIMUM_CAPACITY];
    752  1.9.2.5  pgoyette 		e->private = AXP_SENSOR_BATT_MAXIMUM_CAPACITY;
    753  1.9.2.5  pgoyette 		e->units = ENVSYS_SAMPHOUR;
    754  1.9.2.5  pgoyette 		e->state = ENVSYS_SINVALID;
    755  1.9.2.5  pgoyette 		strlcpy(e->desc, "battery maximum capacity", sizeof(e->desc));
    756  1.9.2.5  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    757  1.9.2.5  pgoyette 	}
    758  1.9.2.5  pgoyette 
    759  1.9.2.5  pgoyette 	if (c->coulomb_step) {
    760  1.9.2.5  pgoyette 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CURRENT_CAPACITY];
    761  1.9.2.5  pgoyette 		e->private = AXP_SENSOR_BATT_CURRENT_CAPACITY;
    762  1.9.2.5  pgoyette 		e->units = ENVSYS_SAMPHOUR;
    763  1.9.2.5  pgoyette 		e->state = ENVSYS_SINVALID;
    764  1.9.2.5  pgoyette 		strlcpy(e->desc, "battery current capacity", sizeof(e->desc));
    765  1.9.2.5  pgoyette 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
    766  1.9.2.5  pgoyette 	}
    767  1.9.2.2  pgoyette }
    768  1.9.2.2  pgoyette 
    769  1.9.2.2  pgoyette static void
    770  1.9.2.2  pgoyette axppmic_attach_sensors(struct axppmic_softc *sc)
    771  1.9.2.2  pgoyette {
    772  1.9.2.2  pgoyette 	if (sc->sc_conf->has_battery) {
    773  1.9.2.2  pgoyette 		sc->sc_sme = sysmon_envsys_create();
    774  1.9.2.2  pgoyette 		sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    775  1.9.2.2  pgoyette 		sc->sc_sme->sme_cookie = sc;
    776  1.9.2.2  pgoyette 		sc->sc_sme->sme_refresh = axppmic_sensor_refresh;
    777  1.9.2.2  pgoyette 		sc->sc_sme->sme_class = SME_CLASS_BATTERY;
    778  1.9.2.2  pgoyette 		sc->sc_sme->sme_flags = SME_INIT_REFRESH;
    779  1.9.2.2  pgoyette 
    780  1.9.2.2  pgoyette 		axppmic_attach_acadapter(sc);
    781  1.9.2.2  pgoyette 		axppmic_attach_battery(sc);
    782  1.9.2.2  pgoyette 
    783  1.9.2.2  pgoyette 		sysmon_envsys_register(sc->sc_sme);
    784  1.9.2.2  pgoyette 	}
    785  1.9.2.2  pgoyette }
    786  1.9.2.2  pgoyette 
    787  1.9.2.2  pgoyette 
    788  1.9.2.2  pgoyette static int
    789  1.9.2.2  pgoyette axppmic_match(device_t parent, cfdata_t match, void *aux)
    790  1.9.2.2  pgoyette {
    791  1.9.2.2  pgoyette 	struct i2c_attach_args *ia = aux;
    792  1.9.2.3  pgoyette 	int match_result;
    793  1.9.2.2  pgoyette 
    794  1.9.2.4  pgoyette 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    795  1.9.2.3  pgoyette 		return match_result;
    796  1.9.2.2  pgoyette 
    797  1.9.2.3  pgoyette 	/* This device is direct-config only. */
    798  1.9.2.3  pgoyette 
    799  1.9.2.3  pgoyette 	return 0;
    800  1.9.2.2  pgoyette }
    801  1.9.2.2  pgoyette 
    802  1.9.2.2  pgoyette static void
    803  1.9.2.2  pgoyette axppmic_attach(device_t parent, device_t self, void *aux)
    804  1.9.2.2  pgoyette {
    805  1.9.2.2  pgoyette 	struct axppmic_softc *sc = device_private(self);
    806  1.9.2.4  pgoyette 	const struct device_compatible_entry *dce = NULL;
    807  1.9.2.2  pgoyette 	const struct axppmic_config *c;
    808  1.9.2.2  pgoyette 	struct axpreg_attach_args aaa;
    809  1.9.2.2  pgoyette 	struct i2c_attach_args *ia = aux;
    810  1.9.2.2  pgoyette 	int phandle, child, i;
    811  1.9.2.2  pgoyette 	uint32_t irq_mask;
    812  1.9.2.2  pgoyette 	void *ih;
    813  1.9.2.2  pgoyette 
    814  1.9.2.4  pgoyette 	(void) iic_compatible_match(ia, compat_data, &dce);
    815  1.9.2.3  pgoyette 	KASSERT(dce != NULL);
    816  1.9.2.4  pgoyette 	c = (void *)dce->data;
    817  1.9.2.2  pgoyette 
    818  1.9.2.2  pgoyette 	sc->sc_dev = self;
    819  1.9.2.2  pgoyette 	sc->sc_i2c = ia->ia_tag;
    820  1.9.2.2  pgoyette 	sc->sc_addr = ia->ia_addr;
    821  1.9.2.2  pgoyette 	sc->sc_phandle = ia->ia_cookie;
    822  1.9.2.2  pgoyette 	sc->sc_conf = c;
    823  1.9.2.2  pgoyette 
    824  1.9.2.2  pgoyette 	aprint_naive("\n");
    825  1.9.2.2  pgoyette 	aprint_normal(": %s\n", c->name);
    826  1.9.2.2  pgoyette 
    827  1.9.2.2  pgoyette 	sc->sc_smpsw.smpsw_name = device_xname(self);
    828  1.9.2.2  pgoyette 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
    829  1.9.2.2  pgoyette 	sysmon_pswitch_register(&sc->sc_smpsw);
    830  1.9.2.2  pgoyette 
    831  1.9.2.2  pgoyette 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    832  1.9.2.2  pgoyette 	for (i = 1; i <= c->irq_regs; i++) {
    833  1.9.2.2  pgoyette 		irq_mask = 0;
    834  1.9.2.2  pgoyette 		if (i == c->poklirq.reg)
    835  1.9.2.2  pgoyette 			irq_mask |= c->poklirq.mask;
    836  1.9.2.2  pgoyette 		if (i == c->acinirq.reg)
    837  1.9.2.2  pgoyette 			irq_mask |= c->acinirq.mask;
    838  1.9.2.2  pgoyette 		if (i == c->vbusirq.reg)
    839  1.9.2.2  pgoyette 			irq_mask |= c->vbusirq.mask;
    840  1.9.2.2  pgoyette 		if (i == c->battirq.reg)
    841  1.9.2.2  pgoyette 			irq_mask |= c->battirq.mask;
    842  1.9.2.2  pgoyette 		if (i == c->chargeirq.reg)
    843  1.9.2.2  pgoyette 			irq_mask |= c->chargeirq.mask;
    844  1.9.2.2  pgoyette 		if (i == c->chargestirq.reg)
    845  1.9.2.2  pgoyette 			irq_mask |= c->chargestirq.mask;
    846  1.9.2.2  pgoyette 		axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_IRQ_ENABLE_REG(i), irq_mask, I2C_F_POLL);
    847  1.9.2.2  pgoyette 	}
    848  1.9.2.2  pgoyette 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    849  1.9.2.2  pgoyette 
    850  1.9.2.2  pgoyette 	ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    851  1.9.2.2  pgoyette 	    axppmic_intr, sc);
    852  1.9.2.2  pgoyette 	if (ih == NULL) {
    853  1.9.2.2  pgoyette 		aprint_error_dev(self, "WARNING: couldn't establish interrupt handler\n");
    854  1.9.2.2  pgoyette 	}
    855  1.9.2.2  pgoyette 
    856  1.9.2.2  pgoyette 	fdtbus_register_power_controller(sc->sc_dev, sc->sc_phandle,
    857  1.9.2.2  pgoyette 	    &axppmic_power_funcs);
    858  1.9.2.2  pgoyette 
    859  1.9.2.2  pgoyette 	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    860  1.9.2.2  pgoyette 	if (phandle > 0) {
    861  1.9.2.2  pgoyette 		aaa.reg_i2c = sc->sc_i2c;
    862  1.9.2.2  pgoyette 		aaa.reg_addr = sc->sc_addr;
    863  1.9.2.2  pgoyette 		for (i = 0; i < c->ncontrols; i++) {
    864  1.9.2.2  pgoyette 			const struct axppmic_ctrl *ctrl = &c->controls[i];
    865  1.9.2.2  pgoyette 			child = of_find_firstchild_byname(phandle, ctrl->c_name);
    866  1.9.2.2  pgoyette 			if (child <= 0)
    867  1.9.2.2  pgoyette 				continue;
    868  1.9.2.2  pgoyette 			aaa.reg_ctrl = ctrl;
    869  1.9.2.2  pgoyette 			aaa.reg_phandle = child;
    870  1.9.2.2  pgoyette 			config_found(sc->sc_dev, &aaa, NULL);
    871  1.9.2.2  pgoyette 		}
    872  1.9.2.2  pgoyette 	}
    873  1.9.2.2  pgoyette 
    874  1.9.2.2  pgoyette 	if (c->has_battery)
    875  1.9.2.2  pgoyette 		axppmic_attach_sensors(sc);
    876  1.9.2.2  pgoyette }
    877  1.9.2.2  pgoyette 
    878  1.9.2.2  pgoyette static int
    879  1.9.2.2  pgoyette axpreg_acquire(device_t dev)
    880  1.9.2.2  pgoyette {
    881  1.9.2.2  pgoyette 	return 0;
    882  1.9.2.2  pgoyette }
    883  1.9.2.2  pgoyette 
    884  1.9.2.2  pgoyette static void
    885  1.9.2.2  pgoyette axpreg_release(device_t dev)
    886  1.9.2.2  pgoyette {
    887  1.9.2.2  pgoyette }
    888  1.9.2.2  pgoyette 
    889  1.9.2.2  pgoyette static int
    890  1.9.2.2  pgoyette axpreg_enable(device_t dev, bool enable)
    891  1.9.2.2  pgoyette {
    892  1.9.2.2  pgoyette 	struct axpreg_softc *sc = device_private(dev);
    893  1.9.2.2  pgoyette 	const struct axppmic_ctrl *c = sc->sc_ctrl;
    894  1.9.2.2  pgoyette 	const int flags = (cold ? I2C_F_POLL : 0);
    895  1.9.2.2  pgoyette 	uint8_t val;
    896  1.9.2.2  pgoyette 	int error;
    897  1.9.2.2  pgoyette 
    898  1.9.2.2  pgoyette 	if (!c->c_enable_mask)
    899  1.9.2.2  pgoyette 		return EINVAL;
    900  1.9.2.2  pgoyette 
    901  1.9.2.2  pgoyette 	iic_acquire_bus(sc->sc_i2c, flags);
    902  1.9.2.2  pgoyette 	if ((error = axppmic_read(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, &val, flags)) == 0) {
    903  1.9.2.2  pgoyette 		if (enable)
    904  1.9.2.2  pgoyette 			val |= c->c_enable_mask;
    905  1.9.2.2  pgoyette 		else
    906  1.9.2.2  pgoyette 			val &= ~c->c_enable_mask;
    907  1.9.2.2  pgoyette 		error = axppmic_write(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, val, flags);
    908  1.9.2.2  pgoyette 	}
    909  1.9.2.2  pgoyette 	iic_release_bus(sc->sc_i2c, flags);
    910  1.9.2.2  pgoyette 
    911  1.9.2.2  pgoyette 	return error;
    912  1.9.2.2  pgoyette }
    913  1.9.2.2  pgoyette 
    914  1.9.2.2  pgoyette static int
    915  1.9.2.2  pgoyette axpreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    916  1.9.2.2  pgoyette {
    917  1.9.2.2  pgoyette 	struct axpreg_softc *sc = device_private(dev);
    918  1.9.2.2  pgoyette 	const struct axppmic_ctrl *c = sc->sc_ctrl;
    919  1.9.2.2  pgoyette 
    920  1.9.2.2  pgoyette 	return axppmic_set_voltage(sc->sc_i2c, sc->sc_addr, c,
    921  1.9.2.2  pgoyette 	    min_uvol / 1000, max_uvol / 1000);
    922  1.9.2.2  pgoyette }
    923  1.9.2.2  pgoyette 
    924  1.9.2.2  pgoyette static int
    925  1.9.2.2  pgoyette axpreg_get_voltage(device_t dev, u_int *puvol)
    926  1.9.2.2  pgoyette {
    927  1.9.2.2  pgoyette 	struct axpreg_softc *sc = device_private(dev);
    928  1.9.2.2  pgoyette 	const struct axppmic_ctrl *c = sc->sc_ctrl;
    929  1.9.2.2  pgoyette 	int error;
    930  1.9.2.2  pgoyette 	u_int vol;
    931  1.9.2.2  pgoyette 
    932  1.9.2.2  pgoyette 	error = axppmic_get_voltage(sc->sc_i2c, sc->sc_addr, c, &vol);
    933  1.9.2.2  pgoyette 	if (error)
    934  1.9.2.2  pgoyette 		return error;
    935  1.9.2.2  pgoyette 
    936  1.9.2.2  pgoyette 	*puvol = vol * 1000;
    937  1.9.2.2  pgoyette 	return 0;
    938  1.9.2.2  pgoyette }
    939  1.9.2.2  pgoyette 
    940  1.9.2.2  pgoyette static struct fdtbus_regulator_controller_func axpreg_funcs = {
    941  1.9.2.2  pgoyette 	.acquire = axpreg_acquire,
    942  1.9.2.2  pgoyette 	.release = axpreg_release,
    943  1.9.2.2  pgoyette 	.enable = axpreg_enable,
    944  1.9.2.2  pgoyette 	.set_voltage = axpreg_set_voltage,
    945  1.9.2.2  pgoyette 	.get_voltage = axpreg_get_voltage,
    946  1.9.2.2  pgoyette };
    947  1.9.2.2  pgoyette 
    948  1.9.2.2  pgoyette static int
    949  1.9.2.2  pgoyette axpreg_match(device_t parent, cfdata_t match, void *aux)
    950  1.9.2.2  pgoyette {
    951  1.9.2.2  pgoyette 	return 1;
    952  1.9.2.2  pgoyette }
    953  1.9.2.2  pgoyette 
    954  1.9.2.2  pgoyette static void
    955  1.9.2.2  pgoyette axpreg_attach(device_t parent, device_t self, void *aux)
    956  1.9.2.2  pgoyette {
    957  1.9.2.2  pgoyette 	struct axpreg_softc *sc = device_private(self);
    958  1.9.2.2  pgoyette 	struct axpreg_attach_args *aaa = aux;
    959  1.9.2.2  pgoyette 	const int phandle = aaa->reg_phandle;
    960  1.9.2.2  pgoyette 	const char *name;
    961  1.9.2.2  pgoyette 
    962  1.9.2.2  pgoyette 	sc->sc_dev = self;
    963  1.9.2.2  pgoyette 	sc->sc_i2c = aaa->reg_i2c;
    964  1.9.2.2  pgoyette 	sc->sc_addr = aaa->reg_addr;
    965  1.9.2.2  pgoyette 	sc->sc_ctrl = aaa->reg_ctrl;
    966  1.9.2.2  pgoyette 
    967  1.9.2.2  pgoyette 	fdtbus_register_regulator_controller(self, phandle,
    968  1.9.2.2  pgoyette 	    &axpreg_funcs);
    969  1.9.2.2  pgoyette 
    970  1.9.2.2  pgoyette 	aprint_naive("\n");
    971  1.9.2.2  pgoyette 	name = fdtbus_get_string(phandle, "regulator-name");
    972  1.9.2.2  pgoyette 	if (name)
    973  1.9.2.2  pgoyette 		aprint_normal(": %s\n", name);
    974  1.9.2.2  pgoyette 	else
    975  1.9.2.2  pgoyette 		aprint_normal("\n");
    976  1.9.2.2  pgoyette }
    977  1.9.2.2  pgoyette 
    978  1.9.2.2  pgoyette CFATTACH_DECL_NEW(axppmic, sizeof(struct axppmic_softc),
    979  1.9.2.2  pgoyette     axppmic_match, axppmic_attach, NULL, NULL);
    980  1.9.2.2  pgoyette 
    981  1.9.2.2  pgoyette CFATTACH_DECL_NEW(axpreg, sizeof(struct axpreg_softc),
    982  1.9.2.2  pgoyette     axpreg_match, axpreg_attach, NULL, NULL);
    983