]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/tm6000/tm6000-alsa.c
Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuil...
[net-next-2.6.git] / drivers / staging / tm6000 / tm6000-alsa.c
CommitLineData
d72d5067
MCC
1/*
2 *
e28f49b0 3 * Support for audio capture for tm5600/6000/6010
7ca30c52 4 * (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
d72d5067 5 *
7ca30c52 6 * Based on cx88-alsa.c
d72d5067
MCC
7 *
8 * This program is free software; you can redistribute it and/or modify
7ca30c52
MCC
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
d72d5067
MCC
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
6ad6e88a 17#include <linux/usb.h>
4ef09889 18#include <linux/slab.h>
d72d5067
MCC
19
20#include <asm/delay.h>
d72d5067
MCC
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/control.h>
25#include <sound/initval.h>
d72d5067 26
d72d5067 27
7ca30c52
MCC
28#include "tm6000.h"
29#include "tm6000-regs.h"
30
31#undef dprintk
d72d5067 32
7ca30c52
MCC
33#define dprintk(level, fmt, arg...) do { \
34 if (debug >= level) \
35 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
36 } while (0)
d72d5067
MCC
37
38/****************************************************************************
39 Data type declarations - Can be moded to a header file later
40 ****************************************************************************/
41
7ca30c52 42struct snd_tm6000_card {
d72d5067
MCC
43 struct snd_card *card;
44
45 spinlock_t reg_lock;
7ca30c52 46
d72d5067
MCC
47 atomic_t count;
48
d72d5067
MCC
49 unsigned int period_size;
50 unsigned int num_periods;
51
7ca30c52
MCC
52 struct tm6000_core *core;
53 struct tm6000_buffer *buf;
d72d5067 54
7ca30c52 55 int bufsize;
d72d5067 56
7ca30c52 57 struct snd_pcm_substream *substream;
d72d5067 58};
d72d5067
MCC
59
60
61/****************************************************************************
62 Module global static vars
63 ****************************************************************************/
64
65static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
6ad6e88a 66static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
d72d5067
MCC
67static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
68
69module_param_array(enable, bool, NULL, 0444);
7ca30c52 70MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
d72d5067
MCC
71
72module_param_array(index, int, NULL, 0444);
7ca30c52 73MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
d72d5067
MCC
74
75
76/****************************************************************************
77 Module macros
78 ****************************************************************************/
79
e28f49b0 80MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
d72d5067
MCC
81MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
82MODULE_LICENSE("GPL");
7ca30c52 83MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
e28f49b0
DB
84 "{{Trident,tm6000},"
85 "{{Trident,tm6010}");
d72d5067 86static unsigned int debug;
7ca30c52
MCC
87module_param(debug, int, 0644);
88MODULE_PARM_DESC(debug, "enable debug messages");
d72d5067
MCC
89
90/****************************************************************************
91 Module specific funtions
92 ****************************************************************************/
93
94/*
95 * BOARD Specific: Sets audio DMA
96 */
97
7ca30c52 98static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
d72d5067 99{
7ca30c52
MCC
100 struct tm6000_core *core = chip->core;
101 int val;
d72d5067 102
7ca30c52 103 /* Enables audio */
9afec493 104 val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
7ca30c52 105 val |= 0x20;
9afec493 106 tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
d72d5067 107
9afec493 108 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0x80);
d72d5067
MCC
109
110 return 0;
111}
112
113/*
114 * BOARD Specific: Resets audio DMA
115 */
7ca30c52 116static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
d72d5067 117{
7ca30c52
MCC
118 struct tm6000_core *core = chip->core;
119 int val;
d72d5067
MCC
120 dprintk(1, "Stopping audio DMA\n");
121
7ca30c52 122 /* Enables audio */
9afec493 123 val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
7ca30c52 124 val &= ~0x20;
9afec493 125 tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
d72d5067 126
9afec493 127 tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0);
d72d5067
MCC
128
129 return 0;
130}
131
7ca30c52 132static int dsp_buffer_free(struct snd_tm6000_card *chip)
d72d5067 133{
7ca30c52 134 BUG_ON(!chip->bufsize);
d72d5067 135
7ca30c52 136 dprintk(2, "Freeing buffer\n");
d72d5067 137
7ca30c52 138 /* FIXME: Frees buffer */
d72d5067 139
7ca30c52 140 chip->bufsize = 0;
d72d5067 141
7ca30c52 142 return 0;
d72d5067
MCC
143}
144
145/****************************************************************************
146 ALSA PCM Interface
147 ****************************************************************************/
148
149/*
150 * Digital hardware definition
151 */
152#define DEFAULT_FIFO_SIZE 4096
7ca30c52
MCC
153
154static struct snd_pcm_hardware snd_tm6000_digital_hw = {
d72d5067
MCC
155 .info = SNDRV_PCM_INFO_MMAP |
156 SNDRV_PCM_INFO_INTERLEAVED |
157 SNDRV_PCM_INFO_BLOCK_TRANSFER |
158 SNDRV_PCM_INFO_MMAP_VALID,
159 .formats = SNDRV_PCM_FMTBIT_S16_LE,
160
7ca30c52
MCC
161 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
162 .rate_min = 44100,
d72d5067
MCC
163 .rate_max = 48000,
164 .channels_min = 2,
165 .channels_max = 2,
d72d5067
MCC
166 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
167 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
168 .periods_min = 1,
169 .periods_max = 1024,
170 .buffer_bytes_max = (1024*1024),
171};
172
173/*
174 * audio pcm capture open callback
175 */
7ca30c52 176static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
d72d5067 177{
7ca30c52 178 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 int err;
181
7ca30c52
MCC
182 err = snd_pcm_hw_constraint_pow2(runtime, 0,
183 SNDRV_PCM_HW_PARAM_PERIODS);
d72d5067
MCC
184 if (err < 0)
185 goto _error;
186
187 chip->substream = substream;
188
7ca30c52 189 runtime->hw = snd_tm6000_digital_hw;
d72d5067
MCC
190
191 return 0;
192_error:
7ca30c52 193 dprintk(1, "Error opening PCM!\n");
d72d5067
MCC
194 return err;
195}
196
197/*
198 * audio close callback
199 */
7ca30c52 200static int snd_tm6000_close(struct snd_pcm_substream *substream)
d72d5067
MCC
201{
202 return 0;
203}
204
205/*
206 * hw_params callback
207 */
7ca30c52
MCC
208static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
209 struct snd_pcm_hw_params *hw_params)
d72d5067 210{
7ca30c52 211 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
212
213 if (substream->runtime->dma_area) {
214 dsp_buffer_free(chip);
215 substream->runtime->dma_area = NULL;
216 }
217
218 chip->period_size = params_period_bytes(hw_params);
219 chip->num_periods = params_periods(hw_params);
7ca30c52
MCC
220 chip->bufsize = chip->period_size * params_periods(hw_params);
221
222 BUG_ON(!chip->bufsize);
223
224 dprintk(1, "Setting buffer\n");
d72d5067 225
7ca30c52
MCC
226 /* FIXME: Allocate buffer for audio */
227
228
229 return 0;
d72d5067
MCC
230}
231
232/*
233 * hw free callback
234 */
7ca30c52 235static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
d72d5067
MCC
236{
237
7ca30c52 238 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
239
240 if (substream->runtime->dma_area) {
241 dsp_buffer_free(chip);
242 substream->runtime->dma_area = NULL;
243 }
244
245 return 0;
246}
247
248/*
249 * prepare callback
250 */
7ca30c52 251static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
d72d5067
MCC
252{
253 return 0;
254}
255
7ca30c52 256
d72d5067
MCC
257/*
258 * trigger callback
259 */
7ca30c52 260static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
d72d5067 261{
7ca30c52 262 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
263 int err;
264
d72d5067
MCC
265 spin_lock(&chip->reg_lock);
266
267 switch (cmd) {
268 case SNDRV_PCM_TRIGGER_START:
7ca30c52 269 err = _tm6000_start_audio_dma(chip);
d72d5067
MCC
270 break;
271 case SNDRV_PCM_TRIGGER_STOP:
7ca30c52 272 err = _tm6000_stop_audio_dma(chip);
d72d5067
MCC
273 break;
274 default:
7ca30c52 275 err = -EINVAL;
d72d5067
MCC
276 break;
277 }
278
279 spin_unlock(&chip->reg_lock);
280
281 return err;
282}
283
284/*
285 * pointer callback
286 */
7ca30c52 287static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
d72d5067 288{
7ca30c52 289 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
290 struct snd_pcm_runtime *runtime = substream->runtime;
291 u16 count;
292
293 count = atomic_read(&chip->count);
294
d72d5067
MCC
295 return runtime->period_size * (count & (runtime->periods-1));
296}
297
d72d5067
MCC
298/*
299 * operators
300 */
7ca30c52
MCC
301static struct snd_pcm_ops snd_tm6000_pcm_ops = {
302 .open = snd_tm6000_pcm_open,
303 .close = snd_tm6000_close,
d72d5067 304 .ioctl = snd_pcm_lib_ioctl,
7ca30c52
MCC
305 .hw_params = snd_tm6000_hw_params,
306 .hw_free = snd_tm6000_hw_free,
307 .prepare = snd_tm6000_prepare,
308 .trigger = snd_tm6000_card_trigger,
309 .pointer = snd_tm6000_pointer,
d72d5067
MCC
310};
311
312/*
313 * create a PCM device
314 */
7ca30c52
MCC
315static int __devinit snd_tm6000_pcm(struct snd_tm6000_card *chip,
316 int device, char *name)
d72d5067
MCC
317{
318 int err;
319 struct snd_pcm *pcm;
320
321 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
322 if (err < 0)
323 return err;
324 pcm->private_data = chip;
325 strcpy(pcm->name, name);
7ca30c52 326 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
d72d5067
MCC
327
328 return 0;
329}
330
7ca30c52 331/* FIXME: Control interface - How to control volume/mute? */
d72d5067
MCC
332
333/****************************************************************************
334 Basic Flow for Sound Devices
335 ****************************************************************************/
336
d72d5067
MCC
337/*
338 * Alsa Constructor - Component probe
339 */
340
576d5739 341int tm6000_audio_init(struct tm6000_core *dev, int idx)
d72d5067 342{
6ad6e88a
MCC
343 struct snd_card *card;
344 struct snd_tm6000_card *chip;
345 int rc, len;
346 char component[14];
347
348 if (idx >= SNDRV_CARDS)
349 return -ENODEV;
350
351 if (!enable[idx])
352 return -ENOENT;
353
427f7fac
MCC
354 rc = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
355 if (rc < 0) {
6ad6e88a 356 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
427f7fac 357 return rc;
6ad6e88a
MCC
358 }
359
360 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
361 if (!chip) {
362 rc = -ENOMEM;
363 goto error;
364 }
365
366 chip->core = dev;
367 chip->card = card;
368
369 strcpy(card->driver, "tm6000-alsa");
370 sprintf(component, "USB%04x:%04x",
371 le16_to_cpu(dev->udev->descriptor.idVendor),
372 le16_to_cpu(dev->udev->descriptor.idProduct));
373 snd_component_add(card, component);
374
375 if (dev->udev->descriptor.iManufacturer)
376 len = usb_string(dev->udev,
377 dev->udev->descriptor.iManufacturer,
378 card->longname, sizeof(card->longname));
379 else
380 len = 0;
381
382 if (len > 0)
383 strlcat(card->longname, " ", sizeof(card->longname));
384
385 strlcat(card->longname, card->shortname, sizeof(card->longname));
386
387 len = strlcat(card->longname, " at ", sizeof(card->longname));
388
389 if (len < sizeof(card->longname))
390 usb_make_path(dev->udev, card->longname + len,
391 sizeof(card->longname) - len);
392
393 strlcat(card->longname,
394 dev->udev->speed == USB_SPEED_LOW ? ", low speed" :
395 dev->udev->speed == USB_SPEED_FULL ? ", full speed" :
396 ", high speed",
397 sizeof(card->longname));
398
399 rc = snd_tm6000_pcm(chip, 0, "tm6000 Digital");
400 if (rc < 0)
401 goto error;
402
403 rc = snd_card_register(card);
404 if (rc < 0)
405 goto error;
406
d72d5067 407
d72d5067 408 return 0;
d72d5067 409
6ad6e88a
MCC
410error:
411 snd_card_free(card);
412 return rc;
d72d5067
MCC
413}
414
0439db75
SR
415static int tm6000_audio_fini(struct tm6000_core *dev)
416{
417 return 0;
418}
419
420struct tm6000_ops audio_ops = {
421 .id = TM6000_AUDIO,
422 .name = "TM6000 Audio Extension",
423 .init = tm6000_audio_init,
424 .fini = tm6000_audio_fini,
425};
426
427static int __init tm6000_alsa_register(void)
428{
429 return tm6000_register_extension(&audio_ops);
430}
431
432static void __exit tm6000_alsa_unregister(void)
433{
434 tm6000_unregister_extension(&audio_ops);
435}
436
437module_init(tm6000_alsa_register);
438module_exit(tm6000_alsa_unregister);