blob: 7cc2ac740d8a8d3439fdff2b47deefd0b3498123 [file] [log] [blame]
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -03001/*
2 *
Dmitri Belimove28f49b2010-02-22 06:32:15 -03003 * Support for audio capture for tm5600/6000/6010
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -03004 * (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -03005 *
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -03006 * Based on cx88-alsa.c
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -03007 *
8 * This program is free software; you can redistribute it and/or modify
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -03009 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030011 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -030017#include <linux/usb.h>
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030018
19#include <asm/delay.h>
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030020#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>
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030025
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030026
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030027#include "tm6000.h"
28#include "tm6000-regs.h"
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030029
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030030#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)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030036
37/****************************************************************************
38 Data type declarations - Can be moded to a header file later
39 ****************************************************************************/
40
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030041struct snd_tm6000_card {
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030042 struct snd_card *card;
43
44 spinlock_t reg_lock;
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030045
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030046 atomic_t count;
47
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030048 unsigned int period_size;
49 unsigned int num_periods;
50
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030051 struct tm6000_core *core;
52 struct tm6000_buffer *buf;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030053
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030054 int bufsize;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030055
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030056 struct snd_pcm_substream *substream;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030057};
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030058
59
60/****************************************************************************
61 Module global static vars
62 ****************************************************************************/
63
64static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -030065static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030066static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
67
68module_param_array(enable, bool, NULL, 0444);
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030069MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030070
71module_param_array(index, int, NULL, 0444);
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030072MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030073
74
75/****************************************************************************
76 Module macros
77 ****************************************************************************/
78
Dmitri Belimove28f49b2010-02-22 06:32:15 -030079MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030080MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
81MODULE_LICENSE("GPL");
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030082MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
Dmitri Belimove28f49b2010-02-22 06:32:15 -030083 "{{Trident,tm6000},"
84 "{{Trident,tm6010}");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030085static unsigned int debug;
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030086module_param(debug, int, 0644);
87MODULE_PARM_DESC(debug, "enable debug messages");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030088
89/****************************************************************************
90 Module specific funtions
91 ****************************************************************************/
92
93/*
94 * BOARD Specific: Sets audio DMA
95 */
96
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030097static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -030098{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -030099 struct tm6000_core *core = chip->core;
100 int val;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300101
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300102 /* 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);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300106
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300107 tm6000_set_reg(core, REQ_08_SET_GET_AVREG_BIT, 0x01, 0x80);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300108
109 return 0;
110}
111
112/*
113 * BOARD Specific: Resets audio DMA
114 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300115static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300116{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300117 struct tm6000_core *core = chip->core;
118 int val;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300119 dprintk(1, "Stopping audio DMA\n");
120
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300121 /* 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);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300125
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300126 tm6000_set_reg(core, REQ_08_SET_GET_AVREG_BIT, 0x01, 0);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300127
128 return 0;
129}
130
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300131static int dsp_buffer_free(struct snd_tm6000_card *chip)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300132{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300133 BUG_ON(!chip->bufsize);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300134
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300135 dprintk(2, "Freeing buffer\n");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300136
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300137 /* FIXME: Frees buffer */
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300138
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300139 chip->bufsize = 0;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300140
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300141 return 0;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300142}
143
144/****************************************************************************
145 ALSA PCM Interface
146 ****************************************************************************/
147
148/*
149 * Digital hardware definition
150 */
151#define DEFAULT_FIFO_SIZE 4096
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300152
153static struct snd_pcm_hardware snd_tm6000_digital_hw = {
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300154 .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
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300160 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
161 .rate_min = 44100,
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300162 .rate_max = 48000,
163 .channels_min = 2,
164 .channels_max = 2,
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300165 .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 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300175static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300176{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300177 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300178 struct snd_pcm_runtime *runtime = substream->runtime;
179 int err;
180
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300181 err = snd_pcm_hw_constraint_pow2(runtime, 0,
182 SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300183 if (err < 0)
184 goto _error;
185
186 chip->substream = substream;
187
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300188 runtime->hw = snd_tm6000_digital_hw;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300189
190 return 0;
191_error:
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300192 dprintk(1, "Error opening PCM!\n");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300193 return err;
194}
195
196/*
197 * audio close callback
198 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300199static int snd_tm6000_close(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300200{
201 return 0;
202}
203
204/*
205 * hw_params callback
206 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300207static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
208 struct snd_pcm_hw_params *hw_params)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300209{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300210 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300211
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);
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300219 chip->bufsize = chip->period_size * params_periods(hw_params);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300220
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300221 BUG_ON(!chip->bufsize);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300222
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300223 dprintk(1, "Setting buffer\n");
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300224
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300225 /* FIXME: Allocate buffer for audio */
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300226
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300227
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300228 return 0;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300229}
230
231/*
232 * hw free callback
233 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300234static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300235{
236
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300237 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300238
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 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300250static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300251{
252 return 0;
253}
254
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300255
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300256/*
257 * trigger callback
258 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300259static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300260{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300261 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300262 int err;
263
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300264 spin_lock(&chip->reg_lock);
265
266 switch (cmd) {
267 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300268 err = _tm6000_start_audio_dma(chip);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300269 break;
270 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300271 err = _tm6000_stop_audio_dma(chip);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300272 break;
273 default:
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300274 err = -EINVAL;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300275 break;
276 }
277
278 spin_unlock(&chip->reg_lock);
279
280 return err;
281}
282
283/*
284 * pointer callback
285 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300286static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300287{
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300288 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300289 struct snd_pcm_runtime *runtime = substream->runtime;
290 u16 count;
291
292 count = atomic_read(&chip->count);
293
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300294 return runtime->period_size * (count & (runtime->periods-1));
295}
296
297/*
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300298 * operators
299 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300300static struct snd_pcm_ops snd_tm6000_pcm_ops = {
301 .open = snd_tm6000_pcm_open,
302 .close = snd_tm6000_close,
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300303 .ioctl = snd_pcm_lib_ioctl,
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300304 .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,
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300309};
310
311/*
312 * create a PCM device
313 */
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300314static int __devinit snd_tm6000_pcm(struct snd_tm6000_card *chip,
315 int device, char *name)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300316{
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);
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300325 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300326
327 return 0;
328}
329
Mauro Carvalho Chehab7ca30c52008-01-11 13:19:09 -0300330/* FIXME: Control interface - How to control volume/mute? */
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300331
332/****************************************************************************
333 Basic Flow for Sound Devices
334 ****************************************************************************/
335
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300336/*
337 * Alsa Constructor - Component probe
338 */
339
Mauro Carvalho Chehab576d5732008-01-11 13:55:45 -0300340int tm6000_audio_init(struct tm6000_core *dev, int idx)
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300341{
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -0300342 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
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300353 rc = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
354 if (rc < 0) {
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -0300355 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -0300356 return rc;
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -0300357 }
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
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300406
407 return 0;
Mauro Carvalho Chehab6ad6e882008-01-11 13:51:45 -0300408
409error:
410 snd_card_free(card);
411 return rc;
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300412}
Mauro Carvalho Chehab576d5732008-01-11 13:55:45 -0300413EXPORT_SYMBOL_GPL(tm6000_audio_init);
Mauro Carvalho Chehabd72d5062008-01-11 10:28:50 -0300414