wss_lib.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
  4. *
  5. * Bugs:
  6. * - sometimes record brokes playback with WSS portion of
  7. * Yamaha OPL3-SA3 chip
  8. * - CS4231 (GUS MAX) - still trouble with occasional noises
  9. * - broken initialization?
  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. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/slab.h>
  31. #include <linux/ioport.h>
  32. #include <sound/core.h>
  33. #include <sound/wss.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/tlv.h>
  36. #include <asm/io.h>
  37. #include <asm/dma.h>
  38. #include <asm/irq.h>
  39. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  40. MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
  41. MODULE_LICENSE("GPL");
  42. #if 0
  43. #define SNDRV_DEBUG_MCE
  44. #endif
  45. /*
  46. * Some variables
  47. */
  48. static unsigned char freq_bits[14] = {
  49. /* 5510 */ 0x00 | CS4231_XTAL2,
  50. /* 6620 */ 0x0E | CS4231_XTAL2,
  51. /* 8000 */ 0x00 | CS4231_XTAL1,
  52. /* 9600 */ 0x0E | CS4231_XTAL1,
  53. /* 11025 */ 0x02 | CS4231_XTAL2,
  54. /* 16000 */ 0x02 | CS4231_XTAL1,
  55. /* 18900 */ 0x04 | CS4231_XTAL2,
  56. /* 22050 */ 0x06 | CS4231_XTAL2,
  57. /* 27042 */ 0x04 | CS4231_XTAL1,
  58. /* 32000 */ 0x06 | CS4231_XTAL1,
  59. /* 33075 */ 0x0C | CS4231_XTAL2,
  60. /* 37800 */ 0x08 | CS4231_XTAL2,
  61. /* 44100 */ 0x0A | CS4231_XTAL2,
  62. /* 48000 */ 0x0C | CS4231_XTAL1
  63. };
  64. static unsigned int rates[14] = {
  65. 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
  66. 27042, 32000, 33075, 37800, 44100, 48000
  67. };
  68. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  69. .count = ARRAY_SIZE(rates),
  70. .list = rates,
  71. .mask = 0,
  72. };
  73. static int snd_wss_xrate(struct snd_pcm_runtime *runtime)
  74. {
  75. return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  76. &hw_constraints_rates);
  77. }
  78. static unsigned char snd_wss_original_image[32] =
  79. {
  80. 0x00, /* 00/00 - lic */
  81. 0x00, /* 01/01 - ric */
  82. 0x9f, /* 02/02 - la1ic */
  83. 0x9f, /* 03/03 - ra1ic */
  84. 0x9f, /* 04/04 - la2ic */
  85. 0x9f, /* 05/05 - ra2ic */
  86. 0xbf, /* 06/06 - loc */
  87. 0xbf, /* 07/07 - roc */
  88. 0x20, /* 08/08 - pdfr */
  89. CS4231_AUTOCALIB, /* 09/09 - ic */
  90. 0x00, /* 0a/10 - pc */
  91. 0x00, /* 0b/11 - ti */
  92. CS4231_MODE2, /* 0c/12 - mi */
  93. 0xfc, /* 0d/13 - lbc */
  94. 0x00, /* 0e/14 - pbru */
  95. 0x00, /* 0f/15 - pbrl */
  96. 0x80, /* 10/16 - afei */
  97. 0x01, /* 11/17 - afeii */
  98. 0x9f, /* 12/18 - llic */
  99. 0x9f, /* 13/19 - rlic */
  100. 0x00, /* 14/20 - tlb */
  101. 0x00, /* 15/21 - thb */
  102. 0x00, /* 16/22 - la3mic/reserved */
  103. 0x00, /* 17/23 - ra3mic/reserved */
  104. 0x00, /* 18/24 - afs */
  105. 0x00, /* 19/25 - lamoc/version */
  106. 0xcf, /* 1a/26 - mioc */
  107. 0x00, /* 1b/27 - ramoc/reserved */
  108. 0x20, /* 1c/28 - cdfr */
  109. 0x00, /* 1d/29 - res4 */
  110. 0x00, /* 1e/30 - cbru */
  111. 0x00, /* 1f/31 - cbrl */
  112. };
  113. static unsigned char snd_opti93x_original_image[32] =
  114. {
  115. 0x00, /* 00/00 - l_mixout_outctrl */
  116. 0x00, /* 01/01 - r_mixout_outctrl */
  117. 0x88, /* 02/02 - l_cd_inctrl */
  118. 0x88, /* 03/03 - r_cd_inctrl */
  119. 0x88, /* 04/04 - l_a1/fm_inctrl */
  120. 0x88, /* 05/05 - r_a1/fm_inctrl */
  121. 0x80, /* 06/06 - l_dac_inctrl */
  122. 0x80, /* 07/07 - r_dac_inctrl */
  123. 0x00, /* 08/08 - ply_dataform_reg */
  124. 0x00, /* 09/09 - if_conf */
  125. 0x00, /* 0a/10 - pin_ctrl */
  126. 0x00, /* 0b/11 - err_init_reg */
  127. 0x0a, /* 0c/12 - id_reg */
  128. 0x00, /* 0d/13 - reserved */
  129. 0x00, /* 0e/14 - ply_upcount_reg */
  130. 0x00, /* 0f/15 - ply_lowcount_reg */
  131. 0x88, /* 10/16 - reserved/l_a1_inctrl */
  132. 0x88, /* 11/17 - reserved/r_a1_inctrl */
  133. 0x88, /* 12/18 - l_line_inctrl */
  134. 0x88, /* 13/19 - r_line_inctrl */
  135. 0x88, /* 14/20 - l_mic_inctrl */
  136. 0x88, /* 15/21 - r_mic_inctrl */
  137. 0x80, /* 16/22 - l_out_outctrl */
  138. 0x80, /* 17/23 - r_out_outctrl */
  139. 0x00, /* 18/24 - reserved */
  140. 0x00, /* 19/25 - reserved */
  141. 0x00, /* 1a/26 - reserved */
  142. 0x00, /* 1b/27 - reserved */
  143. 0x00, /* 1c/28 - cap_dataform_reg */
  144. 0x00, /* 1d/29 - reserved */
  145. 0x00, /* 1e/30 - cap_upcount_reg */
  146. 0x00 /* 1f/31 - cap_lowcount_reg */
  147. };
  148. /*
  149. * Basic I/O functions
  150. */
  151. static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
  152. {
  153. outb(val, chip->port + offset);
  154. }
  155. static inline u8 wss_inb(struct snd_wss *chip, u8 offset)
  156. {
  157. return inb(chip->port + offset);
  158. }
  159. static void snd_wss_wait(struct snd_wss *chip)
  160. {
  161. int timeout;
  162. for (timeout = 250;
  163. timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  164. timeout--)
  165. udelay(100);
  166. }
  167. static void snd_wss_outm(struct snd_wss *chip, unsigned char reg,
  168. unsigned char mask, unsigned char value)
  169. {
  170. unsigned char tmp = (chip->image[reg] & mask) | value;
  171. snd_wss_wait(chip);
  172. #ifdef CONFIG_SND_DEBUG
  173. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  174. snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  175. #endif
  176. chip->image[reg] = tmp;
  177. if (!chip->calibrate_mute) {
  178. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  179. wmb();
  180. wss_outb(chip, CS4231P(REG), tmp);
  181. mb();
  182. }
  183. }
  184. static void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
  185. unsigned char value)
  186. {
  187. int timeout;
  188. for (timeout = 250;
  189. timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  190. timeout--)
  191. udelay(10);
  192. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  193. wss_outb(chip, CS4231P(REG), value);
  194. mb();
  195. }
  196. void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
  197. {
  198. snd_wss_wait(chip);
  199. #ifdef CONFIG_SND_DEBUG
  200. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  201. snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  202. #endif
  203. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  204. wss_outb(chip, CS4231P(REG), value);
  205. chip->image[reg] = value;
  206. mb();
  207. snd_printdd("codec out - reg 0x%x = 0x%x\n",
  208. chip->mce_bit | reg, value);
  209. }
  210. EXPORT_SYMBOL(snd_wss_out);
  211. unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
  212. {
  213. snd_wss_wait(chip);
  214. #ifdef CONFIG_SND_DEBUG
  215. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  216. snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
  217. #endif
  218. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  219. mb();
  220. return wss_inb(chip, CS4231P(REG));
  221. }
  222. EXPORT_SYMBOL(snd_wss_in);
  223. void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
  224. unsigned char val)
  225. {
  226. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
  227. wss_outb(chip, CS4231P(REG),
  228. reg | (chip->image[CS4236_EXT_REG] & 0x01));
  229. wss_outb(chip, CS4231P(REG), val);
  230. chip->eimage[CS4236_REG(reg)] = val;
  231. #if 0
  232. printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
  233. #endif
  234. }
  235. EXPORT_SYMBOL(snd_cs4236_ext_out);
  236. unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
  237. {
  238. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
  239. wss_outb(chip, CS4231P(REG),
  240. reg | (chip->image[CS4236_EXT_REG] & 0x01));
  241. #if 1
  242. return wss_inb(chip, CS4231P(REG));
  243. #else
  244. {
  245. unsigned char res;
  246. res = wss_inb(chip, CS4231P(REG));
  247. printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
  248. return res;
  249. }
  250. #endif
  251. }
  252. EXPORT_SYMBOL(snd_cs4236_ext_in);
  253. #if 0
  254. static void snd_wss_debug(struct snd_wss *chip)
  255. {
  256. printk(KERN_DEBUG
  257. "CS4231 REGS: INDEX = 0x%02x "
  258. " STATUS = 0x%02x\n",
  259. wss_inb(chip, CS4231P(REGSEL),
  260. wss_inb(chip, CS4231P(STATUS)));
  261. printk(KERN_DEBUG
  262. " 0x00: left input = 0x%02x "
  263. " 0x10: alt 1 (CFIG 2) = 0x%02x\n",
  264. snd_wss_in(chip, 0x00),
  265. snd_wss_in(chip, 0x10));
  266. printk(KERN_DEBUG
  267. " 0x01: right input = 0x%02x "
  268. " 0x11: alt 2 (CFIG 3) = 0x%02x\n",
  269. snd_wss_in(chip, 0x01),
  270. snd_wss_in(chip, 0x11));
  271. printk(KERN_DEBUG
  272. " 0x02: GF1 left input = 0x%02x "
  273. " 0x12: left line in = 0x%02x\n",
  274. snd_wss_in(chip, 0x02),
  275. snd_wss_in(chip, 0x12));
  276. printk(KERN_DEBUG
  277. " 0x03: GF1 right input = 0x%02x "
  278. " 0x13: right line in = 0x%02x\n",
  279. snd_wss_in(chip, 0x03),
  280. snd_wss_in(chip, 0x13));
  281. printk(KERN_DEBUG
  282. " 0x04: CD left input = 0x%02x "
  283. " 0x14: timer low = 0x%02x\n",
  284. snd_wss_in(chip, 0x04),
  285. snd_wss_in(chip, 0x14));
  286. printk(KERN_DEBUG
  287. " 0x05: CD right input = 0x%02x "
  288. " 0x15: timer high = 0x%02x\n",
  289. snd_wss_in(chip, 0x05),
  290. snd_wss_in(chip, 0x15));
  291. printk(KERN_DEBUG
  292. " 0x06: left output = 0x%02x "
  293. " 0x16: left MIC (PnP) = 0x%02x\n",
  294. snd_wss_in(chip, 0x06),
  295. snd_wss_in(chip, 0x16));
  296. printk(KERN_DEBUG
  297. " 0x07: right output = 0x%02x "
  298. " 0x17: right MIC (PnP) = 0x%02x\n",
  299. snd_wss_in(chip, 0x07),
  300. snd_wss_in(chip, 0x17));
  301. printk(KERN_DEBUG
  302. " 0x08: playback format = 0x%02x "
  303. " 0x18: IRQ status = 0x%02x\n",
  304. snd_wss_in(chip, 0x08),
  305. snd_wss_in(chip, 0x18));
  306. printk(KERN_DEBUG
  307. " 0x09: iface (CFIG 1) = 0x%02x "
  308. " 0x19: left line out = 0x%02x\n",
  309. snd_wss_in(chip, 0x09),
  310. snd_wss_in(chip, 0x19));
  311. printk(KERN_DEBUG
  312. " 0x0a: pin control = 0x%02x "
  313. " 0x1a: mono control = 0x%02x\n",
  314. snd_wss_in(chip, 0x0a),
  315. snd_wss_in(chip, 0x1a));
  316. printk(KERN_DEBUG
  317. " 0x0b: init & status = 0x%02x "
  318. " 0x1b: right line out = 0x%02x\n",
  319. snd_wss_in(chip, 0x0b),
  320. snd_wss_in(chip, 0x1b));
  321. printk(KERN_DEBUG
  322. " 0x0c: revision & mode = 0x%02x "
  323. " 0x1c: record format = 0x%02x\n",
  324. snd_wss_in(chip, 0x0c),
  325. snd_wss_in(chip, 0x1c));
  326. printk(KERN_DEBUG
  327. " 0x0d: loopback = 0x%02x "
  328. " 0x1d: var freq (PnP) = 0x%02x\n",
  329. snd_wss_in(chip, 0x0d),
  330. snd_wss_in(chip, 0x1d));
  331. printk(KERN_DEBUG
  332. " 0x0e: ply upr count = 0x%02x "
  333. " 0x1e: ply lwr count = 0x%02x\n",
  334. snd_wss_in(chip, 0x0e),
  335. snd_wss_in(chip, 0x1e));
  336. printk(KERN_DEBUG
  337. " 0x0f: rec upr count = 0x%02x "
  338. " 0x1f: rec lwr count = 0x%02x\n",
  339. snd_wss_in(chip, 0x0f),
  340. snd_wss_in(chip, 0x1f));
  341. }
  342. #endif
  343. /*
  344. * CS4231 detection / MCE routines
  345. */
  346. static void snd_wss_busy_wait(struct snd_wss *chip)
  347. {
  348. int timeout;
  349. /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
  350. for (timeout = 5; timeout > 0; timeout--)
  351. wss_inb(chip, CS4231P(REGSEL));
  352. /* end of cleanup sequence */
  353. for (timeout = 25000;
  354. timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  355. timeout--)
  356. udelay(10);
  357. }
  358. void snd_wss_mce_up(struct snd_wss *chip)
  359. {
  360. unsigned long flags;
  361. int timeout;
  362. snd_wss_wait(chip);
  363. #ifdef CONFIG_SND_DEBUG
  364. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  365. snd_printk("mce_up - auto calibration time out (0)\n");
  366. #endif
  367. spin_lock_irqsave(&chip->reg_lock, flags);
  368. chip->mce_bit |= CS4231_MCE;
  369. timeout = wss_inb(chip, CS4231P(REGSEL));
  370. if (timeout == 0x80)
  371. snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
  372. if (!(timeout & CS4231_MCE))
  373. wss_outb(chip, CS4231P(REGSEL),
  374. chip->mce_bit | (timeout & 0x1f));
  375. spin_unlock_irqrestore(&chip->reg_lock, flags);
  376. }
  377. EXPORT_SYMBOL(snd_wss_mce_up);
  378. void snd_wss_mce_down(struct snd_wss *chip)
  379. {
  380. unsigned long flags;
  381. unsigned long end_time;
  382. int timeout;
  383. int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848;
  384. snd_wss_busy_wait(chip);
  385. #ifdef CONFIG_SND_DEBUG
  386. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  387. snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
  388. #endif
  389. spin_lock_irqsave(&chip->reg_lock, flags);
  390. chip->mce_bit &= ~CS4231_MCE;
  391. timeout = wss_inb(chip, CS4231P(REGSEL));
  392. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  393. spin_unlock_irqrestore(&chip->reg_lock, flags);
  394. if (timeout == 0x80)
  395. snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  396. if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask))
  397. return;
  398. /*
  399. * Wait for (possible -- during init auto-calibration may not be set)
  400. * calibration process to start. Needs upto 5 sample periods on AD1848
  401. * which at the slowest possible rate of 5.5125 kHz means 907 us.
  402. */
  403. msleep(1);
  404. snd_printdd("(1) jiffies = %lu\n", jiffies);
  405. /* check condition up to 250 ms */
  406. end_time = jiffies + msecs_to_jiffies(250);
  407. while (snd_wss_in(chip, CS4231_TEST_INIT) &
  408. CS4231_CALIB_IN_PROGRESS) {
  409. if (time_after(jiffies, end_time)) {
  410. snd_printk(KERN_ERR "mce_down - "
  411. "auto calibration time out (2)\n");
  412. return;
  413. }
  414. msleep(1);
  415. }
  416. snd_printdd("(2) jiffies = %lu\n", jiffies);
  417. /* check condition up to 100 ms */
  418. end_time = jiffies + msecs_to_jiffies(100);
  419. while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
  420. if (time_after(jiffies, end_time)) {
  421. snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
  422. return;
  423. }
  424. msleep(1);
  425. }
  426. snd_printdd("(3) jiffies = %lu\n", jiffies);
  427. snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
  428. }
  429. EXPORT_SYMBOL(snd_wss_mce_down);
  430. static unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
  431. {
  432. switch (format & 0xe0) {
  433. case CS4231_LINEAR_16:
  434. case CS4231_LINEAR_16_BIG:
  435. size >>= 1;
  436. break;
  437. case CS4231_ADPCM_16:
  438. return size >> 2;
  439. }
  440. if (format & CS4231_STEREO)
  441. size >>= 1;
  442. return size;
  443. }
  444. static int snd_wss_trigger(struct snd_pcm_substream *substream,
  445. int cmd)
  446. {
  447. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  448. int result = 0;
  449. unsigned int what;
  450. struct snd_pcm_substream *s;
  451. int do_start;
  452. switch (cmd) {
  453. case SNDRV_PCM_TRIGGER_START:
  454. case SNDRV_PCM_TRIGGER_RESUME:
  455. do_start = 1; break;
  456. case SNDRV_PCM_TRIGGER_STOP:
  457. case SNDRV_PCM_TRIGGER_SUSPEND:
  458. do_start = 0; break;
  459. default:
  460. return -EINVAL;
  461. }
  462. what = 0;
  463. snd_pcm_group_for_each_entry(s, substream) {
  464. if (s == chip->playback_substream) {
  465. what |= CS4231_PLAYBACK_ENABLE;
  466. snd_pcm_trigger_done(s, substream);
  467. } else if (s == chip->capture_substream) {
  468. what |= CS4231_RECORD_ENABLE;
  469. snd_pcm_trigger_done(s, substream);
  470. }
  471. }
  472. spin_lock(&chip->reg_lock);
  473. if (do_start) {
  474. chip->image[CS4231_IFACE_CTRL] |= what;
  475. if (chip->trigger)
  476. chip->trigger(chip, what, 1);
  477. } else {
  478. chip->image[CS4231_IFACE_CTRL] &= ~what;
  479. if (chip->trigger)
  480. chip->trigger(chip, what, 0);
  481. }
  482. snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  483. spin_unlock(&chip->reg_lock);
  484. #if 0
  485. snd_wss_debug(chip);
  486. #endif
  487. return result;
  488. }
  489. /*
  490. * CODEC I/O
  491. */
  492. static unsigned char snd_wss_get_rate(unsigned int rate)
  493. {
  494. int i;
  495. for (i = 0; i < ARRAY_SIZE(rates); i++)
  496. if (rate == rates[i])
  497. return freq_bits[i];
  498. // snd_BUG();
  499. return freq_bits[ARRAY_SIZE(rates) - 1];
  500. }
  501. static unsigned char snd_wss_get_format(struct snd_wss *chip,
  502. int format,
  503. int channels)
  504. {
  505. unsigned char rformat;
  506. rformat = CS4231_LINEAR_8;
  507. switch (format) {
  508. case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
  509. case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
  510. case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
  511. case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
  512. case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
  513. }
  514. if (channels > 1)
  515. rformat |= CS4231_STEREO;
  516. #if 0
  517. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  518. #endif
  519. return rformat;
  520. }
  521. static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
  522. {
  523. unsigned long flags;
  524. mute = mute ? 1 : 0;
  525. spin_lock_irqsave(&chip->reg_lock, flags);
  526. if (chip->calibrate_mute == mute) {
  527. spin_unlock_irqrestore(&chip->reg_lock, flags);
  528. return;
  529. }
  530. if (!mute) {
  531. snd_wss_dout(chip, CS4231_LEFT_INPUT,
  532. chip->image[CS4231_LEFT_INPUT]);
  533. snd_wss_dout(chip, CS4231_RIGHT_INPUT,
  534. chip->image[CS4231_RIGHT_INPUT]);
  535. snd_wss_dout(chip, CS4231_LOOPBACK,
  536. chip->image[CS4231_LOOPBACK]);
  537. }
  538. snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
  539. mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
  540. snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
  541. mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
  542. snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
  543. mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
  544. snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
  545. mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
  546. snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
  547. mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
  548. snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
  549. mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
  550. if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
  551. snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
  552. mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
  553. snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
  554. mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
  555. snd_wss_dout(chip, CS4231_MONO_CTRL,
  556. mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
  557. }
  558. if (chip->hardware == WSS_HW_INTERWAVE) {
  559. snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
  560. mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
  561. snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
  562. mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
  563. snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
  564. mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
  565. snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
  566. mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
  567. }
  568. chip->calibrate_mute = mute;
  569. spin_unlock_irqrestore(&chip->reg_lock, flags);
  570. }
  571. static void snd_wss_playback_format(struct snd_wss *chip,
  572. struct snd_pcm_hw_params *params,
  573. unsigned char pdfr)
  574. {
  575. unsigned long flags;
  576. int full_calib = 1;
  577. mutex_lock(&chip->mce_mutex);
  578. snd_wss_calibrate_mute(chip, 1);
  579. if (chip->hardware == WSS_HW_CS4231A ||
  580. (chip->hardware & WSS_HW_CS4232_MASK)) {
  581. spin_lock_irqsave(&chip->reg_lock, flags);
  582. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
  583. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  584. chip->image[CS4231_ALT_FEATURE_1] | 0x10);
  585. chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
  586. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  587. chip->image[CS4231_PLAYBK_FORMAT]);
  588. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  589. chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
  590. udelay(100); /* Fixes audible clicks at least on GUS MAX */
  591. full_calib = 0;
  592. }
  593. spin_unlock_irqrestore(&chip->reg_lock, flags);
  594. }
  595. if (full_calib) {
  596. snd_wss_mce_up(chip);
  597. spin_lock_irqsave(&chip->reg_lock, flags);
  598. if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
  599. if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
  600. pdfr = (pdfr & 0xf0) |
  601. (chip->image[CS4231_REC_FORMAT] & 0x0f);
  602. } else {
  603. chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
  604. }
  605. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
  606. spin_unlock_irqrestore(&chip->reg_lock, flags);
  607. if (chip->hardware == WSS_HW_OPL3SA2)
  608. udelay(100); /* this seems to help */
  609. snd_wss_mce_down(chip);
  610. }
  611. snd_wss_calibrate_mute(chip, 0);
  612. mutex_unlock(&chip->mce_mutex);
  613. }
  614. static void snd_wss_capture_format(struct snd_wss *chip,
  615. struct snd_pcm_hw_params *params,
  616. unsigned char cdfr)
  617. {
  618. unsigned long flags;
  619. int full_calib = 1;
  620. mutex_lock(&chip->mce_mutex);
  621. snd_wss_calibrate_mute(chip, 1);
  622. if (chip->hardware == WSS_HW_CS4231A ||
  623. (chip->hardware & WSS_HW_CS4232_MASK)) {
  624. spin_lock_irqsave(&chip->reg_lock, flags);
  625. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
  626. (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  627. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  628. chip->image[CS4231_ALT_FEATURE_1] | 0x20);
  629. snd_wss_out(chip, CS4231_REC_FORMAT,
  630. chip->image[CS4231_REC_FORMAT] = cdfr);
  631. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  632. chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
  633. full_calib = 0;
  634. }
  635. spin_unlock_irqrestore(&chip->reg_lock, flags);
  636. }
  637. if (full_calib) {
  638. snd_wss_mce_up(chip);
  639. spin_lock_irqsave(&chip->reg_lock, flags);
  640. if (chip->hardware != WSS_HW_INTERWAVE &&
  641. !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  642. if (chip->single_dma)
  643. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
  644. else
  645. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  646. (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
  647. (cdfr & 0x0f));
  648. spin_unlock_irqrestore(&chip->reg_lock, flags);
  649. snd_wss_mce_down(chip);
  650. snd_wss_mce_up(chip);
  651. spin_lock_irqsave(&chip->reg_lock, flags);
  652. }
  653. if (chip->hardware & WSS_HW_AD1848_MASK)
  654. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
  655. else
  656. snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
  657. spin_unlock_irqrestore(&chip->reg_lock, flags);
  658. snd_wss_mce_down(chip);
  659. }
  660. snd_wss_calibrate_mute(chip, 0);
  661. mutex_unlock(&chip->mce_mutex);
  662. }
  663. /*
  664. * Timer interface
  665. */
  666. static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
  667. {
  668. struct snd_wss *chip = snd_timer_chip(timer);
  669. if (chip->hardware & WSS_HW_CS4236B_MASK)
  670. return 14467;
  671. else
  672. return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
  673. }
  674. static int snd_wss_timer_start(struct snd_timer *timer)
  675. {
  676. unsigned long flags;
  677. unsigned int ticks;
  678. struct snd_wss *chip = snd_timer_chip(timer);
  679. spin_lock_irqsave(&chip->reg_lock, flags);
  680. ticks = timer->sticks;
  681. if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
  682. (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
  683. (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
  684. chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
  685. snd_wss_out(chip, CS4231_TIMER_HIGH,
  686. chip->image[CS4231_TIMER_HIGH]);
  687. chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
  688. snd_wss_out(chip, CS4231_TIMER_LOW,
  689. chip->image[CS4231_TIMER_LOW]);
  690. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  691. chip->image[CS4231_ALT_FEATURE_1] |
  692. CS4231_TIMER_ENABLE);
  693. }
  694. spin_unlock_irqrestore(&chip->reg_lock, flags);
  695. return 0;
  696. }
  697. static int snd_wss_timer_stop(struct snd_timer *timer)
  698. {
  699. unsigned long flags;
  700. struct snd_wss *chip = snd_timer_chip(timer);
  701. spin_lock_irqsave(&chip->reg_lock, flags);
  702. chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
  703. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  704. chip->image[CS4231_ALT_FEATURE_1]);
  705. spin_unlock_irqrestore(&chip->reg_lock, flags);
  706. return 0;
  707. }
  708. static void snd_wss_init(struct snd_wss *chip)
  709. {
  710. unsigned long flags;
  711. snd_wss_mce_down(chip);
  712. #ifdef SNDRV_DEBUG_MCE
  713. snd_printk("init: (1)\n");
  714. #endif
  715. snd_wss_mce_up(chip);
  716. spin_lock_irqsave(&chip->reg_lock, flags);
  717. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
  718. CS4231_PLAYBACK_PIO |
  719. CS4231_RECORD_ENABLE |
  720. CS4231_RECORD_PIO |
  721. CS4231_CALIB_MODE);
  722. chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
  723. snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  724. spin_unlock_irqrestore(&chip->reg_lock, flags);
  725. snd_wss_mce_down(chip);
  726. #ifdef SNDRV_DEBUG_MCE
  727. snd_printk("init: (2)\n");
  728. #endif
  729. snd_wss_mce_up(chip);
  730. spin_lock_irqsave(&chip->reg_lock, flags);
  731. snd_wss_out(chip,
  732. CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
  733. spin_unlock_irqrestore(&chip->reg_lock, flags);
  734. snd_wss_mce_down(chip);
  735. #ifdef SNDRV_DEBUG_MCE
  736. snd_printk("init: (3) - afei = 0x%x\n",
  737. chip->image[CS4231_ALT_FEATURE_1]);
  738. #endif
  739. spin_lock_irqsave(&chip->reg_lock, flags);
  740. snd_wss_out(chip, CS4231_ALT_FEATURE_2,
  741. chip->image[CS4231_ALT_FEATURE_2]);
  742. spin_unlock_irqrestore(&chip->reg_lock, flags);
  743. snd_wss_mce_up(chip);
  744. spin_lock_irqsave(&chip->reg_lock, flags);
  745. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  746. chip->image[CS4231_PLAYBK_FORMAT]);
  747. spin_unlock_irqrestore(&chip->reg_lock, flags);
  748. snd_wss_mce_down(chip);
  749. #ifdef SNDRV_DEBUG_MCE
  750. snd_printk("init: (4)\n");
  751. #endif
  752. snd_wss_mce_up(chip);
  753. spin_lock_irqsave(&chip->reg_lock, flags);
  754. if (!(chip->hardware & WSS_HW_AD1848_MASK))
  755. snd_wss_out(chip, CS4231_REC_FORMAT,
  756. chip->image[CS4231_REC_FORMAT]);
  757. spin_unlock_irqrestore(&chip->reg_lock, flags);
  758. snd_wss_mce_down(chip);
  759. #ifdef SNDRV_DEBUG_MCE
  760. snd_printk("init: (5)\n");
  761. #endif
  762. }
  763. static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
  764. {
  765. unsigned long flags;
  766. mutex_lock(&chip->open_mutex);
  767. if ((chip->mode & mode) ||
  768. ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
  769. mutex_unlock(&chip->open_mutex);
  770. return -EAGAIN;
  771. }
  772. if (chip->mode & WSS_MODE_OPEN) {
  773. chip->mode |= mode;
  774. mutex_unlock(&chip->open_mutex);
  775. return 0;
  776. }
  777. /* ok. now enable and ack CODEC IRQ */
  778. spin_lock_irqsave(&chip->reg_lock, flags);
  779. if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
  780. snd_wss_out(chip, CS4231_IRQ_STATUS,
  781. CS4231_PLAYBACK_IRQ |
  782. CS4231_RECORD_IRQ |
  783. CS4231_TIMER_IRQ);
  784. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  785. }
  786. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  787. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  788. chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
  789. snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  790. if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
  791. snd_wss_out(chip, CS4231_IRQ_STATUS,
  792. CS4231_PLAYBACK_IRQ |
  793. CS4231_RECORD_IRQ |
  794. CS4231_TIMER_IRQ);
  795. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  796. }
  797. spin_unlock_irqrestore(&chip->reg_lock, flags);
  798. chip->mode = mode;
  799. mutex_unlock(&chip->open_mutex);
  800. return 0;
  801. }
  802. static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
  803. {
  804. unsigned long flags;
  805. mutex_lock(&chip->open_mutex);
  806. chip->mode &= ~mode;
  807. if (chip->mode & WSS_MODE_OPEN) {
  808. mutex_unlock(&chip->open_mutex);
  809. return;
  810. }
  811. snd_wss_calibrate_mute(chip, 1);
  812. /* disable IRQ */
  813. spin_lock_irqsave(&chip->reg_lock, flags);
  814. if (!(chip->hardware & WSS_HW_AD1848_MASK))
  815. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  816. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  817. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  818. chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
  819. snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  820. /* now disable record & playback */
  821. if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  822. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
  823. spin_unlock_irqrestore(&chip->reg_lock, flags);
  824. snd_wss_mce_up(chip);
  825. spin_lock_irqsave(&chip->reg_lock, flags);
  826. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  827. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  828. snd_wss_out(chip, CS4231_IFACE_CTRL,
  829. chip->image[CS4231_IFACE_CTRL]);
  830. spin_unlock_irqrestore(&chip->reg_lock, flags);
  831. snd_wss_mce_down(chip);
  832. spin_lock_irqsave(&chip->reg_lock, flags);
  833. }
  834. /* clear IRQ again */
  835. if (!(chip->hardware & WSS_HW_AD1848_MASK))
  836. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  837. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  838. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  839. spin_unlock_irqrestore(&chip->reg_lock, flags);
  840. snd_wss_calibrate_mute(chip, 0);
  841. chip->mode = 0;
  842. mutex_unlock(&chip->open_mutex);
  843. }
  844. /*
  845. * timer open/close
  846. */
  847. static int snd_wss_timer_open(struct snd_timer *timer)
  848. {
  849. struct snd_wss *chip = snd_timer_chip(timer);
  850. snd_wss_open(chip, WSS_MODE_TIMER);
  851. return 0;
  852. }
  853. static int snd_wss_timer_close(struct snd_timer *timer)
  854. {
  855. struct snd_wss *chip = snd_timer_chip(timer);
  856. snd_wss_close(chip, WSS_MODE_TIMER);
  857. return 0;
  858. }
  859. static struct snd_timer_hardware snd_wss_timer_table =
  860. {
  861. .flags = SNDRV_TIMER_HW_AUTO,
  862. .resolution = 9945,
  863. .ticks = 65535,
  864. .open = snd_wss_timer_open,
  865. .close = snd_wss_timer_close,
  866. .c_resolution = snd_wss_timer_resolution,
  867. .start = snd_wss_timer_start,
  868. .stop = snd_wss_timer_stop,
  869. };
  870. /*
  871. * ok.. exported functions..
  872. */
  873. static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
  874. struct snd_pcm_hw_params *hw_params)
  875. {
  876. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  877. unsigned char new_pdfr;
  878. int err;
  879. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  880. return err;
  881. new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
  882. params_channels(hw_params)) |
  883. snd_wss_get_rate(params_rate(hw_params));
  884. chip->set_playback_format(chip, hw_params, new_pdfr);
  885. return 0;
  886. }
  887. static int snd_wss_playback_hw_free(struct snd_pcm_substream *substream)
  888. {
  889. return snd_pcm_lib_free_pages(substream);
  890. }
  891. static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
  892. {
  893. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  894. struct snd_pcm_runtime *runtime = substream->runtime;
  895. unsigned long flags;
  896. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  897. unsigned int count = snd_pcm_lib_period_bytes(substream);
  898. spin_lock_irqsave(&chip->reg_lock, flags);
  899. chip->p_dma_size = size;
  900. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
  901. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  902. count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
  903. snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  904. snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  905. spin_unlock_irqrestore(&chip->reg_lock, flags);
  906. #if 0
  907. snd_wss_debug(chip);
  908. #endif
  909. return 0;
  910. }
  911. static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
  912. struct snd_pcm_hw_params *hw_params)
  913. {
  914. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  915. unsigned char new_cdfr;
  916. int err;
  917. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  918. return err;
  919. new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
  920. params_channels(hw_params)) |
  921. snd_wss_get_rate(params_rate(hw_params));
  922. chip->set_capture_format(chip, hw_params, new_cdfr);
  923. return 0;
  924. }
  925. static int snd_wss_capture_hw_free(struct snd_pcm_substream *substream)
  926. {
  927. return snd_pcm_lib_free_pages(substream);
  928. }
  929. static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
  930. {
  931. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  932. struct snd_pcm_runtime *runtime = substream->runtime;
  933. unsigned long flags;
  934. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  935. unsigned int count = snd_pcm_lib_period_bytes(substream);
  936. spin_lock_irqsave(&chip->reg_lock, flags);
  937. chip->c_dma_size = size;
  938. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  939. snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  940. if (chip->hardware & WSS_HW_AD1848_MASK)
  941. count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT],
  942. count);
  943. else
  944. count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT],
  945. count);
  946. count--;
  947. if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
  948. snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  949. snd_wss_out(chip, CS4231_PLY_UPR_CNT,
  950. (unsigned char) (count >> 8));
  951. } else {
  952. snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
  953. snd_wss_out(chip, CS4231_REC_UPR_CNT,
  954. (unsigned char) (count >> 8));
  955. }
  956. spin_unlock_irqrestore(&chip->reg_lock, flags);
  957. return 0;
  958. }
  959. void snd_wss_overrange(struct snd_wss *chip)
  960. {
  961. unsigned long flags;
  962. unsigned char res;
  963. spin_lock_irqsave(&chip->reg_lock, flags);
  964. res = snd_wss_in(chip, CS4231_TEST_INIT);
  965. spin_unlock_irqrestore(&chip->reg_lock, flags);
  966. if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
  967. chip->capture_substream->runtime->overrange++;
  968. }
  969. EXPORT_SYMBOL(snd_wss_overrange);
  970. irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
  971. {
  972. struct snd_wss *chip = dev_id;
  973. unsigned char status;
  974. status = snd_wss_in(chip, CS4231_IRQ_STATUS);
  975. if (status & CS4231_TIMER_IRQ) {
  976. if (chip->timer)
  977. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  978. }
  979. if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
  980. if (status & CS4231_PLAYBACK_IRQ) {
  981. if (chip->mode & WSS_MODE_PLAY) {
  982. if (chip->playback_substream)
  983. snd_pcm_period_elapsed(chip->playback_substream);
  984. }
  985. if (chip->mode & WSS_MODE_RECORD) {
  986. if (chip->capture_substream) {
  987. snd_wss_overrange(chip);
  988. snd_pcm_period_elapsed(chip->capture_substream);
  989. }
  990. }
  991. }
  992. } else {
  993. if (status & CS4231_PLAYBACK_IRQ) {
  994. if (chip->playback_substream)
  995. snd_pcm_period_elapsed(chip->playback_substream);
  996. }
  997. if (status & CS4231_RECORD_IRQ) {
  998. if (chip->capture_substream) {
  999. snd_wss_overrange(chip);
  1000. snd_pcm_period_elapsed(chip->capture_substream);
  1001. }
  1002. }
  1003. }
  1004. spin_lock(&chip->reg_lock);
  1005. snd_wss_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
  1006. spin_unlock(&chip->reg_lock);
  1007. return IRQ_HANDLED;
  1008. }
  1009. EXPORT_SYMBOL(snd_wss_interrupt);
  1010. static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
  1011. {
  1012. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1013. size_t ptr;
  1014. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
  1015. return 0;
  1016. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  1017. return bytes_to_frames(substream->runtime, ptr);
  1018. }
  1019. static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
  1020. {
  1021. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1022. size_t ptr;
  1023. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
  1024. return 0;
  1025. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  1026. return bytes_to_frames(substream->runtime, ptr);
  1027. }
  1028. /*
  1029. */
  1030. static int snd_wss_probe(struct snd_wss *chip)
  1031. {
  1032. unsigned long flags;
  1033. int i, id, rev;
  1034. unsigned char *ptr;
  1035. unsigned int hw;
  1036. #if 0
  1037. snd_wss_debug(chip);
  1038. #endif
  1039. id = 0;
  1040. for (i = 0; i < 50; i++) {
  1041. mb();
  1042. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  1043. udelay(2000);
  1044. else {
  1045. spin_lock_irqsave(&chip->reg_lock, flags);
  1046. snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
  1047. id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
  1048. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1049. if (id == 0x0a)
  1050. break; /* this is valid value */
  1051. }
  1052. }
  1053. snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
  1054. if (id != 0x0a)
  1055. return -ENODEV; /* no valid device found */
  1056. hw = chip->hardware;
  1057. if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
  1058. rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
  1059. snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
  1060. if (rev == 0x80) {
  1061. unsigned char tmp = snd_wss_in(chip, 23);
  1062. snd_wss_out(chip, 23, ~tmp);
  1063. if (snd_wss_in(chip, 23) != tmp)
  1064. chip->hardware = WSS_HW_AD1845;
  1065. else
  1066. chip->hardware = WSS_HW_CS4231;
  1067. } else if (rev == 0xa0) {
  1068. chip->hardware = WSS_HW_CS4231A;
  1069. } else if (rev == 0xa2) {
  1070. chip->hardware = WSS_HW_CS4232;
  1071. } else if (rev == 0xb2) {
  1072. chip->hardware = WSS_HW_CS4232A;
  1073. } else if (rev == 0x83) {
  1074. chip->hardware = WSS_HW_CS4236;
  1075. } else if (rev == 0x03) {
  1076. chip->hardware = WSS_HW_CS4236B;
  1077. } else {
  1078. snd_printk("unknown CS chip with version 0x%x\n", rev);
  1079. return -ENODEV; /* unknown CS4231 chip? */
  1080. }
  1081. }
  1082. spin_lock_irqsave(&chip->reg_lock, flags);
  1083. wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
  1084. wss_outb(chip, CS4231P(STATUS), 0);
  1085. mb();
  1086. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1087. chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
  1088. switch (chip->hardware) {
  1089. case WSS_HW_INTERWAVE:
  1090. chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
  1091. break;
  1092. case WSS_HW_CS4235:
  1093. case WSS_HW_CS4236B:
  1094. case WSS_HW_CS4237B:
  1095. case WSS_HW_CS4238B:
  1096. case WSS_HW_CS4239:
  1097. if (hw == WSS_HW_DETECT3)
  1098. chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
  1099. else
  1100. chip->hardware = WSS_HW_CS4236;
  1101. break;
  1102. }
  1103. chip->image[CS4231_IFACE_CTRL] =
  1104. (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
  1105. (chip->single_dma ? CS4231_SINGLE_DMA : 0);
  1106. if (chip->hardware != WSS_HW_OPTI93X) {
  1107. chip->image[CS4231_ALT_FEATURE_1] = 0x80;
  1108. chip->image[CS4231_ALT_FEATURE_2] =
  1109. chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
  1110. }
  1111. ptr = (unsigned char *) &chip->image;
  1112. snd_wss_mce_down(chip);
  1113. spin_lock_irqsave(&chip->reg_lock, flags);
  1114. for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
  1115. snd_wss_out(chip, i, *ptr++);
  1116. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1117. snd_wss_mce_up(chip);
  1118. snd_wss_mce_down(chip);
  1119. mdelay(2);
  1120. /* ok.. try check hardware version for CS4236+ chips */
  1121. if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
  1122. if (chip->hardware == WSS_HW_CS4236B) {
  1123. rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
  1124. snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
  1125. id = snd_cs4236_ext_in(chip, CS4236_VERSION);
  1126. snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
  1127. snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
  1128. if ((id & 0x1f) == 0x1d) { /* CS4235 */
  1129. chip->hardware = WSS_HW_CS4235;
  1130. switch (id >> 5) {
  1131. case 4:
  1132. case 5:
  1133. case 6:
  1134. break;
  1135. default:
  1136. snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
  1137. }
  1138. } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
  1139. switch (id >> 5) {
  1140. case 4:
  1141. case 5:
  1142. case 6:
  1143. case 7:
  1144. chip->hardware = WSS_HW_CS4236B;
  1145. break;
  1146. default:
  1147. snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
  1148. }
  1149. } else if ((id & 0x1f) == 0x08) { /* CS4237B */
  1150. chip->hardware = WSS_HW_CS4237B;
  1151. switch (id >> 5) {
  1152. case 4:
  1153. case 5:
  1154. case 6:
  1155. case 7:
  1156. break;
  1157. default:
  1158. snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
  1159. }
  1160. } else if ((id & 0x1f) == 0x09) { /* CS4238B */
  1161. chip->hardware = WSS_HW_CS4238B;
  1162. switch (id >> 5) {
  1163. case 5:
  1164. case 6:
  1165. case 7:
  1166. break;
  1167. default:
  1168. snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
  1169. }
  1170. } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
  1171. chip->hardware = WSS_HW_CS4239;
  1172. switch (id >> 5) {
  1173. case 4:
  1174. case 5:
  1175. case 6:
  1176. break;
  1177. default:
  1178. snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
  1179. }
  1180. } else {
  1181. snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
  1182. }
  1183. }
  1184. }
  1185. return 0; /* all things are ok.. */
  1186. }
  1187. /*
  1188. */
  1189. static struct snd_pcm_hardware snd_wss_playback =
  1190. {
  1191. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1192. SNDRV_PCM_INFO_MMAP_VALID |
  1193. SNDRV_PCM_INFO_RESUME |
  1194. SNDRV_PCM_INFO_SYNC_START),
  1195. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1196. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1197. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1198. .rate_min = 5510,
  1199. .rate_max = 48000,
  1200. .channels_min = 1,
  1201. .channels_max = 2,
  1202. .buffer_bytes_max = (128*1024),
  1203. .period_bytes_min = 64,
  1204. .period_bytes_max = (128*1024),
  1205. .periods_min = 1,
  1206. .periods_max = 1024,
  1207. .fifo_size = 0,
  1208. };
  1209. static struct snd_pcm_hardware snd_wss_capture =
  1210. {
  1211. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1212. SNDRV_PCM_INFO_MMAP_VALID |
  1213. SNDRV_PCM_INFO_RESUME |
  1214. SNDRV_PCM_INFO_SYNC_START),
  1215. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1216. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1217. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1218. .rate_min = 5510,
  1219. .rate_max = 48000,
  1220. .channels_min = 1,
  1221. .channels_max = 2,
  1222. .buffer_bytes_max = (128*1024),
  1223. .period_bytes_min = 64,
  1224. .period_bytes_max = (128*1024),
  1225. .periods_min = 1,
  1226. .periods_max = 1024,
  1227. .fifo_size = 0,
  1228. };
  1229. /*
  1230. */
  1231. static int snd_wss_playback_open(struct snd_pcm_substream *substream)
  1232. {
  1233. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1234. struct snd_pcm_runtime *runtime = substream->runtime;
  1235. int err;
  1236. runtime->hw = snd_wss_playback;
  1237. /* hardware limitation of older chipsets */
  1238. if (chip->hardware & WSS_HW_AD1848_MASK)
  1239. runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1240. SNDRV_PCM_FMTBIT_S16_BE);
  1241. /* hardware bug in InterWave chipset */
  1242. if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
  1243. runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
  1244. /* hardware limitation of cheap chips */
  1245. if (chip->hardware == WSS_HW_CS4235 ||
  1246. chip->hardware == WSS_HW_CS4239)
  1247. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1248. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  1249. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  1250. if (chip->claim_dma) {
  1251. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
  1252. return err;
  1253. }
  1254. err = snd_wss_open(chip, WSS_MODE_PLAY);
  1255. if (err < 0) {
  1256. if (chip->release_dma)
  1257. chip->release_dma(chip, chip->dma_private_data, chip->dma1);
  1258. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1259. return err;
  1260. }
  1261. chip->playback_substream = substream;
  1262. snd_pcm_set_sync(substream);
  1263. chip->rate_constraint(runtime);
  1264. return 0;
  1265. }
  1266. static int snd_wss_capture_open(struct snd_pcm_substream *substream)
  1267. {
  1268. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1269. struct snd_pcm_runtime *runtime = substream->runtime;
  1270. int err;
  1271. runtime->hw = snd_wss_capture;
  1272. /* hardware limitation of older chipsets */
  1273. if (chip->hardware & WSS_HW_AD1848_MASK)
  1274. runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1275. SNDRV_PCM_FMTBIT_S16_BE);
  1276. /* hardware limitation of cheap chips */
  1277. if (chip->hardware == WSS_HW_CS4235 ||
  1278. chip->hardware == WSS_HW_CS4239)
  1279. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1280. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  1281. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  1282. if (chip->claim_dma) {
  1283. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
  1284. return err;
  1285. }
  1286. err = snd_wss_open(chip, WSS_MODE_RECORD);
  1287. if (err < 0) {
  1288. if (chip->release_dma)
  1289. chip->release_dma(chip, chip->dma_private_data, chip->dma2);
  1290. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1291. return err;
  1292. }
  1293. chip->capture_substream = substream;
  1294. snd_pcm_set_sync(substream);
  1295. chip->rate_constraint(runtime);
  1296. return 0;
  1297. }
  1298. static int snd_wss_playback_close(struct snd_pcm_substream *substream)
  1299. {
  1300. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1301. chip->playback_substream = NULL;
  1302. snd_wss_close(chip, WSS_MODE_PLAY);
  1303. return 0;
  1304. }
  1305. static int snd_wss_capture_close(struct snd_pcm_substream *substream)
  1306. {
  1307. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1308. chip->capture_substream = NULL;
  1309. snd_wss_close(chip, WSS_MODE_RECORD);
  1310. return 0;
  1311. }
  1312. static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
  1313. {
  1314. int tmp;
  1315. if (!chip->thinkpad_flag)
  1316. return;
  1317. outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
  1318. tmp = inb(AD1848_THINKPAD_CTL_PORT2);
  1319. if (on)
  1320. /* turn it on */
  1321. tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
  1322. else
  1323. /* turn it off */
  1324. tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
  1325. outb(tmp, AD1848_THINKPAD_CTL_PORT2);
  1326. }
  1327. #ifdef CONFIG_PM
  1328. /* lowlevel suspend callback for CS4231 */
  1329. static void snd_wss_suspend(struct snd_wss *chip)
  1330. {
  1331. int reg;
  1332. unsigned long flags;
  1333. snd_pcm_suspend_all(chip->pcm);
  1334. spin_lock_irqsave(&chip->reg_lock, flags);
  1335. for (reg = 0; reg < 32; reg++)
  1336. chip->image[reg] = snd_wss_in(chip, reg);
  1337. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1338. if (chip->thinkpad_flag)
  1339. snd_wss_thinkpad_twiddle(chip, 0);
  1340. }
  1341. /* lowlevel resume callback for CS4231 */
  1342. static void snd_wss_resume(struct snd_wss *chip)
  1343. {
  1344. int reg;
  1345. unsigned long flags;
  1346. /* int timeout; */
  1347. if (chip->thinkpad_flag)
  1348. snd_wss_thinkpad_twiddle(chip, 1);
  1349. snd_wss_mce_up(chip);
  1350. spin_lock_irqsave(&chip->reg_lock, flags);
  1351. for (reg = 0; reg < 32; reg++) {
  1352. switch (reg) {
  1353. case CS4231_VERSION:
  1354. break;
  1355. default:
  1356. snd_wss_out(chip, reg, chip->image[reg]);
  1357. break;
  1358. }
  1359. }
  1360. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1361. #if 1
  1362. snd_wss_mce_down(chip);
  1363. #else
  1364. /* The following is a workaround to avoid freeze after resume on TP600E.
  1365. This is the first half of copy of snd_wss_mce_down(), but doesn't
  1366. include rescheduling. -- iwai
  1367. */
  1368. snd_wss_busy_wait(chip);
  1369. spin_lock_irqsave(&chip->reg_lock, flags);
  1370. chip->mce_bit &= ~CS4231_MCE;
  1371. timeout = wss_inb(chip, CS4231P(REGSEL));
  1372. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  1373. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1374. if (timeout == 0x80)
  1375. snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  1376. if ((timeout & CS4231_MCE) == 0 ||
  1377. !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
  1378. return;
  1379. }
  1380. snd_wss_busy_wait(chip);
  1381. #endif
  1382. }
  1383. #endif /* CONFIG_PM */
  1384. static int snd_wss_free(struct snd_wss *chip)
  1385. {
  1386. release_and_free_resource(chip->res_port);
  1387. release_and_free_resource(chip->res_cport);
  1388. if (chip->irq >= 0) {
  1389. disable_irq(chip->irq);
  1390. if (!(chip->hwshare & WSS_HWSHARE_IRQ))
  1391. free_irq(chip->irq, (void *) chip);
  1392. }
  1393. if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
  1394. snd_dma_disable(chip->dma1);
  1395. free_dma(chip->dma1);
  1396. }
  1397. if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
  1398. chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
  1399. snd_dma_disable(chip->dma2);
  1400. free_dma(chip->dma2);
  1401. }
  1402. if (chip->timer)
  1403. snd_device_free(chip->card, chip->timer);
  1404. kfree(chip);
  1405. return 0;
  1406. }
  1407. static int snd_wss_dev_free(struct snd_device *device)
  1408. {
  1409. struct snd_wss *chip = device->device_data;
  1410. return snd_wss_free(chip);
  1411. }
  1412. const char *snd_wss_chip_id(struct snd_wss *chip)
  1413. {
  1414. switch (chip->hardware) {
  1415. case WSS_HW_CS4231:
  1416. return "CS4231";
  1417. case WSS_HW_CS4231A:
  1418. return "CS4231A";
  1419. case WSS_HW_CS4232:
  1420. return "CS4232";
  1421. case WSS_HW_CS4232A:
  1422. return "CS4232A";
  1423. case WSS_HW_CS4235:
  1424. return "CS4235";
  1425. case WSS_HW_CS4236:
  1426. return "CS4236";
  1427. case WSS_HW_CS4236B:
  1428. return "CS4236B";
  1429. case WSS_HW_CS4237B:
  1430. return "CS4237B";
  1431. case WSS_HW_CS4238B:
  1432. return "CS4238B";
  1433. case WSS_HW_CS4239:
  1434. return "CS4239";
  1435. case WSS_HW_INTERWAVE:
  1436. return "AMD InterWave";
  1437. case WSS_HW_OPL3SA2:
  1438. return chip->card->shortname;
  1439. case WSS_HW_AD1845:
  1440. return "AD1845";
  1441. case WSS_HW_OPTI93X:
  1442. return "OPTi 93x";
  1443. case WSS_HW_AD1847:
  1444. return "AD1847";
  1445. case WSS_HW_AD1848:
  1446. return "AD1848";
  1447. case WSS_HW_CS4248:
  1448. return "CS4248";
  1449. case WSS_HW_CMI8330:
  1450. return "CMI8330/C3D";
  1451. default:
  1452. return "???";
  1453. }
  1454. }
  1455. EXPORT_SYMBOL(snd_wss_chip_id);
  1456. static int snd_wss_new(struct snd_card *card,
  1457. unsigned short hardware,
  1458. unsigned short hwshare,
  1459. struct snd_wss **rchip)
  1460. {
  1461. struct snd_wss *chip;
  1462. *rchip = NULL;
  1463. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1464. if (chip == NULL)
  1465. return -ENOMEM;
  1466. chip->hardware = hardware;
  1467. chip->hwshare = hwshare;
  1468. spin_lock_init(&chip->reg_lock);
  1469. mutex_init(&chip->mce_mutex);
  1470. mutex_init(&chip->open_mutex);
  1471. chip->card = card;
  1472. chip->rate_constraint = snd_wss_xrate;
  1473. chip->set_playback_format = snd_wss_playback_format;
  1474. chip->set_capture_format = snd_wss_capture_format;
  1475. if (chip->hardware == WSS_HW_OPTI93X)
  1476. memcpy(&chip->image, &snd_opti93x_original_image,
  1477. sizeof(snd_opti93x_original_image));
  1478. else
  1479. memcpy(&chip->image, &snd_wss_original_image,
  1480. sizeof(snd_wss_original_image));
  1481. *rchip = chip;
  1482. return 0;
  1483. }
  1484. int snd_wss_create(struct snd_card *card,
  1485. unsigned long port,
  1486. unsigned long cport,
  1487. int irq, int dma1, int dma2,
  1488. unsigned short hardware,
  1489. unsigned short hwshare,
  1490. struct snd_wss **rchip)
  1491. {
  1492. static struct snd_device_ops ops = {
  1493. .dev_free = snd_wss_dev_free,
  1494. };
  1495. struct snd_wss *chip;
  1496. int err;
  1497. err = snd_wss_new(card, hardware, hwshare, &chip);
  1498. if (err < 0)
  1499. return err;
  1500. chip->irq = -1;
  1501. chip->dma1 = -1;
  1502. chip->dma2 = -1;
  1503. chip->res_port = request_region(port, 4, "CS4231");
  1504. if (!chip->res_port) {
  1505. snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
  1506. snd_wss_free(chip);
  1507. return -EBUSY;
  1508. }
  1509. chip->port = port;
  1510. if ((long)cport >= 0) {
  1511. chip->res_cport = request_region(cport, 8, "CS4232 Control");
  1512. if (!chip->res_cport) {
  1513. snd_printk(KERN_ERR
  1514. "wss: can't grab control port 0x%lx\n", cport);
  1515. snd_wss_free(chip);
  1516. return -ENODEV;
  1517. }
  1518. }
  1519. chip->cport = cport;
  1520. if (!(hwshare & WSS_HWSHARE_IRQ))
  1521. if (request_irq(irq, snd_wss_interrupt, IRQF_DISABLED,
  1522. "CS4231", (void *) chip)) {
  1523. snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
  1524. snd_wss_free(chip);
  1525. return -EBUSY;
  1526. }
  1527. chip->irq = irq;
  1528. if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
  1529. snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
  1530. snd_wss_free(chip);
  1531. return -EBUSY;
  1532. }
  1533. chip->dma1 = dma1;
  1534. if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
  1535. dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
  1536. snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
  1537. snd_wss_free(chip);
  1538. return -EBUSY;
  1539. }
  1540. if (dma1 == dma2 || dma2 < 0) {
  1541. chip->single_dma = 1;
  1542. chip->dma2 = chip->dma1;
  1543. } else
  1544. chip->dma2 = dma2;
  1545. /* global setup */
  1546. if (snd_wss_probe(chip) < 0) {
  1547. snd_wss_free(chip);
  1548. return -ENODEV;
  1549. }
  1550. snd_wss_init(chip);
  1551. #if 0
  1552. if (chip->hardware & WSS_HW_CS4232_MASK) {
  1553. if (chip->res_cport == NULL)
  1554. snd_printk("CS4232 control port features are not accessible\n");
  1555. }
  1556. #endif
  1557. /* Register device */
  1558. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1559. if (err < 0) {
  1560. snd_wss_free(chip);
  1561. return err;
  1562. }
  1563. #ifdef CONFIG_PM
  1564. /* Power Management */
  1565. chip->suspend = snd_wss_suspend;
  1566. chip->resume = snd_wss_resume;
  1567. #endif
  1568. *rchip = chip;
  1569. return 0;
  1570. }
  1571. EXPORT_SYMBOL(snd_wss_create);
  1572. static struct snd_pcm_ops snd_wss_playback_ops = {
  1573. .open = snd_wss_playback_open,
  1574. .close = snd_wss_playback_close,
  1575. .ioctl = snd_pcm_lib_ioctl,
  1576. .hw_params = snd_wss_playback_hw_params,
  1577. .hw_free = snd_wss_playback_hw_free,
  1578. .prepare = snd_wss_playback_prepare,
  1579. .trigger = snd_wss_trigger,
  1580. .pointer = snd_wss_playback_pointer,
  1581. };
  1582. static struct snd_pcm_ops snd_wss_capture_ops = {
  1583. .open = snd_wss_capture_open,
  1584. .close = snd_wss_capture_close,
  1585. .ioctl = snd_pcm_lib_ioctl,
  1586. .hw_params = snd_wss_capture_hw_params,
  1587. .hw_free = snd_wss_capture_hw_free,
  1588. .prepare = snd_wss_capture_prepare,
  1589. .trigger = snd_wss_trigger,
  1590. .pointer = snd_wss_capture_pointer,
  1591. };
  1592. int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
  1593. {
  1594. struct snd_pcm *pcm;
  1595. int err;
  1596. err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
  1597. if (err < 0)
  1598. return err;
  1599. spin_lock_init(&chip->reg_lock);
  1600. mutex_init(&chip->mce_mutex);
  1601. mutex_init(&chip->open_mutex);
  1602. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
  1603. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
  1604. /* temporary */
  1605. if (chip->hardware & WSS_HW_AD1848_MASK) {
  1606. chip->rate_constraint = snd_wss_xrate;
  1607. chip->set_playback_format = snd_wss_playback_format;
  1608. chip->set_capture_format = snd_wss_capture_format;
  1609. }
  1610. /* global setup */
  1611. pcm->private_data = chip;
  1612. pcm->info_flags = 0;
  1613. if (chip->single_dma)
  1614. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1615. if (chip->hardware != WSS_HW_INTERWAVE)
  1616. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1617. strcpy(pcm->name, snd_wss_chip_id(chip));
  1618. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1619. snd_dma_isa_data(),
  1620. 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1621. chip->pcm = pcm;
  1622. if (rpcm)
  1623. *rpcm = pcm;
  1624. return 0;
  1625. }
  1626. EXPORT_SYMBOL(snd_wss_pcm);
  1627. static void snd_wss_timer_free(struct snd_timer *timer)
  1628. {
  1629. struct snd_wss *chip = timer->private_data;
  1630. chip->timer = NULL;
  1631. }
  1632. int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
  1633. {
  1634. struct snd_timer *timer;
  1635. struct snd_timer_id tid;
  1636. int err;
  1637. /* Timer initialization */
  1638. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1639. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1640. tid.card = chip->card->number;
  1641. tid.device = device;
  1642. tid.subdevice = 0;
  1643. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1644. return err;
  1645. strcpy(timer->name, snd_wss_chip_id(chip));
  1646. timer->private_data = chip;
  1647. timer->private_free = snd_wss_timer_free;
  1648. timer->hw = snd_wss_timer_table;
  1649. chip->timer = timer;
  1650. if (rtimer)
  1651. *rtimer = timer;
  1652. return 0;
  1653. }
  1654. EXPORT_SYMBOL(snd_wss_timer);
  1655. /*
  1656. * MIXER part
  1657. */
  1658. static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
  1659. struct snd_ctl_elem_info *uinfo)
  1660. {
  1661. static char *texts[4] = {
  1662. "Line", "Aux", "Mic", "Mix"
  1663. };
  1664. static char *opl3sa_texts[4] = {
  1665. "Line", "CD", "Mic", "Mix"
  1666. };
  1667. static char *gusmax_texts[4] = {
  1668. "Line", "Synth", "Mic", "Mix"
  1669. };
  1670. char **ptexts = texts;
  1671. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1672. snd_assert(chip->card != NULL, return -EINVAL);
  1673. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1674. uinfo->count = 2;
  1675. uinfo->value.enumerated.items = 4;
  1676. if (uinfo->value.enumerated.item > 3)
  1677. uinfo->value.enumerated.item = 3;
  1678. if (!strcmp(chip->card->driver, "GUS MAX"))
  1679. ptexts = gusmax_texts;
  1680. switch (chip->hardware) {
  1681. case WSS_HW_INTERWAVE:
  1682. ptexts = gusmax_texts;
  1683. break;
  1684. case WSS_HW_OPL3SA2:
  1685. ptexts = opl3sa_texts;
  1686. break;
  1687. }
  1688. strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
  1689. return 0;
  1690. }
  1691. static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
  1692. struct snd_ctl_elem_value *ucontrol)
  1693. {
  1694. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1695. unsigned long flags;
  1696. spin_lock_irqsave(&chip->reg_lock, flags);
  1697. ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1698. ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1699. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1700. return 0;
  1701. }
  1702. static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
  1703. struct snd_ctl_elem_value *ucontrol)
  1704. {
  1705. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1706. unsigned long flags;
  1707. unsigned short left, right;
  1708. int change;
  1709. if (ucontrol->value.enumerated.item[0] > 3 ||
  1710. ucontrol->value.enumerated.item[1] > 3)
  1711. return -EINVAL;
  1712. left = ucontrol->value.enumerated.item[0] << 6;
  1713. right = ucontrol->value.enumerated.item[1] << 6;
  1714. spin_lock_irqsave(&chip->reg_lock, flags);
  1715. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1716. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1717. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1718. right != chip->image[CS4231_RIGHT_INPUT];
  1719. snd_wss_out(chip, CS4231_LEFT_INPUT, left);
  1720. snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
  1721. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1722. return change;
  1723. }
  1724. int snd_wss_info_single(struct snd_kcontrol *kcontrol,
  1725. struct snd_ctl_elem_info *uinfo)
  1726. {
  1727. int mask = (kcontrol->private_value >> 16) & 0xff;
  1728. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1729. uinfo->count = 1;
  1730. uinfo->value.integer.min = 0;
  1731. uinfo->value.integer.max = mask;
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL(snd_wss_info_single);
  1735. int snd_wss_get_single(struct snd_kcontrol *kcontrol,
  1736. struct snd_ctl_elem_value *ucontrol)
  1737. {
  1738. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1739. unsigned long flags;
  1740. int reg = kcontrol->private_value & 0xff;
  1741. int shift = (kcontrol->private_value >> 8) & 0xff;
  1742. int mask = (kcontrol->private_value >> 16) & 0xff;
  1743. int invert = (kcontrol->private_value >> 24) & 0xff;
  1744. spin_lock_irqsave(&chip->reg_lock, flags);
  1745. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1746. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1747. if (invert)
  1748. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1749. return 0;
  1750. }
  1751. EXPORT_SYMBOL(snd_wss_get_single);
  1752. int snd_wss_put_single(struct snd_kcontrol *kcontrol,
  1753. struct snd_ctl_elem_value *ucontrol)
  1754. {
  1755. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1756. unsigned long flags;
  1757. int reg = kcontrol->private_value & 0xff;
  1758. int shift = (kcontrol->private_value >> 8) & 0xff;
  1759. int mask = (kcontrol->private_value >> 16) & 0xff;
  1760. int invert = (kcontrol->private_value >> 24) & 0xff;
  1761. int change;
  1762. unsigned short val;
  1763. val = (ucontrol->value.integer.value[0] & mask);
  1764. if (invert)
  1765. val = mask - val;
  1766. val <<= shift;
  1767. spin_lock_irqsave(&chip->reg_lock, flags);
  1768. val = (chip->image[reg] & ~(mask << shift)) | val;
  1769. change = val != chip->image[reg];
  1770. snd_wss_out(chip, reg, val);
  1771. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1772. return change;
  1773. }
  1774. EXPORT_SYMBOL(snd_wss_put_single);
  1775. int snd_wss_info_double(struct snd_kcontrol *kcontrol,
  1776. struct snd_ctl_elem_info *uinfo)
  1777. {
  1778. int mask = (kcontrol->private_value >> 24) & 0xff;
  1779. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1780. uinfo->count = 2;
  1781. uinfo->value.integer.min = 0;
  1782. uinfo->value.integer.max = mask;
  1783. return 0;
  1784. }
  1785. EXPORT_SYMBOL(snd_wss_info_double);
  1786. int snd_wss_get_double(struct snd_kcontrol *kcontrol,
  1787. struct snd_ctl_elem_value *ucontrol)
  1788. {
  1789. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1790. unsigned long flags;
  1791. int left_reg = kcontrol->private_value & 0xff;
  1792. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1793. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1794. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1795. int mask = (kcontrol->private_value >> 24) & 0xff;
  1796. int invert = (kcontrol->private_value >> 22) & 1;
  1797. spin_lock_irqsave(&chip->reg_lock, flags);
  1798. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1799. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1800. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1801. if (invert) {
  1802. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1803. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1804. }
  1805. return 0;
  1806. }
  1807. EXPORT_SYMBOL(snd_wss_get_double);
  1808. int snd_wss_put_double(struct snd_kcontrol *kcontrol,
  1809. struct snd_ctl_elem_value *ucontrol)
  1810. {
  1811. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1812. unsigned long flags;
  1813. int left_reg = kcontrol->private_value & 0xff;
  1814. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1815. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1816. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1817. int mask = (kcontrol->private_value >> 24) & 0xff;
  1818. int invert = (kcontrol->private_value >> 22) & 1;
  1819. int change;
  1820. unsigned short val1, val2;
  1821. val1 = ucontrol->value.integer.value[0] & mask;
  1822. val2 = ucontrol->value.integer.value[1] & mask;
  1823. if (invert) {
  1824. val1 = mask - val1;
  1825. val2 = mask - val2;
  1826. }
  1827. val1 <<= shift_left;
  1828. val2 <<= shift_right;
  1829. spin_lock_irqsave(&chip->reg_lock, flags);
  1830. if (left_reg != right_reg) {
  1831. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1832. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1833. change = val1 != chip->image[left_reg] ||
  1834. val2 != chip->image[right_reg];
  1835. snd_wss_out(chip, left_reg, val1);
  1836. snd_wss_out(chip, right_reg, val2);
  1837. } else {
  1838. mask = (mask << shift_left) | (mask << shift_right);
  1839. val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
  1840. change = val1 != chip->image[left_reg];
  1841. snd_wss_out(chip, left_reg, val1);
  1842. }
  1843. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1844. return change;
  1845. }
  1846. EXPORT_SYMBOL(snd_wss_put_double);
  1847. static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
  1848. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  1849. static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
  1850. static struct snd_kcontrol_new snd_ad1848_controls[] = {
  1851. WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT,
  1852. 7, 7, 1, 1),
  1853. WSS_DOUBLE_TLV("PCM Playback Volume", 0,
  1854. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
  1855. db_scale_6bit),
  1856. WSS_DOUBLE("Aux Playback Switch", 0,
  1857. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1858. WSS_DOUBLE_TLV("Aux Playback Volume", 0,
  1859. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
  1860. db_scale_5bit_12db_max),
  1861. WSS_DOUBLE("Aux Playback Switch", 1,
  1862. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1863. WSS_DOUBLE_TLV("Aux Playback Volume", 1,
  1864. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
  1865. db_scale_5bit_12db_max),
  1866. WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
  1867. 0, 0, 15, 0, db_scale_rec_gain),
  1868. {
  1869. .name = "Capture Source",
  1870. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1871. .info = snd_wss_info_mux,
  1872. .get = snd_wss_get_mux,
  1873. .put = snd_wss_put_mux,
  1874. },
  1875. WSS_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  1876. WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 1, 63, 0,
  1877. db_scale_6bit),
  1878. };
  1879. static struct snd_kcontrol_new snd_wss_controls[] = {
  1880. WSS_DOUBLE("PCM Playback Switch", 0,
  1881. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1882. WSS_DOUBLE("PCM Playback Volume", 0,
  1883. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  1884. WSS_DOUBLE("Line Playback Switch", 0,
  1885. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1886. WSS_DOUBLE("Line Playback Volume", 0,
  1887. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  1888. WSS_DOUBLE("Aux Playback Switch", 0,
  1889. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1890. WSS_DOUBLE("Aux Playback Volume", 0,
  1891. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  1892. WSS_DOUBLE("Aux Playback Switch", 1,
  1893. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1894. WSS_DOUBLE("Aux Playback Volume", 1,
  1895. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  1896. WSS_SINGLE("Mono Playback Switch", 0,
  1897. CS4231_MONO_CTRL, 7, 1, 1),
  1898. WSS_SINGLE("Mono Playback Volume", 0,
  1899. CS4231_MONO_CTRL, 0, 15, 1),
  1900. WSS_SINGLE("Mono Output Playback Switch", 0,
  1901. CS4231_MONO_CTRL, 6, 1, 1),
  1902. WSS_SINGLE("Mono Output Playback Bypass", 0,
  1903. CS4231_MONO_CTRL, 5, 1, 0),
  1904. WSS_DOUBLE("Capture Volume", 0,
  1905. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1906. {
  1907. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1908. .name = "Capture Source",
  1909. .info = snd_wss_info_mux,
  1910. .get = snd_wss_get_mux,
  1911. .put = snd_wss_put_mux,
  1912. },
  1913. WSS_DOUBLE("Mic Boost", 0,
  1914. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1915. WSS_SINGLE("Loopback Capture Switch", 0,
  1916. CS4231_LOOPBACK, 0, 1, 0),
  1917. WSS_SINGLE("Loopback Capture Volume", 0,
  1918. CS4231_LOOPBACK, 2, 63, 1)
  1919. };
  1920. static struct snd_kcontrol_new snd_opti93x_controls[] = {
  1921. WSS_DOUBLE("Master Playback Switch", 0,
  1922. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
  1923. WSS_DOUBLE("Master Playback Volume", 0,
  1924. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
  1925. WSS_DOUBLE("PCM Playback Switch", 0,
  1926. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1927. WSS_DOUBLE("PCM Playback Volume", 0,
  1928. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
  1929. WSS_DOUBLE("FM Playback Switch", 0,
  1930. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1931. WSS_DOUBLE("FM Playback Volume", 0,
  1932. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
  1933. WSS_DOUBLE("Line Playback Switch", 0,
  1934. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1935. WSS_DOUBLE("Line Playback Volume", 0,
  1936. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
  1937. WSS_DOUBLE("Mic Playback Switch", 0,
  1938. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
  1939. WSS_DOUBLE("Mic Playback Volume", 0,
  1940. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
  1941. WSS_DOUBLE("Mic Boost", 0,
  1942. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1943. WSS_DOUBLE("CD Playback Switch", 0,
  1944. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1945. WSS_DOUBLE("CD Playback Volume", 0,
  1946. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
  1947. WSS_DOUBLE("Aux Playback Switch", 0,
  1948. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
  1949. WSS_DOUBLE("Aux Playback Volume", 0,
  1950. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
  1951. WSS_DOUBLE("Capture Volume", 0,
  1952. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1953. {
  1954. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1955. .name = "Capture Source",
  1956. .info = snd_wss_info_mux,
  1957. .get = snd_wss_get_mux,
  1958. .put = snd_wss_put_mux,
  1959. }
  1960. };
  1961. int snd_wss_mixer(struct snd_wss *chip)
  1962. {
  1963. struct snd_card *card;
  1964. unsigned int idx;
  1965. int err;
  1966. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1967. card = chip->card;
  1968. strcpy(card->mixername, chip->pcm->name);
  1969. if (chip->hardware == WSS_HW_OPTI93X)
  1970. for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
  1971. err = snd_ctl_add(card,
  1972. snd_ctl_new1(&snd_opti93x_controls[idx],
  1973. chip));
  1974. if (err < 0)
  1975. return err;
  1976. }
  1977. else if (chip->hardware & WSS_HW_AD1848_MASK)
  1978. for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++) {
  1979. err = snd_ctl_add(card,
  1980. snd_ctl_new1(&snd_ad1848_controls[idx],
  1981. chip));
  1982. if (err < 0)
  1983. return err;
  1984. }
  1985. else
  1986. for (idx = 0; idx < ARRAY_SIZE(snd_wss_controls); idx++) {
  1987. err = snd_ctl_add(card,
  1988. snd_ctl_new1(&snd_wss_controls[idx],
  1989. chip));
  1990. if (err < 0)
  1991. return err;
  1992. }
  1993. return 0;
  1994. }
  1995. EXPORT_SYMBOL(snd_wss_mixer);
  1996. const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
  1997. {
  1998. return direction == SNDRV_PCM_STREAM_PLAYBACK ?
  1999. &snd_wss_playback_ops : &snd_wss_capture_ops;
  2000. }
  2001. EXPORT_SYMBOL(snd_wss_get_pcm_ops);
  2002. /*
  2003. * INIT part
  2004. */
  2005. static int __init alsa_wss_init(void)
  2006. {
  2007. return 0;
  2008. }
  2009. static void __exit alsa_wss_exit(void)
  2010. {
  2011. }
  2012. module_init(alsa_wss_init);
  2013. module_exit(alsa_wss_exit);