Home | History | Annotate | Line # | Download | only in dev
adc.c revision 1.15
      1 /*	$NetBSD: adc.c,v 1.15 2021/08/07 16:19:05 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.15 2021/08/07 16:19:05 thorpej Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/malloc.h>
     37 #include <sys/systm.h>
     38 
     39 #include <sh3/adcreg.h>
     40 #include <sh3/dev/adcvar.h>
     41 
     42 #define ADC_(x)    (*((volatile uint8_t *)SH7709_AD ## x))
     43 
     44 
     45 static int	adc_match(device_t, cfdata_t, void *);
     46 static void	adc_attach(device_t, device_t, void *);
     47 
     48 CFATTACH_DECL_NEW(adc, 0,
     49     adc_match, adc_attach, NULL, NULL);
     50 
     51 static int	adc_search(device_t, cfdata_t, const int *, void *);
     52 static int	adc_print(void *, const char *);
     53 
     54 
     55 static int
     56 adc_match(device_t parent, cfdata_t cf, void *aux)
     57 {
     58 
     59 	/* REMINDER: also in 7727 and 7729 */
     60 	if ((cpu_product != CPU_PRODUCT_7709)
     61 	    && (cpu_product != CPU_PRODUCT_7709A)
     62 	    && (cpu_product != CPU_PRODUCT_7706))
     63 		return (0);
     64 
     65 	if (strcmp(cf->cf_name, "adc") != 0)
     66 		return (0);
     67 
     68 	return (1);
     69 }
     70 
     71 
     72 static void
     73 adc_attach(device_t parent, device_t self, void *aux)
     74 {
     75 
     76 	ADC_(CSR) = 0;
     77 	ADC_(CR) = 0;
     78 
     79 	aprint_naive("\n");
     80 	aprint_normal("\n");
     81 
     82 	config_search(self, NULL,
     83 	    CFARGS(.search = adc_search));
     84 
     85 	/*
     86 	 * XXX: TODO: provide hooks to manage power.  For now register
     87 	 * null hooks which is no worse than before.
     88 	 *
     89 	 * NB: ADC registers are reset by standby!
     90 	 */
     91 	if (!pmf_device_register(self, NULL, NULL))
     92 		aprint_error_dev(self, "unable to establish power handler\n");
     93 }
     94 
     95 
     96 static int
     97 adc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
     98 {
     99 
    100 	if (config_probe(parent, cf, NULL))
    101 		config_attach(parent, cf, NULL, adc_print, CFARGS_NONE);
    102 
    103 	return (0);
    104 }
    105 
    106 
    107 static int
    108 adc_print(void *aux, const char *pnp)
    109 {
    110 
    111 	return (pnp ? QUIET : UNCONF);
    112 }
    113 
    114 
    115 /*
    116  * Sample specified ADC channel.
    117  * Must be called at spltty().
    118  */
    119 int
    120 adc_sample_channel(int chan)
    121 {
    122 	volatile uint8_t *hireg;
    123 	volatile uint8_t *loreg;
    124 	int regoff;
    125 	uint8_t csr;
    126 	int timo;
    127 #ifdef DIAGNOSTIC
    128 	uint8_t cr;
    129 	char bits[128];
    130 #endif
    131 	if ((chan < 0) || (chan >= 8))
    132 		return (-1);
    133 
    134 	regoff = (chan & 0x03) << 2;
    135 	hireg = (volatile uint8_t *)(SH7709_ADDRAH + regoff);
    136 	loreg = (volatile uint8_t *)(SH7709_ADDRAL + regoff);
    137 
    138 	/* the loop below typically takes 27 iterations on j680 */
    139 	timo = 300;
    140 
    141 #ifdef DIAGNOSTIC
    142 	csr = ADC_(CSR);
    143 	if ((csr & SH7709_ADCSR_ADST) != 0) {
    144 		/* another conversion is in progress?! */
    145 	        snprintb(bits, sizeof(bits), SH7709_ADCSR_BITS, csr);
    146 		printf("adc_sample_channel(%d): CSR=%s", chan, bits);
    147 		cr = ADC_(CR);
    148 		cr &= ~0x07;	/* three lower bits always read as 1s */
    149 	        snprintb(bits, sizeof(bits), SH7709_ADCR_BITS, cr);
    150 		printf(", CR=%s\n", bits);
    151 		return (-1);
    152 	}
    153 #endif
    154 
    155 	/* start scanning */
    156 	ADC_(CSR) = chan | SH7709_ADCSR_ADST | SH7709_ADCSR_CKS;
    157 
    158 	do {
    159 		csr = ADC_(CSR);
    160 		if (timo-- == 0)
    161 			break;
    162 	} while ((csr & SH7709_ADCSR_ADF) == 0);
    163 
    164 	/* stop scanning */
    165 	csr &= ~(SH7709_ADCSR_ADF | SH7709_ADCSR_ADST);
    166 	ADC_(CSR) = csr;
    167 
    168 	if (timo <= 0) {
    169 		printf("adc_sample_channel(%d): timed out\n", chan);
    170 		return (-1);
    171 	}
    172 
    173 	/*
    174 	 * 10 bit of data: bits [9..2] in the high register, bits
    175 	 * [1..0] in the bits [7..6] of the low register.
    176 	 */
    177 	return (((*hireg << 8) | *loreg) >> 6);
    178 }
    179