1e4afd792SAlexander Motin /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4f81b451dSRuslan Bukin * Copyright (c) 2012-2021 Ruslan Bukin <br@bsdpad.com>
5d7fde2c9SFlorian Walpen * Copyright (c) 2023-2024 Florian Walpen <dev@submerge.ch>
6e4afd792SAlexander Motin * All rights reserved.
7e4afd792SAlexander Motin *
8e4afd792SAlexander Motin * Redistribution and use in source and binary forms, with or without
9e4afd792SAlexander Motin * modification, are permitted provided that the following conditions
10e4afd792SAlexander Motin * are met:
11e4afd792SAlexander Motin * 1. Redistributions of source code must retain the above copyright
12e4afd792SAlexander Motin * notice, this list of conditions and the following disclaimer.
13e4afd792SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright
14e4afd792SAlexander Motin * notice, this list of conditions and the following disclaimer in the
15e4afd792SAlexander Motin * documentation and/or other materials provided with the distribution.
16e4afd792SAlexander Motin *
17e4afd792SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18e4afd792SAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e4afd792SAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e4afd792SAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21e4afd792SAlexander Motin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e4afd792SAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e4afd792SAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e4afd792SAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e4afd792SAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e4afd792SAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e4afd792SAlexander Motin * SUCH DAMAGE.
28e4afd792SAlexander Motin */
29e4afd792SAlexander Motin
30e4afd792SAlexander Motin /*
31e4afd792SAlexander Motin * RME HDSPe driver for FreeBSD (pcm-part).
32e4afd792SAlexander Motin * Supported cards: AIO, RayDAT.
33e4afd792SAlexander Motin */
34e4afd792SAlexander Motin
35e4afd792SAlexander Motin #include <dev/sound/pcm/sound.h>
36e4afd792SAlexander Motin #include <dev/sound/pci/hdspe.h>
37e4afd792SAlexander Motin
38e4afd792SAlexander Motin #include <dev/pci/pcireg.h>
39e4afd792SAlexander Motin #include <dev/pci/pcivar.h>
40e4afd792SAlexander Motin
41e4afd792SAlexander Motin #include <mixer_if.h>
42e4afd792SAlexander Motin
43dc15f025SFlorian Walpen #define HDSPE_MATRIX_MAX 8
44dc15f025SFlorian Walpen
45e4afd792SAlexander Motin struct hdspe_latency {
46e4afd792SAlexander Motin uint32_t n;
47e4afd792SAlexander Motin uint32_t period;
48e4afd792SAlexander Motin float ms;
49e4afd792SAlexander Motin };
50e4afd792SAlexander Motin
51e4afd792SAlexander Motin static struct hdspe_latency latency_map[] = {
52e4afd792SAlexander Motin { 7, 32, 0.7 },
53e4afd792SAlexander Motin { 0, 64, 1.5 },
54e4afd792SAlexander Motin { 1, 128, 3 },
55e4afd792SAlexander Motin { 2, 256, 6 },
56e4afd792SAlexander Motin { 3, 512, 12 },
57e4afd792SAlexander Motin { 4, 1024, 23 },
58e4afd792SAlexander Motin { 5, 2048, 46 },
59e4afd792SAlexander Motin { 6, 4096, 93 },
60e4afd792SAlexander Motin
61e4afd792SAlexander Motin { 0, 0, 0 },
62e4afd792SAlexander Motin };
63e4afd792SAlexander Motin
64e4afd792SAlexander Motin struct hdspe_rate {
65e4afd792SAlexander Motin uint32_t speed;
66e4afd792SAlexander Motin uint32_t reg;
67e4afd792SAlexander Motin };
68e4afd792SAlexander Motin
69e4afd792SAlexander Motin static struct hdspe_rate rate_map[] = {
70e4afd792SAlexander Motin { 32000, (HDSPE_FREQ_32000) },
71e4afd792SAlexander Motin { 44100, (HDSPE_FREQ_44100) },
72e4afd792SAlexander Motin { 48000, (HDSPE_FREQ_48000) },
73e4afd792SAlexander Motin { 64000, (HDSPE_FREQ_32000 | HDSPE_FREQ_DOUBLE) },
74e4afd792SAlexander Motin { 88200, (HDSPE_FREQ_44100 | HDSPE_FREQ_DOUBLE) },
75e4afd792SAlexander Motin { 96000, (HDSPE_FREQ_48000 | HDSPE_FREQ_DOUBLE) },
76e4afd792SAlexander Motin { 128000, (HDSPE_FREQ_32000 | HDSPE_FREQ_QUAD) },
77e4afd792SAlexander Motin { 176400, (HDSPE_FREQ_44100 | HDSPE_FREQ_QUAD) },
78e4afd792SAlexander Motin { 192000, (HDSPE_FREQ_48000 | HDSPE_FREQ_QUAD) },
79e4afd792SAlexander Motin
80e4afd792SAlexander Motin { 0, 0 },
81e4afd792SAlexander Motin };
82e4afd792SAlexander Motin
83d7fde2c9SFlorian Walpen static uint32_t
hdspe_channel_play_ports(struct hdspe_channel * hc)84d7fde2c9SFlorian Walpen hdspe_channel_play_ports(struct hdspe_channel *hc)
85d7fde2c9SFlorian Walpen {
86d7fde2c9SFlorian Walpen return (hc->ports & (HDSPE_CHAN_AIO_ALL | HDSPE_CHAN_RAY_ALL));
87d7fde2c9SFlorian Walpen }
88d7fde2c9SFlorian Walpen
89d7fde2c9SFlorian Walpen static uint32_t
hdspe_channel_rec_ports(struct hdspe_channel * hc)90d7fde2c9SFlorian Walpen hdspe_channel_rec_ports(struct hdspe_channel *hc)
91d7fde2c9SFlorian Walpen {
92d7fde2c9SFlorian Walpen return (hc->ports & (HDSPE_CHAN_AIO_ALL_REC | HDSPE_CHAN_RAY_ALL));
93d7fde2c9SFlorian Walpen }
94d7fde2c9SFlorian Walpen
95d7fde2c9SFlorian Walpen static unsigned int
hdspe_adat_width(uint32_t speed)96d7fde2c9SFlorian Walpen hdspe_adat_width(uint32_t speed)
97d7fde2c9SFlorian Walpen {
98d7fde2c9SFlorian Walpen if (speed > 96000)
99d7fde2c9SFlorian Walpen return (2);
100d7fde2c9SFlorian Walpen if (speed > 48000)
101d7fde2c9SFlorian Walpen return (4);
102d7fde2c9SFlorian Walpen return (8);
103d7fde2c9SFlorian Walpen }
104d7fde2c9SFlorian Walpen
105d7fde2c9SFlorian Walpen static uint32_t
hdspe_port_first(uint32_t ports)106d7fde2c9SFlorian Walpen hdspe_port_first(uint32_t ports)
107d7fde2c9SFlorian Walpen {
108d7fde2c9SFlorian Walpen return (ports & (~(ports - 1))); /* Extract first bit set. */
109d7fde2c9SFlorian Walpen }
110d7fde2c9SFlorian Walpen
111d7fde2c9SFlorian Walpen static uint32_t
hdspe_port_first_row(uint32_t ports)112d7fde2c9SFlorian Walpen hdspe_port_first_row(uint32_t ports)
113d7fde2c9SFlorian Walpen {
114d7fde2c9SFlorian Walpen uint32_t ends;
115d7fde2c9SFlorian Walpen
116d7fde2c9SFlorian Walpen /* Restrict ports to one set with contiguous slots. */
1178fb46756SFlorian Walpen if (ports & HDSPE_CHAN_AIO_ALL)
1188fb46756SFlorian Walpen ports &= HDSPE_CHAN_AIO_ALL; /* All AIO slots. */
119d7fde2c9SFlorian Walpen else if (ports & HDSPE_CHAN_RAY_ALL)
120d7fde2c9SFlorian Walpen ports &= HDSPE_CHAN_RAY_ALL; /* All RayDAT slots. */
121d7fde2c9SFlorian Walpen
122d7fde2c9SFlorian Walpen /* Ends of port rows are followed by a port which is not in the set. */
123d7fde2c9SFlorian Walpen ends = ports & (~(ports >> 1));
124d7fde2c9SFlorian Walpen /* First row of contiguous ports ends in the first row end. */
125d7fde2c9SFlorian Walpen return (ports & (ends ^ (ends - 1)));
126d7fde2c9SFlorian Walpen }
127d7fde2c9SFlorian Walpen
128d7fde2c9SFlorian Walpen static unsigned int
hdspe_channel_count(uint32_t ports,uint32_t adat_width)129d7fde2c9SFlorian Walpen hdspe_channel_count(uint32_t ports, uint32_t adat_width)
130d7fde2c9SFlorian Walpen {
131d7fde2c9SFlorian Walpen unsigned int count = 0;
132d7fde2c9SFlorian Walpen
133d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_ALL) {
134d7fde2c9SFlorian Walpen /* AIO ports. */
135d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_LINE)
136d7fde2c9SFlorian Walpen count += 2;
1379e7e15b5SRuslan Bukin if (ports & HDSPE_CHAN_AIO_EXT)
1389e7e15b5SRuslan Bukin count += 4;
139d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_PHONE)
140d7fde2c9SFlorian Walpen count += 2;
141d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_AES)
142d7fde2c9SFlorian Walpen count += 2;
143d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_SPDIF)
144d7fde2c9SFlorian Walpen count += 2;
145d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_AIO_ADAT)
146d7fde2c9SFlorian Walpen count += adat_width;
147d7fde2c9SFlorian Walpen } else if (ports & HDSPE_CHAN_RAY_ALL) {
148d7fde2c9SFlorian Walpen /* RayDAT ports. */
149d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_AES)
150d7fde2c9SFlorian Walpen count += 2;
151d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_SPDIF)
152d7fde2c9SFlorian Walpen count += 2;
153d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_ADAT1)
154d7fde2c9SFlorian Walpen count += adat_width;
155d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_ADAT2)
156d7fde2c9SFlorian Walpen count += adat_width;
157d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_ADAT3)
158d7fde2c9SFlorian Walpen count += adat_width;
159d7fde2c9SFlorian Walpen if (ports & HDSPE_CHAN_RAY_ADAT4)
160d7fde2c9SFlorian Walpen count += adat_width;
161d7fde2c9SFlorian Walpen }
162d7fde2c9SFlorian Walpen
163d7fde2c9SFlorian Walpen return (count);
164d7fde2c9SFlorian Walpen }
165d7fde2c9SFlorian Walpen
166d7fde2c9SFlorian Walpen static unsigned int
hdspe_channel_offset(uint32_t subset,uint32_t ports,unsigned int adat_width)167d7fde2c9SFlorian Walpen hdspe_channel_offset(uint32_t subset, uint32_t ports, unsigned int adat_width)
168d7fde2c9SFlorian Walpen {
169d7fde2c9SFlorian Walpen uint32_t preceding;
170d7fde2c9SFlorian Walpen
171d7fde2c9SFlorian Walpen /* Make sure we have a subset of ports. */
172d7fde2c9SFlorian Walpen subset &= ports;
173d7fde2c9SFlorian Walpen /* Include all ports preceding the first one of the subset. */
174d7fde2c9SFlorian Walpen preceding = ports & (~subset & (subset - 1));
175d7fde2c9SFlorian Walpen
176d7fde2c9SFlorian Walpen if (preceding & HDSPE_CHAN_AIO_ALL)
177d7fde2c9SFlorian Walpen preceding &= HDSPE_CHAN_AIO_ALL; /* Contiguous AIO slots. */
178d7fde2c9SFlorian Walpen else if (preceding & HDSPE_CHAN_RAY_ALL)
179d7fde2c9SFlorian Walpen preceding &= HDSPE_CHAN_RAY_ALL; /* Contiguous RayDAT slots. */
180d7fde2c9SFlorian Walpen
181d7fde2c9SFlorian Walpen return (hdspe_channel_count(preceding, adat_width));
182d7fde2c9SFlorian Walpen }
183d7fde2c9SFlorian Walpen
184d7fde2c9SFlorian Walpen static unsigned int
hdspe_port_slot_offset(uint32_t port,unsigned int adat_width)185d7fde2c9SFlorian Walpen hdspe_port_slot_offset(uint32_t port, unsigned int adat_width)
186d7fde2c9SFlorian Walpen {
187d7fde2c9SFlorian Walpen /* Exctract the first port (lowest bit) if set of ports. */
188d7fde2c9SFlorian Walpen switch (hdspe_port_first(port)) {
189d7fde2c9SFlorian Walpen /* AIO ports */
190d7fde2c9SFlorian Walpen case HDSPE_CHAN_AIO_LINE:
191d7fde2c9SFlorian Walpen return (0);
1929e7e15b5SRuslan Bukin case HDSPE_CHAN_AIO_EXT:
1939e7e15b5SRuslan Bukin return (2);
194d7fde2c9SFlorian Walpen case HDSPE_CHAN_AIO_PHONE:
195d7fde2c9SFlorian Walpen return (6);
196d7fde2c9SFlorian Walpen case HDSPE_CHAN_AIO_AES:
197d7fde2c9SFlorian Walpen return (8);
198d7fde2c9SFlorian Walpen case HDSPE_CHAN_AIO_SPDIF:
199d7fde2c9SFlorian Walpen return (10);
200d7fde2c9SFlorian Walpen case HDSPE_CHAN_AIO_ADAT:
201d7fde2c9SFlorian Walpen return (12);
202d7fde2c9SFlorian Walpen
203d7fde2c9SFlorian Walpen /* RayDAT ports */
204d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_AES:
205d7fde2c9SFlorian Walpen return (0);
206d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_SPDIF:
207d7fde2c9SFlorian Walpen return (2);
208d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_ADAT1:
209d7fde2c9SFlorian Walpen return (4);
210d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_ADAT2:
211d7fde2c9SFlorian Walpen return (4 + adat_width);
212d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_ADAT3:
213d7fde2c9SFlorian Walpen return (4 + 2 * adat_width);
214d7fde2c9SFlorian Walpen case HDSPE_CHAN_RAY_ADAT4:
215d7fde2c9SFlorian Walpen return (4 + 3 * adat_width);
216d7fde2c9SFlorian Walpen default:
217d7fde2c9SFlorian Walpen return (0);
218d7fde2c9SFlorian Walpen }
219d7fde2c9SFlorian Walpen }
220d7fde2c9SFlorian Walpen
221d7fde2c9SFlorian Walpen static unsigned int
hdspe_port_slot_width(uint32_t ports,unsigned int adat_width)222d7fde2c9SFlorian Walpen hdspe_port_slot_width(uint32_t ports, unsigned int adat_width)
223d7fde2c9SFlorian Walpen {
224d7fde2c9SFlorian Walpen uint32_t row;
225d7fde2c9SFlorian Walpen
226d7fde2c9SFlorian Walpen /* Count number of contiguous slots from the first physical port. */
227d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
228d7fde2c9SFlorian Walpen return (hdspe_channel_count(row, adat_width));
229d7fde2c9SFlorian Walpen }
230d7fde2c9SFlorian Walpen
231e4afd792SAlexander Motin static int
hdspe_hw_mixer(struct sc_chinfo * ch,unsigned int dst,unsigned int src,unsigned short data)232e4afd792SAlexander Motin hdspe_hw_mixer(struct sc_chinfo *ch, unsigned int dst,
233e4afd792SAlexander Motin unsigned int src, unsigned short data)
234e4afd792SAlexander Motin {
23520a9f771SRuslan Bukin struct sc_pcminfo *scp;
23620a9f771SRuslan Bukin struct sc_info *sc;
23720a9f771SRuslan Bukin int offs;
238e4afd792SAlexander Motin
23920a9f771SRuslan Bukin scp = ch->parent;
24020a9f771SRuslan Bukin sc = scp->sc;
24120a9f771SRuslan Bukin
24220a9f771SRuslan Bukin offs = 0;
243e4afd792SAlexander Motin if (ch->dir == PCMDIR_PLAY)
244e4afd792SAlexander Motin offs = 64;
245e4afd792SAlexander Motin
246e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_MIXER_BASE +
247e4afd792SAlexander Motin ((offs + src + 128 * dst) * sizeof(uint32_t)),
248e4afd792SAlexander Motin data & 0xFFFF);
249e4afd792SAlexander Motin
25020a9f771SRuslan Bukin return (0);
251e4afd792SAlexander Motin };
252e4afd792SAlexander Motin
253e4afd792SAlexander Motin static int
hdspechan_setgain(struct sc_chinfo * ch)254e4afd792SAlexander Motin hdspechan_setgain(struct sc_chinfo *ch)
255e4afd792SAlexander Motin {
256d7fde2c9SFlorian Walpen struct sc_info *sc;
257d7fde2c9SFlorian Walpen uint32_t port, ports;
258d7fde2c9SFlorian Walpen unsigned int slot, end_slot;
259d7fde2c9SFlorian Walpen unsigned short volume;
260e4afd792SAlexander Motin
261d7fde2c9SFlorian Walpen sc = ch->parent->sc;
262d7fde2c9SFlorian Walpen
263d7fde2c9SFlorian Walpen /* Iterate through all physical ports of the channel. */
264d7fde2c9SFlorian Walpen ports = ch->ports;
265d7fde2c9SFlorian Walpen port = hdspe_port_first(ports);
266d7fde2c9SFlorian Walpen while (port != 0) {
267d7fde2c9SFlorian Walpen /* Get slot range of the physical port. */
268d7fde2c9SFlorian Walpen slot =
269d7fde2c9SFlorian Walpen hdspe_port_slot_offset(port, hdspe_adat_width(sc->speed));
270d7fde2c9SFlorian Walpen end_slot = slot +
271d7fde2c9SFlorian Walpen hdspe_port_slot_width(port, hdspe_adat_width(sc->speed));
272d7fde2c9SFlorian Walpen
273d7fde2c9SFlorian Walpen /* Treat first slot as left channel. */
274d7fde2c9SFlorian Walpen volume = ch->lvol * HDSPE_MAX_GAIN / 100;
275d7fde2c9SFlorian Walpen for (; slot < end_slot; slot++) {
276d7fde2c9SFlorian Walpen hdspe_hw_mixer(ch, slot, slot, volume);
277d7fde2c9SFlorian Walpen /* Subsequent slots all get the right channel volume. */
278d7fde2c9SFlorian Walpen volume = ch->rvol * HDSPE_MAX_GAIN / 100;
279d7fde2c9SFlorian Walpen }
280d7fde2c9SFlorian Walpen
281d7fde2c9SFlorian Walpen ports &= ~port;
282d7fde2c9SFlorian Walpen port = hdspe_port_first(ports);
283d7fde2c9SFlorian Walpen }
284e4afd792SAlexander Motin
28520a9f771SRuslan Bukin return (0);
286e4afd792SAlexander Motin }
287e4afd792SAlexander Motin
288e4afd792SAlexander Motin static int
hdspemixer_init(struct snd_mixer * m)289e4afd792SAlexander Motin hdspemixer_init(struct snd_mixer *m)
290e4afd792SAlexander Motin {
29120a9f771SRuslan Bukin struct sc_pcminfo *scp;
29220a9f771SRuslan Bukin struct sc_info *sc;
293e4afd792SAlexander Motin int mask;
294e4afd792SAlexander Motin
29520a9f771SRuslan Bukin scp = mix_getdevinfo(m);
29620a9f771SRuslan Bukin sc = scp->sc;
297e4afd792SAlexander Motin if (sc == NULL)
29820a9f771SRuslan Bukin return (-1);
299e4afd792SAlexander Motin
300e4afd792SAlexander Motin mask = SOUND_MASK_PCM;
301e4afd792SAlexander Motin
302d7fde2c9SFlorian Walpen if (hdspe_channel_play_ports(scp->hc))
303e4afd792SAlexander Motin mask |= SOUND_MASK_VOLUME;
304e4afd792SAlexander Motin
305d7fde2c9SFlorian Walpen if (hdspe_channel_rec_ports(scp->hc))
306e4afd792SAlexander Motin mask |= SOUND_MASK_RECLEV;
307e4afd792SAlexander Motin
308e4afd792SAlexander Motin snd_mtxlock(sc->lock);
309e4afd792SAlexander Motin pcm_setflags(scp->dev, pcm_getflags(scp->dev) | SD_F_SOFTPCMVOL);
310e4afd792SAlexander Motin mix_setdevs(m, mask);
311e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
312e4afd792SAlexander Motin
31320a9f771SRuslan Bukin return (0);
314e4afd792SAlexander Motin }
315e4afd792SAlexander Motin
316e4afd792SAlexander Motin static int
hdspemixer_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)317e4afd792SAlexander Motin hdspemixer_set(struct snd_mixer *m, unsigned dev,
318e4afd792SAlexander Motin unsigned left, unsigned right)
319e4afd792SAlexander Motin {
32020a9f771SRuslan Bukin struct sc_pcminfo *scp;
321e4afd792SAlexander Motin struct sc_chinfo *ch;
322e4afd792SAlexander Motin int i;
323e4afd792SAlexander Motin
32420a9f771SRuslan Bukin scp = mix_getdevinfo(m);
32520a9f771SRuslan Bukin
326e4afd792SAlexander Motin #if 0
327e4afd792SAlexander Motin device_printf(scp->dev, "hdspemixer_set() %d %d\n",
328e4afd792SAlexander Motin left, right);
329e4afd792SAlexander Motin #endif
330e4afd792SAlexander Motin
331e4afd792SAlexander Motin for (i = 0; i < scp->chnum; i++) {
332e4afd792SAlexander Motin ch = &scp->chan[i];
333e4afd792SAlexander Motin if ((dev == SOUND_MIXER_VOLUME && ch->dir == PCMDIR_PLAY) ||
334e4afd792SAlexander Motin (dev == SOUND_MIXER_RECLEV && ch->dir == PCMDIR_REC)) {
335e4afd792SAlexander Motin ch->lvol = left;
336e4afd792SAlexander Motin ch->rvol = right;
337e4afd792SAlexander Motin if (ch->run)
338e4afd792SAlexander Motin hdspechan_setgain(ch);
339e4afd792SAlexander Motin }
340e4afd792SAlexander Motin }
341e4afd792SAlexander Motin
34220a9f771SRuslan Bukin return (0);
343e4afd792SAlexander Motin }
344e4afd792SAlexander Motin
345e4afd792SAlexander Motin static kobj_method_t hdspemixer_methods[] = {
346e4afd792SAlexander Motin KOBJMETHOD(mixer_init, hdspemixer_init),
347e4afd792SAlexander Motin KOBJMETHOD(mixer_set, hdspemixer_set),
348e4afd792SAlexander Motin KOBJMETHOD_END
349e4afd792SAlexander Motin };
350e4afd792SAlexander Motin MIXER_DECLARE(hdspemixer);
351e4afd792SAlexander Motin
352e4afd792SAlexander Motin static void
hdspechan_enable(struct sc_chinfo * ch,int value)353e4afd792SAlexander Motin hdspechan_enable(struct sc_chinfo *ch, int value)
354e4afd792SAlexander Motin {
35520a9f771SRuslan Bukin struct sc_pcminfo *scp;
35620a9f771SRuslan Bukin struct sc_info *sc;
357d7fde2c9SFlorian Walpen uint32_t row, ports;
358e4afd792SAlexander Motin int reg;
359d7fde2c9SFlorian Walpen unsigned int slot, end_slot;
360e4afd792SAlexander Motin
36120a9f771SRuslan Bukin scp = ch->parent;
36220a9f771SRuslan Bukin sc = scp->sc;
36320a9f771SRuslan Bukin
364e4afd792SAlexander Motin if (ch->dir == PCMDIR_PLAY)
365e4afd792SAlexander Motin reg = HDSPE_OUT_ENABLE_BASE;
366e4afd792SAlexander Motin else
367e4afd792SAlexander Motin reg = HDSPE_IN_ENABLE_BASE;
368e4afd792SAlexander Motin
369e4afd792SAlexander Motin ch->run = value;
370e4afd792SAlexander Motin
371d7fde2c9SFlorian Walpen /* Iterate through rows of ports with contiguous slots. */
372d7fde2c9SFlorian Walpen ports = ch->ports;
373d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
374d7fde2c9SFlorian Walpen while (row != 0) {
375d7fde2c9SFlorian Walpen slot =
376d7fde2c9SFlorian Walpen hdspe_port_slot_offset(row, hdspe_adat_width(sc->speed));
377d7fde2c9SFlorian Walpen end_slot = slot +
378d7fde2c9SFlorian Walpen hdspe_port_slot_width(row, hdspe_adat_width(sc->speed));
379d7fde2c9SFlorian Walpen
380d7fde2c9SFlorian Walpen for (; slot < end_slot; slot++) {
381d7fde2c9SFlorian Walpen hdspe_write_1(sc, reg + (4 * slot), value);
382d7fde2c9SFlorian Walpen }
383d7fde2c9SFlorian Walpen
384d7fde2c9SFlorian Walpen ports &= ~row;
385d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
386d7fde2c9SFlorian Walpen }
387e4afd792SAlexander Motin }
388e4afd792SAlexander Motin
389e4afd792SAlexander Motin static int
hdspe_running(struct sc_info * sc)390e4afd792SAlexander Motin hdspe_running(struct sc_info *sc)
391e4afd792SAlexander Motin {
392e4afd792SAlexander Motin struct sc_pcminfo *scp;
393e4afd792SAlexander Motin struct sc_chinfo *ch;
394e4afd792SAlexander Motin device_t *devlist;
39520a9f771SRuslan Bukin int devcount;
39620a9f771SRuslan Bukin int i, j;
39720a9f771SRuslan Bukin int err;
398e4afd792SAlexander Motin
399e4afd792SAlexander Motin if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
400e4afd792SAlexander Motin goto bad;
401e4afd792SAlexander Motin
402e4afd792SAlexander Motin for (i = 0; i < devcount; i++) {
403e4afd792SAlexander Motin scp = device_get_ivars(devlist[i]);
404e4afd792SAlexander Motin for (j = 0; j < scp->chnum; j++) {
405e4afd792SAlexander Motin ch = &scp->chan[j];
406e4afd792SAlexander Motin if (ch->run)
407e4afd792SAlexander Motin goto bad;
408e4afd792SAlexander Motin }
409e4afd792SAlexander Motin }
410e4afd792SAlexander Motin
411b5db12bfSKevin Lo free(devlist, M_TEMP);
41220a9f771SRuslan Bukin
41320a9f771SRuslan Bukin return (0);
414e4afd792SAlexander Motin bad:
415e4afd792SAlexander Motin
416e4afd792SAlexander Motin #if 0
417e4afd792SAlexander Motin device_printf(sc->dev, "hdspe is running\n");
418e4afd792SAlexander Motin #endif
419e4afd792SAlexander Motin
420b5db12bfSKevin Lo free(devlist, M_TEMP);
42120a9f771SRuslan Bukin
42220a9f771SRuslan Bukin return (1);
423e4afd792SAlexander Motin }
424e4afd792SAlexander Motin
425e4afd792SAlexander Motin static void
hdspe_start_audio(struct sc_info * sc)426e4afd792SAlexander Motin hdspe_start_audio(struct sc_info *sc)
427e4afd792SAlexander Motin {
428e4afd792SAlexander Motin
429e4afd792SAlexander Motin sc->ctrl_register |= (HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
430e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
431e4afd792SAlexander Motin }
432e4afd792SAlexander Motin
433e4afd792SAlexander Motin static void
hdspe_stop_audio(struct sc_info * sc)434e4afd792SAlexander Motin hdspe_stop_audio(struct sc_info *sc)
435e4afd792SAlexander Motin {
436e4afd792SAlexander Motin
437e4afd792SAlexander Motin if (hdspe_running(sc) == 1)
438e4afd792SAlexander Motin return;
439e4afd792SAlexander Motin
440e4afd792SAlexander Motin sc->ctrl_register &= ~(HDSPE_AUDIO_INT_ENABLE | HDSPE_ENABLE);
441e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
442e4afd792SAlexander Motin }
443e4afd792SAlexander Motin
444d7fde2c9SFlorian Walpen static void
buffer_mux_write(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int samples,unsigned int slots,unsigned int channels)445d7fde2c9SFlorian Walpen buffer_mux_write(uint32_t *dma, uint32_t *pcm, unsigned int pos,
446d7fde2c9SFlorian Walpen unsigned int samples, unsigned int slots, unsigned int channels)
447d7fde2c9SFlorian Walpen {
448d7fde2c9SFlorian Walpen int slot;
449d7fde2c9SFlorian Walpen
450d7fde2c9SFlorian Walpen for (; samples > 0; samples--) {
451d7fde2c9SFlorian Walpen for (slot = 0; slot < slots; slot++) {
452d7fde2c9SFlorian Walpen dma[slot * HDSPE_CHANBUF_SAMPLES + pos] =
453d7fde2c9SFlorian Walpen pcm[pos * channels + slot];
454d7fde2c9SFlorian Walpen }
455d7fde2c9SFlorian Walpen pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES;
456d7fde2c9SFlorian Walpen }
457d7fde2c9SFlorian Walpen }
458d7fde2c9SFlorian Walpen
459d7fde2c9SFlorian Walpen static void
buffer_mux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t ports,unsigned int pos,unsigned int samples,unsigned int adat_width,unsigned int pcm_width)460d7fde2c9SFlorian Walpen buffer_mux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports,
461d7fde2c9SFlorian Walpen unsigned int pos, unsigned int samples, unsigned int adat_width,
462d7fde2c9SFlorian Walpen unsigned int pcm_width)
463d7fde2c9SFlorian Walpen {
464d7fde2c9SFlorian Walpen unsigned int slot_offset, slots;
465d7fde2c9SFlorian Walpen unsigned int channels, chan_pos;
466d7fde2c9SFlorian Walpen
467d7fde2c9SFlorian Walpen /* Translate DMA slot offset to DMA buffer offset. */
468d7fde2c9SFlorian Walpen slot_offset = hdspe_port_slot_offset(subset, adat_width);
469d7fde2c9SFlorian Walpen dma += slot_offset * HDSPE_CHANBUF_SAMPLES;
470d7fde2c9SFlorian Walpen
471d7fde2c9SFlorian Walpen /* Channel position of the port subset and total number of channels. */
472d7fde2c9SFlorian Walpen chan_pos = hdspe_channel_offset(subset, ports, pcm_width);
473d7fde2c9SFlorian Walpen pcm += chan_pos;
474d7fde2c9SFlorian Walpen channels = hdspe_channel_count(ports, pcm_width);
475d7fde2c9SFlorian Walpen
476d7fde2c9SFlorian Walpen /* Only copy as much as supported by both hardware and pcm channel. */
477d7fde2c9SFlorian Walpen slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width));
478d7fde2c9SFlorian Walpen
479d7fde2c9SFlorian Walpen /* Let the compiler inline and loop unroll common cases. */
480d7fde2c9SFlorian Walpen if (slots == 2)
481d7fde2c9SFlorian Walpen buffer_mux_write(dma, pcm, pos, samples, 2, channels);
482d7fde2c9SFlorian Walpen else if (slots == 4)
483d7fde2c9SFlorian Walpen buffer_mux_write(dma, pcm, pos, samples, 4, channels);
484d7fde2c9SFlorian Walpen else if (slots == 8)
485d7fde2c9SFlorian Walpen buffer_mux_write(dma, pcm, pos, samples, 8, channels);
486d7fde2c9SFlorian Walpen else
487d7fde2c9SFlorian Walpen buffer_mux_write(dma, pcm, pos, samples, slots, channels);
488d7fde2c9SFlorian Walpen }
489d7fde2c9SFlorian Walpen
490d7fde2c9SFlorian Walpen static void
buffer_demux_read(uint32_t * dma,uint32_t * pcm,unsigned int pos,unsigned int samples,unsigned int slots,unsigned int channels)491d7fde2c9SFlorian Walpen buffer_demux_read(uint32_t *dma, uint32_t *pcm, unsigned int pos,
492d7fde2c9SFlorian Walpen unsigned int samples, unsigned int slots, unsigned int channels)
493d7fde2c9SFlorian Walpen {
494d7fde2c9SFlorian Walpen int slot;
495d7fde2c9SFlorian Walpen
496d7fde2c9SFlorian Walpen for (; samples > 0; samples--) {
497d7fde2c9SFlorian Walpen for (slot = 0; slot < slots; slot++) {
498d7fde2c9SFlorian Walpen pcm[pos * channels + slot] =
499d7fde2c9SFlorian Walpen dma[slot * HDSPE_CHANBUF_SAMPLES + pos];
500d7fde2c9SFlorian Walpen }
501d7fde2c9SFlorian Walpen pos = (pos + 1) % HDSPE_CHANBUF_SAMPLES;
502d7fde2c9SFlorian Walpen }
503d7fde2c9SFlorian Walpen }
504d7fde2c9SFlorian Walpen
505d7fde2c9SFlorian Walpen static void
buffer_demux_port(uint32_t * dma,uint32_t * pcm,uint32_t subset,uint32_t ports,unsigned int pos,unsigned int samples,unsigned int adat_width,unsigned int pcm_width)506d7fde2c9SFlorian Walpen buffer_demux_port(uint32_t *dma, uint32_t *pcm, uint32_t subset, uint32_t ports,
507d7fde2c9SFlorian Walpen unsigned int pos, unsigned int samples, unsigned int adat_width,
508d7fde2c9SFlorian Walpen unsigned int pcm_width)
509d7fde2c9SFlorian Walpen {
510d7fde2c9SFlorian Walpen unsigned int slot_offset, slots;
511d7fde2c9SFlorian Walpen unsigned int channels, chan_pos;
512d7fde2c9SFlorian Walpen
513d7fde2c9SFlorian Walpen /* Translate port slot offset to DMA buffer offset. */
514d7fde2c9SFlorian Walpen slot_offset = hdspe_port_slot_offset(subset, adat_width);
515d7fde2c9SFlorian Walpen dma += slot_offset * HDSPE_CHANBUF_SAMPLES;
516d7fde2c9SFlorian Walpen
517d7fde2c9SFlorian Walpen /* Channel position of the port subset and total number of channels. */
518d7fde2c9SFlorian Walpen chan_pos = hdspe_channel_offset(subset, ports, pcm_width);
519d7fde2c9SFlorian Walpen pcm += chan_pos;
520d7fde2c9SFlorian Walpen channels = hdspe_channel_count(ports, pcm_width);
521d7fde2c9SFlorian Walpen
522d7fde2c9SFlorian Walpen /* Only copy as much as supported by both hardware and pcm channel. */
523d7fde2c9SFlorian Walpen slots = hdspe_port_slot_width(subset, MIN(adat_width, pcm_width));
524d7fde2c9SFlorian Walpen
525d7fde2c9SFlorian Walpen /* Let the compiler inline and loop unroll common cases. */
526d7fde2c9SFlorian Walpen if (slots == 2)
527d7fde2c9SFlorian Walpen buffer_demux_read(dma, pcm, pos, samples, 2, channels);
528d7fde2c9SFlorian Walpen else if (slots == 4)
529d7fde2c9SFlorian Walpen buffer_demux_read(dma, pcm, pos, samples, 4, channels);
530d7fde2c9SFlorian Walpen else if (slots == 8)
531d7fde2c9SFlorian Walpen buffer_demux_read(dma, pcm, pos, samples, 8, channels);
532d7fde2c9SFlorian Walpen else
533d7fde2c9SFlorian Walpen buffer_demux_read(dma, pcm, pos, samples, slots, channels);
534d7fde2c9SFlorian Walpen }
535d7fde2c9SFlorian Walpen
536d7fde2c9SFlorian Walpen
537d7fde2c9SFlorian Walpen /* Copy data between DMA and PCM buffers. */
538e4afd792SAlexander Motin static void
buffer_copy(struct sc_chinfo * ch)539e4afd792SAlexander Motin buffer_copy(struct sc_chinfo *ch)
540e4afd792SAlexander Motin {
54120a9f771SRuslan Bukin struct sc_pcminfo *scp;
54220a9f771SRuslan Bukin struct sc_info *sc;
543d7fde2c9SFlorian Walpen uint32_t row, ports;
544ee36e7faSFlorian Walpen uint32_t dma_pos;
545ee36e7faSFlorian Walpen unsigned int pos, length, offset;
546d7fde2c9SFlorian Walpen unsigned int n;
547d7fde2c9SFlorian Walpen unsigned int adat_width, pcm_width;
548e4afd792SAlexander Motin
54920a9f771SRuslan Bukin scp = ch->parent;
55020a9f771SRuslan Bukin sc = scp->sc;
55120a9f771SRuslan Bukin
552f81b451dSRuslan Bukin n = AFMT_CHANNEL(ch->format); /* n channels */
553f81b451dSRuslan Bukin
554d7fde2c9SFlorian Walpen /* Let pcm formats differ from current hardware ADAT width. */
555d7fde2c9SFlorian Walpen adat_width = hdspe_adat_width(sc->speed);
556d7fde2c9SFlorian Walpen if (n == hdspe_channel_count(ch->ports, 2))
557d7fde2c9SFlorian Walpen pcm_width = 2;
558d7fde2c9SFlorian Walpen else if (n == hdspe_channel_count(ch->ports, 4))
559d7fde2c9SFlorian Walpen pcm_width = 4;
560d7fde2c9SFlorian Walpen else
561d7fde2c9SFlorian Walpen pcm_width = 8;
562e4afd792SAlexander Motin
563ee36e7faSFlorian Walpen /* Derive buffer position and length to be copied. */
564ee36e7faSFlorian Walpen if (ch->dir == PCMDIR_PLAY) {
565ee36e7faSFlorian Walpen /* Position per channel is n times smaller than PCM. */
566ee36e7faSFlorian Walpen pos = sndbuf_getreadyptr(ch->buffer) / n;
567ee36e7faSFlorian Walpen length = sndbuf_getready(ch->buffer) / n;
568ee36e7faSFlorian Walpen /* Copy no more than 2 periods in advance. */
569ee36e7faSFlorian Walpen if (length > (sc->period * 4 * 2))
570ee36e7faSFlorian Walpen length = (sc->period * 4 * 2);
571ee36e7faSFlorian Walpen /* Skip what was already copied last time. */
572ee36e7faSFlorian Walpen offset = (ch->position + HDSPE_CHANBUF_SIZE) - pos;
573ee36e7faSFlorian Walpen offset %= HDSPE_CHANBUF_SIZE;
574ee36e7faSFlorian Walpen if (offset <= length) {
575ee36e7faSFlorian Walpen pos = (pos + offset) % HDSPE_CHANBUF_SIZE;
576ee36e7faSFlorian Walpen length -= offset;
577ee36e7faSFlorian Walpen }
578ee36e7faSFlorian Walpen } else {
579ee36e7faSFlorian Walpen /* Position per channel is n times smaller than PCM. */
580ee36e7faSFlorian Walpen pos = sndbuf_getfreeptr(ch->buffer) / n;
581ee36e7faSFlorian Walpen /* Get DMA buffer write position. */
582ee36e7faSFlorian Walpen dma_pos = hdspe_read_2(sc, HDSPE_STATUS_REG);
583ee36e7faSFlorian Walpen dma_pos &= HDSPE_BUF_POSITION_MASK;
584ee36e7faSFlorian Walpen /* Copy what is newly available. */
585ee36e7faSFlorian Walpen length = (dma_pos + HDSPE_CHANBUF_SIZE) - pos;
586ee36e7faSFlorian Walpen length %= HDSPE_CHANBUF_SIZE;
587ee36e7faSFlorian Walpen }
588e4afd792SAlexander Motin
589ee36e7faSFlorian Walpen /* Position and length in samples (4 bytes). */
590ee36e7faSFlorian Walpen pos /= 4;
591ee36e7faSFlorian Walpen length /= 4;
592e4afd792SAlexander Motin
593d7fde2c9SFlorian Walpen /* Iterate through rows of ports with contiguous slots. */
594d7fde2c9SFlorian Walpen ports = ch->ports;
595d7fde2c9SFlorian Walpen if (pcm_width == adat_width)
596d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
597d7fde2c9SFlorian Walpen else
598d7fde2c9SFlorian Walpen row = hdspe_port_first(ports);
599e4afd792SAlexander Motin
600d7fde2c9SFlorian Walpen while (row != 0) {
601d7fde2c9SFlorian Walpen if (ch->dir == PCMDIR_PLAY)
602d7fde2c9SFlorian Walpen buffer_mux_port(sc->pbuf, ch->data, row, ch->ports, pos,
603ee36e7faSFlorian Walpen length, adat_width, pcm_width);
604d7fde2c9SFlorian Walpen else
605d7fde2c9SFlorian Walpen buffer_demux_port(sc->rbuf, ch->data, row, ch->ports,
606ee36e7faSFlorian Walpen pos, length, adat_width, pcm_width);
607e4afd792SAlexander Motin
608d7fde2c9SFlorian Walpen ports &= ~row;
609d7fde2c9SFlorian Walpen if (pcm_width == adat_width)
610d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
611d7fde2c9SFlorian Walpen else
612d7fde2c9SFlorian Walpen row = hdspe_port_first(ports);
613e4afd792SAlexander Motin }
614ee36e7faSFlorian Walpen
615ee36e7faSFlorian Walpen ch->position = ((pos + length) * 4) % HDSPE_CHANBUF_SIZE;
616e4afd792SAlexander Motin }
617e4afd792SAlexander Motin
618e4afd792SAlexander Motin static int
clean(struct sc_chinfo * ch)61920a9f771SRuslan Bukin clean(struct sc_chinfo *ch)
62020a9f771SRuslan Bukin {
62120a9f771SRuslan Bukin struct sc_pcminfo *scp;
62220a9f771SRuslan Bukin struct sc_info *sc;
62320a9f771SRuslan Bukin uint32_t *buf;
624d7fde2c9SFlorian Walpen uint32_t row, ports;
625d7fde2c9SFlorian Walpen unsigned int offset, slots;
62620a9f771SRuslan Bukin
62720a9f771SRuslan Bukin scp = ch->parent;
62820a9f771SRuslan Bukin sc = scp->sc;
62920a9f771SRuslan Bukin buf = sc->rbuf;
630e4afd792SAlexander Motin
631d7fde2c9SFlorian Walpen if (ch->dir == PCMDIR_PLAY)
632e4afd792SAlexander Motin buf = sc->pbuf;
633e4afd792SAlexander Motin
634d7fde2c9SFlorian Walpen /* Iterate through rows of ports with contiguous slots. */
635d7fde2c9SFlorian Walpen ports = ch->ports;
636d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
637d7fde2c9SFlorian Walpen while (row != 0) {
638d7fde2c9SFlorian Walpen offset = hdspe_port_slot_offset(row,
639d7fde2c9SFlorian Walpen hdspe_adat_width(sc->speed));
640d7fde2c9SFlorian Walpen slots = hdspe_port_slot_width(row, hdspe_adat_width(sc->speed));
641d7fde2c9SFlorian Walpen
642d7fde2c9SFlorian Walpen bzero(buf + offset * HDSPE_CHANBUF_SAMPLES,
643d7fde2c9SFlorian Walpen slots * HDSPE_CHANBUF_SIZE);
644d7fde2c9SFlorian Walpen
645d7fde2c9SFlorian Walpen ports &= ~row;
646d7fde2c9SFlorian Walpen row = hdspe_port_first_row(ports);
647d7fde2c9SFlorian Walpen }
648e4afd792SAlexander Motin
649ee36e7faSFlorian Walpen ch->position = 0;
650ee36e7faSFlorian Walpen
65120a9f771SRuslan Bukin return (0);
652e4afd792SAlexander Motin }
653e4afd792SAlexander Motin
654e4afd792SAlexander Motin /* Channel interface. */
65585d1c84cSChristos Margiolis static int
hdspechan_free(kobj_t obj,void * data)65685d1c84cSChristos Margiolis hdspechan_free(kobj_t obj, void *data)
65785d1c84cSChristos Margiolis {
65885d1c84cSChristos Margiolis struct sc_pcminfo *scp;
65985d1c84cSChristos Margiolis struct sc_chinfo *ch;
66085d1c84cSChristos Margiolis struct sc_info *sc;
66185d1c84cSChristos Margiolis
66285d1c84cSChristos Margiolis ch = data;
66385d1c84cSChristos Margiolis scp = ch->parent;
66485d1c84cSChristos Margiolis sc = scp->sc;
66585d1c84cSChristos Margiolis
66685d1c84cSChristos Margiolis #if 0
66785d1c84cSChristos Margiolis device_printf(scp->dev, "hdspechan_free()\n");
66885d1c84cSChristos Margiolis #endif
66985d1c84cSChristos Margiolis
67085d1c84cSChristos Margiolis snd_mtxlock(sc->lock);
67185d1c84cSChristos Margiolis if (ch->data != NULL) {
67285d1c84cSChristos Margiolis free(ch->data, M_HDSPE);
67385d1c84cSChristos Margiolis ch->data = NULL;
67485d1c84cSChristos Margiolis }
67585d1c84cSChristos Margiolis if (ch->caps != NULL) {
67685d1c84cSChristos Margiolis free(ch->caps, M_HDSPE);
67785d1c84cSChristos Margiolis ch->caps = NULL;
67885d1c84cSChristos Margiolis }
67985d1c84cSChristos Margiolis snd_mtxunlock(sc->lock);
68085d1c84cSChristos Margiolis
68185d1c84cSChristos Margiolis return (0);
68285d1c84cSChristos Margiolis }
68385d1c84cSChristos Margiolis
684e4afd792SAlexander Motin static void *
hdspechan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)685e4afd792SAlexander Motin hdspechan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
686e4afd792SAlexander Motin struct pcm_channel *c, int dir)
687e4afd792SAlexander Motin {
68820a9f771SRuslan Bukin struct sc_pcminfo *scp;
689e4afd792SAlexander Motin struct sc_chinfo *ch;
69020a9f771SRuslan Bukin struct sc_info *sc;
691e4afd792SAlexander Motin int num;
692e4afd792SAlexander Motin
69320a9f771SRuslan Bukin scp = devinfo;
69420a9f771SRuslan Bukin sc = scp->sc;
69520a9f771SRuslan Bukin
696e4afd792SAlexander Motin snd_mtxlock(sc->lock);
697e4afd792SAlexander Motin num = scp->chnum;
698e4afd792SAlexander Motin
699e4afd792SAlexander Motin ch = &scp->chan[num];
700d7fde2c9SFlorian Walpen
701d7fde2c9SFlorian Walpen if (dir == PCMDIR_PLAY)
702d7fde2c9SFlorian Walpen ch->ports = hdspe_channel_play_ports(scp->hc);
703d7fde2c9SFlorian Walpen else
704d7fde2c9SFlorian Walpen ch->ports = hdspe_channel_rec_ports(scp->hc);
705d7fde2c9SFlorian Walpen
706e4afd792SAlexander Motin ch->run = 0;
707e4afd792SAlexander Motin ch->lvol = 0;
708e4afd792SAlexander Motin ch->rvol = 0;
709e4afd792SAlexander Motin
710d7fde2c9SFlorian Walpen /* Support all possible ADAT widths as channel formats. */
711d7fde2c9SFlorian Walpen ch->cap_fmts[0] =
712d7fde2c9SFlorian Walpen SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 2), 0);
713d7fde2c9SFlorian Walpen ch->cap_fmts[1] =
714d7fde2c9SFlorian Walpen SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 4), 0);
715d7fde2c9SFlorian Walpen ch->cap_fmts[2] =
716d7fde2c9SFlorian Walpen SND_FORMAT(AFMT_S32_LE, hdspe_channel_count(ch->ports, 8), 0);
717d7fde2c9SFlorian Walpen ch->cap_fmts[3] = 0;
718d7fde2c9SFlorian Walpen ch->caps = malloc(sizeof(struct pcmchan_caps), M_HDSPE, M_NOWAIT);
719d7fde2c9SFlorian Walpen *(ch->caps) = (struct pcmchan_caps) {32000, 192000, ch->cap_fmts, 0};
720d7fde2c9SFlorian Walpen
721d7fde2c9SFlorian Walpen /* Allocate maximum buffer size. */
722d7fde2c9SFlorian Walpen ch->size = HDSPE_CHANBUF_SIZE * hdspe_channel_count(ch->ports, 8);
723e4afd792SAlexander Motin ch->data = malloc(ch->size, M_HDSPE, M_NOWAIT);
724ee36e7faSFlorian Walpen ch->position = 0;
725e4afd792SAlexander Motin
726e4afd792SAlexander Motin ch->buffer = b;
727e4afd792SAlexander Motin ch->channel = c;
728e4afd792SAlexander Motin ch->parent = scp;
729e4afd792SAlexander Motin
730e4afd792SAlexander Motin ch->dir = dir;
731e4afd792SAlexander Motin
732e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
733e4afd792SAlexander Motin
734e4afd792SAlexander Motin if (sndbuf_setup(ch->buffer, ch->data, ch->size) != 0) {
735e4afd792SAlexander Motin device_printf(scp->dev, "Can't setup sndbuf.\n");
73685d1c84cSChristos Margiolis hdspechan_free(obj, ch);
73720a9f771SRuslan Bukin return (NULL);
738e4afd792SAlexander Motin }
739e4afd792SAlexander Motin
74020a9f771SRuslan Bukin return (ch);
741e4afd792SAlexander Motin }
742e4afd792SAlexander Motin
743e4afd792SAlexander Motin static int
hdspechan_trigger(kobj_t obj,void * data,int go)744e4afd792SAlexander Motin hdspechan_trigger(kobj_t obj, void *data, int go)
745e4afd792SAlexander Motin {
74620a9f771SRuslan Bukin struct sc_pcminfo *scp;
74720a9f771SRuslan Bukin struct sc_chinfo *ch;
74820a9f771SRuslan Bukin struct sc_info *sc;
74920a9f771SRuslan Bukin
75020a9f771SRuslan Bukin ch = data;
75120a9f771SRuslan Bukin scp = ch->parent;
75220a9f771SRuslan Bukin sc = scp->sc;
753e4afd792SAlexander Motin
754e4afd792SAlexander Motin snd_mtxlock(sc->lock);
755e4afd792SAlexander Motin switch (go) {
756e4afd792SAlexander Motin case PCMTRIG_START:
757e4afd792SAlexander Motin #if 0
758e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_trigger(): start\n");
759e4afd792SAlexander Motin #endif
760e4afd792SAlexander Motin hdspechan_enable(ch, 1);
761e4afd792SAlexander Motin hdspechan_setgain(ch);
762e4afd792SAlexander Motin hdspe_start_audio(sc);
763e4afd792SAlexander Motin break;
764e4afd792SAlexander Motin
765e4afd792SAlexander Motin case PCMTRIG_STOP:
766e4afd792SAlexander Motin case PCMTRIG_ABORT:
767e4afd792SAlexander Motin #if 0
768e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_trigger(): stop or abort\n");
769e4afd792SAlexander Motin #endif
770e4afd792SAlexander Motin clean(ch);
771e4afd792SAlexander Motin hdspechan_enable(ch, 0);
772e4afd792SAlexander Motin hdspe_stop_audio(sc);
773e4afd792SAlexander Motin break;
774e4afd792SAlexander Motin
775e4afd792SAlexander Motin case PCMTRIG_EMLDMAWR:
776e4afd792SAlexander Motin case PCMTRIG_EMLDMARD:
777e4afd792SAlexander Motin if(ch->run)
778e4afd792SAlexander Motin buffer_copy(ch);
779e4afd792SAlexander Motin break;
780e4afd792SAlexander Motin }
781e4afd792SAlexander Motin
782e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
783e4afd792SAlexander Motin
78420a9f771SRuslan Bukin return (0);
785e4afd792SAlexander Motin }
786e4afd792SAlexander Motin
787e4afd792SAlexander Motin static uint32_t
hdspechan_getptr(kobj_t obj,void * data)788e4afd792SAlexander Motin hdspechan_getptr(kobj_t obj, void *data)
789e4afd792SAlexander Motin {
79020a9f771SRuslan Bukin struct sc_pcminfo *scp;
79120a9f771SRuslan Bukin struct sc_chinfo *ch;
79220a9f771SRuslan Bukin struct sc_info *sc;
793e4afd792SAlexander Motin uint32_t ret, pos;
794e4afd792SAlexander Motin
79520a9f771SRuslan Bukin ch = data;
79620a9f771SRuslan Bukin scp = ch->parent;
79720a9f771SRuslan Bukin sc = scp->sc;
79820a9f771SRuslan Bukin
799e4afd792SAlexander Motin snd_mtxlock(sc->lock);
800e4afd792SAlexander Motin ret = hdspe_read_2(sc, HDSPE_STATUS_REG);
801e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
802e4afd792SAlexander Motin
803e4afd792SAlexander Motin pos = ret & HDSPE_BUF_POSITION_MASK;
804d7fde2c9SFlorian Walpen pos *= AFMT_CHANNEL(ch->format); /* Hardbuf with multiple channels. */
805e4afd792SAlexander Motin
80620a9f771SRuslan Bukin return (pos);
807e4afd792SAlexander Motin }
808e4afd792SAlexander Motin
809e4afd792SAlexander Motin static int
hdspechan_setformat(kobj_t obj,void * data,uint32_t format)810e4afd792SAlexander Motin hdspechan_setformat(kobj_t obj, void *data, uint32_t format)
811e4afd792SAlexander Motin {
81220a9f771SRuslan Bukin struct sc_chinfo *ch;
81320a9f771SRuslan Bukin
81420a9f771SRuslan Bukin ch = data;
815e4afd792SAlexander Motin
816e4afd792SAlexander Motin #if 0
817e4afd792SAlexander Motin struct sc_pcminfo *scp = ch->parent;
818e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_setformat(%d)\n", format);
819e4afd792SAlexander Motin #endif
820e4afd792SAlexander Motin
821e4afd792SAlexander Motin ch->format = format;
822e4afd792SAlexander Motin
82320a9f771SRuslan Bukin return (0);
824e4afd792SAlexander Motin }
825e4afd792SAlexander Motin
826e4afd792SAlexander Motin static uint32_t
hdspechan_setspeed(kobj_t obj,void * data,uint32_t speed)827e4afd792SAlexander Motin hdspechan_setspeed(kobj_t obj, void *data, uint32_t speed)
828e4afd792SAlexander Motin {
82920a9f771SRuslan Bukin struct sc_pcminfo *scp;
83020a9f771SRuslan Bukin struct hdspe_rate *hr;
83120a9f771SRuslan Bukin struct sc_chinfo *ch;
83220a9f771SRuslan Bukin struct sc_info *sc;
833e4afd792SAlexander Motin long long period;
834e4afd792SAlexander Motin int threshold;
835e4afd792SAlexander Motin int i;
836e4afd792SAlexander Motin
83720a9f771SRuslan Bukin ch = data;
83820a9f771SRuslan Bukin scp = ch->parent;
83920a9f771SRuslan Bukin sc = scp->sc;
84020a9f771SRuslan Bukin hr = NULL;
84120a9f771SRuslan Bukin
842e4afd792SAlexander Motin #if 0
843e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_setspeed(%d)\n", speed);
844e4afd792SAlexander Motin #endif
845e4afd792SAlexander Motin
846e4afd792SAlexander Motin if (hdspe_running(sc) == 1)
847e4afd792SAlexander Motin goto end;
848e4afd792SAlexander Motin
8496c892b79SFlorian Walpen if (sc->force_speed > 0)
8506c892b79SFlorian Walpen speed = sc->force_speed;
8516c892b79SFlorian Walpen
852e4afd792SAlexander Motin /* First look for equal frequency. */
853e4afd792SAlexander Motin for (i = 0; rate_map[i].speed != 0; i++) {
854e4afd792SAlexander Motin if (rate_map[i].speed == speed)
855e4afd792SAlexander Motin hr = &rate_map[i];
856e4afd792SAlexander Motin }
857e4afd792SAlexander Motin
858e4afd792SAlexander Motin /* If no match, just find nearest. */
859e4afd792SAlexander Motin if (hr == NULL) {
860e4afd792SAlexander Motin for (i = 0; rate_map[i].speed != 0; i++) {
861e4afd792SAlexander Motin hr = &rate_map[i];
862e4afd792SAlexander Motin threshold = hr->speed + ((rate_map[i + 1].speed != 0) ?
863e4afd792SAlexander Motin ((rate_map[i + 1].speed - hr->speed) >> 1) : 0);
864e4afd792SAlexander Motin if (speed < threshold)
865e4afd792SAlexander Motin break;
866e4afd792SAlexander Motin }
867e4afd792SAlexander Motin }
868e4afd792SAlexander Motin
869e4afd792SAlexander Motin switch (sc->type) {
870b6052c10SRuslan Bukin case HDSPE_RAYDAT:
871b6052c10SRuslan Bukin case HDSPE_AIO:
872e4afd792SAlexander Motin period = HDSPE_FREQ_AIO;
873e4afd792SAlexander Motin break;
874e4afd792SAlexander Motin default:
875e4afd792SAlexander Motin /* Unsupported card. */
876e4afd792SAlexander Motin goto end;
877e4afd792SAlexander Motin }
878e4afd792SAlexander Motin
879e4afd792SAlexander Motin /* Write frequency on the device. */
880e4afd792SAlexander Motin sc->ctrl_register &= ~HDSPE_FREQ_MASK;
881e4afd792SAlexander Motin sc->ctrl_register |= hr->reg;
882e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
883e4afd792SAlexander Motin
884e4afd792SAlexander Motin speed = hr->speed;
885e4afd792SAlexander Motin if (speed > 96000)
886e4afd792SAlexander Motin speed /= 4;
887e4afd792SAlexander Motin else if (speed > 48000)
888e4afd792SAlexander Motin speed /= 2;
889e4afd792SAlexander Motin
890e4afd792SAlexander Motin /* Set DDS value. */
891e4afd792SAlexander Motin period /= speed;
892e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_FREQ_REG, period);
893e4afd792SAlexander Motin
894e4afd792SAlexander Motin sc->speed = hr->speed;
895e4afd792SAlexander Motin end:
89620a9f771SRuslan Bukin
89720a9f771SRuslan Bukin return (sc->speed);
898e4afd792SAlexander Motin }
899e4afd792SAlexander Motin
900e4afd792SAlexander Motin static uint32_t
hdspechan_setblocksize(kobj_t obj,void * data,uint32_t blocksize)901e4afd792SAlexander Motin hdspechan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
902e4afd792SAlexander Motin {
90320a9f771SRuslan Bukin struct hdspe_latency *hl;
90420a9f771SRuslan Bukin struct sc_pcminfo *scp;
90520a9f771SRuslan Bukin struct sc_chinfo *ch;
90620a9f771SRuslan Bukin struct sc_info *sc;
907e4afd792SAlexander Motin int threshold;
908e4afd792SAlexander Motin int i;
909e4afd792SAlexander Motin
91020a9f771SRuslan Bukin ch = data;
91120a9f771SRuslan Bukin scp = ch->parent;
91220a9f771SRuslan Bukin sc = scp->sc;
91320a9f771SRuslan Bukin hl = NULL;
91420a9f771SRuslan Bukin
915e4afd792SAlexander Motin #if 0
916e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_setblocksize(%d)\n", blocksize);
917e4afd792SAlexander Motin #endif
918e4afd792SAlexander Motin
919e4afd792SAlexander Motin if (hdspe_running(sc) == 1)
920e4afd792SAlexander Motin goto end;
921e4afd792SAlexander Motin
922e4afd792SAlexander Motin if (blocksize > HDSPE_LAT_BYTES_MAX)
923e4afd792SAlexander Motin blocksize = HDSPE_LAT_BYTES_MAX;
924e4afd792SAlexander Motin else if (blocksize < HDSPE_LAT_BYTES_MIN)
925e4afd792SAlexander Motin blocksize = HDSPE_LAT_BYTES_MIN;
926e4afd792SAlexander Motin
927e4afd792SAlexander Motin blocksize /= 4 /* samples */;
928e4afd792SAlexander Motin
929fb877263SFlorian Walpen if (sc->force_period > 0)
930fb877263SFlorian Walpen blocksize = sc->force_period;
931fb877263SFlorian Walpen
932e4afd792SAlexander Motin /* First look for equal latency. */
933e4afd792SAlexander Motin for (i = 0; latency_map[i].period != 0; i++) {
934d7fde2c9SFlorian Walpen if (latency_map[i].period == blocksize)
935e4afd792SAlexander Motin hl = &latency_map[i];
936e4afd792SAlexander Motin }
937e4afd792SAlexander Motin
938e4afd792SAlexander Motin /* If no match, just find nearest. */
939e4afd792SAlexander Motin if (hl == NULL) {
940e4afd792SAlexander Motin for (i = 0; latency_map[i].period != 0; i++) {
941e4afd792SAlexander Motin hl = &latency_map[i];
942e4afd792SAlexander Motin threshold = hl->period + ((latency_map[i + 1].period != 0) ?
943e4afd792SAlexander Motin ((latency_map[i + 1].period - hl->period) >> 1) : 0);
944e4afd792SAlexander Motin if (blocksize < threshold)
945e4afd792SAlexander Motin break;
946e4afd792SAlexander Motin }
947e4afd792SAlexander Motin }
948e4afd792SAlexander Motin
949e4afd792SAlexander Motin snd_mtxlock(sc->lock);
950e4afd792SAlexander Motin sc->ctrl_register &= ~HDSPE_LAT_MASK;
951e4afd792SAlexander Motin sc->ctrl_register |= hdspe_encode_latency(hl->n);
952e4afd792SAlexander Motin hdspe_write_4(sc, HDSPE_CONTROL_REG, sc->ctrl_register);
953e4afd792SAlexander Motin sc->period = hl->period;
954e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
955e4afd792SAlexander Motin
956e4afd792SAlexander Motin #if 0
957e4afd792SAlexander Motin device_printf(scp->dev, "New period=%d\n", sc->period);
958e4afd792SAlexander Motin #endif
959e4afd792SAlexander Motin
960f81b451dSRuslan Bukin sndbuf_resize(ch->buffer,
961f81b451dSRuslan Bukin (HDSPE_CHANBUF_SIZE * AFMT_CHANNEL(ch->format)) / (sc->period * 4),
962e4afd792SAlexander Motin (sc->period * 4));
963e4afd792SAlexander Motin end:
96420a9f771SRuslan Bukin
96520a9f771SRuslan Bukin return (sndbuf_getblksz(ch->buffer));
966e4afd792SAlexander Motin }
967e4afd792SAlexander Motin
968d7fde2c9SFlorian Walpen static uint32_t hdspe_bkp_fmt[] = {
969e4afd792SAlexander Motin SND_FORMAT(AFMT_S32_LE, 2, 0),
970e4afd792SAlexander Motin 0
971e4afd792SAlexander Motin };
972e4afd792SAlexander Motin
973d7fde2c9SFlorian Walpen static struct pcmchan_caps hdspe_bkp_caps = {32000, 192000, hdspe_bkp_fmt, 0};
974e4afd792SAlexander Motin
975e4afd792SAlexander Motin static struct pcmchan_caps *
hdspechan_getcaps(kobj_t obj,void * data)976e4afd792SAlexander Motin hdspechan_getcaps(kobj_t obj, void *data)
977e4afd792SAlexander Motin {
97820a9f771SRuslan Bukin struct sc_chinfo *ch;
97920a9f771SRuslan Bukin
98020a9f771SRuslan Bukin ch = data;
981e4afd792SAlexander Motin
982e4afd792SAlexander Motin #if 0
983e4afd792SAlexander Motin struct sc_pcminfo *scl = ch->parent;
984e4afd792SAlexander Motin device_printf(scp->dev, "hdspechan_getcaps()\n");
985e4afd792SAlexander Motin #endif
986e4afd792SAlexander Motin
987d7fde2c9SFlorian Walpen if (ch->caps != NULL)
988d7fde2c9SFlorian Walpen return (ch->caps);
989d7fde2c9SFlorian Walpen
990d7fde2c9SFlorian Walpen return (&hdspe_bkp_caps);
991e4afd792SAlexander Motin }
992e4afd792SAlexander Motin
993e4afd792SAlexander Motin static kobj_method_t hdspechan_methods[] = {
994e4afd792SAlexander Motin KOBJMETHOD(channel_init, hdspechan_init),
995e4afd792SAlexander Motin KOBJMETHOD(channel_free, hdspechan_free),
996e4afd792SAlexander Motin KOBJMETHOD(channel_setformat, hdspechan_setformat),
997e4afd792SAlexander Motin KOBJMETHOD(channel_setspeed, hdspechan_setspeed),
998e4afd792SAlexander Motin KOBJMETHOD(channel_setblocksize, hdspechan_setblocksize),
999e4afd792SAlexander Motin KOBJMETHOD(channel_trigger, hdspechan_trigger),
1000e4afd792SAlexander Motin KOBJMETHOD(channel_getptr, hdspechan_getptr),
1001e4afd792SAlexander Motin KOBJMETHOD(channel_getcaps, hdspechan_getcaps),
1002e4afd792SAlexander Motin KOBJMETHOD_END
1003e4afd792SAlexander Motin };
1004e4afd792SAlexander Motin CHANNEL_DECLARE(hdspechan);
1005e4afd792SAlexander Motin
1006e4afd792SAlexander Motin static int
hdspe_pcm_probe(device_t dev)1007e4afd792SAlexander Motin hdspe_pcm_probe(device_t dev)
1008e4afd792SAlexander Motin {
1009e4afd792SAlexander Motin
1010e4afd792SAlexander Motin #if 0
1011e4afd792SAlexander Motin device_printf(dev,"hdspe_pcm_probe()\n");
1012e4afd792SAlexander Motin #endif
1013e4afd792SAlexander Motin
101420a9f771SRuslan Bukin return (0);
1015e4afd792SAlexander Motin }
1016e4afd792SAlexander Motin
1017e4afd792SAlexander Motin static uint32_t
hdspe_pcm_intr(struct sc_pcminfo * scp)101820a9f771SRuslan Bukin hdspe_pcm_intr(struct sc_pcminfo *scp)
101920a9f771SRuslan Bukin {
1020e4afd792SAlexander Motin struct sc_chinfo *ch;
102120a9f771SRuslan Bukin struct sc_info *sc;
1022e4afd792SAlexander Motin int i;
1023e4afd792SAlexander Motin
102420a9f771SRuslan Bukin sc = scp->sc;
102520a9f771SRuslan Bukin
1026e4afd792SAlexander Motin for (i = 0; i < scp->chnum; i++) {
1027e4afd792SAlexander Motin ch = &scp->chan[i];
1028e4afd792SAlexander Motin snd_mtxunlock(sc->lock);
1029e4afd792SAlexander Motin chn_intr(ch->channel);
1030e4afd792SAlexander Motin snd_mtxlock(sc->lock);
1031e4afd792SAlexander Motin }
1032e4afd792SAlexander Motin
103320a9f771SRuslan Bukin return (0);
1034e4afd792SAlexander Motin }
1035e4afd792SAlexander Motin
1036e4afd792SAlexander Motin static int
hdspe_pcm_attach(device_t dev)1037e4afd792SAlexander Motin hdspe_pcm_attach(device_t dev)
1038e4afd792SAlexander Motin {
1039e4afd792SAlexander Motin char status[SND_STATUSLEN];
104020a9f771SRuslan Bukin struct sc_pcminfo *scp;
1041f7d3d0a4SChristos Margiolis const char *buf;
1042dc15f025SFlorian Walpen uint32_t pcm_flags;
1043d7fde2c9SFlorian Walpen int err;
1044d7fde2c9SFlorian Walpen int play, rec;
1045e4afd792SAlexander Motin
1046e4afd792SAlexander Motin scp = device_get_ivars(dev);
1047e4afd792SAlexander Motin scp->ih = &hdspe_pcm_intr;
1048e4afd792SAlexander Motin
1049d7fde2c9SFlorian Walpen if (scp->hc->ports & HDSPE_CHAN_AIO_ALL)
1050f7d3d0a4SChristos Margiolis buf = "AIO";
1051d7fde2c9SFlorian Walpen else if (scp->hc->ports & HDSPE_CHAN_RAY_ALL)
1052f7d3d0a4SChristos Margiolis buf = "RayDAT";
1053d7fde2c9SFlorian Walpen else
1054f7d3d0a4SChristos Margiolis buf = "?";
1055f7d3d0a4SChristos Margiolis device_set_descf(dev, "HDSPe %s [%s]", buf, scp->hc->descr);
1056e4afd792SAlexander Motin
1057e4afd792SAlexander Motin /*
1058e4afd792SAlexander Motin * We don't register interrupt handler with snd_setup_intr
1059e4afd792SAlexander Motin * in pcm device. Mark pcm device as MPSAFE manually.
1060e4afd792SAlexander Motin */
1061dc15f025SFlorian Walpen pcm_flags = pcm_getflags(dev) | SD_F_MPSAFE;
1062dc15f025SFlorian Walpen if (hdspe_channel_count(scp->hc->ports, 8) > HDSPE_MATRIX_MAX)
1063dc15f025SFlorian Walpen /* Disable vchan conversion, too many channels. */
1064dc15f025SFlorian Walpen pcm_flags |= SD_F_BITPERFECT;
1065dc15f025SFlorian Walpen pcm_setflags(dev, pcm_flags);
1066e4afd792SAlexander Motin
1067*516a9c02SChristos Margiolis pcm_init(dev, scp);
1068*516a9c02SChristos Margiolis
1069d7fde2c9SFlorian Walpen play = (hdspe_channel_play_ports(scp->hc)) ? 1 : 0;
1070d7fde2c9SFlorian Walpen rec = (hdspe_channel_rec_ports(scp->hc)) ? 1 : 0;
1071e4afd792SAlexander Motin
1072e4afd792SAlexander Motin scp->chnum = 0;
1073d7fde2c9SFlorian Walpen if (play) {
1074e4afd792SAlexander Motin pcm_addchan(dev, PCMDIR_PLAY, &hdspechan_class, scp);
1075e4afd792SAlexander Motin scp->chnum++;
1076e4afd792SAlexander Motin }
1077e4afd792SAlexander Motin
1078d7fde2c9SFlorian Walpen if (rec) {
1079e4afd792SAlexander Motin pcm_addchan(dev, PCMDIR_REC, &hdspechan_class, scp);
1080e4afd792SAlexander Motin scp->chnum++;
1081e4afd792SAlexander Motin }
1082e4afd792SAlexander Motin
1083837cd192SChristos Margiolis snprintf(status, SND_STATUSLEN, "port 0x%jx irq %jd on %s",
1084e4afd792SAlexander Motin rman_get_start(scp->sc->cs),
1085e4afd792SAlexander Motin rman_get_start(scp->sc->irq),
1086837cd192SChristos Margiolis device_get_nameunit(device_get_parent(dev)));
1087*516a9c02SChristos Margiolis err = pcm_register(dev, status);
1088*516a9c02SChristos Margiolis if (err) {
1089*516a9c02SChristos Margiolis device_printf(dev, "Can't register pcm.\n");
1090*516a9c02SChristos Margiolis return (ENXIO);
1091*516a9c02SChristos Margiolis }
1092e4afd792SAlexander Motin
1093e4afd792SAlexander Motin mixer_init(dev, &hdspemixer_class, scp);
1094e4afd792SAlexander Motin
109520a9f771SRuslan Bukin return (0);
1096e4afd792SAlexander Motin }
1097e4afd792SAlexander Motin
1098e4afd792SAlexander Motin static int
hdspe_pcm_detach(device_t dev)1099e4afd792SAlexander Motin hdspe_pcm_detach(device_t dev)
1100e4afd792SAlexander Motin {
1101e4afd792SAlexander Motin int err;
1102e4afd792SAlexander Motin
1103e4afd792SAlexander Motin err = pcm_unregister(dev);
1104e4afd792SAlexander Motin if (err) {
1105e4afd792SAlexander Motin device_printf(dev, "Can't unregister device.\n");
110620a9f771SRuslan Bukin return (err);
1107e4afd792SAlexander Motin }
1108e4afd792SAlexander Motin
110920a9f771SRuslan Bukin return (0);
1110e4afd792SAlexander Motin }
1111e4afd792SAlexander Motin
1112e4afd792SAlexander Motin static device_method_t hdspe_pcm_methods[] = {
1113e4afd792SAlexander Motin DEVMETHOD(device_probe, hdspe_pcm_probe),
1114e4afd792SAlexander Motin DEVMETHOD(device_attach, hdspe_pcm_attach),
1115e4afd792SAlexander Motin DEVMETHOD(device_detach, hdspe_pcm_detach),
1116e4afd792SAlexander Motin { 0, 0 }
1117e4afd792SAlexander Motin };
1118e4afd792SAlexander Motin
1119e4afd792SAlexander Motin static driver_t hdspe_pcm_driver = {
1120e4afd792SAlexander Motin "pcm",
1121e4afd792SAlexander Motin hdspe_pcm_methods,
1122e4afd792SAlexander Motin PCM_SOFTC_SIZE,
1123e4afd792SAlexander Motin };
1124e4afd792SAlexander Motin
11252287364eSJohn Baldwin DRIVER_MODULE(snd_hdspe_pcm, hdspe, hdspe_pcm_driver, 0, 0);
1126e4afd792SAlexander Motin MODULE_DEPEND(snd_hdspe, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1127e4afd792SAlexander Motin MODULE_VERSION(snd_hdspe, 1);
1128