Home | History | Annotate | Line # | Download | only in i2c
sy8106a.c revision 1.3
      1 /* $NetBSD: sy8106a.c,v 1.3 2018/06/18 17:07:07 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: sy8106a.c,v 1.3 2018/06/18 17:07:07 thorpej Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/conf.h>
     37 #include <sys/bus.h>
     38 #include <sys/kmem.h>
     39 
     40 #include <dev/i2c/i2cvar.h>
     41 
     42 #include <dev/fdt/fdtvar.h>
     43 
     44 #define	VOUT1_SEL		0x01
     45 #define	 SEL_GO			__BIT(7)
     46 #define	 SEL_VOLTAGE		__BITS(6,0)
     47 #define	  SEL_VOLTAGE_BASE	680000	/* uV */
     48 #define	  SEL_VOLTAGE_STEP	10000	/* uV */
     49 #define	VOUT_COM		0x02
     50 #define	 COM_DISABLE		__BIT(0)
     51 #define	SYS_STATUS		0x06
     52 
     53 struct sy8106a_softc {
     54 	device_t	sc_dev;
     55 	i2c_tag_t	sc_i2c;
     56 	i2c_addr_t	sc_addr;
     57 	int		sc_phandle;
     58 
     59 	u_int		sc_ramp_delay;
     60 };
     61 
     62 static const char * compatible[] = {
     63 	"silergy,sy8106a",
     64 	NULL
     65 };
     66 
     67 static const struct device_compatible_entry sy8106a_compat_data[] = {
     68 	DEVICE_COMPAT_ENTRY(compatible),
     69 	DEVICE_COMPAT_TERMINATOR
     70 };
     71 
     72 static uint8_t
     73 sy8106a_read(struct sy8106a_softc *sc, uint8_t reg, int flags)
     74 {
     75 	uint8_t val = 0;
     76 	int error;
     77 
     78 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
     79 	    &reg, 1, &val, 1, flags);
     80 	if (error != 0)
     81 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
     82 
     83 	return val;
     84 }
     85 
     86 static void
     87 sy8106a_write(struct sy8106a_softc *sc, uint8_t reg, uint8_t val, int flags)
     88 {
     89 	uint8_t buf[2];
     90 	int error;
     91 
     92 	buf[0] = reg;
     93 	buf[1] = val;
     94 
     95 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
     96 	    NULL, 0, buf, 2, flags);
     97 	if (error != 0)
     98 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
     99 }
    100 
    101 #define	I2C_READ(sc, reg)	sy8106a_read((sc), (reg), I2C_F_POLL)
    102 #define	I2C_WRITE(sc, reg, val)	sy8106a_write((sc), (reg), (val), I2C_F_POLL)
    103 #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
    104 #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
    105 
    106 static int
    107 sy8106a_acquire(device_t dev)
    108 {
    109 	return 0;
    110 }
    111 
    112 static void
    113 sy8106a_release(device_t dev)
    114 {
    115 }
    116 
    117 static int
    118 sy8106a_enable(device_t dev, bool enable)
    119 {
    120 	struct sy8106a_softc * const sc = device_private(dev);
    121 	uint8_t val;
    122 
    123 	I2C_LOCK(sc);
    124 	val = I2C_READ(sc, VOUT_COM);
    125 	if (enable)
    126 		val &= ~COM_DISABLE;
    127 	else
    128 		val |= COM_DISABLE;
    129 	I2C_WRITE(sc, VOUT_COM, val);
    130 	I2C_UNLOCK(sc);
    131 
    132 	if (sc->sc_ramp_delay)
    133 		delay(sc->sc_ramp_delay);
    134 
    135 	return 0;
    136 }
    137 
    138 static int
    139 sy8106a_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    140 {
    141 	struct sy8106a_softc * const sc = device_private(dev);
    142 	uint8_t val, oval;
    143 	u_int cur_uvol;
    144 
    145 	I2C_LOCK(sc);
    146 
    147 	/* Get current voltage */
    148 	oval = I2C_READ(sc, VOUT1_SEL);
    149 	cur_uvol = __SHIFTOUT(oval, SEL_VOLTAGE) * SEL_VOLTAGE_STEP +
    150 	    SEL_VOLTAGE_BASE;
    151 
    152 	/* Set new voltage */
    153 	val = SEL_GO | ((min_uvol - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP);
    154 	I2C_WRITE(sc, VOUT1_SEL, val);
    155 
    156 	I2C_UNLOCK(sc);
    157 
    158 	/* Time to delay is based on the number of voltage steps */
    159 	if (sc->sc_ramp_delay)
    160 		delay((abs(cur_uvol - min_uvol) / SEL_VOLTAGE_STEP) * sc->sc_ramp_delay);
    161 
    162 	return 0;
    163 }
    164 
    165 static int
    166 sy8106a_get_voltage(device_t dev, u_int *puvol)
    167 {
    168 	struct sy8106a_softc * const sc = device_private(dev);
    169 	uint8_t val;
    170 
    171 	I2C_LOCK(sc);
    172 	val = I2C_READ(sc, VOUT1_SEL);
    173 	I2C_UNLOCK(sc);
    174 
    175 	*puvol = __SHIFTOUT(val, SEL_VOLTAGE) * SEL_VOLTAGE_STEP +
    176 	    SEL_VOLTAGE_BASE;
    177 
    178 	return 0;
    179 }
    180 
    181 static struct fdtbus_regulator_controller_func sy8106a_funcs = {
    182 	.acquire = sy8106a_acquire,
    183 	.release = sy8106a_release,
    184 	.enable = sy8106a_enable,
    185 	.set_voltage = sy8106a_set_voltage,
    186 	.get_voltage = sy8106a_get_voltage,
    187 };
    188 
    189 static int
    190 sy8106a_match(device_t parent, cfdata_t match, void *aux)
    191 {
    192 	struct i2c_attach_args *ia = aux;
    193 	int match_result;
    194 
    195 	if (iic_use_direct_match(ia, match, sy8106a_compat_data, &match_result))
    196 		return match_result;
    197 
    198 	return 0;
    199 }
    200 
    201 static void
    202 sy8106a_attach(device_t parent, device_t self, void *aux)
    203 {
    204 	struct sy8106a_softc * const sc = device_private(self);
    205 	struct i2c_attach_args *ia = aux;
    206 
    207 	sc->sc_dev = self;
    208 	sc->sc_i2c = ia->ia_tag;
    209 	sc->sc_addr = ia->ia_addr;
    210 	sc->sc_phandle = ia->ia_cookie;
    211 
    212 	aprint_naive("\n");
    213 	aprint_normal(": Silergy SY8106A regulator\n");
    214 
    215 	of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay",
    216 	    &sc->sc_ramp_delay);
    217 
    218 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    219 	    &sy8106a_funcs);
    220 }
    221 
    222 CFATTACH_DECL_NEW(sy8106a, sizeof(struct sy8106a_softc),
    223     sy8106a_match, sy8106a_attach, NULL, NULL);
    224