control.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 <sound/tlv.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. * data that needs to be sent to device. sets up card internal stuff.
  25. * values dumped from windows driver and filtered by trial'n'error.
  26. */
  27. static const struct {
  28. u8 type;
  29. u8 reg;
  30. u8 value;
  31. }
  32. init_data[] = {
  33. { 0x22, 0x00, 0x00 }, { 0x20, 0x00, 0x08 }, { 0x22, 0x01, 0x01 },
  34. { 0x20, 0x01, 0x08 }, { 0x22, 0x02, 0x00 }, { 0x20, 0x02, 0x08 },
  35. { 0x22, 0x03, 0x00 }, { 0x20, 0x03, 0x08 }, { 0x22, 0x04, 0x00 },
  36. { 0x20, 0x04, 0x08 }, { 0x22, 0x05, 0x01 }, { 0x20, 0x05, 0x08 },
  37. { 0x22, 0x04, 0x01 }, { 0x12, 0x04, 0x00 }, { 0x12, 0x05, 0x00 },
  38. { 0x12, 0x0d, 0x78 }, { 0x12, 0x21, 0x82 }, { 0x12, 0x22, 0x80 },
  39. { 0x12, 0x23, 0x00 }, { 0x12, 0x06, 0x02 }, { 0x12, 0x03, 0x00 },
  40. { 0x12, 0x02, 0x00 }, { 0x22, 0x03, 0x01 },
  41. { 0 } /* TERMINATING ENTRY */
  42. };
  43. static const int rates_altsetting[] = { 1, 1, 2, 2, 3, 3 };
  44. /* values to write to soundcard register for all samplerates */
  45. static const u16 rates_6fire_vl[] = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01};
  46. static const u16 rates_6fire_vh[] = {0x11, 0x11, 0x10, 0x10, 0x00, 0x00};
  47. static DECLARE_TLV_DB_MINMAX(tlv_output, -9000, 0);
  48. enum {
  49. DIGITAL_THRU_ONLY_SAMPLERATE = 3
  50. };
  51. static void usb6fire_control_master_vol_update(struct control_runtime *rt)
  52. {
  53. struct comm_runtime *comm_rt = rt->chip->comm;
  54. if (comm_rt) {
  55. /* set volume */
  56. comm_rt->write8(comm_rt, 0x12, 0x0f, 180 - rt->master_vol);
  57. /* unmute */
  58. comm_rt->write8(comm_rt, 0x12, 0x0e, 0x00);
  59. }
  60. }
  61. static void usb6fire_control_line_phono_update(struct control_runtime *rt)
  62. {
  63. struct comm_runtime *comm_rt = rt->chip->comm;
  64. if (comm_rt) {
  65. comm_rt->write8(comm_rt, 0x22, 0x02, rt->line_phono_switch);
  66. comm_rt->write8(comm_rt, 0x21, 0x02, rt->line_phono_switch);
  67. }
  68. }
  69. static void usb6fire_control_opt_coax_update(struct control_runtime *rt)
  70. {
  71. struct comm_runtime *comm_rt = rt->chip->comm;
  72. if (comm_rt) {
  73. comm_rt->write8(comm_rt, 0x22, 0x00, rt->opt_coax_switch);
  74. comm_rt->write8(comm_rt, 0x21, 0x00, rt->opt_coax_switch);
  75. }
  76. }
  77. static int usb6fire_control_set_rate(struct control_runtime *rt, int rate)
  78. {
  79. int ret;
  80. struct usb_device *device = rt->chip->dev;
  81. struct comm_runtime *comm_rt = rt->chip->comm;
  82. if (rate < 0 || rate >= CONTROL_N_RATES)
  83. return -EINVAL;
  84. ret = usb_set_interface(device, 1, rates_altsetting[rate]);
  85. if (ret < 0)
  86. return ret;
  87. /* set soundcard clock */
  88. ret = comm_rt->write16(comm_rt, 0x02, 0x01, rates_6fire_vl[rate],
  89. rates_6fire_vh[rate]);
  90. if (ret < 0)
  91. return ret;
  92. return 0;
  93. }
  94. static int usb6fire_control_set_channels(
  95. struct control_runtime *rt, int n_analog_out,
  96. int n_analog_in, bool spdif_out, bool spdif_in)
  97. {
  98. int ret;
  99. struct comm_runtime *comm_rt = rt->chip->comm;
  100. /* enable analog inputs and outputs
  101. * (one bit per stereo-channel) */
  102. ret = comm_rt->write16(comm_rt, 0x02, 0x02,
  103. (1 << (n_analog_out / 2)) - 1,
  104. (1 << (n_analog_in / 2)) - 1);
  105. if (ret < 0)
  106. return ret;
  107. /* disable digital inputs and outputs */
  108. /* TODO: use spdif_x to enable/disable digital channels */
  109. ret = comm_rt->write16(comm_rt, 0x02, 0x03, 0x00, 0x00);
  110. if (ret < 0)
  111. return ret;
  112. return 0;
  113. }
  114. static int usb6fire_control_streaming_update(struct control_runtime *rt)
  115. {
  116. struct comm_runtime *comm_rt = rt->chip->comm;
  117. if (comm_rt) {
  118. if (!rt->usb_streaming && rt->digital_thru_switch)
  119. usb6fire_control_set_rate(rt,
  120. DIGITAL_THRU_ONLY_SAMPLERATE);
  121. return comm_rt->write16(comm_rt, 0x02, 0x00, 0x00,
  122. (rt->usb_streaming ? 0x01 : 0x00) |
  123. (rt->digital_thru_switch ? 0x08 : 0x00));
  124. }
  125. return -EINVAL;
  126. }
  127. static int usb6fire_control_master_vol_info(struct snd_kcontrol *kcontrol,
  128. struct snd_ctl_elem_info *uinfo)
  129. {
  130. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  131. uinfo->count = 1;
  132. uinfo->value.integer.min = 0;
  133. uinfo->value.integer.max = 180;
  134. return 0;
  135. }
  136. static int usb6fire_control_master_vol_put(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  140. int changed = 0;
  141. if (rt->master_vol != ucontrol->value.integer.value[0]) {
  142. rt->master_vol = ucontrol->value.integer.value[0];
  143. usb6fire_control_master_vol_update(rt);
  144. changed = 1;
  145. }
  146. return changed;
  147. }
  148. static int usb6fire_control_master_vol_get(struct snd_kcontrol *kcontrol,
  149. struct snd_ctl_elem_value *ucontrol)
  150. {
  151. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  152. ucontrol->value.integer.value[0] = rt->master_vol;
  153. return 0;
  154. }
  155. static int usb6fire_control_line_phono_info(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_info *uinfo)
  157. {
  158. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  159. uinfo->count = 1;
  160. uinfo->value.enumerated.items = 2;
  161. if (uinfo->value.enumerated.item > 1)
  162. uinfo->value.enumerated.item = 1;
  163. strcpy(uinfo->value.enumerated.name,
  164. line_phono_texts[uinfo->value.enumerated.item]);
  165. return 0;
  166. }
  167. static int usb6fire_control_line_phono_put(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_value *ucontrol)
  169. {
  170. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  171. int changed = 0;
  172. if (rt->line_phono_switch != ucontrol->value.integer.value[0]) {
  173. rt->line_phono_switch = ucontrol->value.integer.value[0];
  174. usb6fire_control_line_phono_update(rt);
  175. changed = 1;
  176. }
  177. return changed;
  178. }
  179. static int usb6fire_control_line_phono_get(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_value *ucontrol)
  181. {
  182. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  183. ucontrol->value.integer.value[0] = rt->line_phono_switch;
  184. return 0;
  185. }
  186. static int usb6fire_control_opt_coax_info(struct snd_kcontrol *kcontrol,
  187. struct snd_ctl_elem_info *uinfo)
  188. {
  189. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  190. uinfo->count = 1;
  191. uinfo->value.enumerated.items = 2;
  192. if (uinfo->value.enumerated.item > 1)
  193. uinfo->value.enumerated.item = 1;
  194. strcpy(uinfo->value.enumerated.name,
  195. opt_coax_texts[uinfo->value.enumerated.item]);
  196. return 0;
  197. }
  198. static int usb6fire_control_opt_coax_put(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_value *ucontrol)
  200. {
  201. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  202. int changed = 0;
  203. if (rt->opt_coax_switch != ucontrol->value.enumerated.item[0]) {
  204. rt->opt_coax_switch = ucontrol->value.enumerated.item[0];
  205. usb6fire_control_opt_coax_update(rt);
  206. changed = 1;
  207. }
  208. return changed;
  209. }
  210. static int usb6fire_control_opt_coax_get(struct snd_kcontrol *kcontrol,
  211. struct snd_ctl_elem_value *ucontrol)
  212. {
  213. struct control_runtime *rt = snd_kcontrol_chip(kcontrol);
  214. ucontrol->value.enumerated.item[0] = rt->opt_coax_switch;
  215. return 0;
  216. }
  217. static int usb6fire_control_digital_thru_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->digital_thru_switch != ucontrol->value.integer.value[0]) {
  223. rt->digital_thru_switch = ucontrol->value.integer.value[0];
  224. usb6fire_control_streaming_update(rt);
  225. changed = 1;
  226. }
  227. return changed;
  228. }
  229. static int usb6fire_control_digital_thru_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.integer.value[0] = rt->digital_thru_switch;
  234. return 0;
  235. }
  236. static struct __devinitdata snd_kcontrol_new elements[] = {
  237. {
  238. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  239. .name = "Master Playback Volume",
  240. .index = 0,
  241. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  242. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  243. .info = usb6fire_control_master_vol_info,
  244. .get = usb6fire_control_master_vol_get,
  245. .put = usb6fire_control_master_vol_put,
  246. .tlv = { .p = tlv_output }
  247. },
  248. {
  249. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  250. .name = "Line/Phono Capture Route",
  251. .index = 0,
  252. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  253. .info = usb6fire_control_line_phono_info,
  254. .get = usb6fire_control_line_phono_get,
  255. .put = usb6fire_control_line_phono_put
  256. },
  257. {
  258. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  259. .name = "Opt/Coax Capture Route",
  260. .index = 0,
  261. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  262. .info = usb6fire_control_opt_coax_info,
  263. .get = usb6fire_control_opt_coax_get,
  264. .put = usb6fire_control_opt_coax_put
  265. },
  266. {
  267. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  268. .name = "Digital Thru Playback Route",
  269. .index = 0,
  270. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  271. .info = snd_ctl_boolean_mono_info,
  272. .get = usb6fire_control_digital_thru_get,
  273. .put = usb6fire_control_digital_thru_put
  274. },
  275. {}
  276. };
  277. int __devinit usb6fire_control_init(struct sfire_chip *chip)
  278. {
  279. int i;
  280. int ret;
  281. struct control_runtime *rt = kzalloc(sizeof(struct control_runtime),
  282. GFP_KERNEL);
  283. struct comm_runtime *comm_rt = chip->comm;
  284. if (!rt)
  285. return -ENOMEM;
  286. rt->chip = chip;
  287. rt->update_streaming = usb6fire_control_streaming_update;
  288. rt->set_rate = usb6fire_control_set_rate;
  289. rt->set_channels = usb6fire_control_set_channels;
  290. i = 0;
  291. while (init_data[i].type) {
  292. comm_rt->write8(comm_rt, init_data[i].type, init_data[i].reg,
  293. init_data[i].value);
  294. i++;
  295. }
  296. usb6fire_control_opt_coax_update(rt);
  297. usb6fire_control_line_phono_update(rt);
  298. usb6fire_control_master_vol_update(rt);
  299. usb6fire_control_streaming_update(rt);
  300. i = 0;
  301. while (elements[i].name) {
  302. ret = snd_ctl_add(chip->card, snd_ctl_new1(&elements[i], rt));
  303. if (ret < 0) {
  304. kfree(rt);
  305. snd_printk(KERN_ERR PREFIX "cannot add control.\n");
  306. return ret;
  307. }
  308. i++;
  309. }
  310. chip->control = rt;
  311. return 0;
  312. }
  313. void usb6fire_control_abort(struct sfire_chip *chip)
  314. {}
  315. void usb6fire_control_destroy(struct sfire_chip *chip)
  316. {
  317. kfree(chip->control);
  318. chip->control = NULL;
  319. }