wss_lib.c 66 KB

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