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