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