wss_lib.c 66 KB

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