Home | History | Annotate | Line # | Download | only in i2c
      1  1.10   thorpej /* $NetBSD: fan53555.c,v 1.10 2025/09/17 13:42:42 thorpej 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.10   thorpej __KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.10 2025/09/17 13:42:42 thorpej 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	VSEL0_REG		0x00
     45   1.1  jmcneill #define	VSEL1_REG		0x01
     46   1.1  jmcneill #define	 VSEL_BUCK_EN			__BIT(7)
     47   1.1  jmcneill #define	 VSEL_MODE			__BIT(6)
     48   1.1  jmcneill #define	 VSEL_NSEL			__BITS(5,0)
     49   1.1  jmcneill #define	CONTROL_REG		0x02
     50   1.1  jmcneill #define	 CONTROL_OUTPUT_DISCHARGE	__BIT(7)
     51   1.1  jmcneill #define	 CONTROL_SLEW			__BITS(6,4)
     52   1.1  jmcneill #define	 CONTROL_RESET			__BIT(2)
     53   1.1  jmcneill #define	ID1_REG			0x03
     54   1.1  jmcneill #define	 ID1_VENDOR			__BITS(7,5)
     55   1.1  jmcneill #define	 ID1_DIE_ID			__BITS(3,0)
     56   1.1  jmcneill #define	  SILERGY_DIE_ID_SYR82X		8
     57   1.4       mrg #define	  SILERGY_DIE_ID_SYR83X		9
     58   1.1  jmcneill #define	ID2_REG			0x04
     59   1.1  jmcneill #define	 ID2_DIE_REV			__BITS(3,0)
     60   1.1  jmcneill #define	MONITOR_REG		0x05
     61   1.1  jmcneill #define	 MONITOR_PGOOD			__BIT(7)
     62   1.1  jmcneill 
     63   1.1  jmcneill struct fan53555_softc {
     64   1.1  jmcneill 	device_t	sc_dev;
     65   1.1  jmcneill 	i2c_tag_t	sc_i2c;
     66   1.1  jmcneill 	i2c_addr_t	sc_addr;
     67   1.1  jmcneill 	int		sc_phandle;
     68   1.1  jmcneill 
     69   1.1  jmcneill 	u_int		sc_suspend_reg;
     70   1.1  jmcneill 	u_int		sc_runtime_reg;
     71   1.1  jmcneill 
     72   1.1  jmcneill 	u_int		sc_base;
     73   1.1  jmcneill 	u_int		sc_step;
     74   1.1  jmcneill 
     75   1.1  jmcneill 	u_int		sc_ramp_delay;
     76   1.1  jmcneill 	u_int		sc_suspend_voltage_selector;
     77   1.1  jmcneill };
     78   1.1  jmcneill 
     79   1.1  jmcneill enum fan53555_vendor {
     80   1.1  jmcneill 	FAN_VENDOR_FAIRCHILD = 1,
     81   1.1  jmcneill 	FAN_VENDOR_SILERGY,
     82   1.1  jmcneill };
     83   1.1  jmcneill 
     84   1.2  jmcneill /*
     85   1.2  jmcneill  * Transition slew rates.
     86   1.2  jmcneill  * Array index is reg value, value is slew rate in uV / us
     87   1.2  jmcneill  */
     88   1.2  jmcneill static const int fan53555_slew_rates[] = {
     89   1.2  jmcneill 	64000, 32000, 16000, 8000, 4000, 2000, 1000, 500
     90   1.2  jmcneill };
     91   1.2  jmcneill 
     92   1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
     93   1.6   thorpej 	{ .compat = "silergy,syr827",	.value = FAN_VENDOR_SILERGY },
     94   1.6   thorpej 	{ .compat = "silergy,syr828",	.value = FAN_VENDOR_SILERGY },
     95   1.9   thorpej 	DEVICE_COMPAT_EOL
     96   1.1  jmcneill };
     97   1.1  jmcneill 
     98   1.1  jmcneill static uint8_t
     99   1.1  jmcneill fan53555_read(struct fan53555_softc *sc, uint8_t reg, int flags)
    100   1.1  jmcneill {
    101   1.1  jmcneill 	uint8_t val = 0;
    102   1.1  jmcneill 	int error;
    103   1.1  jmcneill 
    104   1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    105   1.1  jmcneill 	    &reg, 1, &val, 1, flags);
    106   1.1  jmcneill 	if (error != 0)
    107   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
    108   1.1  jmcneill 
    109   1.1  jmcneill 	return val;
    110   1.1  jmcneill }
    111   1.1  jmcneill 
    112   1.1  jmcneill static void
    113   1.1  jmcneill fan53555_write(struct fan53555_softc *sc, uint8_t reg, uint8_t val, int flags)
    114   1.1  jmcneill {
    115   1.1  jmcneill 	uint8_t buf[2];
    116   1.1  jmcneill 	int error;
    117   1.1  jmcneill 
    118   1.1  jmcneill 	buf[0] = reg;
    119   1.1  jmcneill 	buf[1] = val;
    120   1.1  jmcneill 
    121   1.1  jmcneill 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    122   1.1  jmcneill 	    NULL, 0, buf, 2, flags);
    123   1.1  jmcneill 	if (error != 0)
    124   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
    125   1.1  jmcneill }
    126   1.1  jmcneill 
    127   1.5   thorpej #define	I2C_READ(sc, reg)	fan53555_read((sc), (reg), 0)
    128   1.5   thorpej #define	I2C_WRITE(sc, reg, val)	fan53555_write((sc), (reg), (val), 0)
    129   1.5   thorpej #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
    130   1.5   thorpej #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
    131   1.1  jmcneill 
    132   1.1  jmcneill static int
    133   1.1  jmcneill fan53555_acquire(device_t dev)
    134   1.1  jmcneill {
    135   1.1  jmcneill 	return 0;
    136   1.1  jmcneill }
    137   1.1  jmcneill 
    138   1.1  jmcneill static void
    139   1.1  jmcneill fan53555_release(device_t dev)
    140   1.1  jmcneill {
    141   1.1  jmcneill }
    142   1.1  jmcneill 
    143   1.1  jmcneill static int
    144   1.1  jmcneill fan53555_enable(device_t dev, bool enable)
    145   1.1  jmcneill {
    146   1.1  jmcneill 	struct fan53555_softc * const sc = device_private(dev);
    147   1.1  jmcneill 	uint8_t val;
    148   1.1  jmcneill 
    149   1.1  jmcneill 	I2C_LOCK(sc);
    150   1.1  jmcneill 	val = I2C_READ(sc, sc->sc_runtime_reg);
    151   1.1  jmcneill 	if (enable)
    152   1.1  jmcneill 		val |= VSEL_BUCK_EN;
    153   1.1  jmcneill 	else
    154   1.1  jmcneill 		val &= ~VSEL_BUCK_EN;
    155   1.1  jmcneill 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
    156   1.1  jmcneill 	I2C_UNLOCK(sc);
    157   1.1  jmcneill 
    158   1.1  jmcneill 	if (sc->sc_ramp_delay)
    159   1.1  jmcneill 		delay(sc->sc_ramp_delay);
    160   1.1  jmcneill 
    161   1.1  jmcneill 	return 0;
    162   1.1  jmcneill }
    163   1.1  jmcneill 
    164   1.1  jmcneill static int
    165   1.1  jmcneill fan53555_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
    166   1.1  jmcneill {
    167   1.1  jmcneill 	struct fan53555_softc * const sc = device_private(dev);
    168   1.1  jmcneill 	uint8_t val, oval;
    169   1.1  jmcneill 	u_int cur_uvol;
    170   1.1  jmcneill 
    171   1.1  jmcneill 	I2C_LOCK(sc);
    172   1.1  jmcneill 
    173   1.1  jmcneill 	/* Get current voltage */
    174   1.1  jmcneill 	oval = I2C_READ(sc, sc->sc_runtime_reg);
    175   1.1  jmcneill 	cur_uvol = __SHIFTOUT(oval, VSEL_NSEL) * sc->sc_step + sc->sc_base;
    176   1.1  jmcneill 
    177   1.1  jmcneill 	/* Set new voltage */
    178   1.1  jmcneill 	val = oval & ~VSEL_NSEL;
    179   1.1  jmcneill 	val |= __SHIFTIN((min_uvol - sc->sc_base) / sc->sc_step, VSEL_NSEL);
    180   1.1  jmcneill 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
    181   1.1  jmcneill 
    182   1.1  jmcneill 	I2C_UNLOCK(sc);
    183   1.1  jmcneill 
    184   1.1  jmcneill 	/* Time to delay is based on the number of voltage steps */
    185   1.1  jmcneill 	if (sc->sc_ramp_delay)
    186   1.1  jmcneill 		delay((abs(cur_uvol - min_uvol) / sc->sc_step) * sc->sc_ramp_delay);
    187   1.1  jmcneill 
    188   1.1  jmcneill 	return 0;
    189   1.1  jmcneill }
    190   1.1  jmcneill 
    191   1.1  jmcneill static int
    192   1.1  jmcneill fan53555_get_voltage(device_t dev, u_int *puvol)
    193   1.1  jmcneill {
    194   1.1  jmcneill 	struct fan53555_softc * const sc = device_private(dev);
    195   1.1  jmcneill 	uint8_t val;
    196   1.1  jmcneill 
    197   1.1  jmcneill 	I2C_LOCK(sc);
    198   1.1  jmcneill 	val = I2C_READ(sc, sc->sc_runtime_reg);
    199   1.1  jmcneill 	I2C_UNLOCK(sc);
    200   1.1  jmcneill 
    201   1.1  jmcneill 	*puvol = __SHIFTOUT(val, VSEL_NSEL) * sc->sc_step + sc->sc_base;
    202   1.1  jmcneill 
    203   1.1  jmcneill 	return 0;
    204   1.1  jmcneill }
    205   1.1  jmcneill 
    206   1.1  jmcneill static struct fdtbus_regulator_controller_func fan53555_funcs = {
    207   1.1  jmcneill 	.acquire = fan53555_acquire,
    208   1.1  jmcneill 	.release = fan53555_release,
    209   1.1  jmcneill 	.enable = fan53555_enable,
    210   1.1  jmcneill 	.set_voltage = fan53555_set_voltage,
    211   1.1  jmcneill 	.get_voltage = fan53555_get_voltage,
    212   1.1  jmcneill };
    213   1.1  jmcneill 
    214   1.1  jmcneill static int
    215   1.1  jmcneill fan53555_init(struct fan53555_softc *sc, enum fan53555_vendor vendor)
    216   1.1  jmcneill {
    217   1.2  jmcneill 	uint8_t id1, control;
    218   1.2  jmcneill 	int n;
    219   1.1  jmcneill 
    220   1.1  jmcneill 	I2C_LOCK(sc);
    221   1.1  jmcneill 	id1 = I2C_READ(sc, ID1_REG);
    222   1.1  jmcneill 	I2C_UNLOCK(sc);
    223   1.1  jmcneill 
    224   1.1  jmcneill 	const u_int die_id = __SHIFTOUT(id1, ID1_DIE_ID);
    225   1.1  jmcneill 
    226   1.1  jmcneill 	aprint_naive("\n");
    227   1.1  jmcneill 	switch (vendor) {
    228   1.1  jmcneill 	case FAN_VENDOR_SILERGY:
    229   1.1  jmcneill 		switch (die_id) {
    230   1.1  jmcneill 		case SILERGY_DIE_ID_SYR82X:
    231   1.1  jmcneill 			aprint_normal(": Silergy SYR82X\n");
    232   1.1  jmcneill 			sc->sc_base = 712500;
    233   1.1  jmcneill 			sc->sc_step = 12500;
    234   1.1  jmcneill 			break;
    235   1.4       mrg 		case SILERGY_DIE_ID_SYR83X:
    236   1.4       mrg 			aprint_normal(": Silergy SYR83X\n");
    237   1.4       mrg 			sc->sc_base = 712500;
    238   1.4       mrg 			sc->sc_step = 12500;
    239   1.4       mrg 			break;
    240   1.1  jmcneill 		default:
    241   1.1  jmcneill 			aprint_error(": Unsupported Silergy chip (0x%x)\n", die_id);
    242   1.1  jmcneill 			return ENXIO;
    243   1.1  jmcneill 		}
    244   1.1  jmcneill 		break;
    245   1.1  jmcneill 	default:
    246   1.1  jmcneill 		aprint_error(": Unsupported vendor (%d)\n", vendor);
    247   1.1  jmcneill 		return ENXIO;
    248   1.1  jmcneill 	}
    249   1.1  jmcneill 
    250   1.3  jmcneill 	sc->sc_suspend_voltage_selector = -1;
    251   1.3  jmcneill 	of_getprop_uint32(sc->sc_phandle, "fcs,suspend-voltage-selector",
    252   1.2  jmcneill 	    &sc->sc_suspend_voltage_selector);
    253   1.1  jmcneill 	switch (sc->sc_suspend_voltage_selector) {
    254   1.1  jmcneill 	case 0:
    255   1.1  jmcneill 		sc->sc_suspend_reg = VSEL0_REG;
    256   1.1  jmcneill 		sc->sc_runtime_reg = VSEL1_REG;
    257   1.1  jmcneill 		break;
    258   1.1  jmcneill 	case 1:
    259   1.1  jmcneill 		sc->sc_suspend_reg = VSEL1_REG;
    260   1.1  jmcneill 		sc->sc_runtime_reg = VSEL0_REG;
    261   1.1  jmcneill 		break;
    262   1.1  jmcneill 	default:
    263   1.2  jmcneill 		aprint_error_dev(sc->sc_dev, "unsupported 'fcs,suspend-voltage-selector' value %u\n", sc->sc_suspend_voltage_selector);
    264   1.1  jmcneill 		return EINVAL;
    265   1.1  jmcneill 	}
    266   1.1  jmcneill 
    267   1.2  jmcneill 	of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay",
    268   1.2  jmcneill 	    &sc->sc_ramp_delay);
    269   1.2  jmcneill 	if (sc->sc_ramp_delay) {
    270   1.2  jmcneill 		for (n = 0; n < __arraycount(fan53555_slew_rates); n++)
    271   1.2  jmcneill 			if (sc->sc_ramp_delay > fan53555_slew_rates[n])
    272   1.2  jmcneill 				break;
    273   1.2  jmcneill 		if (n == __arraycount(fan53555_slew_rates)) {
    274   1.2  jmcneill 			aprint_error_dev(sc->sc_dev, "unsupported 'regulator-ramp-delay' value %u\n", sc->sc_ramp_delay);
    275   1.2  jmcneill 			return EINVAL;
    276   1.2  jmcneill 		}
    277   1.2  jmcneill 		I2C_LOCK(sc);
    278   1.2  jmcneill 		control = I2C_READ(sc, CONTROL_REG);
    279   1.2  jmcneill 		control &= ~CONTROL_SLEW;
    280   1.2  jmcneill 		control |= __SHIFTIN(fan53555_slew_rates[n], CONTROL_SLEW);
    281   1.2  jmcneill 		I2C_WRITE(sc, CONTROL_REG, control);
    282   1.2  jmcneill 		I2C_UNLOCK(sc);
    283   1.2  jmcneill 	}
    284   1.2  jmcneill 
    285   1.1  jmcneill 	return 0;
    286   1.1  jmcneill }
    287   1.1  jmcneill 
    288   1.1  jmcneill static int
    289   1.1  jmcneill fan53555_match(device_t parent, cfdata_t match, void *aux)
    290   1.1  jmcneill {
    291   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    292   1.1  jmcneill 	int match_result;
    293   1.1  jmcneill 
    294   1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    295   1.1  jmcneill 		return match_result;
    296   1.1  jmcneill 
    297   1.1  jmcneill 	return 0;
    298   1.1  jmcneill }
    299   1.1  jmcneill 
    300   1.1  jmcneill static void
    301   1.1  jmcneill fan53555_attach(device_t parent, device_t self, void *aux)
    302   1.1  jmcneill {
    303   1.1  jmcneill 	struct fan53555_softc * const sc = device_private(self);
    304   1.1  jmcneill 	const struct device_compatible_entry *compat;
    305   1.1  jmcneill 	struct i2c_attach_args *ia = aux;
    306   1.1  jmcneill 
    307   1.1  jmcneill 	sc->sc_dev = self;
    308   1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    309   1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    310  1.10   thorpej 	sc->sc_phandle = devhandle_to_of(device_handle(self));
    311   1.1  jmcneill 
    312   1.7   thorpej 	compat = iic_compatible_lookup(ia, compat_data);
    313   1.6   thorpej 	KASSERT(compat != NULL);
    314   1.1  jmcneill 
    315   1.6   thorpej 	if (fan53555_init(sc, compat->value) != 0)
    316   1.1  jmcneill 		return;
    317   1.1  jmcneill 
    318   1.1  jmcneill 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
    319   1.1  jmcneill 	    &fan53555_funcs);
    320   1.1  jmcneill }
    321   1.1  jmcneill 
    322   1.1  jmcneill CFATTACH_DECL_NEW(fan53555reg, sizeof(struct fan53555_softc),
    323   1.1  jmcneill     fan53555_match, fan53555_attach, NULL, NULL);
    324