Home | History | Annotate | Line # | Download | only in dev
adc.c revision 1.5
      1 /*	$NetBSD: adc.c,v 1.5 2005/08/26 13:19:37 drochner 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.5 2005/08/26 13:19:37 drochner 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 #ifdef GPROF
     39 #include <sys/gmon.h>
     40 #endif
     41 
     42 #include <sh3/adcreg.h>
     43 #include <sh3/dev/adcvar.h>
     44 
     45 #define ADC_(x)    (*((volatile uint8_t *)SH7709_AD ## x))
     46 
     47 
     48 struct adc_softc {
     49 	struct device sc_dev;
     50 };
     51 
     52 static int	adc_match(struct device *, struct cfdata *, void *);
     53 static void	adc_attach(struct device *, struct device *, void *);
     54 
     55 CFATTACH_DECL(adc, sizeof(struct adc_softc),
     56     adc_match, adc_attach, NULL, NULL);
     57 
     58 static int	adc_search(struct device *, struct cfdata *,
     59 			   const int *, void *);
     60 static int	adc_print(void *, const char *);
     61 
     62 
     63 static int
     64 adc_match(struct device *parent, struct cfdata *cfp, void *aux)
     65 {
     66 
     67 	/* REMINDER: also in 7727 and 7729 */
     68 	if ((cpu_product != CPU_PRODUCT_7709)
     69 	    && (cpu_product != CPU_PRODUCT_7709A))
     70 		return (0);
     71 
     72 	if (strcmp(cfp->cf_name, "adc") != 0)
     73 		return (0);
     74 
     75 	return (1);
     76 }
     77 
     78 
     79 static void
     80 adc_attach(struct device *parent, struct device *self, void *aux)
     81 {
     82 	/* struct adc_softc *sc = (struct adc_softc *)self; */
     83 
     84 	ADC_(CSR) = 0;
     85 	ADC_(CR) = 0;
     86 
     87 	printf("\n");
     88 	config_search_ia(adc_search, self, "adc", NULL);
     89 }
     90 
     91 
     92 static int
     93 adc_search(struct device *parent, struct cfdata *cf,
     94 	   const int *ldesc, void *aux)
     95 {
     96 
     97 	if (config_match(parent, cf, NULL) > 0)
     98 		config_attach(parent, cf, NULL, adc_print);
     99 
    100 	return (0);
    101 }
    102 
    103 
    104 static int
    105 adc_print(void *aux, const char *pnp)
    106 {
    107 
    108 	return (pnp ? QUIET : UNCONF);
    109 }
    110 
    111 
    112 /*
    113  * Sample specified ADC channel.
    114  * Must be called at spltty().
    115  */
    116 int
    117 adc_sample_channel(int chan)
    118 {
    119 	volatile uint8_t *hireg;
    120 	volatile uint8_t *loreg;
    121 	int regoff;
    122 	uint8_t csr;
    123 	int timo;
    124 #ifdef DIAGNOSTIC
    125 	uint8_t cr;
    126 	char bits[128];
    127 #endif
    128 	if ((chan < 0) || (chan >= 8))
    129 		return (-1);
    130 
    131 	regoff = (chan & 0x03) << 2;
    132 	hireg = (volatile uint8_t *)(SH7709_ADDRAH + regoff);
    133 	loreg = (volatile uint8_t *)(SH7709_ADDRAL + regoff);
    134 
    135 	/* the loop below typically takes 27 iterations on j680 */
    136 	timo = 300;
    137 
    138 #ifdef DIAGNOSTIC
    139 	csr = ADC_(CSR);
    140 	if ((csr & SH7709_ADCSR_ADST) != 0) {
    141 		/* another conversion is in progress?! */
    142 		printf("adc_sample_channel(%d): CSR=%s", chan,
    143 		       bitmask_snprintf(csr, SH7709_ADCSR_BITS,
    144 					bits, sizeof(bits)));
    145 		cr = ADC_(CR);
    146 		cr &= ~0x07;	/* three lower bits always read as 1s */
    147 		printf(", CR=%s\n",
    148 		       bitmask_snprintf(cr, SH7709_ADCR_BITS,
    149 					bits, sizeof(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