xref: /linux/sound/usb/6fire/control.c (revision 3bb598fb23b6040e67b5e6db9a00b28cd26e5809)
1 /*
2  * Linux driver for TerraTec DMX 6Fire USB
3  *
4  * Mixer control
5  *
6  * Author:	Torsten Schenk <torsten.schenk@zoho.com>
7  * Created:	Jan 01, 2011
8  * Version:	0.3.0
9  * Copyright:	(C) Torsten Schenk
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  */
16 
17 #include <linux/interrupt.h>
18 #include <sound/control.h>
19 
20 #include "control.h"
21 #include "comm.h"
22 #include "chip.h"
23 
24 static char *opt_coax_texts[2] = { "Optical", "Coax" };
25 static char *line_phono_texts[2] = { "Line", "Phono" };
26 
27 /*
28  * calculated with $value\[i\] = 128 \cdot sqrt[3]{\frac{i}{128}}$
29  * this is done because the linear values cause rapid degredation
30  * of volume in the uppermost region.
31  */
32 static const u8 log_volume_table[128] = {
33 	0x00, 0x19, 0x20, 0x24, 0x28, 0x2b, 0x2e, 0x30, 0x32, 0x34,
34 	0x36, 0x38, 0x3a, 0x3b, 0x3d, 0x3e, 0x40, 0x41, 0x42, 0x43,
35 	0x44, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
36 	0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56,
37 	0x56, 0x57, 0x58, 0x58, 0x59, 0x5a, 0x5b, 0x5b, 0x5c, 0x5c,
38 	0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62,
39 	0x63, 0x63, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68,
40 	0x68, 0x69, 0x69, 0x6a, 0x6a, 0x6b, 0x6b, 0x6c, 0x6c, 0x6c,
41 	0x6d, 0x6d, 0x6e, 0x6e, 0x6f, 0x6f, 0x70, 0x70, 0x70, 0x71,
42 	0x71, 0x72, 0x72, 0x73, 0x73, 0x73, 0x74, 0x74, 0x75, 0x75,
43 	0x75, 0x76, 0x76, 0x77, 0x77, 0x77, 0x78, 0x78, 0x78, 0x79,
44 	0x79, 0x7a, 0x7a, 0x7a, 0x7b, 0x7b, 0x7b, 0x7c, 0x7c, 0x7c,
45 	0x7d, 0x7d, 0x7d, 0x7e, 0x7e, 0x7e, 0x7f, 0x7f };
46 
47 /*
48  * data that needs to be sent to device. sets up card internal stuff.
49  * values dumped from windows driver and filtered by trial'n'error.
50  */
51 static const struct {
52 	u8 type;
53 	u8 reg;
54 	u8 value;
55 }
56 init_data[] = {
57 	{ 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
58 	{ 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
59 	{ 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
60 	{ 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
61 	{ 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
62 	{ 0x12, 0x0d, 0x78 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
63 	{ 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
64 	{ 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
65 	{ 0 } /* TERMINATING ENTRY */
66 };
67 
68 static void usb6fire_control_master_vol_update(struct control_runtime *rt)
69 {
70 	struct comm_runtime *comm_rt = rt->chip->comm;
71 	if (comm_rt) {
72 		/* set volume */
73 		comm_rt->write8(comm_rt, 0x12, 0x0f, 0x7f -
74 				log_volume_table[rt->master_vol]);
75 		 /* unmute */
76 		comm_rt->write8(comm_rt, 0x12, 0x0e, 0x00);
77 	}
78 }
79 
80 static void usb6fire_control_line_phono_update(struct control_runtime *rt)
81 {
82 	struct comm_runtime *comm_rt = rt->chip->comm;
83 	if (comm_rt) {
84 		comm_rt->write8(comm_rt, 0x22, 0x02, rt->line_phono_switch);
85 		comm_rt->write8(comm_rt, 0x21, 0x02, rt->line_phono_switch);
86 	}
87 }
88 
89 static void usb6fire_control_opt_coax_update(struct control_runtime *rt)
90 {
91 	struct comm_runtime *comm_rt = rt->chip->comm;
92 	if (comm_rt) {
93 		comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch);
94 		comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch);
95 	}
96 }
97 
98 static int usb6fire_control_master_vol_info(struct snd_kcontrol *kcontrol,
99 		struct snd_ctl_elem_info *uinfo)
100 {
101 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
102 	uinfo->count = 1;
103 	uinfo->value.integer.min = 0;
104 	uinfo->value.integer.max = 127;
105 	return 0;
106 }
107 
108 static int usb6fire_control_master_vol_put(struct snd_kcontrol *kcontrol,
109 		struct snd_ctl_elem_value *ucontrol)
110 {
111 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
112 	int changed = 0;
113 	if (rt->master_vol != ucontrol->value.integer.value[0]) {
114 		rt->master_vol = ucontrol->value.integer.value[0];
115 		usb6fire_control_master_vol_update(rt);
116 		changed = 1;
117 	}
118 	return changed;
119 }
120 
121 static int usb6fire_control_master_vol_get(struct snd_kcontrol *kcontrol,
122 		struct snd_ctl_elem_value *ucontrol)
123 {
124 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
125 	ucontrol->value.integer.value[0] = rt->master_vol;
126 	return 0;
127 }
128 
129 static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol,
130 		struct snd_ctl_elem_info *uinfo)
131 {
132 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
133 	uinfo->count = 1;
134 	uinfo->value.enumerated.items = 2;
135 	if (uinfo->value.enumerated.item > 1)
136 		uinfo->value.enumerated.item = 1;
137 	strcpy(uinfo->value.enumerated.name,
138 			line_phono_texts[uinfo->value.enumerated.item]);
139 	return 0;
140 }
141 
142 static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol,
143 		struct snd_ctl_elem_value *ucontrol)
144 {
145 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
146 	int changed = 0;
147 	if (rt->line_phono_switch != ucontrol->value.integer.value[0]) {
148 		rt->line_phono_switch = ucontrol->value.integer.value[0];
149 		usb6fire_control_line_phono_update(rt);
150 		changed = 1;
151 	}
152 	return changed;
153 }
154 
155 static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol,
156 		struct snd_ctl_elem_value *ucontrol)
157 {
158 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
159 	ucontrol->value.integer.value[0] = rt->line_phono_switch;
160 	return 0;
161 }
162 
163 static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol,
164 		struct snd_ctl_elem_info *uinfo)
165 {
166 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
167 	uinfo->count = 1;
168 	uinfo->value.enumerated.items = 2;
169 	if (uinfo->value.enumerated.item > 1)
170 		uinfo->value.enumerated.item = 1;
171 	strcpy(uinfo->value.enumerated.name,
172 			opt_coax_texts[uinfo->value.enumerated.item]);
173 	return 0;
174 }
175 
176 static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol,
177 		struct snd_ctl_elem_value *ucontrol)
178 {
179 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
180 	int changed = 0;
181 
182 	if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) {
183 		rt->opt_coax_switch = ucontrol->value.enumerated.item[0];
184 		usb6fire_control_opt_coax_update(rt);
185 		changed = 1;
186 	}
187 	return changed;
188 }
189 
190 static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol,
191 		struct snd_ctl_elem_value *ucontrol)
192 {
193 	struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
194 	ucontrol->value.enumerated.item[0] = rt->opt_coax_switch;
195 	return 0;
196 }
197 
198 static struct __devinitdata snd_kcontrol_new elements[] = {
199 	{
200 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
201 		.name = "Master Playback Volume",
202 		.index = 0,
203 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
204 		.info = usb6fire_control_master_vol_info,
205 		.get = usb6fire_control_master_vol_get,
206 		.put = usb6fire_control_master_vol_put
207 	},
208 	{
209 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
210 		.name = "Line/Phono Capture Route",
211 		.index = 0,
212 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
213 		.info = usb6fire_control_line_phono_info,
214 		.get = usb6fire_control_line_phono_get,
215 		.put = usb6fire_control_line_phono_put
216 	},
217 	{
218 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
219 		.name = "Opt/Coax Capture Route",
220 		.index = 0,
221 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
222 		.info = usb6fire_control_opt_coax_info,
223 		.get = usb6fire_control_opt_coax_get,
224 		.put = usb6fire_control_opt_coax_put
225 	},
226 	{}
227 };
228 
229 int __devinit usb6fire_control_init(struct sfire_chip *chip)
230 {
231 	int i;
232 	int ret;
233 	struct control_runtime *rt = kzalloc(sizeof(struct control_runtime),
234 			GFP_KERNEL);
235 	struct comm_runtime *comm_rt = chip->comm;
236 
237 	if (!rt)
238 		return -ENOMEM;
239 
240 	rt->chip = chip;
241 
242 	i = 0;
243 	while (init_data[i].type) {
244 		comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg,
245 				init_data[i].value);
246 		i++;
247 	}
248 
249 	usb6fire_control_opt_coax_update(rt);
250 	usb6fire_control_line_phono_update(rt);
251 	usb6fire_control_master_vol_update(rt);
252 
253 	i = 0;
254 	while (elements[i].name) {
255 		ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt));
256 		if (ret < 0) {
257 			kfree(rt);
258 			snd_printk(KERN_ERR PREFIX "cannot add control.\n");
259 			return ret;
260 		}
261 		i++;
262 	}
263 
264 	chip->control = rt;
265 	return 0;
266 }
267 
268 void usb6fire_control_abort(struct sfire_chip *chip)
269 {}
270 
271 void usb6fire_control_destroy(struct sfire_chip *chip)
272 {
273 	kfree(chip->control);
274 	chip->control = NULL;
275 }
276