Home | History | Annotate | Line # | Download | only in dev
ucbsnd.c revision 1.1
      1 /*	$NetBSD: ucbsnd.c,v 1.1 2000/01/12 14:56:21 uch Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2000, by UCHIYAMA Yasushi
      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. The name of the developer may NOT be used to endorse or promote products
     13  *    derived from this software without specific prior written permission.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  *
     27  */
     28 
     29 /*
     30  * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
     31  *	Audio codec part.
     32  */
     33 #define UCBSNDDEBUG
     34 
     35 #include "opt_tx39_debug.h"
     36 #include "opt_use_poll.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/intr.h>
     44 
     45 #include <hpcmips/tx/tx39var.h>
     46 #include <hpcmips/tx/tx39sibvar.h>
     47 #include <hpcmips/tx/tx39sibreg.h>
     48 #include <hpcmips/tx/tx39icureg.h>
     49 #include <hpcmips/tx/txsnd.h>
     50 
     51 #include <hpcmips/dev/ucb1200var.h>
     52 #include <hpcmips/dev/ucb1200reg.h>
     53 
     54 #ifdef UCBSNDDEBUG
     55 int	ucbsnd_debug = 1;
     56 #define	DPRINTF(arg) if (ucbsnd_debug) printf arg;
     57 #define	DPRINTFN(n, arg) if (ucbsnd_debug > (n)) printf arg;
     58 #else
     59 #define	DPRINTF(arg)
     60 #define DPRINTFN(n, arg)
     61 #endif
     62 
     63 enum ucbsnd_state {
     64 /* 0 */	UCBSND_IDLE,
     65 /* 1 */	UCBSND_INIT,
     66 /* 2 */ UCBSND_ENABLE_SAMPLERATE,
     67 /* 3 */ UCBSND_ENABLE_OUTPUTPATH,
     68 /* 5 */ UCBSND_ENABLE_SPEAKER0,
     69 /* 6 */ UCBSND_ENABLE_SPEAKER1,
     70 /* 7 */ UCBSND_TRANSITION_PIO,
     71 /* 8 */ UCBSND_PIO,
     72 /* 9 */ UCBSND_TRANSITION_DISABLE,
     73 /*10 */ UCBSND_DISABLE_OUTPUTPATH,
     74 /*11 */ UCBSND_DISABLE_SPEAKER0,
     75 /*12 */ UCBSND_DISABLE_SPEAKER1,
     76 /*13 */	UCBSND_DISABLE_SIB
     77 };
     78 
     79 struct ucbsnd_softc {
     80 	struct device		sc_dev;
     81 	struct device		*sc_sib; /* parent (TX39 SIB module) */
     82 	struct device		*sc_ucb; /* parent (UCB1200 module) */
     83 	tx_chipset_tag_t	sc_tc;
     84 
     85 	struct	tx_sound_tag sc_tag;
     86 
     87 	/*
     88 	 *  audio codec state machine
     89 	 */
     90 	enum ucbsnd_state sa_state;
     91 
     92 	int		sa_snd_rate; /* passed down from SIB module */
     93 	int		sa_tel_rate;
     94 	int		sa_enabled;
     95 	void*		sa_sf0ih;
     96 	void*		sa_sndih;
     97 	int		sa_retry;
     98 	int		sa_cnt; /* misc counter */
     99 
    100 };
    101 
    102 int	ucbsnd_match	__P((struct device*, struct cfdata*, void*));
    103 void	ucbsnd_attach	__P((struct device*, struct device*, void*));
    104 
    105 int	ucbsnd_exec_output	__P((void*));
    106 int	ucbsnd_busy __P((void*));
    107 
    108 void	ucbsnd_sound_init	__P((struct ucbsnd_softc*));
    109 void	__ucbsnd_sound_click	__P((tx_sound_tag_t));
    110 
    111 struct cfattach ucbsnd_ca = {
    112 	sizeof(struct ucbsnd_softc), ucbsnd_match, ucbsnd_attach
    113 };
    114 
    115 int
    116 ucbsnd_match(parent, cf, aux)
    117 	struct device *parent;
    118 	struct cfdata *cf;
    119 	void *aux;
    120 {
    121 	return 1;
    122 }
    123 
    124 void
    125 ucbsnd_attach(parent, self, aux)
    126 	struct device *parent;
    127 	struct device *self;
    128 	void *aux;
    129 {
    130 	struct ucb1200_attach_args *ucba = aux;
    131 	struct ucbsnd_softc *sc = (void*)self;
    132 	tx_chipset_tag_t tc;
    133 
    134 	tc = sc->sc_tc = ucba->ucba_tc;
    135 	sc->sc_sib = ucba->ucba_sib;
    136 	sc->sc_ucb = ucba->ucba_ucb;
    137 #define SOUND_TEST
    138 #ifdef SOUND_TEST
    139 	ucbsnd_sound_init(sc);
    140 #endif
    141 	sc->sa_snd_rate = ucba->ucba_snd_rate;
    142 	sc->sa_tel_rate = ucba->ucba_tel_rate;
    143 
    144 #define KHZ(a) ((a) / 1000), (((a) % 1000))
    145 	printf(": audio %d.%03d kHz telecom %d.%03d kHz",
    146 	       KHZ((tx39sib_clock(sc->sc_sib) * 2) /
    147 		   (sc->sa_snd_rate * 64)),
    148 	       KHZ((tx39sib_clock(sc->sc_sib) * 2) /
    149 		   (sc->sa_tel_rate * 64)));
    150 
    151 	ucb1200_state_install(parent, ucbsnd_busy, self,
    152 			      UCB1200_SND_MODULE);
    153 
    154 	printf("\n");
    155 }
    156 
    157 int
    158 ucbsnd_busy(arg)
    159 	void *arg;
    160 {
    161 	struct ucbsnd_softc *sc = arg;
    162 
    163 	return sc->sa_state != UCBSND_IDLE;
    164 }
    165 
    166 int
    167 ucbsnd_exec_output(arg)
    168 	void *arg;
    169 {
    170 	struct ucbsnd_softc *sc = arg;
    171 	tx_chipset_tag_t tc = sc->sc_tc;
    172 	txreg_t reg;
    173 
    174 	switch (sc->sa_state) {
    175 	default:
    176 		panic("ucbsnd_exec_output: invalid state %d", sc->sa_state);
    177 		/* NOTREACHED */
    178 		break;
    179 
    180 	case UCBSND_IDLE:
    181 		/* nothing to do */
    182 		return 0;
    183 
    184 	case UCBSND_INIT:
    185 		sc->sa_sf0ih = tx_intr_establish(
    186 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
    187 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    188 
    189 		sc->sa_state = UCBSND_ENABLE_SAMPLERATE;
    190 		return 0;
    191 
    192 	case UCBSND_ENABLE_SAMPLERATE:
    193 		/* Enable UCB1200 side sample rate */
    194 		reg = TX39_SIBSF0_WRITE;
    195 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLA_REG);
    196 		reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_rate);
    197 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    198 
    199 		sc->sa_state = UCBSND_ENABLE_OUTPUTPATH;
    200 		return 0;
    201 
    202 	case UCBSND_ENABLE_OUTPUTPATH:
    203 		/* Enable UCB1200 side */
    204 		reg = TX39_SIBSF0_WRITE;
    205 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
    206 		reg = TX39_SIBSF0_REGDATA_SET(reg,
    207 					      UCB1200_AUDIOCTRLB_OUTEN);
    208 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    209 
    210 		/* Enable SIB side */
    211 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
    212 		tx_conf_write(tc, TX39_SIBCTRL_REG,
    213 			      reg | TX39_SIBCTRL_ENSND);
    214 
    215 		sc->sa_state = UCBSND_ENABLE_SPEAKER0;
    216 		sc->sa_retry = 10;
    217 		return 0;
    218 
    219 	case UCBSND_ENABLE_SPEAKER0:
    220 		/* Speaker on */
    221 
    222 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
    223 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    224 
    225 		sc->sa_state = UCBSND_ENABLE_SPEAKER1;
    226 		return 0;
    227 
    228 	case UCBSND_ENABLE_SPEAKER1:
    229 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
    230 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
    231 		    --sc->sa_retry > 0) {
    232 
    233 			sc->sa_state = UCBSND_ENABLE_SPEAKER0;
    234 			return 0;
    235 		}
    236 
    237 		if (sc->sa_retry <= 0) {
    238 			printf("ucbsnd_exec_output: subframe0 busy\n");
    239 
    240 			sc->sa_state = UCBSND_IDLE;
    241 			return 0;
    242 		}
    243 
    244 		reg |= TX39_SIBSF0_WRITE;
    245 		reg |= UCB1200_IO_DATA_SPEAKER;
    246 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    247 
    248 		sc->sa_state = UCBSND_TRANSITION_PIO;
    249 		sc->sa_cnt = 0;
    250 		return 0;
    251 
    252 	case UCBSND_TRANSITION_PIO:
    253 		/* change interrupt source */
    254 		tx_intr_disestablish(tc, sc->sa_sf0ih);
    255 
    256 		sc->sa_sndih = tx_intr_establish(
    257 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SNDININT),
    258 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    259 		sc->sa_enabled = 1;
    260 
    261 		sc->sa_state = UCBSND_PIO;
    262 		return 0;
    263 
    264 	case UCBSND_PIO:
    265 		sc->sa_cnt++;
    266 
    267 		if ((sc->sa_cnt % 20) == 0)
    268 			tx_conf_write(tc, TX39_SIBSNDHOLD_REG, sc->sa_cnt + 5000);
    269 		else
    270 			tx_conf_write(tc, TX39_SIBSNDHOLD_REG, 0);
    271 
    272 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
    273 
    274 		if (sc->sa_cnt > 1000)
    275 			sc->sa_state = UCBSND_TRANSITION_DISABLE;
    276 
    277 		return 0;
    278 
    279 	case UCBSND_TRANSITION_DISABLE:
    280 		/* change interrupt source */
    281 		sc->sa_enabled = 0;
    282 		tx_intr_disestablish(tc, sc->sa_sndih);
    283 		sc->sa_sf0ih = tx_intr_establish(
    284 			tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
    285 			IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
    286 
    287 		sc->sa_state = UCBSND_DISABLE_OUTPUTPATH;
    288 		return 0;
    289 
    290 	case UCBSND_DISABLE_OUTPUTPATH:
    291 		/* disable codec output path and mute */
    292 		reg = TX39_SIBSF0_WRITE;
    293 		reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
    294 		reg = TX39_SIBSF0_REGDATA_SET(reg, UCB1200_AUDIOCTRLB_MUTE);
    295 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    296 
    297 		sc->sa_state = UCBSND_DISABLE_SPEAKER0;
    298 		sc->sa_retry = 10;
    299 		return 0;
    300 
    301 	case UCBSND_DISABLE_SPEAKER0:
    302 		/* Speaker off */
    303 		reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
    304 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    305 
    306 		sc->sa_state = UCBSND_DISABLE_SPEAKER1;
    307 		return 0;
    308 
    309 	case UCBSND_DISABLE_SPEAKER1:
    310 		reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
    311 		if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
    312 		    --sc->sa_retry > 0) {
    313 
    314 			sc->sa_state = UCBSND_DISABLE_SPEAKER0;
    315 			return 0;
    316 		}
    317 
    318 		if (sc->sa_retry <= 0) {
    319 			printf("ucbsnd_exec_output: subframe0 busy\n");
    320 
    321 			sc->sa_state = UCBSND_IDLE;
    322 			return 0;
    323 		}
    324 
    325 		reg |= TX39_SIBSF0_WRITE;
    326 		reg &= ~UCB1200_IO_DATA_SPEAKER;
    327 		tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
    328 
    329 		sc->sa_state = UCBSND_DISABLE_SIB;
    330 		return 0;
    331 
    332 	case UCBSND_DISABLE_SIB:
    333 		/* Disable SIB side */
    334 		reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
    335 		reg &= ~TX39_SIBCTRL_ENSND;
    336 		tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
    337 
    338 		/* end audio disable sequence */
    339 		tx_intr_disestablish(tc, sc->sa_sf0ih);
    340 		sc->sa_state = UCBSND_IDLE;
    341 
    342 		return 0;
    343 	}
    344 
    345 	return 0;
    346 }
    347 
    348 /*
    349  * global sound interface.
    350  */
    351 void
    352 ucbsnd_sound_init(sc)
    353 	struct ucbsnd_softc *sc;
    354 {
    355 	tx_sound_tag_t ts = &sc->sc_tag;
    356 	tx_chipset_tag_t tc = sc->sc_tc;
    357 
    358 	ts->ts_v = sc;
    359 	ts->ts_click = __ucbsnd_sound_click;
    360 
    361 	tx_conf_register_sound(tc, ts);
    362 }
    363 
    364 void
    365 __ucbsnd_sound_click(arg)
    366 	tx_sound_tag_t arg;
    367 {
    368 	struct ucbsnd_softc *sc = (void*)arg;
    369 
    370 	if (sc->sa_state == UCBSND_IDLE) {
    371 		sc->sa_state = UCBSND_INIT;
    372 		ucbsnd_exec_output((void*)sc);
    373 	}
    374 }
    375 
    376 
    377