2 * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
4 * Based on the audio support from the tw6869 driver:
5 * Copyright 2015 www.starterkit.ru <info@starterkit.ru>
8 * Driver for Intersil|Techwell TW6869 based DVR cards
9 * (c) 2011-12 liran <jli11@intersil.com> [Intersil|Techwell China]
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of version 2 of the GNU General Public License
13 * as published by the Free Software Foundation.
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/kmod.h>
21 #include <linux/mutex.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/control.h>
30 #include "tw686x-regs.h"
32 #define AUDIO_CHANNEL_OFFSET 8
34 void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
35 unsigned int pb_status)
40 for_each_set_bit(ch, &requests, max_channels(dev)) {
41 struct tw686x_audio_channel *ac = &dev->audio_channels[ch];
42 struct tw686x_audio_buf *done = NULL;
43 struct tw686x_audio_buf *next = NULL;
44 struct tw686x_dma_desc *desc;
46 pb = !!(pb_status & BIT(AUDIO_CHANNEL_OFFSET + ch));
48 spin_lock_irqsave(&ac->lock, flags);
51 if (!ac->ss || !ac->curr_bufs[0] || !ac->curr_bufs[1]) {
52 spin_unlock_irqrestore(&ac->lock, flags);
56 if (!list_empty(&ac->buf_list)) {
57 next = list_first_entry(&ac->buf_list,
58 struct tw686x_audio_buf, list);
59 list_move_tail(&next->list, &ac->buf_list);
60 done = ac->curr_bufs[!pb];
61 ac->curr_bufs[pb] = next;
63 spin_unlock_irqrestore(&ac->lock, flags);
68 * Checking for a non-nil dma_desc[pb]->virt buffer is
69 * the same as checking for memcpy DMA mode.
71 desc = &ac->dma_descs[pb];
73 memcpy(done->virt, desc->virt,
76 u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
77 reg_write(dev, reg, next->dma);
79 ac->ptr = done->dma - ac->buf[0].dma;
80 snd_pcm_period_elapsed(ac->ss);
84 static int tw686x_pcm_hw_params(struct snd_pcm_substream *ss,
85 struct snd_pcm_hw_params *hw_params)
87 return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
90 static int tw686x_pcm_hw_free(struct snd_pcm_substream *ss)
92 return snd_pcm_lib_free_pages(ss);
96 * Audio parameters are global and shared among all
97 * capture channels. The driver prevents changes to
98 * the parameters if any audio channel is capturing.
100 static const struct snd_pcm_hardware tw686x_capture_hw = {
101 .info = (SNDRV_PCM_INFO_MMAP |
102 SNDRV_PCM_INFO_INTERLEAVED |
103 SNDRV_PCM_INFO_BLOCK_TRANSFER |
104 SNDRV_PCM_INFO_MMAP_VALID),
105 .formats = SNDRV_PCM_FMTBIT_S16_LE,
106 .rates = SNDRV_PCM_RATE_8000_48000,
111 .buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
112 .period_bytes_min = AUDIO_DMA_SIZE_MIN,
113 .period_bytes_max = AUDIO_DMA_SIZE_MAX,
114 .periods_min = TW686X_AUDIO_PERIODS_MIN,
115 .periods_max = TW686X_AUDIO_PERIODS_MAX,
118 static int tw686x_pcm_open(struct snd_pcm_substream *ss)
120 struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
121 struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
122 struct snd_pcm_runtime *rt = ss->runtime;
126 rt->hw = tw686x_capture_hw;
128 err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
135 static int tw686x_pcm_close(struct snd_pcm_substream *ss)
137 struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
138 struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
144 static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
146 struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
147 struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
148 struct snd_pcm_runtime *rt = ss->runtime;
149 unsigned int period_size = snd_pcm_lib_period_bytes(ss);
150 struct tw686x_audio_buf *p_buf, *b_buf;
154 spin_lock_irqsave(&dev->lock, flags);
156 * Given the audio parameters are global (i.e. shared across
157 * DMA channels), we need to check new params are allowed.
159 if (((dev->audio_rate != rt->rate) ||
160 (dev->period_size != period_size)) && dev->audio_enabled)
163 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
164 spin_unlock_irqrestore(&dev->lock, flags);
166 if (dev->audio_rate != rt->rate) {
169 dev->audio_rate = rt->rate;
170 reg = ((125000000 / rt->rate) << 16) +
171 ((125000000 % rt->rate) << 16) / rt->rate;
173 reg_write(dev, AUDIO_CONTROL2, reg);
176 if (dev->period_size != period_size) {
179 dev->period_size = period_size;
180 reg = reg_read(dev, AUDIO_CONTROL1);
181 reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
182 reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
184 reg_write(dev, AUDIO_CONTROL1, reg);
187 if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
188 rt->periods > TW686X_AUDIO_PERIODS_MAX)
191 spin_lock_irqsave(&ac->lock, flags);
192 INIT_LIST_HEAD(&ac->buf_list);
194 for (i = 0; i < rt->periods; i++) {
195 ac->buf[i].dma = rt->dma_addr + period_size * i;
196 ac->buf[i].virt = rt->dma_area + period_size * i;
197 INIT_LIST_HEAD(&ac->buf[i].list);
198 list_add_tail(&ac->buf[i].list, &ac->buf_list);
201 p_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
202 list_move_tail(&p_buf->list, &ac->buf_list);
204 b_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
205 list_move_tail(&b_buf->list, &ac->buf_list);
207 ac->curr_bufs[0] = p_buf;
208 ac->curr_bufs[1] = b_buf;
211 if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
212 reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
213 reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
216 spin_unlock_irqrestore(&ac->lock, flags);
221 spin_unlock_irqrestore(&dev->lock, flags);
225 static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
227 struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
228 struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
233 case SNDRV_PCM_TRIGGER_START:
234 if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
235 spin_lock_irqsave(&dev->lock, flags);
236 dev->audio_enabled = 1;
237 tw686x_enable_channel(dev,
238 AUDIO_CHANNEL_OFFSET + ac->ch);
239 spin_unlock_irqrestore(&dev->lock, flags);
241 mod_timer(&dev->dma_delay_timer,
242 jiffies + msecs_to_jiffies(100));
247 case SNDRV_PCM_TRIGGER_STOP:
248 spin_lock_irqsave(&dev->lock, flags);
249 dev->audio_enabled = 0;
250 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
251 spin_unlock_irqrestore(&dev->lock, flags);
253 spin_lock_irqsave(&ac->lock, flags);
254 ac->curr_bufs[0] = NULL;
255 ac->curr_bufs[1] = NULL;
256 spin_unlock_irqrestore(&ac->lock, flags);
264 static snd_pcm_uframes_t tw686x_pcm_pointer(struct snd_pcm_substream *ss)
266 struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
267 struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
269 return bytes_to_frames(ss->runtime, ac->ptr);
272 static const struct snd_pcm_ops tw686x_pcm_ops = {
273 .open = tw686x_pcm_open,
274 .close = tw686x_pcm_close,
275 .ioctl = snd_pcm_lib_ioctl,
276 .hw_params = tw686x_pcm_hw_params,
277 .hw_free = tw686x_pcm_hw_free,
278 .prepare = tw686x_pcm_prepare,
279 .trigger = tw686x_pcm_trigger,
280 .pointer = tw686x_pcm_pointer,
283 static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
285 struct snd_card *card = dev->snd_card;
287 struct snd_pcm_substream *ss;
291 err = snd_pcm_new(card, card->driver, 0, 0, max_channels(dev), &pcm);
295 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &tw686x_pcm_ops);
296 snd_pcm_chip(pcm) = dev;
298 strlcpy(pcm->name, "tw686x PCM", sizeof(pcm->name));
300 for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
301 ss; ss = ss->next, i++)
302 snprintf(ss->name, sizeof(ss->name), "vch%u audio", i);
304 return snd_pcm_lib_preallocate_pages_for_all(pcm,
306 snd_dma_pci_data(dev->pci_dev),
307 TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
308 TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
311 static void tw686x_audio_dma_free(struct tw686x_dev *dev,
312 struct tw686x_audio_channel *ac)
316 for (pb = 0; pb < 2; pb++) {
317 if (!ac->dma_descs[pb].virt)
319 pci_free_consistent(dev->pci_dev, ac->dma_descs[pb].size,
320 ac->dma_descs[pb].virt,
321 ac->dma_descs[pb].phys);
322 ac->dma_descs[pb].virt = NULL;
326 static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
327 struct tw686x_audio_channel *ac)
332 * In the memcpy DMA mode we allocate a consistent buffer
333 * and use it for the DMA capture. Otherwise, DMA
334 * acts on the ALSA buffers as received in pcm_prepare.
336 if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
339 for (pb = 0; pb < 2; pb++) {
340 u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
343 virt = pci_alloc_consistent(dev->pci_dev, AUDIO_DMA_SIZE_MAX,
344 &ac->dma_descs[pb].phys);
346 dev_err(&dev->pci_dev->dev,
347 "dma%d: unable to allocate audio DMA %s-buffer\n",
348 ac->ch, pb ? "B" : "P");
351 ac->dma_descs[pb].virt = virt;
352 ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
353 reg_write(dev, reg, ac->dma_descs[pb].phys);
358 void tw686x_audio_free(struct tw686x_dev *dev)
364 spin_lock_irqsave(&dev->lock, flags);
365 dma_cmd = reg_read(dev, DMA_CMD);
366 dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
367 reg_write(dev, DMA_CMD, dma_cmd & ~0xff00);
368 reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask & ~0xff00);
369 spin_unlock_irqrestore(&dev->lock, flags);
373 snd_card_free(dev->snd_card);
374 dev->snd_card = NULL;
377 int tw686x_audio_init(struct tw686x_dev *dev)
379 struct pci_dev *pci_dev = dev->pci_dev;
380 struct snd_card *card;
383 /* Enable external audio */
384 reg_write(dev, AUDIO_CONTROL1, BIT(0));
386 err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
388 THIS_MODULE, 0, &card);
392 dev->snd_card = card;
393 strlcpy(card->driver, "tw686x", sizeof(card->driver));
394 strlcpy(card->shortname, "tw686x", sizeof(card->shortname));
395 strlcpy(card->longname, pci_name(pci_dev), sizeof(card->longname));
396 snd_card_set_dev(card, &pci_dev->dev);
398 for (ch = 0; ch < max_channels(dev); ch++) {
399 struct tw686x_audio_channel *ac;
401 ac = &dev->audio_channels[ch];
402 spin_lock_init(&ac->lock);
406 err = tw686x_audio_dma_alloc(dev, ac);
411 err = tw686x_snd_pcm_init(dev);
415 err = snd_card_register(card);
420 for (ch = 0; ch < max_channels(dev); ch++) {
421 if (!dev->audio_channels[ch].dev)
423 tw686x_audio_dma_free(dev, &dev->audio_channels[ch]);
426 dev->snd_card = NULL;