ucbsnd.c revision 1.1 1 1.1 uch /* $NetBSD: ucbsnd.c,v 1.1 2000/01/12 14:56:21 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.1 uch
87 1.1 uch /*
88 1.1 uch * audio codec state machine
89 1.1 uch */
90 1.1 uch enum ucbsnd_state sa_state;
91 1.1 uch
92 1.1 uch int sa_snd_rate; /* passed down from SIB module */
93 1.1 uch int sa_tel_rate;
94 1.1 uch int sa_enabled;
95 1.1 uch void* sa_sf0ih;
96 1.1 uch void* sa_sndih;
97 1.1 uch int sa_retry;
98 1.1 uch int sa_cnt; /* misc counter */
99 1.1 uch
100 1.1 uch };
101 1.1 uch
102 1.1 uch int ucbsnd_match __P((struct device*, struct cfdata*, void*));
103 1.1 uch void ucbsnd_attach __P((struct device*, struct device*, void*));
104 1.1 uch
105 1.1 uch int ucbsnd_exec_output __P((void*));
106 1.1 uch int ucbsnd_busy __P((void*));
107 1.1 uch
108 1.1 uch void ucbsnd_sound_init __P((struct ucbsnd_softc*));
109 1.1 uch void __ucbsnd_sound_click __P((tx_sound_tag_t));
110 1.1 uch
111 1.1 uch struct cfattach ucbsnd_ca = {
112 1.1 uch sizeof(struct ucbsnd_softc), ucbsnd_match, ucbsnd_attach
113 1.1 uch };
114 1.1 uch
115 1.1 uch int
116 1.1 uch ucbsnd_match(parent, cf, aux)
117 1.1 uch struct device *parent;
118 1.1 uch struct cfdata *cf;
119 1.1 uch void *aux;
120 1.1 uch {
121 1.1 uch return 1;
122 1.1 uch }
123 1.1 uch
124 1.1 uch void
125 1.1 uch ucbsnd_attach(parent, self, aux)
126 1.1 uch struct device *parent;
127 1.1 uch struct device *self;
128 1.1 uch void *aux;
129 1.1 uch {
130 1.1 uch struct ucb1200_attach_args *ucba = aux;
131 1.1 uch struct ucbsnd_softc *sc = (void*)self;
132 1.1 uch tx_chipset_tag_t tc;
133 1.1 uch
134 1.1 uch tc = sc->sc_tc = ucba->ucba_tc;
135 1.1 uch sc->sc_sib = ucba->ucba_sib;
136 1.1 uch sc->sc_ucb = ucba->ucba_ucb;
137 1.1 uch #define SOUND_TEST
138 1.1 uch #ifdef SOUND_TEST
139 1.1 uch ucbsnd_sound_init(sc);
140 1.1 uch #endif
141 1.1 uch sc->sa_snd_rate = ucba->ucba_snd_rate;
142 1.1 uch sc->sa_tel_rate = ucba->ucba_tel_rate;
143 1.1 uch
144 1.1 uch #define KHZ(a) ((a) / 1000), (((a) % 1000))
145 1.1 uch printf(": audio %d.%03d kHz telecom %d.%03d kHz",
146 1.1 uch KHZ((tx39sib_clock(sc->sc_sib) * 2) /
147 1.1 uch (sc->sa_snd_rate * 64)),
148 1.1 uch KHZ((tx39sib_clock(sc->sc_sib) * 2) /
149 1.1 uch (sc->sa_tel_rate * 64)));
150 1.1 uch
151 1.1 uch ucb1200_state_install(parent, ucbsnd_busy, self,
152 1.1 uch UCB1200_SND_MODULE);
153 1.1 uch
154 1.1 uch printf("\n");
155 1.1 uch }
156 1.1 uch
157 1.1 uch int
158 1.1 uch ucbsnd_busy(arg)
159 1.1 uch void *arg;
160 1.1 uch {
161 1.1 uch struct ucbsnd_softc *sc = arg;
162 1.1 uch
163 1.1 uch return sc->sa_state != UCBSND_IDLE;
164 1.1 uch }
165 1.1 uch
166 1.1 uch int
167 1.1 uch ucbsnd_exec_output(arg)
168 1.1 uch void *arg;
169 1.1 uch {
170 1.1 uch struct ucbsnd_softc *sc = arg;
171 1.1 uch tx_chipset_tag_t tc = sc->sc_tc;
172 1.1 uch txreg_t reg;
173 1.1 uch
174 1.1 uch switch (sc->sa_state) {
175 1.1 uch default:
176 1.1 uch panic("ucbsnd_exec_output: invalid state %d", sc->sa_state);
177 1.1 uch /* NOTREACHED */
178 1.1 uch break;
179 1.1 uch
180 1.1 uch case UCBSND_IDLE:
181 1.1 uch /* nothing to do */
182 1.1 uch return 0;
183 1.1 uch
184 1.1 uch case UCBSND_INIT:
185 1.1 uch sc->sa_sf0ih = tx_intr_establish(
186 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
187 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
188 1.1 uch
189 1.1 uch sc->sa_state = UCBSND_ENABLE_SAMPLERATE;
190 1.1 uch return 0;
191 1.1 uch
192 1.1 uch case UCBSND_ENABLE_SAMPLERATE:
193 1.1 uch /* Enable UCB1200 side sample rate */
194 1.1 uch reg = TX39_SIBSF0_WRITE;
195 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLA_REG);
196 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_rate);
197 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
198 1.1 uch
199 1.1 uch sc->sa_state = UCBSND_ENABLE_OUTPUTPATH;
200 1.1 uch return 0;
201 1.1 uch
202 1.1 uch case UCBSND_ENABLE_OUTPUTPATH:
203 1.1 uch /* Enable UCB1200 side */
204 1.1 uch reg = TX39_SIBSF0_WRITE;
205 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
206 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg,
207 1.1 uch UCB1200_AUDIOCTRLB_OUTEN);
208 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
209 1.1 uch
210 1.1 uch /* Enable SIB side */
211 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
212 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG,
213 1.1 uch reg | TX39_SIBCTRL_ENSND);
214 1.1 uch
215 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER0;
216 1.1 uch sc->sa_retry = 10;
217 1.1 uch return 0;
218 1.1 uch
219 1.1 uch case UCBSND_ENABLE_SPEAKER0:
220 1.1 uch /* Speaker on */
221 1.1 uch
222 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
223 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
224 1.1 uch
225 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER1;
226 1.1 uch return 0;
227 1.1 uch
228 1.1 uch case UCBSND_ENABLE_SPEAKER1:
229 1.1 uch reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
230 1.1 uch if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
231 1.1 uch --sc->sa_retry > 0) {
232 1.1 uch
233 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER0;
234 1.1 uch return 0;
235 1.1 uch }
236 1.1 uch
237 1.1 uch if (sc->sa_retry <= 0) {
238 1.1 uch printf("ucbsnd_exec_output: subframe0 busy\n");
239 1.1 uch
240 1.1 uch sc->sa_state = UCBSND_IDLE;
241 1.1 uch return 0;
242 1.1 uch }
243 1.1 uch
244 1.1 uch reg |= TX39_SIBSF0_WRITE;
245 1.1 uch reg |= UCB1200_IO_DATA_SPEAKER;
246 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
247 1.1 uch
248 1.1 uch sc->sa_state = UCBSND_TRANSITION_PIO;
249 1.1 uch sc->sa_cnt = 0;
250 1.1 uch return 0;
251 1.1 uch
252 1.1 uch case UCBSND_TRANSITION_PIO:
253 1.1 uch /* change interrupt source */
254 1.1 uch tx_intr_disestablish(tc, sc->sa_sf0ih);
255 1.1 uch
256 1.1 uch sc->sa_sndih = tx_intr_establish(
257 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SNDININT),
258 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
259 1.1 uch sc->sa_enabled = 1;
260 1.1 uch
261 1.1 uch sc->sa_state = UCBSND_PIO;
262 1.1 uch return 0;
263 1.1 uch
264 1.1 uch case UCBSND_PIO:
265 1.1 uch sc->sa_cnt++;
266 1.1 uch
267 1.1 uch if ((sc->sa_cnt % 20) == 0)
268 1.1 uch tx_conf_write(tc, TX39_SIBSNDHOLD_REG, sc->sa_cnt + 5000);
269 1.1 uch else
270 1.1 uch tx_conf_write(tc, TX39_SIBSNDHOLD_REG, 0);
271 1.1 uch
272 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
273 1.1 uch
274 1.1 uch if (sc->sa_cnt > 1000)
275 1.1 uch sc->sa_state = UCBSND_TRANSITION_DISABLE;
276 1.1 uch
277 1.1 uch return 0;
278 1.1 uch
279 1.1 uch case UCBSND_TRANSITION_DISABLE:
280 1.1 uch /* change interrupt source */
281 1.1 uch sc->sa_enabled = 0;
282 1.1 uch tx_intr_disestablish(tc, sc->sa_sndih);
283 1.1 uch sc->sa_sf0ih = tx_intr_establish(
284 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
285 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
286 1.1 uch
287 1.1 uch sc->sa_state = UCBSND_DISABLE_OUTPUTPATH;
288 1.1 uch return 0;
289 1.1 uch
290 1.1 uch case UCBSND_DISABLE_OUTPUTPATH:
291 1.1 uch /* disable codec output path and mute */
292 1.1 uch reg = TX39_SIBSF0_WRITE;
293 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
294 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg, UCB1200_AUDIOCTRLB_MUTE);
295 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
296 1.1 uch
297 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER0;
298 1.1 uch sc->sa_retry = 10;
299 1.1 uch return 0;
300 1.1 uch
301 1.1 uch case UCBSND_DISABLE_SPEAKER0:
302 1.1 uch /* Speaker off */
303 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
304 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
305 1.1 uch
306 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER1;
307 1.1 uch return 0;
308 1.1 uch
309 1.1 uch case UCBSND_DISABLE_SPEAKER1:
310 1.1 uch reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
311 1.1 uch if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
312 1.1 uch --sc->sa_retry > 0) {
313 1.1 uch
314 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER0;
315 1.1 uch return 0;
316 1.1 uch }
317 1.1 uch
318 1.1 uch if (sc->sa_retry <= 0) {
319 1.1 uch printf("ucbsnd_exec_output: subframe0 busy\n");
320 1.1 uch
321 1.1 uch sc->sa_state = UCBSND_IDLE;
322 1.1 uch return 0;
323 1.1 uch }
324 1.1 uch
325 1.1 uch reg |= TX39_SIBSF0_WRITE;
326 1.1 uch reg &= ~UCB1200_IO_DATA_SPEAKER;
327 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
328 1.1 uch
329 1.1 uch sc->sa_state = UCBSND_DISABLE_SIB;
330 1.1 uch return 0;
331 1.1 uch
332 1.1 uch case UCBSND_DISABLE_SIB:
333 1.1 uch /* Disable SIB side */
334 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
335 1.1 uch reg &= ~TX39_SIBCTRL_ENSND;
336 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
337 1.1 uch
338 1.1 uch /* end audio disable sequence */
339 1.1 uch tx_intr_disestablish(tc, sc->sa_sf0ih);
340 1.1 uch sc->sa_state = UCBSND_IDLE;
341 1.1 uch
342 1.1 uch return 0;
343 1.1 uch }
344 1.1 uch
345 1.1 uch return 0;
346 1.1 uch }
347 1.1 uch
348 1.1 uch /*
349 1.1 uch * global sound interface.
350 1.1 uch */
351 1.1 uch void
352 1.1 uch ucbsnd_sound_init(sc)
353 1.1 uch struct ucbsnd_softc *sc;
354 1.1 uch {
355 1.1 uch tx_sound_tag_t ts = &sc->sc_tag;
356 1.1 uch tx_chipset_tag_t tc = sc->sc_tc;
357 1.1 uch
358 1.1 uch ts->ts_v = sc;
359 1.1 uch ts->ts_click = __ucbsnd_sound_click;
360 1.1 uch
361 1.1 uch tx_conf_register_sound(tc, ts);
362 1.1 uch }
363 1.1 uch
364 1.1 uch void
365 1.1 uch __ucbsnd_sound_click(arg)
366 1.1 uch tx_sound_tag_t arg;
367 1.1 uch {
368 1.1 uch struct ucbsnd_softc *sc = (void*)arg;
369 1.1 uch
370 1.1 uch if (sc->sa_state == UCBSND_IDLE) {
371 1.1 uch sc->sa_state = UCBSND_INIT;
372 1.1 uch ucbsnd_exec_output((void*)sc);
373 1.1 uch }
374 1.1 uch }
375 1.1 uch
376 1.1 uch
377