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