control.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include <linux/interrupt.h>
  17. #include <sound/control.h>
  18. #include "control.h"
  19. #include "comm.h"
  20. #include "chip.h"
  21. static char *opt_coax_texts[2] = { "Optical", "Coax" };
  22. static char *line_phono_texts[2] = { "Line", "Phono" };
  23. /*
  24. * calculated with $value\[i\] = 128 \cdot sqrt[3]{\frac{i}{128}}$
  25. * this is done because the linear values cause rapid degredation
  26. * of volume in the uppermost region.
  27. */
  28. static const u8 log_volume_table[128] = {
  29. 0x00, 0x19, 0x20, 0x24, 0x28, 0x2b, 0x2e, 0x30, 0x32, 0x34,
  30. 0x36, 0x38, 0x3a, 0x3b, 0x3d, 0x3e, 0x40, 0x41, 0x42, 0x43,
  31. 0x44, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
  32. 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x53, 0x54, 0x55, 0x56,
  33. 0x56, 0x57, 0x58, 0x58, 0x59, 0x5a, 0x5b, 0x5b, 0x5c, 0x5c,
  34. 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62,
  35. 0x63, 0x63, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68,
  36. 0x68, 0x69, 0x69, 0x6a, 0x6a, 0x6b, 0x6b, 0x6c, 0x6c, 0x6c,
  37. 0x6d, 0x6d, 0x6e, 0x6e, 0x6f, 0x6f, 0x70, 0x70, 0x70, 0x71,
  38. 0x71, 0x72, 0x72, 0x73, 0x73, 0x73, 0x74, 0x74, 0x75, 0x75,
  39. 0x75, 0x76, 0x76, 0x77, 0x77, 0x77, 0x78, 0x78, 0x78, 0x79,
  40. 0x79, 0x7a, 0x7a, 0x7a, 0x7b, 0x7b, 0x7b, 0x7c, 0x7c, 0x7c,
  41. 0x7d, 0x7d, 0x7d, 0x7e, 0x7e, 0x7e, 0x7f, 0x7f };
  42. /*
  43. * data that needs to be sent to device. sets up card internal stuff.
  44. * values dumped from windows driver and filtered by trial'n'error.
  45. */
  46. static const struct {
  47. u8 type;
  48. u8 reg;
  49. u8 value;
  50. }
  51. init_data[] = {
  52. { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
  53. { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
  54. { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
  55. { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
  56. { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
  57. { 0x12, 0x0d, 0x78 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
  58. { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
  59. { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
  60. { 0 } /* TERMINATING ENTRY */
  61. };
  62. static const int rates_altsetting[] = { 1, 1, 2, 2, 3, 3 };
  63. /* values to write to soundcard register for all samplerates */
  64. static const u16 rates_6fire_vl[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
  65. static const u16 rates_6fire_vh[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00};
  66. enum {
  67. DIGITAL_THRU_ONLY_SAMPLERATE = 3
  68. };
  69. static void usb6fire_control_master_vol_update(struct control_runtime *rt)
  70. {
  71. struct comm_runtime *comm_rt = rt->chip->comm;
  72. if (comm_rt) {
  73. /* set volume */
  74. comm_rt->write8(comm_rt, 0x12, 0x0f, 0x7f -
  75. log_volume_table[rt->master_vol]);
  76. /* unmute */
  77. comm_rt->write8(comm_rt, 0x12, 0x0e, 0x00);
  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. static void usb6fire_control_opt_coax_update(struct control_runtime *rt)
  89. {
  90. struct comm_runtime *comm_rt = rt->chip->comm;
  91. if (comm_rt) {
  92. comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch);
  93. comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch);
  94. }
  95. }
  96. static int usb6fire_control_set_rate(struct control_runtime *rt, int rate)
  97. {
  98. int ret;
  99. struct usb_device *device = rt->chip->dev;
  100. struct comm_runtime *comm_rt = rt->chip->comm;
  101. if (rate < 0 || rate >= CONTROL_N_RATES)
  102. return -EINVAL;
  103. ret = usb_set_interface(device, 1, rates_altsetting[rate]);
  104. if (ret < 0)
  105. return ret;
  106. /* set soundcard clock */
  107. ret = comm_rt->write16(comm_rt, 0x02, 0x01, rates_6fire_vl[rate],
  108. rates_6fire_vh[rate]);
  109. if (ret < 0)
  110. return ret;
  111. return 0;
  112. }
  113. static int usb6fire_control_set_channels(
  114. struct control_runtime *rt, int n_analog_out,
  115. int n_analog_in, bool spdif_out, bool spdif_in)
  116. {
  117. int ret;
  118. struct comm_runtime *comm_rt = rt->chip->comm;
  119. /* enable analog inputs and outputs
  120. * (one bit per stereo-channel) */
  121. ret = comm_rt->write16(comm_rt, 0x02, 0x02,
  122. (1 << (n_analog_out / 2)) - 1,
  123. (1 << (n_analog_in / 2)) - 1);
  124. if (ret < 0)
  125. return ret;
  126. /* disable digital inputs and outputs */
  127. /* TODO: use spdif_x to enable/disable digital channels */
  128. ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static int usb6fire_control_streaming_update(struct control_runtime *rt)
  134. {
  135. struct comm_runtime *comm_rt = rt->chip->comm;
  136. if (comm_rt) {
  137. if (!rt->usb_streaming && rt->digital_thru_switch)
  138. usb6fire_control_set_rate(rt,
  139. DIGITAL_THRU_ONLY_SAMPLERATE);
  140. return comm_rt->write16(comm_rt, 0x02, 0x00, 0x00,
  141. (rt->usb_streaming ? 0x01 : 0x00) |
  142. (rt->digital_thru_switch ? 0x08 : 0x00));
  143. }
  144. return -EINVAL;
  145. }
  146. static int usb6fire_control_master_vol_info(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_info *uinfo)
  148. {
  149. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  150. uinfo->count = 1;
  151. uinfo->value.integer.min = 0;
  152. uinfo->value.integer.max = 127;
  153. return 0;
  154. }
  155. static int usb6fire_control_master_vol_put(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_value *ucontrol)
  157. {
  158. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  159. int changed = 0;
  160. if (rt->master_vol != ucontrol->value.integer.value[0]) {
  161. rt->master_vol = ucontrol->value.integer.value[0];
  162. usb6fire_control_master_vol_update(rt);
  163. changed = 1;
  164. }
  165. return changed;
  166. }
  167. static int usb6fire_control_master_vol_get(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_value *ucontrol)
  169. {
  170. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  171. ucontrol->value.integer.value[0] = rt->master_vol;
  172. return 0;
  173. }
  174. static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol,
  175. struct snd_ctl_elem_info *uinfo)
  176. {
  177. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  178. uinfo->count = 1;
  179. uinfo->value.enumerated.items = 2;
  180. if (uinfo->value.enumerated.item > 1)
  181. uinfo->value.enumerated.item = 1;
  182. strcpy(uinfo->value.enumerated.name,
  183. line_phono_texts[uinfo->value.enumerated.item]);
  184. return 0;
  185. }
  186. static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol,
  187. struct snd_ctl_elem_value *ucontrol)
  188. {
  189. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  190. int changed = 0;
  191. if (rt->line_phono_switch != ucontrol->value.integer.value[0]) {
  192. rt->line_phono_switch = ucontrol->value.integer.value[0];
  193. usb6fire_control_line_phono_update(rt);
  194. changed = 1;
  195. }
  196. return changed;
  197. }
  198. static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_value *ucontrol)
  200. {
  201. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  202. ucontrol->value.integer.value[0] = rt->line_phono_switch;
  203. return 0;
  204. }
  205. static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol,
  206. struct snd_ctl_elem_info *uinfo)
  207. {
  208. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  209. uinfo->count = 1;
  210. uinfo->value.enumerated.items = 2;
  211. if (uinfo->value.enumerated.item > 1)
  212. uinfo->value.enumerated.item = 1;
  213. strcpy(uinfo->value.enumerated.name,
  214. opt_coax_texts[uinfo->value.enumerated.item]);
  215. return 0;
  216. }
  217. static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_value *ucontrol)
  219. {
  220. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  221. int changed = 0;
  222. if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) {
  223. rt->opt_coax_switch = ucontrol->value.enumerated.item[0];
  224. usb6fire_control_opt_coax_update(rt);
  225. changed = 1;
  226. }
  227. return changed;
  228. }
  229. static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol,
  230. struct snd_ctl_elem_value *ucontrol)
  231. {
  232. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  233. ucontrol->value.enumerated.item[0] = rt->opt_coax_switch;
  234. return 0;
  235. }
  236. static int usb6fire_control_digital_thru_put(struct snd_kcontrol *kcontrol,
  237. struct snd_ctl_elem_value *ucontrol)
  238. {
  239. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  240. int changed = 0;
  241. if (rt->digital_thru_switch != ucontrol->value.integer.value[0]) {
  242. rt->digital_thru_switch = ucontrol->value.integer.value[0];
  243. usb6fire_control_streaming_update(rt);
  244. changed = 1;
  245. }
  246. return changed;
  247. }
  248. static int usb6fire_control_digital_thru_get(struct snd_kcontrol *kcontrol,
  249. struct snd_ctl_elem_value *ucontrol)
  250. {
  251. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  252. ucontrol->value.integer.value[0] = rt->digital_thru_switch;
  253. return 0;
  254. }
  255. static struct __devinitdata snd_kcontrol_new elements[] = {
  256. {
  257. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  258. .name = "Master Playback Volume",
  259. .index = 0,
  260. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  261. .info = usb6fire_control_master_vol_info,
  262. .get = usb6fire_control_master_vol_get,
  263. .put = usb6fire_control_master_vol_put
  264. },
  265. {
  266. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  267. .name = "Line/Phono Capture Route",
  268. .index = 0,
  269. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  270. .info = usb6fire_control_line_phono_info,
  271. .get = usb6fire_control_line_phono_get,
  272. .put = usb6fire_control_line_phono_put
  273. },
  274. {
  275. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  276. .name = "Opt/Coax Capture Route",
  277. .index = 0,
  278. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  279. .info = usb6fire_control_opt_coax_info,
  280. .get = usb6fire_control_opt_coax_get,
  281. .put = usb6fire_control_opt_coax_put
  282. },
  283. {
  284. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  285. .name = "Digital Thru Playback Route",
  286. .index = 0,
  287. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  288. .info = snd_ctl_boolean_mono_info,
  289. .get = usb6fire_control_digital_thru_get,
  290. .put = usb6fire_control_digital_thru_put
  291. },
  292. {}
  293. };
  294. int __devinit usb6fire_control_init(struct sfire_chip *chip)
  295. {
  296. int i;
  297. int ret;
  298. struct control_runtime *rt = kzalloc(sizeof(struct control_runtime),
  299. GFP_KERNEL);
  300. struct comm_runtime *comm_rt = chip->comm;
  301. if (!rt)
  302. return -ENOMEM;
  303. rt->chip = chip;
  304. rt->update_streaming = usb6fire_control_streaming_update;
  305. rt->set_rate = usb6fire_control_set_rate;
  306. rt->set_channels = usb6fire_control_set_channels;
  307. i = 0;
  308. while (init_data[i].type) {
  309. comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg,
  310. init_data[i].value);
  311. i++;
  312. }
  313. usb6fire_control_opt_coax_update(rt);
  314. usb6fire_control_line_phono_update(rt);
  315. usb6fire_control_master_vol_update(rt);
  316. usb6fire_control_streaming_update(rt);
  317. i = 0;
  318. while (elements[i].name) {
  319. ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt));
  320. if (ret < 0) {
  321. kfree(rt);
  322. snd_printk(KERN_ERR PREFIX "cannot add control.\n");
  323. return ret;
  324. }
  325. i++;
  326. }
  327. chip->control = rt;
  328. return 0;
  329. }
  330. void usb6fire_control_abort(struct sfire_chip *chip)
  331. {}
  332. void usb6fire_control_destroy(struct sfire_chip *chip)
  333. {
  334. kfree(chip->control);
  335. chip->control = NULL;
  336. }