Home | History | Annotate | Line # | Download | only in i2c
fan53555.c revision 1.2.4.2
      1  1.2.4.2  pgoyette /* $NetBSD: fan53555.c,v 1.2.4.2 2018/09/06 06:55:49 pgoyette Exp $ */
      2  1.2.4.2  pgoyette 
      3  1.2.4.2  pgoyette /*-
      4  1.2.4.2  pgoyette  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.2.4.2  pgoyette  * All rights reserved.
      6  1.2.4.2  pgoyette  *
      7  1.2.4.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.2.4.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.2.4.2  pgoyette  * are met:
     10  1.2.4.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.2.4.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.2.4.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.4.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.4.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.2.4.2  pgoyette  *
     16  1.2.4.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.2.4.2  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.4.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.4.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.2.4.2  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.4.2  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.4.2  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.4.2  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.4.2  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.4.2  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.4.2  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.4.2  pgoyette  */
     28  1.2.4.2  pgoyette 
     29  1.2.4.2  pgoyette #include <sys/cdefs.h>
     30  1.2.4.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.2.4.2 2018/09/06 06:55:49 pgoyette Exp $");
     31  1.2.4.2  pgoyette 
     32  1.2.4.2  pgoyette #include <sys/param.h>
     33  1.2.4.2  pgoyette #include <sys/systm.h>
     34  1.2.4.2  pgoyette #include <sys/kernel.h>
     35  1.2.4.2  pgoyette #include <sys/device.h>
     36  1.2.4.2  pgoyette #include <sys/conf.h>
     37  1.2.4.2  pgoyette #include <sys/bus.h>
     38  1.2.4.2  pgoyette #include <sys/kmem.h>
     39  1.2.4.2  pgoyette 
     40  1.2.4.2  pgoyette #include <dev/i2c/i2cvar.h>
     41  1.2.4.2  pgoyette 
     42  1.2.4.2  pgoyette #include <dev/fdt/fdtvar.h>
     43  1.2.4.2  pgoyette 
     44  1.2.4.2  pgoyette #define	VSEL0_REG		0x00
     45  1.2.4.2  pgoyette #define	VSEL1_REG		0x01
     46  1.2.4.2  pgoyette #define	 VSEL_BUCK_EN			__BIT(7)
     47  1.2.4.2  pgoyette #define	 VSEL_MODE			__BIT(6)
     48  1.2.4.2  pgoyette #define	 VSEL_NSEL			__BITS(5,0)
     49  1.2.4.2  pgoyette #define	CONTROL_REG		0x02
     50  1.2.4.2  pgoyette #define	 CONTROL_OUTPUT_DISCHARGE	__BIT(7)
     51  1.2.4.2  pgoyette #define	 CONTROL_SLEW			__BITS(6,4)
     52  1.2.4.2  pgoyette #define	 CONTROL_RESET			__BIT(2)
     53  1.2.4.2  pgoyette #define	ID1_REG			0x03
     54  1.2.4.2  pgoyette #define	 ID1_VENDOR			__BITS(7,5)
     55  1.2.4.2  pgoyette #define	 ID1_DIE_ID			__BITS(3,0)
     56  1.2.4.2  pgoyette #define	  SILERGY_DIE_ID_SYR82X		8
     57  1.2.4.2  pgoyette #define	ID2_REG			0x04
     58  1.2.4.2  pgoyette #define	 ID2_DIE_REV			__BITS(3,0)
     59  1.2.4.2  pgoyette #define	MONITOR_REG		0x05
     60  1.2.4.2  pgoyette #define	 MONITOR_PGOOD			__BIT(7)
     61  1.2.4.2  pgoyette 
     62  1.2.4.2  pgoyette struct fan53555_softc {
     63  1.2.4.2  pgoyette 	device_t	sc_dev;
     64  1.2.4.2  pgoyette 	i2c_tag_t	sc_i2c;
     65  1.2.4.2  pgoyette 	i2c_addr_t	sc_addr;
     66  1.2.4.2  pgoyette 	int		sc_phandle;
     67  1.2.4.2  pgoyette 
     68  1.2.4.2  pgoyette 	u_int		sc_suspend_reg;
     69  1.2.4.2  pgoyette 	u_int		sc_runtime_reg;
     70  1.2.4.2  pgoyette 
     71  1.2.4.2  pgoyette 	u_int		sc_base;
     72  1.2.4.2  pgoyette 	u_int		sc_step;
     73  1.2.4.2  pgoyette 
     74  1.2.4.2  pgoyette 	u_int		sc_ramp_delay;
     75  1.2.4.2  pgoyette 	u_int		sc_suspend_voltage_selector;
     76  1.2.4.2  pgoyette };
     77  1.2.4.2  pgoyette 
     78  1.2.4.2  pgoyette enum fan53555_vendor {
     79  1.2.4.2  pgoyette 	FAN_VENDOR_FAIRCHILD = 1,
     80  1.2.4.2  pgoyette 	FAN_VENDOR_SILERGY,
     81  1.2.4.2  pgoyette };
     82  1.2.4.2  pgoyette 
     83  1.2.4.2  pgoyette /*
     84  1.2.4.2  pgoyette  * Transition slew rates.
     85  1.2.4.2  pgoyette  * Array index is reg value, value is slew rate in uV / us
     86  1.2.4.2  pgoyette  */
     87  1.2.4.2  pgoyette static const int fan53555_slew_rates[] = {
     88  1.2.4.2  pgoyette 	64000, 32000, 16000, 8000, 4000, 2000, 1000, 500
     89  1.2.4.2  pgoyette };
     90  1.2.4.2  pgoyette 
     91  1.2.4.2  pgoyette static const struct device_compatible_entry compat_data[] = {
     92  1.2.4.2  pgoyette 	{ "silergy,syr827",		FAN_VENDOR_SILERGY },
     93  1.2.4.2  pgoyette 	{ "silergy,syr828",		FAN_VENDOR_SILERGY },
     94  1.2.4.2  pgoyette 	{ NULL,				0 }
     95  1.2.4.2  pgoyette };
     96  1.2.4.2  pgoyette 
     97  1.2.4.2  pgoyette static uint8_t
     98  1.2.4.2  pgoyette fan53555_read(struct fan53555_softc *sc, uint8_t reg, int flags)
     99  1.2.4.2  pgoyette {
    100  1.2.4.2  pgoyette 	uint8_t val = 0;
    101  1.2.4.2  pgoyette 	int error;
    102  1.2.4.2  pgoyette 
    103  1.2.4.2  pgoyette 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    104  1.2.4.2  pgoyette 	    &reg, 1, &val, 1, flags);
    105  1.2.4.2  pgoyette 	if (error != 0)
    106  1.2.4.2  pgoyette 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    107  1.2.4.2  pgoyette 
    108  1.2.4.2  pgoyette 	return val;
    109  1.2.4.2  pgoyette }
    110  1.2.4.2  pgoyette 
    111  1.2.4.2  pgoyette static void
    112  1.2.4.2  pgoyette fan53555_write(struct fan53555_softc *sc, uint8_t reg, uint8_t val, int flags)
    113  1.2.4.2  pgoyette {
    114  1.2.4.2  pgoyette 	uint8_t buf[2];
    115  1.2.4.2  pgoyette 	int error;
    116  1.2.4.2  pgoyette 
    117  1.2.4.2  pgoyette 	buf[0] = reg;
    118  1.2.4.2  pgoyette 	buf[1] = val;
    119  1.2.4.2  pgoyette 
    120  1.2.4.2  pgoyette 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    121  1.2.4.2  pgoyette 	    NULL, 0, buf, 2, flags);
    122  1.2.4.2  pgoyette 	if (error != 0)
    123  1.2.4.2  pgoyette 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    124  1.2.4.2  pgoyette }
    125  1.2.4.2  pgoyette 
    126  1.2.4.2  pgoyette #define	I2C_READ(sc, reg)	fan53555_read((sc), (reg), I2C_F_POLL)
    127  1.2.4.2  pgoyette #define	I2C_WRITE(sc, reg, val)	fan53555_write((sc), (reg), (val), I2C_F_POLL)
    128  1.2.4.2  pgoyette #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
    129  1.2.4.2  pgoyette #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
    130  1.2.4.2  pgoyette 
    131  1.2.4.2  pgoyette static int
    132  1.2.4.2  pgoyette fan53555_acquire(device_t dev)
    133  1.2.4.2  pgoyette {
    134  1.2.4.2  pgoyette 	return 0;
    135  1.2.4.2  pgoyette }
    136  1.2.4.2  pgoyette 
    137  1.2.4.2  pgoyette static void
    138  1.2.4.2  pgoyette fan53555_release(device_t dev)
    139  1.2.4.2  pgoyette {
    140  1.2.4.2  pgoyette }
    141  1.2.4.2  pgoyette 
    142  1.2.4.2  pgoyette static int
    143  1.2.4.2  pgoyette fan53555_enable(device_t dev, bool enable)
    144  1.2.4.2  pgoyette {
    145  1.2.4.2  pgoyette 	struct fan53555_softc * const sc = device_private(dev);
    146  1.2.4.2  pgoyette 	uint8_t val;
    147  1.2.4.2  pgoyette 
    148  1.2.4.2  pgoyette 	I2C_LOCK(sc);
    149  1.2.4.2  pgoyette 	val = I2C_READ(sc, sc->sc_runtime_reg);
    150  1.2.4.2  pgoyette 	if (enable)
    151  1.2.4.2  pgoyette 		val |= VSEL_BUCK_EN;
    152  1.2.4.2  pgoyette 	else
    153  1.2.4.2  pgoyette 		val &= ~VSEL_BUCK_EN;
    154  1.2.4.2  pgoyette 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
    155  1.2.4.2  pgoyette 	I2C_UNLOCK(sc);
    156  1.2.4.2  pgoyette 
    157  1.2.4.2  pgoyette 	if (sc->sc_ramp_delay)
    158  1.2.4.2  pgoyette 		delay(sc->sc_ramp_delay);
    159  1.2.4.2  pgoyette 
    160  1.2.4.2  pgoyette 	return 0;
    161  1.2.4.2  pgoyette }
    162  1.2.4.2  pgoyette 
    163  1.2.4.2  pgoyette static int
    164  1.2.4.2  pgoyette fan53555_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    165  1.2.4.2  pgoyette {
    166  1.2.4.2  pgoyette 	struct fan53555_softc * const sc = device_private(dev);
    167  1.2.4.2  pgoyette 	uint8_t val, oval;
    168  1.2.4.2  pgoyette 	u_int cur_uvol;
    169  1.2.4.2  pgoyette 
    170  1.2.4.2  pgoyette 	I2C_LOCK(sc);
    171  1.2.4.2  pgoyette 
    172  1.2.4.2  pgoyette 	/* Get current voltage */
    173  1.2.4.2  pgoyette 	oval = I2C_READ(sc, sc->sc_runtime_reg);
    174  1.2.4.2  pgoyette 	cur_uvol = __SHIFTOUT(oval, VSEL_NSEL) * sc->sc_step + sc->sc_base;
    175  1.2.4.2  pgoyette 
    176  1.2.4.2  pgoyette 	/* Set new voltage */
    177  1.2.4.2  pgoyette 	val = oval & ~VSEL_NSEL;
    178  1.2.4.2  pgoyette 	val |= __SHIFTIN((min_uvol - sc->sc_base) / sc->sc_step, VSEL_NSEL);
    179  1.2.4.2  pgoyette 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
    180  1.2.4.2  pgoyette 
    181  1.2.4.2  pgoyette 	I2C_UNLOCK(sc);
    182  1.2.4.2  pgoyette 
    183  1.2.4.2  pgoyette 	/* Time to delay is based on the number of voltage steps */
    184  1.2.4.2  pgoyette 	if (sc->sc_ramp_delay)
    185  1.2.4.2  pgoyette 		delay((abs(cur_uvol - min_uvol) / sc->sc_step) * sc->sc_ramp_delay);
    186  1.2.4.2  pgoyette 
    187  1.2.4.2  pgoyette 	return 0;
    188  1.2.4.2  pgoyette }
    189  1.2.4.2  pgoyette 
    190  1.2.4.2  pgoyette static int
    191  1.2.4.2  pgoyette fan53555_get_voltage(device_t dev, u_int *puvol)
    192  1.2.4.2  pgoyette {
    193  1.2.4.2  pgoyette 	struct fan53555_softc * const sc = device_private(dev);
    194  1.2.4.2  pgoyette 	uint8_t val;
    195  1.2.4.2  pgoyette 
    196  1.2.4.2  pgoyette 	I2C_LOCK(sc);
    197  1.2.4.2  pgoyette 	val = I2C_READ(sc, sc->sc_runtime_reg);
    198  1.2.4.2  pgoyette 	I2C_UNLOCK(sc);
    199  1.2.4.2  pgoyette 
    200  1.2.4.2  pgoyette 	*puvol = __SHIFTOUT(val, VSEL_NSEL) * sc->sc_step + sc->sc_base;
    201  1.2.4.2  pgoyette 
    202  1.2.4.2  pgoyette 	return 0;
    203  1.2.4.2  pgoyette }
    204  1.2.4.2  pgoyette 
    205  1.2.4.2  pgoyette static struct fdtbus_regulator_controller_func fan53555_funcs = {
    206  1.2.4.2  pgoyette 	.acquire = fan53555_acquire,
    207  1.2.4.2  pgoyette 	.release = fan53555_release,
    208  1.2.4.2  pgoyette 	.enable = fan53555_enable,
    209  1.2.4.2  pgoyette 	.set_voltage = fan53555_set_voltage,
    210  1.2.4.2  pgoyette 	.get_voltage = fan53555_get_voltage,
    211  1.2.4.2  pgoyette };
    212  1.2.4.2  pgoyette 
    213  1.2.4.2  pgoyette static int
    214  1.2.4.2  pgoyette fan53555_init(struct fan53555_softc *sc, enum fan53555_vendor vendor)
    215  1.2.4.2  pgoyette {
    216  1.2.4.2  pgoyette 	uint8_t id1, control;
    217  1.2.4.2  pgoyette 	int n;
    218  1.2.4.2  pgoyette 
    219  1.2.4.2  pgoyette 	I2C_LOCK(sc);
    220  1.2.4.2  pgoyette 	id1 = I2C_READ(sc, ID1_REG);
    221  1.2.4.2  pgoyette 	I2C_UNLOCK(sc);
    222  1.2.4.2  pgoyette 
    223  1.2.4.2  pgoyette 	const u_int die_id = __SHIFTOUT(id1, ID1_DIE_ID);
    224  1.2.4.2  pgoyette 
    225  1.2.4.2  pgoyette 	aprint_naive("\n");
    226  1.2.4.2  pgoyette 	switch (vendor) {
    227  1.2.4.2  pgoyette 	case FAN_VENDOR_SILERGY:
    228  1.2.4.2  pgoyette 		switch (die_id) {
    229  1.2.4.2  pgoyette 		case SILERGY_DIE_ID_SYR82X:
    230  1.2.4.2  pgoyette 			aprint_normal(": Silergy SYR82X\n");
    231  1.2.4.2  pgoyette 			sc->sc_base = 712500;
    232  1.2.4.2  pgoyette 			sc->sc_step = 12500;
    233  1.2.4.2  pgoyette 			break;
    234  1.2.4.2  pgoyette 		default:
    235  1.2.4.2  pgoyette 			aprint_error(": Unsupported Silergy chip (0x%x)\n", die_id);
    236  1.2.4.2  pgoyette 			return ENXIO;
    237  1.2.4.2  pgoyette 		}
    238  1.2.4.2  pgoyette 		break;
    239  1.2.4.2  pgoyette 	default:
    240  1.2.4.2  pgoyette 		aprint_error(": Unsupported vendor (%d)\n", vendor);
    241  1.2.4.2  pgoyette 		return ENXIO;
    242  1.2.4.2  pgoyette 	}
    243  1.2.4.2  pgoyette 
    244  1.2.4.2  pgoyette 	of_getprop_uint32(sc->sc_phandle, "suspend_voltage_selector",
    245  1.2.4.2  pgoyette 	    &sc->sc_suspend_voltage_selector);
    246  1.2.4.2  pgoyette 	switch (sc->sc_suspend_voltage_selector) {
    247  1.2.4.2  pgoyette 	case 0:
    248  1.2.4.2  pgoyette 		sc->sc_suspend_reg = VSEL0_REG;
    249  1.2.4.2  pgoyette 		sc->sc_runtime_reg = VSEL1_REG;
    250  1.2.4.2  pgoyette 		break;
    251  1.2.4.2  pgoyette 	case 1:
    252  1.2.4.2  pgoyette 		sc->sc_suspend_reg = VSEL1_REG;
    253  1.2.4.2  pgoyette 		sc->sc_runtime_reg = VSEL0_REG;
    254  1.2.4.2  pgoyette 		break;
    255  1.2.4.2  pgoyette 	default:
    256  1.2.4.2  pgoyette 		aprint_error_dev(sc->sc_dev, "unsupported 'fcs,suspend-voltage-selector' value %u\n", sc->sc_suspend_voltage_selector);
    257  1.2.4.2  pgoyette 		return EINVAL;
    258  1.2.4.2  pgoyette 	}
    259  1.2.4.2  pgoyette 
    260  1.2.4.2  pgoyette 	of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay",
    261  1.2.4.2  pgoyette 	    &sc->sc_ramp_delay);
    262  1.2.4.2  pgoyette 	if (sc->sc_ramp_delay) {
    263  1.2.4.2  pgoyette 		for (n = 0; n < __arraycount(fan53555_slew_rates); n++)
    264  1.2.4.2  pgoyette 			if (sc->sc_ramp_delay > fan53555_slew_rates[n])
    265  1.2.4.2  pgoyette 				break;
    266  1.2.4.2  pgoyette 		if (n == __arraycount(fan53555_slew_rates)) {
    267  1.2.4.2  pgoyette 			aprint_error_dev(sc->sc_dev, "unsupported 'regulator-ramp-delay' value %u\n", sc->sc_ramp_delay);
    268  1.2.4.2  pgoyette 			return EINVAL;
    269  1.2.4.2  pgoyette 		}
    270  1.2.4.2  pgoyette 		I2C_LOCK(sc);
    271  1.2.4.2  pgoyette 		control = I2C_READ(sc, CONTROL_REG);
    272  1.2.4.2  pgoyette 		control &= ~CONTROL_SLEW;
    273  1.2.4.2  pgoyette 		control |= __SHIFTIN(fan53555_slew_rates[n], CONTROL_SLEW);
    274  1.2.4.2  pgoyette 		I2C_WRITE(sc, CONTROL_REG, control);
    275  1.2.4.2  pgoyette 		I2C_UNLOCK(sc);
    276  1.2.4.2  pgoyette 	}
    277  1.2.4.2  pgoyette 
    278  1.2.4.2  pgoyette 	return 0;
    279  1.2.4.2  pgoyette }
    280  1.2.4.2  pgoyette 
    281  1.2.4.2  pgoyette static int
    282  1.2.4.2  pgoyette fan53555_match(device_t parent, cfdata_t match, void *aux)
    283  1.2.4.2  pgoyette {
    284  1.2.4.2  pgoyette 	struct i2c_attach_args *ia = aux;
    285  1.2.4.2  pgoyette 	int match_result;
    286  1.2.4.2  pgoyette 
    287  1.2.4.2  pgoyette 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    288  1.2.4.2  pgoyette 		return match_result;
    289  1.2.4.2  pgoyette 
    290  1.2.4.2  pgoyette 	return 0;
    291  1.2.4.2  pgoyette }
    292  1.2.4.2  pgoyette 
    293  1.2.4.2  pgoyette static void
    294  1.2.4.2  pgoyette fan53555_attach(device_t parent, device_t self, void *aux)
    295  1.2.4.2  pgoyette {
    296  1.2.4.2  pgoyette 	struct fan53555_softc * const sc = device_private(self);
    297  1.2.4.2  pgoyette 	const struct device_compatible_entry *compat;
    298  1.2.4.2  pgoyette 	struct i2c_attach_args *ia = aux;
    299  1.2.4.2  pgoyette 
    300  1.2.4.2  pgoyette 	sc->sc_dev = self;
    301  1.2.4.2  pgoyette 	sc->sc_i2c = ia->ia_tag;
    302  1.2.4.2  pgoyette 	sc->sc_addr = ia->ia_addr;
    303  1.2.4.2  pgoyette 	sc->sc_phandle = ia->ia_cookie;
    304  1.2.4.2  pgoyette 
    305  1.2.4.2  pgoyette 	iic_compatible_match(ia, compat_data, &compat);
    306  1.2.4.2  pgoyette 
    307  1.2.4.2  pgoyette 	if (fan53555_init(sc, compat->data) != 0)
    308  1.2.4.2  pgoyette 		return;
    309  1.2.4.2  pgoyette 
    310  1.2.4.2  pgoyette 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    311  1.2.4.2  pgoyette 	    &fan53555_funcs);
    312  1.2.4.2  pgoyette }
    313  1.2.4.2  pgoyette 
    314  1.2.4.2  pgoyette CFATTACH_DECL_NEW(fan53555reg, sizeof(struct fan53555_softc),
    315  1.2.4.2  pgoyette     fan53555_match, fan53555_attach, NULL, NULL);
    316