tms320av110.c revision 1.18.28.1 1 /* $NetBSD: tms320av110.c,v 1.18.28.1 2007/02/27 14:16:03 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Machine independent part of TMS320AV110 driver.
41 *
42 * Currently, only minimum support for audio output. For audio/video
43 * synchronization, more is needed.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: tms320av110.c,v 1.18.28.1 2007/02/27 14:16:03 ad Exp $");
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/device.h>
53 #include <sys/proc.h>
54
55 #include <sys/audioio.h>
56 #include <dev/audio_if.h>
57
58 #include <dev/ic/tms320av110reg.h>
59 #include <dev/ic/tms320av110var.h>
60
61 #include <machine/bus.h>
62
63 int tav_open(void *, int);
64 void tav_close(void *);
65 int tav_drain(void *);
66 int tav_query_encoding(void *, struct audio_encoding *);
67 int tav_set_params(void *, int, int, audio_params_t *, audio_params_t *,
68 stream_filter_list_t *, stream_filter_list_t *);
69 int tav_round_blocksize(void *, int, int, const audio_params_t *);
70 int tav_init_output(void *, void *, int);
71 int tav_start_output(void *, void *, int, void (*)(void *), void *);
72 int tav_start_input(void *, void *, int, void (*)(void *), void *);
73 int tav_halt_output(void *);
74 int tav_halt_input(void *);
75 int tav_speaker_ctl(void *, int);
76 int tav_getdev(void *, struct audio_device *);
77 int tav_setfd(void *, int);
78 int tav_set_port(void *, mixer_ctrl_t *);
79 int tav_get_port(void *, mixer_ctrl_t *);
80 int tav_query_devinfo(void *, mixer_devinfo_t *);
81 int tav_get_props(void *);
82
83 const struct audio_hw_if tav_audio_if = {
84 tav_open,
85 tav_close,
86 0 /* tav_drain*/, /* optional */
87 tav_query_encoding,
88 tav_set_params,
89 tav_round_blocksize,
90 0 /* commit_settings */, /* optional */
91 tav_init_output, /* optional */
92 0 /* tav_init_input */, /* optional */
93 tav_start_output,
94 tav_start_input,
95 tav_halt_output,
96 tav_halt_input,
97 tav_speaker_ctl, /* optional */
98 tav_getdev,
99 0 /* setfd */, /* optional */
100 tav_set_port,
101 tav_get_port,
102 tav_query_devinfo,
103 0 /* alloc */, /* optional */
104 0 /* free */, /* optional */
105 0 /* round_buffersize */, /* optional */
106 0 /* mappage */, /* optional */
107 tav_get_props,
108 0 /* dev_ioctl */ /* optional */,
109 tav_get_lock,
110 };
111
112 void
113 tms320av110_attach_mi(struct tav_softc *sc)
114 {
115 bus_space_tag_t iot;
116 bus_space_handle_t ioh;
117
118 iot = sc->sc_iot;
119 ioh = sc->sc_ioh;
120 tav_write_byte(iot, ioh, TAV_RESET, 1);
121 while (tav_read_byte(iot, ioh, TAV_RESET))
122 delay(250);
123
124 tav_write_byte(iot, ioh, TAV_PCM_ORD, sc->sc_pcm_ord);
125 tav_write_byte(iot, ioh, TAV_PCM_18, sc->sc_pcm_18);
126 tav_write_byte(iot, ioh, TAV_DIF, sc->sc_dif);
127 tav_write_byte(iot, ioh, TAV_PCM_DIV, sc->sc_pcm_div);
128
129 printf(": chip rev. %d, %d bytes buffer\n",
130 tav_read_byte(iot, ioh, TAV_VERSION),
131 TAV_DRAM_SIZE(tav_read_byte(iot, ioh, TAV_DRAM_EXT)));
132
133 tav_write_byte(iot, ioh, TAV_AUD_ID_EN, 0);
134 tav_write_byte(iot, ioh, TAV_SKIP, 0);
135 tav_write_byte(iot, ioh, TAV_REPEAT, 0);
136 tav_write_byte(iot, ioh, TAV_MUTE, 0);
137 tav_write_byte(iot, ioh, TAV_PLAY, 1);
138 tav_write_byte(iot, ioh, TAV_SYNC_ECM, 0);
139 tav_write_byte(iot, ioh, TAV_CRC_ECM, 0);
140 tav_write_byte(iot, ioh, TAV_ATTEN_L, 0);
141 tav_write_byte(iot, ioh, TAV_ATTEN_R, 0);
142 tav_write_short(iot, ioh, TAV_FREE_FORM, 0);
143 tav_write_byte(iot, ioh, TAV_SIN_EN, 0);
144 tav_write_byte(iot, ioh, TAV_SYNC_ECM, TAV_ECM_REPEAT);
145 tav_write_byte(iot, ioh, TAV_CRC_ECM, TAV_ECM_REPEAT);
146
147 audio_attach_mi(&tav_audio_if, sc, &sc->sc_dev);
148 }
149
150 int
151 tms320av110_intr(void *p)
152 {
153 struct tav_softc *sc;
154 uint16_t intlist;
155
156 sc = p;
157
158 mutex_enter(&sc->sc_lock);
159
160 intlist = tav_read_short(sc->sc_iot, sc->sc_ioh, TAV_INTR)
161 /* & tav_read_short(sc->sc_iot, sc->sc_ioh, TAV_INTR_EN)*/;
162
163 if (!intlist) {
164 mutex_exit(&sc->sc_lock);
165 return 0;
166 }
167
168 /* ack now, so that we don't miss later interrupts */
169 if (sc->sc_intack)
170 (sc->sc_intack)(sc);
171
172 if (intlist & TAV_INTR_LOWWATER) {
173 (*sc->sc_intr)(sc->sc_intrarg);
174 }
175
176 if (intlist & TAV_INTR_PCM_OUTPUT_UNDERFLOW) {
177 wakeup(sc);
178 }
179
180 mutex_exit(&sc->sc_lock);
181
182 return 1;
183 }
184
185 struct audio_encoding tav_encodings[] = {
186 {0, AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM, 1, 0,},
187 {1, AudioEmpeg_l2_packets, AUDIO_ENCODING_MPEG_L2_PACKETS, 1, 0,},
188 {2, AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM, 1, 0,},
189 {3, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16, 0,},
190 };
191
192 int
193 tav_open(void *hdl, int flags)
194 {
195
196 /* dummy */
197 return 0;
198 }
199
200 void
201 tav_close(void *hdl)
202 {
203 struct tav_softc *sc;
204 bus_space_tag_t iot;
205 bus_space_handle_t ioh;
206
207 sc = hdl;
208 iot = sc->sc_iot;
209 ioh = sc->sc_ioh;
210
211 /* re"start" chip, also clears interrupts and interrupt enable */
212 tav_write_short(iot, ioh, TAV_INTR_EN, 0);
213 if (sc->sc_intack)
214 (*sc->sc_intack)(sc);
215 }
216
217 int
218 tav_drain(void *hdl)
219 {
220 struct tav_softc *sc;
221 bus_space_tag_t iot;
222 bus_space_handle_t ioh;
223 u_int16_t mask;
224
225 sc = hdl;
226 iot = sc->sc_iot;
227 ioh = sc->sc_ioh;
228
229 /*
230 * tsleep waiting for underflow interrupt.
231 */
232 if (tav_read_short(iot, ioh, TAV_BUFF)) {
233 mask = tav_read_short(iot, ioh, TAV_INTR_EN);
234 tav_write_short(iot, ioh, TAV_INTR_EN,
235 mask|TAV_INTR_PCM_OUTPUT_UNDERFLOW);
236
237 /* still more than zero? */
238 if (tav_read_short(iot, ioh, TAV_BUFF))
239 kpause("tavdrain", &sc->sc_lock, TRUE, 32*hz);
240
241 /* can be really that long for mpeg */
242
243 mask = tav_read_short(iot, ioh, TAV_INTR_EN);
244 tav_write_short(iot, ioh, TAV_INTR_EN,
245 mask & ~TAV_INTR_PCM_OUTPUT_UNDERFLOW);
246 }
247
248 return 0;
249 }
250
251 int
252 tav_query_encoding(void *hdl, struct audio_encoding *ae)
253 {
254 struct tav_softc *sc;
255
256 sc = hdl;
257 if (ae->index >= sizeof(tav_encodings)/sizeof(*ae))
258 return EINVAL;
259
260 *ae = tav_encodings[ae->index];
261
262 return 0;
263 }
264
265 int
266 tav_start_input(void *hdl, void *block, int bsize,
267 void (*intr)(void *), void *intrarg)
268 {
269
270 return ENOTTY;
271 }
272
273 int
274 tav_halt_input(void *hdl)
275 {
276
277 return ENOTTY;
278 }
279
280 int
281 tav_start_output(void *hdl, void *block, int bsize,
282 void (*intr)(void *), void *intrarg)
283 {
284 struct tav_softc *sc;
285 bus_space_tag_t iot;
286 bus_space_handle_t ioh;
287 uint8_t *ptr;
288 int count;
289
290 sc = hdl;
291 iot = sc->sc_iot;
292 ioh = sc->sc_ioh;
293 ptr = block;
294 count = bsize;
295
296 sc->sc_intr = intr;
297 sc->sc_intrarg = intrarg;
298
299 bus_space_write_multi_1(iot, ioh, TAV_DATAIN, ptr, count);
300 tav_write_short(iot, ioh, TAV_INTR_EN, TAV_INTR_LOWWATER);
301
302 return 0;
303 }
304
305 int
306 tav_init_output(void *hdl, void *buffer, int size)
307 {
308 struct tav_softc *sc;
309 bus_space_tag_t iot;
310 bus_space_handle_t ioh;
311
312 sc = hdl;
313 iot = sc->sc_iot;
314 ioh = sc->sc_ioh;
315
316 tav_write_byte(iot, ioh, TAV_PLAY, 1);
317 tav_write_byte(iot, ioh, TAV_MUTE, 0);
318
319 return 0;
320 }
321
322 int
323 tav_halt_output(void *hdl)
324 {
325 struct tav_softc *sc;
326 bus_space_tag_t iot;
327 bus_space_handle_t ioh;
328
329 sc = hdl;
330 iot = sc->sc_iot;
331 ioh = sc->sc_ioh;
332
333 tav_write_byte(iot, ioh, TAV_PLAY, 0);
334
335 return 0;
336 }
337
338 int
339 tav_getdev(void *hdl, struct audio_device *ret)
340 {
341 struct tav_softc *sc;
342 bus_space_tag_t iot;
343 bus_space_handle_t ioh;
344
345 sc = hdl;
346 iot = sc->sc_iot;
347 ioh = sc->sc_ioh;
348
349 strlcpy(ret->name, "tms320av110", sizeof(ret->name));
350 /* guaranteed to be <= 4 in length */
351 snprintf(ret->version, sizeof(ret->version), "%u",
352 tav_read_byte(iot, ioh, TAV_VERSION));
353 strlcpy(ret->config, sc->sc_dev.dv_xname, sizeof(ret->config));
354
355 return 0;
356 }
357
358 int
359 tav_round_blocksize(void *hdl, int size, int mode, const audio_params_t *param)
360 {
361 struct tav_softc *sc;
362 bus_space_tag_t iot;
363 bus_space_handle_t ioh;
364 int maxhalf;
365
366 sc = hdl;
367 iot = sc->sc_iot;
368 ioh = sc->sc_ioh;
369
370 maxhalf = TAV_DRAM_HSIZE(tav_read_byte(iot, ioh, TAV_DRAM_EXT));
371 if (size > maxhalf)
372 size = maxhalf;
373
374 /* XXX should round to 128 bytes limits for audio bypass */
375 size &= ~3;
376
377 tav_write_short(iot, ioh, TAV_BALE_LIM, size/8);
378
379 /* the buffer limits are in units of 4 bytes */
380 return (size);
381 }
382
383 int
384 tav_get_props(void *hdl)
385 {
386 return 0;
387 }
388
389 int
390 tav_set_params(void *hdl, int setmode, int usemode, audio_params_t *p,
391 audio_params_t *r, stream_filter_list_t *pfil, stream_filter_list_t *rfil)
392 {
393 struct tav_softc *sc;
394 bus_space_tag_t iot;
395 bus_space_handle_t ioh;
396
397 sc = hdl;
398 iot = sc->sc_iot;
399 ioh = sc->sc_ioh;
400
401 if (!(setmode & AUMODE_PLAY))
402 return 0;
403
404 if (p->encoding == AUDIO_ENCODING_ULAW)
405 p->encoding = AUDIO_ENCODING_MPEG_L2_STREAM;
406
407 switch(p->encoding) {
408 default:
409 return EINVAL;
410
411 case AUDIO_ENCODING_SLINEAR_BE:
412
413 /* XXX: todo: add 8bit and mono using software */
414 p->precision = 16;
415 p->channels = 2;
416
417 /* XXX: this might depend on the specific board.
418 should be handled by the backend */
419
420 p->sample_rate = 44100;
421
422 bus_space_write_1(iot, ioh, TAV_STR_SEL,
423 TAV_STR_SEL_AUDIO_BYPASS);
424 break;
425
426 /* XXX: later: add ULINEAR, and LE using software encoding */
427
428 case AUDIO_ENCODING_MPEG_L1_STREAM:
429 /* FALLTHROUGH */
430 case AUDIO_ENCODING_MPEG_L2_STREAM:
431 bus_space_write_1(iot, ioh, TAV_STR_SEL,
432 TAV_STR_SEL_MPEG_AUDIO_STREAM);
433 p->sample_rate = 44100;
434 p->precision = 1;
435 break;
436
437 case AUDIO_ENCODING_MPEG_L1_PACKETS:
438 /* FALLTHROUGH */
439 case AUDIO_ENCODING_MPEG_L2_PACKETS:
440 bus_space_write_1(iot, ioh, TAV_STR_SEL,
441 TAV_STR_SEL_MPEG_AUDIO_PACKETS);
442 p->sample_rate = 44100;
443 p->precision = 1;
444 break;
445
446 case AUDIO_ENCODING_MPEG_L1_SYSTEM:
447 /* FALLTHROUGH */
448 case AUDIO_ENCODING_MPEG_L2_SYSTEM:
449 bus_space_write_1(iot, ioh, TAV_STR_SEL,
450 TAV_STR_SEL_MPEG_SYSTEM_STREAM);
451 p->sample_rate = 44100;
452 p->precision = 1;
453 break;
454 }
455 tav_write_byte(iot, ioh, TAV_RESTART, 1);
456 do {
457 delay(10);
458 } while (tav_read_byte(iot, ioh, TAV_RESTART));
459
460 return 0;
461 }
462
463 int
464 tav_set_port(void *hdl, mixer_ctrl_t *mc)
465 {
466 struct tav_softc *sc;
467
468 sc = hdl;
469 /* dummy */
470 return 0;
471 }
472
473 int
474 tav_get_port(void *hdl, mixer_ctrl_t *mc)
475 {
476 struct tav_softc *sc;
477
478 sc = hdl;
479 /* dummy */
480 return 0;
481 }
482
483 int
484 tav_query_devinfo(void *hdl, mixer_devinfo_t *di)
485 {
486 return ENXIO;
487 }
488
489 int
490 tav_speaker_ctl(void *hdl, int value)
491 {
492 struct tav_softc *sc;
493 bus_space_tag_t iot;
494 bus_space_handle_t ioh;
495
496 sc = hdl;
497 iot = sc->sc_iot;
498 ioh = sc->sc_ioh;
499
500 tav_write_byte(iot, ioh, TAV_MUTE, !value);
501
502 return 0;
503 }
504