Home | History | Annotate | Line # | Download | only in i2c
rkpmic.c revision 1.17
      1  1.17   thorpej /* $NetBSD: rkpmic.c,v 1.17 2025/09/17 13:42:43 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 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.17   thorpej __KERNEL_RCSID(0, "$NetBSD: rkpmic.c,v 1.17 2025/09/17 13:42:43 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.1  jmcneill 
     40   1.4       tnn #include <dev/clock_subr.h>
     41   1.4       tnn 
     42   1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43   1.1  jmcneill 
     44   1.6  jmcneill #include <dev/clk/clk_backend.h>
     45   1.6  jmcneill 
     46   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47   1.1  jmcneill 
     48   1.4       tnn #define	SECONDS_REG		0x00
     49   1.4       tnn #define	MINUTES_REG		0x01
     50   1.4       tnn #define	HOURS_REG		0x02
     51   1.4       tnn #define	DAYS_REG		0x03
     52   1.4       tnn #define	MONTHS_REG		0x04
     53   1.4       tnn #define	YEARS_REG		0x05
     54   1.4       tnn #define	WEEKS_REG		0x06
     55   1.4       tnn 
     56   1.4       tnn #define	RTC_CTRL_REG		0x10
     57   1.4       tnn #define	RTC_CTRL_READSEL	__BIT(7)
     58   1.4       tnn #define	RTC_CTRL_GET_TIME	__BIT(6)
     59   1.4       tnn #define	RTC_CTRL_SET_32_COUNTER	__BIT(5)
     60   1.4       tnn #define	RTC_CTRL_TEST_MODE	__BIT(4)
     61   1.4       tnn #define	RTC_CTRL_AMPM_MODE	__BIT(3)
     62   1.4       tnn #define	RTC_CTRL_AUTO_COMP	__BIT(2)
     63   1.4       tnn #define	RTC_CTRL_ROUND_30S	__BIT(1)
     64   1.4       tnn #define	RTC_CTRL_STOP_RTC	__BIT(0)
     65   1.4       tnn 
     66   1.4       tnn #define	RTC_INT_REG		0x12
     67   1.4       tnn #define	RTC_COMP_LSB_REG	0x13
     68   1.4       tnn #define	RTC_COMP_MSB_REG	0x14
     69   1.1  jmcneill #define	CHIP_NAME_REG		0x17
     70   1.1  jmcneill #define	CHIP_VER_REG		0x18
     71   1.1  jmcneill 
     72   1.6  jmcneill #define	CLK32OUT_REG		0x20
     73   1.6  jmcneill #define	CLK32OUT_CLKOUT2_EN	__BIT(0)
     74   1.6  jmcneill 
     75   1.8  jmcneill #define	DEVCTRL_REG		0x4b
     76   1.8  jmcneill #define	DEVCTRL_DEV_OFF_RST	__BIT(3)
     77   1.8  jmcneill 
     78   1.1  jmcneill struct rkpmic_ctrl {
     79   1.1  jmcneill 	const char *	name;
     80   1.1  jmcneill 	uint8_t		enable_reg;
     81   1.1  jmcneill 	uint8_t		enable_mask;
     82   1.1  jmcneill 	uint8_t		vsel_reg;
     83   1.1  jmcneill 	uint8_t		vsel_mask;
     84   1.1  jmcneill 	u_int		base;
     85   1.1  jmcneill 	u_int		step;
     86   1.2  jmcneill 	u_int		flags;
     87   1.2  jmcneill #define	F_ENABLE_WRITE_MASK	0x00
     88   1.1  jmcneill };
     89   1.1  jmcneill 
     90   1.1  jmcneill struct rkpmic_config {
     91   1.1  jmcneill 	const char *	name;
     92   1.1  jmcneill 	const struct rkpmic_ctrl *ctrl;
     93   1.1  jmcneill 	u_int		nctrl;
     94   1.8  jmcneill 
     95   1.8  jmcneill 	u_int		poweroff_reg;
     96   1.8  jmcneill 	u_int		poweroff_mask;
     97   1.1  jmcneill };
     98   1.1  jmcneill 
     99   1.2  jmcneill static const struct rkpmic_ctrl rk805_ctrls[] = {
    100   1.2  jmcneill 	/* DCDC */
    101   1.2  jmcneill 	{ .name = "DCDC_REG1",	.flags = F_ENABLE_WRITE_MASK,
    102   1.2  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(0),
    103   1.2  jmcneill 	  .vsel_reg = 0x2f,	.vsel_mask = __BITS(5,0),
    104   1.2  jmcneill 	  .base = 712500,	.step = 12500 },
    105   1.2  jmcneill 	{ .name = "DCDC_REG2",	.flags = F_ENABLE_WRITE_MASK,
    106   1.2  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(1),
    107   1.2  jmcneill 	  .vsel_reg = 0x33,	.vsel_mask = __BITS(5,0),
    108   1.2  jmcneill 	  .base = 712500,	.step = 12500 },
    109   1.2  jmcneill 	{ .name = "DCDC_REG3",	.flags = F_ENABLE_WRITE_MASK,
    110   1.2  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(2) },
    111   1.2  jmcneill 	{ .name = "DCDC_REG4",	.flags = F_ENABLE_WRITE_MASK,
    112   1.2  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(3),
    113   1.2  jmcneill 	  .vsel_reg = 0x38,	.vsel_mask = __BITS(3,0),
    114   1.2  jmcneill 	  .base = 800000,	.step = 100000 },
    115   1.2  jmcneill 
    116   1.2  jmcneill 	/* LDO */
    117   1.2  jmcneill 	{ .name = "LDO_REG1",	.flags = F_ENABLE_WRITE_MASK,
    118   1.2  jmcneill 	  .enable_reg = 0x27,	.enable_mask = __BIT(0),
    119   1.2  jmcneill 	  .vsel_reg = 0x3b,	.vsel_mask = __BITS(4,0),
    120   1.2  jmcneill 	  .base = 800000,	.step = 100000 },
    121   1.2  jmcneill 	{ .name = "LDO_REG2",	.flags = F_ENABLE_WRITE_MASK,
    122   1.2  jmcneill 	  .enable_reg = 0x27,	.enable_mask = __BIT(1),
    123   1.2  jmcneill 	  .vsel_reg = 0x3d,	.vsel_mask = __BITS(4,0),
    124   1.2  jmcneill 	  .base = 800000,	.step = 100000 },
    125   1.2  jmcneill 	{ .name = "LDO_REG3",	.flags = F_ENABLE_WRITE_MASK,
    126   1.2  jmcneill 	  .enable_reg = 0x27,	.enable_mask = __BIT(2),
    127   1.2  jmcneill 	  .vsel_reg = 0x3f,	.vsel_mask = __BITS(4,0),
    128   1.2  jmcneill 	  .base = 800000,	.step = 100000 },
    129   1.2  jmcneill };
    130   1.2  jmcneill 
    131   1.2  jmcneill static const struct rkpmic_config rk805_config = {
    132   1.2  jmcneill 	.name = "RK805",
    133   1.2  jmcneill 	.ctrl = rk805_ctrls,
    134   1.2  jmcneill 	.nctrl = __arraycount(rk805_ctrls),
    135   1.2  jmcneill };
    136   1.2  jmcneill 
    137   1.1  jmcneill static const struct rkpmic_ctrl rk808_ctrls[] = {
    138   1.1  jmcneill 	/* DCDC */
    139   1.1  jmcneill 	{ .name = "DCDC_REG1",
    140   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(0),
    141   1.1  jmcneill 	  .vsel_reg = 0x2f,	.vsel_mask = __BITS(5,0),
    142   1.1  jmcneill 	  .base = 712500,	.step = 12500 },
    143   1.1  jmcneill 	{ .name = "DCDC_REG2",
    144   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(1),
    145   1.1  jmcneill 	  .vsel_reg = 0x33,	.vsel_mask = __BITS(5,0),
    146   1.1  jmcneill 	  .base = 712500,	.step = 12500 },
    147   1.1  jmcneill 	{ .name = "DCDC_REG3",
    148   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(2) },
    149   1.1  jmcneill 	{ .name = "DCDC_REG4",
    150   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(3),
    151   1.1  jmcneill 	  .vsel_reg = 0x38,	.vsel_mask = __BITS(3,0),
    152   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    153   1.1  jmcneill 
    154   1.1  jmcneill 	/* LDO */
    155   1.1  jmcneill 	{ .name = "LDO_REG1",
    156   1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
    157   1.1  jmcneill 	  .vsel_reg = 0x3b,	.vsel_mask = __BITS(4,0),
    158   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    159   1.1  jmcneill 	{ .name = "LDO_REG2",
    160   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(1),
    161   1.1  jmcneill 	  .vsel_reg = 0x3d,	.vsel_mask = __BITS(4,0),
    162   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    163   1.1  jmcneill 	{ .name = "LDO_REG3",
    164   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(2),
    165   1.1  jmcneill 	  .vsel_reg = 0x3f,	.vsel_mask = __BITS(3,0),
    166   1.1  jmcneill 	  .base = 800000,	.step = 100000 },
    167   1.1  jmcneill 	{ .name = "LDO_REG4",
    168   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(3),
    169   1.1  jmcneill 	  .vsel_reg = 0x41,	.vsel_mask = __BITS(4,0),
    170   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    171   1.1  jmcneill 	{ .name = "LDO_REG5",
    172   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(4),
    173   1.1  jmcneill 	  .vsel_reg = 0x43,	.vsel_mask = __BITS(4,0),
    174   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    175   1.1  jmcneill 	{ .name = "LDO_REG6",
    176   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(5),
    177   1.1  jmcneill 	  .vsel_reg = 0x45,	.vsel_mask = __BITS(4,0),
    178   1.1  jmcneill 	  .base = 800000,	.step = 100000 },
    179   1.1  jmcneill 	{ .name = "LDO_REG7",
    180   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(6),
    181   1.1  jmcneill 	  .vsel_reg = 0x47,	.vsel_mask = __BITS(4,0),
    182   1.1  jmcneill 	  .base = 800000,	.step = 100000 },
    183   1.1  jmcneill 	{ .name = "LDO_REG8",
    184   1.3  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(7),
    185   1.1  jmcneill 	  .vsel_reg = 0x49,	.vsel_mask = __BITS(4,0),
    186   1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    187   1.1  jmcneill 
    188   1.1  jmcneill 	/* SWITCH */
    189   1.1  jmcneill 	{ .name = "SWITCH_REG1",
    190   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(5) },
    191   1.1  jmcneill 	{ .name = "SWITCH_REG2",
    192   1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(6) },
    193   1.1  jmcneill };
    194   1.1  jmcneill 
    195   1.1  jmcneill static const struct rkpmic_config rk808_config = {
    196   1.1  jmcneill 	.name = "RK808",
    197   1.1  jmcneill 	.ctrl = rk808_ctrls,
    198   1.1  jmcneill 	.nctrl = __arraycount(rk808_ctrls),
    199   1.8  jmcneill 	.poweroff_reg = DEVCTRL_REG,
    200   1.8  jmcneill 	.poweroff_mask = DEVCTRL_DEV_OFF_RST,
    201   1.1  jmcneill };
    202   1.1  jmcneill 
    203   1.6  jmcneill struct rkpmic_softc;
    204   1.6  jmcneill 
    205   1.6  jmcneill struct rkpmic_clk {
    206   1.6  jmcneill 	struct clk	base;
    207   1.6  jmcneill };
    208   1.6  jmcneill 
    209   1.1  jmcneill struct rkpmic_softc {
    210   1.1  jmcneill 	device_t	sc_dev;
    211   1.1  jmcneill 	i2c_tag_t	sc_i2c;
    212   1.1  jmcneill 	i2c_addr_t	sc_addr;
    213   1.1  jmcneill 	int		sc_phandle;
    214   1.4       tnn 	struct todr_chip_handle sc_todr;
    215   1.9   thorpej 	const struct rkpmic_config *sc_conf;
    216   1.6  jmcneill 	struct clk_domain sc_clkdom;
    217   1.6  jmcneill 	struct rkpmic_clk sc_clk[2];
    218   1.1  jmcneill };
    219   1.1  jmcneill 
    220   1.1  jmcneill struct rkreg_softc {
    221   1.1  jmcneill 	device_t	sc_dev;
    222   1.1  jmcneill 	struct rkpmic_softc *sc_pmic;
    223   1.1  jmcneill 	const struct rkpmic_ctrl *sc_ctrl;
    224   1.1  jmcneill };
    225   1.1  jmcneill 
    226   1.1  jmcneill struct rkreg_attach_args {
    227   1.1  jmcneill 	const struct rkpmic_ctrl *reg_ctrl;
    228   1.1  jmcneill 	int		reg_phandle;
    229   1.1  jmcneill };
    230   1.1  jmcneill 
    231   1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
    232   1.9   thorpej 	{ .compat = "rockchip,rk805",	.data = &rk805_config },
    233   1.9   thorpej 	{ .compat = "rockchip,rk808",	.data = &rk808_config },
    234  1.12   thorpej 	DEVICE_COMPAT_EOL
    235   1.1  jmcneill };
    236   1.1  jmcneill 
    237   1.1  jmcneill static uint8_t
    238   1.1  jmcneill rkpmic_read(struct rkpmic_softc *sc, uint8_t reg, int flags)
    239   1.1  jmcneill {
    240   1.1  jmcneill 	uint8_t val = 0;
    241   1.1  jmcneill 	int error;
    242   1.1  jmcneill 
    243   1.1  jmcneill 	error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, flags);
    244   1.1  jmcneill 	if (error != 0)
    245   1.4       tnn 		device_printf(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    246   1.1  jmcneill 
    247   1.1  jmcneill 	return val;
    248   1.1  jmcneill }
    249   1.1  jmcneill 
    250   1.1  jmcneill static void
    251   1.1  jmcneill rkpmic_write(struct rkpmic_softc *sc, uint8_t reg, uint8_t val, int flags)
    252   1.1  jmcneill {
    253   1.1  jmcneill 	int error;
    254   1.1  jmcneill 
    255   1.1  jmcneill 	error = iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, flags);
    256   1.1  jmcneill 	if (error != 0)
    257   1.4       tnn 		device_printf(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    258   1.1  jmcneill }
    259   1.1  jmcneill 
    260   1.7   thorpej #define	I2C_READ(sc, reg)	rkpmic_read((sc), (reg), 0)
    261   1.7   thorpej #define	I2C_WRITE(sc, reg, val)	rkpmic_write((sc), (reg), (val), 0)
    262   1.7   thorpej #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
    263   1.7   thorpej #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
    264   1.1  jmcneill 
    265   1.1  jmcneill static int
    266   1.4       tnn rkpmic_todr_settime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    267   1.4       tnn {
    268  1.15   thorpej 	struct rkpmic_softc * const sc = device_private(ch->todr_dev);
    269   1.4       tnn 	uint8_t val;
    270   1.7   thorpej 	int error;
    271   1.4       tnn 
    272   1.4       tnn 	if (dt->dt_year < 2000 || dt->dt_year >= 2100) {
    273   1.4       tnn 		device_printf(sc->sc_dev, "year out of range\n");
    274   1.4       tnn 		return EINVAL;
    275   1.4       tnn 	}
    276   1.4       tnn 
    277   1.7   thorpej 	if ((error = I2C_LOCK(sc)) != 0)
    278   1.7   thorpej 		return error;
    279   1.7   thorpej 
    280   1.7   thorpej 	/* XXX Fix error reporting. */
    281   1.4       tnn 
    282   1.4       tnn 	val = I2C_READ(sc, RTC_CTRL_REG);
    283   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, val | RTC_CTRL_STOP_RTC);
    284   1.4       tnn 	I2C_WRITE(sc, SECONDS_REG, bintobcd(dt->dt_sec));
    285   1.4       tnn 	I2C_WRITE(sc, MINUTES_REG, bintobcd(dt->dt_min));
    286   1.4       tnn 	I2C_WRITE(sc, HOURS_REG, bintobcd(dt->dt_hour));
    287   1.4       tnn 	I2C_WRITE(sc, DAYS_REG, bintobcd(dt->dt_day));
    288   1.4       tnn 	I2C_WRITE(sc, MONTHS_REG, bintobcd(dt->dt_mon));
    289   1.4       tnn 	I2C_WRITE(sc, YEARS_REG, bintobcd(dt->dt_year % 100));
    290   1.4       tnn 	I2C_WRITE(sc, WEEKS_REG, bintobcd(dt->dt_wday == 0 ? 7 : dt->dt_wday));
    291   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, val);
    292   1.4       tnn 	I2C_UNLOCK(sc);
    293   1.4       tnn 
    294   1.4       tnn 	return 0;
    295   1.4       tnn }
    296   1.4       tnn 
    297   1.4       tnn static int
    298   1.4       tnn rkpmic_todr_gettime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    299   1.4       tnn {
    300  1.15   thorpej 	struct rkpmic_softc * const sc = device_private(ch->todr_dev);
    301   1.4       tnn 	uint8_t val;
    302   1.7   thorpej 	int error;
    303   1.7   thorpej 
    304   1.7   thorpej 	if ((error = I2C_LOCK(sc)) != 0)
    305   1.7   thorpej 		return error;
    306   1.4       tnn 
    307   1.7   thorpej 	/* XXX Fix error reporting. */
    308   1.4       tnn 
    309   1.4       tnn 	val = I2C_READ(sc, RTC_CTRL_REG);
    310   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, val | RTC_CTRL_GET_TIME | RTC_CTRL_READSEL);
    311   1.5       tnn 	delay(1000000 / 32768); /* wait one cycle for shadow regs to latch */
    312   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, val | RTC_CTRL_READSEL);
    313   1.4       tnn 	dt->dt_sec = bcdtobin(I2C_READ(sc, SECONDS_REG));
    314   1.4       tnn 	dt->dt_min = bcdtobin(I2C_READ(sc, MINUTES_REG));
    315   1.4       tnn 	dt->dt_hour = bcdtobin(I2C_READ(sc, HOURS_REG));
    316   1.4       tnn 	dt->dt_day = bcdtobin(I2C_READ(sc, DAYS_REG));
    317   1.4       tnn 	dt->dt_mon = bcdtobin(I2C_READ(sc, MONTHS_REG));
    318   1.4       tnn 	dt->dt_year = 2000 + bcdtobin(I2C_READ(sc, YEARS_REG));
    319   1.4       tnn 	dt->dt_wday = bcdtobin(I2C_READ(sc, WEEKS_REG));
    320   1.4       tnn 	if (dt->dt_wday == 7)
    321   1.4       tnn 		dt->dt_wday = 0;
    322   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, val);
    323   1.4       tnn 	I2C_UNLOCK(sc);
    324   1.4       tnn 
    325   1.4       tnn 	/*
    326   1.4       tnn 	 * RK808 has a hw bug which makes the 31st of November a valid day.
    327   1.4       tnn 	 * If we detect the 31st of November we skip ahead one day.
    328   1.4       tnn 	 * If the system has been turned off during the crossover the clock
    329   1.4       tnn 	 * will have lost a day. No easy way to detect this. Oh well.
    330   1.4       tnn 	 */
    331   1.4       tnn 	if (dt->dt_mon == 11 && dt->dt_day == 31) {
    332   1.4       tnn 		dt->dt_day--;
    333   1.4       tnn 		clock_secs_to_ymdhms(clock_ymdhms_to_secs(dt) + 86400, dt);
    334   1.4       tnn 		rkpmic_todr_settime(ch, dt);
    335   1.4       tnn 	}
    336   1.4       tnn 
    337   1.4       tnn #if 0
    338   1.4       tnn 	device_printf(sc->sc_dev, "%04" PRIu64 "-%02u-%02u (%u) %02u:%02u:%02u\n",
    339   1.4       tnn 	    dt->dt_year, dt->dt_mon, dt->dt_day, dt->dt_wday,
    340   1.4       tnn 	    dt->dt_hour, dt->dt_min, dt->dt_sec);
    341   1.4       tnn #endif
    342   1.4       tnn 
    343   1.4       tnn 	return 0;
    344   1.4       tnn }
    345   1.4       tnn 
    346   1.6  jmcneill static struct clk *
    347   1.6  jmcneill rkpmic_clk_decode(device_t dev, int cc_phandle, const void *data, size_t len)
    348   1.6  jmcneill {
    349   1.6  jmcneill 	struct rkpmic_softc * const sc = device_private(dev);
    350   1.6  jmcneill 
    351   1.6  jmcneill 	if (len != 4)
    352   1.6  jmcneill 		return NULL;
    353   1.6  jmcneill 
    354   1.6  jmcneill 	const u_int id = be32dec(data);
    355   1.6  jmcneill 	if (id >= __arraycount(sc->sc_clk))
    356   1.6  jmcneill 		return NULL;
    357   1.6  jmcneill 
    358   1.6  jmcneill 	return &sc->sc_clk[id].base;
    359   1.6  jmcneill }
    360   1.6  jmcneill 
    361   1.6  jmcneill static const struct fdtbus_clock_controller_func rkpmic_clk_fdt_funcs = {
    362   1.6  jmcneill 	.decode = rkpmic_clk_decode
    363   1.6  jmcneill };
    364   1.6  jmcneill 
    365   1.6  jmcneill static struct clk *
    366   1.6  jmcneill rkpmic_clk_get(void *priv, const char *name)
    367   1.6  jmcneill {
    368   1.6  jmcneill 	struct rkpmic_softc * const sc = priv;
    369   1.6  jmcneill 	u_int n;
    370   1.6  jmcneill 
    371   1.6  jmcneill 	for (n = 0; n < __arraycount(sc->sc_clk); n++) {
    372   1.6  jmcneill 		if (strcmp(name, sc->sc_clk[n].base.name) == 0)
    373   1.6  jmcneill 			return &sc->sc_clk[n].base;
    374   1.6  jmcneill 	}
    375   1.6  jmcneill 
    376   1.6  jmcneill 	return NULL;
    377   1.6  jmcneill }
    378   1.6  jmcneill 
    379   1.6  jmcneill static u_int
    380   1.6  jmcneill rkpmic_clk_get_rate(void *priv, struct clk *clk)
    381   1.6  jmcneill {
    382   1.6  jmcneill 	return 32768;
    383   1.6  jmcneill }
    384   1.6  jmcneill 
    385   1.6  jmcneill static int
    386   1.6  jmcneill rkpmic_clk_enable(void *priv, struct clk *clk)
    387   1.6  jmcneill {
    388   1.6  jmcneill 	struct rkpmic_softc * const sc = priv;
    389   1.6  jmcneill 	uint8_t val;
    390   1.6  jmcneill 
    391   1.6  jmcneill 	if (clk != &sc->sc_clk[1].base)
    392   1.6  jmcneill 		return 0;
    393   1.6  jmcneill 
    394   1.6  jmcneill 	I2C_LOCK(sc);
    395   1.6  jmcneill 	val = I2C_READ(sc, CLK32OUT_REG);
    396   1.6  jmcneill 	val |= CLK32OUT_CLKOUT2_EN;
    397   1.6  jmcneill 	I2C_WRITE(sc, CLK32OUT_REG, val);
    398   1.6  jmcneill 	I2C_UNLOCK(sc);
    399   1.6  jmcneill 
    400   1.6  jmcneill 	return 0;
    401   1.6  jmcneill }
    402   1.6  jmcneill 
    403   1.6  jmcneill static int
    404   1.6  jmcneill rkpmic_clk_disable(void *priv, struct clk *clk)
    405   1.6  jmcneill {
    406   1.6  jmcneill 	struct rkpmic_softc * const sc = priv;
    407   1.6  jmcneill 	uint8_t val;
    408   1.6  jmcneill 
    409   1.6  jmcneill 	if (clk != &sc->sc_clk[1].base)
    410   1.6  jmcneill 		return EIO;
    411   1.6  jmcneill 
    412   1.6  jmcneill 	I2C_LOCK(sc);
    413   1.6  jmcneill 	val = I2C_READ(sc, CLK32OUT_REG);
    414   1.6  jmcneill 	val &= ~CLK32OUT_CLKOUT2_EN;
    415   1.6  jmcneill 	I2C_WRITE(sc, CLK32OUT_REG, val);
    416   1.6  jmcneill 	I2C_UNLOCK(sc);
    417   1.6  jmcneill 
    418   1.6  jmcneill 	return 0;
    419   1.6  jmcneill }
    420   1.6  jmcneill 
    421   1.6  jmcneill static const struct clk_funcs rkpmic_clk_funcs = {
    422   1.6  jmcneill 	.get = rkpmic_clk_get,
    423   1.6  jmcneill 	.get_rate = rkpmic_clk_get_rate,
    424   1.6  jmcneill 	.enable = rkpmic_clk_enable,
    425   1.6  jmcneill 	.disable = rkpmic_clk_disable,
    426   1.6  jmcneill };
    427   1.4       tnn 
    428   1.8  jmcneill static void
    429   1.8  jmcneill rkpmic_power_poweroff(device_t dev)
    430   1.8  jmcneill {
    431   1.8  jmcneill 	struct rkpmic_softc * const sc = device_private(dev);
    432   1.8  jmcneill 	uint8_t val;
    433   1.8  jmcneill 
    434   1.8  jmcneill 	delay(1000000);
    435   1.8  jmcneill 
    436   1.8  jmcneill 	I2C_LOCK(sc);
    437   1.8  jmcneill 	val = I2C_READ(sc, sc->sc_conf->poweroff_reg);
    438   1.8  jmcneill 	val |= sc->sc_conf->poweroff_mask;
    439   1.8  jmcneill 	I2C_WRITE(sc, sc->sc_conf->poweroff_reg, val);
    440   1.8  jmcneill 	I2C_UNLOCK(sc);
    441   1.8  jmcneill }
    442   1.8  jmcneill 
    443   1.8  jmcneill static struct fdtbus_power_controller_func rkpmic_power_funcs = {
    444   1.8  jmcneill 	.poweroff = rkpmic_power_poweroff,
    445   1.8  jmcneill };
    446   1.8  jmcneill 
    447   1.4       tnn static int
    448   1.1  jmcneill rkpmic_match(device_t parent, cfdata_t match, void *aux)
    449   1.1  jmcneill {
    450   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    451   1.1  jmcneill 	int match_result;
    452   1.1  jmcneill 
    453   1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    454   1.1  jmcneill 		return match_result;
    455   1.1  jmcneill 
    456   1.1  jmcneill 	return 0;
    457   1.1  jmcneill }
    458   1.1  jmcneill 
    459   1.1  jmcneill static void
    460   1.1  jmcneill rkpmic_attach(device_t parent, device_t self, void *aux)
    461   1.1  jmcneill {
    462   1.1  jmcneill 	struct rkpmic_softc * const sc = device_private(self);
    463   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    464   1.1  jmcneill 	struct rkreg_attach_args raa;
    465   1.1  jmcneill 	const struct device_compatible_entry *entry;
    466   1.1  jmcneill 	int child, regulators;
    467   1.1  jmcneill 	u_int chipid, n;
    468   1.1  jmcneill 
    469  1.10   thorpej 	entry = iic_compatible_lookup(ia, compat_data);
    470  1.10   thorpej 	KASSERT(entry != NULL);
    471   1.1  jmcneill 
    472   1.1  jmcneill 	sc->sc_dev = self;
    473   1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    474   1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    475  1.17   thorpej 	sc->sc_phandle = devhandle_to_of(device_handle(self));
    476   1.9   thorpej 	sc->sc_conf = entry->data;
    477   1.1  jmcneill 
    478   1.4       tnn 	memset(&sc->sc_todr, 0, sizeof(sc->sc_todr));
    479  1.15   thorpej 	sc->sc_todr.todr_dev = self;
    480   1.4       tnn 	sc->sc_todr.todr_gettime_ymdhms = rkpmic_todr_gettime;
    481   1.4       tnn 	sc->sc_todr.todr_settime_ymdhms = rkpmic_todr_settime;
    482   1.4       tnn 
    483   1.1  jmcneill 	aprint_naive("\n");
    484  1.15   thorpej 	aprint_normal(": %s Power Management and Real Time Clock IC\n",
    485  1.15   thorpej 	    sc->sc_conf->name);
    486   1.1  jmcneill 
    487   1.1  jmcneill 	I2C_LOCK(sc);
    488   1.1  jmcneill 	chipid = I2C_READ(sc, CHIP_NAME_REG) << 8;
    489   1.1  jmcneill 	chipid |= I2C_READ(sc, CHIP_VER_REG);
    490   1.1  jmcneill 	aprint_debug_dev(self, "Chip ID 0x%04x\n", chipid);
    491   1.4       tnn 	I2C_WRITE(sc, RTC_CTRL_REG, 0x0);
    492   1.4       tnn 	I2C_WRITE(sc, RTC_INT_REG, 0);
    493   1.4       tnn 	I2C_WRITE(sc, RTC_COMP_LSB_REG, 0);
    494   1.4       tnn 	I2C_WRITE(sc, RTC_COMP_MSB_REG, 0);
    495   1.1  jmcneill 	I2C_UNLOCK(sc);
    496   1.1  jmcneill 
    497  1.16   thorpej 	todr_attach(&sc->sc_todr);
    498   1.4       tnn 
    499   1.6  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    500   1.6  jmcneill 	sc->sc_clkdom.funcs = &rkpmic_clk_funcs;
    501   1.6  jmcneill 	sc->sc_clkdom.priv = sc;
    502   1.6  jmcneill 
    503   1.6  jmcneill 	sc->sc_clk[0].base.domain = &sc->sc_clkdom;
    504   1.6  jmcneill 	sc->sc_clk[0].base.name = "xin32k";
    505   1.6  jmcneill 	clk_attach(&sc->sc_clk[0].base);
    506   1.6  jmcneill 
    507   1.6  jmcneill 	sc->sc_clk[1].base.domain = &sc->sc_clkdom;
    508   1.6  jmcneill 	sc->sc_clk[1].base.name = "clkout2";
    509   1.6  jmcneill 	clk_attach(&sc->sc_clk[1].base);
    510   1.6  jmcneill 
    511   1.6  jmcneill 	fdtbus_register_clock_controller(self, sc->sc_phandle,
    512   1.6  jmcneill 	    &rkpmic_clk_fdt_funcs);
    513   1.6  jmcneill 
    514   1.8  jmcneill 	if (of_hasprop(sc->sc_phandle, "rockchip,system-power-controller") &&
    515   1.8  jmcneill 	    sc->sc_conf->poweroff_mask != 0)
    516   1.8  jmcneill 		fdtbus_register_power_controller(self, sc->sc_phandle,
    517   1.8  jmcneill 		    &rkpmic_power_funcs);
    518   1.8  jmcneill 
    519   1.1  jmcneill 	regulators = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    520   1.1  jmcneill 	if (regulators < 0)
    521   1.1  jmcneill 		return;
    522   1.1  jmcneill 
    523   1.1  jmcneill 	for (n = 0; n < sc->sc_conf->nctrl; n++) {
    524   1.1  jmcneill 		child = of_find_firstchild_byname(regulators, sc->sc_conf->ctrl[n].name);
    525   1.1  jmcneill 		if (child < 0)
    526   1.1  jmcneill 			continue;
    527   1.1  jmcneill 		raa.reg_ctrl = &sc->sc_conf->ctrl[n];
    528   1.1  jmcneill 		raa.reg_phandle = child;
    529  1.14   thorpej 		config_found(self, &raa, NULL, CFARGS_NONE);
    530   1.1  jmcneill 	}
    531   1.1  jmcneill }
    532   1.1  jmcneill 
    533   1.1  jmcneill static int
    534   1.1  jmcneill rkreg_acquire(device_t dev)
    535   1.1  jmcneill {
    536   1.1  jmcneill 	return 0;
    537   1.1  jmcneill }
    538   1.1  jmcneill 
    539   1.1  jmcneill static void
    540   1.1  jmcneill rkreg_release(device_t dev)
    541   1.1  jmcneill {
    542   1.1  jmcneill }
    543   1.1  jmcneill 
    544   1.1  jmcneill static int
    545   1.1  jmcneill rkreg_enable(device_t dev, bool enable)
    546   1.1  jmcneill {
    547   1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    548   1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    549   1.1  jmcneill 	uint8_t val;
    550   1.1  jmcneill 
    551   1.1  jmcneill 	if (!c->enable_mask)
    552   1.1  jmcneill 		return EINVAL;
    553   1.1  jmcneill 
    554   1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    555   1.2  jmcneill 	if (c->flags & F_ENABLE_WRITE_MASK)
    556   1.2  jmcneill 		val |= c->enable_mask << 4;
    557   1.2  jmcneill 	else
    558   1.2  jmcneill 		val = I2C_READ(sc->sc_pmic, c->enable_reg);
    559   1.1  jmcneill 	if (enable)
    560   1.1  jmcneill 		val |= c->enable_mask;
    561   1.1  jmcneill 	else
    562   1.1  jmcneill 		val &= ~c->enable_mask;
    563   1.1  jmcneill 	I2C_WRITE(sc->sc_pmic, c->enable_reg, val);
    564   1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    565   1.1  jmcneill 
    566   1.1  jmcneill 	return 0;
    567   1.1  jmcneill }
    568   1.1  jmcneill 
    569   1.1  jmcneill static int
    570   1.1  jmcneill rkreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    571   1.1  jmcneill {
    572   1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    573   1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    574   1.1  jmcneill 	uint8_t val;
    575   1.1  jmcneill 	u_int vsel;
    576   1.1  jmcneill 
    577   1.1  jmcneill 	if (!c->vsel_mask)
    578   1.1  jmcneill 		return EINVAL;
    579   1.1  jmcneill 
    580   1.1  jmcneill 	if (min_uvol < c->base)
    581   1.1  jmcneill 		return ERANGE;
    582   1.1  jmcneill 
    583   1.1  jmcneill 	vsel = (min_uvol - c->base) / c->step;
    584   1.1  jmcneill 	if (vsel > __SHIFTOUT_MASK(c->vsel_mask))
    585   1.1  jmcneill 		return ERANGE;
    586   1.1  jmcneill 
    587   1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    588   1.1  jmcneill 	val = I2C_READ(sc->sc_pmic, c->vsel_reg);
    589   1.1  jmcneill 	val &= ~c->vsel_mask;
    590   1.1  jmcneill 	val |= __SHIFTIN(vsel, c->vsel_mask);
    591   1.1  jmcneill 	I2C_WRITE(sc->sc_pmic, c->vsel_reg, val);
    592   1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    593   1.1  jmcneill 
    594   1.1  jmcneill 	return 0;
    595   1.1  jmcneill }
    596   1.1  jmcneill 
    597   1.1  jmcneill static int
    598   1.1  jmcneill rkreg_get_voltage(device_t dev, u_int *puvol)
    599   1.1  jmcneill {
    600   1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    601   1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    602   1.1  jmcneill 	uint8_t val;
    603   1.1  jmcneill 
    604   1.1  jmcneill 	if (!c->vsel_mask)
    605   1.1  jmcneill 		return EINVAL;
    606   1.1  jmcneill 
    607   1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    608   1.1  jmcneill 	val = I2C_READ(sc->sc_pmic, c->vsel_reg);
    609   1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    610   1.1  jmcneill 
    611   1.1  jmcneill 	*puvol = __SHIFTOUT(val, c->vsel_mask) * c->step + c->base;
    612   1.1  jmcneill 
    613   1.1  jmcneill 	return 0;
    614   1.1  jmcneill }
    615   1.1  jmcneill 
    616   1.1  jmcneill static struct fdtbus_regulator_controller_func rkreg_funcs = {
    617   1.1  jmcneill 	.acquire = rkreg_acquire,
    618   1.1  jmcneill 	.release = rkreg_release,
    619   1.1  jmcneill 	.enable = rkreg_enable,
    620   1.1  jmcneill 	.set_voltage = rkreg_set_voltage,
    621   1.1  jmcneill 	.get_voltage = rkreg_get_voltage,
    622   1.1  jmcneill };
    623   1.1  jmcneill 
    624   1.1  jmcneill static int
    625   1.1  jmcneill rkreg_match(device_t parent, cfdata_t match, void *aux)
    626   1.1  jmcneill {
    627   1.1  jmcneill 	return 1;
    628   1.1  jmcneill }
    629   1.1  jmcneill 
    630   1.1  jmcneill static void
    631   1.1  jmcneill rkreg_attach(device_t parent, device_t self, void *aux)
    632   1.1  jmcneill {
    633   1.1  jmcneill 	struct rkreg_softc * const sc = device_private(self);
    634   1.1  jmcneill 	struct rkreg_attach_args *raa = aux;
    635   1.1  jmcneill 	const int phandle = raa->reg_phandle;
    636   1.1  jmcneill 	const char *name;
    637   1.1  jmcneill 
    638   1.1  jmcneill 	sc->sc_dev = self;
    639   1.1  jmcneill 	sc->sc_pmic = device_private(parent);
    640   1.1  jmcneill 	sc->sc_ctrl = raa->reg_ctrl;
    641   1.1  jmcneill 
    642   1.1  jmcneill 	fdtbus_register_regulator_controller(self, phandle,
    643   1.1  jmcneill 	    &rkreg_funcs);
    644   1.1  jmcneill 
    645   1.1  jmcneill 	aprint_naive("\n");
    646   1.1  jmcneill 	name = fdtbus_get_string(phandle, "regulator-name");
    647   1.1  jmcneill 	if (!name)
    648   1.1  jmcneill 		name = fdtbus_get_string(phandle, "name");
    649   1.1  jmcneill 	aprint_normal(": %s\n", name);
    650   1.1  jmcneill }
    651   1.1  jmcneill 
    652   1.1  jmcneill CFATTACH_DECL_NEW(rkpmic, sizeof(struct rkpmic_softc),
    653   1.1  jmcneill     rkpmic_match, rkpmic_attach, NULL, NULL);
    654   1.1  jmcneill 
    655   1.1  jmcneill CFATTACH_DECL_NEW(rkreg, sizeof(struct rkreg_softc),
    656   1.1  jmcneill     rkreg_match, rkreg_attach, NULL, NULL);
    657