pcxhr_mixer.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. #define __NO_VERSION__
  2. /*
  3. * Driver for Digigram pcxhr compatible soundcards
  4. *
  5. * mixer callbacks
  6. *
  7. * Copyright (c) 2004 by Digigram <alsa@digigram.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <sound/driver.h>
  24. #include <linux/time.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <sound/core.h>
  28. #include "pcxhr.h"
  29. #include "pcxhr_hwdep.h"
  30. #include "pcxhr_core.h"
  31. #include <sound/control.h>
  32. #include <sound/asoundef.h>
  33. #include "pcxhr_mixer.h"
  34. #define PCXHR_ANALOG_CAPTURE_LEVEL_MIN 0 /* -96.0 dB */
  35. #define PCXHR_ANALOG_CAPTURE_LEVEL_MAX 255 /* +31.5 dB */
  36. #define PCXHR_ANALOG_CAPTURE_ZERO_LEVEL 224 /* +16.0 dB ( +31.5 dB - fix level +15.5 dB ) */
  37. #define PCXHR_ANALOG_PLAYBACK_LEVEL_MIN 0 /* -128.0 dB */
  38. #define PCXHR_ANALOG_PLAYBACK_LEVEL_MAX 128 /* 0.0 dB */
  39. #define PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL 104 /* -24.0 dB ( 0.0 dB - fix level +24.0 dB ) */
  40. static int pcxhr_update_analog_audio_level(struct snd_pcxhr *chip, int is_capture, int channel)
  41. {
  42. int err, vol;
  43. struct pcxhr_rmh rmh;
  44. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
  45. if (is_capture) {
  46. rmh.cmd[0] |= IO_NUM_REG_IN_ANA_LEVEL;
  47. rmh.cmd[2] = chip->analog_capture_volume[channel];
  48. } else {
  49. rmh.cmd[0] |= IO_NUM_REG_OUT_ANA_LEVEL;
  50. if (chip->analog_playback_active[channel])
  51. vol = chip->analog_playback_volume[channel];
  52. else
  53. vol = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN;
  54. rmh.cmd[2] = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX - vol; /* playback analog levels are inversed */
  55. }
  56. rmh.cmd[1] = 1 << ((2 * chip->chip_idx) + channel); /* audio mask */
  57. rmh.cmd_len = 3;
  58. err = pcxhr_send_msg(chip->mgr, &rmh);
  59. if (err < 0) {
  60. snd_printk(KERN_DEBUG "error update_analog_audio_level card(%d) "
  61. "is_capture(%d) err(%x)\n", chip->chip_idx, is_capture, err);
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. /*
  67. * analog level control
  68. */
  69. static int pcxhr_analog_vol_info(struct snd_kcontrol *kcontrol,
  70. struct snd_ctl_elem_info *uinfo)
  71. {
  72. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  73. uinfo->count = 2;
  74. if (kcontrol->private_value == 0) { /* playback */
  75. uinfo->value.integer.min = PCXHR_ANALOG_PLAYBACK_LEVEL_MIN; /* -128 dB */
  76. uinfo->value.integer.max = PCXHR_ANALOG_PLAYBACK_LEVEL_MAX; /* 0 dB */
  77. } else { /* capture */
  78. uinfo->value.integer.min = PCXHR_ANALOG_CAPTURE_LEVEL_MIN; /* -96 dB */
  79. uinfo->value.integer.max = PCXHR_ANALOG_CAPTURE_LEVEL_MAX; /* 31.5 dB */
  80. }
  81. return 0;
  82. }
  83. static int pcxhr_analog_vol_get(struct snd_kcontrol *kcontrol,
  84. struct snd_ctl_elem_value *ucontrol)
  85. {
  86. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  87. down(&chip->mgr->mixer_mutex);
  88. if (kcontrol->private_value == 0) { /* playback */
  89. ucontrol->value.integer.value[0] = chip->analog_playback_volume[0];
  90. ucontrol->value.integer.value[1] = chip->analog_playback_volume[1];
  91. } else { /* capture */
  92. ucontrol->value.integer.value[0] = chip->analog_capture_volume[0];
  93. ucontrol->value.integer.value[1] = chip->analog_capture_volume[1];
  94. }
  95. up(&chip->mgr->mixer_mutex);
  96. return 0;
  97. }
  98. static int pcxhr_analog_vol_put(struct snd_kcontrol *kcontrol,
  99. struct snd_ctl_elem_value *ucontrol)
  100. {
  101. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  102. int changed = 0;
  103. int is_capture, i;
  104. down(&chip->mgr->mixer_mutex);
  105. is_capture = (kcontrol->private_value != 0);
  106. for (i = 0; i < 2; i++) {
  107. int new_volume = ucontrol->value.integer.value[i];
  108. int* stored_volume = is_capture ? &chip->analog_capture_volume[i] :
  109. &chip->analog_playback_volume[i];
  110. if (*stored_volume != new_volume) {
  111. *stored_volume = new_volume;
  112. changed = 1;
  113. pcxhr_update_analog_audio_level(chip, is_capture, i);
  114. }
  115. }
  116. up(&chip->mgr->mixer_mutex);
  117. return changed;
  118. }
  119. static struct snd_kcontrol_new pcxhr_control_analog_level = {
  120. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  121. /* name will be filled later */
  122. .info = pcxhr_analog_vol_info,
  123. .get = pcxhr_analog_vol_get,
  124. .put = pcxhr_analog_vol_put,
  125. };
  126. /* shared */
  127. static int pcxhr_sw_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  128. {
  129. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  130. uinfo->count = 2;
  131. uinfo->value.integer.min = 0;
  132. uinfo->value.integer.max = 1;
  133. return 0;
  134. }
  135. static int pcxhr_audio_sw_get(struct snd_kcontrol *kcontrol,
  136. struct snd_ctl_elem_value *ucontrol)
  137. {
  138. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  139. down(&chip->mgr->mixer_mutex);
  140. ucontrol->value.integer.value[0] = chip->analog_playback_active[0];
  141. ucontrol->value.integer.value[1] = chip->analog_playback_active[1];
  142. up(&chip->mgr->mixer_mutex);
  143. return 0;
  144. }
  145. static int pcxhr_audio_sw_put(struct snd_kcontrol *kcontrol,
  146. struct snd_ctl_elem_value *ucontrol)
  147. {
  148. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  149. int i, changed = 0;
  150. down(&chip->mgr->mixer_mutex);
  151. for(i = 0; i < 2; i++) {
  152. if (chip->analog_playback_active[i] != ucontrol->value.integer.value[i]) {
  153. chip->analog_playback_active[i] = ucontrol->value.integer.value[i];
  154. changed = 1;
  155. pcxhr_update_analog_audio_level(chip, 0, i); /* update playback levels */
  156. }
  157. }
  158. up(&chip->mgr->mixer_mutex);
  159. return changed;
  160. }
  161. static struct snd_kcontrol_new pcxhr_control_output_switch = {
  162. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  163. .name = "Master Playback Switch",
  164. .info = pcxhr_sw_info, /* shared */
  165. .get = pcxhr_audio_sw_get,
  166. .put = pcxhr_audio_sw_put
  167. };
  168. #define PCXHR_DIGITAL_LEVEL_MIN 0x000 /* -110 dB */
  169. #define PCXHR_DIGITAL_LEVEL_MAX 0x1ff /* +18 dB */
  170. #define PCXHR_DIGITAL_ZERO_LEVEL 0x1b7 /* 0 dB */
  171. #define MORE_THAN_ONE_STREAM_LEVEL 0x000001
  172. #define VALID_STREAM_PAN_LEVEL_MASK 0x800000
  173. #define VALID_STREAM_LEVEL_MASK 0x400000
  174. #define VALID_STREAM_LEVEL_1_MASK 0x200000
  175. #define VALID_STREAM_LEVEL_2_MASK 0x100000
  176. static int pcxhr_update_playback_stream_level(struct snd_pcxhr* chip, int idx)
  177. {
  178. int err;
  179. struct pcxhr_rmh rmh;
  180. struct pcxhr_pipe *pipe = &chip->playback_pipe;
  181. int left, right;
  182. if (chip->digital_playback_active[idx][0])
  183. left = chip->digital_playback_volume[idx][0];
  184. else
  185. left = PCXHR_DIGITAL_LEVEL_MIN;
  186. if (chip->digital_playback_active[idx][1])
  187. right = chip->digital_playback_volume[idx][1];
  188. else
  189. right = PCXHR_DIGITAL_LEVEL_MIN;
  190. pcxhr_init_rmh(&rmh, CMD_STREAM_OUT_LEVEL_ADJUST);
  191. /* add pipe and stream mask */
  192. pcxhr_set_pipe_cmd_params(&rmh, 0, pipe->first_audio, 0, 1<<idx);
  193. /* volume left->left / right->right panoramic level */
  194. rmh.cmd[0] |= MORE_THAN_ONE_STREAM_LEVEL;
  195. rmh.cmd[2] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_1_MASK;
  196. rmh.cmd[2] |= (left << 10);
  197. rmh.cmd[3] = VALID_STREAM_PAN_LEVEL_MASK | VALID_STREAM_LEVEL_2_MASK;
  198. rmh.cmd[3] |= right;
  199. rmh.cmd_len = 4;
  200. err = pcxhr_send_msg(chip->mgr, &rmh);
  201. if (err < 0) {
  202. snd_printk(KERN_DEBUG "error update_playback_stream_level "
  203. "card(%d) err(%x)\n", chip->chip_idx, err);
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. #define AUDIO_IO_HAS_MUTE_LEVEL 0x400000
  209. #define AUDIO_IO_HAS_MUTE_MONITOR_1 0x200000
  210. #define VALID_AUDIO_IO_DIGITAL_LEVEL 0x000001
  211. #define VALID_AUDIO_IO_MONITOR_LEVEL 0x000002
  212. #define VALID_AUDIO_IO_MUTE_LEVEL 0x000004
  213. #define VALID_AUDIO_IO_MUTE_MONITOR_1 0x000008
  214. static int pcxhr_update_audio_pipe_level(struct snd_pcxhr* chip, int capture, int channel)
  215. {
  216. int err;
  217. struct pcxhr_rmh rmh;
  218. struct pcxhr_pipe *pipe;
  219. if (capture)
  220. pipe = &chip->capture_pipe[0];
  221. else
  222. pipe = &chip->playback_pipe;
  223. pcxhr_init_rmh(&rmh, CMD_AUDIO_LEVEL_ADJUST);
  224. /* add channel mask */
  225. pcxhr_set_pipe_cmd_params(&rmh, capture, 0, 0, 1 << (channel + pipe->first_audio));
  226. /* TODO : if mask (3 << pipe->first_audio) is used, left and right channel
  227. * will be programmed to the same params
  228. */
  229. if (capture) {
  230. rmh.cmd[0] |= VALID_AUDIO_IO_DIGITAL_LEVEL;
  231. /* VALID_AUDIO_IO_MUTE_LEVEL not yet handled (capture pipe level) */
  232. rmh.cmd[2] = chip->digital_capture_volume[channel];
  233. } else {
  234. rmh.cmd[0] |= VALID_AUDIO_IO_MONITOR_LEVEL | VALID_AUDIO_IO_MUTE_MONITOR_1;
  235. /* VALID_AUDIO_IO_DIGITAL_LEVEL and VALID_AUDIO_IO_MUTE_LEVEL not yet
  236. * handled (playback pipe level)
  237. */
  238. rmh.cmd[2] = chip->monitoring_volume[channel] << 10;
  239. if (chip->monitoring_active[channel] == 0)
  240. rmh.cmd[2] |= AUDIO_IO_HAS_MUTE_MONITOR_1;
  241. }
  242. rmh.cmd_len = 3;
  243. err = pcxhr_send_msg(chip->mgr, &rmh);
  244. if(err<0) {
  245. snd_printk(KERN_DEBUG "error update_audio_level card(%d) err(%x)\n",
  246. chip->chip_idx, err);
  247. return -EINVAL;
  248. }
  249. return 0;
  250. }
  251. /* shared */
  252. static int pcxhr_digital_vol_info(struct snd_kcontrol *kcontrol,
  253. struct snd_ctl_elem_info *uinfo)
  254. {
  255. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  256. uinfo->count = 2;
  257. uinfo->value.integer.min = PCXHR_DIGITAL_LEVEL_MIN; /* -109.5 dB */
  258. uinfo->value.integer.max = PCXHR_DIGITAL_LEVEL_MAX; /* 18.0 dB */
  259. return 0;
  260. }
  261. static int pcxhr_pcm_vol_get(struct snd_kcontrol *kcontrol,
  262. struct snd_ctl_elem_value *ucontrol)
  263. {
  264. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  265. int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
  266. int *stored_volume;
  267. int is_capture = kcontrol->private_value;
  268. down(&chip->mgr->mixer_mutex);
  269. if (is_capture)
  270. stored_volume = chip->digital_capture_volume; /* digital capture */
  271. else
  272. stored_volume = chip->digital_playback_volume[idx]; /* digital playback */
  273. ucontrol->value.integer.value[0] = stored_volume[0];
  274. ucontrol->value.integer.value[1] = stored_volume[1];
  275. up(&chip->mgr->mixer_mutex);
  276. return 0;
  277. }
  278. static int pcxhr_pcm_vol_put(struct snd_kcontrol *kcontrol,
  279. struct snd_ctl_elem_value *ucontrol)
  280. {
  281. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  282. int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
  283. int changed = 0;
  284. int is_capture = kcontrol->private_value;
  285. int *stored_volume;
  286. int i;
  287. down(&chip->mgr->mixer_mutex);
  288. if (is_capture)
  289. stored_volume = chip->digital_capture_volume; /* digital capture */
  290. else
  291. stored_volume = chip->digital_playback_volume[idx]; /* digital playback */
  292. for (i = 0; i < 2; i++) {
  293. if (stored_volume[i] != ucontrol->value.integer.value[i]) {
  294. stored_volume[i] = ucontrol->value.integer.value[i];
  295. changed = 1;
  296. if (is_capture) /* update capture volume */
  297. pcxhr_update_audio_pipe_level(chip, 1, i);
  298. }
  299. }
  300. if (! is_capture && changed)
  301. pcxhr_update_playback_stream_level(chip, idx); /* update playback volume */
  302. up(&chip->mgr->mixer_mutex);
  303. return changed;
  304. }
  305. static struct snd_kcontrol_new snd_pcxhr_pcm_vol =
  306. {
  307. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  308. /* name will be filled later */
  309. /* count will be filled later */
  310. .info = pcxhr_digital_vol_info, /* shared */
  311. .get = pcxhr_pcm_vol_get,
  312. .put = pcxhr_pcm_vol_put,
  313. };
  314. static int pcxhr_pcm_sw_get(struct snd_kcontrol *kcontrol,
  315. struct snd_ctl_elem_value *ucontrol)
  316. {
  317. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  318. int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
  319. down(&chip->mgr->mixer_mutex);
  320. ucontrol->value.integer.value[0] = chip->digital_playback_active[idx][0];
  321. ucontrol->value.integer.value[1] = chip->digital_playback_active[idx][1];
  322. up(&chip->mgr->mixer_mutex);
  323. return 0;
  324. }
  325. static int pcxhr_pcm_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  326. {
  327. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  328. int changed = 0;
  329. int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); /* index */
  330. int i, j;
  331. down(&chip->mgr->mixer_mutex);
  332. j = idx;
  333. for (i = 0; i < 2; i++) {
  334. if (chip->digital_playback_active[j][i] != ucontrol->value.integer.value[i]) {
  335. chip->digital_playback_active[j][i] = ucontrol->value.integer.value[i];
  336. changed = 1;
  337. }
  338. }
  339. if (changed)
  340. pcxhr_update_playback_stream_level(chip, idx);
  341. up(&chip->mgr->mixer_mutex);
  342. return changed;
  343. }
  344. static struct snd_kcontrol_new pcxhr_control_pcm_switch = {
  345. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  346. .name = "PCM Playback Switch",
  347. .count = PCXHR_PLAYBACK_STREAMS,
  348. .info = pcxhr_sw_info, /* shared */
  349. .get = pcxhr_pcm_sw_get,
  350. .put = pcxhr_pcm_sw_put
  351. };
  352. /*
  353. * monitoring level control
  354. */
  355. static int pcxhr_monitor_vol_get(struct snd_kcontrol *kcontrol,
  356. struct snd_ctl_elem_value *ucontrol)
  357. {
  358. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  359. down(&chip->mgr->mixer_mutex);
  360. ucontrol->value.integer.value[0] = chip->monitoring_volume[0];
  361. ucontrol->value.integer.value[1] = chip->monitoring_volume[1];
  362. up(&chip->mgr->mixer_mutex);
  363. return 0;
  364. }
  365. static int pcxhr_monitor_vol_put(struct snd_kcontrol *kcontrol,
  366. struct snd_ctl_elem_value *ucontrol)
  367. {
  368. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  369. int changed = 0;
  370. int i;
  371. down(&chip->mgr->mixer_mutex);
  372. for (i = 0; i < 2; i++) {
  373. if (chip->monitoring_volume[i] != ucontrol->value.integer.value[i]) {
  374. chip->monitoring_volume[i] = ucontrol->value.integer.value[i];
  375. if(chip->monitoring_active[i]) /* do only when monitoring is unmuted */
  376. /* update monitoring volume and mute */
  377. pcxhr_update_audio_pipe_level(chip, 0, i);
  378. changed = 1;
  379. }
  380. }
  381. up(&chip->mgr->mixer_mutex);
  382. return changed;
  383. }
  384. static struct snd_kcontrol_new pcxhr_control_monitor_vol = {
  385. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  386. .name = "Monitoring Volume",
  387. .info = pcxhr_digital_vol_info, /* shared */
  388. .get = pcxhr_monitor_vol_get,
  389. .put = pcxhr_monitor_vol_put,
  390. };
  391. /*
  392. * monitoring switch control
  393. */
  394. static int pcxhr_monitor_sw_get(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  398. down(&chip->mgr->mixer_mutex);
  399. ucontrol->value.integer.value[0] = chip->monitoring_active[0];
  400. ucontrol->value.integer.value[1] = chip->monitoring_active[1];
  401. up(&chip->mgr->mixer_mutex);
  402. return 0;
  403. }
  404. static int pcxhr_monitor_sw_put(struct snd_kcontrol *kcontrol,
  405. struct snd_ctl_elem_value *ucontrol)
  406. {
  407. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  408. int changed = 0;
  409. int i;
  410. down(&chip->mgr->mixer_mutex);
  411. for (i = 0; i < 2; i++) {
  412. if (chip->monitoring_active[i] != ucontrol->value.integer.value[i]) {
  413. chip->monitoring_active[i] = ucontrol->value.integer.value[i];
  414. changed |= (1<<i); /* mask 0x01 and 0x02 */
  415. }
  416. }
  417. if(changed & 0x01)
  418. /* update left monitoring volume and mute */
  419. pcxhr_update_audio_pipe_level(chip, 0, 0);
  420. if(changed & 0x02)
  421. /* update right monitoring volume and mute */
  422. pcxhr_update_audio_pipe_level(chip, 0, 1);
  423. up(&chip->mgr->mixer_mutex);
  424. return (changed != 0);
  425. }
  426. static struct snd_kcontrol_new pcxhr_control_monitor_sw = {
  427. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  428. .name = "Monitoring Switch",
  429. .info = pcxhr_sw_info, /* shared */
  430. .get = pcxhr_monitor_sw_get,
  431. .put = pcxhr_monitor_sw_put
  432. };
  433. /*
  434. * audio source select
  435. */
  436. #define PCXHR_SOURCE_AUDIO01_UER 0x000100
  437. #define PCXHR_SOURCE_AUDIO01_SYNC 0x000200
  438. #define PCXHR_SOURCE_AUDIO23_UER 0x000400
  439. #define PCXHR_SOURCE_AUDIO45_UER 0x001000
  440. #define PCXHR_SOURCE_AUDIO67_UER 0x040000
  441. static int pcxhr_set_audio_source(struct snd_pcxhr* chip)
  442. {
  443. struct pcxhr_rmh rmh;
  444. unsigned int mask, reg;
  445. unsigned int codec;
  446. int err, use_src, changed;
  447. switch (chip->chip_idx) {
  448. case 0 : mask = PCXHR_SOURCE_AUDIO01_UER; codec = CS8420_01_CS; break;
  449. case 1 : mask = PCXHR_SOURCE_AUDIO23_UER; codec = CS8420_23_CS; break;
  450. case 2 : mask = PCXHR_SOURCE_AUDIO45_UER; codec = CS8420_45_CS; break;
  451. case 3 : mask = PCXHR_SOURCE_AUDIO67_UER; codec = CS8420_67_CS; break;
  452. default: return -EINVAL;
  453. }
  454. reg = 0; /* audio source from analog plug */
  455. use_src = 0; /* do not activate codec SRC */
  456. if (chip->audio_capture_source != 0) {
  457. reg = mask; /* audio source from digital plug */
  458. if (chip->audio_capture_source == 2)
  459. use_src = 1;
  460. }
  461. /* set the input source */
  462. pcxhr_write_io_num_reg_cont(chip->mgr, mask, reg, &changed);
  463. /* resync them (otherwise channel inversion possible) */
  464. if (changed) {
  465. pcxhr_init_rmh(&rmh, CMD_RESYNC_AUDIO_INPUTS);
  466. rmh.cmd[0] |= (1 << chip->chip_idx);
  467. err = pcxhr_send_msg(chip->mgr, &rmh);
  468. if (err)
  469. return err;
  470. }
  471. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); /* set codec SRC on off */
  472. rmh.cmd_len = 3;
  473. rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
  474. rmh.cmd[1] = codec;
  475. rmh.cmd[2] = (CS8420_DATA_FLOW_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x54);
  476. err = pcxhr_send_msg(chip->mgr, &rmh);
  477. if(err)
  478. return err;
  479. rmh.cmd[2] = (CS8420_CLOCK_SRC_CTL & CHIP_SIG_AND_MAP_SPI) | (use_src ? 0x41 : 0x49);
  480. err = pcxhr_send_msg(chip->mgr, &rmh);
  481. return err;
  482. }
  483. static int pcxhr_audio_src_info(struct snd_kcontrol *kcontrol,
  484. struct snd_ctl_elem_info *uinfo)
  485. {
  486. static char *texts[3] = {"Analog", "Digital", "Digi+SRC"};
  487. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  488. uinfo->count = 1;
  489. uinfo->value.enumerated.items = 3;
  490. if (uinfo->value.enumerated.item > 2)
  491. uinfo->value.enumerated.item = 2;
  492. strcpy(uinfo->value.enumerated.name,
  493. texts[uinfo->value.enumerated.item]);
  494. return 0;
  495. }
  496. static int pcxhr_audio_src_get(struct snd_kcontrol *kcontrol,
  497. struct snd_ctl_elem_value *ucontrol)
  498. {
  499. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  500. ucontrol->value.enumerated.item[0] = chip->audio_capture_source;
  501. return 0;
  502. }
  503. static int pcxhr_audio_src_put(struct snd_kcontrol *kcontrol,
  504. struct snd_ctl_elem_value *ucontrol)
  505. {
  506. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  507. int ret = 0;
  508. down(&chip->mgr->mixer_mutex);
  509. if (chip->audio_capture_source != ucontrol->value.enumerated.item[0]) {
  510. chip->audio_capture_source = ucontrol->value.enumerated.item[0];
  511. pcxhr_set_audio_source(chip);
  512. ret = 1;
  513. }
  514. up(&chip->mgr->mixer_mutex);
  515. return ret;
  516. }
  517. static struct snd_kcontrol_new pcxhr_control_audio_src = {
  518. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  519. .name = "Capture Source",
  520. .info = pcxhr_audio_src_info,
  521. .get = pcxhr_audio_src_get,
  522. .put = pcxhr_audio_src_put,
  523. };
  524. /*
  525. * clock type selection
  526. * enum pcxhr_clock_type {
  527. * PCXHR_CLOCK_TYPE_INTERNAL = 0,
  528. * PCXHR_CLOCK_TYPE_WORD_CLOCK,
  529. * PCXHR_CLOCK_TYPE_AES_SYNC,
  530. * PCXHR_CLOCK_TYPE_AES_1,
  531. * PCXHR_CLOCK_TYPE_AES_2,
  532. * PCXHR_CLOCK_TYPE_AES_3,
  533. * PCXHR_CLOCK_TYPE_AES_4,
  534. * };
  535. */
  536. static int pcxhr_clock_type_info(struct snd_kcontrol *kcontrol,
  537. struct snd_ctl_elem_info *uinfo)
  538. {
  539. static char *texts[7] = {
  540. "Internal", "WordClock", "AES Sync", "AES 1", "AES 2", "AES 3", "AES 4"
  541. };
  542. struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
  543. int clock_items = 3 + mgr->capture_chips;
  544. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  545. uinfo->count = 1;
  546. uinfo->value.enumerated.items = clock_items;
  547. if (uinfo->value.enumerated.item >= clock_items)
  548. uinfo->value.enumerated.item = clock_items-1;
  549. strcpy(uinfo->value.enumerated.name,
  550. texts[uinfo->value.enumerated.item]);
  551. return 0;
  552. }
  553. static int pcxhr_clock_type_get(struct snd_kcontrol *kcontrol,
  554. struct snd_ctl_elem_value *ucontrol)
  555. {
  556. struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
  557. ucontrol->value.enumerated.item[0] = mgr->use_clock_type;
  558. return 0;
  559. }
  560. static int pcxhr_clock_type_put(struct snd_kcontrol *kcontrol,
  561. struct snd_ctl_elem_value *ucontrol)
  562. {
  563. struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
  564. int rate, ret = 0;
  565. down(&mgr->mixer_mutex);
  566. if (mgr->use_clock_type != ucontrol->value.enumerated.item[0]) {
  567. down(&mgr->setup_mutex);
  568. mgr->use_clock_type = ucontrol->value.enumerated.item[0];
  569. if (mgr->use_clock_type)
  570. pcxhr_get_external_clock(mgr, mgr->use_clock_type, &rate);
  571. else
  572. rate = mgr->sample_rate;
  573. if (rate) {
  574. pcxhr_set_clock(mgr, rate);
  575. if (mgr->sample_rate)
  576. mgr->sample_rate = rate;
  577. }
  578. up(&mgr->setup_mutex);
  579. ret = 1; /* return 1 even if the set was not done. ok ? */
  580. }
  581. up(&mgr->mixer_mutex);
  582. return ret;
  583. }
  584. static struct snd_kcontrol_new pcxhr_control_clock_type = {
  585. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  586. .name = "Clock Mode",
  587. .info = pcxhr_clock_type_info,
  588. .get = pcxhr_clock_type_get,
  589. .put = pcxhr_clock_type_put,
  590. };
  591. /*
  592. * clock rate control
  593. * specific control that scans the sample rates on the external plugs
  594. */
  595. static int pcxhr_clock_rate_info(struct snd_kcontrol *kcontrol,
  596. struct snd_ctl_elem_info *uinfo)
  597. {
  598. struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
  599. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  600. uinfo->count = 3 + mgr->capture_chips;
  601. uinfo->value.integer.min = 0; /* clock not present */
  602. uinfo->value.integer.max = 192000; /* max sample rate 192 kHz */
  603. return 0;
  604. }
  605. static int pcxhr_clock_rate_get(struct snd_kcontrol *kcontrol,
  606. struct snd_ctl_elem_value *ucontrol)
  607. {
  608. struct pcxhr_mgr *mgr = snd_kcontrol_chip(kcontrol);
  609. int i, err, rate;
  610. down(&mgr->mixer_mutex);
  611. for(i = 0; i < 3 + mgr->capture_chips; i++) {
  612. if (i == PCXHR_CLOCK_TYPE_INTERNAL)
  613. rate = mgr->sample_rate_real;
  614. else {
  615. err = pcxhr_get_external_clock(mgr, i, &rate);
  616. if (err)
  617. break;
  618. }
  619. ucontrol->value.integer.value[i] = rate;
  620. }
  621. up(&mgr->mixer_mutex);
  622. return 0;
  623. }
  624. static struct snd_kcontrol_new pcxhr_control_clock_rate = {
  625. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  626. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  627. .name = "Clock Rates",
  628. .info = pcxhr_clock_rate_info,
  629. .get = pcxhr_clock_rate_get,
  630. };
  631. /*
  632. * IEC958 status bits
  633. */
  634. static int pcxhr_iec958_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  635. {
  636. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  637. uinfo->count = 1;
  638. return 0;
  639. }
  640. static int pcxhr_iec958_capture_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char* aes_bits)
  641. {
  642. int i, err;
  643. unsigned char temp;
  644. struct pcxhr_rmh rmh;
  645. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
  646. rmh.cmd[0] |= IO_NUM_UER_CHIP_REG;
  647. switch (chip->chip_idx) {
  648. case 0: rmh.cmd[1] = CS8420_01_CS; break; /* use CS8416_01_CS for AES SYNC plug */
  649. case 1: rmh.cmd[1] = CS8420_23_CS; break;
  650. case 2: rmh.cmd[1] = CS8420_45_CS; break;
  651. case 3: rmh.cmd[1] = CS8420_67_CS; break;
  652. default: return -EINVAL;
  653. }
  654. switch (aes_idx) {
  655. case 0: rmh.cmd[2] = CS8420_CSB0; break; /* use CS8416_CSBx for AES SYNC plug */
  656. case 1: rmh.cmd[2] = CS8420_CSB1; break;
  657. case 2: rmh.cmd[2] = CS8420_CSB2; break;
  658. case 3: rmh.cmd[2] = CS8420_CSB3; break;
  659. case 4: rmh.cmd[2] = CS8420_CSB4; break;
  660. default: return -EINVAL;
  661. }
  662. rmh.cmd[1] &= 0x0fffff; /* size and code the chip id for the fpga */
  663. rmh.cmd[2] &= CHIP_SIG_AND_MAP_SPI; /* chip signature + map for spi read */
  664. rmh.cmd_len = 3;
  665. err = pcxhr_send_msg(chip->mgr, &rmh);
  666. if (err)
  667. return err;
  668. temp = 0;
  669. for (i = 0; i < 8; i++) {
  670. /* attention : reversed bit order (not with CS8416_01_CS) */
  671. temp <<= 1;
  672. if (rmh.stat[1] & (1 << i))
  673. temp |= 1;
  674. }
  675. snd_printdd("read iec958 AES %d byte %d = 0x%x\n", chip->chip_idx, aes_idx, temp);
  676. *aes_bits = temp;
  677. return 0;
  678. }
  679. static int pcxhr_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  680. {
  681. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  682. unsigned char aes_bits;
  683. int i, err;
  684. down(&chip->mgr->mixer_mutex);
  685. for(i = 0; i < 5; i++) {
  686. if (kcontrol->private_value == 0) /* playback */
  687. aes_bits = chip->aes_bits[i];
  688. else { /* capture */
  689. err = pcxhr_iec958_capture_byte(chip, i, &aes_bits);
  690. if (err)
  691. break;
  692. }
  693. ucontrol->value.iec958.status[i] = aes_bits;
  694. }
  695. up(&chip->mgr->mixer_mutex);
  696. return 0;
  697. }
  698. static int pcxhr_iec958_mask_get(struct snd_kcontrol *kcontrol,
  699. struct snd_ctl_elem_value *ucontrol)
  700. {
  701. int i;
  702. for (i = 0; i < 5; i++)
  703. ucontrol->value.iec958.status[i] = 0xff;
  704. return 0;
  705. }
  706. static int pcxhr_iec958_update_byte(struct snd_pcxhr *chip, int aes_idx, unsigned char aes_bits)
  707. {
  708. int i, err, cmd;
  709. unsigned char new_bits = aes_bits;
  710. unsigned char old_bits = chip->aes_bits[aes_idx];
  711. struct pcxhr_rmh rmh;
  712. for (i = 0; i < 8; i++) {
  713. if ((old_bits & 0x01) != (new_bits & 0x01)) {
  714. cmd = chip->chip_idx & 0x03; /* chip index 0..3 */
  715. if(chip->chip_idx > 3)
  716. /* new bit used if chip_idx>3 (PCX1222HR) */
  717. cmd |= 1 << 22;
  718. cmd |= ((aes_idx << 3) + i) << 2; /* add bit offset */
  719. cmd |= (new_bits & 0x01) << 23; /* add bit value */
  720. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
  721. rmh.cmd[0] |= IO_NUM_REG_CUER;
  722. rmh.cmd[1] = cmd;
  723. rmh.cmd_len = 2;
  724. snd_printdd("write iec958 AES %d byte %d bit %d (cmd %x)\n",
  725. chip->chip_idx, aes_idx, i, cmd);
  726. err = pcxhr_send_msg(chip->mgr, &rmh);
  727. if (err)
  728. return err;
  729. }
  730. old_bits >>= 1;
  731. new_bits >>= 1;
  732. }
  733. chip->aes_bits[aes_idx] = aes_bits;
  734. return 0;
  735. }
  736. static int pcxhr_iec958_put(struct snd_kcontrol *kcontrol,
  737. struct snd_ctl_elem_value *ucontrol)
  738. {
  739. struct snd_pcxhr *chip = snd_kcontrol_chip(kcontrol);
  740. int i, changed = 0;
  741. /* playback */
  742. down(&chip->mgr->mixer_mutex);
  743. for (i = 0; i < 5; i++) {
  744. if (ucontrol->value.iec958.status[i] != chip->aes_bits[i]) {
  745. pcxhr_iec958_update_byte(chip, i, ucontrol->value.iec958.status[i]);
  746. changed = 1;
  747. }
  748. }
  749. up(&chip->mgr->mixer_mutex);
  750. return changed;
  751. }
  752. static struct snd_kcontrol_new pcxhr_control_playback_iec958_mask = {
  753. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  754. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  755. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  756. .info = pcxhr_iec958_info,
  757. .get = pcxhr_iec958_mask_get
  758. };
  759. static struct snd_kcontrol_new pcxhr_control_playback_iec958 = {
  760. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  761. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  762. .info = pcxhr_iec958_info,
  763. .get = pcxhr_iec958_get,
  764. .put = pcxhr_iec958_put,
  765. .private_value = 0 /* playback */
  766. };
  767. static struct snd_kcontrol_new pcxhr_control_capture_iec958_mask = {
  768. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  769. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  770. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
  771. .info = pcxhr_iec958_info,
  772. .get = pcxhr_iec958_mask_get
  773. };
  774. static struct snd_kcontrol_new pcxhr_control_capture_iec958 = {
  775. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  776. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  777. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  778. .info = pcxhr_iec958_info,
  779. .get = pcxhr_iec958_get,
  780. .private_value = 1 /* capture */
  781. };
  782. static void pcxhr_init_audio_levels(struct snd_pcxhr *chip)
  783. {
  784. int i;
  785. for (i = 0; i < 2; i++) {
  786. if (chip->nb_streams_play) {
  787. int j;
  788. /* at boot time the digital volumes are unmuted 0dB */
  789. for (j = 0; j < PCXHR_PLAYBACK_STREAMS; j++) {
  790. chip->digital_playback_active[j][i] = 1;
  791. chip->digital_playback_volume[j][i] = PCXHR_DIGITAL_ZERO_LEVEL;
  792. }
  793. /* after boot, only two bits are set on the uer interface */
  794. chip->aes_bits[0] = IEC958_AES0_PROFESSIONAL | IEC958_AES0_PRO_FS_48000;
  795. /* only for test purpose, remove later */
  796. #ifdef CONFIG_SND_DEBUG
  797. /* analog volumes for playback (is LEVEL_MIN after boot) */
  798. chip->analog_playback_active[i] = 1;
  799. chip->analog_playback_volume[i] = PCXHR_ANALOG_PLAYBACK_ZERO_LEVEL;
  800. pcxhr_update_analog_audio_level(chip, 0, i);
  801. #endif
  802. /* test end */
  803. }
  804. if (chip->nb_streams_capt) {
  805. /* at boot time the digital volumes are unmuted 0dB */
  806. chip->digital_capture_volume[i] = PCXHR_DIGITAL_ZERO_LEVEL;
  807. /* only for test purpose, remove later */
  808. #ifdef CONFIG_SND_DEBUG
  809. /* analog volumes for playback (is LEVEL_MIN after boot) */
  810. chip->analog_capture_volume[i] = PCXHR_ANALOG_CAPTURE_ZERO_LEVEL;
  811. pcxhr_update_analog_audio_level(chip, 1, i);
  812. #endif
  813. /* test end */
  814. }
  815. }
  816. return;
  817. }
  818. int pcxhr_create_mixer(struct pcxhr_mgr *mgr)
  819. {
  820. struct snd_pcxhr *chip;
  821. int err, i;
  822. init_MUTEX(&mgr->mixer_mutex); /* can be in another place */
  823. for (i = 0; i < mgr->num_cards; i++) {
  824. struct snd_kcontrol_new temp;
  825. chip = mgr->chip[i];
  826. if (chip->nb_streams_play) {
  827. /* analog output level control */
  828. temp = pcxhr_control_analog_level;
  829. temp.name = "Master Playback Volume";
  830. temp.private_value = 0; /* playback */
  831. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
  832. return err;
  833. /* output mute controls */
  834. if ((err = snd_ctl_add(chip->card,
  835. snd_ctl_new1(&pcxhr_control_output_switch,
  836. chip))) < 0)
  837. return err;
  838. temp = snd_pcxhr_pcm_vol;
  839. temp.name = "PCM Playback Volume";
  840. temp.count = PCXHR_PLAYBACK_STREAMS;
  841. temp.private_value = 0; /* playback */
  842. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
  843. return err;
  844. if ((err = snd_ctl_add(chip->card,
  845. snd_ctl_new1(&pcxhr_control_pcm_switch,
  846. chip))) < 0)
  847. return err;
  848. /* IEC958 controls */
  849. if ((err = snd_ctl_add(chip->card,
  850. snd_ctl_new1(&pcxhr_control_playback_iec958_mask,
  851. chip))) < 0)
  852. return err;
  853. if ((err = snd_ctl_add(chip->card,
  854. snd_ctl_new1(&pcxhr_control_playback_iec958,
  855. chip))) < 0)
  856. return err;
  857. }
  858. if (chip->nb_streams_capt) {
  859. /* analog input level control only on first two chips !*/
  860. temp = pcxhr_control_analog_level;
  861. temp.name = "Master Capture Volume";
  862. temp.private_value = 1; /* capture */
  863. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
  864. return err;
  865. temp = snd_pcxhr_pcm_vol;
  866. temp.name = "PCM Capture Volume";
  867. temp.count = 1;
  868. temp.private_value = 1; /* capture */
  869. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&temp, chip))) < 0)
  870. return err;
  871. /* Audio source */
  872. if ((err = snd_ctl_add(chip->card,
  873. snd_ctl_new1(&pcxhr_control_audio_src,
  874. chip))) < 0)
  875. return err;
  876. /* IEC958 controls */
  877. if ((err = snd_ctl_add(chip->card,
  878. snd_ctl_new1(&pcxhr_control_capture_iec958_mask,
  879. chip))) < 0)
  880. return err;
  881. if ((err = snd_ctl_add(chip->card,
  882. snd_ctl_new1(&pcxhr_control_capture_iec958,
  883. chip))) < 0)
  884. return err;
  885. }
  886. /* monitoring only if playback and capture device available */
  887. if (chip->nb_streams_capt > 0 && chip->nb_streams_play > 0) {
  888. /* monitoring */
  889. if ((err = snd_ctl_add(chip->card,
  890. snd_ctl_new1(&pcxhr_control_monitor_vol,
  891. chip))) < 0)
  892. return err;
  893. if ((err = snd_ctl_add(chip->card,
  894. snd_ctl_new1(&pcxhr_control_monitor_sw,
  895. chip))) < 0)
  896. return err;
  897. }
  898. if (i == 0) {
  899. /* clock mode only one control per pcxhr */
  900. if ((err = snd_ctl_add(chip->card,
  901. snd_ctl_new1(&pcxhr_control_clock_type,
  902. mgr))) < 0)
  903. return err;
  904. /* non standard control used to scan the external clock presence/frequencies */
  905. if ((err = snd_ctl_add(chip->card,
  906. snd_ctl_new1(&pcxhr_control_clock_rate,
  907. mgr))) < 0)
  908. return err;
  909. }
  910. /* init values for the mixer data */
  911. pcxhr_init_audio_levels(chip);
  912. }
  913. return 0;
  914. }