wss_lib.c 64 KB

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