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