2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
11 * - Holger Ruckdeschel: he found out how to control individual channel
12 * volumes and introduced mute switch
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
20 #include <linux/interrupt.h>
21 #include <sound/control.h>
22 #include <sound/tlv.h>
28 static const char * const opt_coax_texts
[2] = { "Optical", "Coax" };
29 static const char * const line_phono_texts
[2] = { "Line", "Phono" };
32 * data that needs to be sent to device. sets up card internal stuff.
33 * values dumped from windows driver and filtered by trial'n'error.
41 { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
42 { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
43 { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
44 { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
45 { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
46 { 0x12, 0x0d, 0x38 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
47 { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
48 { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
49 { 0 } /* TERMINATING ENTRY */
52 static const int rates_altsetting
[] = { 1, 1, 2, 2, 3, 3 };
53 /* values to write to soundcard register for all samplerates */
54 static const u16 rates_6fire_vl
[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
55 static const u16 rates_6fire_vh
[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00};
57 static DECLARE_TLV_DB_MINMAX(tlv_output
, -9000, 0);
58 static DECLARE_TLV_DB_MINMAX(tlv_input
, -1500, 1500);
61 DIGITAL_THRU_ONLY_SAMPLERATE
= 3
64 static void usb6fire_control_output_vol_update(struct control_runtime
*rt
)
66 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
70 for (i
= 0; i
< 6; i
++)
71 if (!(rt
->ovol_updated
& (1 << i
))) {
72 comm_rt
->write8(comm_rt
, 0x12, 0x0f + i
,
73 180 - rt
->output_vol
[i
]);
74 rt
->ovol_updated
|= 1 << i
;
78 static void usb6fire_control_output_mute_update(struct control_runtime
*rt
)
80 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
83 comm_rt
->write8(comm_rt
, 0x12, 0x0e, ~rt
->output_mute
);
86 static void usb6fire_control_input_vol_update(struct control_runtime
*rt
)
88 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
92 for (i
= 0; i
< 2; i
++)
93 if (!(rt
->ivol_updated
& (1 << i
))) {
94 comm_rt
->write8(comm_rt
, 0x12, 0x1c + i
,
95 rt
->input_vol
[i
] & 0x3f);
96 rt
->ivol_updated
|= 1 << i
;
100 static void usb6fire_control_line_phono_update(struct control_runtime
*rt
)
102 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
104 comm_rt
->write8(comm_rt
, 0x22, 0x02, rt
->line_phono_switch
);
105 comm_rt
->write8(comm_rt
, 0x21, 0x02, rt
->line_phono_switch
);
109 static void usb6fire_control_opt_coax_update(struct control_runtime
*rt
)
111 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
113 comm_rt
->write8(comm_rt
, 0x22, 0x00, rt
->opt_coax_switch
);
114 comm_rt
->write8(comm_rt
, 0x21, 0x00, rt
->opt_coax_switch
);
118 static int usb6fire_control_set_rate(struct control_runtime
*rt
, int rate
)
121 struct usb_device
*device
= rt
->chip
->dev
;
122 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
124 if (rate
< 0 || rate
>= CONTROL_N_RATES
)
127 ret
= usb_set_interface(device
, 1, rates_altsetting
[rate
]);
131 /* set soundcard clock */
132 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x01, rates_6fire_vl
[rate
],
133 rates_6fire_vh
[rate
]);
140 static int usb6fire_control_set_channels(
141 struct control_runtime
*rt
, int n_analog_out
,
142 int n_analog_in
, bool spdif_out
, bool spdif_in
)
145 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
147 /* enable analog inputs and outputs
148 * (one bit per stereo-channel) */
149 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x02,
150 (1 << (n_analog_out
/ 2)) - 1,
151 (1 << (n_analog_in
/ 2)) - 1);
155 /* disable digital inputs and outputs */
156 /* TODO: use spdif_x to enable/disable digital channels */
157 ret
= comm_rt
->write16(comm_rt
, 0x02, 0x03, 0x00, 0x00);
164 static int usb6fire_control_streaming_update(struct control_runtime
*rt
)
166 struct comm_runtime
*comm_rt
= rt
->chip
->comm
;
169 if (!rt
->usb_streaming
&& rt
->digital_thru_switch
)
170 usb6fire_control_set_rate(rt
,
171 DIGITAL_THRU_ONLY_SAMPLERATE
);
172 return comm_rt
->write16(comm_rt
, 0x02, 0x00, 0x00,
173 (rt
->usb_streaming
? 0x01 : 0x00) |
174 (rt
->digital_thru_switch
? 0x08 : 0x00));
179 static int usb6fire_control_output_vol_info(struct snd_kcontrol
*kcontrol
,
180 struct snd_ctl_elem_info
*uinfo
)
182 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
184 uinfo
->value
.integer
.min
= 0;
185 uinfo
->value
.integer
.max
= 180;
189 static int usb6fire_control_output_vol_put(struct snd_kcontrol
*kcontrol
,
190 struct snd_ctl_elem_value
*ucontrol
)
192 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
193 unsigned int ch
= kcontrol
->private_value
;
197 dev_err(&rt
->chip
->dev
->dev
,
198 "Invalid channel in volume control.");
202 if (rt
->output_vol
[ch
] != ucontrol
->value
.integer
.value
[0]) {
203 rt
->output_vol
[ch
] = ucontrol
->value
.integer
.value
[0];
204 rt
->ovol_updated
&= ~(1 << ch
);
207 if (rt
->output_vol
[ch
+ 1] != ucontrol
->value
.integer
.value
[1]) {
208 rt
->output_vol
[ch
+ 1] = ucontrol
->value
.integer
.value
[1];
209 rt
->ovol_updated
&= ~(2 << ch
);
214 usb6fire_control_output_vol_update(rt
);
219 static int usb6fire_control_output_vol_get(struct snd_kcontrol
*kcontrol
,
220 struct snd_ctl_elem_value
*ucontrol
)
222 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
223 unsigned int ch
= kcontrol
->private_value
;
226 dev_err(&rt
->chip
->dev
->dev
,
227 "Invalid channel in volume control.");
231 ucontrol
->value
.integer
.value
[0] = rt
->output_vol
[ch
];
232 ucontrol
->value
.integer
.value
[1] = rt
->output_vol
[ch
+ 1];
236 static int usb6fire_control_output_mute_put(struct snd_kcontrol
*kcontrol
,
237 struct snd_ctl_elem_value
*ucontrol
)
239 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
240 unsigned int ch
= kcontrol
->private_value
;
241 u8 old
= rt
->output_mute
;
245 dev_err(&rt
->chip
->dev
->dev
,
246 "Invalid channel in volume control.");
250 rt
->output_mute
&= ~(3 << ch
);
251 if (ucontrol
->value
.integer
.value
[0])
253 if (ucontrol
->value
.integer
.value
[1])
255 rt
->output_mute
|= value
<< ch
;
257 if (rt
->output_mute
!= old
)
258 usb6fire_control_output_mute_update(rt
);
260 return rt
->output_mute
!= old
;
263 static int usb6fire_control_output_mute_get(struct snd_kcontrol
*kcontrol
,
264 struct snd_ctl_elem_value
*ucontrol
)
266 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
267 unsigned int ch
= kcontrol
->private_value
;
268 u8 value
= rt
->output_mute
>> ch
;
271 dev_err(&rt
->chip
->dev
->dev
,
272 "Invalid channel in volume control.");
276 ucontrol
->value
.integer
.value
[0] = 1 & value
;
278 ucontrol
->value
.integer
.value
[1] = 1 & value
;
283 static int usb6fire_control_input_vol_info(struct snd_kcontrol
*kcontrol
,
284 struct snd_ctl_elem_info
*uinfo
)
286 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
288 uinfo
->value
.integer
.min
= 0;
289 uinfo
->value
.integer
.max
= 30;
293 static int usb6fire_control_input_vol_put(struct snd_kcontrol
*kcontrol
,
294 struct snd_ctl_elem_value
*ucontrol
)
296 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
299 if (rt
->input_vol
[0] != ucontrol
->value
.integer
.value
[0]) {
300 rt
->input_vol
[0] = ucontrol
->value
.integer
.value
[0] - 15;
301 rt
->ivol_updated
&= ~(1 << 0);
304 if (rt
->input_vol
[1] != ucontrol
->value
.integer
.value
[1]) {
305 rt
->input_vol
[1] = ucontrol
->value
.integer
.value
[1] - 15;
306 rt
->ivol_updated
&= ~(1 << 1);
311 usb6fire_control_input_vol_update(rt
);
316 static int usb6fire_control_input_vol_get(struct snd_kcontrol
*kcontrol
,
317 struct snd_ctl_elem_value
*ucontrol
)
319 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
321 ucontrol
->value
.integer
.value
[0] = rt
->input_vol
[0] + 15;
322 ucontrol
->value
.integer
.value
[1] = rt
->input_vol
[1] + 15;
327 static int usb6fire_control_line_phono_info(struct snd_kcontrol
*kcontrol
,
328 struct snd_ctl_elem_info
*uinfo
)
330 return snd_ctl_enum_info(uinfo
, 1, 2, line_phono_texts
);
333 static int usb6fire_control_line_phono_put(struct snd_kcontrol
*kcontrol
,
334 struct snd_ctl_elem_value
*ucontrol
)
336 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
338 if (rt
->line_phono_switch
!= ucontrol
->value
.integer
.value
[0]) {
339 rt
->line_phono_switch
= ucontrol
->value
.integer
.value
[0];
340 usb6fire_control_line_phono_update(rt
);
346 static int usb6fire_control_line_phono_get(struct snd_kcontrol
*kcontrol
,
347 struct snd_ctl_elem_value
*ucontrol
)
349 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
350 ucontrol
->value
.integer
.value
[0] = rt
->line_phono_switch
;
354 static int usb6fire_control_opt_coax_info(struct snd_kcontrol
*kcontrol
,
355 struct snd_ctl_elem_info
*uinfo
)
357 return snd_ctl_enum_info(uinfo
, 1, 2, opt_coax_texts
);
360 static int usb6fire_control_opt_coax_put(struct snd_kcontrol
*kcontrol
,
361 struct snd_ctl_elem_value
*ucontrol
)
363 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
366 if (rt
->opt_coax_switch
!= ucontrol
->value
.enumerated
.item
[0]) {
367 rt
->opt_coax_switch
= ucontrol
->value
.enumerated
.item
[0];
368 usb6fire_control_opt_coax_update(rt
);
374 static int usb6fire_control_opt_coax_get(struct snd_kcontrol
*kcontrol
,
375 struct snd_ctl_elem_value
*ucontrol
)
377 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
378 ucontrol
->value
.enumerated
.item
[0] = rt
->opt_coax_switch
;
382 static int usb6fire_control_digital_thru_put(struct snd_kcontrol
*kcontrol
,
383 struct snd_ctl_elem_value
*ucontrol
)
385 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
388 if (rt
->digital_thru_switch
!= ucontrol
->value
.integer
.value
[0]) {
389 rt
->digital_thru_switch
= ucontrol
->value
.integer
.value
[0];
390 usb6fire_control_streaming_update(rt
);
396 static int usb6fire_control_digital_thru_get(struct snd_kcontrol
*kcontrol
,
397 struct snd_ctl_elem_value
*ucontrol
)
399 struct control_runtime
*rt
= snd_kcontrol_chip(kcontrol
);
400 ucontrol
->value
.integer
.value
[0] = rt
->digital_thru_switch
;
404 static struct snd_kcontrol_new vol_elements
[] = {
406 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
407 .name
= "Analog Playback Volume",
410 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
411 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
412 .info
= usb6fire_control_output_vol_info
,
413 .get
= usb6fire_control_output_vol_get
,
414 .put
= usb6fire_control_output_vol_put
,
415 .tlv
= { .p
= tlv_output
}
418 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
419 .name
= "Analog Playback Volume",
422 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
423 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
424 .info
= usb6fire_control_output_vol_info
,
425 .get
= usb6fire_control_output_vol_get
,
426 .put
= usb6fire_control_output_vol_put
,
427 .tlv
= { .p
= tlv_output
}
430 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
431 .name
= "Analog Playback Volume",
434 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
435 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
436 .info
= usb6fire_control_output_vol_info
,
437 .get
= usb6fire_control_output_vol_get
,
438 .put
= usb6fire_control_output_vol_put
,
439 .tlv
= { .p
= tlv_output
}
444 static struct snd_kcontrol_new mute_elements
[] = {
446 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
447 .name
= "Analog Playback Switch",
450 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
451 .info
= snd_ctl_boolean_stereo_info
,
452 .get
= usb6fire_control_output_mute_get
,
453 .put
= usb6fire_control_output_mute_put
,
456 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
457 .name
= "Analog Playback Switch",
460 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
461 .info
= snd_ctl_boolean_stereo_info
,
462 .get
= usb6fire_control_output_mute_get
,
463 .put
= usb6fire_control_output_mute_put
,
466 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
467 .name
= "Analog Playback Switch",
470 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
471 .info
= snd_ctl_boolean_stereo_info
,
472 .get
= usb6fire_control_output_mute_get
,
473 .put
= usb6fire_control_output_mute_put
,
478 static struct snd_kcontrol_new elements
[] = {
480 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
481 .name
= "Line/Phono Capture Route",
483 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
484 .info
= usb6fire_control_line_phono_info
,
485 .get
= usb6fire_control_line_phono_get
,
486 .put
= usb6fire_control_line_phono_put
489 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
490 .name
= "Opt/Coax Capture Route",
492 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
493 .info
= usb6fire_control_opt_coax_info
,
494 .get
= usb6fire_control_opt_coax_get
,
495 .put
= usb6fire_control_opt_coax_put
498 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
499 .name
= "Digital Thru Playback Route",
501 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
502 .info
= snd_ctl_boolean_mono_info
,
503 .get
= usb6fire_control_digital_thru_get
,
504 .put
= usb6fire_control_digital_thru_put
507 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
508 .name
= "Analog Capture Volume",
510 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
511 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
512 .info
= usb6fire_control_input_vol_info
,
513 .get
= usb6fire_control_input_vol_get
,
514 .put
= usb6fire_control_input_vol_put
,
515 .tlv
= { .p
= tlv_input
}
520 static int usb6fire_control_add_virtual(
521 struct control_runtime
*rt
,
522 struct snd_card
*card
,
524 struct snd_kcontrol_new
*elems
)
528 struct snd_kcontrol
*vmaster
=
529 snd_ctl_make_virtual_master(name
, tlv_output
);
530 struct snd_kcontrol
*control
;
534 ret
= snd_ctl_add(card
, vmaster
);
539 while (elems
[i
].name
) {
540 control
= snd_ctl_new1(&elems
[i
], rt
);
543 ret
= snd_ctl_add(card
, control
);
546 ret
= snd_ctl_add_slave(vmaster
, control
);
554 int usb6fire_control_init(struct sfire_chip
*chip
)
558 struct control_runtime
*rt
= kzalloc(sizeof(struct control_runtime
),
560 struct comm_runtime
*comm_rt
= chip
->comm
;
566 rt
->update_streaming
= usb6fire_control_streaming_update
;
567 rt
->set_rate
= usb6fire_control_set_rate
;
568 rt
->set_channels
= usb6fire_control_set_channels
;
571 while (init_data
[i
].type
) {
572 comm_rt
->write8(comm_rt
, init_data
[i
].type
, init_data
[i
].reg
,
577 usb6fire_control_opt_coax_update(rt
);
578 usb6fire_control_line_phono_update(rt
);
579 usb6fire_control_output_vol_update(rt
);
580 usb6fire_control_output_mute_update(rt
);
581 usb6fire_control_input_vol_update(rt
);
582 usb6fire_control_streaming_update(rt
);
584 ret
= usb6fire_control_add_virtual(rt
, chip
->card
,
585 "Master Playback Volume", vol_elements
);
587 dev_err(&chip
->dev
->dev
, "cannot add control.\n");
591 ret
= usb6fire_control_add_virtual(rt
, chip
->card
,
592 "Master Playback Switch", mute_elements
);
594 dev_err(&chip
->dev
->dev
, "cannot add control.\n");
600 while (elements
[i
].name
) {
601 ret
= snd_ctl_add(chip
->card
, snd_ctl_new1(&elements
[i
], rt
));
604 dev_err(&chip
->dev
->dev
, "cannot add control.\n");
614 void usb6fire_control_abort(struct sfire_chip
*chip
)
617 void usb6fire_control_destroy(struct sfire_chip
*chip
)
619 kfree(chip
->control
);
620 chip
->control
= NULL
;