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