Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c Add a callback to change the regulator outputs.



details:   https://anonhg.NetBSD.org/src/rev/fd1bc2489c77
branches:  trunk
changeset: 330783:fd1bc2489c77
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Sun Jul 20 23:01:22 2014 +0000

description:
Add a callback to change the regulator outputs.
Tested on beaglebone.

diffstat:

 sys/dev/i2c/tps65217pmic.c    |  89 ++++++++++++++++++++++++++++++++++++++----
 sys/dev/i2c/tps65217pmicreg.h |   8 +++-
 sys/dev/i2c/tps65217pmicvar.h |  38 ++++++++++++++++++
 3 files changed, 125 insertions(+), 10 deletions(-)

diffs (205 lines):

diff -r f8253ded3b31 -r fd1bc2489c77 sys/dev/i2c/tps65217pmic.c
--- a/sys/dev/i2c/tps65217pmic.c        Sun Jul 20 23:00:49 2014 +0000
+++ b/sys/dev/i2c/tps65217pmic.c        Sun Jul 20 23:01:22 2014 +0000
@@ -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 @@
 #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 @@
        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);
+
+       iic_release_bus(sc->sc_tag, I2C_F_POLL);
+}
 
-       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);
+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 @@
        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;
+}
diff -r f8253ded3b31 -r fd1bc2489c77 sys/dev/i2c/tps65217pmicreg.h
--- a/sys/dev/i2c/tps65217pmicreg.h     Sun Jul 20 23:00:49 2014 +0000
+++ b/sys/dev/i2c/tps65217pmicreg.h     Sun Jul 20 23:01:22 2014 +0000
@@ -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
diff -r f8253ded3b31 -r fd1bc2489c77 sys/dev/i2c/tps65217pmicvar.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/tps65217pmicvar.h     Sun Jul 20 23:01:22 2014 +0000
@@ -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_ */
+



Home | Main Index | Thread Index | Old Index