Home | History | Annotate | Line # | Download | only in i2c
      1  1.42   thorpej /* $NetBSD: axppmic.c,v 1.42 2025/09/17 13:42:42 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2014-2018 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.42   thorpej __KERNEL_RCSID(0, "$NetBSD: axppmic.c,v 1.42 2025/09/17 13:42:42 thorpej Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/systm.h>
     34   1.1  jmcneill #include <sys/kernel.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/conf.h>
     37   1.1  jmcneill #include <sys/bus.h>
     38   1.1  jmcneill #include <sys/kmem.h>
     39  1.29   thorpej #include <sys/workqueue.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/i2c/i2cvar.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     44   1.1  jmcneill #include <dev/sysmon/sysmon_taskq.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47   1.1  jmcneill 
     48   1.3  jmcneill #define	AXP_POWER_SOURCE_REG	0x00
     49   1.3  jmcneill #define	 AXP_POWER_SOURCE_ACIN_PRESENT	__BIT(7)
     50   1.3  jmcneill #define	 AXP_POWER_SOURCE_VBUS_PRESENT	__BIT(5)
     51  1.10  jmcneill #define	 AXP_POWER_SOURCE_CHARGE_DIRECTION __BIT(2)
     52   1.3  jmcneill 
     53   1.2  jmcneill #define	AXP_POWER_MODE_REG	0x01
     54   1.2  jmcneill #define	 AXP_POWER_MODE_BATT_VALID	__BIT(4)
     55   1.2  jmcneill #define	 AXP_POWER_MODE_BATT_PRESENT	__BIT(5)
     56   1.2  jmcneill #define	 AXP_POWER_MODE_BATT_CHARGING	__BIT(6)
     57   1.2  jmcneill 
     58  1.19  jmcneill #define	AXP_CHIP_ID_REG		0x03
     59  1.19  jmcneill 
     60   1.1  jmcneill #define AXP_POWER_DISABLE_REG	0x32
     61   1.1  jmcneill #define	 AXP_POWER_DISABLE_CTRL	__BIT(7)
     62   1.1  jmcneill 
     63   1.1  jmcneill #define AXP_IRQ_ENABLE_REG(n)	(0x40 + (n) - 1)
     64   1.5  jmcneill #define	 AXP_IRQ1_ACIN_RAISE	__BIT(6)
     65   1.5  jmcneill #define	 AXP_IRQ1_ACIN_LOWER	__BIT(5)
     66   1.5  jmcneill #define	 AXP_IRQ1_VBUS_RAISE	__BIT(3)
     67   1.5  jmcneill #define	 AXP_IRQ1_VBUS_LOWER	__BIT(2)
     68   1.1  jmcneill #define AXP_IRQ_STATUS_REG(n)	(0x48 + (n) - 1)
     69   1.1  jmcneill 
     70  1.10  jmcneill #define	AXP_BATSENSE_HI_REG	0x78
     71  1.10  jmcneill #define	AXP_BATSENSE_LO_REG	0x79
     72  1.10  jmcneill 
     73  1.10  jmcneill #define	AXP_BATTCHG_HI_REG	0x7a
     74  1.10  jmcneill #define	AXP_BATTCHG_LO_REG	0x7b
     75  1.10  jmcneill 
     76  1.10  jmcneill #define	AXP_BATTDISCHG_HI_REG	0x7c
     77  1.10  jmcneill #define	AXP_BATTDISCHG_LO_REG	0x7d
     78  1.10  jmcneill 
     79  1.10  jmcneill #define	AXP_ADC_RAW(_hi, _lo)	\
     80  1.15  jakllsch 	(((u_int)(_hi) << 4) | ((_lo) & 0xf))
     81  1.10  jmcneill 
     82  1.37  jmcneill #define	AXP_GPIO_CTRL_REG(pin)	(0x90 + (pin) * 2)
     83  1.37  jmcneill #define	 AXP_GPIO_CTRL_FUNC_MASK 	__BITS(2,0)
     84  1.37  jmcneill #define	 AXP_GPIO_CTRL_FUNC_LOW	 	0
     85  1.37  jmcneill #define	 AXP_GPIO_CTRL_FUNC_HIGH	1
     86  1.37  jmcneill #define	 AXP_GPIO_CTRL_FUNC_INPUT	2
     87  1.37  jmcneill #define	AXP_GPIO_SIGNAL_REG	0x94
     88  1.37  jmcneill 
     89   1.2  jmcneill #define	AXP_FUEL_GAUGE_CTRL_REG	0xb8
     90   1.2  jmcneill #define	 AXP_FUEL_GAUGE_CTRL_EN	__BIT(7)
     91  1.10  jmcneill 
     92   1.2  jmcneill #define	AXP_BATT_CAP_REG	0xb9
     93   1.2  jmcneill #define	 AXP_BATT_CAP_VALID	__BIT(7)
     94   1.2  jmcneill #define	 AXP_BATT_CAP_PERCENT	__BITS(6,0)
     95   1.2  jmcneill 
     96  1.16  jakllsch #define	AXP_BATT_MAX_CAP_HI_REG	0xe0
     97  1.16  jakllsch #define	 AXP_BATT_MAX_CAP_VALID	__BIT(7)
     98  1.16  jakllsch #define	AXP_BATT_MAX_CAP_LO_REG	0xe1
     99  1.16  jakllsch 
    100  1.16  jakllsch #define	AXP_BATT_COULOMB_HI_REG	0xe2
    101  1.16  jakllsch #define	 AXP_BATT_COULOMB_VALID	__BIT(7)
    102  1.16  jakllsch #define	AXP_BATT_COULOMB_LO_REG	0xe3
    103  1.16  jakllsch 
    104  1.16  jakllsch #define	AXP_COULOMB_RAW(_hi, _lo)	\
    105  1.16  jakllsch 	(((u_int)(_hi & ~__BIT(7)) << 8) | (_lo))
    106  1.16  jakllsch 
    107   1.2  jmcneill #define	AXP_BATT_CAP_WARN_REG	0xe6
    108   1.2  jmcneill #define	 AXP_BATT_CAP_WARN_LV1	__BITS(7,4)
    109   1.2  jmcneill #define	 AXP_BATT_CAP_WARN_LV2	__BITS(3,0)
    110   1.2  jmcneill 
    111  1.19  jmcneill #define	AXP_ADDR_EXT_REG	0xff	/* AXP806 */
    112  1.19  jmcneill #define	 AXP_ADDR_EXT_MASTER	0
    113  1.19  jmcneill #define	 AXP_ADDR_EXT_SLAVE	__BIT(4)
    114  1.19  jmcneill 
    115   1.1  jmcneill struct axppmic_ctrl {
    116   1.1  jmcneill 	device_t	c_dev;
    117   1.1  jmcneill 
    118   1.1  jmcneill 	const char *	c_name;
    119   1.1  jmcneill 	u_int		c_min;
    120   1.1  jmcneill 	u_int		c_max;
    121   1.1  jmcneill 	u_int		c_step1;
    122   1.1  jmcneill 	u_int		c_step1cnt;
    123   1.1  jmcneill 	u_int		c_step2;
    124   1.1  jmcneill 	u_int		c_step2cnt;
    125  1.24  jmcneill 	u_int		c_step2start;
    126   1.1  jmcneill 
    127   1.1  jmcneill 	uint8_t		c_enable_reg;
    128   1.1  jmcneill 	uint8_t		c_enable_mask;
    129  1.23  jmcneill 	uint8_t		c_enable_val;
    130  1.23  jmcneill 	uint8_t		c_disable_val;
    131   1.1  jmcneill 
    132   1.1  jmcneill 	uint8_t		c_voltage_reg;
    133   1.1  jmcneill 	uint8_t		c_voltage_mask;
    134   1.1  jmcneill };
    135   1.1  jmcneill 
    136   1.1  jmcneill #define AXP_CTRL(name, min, max, step, ereg, emask, vreg, vmask)	\
    137   1.1  jmcneill 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    138   1.1  jmcneill 	  .c_step1 = (step), .c_step1cnt = (((max) - (min)) / (step)) + 1, \
    139   1.1  jmcneill 	  .c_step2 = 0, .c_step2cnt = 0,				\
    140   1.1  jmcneill 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    141  1.23  jmcneill 	  .c_enable_val = (emask), .c_disable_val = 0,			\
    142   1.1  jmcneill 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    143   1.1  jmcneill 
    144   1.1  jmcneill #define AXP_CTRL2(name, min, max, step1, step1cnt, step2, step2cnt, ereg, emask, vreg, vmask) \
    145   1.1  jmcneill 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    146   1.1  jmcneill 	  .c_step1 = (step1), .c_step1cnt = (step1cnt),			\
    147   1.1  jmcneill 	  .c_step2 = (step2), .c_step2cnt = (step2cnt),			\
    148   1.1  jmcneill 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    149  1.23  jmcneill 	  .c_enable_val = (emask), .c_disable_val = 0,			\
    150   1.1  jmcneill 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    151   1.1  jmcneill 
    152  1.24  jmcneill #define AXP_CTRL2_RANGE(name, min, max, step1, step1cnt, step2start, step2, step2cnt, ereg, emask, vreg, vmask) \
    153  1.24  jmcneill 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    154  1.24  jmcneill 	  .c_step1 = (step1), .c_step1cnt = (step1cnt),			\
    155  1.24  jmcneill 	  .c_step2start = (step2start),					\
    156  1.24  jmcneill 	  .c_step2 = (step2), .c_step2cnt = (step2cnt),			\
    157  1.24  jmcneill 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    158  1.24  jmcneill 	  .c_enable_val = (emask), .c_disable_val = 0,			\
    159  1.24  jmcneill 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    160  1.24  jmcneill 
    161  1.23  jmcneill #define AXP_CTRL_IO(name, min, max, step, ereg, emask, eval, dval, vreg, vmask)	\
    162  1.23  jmcneill 	{ .c_name = (name), .c_min = (min), .c_max = (max),		\
    163  1.23  jmcneill 	  .c_step1 = (step), .c_step1cnt = (((max) - (min)) / (step)) + 1, \
    164  1.23  jmcneill 	  .c_step2 = 0, .c_step2cnt = 0,				\
    165  1.23  jmcneill 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    166  1.23  jmcneill 	  .c_enable_val = (eval), .c_disable_val = (dval),		\
    167  1.23  jmcneill 	  .c_voltage_reg = (vreg), .c_voltage_mask = (vmask) }
    168  1.23  jmcneill 
    169  1.24  jmcneill #define AXP_CTRL_SW(name, ereg, emask)					\
    170  1.24  jmcneill 	{ .c_name = (name), 						\
    171  1.24  jmcneill 	  .c_enable_reg = (ereg), .c_enable_mask = (emask),		\
    172  1.24  jmcneill 	  .c_enable_val = (emask), .c_disable_val = 0 }
    173  1.23  jmcneill 
    174   1.1  jmcneill static const struct axppmic_ctrl axp803_ctrls[] = {
    175   1.1  jmcneill 	AXP_CTRL("dldo1", 700, 3300, 100,
    176   1.1  jmcneill 		0x12, __BIT(3), 0x15, __BITS(4,0)),
    177   1.1  jmcneill 	AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
    178   1.1  jmcneill 		0x12, __BIT(4), 0x16, __BITS(4,0)),
    179   1.1  jmcneill 	AXP_CTRL("dldo3", 700, 3300, 100,
    180   1.1  jmcneill 	 	0x12, __BIT(5), 0x17, __BITS(4,0)),
    181   1.1  jmcneill 	AXP_CTRL("dldo4", 700, 3300, 100,
    182   1.1  jmcneill 		0x12, __BIT(6), 0x18, __BITS(4,0)),
    183   1.1  jmcneill 	AXP_CTRL("eldo1", 700, 1900, 50,
    184   1.1  jmcneill 		0x12, __BIT(0), 0x19, __BITS(4,0)),
    185   1.1  jmcneill 	AXP_CTRL("eldo2", 700, 1900, 50,
    186   1.1  jmcneill 		0x12, __BIT(1), 0x1a, __BITS(4,0)),
    187   1.1  jmcneill 	AXP_CTRL("eldo3", 700, 1900, 50,
    188   1.1  jmcneill 		0x12, __BIT(2), 0x1b, __BITS(4,0)),
    189   1.1  jmcneill 	AXP_CTRL("fldo1", 700, 1450, 50,
    190   1.1  jmcneill 		0x13, __BIT(2), 0x1c, __BITS(3,0)),
    191   1.1  jmcneill 	AXP_CTRL("fldo2", 700, 1450, 50,
    192   1.1  jmcneill 		0x13, __BIT(3), 0x1d, __BITS(3,0)),
    193   1.1  jmcneill 	AXP_CTRL("dcdc1", 1600, 3400, 100,
    194   1.1  jmcneill 		0x10, __BIT(0), 0x20, __BITS(4,0)),
    195   1.6  jmcneill 	AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
    196   1.1  jmcneill 		0x10, __BIT(1), 0x21, __BITS(6,0)),
    197   1.6  jmcneill 	AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
    198   1.1  jmcneill 		0x10, __BIT(2), 0x22, __BITS(6,0)),
    199   1.6  jmcneill 	AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
    200   1.1  jmcneill 		0x10, __BIT(3), 0x23, __BITS(6,0)),
    201   1.1  jmcneill 	AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
    202   1.1  jmcneill 		0x10, __BIT(4), 0x24, __BITS(6,0)),
    203   1.1  jmcneill 	AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
    204   1.1  jmcneill 		0x10, __BIT(5), 0x25, __BITS(6,0)),
    205   1.1  jmcneill 	AXP_CTRL("aldo1", 700, 3300, 100,
    206   1.1  jmcneill 		0x13, __BIT(5), 0x28, __BITS(4,0)),
    207   1.1  jmcneill 	AXP_CTRL("aldo2", 700, 3300, 100,
    208   1.1  jmcneill 		0x13, __BIT(6), 0x29, __BITS(4,0)),
    209   1.1  jmcneill 	AXP_CTRL("aldo3", 700, 3300, 100,
    210   1.1  jmcneill 		0x13, __BIT(7), 0x2a, __BITS(4,0)),
    211   1.1  jmcneill };
    212   1.1  jmcneill 
    213   1.1  jmcneill static const struct axppmic_ctrl axp805_ctrls[] = {
    214   1.1  jmcneill 	AXP_CTRL2("dcdca", 600, 1520, 10, 51, 20, 21,
    215   1.1  jmcneill 		0x10, __BIT(0), 0x12, __BITS(6,0)),
    216   1.1  jmcneill 	AXP_CTRL("dcdcb", 1000, 2550, 50,
    217   1.1  jmcneill 		0x10, __BIT(1), 0x13, __BITS(4,0)),
    218   1.1  jmcneill 	AXP_CTRL2("dcdcc", 600, 1520, 10, 51, 20, 21,
    219   1.1  jmcneill 		0x10, __BIT(2), 0x14, __BITS(6,0)),
    220   1.1  jmcneill 	AXP_CTRL2("dcdcd", 600, 3300, 20, 46, 100, 18,
    221   1.1  jmcneill 		0x10, __BIT(3), 0x15, __BITS(5,0)),
    222   1.1  jmcneill 	AXP_CTRL("dcdce", 1100, 3400, 100,
    223   1.1  jmcneill 		0x10, __BIT(4), 0x16, __BITS(4,0)),
    224   1.1  jmcneill 	AXP_CTRL("aldo1", 700, 3300, 100,
    225   1.1  jmcneill 		0x10, __BIT(5), 0x17, __BITS(4,0)),
    226   1.1  jmcneill 	AXP_CTRL("aldo2", 700, 3400, 100,
    227   1.1  jmcneill 		0x10, __BIT(6), 0x18, __BITS(4,0)),
    228   1.1  jmcneill 	AXP_CTRL("aldo3", 700, 3300, 100,
    229   1.1  jmcneill 		0x10, __BIT(7), 0x19, __BITS(4,0)),
    230   1.1  jmcneill 	AXP_CTRL("bldo1", 700, 1900, 100,
    231   1.1  jmcneill 		0x11, __BIT(0), 0x20, __BITS(3,0)),
    232   1.1  jmcneill 	AXP_CTRL("bldo2", 700, 1900, 100,
    233   1.1  jmcneill 		0x11, __BIT(1), 0x21, __BITS(3,0)),
    234   1.1  jmcneill 	AXP_CTRL("bldo3", 700, 1900, 100,
    235   1.1  jmcneill 		0x11, __BIT(2), 0x22, __BITS(3,0)),
    236   1.1  jmcneill 	AXP_CTRL("bldo4", 700, 1900, 100,
    237   1.1  jmcneill 		0x11, __BIT(3), 0x23, __BITS(3,0)),
    238  1.38     skrll 	AXP_CTRL("cldo1", 700, 3300, 100,
    239   1.1  jmcneill 		0x11, __BIT(4), 0x24, __BITS(4,0)),
    240   1.1  jmcneill 	AXP_CTRL2("cldo2", 700, 4200, 100, 28, 200, 4,
    241   1.1  jmcneill 		0x11, __BIT(5), 0x25, __BITS(4,0)),
    242  1.38     skrll 	AXP_CTRL("cldo3", 700, 3300, 100,
    243   1.1  jmcneill 		0x11, __BIT(6), 0x26, __BITS(4,0)),
    244   1.1  jmcneill };
    245   1.1  jmcneill 
    246  1.21  jmcneill static const struct axppmic_ctrl axp809_ctrls[] = {
    247  1.24  jmcneill 	AXP_CTRL("dc5ldo", 700, 1400, 100,
    248  1.24  jmcneill 		0x10, __BIT(0), 0x1c, __BITS(2,0)),
    249  1.24  jmcneill 	AXP_CTRL("dcdc1", 1600, 3400, 100,
    250  1.24  jmcneill 		0x10, __BIT(1), 0x21, __BITS(4,0)),
    251  1.24  jmcneill 	AXP_CTRL("dcdc2", 600, 1540, 20,
    252  1.24  jmcneill 		0x10, __BIT(2), 0x22, __BITS(5,0)),
    253  1.24  jmcneill 	AXP_CTRL("dcdc3", 600, 1860, 20,
    254  1.24  jmcneill 		0x10, __BIT(3), 0x23, __BITS(5,0)),
    255  1.24  jmcneill 	AXP_CTRL2_RANGE("dcdc4", 600, 2600, 20, 47, 1800, 100, 9,
    256  1.24  jmcneill 		0x10, __BIT(4), 0x24, __BITS(5,0)),
    257  1.24  jmcneill 	AXP_CTRL("dcdc5", 1000, 2550, 50,
    258  1.24  jmcneill 		0x10, __BIT(5), 0x25, __BITS(4,0)),
    259  1.24  jmcneill 	AXP_CTRL("aldo1", 700, 3300, 100,
    260  1.24  jmcneill 		0x10, __BIT(6), 0x28, __BITS(4,0)),
    261  1.24  jmcneill 	AXP_CTRL("aldo2", 700, 3300, 100,
    262  1.24  jmcneill 		0x10, __BIT(7), 0x29, __BITS(4,0)),
    263  1.24  jmcneill 	AXP_CTRL("eldo1", 700, 3300, 100,
    264  1.24  jmcneill 		0x12, __BIT(0), 0x19, __BITS(4,0)),
    265  1.24  jmcneill 	AXP_CTRL("eldo2", 700, 3300, 100,
    266  1.24  jmcneill 		0x12, __BIT(1), 0x1a, __BITS(4,0)),
    267  1.24  jmcneill 	AXP_CTRL("eldo3", 700, 3300, 100,
    268  1.24  jmcneill 		0x12, __BIT(2), 0x1b, __BITS(4,0)),
    269  1.24  jmcneill 	AXP_CTRL2_RANGE("dldo1", 700, 4000, 100, 26, 3400, 200, 4,
    270  1.24  jmcneill 		0x12, __BIT(3), 0x15, __BITS(4,0)),
    271  1.24  jmcneill 	AXP_CTRL("dldo2", 700, 3300, 100,
    272  1.24  jmcneill 		0x12, __BIT(4), 0x16, __BITS(4,0)),
    273  1.24  jmcneill 	AXP_CTRL("aldo3", 700, 3300, 100,
    274  1.24  jmcneill 		0x12, __BIT(5), 0x2a, __BITS(4,0)),
    275  1.24  jmcneill 	AXP_CTRL_SW("sw",
    276  1.24  jmcneill 		0x12, __BIT(6)),
    277  1.24  jmcneill 	/* dc1sw is another switch for dcdc1 */
    278  1.24  jmcneill 	AXP_CTRL("dc1sw", 1600, 3400, 100,
    279  1.24  jmcneill 		0x12, __BIT(7), 0x21, __BITS(4,0)),
    280  1.23  jmcneill 	AXP_CTRL_IO("ldo_io0", 700, 3300, 100,
    281  1.23  jmcneill 		0x90, __BITS(3,0), 0x3, 0x7, 0x91, __BITS(4,0)),
    282  1.23  jmcneill 	AXP_CTRL_IO("ldo_io1", 700, 3300, 100,
    283  1.23  jmcneill 		0x92, __BITS(3,0), 0x3, 0x7, 0x93, __BITS(4,0)),
    284  1.21  jmcneill };
    285  1.21  jmcneill 
    286  1.17  jmcneill static const struct axppmic_ctrl axp813_ctrls[] = {
    287  1.17  jmcneill 	AXP_CTRL("dldo1", 700, 3300, 100,
    288  1.17  jmcneill 		0x12, __BIT(3), 0x15, __BITS(4,0)),
    289  1.17  jmcneill 	AXP_CTRL2("dldo2", 700, 4200, 100, 28, 200, 4,
    290  1.17  jmcneill 		0x12, __BIT(4), 0x16, __BITS(4,0)),
    291  1.17  jmcneill 	AXP_CTRL("dldo3", 700, 3300, 100,
    292  1.17  jmcneill 	 	0x12, __BIT(5), 0x17, __BITS(4,0)),
    293  1.17  jmcneill 	AXP_CTRL("dldo4", 700, 3300, 100,
    294  1.17  jmcneill 		0x12, __BIT(6), 0x18, __BITS(4,0)),
    295  1.17  jmcneill 	AXP_CTRL("eldo1", 700, 1900, 50,
    296  1.17  jmcneill 		0x12, __BIT(0), 0x19, __BITS(4,0)),
    297  1.17  jmcneill 	AXP_CTRL("eldo2", 700, 1900, 50,
    298  1.17  jmcneill 		0x12, __BIT(1), 0x1a, __BITS(4,0)),
    299  1.17  jmcneill 	AXP_CTRL("eldo3", 700, 1900, 50,
    300  1.17  jmcneill 		0x12, __BIT(2), 0x1b, __BITS(4,0)),
    301  1.17  jmcneill 	AXP_CTRL("fldo1", 700, 1450, 50,
    302  1.17  jmcneill 		0x13, __BIT(2), 0x1c, __BITS(3,0)),
    303  1.17  jmcneill 	AXP_CTRL("fldo2", 700, 1450, 50,
    304  1.17  jmcneill 		0x13, __BIT(3), 0x1d, __BITS(3,0)),
    305  1.17  jmcneill 	AXP_CTRL("dcdc1", 1600, 3400, 100,
    306  1.17  jmcneill 		0x10, __BIT(0), 0x20, __BITS(4,0)),
    307  1.17  jmcneill 	AXP_CTRL2("dcdc2", 500, 1300, 10, 70, 20, 5,
    308  1.17  jmcneill 		0x10, __BIT(1), 0x21, __BITS(6,0)),
    309  1.17  jmcneill 	AXP_CTRL2("dcdc3", 500, 1300, 10, 70, 20, 5,
    310  1.17  jmcneill 		0x10, __BIT(2), 0x22, __BITS(6,0)),
    311  1.17  jmcneill 	AXP_CTRL2("dcdc4", 500, 1300, 10, 70, 20, 5,
    312  1.17  jmcneill 		0x10, __BIT(3), 0x23, __BITS(6,0)),
    313  1.17  jmcneill 	AXP_CTRL2("dcdc5", 800, 1840, 10, 33, 20, 36,
    314  1.17  jmcneill 		0x10, __BIT(4), 0x24, __BITS(6,0)),
    315  1.17  jmcneill 	AXP_CTRL2("dcdc6", 600, 1520, 10, 51, 20, 21,
    316  1.17  jmcneill 		0x10, __BIT(5), 0x25, __BITS(6,0)),
    317  1.17  jmcneill 	AXP_CTRL2("dcdc7", 600, 1520, 10, 51, 20, 21,
    318  1.17  jmcneill 		0x10, __BIT(6), 0x26, __BITS(6,0)),
    319  1.17  jmcneill 	AXP_CTRL("aldo1", 700, 3300, 100,
    320  1.17  jmcneill 		0x13, __BIT(5), 0x28, __BITS(4,0)),
    321  1.17  jmcneill 	AXP_CTRL("aldo2", 700, 3300, 100,
    322  1.17  jmcneill 		0x13, __BIT(6), 0x29, __BITS(4,0)),
    323  1.17  jmcneill 	AXP_CTRL("aldo3", 700, 3300, 100,
    324  1.17  jmcneill 		0x13, __BIT(7), 0x2a, __BITS(4,0)),
    325  1.17  jmcneill };
    326  1.17  jmcneill 
    327  1.40     skrll static const struct axppmic_ctrl axp15060_ctrls[] = {
    328  1.40     skrll 	AXP_CTRL( "dcdc1",  1500, 3400, 100,
    329  1.40     skrll 		 0x13, __BITS(4, 0),
    330  1.40     skrll 		 0x10, __BIT(0)),
    331  1.40     skrll 	// DCDC2: 0.5~1.2V, 10mV/step, 1.22~1.54V, 20mV/step, IMAX=3.5A, DVM
    332  1.40     skrll 	AXP_CTRL2_RANGE("dcdc2",
    333  1.40     skrll 			500, 1540, 70, 10, 1220, 16 , 20,
    334  1.40     skrll 			0x14, __BITS(6, 0),
    335  1.40     skrll 			0x10, __BIT(1)),
    336  1.40     skrll 	// DCDC3: 0.5~1.2V, 10mV/step, 1.22~1.54V, 20mV/step, IMAX=3.5A, DVM
    337  1.40     skrll 	AXP_CTRL2_RANGE("dcdc3",
    338  1.40     skrll 			500, 1540, 70, 10, 1220, 16 , 20,
    339  1.40     skrll 			0x15, __BITS(6, 0),
    340  1.40     skrll 			0x10, __BIT(2)),
    341  1.40     skrll 	// DCDC4: 0.5~1.2V, 10mV/step, 1.22~1.54V, 20mV/step, IMAX=3.5A, DVM
    342  1.40     skrll 	AXP_CTRL2_RANGE("dcdc4",
    343  1.40     skrll 			500, 1540, 70, 10, 1220, 16 , 20,
    344  1.40     skrll 			0x16, __BITS(6, 0),
    345  1.40     skrll 			0x10, __BIT(3)),
    346  1.40     skrll 	// DCDC5: 0.8~1.12V, 10mV/step, 1.14~1.84V, 20mV/step, IMAX=2.5A, DVM
    347  1.40     skrll 	AXP_CTRL2_RANGE("dcdc5",
    348  1.40     skrll 			800, 1840,
    349  1.40     skrll 			32, 10,
    350  1.40     skrll 			1140, 35, 20,
    351  1.40     skrll 			0x17, __BITS(6, 0),
    352  1.40     skrll 			0x10, __BIT(4)),
    353  1.40     skrll 	AXP_CTRL("dcdc6", 500, 3400, 100,
    354  1.40     skrll 		 0x18, __BITS(4, 0),
    355  1.40     skrll 		 0x10, __BIT(5)),
    356  1.40     skrll 	AXP_CTRL("aldo1", 700, 3300, 100,
    357  1.40     skrll 		 0x19, __BITS(4, 0),
    358  1.40     skrll 		 0x11, __BIT(0)),
    359  1.40     skrll 	AXP_CTRL("aldo2", 700, 3300, 100,
    360  1.40     skrll 		 0x20, __BITS(4, 0),
    361  1.40     skrll 		 0x11, __BIT(1)),
    362  1.40     skrll 	AXP_CTRL("aldo3", 700, 3300, 100,
    363  1.40     skrll 		 0x21, __BITS(4, 0),
    364  1.40     skrll 		 0x11, __BIT(2)),
    365  1.40     skrll 	AXP_CTRL("aldo4", 700, 3300, 100,
    366  1.40     skrll 		 0x22, __BITS(4, 0),
    367  1.40     skrll 		 0x11, __BIT(3)),
    368  1.40     skrll 	AXP_CTRL("aldo5", 700, 3300, 100,
    369  1.40     skrll 		 0x23, __BITS(4, 0),
    370  1.40     skrll 		 0x11, __BIT(4)),
    371  1.40     skrll 	AXP_CTRL("bldo1", 700, 3300, 100,
    372  1.40     skrll 		 0x24, __BITS(4, 0),
    373  1.40     skrll 		 0x11, __BIT(5)),
    374  1.40     skrll 	AXP_CTRL("bldo2", 700, 3300, 100,
    375  1.40     skrll 		 0x25, __BITS(4, 0),
    376  1.40     skrll 		 0x11, __BIT(6)),
    377  1.40     skrll 	AXP_CTRL("bldo3", 700, 3300, 100,
    378  1.40     skrll 		 0x26, __BITS(4, 0),
    379  1.40     skrll 		 0x11, __BIT(7)),
    380  1.40     skrll 	AXP_CTRL("bldo4", 700, 3300, 100,
    381  1.40     skrll 		 0x27, __BITS(4, 0),
    382  1.40     skrll 		 0x12, __BIT(0)),
    383  1.40     skrll 	AXP_CTRL("bldo5", 700, 3300, 100,
    384  1.40     skrll 		 0x28, __BITS(4, 0),
    385  1.40     skrll 		 0x12, __BIT(1)),
    386  1.40     skrll 	AXP_CTRL("cldo1", 700, 3300, 100,
    387  1.40     skrll 		 0x29, __BITS(4, 0),
    388  1.40     skrll 		 0x12, __BIT(2)),
    389  1.40     skrll 	AXP_CTRL("cldo2", 700, 3300, 100,
    390  1.40     skrll 		 0x2a, __BITS(4, 0),
    391  1.40     skrll 		 0x12, __BIT(3)),
    392  1.40     skrll 	AXP_CTRL("cldo3", 700, 3300, 100,
    393  1.40     skrll 		 0x2b, __BITS(4, 0),
    394  1.40     skrll 		 0x12, __BIT(4)),
    395  1.40     skrll 	AXP_CTRL("cldo4", 700, 4200, 100,
    396  1.40     skrll 		 0x2d, __BITS(5, 0),
    397  1.40     skrll 		 0x12, __BIT(5)),
    398  1.40     skrll 	AXP_CTRL("cpusldo", 700, 1400, 50,
    399  1.40     skrll 		 0x2e, __BITS(3, 0),
    400  1.40     skrll 		 0x12, __BIT(6)),
    401  1.40     skrll };
    402  1.40     skrll 
    403  1.40     skrll 
    404   1.8  jmcneill struct axppmic_irq {
    405   1.8  jmcneill 	u_int reg;
    406   1.8  jmcneill 	uint8_t mask;
    407   1.8  jmcneill };
    408   1.8  jmcneill 
    409   1.8  jmcneill #define	AXPPMIC_IRQ(_reg, _mask)	\
    410   1.8  jmcneill 	{ .reg = (_reg), .mask = (_mask) }
    411   1.8  jmcneill 
    412   1.1  jmcneill struct axppmic_config {
    413   1.1  jmcneill 	const char *name;
    414  1.37  jmcneill 	const char *gpio_compat;
    415  1.37  jmcneill 	u_int gpio_npins;
    416   1.1  jmcneill 	const struct axppmic_ctrl *controls;
    417   1.1  jmcneill 	u_int ncontrols;
    418   1.1  jmcneill 	u_int irq_regs;
    419   1.2  jmcneill 	bool has_battery;
    420   1.2  jmcneill 	bool has_fuel_gauge;
    421  1.19  jmcneill 	bool has_mode_set;
    422   1.8  jmcneill 	struct axppmic_irq poklirq;
    423   1.8  jmcneill 	struct axppmic_irq acinirq;
    424   1.8  jmcneill 	struct axppmic_irq vbusirq;
    425   1.8  jmcneill 	struct axppmic_irq battirq;
    426   1.8  jmcneill 	struct axppmic_irq chargeirq;
    427   1.8  jmcneill 	struct axppmic_irq chargestirq;
    428  1.10  jmcneill 	u_int batsense_step;	/* uV */
    429  1.10  jmcneill 	u_int charge_step;	/* uA */
    430  1.10  jmcneill 	u_int discharge_step;	/* uA */
    431  1.10  jmcneill 	u_int maxcap_step;	/* uAh */
    432  1.10  jmcneill 	u_int coulomb_step;	/* uAh */
    433   1.2  jmcneill };
    434   1.2  jmcneill 
    435   1.2  jmcneill enum axppmic_sensor {
    436   1.3  jmcneill 	AXP_SENSOR_ACIN_PRESENT,
    437   1.3  jmcneill 	AXP_SENSOR_VBUS_PRESENT,
    438   1.2  jmcneill 	AXP_SENSOR_BATT_PRESENT,
    439   1.2  jmcneill 	AXP_SENSOR_BATT_CHARGING,
    440   1.2  jmcneill 	AXP_SENSOR_BATT_CHARGE_STATE,
    441  1.10  jmcneill 	AXP_SENSOR_BATT_VOLTAGE,
    442  1.10  jmcneill 	AXP_SENSOR_BATT_CHARGE_CURRENT,
    443  1.10  jmcneill 	AXP_SENSOR_BATT_DISCHARGE_CURRENT,
    444  1.10  jmcneill 	AXP_SENSOR_BATT_CAPACITY_PERCENT,
    445  1.16  jakllsch 	AXP_SENSOR_BATT_MAXIMUM_CAPACITY,
    446  1.16  jakllsch 	AXP_SENSOR_BATT_CURRENT_CAPACITY,
    447   1.2  jmcneill 	AXP_NSENSORS
    448   1.1  jmcneill };
    449   1.1  jmcneill 
    450   1.1  jmcneill struct axppmic_softc {
    451   1.1  jmcneill 	device_t	sc_dev;
    452   1.1  jmcneill 	i2c_tag_t	sc_i2c;
    453   1.1  jmcneill 	i2c_addr_t	sc_addr;
    454   1.1  jmcneill 	int		sc_phandle;
    455   1.1  jmcneill 
    456  1.29   thorpej 	void		*sc_ih;
    457  1.29   thorpej 	struct workqueue *sc_wq;
    458  1.29   thorpej 
    459  1.29   thorpej 	kmutex_t	sc_intr_lock;
    460  1.29   thorpej 	struct work	sc_work;
    461  1.29   thorpej 	bool		sc_work_scheduled;
    462  1.29   thorpej 
    463   1.8  jmcneill 	const struct axppmic_config *sc_conf;
    464   1.2  jmcneill 
    465   1.1  jmcneill 	struct sysmon_pswitch sc_smpsw;
    466   1.1  jmcneill 
    467   1.2  jmcneill 	struct sysmon_envsys *sc_sme;
    468   1.3  jmcneill 
    469   1.2  jmcneill 	envsys_data_t	sc_sensor[AXP_NSENSORS];
    470   1.4  jmcneill 
    471   1.4  jmcneill 	u_int		sc_warn_thres;
    472   1.4  jmcneill 	u_int		sc_shut_thres;
    473   1.1  jmcneill };
    474   1.1  jmcneill 
    475  1.37  jmcneill struct axppmic_gpio_pin {
    476  1.37  jmcneill 	struct axppmic_softc *pin_sc;
    477  1.37  jmcneill 	u_int pin_nr;
    478  1.37  jmcneill 	int pin_flags;
    479  1.37  jmcneill 	bool pin_actlo;
    480  1.37  jmcneill };
    481  1.37  jmcneill 
    482   1.1  jmcneill struct axpreg_softc {
    483   1.1  jmcneill 	device_t	sc_dev;
    484   1.1  jmcneill 	i2c_tag_t	sc_i2c;
    485   1.1  jmcneill 	i2c_addr_t	sc_addr;
    486   1.1  jmcneill 	const struct axppmic_ctrl *sc_ctrl;
    487   1.1  jmcneill };
    488   1.1  jmcneill 
    489   1.1  jmcneill struct axpreg_attach_args {
    490   1.1  jmcneill 	const struct axppmic_ctrl *reg_ctrl;
    491   1.1  jmcneill 	int		reg_phandle;
    492   1.1  jmcneill 	i2c_tag_t	reg_i2c;
    493   1.1  jmcneill 	i2c_addr_t	reg_addr;
    494   1.1  jmcneill };
    495   1.1  jmcneill 
    496   1.1  jmcneill static const struct axppmic_config axp803_config = {
    497   1.1  jmcneill 	.name = "AXP803",
    498  1.37  jmcneill 	.gpio_compat = "x-powers,axp803-gpio",
    499  1.37  jmcneill 	.gpio_npins = 2,
    500   1.1  jmcneill 	.controls = axp803_ctrls,
    501   1.1  jmcneill 	.ncontrols = __arraycount(axp803_ctrls),
    502   1.1  jmcneill 	.irq_regs = 6,
    503   1.2  jmcneill 	.has_battery = true,
    504   1.2  jmcneill 	.has_fuel_gauge = true,
    505  1.10  jmcneill 	.batsense_step = 1100,
    506  1.10  jmcneill 	.charge_step = 1000,
    507  1.10  jmcneill 	.discharge_step = 1000,
    508  1.16  jakllsch 	.maxcap_step = 1456,
    509  1.16  jakllsch 	.coulomb_step = 1456,
    510   1.8  jmcneill 	.poklirq = AXPPMIC_IRQ(5, __BIT(3)),
    511   1.8  jmcneill 	.acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
    512   1.8  jmcneill 	.vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
    513   1.8  jmcneill 	.battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
    514   1.8  jmcneill 	.chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
    515  1.38     skrll 	.chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
    516   1.1  jmcneill };
    517   1.1  jmcneill 
    518   1.1  jmcneill static const struct axppmic_config axp805_config = {
    519  1.19  jmcneill 	.name = "AXP805",
    520  1.19  jmcneill 	.controls = axp805_ctrls,
    521  1.19  jmcneill 	.ncontrols = __arraycount(axp805_ctrls),
    522  1.19  jmcneill 	.irq_regs = 2,
    523  1.19  jmcneill 	.poklirq = AXPPMIC_IRQ(2, __BIT(0)),
    524  1.19  jmcneill };
    525  1.19  jmcneill 
    526  1.19  jmcneill static const struct axppmic_config axp806_config = {
    527  1.19  jmcneill 	.name = "AXP806",
    528   1.1  jmcneill 	.controls = axp805_ctrls,
    529   1.1  jmcneill 	.ncontrols = __arraycount(axp805_ctrls),
    530  1.19  jmcneill #if notyet
    531   1.1  jmcneill 	.irq_regs = 2,
    532   1.8  jmcneill 	.poklirq = AXPPMIC_IRQ(2, __BIT(0)),
    533  1.19  jmcneill #endif
    534  1.19  jmcneill 	.has_mode_set = true,
    535   1.1  jmcneill };
    536   1.1  jmcneill 
    537  1.21  jmcneill static const struct axppmic_config axp809_config = {
    538  1.21  jmcneill 	.name = "AXP809",
    539  1.21  jmcneill 	.controls = axp809_ctrls,
    540  1.21  jmcneill 	.ncontrols = __arraycount(axp809_ctrls),
    541  1.21  jmcneill };
    542  1.21  jmcneill 
    543  1.17  jmcneill static const struct axppmic_config axp813_config = {
    544  1.17  jmcneill 	.name = "AXP813",
    545  1.37  jmcneill 	.gpio_compat = "x-powers,axp813-gpio",
    546  1.37  jmcneill 	.gpio_npins = 2,
    547  1.17  jmcneill 	.controls = axp813_ctrls,
    548  1.17  jmcneill 	.ncontrols = __arraycount(axp813_ctrls),
    549  1.17  jmcneill 	.irq_regs = 6,
    550  1.17  jmcneill 	.has_battery = true,
    551  1.17  jmcneill 	.has_fuel_gauge = true,
    552  1.17  jmcneill 	.batsense_step = 1100,
    553  1.17  jmcneill 	.charge_step = 1000,
    554  1.17  jmcneill 	.discharge_step = 1000,
    555  1.17  jmcneill 	.maxcap_step = 1456,
    556  1.17  jmcneill 	.coulomb_step = 1456,
    557  1.17  jmcneill 	.poklirq = AXPPMIC_IRQ(5, __BIT(3)),
    558  1.17  jmcneill 	.acinirq = AXPPMIC_IRQ(1, __BITS(6,5)),
    559  1.17  jmcneill 	.vbusirq = AXPPMIC_IRQ(1, __BITS(3,2)),
    560  1.17  jmcneill 	.battirq = AXPPMIC_IRQ(2, __BITS(7,6)),
    561  1.17  jmcneill 	.chargeirq = AXPPMIC_IRQ(2, __BITS(3,2)),
    562  1.38     skrll 	.chargestirq = AXPPMIC_IRQ(4, __BITS(1,0)),
    563  1.17  jmcneill };
    564  1.17  jmcneill 
    565  1.40     skrll static const struct axppmic_config axp15060_config = {
    566  1.40     skrll 	.name = "AXP15060",
    567  1.40     skrll 	.controls = axp15060_ctrls,
    568  1.40     skrll 	.ncontrols = __arraycount(axp15060_ctrls),
    569  1.40     skrll };
    570  1.40     skrll 
    571  1.14   thorpej static const struct device_compatible_entry compat_data[] = {
    572  1.30   thorpej 	{ .compat = "x-powers,axp803",		.data = &axp803_config },
    573  1.30   thorpej 	{ .compat = "x-powers,axp805",		.data = &axp805_config },
    574  1.30   thorpej 	{ .compat = "x-powers,axp806",		.data = &axp806_config },
    575  1.30   thorpej 	{ .compat = "x-powers,axp809",		.data = &axp809_config },
    576  1.30   thorpej 	{ .compat = "x-powers,axp813",		.data = &axp813_config },
    577  1.40     skrll 	{ .compat = "x-powers,axp15060",	.data = &axp15060_config },
    578  1.33   thorpej 	DEVICE_COMPAT_EOL
    579   1.1  jmcneill };
    580   1.1  jmcneill 
    581   1.1  jmcneill static int
    582   1.1  jmcneill axppmic_read(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t *val, int flags)
    583   1.1  jmcneill {
    584   1.1  jmcneill 	return iic_smbus_read_byte(tag, addr, reg, val, flags);
    585   1.1  jmcneill }
    586   1.1  jmcneill 
    587   1.1  jmcneill static int
    588   1.1  jmcneill axppmic_write(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg, uint8_t val, int flags)
    589   1.1  jmcneill {
    590   1.1  jmcneill 	return iic_smbus_write_byte(tag, addr, reg, val, flags);
    591   1.1  jmcneill }
    592   1.1  jmcneill 
    593   1.1  jmcneill static int
    594   1.1  jmcneill axppmic_set_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int min, u_int max)
    595   1.1  jmcneill {
    596   1.1  jmcneill 	u_int vol, reg_val;
    597   1.1  jmcneill 	int nstep, error;
    598   1.1  jmcneill 	uint8_t val;
    599   1.1  jmcneill 
    600   1.1  jmcneill 	if (!c->c_voltage_mask)
    601   1.1  jmcneill 		return EINVAL;
    602   1.1  jmcneill 
    603   1.1  jmcneill 	if (min < c->c_min || min > c->c_max)
    604   1.1  jmcneill 		return EINVAL;
    605   1.1  jmcneill 
    606   1.1  jmcneill 	reg_val = 0;
    607   1.1  jmcneill 	nstep = 1;
    608   1.1  jmcneill 	vol = c->c_min;
    609   1.1  jmcneill 
    610   1.1  jmcneill 	for (nstep = 0; nstep < c->c_step1cnt && vol < min; nstep++) {
    611   1.1  jmcneill 		++reg_val;
    612   1.1  jmcneill 		vol += c->c_step1;
    613   1.1  jmcneill 	}
    614  1.24  jmcneill 
    615  1.24  jmcneill 	if (c->c_step2start)
    616  1.24  jmcneill 		vol = c->c_step2start;
    617  1.24  jmcneill 
    618   1.1  jmcneill 	for (nstep = 0; nstep < c->c_step2cnt && vol < min; nstep++) {
    619   1.1  jmcneill 		++reg_val;
    620   1.1  jmcneill 		vol += c->c_step2;
    621   1.1  jmcneill 	}
    622   1.1  jmcneill 
    623   1.1  jmcneill 	if (vol > max)
    624   1.1  jmcneill 		return EINVAL;
    625   1.1  jmcneill 
    626  1.29   thorpej 	iic_acquire_bus(tag, 0);
    627  1.29   thorpej 	if ((error = axppmic_read(tag, addr, c->c_voltage_reg, &val, 0)) == 0) {
    628   1.1  jmcneill 		val &= ~c->c_voltage_mask;
    629   1.1  jmcneill 		val |= __SHIFTIN(reg_val, c->c_voltage_mask);
    630  1.29   thorpej 		error = axppmic_write(tag, addr, c->c_voltage_reg, val, 0);
    631   1.1  jmcneill 	}
    632  1.29   thorpej 	iic_release_bus(tag, 0);
    633   1.1  jmcneill 
    634   1.1  jmcneill 	return error;
    635   1.1  jmcneill }
    636   1.1  jmcneill 
    637   1.1  jmcneill static int
    638   1.1  jmcneill axppmic_get_voltage(i2c_tag_t tag, i2c_addr_t addr, const struct axppmic_ctrl *c, u_int *pvol)
    639   1.1  jmcneill {
    640   1.1  jmcneill 	int reg_val, error;
    641   1.1  jmcneill 	uint8_t val;
    642   1.1  jmcneill 
    643   1.1  jmcneill 	if (!c->c_voltage_mask)
    644   1.1  jmcneill 		return EINVAL;
    645   1.1  jmcneill 
    646  1.29   thorpej 	iic_acquire_bus(tag, 0);
    647  1.29   thorpej 	error = axppmic_read(tag, addr, c->c_voltage_reg, &val, 0);
    648  1.29   thorpej 	iic_release_bus(tag, 0);
    649   1.1  jmcneill 	if (error)
    650   1.1  jmcneill 		return error;
    651   1.1  jmcneill 
    652   1.1  jmcneill 	reg_val = __SHIFTOUT(val, c->c_voltage_mask);
    653   1.1  jmcneill 	if (reg_val < c->c_step1cnt) {
    654   1.1  jmcneill 		*pvol = c->c_min + reg_val * c->c_step1;
    655  1.24  jmcneill 	} else if (c->c_step2start) {
    656  1.24  jmcneill 		*pvol = c->c_step2start +
    657  1.24  jmcneill 		    ((reg_val - c->c_step1cnt) * c->c_step2);
    658   1.1  jmcneill 	} else {
    659   1.1  jmcneill 		*pvol = c->c_min + (c->c_step1cnt * c->c_step1) +
    660   1.1  jmcneill 		    ((reg_val - c->c_step1cnt) * c->c_step2);
    661   1.1  jmcneill 	}
    662   1.1  jmcneill 
    663   1.1  jmcneill 	return 0;
    664   1.1  jmcneill }
    665   1.1  jmcneill 
    666   1.1  jmcneill static void
    667   1.1  jmcneill axppmic_power_poweroff(device_t dev)
    668   1.1  jmcneill {
    669   1.1  jmcneill 	struct axppmic_softc *sc = device_private(dev);
    670  1.28   thorpej 	int error;
    671   1.1  jmcneill 
    672   1.1  jmcneill 	delay(1000000);
    673   1.1  jmcneill 
    674  1.29   thorpej 	error = iic_acquire_bus(sc->sc_i2c, 0);
    675  1.28   thorpej 	if (error == 0) {
    676  1.28   thorpej 		error = axppmic_write(sc->sc_i2c, sc->sc_addr,
    677  1.29   thorpej 		    AXP_POWER_DISABLE_REG, AXP_POWER_DISABLE_CTRL, 0);
    678  1.29   thorpej 		iic_release_bus(sc->sc_i2c, 0);
    679  1.28   thorpej 	}
    680  1.28   thorpej 	if (error) {
    681  1.28   thorpej 		device_printf(dev, "WARNING: unable to power off, error %d\n",
    682  1.28   thorpej 		    error);
    683  1.28   thorpej 	}
    684   1.1  jmcneill }
    685   1.1  jmcneill 
    686   1.1  jmcneill static struct fdtbus_power_controller_func axppmic_power_funcs = {
    687   1.1  jmcneill 	.poweroff = axppmic_power_poweroff,
    688   1.1  jmcneill };
    689   1.1  jmcneill 
    690  1.37  jmcneill static int
    691  1.37  jmcneill axppmic_gpio_ctl(struct axppmic_softc *sc, uint8_t pin, uint8_t func)
    692  1.37  jmcneill {
    693  1.37  jmcneill 	uint8_t val;
    694  1.37  jmcneill 	int error;
    695  1.37  jmcneill 
    696  1.37  jmcneill 	KASSERT(pin < sc->sc_conf->gpio_npins);
    697  1.37  jmcneill 	KASSERT((func & ~AXP_GPIO_CTRL_FUNC_MASK) == 0);
    698  1.37  jmcneill 
    699  1.37  jmcneill 	iic_acquire_bus(sc->sc_i2c, 0);
    700  1.37  jmcneill 	error = axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_GPIO_CTRL_REG(pin),
    701  1.37  jmcneill 	    &val, 0);
    702  1.37  jmcneill 	if (error == 0) {
    703  1.37  jmcneill 		val &= ~AXP_GPIO_CTRL_FUNC_MASK;
    704  1.37  jmcneill 		val |= func;
    705  1.37  jmcneill 		error = axppmic_write(sc->sc_i2c, sc->sc_addr,
    706  1.37  jmcneill 		    AXP_GPIO_CTRL_REG(pin), val, 0);
    707  1.37  jmcneill 	}
    708  1.37  jmcneill 	iic_release_bus(sc->sc_i2c, 0);
    709  1.37  jmcneill 
    710  1.37  jmcneill 	return error;
    711  1.37  jmcneill }
    712  1.37  jmcneill 
    713  1.37  jmcneill static void *
    714  1.37  jmcneill axppmic_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    715  1.37  jmcneill {
    716  1.37  jmcneill 	struct axppmic_softc *sc = device_private(dev);
    717  1.37  jmcneill 	struct axppmic_gpio_pin *gpin;
    718  1.37  jmcneill 	const u_int *gpio = data;
    719  1.37  jmcneill 	int error;
    720  1.37  jmcneill 
    721  1.37  jmcneill 	if (len != 12) {
    722  1.37  jmcneill 		return NULL;
    723  1.37  jmcneill 	}
    724  1.37  jmcneill 
    725  1.37  jmcneill 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    726  1.37  jmcneill 	const bool actlo = be32toh(gpio[2]) & 1;
    727  1.37  jmcneill 
    728  1.37  jmcneill 	if (pin >= sc->sc_conf->gpio_npins) {
    729  1.37  jmcneill 		return NULL;
    730  1.37  jmcneill 	}
    731  1.37  jmcneill 
    732  1.37  jmcneill 	if ((flags & GPIO_PIN_INPUT) != 0) {
    733  1.37  jmcneill 		error = axppmic_gpio_ctl(sc, pin, AXP_GPIO_CTRL_FUNC_INPUT);
    734  1.37  jmcneill 		if (error != 0) {
    735  1.37  jmcneill 			return NULL;
    736  1.37  jmcneill 		}
    737  1.37  jmcneill 	}
    738  1.37  jmcneill 
    739  1.37  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    740  1.37  jmcneill 	gpin->pin_sc = sc;
    741  1.37  jmcneill 	gpin->pin_nr = pin;
    742  1.37  jmcneill 	gpin->pin_flags = flags;
    743  1.37  jmcneill 	gpin->pin_actlo = actlo;
    744  1.37  jmcneill 
    745  1.37  jmcneill 	return gpin;
    746  1.37  jmcneill }
    747  1.37  jmcneill 
    748  1.37  jmcneill static void
    749  1.37  jmcneill axppmic_gpio_release(device_t dev, void *priv)
    750  1.37  jmcneill {
    751  1.37  jmcneill 	struct axppmic_softc *sc = device_private(dev);
    752  1.37  jmcneill 	struct axppmic_gpio_pin *gpin = priv;
    753  1.37  jmcneill 
    754  1.37  jmcneill 	axppmic_gpio_ctl(sc, gpin->pin_nr, AXP_GPIO_CTRL_FUNC_INPUT);
    755  1.37  jmcneill 
    756  1.37  jmcneill 	kmem_free(gpin, sizeof(*gpin));
    757  1.37  jmcneill }
    758  1.37  jmcneill 
    759  1.37  jmcneill static int
    760  1.37  jmcneill axppmic_gpio_read(device_t dev, void *priv, bool raw)
    761  1.37  jmcneill {
    762  1.37  jmcneill 	struct axppmic_softc *sc = device_private(dev);
    763  1.37  jmcneill 	struct axppmic_gpio_pin *gpin = priv;
    764  1.37  jmcneill 	uint8_t data;
    765  1.37  jmcneill 	int error, val;
    766  1.37  jmcneill 
    767  1.37  jmcneill 	KASSERT(sc == gpin->pin_sc);
    768  1.37  jmcneill 
    769  1.37  jmcneill 	const uint8_t data_mask = __BIT(gpin->pin_nr);
    770  1.37  jmcneill 
    771  1.37  jmcneill 	iic_acquire_bus(sc->sc_i2c, 0);
    772  1.37  jmcneill 	error = axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_GPIO_SIGNAL_REG,
    773  1.37  jmcneill 	    &data, 0);
    774  1.37  jmcneill 	iic_release_bus(sc->sc_i2c, 0);
    775  1.37  jmcneill 
    776  1.37  jmcneill 	if (error != 0) {
    777  1.37  jmcneill 		device_printf(dev, "WARNING: failed to read pin %d: %d\n",
    778  1.37  jmcneill 		    gpin->pin_nr, error);
    779  1.37  jmcneill 		val = 0;
    780  1.37  jmcneill 	} else {
    781  1.37  jmcneill 		val = __SHIFTOUT(data, data_mask);
    782  1.37  jmcneill 	}
    783  1.37  jmcneill 	if (!raw && gpin->pin_actlo) {
    784  1.37  jmcneill 		val = !val;
    785  1.37  jmcneill 	}
    786  1.37  jmcneill 
    787  1.37  jmcneill 	return val;
    788  1.37  jmcneill }
    789  1.37  jmcneill 
    790  1.37  jmcneill static void
    791  1.37  jmcneill axppmic_gpio_write(device_t dev, void *priv, int val, bool raw)
    792  1.37  jmcneill {
    793  1.37  jmcneill 	struct axppmic_softc *sc = device_private(dev);
    794  1.37  jmcneill 	struct axppmic_gpio_pin *gpin = priv;
    795  1.37  jmcneill 	int error;
    796  1.37  jmcneill 
    797  1.37  jmcneill 	if (!raw && gpin->pin_actlo) {
    798  1.37  jmcneill 		val = !val;
    799  1.37  jmcneill 	}
    800  1.37  jmcneill 
    801  1.37  jmcneill 	error = axppmic_gpio_ctl(sc, gpin->pin_nr,
    802  1.37  jmcneill 	    val == 0 ? AXP_GPIO_CTRL_FUNC_LOW : AXP_GPIO_CTRL_FUNC_HIGH);
    803  1.37  jmcneill 	if (error != 0) {
    804  1.37  jmcneill 		device_printf(dev, "WARNING: failed to write pin %d: %d\n",
    805  1.37  jmcneill 		    gpin->pin_nr, error);
    806  1.37  jmcneill 	}
    807  1.37  jmcneill }
    808  1.37  jmcneill 
    809  1.37  jmcneill static struct fdtbus_gpio_controller_func axppmic_gpio_funcs = {
    810  1.37  jmcneill 	.acquire = axppmic_gpio_acquire,
    811  1.37  jmcneill 	.release = axppmic_gpio_release,
    812  1.37  jmcneill 	.read = axppmic_gpio_read,
    813  1.37  jmcneill 	.write = axppmic_gpio_write,
    814  1.37  jmcneill };
    815  1.37  jmcneill 
    816   1.1  jmcneill static void
    817   1.1  jmcneill axppmic_task_shut(void *priv)
    818   1.1  jmcneill {
    819   1.1  jmcneill 	struct axppmic_softc *sc = priv;
    820   1.1  jmcneill 
    821   1.1  jmcneill 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
    822   1.1  jmcneill }
    823   1.1  jmcneill 
    824   1.2  jmcneill static void
    825   1.8  jmcneill axppmic_sensor_update(struct sysmon_envsys *sme, envsys_data_t *e)
    826   1.2  jmcneill {
    827   1.2  jmcneill 	struct axppmic_softc *sc = sme->sme_cookie;
    828  1.10  jmcneill 	const struct axppmic_config *c = sc->sc_conf;
    829  1.10  jmcneill 	uint8_t val, lo, hi;
    830   1.2  jmcneill 
    831   1.2  jmcneill 	e->state = ENVSYS_SINVALID;
    832   1.2  jmcneill 
    833  1.10  jmcneill 	const bool battery_present =
    834  1.10  jmcneill 	    sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].state == ENVSYS_SVALID &&
    835  1.10  jmcneill 	    sc->sc_sensor[AXP_SENSOR_BATT_PRESENT].value_cur == 1;
    836  1.10  jmcneill 
    837   1.2  jmcneill 	switch (e->private) {
    838   1.3  jmcneill 	case AXP_SENSOR_ACIN_PRESENT:
    839  1.29   thorpej 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, 0) == 0) {
    840   1.3  jmcneill 			e->state = ENVSYS_SVALID;
    841   1.3  jmcneill 			e->value_cur = !!(val & AXP_POWER_SOURCE_ACIN_PRESENT);
    842   1.3  jmcneill 		}
    843   1.3  jmcneill 		break;
    844   1.3  jmcneill 	case AXP_SENSOR_VBUS_PRESENT:
    845  1.29   thorpej 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, 0) == 0) {
    846   1.3  jmcneill 			e->state = ENVSYS_SVALID;
    847   1.3  jmcneill 			e->value_cur = !!(val & AXP_POWER_SOURCE_VBUS_PRESENT);
    848   1.3  jmcneill 		}
    849   1.3  jmcneill 		break;
    850   1.2  jmcneill 	case AXP_SENSOR_BATT_PRESENT:
    851  1.29   thorpej 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, 0) == 0) {
    852   1.2  jmcneill 			if (val & AXP_POWER_MODE_BATT_VALID) {
    853   1.2  jmcneill 				e->state = ENVSYS_SVALID;
    854   1.2  jmcneill 				e->value_cur = !!(val & AXP_POWER_MODE_BATT_PRESENT);
    855   1.2  jmcneill 			}
    856   1.2  jmcneill 		}
    857   1.2  jmcneill 		break;
    858   1.2  jmcneill 	case AXP_SENSOR_BATT_CHARGING:
    859  1.29   thorpej 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_MODE_REG, &val, 0) == 0) {
    860   1.2  jmcneill 			e->state = ENVSYS_SVALID;
    861   1.2  jmcneill 			e->value_cur = !!(val & AXP_POWER_MODE_BATT_CHARGING);
    862   1.2  jmcneill 		}
    863   1.2  jmcneill 		break;
    864   1.2  jmcneill 	case AXP_SENSOR_BATT_CHARGE_STATE:
    865  1.10  jmcneill 		if (battery_present &&
    866  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, 0) == 0 &&
    867   1.4  jmcneill 		    (val & AXP_BATT_CAP_VALID) != 0) {
    868   1.2  jmcneill 			const u_int batt_val = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
    869   1.4  jmcneill 			if (batt_val <= sc->sc_shut_thres) {
    870   1.2  jmcneill 				e->state = ENVSYS_SCRITICAL;
    871   1.2  jmcneill 				e->value_cur = ENVSYS_BATTERY_CAPACITY_CRITICAL;
    872   1.4  jmcneill 			} else if (batt_val <= sc->sc_warn_thres) {
    873   1.2  jmcneill 				e->state = ENVSYS_SWARNUNDER;
    874   1.2  jmcneill 				e->value_cur = ENVSYS_BATTERY_CAPACITY_WARNING;
    875   1.2  jmcneill 			} else {
    876   1.2  jmcneill 				e->state = ENVSYS_SVALID;
    877   1.2  jmcneill 				e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
    878   1.2  jmcneill 			}
    879   1.2  jmcneill 		}
    880   1.2  jmcneill 		break;
    881  1.10  jmcneill 	case AXP_SENSOR_BATT_CAPACITY_PERCENT:
    882  1.10  jmcneill 		if (battery_present &&
    883  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_REG, &val, 0) == 0 &&
    884   1.2  jmcneill 		    (val & AXP_BATT_CAP_VALID) != 0) {
    885   1.2  jmcneill 			e->state = ENVSYS_SVALID;
    886   1.2  jmcneill 			e->value_cur = __SHIFTOUT(val, AXP_BATT_CAP_PERCENT);
    887   1.2  jmcneill 		}
    888   1.2  jmcneill 		break;
    889  1.10  jmcneill 	case AXP_SENSOR_BATT_VOLTAGE:
    890  1.10  jmcneill 		if (battery_present &&
    891  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_HI_REG, &hi, 0) == 0 &&
    892  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATSENSE_LO_REG, &lo, 0) == 0) {
    893  1.10  jmcneill 			e->state = ENVSYS_SVALID;
    894  1.10  jmcneill 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->batsense_step;
    895  1.10  jmcneill 		}
    896  1.10  jmcneill 		break;
    897  1.10  jmcneill 	case AXP_SENSOR_BATT_CHARGE_CURRENT:
    898  1.10  jmcneill 		if (battery_present &&
    899  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, 0) == 0 &&
    900  1.10  jmcneill 		    (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) != 0 &&
    901  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_HI_REG, &hi, 0) == 0 &&
    902  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTCHG_LO_REG, &lo, 0) == 0) {
    903  1.10  jmcneill 			e->state = ENVSYS_SVALID;
    904  1.10  jmcneill 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->charge_step;
    905  1.10  jmcneill 		}
    906  1.10  jmcneill 		break;
    907  1.10  jmcneill 	case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
    908  1.10  jmcneill 		if (battery_present &&
    909  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_POWER_SOURCE_REG, &val, 0) == 0 &&
    910  1.10  jmcneill 		    (val & AXP_POWER_SOURCE_CHARGE_DIRECTION) == 0 &&
    911  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_HI_REG, &hi, 0) == 0 &&
    912  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATTDISCHG_LO_REG, &lo, 0) == 0) {
    913  1.10  jmcneill 			e->state = ENVSYS_SVALID;
    914  1.10  jmcneill 			e->value_cur = AXP_ADC_RAW(hi, lo) * c->discharge_step;
    915  1.10  jmcneill 		}
    916  1.10  jmcneill 		break;
    917  1.16  jakllsch 	case AXP_SENSOR_BATT_MAXIMUM_CAPACITY:
    918  1.16  jakllsch 		if (battery_present &&
    919  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_HI_REG, &hi, 0) == 0 &&
    920  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_MAX_CAP_LO_REG, &lo, 0) == 0) {
    921  1.16  jakllsch 			e->state = (hi & AXP_BATT_MAX_CAP_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
    922  1.16  jakllsch 			e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->maxcap_step;
    923  1.16  jakllsch 		}
    924  1.16  jakllsch 		break;
    925  1.16  jakllsch 	case AXP_SENSOR_BATT_CURRENT_CAPACITY:
    926  1.16  jakllsch 		if (battery_present &&
    927  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_HI_REG, &hi, 0) == 0 &&
    928  1.29   thorpej 		    axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_COULOMB_LO_REG, &lo, 0) == 0) {
    929  1.16  jakllsch 			e->state = (hi & AXP_BATT_COULOMB_VALID) ? ENVSYS_SVALID : ENVSYS_SINVALID;
    930  1.16  jakllsch 			e->value_cur = AXP_COULOMB_RAW(hi, lo) * c->coulomb_step;
    931  1.16  jakllsch 		}
    932  1.16  jakllsch 		break;
    933   1.2  jmcneill 	}
    934   1.8  jmcneill }
    935   1.8  jmcneill 
    936   1.8  jmcneill static void
    937   1.8  jmcneill axppmic_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
    938   1.8  jmcneill {
    939   1.8  jmcneill 	struct axppmic_softc *sc = sme->sme_cookie;
    940   1.8  jmcneill 
    941   1.8  jmcneill 	switch (e->private) {
    942  1.10  jmcneill 	case AXP_SENSOR_BATT_CAPACITY_PERCENT:
    943  1.10  jmcneill 	case AXP_SENSOR_BATT_VOLTAGE:
    944  1.10  jmcneill 	case AXP_SENSOR_BATT_CHARGE_CURRENT:
    945  1.10  jmcneill 	case AXP_SENSOR_BATT_DISCHARGE_CURRENT:
    946  1.10  jmcneill 		/* Always update battery capacity and ADCs */
    947  1.29   thorpej 		iic_acquire_bus(sc->sc_i2c, 0);
    948   1.8  jmcneill 		axppmic_sensor_update(sme, e);
    949  1.29   thorpej 		iic_release_bus(sc->sc_i2c, 0);
    950   1.8  jmcneill 		break;
    951   1.8  jmcneill 	default:
    952   1.8  jmcneill 		/* Refresh if the sensor is not in valid state */
    953   1.8  jmcneill 		if (e->state != ENVSYS_SVALID) {
    954  1.29   thorpej 			iic_acquire_bus(sc->sc_i2c, 0);
    955   1.8  jmcneill 			axppmic_sensor_update(sme, e);
    956  1.29   thorpej 			iic_release_bus(sc->sc_i2c, 0);
    957   1.8  jmcneill 		}
    958   1.8  jmcneill 		break;
    959   1.8  jmcneill 	}
    960   1.8  jmcneill }
    961   1.8  jmcneill 
    962   1.8  jmcneill static int
    963   1.8  jmcneill axppmic_intr(void *priv)
    964   1.8  jmcneill {
    965  1.29   thorpej 	struct axppmic_softc * const sc = priv;
    966  1.29   thorpej 
    967  1.29   thorpej 	mutex_enter(&sc->sc_intr_lock);
    968  1.29   thorpej 
    969  1.29   thorpej 	fdtbus_intr_mask(sc->sc_phandle, sc->sc_ih);
    970  1.29   thorpej 
    971  1.29   thorpej 	/* Interrupt is always masked when work is scheduled! */
    972  1.29   thorpej 	KASSERT(!sc->sc_work_scheduled);
    973  1.29   thorpej 	sc->sc_work_scheduled = true;
    974  1.29   thorpej 	workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
    975  1.29   thorpej 
    976  1.29   thorpej 	mutex_exit(&sc->sc_intr_lock);
    977  1.29   thorpej 
    978  1.29   thorpej 	return 1;
    979  1.29   thorpej }
    980  1.29   thorpej 
    981  1.29   thorpej static void
    982  1.29   thorpej axppmic_work(struct work *work, void *arg)
    983  1.29   thorpej {
    984  1.29   thorpej 	struct axppmic_softc * const sc =
    985  1.29   thorpej 	    container_of(work, struct axppmic_softc, sc_work);
    986  1.29   thorpej 	const struct axppmic_config * const c = sc->sc_conf;
    987  1.29   thorpej 	const int flags = 0;
    988   1.8  jmcneill 	uint8_t stat;
    989   1.8  jmcneill 	u_int n;
    990   1.8  jmcneill 
    991  1.29   thorpej 	KASSERT(sc->sc_work_scheduled);
    992  1.29   thorpej 
    993   1.8  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
    994   1.8  jmcneill 	for (n = 1; n <= c->irq_regs; n++) {
    995   1.8  jmcneill 		if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_IRQ_STATUS_REG(n), &stat, flags) == 0) {
    996  1.29   thorpej 			if (stat != 0) {
    997  1.29   thorpej 				axppmic_write(sc->sc_i2c, sc->sc_addr,
    998  1.29   thorpej 				    AXP_IRQ_STATUS_REG(n), stat, flags);
    999  1.29   thorpej 			}
   1000  1.29   thorpej 
   1001   1.8  jmcneill 			if (n == c->poklirq.reg && (stat & c->poklirq.mask) != 0)
   1002   1.8  jmcneill 				sysmon_task_queue_sched(0, axppmic_task_shut, sc);
   1003   1.8  jmcneill 			if (n == c->acinirq.reg && (stat & c->acinirq.mask) != 0)
   1004   1.8  jmcneill 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT]);
   1005   1.8  jmcneill 			if (n == c->vbusirq.reg && (stat & c->vbusirq.mask) != 0)
   1006   1.8  jmcneill 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT]);
   1007   1.8  jmcneill 			if (n == c->battirq.reg && (stat & c->battirq.mask) != 0)
   1008   1.8  jmcneill 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT]);
   1009   1.8  jmcneill 			if (n == c->chargeirq.reg && (stat & c->chargeirq.mask) != 0)
   1010   1.8  jmcneill 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING]);
   1011   1.8  jmcneill 			if (n == c->chargestirq.reg && (stat & c->chargestirq.mask) != 0)
   1012   1.8  jmcneill 				axppmic_sensor_update(sc->sc_sme, &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE]);
   1013   1.8  jmcneill 		}
   1014   1.8  jmcneill 	}
   1015   1.2  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
   1016   1.8  jmcneill 
   1017  1.29   thorpej 	mutex_enter(&sc->sc_intr_lock);
   1018  1.29   thorpej 	sc->sc_work_scheduled = false;
   1019  1.29   thorpej 	fdtbus_intr_unmask(sc->sc_phandle, sc->sc_ih);
   1020  1.29   thorpej 	mutex_exit(&sc->sc_intr_lock);
   1021   1.2  jmcneill }
   1022   1.2  jmcneill 
   1023   1.2  jmcneill static void
   1024   1.3  jmcneill axppmic_attach_acadapter(struct axppmic_softc *sc)
   1025   1.3  jmcneill {
   1026   1.3  jmcneill 	envsys_data_t *e;
   1027   1.3  jmcneill 
   1028   1.3  jmcneill 	e = &sc->sc_sensor[AXP_SENSOR_ACIN_PRESENT];
   1029   1.3  jmcneill 	e->private = AXP_SENSOR_ACIN_PRESENT;
   1030   1.3  jmcneill 	e->units = ENVSYS_INDICATOR;
   1031   1.3  jmcneill 	e->state = ENVSYS_SINVALID;
   1032   1.3  jmcneill 	strlcpy(e->desc, "ACIN present", sizeof(e->desc));
   1033   1.3  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1034   1.3  jmcneill 
   1035   1.3  jmcneill 	e = &sc->sc_sensor[AXP_SENSOR_VBUS_PRESENT];
   1036   1.3  jmcneill 	e->private = AXP_SENSOR_VBUS_PRESENT;
   1037   1.3  jmcneill 	e->units = ENVSYS_INDICATOR;
   1038   1.3  jmcneill 	e->state = ENVSYS_SINVALID;
   1039   1.3  jmcneill 	strlcpy(e->desc, "VBUS present", sizeof(e->desc));
   1040   1.3  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1041   1.3  jmcneill }
   1042   1.3  jmcneill 
   1043   1.3  jmcneill static void
   1044   1.2  jmcneill axppmic_attach_battery(struct axppmic_softc *sc)
   1045   1.2  jmcneill {
   1046  1.10  jmcneill 	const struct axppmic_config *c = sc->sc_conf;
   1047   1.2  jmcneill 	envsys_data_t *e;
   1048   1.4  jmcneill 	uint8_t val;
   1049   1.4  jmcneill 
   1050  1.27   thorpej 	iic_acquire_bus(sc->sc_i2c, 0);
   1051  1.29   thorpej 	if (axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_BATT_CAP_WARN_REG, &val, 0) == 0) {
   1052   1.4  jmcneill 		sc->sc_warn_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV1) + 5;
   1053   1.4  jmcneill 		sc->sc_shut_thres = __SHIFTOUT(val, AXP_BATT_CAP_WARN_LV2);
   1054   1.4  jmcneill 	}
   1055  1.27   thorpej 	iic_release_bus(sc->sc_i2c, 0);
   1056   1.2  jmcneill 
   1057   1.2  jmcneill 	e = &sc->sc_sensor[AXP_SENSOR_BATT_PRESENT];
   1058   1.2  jmcneill 	e->private = AXP_SENSOR_BATT_PRESENT;
   1059   1.2  jmcneill 	e->units = ENVSYS_INDICATOR;
   1060   1.2  jmcneill 	e->state = ENVSYS_SINVALID;
   1061   1.2  jmcneill 	strlcpy(e->desc, "battery present", sizeof(e->desc));
   1062   1.2  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1063   1.2  jmcneill 
   1064   1.2  jmcneill 	e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGING];
   1065   1.2  jmcneill 	e->private = AXP_SENSOR_BATT_CHARGING;
   1066   1.2  jmcneill 	e->units = ENVSYS_BATTERY_CHARGE;
   1067   1.2  jmcneill 	e->state = ENVSYS_SINVALID;
   1068   1.2  jmcneill 	strlcpy(e->desc, "charging", sizeof(e->desc));
   1069   1.2  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1070   1.2  jmcneill 
   1071   1.2  jmcneill 	e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_STATE];
   1072   1.2  jmcneill 	e->private = AXP_SENSOR_BATT_CHARGE_STATE;
   1073   1.2  jmcneill 	e->units = ENVSYS_BATTERY_CAPACITY;
   1074   1.2  jmcneill 	e->flags = ENVSYS_FMONSTCHANGED;
   1075   1.9  jmcneill 	e->state = ENVSYS_SINVALID;
   1076   1.2  jmcneill 	e->value_cur = ENVSYS_BATTERY_CAPACITY_NORMAL;
   1077   1.2  jmcneill 	strlcpy(e->desc, "charge state", sizeof(e->desc));
   1078   1.2  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1079   1.2  jmcneill 
   1080  1.10  jmcneill 	if (c->batsense_step) {
   1081  1.10  jmcneill 		e = &sc->sc_sensor[AXP_SENSOR_BATT_VOLTAGE];
   1082  1.10  jmcneill 		e->private = AXP_SENSOR_BATT_VOLTAGE;
   1083  1.10  jmcneill 		e->units = ENVSYS_SVOLTS_DC;
   1084  1.10  jmcneill 		e->state = ENVSYS_SINVALID;
   1085  1.10  jmcneill 		strlcpy(e->desc, "battery voltage", sizeof(e->desc));
   1086  1.10  jmcneill 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1087  1.10  jmcneill 	}
   1088  1.10  jmcneill 
   1089  1.10  jmcneill 	if (c->charge_step) {
   1090  1.10  jmcneill 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CHARGE_CURRENT];
   1091  1.10  jmcneill 		e->private = AXP_SENSOR_BATT_CHARGE_CURRENT;
   1092  1.10  jmcneill 		e->units = ENVSYS_SAMPS;
   1093  1.10  jmcneill 		e->state = ENVSYS_SINVALID;
   1094  1.10  jmcneill 		strlcpy(e->desc, "battery charge current", sizeof(e->desc));
   1095  1.10  jmcneill 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1096  1.10  jmcneill 	}
   1097  1.10  jmcneill 
   1098  1.10  jmcneill 	if (c->discharge_step) {
   1099  1.10  jmcneill 		e = &sc->sc_sensor[AXP_SENSOR_BATT_DISCHARGE_CURRENT];
   1100  1.10  jmcneill 		e->private = AXP_SENSOR_BATT_DISCHARGE_CURRENT;
   1101  1.10  jmcneill 		e->units = ENVSYS_SAMPS;
   1102  1.10  jmcneill 		e->state = ENVSYS_SINVALID;
   1103  1.10  jmcneill 		strlcpy(e->desc, "battery discharge current", sizeof(e->desc));
   1104  1.10  jmcneill 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1105  1.10  jmcneill 	}
   1106  1.10  jmcneill 
   1107  1.10  jmcneill 	if (c->has_fuel_gauge) {
   1108  1.10  jmcneill 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CAPACITY_PERCENT];
   1109  1.10  jmcneill 		e->private = AXP_SENSOR_BATT_CAPACITY_PERCENT;
   1110   1.2  jmcneill 		e->units = ENVSYS_INTEGER;
   1111   1.2  jmcneill 		e->state = ENVSYS_SINVALID;
   1112   1.2  jmcneill 		e->flags = ENVSYS_FPERCENT;
   1113   1.2  jmcneill 		strlcpy(e->desc, "battery percent", sizeof(e->desc));
   1114   1.2  jmcneill 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1115   1.2  jmcneill 	}
   1116  1.16  jakllsch 
   1117  1.16  jakllsch 	if (c->maxcap_step) {
   1118  1.16  jakllsch 		e = &sc->sc_sensor[AXP_SENSOR_BATT_MAXIMUM_CAPACITY];
   1119  1.16  jakllsch 		e->private = AXP_SENSOR_BATT_MAXIMUM_CAPACITY;
   1120  1.16  jakllsch 		e->units = ENVSYS_SAMPHOUR;
   1121  1.16  jakllsch 		e->state = ENVSYS_SINVALID;
   1122  1.16  jakllsch 		strlcpy(e->desc, "battery maximum capacity", sizeof(e->desc));
   1123  1.16  jakllsch 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1124  1.16  jakllsch 	}
   1125  1.16  jakllsch 
   1126  1.16  jakllsch 	if (c->coulomb_step) {
   1127  1.16  jakllsch 		e = &sc->sc_sensor[AXP_SENSOR_BATT_CURRENT_CAPACITY];
   1128  1.16  jakllsch 		e->private = AXP_SENSOR_BATT_CURRENT_CAPACITY;
   1129  1.16  jakllsch 		e->units = ENVSYS_SAMPHOUR;
   1130  1.16  jakllsch 		e->state = ENVSYS_SINVALID;
   1131  1.16  jakllsch 		strlcpy(e->desc, "battery current capacity", sizeof(e->desc));
   1132  1.16  jakllsch 		sysmon_envsys_sensor_attach(sc->sc_sme, e);
   1133  1.16  jakllsch 	}
   1134   1.2  jmcneill }
   1135   1.2  jmcneill 
   1136   1.2  jmcneill static void
   1137   1.2  jmcneill axppmic_attach_sensors(struct axppmic_softc *sc)
   1138   1.2  jmcneill {
   1139   1.8  jmcneill 	if (sc->sc_conf->has_battery) {
   1140   1.2  jmcneill 		sc->sc_sme = sysmon_envsys_create();
   1141   1.2  jmcneill 		sc->sc_sme->sme_name = device_xname(sc->sc_dev);
   1142   1.2  jmcneill 		sc->sc_sme->sme_cookie = sc;
   1143   1.2  jmcneill 		sc->sc_sme->sme_refresh = axppmic_sensor_refresh;
   1144   1.2  jmcneill 		sc->sc_sme->sme_class = SME_CLASS_BATTERY;
   1145   1.5  jmcneill 		sc->sc_sme->sme_flags = SME_INIT_REFRESH;
   1146   1.2  jmcneill 
   1147   1.3  jmcneill 		axppmic_attach_acadapter(sc);
   1148   1.2  jmcneill 		axppmic_attach_battery(sc);
   1149   1.2  jmcneill 
   1150   1.2  jmcneill 		sysmon_envsys_register(sc->sc_sme);
   1151   1.2  jmcneill 	}
   1152   1.2  jmcneill }
   1153   1.2  jmcneill 
   1154   1.2  jmcneill 
   1155   1.1  jmcneill static int
   1156   1.1  jmcneill axppmic_match(device_t parent, cfdata_t match, void *aux)
   1157   1.1  jmcneill {
   1158   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
   1159  1.12   thorpej 	int match_result;
   1160   1.1  jmcneill 
   1161  1.14   thorpej 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
   1162  1.12   thorpej 		return match_result;
   1163   1.1  jmcneill 
   1164  1.11   thorpej 	/* This device is direct-config only. */
   1165  1.11   thorpej 
   1166  1.11   thorpej 	return 0;
   1167   1.1  jmcneill }
   1168   1.1  jmcneill 
   1169   1.1  jmcneill static void
   1170   1.1  jmcneill axppmic_attach(device_t parent, device_t self, void *aux)
   1171   1.1  jmcneill {
   1172   1.1  jmcneill 	struct axppmic_softc *sc = device_private(self);
   1173  1.13   thorpej 	const struct device_compatible_entry *dce = NULL;
   1174   1.1  jmcneill 	const struct axppmic_config *c;
   1175   1.1  jmcneill 	struct axpreg_attach_args aaa;
   1176   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
   1177   1.1  jmcneill 	int phandle, child, i;
   1178  1.19  jmcneill 	uint8_t irq_mask, val;
   1179  1.19  jmcneill 	int error;
   1180   1.1  jmcneill 
   1181  1.31   thorpej 	dce = iic_compatible_lookup(ia, compat_data);
   1182  1.12   thorpej 	KASSERT(dce != NULL);
   1183  1.30   thorpej 	c = dce->data;
   1184   1.1  jmcneill 
   1185   1.1  jmcneill 	sc->sc_dev = self;
   1186   1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
   1187   1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
   1188  1.42   thorpej 	sc->sc_phandle = devhandle_to_of(device_handle(self));
   1189   1.8  jmcneill 	sc->sc_conf = c;
   1190   1.1  jmcneill 
   1191   1.1  jmcneill 	aprint_naive("\n");
   1192   1.1  jmcneill 	aprint_normal(": %s\n", c->name);
   1193   1.1  jmcneill 
   1194  1.19  jmcneill 	if (c->has_mode_set) {
   1195  1.39     skrll 		const bool master_mode =
   1196  1.39     skrll 		    of_hasprop(sc->sc_phandle, "x-powers,self-working-mode") ||
   1197  1.19  jmcneill 		    of_hasprop(sc->sc_phandle, "x-powers,master-mode");
   1198  1.19  jmcneill 
   1199  1.27   thorpej 		iic_acquire_bus(sc->sc_i2c, 0);
   1200  1.19  jmcneill 		axppmic_write(sc->sc_i2c, sc->sc_addr, AXP_ADDR_EXT_REG,
   1201  1.27   thorpej 		    master_mode ? AXP_ADDR_EXT_MASTER : AXP_ADDR_EXT_SLAVE, 0);
   1202  1.27   thorpej 		iic_release_bus(sc->sc_i2c, 0);
   1203  1.19  jmcneill 	}
   1204  1.19  jmcneill 
   1205  1.27   thorpej 	iic_acquire_bus(sc->sc_i2c, 0);
   1206  1.27   thorpej 	error = axppmic_read(sc->sc_i2c, sc->sc_addr, AXP_CHIP_ID_REG, &val, 0);
   1207  1.27   thorpej 	iic_release_bus(sc->sc_i2c, 0);
   1208  1.19  jmcneill 	if (error != 0) {
   1209  1.19  jmcneill 		aprint_error_dev(self, "couldn't read chipid\n");
   1210  1.19  jmcneill 		return;
   1211  1.19  jmcneill 	}
   1212  1.19  jmcneill 	aprint_debug_dev(self, "chipid %#x\n", val);
   1213  1.19  jmcneill 
   1214   1.1  jmcneill 	sc->sc_smpsw.smpsw_name = device_xname(self);
   1215   1.1  jmcneill 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
   1216   1.1  jmcneill 	sysmon_pswitch_register(&sc->sc_smpsw);
   1217   1.1  jmcneill 
   1218  1.29   thorpej 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
   1219  1.29   thorpej 
   1220  1.19  jmcneill 	if (c->irq_regs > 0) {
   1221  1.29   thorpej 		char intrstr[128];
   1222  1.29   thorpej 
   1223  1.29   thorpej 		if (!fdtbus_intr_str(sc->sc_phandle, 0,
   1224  1.29   thorpej 				     intrstr, sizeof(intrstr))) {
   1225  1.29   thorpej 			aprint_error_dev(self,
   1226  1.29   thorpej 			    "WARNING: failed to decode interrupt\n");
   1227  1.29   thorpej 		}
   1228  1.29   thorpej 
   1229  1.29   thorpej 		sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM,
   1230  1.29   thorpej 						  FDT_INTR_MPSAFE,
   1231  1.29   thorpej 						  axppmic_intr, sc);
   1232  1.29   thorpej 		if (sc->sc_ih == NULL) {
   1233  1.29   thorpej 			aprint_error_dev(self,
   1234  1.29   thorpej 			    "WARNING: couldn't establish interrupt handler\n");
   1235  1.29   thorpej 		}
   1236  1.29   thorpej 
   1237  1.29   thorpej 		error = workqueue_create(&sc->sc_wq, device_xname(self),
   1238  1.29   thorpej 					 axppmic_work, NULL,
   1239  1.29   thorpej 					 PRI_SOFTSERIAL, IPL_VM,
   1240  1.29   thorpej 					 WQ_MPSAFE);
   1241  1.29   thorpej 		if (error) {
   1242  1.29   thorpej 			sc->sc_wq = NULL;
   1243  1.29   thorpej 			aprint_error_dev(self,
   1244  1.29   thorpej 			    "WARNING: couldn't create work queue: error %d\n",
   1245  1.29   thorpej 			    error);
   1246  1.19  jmcneill 		}
   1247  1.19  jmcneill 
   1248  1.29   thorpej 		if (sc->sc_ih != NULL && sc->sc_wq != NULL) {
   1249  1.29   thorpej 			iic_acquire_bus(sc->sc_i2c, 0);
   1250  1.29   thorpej 			for (i = 1; i <= c->irq_regs; i++) {
   1251  1.29   thorpej 				irq_mask = 0;
   1252  1.29   thorpej 				if (i == c->poklirq.reg)
   1253  1.29   thorpej 					irq_mask |= c->poklirq.mask;
   1254  1.29   thorpej 				if (i == c->acinirq.reg)
   1255  1.29   thorpej 					irq_mask |= c->acinirq.mask;
   1256  1.29   thorpej 				if (i == c->vbusirq.reg)
   1257  1.29   thorpej 					irq_mask |= c->vbusirq.mask;
   1258  1.29   thorpej 				if (i == c->battirq.reg)
   1259  1.29   thorpej 					irq_mask |= c->battirq.mask;
   1260  1.29   thorpej 				if (i == c->chargeirq.reg)
   1261  1.29   thorpej 					irq_mask |= c->chargeirq.mask;
   1262  1.29   thorpej 				if (i == c->chargestirq.reg)
   1263  1.29   thorpej 					irq_mask |= c->chargestirq.mask;
   1264  1.29   thorpej 				axppmic_write(sc->sc_i2c, sc->sc_addr,
   1265  1.29   thorpej 					      AXP_IRQ_ENABLE_REG(i),
   1266  1.29   thorpej 					      irq_mask, 0);
   1267  1.29   thorpej 			}
   1268  1.29   thorpej 			iic_release_bus(sc->sc_i2c, 0);
   1269  1.19  jmcneill 		}
   1270   1.1  jmcneill 	}
   1271   1.1  jmcneill 
   1272   1.1  jmcneill 	fdtbus_register_power_controller(sc->sc_dev, sc->sc_phandle,
   1273   1.1  jmcneill 	    &axppmic_power_funcs);
   1274   1.1  jmcneill 
   1275  1.37  jmcneill 	if (c->gpio_compat != NULL) {
   1276  1.37  jmcneill 		phandle = of_find_bycompat(sc->sc_phandle, c->gpio_compat);
   1277  1.37  jmcneill 		if (phandle > 0) {
   1278  1.37  jmcneill 			fdtbus_register_gpio_controller(self, phandle,
   1279  1.37  jmcneill 			    &axppmic_gpio_funcs);
   1280  1.37  jmcneill 		}
   1281  1.37  jmcneill 	}
   1282  1.37  jmcneill 
   1283   1.1  jmcneill 	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
   1284   1.2  jmcneill 	if (phandle > 0) {
   1285   1.2  jmcneill 		aaa.reg_i2c = sc->sc_i2c;
   1286   1.2  jmcneill 		aaa.reg_addr = sc->sc_addr;
   1287   1.2  jmcneill 		for (i = 0; i < c->ncontrols; i++) {
   1288   1.2  jmcneill 			const struct axppmic_ctrl *ctrl = &c->controls[i];
   1289   1.2  jmcneill 			child = of_find_firstchild_byname(phandle, ctrl->c_name);
   1290   1.2  jmcneill 			if (child <= 0)
   1291   1.2  jmcneill 				continue;
   1292   1.2  jmcneill 			aaa.reg_ctrl = ctrl;
   1293   1.2  jmcneill 			aaa.reg_phandle = child;
   1294  1.36   thorpej 			config_found(sc->sc_dev, &aaa, NULL, CFARGS_NONE);
   1295   1.2  jmcneill 		}
   1296   1.2  jmcneill 	}
   1297   1.1  jmcneill 
   1298   1.2  jmcneill 	if (c->has_battery)
   1299   1.2  jmcneill 		axppmic_attach_sensors(sc);
   1300   1.1  jmcneill }
   1301   1.1  jmcneill 
   1302   1.1  jmcneill static int
   1303   1.1  jmcneill axpreg_acquire(device_t dev)
   1304   1.1  jmcneill {
   1305   1.1  jmcneill 	return 0;
   1306   1.1  jmcneill }
   1307   1.1  jmcneill 
   1308   1.1  jmcneill static void
   1309   1.1  jmcneill axpreg_release(device_t dev)
   1310   1.1  jmcneill {
   1311   1.1  jmcneill }
   1312   1.1  jmcneill 
   1313   1.1  jmcneill static int
   1314   1.1  jmcneill axpreg_enable(device_t dev, bool enable)
   1315   1.1  jmcneill {
   1316   1.1  jmcneill 	struct axpreg_softc *sc = device_private(dev);
   1317   1.1  jmcneill 	const struct axppmic_ctrl *c = sc->sc_ctrl;
   1318  1.25   thorpej 	const int flags = 0;
   1319   1.1  jmcneill 	uint8_t val;
   1320   1.1  jmcneill 	int error;
   1321   1.1  jmcneill 
   1322   1.1  jmcneill 	if (!c->c_enable_mask)
   1323   1.1  jmcneill 		return EINVAL;
   1324   1.1  jmcneill 
   1325   1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, flags);
   1326   1.1  jmcneill 	if ((error = axppmic_read(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, &val, flags)) == 0) {
   1327  1.23  jmcneill 		val &= ~c->c_enable_mask;
   1328   1.1  jmcneill 		if (enable)
   1329  1.23  jmcneill 			val |= c->c_enable_val;
   1330   1.1  jmcneill 		else
   1331  1.23  jmcneill 			val |= c->c_disable_val;
   1332   1.1  jmcneill 		error = axppmic_write(sc->sc_i2c, sc->sc_addr, c->c_enable_reg, val, flags);
   1333   1.1  jmcneill 	}
   1334   1.1  jmcneill 	iic_release_bus(sc->sc_i2c, flags);
   1335   1.1  jmcneill 
   1336   1.1  jmcneill 	return error;
   1337   1.1  jmcneill }
   1338   1.1  jmcneill 
   1339   1.1  jmcneill static int
   1340   1.1  jmcneill axpreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
   1341   1.1  jmcneill {
   1342   1.1  jmcneill 	struct axpreg_softc *sc = device_private(dev);
   1343   1.1  jmcneill 	const struct axppmic_ctrl *c = sc->sc_ctrl;
   1344   1.1  jmcneill 
   1345   1.1  jmcneill 	return axppmic_set_voltage(sc->sc_i2c, sc->sc_addr, c,
   1346   1.1  jmcneill 	    min_uvol / 1000, max_uvol / 1000);
   1347   1.1  jmcneill }
   1348   1.1  jmcneill 
   1349   1.1  jmcneill static int
   1350   1.1  jmcneill axpreg_get_voltage(device_t dev, u_int *puvol)
   1351   1.1  jmcneill {
   1352   1.1  jmcneill 	struct axpreg_softc *sc = device_private(dev);
   1353   1.1  jmcneill 	const struct axppmic_ctrl *c = sc->sc_ctrl;
   1354   1.1  jmcneill 	int error;
   1355   1.1  jmcneill 	u_int vol;
   1356   1.1  jmcneill 
   1357   1.1  jmcneill 	error = axppmic_get_voltage(sc->sc_i2c, sc->sc_addr, c, &vol);
   1358   1.1  jmcneill 	if (error)
   1359   1.1  jmcneill 		return error;
   1360   1.1  jmcneill 
   1361   1.1  jmcneill 	*puvol = vol * 1000;
   1362   1.1  jmcneill 	return 0;
   1363   1.1  jmcneill }
   1364   1.1  jmcneill 
   1365   1.1  jmcneill static struct fdtbus_regulator_controller_func axpreg_funcs = {
   1366   1.1  jmcneill 	.acquire = axpreg_acquire,
   1367   1.1  jmcneill 	.release = axpreg_release,
   1368   1.1  jmcneill 	.enable = axpreg_enable,
   1369   1.1  jmcneill 	.set_voltage = axpreg_set_voltage,
   1370   1.1  jmcneill 	.get_voltage = axpreg_get_voltage,
   1371   1.1  jmcneill };
   1372   1.1  jmcneill 
   1373   1.1  jmcneill static int
   1374   1.1  jmcneill axpreg_match(device_t parent, cfdata_t match, void *aux)
   1375   1.1  jmcneill {
   1376   1.1  jmcneill 	return 1;
   1377   1.1  jmcneill }
   1378   1.1  jmcneill 
   1379   1.1  jmcneill static void
   1380   1.1  jmcneill axpreg_attach(device_t parent, device_t self, void *aux)
   1381   1.1  jmcneill {
   1382   1.1  jmcneill 	struct axpreg_softc *sc = device_private(self);
   1383   1.1  jmcneill 	struct axpreg_attach_args *aaa = aux;
   1384   1.1  jmcneill 	const int phandle = aaa->reg_phandle;
   1385   1.1  jmcneill 	const char *name;
   1386  1.20  jmcneill 	u_int uvol, min_uvol, max_uvol;
   1387   1.1  jmcneill 
   1388   1.1  jmcneill 	sc->sc_dev = self;
   1389   1.1  jmcneill 	sc->sc_i2c = aaa->reg_i2c;
   1390   1.1  jmcneill 	sc->sc_addr = aaa->reg_addr;
   1391   1.1  jmcneill 	sc->sc_ctrl = aaa->reg_ctrl;
   1392   1.1  jmcneill 
   1393   1.1  jmcneill 	fdtbus_register_regulator_controller(self, phandle,
   1394   1.1  jmcneill 	    &axpreg_funcs);
   1395   1.1  jmcneill 
   1396   1.1  jmcneill 	aprint_naive("\n");
   1397   1.1  jmcneill 	name = fdtbus_get_string(phandle, "regulator-name");
   1398   1.1  jmcneill 	if (name)
   1399   1.1  jmcneill 		aprint_normal(": %s\n", name);
   1400   1.1  jmcneill 	else
   1401   1.1  jmcneill 		aprint_normal("\n");
   1402  1.20  jmcneill 
   1403  1.35     skrll 	int error = axpreg_get_voltage(self, &uvol);
   1404  1.35     skrll 	if (error)
   1405  1.35     skrll 		return;
   1406  1.35     skrll 
   1407  1.20  jmcneill 	if (of_getprop_uint32(phandle, "regulator-min-microvolt", &min_uvol) == 0 &&
   1408  1.20  jmcneill 	    of_getprop_uint32(phandle, "regulator-max-microvolt", &max_uvol) == 0) {
   1409  1.20  jmcneill 		if (uvol < min_uvol || uvol > max_uvol) {
   1410  1.22  jmcneill 			aprint_debug_dev(self, "fix voltage %u uV -> %u/%u uV\n",
   1411  1.22  jmcneill 			    uvol, min_uvol, max_uvol);
   1412  1.20  jmcneill 			axpreg_set_voltage(self, min_uvol, max_uvol);
   1413  1.20  jmcneill 		}
   1414  1.20  jmcneill 	}
   1415  1.22  jmcneill 
   1416  1.22  jmcneill 	if (of_hasprop(phandle, "regulator-always-on") ||
   1417  1.22  jmcneill 	    of_hasprop(phandle, "regulator-boot-on")) {
   1418  1.22  jmcneill 		axpreg_enable(self, true);
   1419  1.22  jmcneill 	}
   1420   1.1  jmcneill }
   1421   1.1  jmcneill 
   1422   1.1  jmcneill CFATTACH_DECL_NEW(axppmic, sizeof(struct axppmic_softc),
   1423   1.1  jmcneill     axppmic_match, axppmic_attach, NULL, NULL);
   1424   1.1  jmcneill 
   1425   1.1  jmcneill CFATTACH_DECL_NEW(axpreg, sizeof(struct axpreg_softc),
   1426   1.1  jmcneill     axpreg_match, axpreg_attach, NULL, NULL);
   1427