Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[pandora-kernel.git] / drivers / staging / tm6000 / tm6000-alsa.c
1 /*
2  *
3  *  Support for audio capture for tm5600/6000/6010
4  *    (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
5  *
6  *  Based on cx88-alsa.c
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/usb.h>
18
19 #include <asm/delay.h>
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>
25
26
27 #include "tm6000.h"
28 #include "tm6000-regs.h"
29
30 #undef dprintk
31
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)
36
37 /****************************************************************************
38         Data type declarations - Can be moded to a header file later
39  ****************************************************************************/
40
41 struct snd_tm6000_card {
42         struct snd_card            *card;
43
44         spinlock_t                 reg_lock;
45
46         atomic_t                   count;
47
48         unsigned int               period_size;
49         unsigned int               num_periods;
50
51         struct tm6000_core         *core;
52         struct tm6000_buffer       *buf;
53
54         int                        bufsize;
55
56         struct snd_pcm_substream *substream;
57 };
58
59
60 /****************************************************************************
61                         Module global static vars
62  ****************************************************************************/
63
64 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
65 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
66 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
67
68 module_param_array(enable, bool, NULL, 0444);
69 MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
70
71 module_param_array(index, int, NULL, 0444);
72 MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
73
74
75 /****************************************************************************
76                                 Module macros
77  ****************************************************************************/
78
79 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
80 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
81 MODULE_LICENSE("GPL");
82 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
83                         "{{Trident,tm6000},"
84                         "{{Trident,tm6010}");
85 static unsigned int debug;
86 module_param(debug, int, 0644);
87 MODULE_PARM_DESC(debug, "enable debug messages");
88
89 /****************************************************************************
90                         Module specific funtions
91  ****************************************************************************/
92
93 /*
94  * BOARD Specific: Sets audio DMA
95  */
96
97 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
98 {
99         struct tm6000_core *core = chip->core;
100         int val;
101
102         /* Enables audio */
103         val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
104         val |= 0x20;
105         tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
106
107         tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0x80);
108
109         return 0;
110 }
111
112 /*
113  * BOARD Specific: Resets audio DMA
114  */
115 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
116 {
117         struct tm6000_core *core = chip->core;
118         int val;
119         dprintk(1, "Stopping audio DMA\n");
120
121         /* Enables audio */
122         val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
123         val &= ~0x20;
124         tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
125
126         tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0);
127
128         return 0;
129 }
130
131 static int dsp_buffer_free(struct snd_tm6000_card *chip)
132 {
133         BUG_ON(!chip->bufsize);
134
135         dprintk(2, "Freeing buffer\n");
136
137         /* FIXME: Frees buffer */
138
139         chip->bufsize = 0;
140
141        return 0;
142 }
143
144 /****************************************************************************
145                                 ALSA PCM Interface
146  ****************************************************************************/
147
148 /*
149  * Digital hardware definition
150  */
151 #define DEFAULT_FIFO_SIZE       4096
152
153 static struct snd_pcm_hardware snd_tm6000_digital_hw = {
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
160         .rates =                SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
161         .rate_min =             44100,
162         .rate_max =             48000,
163         .channels_min = 2,
164         .channels_max = 2,
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  */
175 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
176 {
177         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
178         struct snd_pcm_runtime *runtime = substream->runtime;
179         int err;
180
181         err = snd_pcm_hw_constraint_pow2(runtime, 0,
182                                          SNDRV_PCM_HW_PARAM_PERIODS);
183         if (err < 0)
184                 goto _error;
185
186         chip->substream = substream;
187
188         runtime->hw = snd_tm6000_digital_hw;
189
190         return 0;
191 _error:
192         dprintk(1, "Error opening PCM!\n");
193         return err;
194 }
195
196 /*
197  * audio close callback
198  */
199 static int snd_tm6000_close(struct snd_pcm_substream *substream)
200 {
201         return 0;
202 }
203
204 /*
205  * hw_params callback
206  */
207 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
208                               struct snd_pcm_hw_params *hw_params)
209 {
210         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
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);
219         chip->bufsize = chip->period_size * params_periods(hw_params);
220
221         BUG_ON(!chip->bufsize);
222
223         dprintk(1, "Setting buffer\n");
224
225         /* FIXME: Allocate buffer for audio */
226
227
228         return 0;
229 }
230
231 /*
232  * hw free callback
233  */
234 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
235 {
236
237         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
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  */
250 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
251 {
252         return 0;
253 }
254
255
256 /*
257  * trigger callback
258  */
259 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
260 {
261         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
262         int err;
263
264         spin_lock(&chip->reg_lock);
265
266         switch (cmd) {
267         case SNDRV_PCM_TRIGGER_START:
268                 err = _tm6000_start_audio_dma(chip);
269                 break;
270         case SNDRV_PCM_TRIGGER_STOP:
271                 err = _tm6000_stop_audio_dma(chip);
272                 break;
273         default:
274                 err = -EINVAL;
275                 break;
276         }
277
278         spin_unlock(&chip->reg_lock);
279
280         return err;
281 }
282
283 /*
284  * pointer callback
285  */
286 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
287 {
288         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
289         struct snd_pcm_runtime *runtime = substream->runtime;
290         u16 count;
291
292         count = atomic_read(&chip->count);
293
294         return runtime->period_size * (count & (runtime->periods-1));
295 }
296
297 /*
298  * operators
299  */
300 static struct snd_pcm_ops snd_tm6000_pcm_ops = {
301         .open = snd_tm6000_pcm_open,
302         .close = snd_tm6000_close,
303         .ioctl = snd_pcm_lib_ioctl,
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,
309 };
310
311 /*
312  * create a PCM device
313  */
314 static int __devinit snd_tm6000_pcm(struct snd_tm6000_card *chip,
315                                     int device, char *name)
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);
325         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
326
327         return 0;
328 }
329
330 /* FIXME: Control interface - How to control volume/mute? */
331
332 /****************************************************************************
333                         Basic Flow for Sound Devices
334  ****************************************************************************/
335
336 /*
337  * Alsa Constructor - Component probe
338  */
339
340 int tm6000_audio_init(struct tm6000_core *dev, int idx)
341 {
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
353         rc = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
354         if (rc < 0) {
355                 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
356                 return rc;
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
406
407         return 0;
408
409 error:
410         snd_card_free(card);
411         return rc;
412 }
413
414 static int tm6000_audio_fini(struct tm6000_core *dev)
415 {
416         return 0;
417 }
418
419 struct tm6000_ops audio_ops = {
420         .id     = TM6000_AUDIO,
421         .name   = "TM6000 Audio Extension",
422         .init   = tm6000_audio_init,
423         .fini   = tm6000_audio_fini,
424 };
425
426 static int __init tm6000_alsa_register(void)
427 {
428         return tm6000_register_extension(&audio_ops);
429 }
430
431 static void __exit tm6000_alsa_unregister(void)
432 {
433         tm6000_unregister_extension(&audio_ops);
434 }
435
436 module_init(tm6000_alsa_register);
437 module_exit(tm6000_alsa_unregister);