control.c 11 KB

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