/*-1* Copyright (c) 2014 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Vybrid Family 12-bit Analog to Digital Converter (ADC)28* Chapter 37, Vybrid Reference Manual, Rev. 5, 07/201329*/3031#include <sys/param.h>32#include <sys/systm.h>33#include <sys/bus.h>34#include <sys/kernel.h>35#include <sys/module.h>36#include <sys/malloc.h>37#include <sys/rman.h>38#include <sys/timeet.h>39#include <sys/timetc.h>4041#include <dev/ofw/openfirm.h>42#include <dev/ofw/ofw_bus.h>43#include <dev/ofw/ofw_bus_subr.h>4445#include <machine/bus.h>46#include <machine/cpu.h>47#include <machine/intr.h>4849#include <arm/freescale/vybrid/vf_common.h>50#include <arm/freescale/vybrid/vf_adc.h>5152#define ADC_HC0 0x00 /* Ctrl reg for hardware triggers */53#define ADC_HC1 0x04 /* Ctrl reg for hardware triggers */54#define HC_AIEN (1 << 7) /* Conversion Complete Int Control */55#define HC_ADCH_M 0x1f /* Input Channel Select Mask */56#define HC_ADCH_S 0 /* Input Channel Select Shift */57#define ADC_HS 0x08 /* Status register for HW triggers */58#define HS_COCO0 (1 << 0) /* Conversion Complete Flag */59#define HS_COCO1 (1 << 1) /* Conversion Complete Flag */60#define ADC_R0 0x0C /* Data result reg for HW triggers */61#define ADC_R1 0x10 /* Data result reg for HW triggers */62#define ADC_CFG 0x14 /* Configuration register */63#define CFG_OVWREN (1 << 16) /* Data Overwrite Enable */64#define CFG_AVGS_M 0x3 /* Hardware Average select Mask */65#define CFG_AVGS_S 14 /* Hardware Average select Shift */66#define CFG_ADTRG (1 << 13) /* Conversion Trigger Select */67#define CFG_REFSEL_M 0x3 /* Voltage Reference Select Mask */68#define CFG_REFSEL_S 11 /* Voltage Reference Select Shift */69#define CFG_ADHSC (1 << 10) /* High Speed Configuration */70#define CFG_ADSTS_M 0x3 /* Defines the sample time duration */71#define CFG_ADSTS_S 8 /* Defines the sample time duration */72#define CFG_ADLPC (1 << 7) /* Low-Power Configuration */73#define CFG_ADIV_M 0x3 /* Clock Divide Select */74#define CFG_ADIV_S 5 /* Clock Divide Select */75#define CFG_ADLSMP (1 << 4) /* Long Sample Time Configuration */76#define CFG_MODE_M 0x3 /* Conversion Mode Selection Mask */77#define CFG_MODE_S 2 /* Conversion Mode Selection Shift */78#define CFG_MODE_12 0x2 /* 12-bit mode */79#define CFG_ADICLK_M 0x3 /* Input Clock Select Mask */80#define CFG_ADICLK_S 0 /* Input Clock Select Shift */81#define ADC_GC 0x18 /* General control register */82#define GC_CAL (1 << 7) /* Calibration */83#define GC_ADCO (1 << 6) /* Continuous Conversion Enable */84#define GC_AVGE (1 << 5) /* Hardware average enable */85#define GC_ACFE (1 << 4) /* Compare Function Enable */86#define GC_ACFGT (1 << 3) /* Compare Function Greater Than En */87#define GC_ACREN (1 << 2) /* Compare Function Range En */88#define GC_DMAEN (1 << 1) /* DMA Enable */89#define GC_ADACKEN (1 << 0) /* Asynchronous clock output enable */90#define ADC_GS 0x1C /* General status register */91#define GS_AWKST (1 << 2) /* Asynchronous wakeup int status */92#define GS_CALF (1 << 1) /* Calibration Failed Flag */93#define GS_ADACT (1 << 0) /* Conversion Active */94#define ADC_CV 0x20 /* Compare value register */95#define CV_CV2_M 0xfff /* Compare Value 2 Mask */96#define CV_CV2_S 16 /* Compare Value 2 Shift */97#define CV_CV1_M 0xfff /* Compare Value 1 Mask */98#define CV_CV1_S 0 /* Compare Value 1 Shift */99#define ADC_OFS 0x24 /* Offset correction value register */100#define OFS_SIGN 12 /* Sign bit */101#define OFS_M 0xfff /* Offset value Mask */102#define OFS_S 0 /* Offset value Shift */103#define ADC_CAL 0x28 /* Calibration value register */104#define CAL_CODE_M 0xf /* Calibration Result Value Mask */105#define CAL_CODE_S 0 /* Calibration Result Value Shift */106#define ADC_PCTL 0x30 /* Pin control register */107108struct adc_softc {109struct resource *res[2];110bus_space_tag_t bst;111bus_space_handle_t bsh;112void *ih;113};114115struct adc_softc *adc_sc;116117static struct resource_spec adc_spec[] = {118{ SYS_RES_MEMORY, 0, RF_ACTIVE },119{ SYS_RES_IRQ, 0, RF_ACTIVE },120{ -1, 0 }121};122123static int124adc_probe(device_t dev)125{126127if (!ofw_bus_status_okay(dev))128return (ENXIO);129130if (!ofw_bus_is_compatible(dev, "fsl,mvf600-adc"))131return (ENXIO);132133device_set_desc(dev, "Vybrid Family "134"12-bit Analog to Digital Converter");135return (BUS_PROBE_DEFAULT);136}137138static void139adc_intr(void *arg)140{141142/* Conversation complete */143}144145uint32_t146adc_read(void)147{148struct adc_softc *sc;149150sc = adc_sc;151if (sc == NULL)152return (0);153154return (READ4(sc, ADC_R0));155}156157uint32_t158adc_enable(int channel)159{160struct adc_softc *sc;161int reg;162163sc = adc_sc;164if (sc == NULL)165return (1);166167reg = READ4(sc, ADC_HC0);168reg &= ~(HC_ADCH_M << HC_ADCH_S);169reg |= (channel << HC_ADCH_S);170WRITE4(sc, ADC_HC0, reg);171172return (0);173}174175static int176adc_attach(device_t dev)177{178struct adc_softc *sc;179int err;180int reg;181182sc = device_get_softc(dev);183184if (bus_alloc_resources(dev, adc_spec, sc->res)) {185device_printf(dev, "could not allocate resources\n");186return (ENXIO);187}188189/* Memory interface */190sc->bst = rman_get_bustag(sc->res[0]);191sc->bsh = rman_get_bushandle(sc->res[0]);192193adc_sc = sc;194195/* Setup interrupt handler */196err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,197NULL, adc_intr, sc, &sc->ih);198if (err) {199device_printf(dev, "Unable to alloc interrupt resource.\n");200return (ENXIO);201}202203/* Configure 12-bit mode */204reg = READ4(sc, ADC_CFG);205reg &= ~(CFG_MODE_M << CFG_MODE_S);206reg |= (CFG_MODE_12 << CFG_MODE_S); /* 12bit */207WRITE4(sc, ADC_CFG, reg);208209/* Configure for continuous conversion */210reg = READ4(sc, ADC_GC);211reg |= (GC_ADCO | GC_AVGE);212WRITE4(sc, ADC_GC, reg);213214/* Disable interrupts */215reg = READ4(sc, ADC_HC0);216reg &= HC_AIEN;217WRITE4(sc, ADC_HC0, reg);218219return (0);220}221222static device_method_t adc_methods[] = {223DEVMETHOD(device_probe, adc_probe),224DEVMETHOD(device_attach, adc_attach),225{ 0, 0 }226};227228static driver_t adc_driver = {229"adc",230adc_methods,231sizeof(struct adc_softc),232};233234DRIVER_MODULE(adc, simplebus, adc_driver, 0, 0);235236237