blob: 4a37a1c51c48830274033981dd2cb07bc307b602 [file] [log] [blame]
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001/*
Hans Verkuildcae5da2013-03-25 05:35:17 -03002 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
3 *
4 * Original author:
5 * Ben Collins <bcollins@ubuntu.com>
6 *
7 * Additional work by:
8 * John Brooks <john.brooks@bluecherry.net>
Ben Collinsfaa4fd22010-06-17 13:27:26 -04009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Ben Collinsfaa4fd22010-06-17 13:27:26 -040019 */
20
21#include <linux/kernel.h>
22#include <linux/mempool.h>
23#include <linux/poll.h>
24#include <linux/kthread.h>
25#include <linux/freezer.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030026#include <linux/module.h>
27#include <linux/slab.h>
28
Ben Collinsfaa4fd22010-06-17 13:27:26 -040029#include <sound/core.h>
30#include <sound/initval.h>
31#include <sound/pcm.h>
32#include <sound/control.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030033
Krzysztof Hałasaae69b222011-02-11 13:36:27 +010034#include "solo6x10.h"
Hans Verkuildad7fab2013-03-25 05:42:46 -030035#include "solo6x10-tw28.h"
Ben Collinsfaa4fd22010-06-17 13:27:26 -040036
Ben Collinsfaa4fd22010-06-17 13:27:26 -040037#define G723_FDMA_PAGES 32
38#define G723_PERIOD_BYTES 48
39#define G723_PERIOD_BLOCK 1024
40#define G723_FRAMES_PER_PAGE 48
41
42/* Sets up channels 16-19 for decoding and 0-15 for encoding */
43#define OUTMODE_MASK 0x300
44
45#define SAMPLERATE 8000
46#define BITRATE 25
47
48/* The solo writes to 1k byte pages, 32 pages, in the dma. Each 1k page
49 * is broken down to 20 * 48 byte regions (one for each channel possible)
50 * with the rest of the page being dummy data. */
Krzysztof Hałasa9d1b1f62015-06-08 10:35:05 -030051#define PERIODS G723_FDMA_PAGES
Hans Verkuildcae5da2013-03-25 05:35:17 -030052#define G723_INTR_ORDER 4 /* 0 - 4 */
Ben Collinsfaa4fd22010-06-17 13:27:26 -040053
54struct solo_snd_pcm {
Hans Verkuildcae5da2013-03-25 05:35:17 -030055 int on;
56 spinlock_t lock;
Ismael Luceno8a4d9a92014-12-24 08:35:59 -030057 struct solo_dev *solo_dev;
58 u8 *g723_buf;
Hans Verkuildcae5da2013-03-25 05:35:17 -030059 dma_addr_t g723_dma;
Ben Collinsfaa4fd22010-06-17 13:27:26 -040060};
61
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010062static void solo_g723_config(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040063{
64 int clk_div;
65
Hans Verkuildcae5da2013-03-25 05:35:17 -030066 clk_div = (solo_dev->clock_mhz * 1000000)
67 / (SAMPLERATE * (BITRATE * 2) * 2);
Ben Collinsfaa4fd22010-06-17 13:27:26 -040068
69 solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
Hans Verkuildcae5da2013-03-25 05:35:17 -030070 SOLO_AUDIO_BITRATE(BITRATE)
71 | SOLO_AUDIO_CLK_DIV(clk_div));
Ben Collinsfaa4fd22010-06-17 13:27:26 -040072
73 solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
Hans Verkuildcae5da2013-03-25 05:35:17 -030074 SOLO_AUDIO_FDMA_INTERVAL(1)
75 | SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER)
76 | SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
Ben Collinsfaa4fd22010-06-17 13:27:26 -040077
78 solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
Hans Verkuildcae5da2013-03-25 05:35:17 -030079 SOLO_AUDIO_ENABLE
80 | SOLO_AUDIO_I2S_MODE
81 | SOLO_AUDIO_I2S_MULTI(3)
82 | SOLO_AUDIO_MODE(OUTMODE_MASK));
Ben Collinsfaa4fd22010-06-17 13:27:26 -040083}
84
Krzysztof Hałasadecebab2011-02-11 13:38:20 +010085void solo_g723_isr(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040086{
87 struct snd_pcm_str *pstr =
88 &solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
89 struct snd_pcm_substream *ss;
90 struct solo_snd_pcm *solo_pcm;
91
Ben Collinsfaa4fd22010-06-17 13:27:26 -040092 for (ss = pstr->substream; ss != NULL; ss = ss->next) {
93 if (snd_pcm_substream_chip(ss) == NULL)
94 continue;
95
96 /* This means open() hasn't been called on this one */
97 if (snd_pcm_substream_chip(ss) == solo_dev)
98 continue;
99
100 /* Haven't triggered a start yet */
101 solo_pcm = snd_pcm_substream_chip(ss);
102 if (!solo_pcm->on)
103 continue;
104
105 snd_pcm_period_elapsed(ss);
106 }
107}
108
109static int snd_solo_hw_params(struct snd_pcm_substream *ss,
110 struct snd_pcm_hw_params *hw_params)
111{
112 return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
113}
114
115static int snd_solo_hw_free(struct snd_pcm_substream *ss)
116{
117 return snd_pcm_lib_free_pages(ss);
118}
119
Hans Verkuildcae5da2013-03-25 05:35:17 -0300120static const struct snd_pcm_hardware snd_solo_pcm_hw = {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400121 .info = (SNDRV_PCM_INFO_MMAP |
122 SNDRV_PCM_INFO_INTERLEAVED |
123 SNDRV_PCM_INFO_BLOCK_TRANSFER |
124 SNDRV_PCM_INFO_MMAP_VALID),
125 .formats = SNDRV_PCM_FMTBIT_U8,
126 .rates = SNDRV_PCM_RATE_8000,
Hans Verkuildcae5da2013-03-25 05:35:17 -0300127 .rate_min = SAMPLERATE,
128 .rate_max = SAMPLERATE,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400129 .channels_min = 1,
130 .channels_max = 1,
Krzysztof Hałasa9d1b1f62015-06-08 10:35:05 -0300131 .buffer_bytes_max = G723_PERIOD_BYTES * PERIODS,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400132 .period_bytes_min = G723_PERIOD_BYTES,
133 .period_bytes_max = G723_PERIOD_BYTES,
Krzysztof Hałasa9d1b1f62015-06-08 10:35:05 -0300134 .periods_min = PERIODS,
135 .periods_max = PERIODS,
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400136};
137
138static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
139{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100140 struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400141 struct solo_snd_pcm *solo_pcm;
142
143 solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
144 if (solo_pcm == NULL)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300145 goto oom;
146
147 solo_pcm->g723_buf = pci_alloc_consistent(solo_dev->pdev,
148 G723_PERIOD_BYTES,
149 &solo_pcm->g723_dma);
150 if (solo_pcm->g723_buf == NULL)
151 goto oom;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400152
153 spin_lock_init(&solo_pcm->lock);
154 solo_pcm->solo_dev = solo_dev;
155 ss->runtime->hw = snd_solo_pcm_hw;
156
157 snd_pcm_substream_chip(ss) = solo_pcm;
158
159 return 0;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300160
161oom:
162 kfree(solo_pcm);
163 return -ENOMEM;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400164}
165
166static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
167{
168 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
169
170 snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300171 pci_free_consistent(solo_pcm->solo_dev->pdev, G723_PERIOD_BYTES,
172 solo_pcm->g723_buf, solo_pcm->g723_dma);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400173 kfree(solo_pcm);
174
Ben Collins98e2d5a2010-11-04 22:37:15 -0400175 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400176}
177
178static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
179{
180 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100181 struct solo_dev *solo_dev = solo_pcm->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400182 int ret = 0;
183
184 spin_lock(&solo_pcm->lock);
185
186 switch (cmd) {
187 case SNDRV_PCM_TRIGGER_START:
188 if (solo_pcm->on == 0) {
189 /* If this is the first user, switch on interrupts */
190 if (atomic_inc_return(&solo_dev->snd_users) == 1)
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100191 solo_irq_on(solo_dev, SOLO_IRQ_G723);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400192 solo_pcm->on = 1;
193 }
194 break;
195 case SNDRV_PCM_TRIGGER_STOP:
196 if (solo_pcm->on) {
197 /* If this was our last user, switch them off */
198 if (atomic_dec_return(&solo_dev->snd_users) == 0)
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100199 solo_irq_off(solo_dev, SOLO_IRQ_G723);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400200 solo_pcm->on = 0;
201 }
202 break;
203 default:
204 ret = -EINVAL;
205 }
206
207 spin_unlock(&solo_pcm->lock);
208
209 return ret;
210}
211
212static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
213{
Ben Collins98e2d5a2010-11-04 22:37:15 -0400214 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400215}
216
217static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
218{
219 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100220 struct solo_dev *solo_dev = solo_pcm->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400221 snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
222
223 return idx * G723_FRAMES_PER_PAGE;
224}
225
226static int snd_solo_pcm_copy(struct snd_pcm_substream *ss, int channel,
227 snd_pcm_uframes_t pos, void __user *dst,
228 snd_pcm_uframes_t count)
229{
230 struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100231 struct solo_dev *solo_dev = solo_pcm->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400232 int err, i;
233
234 for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
235 int page = (pos / G723_FRAMES_PER_PAGE) + i;
236
Hans Verkuildcae5da2013-03-25 05:35:17 -0300237 err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
238 SOLO_G723_EXT_ADDR(solo_dev) +
239 (page * G723_PERIOD_BLOCK) +
240 (ss->number * G723_PERIOD_BYTES),
241 G723_PERIOD_BYTES, 0, 0);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400242 if (err)
243 return err;
244
245 err = copy_to_user(dst + (i * G723_PERIOD_BYTES),
246 solo_pcm->g723_buf, G723_PERIOD_BYTES);
247
248 if (err)
Dan Carpenter6a4ca032010-08-10 08:09:05 +0200249 return -EFAULT;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400250 }
251
252 return 0;
253}
254
255static struct snd_pcm_ops snd_solo_pcm_ops = {
256 .open = snd_solo_pcm_open,
257 .close = snd_solo_pcm_close,
258 .ioctl = snd_pcm_lib_ioctl,
259 .hw_params = snd_solo_hw_params,
260 .hw_free = snd_solo_hw_free,
261 .prepare = snd_solo_pcm_prepare,
262 .trigger = snd_solo_pcm_trigger,
263 .pointer = snd_solo_pcm_pointer,
264 .copy = snd_solo_pcm_copy,
265};
266
267static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
268 struct snd_ctl_elem_info *info)
269{
270 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
271 info->count = 1;
272 info->value.integer.min = 0;
273 info->value.integer.max = 15;
274 info->value.integer.step = 1;
275
276 return 0;
277}
278
279static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
280 struct snd_ctl_elem_value *value)
281{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100282 struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400283 u8 ch = value->id.numid - 1;
284
285 value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
286
Ben Collins98e2d5a2010-11-04 22:37:15 -0400287 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400288}
289
290static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *value)
292{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100293 struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400294 u8 ch = value->id.numid - 1;
Ben Collins98e2d5a2010-11-04 22:37:15 -0400295 u8 old_val;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400296
Ben Collins98e2d5a2010-11-04 22:37:15 -0400297 old_val = tw28_get_audio_gain(solo_dev, ch);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400298 if (old_val == value->value.integer.value[0])
299 return 0;
300
301 tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
302
Ben Collins98e2d5a2010-11-04 22:37:15 -0400303 return 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400304}
305
306static struct snd_kcontrol_new snd_solo_capture_volume = {
307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
308 .name = "Capture Volume",
309 .info = snd_solo_capture_volume_info,
310 .get = snd_solo_capture_volume_get,
311 .put = snd_solo_capture_volume_put,
312};
313
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100314static int solo_snd_pcm_init(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400315{
316 struct snd_card *card = solo_dev->snd_card;
317 struct snd_pcm *pcm;
318 struct snd_pcm_substream *ss;
319 int ret;
320 int i;
321
322 ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
323 &pcm);
324 if (ret < 0)
325 return ret;
326
327 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
328 &snd_solo_pcm_ops);
329
330 snd_pcm_chip(pcm) = solo_dev;
331 pcm->info_flags = 0;
332 strcpy(pcm->name, card->shortname);
333
334 for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
335 ss; ss = ss->next, i++)
336 sprintf(ss->name, "Camera #%d Audio", i);
337
338 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
339 SNDRV_DMA_TYPE_CONTINUOUS,
340 snd_dma_continuous_data(GFP_KERNEL),
Krzysztof Hałasa9d1b1f62015-06-08 10:35:05 -0300341 G723_PERIOD_BYTES * PERIODS,
342 G723_PERIOD_BYTES * PERIODS);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400343 if (ret < 0)
344 return ret;
345
346 solo_dev->snd_pcm = pcm;
347
348 return 0;
349}
350
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100351int solo_g723_init(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400352{
353 static struct snd_device_ops ops = { NULL };
354 struct snd_card *card;
355 struct snd_kcontrol_new kctl;
356 char name[32];
357 int ret;
358
359 atomic_set(&solo_dev->snd_users, 0);
360
361 /* Allows for easier mapping between video and audio */
362 sprintf(name, "Softlogic%d", solo_dev->vfd->num);
363
Takashi Iwai3a3d2af2014-01-29 15:03:37 +0100364 ret = snd_card_new(&solo_dev->pdev->dev,
365 SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
366 &solo_dev->snd_card);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400367 if (ret < 0)
368 return ret;
369
370 card = solo_dev->snd_card;
371
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100372 strcpy(card->driver, SOLO6X10_NAME);
373 strcpy(card->shortname, "SOLO-6x10 Audio");
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400374 sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
375 pci_name(solo_dev->pdev), solo_dev->pdev->irq);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400376
377 ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
378 if (ret < 0)
379 goto snd_error;
380
381 /* Mixer controls */
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100382 strcpy(card->mixername, "SOLO-6x10");
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400383 kctl = snd_solo_capture_volume;
384 kctl.count = solo_dev->nr_chans;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300385
Ben Collins98e2d5a2010-11-04 22:37:15 -0400386 ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400387 if (ret < 0)
388 return ret;
389
facugaichafabbe62010-11-10 10:39:33 -0300390 ret = solo_snd_pcm_init(solo_dev);
391 if (ret < 0)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400392 goto snd_error;
393
facugaichafabbe62010-11-10 10:39:33 -0300394 ret = snd_card_register(card);
395 if (ret < 0)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400396 goto snd_error;
397
398 solo_g723_config(solo_dev);
399
400 dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
401
402 return 0;
403
404snd_error:
405 snd_card_free(card);
406 return ret;
407}
408
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100409void solo_g723_exit(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400410{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300411 if (!solo_dev->snd_card)
412 return;
413
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400414 solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100415 solo_irq_off(solo_dev, SOLO_IRQ_G723);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400416
417 snd_card_free(solo_dev->snd_card);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300418 solo_dev->snd_card = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400419}