treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / pci / solo6x10 / solo6x10-g723.c
blobd6d16e8fd997e403fa0a73cc8b1e758fddd62d71
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Original author:
6 * Ben Collins <bcollins@ubuntu.com>
8 * Additional work by:
9 * John Brooks <john.brooks@bluecherry.net>
12 #include <linux/kernel.h>
13 #include <linux/mempool.h>
14 #include <linux/poll.h>
15 #include <linux/kthread.h>
16 #include <linux/freezer.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
20 #include <sound/core.h>
21 #include <sound/initval.h>
22 #include <sound/pcm.h>
23 #include <sound/control.h>
25 #include "solo6x10.h"
26 #include "solo6x10-tw28.h"
28 #define G723_FDMA_PAGES 32
29 #define G723_PERIOD_BYTES 48
30 #define G723_PERIOD_BLOCK 1024
31 #define G723_FRAMES_PER_PAGE 48
33 /* Sets up channels 16-19 for decoding and 0-15 for encoding */
34 #define OUTMODE_MASK 0x300
36 #define SAMPLERATE 8000
37 #define BITRATE 25
39 /* The solo writes to 1k byte pages, 32 pages, in the dma. Each 1k page
40 * is broken down to 20 * 48 byte regions (one for each channel possible)
41 * with the rest of the page being dummy data. */
42 #define PERIODS G723_FDMA_PAGES
43 #define G723_INTR_ORDER 4 /* 0 - 4 */
45 struct solo_snd_pcm {
46 int on;
47 spinlock_t lock;
48 struct solo_dev *solo_dev;
49 u8 *g723_buf;
50 dma_addr_t g723_dma;
53 static void solo_g723_config(struct solo_dev *solo_dev)
55 int clk_div;
57 clk_div = (solo_dev->clock_mhz * 1000000)
58 / (SAMPLERATE * (BITRATE * 2) * 2);
60 solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
61 SOLO_AUDIO_BITRATE(BITRATE)
62 | SOLO_AUDIO_CLK_DIV(clk_div));
64 solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
65 SOLO_AUDIO_FDMA_INTERVAL(1)
66 | SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER)
67 | SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
69 solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
70 SOLO_AUDIO_ENABLE
71 | SOLO_AUDIO_I2S_MODE
72 | SOLO_AUDIO_I2S_MULTI(3)
73 | SOLO_AUDIO_MODE(OUTMODE_MASK));
76 void solo_g723_isr(struct solo_dev *solo_dev)
78 struct snd_pcm_str *pstr =
79 &solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
80 struct snd_pcm_substream *ss;
81 struct solo_snd_pcm *solo_pcm;
83 for (ss = pstr->substream; ss != NULL; ss = ss->next) {
84 if (snd_pcm_substream_chip(ss) == NULL)
85 continue;
87 /* This means open() hasn't been called on this one */
88 if (snd_pcm_substream_chip(ss) == solo_dev)
89 continue;
91 /* Haven't triggered a start yet */
92 solo_pcm = snd_pcm_substream_chip(ss);
93 if (!solo_pcm->on)
94 continue;
96 snd_pcm_period_elapsed(ss);
100 static const struct snd_pcm_hardware snd_solo_pcm_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_U8,
106 .rates = SNDRV_PCM_RATE_8000,
107 .rate_min = SAMPLERATE,
108 .rate_max = SAMPLERATE,
109 .channels_min = 1,
110 .channels_max = 1,
111 .buffer_bytes_max = G723_PERIOD_BYTES * PERIODS,
112 .period_bytes_min = G723_PERIOD_BYTES,
113 .period_bytes_max = G723_PERIOD_BYTES,
114 .periods_min = PERIODS,
115 .periods_max = PERIODS,
118 static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
120 struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
121 struct solo_snd_pcm *solo_pcm;
123 solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
124 if (solo_pcm == NULL)
125 goto oom;
127 solo_pcm->g723_buf = pci_alloc_consistent(solo_dev->pdev,
128 G723_PERIOD_BYTES,
129 &solo_pcm->g723_dma);
130 if (solo_pcm->g723_buf == NULL)
131 goto oom;
133 spin_lock_init(&solo_pcm->lock);
134 solo_pcm->solo_dev = solo_dev;
135 ss->runtime->hw = snd_solo_pcm_hw;
137 snd_pcm_substream_chip(ss) = solo_pcm;
139 return 0;
141 oom:
142 kfree(solo_pcm);
143 return -ENOMEM;
146 static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
148 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
150 snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
151 pci_free_consistent(solo_pcm->solo_dev->pdev, G723_PERIOD_BYTES,
152 solo_pcm->g723_buf, solo_pcm->g723_dma);
153 kfree(solo_pcm);
155 return 0;
158 static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
160 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
161 struct solo_dev *solo_dev = solo_pcm->solo_dev;
162 int ret = 0;
164 spin_lock(&solo_pcm->lock);
166 switch (cmd) {
167 case SNDRV_PCM_TRIGGER_START:
168 if (solo_pcm->on == 0) {
169 /* If this is the first user, switch on interrupts */
170 if (atomic_inc_return(&solo_dev->snd_users) == 1)
171 solo_irq_on(solo_dev, SOLO_IRQ_G723);
172 solo_pcm->on = 1;
174 break;
175 case SNDRV_PCM_TRIGGER_STOP:
176 if (solo_pcm->on) {
177 /* If this was our last user, switch them off */
178 if (atomic_dec_return(&solo_dev->snd_users) == 0)
179 solo_irq_off(solo_dev, SOLO_IRQ_G723);
180 solo_pcm->on = 0;
182 break;
183 default:
184 ret = -EINVAL;
187 spin_unlock(&solo_pcm->lock);
189 return ret;
192 static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
194 return 0;
197 static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
199 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
200 struct solo_dev *solo_dev = solo_pcm->solo_dev;
201 snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
203 return idx * G723_FRAMES_PER_PAGE;
206 static int snd_solo_pcm_copy_user(struct snd_pcm_substream *ss, int channel,
207 unsigned long pos, void __user *dst,
208 unsigned long count)
210 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
211 struct solo_dev *solo_dev = solo_pcm->solo_dev;
212 int err, i;
214 for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
215 int page = (pos / G723_FRAMES_PER_PAGE) + i;
217 err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
218 SOLO_G723_EXT_ADDR(solo_dev) +
219 (page * G723_PERIOD_BLOCK) +
220 (ss->number * G723_PERIOD_BYTES),
221 G723_PERIOD_BYTES, 0, 0);
222 if (err)
223 return err;
225 if (copy_to_user(dst, solo_pcm->g723_buf, G723_PERIOD_BYTES))
226 return -EFAULT;
227 dst += G723_PERIOD_BYTES;
230 return 0;
233 static int snd_solo_pcm_copy_kernel(struct snd_pcm_substream *ss, int channel,
234 unsigned long pos, void *dst,
235 unsigned long count)
237 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
238 struct solo_dev *solo_dev = solo_pcm->solo_dev;
239 int err, i;
241 for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
242 int page = (pos / G723_FRAMES_PER_PAGE) + i;
244 err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
245 SOLO_G723_EXT_ADDR(solo_dev) +
246 (page * G723_PERIOD_BLOCK) +
247 (ss->number * G723_PERIOD_BYTES),
248 G723_PERIOD_BYTES, 0, 0);
249 if (err)
250 return err;
252 memcpy(dst, solo_pcm->g723_buf, G723_PERIOD_BYTES);
253 dst += G723_PERIOD_BYTES;
256 return 0;
259 static const struct snd_pcm_ops snd_solo_pcm_ops = {
260 .open = snd_solo_pcm_open,
261 .close = snd_solo_pcm_close,
262 .prepare = snd_solo_pcm_prepare,
263 .trigger = snd_solo_pcm_trigger,
264 .pointer = snd_solo_pcm_pointer,
265 .copy_user = snd_solo_pcm_copy_user,
266 .copy_kernel = snd_solo_pcm_copy_kernel,
269 static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_info *info)
272 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
273 info->count = 1;
274 info->value.integer.min = 0;
275 info->value.integer.max = 15;
276 info->value.integer.step = 1;
278 return 0;
281 static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
282 struct snd_ctl_elem_value *value)
284 struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
285 u8 ch = value->id.numid - 1;
287 value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
289 return 0;
292 static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_value *value)
295 struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
296 u8 ch = value->id.numid - 1;
297 u8 old_val;
299 old_val = tw28_get_audio_gain(solo_dev, ch);
300 if (old_val == value->value.integer.value[0])
301 return 0;
303 tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
305 return 1;
308 static const struct snd_kcontrol_new snd_solo_capture_volume = {
309 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
310 .name = "Capture Volume",
311 .info = snd_solo_capture_volume_info,
312 .get = snd_solo_capture_volume_get,
313 .put = snd_solo_capture_volume_put,
316 static int solo_snd_pcm_init(struct solo_dev *solo_dev)
318 struct snd_card *card = solo_dev->snd_card;
319 struct snd_pcm *pcm;
320 struct snd_pcm_substream *ss;
321 int ret;
322 int i;
324 ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
325 &pcm);
326 if (ret < 0)
327 return ret;
329 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
330 &snd_solo_pcm_ops);
332 snd_pcm_chip(pcm) = solo_dev;
333 pcm->info_flags = 0;
334 strscpy(pcm->name, card->shortname, sizeof(pcm->name));
336 for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
337 ss; ss = ss->next, i++)
338 sprintf(ss->name, "Camera #%d Audio", i);
340 snd_pcm_set_managed_buffer_all(pcm,
341 SNDRV_DMA_TYPE_CONTINUOUS,
342 NULL,
343 G723_PERIOD_BYTES * PERIODS,
344 G723_PERIOD_BYTES * PERIODS);
346 solo_dev->snd_pcm = pcm;
348 return 0;
351 int solo_g723_init(struct solo_dev *solo_dev)
353 static struct snd_device_ops ops = { };
354 struct snd_card *card;
355 struct snd_kcontrol_new kctl;
356 char name[32];
357 int ret;
359 atomic_set(&solo_dev->snd_users, 0);
361 /* Allows for easier mapping between video and audio */
362 sprintf(name, "Softlogic%d", solo_dev->vfd->num);
364 ret = snd_card_new(&solo_dev->pdev->dev,
365 SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
366 &solo_dev->snd_card);
367 if (ret < 0)
368 return ret;
370 card = solo_dev->snd_card;
372 strscpy(card->driver, SOLO6X10_NAME, sizeof(card->driver));
373 strscpy(card->shortname, "SOLO-6x10 Audio", sizeof(card->shortname));
374 sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
375 pci_name(solo_dev->pdev), solo_dev->pdev->irq);
377 ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
378 if (ret < 0)
379 goto snd_error;
381 /* Mixer controls */
382 strscpy(card->mixername, "SOLO-6x10", sizeof(card->mixername));
383 kctl = snd_solo_capture_volume;
384 kctl.count = solo_dev->nr_chans;
386 ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
387 if (ret < 0)
388 return ret;
390 ret = solo_snd_pcm_init(solo_dev);
391 if (ret < 0)
392 goto snd_error;
394 ret = snd_card_register(card);
395 if (ret < 0)
396 goto snd_error;
398 solo_g723_config(solo_dev);
400 dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
402 return 0;
404 snd_error:
405 snd_card_free(card);
406 return ret;
409 void solo_g723_exit(struct solo_dev *solo_dev)
411 if (!solo_dev->snd_card)
412 return;
414 solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
415 solo_irq_off(solo_dev, SOLO_IRQ_G723);
417 snd_card_free(solo_dev->snd_card);
418 solo_dev->snd_card = NULL;