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