wss_lib.c 67 KB

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