Module Name: src Committed By: bouyer Date: Sun Jul 20 23:01:22 UTC 2014
Modified Files: src/sys/dev/i2c: tps65217pmic.c tps65217pmicreg.h Added Files: src/sys/dev/i2c: tps65217pmicvar.h Log Message: Add a callback to change the regulator outputs. Tested on beaglebone. To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/sys/dev/i2c/tps65217pmic.c cvs rdiff -u -r1.6 -r1.7 src/sys/dev/i2c/tps65217pmicreg.h cvs rdiff -u -r0 -r1.1 src/sys/dev/i2c/tps65217pmicvar.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/i2c/tps65217pmic.c diff -u src/sys/dev/i2c/tps65217pmic.c:1.9 src/sys/dev/i2c/tps65217pmic.c:1.10 --- src/sys/dev/i2c/tps65217pmic.c:1.9 Wed Jan 8 16:49:48 2014 +++ src/sys/dev/i2c/tps65217pmic.c Sun Jul 20 23:01:22 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: tps65217pmic.c,v 1.9 2014/01/08 16:49:48 jakllsch Exp $ */ +/* $NetBSD: tps65217pmic.c,v 1.10 2014/07/20 23:01:22 bouyer Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: tps65217pmic.c,v 1.9 2014/01/08 16:49:48 jakllsch Exp $"); +__KERNEL_RCSID(0, "$NetBSD: tps65217pmic.c,v 1.10 2014/07/20 23:01:22 bouyer Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -49,6 +49,7 @@ __KERNEL_RCSID(0, "$NetBSD: tps65217pmic #include <dev/sysmon/sysmonvar.h> #include <dev/i2c/tps65217pmicreg.h> +#include <dev/i2c/tps65217pmicvar.h> #define NTPS_REG 7 #define SNUM_REGS NTPS_REG-1 @@ -608,26 +609,49 @@ tps65217pmic_reg_read(struct tps65217pmi return rv; } +static void +tps65217pmic_reg_write_unlocked(struct tps65217pmic_softc *sc, + uint8_t reg, uint8_t data) +{ + uint8_t wbuf[2]; + + wbuf[0] = reg; + wbuf[1] = data; + + if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, NULL, 0, + wbuf, 2, I2C_F_POLL)) { + aprint_error_dev(sc->sc_dev, "cannot execute I2C write\n"); + } +} + static void __unused tps65217pmic_reg_write(struct tps65217pmic_softc *sc, uint8_t reg, uint8_t data) { - uint8_t wbuf[2]; if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL) != 0) { aprint_error_dev(sc->sc_dev, "cannot acquire bus for write\n"); return; } - wbuf[0] = reg; - wbuf[1] = data; + tps65217pmic_reg_write_unlocked(sc, reg, data); - if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, wbuf, - 2, NULL, 0, I2C_F_POLL)) { - aprint_error_dev(sc->sc_dev, "cannot execute I2C write\n"); - iic_release_bus(sc->sc_tag, I2C_F_POLL); + iic_release_bus(sc->sc_tag, I2C_F_POLL); +} + +static void +tps65217pmic_reg_write_l2(struct tps65217pmic_softc *sc, + uint8_t reg, uint8_t data) +{ + uint8_t regpw = reg ^ TPS65217PMIC_PASSWORD_XOR; + if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL) != 0) { + aprint_error_dev(sc->sc_dev, "cannot acquire bus for write\n"); return; } + tps65217pmic_reg_write_unlocked(sc, TPS65217PMIC_PASSWORD, regpw); + tps65217pmic_reg_write_unlocked(sc, reg, data); + tps65217pmic_reg_write_unlocked(sc, TPS65217PMIC_PASSWORD, regpw); + tps65217pmic_reg_write_unlocked(sc, reg, data); iic_release_bus(sc->sc_tag, I2C_F_POLL); } @@ -702,3 +726,50 @@ tps65217pmic_envsys_refresh(struct sysmo mutex_exit(&sc->sc_lock); } +int +tps65217pmic_set_volt(device_t self, const char *name, int mvolt) +{ + int i; + struct tps65217pmic_softc *sc = device_private(self); + struct tps_reg_param *regulator = NULL; + uint8_t val; + + for (i = 0; i < __arraycount(tps_regulators); i++) { + if (strcmp(name, tps_regulators[i].name) == 0) { + regulator = &tps_regulators[i]; + break; + } + } + if (regulator == NULL) + return EINVAL; + + if (regulator->voltage_min > mvolt || regulator->voltage_max < mvolt) + return EINVAL; + + if (!regulator->is_enabled) + return EINVAL; + + if (regulator->is_tracking) + return EINVAL; + + if (regulator->is_xadj) + return EINVAL; + + /* find closest voltage entry */ + for (i = 0; i < regulator->nvoltages; i++) { + if (mvolt <= regulator->voltages[i]) { + break; + } + } + KASSERT(i < regulator->nvoltages); + tps65217pmic_reg_write_l2(sc, regulator->defreg_num, i); + + val = tps65217pmic_reg_read(sc, TPS65217PMIC_DEFSLEW); + val |= TPS65217PMIC_DEFSLEW_GO; + tps65217pmic_reg_write_l2(sc, TPS65217PMIC_DEFSLEW, val); + + while (val & TPS65217PMIC_DEFSLEW_GO) { + val = tps65217pmic_reg_read(sc, TPS65217PMIC_DEFSLEW); + } + return 0; +} Index: src/sys/dev/i2c/tps65217pmicreg.h diff -u src/sys/dev/i2c/tps65217pmicreg.h:1.6 src/sys/dev/i2c/tps65217pmicreg.h:1.7 --- src/sys/dev/i2c/tps65217pmicreg.h:1.6 Sun Aug 4 00:24:28 2013 +++ src/sys/dev/i2c/tps65217pmicreg.h Sun Jul 20 23:01:22 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: tps65217pmicreg.h,v 1.6 2013/08/04 00:24:28 rkujawa Exp $ */ +/* $NetBSD: tps65217pmicreg.h,v 1.7 2014/07/20 23:01:22 bouyer Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -83,6 +83,9 @@ #define TPS65217PMIC_STATUS_USBPWR __BIT(2) #define TPS65217PMIC_STATUS_ACPWR __BIT(3) +#define TPS65217PMIC_PASSWORD 0x0B +#define TPS65217PMIC_PASSWORD_XOR 0x7d + #define TPS65217PMIC_PGOOD 0x0C #define TPS65217PMIC_PGOOD_LDO3PG __BIT(0) #define TPS65217PMIC_PGOOD_LDO4PG __BIT(1) @@ -92,6 +95,9 @@ #define TPS65217PMIC_PGOOD_LDO1PG __BIT(5) #define TPS65217PMIC_PGOOD_LDO2PG __BIT(6) +#define TPS65217PMIC_DEFSLEW 0x11 +#define TPS65217PMIC_DEFSLEW_GO __BIT(7) + #define TPS65217PMIC_DEFLDO1 0x12 #define TPS65217PMIC_DEFLDO2 0x13 #define TPS65217PMIC_DEFLDO3 0x14 Added files: Index: src/sys/dev/i2c/tps65217pmicvar.h diff -u /dev/null src/sys/dev/i2c/tps65217pmicvar.h:1.1 --- /dev/null Sun Jul 20 23:01:22 2014 +++ src/sys/dev/i2c/tps65217pmicvar.h Sun Jul 20 23:01:22 2014 @@ -0,0 +1,38 @@ +/* $NetBSD: tps65217pmicvar.h,v 1.1 2014/07/20 23:01:22 bouyer Exp $ */ + +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Radoslaw Kujawa. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*#include <sys/cdefs.h>*/ + +#ifndef _TPS65217PMICVAR_H_ +#define _TPS65217PMICVAR_H_ +int tps65217pmic_set_volt(device_t, const char *, int); +#endif /* _TPS65217PMICVAR_H_ */ +