wss_lib.c 62 KB

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