Home | History | Annotate | Line # | Download | only in i2c
rkpmic.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: rkpmic.c,v 1.1 2018/09/02 01:16:58 jmcneill 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: rkpmic.c,v 1.1 2018/09/02 01:16:58 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/fdt/fdtvar.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #define	CHIP_NAME_REG		0x17
     45  1.1  jmcneill #define	CHIP_VER_REG		0x18
     46  1.1  jmcneill 
     47  1.1  jmcneill struct rkpmic_ctrl {
     48  1.1  jmcneill 	const char *	name;
     49  1.1  jmcneill 	uint8_t		enable_reg;
     50  1.1  jmcneill 	uint8_t		enable_mask;
     51  1.1  jmcneill 	uint8_t		vsel_reg;
     52  1.1  jmcneill 	uint8_t		vsel_mask;
     53  1.1  jmcneill 	u_int		base;
     54  1.1  jmcneill 	u_int		step;
     55  1.1  jmcneill };
     56  1.1  jmcneill 
     57  1.1  jmcneill struct rkpmic_config {
     58  1.1  jmcneill 	const char *	name;
     59  1.1  jmcneill 	const struct rkpmic_ctrl *ctrl;
     60  1.1  jmcneill 	u_int		nctrl;
     61  1.1  jmcneill };
     62  1.1  jmcneill 
     63  1.1  jmcneill static const struct rkpmic_ctrl rk808_ctrls[] = {
     64  1.1  jmcneill 	/* DCDC */
     65  1.1  jmcneill 	{ .name = "DCDC_REG1",
     66  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(0),
     67  1.1  jmcneill 	  .vsel_reg = 0x2f,	.vsel_mask = __BITS(5,0),
     68  1.1  jmcneill 	  .base = 712500,	.step = 12500 },
     69  1.1  jmcneill 	{ .name = "DCDC_REG2",
     70  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(1),
     71  1.1  jmcneill 	  .vsel_reg = 0x33,	.vsel_mask = __BITS(5,0),
     72  1.1  jmcneill 	  .base = 712500,	.step = 12500 },
     73  1.1  jmcneill 	{ .name = "DCDC_REG3",
     74  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(2) },
     75  1.1  jmcneill 	{ .name = "DCDC_REG4",
     76  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(3),
     77  1.1  jmcneill 	  .vsel_reg = 0x38,	.vsel_mask = __BITS(3,0),
     78  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
     79  1.1  jmcneill 
     80  1.1  jmcneill 	/* LDO */
     81  1.1  jmcneill 	{ .name = "LDO_REG1",
     82  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
     83  1.1  jmcneill 	  .vsel_reg = 0x3b,	.vsel_mask = __BITS(4,0),
     84  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
     85  1.1  jmcneill 	{ .name = "LDO_REG2",
     86  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
     87  1.1  jmcneill 	  .vsel_reg = 0x3d,	.vsel_mask = __BITS(4,0),
     88  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
     89  1.1  jmcneill 	{ .name = "LDO_REG3",
     90  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
     91  1.1  jmcneill 	  .vsel_reg = 0x3f,	.vsel_mask = __BITS(3,0),
     92  1.1  jmcneill 	  .base = 800000,	.step = 100000 },
     93  1.1  jmcneill 	{ .name = "LDO_REG4",
     94  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
     95  1.1  jmcneill 	  .vsel_reg = 0x41,	.vsel_mask = __BITS(4,0),
     96  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
     97  1.1  jmcneill 	{ .name = "LDO_REG5",
     98  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
     99  1.1  jmcneill 	  .vsel_reg = 0x43,	.vsel_mask = __BITS(4,0),
    100  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    101  1.1  jmcneill 	{ .name = "LDO_REG6",
    102  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
    103  1.1  jmcneill 	  .vsel_reg = 0x45,	.vsel_mask = __BITS(4,0),
    104  1.1  jmcneill 	  .base = 800000,	.step = 100000 },
    105  1.1  jmcneill 	{ .name = "LDO_REG7",
    106  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
    107  1.1  jmcneill 	  .vsel_reg = 0x47,	.vsel_mask = __BITS(4,0),
    108  1.1  jmcneill 	  .base = 800000,	.step = 100000 },
    109  1.1  jmcneill 	{ .name = "LDO_REG8",
    110  1.1  jmcneill 	  .enable_reg = 0x24,	.enable_mask = __BIT(0),
    111  1.1  jmcneill 	  .vsel_reg = 0x49,	.vsel_mask = __BITS(4,0),
    112  1.1  jmcneill 	  .base = 1800000,	.step = 100000 },
    113  1.1  jmcneill 
    114  1.1  jmcneill 	/* SWITCH */
    115  1.1  jmcneill 	{ .name = "SWITCH_REG1",
    116  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(5) },
    117  1.1  jmcneill 	{ .name = "SWITCH_REG2",
    118  1.1  jmcneill 	  .enable_reg = 0x23,	.enable_mask = __BIT(6) },
    119  1.1  jmcneill };
    120  1.1  jmcneill 
    121  1.1  jmcneill static const struct rkpmic_config rk808_config = {
    122  1.1  jmcneill 	.name = "RK808",
    123  1.1  jmcneill 	.ctrl = rk808_ctrls,
    124  1.1  jmcneill 	.nctrl = __arraycount(rk808_ctrls),
    125  1.1  jmcneill };
    126  1.1  jmcneill 
    127  1.1  jmcneill struct rkpmic_softc {
    128  1.1  jmcneill 	device_t	sc_dev;
    129  1.1  jmcneill 	i2c_tag_t	sc_i2c;
    130  1.1  jmcneill 	i2c_addr_t	sc_addr;
    131  1.1  jmcneill 	int		sc_phandle;
    132  1.1  jmcneill 
    133  1.1  jmcneill 	struct rkpmic_config *sc_conf;
    134  1.1  jmcneill };
    135  1.1  jmcneill 
    136  1.1  jmcneill struct rkreg_softc {
    137  1.1  jmcneill 	device_t	sc_dev;
    138  1.1  jmcneill 	struct rkpmic_softc *sc_pmic;
    139  1.1  jmcneill 	const struct rkpmic_ctrl *sc_ctrl;
    140  1.1  jmcneill };
    141  1.1  jmcneill 
    142  1.1  jmcneill struct rkreg_attach_args {
    143  1.1  jmcneill 	const struct rkpmic_ctrl *reg_ctrl;
    144  1.1  jmcneill 	int		reg_phandle;
    145  1.1  jmcneill };
    146  1.1  jmcneill 
    147  1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
    148  1.1  jmcneill 	{ "rockchip,rk808",	(uintptr_t)&rk808_config },
    149  1.1  jmcneill 	{ NULL }
    150  1.1  jmcneill };
    151  1.1  jmcneill 
    152  1.1  jmcneill static uint8_t
    153  1.1  jmcneill rkpmic_read(struct rkpmic_softc *sc, uint8_t reg, int flags)
    154  1.1  jmcneill {
    155  1.1  jmcneill 	uint8_t val = 0;
    156  1.1  jmcneill 	int error;
    157  1.1  jmcneill 
    158  1.1  jmcneill 	error = iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, flags);
    159  1.1  jmcneill 	if (error != 0)
    160  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    161  1.1  jmcneill 
    162  1.1  jmcneill 	return val;
    163  1.1  jmcneill }
    164  1.1  jmcneill 
    165  1.1  jmcneill static void
    166  1.1  jmcneill rkpmic_write(struct rkpmic_softc *sc, uint8_t reg, uint8_t val, int flags)
    167  1.1  jmcneill {
    168  1.1  jmcneill 	int error;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	error = iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, flags);
    171  1.1  jmcneill 	if (error != 0)
    172  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    173  1.1  jmcneill }
    174  1.1  jmcneill 
    175  1.1  jmcneill #define	I2C_READ(sc, reg)	rkpmic_read((sc), (reg), I2C_F_POLL)
    176  1.1  jmcneill #define	I2C_WRITE(sc, reg, val)	rkpmic_write((sc), (reg), (val), I2C_F_POLL)
    177  1.1  jmcneill #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
    178  1.1  jmcneill #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
    179  1.1  jmcneill 
    180  1.1  jmcneill static int
    181  1.1  jmcneill rkpmic_match(device_t parent, cfdata_t match, void *aux)
    182  1.1  jmcneill {
    183  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    184  1.1  jmcneill 	int match_result;
    185  1.1  jmcneill 
    186  1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    187  1.1  jmcneill 		return match_result;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	return 0;
    190  1.1  jmcneill }
    191  1.1  jmcneill 
    192  1.1  jmcneill static void
    193  1.1  jmcneill rkpmic_attach(device_t parent, device_t self, void *aux)
    194  1.1  jmcneill {
    195  1.1  jmcneill 	struct rkpmic_softc * const sc = device_private(self);
    196  1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    197  1.1  jmcneill 	struct rkreg_attach_args raa;
    198  1.1  jmcneill 	const struct device_compatible_entry *entry;
    199  1.1  jmcneill 	int child, regulators;
    200  1.1  jmcneill 	u_int chipid, n;
    201  1.1  jmcneill 
    202  1.1  jmcneill 	iic_compatible_match(ia, compat_data, &entry);
    203  1.1  jmcneill 
    204  1.1  jmcneill 	sc->sc_dev = self;
    205  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    206  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    207  1.1  jmcneill 	sc->sc_phandle = ia->ia_cookie;
    208  1.1  jmcneill 	sc->sc_conf = (void *)entry->data;
    209  1.1  jmcneill 
    210  1.1  jmcneill 	aprint_naive("\n");
    211  1.1  jmcneill 	aprint_normal(": %s Power Management IC\n", sc->sc_conf->name);
    212  1.1  jmcneill 
    213  1.1  jmcneill 	I2C_LOCK(sc);
    214  1.1  jmcneill 	chipid = I2C_READ(sc, CHIP_NAME_REG) << 8;
    215  1.1  jmcneill 	chipid |= I2C_READ(sc, CHIP_VER_REG);
    216  1.1  jmcneill 	aprint_debug_dev(self, "Chip ID 0x%04x\n", chipid);
    217  1.1  jmcneill 	I2C_UNLOCK(sc);
    218  1.1  jmcneill 
    219  1.1  jmcneill 	regulators = of_find_firstchild_byname(sc->sc_phandle, "regulators");
    220  1.1  jmcneill 	if (regulators < 0)
    221  1.1  jmcneill 		return;
    222  1.1  jmcneill 
    223  1.1  jmcneill 	for (n = 0; n < sc->sc_conf->nctrl; n++) {
    224  1.1  jmcneill 		child = of_find_firstchild_byname(regulators, sc->sc_conf->ctrl[n].name);
    225  1.1  jmcneill 		if (child < 0)
    226  1.1  jmcneill 			continue;
    227  1.1  jmcneill 		raa.reg_ctrl = &sc->sc_conf->ctrl[n];
    228  1.1  jmcneill 		raa.reg_phandle = child;
    229  1.1  jmcneill 		config_found(self, &raa, NULL);
    230  1.1  jmcneill 	}
    231  1.1  jmcneill }
    232  1.1  jmcneill 
    233  1.1  jmcneill static int
    234  1.1  jmcneill rkreg_acquire(device_t dev)
    235  1.1  jmcneill {
    236  1.1  jmcneill 	return 0;
    237  1.1  jmcneill }
    238  1.1  jmcneill 
    239  1.1  jmcneill static void
    240  1.1  jmcneill rkreg_release(device_t dev)
    241  1.1  jmcneill {
    242  1.1  jmcneill }
    243  1.1  jmcneill 
    244  1.1  jmcneill static int
    245  1.1  jmcneill rkreg_enable(device_t dev, bool enable)
    246  1.1  jmcneill {
    247  1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    248  1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    249  1.1  jmcneill 	uint8_t val;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	if (!c->enable_mask)
    252  1.1  jmcneill 		return EINVAL;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    255  1.1  jmcneill 	val = I2C_READ(sc->sc_pmic, c->enable_reg);
    256  1.1  jmcneill 	if (enable)
    257  1.1  jmcneill 		val |= c->enable_mask;
    258  1.1  jmcneill 	else
    259  1.1  jmcneill 		val &= ~c->enable_mask;
    260  1.1  jmcneill 	I2C_WRITE(sc->sc_pmic, c->enable_reg, val);
    261  1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    262  1.1  jmcneill 
    263  1.1  jmcneill 	return 0;
    264  1.1  jmcneill }
    265  1.1  jmcneill 
    266  1.1  jmcneill static int
    267  1.1  jmcneill rkreg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    268  1.1  jmcneill {
    269  1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    270  1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    271  1.1  jmcneill 	uint8_t val;
    272  1.1  jmcneill 	u_int vsel;
    273  1.1  jmcneill 
    274  1.1  jmcneill 	if (!c->vsel_mask)
    275  1.1  jmcneill 		return EINVAL;
    276  1.1  jmcneill 
    277  1.1  jmcneill 	if (min_uvol < c->base)
    278  1.1  jmcneill 		return ERANGE;
    279  1.1  jmcneill 
    280  1.1  jmcneill 	vsel = (min_uvol - c->base) / c->step;
    281  1.1  jmcneill 	if (vsel > __SHIFTOUT_MASK(c->vsel_mask))
    282  1.1  jmcneill 		return ERANGE;
    283  1.1  jmcneill 
    284  1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    285  1.1  jmcneill 	val = I2C_READ(sc->sc_pmic, c->vsel_reg);
    286  1.1  jmcneill 	val &= ~c->vsel_mask;
    287  1.1  jmcneill 	val |= __SHIFTIN(vsel, c->vsel_mask);
    288  1.1  jmcneill 	I2C_WRITE(sc->sc_pmic, c->vsel_reg, val);
    289  1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    290  1.1  jmcneill 
    291  1.1  jmcneill 	return 0;
    292  1.1  jmcneill }
    293  1.1  jmcneill 
    294  1.1  jmcneill static int
    295  1.1  jmcneill rkreg_get_voltage(device_t dev, u_int *puvol)
    296  1.1  jmcneill {
    297  1.1  jmcneill 	struct rkreg_softc * const sc = device_private(dev);
    298  1.1  jmcneill 	const struct rkpmic_ctrl *c = sc->sc_ctrl;
    299  1.1  jmcneill 	uint8_t val;
    300  1.1  jmcneill 
    301  1.1  jmcneill 	if (!c->vsel_mask)
    302  1.1  jmcneill 		return EINVAL;
    303  1.1  jmcneill 
    304  1.1  jmcneill 	I2C_LOCK(sc->sc_pmic);
    305  1.1  jmcneill 	val = I2C_READ(sc->sc_pmic, c->vsel_reg);
    306  1.1  jmcneill 	I2C_UNLOCK(sc->sc_pmic);
    307  1.1  jmcneill 
    308  1.1  jmcneill 	*puvol = __SHIFTOUT(val, c->vsel_mask) * c->step + c->base;
    309  1.1  jmcneill 
    310  1.1  jmcneill 	return 0;
    311  1.1  jmcneill }
    312  1.1  jmcneill 
    313  1.1  jmcneill static struct fdtbus_regulator_controller_func rkreg_funcs = {
    314  1.1  jmcneill 	.acquire = rkreg_acquire,
    315  1.1  jmcneill 	.release = rkreg_release,
    316  1.1  jmcneill 	.enable = rkreg_enable,
    317  1.1  jmcneill 	.set_voltage = rkreg_set_voltage,
    318  1.1  jmcneill 	.get_voltage = rkreg_get_voltage,
    319  1.1  jmcneill };
    320  1.1  jmcneill 
    321  1.1  jmcneill static int
    322  1.1  jmcneill rkreg_match(device_t parent, cfdata_t match, void *aux)
    323  1.1  jmcneill {
    324  1.1  jmcneill 	return 1;
    325  1.1  jmcneill }
    326  1.1  jmcneill 
    327  1.1  jmcneill static void
    328  1.1  jmcneill rkreg_attach(device_t parent, device_t self, void *aux)
    329  1.1  jmcneill {
    330  1.1  jmcneill 	struct rkreg_softc * const sc = device_private(self);
    331  1.1  jmcneill 	struct rkreg_attach_args *raa = aux;
    332  1.1  jmcneill 	const int phandle = raa->reg_phandle;
    333  1.1  jmcneill 	const char *name;
    334  1.1  jmcneill 
    335  1.1  jmcneill 	sc->sc_dev = self;
    336  1.1  jmcneill 	sc->sc_pmic = device_private(parent);
    337  1.1  jmcneill 	sc->sc_ctrl = raa->reg_ctrl;
    338  1.1  jmcneill 
    339  1.1  jmcneill 	fdtbus_register_regulator_controller(self, phandle,
    340  1.1  jmcneill 	    &rkreg_funcs);
    341  1.1  jmcneill 
    342  1.1  jmcneill 	aprint_naive("\n");
    343  1.1  jmcneill 	name = fdtbus_get_string(phandle, "regulator-name");
    344  1.1  jmcneill 	if (!name)
    345  1.1  jmcneill 		name = fdtbus_get_string(phandle, "name");
    346  1.1  jmcneill 	aprint_normal(": %s\n", name);
    347  1.1  jmcneill }
    348  1.1  jmcneill 
    349  1.1  jmcneill CFATTACH_DECL_NEW(rkpmic, sizeof(struct rkpmic_softc),
    350  1.1  jmcneill     rkpmic_match, rkpmic_attach, NULL, NULL);
    351  1.1  jmcneill 
    352  1.1  jmcneill CFATTACH_DECL_NEW(rkreg, sizeof(struct rkreg_softc),
    353  1.1  jmcneill     rkreg_match, rkreg_attach, NULL, NULL);
    354