wss_lib.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  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 <sound/core.h>
  34. #include <sound/wss.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/tlv.h>
  37. #include <asm/io.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_RESUME |
  1331. SNDRV_PCM_INFO_SYNC_START),
  1332. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1333. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1334. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1335. .rate_min = 5510,
  1336. .rate_max = 48000,
  1337. .channels_min = 1,
  1338. .channels_max = 2,
  1339. .buffer_bytes_max = (128*1024),
  1340. .period_bytes_min = 64,
  1341. .period_bytes_max = (128*1024),
  1342. .periods_min = 1,
  1343. .periods_max = 1024,
  1344. .fifo_size = 0,
  1345. };
  1346. static struct snd_pcm_hardware snd_wss_capture =
  1347. {
  1348. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1349. SNDRV_PCM_INFO_MMAP_VALID |
  1350. SNDRV_PCM_INFO_RESUME |
  1351. SNDRV_PCM_INFO_SYNC_START),
  1352. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1353. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1354. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1355. .rate_min = 5510,
  1356. .rate_max = 48000,
  1357. .channels_min = 1,
  1358. .channels_max = 2,
  1359. .buffer_bytes_max = (128*1024),
  1360. .period_bytes_min = 64,
  1361. .period_bytes_max = (128*1024),
  1362. .periods_min = 1,
  1363. .periods_max = 1024,
  1364. .fifo_size = 0,
  1365. };
  1366. /*
  1367. */
  1368. static int snd_wss_playback_open(struct snd_pcm_substream *substream)
  1369. {
  1370. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1371. struct snd_pcm_runtime *runtime = substream->runtime;
  1372. int err;
  1373. runtime->hw = snd_wss_playback;
  1374. /* hardware limitation of older chipsets */
  1375. if (chip->hardware & WSS_HW_AD1848_MASK)
  1376. runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1377. SNDRV_PCM_FMTBIT_S16_BE);
  1378. /* hardware bug in InterWave chipset */
  1379. if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
  1380. runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
  1381. /* hardware limitation of cheap chips */
  1382. if (chip->hardware == WSS_HW_CS4235 ||
  1383. chip->hardware == WSS_HW_CS4239)
  1384. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1385. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  1386. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  1387. if (chip->claim_dma) {
  1388. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
  1389. return err;
  1390. }
  1391. err = snd_wss_open(chip, WSS_MODE_PLAY);
  1392. if (err < 0) {
  1393. if (chip->release_dma)
  1394. chip->release_dma(chip, chip->dma_private_data, chip->dma1);
  1395. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1396. return err;
  1397. }
  1398. chip->playback_substream = substream;
  1399. snd_pcm_set_sync(substream);
  1400. chip->rate_constraint(runtime);
  1401. return 0;
  1402. }
  1403. static int snd_wss_capture_open(struct snd_pcm_substream *substream)
  1404. {
  1405. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1406. struct snd_pcm_runtime *runtime = substream->runtime;
  1407. int err;
  1408. runtime->hw = snd_wss_capture;
  1409. /* hardware limitation of older chipsets */
  1410. if (chip->hardware & WSS_HW_AD1848_MASK)
  1411. runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1412. SNDRV_PCM_FMTBIT_S16_BE);
  1413. /* hardware limitation of cheap chips */
  1414. if (chip->hardware == WSS_HW_CS4235 ||
  1415. chip->hardware == WSS_HW_CS4239 ||
  1416. chip->hardware == WSS_HW_OPTI93X)
  1417. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 |
  1418. SNDRV_PCM_FMTBIT_S16_LE;
  1419. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  1420. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  1421. if (chip->claim_dma) {
  1422. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
  1423. return err;
  1424. }
  1425. err = snd_wss_open(chip, WSS_MODE_RECORD);
  1426. if (err < 0) {
  1427. if (chip->release_dma)
  1428. chip->release_dma(chip, chip->dma_private_data, chip->dma2);
  1429. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1430. return err;
  1431. }
  1432. chip->capture_substream = substream;
  1433. snd_pcm_set_sync(substream);
  1434. chip->rate_constraint(runtime);
  1435. return 0;
  1436. }
  1437. static int snd_wss_playback_close(struct snd_pcm_substream *substream)
  1438. {
  1439. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1440. chip->playback_substream = NULL;
  1441. snd_wss_close(chip, WSS_MODE_PLAY);
  1442. return 0;
  1443. }
  1444. static int snd_wss_capture_close(struct snd_pcm_substream *substream)
  1445. {
  1446. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  1447. chip->capture_substream = NULL;
  1448. snd_wss_close(chip, WSS_MODE_RECORD);
  1449. return 0;
  1450. }
  1451. static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
  1452. {
  1453. int tmp;
  1454. if (!chip->thinkpad_flag)
  1455. return;
  1456. outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
  1457. tmp = inb(AD1848_THINKPAD_CTL_PORT2);
  1458. if (on)
  1459. /* turn it on */
  1460. tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
  1461. else
  1462. /* turn it off */
  1463. tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
  1464. outb(tmp, AD1848_THINKPAD_CTL_PORT2);
  1465. }
  1466. #ifdef CONFIG_PM
  1467. /* lowlevel suspend callback for CS4231 */
  1468. static void snd_wss_suspend(struct snd_wss *chip)
  1469. {
  1470. int reg;
  1471. unsigned long flags;
  1472. snd_pcm_suspend_all(chip->pcm);
  1473. spin_lock_irqsave(&chip->reg_lock, flags);
  1474. for (reg = 0; reg < 32; reg++)
  1475. chip->image[reg] = snd_wss_in(chip, reg);
  1476. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1477. if (chip->thinkpad_flag)
  1478. snd_wss_thinkpad_twiddle(chip, 0);
  1479. }
  1480. /* lowlevel resume callback for CS4231 */
  1481. static void snd_wss_resume(struct snd_wss *chip)
  1482. {
  1483. int reg;
  1484. unsigned long flags;
  1485. /* int timeout; */
  1486. if (chip->thinkpad_flag)
  1487. snd_wss_thinkpad_twiddle(chip, 1);
  1488. snd_wss_mce_up(chip);
  1489. spin_lock_irqsave(&chip->reg_lock, flags);
  1490. for (reg = 0; reg < 32; reg++) {
  1491. switch (reg) {
  1492. case CS4231_VERSION:
  1493. break;
  1494. default:
  1495. snd_wss_out(chip, reg, chip->image[reg]);
  1496. break;
  1497. }
  1498. }
  1499. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1500. #if 1
  1501. snd_wss_mce_down(chip);
  1502. #else
  1503. /* The following is a workaround to avoid freeze after resume on TP600E.
  1504. This is the first half of copy of snd_wss_mce_down(), but doesn't
  1505. include rescheduling. -- iwai
  1506. */
  1507. snd_wss_busy_wait(chip);
  1508. spin_lock_irqsave(&chip->reg_lock, flags);
  1509. chip->mce_bit &= ~CS4231_MCE;
  1510. timeout = wss_inb(chip, CS4231P(REGSEL));
  1511. wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  1512. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1513. if (timeout == 0x80)
  1514. snd_printk(KERN_ERR "down [0x%lx]: serious init problem "
  1515. "- codec still busy\n", chip->port);
  1516. if ((timeout & CS4231_MCE) == 0 ||
  1517. !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
  1518. return;
  1519. }
  1520. snd_wss_busy_wait(chip);
  1521. #endif
  1522. }
  1523. #endif /* CONFIG_PM */
  1524. static int snd_wss_free(struct snd_wss *chip)
  1525. {
  1526. release_and_free_resource(chip->res_port);
  1527. release_and_free_resource(chip->res_cport);
  1528. if (chip->irq >= 0) {
  1529. disable_irq(chip->irq);
  1530. if (!(chip->hwshare & WSS_HWSHARE_IRQ))
  1531. free_irq(chip->irq, (void *) chip);
  1532. }
  1533. if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
  1534. snd_dma_disable(chip->dma1);
  1535. free_dma(chip->dma1);
  1536. }
  1537. if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
  1538. chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
  1539. snd_dma_disable(chip->dma2);
  1540. free_dma(chip->dma2);
  1541. }
  1542. if (chip->timer)
  1543. snd_device_free(chip->card, chip->timer);
  1544. kfree(chip);
  1545. return 0;
  1546. }
  1547. static int snd_wss_dev_free(struct snd_device *device)
  1548. {
  1549. struct snd_wss *chip = device->device_data;
  1550. return snd_wss_free(chip);
  1551. }
  1552. const char *snd_wss_chip_id(struct snd_wss *chip)
  1553. {
  1554. switch (chip->hardware) {
  1555. case WSS_HW_CS4231:
  1556. return "CS4231";
  1557. case WSS_HW_CS4231A:
  1558. return "CS4231A";
  1559. case WSS_HW_CS4232:
  1560. return "CS4232";
  1561. case WSS_HW_CS4232A:
  1562. return "CS4232A";
  1563. case WSS_HW_CS4235:
  1564. return "CS4235";
  1565. case WSS_HW_CS4236:
  1566. return "CS4236";
  1567. case WSS_HW_CS4236B:
  1568. return "CS4236B";
  1569. case WSS_HW_CS4237B:
  1570. return "CS4237B";
  1571. case WSS_HW_CS4238B:
  1572. return "CS4238B";
  1573. case WSS_HW_CS4239:
  1574. return "CS4239";
  1575. case WSS_HW_INTERWAVE:
  1576. return "AMD InterWave";
  1577. case WSS_HW_OPL3SA2:
  1578. return chip->card->shortname;
  1579. case WSS_HW_AD1845:
  1580. return "AD1845";
  1581. case WSS_HW_OPTI93X:
  1582. return "OPTi 93x";
  1583. case WSS_HW_AD1847:
  1584. return "AD1847";
  1585. case WSS_HW_AD1848:
  1586. return "AD1848";
  1587. case WSS_HW_CS4248:
  1588. return "CS4248";
  1589. case WSS_HW_CMI8330:
  1590. return "CMI8330/C3D";
  1591. default:
  1592. return "???";
  1593. }
  1594. }
  1595. EXPORT_SYMBOL(snd_wss_chip_id);
  1596. static int snd_wss_new(struct snd_card *card,
  1597. unsigned short hardware,
  1598. unsigned short hwshare,
  1599. struct snd_wss **rchip)
  1600. {
  1601. struct snd_wss *chip;
  1602. *rchip = NULL;
  1603. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1604. if (chip == NULL)
  1605. return -ENOMEM;
  1606. chip->hardware = hardware;
  1607. chip->hwshare = hwshare;
  1608. spin_lock_init(&chip->reg_lock);
  1609. mutex_init(&chip->mce_mutex);
  1610. mutex_init(&chip->open_mutex);
  1611. chip->card = card;
  1612. chip->rate_constraint = snd_wss_xrate;
  1613. chip->set_playback_format = snd_wss_playback_format;
  1614. chip->set_capture_format = snd_wss_capture_format;
  1615. if (chip->hardware == WSS_HW_OPTI93X)
  1616. memcpy(&chip->image, &snd_opti93x_original_image,
  1617. sizeof(snd_opti93x_original_image));
  1618. else
  1619. memcpy(&chip->image, &snd_wss_original_image,
  1620. sizeof(snd_wss_original_image));
  1621. if (chip->hardware & WSS_HW_AD1848_MASK) {
  1622. chip->image[CS4231_PIN_CTRL] = 0;
  1623. chip->image[CS4231_TEST_INIT] = 0;
  1624. }
  1625. *rchip = chip;
  1626. return 0;
  1627. }
  1628. int snd_wss_create(struct snd_card *card,
  1629. unsigned long port,
  1630. unsigned long cport,
  1631. int irq, int dma1, int dma2,
  1632. unsigned short hardware,
  1633. unsigned short hwshare,
  1634. struct snd_wss **rchip)
  1635. {
  1636. static struct snd_device_ops ops = {
  1637. .dev_free = snd_wss_dev_free,
  1638. };
  1639. struct snd_wss *chip;
  1640. int err;
  1641. err = snd_wss_new(card, hardware, hwshare, &chip);
  1642. if (err < 0)
  1643. return err;
  1644. chip->irq = -1;
  1645. chip->dma1 = -1;
  1646. chip->dma2 = -1;
  1647. chip->res_port = request_region(port, 4, "WSS");
  1648. if (!chip->res_port) {
  1649. snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
  1650. snd_wss_free(chip);
  1651. return -EBUSY;
  1652. }
  1653. chip->port = port;
  1654. if ((long)cport >= 0) {
  1655. chip->res_cport = request_region(cport, 8, "CS4232 Control");
  1656. if (!chip->res_cport) {
  1657. snd_printk(KERN_ERR
  1658. "wss: can't grab control port 0x%lx\n", cport);
  1659. snd_wss_free(chip);
  1660. return -ENODEV;
  1661. }
  1662. }
  1663. chip->cport = cport;
  1664. if (!(hwshare & WSS_HWSHARE_IRQ))
  1665. if (request_irq(irq, snd_wss_interrupt, 0,
  1666. "WSS", (void *) chip)) {
  1667. snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
  1668. snd_wss_free(chip);
  1669. return -EBUSY;
  1670. }
  1671. chip->irq = irq;
  1672. if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "WSS - 1")) {
  1673. snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
  1674. snd_wss_free(chip);
  1675. return -EBUSY;
  1676. }
  1677. chip->dma1 = dma1;
  1678. if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
  1679. dma2 >= 0 && request_dma(dma2, "WSS - 2")) {
  1680. snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
  1681. snd_wss_free(chip);
  1682. return -EBUSY;
  1683. }
  1684. if (dma1 == dma2 || dma2 < 0) {
  1685. chip->single_dma = 1;
  1686. chip->dma2 = chip->dma1;
  1687. } else
  1688. chip->dma2 = dma2;
  1689. if (hardware == WSS_HW_THINKPAD) {
  1690. chip->thinkpad_flag = 1;
  1691. chip->hardware = WSS_HW_DETECT; /* reset */
  1692. snd_wss_thinkpad_twiddle(chip, 1);
  1693. }
  1694. /* global setup */
  1695. if (snd_wss_probe(chip) < 0) {
  1696. snd_wss_free(chip);
  1697. return -ENODEV;
  1698. }
  1699. snd_wss_init(chip);
  1700. #if 0
  1701. if (chip->hardware & WSS_HW_CS4232_MASK) {
  1702. if (chip->res_cport == NULL)
  1703. snd_printk(KERN_ERR "CS4232 control port features are "
  1704. "not accessible\n");
  1705. }
  1706. #endif
  1707. /* Register device */
  1708. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1709. if (err < 0) {
  1710. snd_wss_free(chip);
  1711. return err;
  1712. }
  1713. #ifdef CONFIG_PM
  1714. /* Power Management */
  1715. chip->suspend = snd_wss_suspend;
  1716. chip->resume = snd_wss_resume;
  1717. #endif
  1718. *rchip = chip;
  1719. return 0;
  1720. }
  1721. EXPORT_SYMBOL(snd_wss_create);
  1722. static struct snd_pcm_ops snd_wss_playback_ops = {
  1723. .open = snd_wss_playback_open,
  1724. .close = snd_wss_playback_close,
  1725. .ioctl = snd_pcm_lib_ioctl,
  1726. .hw_params = snd_wss_playback_hw_params,
  1727. .hw_free = snd_wss_playback_hw_free,
  1728. .prepare = snd_wss_playback_prepare,
  1729. .trigger = snd_wss_trigger,
  1730. .pointer = snd_wss_playback_pointer,
  1731. };
  1732. static struct snd_pcm_ops snd_wss_capture_ops = {
  1733. .open = snd_wss_capture_open,
  1734. .close = snd_wss_capture_close,
  1735. .ioctl = snd_pcm_lib_ioctl,
  1736. .hw_params = snd_wss_capture_hw_params,
  1737. .hw_free = snd_wss_capture_hw_free,
  1738. .prepare = snd_wss_capture_prepare,
  1739. .trigger = snd_wss_trigger,
  1740. .pointer = snd_wss_capture_pointer,
  1741. };
  1742. int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
  1743. {
  1744. struct snd_pcm *pcm;
  1745. int err;
  1746. err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
  1747. if (err < 0)
  1748. return err;
  1749. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
  1750. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
  1751. /* global setup */
  1752. pcm->private_data = chip;
  1753. pcm->info_flags = 0;
  1754. if (chip->single_dma)
  1755. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1756. if (chip->hardware != WSS_HW_INTERWAVE)
  1757. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1758. strcpy(pcm->name, snd_wss_chip_id(chip));
  1759. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1760. snd_dma_isa_data(),
  1761. 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1762. chip->pcm = pcm;
  1763. if (rpcm)
  1764. *rpcm = pcm;
  1765. return 0;
  1766. }
  1767. EXPORT_SYMBOL(snd_wss_pcm);
  1768. static void snd_wss_timer_free(struct snd_timer *timer)
  1769. {
  1770. struct snd_wss *chip = timer->private_data;
  1771. chip->timer = NULL;
  1772. }
  1773. int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
  1774. {
  1775. struct snd_timer *timer;
  1776. struct snd_timer_id tid;
  1777. int err;
  1778. /* Timer initialization */
  1779. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1780. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1781. tid.card = chip->card->number;
  1782. tid.device = device;
  1783. tid.subdevice = 0;
  1784. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1785. return err;
  1786. strcpy(timer->name, snd_wss_chip_id(chip));
  1787. timer->private_data = chip;
  1788. timer->private_free = snd_wss_timer_free;
  1789. timer->hw = snd_wss_timer_table;
  1790. chip->timer = timer;
  1791. if (rtimer)
  1792. *rtimer = timer;
  1793. return 0;
  1794. }
  1795. EXPORT_SYMBOL(snd_wss_timer);
  1796. /*
  1797. * MIXER part
  1798. */
  1799. static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
  1800. struct snd_ctl_elem_info *uinfo)
  1801. {
  1802. static char *texts[4] = {
  1803. "Line", "Aux", "Mic", "Mix"
  1804. };
  1805. static char *opl3sa_texts[4] = {
  1806. "Line", "CD", "Mic", "Mix"
  1807. };
  1808. static char *gusmax_texts[4] = {
  1809. "Line", "Synth", "Mic", "Mix"
  1810. };
  1811. char **ptexts = texts;
  1812. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1813. if (snd_BUG_ON(!chip->card))
  1814. return -EINVAL;
  1815. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1816. uinfo->count = 2;
  1817. uinfo->value.enumerated.items = 4;
  1818. if (uinfo->value.enumerated.item > 3)
  1819. uinfo->value.enumerated.item = 3;
  1820. if (!strcmp(chip->card->driver, "GUS MAX"))
  1821. ptexts = gusmax_texts;
  1822. switch (chip->hardware) {
  1823. case WSS_HW_INTERWAVE:
  1824. ptexts = gusmax_texts;
  1825. break;
  1826. case WSS_HW_OPTI93X:
  1827. case WSS_HW_OPL3SA2:
  1828. ptexts = opl3sa_texts;
  1829. break;
  1830. }
  1831. strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
  1832. return 0;
  1833. }
  1834. static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
  1835. struct snd_ctl_elem_value *ucontrol)
  1836. {
  1837. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1838. unsigned long flags;
  1839. spin_lock_irqsave(&chip->reg_lock, flags);
  1840. ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1841. ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1842. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1843. return 0;
  1844. }
  1845. static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
  1846. struct snd_ctl_elem_value *ucontrol)
  1847. {
  1848. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1849. unsigned long flags;
  1850. unsigned short left, right;
  1851. int change;
  1852. if (ucontrol->value.enumerated.item[0] > 3 ||
  1853. ucontrol->value.enumerated.item[1] > 3)
  1854. return -EINVAL;
  1855. left = ucontrol->value.enumerated.item[0] << 6;
  1856. right = ucontrol->value.enumerated.item[1] << 6;
  1857. spin_lock_irqsave(&chip->reg_lock, flags);
  1858. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1859. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1860. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1861. right != chip->image[CS4231_RIGHT_INPUT];
  1862. snd_wss_out(chip, CS4231_LEFT_INPUT, left);
  1863. snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
  1864. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1865. return change;
  1866. }
  1867. int snd_wss_info_single(struct snd_kcontrol *kcontrol,
  1868. struct snd_ctl_elem_info *uinfo)
  1869. {
  1870. int mask = (kcontrol->private_value >> 16) & 0xff;
  1871. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1872. uinfo->count = 1;
  1873. uinfo->value.integer.min = 0;
  1874. uinfo->value.integer.max = mask;
  1875. return 0;
  1876. }
  1877. EXPORT_SYMBOL(snd_wss_info_single);
  1878. int snd_wss_get_single(struct snd_kcontrol *kcontrol,
  1879. struct snd_ctl_elem_value *ucontrol)
  1880. {
  1881. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1882. unsigned long flags;
  1883. int reg = kcontrol->private_value & 0xff;
  1884. int shift = (kcontrol->private_value >> 8) & 0xff;
  1885. int mask = (kcontrol->private_value >> 16) & 0xff;
  1886. int invert = (kcontrol->private_value >> 24) & 0xff;
  1887. spin_lock_irqsave(&chip->reg_lock, flags);
  1888. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1889. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1890. if (invert)
  1891. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1892. return 0;
  1893. }
  1894. EXPORT_SYMBOL(snd_wss_get_single);
  1895. int snd_wss_put_single(struct snd_kcontrol *kcontrol,
  1896. struct snd_ctl_elem_value *ucontrol)
  1897. {
  1898. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1899. unsigned long flags;
  1900. int reg = kcontrol->private_value & 0xff;
  1901. int shift = (kcontrol->private_value >> 8) & 0xff;
  1902. int mask = (kcontrol->private_value >> 16) & 0xff;
  1903. int invert = (kcontrol->private_value >> 24) & 0xff;
  1904. int change;
  1905. unsigned short val;
  1906. val = (ucontrol->value.integer.value[0] & mask);
  1907. if (invert)
  1908. val = mask - val;
  1909. val <<= shift;
  1910. spin_lock_irqsave(&chip->reg_lock, flags);
  1911. val = (chip->image[reg] & ~(mask << shift)) | val;
  1912. change = val != chip->image[reg];
  1913. snd_wss_out(chip, reg, val);
  1914. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1915. return change;
  1916. }
  1917. EXPORT_SYMBOL(snd_wss_put_single);
  1918. int snd_wss_info_double(struct snd_kcontrol *kcontrol,
  1919. struct snd_ctl_elem_info *uinfo)
  1920. {
  1921. int mask = (kcontrol->private_value >> 24) & 0xff;
  1922. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1923. uinfo->count = 2;
  1924. uinfo->value.integer.min = 0;
  1925. uinfo->value.integer.max = mask;
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL(snd_wss_info_double);
  1929. int snd_wss_get_double(struct snd_kcontrol *kcontrol,
  1930. struct snd_ctl_elem_value *ucontrol)
  1931. {
  1932. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1933. unsigned long flags;
  1934. int left_reg = kcontrol->private_value & 0xff;
  1935. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1936. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1937. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1938. int mask = (kcontrol->private_value >> 24) & 0xff;
  1939. int invert = (kcontrol->private_value >> 22) & 1;
  1940. spin_lock_irqsave(&chip->reg_lock, flags);
  1941. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1942. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1943. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1944. if (invert) {
  1945. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1946. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1947. }
  1948. return 0;
  1949. }
  1950. EXPORT_SYMBOL(snd_wss_get_double);
  1951. int snd_wss_put_double(struct snd_kcontrol *kcontrol,
  1952. struct snd_ctl_elem_value *ucontrol)
  1953. {
  1954. struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
  1955. unsigned long flags;
  1956. int left_reg = kcontrol->private_value & 0xff;
  1957. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1958. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1959. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1960. int mask = (kcontrol->private_value >> 24) & 0xff;
  1961. int invert = (kcontrol->private_value >> 22) & 1;
  1962. int change;
  1963. unsigned short val1, val2;
  1964. val1 = ucontrol->value.integer.value[0] & mask;
  1965. val2 = ucontrol->value.integer.value[1] & mask;
  1966. if (invert) {
  1967. val1 = mask - val1;
  1968. val2 = mask - val2;
  1969. }
  1970. val1 <<= shift_left;
  1971. val2 <<= shift_right;
  1972. spin_lock_irqsave(&chip->reg_lock, flags);
  1973. if (left_reg != right_reg) {
  1974. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1975. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1976. change = val1 != chip->image[left_reg] ||
  1977. val2 != chip->image[right_reg];
  1978. snd_wss_out(chip, left_reg, val1);
  1979. snd_wss_out(chip, right_reg, val2);
  1980. } else {
  1981. mask = (mask << shift_left) | (mask << shift_right);
  1982. val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
  1983. change = val1 != chip->image[left_reg];
  1984. snd_wss_out(chip, left_reg, val1);
  1985. }
  1986. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1987. return change;
  1988. }
  1989. EXPORT_SYMBOL(snd_wss_put_double);
  1990. static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
  1991. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  1992. static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
  1993. static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
  1994. static struct snd_kcontrol_new snd_wss_controls[] = {
  1995. WSS_DOUBLE("PCM Playback Switch", 0,
  1996. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1997. WSS_DOUBLE_TLV("PCM Playback Volume", 0,
  1998. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
  1999. db_scale_6bit),
  2000. WSS_DOUBLE("Aux Playback Switch", 0,
  2001. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  2002. WSS_DOUBLE_TLV("Aux Playback Volume", 0,
  2003. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
  2004. db_scale_5bit_12db_max),
  2005. WSS_DOUBLE("Aux Playback Switch", 1,
  2006. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  2007. WSS_DOUBLE_TLV("Aux Playback Volume", 1,
  2008. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
  2009. db_scale_5bit_12db_max),
  2010. WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
  2011. 0, 0, 15, 0, db_scale_rec_gain),
  2012. {
  2013. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2014. .name = "Capture Source",
  2015. .info = snd_wss_info_mux,
  2016. .get = snd_wss_get_mux,
  2017. .put = snd_wss_put_mux,
  2018. },
  2019. WSS_DOUBLE("Mic Boost (+20dB)", 0,
  2020. CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  2021. WSS_SINGLE("Loopback Capture Switch", 0,
  2022. CS4231_LOOPBACK, 0, 1, 0),
  2023. WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1,
  2024. db_scale_6bit),
  2025. WSS_DOUBLE("Line Playback Switch", 0,
  2026. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  2027. WSS_DOUBLE_TLV("Line Playback Volume", 0,
  2028. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1,
  2029. db_scale_5bit_12db_max),
  2030. WSS_SINGLE("Beep Playback Switch", 0,
  2031. CS4231_MONO_CTRL, 7, 1, 1),
  2032. WSS_SINGLE_TLV("Beep Playback Volume", 0,
  2033. CS4231_MONO_CTRL, 0, 15, 1,
  2034. db_scale_4bit),
  2035. WSS_SINGLE("Mono Output Playback Switch", 0,
  2036. CS4231_MONO_CTRL, 6, 1, 1),
  2037. WSS_SINGLE("Beep Bypass Playback Switch", 0,
  2038. CS4231_MONO_CTRL, 5, 1, 0),
  2039. };
  2040. int snd_wss_mixer(struct snd_wss *chip)
  2041. {
  2042. struct snd_card *card;
  2043. unsigned int idx;
  2044. int err;
  2045. int count = ARRAY_SIZE(snd_wss_controls);
  2046. if (snd_BUG_ON(!chip || !chip->pcm))
  2047. return -EINVAL;
  2048. card = chip->card;
  2049. strcpy(card->mixername, chip->pcm->name);
  2050. /* Use only the first 11 entries on AD1848 */
  2051. if (chip->hardware & WSS_HW_AD1848_MASK)
  2052. count = 11;
  2053. /* There is no loopback on OPTI93X */
  2054. else if (chip->hardware == WSS_HW_OPTI93X)
  2055. count = 9;
  2056. for (idx = 0; idx < count; idx++) {
  2057. err = snd_ctl_add(card,
  2058. snd_ctl_new1(&snd_wss_controls[idx],
  2059. chip));
  2060. if (err < 0)
  2061. return err;
  2062. }
  2063. return 0;
  2064. }
  2065. EXPORT_SYMBOL(snd_wss_mixer);
  2066. const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
  2067. {
  2068. return direction == SNDRV_PCM_STREAM_PLAYBACK ?
  2069. &snd_wss_playback_ops : &snd_wss_capture_ops;
  2070. }
  2071. EXPORT_SYMBOL(snd_wss_get_pcm_ops);
  2072. /*
  2073. * INIT part
  2074. */
  2075. static int __init alsa_wss_init(void)
  2076. {
  2077. return 0;
  2078. }
  2079. static void __exit alsa_wss_exit(void)
  2080. {
  2081. }
  2082. module_init(alsa_wss_init);
  2083. module_exit(alsa_wss_exit);