2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
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.
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.
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>
26 #include <linux/module.h>
27 #include <linux/slab.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/pcm.h>
32 #include <sound/control.h>
35 #include "solo6x10-tw28.h"
37 #define G723_FDMA_PAGES 32
38 #define G723_PERIOD_BYTES 48
39 #define G723_PERIOD_BLOCK 1024
40 #define G723_FRAMES_PER_PAGE 48
42 /* Sets up channels 16-19 for decoding and 0-15 for encoding */
43 #define OUTMODE_MASK 0x300
45 #define SAMPLERATE 8000
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. */
51 #define PERIODS G723_FDMA_PAGES
52 #define G723_INTR_ORDER 4 /* 0 - 4 */
57 struct solo_dev
*solo_dev
;
62 static void solo_g723_config(struct solo_dev
*solo_dev
)
66 clk_div
= (solo_dev
->clock_mhz
* 1000000)
67 / (SAMPLERATE
* (BITRATE
* 2) * 2);
69 solo_reg_write(solo_dev
, SOLO_AUDIO_SAMPLE
,
70 SOLO_AUDIO_BITRATE(BITRATE
)
71 | SOLO_AUDIO_CLK_DIV(clk_div
));
73 solo_reg_write(solo_dev
, SOLO_AUDIO_FDMA_INTR
,
74 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));
78 solo_reg_write(solo_dev
, SOLO_AUDIO_CONTROL
,
81 | SOLO_AUDIO_I2S_MULTI(3)
82 | SOLO_AUDIO_MODE(OUTMODE_MASK
));
85 void solo_g723_isr(struct solo_dev
*solo_dev
)
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
;
92 for (ss
= pstr
->substream
; ss
!= NULL
; ss
= ss
->next
) {
93 if (snd_pcm_substream_chip(ss
) == NULL
)
96 /* This means open() hasn't been called on this one */
97 if (snd_pcm_substream_chip(ss
) == solo_dev
)
100 /* Haven't triggered a start yet */
101 solo_pcm
= snd_pcm_substream_chip(ss
);
105 snd_pcm_period_elapsed(ss
);
109 static int snd_solo_hw_params(struct snd_pcm_substream
*ss
,
110 struct snd_pcm_hw_params
*hw_params
)
112 return snd_pcm_lib_malloc_pages(ss
, params_buffer_bytes(hw_params
));
115 static int snd_solo_hw_free(struct snd_pcm_substream
*ss
)
117 return snd_pcm_lib_free_pages(ss
);
120 static const struct snd_pcm_hardware snd_solo_pcm_hw
= {
121 .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
,
127 .rate_min
= SAMPLERATE
,
128 .rate_max
= SAMPLERATE
,
131 .buffer_bytes_max
= G723_PERIOD_BYTES
* PERIODS
,
132 .period_bytes_min
= G723_PERIOD_BYTES
,
133 .period_bytes_max
= G723_PERIOD_BYTES
,
134 .periods_min
= PERIODS
,
135 .periods_max
= PERIODS
,
138 static int snd_solo_pcm_open(struct snd_pcm_substream
*ss
)
140 struct solo_dev
*solo_dev
= snd_pcm_substream_chip(ss
);
141 struct solo_snd_pcm
*solo_pcm
;
143 solo_pcm
= kzalloc(sizeof(*solo_pcm
), GFP_KERNEL
);
144 if (solo_pcm
== NULL
)
147 solo_pcm
->g723_buf
= pci_alloc_consistent(solo_dev
->pdev
,
149 &solo_pcm
->g723_dma
);
150 if (solo_pcm
->g723_buf
== NULL
)
153 spin_lock_init(&solo_pcm
->lock
);
154 solo_pcm
->solo_dev
= solo_dev
;
155 ss
->runtime
->hw
= snd_solo_pcm_hw
;
157 snd_pcm_substream_chip(ss
) = solo_pcm
;
166 static int snd_solo_pcm_close(struct snd_pcm_substream
*ss
)
168 struct solo_snd_pcm
*solo_pcm
= snd_pcm_substream_chip(ss
);
170 snd_pcm_substream_chip(ss
) = solo_pcm
->solo_dev
;
171 pci_free_consistent(solo_pcm
->solo_dev
->pdev
, G723_PERIOD_BYTES
,
172 solo_pcm
->g723_buf
, solo_pcm
->g723_dma
);
178 static int snd_solo_pcm_trigger(struct snd_pcm_substream
*ss
, int cmd
)
180 struct solo_snd_pcm
*solo_pcm
= snd_pcm_substream_chip(ss
);
181 struct solo_dev
*solo_dev
= solo_pcm
->solo_dev
;
184 spin_lock(&solo_pcm
->lock
);
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)
191 solo_irq_on(solo_dev
, SOLO_IRQ_G723
);
195 case SNDRV_PCM_TRIGGER_STOP
:
197 /* If this was our last user, switch them off */
198 if (atomic_dec_return(&solo_dev
->snd_users
) == 0)
199 solo_irq_off(solo_dev
, SOLO_IRQ_G723
);
207 spin_unlock(&solo_pcm
->lock
);
212 static int snd_solo_pcm_prepare(struct snd_pcm_substream
*ss
)
217 static snd_pcm_uframes_t
snd_solo_pcm_pointer(struct snd_pcm_substream
*ss
)
219 struct solo_snd_pcm
*solo_pcm
= snd_pcm_substream_chip(ss
);
220 struct solo_dev
*solo_dev
= solo_pcm
->solo_dev
;
221 snd_pcm_uframes_t idx
= solo_reg_read(solo_dev
, SOLO_AUDIO_STA
) & 0x1f;
223 return idx
* G723_FRAMES_PER_PAGE
;
226 static int __snd_solo_pcm_copy(struct snd_pcm_substream
*ss
,
227 unsigned long pos
, void *dst
,
228 unsigned long count
, bool in_kernel
)
230 struct solo_snd_pcm
*solo_pcm
= snd_pcm_substream_chip(ss
);
231 struct solo_dev
*solo_dev
= solo_pcm
->solo_dev
;
234 for (i
= 0; i
< (count
/ G723_FRAMES_PER_PAGE
); i
++) {
235 int page
= (pos
/ G723_FRAMES_PER_PAGE
) + i
;
237 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);
246 memcpy(dst
, solo_pcm
->g723_buf
, G723_PERIOD_BYTES
);
247 else if (copy_to_user((void __user
*)dst
,
248 solo_pcm
->g723_buf
, G723_PERIOD_BYTES
))
250 dst
+= G723_PERIOD_BYTES
;
256 static int snd_solo_pcm_copy_user(struct snd_pcm_substream
*ss
, int channel
,
257 unsigned long pos
, void __user
*dst
,
260 return __snd_solo_pcm_copy(ss
, pos
, (void *)dst
, count
, false);
263 static int snd_solo_pcm_copy_kernel(struct snd_pcm_substream
*ss
, int channel
,
264 unsigned long pos
, void *dst
,
267 return __snd_solo_pcm_copy(ss
, pos
, dst
, count
, true);
270 static const struct snd_pcm_ops snd_solo_pcm_ops
= {
271 .open
= snd_solo_pcm_open
,
272 .close
= snd_solo_pcm_close
,
273 .ioctl
= snd_pcm_lib_ioctl
,
274 .hw_params
= snd_solo_hw_params
,
275 .hw_free
= snd_solo_hw_free
,
276 .prepare
= snd_solo_pcm_prepare
,
277 .trigger
= snd_solo_pcm_trigger
,
278 .pointer
= snd_solo_pcm_pointer
,
279 .copy_user
= snd_solo_pcm_copy_user
,
280 .copy_kernel
= snd_solo_pcm_copy_kernel
,
283 static int snd_solo_capture_volume_info(struct snd_kcontrol
*kcontrol
,
284 struct snd_ctl_elem_info
*info
)
286 info
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
288 info
->value
.integer
.min
= 0;
289 info
->value
.integer
.max
= 15;
290 info
->value
.integer
.step
= 1;
295 static int snd_solo_capture_volume_get(struct snd_kcontrol
*kcontrol
,
296 struct snd_ctl_elem_value
*value
)
298 struct solo_dev
*solo_dev
= snd_kcontrol_chip(kcontrol
);
299 u8 ch
= value
->id
.numid
- 1;
301 value
->value
.integer
.value
[0] = tw28_get_audio_gain(solo_dev
, ch
);
306 static int snd_solo_capture_volume_put(struct snd_kcontrol
*kcontrol
,
307 struct snd_ctl_elem_value
*value
)
309 struct solo_dev
*solo_dev
= snd_kcontrol_chip(kcontrol
);
310 u8 ch
= value
->id
.numid
- 1;
313 old_val
= tw28_get_audio_gain(solo_dev
, ch
);
314 if (old_val
== value
->value
.integer
.value
[0])
317 tw28_set_audio_gain(solo_dev
, ch
, value
->value
.integer
.value
[0]);
322 static const struct snd_kcontrol_new snd_solo_capture_volume
= {
323 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
324 .name
= "Capture Volume",
325 .info
= snd_solo_capture_volume_info
,
326 .get
= snd_solo_capture_volume_get
,
327 .put
= snd_solo_capture_volume_put
,
330 static int solo_snd_pcm_init(struct solo_dev
*solo_dev
)
332 struct snd_card
*card
= solo_dev
->snd_card
;
334 struct snd_pcm_substream
*ss
;
338 ret
= snd_pcm_new(card
, card
->driver
, 0, 0, solo_dev
->nr_chans
,
343 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
346 snd_pcm_chip(pcm
) = solo_dev
;
348 strcpy(pcm
->name
, card
->shortname
);
350 for (i
= 0, ss
= pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
;
351 ss
; ss
= ss
->next
, i
++)
352 sprintf(ss
->name
, "Camera #%d Audio", i
);
354 ret
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
355 SNDRV_DMA_TYPE_CONTINUOUS
,
356 snd_dma_continuous_data(GFP_KERNEL
),
357 G723_PERIOD_BYTES
* PERIODS
,
358 G723_PERIOD_BYTES
* PERIODS
);
362 solo_dev
->snd_pcm
= pcm
;
367 int solo_g723_init(struct solo_dev
*solo_dev
)
369 static struct snd_device_ops ops
= { };
370 struct snd_card
*card
;
371 struct snd_kcontrol_new kctl
;
375 atomic_set(&solo_dev
->snd_users
, 0);
377 /* Allows for easier mapping between video and audio */
378 sprintf(name
, "Softlogic%d", solo_dev
->vfd
->num
);
380 ret
= snd_card_new(&solo_dev
->pdev
->dev
,
381 SNDRV_DEFAULT_IDX1
, name
, THIS_MODULE
, 0,
382 &solo_dev
->snd_card
);
386 card
= solo_dev
->snd_card
;
388 strcpy(card
->driver
, SOLO6X10_NAME
);
389 strcpy(card
->shortname
, "SOLO-6x10 Audio");
390 sprintf(card
->longname
, "%s on %s IRQ %d", card
->shortname
,
391 pci_name(solo_dev
->pdev
), solo_dev
->pdev
->irq
);
393 ret
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, solo_dev
, &ops
);
398 strcpy(card
->mixername
, "SOLO-6x10");
399 kctl
= snd_solo_capture_volume
;
400 kctl
.count
= solo_dev
->nr_chans
;
402 ret
= snd_ctl_add(card
, snd_ctl_new1(&kctl
, solo_dev
));
406 ret
= solo_snd_pcm_init(solo_dev
);
410 ret
= snd_card_register(card
);
414 solo_g723_config(solo_dev
);
416 dev_info(&solo_dev
->pdev
->dev
, "Alsa sound card as %s\n", name
);
425 void solo_g723_exit(struct solo_dev
*solo_dev
)
427 if (!solo_dev
->snd_card
)
430 solo_reg_write(solo_dev
, SOLO_AUDIO_CONTROL
, 0);
431 solo_irq_off(solo_dev
, SOLO_IRQ_G723
);
433 snd_card_free(solo_dev
->snd_card
);
434 solo_dev
->snd_card
= NULL
;