wss_lib.c 64 KB

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