cs4236_lib.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of CS4235/4236B/4237B/4238B/4239 chips
  4. *
  5. * Note:
  6. * -----
  7. *
  8. * Bugs:
  9. * -----
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. /*
  27. * Indirect control registers (CS4236B+)
  28. *
  29. * C0
  30. * D8: WSS reset (all chips)
  31. *
  32. * C1 (all chips except CS4236)
  33. * D7-D5: version
  34. * D4-D0: chip id
  35. * 11101 - CS4235
  36. * 01011 - CS4236B
  37. * 01000 - CS4237B
  38. * 01001 - CS4238B
  39. * 11110 - CS4239
  40. *
  41. * C2
  42. * D7-D4: 3D Space (CS4235,CS4237B,CS4238B,CS4239)
  43. * D3-D0: 3D Center (CS4237B); 3D Volume (CS4238B)
  44. *
  45. * C3
  46. * D7: 3D Enable (CS4237B)
  47. * D6: 3D Mono Enable (CS4237B)
  48. * D5: 3D Serial Output (CS4237B,CS4238B)
  49. * D4: 3D Enable (CS4235,CS4238B,CS4239)
  50. *
  51. * C4
  52. * D7: consumer serial port enable (CS4237B,CS4238B)
  53. * D6: channels status block reset (CS4237B,CS4238B)
  54. * D5: user bit in sub-frame of digital audio data (CS4237B,CS4238B)
  55. * D4: validity bit bit in sub-frame of digital audio data (CS4237B,CS4238B)
  56. *
  57. * C5 lower channel status (digital serial data description) (CS4237B,CS4238B)
  58. * D7-D6: first two bits of category code
  59. * D5: lock
  60. * D4-D3: pre-emphasis (0 = none, 1 = 50/15us)
  61. * D2: copy/copyright (0 = copy inhibited)
  62. * D1: 0 = digital audio / 1 = non-digital audio
  63. *
  64. * C6 upper channel status (digital serial data description) (CS4237B,CS4238B)
  65. * D7-D6: sample frequency (0 = 44.1kHz)
  66. * D5: generation status (0 = no indication, 1 = original/commercially precaptureed data)
  67. * D4-D0: category code (upper bits)
  68. *
  69. * C7 reserved (must write 0)
  70. *
  71. * C8 wavetable control
  72. * D7: volume control interrupt enable (CS4235,CS4239)
  73. * D6: hardware volume control format (CS4235,CS4239)
  74. * D3: wavetable serial port enable (all chips)
  75. * D2: DSP serial port switch (all chips)
  76. * D1: disable MCLK (all chips)
  77. * D0: force BRESET low (all chips)
  78. *
  79. */
  80. #include <asm/io.h>
  81. #include <linux/delay.h>
  82. #include <linux/init.h>
  83. #include <linux/time.h>
  84. #include <linux/wait.h>
  85. #include <sound/core.h>
  86. #include <sound/wss.h>
  87. #include <sound/asoundef.h>
  88. #include <sound/initval.h>
  89. /*
  90. *
  91. */
  92. static unsigned char snd_cs4236_ext_map[18] = {
  93. /* CS4236_LEFT_LINE */ 0xff,
  94. /* CS4236_RIGHT_LINE */ 0xff,
  95. /* CS4236_LEFT_MIC */ 0xdf,
  96. /* CS4236_RIGHT_MIC */ 0xdf,
  97. /* CS4236_LEFT_MIX_CTRL */ 0xe0 | 0x18,
  98. /* CS4236_RIGHT_MIX_CTRL */ 0xe0,
  99. /* CS4236_LEFT_FM */ 0xbf,
  100. /* CS4236_RIGHT_FM */ 0xbf,
  101. /* CS4236_LEFT_DSP */ 0xbf,
  102. /* CS4236_RIGHT_DSP */ 0xbf,
  103. /* CS4236_RIGHT_LOOPBACK */ 0xbf,
  104. /* CS4236_DAC_MUTE */ 0xe0,
  105. /* CS4236_ADC_RATE */ 0x01, /* 48kHz */
  106. /* CS4236_DAC_RATE */ 0x01, /* 48kHz */
  107. /* CS4236_LEFT_MASTER */ 0xbf,
  108. /* CS4236_RIGHT_MASTER */ 0xbf,
  109. /* CS4236_LEFT_WAVE */ 0xbf,
  110. /* CS4236_RIGHT_WAVE */ 0xbf
  111. };
  112. /*
  113. *
  114. */
  115. static void snd_cs4236_ctrl_out(struct snd_wss *chip,
  116. unsigned char reg, unsigned char val)
  117. {
  118. outb(reg, chip->cport + 3);
  119. outb(chip->cimage[reg] = val, chip->cport + 4);
  120. }
  121. static unsigned char snd_cs4236_ctrl_in(struct snd_wss *chip, unsigned char reg)
  122. {
  123. outb(reg, chip->cport + 3);
  124. return inb(chip->cport + 4);
  125. }
  126. /*
  127. * PCM
  128. */
  129. #define CLOCKS 8
  130. static struct snd_ratnum clocks[CLOCKS] = {
  131. { .num = 16934400, .den_min = 353, .den_max = 353, .den_step = 1 },
  132. { .num = 16934400, .den_min = 529, .den_max = 529, .den_step = 1 },
  133. { .num = 16934400, .den_min = 617, .den_max = 617, .den_step = 1 },
  134. { .num = 16934400, .den_min = 1058, .den_max = 1058, .den_step = 1 },
  135. { .num = 16934400, .den_min = 1764, .den_max = 1764, .den_step = 1 },
  136. { .num = 16934400, .den_min = 2117, .den_max = 2117, .den_step = 1 },
  137. { .num = 16934400, .den_min = 2558, .den_max = 2558, .den_step = 1 },
  138. { .num = 16934400/16, .den_min = 21, .den_max = 192, .den_step = 1 }
  139. };
  140. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  141. .nrats = CLOCKS,
  142. .rats = clocks,
  143. };
  144. static int snd_cs4236_xrate(struct snd_pcm_runtime *runtime)
  145. {
  146. return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  147. &hw_constraints_clocks);
  148. }
  149. static unsigned char divisor_to_rate_register(unsigned int divisor)
  150. {
  151. switch (divisor) {
  152. case 353: return 1;
  153. case 529: return 2;
  154. case 617: return 3;
  155. case 1058: return 4;
  156. case 1764: return 5;
  157. case 2117: return 6;
  158. case 2558: return 7;
  159. default:
  160. if (divisor < 21 || divisor > 192) {
  161. snd_BUG();
  162. return 192;
  163. }
  164. return divisor;
  165. }
  166. }
  167. static void snd_cs4236_playback_format(struct snd_wss *chip,
  168. struct snd_pcm_hw_params *params,
  169. unsigned char pdfr)
  170. {
  171. unsigned long flags;
  172. unsigned char rate = divisor_to_rate_register(params->rate_den);
  173. spin_lock_irqsave(&chip->reg_lock, flags);
  174. /* set fast playback format change and clean playback FIFO */
  175. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  176. chip->image[CS4231_ALT_FEATURE_1] | 0x10);
  177. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr & 0xf0);
  178. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  179. chip->image[CS4231_ALT_FEATURE_1] & ~0x10);
  180. snd_cs4236_ext_out(chip, CS4236_DAC_RATE, rate);
  181. spin_unlock_irqrestore(&chip->reg_lock, flags);
  182. }
  183. static void snd_cs4236_capture_format(struct snd_wss *chip,
  184. struct snd_pcm_hw_params *params,
  185. unsigned char cdfr)
  186. {
  187. unsigned long flags;
  188. unsigned char rate = divisor_to_rate_register(params->rate_den);
  189. spin_lock_irqsave(&chip->reg_lock, flags);
  190. /* set fast capture format change and clean capture FIFO */
  191. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  192. chip->image[CS4231_ALT_FEATURE_1] | 0x20);
  193. snd_wss_out(chip, CS4231_REC_FORMAT, cdfr & 0xf0);
  194. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  195. chip->image[CS4231_ALT_FEATURE_1] & ~0x20);
  196. snd_cs4236_ext_out(chip, CS4236_ADC_RATE, rate);
  197. spin_unlock_irqrestore(&chip->reg_lock, flags);
  198. }
  199. #ifdef CONFIG_PM
  200. static void snd_cs4236_suspend(struct snd_wss *chip)
  201. {
  202. int reg;
  203. unsigned long flags;
  204. spin_lock_irqsave(&chip->reg_lock, flags);
  205. for (reg = 0; reg < 32; reg++)
  206. chip->image[reg] = snd_wss_in(chip, reg);
  207. for (reg = 0; reg < 18; reg++)
  208. chip->eimage[reg] = snd_cs4236_ext_in(chip, CS4236_I23VAL(reg));
  209. for (reg = 2; reg < 9; reg++)
  210. chip->cimage[reg] = snd_cs4236_ctrl_in(chip, reg);
  211. spin_unlock_irqrestore(&chip->reg_lock, flags);
  212. }
  213. static void snd_cs4236_resume(struct snd_wss *chip)
  214. {
  215. int reg;
  216. unsigned long flags;
  217. snd_wss_mce_up(chip);
  218. spin_lock_irqsave(&chip->reg_lock, flags);
  219. for (reg = 0; reg < 32; reg++) {
  220. switch (reg) {
  221. case CS4236_EXT_REG:
  222. case CS4231_VERSION:
  223. case 27: /* why? CS4235 - master left */
  224. case 29: /* why? CS4235 - master right */
  225. break;
  226. default:
  227. snd_wss_out(chip, reg, chip->image[reg]);
  228. break;
  229. }
  230. }
  231. for (reg = 0; reg < 18; reg++)
  232. snd_cs4236_ext_out(chip, CS4236_I23VAL(reg), chip->eimage[reg]);
  233. for (reg = 2; reg < 9; reg++) {
  234. switch (reg) {
  235. case 7:
  236. break;
  237. default:
  238. snd_cs4236_ctrl_out(chip, reg, chip->cimage[reg]);
  239. }
  240. }
  241. spin_unlock_irqrestore(&chip->reg_lock, flags);
  242. snd_wss_mce_down(chip);
  243. }
  244. #endif /* CONFIG_PM */
  245. /*
  246. * This function does no fail if the chip is not CS4236B or compatible.
  247. * It just an equivalent to the snd_wss_create() then.
  248. */
  249. int snd_cs4236_create(struct snd_card *card,
  250. unsigned long port,
  251. unsigned long cport,
  252. int irq, int dma1, int dma2,
  253. unsigned short hardware,
  254. unsigned short hwshare,
  255. struct snd_wss **rchip)
  256. {
  257. struct snd_wss *chip;
  258. unsigned char ver1, ver2;
  259. unsigned int reg;
  260. int err;
  261. *rchip = NULL;
  262. if (hardware == WSS_HW_DETECT)
  263. hardware = WSS_HW_DETECT3;
  264. err = snd_wss_create(card, port, cport,
  265. irq, dma1, dma2, hardware, hwshare, &chip);
  266. if (err < 0)
  267. return err;
  268. if ((chip->hardware & WSS_HW_CS4236B_MASK) == 0) {
  269. snd_printd("chip is not CS4236+, hardware=0x%x\n",
  270. chip->hardware);
  271. *rchip = chip;
  272. return 0;
  273. }
  274. #if 0
  275. {
  276. int idx;
  277. for (idx = 0; idx < 8; idx++)
  278. snd_printk(KERN_DEBUG "CD%i = 0x%x\n",
  279. idx, inb(chip->cport + idx));
  280. for (idx = 0; idx < 9; idx++)
  281. snd_printk(KERN_DEBUG "C%i = 0x%x\n",
  282. idx, snd_cs4236_ctrl_in(chip, idx));
  283. }
  284. #endif
  285. if (cport < 0x100 || cport == SNDRV_AUTO_PORT) {
  286. snd_printk(KERN_ERR "please, specify control port "
  287. "for CS4236+ chips\n");
  288. snd_device_free(card, chip);
  289. return -ENODEV;
  290. }
  291. ver1 = snd_cs4236_ctrl_in(chip, 1);
  292. ver2 = snd_cs4236_ext_in(chip, CS4236_VERSION);
  293. snd_printdd("CS4236: [0x%lx] C1 (version) = 0x%x, ext = 0x%x\n",
  294. cport, ver1, ver2);
  295. if (ver1 != ver2) {
  296. snd_printk(KERN_ERR "CS4236+ chip detected, but "
  297. "control port 0x%lx is not valid\n", cport);
  298. snd_device_free(card, chip);
  299. return -ENODEV;
  300. }
  301. snd_cs4236_ctrl_out(chip, 0, 0x00);
  302. snd_cs4236_ctrl_out(chip, 2, 0xff);
  303. snd_cs4236_ctrl_out(chip, 3, 0x00);
  304. snd_cs4236_ctrl_out(chip, 4, 0x80);
  305. reg = ((IEC958_AES1_CON_PCM_CODER & 3) << 6) |
  306. IEC958_AES0_CON_EMPHASIS_NONE;
  307. snd_cs4236_ctrl_out(chip, 5, reg);
  308. snd_cs4236_ctrl_out(chip, 6, IEC958_AES1_CON_PCM_CODER >> 2);
  309. snd_cs4236_ctrl_out(chip, 7, 0x00);
  310. /*
  311. * 0x8c for C8 is valid for Turtle Beach Malibu - the IEC-958
  312. * output is working with this setup, other hardware should
  313. * have different signal paths and this value should be
  314. * selectable in the future
  315. */
  316. snd_cs4236_ctrl_out(chip, 8, 0x8c);
  317. chip->rate_constraint = snd_cs4236_xrate;
  318. chip->set_playback_format = snd_cs4236_playback_format;
  319. chip->set_capture_format = snd_cs4236_capture_format;
  320. #ifdef CONFIG_PM
  321. chip->suspend = snd_cs4236_suspend;
  322. chip->resume = snd_cs4236_resume;
  323. #endif
  324. /* initialize extended registers */
  325. for (reg = 0; reg < sizeof(snd_cs4236_ext_map); reg++)
  326. snd_cs4236_ext_out(chip, CS4236_I23VAL(reg),
  327. snd_cs4236_ext_map[reg]);
  328. /* initialize compatible but more featured registers */
  329. snd_wss_out(chip, CS4231_LEFT_INPUT, 0x40);
  330. snd_wss_out(chip, CS4231_RIGHT_INPUT, 0x40);
  331. snd_wss_out(chip, CS4231_AUX1_LEFT_INPUT, 0xff);
  332. snd_wss_out(chip, CS4231_AUX1_RIGHT_INPUT, 0xff);
  333. snd_wss_out(chip, CS4231_AUX2_LEFT_INPUT, 0xdf);
  334. snd_wss_out(chip, CS4231_AUX2_RIGHT_INPUT, 0xdf);
  335. snd_wss_out(chip, CS4231_RIGHT_LINE_IN, 0xff);
  336. snd_wss_out(chip, CS4231_LEFT_LINE_IN, 0xff);
  337. snd_wss_out(chip, CS4231_RIGHT_LINE_IN, 0xff);
  338. switch (chip->hardware) {
  339. case WSS_HW_CS4235:
  340. case WSS_HW_CS4239:
  341. snd_wss_out(chip, CS4235_LEFT_MASTER, 0xff);
  342. snd_wss_out(chip, CS4235_RIGHT_MASTER, 0xff);
  343. break;
  344. }
  345. *rchip = chip;
  346. return 0;
  347. }
  348. int snd_cs4236_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
  349. {
  350. struct snd_pcm *pcm;
  351. int err;
  352. err = snd_wss_pcm(chip, device, &pcm);
  353. if (err < 0)
  354. return err;
  355. pcm->info_flags &= ~SNDRV_PCM_INFO_JOINT_DUPLEX;
  356. if (rpcm)
  357. *rpcm = pcm;
  358. return 0;
  359. }
  360. /*
  361. * MIXER
  362. */
  363. #define CS4236_SINGLE(xname, xindex, reg, shift, mask, invert) \
  364. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  365. .info = snd_cs4236_info_single, \
  366. .get = snd_cs4236_get_single, .put = snd_cs4236_put_single, \
  367. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  368. static int snd_cs4236_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  369. {
  370. int mask = (kcontrol->private_value >> 16) & 0xff;
  371. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  372. uinfo->count = 1;
  373. uinfo->value.integer.min = 0;
  374. uinfo->value.integer.max = mask;
  375. return 0;
  376. }
  377. static int snd_cs4236_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  378. {
  379. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  380. unsigned long flags;
  381. int reg = kcontrol->private_value & 0xff;
  382. int shift = (kcontrol->private_value >> 8) & 0xff;
  383. int mask = (kcontrol->private_value >> 16) & 0xff;
  384. int invert = (kcontrol->private_value >> 24) & 0xff;
  385. spin_lock_irqsave(&chip->reg_lock, flags);
  386. ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(reg)] >> shift) & mask;
  387. spin_unlock_irqrestore(&chip->reg_lock, flags);
  388. if (invert)
  389. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  390. return 0;
  391. }
  392. static int snd_cs4236_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  393. {
  394. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  395. unsigned long flags;
  396. int reg = kcontrol->private_value & 0xff;
  397. int shift = (kcontrol->private_value >> 8) & 0xff;
  398. int mask = (kcontrol->private_value >> 16) & 0xff;
  399. int invert = (kcontrol->private_value >> 24) & 0xff;
  400. int change;
  401. unsigned short val;
  402. val = (ucontrol->value.integer.value[0] & mask);
  403. if (invert)
  404. val = mask - val;
  405. val <<= shift;
  406. spin_lock_irqsave(&chip->reg_lock, flags);
  407. val = (chip->eimage[CS4236_REG(reg)] & ~(mask << shift)) | val;
  408. change = val != chip->eimage[CS4236_REG(reg)];
  409. snd_cs4236_ext_out(chip, reg, val);
  410. spin_unlock_irqrestore(&chip->reg_lock, flags);
  411. return change;
  412. }
  413. #define CS4236_SINGLEC(xname, xindex, reg, shift, mask, invert) \
  414. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  415. .info = snd_cs4236_info_single, \
  416. .get = snd_cs4236_get_singlec, .put = snd_cs4236_put_singlec, \
  417. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  418. static int snd_cs4236_get_singlec(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  419. {
  420. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  421. unsigned long flags;
  422. int reg = kcontrol->private_value & 0xff;
  423. int shift = (kcontrol->private_value >> 8) & 0xff;
  424. int mask = (kcontrol->private_value >> 16) & 0xff;
  425. int invert = (kcontrol->private_value >> 24) & 0xff;
  426. spin_lock_irqsave(&chip->reg_lock, flags);
  427. ucontrol->value.integer.value[0] = (chip->cimage[reg] >> shift) & mask;
  428. spin_unlock_irqrestore(&chip->reg_lock, flags);
  429. if (invert)
  430. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  431. return 0;
  432. }
  433. static int snd_cs4236_put_singlec(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  434. {
  435. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  436. unsigned long flags;
  437. int reg = kcontrol->private_value & 0xff;
  438. int shift = (kcontrol->private_value >> 8) & 0xff;
  439. int mask = (kcontrol->private_value >> 16) & 0xff;
  440. int invert = (kcontrol->private_value >> 24) & 0xff;
  441. int change;
  442. unsigned short val;
  443. val = (ucontrol->value.integer.value[0] & mask);
  444. if (invert)
  445. val = mask - val;
  446. val <<= shift;
  447. spin_lock_irqsave(&chip->reg_lock, flags);
  448. val = (chip->cimage[reg] & ~(mask << shift)) | val;
  449. change = val != chip->cimage[reg];
  450. snd_cs4236_ctrl_out(chip, reg, val);
  451. spin_unlock_irqrestore(&chip->reg_lock, flags);
  452. return change;
  453. }
  454. #define CS4236_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  455. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  456. .info = snd_cs4236_info_double, \
  457. .get = snd_cs4236_get_double, .put = snd_cs4236_put_double, \
  458. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  459. static int snd_cs4236_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  460. {
  461. int mask = (kcontrol->private_value >> 24) & 0xff;
  462. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  463. uinfo->count = 2;
  464. uinfo->value.integer.min = 0;
  465. uinfo->value.integer.max = mask;
  466. return 0;
  467. }
  468. static int snd_cs4236_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  469. {
  470. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  471. unsigned long flags;
  472. int left_reg = kcontrol->private_value & 0xff;
  473. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  474. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  475. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  476. int mask = (kcontrol->private_value >> 24) & 0xff;
  477. int invert = (kcontrol->private_value >> 22) & 1;
  478. spin_lock_irqsave(&chip->reg_lock, flags);
  479. ucontrol->value.integer.value[0] = (chip->eimage[CS4236_REG(left_reg)] >> shift_left) & mask;
  480. ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask;
  481. spin_unlock_irqrestore(&chip->reg_lock, flags);
  482. if (invert) {
  483. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  484. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  485. }
  486. return 0;
  487. }
  488. static int snd_cs4236_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  489. {
  490. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  491. unsigned long flags;
  492. int left_reg = kcontrol->private_value & 0xff;
  493. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  494. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  495. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  496. int mask = (kcontrol->private_value >> 24) & 0xff;
  497. int invert = (kcontrol->private_value >> 22) & 1;
  498. int change;
  499. unsigned short val1, val2;
  500. val1 = ucontrol->value.integer.value[0] & mask;
  501. val2 = ucontrol->value.integer.value[1] & mask;
  502. if (invert) {
  503. val1 = mask - val1;
  504. val2 = mask - val2;
  505. }
  506. val1 <<= shift_left;
  507. val2 <<= shift_right;
  508. spin_lock_irqsave(&chip->reg_lock, flags);
  509. if (left_reg != right_reg) {
  510. val1 = (chip->eimage[CS4236_REG(left_reg)] & ~(mask << shift_left)) | val1;
  511. val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2;
  512. change = val1 != chip->eimage[CS4236_REG(left_reg)] || val2 != chip->eimage[CS4236_REG(right_reg)];
  513. snd_cs4236_ext_out(chip, left_reg, val1);
  514. snd_cs4236_ext_out(chip, right_reg, val2);
  515. } else {
  516. val1 = (chip->eimage[CS4236_REG(left_reg)] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
  517. change = val1 != chip->eimage[CS4236_REG(left_reg)];
  518. snd_cs4236_ext_out(chip, left_reg, val1);
  519. }
  520. spin_unlock_irqrestore(&chip->reg_lock, flags);
  521. return change;
  522. }
  523. #define CS4236_DOUBLE1(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  524. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  525. .info = snd_cs4236_info_double, \
  526. .get = snd_cs4236_get_double1, .put = snd_cs4236_put_double1, \
  527. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  528. static int snd_cs4236_get_double1(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  529. {
  530. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  531. unsigned long flags;
  532. int left_reg = kcontrol->private_value & 0xff;
  533. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  534. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  535. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  536. int mask = (kcontrol->private_value >> 24) & 0xff;
  537. int invert = (kcontrol->private_value >> 22) & 1;
  538. spin_lock_irqsave(&chip->reg_lock, flags);
  539. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  540. ucontrol->value.integer.value[1] = (chip->eimage[CS4236_REG(right_reg)] >> shift_right) & mask;
  541. spin_unlock_irqrestore(&chip->reg_lock, flags);
  542. if (invert) {
  543. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  544. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  545. }
  546. return 0;
  547. }
  548. static int snd_cs4236_put_double1(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  549. {
  550. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  551. unsigned long flags;
  552. int left_reg = kcontrol->private_value & 0xff;
  553. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  554. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  555. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  556. int mask = (kcontrol->private_value >> 24) & 0xff;
  557. int invert = (kcontrol->private_value >> 22) & 1;
  558. int change;
  559. unsigned short val1, val2;
  560. val1 = ucontrol->value.integer.value[0] & mask;
  561. val2 = ucontrol->value.integer.value[1] & mask;
  562. if (invert) {
  563. val1 = mask - val1;
  564. val2 = mask - val2;
  565. }
  566. val1 <<= shift_left;
  567. val2 <<= shift_right;
  568. spin_lock_irqsave(&chip->reg_lock, flags);
  569. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  570. val2 = (chip->eimage[CS4236_REG(right_reg)] & ~(mask << shift_right)) | val2;
  571. change = val1 != chip->image[left_reg] || val2 != chip->eimage[CS4236_REG(right_reg)];
  572. snd_wss_out(chip, left_reg, val1);
  573. snd_cs4236_ext_out(chip, right_reg, val2);
  574. spin_unlock_irqrestore(&chip->reg_lock, flags);
  575. return change;
  576. }
  577. #define CS4236_MASTER_DIGITAL(xname, xindex) \
  578. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  579. .info = snd_cs4236_info_double, \
  580. .get = snd_cs4236_get_master_digital, .put = snd_cs4236_put_master_digital, \
  581. .private_value = 71 << 24 }
  582. static inline int snd_cs4236_mixer_master_digital_invert_volume(int vol)
  583. {
  584. return (vol < 64) ? 63 - vol : 64 + (71 - vol);
  585. }
  586. static int snd_cs4236_get_master_digital(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  587. {
  588. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  589. unsigned long flags;
  590. spin_lock_irqsave(&chip->reg_lock, flags);
  591. ucontrol->value.integer.value[0] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & 0x7f);
  592. ucontrol->value.integer.value[1] = snd_cs4236_mixer_master_digital_invert_volume(chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & 0x7f);
  593. spin_unlock_irqrestore(&chip->reg_lock, flags);
  594. return 0;
  595. }
  596. static int snd_cs4236_put_master_digital(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  597. {
  598. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  599. unsigned long flags;
  600. int change;
  601. unsigned short val1, val2;
  602. val1 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[0] & 0x7f);
  603. val2 = snd_cs4236_mixer_master_digital_invert_volume(ucontrol->value.integer.value[1] & 0x7f);
  604. spin_lock_irqsave(&chip->reg_lock, flags);
  605. val1 = (chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] & ~0x7f) | val1;
  606. val2 = (chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)] & ~0x7f) | val2;
  607. change = val1 != chip->eimage[CS4236_REG(CS4236_LEFT_MASTER)] || val2 != chip->eimage[CS4236_REG(CS4236_RIGHT_MASTER)];
  608. snd_cs4236_ext_out(chip, CS4236_LEFT_MASTER, val1);
  609. snd_cs4236_ext_out(chip, CS4236_RIGHT_MASTER, val2);
  610. spin_unlock_irqrestore(&chip->reg_lock, flags);
  611. return change;
  612. }
  613. #define CS4235_OUTPUT_ACCU(xname, xindex) \
  614. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  615. .info = snd_cs4236_info_double, \
  616. .get = snd_cs4235_get_output_accu, .put = snd_cs4235_put_output_accu, \
  617. .private_value = 3 << 24 }
  618. static inline int snd_cs4235_mixer_output_accu_get_volume(int vol)
  619. {
  620. switch ((vol >> 5) & 3) {
  621. case 0: return 1;
  622. case 1: return 3;
  623. case 2: return 2;
  624. case 3: return 0;
  625. }
  626. return 3;
  627. }
  628. static inline int snd_cs4235_mixer_output_accu_set_volume(int vol)
  629. {
  630. switch (vol & 3) {
  631. case 0: return 3 << 5;
  632. case 1: return 0 << 5;
  633. case 2: return 2 << 5;
  634. case 3: return 1 << 5;
  635. }
  636. return 1 << 5;
  637. }
  638. static int snd_cs4235_get_output_accu(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  639. {
  640. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  641. unsigned long flags;
  642. spin_lock_irqsave(&chip->reg_lock, flags);
  643. ucontrol->value.integer.value[0] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_LEFT_MASTER]);
  644. ucontrol->value.integer.value[1] = snd_cs4235_mixer_output_accu_get_volume(chip->image[CS4235_RIGHT_MASTER]);
  645. spin_unlock_irqrestore(&chip->reg_lock, flags);
  646. return 0;
  647. }
  648. static int snd_cs4235_put_output_accu(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  649. {
  650. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  651. unsigned long flags;
  652. int change;
  653. unsigned short val1, val2;
  654. val1 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[0]);
  655. val2 = snd_cs4235_mixer_output_accu_set_volume(ucontrol->value.integer.value[1]);
  656. spin_lock_irqsave(&chip->reg_lock, flags);
  657. val1 = (chip->image[CS4235_LEFT_MASTER] & ~(3 << 5)) | val1;
  658. val2 = (chip->image[CS4235_RIGHT_MASTER] & ~(3 << 5)) | val2;
  659. change = val1 != chip->image[CS4235_LEFT_MASTER] || val2 != chip->image[CS4235_RIGHT_MASTER];
  660. snd_wss_out(chip, CS4235_LEFT_MASTER, val1);
  661. snd_wss_out(chip, CS4235_RIGHT_MASTER, val2);
  662. spin_unlock_irqrestore(&chip->reg_lock, flags);
  663. return change;
  664. }
  665. static struct snd_kcontrol_new snd_cs4236_controls[] = {
  666. CS4236_DOUBLE("Master Digital Playback Switch", 0,
  667. CS4236_LEFT_MASTER, CS4236_RIGHT_MASTER, 7, 7, 1, 1),
  668. CS4236_DOUBLE("Master Digital Capture Switch", 0,
  669. CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1),
  670. CS4236_MASTER_DIGITAL("Master Digital Volume", 0),
  671. CS4236_DOUBLE("Capture Boost Volume", 0,
  672. CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1),
  673. WSS_DOUBLE("PCM Playback Switch", 0,
  674. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  675. WSS_DOUBLE("PCM Playback Volume", 0,
  676. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  677. CS4236_DOUBLE("DSP Playback Switch", 0,
  678. CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1),
  679. CS4236_DOUBLE("DSP Playback Volume", 0,
  680. CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 0, 0, 63, 1),
  681. CS4236_DOUBLE("FM Playback Switch", 0,
  682. CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1),
  683. CS4236_DOUBLE("FM Playback Volume", 0,
  684. CS4236_LEFT_FM, CS4236_RIGHT_FM, 0, 0, 63, 1),
  685. CS4236_DOUBLE("Wavetable Playback Switch", 0,
  686. CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1),
  687. CS4236_DOUBLE("Wavetable Playback Volume", 0,
  688. CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 0, 0, 63, 1),
  689. WSS_DOUBLE("Synth Playback Switch", 0,
  690. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  691. WSS_DOUBLE("Synth Volume", 0,
  692. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  693. WSS_DOUBLE("Synth Capture Switch", 0,
  694. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1),
  695. WSS_DOUBLE("Synth Capture Bypass", 0,
  696. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 5, 5, 1, 1),
  697. CS4236_DOUBLE("Mic Playback Switch", 0,
  698. CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1),
  699. CS4236_DOUBLE("Mic Capture Switch", 0,
  700. CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1),
  701. CS4236_DOUBLE("Mic Volume", 0, CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 0, 0, 31, 1),
  702. CS4236_DOUBLE("Mic Playback Boost (+20dB)", 0,
  703. CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 5, 5, 1, 0),
  704. WSS_DOUBLE("Line Playback Switch", 0,
  705. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  706. WSS_DOUBLE("Line Volume", 0,
  707. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  708. WSS_DOUBLE("Line Capture Switch", 0,
  709. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1),
  710. WSS_DOUBLE("Line Capture Bypass", 0,
  711. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 5, 5, 1, 1),
  712. WSS_DOUBLE("CD Playback Switch", 0,
  713. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  714. WSS_DOUBLE("CD Volume", 0,
  715. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  716. WSS_DOUBLE("CD Capture Switch", 0,
  717. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1),
  718. CS4236_DOUBLE1("Mono Output Playback Switch", 0,
  719. CS4231_MONO_CTRL, CS4236_RIGHT_MIX_CTRL, 6, 7, 1, 1),
  720. CS4236_DOUBLE1("Beep Playback Switch", 0,
  721. CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1),
  722. WSS_SINGLE("Beep Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
  723. WSS_SINGLE("Beep Bypass Playback Switch", 0, CS4231_MONO_CTRL, 5, 1, 0),
  724. WSS_DOUBLE("Capture Volume", 0,
  725. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  726. WSS_DOUBLE("Analog Loopback Capture Switch", 0,
  727. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0),
  728. WSS_SINGLE("Digital Loopback Playback Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  729. CS4236_DOUBLE1("Digital Loopback Playback Volume", 0,
  730. CS4231_LOOPBACK, CS4236_RIGHT_LOOPBACK, 2, 0, 63, 1)
  731. };
  732. static struct snd_kcontrol_new snd_cs4235_controls[] = {
  733. WSS_DOUBLE("Master Playback Switch", 0,
  734. CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 7, 7, 1, 1),
  735. WSS_DOUBLE("Master Playback Volume", 0,
  736. CS4235_LEFT_MASTER, CS4235_RIGHT_MASTER, 0, 0, 31, 1),
  737. CS4235_OUTPUT_ACCU("Playback Volume", 0),
  738. WSS_DOUBLE("Synth Playback Switch", 1,
  739. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  740. WSS_DOUBLE("Synth Capture Switch", 1,
  741. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 6, 6, 1, 1),
  742. WSS_DOUBLE("Synth Volume", 1,
  743. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  744. CS4236_DOUBLE("Capture Volume", 0,
  745. CS4236_LEFT_MIX_CTRL, CS4236_RIGHT_MIX_CTRL, 5, 5, 3, 1),
  746. WSS_DOUBLE("PCM Playback Switch", 0,
  747. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  748. WSS_DOUBLE("PCM Capture Switch", 0,
  749. CS4236_DAC_MUTE, CS4236_DAC_MUTE, 7, 6, 1, 1),
  750. WSS_DOUBLE("PCM Volume", 0,
  751. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  752. CS4236_DOUBLE("DSP Switch", 0, CS4236_LEFT_DSP, CS4236_RIGHT_DSP, 7, 7, 1, 1),
  753. CS4236_DOUBLE("FM Switch", 0, CS4236_LEFT_FM, CS4236_RIGHT_FM, 7, 7, 1, 1),
  754. CS4236_DOUBLE("Wavetable Switch", 0,
  755. CS4236_LEFT_WAVE, CS4236_RIGHT_WAVE, 7, 7, 1, 1),
  756. CS4236_DOUBLE("Mic Capture Switch", 0,
  757. CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 7, 7, 1, 1),
  758. CS4236_DOUBLE("Mic Playback Switch", 0,
  759. CS4236_LEFT_MIC, CS4236_RIGHT_MIC, 6, 6, 1, 1),
  760. CS4236_SINGLE("Mic Volume", 0, CS4236_LEFT_MIC, 0, 31, 1),
  761. CS4236_SINGLE("Mic Boost (+20dB)", 0, CS4236_LEFT_MIC, 5, 1, 0),
  762. WSS_DOUBLE("Line Playback Switch", 0,
  763. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  764. WSS_DOUBLE("Line Capture Switch", 0,
  765. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 6, 6, 1, 1),
  766. WSS_DOUBLE("Line Volume", 0,
  767. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  768. WSS_DOUBLE("CD Playback Switch", 1,
  769. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  770. WSS_DOUBLE("CD Capture Switch", 1,
  771. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 6, 6, 1, 1),
  772. WSS_DOUBLE("CD Volume", 1,
  773. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  774. CS4236_DOUBLE1("Beep Playback Switch", 0,
  775. CS4231_MONO_CTRL, CS4236_LEFT_MIX_CTRL, 7, 7, 1, 1),
  776. WSS_SINGLE("Beep Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
  777. WSS_DOUBLE("Analog Loopback Switch", 0,
  778. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 7, 7, 1, 0),
  779. };
  780. #define CS4236_IEC958_ENABLE(xname, xindex) \
  781. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  782. .info = snd_cs4236_info_single, \
  783. .get = snd_cs4236_get_iec958_switch, .put = snd_cs4236_put_iec958_switch, \
  784. .private_value = 1 << 16 }
  785. static int snd_cs4236_get_iec958_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  786. {
  787. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  788. unsigned long flags;
  789. spin_lock_irqsave(&chip->reg_lock, flags);
  790. ucontrol->value.integer.value[0] = chip->image[CS4231_ALT_FEATURE_1] & 0x02 ? 1 : 0;
  791. #if 0
  792. printk(KERN_DEBUG "get valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, "
  793. "C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n",
  794. snd_wss_in(chip, CS4231_ALT_FEATURE_1),
  795. snd_cs4236_ctrl_in(chip, 3),
  796. snd_cs4236_ctrl_in(chip, 4),
  797. snd_cs4236_ctrl_in(chip, 5),
  798. snd_cs4236_ctrl_in(chip, 6),
  799. snd_cs4236_ctrl_in(chip, 8));
  800. #endif
  801. spin_unlock_irqrestore(&chip->reg_lock, flags);
  802. return 0;
  803. }
  804. static int snd_cs4236_put_iec958_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  805. {
  806. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  807. unsigned long flags;
  808. int change;
  809. unsigned short enable, val;
  810. enable = ucontrol->value.integer.value[0] & 1;
  811. mutex_lock(&chip->mce_mutex);
  812. snd_wss_mce_up(chip);
  813. spin_lock_irqsave(&chip->reg_lock, flags);
  814. val = (chip->image[CS4231_ALT_FEATURE_1] & ~0x0e) | (0<<2) | (enable << 1);
  815. change = val != chip->image[CS4231_ALT_FEATURE_1];
  816. snd_wss_out(chip, CS4231_ALT_FEATURE_1, val);
  817. val = snd_cs4236_ctrl_in(chip, 4) | 0xc0;
  818. snd_cs4236_ctrl_out(chip, 4, val);
  819. udelay(100);
  820. val &= ~0x40;
  821. snd_cs4236_ctrl_out(chip, 4, val);
  822. spin_unlock_irqrestore(&chip->reg_lock, flags);
  823. snd_wss_mce_down(chip);
  824. mutex_unlock(&chip->mce_mutex);
  825. #if 0
  826. printk(KERN_DEBUG "set valid: ALT = 0x%x, C3 = 0x%x, C4 = 0x%x, "
  827. "C5 = 0x%x, C6 = 0x%x, C8 = 0x%x\n",
  828. snd_wss_in(chip, CS4231_ALT_FEATURE_1),
  829. snd_cs4236_ctrl_in(chip, 3),
  830. snd_cs4236_ctrl_in(chip, 4),
  831. snd_cs4236_ctrl_in(chip, 5),
  832. snd_cs4236_ctrl_in(chip, 6),
  833. snd_cs4236_ctrl_in(chip, 8));
  834. #endif
  835. return change;
  836. }
  837. static struct snd_kcontrol_new snd_cs4236_iec958_controls[] = {
  838. CS4236_IEC958_ENABLE("IEC958 Output Enable", 0),
  839. CS4236_SINGLEC("IEC958 Output Validity", 0, 4, 4, 1, 0),
  840. CS4236_SINGLEC("IEC958 Output User", 0, 4, 5, 1, 0),
  841. CS4236_SINGLEC("IEC958 Output CSBR", 0, 4, 6, 1, 0),
  842. CS4236_SINGLEC("IEC958 Output Channel Status Low", 0, 5, 1, 127, 0),
  843. CS4236_SINGLEC("IEC958 Output Channel Status High", 0, 6, 0, 255, 0)
  844. };
  845. static struct snd_kcontrol_new snd_cs4236_3d_controls_cs4235[] = {
  846. CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0),
  847. CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1)
  848. };
  849. static struct snd_kcontrol_new snd_cs4236_3d_controls_cs4237[] = {
  850. CS4236_SINGLEC("3D Control - Switch", 0, 3, 7, 1, 0),
  851. CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1),
  852. CS4236_SINGLEC("3D Control - Center", 0, 2, 0, 15, 1),
  853. CS4236_SINGLEC("3D Control - Mono", 0, 3, 6, 1, 0),
  854. CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0)
  855. };
  856. static struct snd_kcontrol_new snd_cs4236_3d_controls_cs4238[] = {
  857. CS4236_SINGLEC("3D Control - Switch", 0, 3, 4, 1, 0),
  858. CS4236_SINGLEC("3D Control - Space", 0, 2, 4, 15, 1),
  859. CS4236_SINGLEC("3D Control - Volume", 0, 2, 0, 15, 1),
  860. CS4236_SINGLEC("3D Control - IEC958", 0, 3, 5, 1, 0)
  861. };
  862. int snd_cs4236_mixer(struct snd_wss *chip)
  863. {
  864. struct snd_card *card;
  865. unsigned int idx, count;
  866. int err;
  867. struct snd_kcontrol_new *kcontrol;
  868. if (snd_BUG_ON(!chip || !chip->card))
  869. return -EINVAL;
  870. card = chip->card;
  871. strcpy(card->mixername, snd_wss_chip_id(chip));
  872. if (chip->hardware == WSS_HW_CS4235 ||
  873. chip->hardware == WSS_HW_CS4239) {
  874. for (idx = 0; idx < ARRAY_SIZE(snd_cs4235_controls); idx++) {
  875. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4235_controls[idx], chip))) < 0)
  876. return err;
  877. }
  878. } else {
  879. for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_controls); idx++) {
  880. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_controls[idx], chip))) < 0)
  881. return err;
  882. }
  883. }
  884. switch (chip->hardware) {
  885. case WSS_HW_CS4235:
  886. case WSS_HW_CS4239:
  887. count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4235);
  888. kcontrol = snd_cs4236_3d_controls_cs4235;
  889. break;
  890. case WSS_HW_CS4237B:
  891. count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4237);
  892. kcontrol = snd_cs4236_3d_controls_cs4237;
  893. break;
  894. case WSS_HW_CS4238B:
  895. count = ARRAY_SIZE(snd_cs4236_3d_controls_cs4238);
  896. kcontrol = snd_cs4236_3d_controls_cs4238;
  897. break;
  898. default:
  899. count = 0;
  900. kcontrol = NULL;
  901. }
  902. for (idx = 0; idx < count; idx++, kcontrol++) {
  903. if ((err = snd_ctl_add(card, snd_ctl_new1(kcontrol, chip))) < 0)
  904. return err;
  905. }
  906. if (chip->hardware == WSS_HW_CS4237B ||
  907. chip->hardware == WSS_HW_CS4238B) {
  908. for (idx = 0; idx < ARRAY_SIZE(snd_cs4236_iec958_controls); idx++) {
  909. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4236_iec958_controls[idx], chip))) < 0)
  910. return err;
  911. }
  912. }
  913. return 0;
  914. }