1b9402e3dSRuslan Bukin /*-
2b9402e3dSRuslan Bukin * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3b9402e3dSRuslan Bukin * All rights reserved.
4b9402e3dSRuslan Bukin *
5b9402e3dSRuslan Bukin * Redistribution and use in source and binary forms, with or without
6b9402e3dSRuslan Bukin * modification, are permitted provided that the following conditions
7b9402e3dSRuslan Bukin * are met:
8b9402e3dSRuslan Bukin * 1. Redistributions of source code must retain the above copyright
9b9402e3dSRuslan Bukin * notice, this list of conditions and the following disclaimer.
10b9402e3dSRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
11b9402e3dSRuslan Bukin * notice, this list of conditions and the following disclaimer in the
12b9402e3dSRuslan Bukin * documentation and/or other materials provided with the distribution.
13b9402e3dSRuslan Bukin *
14b9402e3dSRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15b9402e3dSRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b9402e3dSRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b9402e3dSRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18b9402e3dSRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b9402e3dSRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b9402e3dSRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b9402e3dSRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b9402e3dSRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b9402e3dSRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b9402e3dSRuslan Bukin * SUCH DAMAGE.
25b9402e3dSRuslan Bukin */
26b9402e3dSRuslan Bukin
27b9402e3dSRuslan Bukin /*
28b9402e3dSRuslan Bukin * Vybrid Family 12-bit Analog to Digital Converter (ADC)
29b9402e3dSRuslan Bukin * Chapter 37, Vybrid Reference Manual, Rev. 5, 07/2013
30b9402e3dSRuslan Bukin */
31b9402e3dSRuslan Bukin
32b9402e3dSRuslan Bukin #include <sys/param.h>
33b9402e3dSRuslan Bukin #include <sys/systm.h>
34b9402e3dSRuslan Bukin #include <sys/bus.h>
35b9402e3dSRuslan Bukin #include <sys/kernel.h>
36b9402e3dSRuslan Bukin #include <sys/module.h>
37b9402e3dSRuslan Bukin #include <sys/malloc.h>
38b9402e3dSRuslan Bukin #include <sys/rman.h>
39b9402e3dSRuslan Bukin #include <sys/timeet.h>
40b9402e3dSRuslan Bukin #include <sys/timetc.h>
41b9402e3dSRuslan Bukin
42b9402e3dSRuslan Bukin #include <dev/ofw/openfirm.h>
43b9402e3dSRuslan Bukin #include <dev/ofw/ofw_bus.h>
44b9402e3dSRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
45b9402e3dSRuslan Bukin
46b9402e3dSRuslan Bukin #include <machine/bus.h>
47b9402e3dSRuslan Bukin #include <machine/cpu.h>
48b9402e3dSRuslan Bukin #include <machine/intr.h>
49b9402e3dSRuslan Bukin
50b9402e3dSRuslan Bukin #include <arm/freescale/vybrid/vf_common.h>
51b9402e3dSRuslan Bukin #include <arm/freescale/vybrid/vf_adc.h>
52b9402e3dSRuslan Bukin
53b9402e3dSRuslan Bukin #define ADC_HC0 0x00 /* Ctrl reg for hardware triggers */
54b9402e3dSRuslan Bukin #define ADC_HC1 0x04 /* Ctrl reg for hardware triggers */
55b9402e3dSRuslan Bukin #define HC_AIEN (1 << 7) /* Conversion Complete Int Control */
56b9402e3dSRuslan Bukin #define HC_ADCH_M 0x1f /* Input Channel Select Mask */
57b9402e3dSRuslan Bukin #define HC_ADCH_S 0 /* Input Channel Select Shift */
58b9402e3dSRuslan Bukin #define ADC_HS 0x08 /* Status register for HW triggers */
59b9402e3dSRuslan Bukin #define HS_COCO0 (1 << 0) /* Conversion Complete Flag */
60b9402e3dSRuslan Bukin #define HS_COCO1 (1 << 1) /* Conversion Complete Flag */
61b9402e3dSRuslan Bukin #define ADC_R0 0x0C /* Data result reg for HW triggers */
62b9402e3dSRuslan Bukin #define ADC_R1 0x10 /* Data result reg for HW triggers */
63b9402e3dSRuslan Bukin #define ADC_CFG 0x14 /* Configuration register */
64b9402e3dSRuslan Bukin #define CFG_OVWREN (1 << 16) /* Data Overwrite Enable */
65b9402e3dSRuslan Bukin #define CFG_AVGS_M 0x3 /* Hardware Average select Mask */
66b9402e3dSRuslan Bukin #define CFG_AVGS_S 14 /* Hardware Average select Shift */
67b9402e3dSRuslan Bukin #define CFG_ADTRG (1 << 13) /* Conversion Trigger Select */
68b9402e3dSRuslan Bukin #define CFG_REFSEL_M 0x3 /* Voltage Reference Select Mask */
69b9402e3dSRuslan Bukin #define CFG_REFSEL_S 11 /* Voltage Reference Select Shift */
70b9402e3dSRuslan Bukin #define CFG_ADHSC (1 << 10) /* High Speed Configuration */
71b9402e3dSRuslan Bukin #define CFG_ADSTS_M 0x3 /* Defines the sample time duration */
72b9402e3dSRuslan Bukin #define CFG_ADSTS_S 8 /* Defines the sample time duration */
73b9402e3dSRuslan Bukin #define CFG_ADLPC (1 << 7) /* Low-Power Configuration */
74b9402e3dSRuslan Bukin #define CFG_ADIV_M 0x3 /* Clock Divide Select */
75b9402e3dSRuslan Bukin #define CFG_ADIV_S 5 /* Clock Divide Select */
76b9402e3dSRuslan Bukin #define CFG_ADLSMP (1 << 4) /* Long Sample Time Configuration */
77b9402e3dSRuslan Bukin #define CFG_MODE_M 0x3 /* Conversion Mode Selection Mask */
78b9402e3dSRuslan Bukin #define CFG_MODE_S 2 /* Conversion Mode Selection Shift */
79b9402e3dSRuslan Bukin #define CFG_MODE_12 0x2 /* 12-bit mode */
80b9402e3dSRuslan Bukin #define CFG_ADICLK_M 0x3 /* Input Clock Select Mask */
81b9402e3dSRuslan Bukin #define CFG_ADICLK_S 0 /* Input Clock Select Shift */
82b9402e3dSRuslan Bukin #define ADC_GC 0x18 /* General control register */
83b9402e3dSRuslan Bukin #define GC_CAL (1 << 7) /* Calibration */
84b9402e3dSRuslan Bukin #define GC_ADCO (1 << 6) /* Continuous Conversion Enable */
85b9402e3dSRuslan Bukin #define GC_AVGE (1 << 5) /* Hardware average enable */
86b9402e3dSRuslan Bukin #define GC_ACFE (1 << 4) /* Compare Function Enable */
87b9402e3dSRuslan Bukin #define GC_ACFGT (1 << 3) /* Compare Function Greater Than En */
88b9402e3dSRuslan Bukin #define GC_ACREN (1 << 2) /* Compare Function Range En */
89b9402e3dSRuslan Bukin #define GC_DMAEN (1 << 1) /* DMA Enable */
90b9402e3dSRuslan Bukin #define GC_ADACKEN (1 << 0) /* Asynchronous clock output enable */
91b9402e3dSRuslan Bukin #define ADC_GS 0x1C /* General status register */
92b9402e3dSRuslan Bukin #define GS_AWKST (1 << 2) /* Asynchronous wakeup int status */
93b9402e3dSRuslan Bukin #define GS_CALF (1 << 1) /* Calibration Failed Flag */
94b9402e3dSRuslan Bukin #define GS_ADACT (1 << 0) /* Conversion Active */
95b9402e3dSRuslan Bukin #define ADC_CV 0x20 /* Compare value register */
96b9402e3dSRuslan Bukin #define CV_CV2_M 0xfff /* Compare Value 2 Mask */
97b9402e3dSRuslan Bukin #define CV_CV2_S 16 /* Compare Value 2 Shift */
98b9402e3dSRuslan Bukin #define CV_CV1_M 0xfff /* Compare Value 1 Mask */
99b9402e3dSRuslan Bukin #define CV_CV1_S 0 /* Compare Value 1 Shift */
100b9402e3dSRuslan Bukin #define ADC_OFS 0x24 /* Offset correction value register */
101b9402e3dSRuslan Bukin #define OFS_SIGN 12 /* Sign bit */
102b9402e3dSRuslan Bukin #define OFS_M 0xfff /* Offset value Mask */
103b9402e3dSRuslan Bukin #define OFS_S 0 /* Offset value Shift */
104b9402e3dSRuslan Bukin #define ADC_CAL 0x28 /* Calibration value register */
105b9402e3dSRuslan Bukin #define CAL_CODE_M 0xf /* Calibration Result Value Mask */
106b9402e3dSRuslan Bukin #define CAL_CODE_S 0 /* Calibration Result Value Shift */
107b9402e3dSRuslan Bukin #define ADC_PCTL 0x30 /* Pin control register */
108b9402e3dSRuslan Bukin
109b9402e3dSRuslan Bukin struct adc_softc {
110b9402e3dSRuslan Bukin struct resource *res[2];
111b9402e3dSRuslan Bukin bus_space_tag_t bst;
112b9402e3dSRuslan Bukin bus_space_handle_t bsh;
113b9402e3dSRuslan Bukin void *ih;
114b9402e3dSRuslan Bukin };
115b9402e3dSRuslan Bukin
116b9402e3dSRuslan Bukin struct adc_softc *adc_sc;
117b9402e3dSRuslan Bukin
118b9402e3dSRuslan Bukin static struct resource_spec adc_spec[] = {
119b9402e3dSRuslan Bukin { SYS_RES_MEMORY, 0, RF_ACTIVE },
120b9402e3dSRuslan Bukin { SYS_RES_IRQ, 0, RF_ACTIVE },
121b9402e3dSRuslan Bukin { -1, 0 }
122b9402e3dSRuslan Bukin };
123b9402e3dSRuslan Bukin
124b9402e3dSRuslan Bukin static int
adc_probe(device_t dev)125b9402e3dSRuslan Bukin adc_probe(device_t dev)
126b9402e3dSRuslan Bukin {
127b9402e3dSRuslan Bukin
128b9402e3dSRuslan Bukin if (!ofw_bus_status_okay(dev))
129b9402e3dSRuslan Bukin return (ENXIO);
130b9402e3dSRuslan Bukin
131b9402e3dSRuslan Bukin if (!ofw_bus_is_compatible(dev, "fsl,mvf600-adc"))
132b9402e3dSRuslan Bukin return (ENXIO);
133b9402e3dSRuslan Bukin
134b9402e3dSRuslan Bukin device_set_desc(dev, "Vybrid Family "
135b9402e3dSRuslan Bukin "12-bit Analog to Digital Converter");
136b9402e3dSRuslan Bukin return (BUS_PROBE_DEFAULT);
137b9402e3dSRuslan Bukin }
138b9402e3dSRuslan Bukin
139b9402e3dSRuslan Bukin static void
adc_intr(void * arg)140b9402e3dSRuslan Bukin adc_intr(void *arg)
141b9402e3dSRuslan Bukin {
142b9402e3dSRuslan Bukin
143b9402e3dSRuslan Bukin /* Conversation complete */
144b9402e3dSRuslan Bukin }
145b9402e3dSRuslan Bukin
146b9402e3dSRuslan Bukin uint32_t
adc_read(void)147b9402e3dSRuslan Bukin adc_read(void)
148b9402e3dSRuslan Bukin {
149b9402e3dSRuslan Bukin struct adc_softc *sc;
150b9402e3dSRuslan Bukin
151b9402e3dSRuslan Bukin sc = adc_sc;
152b9402e3dSRuslan Bukin if (sc == NULL)
153b9402e3dSRuslan Bukin return (0);
154b9402e3dSRuslan Bukin
155b9402e3dSRuslan Bukin return (READ4(sc, ADC_R0));
156b9402e3dSRuslan Bukin }
157b9402e3dSRuslan Bukin
158b9402e3dSRuslan Bukin uint32_t
adc_enable(int channel)159b9402e3dSRuslan Bukin adc_enable(int channel)
160b9402e3dSRuslan Bukin {
161b9402e3dSRuslan Bukin struct adc_softc *sc;
162b9402e3dSRuslan Bukin int reg;
163b9402e3dSRuslan Bukin
164b9402e3dSRuslan Bukin sc = adc_sc;
165b9402e3dSRuslan Bukin if (sc == NULL)
166b9402e3dSRuslan Bukin return (1);
167b9402e3dSRuslan Bukin
168b9402e3dSRuslan Bukin reg = READ4(sc, ADC_HC0);
169b9402e3dSRuslan Bukin reg &= ~(HC_ADCH_M << HC_ADCH_S);
170b9402e3dSRuslan Bukin reg |= (channel << HC_ADCH_S);
171b9402e3dSRuslan Bukin WRITE4(sc, ADC_HC0, reg);
172b9402e3dSRuslan Bukin
173b9402e3dSRuslan Bukin return (0);
174b9402e3dSRuslan Bukin }
175b9402e3dSRuslan Bukin
176b9402e3dSRuslan Bukin static int
adc_attach(device_t dev)177b9402e3dSRuslan Bukin adc_attach(device_t dev)
178b9402e3dSRuslan Bukin {
179b9402e3dSRuslan Bukin struct adc_softc *sc;
180b9402e3dSRuslan Bukin int err;
181b9402e3dSRuslan Bukin int reg;
182b9402e3dSRuslan Bukin
183b9402e3dSRuslan Bukin sc = device_get_softc(dev);
184b9402e3dSRuslan Bukin
185b9402e3dSRuslan Bukin if (bus_alloc_resources(dev, adc_spec, sc->res)) {
186b9402e3dSRuslan Bukin device_printf(dev, "could not allocate resources\n");
187b9402e3dSRuslan Bukin return (ENXIO);
188b9402e3dSRuslan Bukin }
189b9402e3dSRuslan Bukin
190b9402e3dSRuslan Bukin /* Memory interface */
191b9402e3dSRuslan Bukin sc->bst = rman_get_bustag(sc->res[0]);
192b9402e3dSRuslan Bukin sc->bsh = rman_get_bushandle(sc->res[0]);
193b9402e3dSRuslan Bukin
194b9402e3dSRuslan Bukin adc_sc = sc;
195b9402e3dSRuslan Bukin
196b9402e3dSRuslan Bukin /* Setup interrupt handler */
197b9402e3dSRuslan Bukin err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,
198b9402e3dSRuslan Bukin NULL, adc_intr, sc, &sc->ih);
199b9402e3dSRuslan Bukin if (err) {
200b9402e3dSRuslan Bukin device_printf(dev, "Unable to alloc interrupt resource.\n");
201b9402e3dSRuslan Bukin return (ENXIO);
202b9402e3dSRuslan Bukin }
203b9402e3dSRuslan Bukin
204b9402e3dSRuslan Bukin /* Configure 12-bit mode */
205b9402e3dSRuslan Bukin reg = READ4(sc, ADC_CFG);
206b9402e3dSRuslan Bukin reg &= ~(CFG_MODE_M << CFG_MODE_S);
207b9402e3dSRuslan Bukin reg |= (CFG_MODE_12 << CFG_MODE_S); /* 12bit */
208b9402e3dSRuslan Bukin WRITE4(sc, ADC_CFG, reg);
209b9402e3dSRuslan Bukin
210b9402e3dSRuslan Bukin /* Configure for continuous conversion */
211b9402e3dSRuslan Bukin reg = READ4(sc, ADC_GC);
212b9402e3dSRuslan Bukin reg |= (GC_ADCO | GC_AVGE);
213b9402e3dSRuslan Bukin WRITE4(sc, ADC_GC, reg);
214b9402e3dSRuslan Bukin
215b9402e3dSRuslan Bukin /* Disable interrupts */
216b9402e3dSRuslan Bukin reg = READ4(sc, ADC_HC0);
217b9402e3dSRuslan Bukin reg &= HC_AIEN;
218b9402e3dSRuslan Bukin WRITE4(sc, ADC_HC0, reg);
219b9402e3dSRuslan Bukin
220b9402e3dSRuslan Bukin return (0);
221b9402e3dSRuslan Bukin }
222b9402e3dSRuslan Bukin
223b9402e3dSRuslan Bukin static device_method_t adc_methods[] = {
224b9402e3dSRuslan Bukin DEVMETHOD(device_probe, adc_probe),
225b9402e3dSRuslan Bukin DEVMETHOD(device_attach, adc_attach),
226b9402e3dSRuslan Bukin { 0, 0 }
227b9402e3dSRuslan Bukin };
228b9402e3dSRuslan Bukin
229b9402e3dSRuslan Bukin static driver_t adc_driver = {
230b9402e3dSRuslan Bukin "adc",
231b9402e3dSRuslan Bukin adc_methods,
232b9402e3dSRuslan Bukin sizeof(struct adc_softc),
233b9402e3dSRuslan Bukin };
234b9402e3dSRuslan Bukin
235*ea538dabSJohn Baldwin DRIVER_MODULE(adc, simplebus, adc_driver, 0, 0);
236