wss_lib.c 60 KB

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