wss_lib.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  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 = 250;
  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. snd_wss_busy_wait(chip);
  384. #ifdef CONFIG_SND_DEBUG
  385. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  386. snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
  387. #endif
  388. spin_lock_irqsave(&chip->reg_lock, flags);
  389. chip->mce_bit &= ~CS4231_MCE;
  390. timeout = wss_inb(chip, CS4231P(REGSEL));
  391. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  392. spin_unlock_irqrestore(&chip->reg_lock, flags);
  393. if (timeout == 0x80)
  394. snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  395. if ((timeout & CS4231_MCE) == 0 ||
  396. !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
  397. return;
  398. }
  399. /*
  400. * Wait for (possible -- during init auto-calibration may not be set)
  401. * calibration process to start. Needs upto 5 sample periods on AD1848
  402. * which at the slowest possible rate of 5.5125 kHz means 907 us.
  403. */
  404. msleep(1);
  405. snd_printdd("(1) jiffies = %lu\n", jiffies);
  406. /* check condition up to 250 ms */
  407. end_time = jiffies + msecs_to_jiffies(250);
  408. while (snd_wss_in(chip, CS4231_TEST_INIT) &
  409. CS4231_CALIB_IN_PROGRESS) {
  410. if (time_after(jiffies, end_time)) {
  411. snd_printk(KERN_ERR "mce_down - "
  412. "auto calibration time out (2)\n");
  413. return;
  414. }
  415. msleep(1);
  416. }
  417. snd_printdd("(2) jiffies = %lu\n", jiffies);
  418. /* check condition up to 100 ms */
  419. end_time = jiffies + msecs_to_jiffies(100);
  420. while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
  421. if (time_after(jiffies, end_time)) {
  422. snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
  423. return;
  424. }
  425. msleep(1);
  426. }
  427. snd_printdd("(3) jiffies = %lu\n", jiffies);
  428. snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
  429. }
  430. EXPORT_SYMBOL(snd_wss_mce_down);
  431. static unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
  432. {
  433. switch (format & 0xe0) {
  434. case CS4231_LINEAR_16:
  435. case CS4231_LINEAR_16_BIG:
  436. size >>= 1;
  437. break;
  438. case CS4231_ADPCM_16:
  439. return size >> 2;
  440. }
  441. if (format & CS4231_STEREO)
  442. size >>= 1;
  443. return size;
  444. }
  445. static int snd_wss_trigger(struct snd_pcm_substream *substream,
  446. int cmd)
  447. {
  448. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  449. int result = 0;
  450. unsigned int what;
  451. struct snd_pcm_substream *s;
  452. int do_start;
  453. switch (cmd) {
  454. case SNDRV_PCM_TRIGGER_START:
  455. case SNDRV_PCM_TRIGGER_RESUME:
  456. do_start = 1; break;
  457. case SNDRV_PCM_TRIGGER_STOP:
  458. case SNDRV_PCM_TRIGGER_SUSPEND:
  459. do_start = 0; break;
  460. default:
  461. return -EINVAL;
  462. }
  463. what = 0;
  464. snd_pcm_group_for_each_entry(s, substream) {
  465. if (s == chip->playback_substream) {
  466. what |= CS4231_PLAYBACK_ENABLE;
  467. snd_pcm_trigger_done(s, substream);
  468. } else if (s == chip->capture_substream) {
  469. what |= CS4231_RECORD_ENABLE;
  470. snd_pcm_trigger_done(s, substream);
  471. }
  472. }
  473. spin_lock(&chip->reg_lock);
  474. if (do_start) {
  475. chip->image[CS4231_IFACE_CTRL] |= what;
  476. if (chip->trigger)
  477. chip->trigger(chip, what, 1);
  478. } else {
  479. chip->image[CS4231_IFACE_CTRL] &= ~what;
  480. if (chip->trigger)
  481. chip->trigger(chip, what, 0);
  482. }
  483. snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  484. spin_unlock(&chip->reg_lock);
  485. #if 0
  486. snd_wss_debug(chip);
  487. #endif
  488. return result;
  489. }
  490. /*
  491. * CODEC I/O
  492. */
  493. static unsigned char snd_wss_get_rate(unsigned int rate)
  494. {
  495. int i;
  496. for (i = 0; i < ARRAY_SIZE(rates); i++)
  497. if (rate == rates[i])
  498. return freq_bits[i];
  499. // snd_BUG();
  500. return freq_bits[ARRAY_SIZE(rates) - 1];
  501. }
  502. static unsigned char snd_wss_get_format(struct snd_wss *chip,
  503. int format,
  504. int channels)
  505. {
  506. unsigned char rformat;
  507. rformat = CS4231_LINEAR_8;
  508. switch (format) {
  509. case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
  510. case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
  511. case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
  512. case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
  513. case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
  514. }
  515. if (channels > 1)
  516. rformat |= CS4231_STEREO;
  517. #if 0
  518. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  519. #endif
  520. return rformat;
  521. }
  522. static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
  523. {
  524. unsigned long flags;
  525. mute = mute ? 1 : 0;
  526. spin_lock_irqsave(&chip->reg_lock, flags);
  527. if (chip->calibrate_mute == mute) {
  528. spin_unlock_irqrestore(&chip->reg_lock, flags);
  529. return;
  530. }
  531. if (!mute) {
  532. snd_wss_dout(chip, CS4231_LEFT_INPUT,
  533. chip->image[CS4231_LEFT_INPUT]);
  534. snd_wss_dout(chip, CS4231_RIGHT_INPUT,
  535. chip->image[CS4231_RIGHT_INPUT]);
  536. snd_wss_dout(chip, CS4231_LOOPBACK,
  537. chip->image[CS4231_LOOPBACK]);
  538. }
  539. snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
  540. mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
  541. snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
  542. mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
  543. snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
  544. mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
  545. snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
  546. mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
  547. snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
  548. mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
  549. snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
  550. mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
  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. if (chip->hardware == WSS_HW_INTERWAVE) {
  558. snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
  559. mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
  560. snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
  561. mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
  562. snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
  563. mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
  564. snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
  565. mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
  566. }
  567. chip->calibrate_mute = mute;
  568. spin_unlock_irqrestore(&chip->reg_lock, flags);
  569. }
  570. static void snd_wss_playback_format(struct snd_wss *chip,
  571. struct snd_pcm_hw_params *params,
  572. unsigned char pdfr)
  573. {
  574. unsigned long flags;
  575. int full_calib = 1;
  576. mutex_lock(&chip->mce_mutex);
  577. snd_wss_calibrate_mute(chip, 1);
  578. if (chip->hardware == WSS_HW_CS4231A ||
  579. (chip->hardware & WSS_HW_CS4232_MASK)) {
  580. spin_lock_irqsave(&chip->reg_lock, flags);
  581. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
  582. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  583. chip->image[CS4231_ALT_FEATURE_1] | 0x10);
  584. chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
  585. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  586. chip->image[CS4231_PLAYBK_FORMAT]);
  587. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  588. chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
  589. udelay(100); /* Fixes audible clicks at least on GUS MAX */
  590. full_calib = 0;
  591. }
  592. spin_unlock_irqrestore(&chip->reg_lock, flags);
  593. }
  594. if (full_calib) {
  595. snd_wss_mce_up(chip);
  596. spin_lock_irqsave(&chip->reg_lock, flags);
  597. if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
  598. if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
  599. pdfr = (pdfr & 0xf0) |
  600. (chip->image[CS4231_REC_FORMAT] & 0x0f);
  601. } else {
  602. chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
  603. }
  604. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
  605. spin_unlock_irqrestore(&chip->reg_lock, flags);
  606. if (chip->hardware == WSS_HW_OPL3SA2)
  607. udelay(100); /* this seems to help */
  608. snd_wss_mce_down(chip);
  609. }
  610. snd_wss_calibrate_mute(chip, 0);
  611. mutex_unlock(&chip->mce_mutex);
  612. }
  613. static void snd_wss_capture_format(struct snd_wss *chip,
  614. struct snd_pcm_hw_params *params,
  615. unsigned char cdfr)
  616. {
  617. unsigned long flags;
  618. int full_calib = 1;
  619. mutex_lock(&chip->mce_mutex);
  620. snd_wss_calibrate_mute(chip, 1);
  621. if (chip->hardware == WSS_HW_CS4231A ||
  622. (chip->hardware & WSS_HW_CS4232_MASK)) {
  623. spin_lock_irqsave(&chip->reg_lock, flags);
  624. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
  625. (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  626. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  627. chip->image[CS4231_ALT_FEATURE_1] | 0x20);
  628. snd_wss_out(chip, CS4231_REC_FORMAT,
  629. chip->image[CS4231_REC_FORMAT] = cdfr);
  630. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  631. chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
  632. full_calib = 0;
  633. }
  634. spin_unlock_irqrestore(&chip->reg_lock, flags);
  635. }
  636. if (full_calib) {
  637. snd_wss_mce_up(chip);
  638. spin_lock_irqsave(&chip->reg_lock, flags);
  639. if (chip->hardware != WSS_HW_INTERWAVE &&
  640. !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  641. if (chip->single_dma)
  642. snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
  643. else
  644. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  645. (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
  646. (cdfr & 0x0f));
  647. spin_unlock_irqrestore(&chip->reg_lock, flags);
  648. snd_wss_mce_down(chip);
  649. snd_wss_mce_up(chip);
  650. spin_lock_irqsave(&chip->reg_lock, flags);
  651. }
  652. snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
  653. spin_unlock_irqrestore(&chip->reg_lock, flags);
  654. snd_wss_mce_down(chip);
  655. }
  656. snd_wss_calibrate_mute(chip, 0);
  657. mutex_unlock(&chip->mce_mutex);
  658. }
  659. /*
  660. * Timer interface
  661. */
  662. static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
  663. {
  664. struct snd_wss *chip = snd_timer_chip(timer);
  665. if (chip->hardware & WSS_HW_CS4236B_MASK)
  666. return 14467;
  667. else
  668. return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
  669. }
  670. static int snd_wss_timer_start(struct snd_timer *timer)
  671. {
  672. unsigned long flags;
  673. unsigned int ticks;
  674. struct snd_wss *chip = snd_timer_chip(timer);
  675. spin_lock_irqsave(&chip->reg_lock, flags);
  676. ticks = timer->sticks;
  677. if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
  678. (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
  679. (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
  680. chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
  681. snd_wss_out(chip, CS4231_TIMER_HIGH,
  682. chip->image[CS4231_TIMER_HIGH]);
  683. chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
  684. snd_wss_out(chip, CS4231_TIMER_LOW,
  685. chip->image[CS4231_TIMER_LOW]);
  686. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  687. chip->image[CS4231_ALT_FEATURE_1] |
  688. CS4231_TIMER_ENABLE);
  689. }
  690. spin_unlock_irqrestore(&chip->reg_lock, flags);
  691. return 0;
  692. }
  693. static int snd_wss_timer_stop(struct snd_timer *timer)
  694. {
  695. unsigned long flags;
  696. struct snd_wss *chip = snd_timer_chip(timer);
  697. spin_lock_irqsave(&chip->reg_lock, flags);
  698. chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
  699. snd_wss_out(chip, CS4231_ALT_FEATURE_1,
  700. chip->image[CS4231_ALT_FEATURE_1]);
  701. spin_unlock_irqrestore(&chip->reg_lock, flags);
  702. return 0;
  703. }
  704. static void snd_wss_init(struct snd_wss *chip)
  705. {
  706. unsigned long flags;
  707. snd_wss_mce_down(chip);
  708. #ifdef SNDRV_DEBUG_MCE
  709. snd_printk("init: (1)\n");
  710. #endif
  711. snd_wss_mce_up(chip);
  712. spin_lock_irqsave(&chip->reg_lock, flags);
  713. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
  714. CS4231_PLAYBACK_PIO |
  715. CS4231_RECORD_ENABLE |
  716. CS4231_RECORD_PIO |
  717. CS4231_CALIB_MODE);
  718. chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
  719. snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  720. spin_unlock_irqrestore(&chip->reg_lock, flags);
  721. snd_wss_mce_down(chip);
  722. #ifdef SNDRV_DEBUG_MCE
  723. snd_printk("init: (2)\n");
  724. #endif
  725. snd_wss_mce_up(chip);
  726. spin_lock_irqsave(&chip->reg_lock, flags);
  727. snd_wss_out(chip,
  728. CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
  729. spin_unlock_irqrestore(&chip->reg_lock, flags);
  730. snd_wss_mce_down(chip);
  731. #ifdef SNDRV_DEBUG_MCE
  732. snd_printk("init: (3) - afei = 0x%x\n",
  733. chip->image[CS4231_ALT_FEATURE_1]);
  734. #endif
  735. spin_lock_irqsave(&chip->reg_lock, flags);
  736. snd_wss_out(chip, CS4231_ALT_FEATURE_2,
  737. chip->image[CS4231_ALT_FEATURE_2]);
  738. spin_unlock_irqrestore(&chip->reg_lock, flags);
  739. snd_wss_mce_up(chip);
  740. spin_lock_irqsave(&chip->reg_lock, flags);
  741. snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
  742. chip->image[CS4231_PLAYBK_FORMAT]);
  743. spin_unlock_irqrestore(&chip->reg_lock, flags);
  744. snd_wss_mce_down(chip);
  745. #ifdef SNDRV_DEBUG_MCE
  746. snd_printk("init: (4)\n");
  747. #endif
  748. snd_wss_mce_up(chip);
  749. spin_lock_irqsave(&chip->reg_lock, flags);
  750. snd_wss_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
  751. spin_unlock_irqrestore(&chip->reg_lock, flags);
  752. snd_wss_mce_down(chip);
  753. #ifdef SNDRV_DEBUG_MCE
  754. snd_printk("init: (5)\n");
  755. #endif
  756. }
  757. static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
  758. {
  759. unsigned long flags;
  760. mutex_lock(&chip->open_mutex);
  761. if ((chip->mode & mode) ||
  762. ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
  763. mutex_unlock(&chip->open_mutex);
  764. return -EAGAIN;
  765. }
  766. if (chip->mode & WSS_MODE_OPEN) {
  767. chip->mode |= mode;
  768. mutex_unlock(&chip->open_mutex);
  769. return 0;
  770. }
  771. /* ok. now enable and ack CODEC IRQ */
  772. spin_lock_irqsave(&chip->reg_lock, flags);
  773. snd_wss_out(chip, CS4231_IRQ_STATUS,
  774. CS4231_PLAYBACK_IRQ |
  775. CS4231_RECORD_IRQ |
  776. CS4231_TIMER_IRQ);
  777. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  778. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  779. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  780. chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
  781. snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  782. snd_wss_out(chip, CS4231_IRQ_STATUS,
  783. CS4231_PLAYBACK_IRQ |
  784. CS4231_RECORD_IRQ |
  785. CS4231_TIMER_IRQ);
  786. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  787. spin_unlock_irqrestore(&chip->reg_lock, flags);
  788. chip->mode = mode;
  789. mutex_unlock(&chip->open_mutex);
  790. return 0;
  791. }
  792. static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
  793. {
  794. unsigned long flags;
  795. mutex_lock(&chip->open_mutex);
  796. chip->mode &= ~mode;
  797. if (chip->mode & WSS_MODE_OPEN) {
  798. mutex_unlock(&chip->open_mutex);
  799. return;
  800. }
  801. snd_wss_calibrate_mute(chip, 1);
  802. /* disable IRQ */
  803. spin_lock_irqsave(&chip->reg_lock, flags);
  804. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  805. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  806. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  807. chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
  808. snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  809. /* now disable record & playback */
  810. if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  811. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
  812. spin_unlock_irqrestore(&chip->reg_lock, flags);
  813. snd_wss_mce_up(chip);
  814. spin_lock_irqsave(&chip->reg_lock, flags);
  815. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  816. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  817. snd_wss_out(chip, CS4231_IFACE_CTRL,
  818. chip->image[CS4231_IFACE_CTRL]);
  819. spin_unlock_irqrestore(&chip->reg_lock, flags);
  820. snd_wss_mce_down(chip);
  821. spin_lock_irqsave(&chip->reg_lock, flags);
  822. }
  823. /* clear IRQ again */
  824. snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
  825. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  826. wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  827. spin_unlock_irqrestore(&chip->reg_lock, flags);
  828. snd_wss_calibrate_mute(chip, 0);
  829. chip->mode = 0;
  830. mutex_unlock(&chip->open_mutex);
  831. }
  832. /*
  833. * timer open/close
  834. */
  835. static int snd_wss_timer_open(struct snd_timer *timer)
  836. {
  837. struct snd_wss *chip = snd_timer_chip(timer);
  838. snd_wss_open(chip, WSS_MODE_TIMER);
  839. return 0;
  840. }
  841. static int snd_wss_timer_close(struct snd_timer *timer)
  842. {
  843. struct snd_wss *chip = snd_timer_chip(timer);
  844. snd_wss_close(chip, WSS_MODE_TIMER);
  845. return 0;
  846. }
  847. static struct snd_timer_hardware snd_wss_timer_table =
  848. {
  849. .flags = SNDRV_TIMER_HW_AUTO,
  850. .resolution = 9945,
  851. .ticks = 65535,
  852. .open = snd_wss_timer_open,
  853. .close = snd_wss_timer_close,
  854. .c_resolution = snd_wss_timer_resolution,
  855. .start = snd_wss_timer_start,
  856. .stop = snd_wss_timer_stop,
  857. };
  858. /*
  859. * ok.. exported functions..
  860. */
  861. static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
  862. struct snd_pcm_hw_params *hw_params)
  863. {
  864. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  865. unsigned char new_pdfr;
  866. int err;
  867. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  868. return err;
  869. new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
  870. params_channels(hw_params)) |
  871. snd_wss_get_rate(params_rate(hw_params));
  872. chip->set_playback_format(chip, hw_params, new_pdfr);
  873. return 0;
  874. }
  875. static int snd_wss_playback_hw_free(struct snd_pcm_substream *substream)
  876. {
  877. return snd_pcm_lib_free_pages(substream);
  878. }
  879. static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
  880. {
  881. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  882. struct snd_pcm_runtime *runtime = substream->runtime;
  883. unsigned long flags;
  884. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  885. unsigned int count = snd_pcm_lib_period_bytes(substream);
  886. spin_lock_irqsave(&chip->reg_lock, flags);
  887. chip->p_dma_size = size;
  888. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
  889. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  890. count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
  891. snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  892. snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  893. spin_unlock_irqrestore(&chip->reg_lock, flags);
  894. #if 0
  895. snd_wss_debug(chip);
  896. #endif
  897. return 0;
  898. }
  899. static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
  900. struct snd_pcm_hw_params *hw_params)
  901. {
  902. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  903. unsigned char new_cdfr;
  904. int err;
  905. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  906. return err;
  907. new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
  908. params_channels(hw_params)) |
  909. snd_wss_get_rate(params_rate(hw_params));
  910. chip->set_capture_format(chip, hw_params, new_cdfr);
  911. return 0;
  912. }
  913. static int snd_wss_capture_hw_free(struct snd_pcm_substream *substream)
  914. {
  915. return snd_pcm_lib_free_pages(substream);
  916. }
  917. static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
  918. {
  919. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  920. struct snd_pcm_runtime *runtime = substream->runtime;
  921. unsigned long flags;
  922. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  923. unsigned int count = snd_pcm_lib_period_bytes(substream);
  924. spin_lock_irqsave(&chip->reg_lock, flags);
  925. chip->c_dma_size = size;
  926. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  927. snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  928. count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
  929. if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
  930. snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  931. snd_wss_out(chip, CS4231_PLY_UPR_CNT,
  932. (unsigned char) (count >> 8));
  933. } else {
  934. snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
  935. snd_wss_out(chip, CS4231_REC_UPR_CNT,
  936. (unsigned char) (count >> 8));
  937. }
  938. spin_unlock_irqrestore(&chip->reg_lock, flags);
  939. return 0;
  940. }
  941. void snd_wss_overrange(struct snd_wss *chip)
  942. {
  943. unsigned long flags;
  944. unsigned char res;
  945. spin_lock_irqsave(&chip->reg_lock, flags);
  946. res = snd_wss_in(chip, CS4231_TEST_INIT);
  947. spin_unlock_irqrestore(&chip->reg_lock, flags);
  948. if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
  949. chip->capture_substream->runtime->overrange++;
  950. }
  951. EXPORT_SYMBOL(snd_wss_overrange);
  952. irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
  953. {
  954. struct snd_wss *chip = dev_id;
  955. unsigned char status;
  956. status = snd_wss_in(chip, CS4231_IRQ_STATUS);
  957. if (status & CS4231_TIMER_IRQ) {
  958. if (chip->timer)
  959. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  960. }
  961. if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
  962. if (status & CS4231_PLAYBACK_IRQ) {
  963. if (chip->mode & WSS_MODE_PLAY) {
  964. if (chip->playback_substream)
  965. snd_pcm_period_elapsed(chip->playback_substream);
  966. }
  967. if (chip->mode & WSS_MODE_RECORD) {
  968. if (chip->capture_substream) {
  969. snd_wss_overrange(chip);
  970. snd_pcm_period_elapsed(chip->capture_substream);
  971. }
  972. }
  973. }
  974. } else {
  975. if (status & CS4231_PLAYBACK_IRQ) {
  976. if (chip->playback_substream)
  977. snd_pcm_period_elapsed(chip->playback_substream);
  978. }
  979. if (status & CS4231_RECORD_IRQ) {
  980. if (chip->capture_substream) {
  981. snd_wss_overrange(chip);
  982. snd_pcm_period_elapsed(chip->capture_substream);
  983. }
  984. }
  985. }
  986. spin_lock(&chip->reg_lock);
  987. snd_wss_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
  988. spin_unlock(&chip->reg_lock);
  989. return IRQ_HANDLED;
  990. }
  991. EXPORT_SYMBOL(snd_wss_interrupt);
  992. static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
  993. {
  994. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  995. size_t ptr;
  996. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
  997. return 0;
  998. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  999. return bytes_to_frames(substream->runtime, ptr);
  1000. }
  1001. static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
  1002. {
  1003. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1004. size_t ptr;
  1005. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
  1006. return 0;
  1007. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  1008. return bytes_to_frames(substream->runtime, ptr);
  1009. }
  1010. /*
  1011. */
  1012. static int snd_wss_probe(struct snd_wss *chip)
  1013. {
  1014. unsigned long flags;
  1015. int i, id, rev;
  1016. unsigned char *ptr;
  1017. unsigned int hw;
  1018. #if 0
  1019. snd_wss_debug(chip);
  1020. #endif
  1021. id = 0;
  1022. for (i = 0; i < 50; i++) {
  1023. mb();
  1024. if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  1025. udelay(2000);
  1026. else {
  1027. spin_lock_irqsave(&chip->reg_lock, flags);
  1028. snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
  1029. id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
  1030. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1031. if (id == 0x0a)
  1032. break; /* this is valid value */
  1033. }
  1034. }
  1035. snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
  1036. if (id != 0x0a)
  1037. return -ENODEV; /* no valid device found */
  1038. hw = chip->hardware;
  1039. if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
  1040. rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
  1041. snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
  1042. if (rev == 0x80) {
  1043. unsigned char tmp = snd_wss_in(chip, 23);
  1044. snd_wss_out(chip, 23, ~tmp);
  1045. if (snd_wss_in(chip, 23) != tmp)
  1046. chip->hardware = WSS_HW_AD1845;
  1047. else
  1048. chip->hardware = WSS_HW_CS4231;
  1049. } else if (rev == 0xa0) {
  1050. chip->hardware = WSS_HW_CS4231A;
  1051. } else if (rev == 0xa2) {
  1052. chip->hardware = WSS_HW_CS4232;
  1053. } else if (rev == 0xb2) {
  1054. chip->hardware = WSS_HW_CS4232A;
  1055. } else if (rev == 0x83) {
  1056. chip->hardware = WSS_HW_CS4236;
  1057. } else if (rev == 0x03) {
  1058. chip->hardware = WSS_HW_CS4236B;
  1059. } else {
  1060. snd_printk("unknown CS chip with version 0x%x\n", rev);
  1061. return -ENODEV; /* unknown CS4231 chip? */
  1062. }
  1063. }
  1064. spin_lock_irqsave(&chip->reg_lock, flags);
  1065. wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
  1066. wss_outb(chip, CS4231P(STATUS), 0);
  1067. mb();
  1068. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1069. chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
  1070. switch (chip->hardware) {
  1071. case WSS_HW_INTERWAVE:
  1072. chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
  1073. break;
  1074. case WSS_HW_CS4235:
  1075. case WSS_HW_CS4236B:
  1076. case WSS_HW_CS4237B:
  1077. case WSS_HW_CS4238B:
  1078. case WSS_HW_CS4239:
  1079. if (hw == WSS_HW_DETECT3)
  1080. chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
  1081. else
  1082. chip->hardware = WSS_HW_CS4236;
  1083. break;
  1084. }
  1085. chip->image[CS4231_IFACE_CTRL] =
  1086. (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
  1087. (chip->single_dma ? CS4231_SINGLE_DMA : 0);
  1088. if (chip->hardware != WSS_HW_OPTI93X) {
  1089. chip->image[CS4231_ALT_FEATURE_1] = 0x80;
  1090. chip->image[CS4231_ALT_FEATURE_2] =
  1091. chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
  1092. }
  1093. ptr = (unsigned char *) &chip->image;
  1094. snd_wss_mce_down(chip);
  1095. spin_lock_irqsave(&chip->reg_lock, flags);
  1096. for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
  1097. snd_wss_out(chip, i, *ptr++);
  1098. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1099. snd_wss_mce_up(chip);
  1100. snd_wss_mce_down(chip);
  1101. mdelay(2);
  1102. /* ok.. try check hardware version for CS4236+ chips */
  1103. if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
  1104. if (chip->hardware == WSS_HW_CS4236B) {
  1105. rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
  1106. snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
  1107. id = snd_cs4236_ext_in(chip, CS4236_VERSION);
  1108. snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
  1109. snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
  1110. if ((id & 0x1f) == 0x1d) { /* CS4235 */
  1111. chip->hardware = WSS_HW_CS4235;
  1112. switch (id >> 5) {
  1113. case 4:
  1114. case 5:
  1115. case 6:
  1116. break;
  1117. default:
  1118. snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
  1119. }
  1120. } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
  1121. switch (id >> 5) {
  1122. case 4:
  1123. case 5:
  1124. case 6:
  1125. case 7:
  1126. chip->hardware = WSS_HW_CS4236B;
  1127. break;
  1128. default:
  1129. snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
  1130. }
  1131. } else if ((id & 0x1f) == 0x08) { /* CS4237B */
  1132. chip->hardware = WSS_HW_CS4237B;
  1133. switch (id >> 5) {
  1134. case 4:
  1135. case 5:
  1136. case 6:
  1137. case 7:
  1138. break;
  1139. default:
  1140. snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
  1141. }
  1142. } else if ((id & 0x1f) == 0x09) { /* CS4238B */
  1143. chip->hardware = WSS_HW_CS4238B;
  1144. switch (id >> 5) {
  1145. case 5:
  1146. case 6:
  1147. case 7:
  1148. break;
  1149. default:
  1150. snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
  1151. }
  1152. } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
  1153. chip->hardware = WSS_HW_CS4239;
  1154. switch (id >> 5) {
  1155. case 4:
  1156. case 5:
  1157. case 6:
  1158. break;
  1159. default:
  1160. snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
  1161. }
  1162. } else {
  1163. snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
  1164. }
  1165. }
  1166. }
  1167. return 0; /* all things are ok.. */
  1168. }
  1169. /*
  1170. */
  1171. static struct snd_pcm_hardware snd_wss_playback =
  1172. {
  1173. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1174. SNDRV_PCM_INFO_MMAP_VALID |
  1175. SNDRV_PCM_INFO_RESUME |
  1176. SNDRV_PCM_INFO_SYNC_START),
  1177. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1178. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1179. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1180. .rate_min = 5510,
  1181. .rate_max = 48000,
  1182. .channels_min = 1,
  1183. .channels_max = 2,
  1184. .buffer_bytes_max = (128*1024),
  1185. .period_bytes_min = 64,
  1186. .period_bytes_max = (128*1024),
  1187. .periods_min = 1,
  1188. .periods_max = 1024,
  1189. .fifo_size = 0,
  1190. };
  1191. static struct snd_pcm_hardware snd_wss_capture =
  1192. {
  1193. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1194. SNDRV_PCM_INFO_MMAP_VALID |
  1195. SNDRV_PCM_INFO_RESUME |
  1196. SNDRV_PCM_INFO_SYNC_START),
  1197. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1198. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1199. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1200. .rate_min = 5510,
  1201. .rate_max = 48000,
  1202. .channels_min = 1,
  1203. .channels_max = 2,
  1204. .buffer_bytes_max = (128*1024),
  1205. .period_bytes_min = 64,
  1206. .period_bytes_max = (128*1024),
  1207. .periods_min = 1,
  1208. .periods_max = 1024,
  1209. .fifo_size = 0,
  1210. };
  1211. /*
  1212. */
  1213. static int snd_wss_playback_open(struct snd_pcm_substream *substream)
  1214. {
  1215. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1216. struct snd_pcm_runtime *runtime = substream->runtime;
  1217. int err;
  1218. runtime->hw = snd_wss_playback;
  1219. /* hardware bug in InterWave chipset */
  1220. if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
  1221. runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
  1222. /* hardware limitation of cheap chips */
  1223. if (chip->hardware == WSS_HW_CS4235 ||
  1224. chip->hardware == WSS_HW_CS4239)
  1225. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1226. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  1227. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  1228. if (chip->claim_dma) {
  1229. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
  1230. return err;
  1231. }
  1232. err = snd_wss_open(chip, WSS_MODE_PLAY);
  1233. if (err < 0) {
  1234. if (chip->release_dma)
  1235. chip->release_dma(chip, chip->dma_private_data, chip->dma1);
  1236. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1237. return err;
  1238. }
  1239. chip->playback_substream = substream;
  1240. snd_pcm_set_sync(substream);
  1241. chip->rate_constraint(runtime);
  1242. return 0;
  1243. }
  1244. static int snd_wss_capture_open(struct snd_pcm_substream *substream)
  1245. {
  1246. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1247. struct snd_pcm_runtime *runtime = substream->runtime;
  1248. int err;
  1249. runtime->hw = snd_wss_capture;
  1250. /* hardware limitation of cheap chips */
  1251. if (chip->hardware == WSS_HW_CS4235 ||
  1252. chip->hardware == WSS_HW_CS4239)
  1253. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1254. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  1255. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  1256. if (chip->claim_dma) {
  1257. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
  1258. return err;
  1259. }
  1260. err = snd_wss_open(chip, WSS_MODE_RECORD);
  1261. if (err < 0) {
  1262. if (chip->release_dma)
  1263. chip->release_dma(chip, chip->dma_private_data, chip->dma2);
  1264. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1265. return err;
  1266. }
  1267. chip->capture_substream = substream;
  1268. snd_pcm_set_sync(substream);
  1269. chip->rate_constraint(runtime);
  1270. return 0;
  1271. }
  1272. static int snd_wss_playback_close(struct snd_pcm_substream *substream)
  1273. {
  1274. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1275. chip->playback_substream = NULL;
  1276. snd_wss_close(chip, WSS_MODE_PLAY);
  1277. return 0;
  1278. }
  1279. static int snd_wss_capture_close(struct snd_pcm_substream *substream)
  1280. {
  1281. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1282. chip->capture_substream = NULL;
  1283. snd_wss_close(chip, WSS_MODE_RECORD);
  1284. return 0;
  1285. }
  1286. #ifdef CONFIG_PM
  1287. /* lowlevel suspend callback for CS4231 */
  1288. static void snd_wss_suspend(struct snd_wss *chip)
  1289. {
  1290. int reg;
  1291. unsigned long flags;
  1292. snd_pcm_suspend_all(chip->pcm);
  1293. spin_lock_irqsave(&chip->reg_lock, flags);
  1294. for (reg = 0; reg < 32; reg++)
  1295. chip->image[reg] = snd_wss_in(chip, reg);
  1296. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1297. }
  1298. /* lowlevel resume callback for CS4231 */
  1299. static void snd_wss_resume(struct snd_wss *chip)
  1300. {
  1301. int reg;
  1302. unsigned long flags;
  1303. /* int timeout; */
  1304. snd_wss_mce_up(chip);
  1305. spin_lock_irqsave(&chip->reg_lock, flags);
  1306. for (reg = 0; reg < 32; reg++) {
  1307. switch (reg) {
  1308. case CS4231_VERSION:
  1309. break;
  1310. default:
  1311. snd_wss_out(chip, reg, chip->image[reg]);
  1312. break;
  1313. }
  1314. }
  1315. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1316. #if 1
  1317. snd_wss_mce_down(chip);
  1318. #else
  1319. /* The following is a workaround to avoid freeze after resume on TP600E.
  1320. This is the first half of copy of snd_wss_mce_down(), but doesn't
  1321. include rescheduling. -- iwai
  1322. */
  1323. snd_wss_busy_wait(chip);
  1324. spin_lock_irqsave(&chip->reg_lock, flags);
  1325. chip->mce_bit &= ~CS4231_MCE;
  1326. timeout = wss_inb(chip, CS4231P(REGSEL));
  1327. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  1328. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1329. if (timeout == 0x80)
  1330. snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  1331. if ((timeout & CS4231_MCE) == 0 ||
  1332. !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
  1333. return;
  1334. }
  1335. snd_wss_busy_wait(chip);
  1336. #endif
  1337. }
  1338. #endif /* CONFIG_PM */
  1339. static int snd_wss_free(struct snd_wss *chip)
  1340. {
  1341. release_and_free_resource(chip->res_port);
  1342. release_and_free_resource(chip->res_cport);
  1343. if (chip->irq >= 0) {
  1344. disable_irq(chip->irq);
  1345. if (!(chip->hwshare & WSS_HWSHARE_IRQ))
  1346. free_irq(chip->irq, (void *) chip);
  1347. }
  1348. if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
  1349. snd_dma_disable(chip->dma1);
  1350. free_dma(chip->dma1);
  1351. }
  1352. if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
  1353. chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
  1354. snd_dma_disable(chip->dma2);
  1355. free_dma(chip->dma2);
  1356. }
  1357. if (chip->timer)
  1358. snd_device_free(chip->card, chip->timer);
  1359. kfree(chip);
  1360. return 0;
  1361. }
  1362. static int snd_wss_dev_free(struct snd_device *device)
  1363. {
  1364. struct snd_wss *chip = device->device_data;
  1365. return snd_wss_free(chip);
  1366. }
  1367. const char *snd_wss_chip_id(struct snd_wss *chip)
  1368. {
  1369. switch (chip->hardware) {
  1370. case WSS_HW_CS4231:
  1371. return "CS4231";
  1372. case WSS_HW_CS4231A:
  1373. return "CS4231A";
  1374. case WSS_HW_CS4232:
  1375. return "CS4232";
  1376. case WSS_HW_CS4232A:
  1377. return "CS4232A";
  1378. case WSS_HW_CS4235:
  1379. return "CS4235";
  1380. case WSS_HW_CS4236:
  1381. return "CS4236";
  1382. case WSS_HW_CS4236B:
  1383. return "CS4236B";
  1384. case WSS_HW_CS4237B:
  1385. return "CS4237B";
  1386. case WSS_HW_CS4238B:
  1387. return "CS4238B";
  1388. case WSS_HW_CS4239:
  1389. return "CS4239";
  1390. case WSS_HW_INTERWAVE:
  1391. return "AMD InterWave";
  1392. case WSS_HW_OPL3SA2:
  1393. return chip->card->shortname;
  1394. case WSS_HW_AD1845:
  1395. return "AD1845";
  1396. case WSS_HW_OPTI93X:
  1397. return "OPTi 93x";
  1398. default:
  1399. return "???";
  1400. }
  1401. }
  1402. EXPORT_SYMBOL(snd_wss_chip_id);
  1403. static int snd_wss_new(struct snd_card *card,
  1404. unsigned short hardware,
  1405. unsigned short hwshare,
  1406. struct snd_wss **rchip)
  1407. {
  1408. struct snd_wss *chip;
  1409. *rchip = NULL;
  1410. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1411. if (chip == NULL)
  1412. return -ENOMEM;
  1413. chip->hardware = hardware;
  1414. chip->hwshare = hwshare;
  1415. spin_lock_init(&chip->reg_lock);
  1416. mutex_init(&chip->mce_mutex);
  1417. mutex_init(&chip->open_mutex);
  1418. chip->card = card;
  1419. chip->rate_constraint = snd_wss_xrate;
  1420. chip->set_playback_format = snd_wss_playback_format;
  1421. chip->set_capture_format = snd_wss_capture_format;
  1422. if (chip->hardware == WSS_HW_OPTI93X)
  1423. memcpy(&chip->image, &snd_opti93x_original_image,
  1424. sizeof(snd_opti93x_original_image));
  1425. else
  1426. memcpy(&chip->image, &snd_wss_original_image,
  1427. sizeof(snd_wss_original_image));
  1428. *rchip = chip;
  1429. return 0;
  1430. }
  1431. int snd_wss_create(struct snd_card *card,
  1432. unsigned long port,
  1433. unsigned long cport,
  1434. int irq, int dma1, int dma2,
  1435. unsigned short hardware,
  1436. unsigned short hwshare,
  1437. struct snd_wss **rchip)
  1438. {
  1439. static struct snd_device_ops ops = {
  1440. .dev_free = snd_wss_dev_free,
  1441. };
  1442. struct snd_wss *chip;
  1443. int err;
  1444. err = snd_wss_new(card, hardware, hwshare, &chip);
  1445. if (err < 0)
  1446. return err;
  1447. chip->irq = -1;
  1448. chip->dma1 = -1;
  1449. chip->dma2 = -1;
  1450. chip->res_port = request_region(port, 4, "CS4231");
  1451. if (!chip->res_port) {
  1452. snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
  1453. snd_wss_free(chip);
  1454. return -EBUSY;
  1455. }
  1456. chip->port = port;
  1457. if ((long)cport >= 0) {
  1458. chip->res_cport = request_region(cport, 8, "CS4232 Control");
  1459. if (!chip->res_cport) {
  1460. snd_printk(KERN_ERR
  1461. "wss: can't grab control port 0x%lx\n", cport);
  1462. snd_wss_free(chip);
  1463. return -ENODEV;
  1464. }
  1465. }
  1466. chip->cport = cport;
  1467. if (!(hwshare & WSS_HWSHARE_IRQ))
  1468. if (request_irq(irq, snd_wss_interrupt, IRQF_DISABLED,
  1469. "CS4231", (void *) chip)) {
  1470. snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
  1471. snd_wss_free(chip);
  1472. return -EBUSY;
  1473. }
  1474. chip->irq = irq;
  1475. if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
  1476. snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
  1477. snd_wss_free(chip);
  1478. return -EBUSY;
  1479. }
  1480. chip->dma1 = dma1;
  1481. if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
  1482. dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
  1483. snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
  1484. snd_wss_free(chip);
  1485. return -EBUSY;
  1486. }
  1487. if (dma1 == dma2 || dma2 < 0) {
  1488. chip->single_dma = 1;
  1489. chip->dma2 = chip->dma1;
  1490. } else
  1491. chip->dma2 = dma2;
  1492. /* global setup */
  1493. if (snd_wss_probe(chip) < 0) {
  1494. snd_wss_free(chip);
  1495. return -ENODEV;
  1496. }
  1497. snd_wss_init(chip);
  1498. #if 0
  1499. if (chip->hardware & WSS_HW_CS4232_MASK) {
  1500. if (chip->res_cport == NULL)
  1501. snd_printk("CS4232 control port features are not accessible\n");
  1502. }
  1503. #endif
  1504. /* Register device */
  1505. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1506. if (err < 0) {
  1507. snd_wss_free(chip);
  1508. return err;
  1509. }
  1510. #ifdef CONFIG_PM
  1511. /* Power Management */
  1512. chip->suspend = snd_wss_suspend;
  1513. chip->resume = snd_wss_resume;
  1514. #endif
  1515. *rchip = chip;
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL(snd_wss_create);
  1519. static struct snd_pcm_ops snd_wss_playback_ops = {
  1520. .open = snd_wss_playback_open,
  1521. .close = snd_wss_playback_close,
  1522. .ioctl = snd_pcm_lib_ioctl,
  1523. .hw_params = snd_wss_playback_hw_params,
  1524. .hw_free = snd_wss_playback_hw_free,
  1525. .prepare = snd_wss_playback_prepare,
  1526. .trigger = snd_wss_trigger,
  1527. .pointer = snd_wss_playback_pointer,
  1528. };
  1529. static struct snd_pcm_ops snd_wss_capture_ops = {
  1530. .open = snd_wss_capture_open,
  1531. .close = snd_wss_capture_close,
  1532. .ioctl = snd_pcm_lib_ioctl,
  1533. .hw_params = snd_wss_capture_hw_params,
  1534. .hw_free = snd_wss_capture_hw_free,
  1535. .prepare = snd_wss_capture_prepare,
  1536. .trigger = snd_wss_trigger,
  1537. .pointer = snd_wss_capture_pointer,
  1538. };
  1539. int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
  1540. {
  1541. struct snd_pcm *pcm;
  1542. int err;
  1543. if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
  1544. return err;
  1545. spin_lock_init(&chip->reg_lock);
  1546. mutex_init(&chip->mce_mutex);
  1547. mutex_init(&chip->open_mutex);
  1548. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
  1549. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
  1550. /* global setup */
  1551. pcm->private_data = chip;
  1552. pcm->info_flags = 0;
  1553. if (chip->single_dma)
  1554. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1555. if (chip->hardware != WSS_HW_INTERWAVE)
  1556. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1557. strcpy(pcm->name, snd_wss_chip_id(chip));
  1558. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1559. snd_dma_isa_data(),
  1560. 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1561. chip->pcm = pcm;
  1562. if (rpcm)
  1563. *rpcm = pcm;
  1564. return 0;
  1565. }
  1566. EXPORT_SYMBOL(snd_wss_pcm);
  1567. static void snd_wss_timer_free(struct snd_timer *timer)
  1568. {
  1569. struct snd_wss *chip = timer->private_data;
  1570. chip->timer = NULL;
  1571. }
  1572. int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
  1573. {
  1574. struct snd_timer *timer;
  1575. struct snd_timer_id tid;
  1576. int err;
  1577. /* Timer initialization */
  1578. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1579. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1580. tid.card = chip->card->number;
  1581. tid.device = device;
  1582. tid.subdevice = 0;
  1583. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1584. return err;
  1585. strcpy(timer->name, snd_wss_chip_id(chip));
  1586. timer->private_data = chip;
  1587. timer->private_free = snd_wss_timer_free;
  1588. timer->hw = snd_wss_timer_table;
  1589. chip->timer = timer;
  1590. if (rtimer)
  1591. *rtimer = timer;
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL(snd_wss_timer);
  1595. /*
  1596. * MIXER part
  1597. */
  1598. static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
  1599. struct snd_ctl_elem_info *uinfo)
  1600. {
  1601. static char *texts[4] = {
  1602. "Line", "Aux", "Mic", "Mix"
  1603. };
  1604. static char *opl3sa_texts[4] = {
  1605. "Line", "CD", "Mic", "Mix"
  1606. };
  1607. static char *gusmax_texts[4] = {
  1608. "Line", "Synth", "Mic", "Mix"
  1609. };
  1610. char **ptexts = texts;
  1611. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1612. snd_assert(chip->card != NULL, return -EINVAL);
  1613. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1614. uinfo->count = 2;
  1615. uinfo->value.enumerated.items = 4;
  1616. if (uinfo->value.enumerated.item > 3)
  1617. uinfo->value.enumerated.item = 3;
  1618. if (!strcmp(chip->card->driver, "GUS MAX"))
  1619. ptexts = gusmax_texts;
  1620. switch (chip->hardware) {
  1621. case WSS_HW_INTERWAVE:
  1622. ptexts = gusmax_texts;
  1623. break;
  1624. case WSS_HW_OPL3SA2:
  1625. ptexts = opl3sa_texts;
  1626. break;
  1627. }
  1628. strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
  1629. return 0;
  1630. }
  1631. static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
  1632. struct snd_ctl_elem_value *ucontrol)
  1633. {
  1634. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1635. unsigned long flags;
  1636. spin_lock_irqsave(&chip->reg_lock, flags);
  1637. ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1638. ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1639. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1640. return 0;
  1641. }
  1642. static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
  1643. struct snd_ctl_elem_value *ucontrol)
  1644. {
  1645. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1646. unsigned long flags;
  1647. unsigned short left, right;
  1648. int change;
  1649. if (ucontrol->value.enumerated.item[0] > 3 ||
  1650. ucontrol->value.enumerated.item[1] > 3)
  1651. return -EINVAL;
  1652. left = ucontrol->value.enumerated.item[0] << 6;
  1653. right = ucontrol->value.enumerated.item[1] << 6;
  1654. spin_lock_irqsave(&chip->reg_lock, flags);
  1655. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1656. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1657. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1658. right != chip->image[CS4231_RIGHT_INPUT];
  1659. snd_wss_out(chip, CS4231_LEFT_INPUT, left);
  1660. snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
  1661. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1662. return change;
  1663. }
  1664. int snd_wss_info_single(struct snd_kcontrol *kcontrol,
  1665. struct snd_ctl_elem_info *uinfo)
  1666. {
  1667. int mask = (kcontrol->private_value >> 16) & 0xff;
  1668. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1669. uinfo->count = 1;
  1670. uinfo->value.integer.min = 0;
  1671. uinfo->value.integer.max = mask;
  1672. return 0;
  1673. }
  1674. EXPORT_SYMBOL(snd_wss_info_single);
  1675. int snd_wss_get_single(struct snd_kcontrol *kcontrol,
  1676. struct snd_ctl_elem_value *ucontrol)
  1677. {
  1678. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1679. unsigned long flags;
  1680. int reg = kcontrol->private_value & 0xff;
  1681. int shift = (kcontrol->private_value >> 8) & 0xff;
  1682. int mask = (kcontrol->private_value >> 16) & 0xff;
  1683. int invert = (kcontrol->private_value >> 24) & 0xff;
  1684. spin_lock_irqsave(&chip->reg_lock, flags);
  1685. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1686. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1687. if (invert)
  1688. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1689. return 0;
  1690. }
  1691. EXPORT_SYMBOL(snd_wss_get_single);
  1692. int snd_wss_put_single(struct snd_kcontrol *kcontrol,
  1693. struct snd_ctl_elem_value *ucontrol)
  1694. {
  1695. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1696. unsigned long flags;
  1697. int reg = kcontrol->private_value & 0xff;
  1698. int shift = (kcontrol->private_value >> 8) & 0xff;
  1699. int mask = (kcontrol->private_value >> 16) & 0xff;
  1700. int invert = (kcontrol->private_value >> 24) & 0xff;
  1701. int change;
  1702. unsigned short val;
  1703. val = (ucontrol->value.integer.value[0] & mask);
  1704. if (invert)
  1705. val = mask - val;
  1706. val <<= shift;
  1707. spin_lock_irqsave(&chip->reg_lock, flags);
  1708. val = (chip->image[reg] & ~(mask << shift)) | val;
  1709. change = val != chip->image[reg];
  1710. snd_wss_out(chip, reg, val);
  1711. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1712. return change;
  1713. }
  1714. EXPORT_SYMBOL(snd_wss_put_single);
  1715. int snd_wss_info_double(struct snd_kcontrol *kcontrol,
  1716. struct snd_ctl_elem_info *uinfo)
  1717. {
  1718. int mask = (kcontrol->private_value >> 24) & 0xff;
  1719. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1720. uinfo->count = 2;
  1721. uinfo->value.integer.min = 0;
  1722. uinfo->value.integer.max = mask;
  1723. return 0;
  1724. }
  1725. EXPORT_SYMBOL(snd_wss_info_double);
  1726. int snd_wss_get_double(struct snd_kcontrol *kcontrol,
  1727. struct snd_ctl_elem_value *ucontrol)
  1728. {
  1729. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1730. unsigned long flags;
  1731. int left_reg = kcontrol->private_value & 0xff;
  1732. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1733. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1734. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1735. int mask = (kcontrol->private_value >> 24) & 0xff;
  1736. int invert = (kcontrol->private_value >> 22) & 1;
  1737. spin_lock_irqsave(&chip->reg_lock, flags);
  1738. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1739. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1740. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1741. if (invert) {
  1742. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1743. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1744. }
  1745. return 0;
  1746. }
  1747. EXPORT_SYMBOL(snd_wss_get_double);
  1748. int snd_wss_put_double(struct snd_kcontrol *kcontrol,
  1749. struct snd_ctl_elem_value *ucontrol)
  1750. {
  1751. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1752. unsigned long flags;
  1753. int left_reg = kcontrol->private_value & 0xff;
  1754. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1755. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1756. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1757. int mask = (kcontrol->private_value >> 24) & 0xff;
  1758. int invert = (kcontrol->private_value >> 22) & 1;
  1759. int change;
  1760. unsigned short val1, val2;
  1761. val1 = ucontrol->value.integer.value[0] & mask;
  1762. val2 = ucontrol->value.integer.value[1] & mask;
  1763. if (invert) {
  1764. val1 = mask - val1;
  1765. val2 = mask - val2;
  1766. }
  1767. val1 <<= shift_left;
  1768. val2 <<= shift_right;
  1769. spin_lock_irqsave(&chip->reg_lock, flags);
  1770. if (left_reg != right_reg) {
  1771. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1772. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1773. change = val1 != chip->image[left_reg] ||
  1774. val2 != chip->image[right_reg];
  1775. snd_wss_out(chip, left_reg, val1);
  1776. snd_wss_out(chip, right_reg, val2);
  1777. } else {
  1778. mask = (mask << shift_left) | (mask << shift_right);
  1779. val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
  1780. change = val1 != chip->image[left_reg];
  1781. snd_wss_out(chip, left_reg, val1);
  1782. }
  1783. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1784. return change;
  1785. }
  1786. EXPORT_SYMBOL(snd_wss_put_double);
  1787. static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
  1788. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  1789. static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
  1790. static struct snd_kcontrol_new snd_ad1848_controls[] = {
  1791. WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT,
  1792. 7, 7, 1, 1),
  1793. WSS_DOUBLE_TLV("PCM Playback Volume", 0,
  1794. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
  1795. db_scale_6bit),
  1796. WSS_DOUBLE("Aux Playback Switch", 0,
  1797. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1798. WSS_DOUBLE_TLV("Aux Playback Volume", 0,
  1799. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
  1800. db_scale_5bit_12db_max),
  1801. WSS_DOUBLE("Aux Playback Switch", 1,
  1802. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1803. WSS_DOUBLE_TLV("Aux Playback Volume", 1,
  1804. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
  1805. db_scale_5bit_12db_max),
  1806. WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
  1807. 0, 0, 15, 0, db_scale_rec_gain),
  1808. {
  1809. .name = "Capture Source",
  1810. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1811. .info = snd_wss_info_mux,
  1812. .get = snd_wss_get_mux,
  1813. .put = snd_wss_put_mux,
  1814. },
  1815. WSS_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  1816. WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 1, 63, 0,
  1817. db_scale_6bit),
  1818. };
  1819. static struct snd_kcontrol_new snd_wss_controls[] = {
  1820. WSS_DOUBLE("PCM Playback Switch", 0,
  1821. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1822. WSS_DOUBLE("PCM Playback Volume", 0,
  1823. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  1824. WSS_DOUBLE("Line Playback Switch", 0,
  1825. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1826. WSS_DOUBLE("Line Playback Volume", 0,
  1827. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  1828. WSS_DOUBLE("Aux Playback Switch", 0,
  1829. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1830. WSS_DOUBLE("Aux Playback Volume", 0,
  1831. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  1832. WSS_DOUBLE("Aux Playback Switch", 1,
  1833. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1834. WSS_DOUBLE("Aux Playback Volume", 1,
  1835. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  1836. WSS_SINGLE("Mono Playback Switch", 0,
  1837. CS4231_MONO_CTRL, 7, 1, 1),
  1838. WSS_SINGLE("Mono Playback Volume", 0,
  1839. CS4231_MONO_CTRL, 0, 15, 1),
  1840. WSS_SINGLE("Mono Output Playback Switch", 0,
  1841. CS4231_MONO_CTRL, 6, 1, 1),
  1842. WSS_SINGLE("Mono Output Playback Bypass", 0,
  1843. CS4231_MONO_CTRL, 5, 1, 0),
  1844. WSS_DOUBLE("Capture Volume", 0,
  1845. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1846. {
  1847. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1848. .name = "Capture Source",
  1849. .info = snd_wss_info_mux,
  1850. .get = snd_wss_get_mux,
  1851. .put = snd_wss_put_mux,
  1852. },
  1853. WSS_DOUBLE("Mic Boost", 0,
  1854. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1855. WSS_SINGLE("Loopback Capture Switch", 0,
  1856. CS4231_LOOPBACK, 0, 1, 0),
  1857. WSS_SINGLE("Loopback Capture Volume", 0,
  1858. CS4231_LOOPBACK, 2, 63, 1)
  1859. };
  1860. static struct snd_kcontrol_new snd_opti93x_controls[] = {
  1861. WSS_DOUBLE("Master Playback Switch", 0,
  1862. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
  1863. WSS_DOUBLE("Master Playback Volume", 0,
  1864. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
  1865. WSS_DOUBLE("PCM Playback Switch", 0,
  1866. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1867. WSS_DOUBLE("PCM Playback Volume", 0,
  1868. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
  1869. WSS_DOUBLE("FM Playback Switch", 0,
  1870. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1871. WSS_DOUBLE("FM Playback Volume", 0,
  1872. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
  1873. WSS_DOUBLE("Line Playback Switch", 0,
  1874. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1875. WSS_DOUBLE("Line Playback Volume", 0,
  1876. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
  1877. WSS_DOUBLE("Mic Playback Switch", 0,
  1878. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
  1879. WSS_DOUBLE("Mic Playback Volume", 0,
  1880. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
  1881. WSS_DOUBLE("Mic Boost", 0,
  1882. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1883. WSS_DOUBLE("CD Playback Switch", 0,
  1884. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1885. WSS_DOUBLE("CD Playback Volume", 0,
  1886. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
  1887. WSS_DOUBLE("Aux Playback Switch", 0,
  1888. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
  1889. WSS_DOUBLE("Aux Playback Volume", 0,
  1890. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
  1891. WSS_DOUBLE("Capture Volume", 0,
  1892. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1893. {
  1894. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1895. .name = "Capture Source",
  1896. .info = snd_wss_info_mux,
  1897. .get = snd_wss_get_mux,
  1898. .put = snd_wss_put_mux,
  1899. }
  1900. };
  1901. int snd_wss_mixer(struct snd_wss *chip)
  1902. {
  1903. struct snd_card *card;
  1904. unsigned int idx;
  1905. int err;
  1906. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1907. card = chip->card;
  1908. strcpy(card->mixername, chip->pcm->name);
  1909. if (chip->hardware == WSS_HW_OPTI93X)
  1910. for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
  1911. err = snd_ctl_add(card,
  1912. snd_ctl_new1(&snd_opti93x_controls[idx],
  1913. chip));
  1914. if (err < 0)
  1915. return err;
  1916. }
  1917. else if (chip->hardware & WSS_HW_AD1848_MASK)
  1918. for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++) {
  1919. err = snd_ctl_add(card,
  1920. snd_ctl_new1(&snd_ad1848_controls[idx],
  1921. chip));
  1922. if (err < 0)
  1923. return err;
  1924. }
  1925. else
  1926. for (idx = 0; idx < ARRAY_SIZE(snd_wss_controls); idx++) {
  1927. err = snd_ctl_add(card,
  1928. snd_ctl_new1(&snd_wss_controls[idx],
  1929. chip));
  1930. if (err < 0)
  1931. return err;
  1932. }
  1933. return 0;
  1934. }
  1935. EXPORT_SYMBOL(snd_wss_mixer);
  1936. /*
  1937. * INIT part
  1938. */
  1939. static int __init alsa_wss_init(void)
  1940. {
  1941. return 0;
  1942. }
  1943. static void __exit alsa_wss_exit(void)
  1944. {
  1945. }
  1946. module_init(alsa_wss_init);
  1947. module_exit(alsa_wss_exit);