1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
4  *
5  * Based on the audio support from the tw6869 driver:
6  * Copyright 2015 www.starterkit.ru <info@starterkit.ru>
7  *
8  * Based on:
9  * Driver for Intersil|Techwell TW6869 based DVR cards
10  * (c) 2011-12 liran <jli11@intersil.com> [Intersil|Techwell China]
11  */
12 
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/kmod.h>
18 #include <linux/mutex.h>
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/control.h>
26 #include "tw686x.h"
27 #include "tw686x-regs.h"
28 
29 #define AUDIO_CHANNEL_OFFSET 8
30 
31 void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
32 		      unsigned int pb_status)
33 {
34 	unsigned long flags;
35 	unsigned int ch, pb;
36 
37 	for_each_set_bit(ch, &requests, max_channels(dev)) {
38 		struct tw686x_audio_channel *ac = &dev->audio_channels[ch];
39 		struct tw686x_audio_buf *done = NULL;
40 		struct tw686x_audio_buf *next = NULL;
41 		struct tw686x_dma_desc *desc;
42 
43 		pb = !!(pb_status & BIT(AUDIO_CHANNEL_OFFSET + ch));
44 
45 		spin_lock_irqsave(&ac->lock, flags);
46 
47 		/* Sanity check */
48 		if (!ac->ss || !ac->curr_bufs[0] || !ac->curr_bufs[1]) {
49 			spin_unlock_irqrestore(&ac->lock, flags);
50 			continue;
51 		}
52 
53 		if (!list_empty(&ac->buf_list)) {
54 			next = list_first_entry(&ac->buf_list,
55 					struct tw686x_audio_buf, list);
56 			list_move_tail(&next->list, &ac->buf_list);
57 			done = ac->curr_bufs[!pb];
58 			ac->curr_bufs[pb] = next;
59 		}
60 		spin_unlock_irqrestore(&ac->lock, flags);
61 
62 		if (!done || !next)
63 			continue;
64 		/*
65 		 * Checking for a non-nil dma_desc[pb]->virt buffer is
66 		 * the same as checking for memcpy DMA mode.
67 		 */
68 		desc = &ac->dma_descs[pb];
69 		if (desc->virt) {
70 			memcpy(done->virt, desc->virt,
71 			       dev->period_size);
72 		} else {
73 			u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
74 			reg_write(dev, reg, next->dma);
75 		}
76 		ac->ptr = done->dma - ac->buf[0].dma;
77 		snd_pcm_period_elapsed(ac->ss);
78 	}
79 }
80 
81 static int tw686x_pcm_hw_params(struct snd_pcm_substream *ss,
82 				struct snd_pcm_hw_params *hw_params)
83 {
84 	return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
85 }
86 
87 static int tw686x_pcm_hw_free(struct snd_pcm_substream *ss)
88 {
89 	return snd_pcm_lib_free_pages(ss);
90 }
91 
92 /*
93  * Audio parameters are global and shared among all
94  * capture channels. The driver prevents changes to
95  * the parameters if any audio channel is capturing.
96  */
97 static const struct snd_pcm_hardware tw686x_capture_hw = {
98 	.info			= (SNDRV_PCM_INFO_MMAP |
99 				   SNDRV_PCM_INFO_INTERLEAVED |
100 				   SNDRV_PCM_INFO_BLOCK_TRANSFER |
101 				   SNDRV_PCM_INFO_MMAP_VALID),
102 	.formats		= SNDRV_PCM_FMTBIT_S16_LE,
103 	.rates			= SNDRV_PCM_RATE_8000_48000,
104 	.rate_min		= 8000,
105 	.rate_max		= 48000,
106 	.channels_min		= 1,
107 	.channels_max		= 1,
108 	.buffer_bytes_max	= TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
109 	.period_bytes_min	= AUDIO_DMA_SIZE_MIN,
110 	.period_bytes_max	= AUDIO_DMA_SIZE_MAX,
111 	.periods_min		= TW686X_AUDIO_PERIODS_MIN,
112 	.periods_max		= TW686X_AUDIO_PERIODS_MAX,
113 };
114 
115 static int tw686x_pcm_open(struct snd_pcm_substream *ss)
116 {
117 	struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
118 	struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
119 	struct snd_pcm_runtime *rt = ss->runtime;
120 	int err;
121 
122 	ac->ss = ss;
123 	rt->hw = tw686x_capture_hw;
124 
125 	err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
126 	if (err < 0)
127 		return err;
128 
129 	return 0;
130 }
131 
132 static int tw686x_pcm_close(struct snd_pcm_substream *ss)
133 {
134 	struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
135 	struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
136 
137 	ac->ss = NULL;
138 	return 0;
139 }
140 
141 static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
142 {
143 	struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
144 	struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
145 	struct snd_pcm_runtime *rt = ss->runtime;
146 	unsigned int period_size = snd_pcm_lib_period_bytes(ss);
147 	struct tw686x_audio_buf *p_buf, *b_buf;
148 	unsigned long flags;
149 	int i;
150 
151 	spin_lock_irqsave(&dev->lock, flags);
152 	/*
153 	 * Given the audio parameters are global (i.e. shared across
154 	 * DMA channels), we need to check new params are allowed.
155 	 */
156 	if (((dev->audio_rate != rt->rate) ||
157 	     (dev->period_size != period_size)) && dev->audio_enabled)
158 		goto err_audio_busy;
159 
160 	tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
161 	spin_unlock_irqrestore(&dev->lock, flags);
162 
163 	if (dev->audio_rate != rt->rate) {
164 		u32 reg;
165 
166 		dev->audio_rate = rt->rate;
167 		reg = ((125000000 / rt->rate) << 16) +
168 		       ((125000000 % rt->rate) << 16) / rt->rate;
169 
170 		reg_write(dev, AUDIO_CONTROL2, reg);
171 	}
172 
173 	if (dev->period_size != period_size) {
174 		u32 reg;
175 
176 		dev->period_size = period_size;
177 		reg = reg_read(dev, AUDIO_CONTROL1);
178 		reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
179 		reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
180 
181 		reg_write(dev, AUDIO_CONTROL1, reg);
182 	}
183 
184 	if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
185 	    rt->periods > TW686X_AUDIO_PERIODS_MAX)
186 		return -EINVAL;
187 
188 	spin_lock_irqsave(&ac->lock, flags);
189 	INIT_LIST_HEAD(&ac->buf_list);
190 
191 	for (i = 0; i < rt->periods; i++) {
192 		ac->buf[i].dma = rt->dma_addr + period_size * i;
193 		ac->buf[i].virt = rt->dma_area + period_size * i;
194 		INIT_LIST_HEAD(&ac->buf[i].list);
195 		list_add_tail(&ac->buf[i].list, &ac->buf_list);
196 	}
197 
198 	p_buf =	list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
199 	list_move_tail(&p_buf->list, &ac->buf_list);
200 
201 	b_buf =	list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
202 	list_move_tail(&b_buf->list, &ac->buf_list);
203 
204 	ac->curr_bufs[0] = p_buf;
205 	ac->curr_bufs[1] = b_buf;
206 	ac->ptr = 0;
207 
208 	if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
209 		reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
210 		reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
211 	}
212 
213 	spin_unlock_irqrestore(&ac->lock, flags);
214 
215 	return 0;
216 
217 err_audio_busy:
218 	spin_unlock_irqrestore(&dev->lock, flags);
219 	return -EBUSY;
220 }
221 
222 static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
223 {
224 	struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
225 	struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
226 	unsigned long flags;
227 	int err = 0;
228 
229 	switch (cmd) {
230 	case SNDRV_PCM_TRIGGER_START:
231 		if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
232 			spin_lock_irqsave(&dev->lock, flags);
233 			dev->audio_enabled = 1;
234 			tw686x_enable_channel(dev,
235 				AUDIO_CHANNEL_OFFSET + ac->ch);
236 			spin_unlock_irqrestore(&dev->lock, flags);
237 
238 			mod_timer(&dev->dma_delay_timer,
239 				  jiffies + msecs_to_jiffies(100));
240 		} else {
241 			err = -EIO;
242 		}
243 		break;
244 	case SNDRV_PCM_TRIGGER_STOP:
245 		spin_lock_irqsave(&dev->lock, flags);
246 		dev->audio_enabled = 0;
247 		tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
248 		spin_unlock_irqrestore(&dev->lock, flags);
249 
250 		spin_lock_irqsave(&ac->lock, flags);
251 		ac->curr_bufs[0] = NULL;
252 		ac->curr_bufs[1] = NULL;
253 		spin_unlock_irqrestore(&ac->lock, flags);
254 		break;
255 	default:
256 		err = -EINVAL;
257 	}
258 	return err;
259 }
260 
261 static snd_pcm_uframes_t tw686x_pcm_pointer(struct snd_pcm_substream *ss)
262 {
263 	struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
264 	struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
265 
266 	return bytes_to_frames(ss->runtime, ac->ptr);
267 }
268 
269 static const struct snd_pcm_ops tw686x_pcm_ops = {
270 	.open = tw686x_pcm_open,
271 	.close = tw686x_pcm_close,
272 	.ioctl = snd_pcm_lib_ioctl,
273 	.hw_params = tw686x_pcm_hw_params,
274 	.hw_free = tw686x_pcm_hw_free,
275 	.prepare = tw686x_pcm_prepare,
276 	.trigger = tw686x_pcm_trigger,
277 	.pointer = tw686x_pcm_pointer,
278 };
279 
280 static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
281 {
282 	struct snd_card *card = dev->snd_card;
283 	struct snd_pcm *pcm;
284 	struct snd_pcm_substream *ss;
285 	unsigned int i;
286 	int err;
287 
288 	err = snd_pcm_new(card, card->driver, 0, 0, max_channels(dev), &pcm);
289 	if (err < 0)
290 		return err;
291 
292 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &tw686x_pcm_ops);
293 	snd_pcm_chip(pcm) = dev;
294 	pcm->info_flags = 0;
295 	strscpy(pcm->name, "tw686x PCM", sizeof(pcm->name));
296 
297 	for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
298 	     ss; ss = ss->next, i++)
299 		snprintf(ss->name, sizeof(ss->name), "vch%u audio", i);
300 
301 	snd_pcm_lib_preallocate_pages_for_all(pcm,
302 				SNDRV_DMA_TYPE_DEV,
303 				&dev->pci_dev->dev,
304 				TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
305 				TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
306 	return 0;
307 }
308 
309 static void tw686x_audio_dma_free(struct tw686x_dev *dev,
310 				  struct tw686x_audio_channel *ac)
311 {
312 	int pb;
313 
314 	for (pb = 0; pb < 2; pb++) {
315 		if (!ac->dma_descs[pb].virt)
316 			continue;
317 		pci_free_consistent(dev->pci_dev, ac->dma_descs[pb].size,
318 				    ac->dma_descs[pb].virt,
319 				    ac->dma_descs[pb].phys);
320 		ac->dma_descs[pb].virt = NULL;
321 	}
322 }
323 
324 static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
325 				  struct tw686x_audio_channel *ac)
326 {
327 	int pb;
328 
329 	/*
330 	 * In the memcpy DMA mode we allocate a consistent buffer
331 	 * and use it for the DMA capture. Otherwise, DMA
332 	 * acts on the ALSA buffers as received in pcm_prepare.
333 	 */
334 	if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
335 		return 0;
336 
337 	for (pb = 0; pb < 2; pb++) {
338 		u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
339 		void *virt;
340 
341 		virt = pci_alloc_consistent(dev->pci_dev, AUDIO_DMA_SIZE_MAX,
342 					    &ac->dma_descs[pb].phys);
343 		if (!virt) {
344 			dev_err(&dev->pci_dev->dev,
345 				"dma%d: unable to allocate audio DMA %s-buffer\n",
346 				ac->ch, pb ? "B" : "P");
347 			return -ENOMEM;
348 		}
349 		ac->dma_descs[pb].virt = virt;
350 		ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
351 		reg_write(dev, reg, ac->dma_descs[pb].phys);
352 	}
353 	return 0;
354 }
355 
356 void tw686x_audio_free(struct tw686x_dev *dev)
357 {
358 	unsigned long flags;
359 	u32 dma_ch_mask;
360 	u32 dma_cmd;
361 
362 	spin_lock_irqsave(&dev->lock, flags);
363 	dma_cmd = reg_read(dev, DMA_CMD);
364 	dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
365 	reg_write(dev, DMA_CMD, dma_cmd & ~0xff00);
366 	reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask & ~0xff00);
367 	spin_unlock_irqrestore(&dev->lock, flags);
368 
369 	if (!dev->snd_card)
370 		return;
371 	snd_card_free(dev->snd_card);
372 	dev->snd_card = NULL;
373 }
374 
375 int tw686x_audio_init(struct tw686x_dev *dev)
376 {
377 	struct pci_dev *pci_dev = dev->pci_dev;
378 	struct snd_card *card;
379 	int err, ch;
380 
381 	/* Enable external audio */
382 	reg_write(dev, AUDIO_CONTROL1, BIT(0));
383 
384 	err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
385 			   SNDRV_DEFAULT_STR1,
386 			   THIS_MODULE, 0, &card);
387 	if (err < 0)
388 		return err;
389 
390 	dev->snd_card = card;
391 	strscpy(card->driver, "tw686x", sizeof(card->driver));
392 	strscpy(card->shortname, "tw686x", sizeof(card->shortname));
393 	strscpy(card->longname, pci_name(pci_dev), sizeof(card->longname));
394 	snd_card_set_dev(card, &pci_dev->dev);
395 
396 	for (ch = 0; ch < max_channels(dev); ch++) {
397 		struct tw686x_audio_channel *ac;
398 
399 		ac = &dev->audio_channels[ch];
400 		spin_lock_init(&ac->lock);
401 		ac->dev = dev;
402 		ac->ch = ch;
403 
404 		err = tw686x_audio_dma_alloc(dev, ac);
405 		if (err < 0)
406 			goto err_cleanup;
407 	}
408 
409 	err = tw686x_snd_pcm_init(dev);
410 	if (err < 0)
411 		goto err_cleanup;
412 
413 	err = snd_card_register(card);
414 	if (!err)
415 		return 0;
416 
417 err_cleanup:
418 	for (ch = 0; ch < max_channels(dev); ch++) {
419 		if (!dev->audio_channels[ch].dev)
420 			continue;
421 		tw686x_audio_dma_free(dev, &dev->audio_channels[ch]);
422 	}
423 	snd_card_free(card);
424 	dev->snd_card = NULL;
425 	return err;
426 }
427