oxygen_mixer.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * C-Media CMI8788 driver - mixer code
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2.
  9. *
  10. * This driver is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this driver; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/mutex.h>
  20. #include <sound/ac97_codec.h>
  21. #include <sound/asoundef.h>
  22. #include <sound/control.h>
  23. #include <sound/tlv.h>
  24. #include "oxygen.h"
  25. #include "cm9780.h"
  26. static int dac_volume_info(struct snd_kcontrol *ctl,
  27. struct snd_ctl_elem_info *info)
  28. {
  29. struct oxygen *chip = ctl->private_data;
  30. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  31. info->count = chip->model.dac_channels_mixer;
  32. info->value.integer.min = chip->model.dac_volume_min;
  33. info->value.integer.max = chip->model.dac_volume_max;
  34. return 0;
  35. }
  36. static int dac_volume_get(struct snd_kcontrol *ctl,
  37. struct snd_ctl_elem_value *value)
  38. {
  39. struct oxygen *chip = ctl->private_data;
  40. unsigned int i;
  41. mutex_lock(&chip->mutex);
  42. for (i = 0; i < chip->model.dac_channels_mixer; ++i)
  43. value->value.integer.value[i] = chip->dac_volume[i];
  44. mutex_unlock(&chip->mutex);
  45. return 0;
  46. }
  47. static int dac_volume_put(struct snd_kcontrol *ctl,
  48. struct snd_ctl_elem_value *value)
  49. {
  50. struct oxygen *chip = ctl->private_data;
  51. unsigned int i;
  52. int changed;
  53. changed = 0;
  54. mutex_lock(&chip->mutex);
  55. for (i = 0; i < chip->model.dac_channels_mixer; ++i)
  56. if (value->value.integer.value[i] != chip->dac_volume[i]) {
  57. chip->dac_volume[i] = value->value.integer.value[i];
  58. changed = 1;
  59. }
  60. if (changed)
  61. chip->model.update_dac_volume(chip);
  62. mutex_unlock(&chip->mutex);
  63. return changed;
  64. }
  65. static int dac_mute_get(struct snd_kcontrol *ctl,
  66. struct snd_ctl_elem_value *value)
  67. {
  68. struct oxygen *chip = ctl->private_data;
  69. mutex_lock(&chip->mutex);
  70. value->value.integer.value[0] = !chip->dac_mute;
  71. mutex_unlock(&chip->mutex);
  72. return 0;
  73. }
  74. static int dac_mute_put(struct snd_kcontrol *ctl,
  75. struct snd_ctl_elem_value *value)
  76. {
  77. struct oxygen *chip = ctl->private_data;
  78. int changed;
  79. mutex_lock(&chip->mutex);
  80. changed = (!value->value.integer.value[0]) != chip->dac_mute;
  81. if (changed) {
  82. chip->dac_mute = !value->value.integer.value[0];
  83. chip->model.update_dac_mute(chip);
  84. }
  85. mutex_unlock(&chip->mutex);
  86. return changed;
  87. }
  88. static unsigned int upmix_item_count(struct oxygen *chip)
  89. {
  90. if (chip->model.dac_channels_pcm < 8)
  91. return 2;
  92. else if (chip->model.update_center_lfe_mix)
  93. return 5;
  94. else
  95. return 3;
  96. }
  97. static int upmix_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
  98. {
  99. static const char *const names[5] = {
  100. "Front",
  101. "Front+Surround",
  102. "Front+Surround+Back",
  103. "Front+Surround+Center/LFE",
  104. "Front+Surround+Center/LFE+Back",
  105. };
  106. struct oxygen *chip = ctl->private_data;
  107. unsigned int count = upmix_item_count(chip);
  108. return snd_ctl_enum_info(info, 1, count, names);
  109. }
  110. static int upmix_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
  111. {
  112. struct oxygen *chip = ctl->private_data;
  113. mutex_lock(&chip->mutex);
  114. value->value.enumerated.item[0] = chip->dac_routing;
  115. mutex_unlock(&chip->mutex);
  116. return 0;
  117. }
  118. void oxygen_update_dac_routing(struct oxygen *chip)
  119. {
  120. /* DAC 0: front, DAC 1: surround, DAC 2: center/LFE, DAC 3: back */
  121. static const unsigned int reg_values[5] = {
  122. /* stereo -> front */
  123. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  124. (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  125. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  126. (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
  127. /* stereo -> front+surround */
  128. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  129. (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  130. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  131. (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
  132. /* stereo -> front+surround+back */
  133. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  134. (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  135. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  136. (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
  137. /* stereo -> front+surround+center/LFE */
  138. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  139. (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  140. (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  141. (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
  142. /* stereo -> front+surround+center/LFE+back */
  143. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  144. (0 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  145. (0 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  146. (0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT),
  147. };
  148. u8 channels;
  149. unsigned int reg_value;
  150. channels = oxygen_read8(chip, OXYGEN_PLAY_CHANNELS) &
  151. OXYGEN_PLAY_CHANNELS_MASK;
  152. if (channels == OXYGEN_PLAY_CHANNELS_2)
  153. reg_value = reg_values[chip->dac_routing];
  154. else if (channels == OXYGEN_PLAY_CHANNELS_8)
  155. /* in 7.1 mode, "rear" channels go to the "back" jack */
  156. reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  157. (3 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  158. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  159. (1 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
  160. else
  161. reg_value = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  162. (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  163. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  164. (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
  165. if (chip->model.adjust_dac_routing)
  166. reg_value = chip->model.adjust_dac_routing(chip, reg_value);
  167. oxygen_write16_masked(chip, OXYGEN_PLAY_ROUTING, reg_value,
  168. OXYGEN_PLAY_DAC0_SOURCE_MASK |
  169. OXYGEN_PLAY_DAC1_SOURCE_MASK |
  170. OXYGEN_PLAY_DAC2_SOURCE_MASK |
  171. OXYGEN_PLAY_DAC3_SOURCE_MASK);
  172. if (chip->model.update_center_lfe_mix)
  173. chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
  174. }
  175. EXPORT_SYMBOL(oxygen_update_dac_routing);
  176. static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
  177. {
  178. struct oxygen *chip = ctl->private_data;
  179. unsigned int count = upmix_item_count(chip);
  180. int changed;
  181. if (value->value.enumerated.item[0] >= count)
  182. return -EINVAL;
  183. mutex_lock(&chip->mutex);
  184. changed = value->value.enumerated.item[0] != chip->dac_routing;
  185. if (changed) {
  186. chip->dac_routing = value->value.enumerated.item[0];
  187. oxygen_update_dac_routing(chip);
  188. }
  189. mutex_unlock(&chip->mutex);
  190. return changed;
  191. }
  192. static int spdif_switch_get(struct snd_kcontrol *ctl,
  193. struct snd_ctl_elem_value *value)
  194. {
  195. struct oxygen *chip = ctl->private_data;
  196. mutex_lock(&chip->mutex);
  197. value->value.integer.value[0] = chip->spdif_playback_enable;
  198. mutex_unlock(&chip->mutex);
  199. return 0;
  200. }
  201. static unsigned int oxygen_spdif_rate(unsigned int oxygen_rate)
  202. {
  203. switch (oxygen_rate) {
  204. case OXYGEN_RATE_32000:
  205. return IEC958_AES3_CON_FS_32000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  206. case OXYGEN_RATE_44100:
  207. return IEC958_AES3_CON_FS_44100 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  208. default: /* OXYGEN_RATE_48000 */
  209. return IEC958_AES3_CON_FS_48000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  210. case OXYGEN_RATE_64000:
  211. return 0xb << OXYGEN_SPDIF_CS_RATE_SHIFT;
  212. case OXYGEN_RATE_88200:
  213. return IEC958_AES3_CON_FS_88200 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  214. case OXYGEN_RATE_96000:
  215. return IEC958_AES3_CON_FS_96000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  216. case OXYGEN_RATE_176400:
  217. return IEC958_AES3_CON_FS_176400 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  218. case OXYGEN_RATE_192000:
  219. return IEC958_AES3_CON_FS_192000 << OXYGEN_SPDIF_CS_RATE_SHIFT;
  220. }
  221. }
  222. void oxygen_update_spdif_source(struct oxygen *chip)
  223. {
  224. u32 old_control, new_control;
  225. u16 old_routing, new_routing;
  226. unsigned int oxygen_rate;
  227. old_control = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
  228. old_routing = oxygen_read16(chip, OXYGEN_PLAY_ROUTING);
  229. if (chip->pcm_active & (1 << PCM_SPDIF)) {
  230. new_control = old_control | OXYGEN_SPDIF_OUT_ENABLE;
  231. new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
  232. | OXYGEN_PLAY_SPDIF_SPDIF;
  233. oxygen_rate = (old_control >> OXYGEN_SPDIF_OUT_RATE_SHIFT)
  234. & OXYGEN_I2S_RATE_MASK;
  235. /* S/PDIF rate was already set by the caller */
  236. } else if ((chip->pcm_active & (1 << PCM_MULTICH)) &&
  237. chip->spdif_playback_enable) {
  238. new_routing = (old_routing & ~OXYGEN_PLAY_SPDIF_MASK)
  239. | OXYGEN_PLAY_SPDIF_MULTICH_01;
  240. oxygen_rate = oxygen_read16(chip, OXYGEN_I2S_MULTICH_FORMAT)
  241. & OXYGEN_I2S_RATE_MASK;
  242. new_control = (old_control & ~OXYGEN_SPDIF_OUT_RATE_MASK) |
  243. (oxygen_rate << OXYGEN_SPDIF_OUT_RATE_SHIFT) |
  244. OXYGEN_SPDIF_OUT_ENABLE;
  245. } else {
  246. new_control = old_control & ~OXYGEN_SPDIF_OUT_ENABLE;
  247. new_routing = old_routing;
  248. oxygen_rate = OXYGEN_RATE_44100;
  249. }
  250. if (old_routing != new_routing) {
  251. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL,
  252. new_control & ~OXYGEN_SPDIF_OUT_ENABLE);
  253. oxygen_write16(chip, OXYGEN_PLAY_ROUTING, new_routing);
  254. }
  255. if (new_control & OXYGEN_SPDIF_OUT_ENABLE)
  256. oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS,
  257. oxygen_spdif_rate(oxygen_rate) |
  258. ((chip->pcm_active & (1 << PCM_SPDIF)) ?
  259. chip->spdif_pcm_bits : chip->spdif_bits));
  260. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, new_control);
  261. }
  262. static int spdif_switch_put(struct snd_kcontrol *ctl,
  263. struct snd_ctl_elem_value *value)
  264. {
  265. struct oxygen *chip = ctl->private_data;
  266. int changed;
  267. mutex_lock(&chip->mutex);
  268. changed = value->value.integer.value[0] != chip->spdif_playback_enable;
  269. if (changed) {
  270. chip->spdif_playback_enable = !!value->value.integer.value[0];
  271. spin_lock_irq(&chip->reg_lock);
  272. oxygen_update_spdif_source(chip);
  273. spin_unlock_irq(&chip->reg_lock);
  274. }
  275. mutex_unlock(&chip->mutex);
  276. return changed;
  277. }
  278. static int spdif_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
  279. {
  280. info->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  281. info->count = 1;
  282. return 0;
  283. }
  284. static void oxygen_to_iec958(u32 bits, struct snd_ctl_elem_value *value)
  285. {
  286. value->value.iec958.status[0] =
  287. bits & (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
  288. OXYGEN_SPDIF_PREEMPHASIS);
  289. value->value.iec958.status[1] = /* category and original */
  290. bits >> OXYGEN_SPDIF_CATEGORY_SHIFT;
  291. }
  292. static u32 iec958_to_oxygen(struct snd_ctl_elem_value *value)
  293. {
  294. u32 bits;
  295. bits = value->value.iec958.status[0] &
  296. (OXYGEN_SPDIF_NONAUDIO | OXYGEN_SPDIF_C |
  297. OXYGEN_SPDIF_PREEMPHASIS);
  298. bits |= value->value.iec958.status[1] << OXYGEN_SPDIF_CATEGORY_SHIFT;
  299. if (bits & OXYGEN_SPDIF_NONAUDIO)
  300. bits |= OXYGEN_SPDIF_V;
  301. return bits;
  302. }
  303. static inline void write_spdif_bits(struct oxygen *chip, u32 bits)
  304. {
  305. oxygen_write32_masked(chip, OXYGEN_SPDIF_OUTPUT_BITS, bits,
  306. OXYGEN_SPDIF_NONAUDIO |
  307. OXYGEN_SPDIF_C |
  308. OXYGEN_SPDIF_PREEMPHASIS |
  309. OXYGEN_SPDIF_CATEGORY_MASK |
  310. OXYGEN_SPDIF_ORIGINAL |
  311. OXYGEN_SPDIF_V);
  312. }
  313. static int spdif_default_get(struct snd_kcontrol *ctl,
  314. struct snd_ctl_elem_value *value)
  315. {
  316. struct oxygen *chip = ctl->private_data;
  317. mutex_lock(&chip->mutex);
  318. oxygen_to_iec958(chip->spdif_bits, value);
  319. mutex_unlock(&chip->mutex);
  320. return 0;
  321. }
  322. static int spdif_default_put(struct snd_kcontrol *ctl,
  323. struct snd_ctl_elem_value *value)
  324. {
  325. struct oxygen *chip = ctl->private_data;
  326. u32 new_bits;
  327. int changed;
  328. new_bits = iec958_to_oxygen(value);
  329. mutex_lock(&chip->mutex);
  330. changed = new_bits != chip->spdif_bits;
  331. if (changed) {
  332. chip->spdif_bits = new_bits;
  333. if (!(chip->pcm_active & (1 << PCM_SPDIF)))
  334. write_spdif_bits(chip, new_bits);
  335. }
  336. mutex_unlock(&chip->mutex);
  337. return changed;
  338. }
  339. static int spdif_mask_get(struct snd_kcontrol *ctl,
  340. struct snd_ctl_elem_value *value)
  341. {
  342. value->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
  343. IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS;
  344. value->value.iec958.status[1] =
  345. IEC958_AES1_CON_CATEGORY | IEC958_AES1_CON_ORIGINAL;
  346. return 0;
  347. }
  348. static int spdif_pcm_get(struct snd_kcontrol *ctl,
  349. struct snd_ctl_elem_value *value)
  350. {
  351. struct oxygen *chip = ctl->private_data;
  352. mutex_lock(&chip->mutex);
  353. oxygen_to_iec958(chip->spdif_pcm_bits, value);
  354. mutex_unlock(&chip->mutex);
  355. return 0;
  356. }
  357. static int spdif_pcm_put(struct snd_kcontrol *ctl,
  358. struct snd_ctl_elem_value *value)
  359. {
  360. struct oxygen *chip = ctl->private_data;
  361. u32 new_bits;
  362. int changed;
  363. new_bits = iec958_to_oxygen(value);
  364. mutex_lock(&chip->mutex);
  365. changed = new_bits != chip->spdif_pcm_bits;
  366. if (changed) {
  367. chip->spdif_pcm_bits = new_bits;
  368. if (chip->pcm_active & (1 << PCM_SPDIF))
  369. write_spdif_bits(chip, new_bits);
  370. }
  371. mutex_unlock(&chip->mutex);
  372. return changed;
  373. }
  374. static int spdif_input_mask_get(struct snd_kcontrol *ctl,
  375. struct snd_ctl_elem_value *value)
  376. {
  377. value->value.iec958.status[0] = 0xff;
  378. value->value.iec958.status[1] = 0xff;
  379. value->value.iec958.status[2] = 0xff;
  380. value->value.iec958.status[3] = 0xff;
  381. return 0;
  382. }
  383. static int spdif_input_default_get(struct snd_kcontrol *ctl,
  384. struct snd_ctl_elem_value *value)
  385. {
  386. struct oxygen *chip = ctl->private_data;
  387. u32 bits;
  388. bits = oxygen_read32(chip, OXYGEN_SPDIF_INPUT_BITS);
  389. value->value.iec958.status[0] = bits;
  390. value->value.iec958.status[1] = bits >> 8;
  391. value->value.iec958.status[2] = bits >> 16;
  392. value->value.iec958.status[3] = bits >> 24;
  393. return 0;
  394. }
  395. static int spdif_bit_switch_get(struct snd_kcontrol *ctl,
  396. struct snd_ctl_elem_value *value)
  397. {
  398. struct oxygen *chip = ctl->private_data;
  399. u32 bit = ctl->private_value;
  400. value->value.integer.value[0] =
  401. !!(oxygen_read32(chip, OXYGEN_SPDIF_CONTROL) & bit);
  402. return 0;
  403. }
  404. static int spdif_bit_switch_put(struct snd_kcontrol *ctl,
  405. struct snd_ctl_elem_value *value)
  406. {
  407. struct oxygen *chip = ctl->private_data;
  408. u32 bit = ctl->private_value;
  409. u32 oldreg, newreg;
  410. int changed;
  411. spin_lock_irq(&chip->reg_lock);
  412. oldreg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
  413. if (value->value.integer.value[0])
  414. newreg = oldreg | bit;
  415. else
  416. newreg = oldreg & ~bit;
  417. changed = newreg != oldreg;
  418. if (changed)
  419. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, newreg);
  420. spin_unlock_irq(&chip->reg_lock);
  421. return changed;
  422. }
  423. static int monitor_volume_info(struct snd_kcontrol *ctl,
  424. struct snd_ctl_elem_info *info)
  425. {
  426. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  427. info->count = 1;
  428. info->value.integer.min = 0;
  429. info->value.integer.max = 1;
  430. return 0;
  431. }
  432. static int monitor_get(struct snd_kcontrol *ctl,
  433. struct snd_ctl_elem_value *value)
  434. {
  435. struct oxygen *chip = ctl->private_data;
  436. u8 bit = ctl->private_value;
  437. int invert = ctl->private_value & (1 << 8);
  438. value->value.integer.value[0] =
  439. !!invert ^ !!(oxygen_read8(chip, OXYGEN_ADC_MONITOR) & bit);
  440. return 0;
  441. }
  442. static int monitor_put(struct snd_kcontrol *ctl,
  443. struct snd_ctl_elem_value *value)
  444. {
  445. struct oxygen *chip = ctl->private_data;
  446. u8 bit = ctl->private_value;
  447. int invert = ctl->private_value & (1 << 8);
  448. u8 oldreg, newreg;
  449. int changed;
  450. spin_lock_irq(&chip->reg_lock);
  451. oldreg = oxygen_read8(chip, OXYGEN_ADC_MONITOR);
  452. if ((!!value->value.integer.value[0] ^ !!invert) != 0)
  453. newreg = oldreg | bit;
  454. else
  455. newreg = oldreg & ~bit;
  456. changed = newreg != oldreg;
  457. if (changed)
  458. oxygen_write8(chip, OXYGEN_ADC_MONITOR, newreg);
  459. spin_unlock_irq(&chip->reg_lock);
  460. return changed;
  461. }
  462. static int ac97_switch_get(struct snd_kcontrol *ctl,
  463. struct snd_ctl_elem_value *value)
  464. {
  465. struct oxygen *chip = ctl->private_data;
  466. unsigned int codec = (ctl->private_value >> 24) & 1;
  467. unsigned int index = ctl->private_value & 0xff;
  468. unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
  469. int invert = ctl->private_value & (1 << 16);
  470. u16 reg;
  471. mutex_lock(&chip->mutex);
  472. reg = oxygen_read_ac97(chip, codec, index);
  473. mutex_unlock(&chip->mutex);
  474. if (!(reg & (1 << bitnr)) ^ !invert)
  475. value->value.integer.value[0] = 1;
  476. else
  477. value->value.integer.value[0] = 0;
  478. return 0;
  479. }
  480. static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
  481. {
  482. unsigned int priv_idx;
  483. u16 value;
  484. if (!chip->controls[control])
  485. return;
  486. priv_idx = chip->controls[control]->private_value & 0xff;
  487. value = oxygen_read_ac97(chip, 0, priv_idx);
  488. if (!(value & 0x8000)) {
  489. oxygen_write_ac97(chip, 0, priv_idx, value | 0x8000);
  490. if (chip->model.ac97_switch)
  491. chip->model.ac97_switch(chip, priv_idx, 0x8000);
  492. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  493. &chip->controls[control]->id);
  494. }
  495. }
  496. static int ac97_switch_put(struct snd_kcontrol *ctl,
  497. struct snd_ctl_elem_value *value)
  498. {
  499. struct oxygen *chip = ctl->private_data;
  500. unsigned int codec = (ctl->private_value >> 24) & 1;
  501. unsigned int index = ctl->private_value & 0xff;
  502. unsigned int bitnr = (ctl->private_value >> 8) & 0xff;
  503. int invert = ctl->private_value & (1 << 16);
  504. u16 oldreg, newreg;
  505. int change;
  506. mutex_lock(&chip->mutex);
  507. oldreg = oxygen_read_ac97(chip, codec, index);
  508. newreg = oldreg;
  509. if (!value->value.integer.value[0] ^ !invert)
  510. newreg |= 1 << bitnr;
  511. else
  512. newreg &= ~(1 << bitnr);
  513. change = newreg != oldreg;
  514. if (change) {
  515. oxygen_write_ac97(chip, codec, index, newreg);
  516. if (codec == 0 && chip->model.ac97_switch)
  517. chip->model.ac97_switch(chip, index, newreg & 0x8000);
  518. if (index == AC97_LINE) {
  519. oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
  520. newreg & 0x8000 ?
  521. CM9780_GPO0 : 0, CM9780_GPO0);
  522. if (!(newreg & 0x8000)) {
  523. mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
  524. mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
  525. mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
  526. }
  527. } else if ((index == AC97_MIC || index == AC97_CD ||
  528. index == AC97_VIDEO || index == AC97_AUX) &&
  529. bitnr == 15 && !(newreg & 0x8000)) {
  530. mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
  531. oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
  532. CM9780_GPO0, CM9780_GPO0);
  533. }
  534. }
  535. mutex_unlock(&chip->mutex);
  536. return change;
  537. }
  538. static int ac97_volume_info(struct snd_kcontrol *ctl,
  539. struct snd_ctl_elem_info *info)
  540. {
  541. int stereo = (ctl->private_value >> 16) & 1;
  542. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  543. info->count = stereo ? 2 : 1;
  544. info->value.integer.min = 0;
  545. info->value.integer.max = 0x1f;
  546. return 0;
  547. }
  548. static int ac97_volume_get(struct snd_kcontrol *ctl,
  549. struct snd_ctl_elem_value *value)
  550. {
  551. struct oxygen *chip = ctl->private_data;
  552. unsigned int codec = (ctl->private_value >> 24) & 1;
  553. int stereo = (ctl->private_value >> 16) & 1;
  554. unsigned int index = ctl->private_value & 0xff;
  555. u16 reg;
  556. mutex_lock(&chip->mutex);
  557. reg = oxygen_read_ac97(chip, codec, index);
  558. mutex_unlock(&chip->mutex);
  559. if (!stereo) {
  560. value->value.integer.value[0] = 31 - (reg & 0x1f);
  561. } else {
  562. value->value.integer.value[0] = 31 - ((reg >> 8) & 0x1f);
  563. value->value.integer.value[1] = 31 - (reg & 0x1f);
  564. }
  565. return 0;
  566. }
  567. static int ac97_volume_put(struct snd_kcontrol *ctl,
  568. struct snd_ctl_elem_value *value)
  569. {
  570. struct oxygen *chip = ctl->private_data;
  571. unsigned int codec = (ctl->private_value >> 24) & 1;
  572. int stereo = (ctl->private_value >> 16) & 1;
  573. unsigned int index = ctl->private_value & 0xff;
  574. u16 oldreg, newreg;
  575. int change;
  576. mutex_lock(&chip->mutex);
  577. oldreg = oxygen_read_ac97(chip, codec, index);
  578. if (!stereo) {
  579. newreg = oldreg & ~0x1f;
  580. newreg |= 31 - (value->value.integer.value[0] & 0x1f);
  581. } else {
  582. newreg = oldreg & ~0x1f1f;
  583. newreg |= (31 - (value->value.integer.value[0] & 0x1f)) << 8;
  584. newreg |= 31 - (value->value.integer.value[1] & 0x1f);
  585. }
  586. change = newreg != oldreg;
  587. if (change)
  588. oxygen_write_ac97(chip, codec, index, newreg);
  589. mutex_unlock(&chip->mutex);
  590. return change;
  591. }
  592. static int mic_fmic_source_info(struct snd_kcontrol *ctl,
  593. struct snd_ctl_elem_info *info)
  594. {
  595. static const char *const names[] = { "Mic Jack", "Front Panel" };
  596. return snd_ctl_enum_info(info, 1, 2, names);
  597. }
  598. static int mic_fmic_source_get(struct snd_kcontrol *ctl,
  599. struct snd_ctl_elem_value *value)
  600. {
  601. struct oxygen *chip = ctl->private_data;
  602. mutex_lock(&chip->mutex);
  603. value->value.enumerated.item[0] =
  604. !!(oxygen_read_ac97(chip, 0, CM9780_JACK) & CM9780_FMIC2MIC);
  605. mutex_unlock(&chip->mutex);
  606. return 0;
  607. }
  608. static int mic_fmic_source_put(struct snd_kcontrol *ctl,
  609. struct snd_ctl_elem_value *value)
  610. {
  611. struct oxygen *chip = ctl->private_data;
  612. u16 oldreg, newreg;
  613. int change;
  614. mutex_lock(&chip->mutex);
  615. oldreg = oxygen_read_ac97(chip, 0, CM9780_JACK);
  616. if (value->value.enumerated.item[0])
  617. newreg = oldreg | CM9780_FMIC2MIC;
  618. else
  619. newreg = oldreg & ~CM9780_FMIC2MIC;
  620. change = newreg != oldreg;
  621. if (change)
  622. oxygen_write_ac97(chip, 0, CM9780_JACK, newreg);
  623. mutex_unlock(&chip->mutex);
  624. return change;
  625. }
  626. static int ac97_fp_rec_volume_info(struct snd_kcontrol *ctl,
  627. struct snd_ctl_elem_info *info)
  628. {
  629. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  630. info->count = 2;
  631. info->value.integer.min = 0;
  632. info->value.integer.max = 7;
  633. return 0;
  634. }
  635. static int ac97_fp_rec_volume_get(struct snd_kcontrol *ctl,
  636. struct snd_ctl_elem_value *value)
  637. {
  638. struct oxygen *chip = ctl->private_data;
  639. u16 reg;
  640. mutex_lock(&chip->mutex);
  641. reg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
  642. mutex_unlock(&chip->mutex);
  643. value->value.integer.value[0] = reg & 7;
  644. value->value.integer.value[1] = (reg >> 8) & 7;
  645. return 0;
  646. }
  647. static int ac97_fp_rec_volume_put(struct snd_kcontrol *ctl,
  648. struct snd_ctl_elem_value *value)
  649. {
  650. struct oxygen *chip = ctl->private_data;
  651. u16 oldreg, newreg;
  652. int change;
  653. mutex_lock(&chip->mutex);
  654. oldreg = oxygen_read_ac97(chip, 1, AC97_REC_GAIN);
  655. newreg = oldreg & ~0x0707;
  656. newreg = newreg | (value->value.integer.value[0] & 7);
  657. newreg = newreg | ((value->value.integer.value[0] & 7) << 8);
  658. change = newreg != oldreg;
  659. if (change)
  660. oxygen_write_ac97(chip, 1, AC97_REC_GAIN, newreg);
  661. mutex_unlock(&chip->mutex);
  662. return change;
  663. }
  664. #define AC97_SWITCH(xname, codec, index, bitnr, invert) { \
  665. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  666. .name = xname, \
  667. .info = snd_ctl_boolean_mono_info, \
  668. .get = ac97_switch_get, \
  669. .put = ac97_switch_put, \
  670. .private_value = ((codec) << 24) | ((invert) << 16) | \
  671. ((bitnr) << 8) | (index), \
  672. }
  673. #define AC97_VOLUME(xname, codec, index, stereo) { \
  674. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  675. .name = xname, \
  676. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  677. SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  678. .info = ac97_volume_info, \
  679. .get = ac97_volume_get, \
  680. .put = ac97_volume_put, \
  681. .tlv = { .p = ac97_db_scale, }, \
  682. .private_value = ((codec) << 24) | ((stereo) << 16) | (index), \
  683. }
  684. static DECLARE_TLV_DB_SCALE(monitor_db_scale, -600, 600, 0);
  685. static DECLARE_TLV_DB_SCALE(ac97_db_scale, -3450, 150, 0);
  686. static DECLARE_TLV_DB_SCALE(ac97_rec_db_scale, 0, 150, 0);
  687. static const struct snd_kcontrol_new controls[] = {
  688. {
  689. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  690. .name = "Master Playback Volume",
  691. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  692. .info = dac_volume_info,
  693. .get = dac_volume_get,
  694. .put = dac_volume_put,
  695. },
  696. {
  697. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  698. .name = "Master Playback Switch",
  699. .info = snd_ctl_boolean_mono_info,
  700. .get = dac_mute_get,
  701. .put = dac_mute_put,
  702. },
  703. {
  704. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  705. .name = "Stereo Upmixing",
  706. .info = upmix_info,
  707. .get = upmix_get,
  708. .put = upmix_put,
  709. },
  710. };
  711. static const struct snd_kcontrol_new spdif_output_controls[] = {
  712. {
  713. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  714. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  715. .info = snd_ctl_boolean_mono_info,
  716. .get = spdif_switch_get,
  717. .put = spdif_switch_put,
  718. },
  719. {
  720. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  721. .device = 1,
  722. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  723. .info = spdif_info,
  724. .get = spdif_default_get,
  725. .put = spdif_default_put,
  726. },
  727. {
  728. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  729. .device = 1,
  730. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  731. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  732. .info = spdif_info,
  733. .get = spdif_mask_get,
  734. },
  735. {
  736. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  737. .device = 1,
  738. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  739. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  740. SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  741. .info = spdif_info,
  742. .get = spdif_pcm_get,
  743. .put = spdif_pcm_put,
  744. },
  745. };
  746. static const struct snd_kcontrol_new spdif_input_controls[] = {
  747. {
  748. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  749. .device = 1,
  750. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),
  751. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  752. .info = spdif_info,
  753. .get = spdif_input_mask_get,
  754. },
  755. {
  756. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  757. .device = 1,
  758. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  759. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  760. .info = spdif_info,
  761. .get = spdif_input_default_get,
  762. },
  763. {
  764. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  765. .name = SNDRV_CTL_NAME_IEC958("Loopback ", NONE, SWITCH),
  766. .info = snd_ctl_boolean_mono_info,
  767. .get = spdif_bit_switch_get,
  768. .put = spdif_bit_switch_put,
  769. .private_value = OXYGEN_SPDIF_LOOPBACK,
  770. },
  771. {
  772. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  773. .name = SNDRV_CTL_NAME_IEC958("Validity Check ",CAPTURE,SWITCH),
  774. .info = snd_ctl_boolean_mono_info,
  775. .get = spdif_bit_switch_get,
  776. .put = spdif_bit_switch_put,
  777. .private_value = OXYGEN_SPDIF_SPDVALID,
  778. },
  779. };
  780. static const struct {
  781. unsigned int pcm_dev;
  782. struct snd_kcontrol_new controls[2];
  783. } monitor_controls[] = {
  784. {
  785. .pcm_dev = CAPTURE_0_FROM_I2S_1,
  786. .controls = {
  787. {
  788. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  789. .name = "Analog Input Monitor Playback Switch",
  790. .info = snd_ctl_boolean_mono_info,
  791. .get = monitor_get,
  792. .put = monitor_put,
  793. .private_value = OXYGEN_ADC_MONITOR_A,
  794. },
  795. {
  796. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  797. .name = "Analog Input Monitor Playback Volume",
  798. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  799. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  800. .info = monitor_volume_info,
  801. .get = monitor_get,
  802. .put = monitor_put,
  803. .private_value = OXYGEN_ADC_MONITOR_A_HALF_VOL
  804. | (1 << 8),
  805. .tlv = { .p = monitor_db_scale, },
  806. },
  807. },
  808. },
  809. {
  810. .pcm_dev = CAPTURE_0_FROM_I2S_2,
  811. .controls = {
  812. {
  813. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  814. .name = "Analog Input Monitor Playback Switch",
  815. .info = snd_ctl_boolean_mono_info,
  816. .get = monitor_get,
  817. .put = monitor_put,
  818. .private_value = OXYGEN_ADC_MONITOR_B,
  819. },
  820. {
  821. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  822. .name = "Analog Input Monitor Playback Volume",
  823. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  824. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  825. .info = monitor_volume_info,
  826. .get = monitor_get,
  827. .put = monitor_put,
  828. .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
  829. | (1 << 8),
  830. .tlv = { .p = monitor_db_scale, },
  831. },
  832. },
  833. },
  834. {
  835. .pcm_dev = CAPTURE_2_FROM_I2S_2,
  836. .controls = {
  837. {
  838. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  839. .name = "Analog Input Monitor Playback Switch",
  840. .index = 1,
  841. .info = snd_ctl_boolean_mono_info,
  842. .get = monitor_get,
  843. .put = monitor_put,
  844. .private_value = OXYGEN_ADC_MONITOR_B,
  845. },
  846. {
  847. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  848. .name = "Analog Input Monitor Playback Volume",
  849. .index = 1,
  850. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  851. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  852. .info = monitor_volume_info,
  853. .get = monitor_get,
  854. .put = monitor_put,
  855. .private_value = OXYGEN_ADC_MONITOR_B_HALF_VOL
  856. | (1 << 8),
  857. .tlv = { .p = monitor_db_scale, },
  858. },
  859. },
  860. },
  861. {
  862. .pcm_dev = CAPTURE_3_FROM_I2S_3,
  863. .controls = {
  864. {
  865. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  866. .name = "Analog Input Monitor Playback Switch",
  867. .index = 2,
  868. .info = snd_ctl_boolean_mono_info,
  869. .get = monitor_get,
  870. .put = monitor_put,
  871. .private_value = OXYGEN_ADC_MONITOR_C,
  872. },
  873. {
  874. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  875. .name = "Analog Input Monitor Playback Volume",
  876. .index = 2,
  877. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  878. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  879. .info = monitor_volume_info,
  880. .get = monitor_get,
  881. .put = monitor_put,
  882. .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
  883. | (1 << 8),
  884. .tlv = { .p = monitor_db_scale, },
  885. },
  886. },
  887. },
  888. {
  889. .pcm_dev = CAPTURE_1_FROM_SPDIF,
  890. .controls = {
  891. {
  892. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  893. .name = "Digital Input Monitor Playback Switch",
  894. .info = snd_ctl_boolean_mono_info,
  895. .get = monitor_get,
  896. .put = monitor_put,
  897. .private_value = OXYGEN_ADC_MONITOR_C,
  898. },
  899. {
  900. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  901. .name = "Digital Input Monitor Playback Volume",
  902. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  903. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  904. .info = monitor_volume_info,
  905. .get = monitor_get,
  906. .put = monitor_put,
  907. .private_value = OXYGEN_ADC_MONITOR_C_HALF_VOL
  908. | (1 << 8),
  909. .tlv = { .p = monitor_db_scale, },
  910. },
  911. },
  912. },
  913. };
  914. static const struct snd_kcontrol_new ac97_controls[] = {
  915. AC97_VOLUME("Mic Capture Volume", 0, AC97_MIC, 0),
  916. AC97_SWITCH("Mic Capture Switch", 0, AC97_MIC, 15, 1),
  917. AC97_SWITCH("Mic Boost (+20dB)", 0, AC97_MIC, 6, 0),
  918. {
  919. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  920. .name = "Mic Source Capture Enum",
  921. .info = mic_fmic_source_info,
  922. .get = mic_fmic_source_get,
  923. .put = mic_fmic_source_put,
  924. },
  925. AC97_SWITCH("Line Capture Switch", 0, AC97_LINE, 15, 1),
  926. AC97_VOLUME("CD Capture Volume", 0, AC97_CD, 1),
  927. AC97_SWITCH("CD Capture Switch", 0, AC97_CD, 15, 1),
  928. AC97_VOLUME("Aux Capture Volume", 0, AC97_AUX, 1),
  929. AC97_SWITCH("Aux Capture Switch", 0, AC97_AUX, 15, 1),
  930. };
  931. static const struct snd_kcontrol_new ac97_fp_controls[] = {
  932. AC97_VOLUME("Front Panel Playback Volume", 1, AC97_HEADPHONE, 1),
  933. AC97_SWITCH("Front Panel Playback Switch", 1, AC97_HEADPHONE, 15, 1),
  934. {
  935. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  936. .name = "Front Panel Capture Volume",
  937. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  938. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  939. .info = ac97_fp_rec_volume_info,
  940. .get = ac97_fp_rec_volume_get,
  941. .put = ac97_fp_rec_volume_put,
  942. .tlv = { .p = ac97_rec_db_scale, },
  943. },
  944. AC97_SWITCH("Front Panel Capture Switch", 1, AC97_REC_GAIN, 15, 1),
  945. };
  946. static void oxygen_any_ctl_free(struct snd_kcontrol *ctl)
  947. {
  948. struct oxygen *chip = ctl->private_data;
  949. unsigned int i;
  950. /* I'm too lazy to write a function for each control :-) */
  951. for (i = 0; i < ARRAY_SIZE(chip->controls); ++i)
  952. chip->controls[i] = NULL;
  953. }
  954. static int add_controls(struct oxygen *chip,
  955. const struct snd_kcontrol_new controls[],
  956. unsigned int count)
  957. {
  958. static const char *const known_ctl_names[CONTROL_COUNT] = {
  959. [CONTROL_SPDIF_PCM] =
  960. SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  961. [CONTROL_SPDIF_INPUT_BITS] =
  962. SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  963. [CONTROL_MIC_CAPTURE_SWITCH] = "Mic Capture Switch",
  964. [CONTROL_LINE_CAPTURE_SWITCH] = "Line Capture Switch",
  965. [CONTROL_CD_CAPTURE_SWITCH] = "CD Capture Switch",
  966. [CONTROL_AUX_CAPTURE_SWITCH] = "Aux Capture Switch",
  967. };
  968. unsigned int i, j;
  969. struct snd_kcontrol_new template;
  970. struct snd_kcontrol *ctl;
  971. int err;
  972. for (i = 0; i < count; ++i) {
  973. template = controls[i];
  974. if (chip->model.control_filter) {
  975. err = chip->model.control_filter(&template);
  976. if (err < 0)
  977. return err;
  978. if (err == 1)
  979. continue;
  980. }
  981. if (!strcmp(template.name, "Stereo Upmixing") &&
  982. chip->model.dac_channels_pcm == 2)
  983. continue;
  984. if (!strcmp(template.name, "Mic Source Capture Enum") &&
  985. !(chip->model.device_config & AC97_FMIC_SWITCH))
  986. continue;
  987. if (!strncmp(template.name, "CD Capture ", 11) &&
  988. !(chip->model.device_config & AC97_CD_INPUT))
  989. continue;
  990. if (!strcmp(template.name, "Master Playback Volume") &&
  991. chip->model.dac_tlv) {
  992. template.tlv.p = chip->model.dac_tlv;
  993. template.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  994. }
  995. ctl = snd_ctl_new1(&template, chip);
  996. if (!ctl)
  997. return -ENOMEM;
  998. err = snd_ctl_add(chip->card, ctl);
  999. if (err < 0)
  1000. return err;
  1001. for (j = 0; j < CONTROL_COUNT; ++j)
  1002. if (!strcmp(ctl->id.name, known_ctl_names[j])) {
  1003. chip->controls[j] = ctl;
  1004. ctl->private_free = oxygen_any_ctl_free;
  1005. }
  1006. }
  1007. return 0;
  1008. }
  1009. int oxygen_mixer_init(struct oxygen *chip)
  1010. {
  1011. unsigned int i;
  1012. int err;
  1013. err = add_controls(chip, controls, ARRAY_SIZE(controls));
  1014. if (err < 0)
  1015. return err;
  1016. if (chip->model.device_config & PLAYBACK_1_TO_SPDIF) {
  1017. err = add_controls(chip, spdif_output_controls,
  1018. ARRAY_SIZE(spdif_output_controls));
  1019. if (err < 0)
  1020. return err;
  1021. }
  1022. if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) {
  1023. err = add_controls(chip, spdif_input_controls,
  1024. ARRAY_SIZE(spdif_input_controls));
  1025. if (err < 0)
  1026. return err;
  1027. }
  1028. for (i = 0; i < ARRAY_SIZE(monitor_controls); ++i) {
  1029. if (!(chip->model.device_config & monitor_controls[i].pcm_dev))
  1030. continue;
  1031. err = add_controls(chip, monitor_controls[i].controls,
  1032. ARRAY_SIZE(monitor_controls[i].controls));
  1033. if (err < 0)
  1034. return err;
  1035. }
  1036. if (chip->has_ac97_0) {
  1037. err = add_controls(chip, ac97_controls,
  1038. ARRAY_SIZE(ac97_controls));
  1039. if (err < 0)
  1040. return err;
  1041. }
  1042. if (chip->has_ac97_1) {
  1043. err = add_controls(chip, ac97_fp_controls,
  1044. ARRAY_SIZE(ac97_fp_controls));
  1045. if (err < 0)
  1046. return err;
  1047. }
  1048. return chip->model.mixer_init ? chip->model.mixer_init(chip) : 0;
  1049. }