1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for TerraTec DMX 6Fire USB
7 * Author: Torsten Schenk <torsten.schenk@zoho.com>
8 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
12 * - Holger Ruckdeschel: he found out how to control individual channel
13 * volumes and introduced mute switch
16 #include <linux/interrupt.h>
17 #include <sound/control.h>
18 #include <sound/tlv.h>
24 static const char * const opt_coax_texts[2] = { "Optical", "Coax" };
25 static const char * const line_phono_texts[2] = { "Line", "Phono" };
28 * data that needs to be sent to device. sets up card internal stuff.
29 * values dumped from windows driver and filtered by trial'n'error.
37 { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
38 { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
39 { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
40 { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
41 { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
42 { 0x12, 0x0d, 0x38 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
43 { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
44 { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
45 { 0 } /* TERMINATING ENTRY */
48 static const int rates_altsetting[] = { 1, 1, 2, 2, 3, 3 };
49 /* values to write to soundcard register for all samplerates */
50 static const u16 rates_6fire_vl[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
51 static const u16 rates_6fire_vh[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00};
53 static DECLARE_TLV_DB_MINMAX(tlv_output, -9000, 0);
54 static DECLARE_TLV_DB_MINMAX(tlv_input, -1500, 1500);
57 DIGITAL_THRU_ONLY_SAMPLERATE = 3
60 static void usb6fire_control_output_vol_update(struct control_runtime *rt)
62 struct comm_runtime *comm_rt = rt->chip->comm;
66 for (i = 0; i < 6; i++)
67 if (!(rt->ovol_updated & (1 << i))) {
68 comm_rt->write8(comm_rt, 0x12, 0x0f + i,
69 180 - rt->output_vol[i]);
70 rt->ovol_updated |= 1 << i;
74 static void usb6fire_control_output_mute_update(struct control_runtime *rt)
76 struct comm_runtime *comm_rt = rt->chip->comm;
79 comm_rt->write8(comm_rt, 0x12, 0x0e, ~rt->output_mute);
82 static void usb6fire_control_input_vol_update(struct control_runtime *rt)
84 struct comm_runtime *comm_rt = rt->chip->comm;
88 for (i = 0; i < 2; i++)
89 if (!(rt->ivol_updated & (1 << i))) {
90 comm_rt->write8(comm_rt, 0x12, 0x1c + i,
91 rt->input_vol[i] & 0x3f);
92 rt->ivol_updated |= 1 << i;
96 static void usb6fire_control_line_phono_update(struct control_runtime *rt)
98 struct comm_runtime *comm_rt = rt->chip->comm;
100 comm_rt->write8(comm_rt, 0x22, 0x02, rt->line_phono_switch);
101 comm_rt->write8(comm_rt, 0x21, 0x02, rt->line_phono_switch);
105 static void usb6fire_control_opt_coax_update(struct control_runtime *rt)
107 struct comm_runtime *comm_rt = rt->chip->comm;
109 comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch);
110 comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch);
114 static int usb6fire_control_set_rate(struct control_runtime *rt, int rate)
117 struct usb_device *device = rt->chip->dev;
118 struct comm_runtime *comm_rt = rt->chip->comm;
120 if (rate < 0 || rate >= CONTROL_N_RATES)
123 ret = usb_set_interface(device, 1, rates_altsetting[rate]);
127 /* set soundcard clock */
128 ret = comm_rt->write16(comm_rt, 0x02, 0x01, rates_6fire_vl[rate],
129 rates_6fire_vh[rate]);
136 static int usb6fire_control_set_channels(
137 struct control_runtime *rt, int n_analog_out,
138 int n_analog_in, bool spdif_out, bool spdif_in)
141 struct comm_runtime *comm_rt = rt->chip->comm;
143 /* enable analog inputs and outputs
144 * (one bit per stereo-channel) */
145 ret = comm_rt->write16(comm_rt, 0x02, 0x02,
146 (1 << (n_analog_out / 2)) - 1,
147 (1 << (n_analog_in / 2)) - 1);
151 /* disable digital inputs and outputs */
152 /* TODO: use spdif_x to enable/disable digital channels */
153 ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00);
160 static int usb6fire_control_streaming_update(struct control_runtime *rt)
162 struct comm_runtime *comm_rt = rt->chip->comm;
165 if (!rt->usb_streaming && rt->digital_thru_switch)
166 usb6fire_control_set_rate(rt,
167 DIGITAL_THRU_ONLY_SAMPLERATE);
168 return comm_rt->write16(comm_rt, 0x02, 0x00, 0x00,
169 (rt->usb_streaming ? 0x01 : 0x00) |
170 (rt->digital_thru_switch ? 0x08 : 0x00));
175 static int usb6fire_control_output_vol_info(struct snd_kcontrol *kcontrol,
176 struct snd_ctl_elem_info *uinfo)
178 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
180 uinfo->value.integer.min = 0;
181 uinfo->value.integer.max = 180;
185 static int usb6fire_control_output_vol_put(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
188 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
189 unsigned int ch = kcontrol->private_value;
193 dev_err(&rt->chip->dev->dev,
194 "Invalid channel in volume control.");
198 if (rt->output_vol[ch] != ucontrol->value.integer.value[0]) {
199 rt->output_vol[ch] = ucontrol->value.integer.value[0];
200 rt->ovol_updated &= ~(1 << ch);
203 if (rt->output_vol[ch + 1] != ucontrol->value.integer.value[1]) {
204 rt->output_vol[ch + 1] = ucontrol->value.integer.value[1];
205 rt->ovol_updated &= ~(2 << ch);
210 usb6fire_control_output_vol_update(rt);
215 static int usb6fire_control_output_vol_get(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_value *ucontrol)
218 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
219 unsigned int ch = kcontrol->private_value;
222 dev_err(&rt->chip->dev->dev,
223 "Invalid channel in volume control.");
227 ucontrol->value.integer.value[0] = rt->output_vol[ch];
228 ucontrol->value.integer.value[1] = rt->output_vol[ch + 1];
232 static int usb6fire_control_output_mute_put(struct snd_kcontrol *kcontrol,
233 struct snd_ctl_elem_value *ucontrol)
235 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
236 unsigned int ch = kcontrol->private_value;
237 u8 old = rt->output_mute;
241 dev_err(&rt->chip->dev->dev,
242 "Invalid channel in volume control.");
246 rt->output_mute &= ~(3 << ch);
247 if (ucontrol->value.integer.value[0])
249 if (ucontrol->value.integer.value[1])
251 rt->output_mute |= value << ch;
253 if (rt->output_mute != old)
254 usb6fire_control_output_mute_update(rt);
256 return rt->output_mute != old;
259 static int usb6fire_control_output_mute_get(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_value *ucontrol)
262 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
263 unsigned int ch = kcontrol->private_value;
264 u8 value = rt->output_mute >> ch;
267 dev_err(&rt->chip->dev->dev,
268 "Invalid channel in volume control.");
272 ucontrol->value.integer.value[0] = 1 & value;
274 ucontrol->value.integer.value[1] = 1 & value;
279 static int usb6fire_control_input_vol_info(struct snd_kcontrol *kcontrol,
280 struct snd_ctl_elem_info *uinfo)
282 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
284 uinfo->value.integer.min = 0;
285 uinfo->value.integer.max = 30;
289 static int usb6fire_control_input_vol_put(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_value *ucontrol)
292 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
295 if (rt->input_vol[0] != ucontrol->value.integer.value[0]) {
296 rt->input_vol[0] = ucontrol->value.integer.value[0] - 15;
297 rt->ivol_updated &= ~(1 << 0);
300 if (rt->input_vol[1] != ucontrol->value.integer.value[1]) {
301 rt->input_vol[1] = ucontrol->value.integer.value[1] - 15;
302 rt->ivol_updated &= ~(1 << 1);
307 usb6fire_control_input_vol_update(rt);
312 static int usb6fire_control_input_vol_get(struct snd_kcontrol *kcontrol,
313 struct snd_ctl_elem_value *ucontrol)
315 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
317 ucontrol->value.integer.value[0] = rt->input_vol[0] + 15;
318 ucontrol->value.integer.value[1] = rt->input_vol[1] + 15;
323 static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol,
324 struct snd_ctl_elem_info *uinfo)
326 return snd_ctl_enum_info(uinfo, 1, 2, line_phono_texts);
329 static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol,
330 struct snd_ctl_elem_value *ucontrol)
332 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
334 if (rt->line_phono_switch != ucontrol->value.integer.value[0]) {
335 rt->line_phono_switch = ucontrol->value.integer.value[0];
336 usb6fire_control_line_phono_update(rt);
342 static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_value *ucontrol)
345 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
346 ucontrol->value.integer.value[0] = rt->line_phono_switch;
350 static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_info *uinfo)
353 return snd_ctl_enum_info(uinfo, 1, 2, opt_coax_texts);
356 static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol,
357 struct snd_ctl_elem_value *ucontrol)
359 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
362 if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) {
363 rt->opt_coax_switch = ucontrol->value.enumerated.item[0];
364 usb6fire_control_opt_coax_update(rt);
370 static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol,
371 struct snd_ctl_elem_value *ucontrol)
373 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
374 ucontrol->value.enumerated.item[0] = rt->opt_coax_switch;
378 static int usb6fire_control_digital_thru_put(struct snd_kcontrol *kcontrol,
379 struct snd_ctl_elem_value *ucontrol)
381 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
384 if (rt->digital_thru_switch != ucontrol->value.integer.value[0]) {
385 rt->digital_thru_switch = ucontrol->value.integer.value[0];
386 usb6fire_control_streaming_update(rt);
392 static int usb6fire_control_digital_thru_get(struct snd_kcontrol *kcontrol,
393 struct snd_ctl_elem_value *ucontrol)
395 struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
396 ucontrol->value.integer.value[0] = rt->digital_thru_switch;
400 static const struct snd_kcontrol_new vol_elements[] = {
402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
403 .name = "Analog Playback Volume",
406 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
407 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
408 .info = usb6fire_control_output_vol_info,
409 .get = usb6fire_control_output_vol_get,
410 .put = usb6fire_control_output_vol_put,
411 .tlv = { .p = tlv_output }
414 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
415 .name = "Analog Playback Volume",
418 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
419 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
420 .info = usb6fire_control_output_vol_info,
421 .get = usb6fire_control_output_vol_get,
422 .put = usb6fire_control_output_vol_put,
423 .tlv = { .p = tlv_output }
426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
427 .name = "Analog Playback Volume",
430 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
431 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
432 .info = usb6fire_control_output_vol_info,
433 .get = usb6fire_control_output_vol_get,
434 .put = usb6fire_control_output_vol_put,
435 .tlv = { .p = tlv_output }
440 static const struct snd_kcontrol_new mute_elements[] = {
442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
443 .name = "Analog Playback Switch",
446 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
447 .info = snd_ctl_boolean_stereo_info,
448 .get = usb6fire_control_output_mute_get,
449 .put = usb6fire_control_output_mute_put,
452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
453 .name = "Analog Playback Switch",
456 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
457 .info = snd_ctl_boolean_stereo_info,
458 .get = usb6fire_control_output_mute_get,
459 .put = usb6fire_control_output_mute_put,
462 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
463 .name = "Analog Playback Switch",
466 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
467 .info = snd_ctl_boolean_stereo_info,
468 .get = usb6fire_control_output_mute_get,
469 .put = usb6fire_control_output_mute_put,
474 static const struct snd_kcontrol_new elements[] = {
476 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
477 .name = "Line/Phono Capture Route",
479 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
480 .info = usb6fire_control_line_phono_info,
481 .get = usb6fire_control_line_phono_get,
482 .put = usb6fire_control_line_phono_put
485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
486 .name = "Opt/Coax Capture Route",
488 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
489 .info = usb6fire_control_opt_coax_info,
490 .get = usb6fire_control_opt_coax_get,
491 .put = usb6fire_control_opt_coax_put
494 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
495 .name = "Digital Thru Playback Route",
497 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
498 .info = snd_ctl_boolean_mono_info,
499 .get = usb6fire_control_digital_thru_get,
500 .put = usb6fire_control_digital_thru_put
503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
504 .name = "Analog Capture Volume",
506 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
507 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
508 .info = usb6fire_control_input_vol_info,
509 .get = usb6fire_control_input_vol_get,
510 .put = usb6fire_control_input_vol_put,
511 .tlv = { .p = tlv_input }
516 static int usb6fire_control_add_virtual(
517 struct control_runtime *rt,
518 struct snd_card *card,
520 const struct snd_kcontrol_new *elems)
524 struct snd_kcontrol *vmaster =
525 snd_ctl_make_virtual_master(name, tlv_output);
526 struct snd_kcontrol *control;
530 ret = snd_ctl_add(card, vmaster);
535 while (elems[i].name) {
536 control = snd_ctl_new1(&elems[i], rt);
539 ret = snd_ctl_add(card, control);
542 ret = snd_ctl_add_follower(vmaster, control);
550 int usb6fire_control_init(struct sfire_chip *chip)
554 struct control_runtime *rt = kzalloc(sizeof(struct control_runtime),
556 struct comm_runtime *comm_rt = chip->comm;
562 rt->update_streaming = usb6fire_control_streaming_update;
563 rt->set_rate = usb6fire_control_set_rate;
564 rt->set_channels = usb6fire_control_set_channels;
567 while (init_data[i].type) {
568 comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg,
573 usb6fire_control_opt_coax_update(rt);
574 usb6fire_control_line_phono_update(rt);
575 usb6fire_control_output_vol_update(rt);
576 usb6fire_control_output_mute_update(rt);
577 usb6fire_control_input_vol_update(rt);
578 usb6fire_control_streaming_update(rt);
580 ret = usb6fire_control_add_virtual(rt, chip->card,
581 "Master Playback Volume", vol_elements);
583 dev_err(&chip->dev->dev, "cannot add control.\n");
587 ret = usb6fire_control_add_virtual(rt, chip->card,
588 "Master Playback Switch", mute_elements);
590 dev_err(&chip->dev->dev, "cannot add control.\n");
596 while (elements[i].name) {
597 ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt));
600 dev_err(&chip->dev->dev, "cannot add control.\n");
610 void usb6fire_control_abort(struct sfire_chip *chip)
613 void usb6fire_control_destroy(struct sfire_chip *chip)
615 kfree(chip->control);
616 chip->control = NULL;