adc.c revision 1.3.4.4 1 1.3.4.4 skrll /* $NetBSD: adc.c,v 1.3.4.4 2004/09/21 13:21:25 skrll Exp $ */
2 1.3.4.2 skrll
3 1.3.4.2 skrll /*
4 1.3.4.2 skrll * Copyright (c) 2003 Valeriy E. Ushakov
5 1.3.4.2 skrll * All rights reserved.
6 1.3.4.2 skrll *
7 1.3.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 skrll * modification, are permitted provided that the following conditions
9 1.3.4.2 skrll * are met:
10 1.3.4.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 skrll * documentation and/or other materials provided with the distribution.
15 1.3.4.2 skrll * 3. The name of the author may not be used to endorse or promote products
16 1.3.4.2 skrll * derived from this software without specific prior written permission
17 1.3.4.2 skrll *
18 1.3.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.3.4.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.3.4.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.3.4.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.3.4.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.3.4.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.3.4.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.3.4.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.3.4.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.3.4.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.3.4.2 skrll */
29 1.3.4.2 skrll
30 1.3.4.2 skrll #include <sys/cdefs.h>
31 1.3.4.4 skrll __KERNEL_RCSID(0, "$NetBSD: adc.c,v 1.3.4.4 2004/09/21 13:21:25 skrll Exp $");
32 1.3.4.2 skrll
33 1.3.4.2 skrll #include <sys/param.h>
34 1.3.4.2 skrll #include <sys/kernel.h>
35 1.3.4.2 skrll #include <sys/device.h>
36 1.3.4.2 skrll #include <sys/malloc.h>
37 1.3.4.2 skrll #include <sys/systm.h>
38 1.3.4.2 skrll #ifdef GPROF
39 1.3.4.2 skrll #include <sys/gmon.h>
40 1.3.4.2 skrll #endif
41 1.3.4.2 skrll
42 1.3.4.2 skrll #include <sh3/adcreg.h>
43 1.3.4.2 skrll #include <sh3/dev/adcvar.h>
44 1.3.4.2 skrll
45 1.3.4.2 skrll #define ADC_(x) (*((volatile uint8_t *)SH7709_AD ## x))
46 1.3.4.2 skrll
47 1.3.4.2 skrll
48 1.3.4.2 skrll struct adc_softc {
49 1.3.4.2 skrll struct device sc_dev;
50 1.3.4.2 skrll };
51 1.3.4.2 skrll
52 1.3.4.2 skrll static int adc_match(struct device *, struct cfdata *, void *);
53 1.3.4.2 skrll static void adc_attach(struct device *, struct device *, void *);
54 1.3.4.2 skrll
55 1.3.4.2 skrll CFATTACH_DECL(adc, sizeof(struct adc_softc),
56 1.3.4.2 skrll adc_match, adc_attach, NULL, NULL);
57 1.3.4.2 skrll
58 1.3.4.2 skrll static int adc_search(struct device *, struct cfdata *, void *);
59 1.3.4.2 skrll static int adc_print(void *, const char *);
60 1.3.4.2 skrll
61 1.3.4.2 skrll
62 1.3.4.2 skrll static int
63 1.3.4.2 skrll adc_match(struct device *parent, struct cfdata *cfp, void *aux)
64 1.3.4.2 skrll {
65 1.3.4.2 skrll
66 1.3.4.2 skrll /* REMINDER: also in 7727 and 7729 */
67 1.3.4.2 skrll if ((cpu_product != CPU_PRODUCT_7709)
68 1.3.4.2 skrll && (cpu_product != CPU_PRODUCT_7709A))
69 1.3.4.2 skrll return (0);
70 1.3.4.2 skrll
71 1.3.4.2 skrll if (strcmp(cfp->cf_name, "adc") != 0)
72 1.3.4.2 skrll return (0);
73 1.3.4.2 skrll
74 1.3.4.2 skrll return (1);
75 1.3.4.2 skrll }
76 1.3.4.2 skrll
77 1.3.4.2 skrll
78 1.3.4.2 skrll static void
79 1.3.4.2 skrll adc_attach(struct device *parent, struct device *self, void *aux)
80 1.3.4.2 skrll {
81 1.3.4.2 skrll /* struct adc_softc *sc = (struct adc_softc *)self; */
82 1.3.4.2 skrll
83 1.3.4.2 skrll ADC_(CSR) = 0;
84 1.3.4.2 skrll ADC_(CR) = 0;
85 1.3.4.2 skrll
86 1.3.4.2 skrll printf("\n");
87 1.3.4.2 skrll config_search(adc_search, self, NULL);
88 1.3.4.2 skrll }
89 1.3.4.2 skrll
90 1.3.4.2 skrll
91 1.3.4.2 skrll static int
92 1.3.4.2 skrll adc_search(struct device *parent, struct cfdata *cf, void *aux)
93 1.3.4.2 skrll {
94 1.3.4.2 skrll
95 1.3.4.2 skrll if (config_match(parent, cf, NULL) > 0)
96 1.3.4.2 skrll config_attach(parent, cf, NULL, adc_print);
97 1.3.4.2 skrll
98 1.3.4.2 skrll return (0);
99 1.3.4.2 skrll }
100 1.3.4.2 skrll
101 1.3.4.2 skrll
102 1.3.4.2 skrll static int
103 1.3.4.2 skrll adc_print(void *aux, const char *pnp)
104 1.3.4.2 skrll {
105 1.3.4.2 skrll
106 1.3.4.2 skrll return (pnp ? QUIET : UNCONF);
107 1.3.4.2 skrll }
108 1.3.4.2 skrll
109 1.3.4.2 skrll
110 1.3.4.2 skrll /*
111 1.3.4.2 skrll * Sample specified ADC channel.
112 1.3.4.2 skrll * Must be called at spltty().
113 1.3.4.2 skrll */
114 1.3.4.2 skrll int
115 1.3.4.2 skrll adc_sample_channel(int chan)
116 1.3.4.2 skrll {
117 1.3.4.2 skrll volatile uint8_t *hireg;
118 1.3.4.2 skrll volatile uint8_t *loreg;
119 1.3.4.2 skrll int regoff;
120 1.3.4.2 skrll uint8_t csr;
121 1.3.4.2 skrll int timo;
122 1.3.4.2 skrll #ifdef DIAGNOSTIC
123 1.3.4.2 skrll uint8_t cr;
124 1.3.4.2 skrll char bits[128];
125 1.3.4.2 skrll #endif
126 1.3.4.2 skrll if ((chan < 0) || (chan >= 8))
127 1.3.4.2 skrll return (-1);
128 1.3.4.2 skrll
129 1.3.4.2 skrll regoff = (chan & 0x03) << 2;
130 1.3.4.2 skrll hireg = (volatile uint8_t *)(SH7709_ADDRAH + regoff);
131 1.3.4.2 skrll loreg = (volatile uint8_t *)(SH7709_ADDRAL + regoff);
132 1.3.4.2 skrll
133 1.3.4.2 skrll /* the loop below typically takes 27 iterations on j680 */
134 1.3.4.2 skrll timo = 300;
135 1.3.4.2 skrll
136 1.3.4.2 skrll #ifdef DIAGNOSTIC
137 1.3.4.2 skrll csr = ADC_(CSR);
138 1.3.4.2 skrll if ((csr & SH7709_ADCSR_ADST) != 0) {
139 1.3.4.2 skrll /* another conversion is in progress?! */
140 1.3.4.2 skrll printf("adc_sample_channel(%d): CSR=%s", chan,
141 1.3.4.2 skrll bitmask_snprintf(csr, SH7709_ADCSR_BITS,
142 1.3.4.2 skrll bits, sizeof(bits)));
143 1.3.4.2 skrll cr = ADC_(CR);
144 1.3.4.2 skrll cr &= ~0x07; /* three lower bits always read as 1s */
145 1.3.4.2 skrll printf(", CR=%s\n",
146 1.3.4.2 skrll bitmask_snprintf(cr, SH7709_ADCR_BITS,
147 1.3.4.2 skrll bits, sizeof(bits)));
148 1.3.4.2 skrll return (-1);
149 1.3.4.2 skrll }
150 1.3.4.2 skrll #endif
151 1.3.4.2 skrll
152 1.3.4.2 skrll /* start scanning */
153 1.3.4.2 skrll ADC_(CSR) = chan | SH7709_ADCSR_ADST | SH7709_ADCSR_CKS;
154 1.3.4.2 skrll
155 1.3.4.2 skrll do {
156 1.3.4.2 skrll csr = ADC_(CSR);
157 1.3.4.2 skrll if (timo-- == 0)
158 1.3.4.2 skrll break;
159 1.3.4.2 skrll } while ((csr & SH7709_ADCSR_ADF) == 0);
160 1.3.4.2 skrll
161 1.3.4.2 skrll /* stop scanning */
162 1.3.4.2 skrll csr &= ~(SH7709_ADCSR_ADF | SH7709_ADCSR_ADST);
163 1.3.4.2 skrll ADC_(CSR) = csr;
164 1.3.4.2 skrll
165 1.3.4.2 skrll if (timo <= 0) {
166 1.3.4.2 skrll printf("adc_sample_channel(%d): timed out\n", chan);
167 1.3.4.2 skrll return (-1);
168 1.3.4.2 skrll }
169 1.3.4.2 skrll
170 1.3.4.2 skrll /*
171 1.3.4.2 skrll * 10 bit of data: bits [9..2] in the high register, bits
172 1.3.4.2 skrll * [1..0] in the bits [7..6] of the low register.
173 1.3.4.2 skrll */
174 1.3.4.2 skrll return (((*hireg << 8) | *loreg) >> 6);
175 1.3.4.2 skrll }
176