Home | History | Annotate | Line # | Download | only in spi
mcp48x1.c revision 1.4
      1 /*      $NetBSD: mcp48x1.c,v 1.4 2025/09/10 00:50:33 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.4 2025/09/10 00:50:33 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 
    119 	/* MCP48x1 is a write-only device, so no way to detect it! */
    120 
    121 	return 1;
    122 }
    123 
    124 static void
    125 mcp48x1dac_attach(device_t parent, device_t self, void *aux)
    126 {
    127 	struct mcp48x1dac_softc *sc;
    128 	struct spi_attach_args *sa;
    129 	int error, cf_flags;
    130 
    131 	aprint_naive(": Digital to Analog converter\n");
    132 	aprint_normal(": MCP48x1 DAC\n");
    133 
    134 	sa = aux;
    135 	sc = device_private(self);
    136 	sc->sc_dev = self;
    137 	sc->sc_sh = sa->sa_handle;
    138 	cf_flags = device_cfdata(sc->sc_dev)->cf_flags;
    139 
    140 	sc->sc_dm = &mcp48x1_models[cf_flags]; /* flag value defines model */
    141 
    142 	error = spi_configure(self, sa->sa_handle, SPI_MODE_0,
    143 	    SPI_FREQ_MHz(20));
    144 	if (error) {
    145 		return;
    146 	}
    147 
    148 	if(!mcp48x1dac_envsys_attach(sc)) {
    149 		aprint_error_dev(sc->sc_dev, "failed to attach envsys\n");
    150 		return;
    151 	};
    152 
    153 	sc->sc_dac_data = 0;
    154 	sc->sc_dac_gain = false;
    155 	sc->sc_dac_shutdown = false;
    156 	mcp48x1dac_write(sc);
    157 
    158 	mcp48x1dac_setup_sysctl(sc);
    159 }
    160 
    161 static void
    162 mcp48x1dac_write(struct mcp48x1dac_softc *sc)
    163 {
    164 	int rv;
    165 	uint16_t reg, regbe;
    166 
    167 	reg = 0;
    168 
    169 	if (!(sc->sc_dac_gain))
    170 		reg |= MCP48X1DAC_GAIN;
    171 
    172 	if (!(sc->sc_dac_shutdown))
    173 		reg |= MCP48X1DAC_SHDN;
    174 
    175 	reg |= sc->sc_dac_data << sc->sc_dm->shift;
    176 
    177 	regbe = htobe16(reg);
    178 
    179 #ifdef MCP48X1DAC_DEBUG
    180 	aprint_normal_dev(sc->sc_dev, "sending %x over SPI\n", regbe);
    181 #endif /* MCP48X1DAC_DEBUG */
    182 
    183 	rv = spi_send(sc->sc_sh, 2, (uint8_t*) &regbe); /* XXX: ugly cast */
    184 
    185 	if (rv != 0)
    186 		aprint_error_dev(sc->sc_dev, "error sending data over SPI\n");
    187 }
    188 
    189 static bool
    190 mcp48x1dac_envsys_attach(struct mcp48x1dac_softc *sc)
    191 {
    192 
    193 	sc->sc_sme = sysmon_envsys_create();
    194 	sc->sc_sm_vo.units = ENVSYS_SVOLTS_DC;
    195 	sc->sc_sm_vo.state = ENVSYS_SINVALID;
    196 	strlcpy(sc->sc_sm_vo.desc, device_xname(sc->sc_dev),
    197 	    sizeof(sc->sc_sm_vo.desc));
    198 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sm_vo)) {
    199 		sysmon_envsys_destroy(sc->sc_sme);
    200 		return false;
    201 	}
    202 
    203 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    204 	sc->sc_sme->sme_refresh = mcp48x1dac_envsys_refresh;
    205 	sc->sc_sme->sme_cookie = sc;
    206 
    207 	if (sysmon_envsys_register(sc->sc_sme)) {
    208 		aprint_error_dev(sc->sc_dev, "unable to register in sysmon\n");
    209 		sysmon_envsys_destroy(sc->sc_sme);
    210 	}
    211 
    212 	return true;
    213 }
    214 
    215 static uint16_t
    216 mcp48x1dac_regval_to_mv(struct mcp48x1dac_softc *sc)
    217 {
    218 	uint16_t mv;
    219 
    220 	mv = (2048 * sc->sc_dac_data / (1 << sc->sc_dm->resolution));
    221 
    222 	if (sc->sc_dac_gain)
    223 		mv *= 2;
    224 
    225 	return mv;
    226 }
    227 
    228 static void
    229 mcp48x1dac_envsys_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    230 {
    231 	struct mcp48x1dac_softc *sc;
    232 
    233 	sc = sme->sme_cookie;
    234 
    235 	edata->value_cur = mcp48x1dac_regval_to_mv(sc);
    236 	edata->state = ENVSYS_SVALID;
    237 }
    238 
    239 static void
    240 mcp48x1dac_setup_sysctl(struct mcp48x1dac_softc *sc)
    241 {
    242 	const struct sysctlnode *me = NULL, *node = NULL;
    243 
    244 	sysctl_createv(NULL, 0, NULL, &me,
    245 	    CTLFLAG_READWRITE,
    246 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    247 	    NULL, 0, NULL, 0,
    248 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    249 
    250 	sysctl_createv(NULL, 0, NULL, &node,
    251 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    252 	    CTLTYPE_INT, "data", "Digital value to convert to analog",
    253 	    sysctl_mcp48x1dac_data, 1, (void *)sc, 0,
    254 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    255 
    256 	sysctl_createv(NULL, 0, NULL, &node,
    257 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    258 	    CTLTYPE_INT, "gain", "Gain 2x enable",
    259 	    sysctl_mcp48x1dac_gain, 1, (void *)sc, 0,
    260 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    261 
    262 }
    263 
    264 
    265 SYSCTL_SETUP(sysctl_mcp48x1dac_setup, "sysctl mcp48x1dac subtree setup")
    266 {
    267 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_PERMANENT,
    268 	    CTLTYPE_NODE, "machdep", NULL, NULL, 0, NULL, 0,
    269 	    CTL_MACHDEP, CTL_EOL);
    270 }
    271 
    272 
    273 static int
    274 sysctl_mcp48x1dac_data(SYSCTLFN_ARGS)
    275 {
    276 	struct sysctlnode node = *rnode;
    277 	struct mcp48x1dac_softc *sc = node.sysctl_data;
    278 	int newdata, err;
    279 
    280 	node.sysctl_data = &sc->sc_dac_data;
    281 	if ((err = (sysctl_lookup(SYSCTLFN_CALL(&node)))) != 0)
    282 		return err;
    283 
    284 	if (newp) {
    285 		newdata = *(int *)node.sysctl_data;
    286 		if (newdata > (1 << sc->sc_dm->resolution))
    287 			return EINVAL;
    288 		sc->sc_dac_data = (uint16_t) newdata;
    289 		mcp48x1dac_write(sc);
    290 		return 0;
    291 	} else {
    292 		/* nothing to do, since we can't read from DAC */
    293 		node.sysctl_size = 4;
    294 	}
    295 
    296 	return err;
    297 }
    298 
    299 static int
    300 sysctl_mcp48x1dac_gain(SYSCTLFN_ARGS)
    301 {
    302 	struct sysctlnode node = *rnode;
    303 	struct mcp48x1dac_softc *sc = node.sysctl_data;
    304 	int newgain, err;
    305 
    306 	node.sysctl_data = &sc->sc_dac_gain;
    307 	if ((err = (sysctl_lookup(SYSCTLFN_CALL(&node)))) != 0)
    308 		return err;
    309 
    310 	if (newp) {
    311 		newgain = *(int *)node.sysctl_data;
    312 		sc->sc_dac_gain = (bool) newgain;
    313 		mcp48x1dac_write(sc);
    314 		return 0;
    315 	} else {
    316 		/* nothing to do, since we can't read from DAC */
    317 		node.sysctl_size = 4;
    318 	}
    319 
    320 	return err;
    321 }
    322 
    323