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