125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2704a84ccSEzequiel Garcia /*
3704a84ccSEzequiel Garcia * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
4704a84ccSEzequiel Garcia *
5704a84ccSEzequiel Garcia * Based on the audio support from the tw6869 driver:
6704a84ccSEzequiel Garcia * Copyright 2015 www.starterkit.ru <info@starterkit.ru>
7704a84ccSEzequiel Garcia *
8704a84ccSEzequiel Garcia * Based on:
9704a84ccSEzequiel Garcia * Driver for Intersil|Techwell TW6869 based DVR cards
10704a84ccSEzequiel Garcia * (c) 2011-12 liran <jli11@intersil.com> [Intersil|Techwell China]
11704a84ccSEzequiel Garcia */
12704a84ccSEzequiel Garcia
13704a84ccSEzequiel Garcia #include <linux/types.h>
14704a84ccSEzequiel Garcia #include <linux/kernel.h>
15704a84ccSEzequiel Garcia #include <linux/module.h>
16704a84ccSEzequiel Garcia #include <linux/init.h>
17704a84ccSEzequiel Garcia #include <linux/kmod.h>
18704a84ccSEzequiel Garcia #include <linux/mutex.h>
19704a84ccSEzequiel Garcia #include <linux/pci.h>
20704a84ccSEzequiel Garcia #include <linux/delay.h>
21704a84ccSEzequiel Garcia
22704a84ccSEzequiel Garcia #include <sound/core.h>
23704a84ccSEzequiel Garcia #include <sound/initval.h>
24704a84ccSEzequiel Garcia #include <sound/pcm.h>
25704a84ccSEzequiel Garcia #include <sound/control.h>
26704a84ccSEzequiel Garcia #include "tw686x.h"
27704a84ccSEzequiel Garcia #include "tw686x-regs.h"
28704a84ccSEzequiel Garcia
29704a84ccSEzequiel Garcia #define AUDIO_CHANNEL_OFFSET 8
30704a84ccSEzequiel Garcia
tw686x_audio_irq(struct tw686x_dev * dev,unsigned long requests,unsigned int pb_status)31704a84ccSEzequiel Garcia void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
32704a84ccSEzequiel Garcia unsigned int pb_status)
33704a84ccSEzequiel Garcia {
34704a84ccSEzequiel Garcia unsigned long flags;
35704a84ccSEzequiel Garcia unsigned int ch, pb;
36704a84ccSEzequiel Garcia
37704a84ccSEzequiel Garcia for_each_set_bit(ch, &requests, max_channels(dev)) {
38704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ch];
39704a84ccSEzequiel Garcia struct tw686x_audio_buf *done = NULL;
40704a84ccSEzequiel Garcia struct tw686x_audio_buf *next = NULL;
41704a84ccSEzequiel Garcia struct tw686x_dma_desc *desc;
42704a84ccSEzequiel Garcia
43704a84ccSEzequiel Garcia pb = !!(pb_status & BIT(AUDIO_CHANNEL_OFFSET + ch));
44704a84ccSEzequiel Garcia
45704a84ccSEzequiel Garcia spin_lock_irqsave(&ac->lock, flags);
46704a84ccSEzequiel Garcia
47704a84ccSEzequiel Garcia /* Sanity check */
48704a84ccSEzequiel Garcia if (!ac->ss || !ac->curr_bufs[0] || !ac->curr_bufs[1]) {
49704a84ccSEzequiel Garcia spin_unlock_irqrestore(&ac->lock, flags);
50704a84ccSEzequiel Garcia continue;
51704a84ccSEzequiel Garcia }
52704a84ccSEzequiel Garcia
53704a84ccSEzequiel Garcia if (!list_empty(&ac->buf_list)) {
54704a84ccSEzequiel Garcia next = list_first_entry(&ac->buf_list,
55704a84ccSEzequiel Garcia struct tw686x_audio_buf, list);
56704a84ccSEzequiel Garcia list_move_tail(&next->list, &ac->buf_list);
57704a84ccSEzequiel Garcia done = ac->curr_bufs[!pb];
58704a84ccSEzequiel Garcia ac->curr_bufs[pb] = next;
59704a84ccSEzequiel Garcia }
60704a84ccSEzequiel Garcia spin_unlock_irqrestore(&ac->lock, flags);
61704a84ccSEzequiel Garcia
62*21047932SHans Verkuil if (!done)
63c156c54dSEzequiel Garcia continue;
64c156c54dSEzequiel Garcia /*
65c156c54dSEzequiel Garcia * Checking for a non-nil dma_desc[pb]->virt buffer is
66c156c54dSEzequiel Garcia * the same as checking for memcpy DMA mode.
67c156c54dSEzequiel Garcia */
68704a84ccSEzequiel Garcia desc = &ac->dma_descs[pb];
69c156c54dSEzequiel Garcia if (desc->virt) {
70c156c54dSEzequiel Garcia memcpy(done->virt, desc->virt,
71447d7c32SEzequiel Garcia dev->period_size);
72c156c54dSEzequiel Garcia } else {
73c156c54dSEzequiel Garcia u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
74c156c54dSEzequiel Garcia reg_write(dev, reg, next->dma);
75c156c54dSEzequiel Garcia }
76704a84ccSEzequiel Garcia ac->ptr = done->dma - ac->buf[0].dma;
77704a84ccSEzequiel Garcia snd_pcm_period_elapsed(ac->ss);
78704a84ccSEzequiel Garcia }
79704a84ccSEzequiel Garcia }
80704a84ccSEzequiel Garcia
81704a84ccSEzequiel Garcia /*
82447d7c32SEzequiel Garcia * Audio parameters are global and shared among all
836deab6feSEzequiel Garcia * capture channels. The driver prevents changes to
846deab6feSEzequiel Garcia * the parameters if any audio channel is capturing.
85704a84ccSEzequiel Garcia */
86704a84ccSEzequiel Garcia static const struct snd_pcm_hardware tw686x_capture_hw = {
87704a84ccSEzequiel Garcia .info = (SNDRV_PCM_INFO_MMAP |
88704a84ccSEzequiel Garcia SNDRV_PCM_INFO_INTERLEAVED |
89704a84ccSEzequiel Garcia SNDRV_PCM_INFO_BLOCK_TRANSFER |
90704a84ccSEzequiel Garcia SNDRV_PCM_INFO_MMAP_VALID),
91704a84ccSEzequiel Garcia .formats = SNDRV_PCM_FMTBIT_S16_LE,
92704a84ccSEzequiel Garcia .rates = SNDRV_PCM_RATE_8000_48000,
93704a84ccSEzequiel Garcia .rate_min = 8000,
94704a84ccSEzequiel Garcia .rate_max = 48000,
95704a84ccSEzequiel Garcia .channels_min = 1,
96704a84ccSEzequiel Garcia .channels_max = 1,
97447d7c32SEzequiel Garcia .buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
98447d7c32SEzequiel Garcia .period_bytes_min = AUDIO_DMA_SIZE_MIN,
99447d7c32SEzequiel Garcia .period_bytes_max = AUDIO_DMA_SIZE_MAX,
100704a84ccSEzequiel Garcia .periods_min = TW686X_AUDIO_PERIODS_MIN,
101704a84ccSEzequiel Garcia .periods_max = TW686X_AUDIO_PERIODS_MAX,
102704a84ccSEzequiel Garcia };
103704a84ccSEzequiel Garcia
tw686x_pcm_open(struct snd_pcm_substream * ss)104704a84ccSEzequiel Garcia static int tw686x_pcm_open(struct snd_pcm_substream *ss)
105704a84ccSEzequiel Garcia {
106704a84ccSEzequiel Garcia struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
107704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
108704a84ccSEzequiel Garcia struct snd_pcm_runtime *rt = ss->runtime;
109704a84ccSEzequiel Garcia int err;
110704a84ccSEzequiel Garcia
111704a84ccSEzequiel Garcia ac->ss = ss;
112704a84ccSEzequiel Garcia rt->hw = tw686x_capture_hw;
113704a84ccSEzequiel Garcia
114704a84ccSEzequiel Garcia err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
115704a84ccSEzequiel Garcia if (err < 0)
116704a84ccSEzequiel Garcia return err;
117704a84ccSEzequiel Garcia
118704a84ccSEzequiel Garcia return 0;
119704a84ccSEzequiel Garcia }
120704a84ccSEzequiel Garcia
tw686x_pcm_close(struct snd_pcm_substream * ss)121704a84ccSEzequiel Garcia static int tw686x_pcm_close(struct snd_pcm_substream *ss)
122704a84ccSEzequiel Garcia {
123704a84ccSEzequiel Garcia struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
124704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
125704a84ccSEzequiel Garcia
126704a84ccSEzequiel Garcia ac->ss = NULL;
127704a84ccSEzequiel Garcia return 0;
128704a84ccSEzequiel Garcia }
129704a84ccSEzequiel Garcia
tw686x_pcm_prepare(struct snd_pcm_substream * ss)130704a84ccSEzequiel Garcia static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
131704a84ccSEzequiel Garcia {
132704a84ccSEzequiel Garcia struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
133704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
134704a84ccSEzequiel Garcia struct snd_pcm_runtime *rt = ss->runtime;
135704a84ccSEzequiel Garcia unsigned int period_size = snd_pcm_lib_period_bytes(ss);
136704a84ccSEzequiel Garcia struct tw686x_audio_buf *p_buf, *b_buf;
137704a84ccSEzequiel Garcia unsigned long flags;
138704a84ccSEzequiel Garcia int i;
139704a84ccSEzequiel Garcia
140704a84ccSEzequiel Garcia spin_lock_irqsave(&dev->lock, flags);
1416deab6feSEzequiel Garcia /*
1426deab6feSEzequiel Garcia * Given the audio parameters are global (i.e. shared across
1436deab6feSEzequiel Garcia * DMA channels), we need to check new params are allowed.
1446deab6feSEzequiel Garcia */
1456deab6feSEzequiel Garcia if (((dev->audio_rate != rt->rate) ||
1466deab6feSEzequiel Garcia (dev->period_size != period_size)) && dev->audio_enabled)
1476deab6feSEzequiel Garcia goto err_audio_busy;
1486deab6feSEzequiel Garcia
149704a84ccSEzequiel Garcia tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
150704a84ccSEzequiel Garcia spin_unlock_irqrestore(&dev->lock, flags);
151704a84ccSEzequiel Garcia
152704a84ccSEzequiel Garcia if (dev->audio_rate != rt->rate) {
153704a84ccSEzequiel Garcia u32 reg;
154704a84ccSEzequiel Garcia
155704a84ccSEzequiel Garcia dev->audio_rate = rt->rate;
156704a84ccSEzequiel Garcia reg = ((125000000 / rt->rate) << 16) +
157704a84ccSEzequiel Garcia ((125000000 % rt->rate) << 16) / rt->rate;
158704a84ccSEzequiel Garcia
159704a84ccSEzequiel Garcia reg_write(dev, AUDIO_CONTROL2, reg);
160704a84ccSEzequiel Garcia }
161704a84ccSEzequiel Garcia
162447d7c32SEzequiel Garcia if (dev->period_size != period_size) {
163447d7c32SEzequiel Garcia u32 reg;
164447d7c32SEzequiel Garcia
165447d7c32SEzequiel Garcia dev->period_size = period_size;
166447d7c32SEzequiel Garcia reg = reg_read(dev, AUDIO_CONTROL1);
167447d7c32SEzequiel Garcia reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
168447d7c32SEzequiel Garcia reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
169447d7c32SEzequiel Garcia
170447d7c32SEzequiel Garcia reg_write(dev, AUDIO_CONTROL1, reg);
171704a84ccSEzequiel Garcia }
172704a84ccSEzequiel Garcia
173447d7c32SEzequiel Garcia if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
174447d7c32SEzequiel Garcia rt->periods > TW686X_AUDIO_PERIODS_MAX)
175447d7c32SEzequiel Garcia return -EINVAL;
176447d7c32SEzequiel Garcia
177704a84ccSEzequiel Garcia spin_lock_irqsave(&ac->lock, flags);
178704a84ccSEzequiel Garcia INIT_LIST_HEAD(&ac->buf_list);
179704a84ccSEzequiel Garcia
180704a84ccSEzequiel Garcia for (i = 0; i < rt->periods; i++) {
181704a84ccSEzequiel Garcia ac->buf[i].dma = rt->dma_addr + period_size * i;
182704a84ccSEzequiel Garcia ac->buf[i].virt = rt->dma_area + period_size * i;
183704a84ccSEzequiel Garcia INIT_LIST_HEAD(&ac->buf[i].list);
184704a84ccSEzequiel Garcia list_add_tail(&ac->buf[i].list, &ac->buf_list);
185704a84ccSEzequiel Garcia }
186704a84ccSEzequiel Garcia
187704a84ccSEzequiel Garcia p_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
188704a84ccSEzequiel Garcia list_move_tail(&p_buf->list, &ac->buf_list);
189704a84ccSEzequiel Garcia
190704a84ccSEzequiel Garcia b_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
191704a84ccSEzequiel Garcia list_move_tail(&b_buf->list, &ac->buf_list);
192704a84ccSEzequiel Garcia
193704a84ccSEzequiel Garcia ac->curr_bufs[0] = p_buf;
194704a84ccSEzequiel Garcia ac->curr_bufs[1] = b_buf;
195704a84ccSEzequiel Garcia ac->ptr = 0;
196c156c54dSEzequiel Garcia
197c156c54dSEzequiel Garcia if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
198c156c54dSEzequiel Garcia reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
199c156c54dSEzequiel Garcia reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
200c156c54dSEzequiel Garcia }
201c156c54dSEzequiel Garcia
202704a84ccSEzequiel Garcia spin_unlock_irqrestore(&ac->lock, flags);
203704a84ccSEzequiel Garcia
204704a84ccSEzequiel Garcia return 0;
2056deab6feSEzequiel Garcia
2066deab6feSEzequiel Garcia err_audio_busy:
2076deab6feSEzequiel Garcia spin_unlock_irqrestore(&dev->lock, flags);
2086deab6feSEzequiel Garcia return -EBUSY;
209704a84ccSEzequiel Garcia }
210704a84ccSEzequiel Garcia
tw686x_pcm_trigger(struct snd_pcm_substream * ss,int cmd)211704a84ccSEzequiel Garcia static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
212704a84ccSEzequiel Garcia {
213704a84ccSEzequiel Garcia struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
214704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
215704a84ccSEzequiel Garcia unsigned long flags;
216704a84ccSEzequiel Garcia int err = 0;
217704a84ccSEzequiel Garcia
218704a84ccSEzequiel Garcia switch (cmd) {
219704a84ccSEzequiel Garcia case SNDRV_PCM_TRIGGER_START:
220704a84ccSEzequiel Garcia if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
221704a84ccSEzequiel Garcia spin_lock_irqsave(&dev->lock, flags);
2226deab6feSEzequiel Garcia dev->audio_enabled = 1;
223704a84ccSEzequiel Garcia tw686x_enable_channel(dev,
224704a84ccSEzequiel Garcia AUDIO_CHANNEL_OFFSET + ac->ch);
225704a84ccSEzequiel Garcia spin_unlock_irqrestore(&dev->lock, flags);
226704a84ccSEzequiel Garcia
227704a84ccSEzequiel Garcia mod_timer(&dev->dma_delay_timer,
228704a84ccSEzequiel Garcia jiffies + msecs_to_jiffies(100));
229704a84ccSEzequiel Garcia } else {
230704a84ccSEzequiel Garcia err = -EIO;
231704a84ccSEzequiel Garcia }
232704a84ccSEzequiel Garcia break;
233704a84ccSEzequiel Garcia case SNDRV_PCM_TRIGGER_STOP:
234704a84ccSEzequiel Garcia spin_lock_irqsave(&dev->lock, flags);
2356deab6feSEzequiel Garcia dev->audio_enabled = 0;
236704a84ccSEzequiel Garcia tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
237704a84ccSEzequiel Garcia spin_unlock_irqrestore(&dev->lock, flags);
238704a84ccSEzequiel Garcia
239704a84ccSEzequiel Garcia spin_lock_irqsave(&ac->lock, flags);
240704a84ccSEzequiel Garcia ac->curr_bufs[0] = NULL;
241704a84ccSEzequiel Garcia ac->curr_bufs[1] = NULL;
242704a84ccSEzequiel Garcia spin_unlock_irqrestore(&ac->lock, flags);
243704a84ccSEzequiel Garcia break;
244704a84ccSEzequiel Garcia default:
245704a84ccSEzequiel Garcia err = -EINVAL;
246704a84ccSEzequiel Garcia }
247704a84ccSEzequiel Garcia return err;
248704a84ccSEzequiel Garcia }
249704a84ccSEzequiel Garcia
tw686x_pcm_pointer(struct snd_pcm_substream * ss)250704a84ccSEzequiel Garcia static snd_pcm_uframes_t tw686x_pcm_pointer(struct snd_pcm_substream *ss)
251704a84ccSEzequiel Garcia {
252704a84ccSEzequiel Garcia struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
253704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
254704a84ccSEzequiel Garcia
255704a84ccSEzequiel Garcia return bytes_to_frames(ss->runtime, ac->ptr);
256704a84ccSEzequiel Garcia }
257704a84ccSEzequiel Garcia
2585c8d8c01SJulia Lawall static const struct snd_pcm_ops tw686x_pcm_ops = {
259704a84ccSEzequiel Garcia .open = tw686x_pcm_open,
260704a84ccSEzequiel Garcia .close = tw686x_pcm_close,
261704a84ccSEzequiel Garcia .prepare = tw686x_pcm_prepare,
262704a84ccSEzequiel Garcia .trigger = tw686x_pcm_trigger,
263704a84ccSEzequiel Garcia .pointer = tw686x_pcm_pointer,
264704a84ccSEzequiel Garcia };
265704a84ccSEzequiel Garcia
tw686x_snd_pcm_init(struct tw686x_dev * dev)266704a84ccSEzequiel Garcia static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
267704a84ccSEzequiel Garcia {
268704a84ccSEzequiel Garcia struct snd_card *card = dev->snd_card;
269704a84ccSEzequiel Garcia struct snd_pcm *pcm;
270704a84ccSEzequiel Garcia struct snd_pcm_substream *ss;
271704a84ccSEzequiel Garcia unsigned int i;
272704a84ccSEzequiel Garcia int err;
273704a84ccSEzequiel Garcia
274704a84ccSEzequiel Garcia err = snd_pcm_new(card, card->driver, 0, 0, max_channels(dev), &pcm);
275704a84ccSEzequiel Garcia if (err < 0)
276704a84ccSEzequiel Garcia return err;
277704a84ccSEzequiel Garcia
278704a84ccSEzequiel Garcia snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &tw686x_pcm_ops);
279704a84ccSEzequiel Garcia snd_pcm_chip(pcm) = dev;
280704a84ccSEzequiel Garcia pcm->info_flags = 0;
281c0decac1SMauro Carvalho Chehab strscpy(pcm->name, "tw686x PCM", sizeof(pcm->name));
282704a84ccSEzequiel Garcia
283704a84ccSEzequiel Garcia for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
284704a84ccSEzequiel Garcia ss; ss = ss->next, i++)
285704a84ccSEzequiel Garcia snprintf(ss->name, sizeof(ss->name), "vch%u audio", i);
286704a84ccSEzequiel Garcia
2875e625dd5STakashi Iwai snd_pcm_set_managed_buffer_all(pcm,
288704a84ccSEzequiel Garcia SNDRV_DMA_TYPE_DEV,
2893b316a8fSTakashi Iwai &dev->pci_dev->dev,
290447d7c32SEzequiel Garcia TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
291447d7c32SEzequiel Garcia TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
29262e94da3STakashi Iwai return 0;
293704a84ccSEzequiel Garcia }
294704a84ccSEzequiel Garcia
tw686x_audio_dma_free(struct tw686x_dev * dev,struct tw686x_audio_channel * ac)295704a84ccSEzequiel Garcia static void tw686x_audio_dma_free(struct tw686x_dev *dev,
296704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac)
297704a84ccSEzequiel Garcia {
298704a84ccSEzequiel Garcia int pb;
299704a84ccSEzequiel Garcia
300704a84ccSEzequiel Garcia for (pb = 0; pb < 2; pb++) {
301704a84ccSEzequiel Garcia if (!ac->dma_descs[pb].virt)
302704a84ccSEzequiel Garcia continue;
303c1dbcf62SChristophe JAILLET dma_free_coherent(&dev->pci_dev->dev, ac->dma_descs[pb].size,
304704a84ccSEzequiel Garcia ac->dma_descs[pb].virt,
305704a84ccSEzequiel Garcia ac->dma_descs[pb].phys);
306704a84ccSEzequiel Garcia ac->dma_descs[pb].virt = NULL;
307704a84ccSEzequiel Garcia }
308704a84ccSEzequiel Garcia }
309704a84ccSEzequiel Garcia
tw686x_audio_dma_alloc(struct tw686x_dev * dev,struct tw686x_audio_channel * ac)310704a84ccSEzequiel Garcia static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
311704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac)
312704a84ccSEzequiel Garcia {
313704a84ccSEzequiel Garcia int pb;
314704a84ccSEzequiel Garcia
315c156c54dSEzequiel Garcia /*
316c1dbcf62SChristophe JAILLET * In the memcpy DMA mode we allocate a coherent buffer
317c156c54dSEzequiel Garcia * and use it for the DMA capture. Otherwise, DMA
318c156c54dSEzequiel Garcia * acts on the ALSA buffers as received in pcm_prepare.
319c156c54dSEzequiel Garcia */
320c156c54dSEzequiel Garcia if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
321c156c54dSEzequiel Garcia return 0;
322c156c54dSEzequiel Garcia
323704a84ccSEzequiel Garcia for (pb = 0; pb < 2; pb++) {
324704a84ccSEzequiel Garcia u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
325704a84ccSEzequiel Garcia void *virt;
326704a84ccSEzequiel Garcia
327c1dbcf62SChristophe JAILLET virt = dma_alloc_coherent(&dev->pci_dev->dev,
328c1dbcf62SChristophe JAILLET AUDIO_DMA_SIZE_MAX,
329c1dbcf62SChristophe JAILLET &ac->dma_descs[pb].phys, GFP_KERNEL);
330704a84ccSEzequiel Garcia if (!virt) {
331704a84ccSEzequiel Garcia dev_err(&dev->pci_dev->dev,
332704a84ccSEzequiel Garcia "dma%d: unable to allocate audio DMA %s-buffer\n",
333704a84ccSEzequiel Garcia ac->ch, pb ? "B" : "P");
334704a84ccSEzequiel Garcia return -ENOMEM;
335704a84ccSEzequiel Garcia }
336704a84ccSEzequiel Garcia ac->dma_descs[pb].virt = virt;
337447d7c32SEzequiel Garcia ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
338704a84ccSEzequiel Garcia reg_write(dev, reg, ac->dma_descs[pb].phys);
339704a84ccSEzequiel Garcia }
340704a84ccSEzequiel Garcia return 0;
341704a84ccSEzequiel Garcia }
342704a84ccSEzequiel Garcia
tw686x_audio_free(struct tw686x_dev * dev)343704a84ccSEzequiel Garcia void tw686x_audio_free(struct tw686x_dev *dev)
344704a84ccSEzequiel Garcia {
345704a84ccSEzequiel Garcia unsigned long flags;
346704a84ccSEzequiel Garcia u32 dma_ch_mask;
347704a84ccSEzequiel Garcia u32 dma_cmd;
348704a84ccSEzequiel Garcia
349704a84ccSEzequiel Garcia spin_lock_irqsave(&dev->lock, flags);
350704a84ccSEzequiel Garcia dma_cmd = reg_read(dev, DMA_CMD);
351704a84ccSEzequiel Garcia dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
352704a84ccSEzequiel Garcia reg_write(dev, DMA_CMD, dma_cmd & ~0xff00);
353704a84ccSEzequiel Garcia reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask & ~0xff00);
354704a84ccSEzequiel Garcia spin_unlock_irqrestore(&dev->lock, flags);
355704a84ccSEzequiel Garcia
356704a84ccSEzequiel Garcia if (!dev->snd_card)
357704a84ccSEzequiel Garcia return;
358704a84ccSEzequiel Garcia snd_card_free(dev->snd_card);
359704a84ccSEzequiel Garcia dev->snd_card = NULL;
360704a84ccSEzequiel Garcia }
361704a84ccSEzequiel Garcia
tw686x_audio_init(struct tw686x_dev * dev)362704a84ccSEzequiel Garcia int tw686x_audio_init(struct tw686x_dev *dev)
363704a84ccSEzequiel Garcia {
364704a84ccSEzequiel Garcia struct pci_dev *pci_dev = dev->pci_dev;
365704a84ccSEzequiel Garcia struct snd_card *card;
366704a84ccSEzequiel Garcia int err, ch;
367704a84ccSEzequiel Garcia
368447d7c32SEzequiel Garcia /* Enable external audio */
369447d7c32SEzequiel Garcia reg_write(dev, AUDIO_CONTROL1, BIT(0));
370704a84ccSEzequiel Garcia
371704a84ccSEzequiel Garcia err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
372704a84ccSEzequiel Garcia SNDRV_DEFAULT_STR1,
373704a84ccSEzequiel Garcia THIS_MODULE, 0, &card);
374704a84ccSEzequiel Garcia if (err < 0)
375704a84ccSEzequiel Garcia return err;
376704a84ccSEzequiel Garcia
377704a84ccSEzequiel Garcia dev->snd_card = card;
378c0decac1SMauro Carvalho Chehab strscpy(card->driver, "tw686x", sizeof(card->driver));
379c0decac1SMauro Carvalho Chehab strscpy(card->shortname, "tw686x", sizeof(card->shortname));
380c0decac1SMauro Carvalho Chehab strscpy(card->longname, pci_name(pci_dev), sizeof(card->longname));
381704a84ccSEzequiel Garcia snd_card_set_dev(card, &pci_dev->dev);
382704a84ccSEzequiel Garcia
383704a84ccSEzequiel Garcia for (ch = 0; ch < max_channels(dev); ch++) {
384704a84ccSEzequiel Garcia struct tw686x_audio_channel *ac;
385704a84ccSEzequiel Garcia
386704a84ccSEzequiel Garcia ac = &dev->audio_channels[ch];
387704a84ccSEzequiel Garcia spin_lock_init(&ac->lock);
388704a84ccSEzequiel Garcia ac->dev = dev;
389704a84ccSEzequiel Garcia ac->ch = ch;
390704a84ccSEzequiel Garcia
391704a84ccSEzequiel Garcia err = tw686x_audio_dma_alloc(dev, ac);
392704a84ccSEzequiel Garcia if (err < 0)
393704a84ccSEzequiel Garcia goto err_cleanup;
394704a84ccSEzequiel Garcia }
395704a84ccSEzequiel Garcia
396704a84ccSEzequiel Garcia err = tw686x_snd_pcm_init(dev);
397704a84ccSEzequiel Garcia if (err < 0)
398704a84ccSEzequiel Garcia goto err_cleanup;
399704a84ccSEzequiel Garcia
400704a84ccSEzequiel Garcia err = snd_card_register(card);
401704a84ccSEzequiel Garcia if (!err)
402704a84ccSEzequiel Garcia return 0;
403704a84ccSEzequiel Garcia
404704a84ccSEzequiel Garcia err_cleanup:
405704a84ccSEzequiel Garcia for (ch = 0; ch < max_channels(dev); ch++) {
406704a84ccSEzequiel Garcia if (!dev->audio_channels[ch].dev)
407704a84ccSEzequiel Garcia continue;
408704a84ccSEzequiel Garcia tw686x_audio_dma_free(dev, &dev->audio_channels[ch]);
409704a84ccSEzequiel Garcia }
410704a84ccSEzequiel Garcia snd_card_free(card);
411704a84ccSEzequiel Garcia dev->snd_card = NULL;
412704a84ccSEzequiel Garcia return err;
413704a84ccSEzequiel Garcia }
414