Home | History | Annotate | Line # | Download | only in spi
mcp48x1.c revision 1.5
      1 /*      $NetBSD: mcp48x1.c,v 1.5 2025/09/11 14:12:38 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.5 2025/09/11 14:12:38 thorpej Exp $");
     34 
     35 /*
     36  * Driver for Microchip MCP4801/MCP4811/MCP4821 DAC.
     37  *
     38  * XXX: needs more testing.
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/kernel.h>
     45 #include <sys/types.h>
     46 #include <sys/sysctl.h>
     47 
     48 #include <dev/sysmon/sysmonvar.h>
     49 
     50 #include <dev/spi/spivar.h>
     51 
     52 #define MCP48X1DAC_DEBUG 0
     53 
     54 #define MCP48X1DAC_WRITE	__BIT(15)	/* active low */
     55 #define MCP48X1DAC_GAIN		__BIT(13)	/* active low */
     56 #define MCP48X1DAC_SHDN		__BIT(12)	/* active low */
     57 #define MCP48X1DAC_DATA		__BITS(11,0)	/* data */
     58 
     59 struct mcp48x1dac_model {
     60 	const char *name;
     61 	uint8_t	resolution;
     62 	uint8_t	shift;			/* data left shift during write */
     63 };
     64 
     65 struct mcp48x1dac_softc {
     66 	device_t sc_dev;
     67 	struct spi_handle *sc_sh;
     68 
     69 	struct mcp48x1dac_model *sc_dm;	/* struct describing DAC model */
     70 
     71 	uint16_t sc_dac_data;
     72 	bool sc_dac_gain;
     73 	bool sc_dac_shutdown;
     74 
     75 	struct sysmon_envsys *sc_sme;
     76 	envsys_data_t sc_sm_vo;		/* envsys "sensor" (Vo) */
     77 };
     78 
     79 static int	mcp48x1dac_match(device_t, cfdata_t, void *);
     80 static void	mcp48x1dac_attach(device_t, device_t, void *);
     81 
     82 static bool	mcp48x1dac_envsys_attach(struct mcp48x1dac_softc *sc);
     83 static void	mcp48x1dac_envsys_refresh(struct sysmon_envsys *,
     84 		    envsys_data_t *);
     85 
     86 static void	mcp48x1dac_write(struct mcp48x1dac_softc *);
     87 static uint16_t mcp48x1dac_regval_to_mv(struct mcp48x1dac_softc *);
     88 
     89 static void	mcp48x1dac_setup_sysctl(struct mcp48x1dac_softc *sc);
     90 static int	sysctl_mcp48x1dac_data(SYSCTLFN_ARGS);
     91 static int	sysctl_mcp48x1dac_gain(SYSCTLFN_ARGS);
     92 
     93 CFATTACH_DECL_NEW(mcp48x1dac, sizeof(struct mcp48x1dac_softc),
     94     mcp48x1dac_match, mcp48x1dac_attach, NULL, NULL);
     95 
     96 static struct mcp48x1dac_model mcp48x1_models[] = {
     97 	{
     98 		.name = "MCP4801",
     99 		.resolution = 8,
    100 		.shift = 4
    101 	},
    102 	{
    103 		.name = "MCP4811",
    104 		.resolution = 10,
    105 		.shift = 2
    106 	},
    107 	{
    108 		.name = "MCP4821",
    109 		.resolution = 12,
    110 		.shift = 0
    111 	}
    112 };
    113 
    114 
    115 static int
    116 mcp48x1dac_match(device_t parent, cfdata_t cf, void *aux)
    117 {
    118 	return SPI_MATCH_DEFAULT;
    119 }
    120 
    121 static void
    122 mcp48x1dac_attach(device_t parent, device_t self, void *aux)
    123 {
    124 	struct mcp48x1dac_softc *sc;
    125 	struct spi_attach_args *sa;
    126 	int error, cf_flags;
    127 
    128 	aprint_naive(": Digital to Analog converter\n");
    129 	aprint_normal(": MCP48x1 DAC\n");
    130 
    131 	sa = aux;
    132 	sc = device_private(self);
    133 	sc->sc_dev = self;
    134 	sc->sc_sh = sa->sa_handle;
    135 	cf_flags = device_cfdata(sc->sc_dev)->cf_flags;
    136 
    137 	sc->sc_dm = &mcp48x1_models[cf_flags]; /* flag value defines model */
    138 
    139 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0,
    140 	    SPI_FREQ_MHz(20));
    141 	if (error) {
    142 		return;
    143 	}
    144 
    145 	if(!mcp48x1dac_envsys_attach(sc)) {
    146 		aprint_error_dev(sc->sc_dev, "failed to attach envsys\n");
    147 		return;
    148 	};
    149 
    150 	sc->sc_dac_data = 0;
    151 	sc->sc_dac_gain = false;
    152 	sc->sc_dac_shutdown = false;
    153 	mcp48x1dac_write(sc);
    154 
    155 	mcp48x1dac_setup_sysctl(sc);
    156 }
    157 
    158 static void
    159 mcp48x1dac_write(struct mcp48x1dac_softc *sc)
    160 {
    161 	int rv;
    162 	uint16_t reg, regbe;
    163 
    164 	reg = 0;
    165 
    166 	if (!(sc->sc_dac_gain))
    167 		reg |= MCP48X1DAC_GAIN;
    168 
    169 	if (!(sc->sc_dac_shutdown))
    170 		reg |= MCP48X1DAC_SHDN;
    171 
    172 	reg |= sc->sc_dac_data << sc->sc_dm->shift;
    173 
    174 	regbe = htobe16(reg);
    175 
    176 #ifdef MCP48X1DAC_DEBUG
    177 	aprint_normal_dev(sc->sc_dev, "sending %x over SPI\n", regbe);
    178 #endif /* MCP48X1DAC_DEBUG */
    179 
    180 	rv = spi_send(sc->sc_sh, 2, (uint8_t*) &regbe); /* XXX: ugly cast */
    181 
    182 	if (rv != 0)
    183 		aprint_error_dev(sc->sc_dev, "error sending data over SPI\n");
    184 }
    185 
    186 static bool
    187 mcp48x1dac_envsys_attach(struct mcp48x1dac_softc *sc)
    188 {
    189 
    190 	sc->sc_sme = sysmon_envsys_create();
    191 	sc->sc_sm_vo.units = ENVSYS_SVOLTS_DC;
    192 	sc->sc_sm_vo.state = ENVSYS_SINVALID;
    193 	strlcpy(sc->sc_sm_vo.desc, device_xname(sc->sc_dev),
    194 	    sizeof(sc->sc_sm_vo.desc));
    195 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sm_vo)) {
    196 		sysmon_envsys_destroy(sc->sc_sme);
    197 		return false;
    198 	}
    199 
    200 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    201 	sc->sc_sme->sme_refresh = mcp48x1dac_envsys_refresh;
    202 	sc->sc_sme->sme_cookie = sc;
    203 
    204 	if (sysmon_envsys_register(sc->sc_sme)) {
    205 		aprint_error_dev(sc->sc_dev, "unable to register in sysmon\n");
    206 		sysmon_envsys_destroy(sc->sc_sme);
    207 	}
    208 
    209 	return true;
    210 }
    211 
    212 static uint16_t
    213 mcp48x1dac_regval_to_mv(struct mcp48x1dac_softc *sc)
    214 {
    215 	uint16_t mv;
    216 
    217 	mv = (2048 * sc->sc_dac_data / (1 << sc->sc_dm->resolution));
    218 
    219 	if (sc->sc_dac_gain)
    220 		mv *= 2;
    221 
    222 	return mv;
    223 }
    224 
    225 static void
    226 mcp48x1dac_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    227 {
    228 	struct mcp48x1dac_softc *sc;
    229 
    230 	sc = sme->sme_cookie;
    231 
    232 	edata->value_cur = mcp48x1dac_regval_to_mv(sc);
    233 	edata->state = ENVSYS_SVALID;
    234 }
    235 
    236 static void
    237 mcp48x1dac_setup_sysctl(struct mcp48x1dac_softc *sc)
    238 {
    239 	const struct sysctlnode *me = NULL, *node = NULL;
    240 
    241 	sysctl_createv(NULL, 0, NULL, &me,
    242 	    CTLFLAG_READWRITE,
    243 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    244 	    NULL, 0, NULL, 0,
    245 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    246 
    247 	sysctl_createv(NULL, 0, NULL, &node,
    248 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    249 	    CTLTYPE_INT, "data", "Digital value to convert to analog",
    250 	    sysctl_mcp48x1dac_data, 1, (void *)sc, 0,
    251 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    252 
    253 	sysctl_createv(NULL, 0, NULL, &node,
    254 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    255 	    CTLTYPE_INT, "gain", "Gain 2x enable",
    256 	    sysctl_mcp48x1dac_gain, 1, (void *)sc, 0,
    257 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    258 
    259 }
    260 
    261 
    262 SYSCTL_SETUP(sysctl_mcp48x1dac_setup, "sysctl mcp48x1dac subtree setup")
    263 {
    264 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_PERMANENT,
    265 	    CTLTYPE_NODE, "machdep", NULL, NULL, 0, NULL, 0,
    266 	    CTL_MACHDEP, CTL_EOL);
    267 }
    268 
    269 
    270 static int
    271 sysctl_mcp48x1dac_data(SYSCTLFN_ARGS)
    272 {
    273 	struct sysctlnode node = *rnode;
    274 	struct mcp48x1dac_softc *sc = node.sysctl_data;
    275 	int newdata, err;
    276 
    277 	node.sysctl_data = &sc->sc_dac_data;
    278 	if ((err = (sysctl_lookup(SYSCTLFN_CALL(&node)))) != 0)
    279 		return err;
    280 
    281 	if (newp) {
    282 		newdata = *(int *)node.sysctl_data;
    283 		if (newdata > (1 << sc->sc_dm->resolution))
    284 			return EINVAL;
    285 		sc->sc_dac_data = (uint16_t) newdata;
    286 		mcp48x1dac_write(sc);
    287 		return 0;
    288 	} else {
    289 		/* nothing to do, since we can't read from DAC */
    290 		node.sysctl_size = 4;
    291 	}
    292 
    293 	return err;
    294 }
    295 
    296 static int
    297 sysctl_mcp48x1dac_gain(SYSCTLFN_ARGS)
    298 {
    299 	struct sysctlnode node = *rnode;
    300 	struct mcp48x1dac_softc *sc = node.sysctl_data;
    301 	int newgain, err;
    302 
    303 	node.sysctl_data = &sc->sc_dac_gain;
    304 	if ((err = (sysctl_lookup(SYSCTLFN_CALL(&node)))) != 0)
    305 		return err;
    306 
    307 	if (newp) {
    308 		newgain = *(int *)node.sysctl_data;
    309 		sc->sc_dac_gain = (bool) newgain;
    310 		mcp48x1dac_write(sc);
    311 		return 0;
    312 	} else {
    313 		/* nothing to do, since we can't read from DAC */
    314 		node.sysctl_size = 4;
    315 	}
    316 
    317 	return err;
    318 }
    319 
    320