cs4231_lib.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
  27. #include <linux/delay.h>
  28. #include <linux/pm.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/slab.h>
  32. #include <linux/ioport.h>
  33. #include <sound/core.h>
  34. #include <sound/cs4231.h>
  35. #include <sound/pcm_params.h>
  36. #include <asm/io.h>
  37. #include <asm/dma.h>
  38. #include <asm/irq.h>
  39. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.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 = 14,
  70. .list = rates,
  71. .mask = 0,
  72. };
  73. static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
  74. {
  75. return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  76. }
  77. static unsigned char snd_cs4231_original_image[32] =
  78. {
  79. 0x00, /* 00/00 - lic */
  80. 0x00, /* 01/01 - ric */
  81. 0x9f, /* 02/02 - la1ic */
  82. 0x9f, /* 03/03 - ra1ic */
  83. 0x9f, /* 04/04 - la2ic */
  84. 0x9f, /* 05/05 - ra2ic */
  85. 0xbf, /* 06/06 - loc */
  86. 0xbf, /* 07/07 - roc */
  87. 0x20, /* 08/08 - pdfr */
  88. CS4231_AUTOCALIB, /* 09/09 - ic */
  89. 0x00, /* 0a/10 - pc */
  90. 0x00, /* 0b/11 - ti */
  91. CS4231_MODE2, /* 0c/12 - mi */
  92. 0xfc, /* 0d/13 - lbc */
  93. 0x00, /* 0e/14 - pbru */
  94. 0x00, /* 0f/15 - pbrl */
  95. 0x80, /* 10/16 - afei */
  96. 0x01, /* 11/17 - afeii */
  97. 0x9f, /* 12/18 - llic */
  98. 0x9f, /* 13/19 - rlic */
  99. 0x00, /* 14/20 - tlb */
  100. 0x00, /* 15/21 - thb */
  101. 0x00, /* 16/22 - la3mic/reserved */
  102. 0x00, /* 17/23 - ra3mic/reserved */
  103. 0x00, /* 18/24 - afs */
  104. 0x00, /* 19/25 - lamoc/version */
  105. 0xcf, /* 1a/26 - mioc */
  106. 0x00, /* 1b/27 - ramoc/reserved */
  107. 0x20, /* 1c/28 - cdfr */
  108. 0x00, /* 1d/29 - res4 */
  109. 0x00, /* 1e/30 - cbru */
  110. 0x00, /* 1f/31 - cbrl */
  111. };
  112. /*
  113. * Basic I/O functions
  114. */
  115. static inline void cs4231_outb(struct snd_cs4231 *chip, u8 offset, u8 val)
  116. {
  117. outb(val, chip->port + offset);
  118. }
  119. static inline u8 cs4231_inb(struct snd_cs4231 *chip, u8 offset)
  120. {
  121. return inb(chip->port + offset);
  122. }
  123. static void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
  124. unsigned char mask, unsigned char value)
  125. {
  126. int timeout;
  127. unsigned char tmp;
  128. for (timeout = 250;
  129. timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  130. timeout--)
  131. udelay(100);
  132. #ifdef CONFIG_SND_DEBUG
  133. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  134. snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  135. #endif
  136. if (chip->calibrate_mute) {
  137. chip->image[reg] &= mask;
  138. chip->image[reg] |= value;
  139. } else {
  140. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  141. mb();
  142. tmp = (chip->image[reg] & mask) | value;
  143. cs4231_outb(chip, CS4231P(REG), tmp);
  144. chip->image[reg] = tmp;
  145. mb();
  146. }
  147. }
  148. static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
  149. {
  150. int timeout;
  151. for (timeout = 250;
  152. timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  153. timeout--)
  154. udelay(10);
  155. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  156. cs4231_outb(chip, CS4231P(REG), value);
  157. mb();
  158. }
  159. void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
  160. {
  161. int timeout;
  162. for (timeout = 250;
  163. timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  164. timeout--)
  165. udelay(100);
  166. #ifdef CONFIG_SND_DEBUG
  167. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  168. snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  169. #endif
  170. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  171. cs4231_outb(chip, CS4231P(REG), value);
  172. chip->image[reg] = value;
  173. mb();
  174. #if 0
  175. printk("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value);
  176. #endif
  177. }
  178. unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
  179. {
  180. int timeout;
  181. for (timeout = 250;
  182. timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  183. timeout--)
  184. udelay(100);
  185. #ifdef CONFIG_SND_DEBUG
  186. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  187. snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
  188. #endif
  189. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
  190. mb();
  191. return cs4231_inb(chip, CS4231P(REG));
  192. }
  193. void snd_cs4236_ext_out(struct snd_cs4231 *chip, unsigned char reg, unsigned char val)
  194. {
  195. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
  196. cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
  197. cs4231_outb(chip, CS4231P(REG), val);
  198. chip->eimage[CS4236_REG(reg)] = val;
  199. #if 0
  200. printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
  201. #endif
  202. }
  203. unsigned char snd_cs4236_ext_in(struct snd_cs4231 *chip, unsigned char reg)
  204. {
  205. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
  206. cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
  207. #if 1
  208. return cs4231_inb(chip, CS4231P(REG));
  209. #else
  210. {
  211. unsigned char res;
  212. res = cs4231_inb(chip, CS4231P(REG));
  213. printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
  214. return res;
  215. }
  216. #endif
  217. }
  218. #if 0
  219. static void snd_cs4231_debug(struct snd_cs4231 *chip)
  220. {
  221. printk("CS4231 REGS: INDEX = 0x%02x ", cs4231_inb(chip, CS4231P(REGSEL)));
  222. printk(" STATUS = 0x%02x\n", cs4231_inb(chip, CS4231P(STATUS)));
  223. printk(" 0x00: left input = 0x%02x ", snd_cs4231_in(chip, 0x00));
  224. printk(" 0x10: alt 1 (CFIG 2) = 0x%02x\n", snd_cs4231_in(chip, 0x10));
  225. printk(" 0x01: right input = 0x%02x ", snd_cs4231_in(chip, 0x01));
  226. printk(" 0x11: alt 2 (CFIG 3) = 0x%02x\n", snd_cs4231_in(chip, 0x11));
  227. printk(" 0x02: GF1 left input = 0x%02x ", snd_cs4231_in(chip, 0x02));
  228. printk(" 0x12: left line in = 0x%02x\n", snd_cs4231_in(chip, 0x12));
  229. printk(" 0x03: GF1 right input = 0x%02x ", snd_cs4231_in(chip, 0x03));
  230. printk(" 0x13: right line in = 0x%02x\n", snd_cs4231_in(chip, 0x13));
  231. printk(" 0x04: CD left input = 0x%02x ", snd_cs4231_in(chip, 0x04));
  232. printk(" 0x14: timer low = 0x%02x\n", snd_cs4231_in(chip, 0x14));
  233. printk(" 0x05: CD right input = 0x%02x ", snd_cs4231_in(chip, 0x05));
  234. printk(" 0x15: timer high = 0x%02x\n", snd_cs4231_in(chip, 0x15));
  235. printk(" 0x06: left output = 0x%02x ", snd_cs4231_in(chip, 0x06));
  236. printk(" 0x16: left MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x16));
  237. printk(" 0x07: right output = 0x%02x ", snd_cs4231_in(chip, 0x07));
  238. printk(" 0x17: right MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x17));
  239. printk(" 0x08: playback format = 0x%02x ", snd_cs4231_in(chip, 0x08));
  240. printk(" 0x18: IRQ status = 0x%02x\n", snd_cs4231_in(chip, 0x18));
  241. printk(" 0x09: iface (CFIG 1) = 0x%02x ", snd_cs4231_in(chip, 0x09));
  242. printk(" 0x19: left line out = 0x%02x\n", snd_cs4231_in(chip, 0x19));
  243. printk(" 0x0a: pin control = 0x%02x ", snd_cs4231_in(chip, 0x0a));
  244. printk(" 0x1a: mono control = 0x%02x\n", snd_cs4231_in(chip, 0x1a));
  245. printk(" 0x0b: init & status = 0x%02x ", snd_cs4231_in(chip, 0x0b));
  246. printk(" 0x1b: right line out = 0x%02x\n", snd_cs4231_in(chip, 0x1b));
  247. printk(" 0x0c: revision & mode = 0x%02x ", snd_cs4231_in(chip, 0x0c));
  248. printk(" 0x1c: record format = 0x%02x\n", snd_cs4231_in(chip, 0x1c));
  249. printk(" 0x0d: loopback = 0x%02x ", snd_cs4231_in(chip, 0x0d));
  250. printk(" 0x1d: var freq (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x1d));
  251. printk(" 0x0e: ply upr count = 0x%02x ", snd_cs4231_in(chip, 0x0e));
  252. printk(" 0x1e: ply lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1e));
  253. printk(" 0x0f: rec upr count = 0x%02x ", snd_cs4231_in(chip, 0x0f));
  254. printk(" 0x1f: rec lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1f));
  255. }
  256. #endif
  257. /*
  258. * CS4231 detection / MCE routines
  259. */
  260. static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
  261. {
  262. int timeout;
  263. /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
  264. for (timeout = 5; timeout > 0; timeout--)
  265. cs4231_inb(chip, CS4231P(REGSEL));
  266. /* end of cleanup sequence */
  267. for (timeout = 250;
  268. timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
  269. timeout--)
  270. udelay(10);
  271. }
  272. void snd_cs4231_mce_up(struct snd_cs4231 *chip)
  273. {
  274. unsigned long flags;
  275. int timeout;
  276. for (timeout = 250; timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT); timeout--)
  277. udelay(100);
  278. #ifdef CONFIG_SND_DEBUG
  279. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  280. snd_printk("mce_up - auto calibration time out (0)\n");
  281. #endif
  282. spin_lock_irqsave(&chip->reg_lock, flags);
  283. chip->mce_bit |= CS4231_MCE;
  284. timeout = cs4231_inb(chip, CS4231P(REGSEL));
  285. if (timeout == 0x80)
  286. snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
  287. if (!(timeout & CS4231_MCE))
  288. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  289. spin_unlock_irqrestore(&chip->reg_lock, flags);
  290. }
  291. void snd_cs4231_mce_down(struct snd_cs4231 *chip)
  292. {
  293. unsigned long flags;
  294. int timeout;
  295. snd_cs4231_busy_wait(chip);
  296. #if 0
  297. printk("(1) timeout = %i\n", timeout);
  298. #endif
  299. #ifdef CONFIG_SND_DEBUG
  300. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  301. snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
  302. #endif
  303. spin_lock_irqsave(&chip->reg_lock, flags);
  304. chip->mce_bit &= ~CS4231_MCE;
  305. timeout = cs4231_inb(chip, CS4231P(REGSEL));
  306. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  307. spin_unlock_irqrestore(&chip->reg_lock, flags);
  308. if (timeout == 0x80)
  309. snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  310. if ((timeout & CS4231_MCE) == 0 ||
  311. !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
  312. return;
  313. }
  314. snd_cs4231_busy_wait(chip);
  315. /* calibration process */
  316. for (timeout = 500; timeout > 0 && (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0; timeout--)
  317. udelay(10);
  318. if ((snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0) {
  319. snd_printd("cs4231_mce_down - auto calibration time out (1)\n");
  320. return;
  321. }
  322. #if 0
  323. printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies);
  324. #endif
  325. /* in 10 ms increments, check condition, up to 250 ms */
  326. timeout = 25;
  327. while (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) {
  328. if (--timeout < 0) {
  329. snd_printk("mce_down - auto calibration time out (2)\n");
  330. return;
  331. }
  332. msleep(10);
  333. }
  334. #if 0
  335. printk("(3) jiffies = %li\n", jiffies);
  336. #endif
  337. /* in 10 ms increments, check condition, up to 100 ms */
  338. timeout = 10;
  339. while (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
  340. if (--timeout < 0) {
  341. snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
  342. return;
  343. }
  344. msleep(10);
  345. }
  346. #if 0
  347. printk("(4) jiffies = %li\n", jiffies);
  348. snd_printk("mce_down - exit = 0x%x\n", cs4231_inb(chip, CS4231P(REGSEL)));
  349. #endif
  350. }
  351. static unsigned int snd_cs4231_get_count(unsigned char format, unsigned int size)
  352. {
  353. switch (format & 0xe0) {
  354. case CS4231_LINEAR_16:
  355. case CS4231_LINEAR_16_BIG:
  356. size >>= 1;
  357. break;
  358. case CS4231_ADPCM_16:
  359. return size >> 2;
  360. }
  361. if (format & CS4231_STEREO)
  362. size >>= 1;
  363. return size;
  364. }
  365. static int snd_cs4231_trigger(struct snd_pcm_substream *substream,
  366. int cmd)
  367. {
  368. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  369. int result = 0;
  370. unsigned int what;
  371. struct list_head *pos;
  372. struct snd_pcm_substream *s;
  373. int do_start;
  374. #if 0
  375. printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, cs4231_inb(chip, CS4231P(STATUS)));
  376. #endif
  377. switch (cmd) {
  378. case SNDRV_PCM_TRIGGER_START:
  379. case SNDRV_PCM_TRIGGER_RESUME:
  380. do_start = 1; break;
  381. case SNDRV_PCM_TRIGGER_STOP:
  382. case SNDRV_PCM_TRIGGER_SUSPEND:
  383. do_start = 0; break;
  384. default:
  385. return -EINVAL;
  386. }
  387. what = 0;
  388. snd_pcm_group_for_each(pos, substream) {
  389. s = snd_pcm_group_substream_entry(pos);
  390. if (s == chip->playback_substream) {
  391. what |= CS4231_PLAYBACK_ENABLE;
  392. snd_pcm_trigger_done(s, substream);
  393. } else if (s == chip->capture_substream) {
  394. what |= CS4231_RECORD_ENABLE;
  395. snd_pcm_trigger_done(s, substream);
  396. }
  397. }
  398. spin_lock(&chip->reg_lock);
  399. if (do_start) {
  400. chip->image[CS4231_IFACE_CTRL] |= what;
  401. if (chip->trigger)
  402. chip->trigger(chip, what, 1);
  403. } else {
  404. chip->image[CS4231_IFACE_CTRL] &= ~what;
  405. if (chip->trigger)
  406. chip->trigger(chip, what, 0);
  407. }
  408. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  409. spin_unlock(&chip->reg_lock);
  410. #if 0
  411. snd_cs4231_debug(chip);
  412. #endif
  413. return result;
  414. }
  415. /*
  416. * CODEC I/O
  417. */
  418. static unsigned char snd_cs4231_get_rate(unsigned int rate)
  419. {
  420. int i;
  421. for (i = 0; i < 14; i++)
  422. if (rate == rates[i])
  423. return freq_bits[i];
  424. // snd_BUG();
  425. return freq_bits[13];
  426. }
  427. static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip,
  428. int format,
  429. int channels)
  430. {
  431. unsigned char rformat;
  432. rformat = CS4231_LINEAR_8;
  433. switch (format) {
  434. case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
  435. case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
  436. case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
  437. case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
  438. case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
  439. }
  440. if (channels > 1)
  441. rformat |= CS4231_STEREO;
  442. #if 0
  443. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  444. #endif
  445. return rformat;
  446. }
  447. static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
  448. {
  449. unsigned long flags;
  450. mute = mute ? 1 : 0;
  451. spin_lock_irqsave(&chip->reg_lock, flags);
  452. if (chip->calibrate_mute == mute) {
  453. spin_unlock_irqrestore(&chip->reg_lock, flags);
  454. return;
  455. }
  456. if (!mute) {
  457. snd_cs4231_dout(chip, CS4231_LEFT_INPUT, chip->image[CS4231_LEFT_INPUT]);
  458. snd_cs4231_dout(chip, CS4231_RIGHT_INPUT, chip->image[CS4231_RIGHT_INPUT]);
  459. snd_cs4231_dout(chip, CS4231_LOOPBACK, chip->image[CS4231_LOOPBACK]);
  460. }
  461. snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
  462. snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
  463. snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
  464. snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
  465. snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
  466. snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
  467. snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN, mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
  468. snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN, mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
  469. snd_cs4231_dout(chip, CS4231_MONO_CTRL, mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
  470. if (chip->hardware == CS4231_HW_INTERWAVE) {
  471. snd_cs4231_dout(chip, CS4231_LEFT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
  472. snd_cs4231_dout(chip, CS4231_RIGHT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
  473. snd_cs4231_dout(chip, CS4231_LINE_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
  474. snd_cs4231_dout(chip, CS4231_LINE_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
  475. }
  476. chip->calibrate_mute = mute;
  477. spin_unlock_irqrestore(&chip->reg_lock, flags);
  478. }
  479. static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
  480. struct snd_pcm_hw_params *params,
  481. unsigned char pdfr)
  482. {
  483. unsigned long flags;
  484. int full_calib = 1;
  485. down(&chip->mce_mutex);
  486. snd_cs4231_calibrate_mute(chip, 1);
  487. if (chip->hardware == CS4231_HW_CS4231A ||
  488. (chip->hardware & CS4231_HW_CS4232_MASK)) {
  489. spin_lock_irqsave(&chip->reg_lock, flags);
  490. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
  491. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10);
  492. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
  493. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
  494. udelay(100); /* Fixes audible clicks at least on GUS MAX */
  495. full_calib = 0;
  496. }
  497. spin_unlock_irqrestore(&chip->reg_lock, flags);
  498. }
  499. if (full_calib) {
  500. snd_cs4231_mce_up(chip);
  501. spin_lock_irqsave(&chip->reg_lock, flags);
  502. if (chip->hardware != CS4231_HW_INTERWAVE && !chip->single_dma) {
  503. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  504. (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
  505. (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
  506. pdfr);
  507. } else {
  508. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
  509. }
  510. spin_unlock_irqrestore(&chip->reg_lock, flags);
  511. snd_cs4231_mce_down(chip);
  512. }
  513. snd_cs4231_calibrate_mute(chip, 0);
  514. up(&chip->mce_mutex);
  515. }
  516. static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
  517. struct snd_pcm_hw_params *params,
  518. unsigned char cdfr)
  519. {
  520. unsigned long flags;
  521. int full_calib = 1;
  522. down(&chip->mce_mutex);
  523. snd_cs4231_calibrate_mute(chip, 1);
  524. if (chip->hardware == CS4231_HW_CS4231A ||
  525. (chip->hardware & CS4231_HW_CS4232_MASK)) {
  526. spin_lock_irqsave(&chip->reg_lock, flags);
  527. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
  528. (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  529. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20);
  530. snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT] = cdfr);
  531. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
  532. full_calib = 0;
  533. }
  534. spin_unlock_irqrestore(&chip->reg_lock, flags);
  535. }
  536. if (full_calib) {
  537. snd_cs4231_mce_up(chip);
  538. spin_lock_irqsave(&chip->reg_lock, flags);
  539. if (chip->hardware != CS4231_HW_INTERWAVE) {
  540. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  541. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  542. ((chip->single_dma ? cdfr : chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
  543. (cdfr & 0x0f));
  544. spin_unlock_irqrestore(&chip->reg_lock, flags);
  545. snd_cs4231_mce_down(chip);
  546. snd_cs4231_mce_up(chip);
  547. spin_lock_irqsave(&chip->reg_lock, flags);
  548. }
  549. }
  550. snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
  551. spin_unlock_irqrestore(&chip->reg_lock, flags);
  552. snd_cs4231_mce_down(chip);
  553. }
  554. snd_cs4231_calibrate_mute(chip, 0);
  555. up(&chip->mce_mutex);
  556. }
  557. /*
  558. * Timer interface
  559. */
  560. static unsigned long snd_cs4231_timer_resolution(struct snd_timer * timer)
  561. {
  562. struct snd_cs4231 *chip = snd_timer_chip(timer);
  563. if (chip->hardware & CS4231_HW_CS4236B_MASK)
  564. return 14467;
  565. else
  566. return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
  567. }
  568. static int snd_cs4231_timer_start(struct snd_timer * timer)
  569. {
  570. unsigned long flags;
  571. unsigned int ticks;
  572. struct snd_cs4231 *chip = snd_timer_chip(timer);
  573. spin_lock_irqsave(&chip->reg_lock, flags);
  574. ticks = timer->sticks;
  575. if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
  576. (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
  577. (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
  578. snd_cs4231_out(chip, CS4231_TIMER_HIGH, chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8));
  579. snd_cs4231_out(chip, CS4231_TIMER_LOW, chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks);
  580. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
  581. }
  582. spin_unlock_irqrestore(&chip->reg_lock, flags);
  583. return 0;
  584. }
  585. static int snd_cs4231_timer_stop(struct snd_timer * timer)
  586. {
  587. unsigned long flags;
  588. struct snd_cs4231 *chip = snd_timer_chip(timer);
  589. spin_lock_irqsave(&chip->reg_lock, flags);
  590. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
  591. spin_unlock_irqrestore(&chip->reg_lock, flags);
  592. return 0;
  593. }
  594. static void snd_cs4231_init(struct snd_cs4231 *chip)
  595. {
  596. unsigned long flags;
  597. snd_cs4231_mce_down(chip);
  598. #ifdef SNDRV_DEBUG_MCE
  599. snd_printk("init: (1)\n");
  600. #endif
  601. snd_cs4231_mce_up(chip);
  602. spin_lock_irqsave(&chip->reg_lock, flags);
  603. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  604. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
  605. CS4231_CALIB_MODE);
  606. chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
  607. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  608. spin_unlock_irqrestore(&chip->reg_lock, flags);
  609. snd_cs4231_mce_down(chip);
  610. #ifdef SNDRV_DEBUG_MCE
  611. snd_printk("init: (2)\n");
  612. #endif
  613. snd_cs4231_mce_up(chip);
  614. spin_lock_irqsave(&chip->reg_lock, flags);
  615. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
  616. spin_unlock_irqrestore(&chip->reg_lock, flags);
  617. snd_cs4231_mce_down(chip);
  618. #ifdef SNDRV_DEBUG_MCE
  619. snd_printk("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
  620. #endif
  621. spin_lock_irqsave(&chip->reg_lock, flags);
  622. snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
  623. spin_unlock_irqrestore(&chip->reg_lock, flags);
  624. snd_cs4231_mce_up(chip);
  625. spin_lock_irqsave(&chip->reg_lock, flags);
  626. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
  627. spin_unlock_irqrestore(&chip->reg_lock, flags);
  628. snd_cs4231_mce_down(chip);
  629. #ifdef SNDRV_DEBUG_MCE
  630. snd_printk("init: (4)\n");
  631. #endif
  632. snd_cs4231_mce_up(chip);
  633. spin_lock_irqsave(&chip->reg_lock, flags);
  634. snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
  635. spin_unlock_irqrestore(&chip->reg_lock, flags);
  636. snd_cs4231_mce_down(chip);
  637. #ifdef SNDRV_DEBUG_MCE
  638. snd_printk("init: (5)\n");
  639. #endif
  640. }
  641. static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
  642. {
  643. unsigned long flags;
  644. down(&chip->open_mutex);
  645. if ((chip->mode & mode) ||
  646. ((chip->mode & CS4231_MODE_OPEN) && chip->single_dma)) {
  647. up(&chip->open_mutex);
  648. return -EAGAIN;
  649. }
  650. if (chip->mode & CS4231_MODE_OPEN) {
  651. chip->mode |= mode;
  652. up(&chip->open_mutex);
  653. return 0;
  654. }
  655. /* ok. now enable and ack CODEC IRQ */
  656. spin_lock_irqsave(&chip->reg_lock, flags);
  657. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  658. CS4231_RECORD_IRQ |
  659. CS4231_TIMER_IRQ);
  660. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  661. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  662. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  663. chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
  664. snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  665. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  666. CS4231_RECORD_IRQ |
  667. CS4231_TIMER_IRQ);
  668. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  669. spin_unlock_irqrestore(&chip->reg_lock, flags);
  670. chip->mode = mode;
  671. up(&chip->open_mutex);
  672. return 0;
  673. }
  674. static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
  675. {
  676. unsigned long flags;
  677. down(&chip->open_mutex);
  678. chip->mode &= ~mode;
  679. if (chip->mode & CS4231_MODE_OPEN) {
  680. up(&chip->open_mutex);
  681. return;
  682. }
  683. snd_cs4231_calibrate_mute(chip, 1);
  684. /* disable IRQ */
  685. spin_lock_irqsave(&chip->reg_lock, flags);
  686. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  687. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  688. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  689. chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
  690. snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  691. /* now disable record & playback */
  692. if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  693. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
  694. spin_unlock_irqrestore(&chip->reg_lock, flags);
  695. snd_cs4231_mce_up(chip);
  696. spin_lock_irqsave(&chip->reg_lock, flags);
  697. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  698. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  699. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  700. spin_unlock_irqrestore(&chip->reg_lock, flags);
  701. snd_cs4231_mce_down(chip);
  702. spin_lock_irqsave(&chip->reg_lock, flags);
  703. }
  704. /* clear IRQ again */
  705. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  706. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  707. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  708. spin_unlock_irqrestore(&chip->reg_lock, flags);
  709. snd_cs4231_calibrate_mute(chip, 0);
  710. chip->mode = 0;
  711. up(&chip->open_mutex);
  712. }
  713. /*
  714. * timer open/close
  715. */
  716. static int snd_cs4231_timer_open(struct snd_timer * timer)
  717. {
  718. struct snd_cs4231 *chip = snd_timer_chip(timer);
  719. snd_cs4231_open(chip, CS4231_MODE_TIMER);
  720. return 0;
  721. }
  722. static int snd_cs4231_timer_close(struct snd_timer * timer)
  723. {
  724. struct snd_cs4231 *chip = snd_timer_chip(timer);
  725. snd_cs4231_close(chip, CS4231_MODE_TIMER);
  726. return 0;
  727. }
  728. static struct snd_timer_hardware snd_cs4231_timer_table =
  729. {
  730. .flags = SNDRV_TIMER_HW_AUTO,
  731. .resolution = 9945,
  732. .ticks = 65535,
  733. .open = snd_cs4231_timer_open,
  734. .close = snd_cs4231_timer_close,
  735. .c_resolution = snd_cs4231_timer_resolution,
  736. .start = snd_cs4231_timer_start,
  737. .stop = snd_cs4231_timer_stop,
  738. };
  739. /*
  740. * ok.. exported functions..
  741. */
  742. static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
  743. struct snd_pcm_hw_params *hw_params)
  744. {
  745. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  746. unsigned char new_pdfr;
  747. int err;
  748. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  749. return err;
  750. new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
  751. snd_cs4231_get_rate(params_rate(hw_params));
  752. chip->set_playback_format(chip, hw_params, new_pdfr);
  753. return 0;
  754. }
  755. static int snd_cs4231_playback_hw_free(struct snd_pcm_substream *substream)
  756. {
  757. return snd_pcm_lib_free_pages(substream);
  758. }
  759. static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
  760. {
  761. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  762. struct snd_pcm_runtime *runtime = substream->runtime;
  763. unsigned long flags;
  764. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  765. unsigned int count = snd_pcm_lib_period_bytes(substream);
  766. spin_lock_irqsave(&chip->reg_lock, flags);
  767. chip->p_dma_size = size;
  768. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
  769. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  770. count = snd_cs4231_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
  771. snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  772. snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  773. spin_unlock_irqrestore(&chip->reg_lock, flags);
  774. #if 0
  775. snd_cs4231_debug(chip);
  776. #endif
  777. return 0;
  778. }
  779. static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
  780. struct snd_pcm_hw_params *hw_params)
  781. {
  782. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  783. unsigned char new_cdfr;
  784. int err;
  785. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  786. return err;
  787. new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
  788. snd_cs4231_get_rate(params_rate(hw_params));
  789. chip->set_capture_format(chip, hw_params, new_cdfr);
  790. return 0;
  791. }
  792. static int snd_cs4231_capture_hw_free(struct snd_pcm_substream *substream)
  793. {
  794. return snd_pcm_lib_free_pages(substream);
  795. }
  796. static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
  797. {
  798. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  799. struct snd_pcm_runtime *runtime = substream->runtime;
  800. unsigned long flags;
  801. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  802. unsigned int count = snd_pcm_lib_period_bytes(substream);
  803. spin_lock_irqsave(&chip->reg_lock, flags);
  804. chip->c_dma_size = size;
  805. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  806. snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  807. count = snd_cs4231_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
  808. if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
  809. snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  810. snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  811. } else {
  812. snd_cs4231_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
  813. snd_cs4231_out(chip, CS4231_REC_UPR_CNT, (unsigned char) (count >> 8));
  814. }
  815. spin_unlock_irqrestore(&chip->reg_lock, flags);
  816. return 0;
  817. }
  818. static void snd_cs4231_overrange(struct snd_cs4231 *chip)
  819. {
  820. unsigned long flags;
  821. unsigned char res;
  822. spin_lock_irqsave(&chip->reg_lock, flags);
  823. res = snd_cs4231_in(chip, CS4231_TEST_INIT);
  824. spin_unlock_irqrestore(&chip->reg_lock, flags);
  825. if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
  826. chip->capture_substream->runtime->overrange++;
  827. }
  828. irqreturn_t snd_cs4231_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  829. {
  830. struct snd_cs4231 *chip = dev_id;
  831. unsigned char status;
  832. status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
  833. if (status & CS4231_TIMER_IRQ) {
  834. if (chip->timer)
  835. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  836. }
  837. if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
  838. if (status & CS4231_PLAYBACK_IRQ) {
  839. if (chip->mode & CS4231_MODE_PLAY) {
  840. if (chip->playback_substream)
  841. snd_pcm_period_elapsed(chip->playback_substream);
  842. }
  843. if (chip->mode & CS4231_MODE_RECORD) {
  844. if (chip->capture_substream) {
  845. snd_cs4231_overrange(chip);
  846. snd_pcm_period_elapsed(chip->capture_substream);
  847. }
  848. }
  849. }
  850. } else {
  851. if (status & CS4231_PLAYBACK_IRQ) {
  852. if (chip->playback_substream)
  853. snd_pcm_period_elapsed(chip->playback_substream);
  854. }
  855. if (status & CS4231_RECORD_IRQ) {
  856. if (chip->capture_substream) {
  857. snd_cs4231_overrange(chip);
  858. snd_pcm_period_elapsed(chip->capture_substream);
  859. }
  860. }
  861. }
  862. spin_lock(&chip->reg_lock);
  863. snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
  864. spin_unlock(&chip->reg_lock);
  865. return IRQ_HANDLED;
  866. }
  867. static snd_pcm_uframes_t snd_cs4231_playback_pointer(struct snd_pcm_substream *substream)
  868. {
  869. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  870. size_t ptr;
  871. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
  872. return 0;
  873. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  874. return bytes_to_frames(substream->runtime, ptr);
  875. }
  876. static snd_pcm_uframes_t snd_cs4231_capture_pointer(struct snd_pcm_substream *substream)
  877. {
  878. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  879. size_t ptr;
  880. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
  881. return 0;
  882. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  883. return bytes_to_frames(substream->runtime, ptr);
  884. }
  885. /*
  886. */
  887. static int snd_cs4231_probe(struct snd_cs4231 *chip)
  888. {
  889. unsigned long flags;
  890. int i, id, rev;
  891. unsigned char *ptr;
  892. unsigned int hw;
  893. #if 0
  894. snd_cs4231_debug(chip);
  895. #endif
  896. id = 0;
  897. for (i = 0; i < 50; i++) {
  898. mb();
  899. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  900. udelay(2000);
  901. else {
  902. spin_lock_irqsave(&chip->reg_lock, flags);
  903. snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
  904. id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
  905. spin_unlock_irqrestore(&chip->reg_lock, flags);
  906. if (id == 0x0a)
  907. break; /* this is valid value */
  908. }
  909. }
  910. snd_printdd("cs4231: port = 0x%lx, id = 0x%x\n", chip->port, id);
  911. if (id != 0x0a)
  912. return -ENODEV; /* no valid device found */
  913. if (((hw = chip->hardware) & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
  914. rev = snd_cs4231_in(chip, CS4231_VERSION) & 0xe7;
  915. snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
  916. if (rev == 0x80) {
  917. unsigned char tmp = snd_cs4231_in(chip, 23);
  918. snd_cs4231_out(chip, 23, ~tmp);
  919. if (snd_cs4231_in(chip, 23) != tmp)
  920. chip->hardware = CS4231_HW_AD1845;
  921. else
  922. chip->hardware = CS4231_HW_CS4231;
  923. } else if (rev == 0xa0) {
  924. chip->hardware = CS4231_HW_CS4231A;
  925. } else if (rev == 0xa2) {
  926. chip->hardware = CS4231_HW_CS4232;
  927. } else if (rev == 0xb2) {
  928. chip->hardware = CS4231_HW_CS4232A;
  929. } else if (rev == 0x83) {
  930. chip->hardware = CS4231_HW_CS4236;
  931. } else if (rev == 0x03) {
  932. chip->hardware = CS4231_HW_CS4236B;
  933. } else {
  934. snd_printk("unknown CS chip with version 0x%x\n", rev);
  935. return -ENODEV; /* unknown CS4231 chip? */
  936. }
  937. }
  938. spin_lock_irqsave(&chip->reg_lock, flags);
  939. cs4231_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
  940. cs4231_outb(chip, CS4231P(STATUS), 0);
  941. mb();
  942. spin_unlock_irqrestore(&chip->reg_lock, flags);
  943. chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
  944. switch (chip->hardware) {
  945. case CS4231_HW_INTERWAVE:
  946. chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
  947. break;
  948. case CS4231_HW_CS4235:
  949. case CS4231_HW_CS4236B:
  950. case CS4231_HW_CS4237B:
  951. case CS4231_HW_CS4238B:
  952. case CS4231_HW_CS4239:
  953. if (hw == CS4231_HW_DETECT3)
  954. chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
  955. else
  956. chip->hardware = CS4231_HW_CS4236;
  957. break;
  958. }
  959. chip->image[CS4231_IFACE_CTRL] =
  960. (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
  961. (chip->single_dma ? CS4231_SINGLE_DMA : 0);
  962. chip->image[CS4231_ALT_FEATURE_1] = 0x80;
  963. chip->image[CS4231_ALT_FEATURE_2] = chip->hardware == CS4231_HW_INTERWAVE ? 0xc2 : 0x01;
  964. ptr = (unsigned char *) &chip->image;
  965. snd_cs4231_mce_down(chip);
  966. spin_lock_irqsave(&chip->reg_lock, flags);
  967. for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
  968. snd_cs4231_out(chip, i, *ptr++);
  969. spin_unlock_irqrestore(&chip->reg_lock, flags);
  970. snd_cs4231_mce_up(chip);
  971. snd_cs4231_mce_down(chip);
  972. mdelay(2);
  973. /* ok.. try check hardware version for CS4236+ chips */
  974. if ((hw & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
  975. if (chip->hardware == CS4231_HW_CS4236B) {
  976. rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
  977. snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
  978. id = snd_cs4236_ext_in(chip, CS4236_VERSION);
  979. snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
  980. snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
  981. if ((id & 0x1f) == 0x1d) { /* CS4235 */
  982. chip->hardware = CS4231_HW_CS4235;
  983. switch (id >> 5) {
  984. case 4:
  985. case 5:
  986. case 6:
  987. break;
  988. default:
  989. snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
  990. }
  991. } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
  992. switch (id >> 5) {
  993. case 4:
  994. case 5:
  995. case 6:
  996. case 7:
  997. chip->hardware = CS4231_HW_CS4236B;
  998. break;
  999. default:
  1000. snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
  1001. }
  1002. } else if ((id & 0x1f) == 0x08) { /* CS4237B */
  1003. chip->hardware = CS4231_HW_CS4237B;
  1004. switch (id >> 5) {
  1005. case 4:
  1006. case 5:
  1007. case 6:
  1008. case 7:
  1009. break;
  1010. default:
  1011. snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
  1012. }
  1013. } else if ((id & 0x1f) == 0x09) { /* CS4238B */
  1014. chip->hardware = CS4231_HW_CS4238B;
  1015. switch (id >> 5) {
  1016. case 5:
  1017. case 6:
  1018. case 7:
  1019. break;
  1020. default:
  1021. snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
  1022. }
  1023. } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
  1024. chip->hardware = CS4231_HW_CS4239;
  1025. switch (id >> 5) {
  1026. case 4:
  1027. case 5:
  1028. case 6:
  1029. break;
  1030. default:
  1031. snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
  1032. }
  1033. } else {
  1034. snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
  1035. }
  1036. }
  1037. }
  1038. return 0; /* all things are ok.. */
  1039. }
  1040. /*
  1041. */
  1042. static struct snd_pcm_hardware snd_cs4231_playback =
  1043. {
  1044. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1045. SNDRV_PCM_INFO_MMAP_VALID |
  1046. SNDRV_PCM_INFO_RESUME |
  1047. SNDRV_PCM_INFO_SYNC_START),
  1048. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1049. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1050. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1051. .rate_min = 5510,
  1052. .rate_max = 48000,
  1053. .channels_min = 1,
  1054. .channels_max = 2,
  1055. .buffer_bytes_max = (128*1024),
  1056. .period_bytes_min = 64,
  1057. .period_bytes_max = (128*1024),
  1058. .periods_min = 1,
  1059. .periods_max = 1024,
  1060. .fifo_size = 0,
  1061. };
  1062. static struct snd_pcm_hardware snd_cs4231_capture =
  1063. {
  1064. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1065. SNDRV_PCM_INFO_MMAP_VALID |
  1066. SNDRV_PCM_INFO_RESUME |
  1067. SNDRV_PCM_INFO_SYNC_START),
  1068. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1069. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1070. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1071. .rate_min = 5510,
  1072. .rate_max = 48000,
  1073. .channels_min = 1,
  1074. .channels_max = 2,
  1075. .buffer_bytes_max = (128*1024),
  1076. .period_bytes_min = 64,
  1077. .period_bytes_max = (128*1024),
  1078. .periods_min = 1,
  1079. .periods_max = 1024,
  1080. .fifo_size = 0,
  1081. };
  1082. /*
  1083. */
  1084. static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
  1085. {
  1086. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1087. struct snd_pcm_runtime *runtime = substream->runtime;
  1088. int err;
  1089. runtime->hw = snd_cs4231_playback;
  1090. /* hardware bug in InterWave chipset */
  1091. if (chip->hardware == CS4231_HW_INTERWAVE && chip->dma1 > 3)
  1092. runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
  1093. /* hardware limitation of cheap chips */
  1094. if (chip->hardware == CS4231_HW_CS4235 ||
  1095. chip->hardware == CS4231_HW_CS4239)
  1096. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1097. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  1098. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  1099. if (chip->claim_dma) {
  1100. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
  1101. return err;
  1102. }
  1103. if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
  1104. if (chip->release_dma)
  1105. chip->release_dma(chip, chip->dma_private_data, chip->dma1);
  1106. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1107. return err;
  1108. }
  1109. chip->playback_substream = substream;
  1110. snd_pcm_set_sync(substream);
  1111. chip->rate_constraint(runtime);
  1112. return 0;
  1113. }
  1114. static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
  1115. {
  1116. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1117. struct snd_pcm_runtime *runtime = substream->runtime;
  1118. int err;
  1119. runtime->hw = snd_cs4231_capture;
  1120. /* hardware limitation of cheap chips */
  1121. if (chip->hardware == CS4231_HW_CS4235 ||
  1122. chip->hardware == CS4231_HW_CS4239)
  1123. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1124. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  1125. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  1126. if (chip->claim_dma) {
  1127. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
  1128. return err;
  1129. }
  1130. if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
  1131. if (chip->release_dma)
  1132. chip->release_dma(chip, chip->dma_private_data, chip->dma2);
  1133. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1134. return err;
  1135. }
  1136. chip->capture_substream = substream;
  1137. snd_pcm_set_sync(substream);
  1138. chip->rate_constraint(runtime);
  1139. return 0;
  1140. }
  1141. static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
  1142. {
  1143. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1144. chip->playback_substream = NULL;
  1145. snd_cs4231_close(chip, CS4231_MODE_PLAY);
  1146. return 0;
  1147. }
  1148. static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
  1149. {
  1150. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1151. chip->capture_substream = NULL;
  1152. snd_cs4231_close(chip, CS4231_MODE_RECORD);
  1153. return 0;
  1154. }
  1155. #ifdef CONFIG_PM
  1156. /* lowlevel suspend callback for CS4231 */
  1157. static void snd_cs4231_suspend(struct snd_cs4231 *chip)
  1158. {
  1159. int reg;
  1160. unsigned long flags;
  1161. snd_pcm_suspend_all(chip->pcm);
  1162. spin_lock_irqsave(&chip->reg_lock, flags);
  1163. for (reg = 0; reg < 32; reg++)
  1164. chip->image[reg] = snd_cs4231_in(chip, reg);
  1165. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1166. }
  1167. /* lowlevel resume callback for CS4231 */
  1168. static void snd_cs4231_resume(struct snd_cs4231 *chip)
  1169. {
  1170. int reg;
  1171. unsigned long flags;
  1172. /* int timeout; */
  1173. snd_cs4231_mce_up(chip);
  1174. spin_lock_irqsave(&chip->reg_lock, flags);
  1175. for (reg = 0; reg < 32; reg++) {
  1176. switch (reg) {
  1177. case CS4231_VERSION:
  1178. break;
  1179. default:
  1180. snd_cs4231_out(chip, reg, chip->image[reg]);
  1181. break;
  1182. }
  1183. }
  1184. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1185. #if 1
  1186. snd_cs4231_mce_down(chip);
  1187. #else
  1188. /* The following is a workaround to avoid freeze after resume on TP600E.
  1189. This is the first half of copy of snd_cs4231_mce_down(), but doesn't
  1190. include rescheduling. -- iwai
  1191. */
  1192. snd_cs4231_busy_wait(chip);
  1193. spin_lock_irqsave(&chip->reg_lock, flags);
  1194. chip->mce_bit &= ~CS4231_MCE;
  1195. timeout = cs4231_inb(chip, CS4231P(REGSEL));
  1196. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  1197. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1198. if (timeout == 0x80)
  1199. snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  1200. if ((timeout & CS4231_MCE) == 0 ||
  1201. !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
  1202. return;
  1203. }
  1204. snd_cs4231_busy_wait(chip);
  1205. #endif
  1206. }
  1207. #endif /* CONFIG_PM */
  1208. static int snd_cs4231_free(struct snd_cs4231 *chip)
  1209. {
  1210. release_and_free_resource(chip->res_port);
  1211. release_and_free_resource(chip->res_cport);
  1212. if (chip->irq >= 0) {
  1213. disable_irq(chip->irq);
  1214. if (!(chip->hwshare & CS4231_HWSHARE_IRQ))
  1215. free_irq(chip->irq, (void *) chip);
  1216. }
  1217. if (!(chip->hwshare & CS4231_HWSHARE_DMA1) && chip->dma1 >= 0) {
  1218. snd_dma_disable(chip->dma1);
  1219. free_dma(chip->dma1);
  1220. }
  1221. if (!(chip->hwshare & CS4231_HWSHARE_DMA2) && chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
  1222. snd_dma_disable(chip->dma2);
  1223. free_dma(chip->dma2);
  1224. }
  1225. if (chip->timer)
  1226. snd_device_free(chip->card, chip->timer);
  1227. kfree(chip);
  1228. return 0;
  1229. }
  1230. static int snd_cs4231_dev_free(struct snd_device *device)
  1231. {
  1232. struct snd_cs4231 *chip = device->device_data;
  1233. return snd_cs4231_free(chip);
  1234. }
  1235. const char *snd_cs4231_chip_id(struct snd_cs4231 *chip)
  1236. {
  1237. switch (chip->hardware) {
  1238. case CS4231_HW_CS4231: return "CS4231";
  1239. case CS4231_HW_CS4231A: return "CS4231A";
  1240. case CS4231_HW_CS4232: return "CS4232";
  1241. case CS4231_HW_CS4232A: return "CS4232A";
  1242. case CS4231_HW_CS4235: return "CS4235";
  1243. case CS4231_HW_CS4236: return "CS4236";
  1244. case CS4231_HW_CS4236B: return "CS4236B";
  1245. case CS4231_HW_CS4237B: return "CS4237B";
  1246. case CS4231_HW_CS4238B: return "CS4238B";
  1247. case CS4231_HW_CS4239: return "CS4239";
  1248. case CS4231_HW_INTERWAVE: return "AMD InterWave";
  1249. case CS4231_HW_OPL3SA2: return chip->card->shortname;
  1250. case CS4231_HW_AD1845: return "AD1845";
  1251. default: return "???";
  1252. }
  1253. }
  1254. static int snd_cs4231_new(struct snd_card *card,
  1255. unsigned short hardware,
  1256. unsigned short hwshare,
  1257. struct snd_cs4231 ** rchip)
  1258. {
  1259. struct snd_cs4231 *chip;
  1260. *rchip = NULL;
  1261. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1262. if (chip == NULL)
  1263. return -ENOMEM;
  1264. chip->hardware = hardware;
  1265. chip->hwshare = hwshare;
  1266. spin_lock_init(&chip->reg_lock);
  1267. init_MUTEX(&chip->mce_mutex);
  1268. init_MUTEX(&chip->open_mutex);
  1269. chip->card = card;
  1270. chip->rate_constraint = snd_cs4231_xrate;
  1271. chip->set_playback_format = snd_cs4231_playback_format;
  1272. chip->set_capture_format = snd_cs4231_capture_format;
  1273. memcpy(&chip->image, &snd_cs4231_original_image, sizeof(snd_cs4231_original_image));
  1274. *rchip = chip;
  1275. return 0;
  1276. }
  1277. int snd_cs4231_create(struct snd_card *card,
  1278. unsigned long port,
  1279. unsigned long cport,
  1280. int irq, int dma1, int dma2,
  1281. unsigned short hardware,
  1282. unsigned short hwshare,
  1283. struct snd_cs4231 ** rchip)
  1284. {
  1285. static struct snd_device_ops ops = {
  1286. .dev_free = snd_cs4231_dev_free,
  1287. };
  1288. struct snd_cs4231 *chip;
  1289. int err;
  1290. err = snd_cs4231_new(card, hardware, hwshare, &chip);
  1291. if (err < 0)
  1292. return err;
  1293. chip->irq = -1;
  1294. chip->dma1 = -1;
  1295. chip->dma2 = -1;
  1296. if ((chip->res_port = request_region(port, 4, "CS4231")) == NULL) {
  1297. snd_printk(KERN_ERR "cs4231: can't grab port 0x%lx\n", port);
  1298. snd_cs4231_free(chip);
  1299. return -EBUSY;
  1300. }
  1301. chip->port = port;
  1302. if ((long)cport >= 0 && (chip->res_cport = request_region(cport, 8, "CS4232 Control")) == NULL) {
  1303. snd_printk(KERN_ERR "cs4231: can't grab control port 0x%lx\n", cport);
  1304. snd_cs4231_free(chip);
  1305. return -ENODEV;
  1306. }
  1307. chip->cport = cport;
  1308. if (!(hwshare & CS4231_HWSHARE_IRQ) && request_irq(irq, snd_cs4231_interrupt, SA_INTERRUPT, "CS4231", (void *) chip)) {
  1309. snd_printk(KERN_ERR "cs4231: can't grab IRQ %d\n", irq);
  1310. snd_cs4231_free(chip);
  1311. return -EBUSY;
  1312. }
  1313. chip->irq = irq;
  1314. if (!(hwshare & CS4231_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
  1315. snd_printk(KERN_ERR "cs4231: can't grab DMA1 %d\n", dma1);
  1316. snd_cs4231_free(chip);
  1317. return -EBUSY;
  1318. }
  1319. chip->dma1 = dma1;
  1320. if (!(hwshare & CS4231_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
  1321. snd_printk(KERN_ERR "cs4231: can't grab DMA2 %d\n", dma2);
  1322. snd_cs4231_free(chip);
  1323. return -EBUSY;
  1324. }
  1325. if (dma1 == dma2 || dma2 < 0) {
  1326. chip->single_dma = 1;
  1327. chip->dma2 = chip->dma1;
  1328. } else
  1329. chip->dma2 = dma2;
  1330. /* global setup */
  1331. if (snd_cs4231_probe(chip) < 0) {
  1332. snd_cs4231_free(chip);
  1333. return -ENODEV;
  1334. }
  1335. snd_cs4231_init(chip);
  1336. #if 0
  1337. if (chip->hardware & CS4231_HW_CS4232_MASK) {
  1338. if (chip->res_cport == NULL)
  1339. snd_printk("CS4232 control port features are not accessible\n");
  1340. }
  1341. #endif
  1342. /* Register device */
  1343. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1344. snd_cs4231_free(chip);
  1345. return err;
  1346. }
  1347. #ifdef CONFIG_PM
  1348. /* Power Management */
  1349. chip->suspend = snd_cs4231_suspend;
  1350. chip->resume = snd_cs4231_resume;
  1351. #endif
  1352. *rchip = chip;
  1353. return 0;
  1354. }
  1355. static struct snd_pcm_ops snd_cs4231_playback_ops = {
  1356. .open = snd_cs4231_playback_open,
  1357. .close = snd_cs4231_playback_close,
  1358. .ioctl = snd_pcm_lib_ioctl,
  1359. .hw_params = snd_cs4231_playback_hw_params,
  1360. .hw_free = snd_cs4231_playback_hw_free,
  1361. .prepare = snd_cs4231_playback_prepare,
  1362. .trigger = snd_cs4231_trigger,
  1363. .pointer = snd_cs4231_playback_pointer,
  1364. };
  1365. static struct snd_pcm_ops snd_cs4231_capture_ops = {
  1366. .open = snd_cs4231_capture_open,
  1367. .close = snd_cs4231_capture_close,
  1368. .ioctl = snd_pcm_lib_ioctl,
  1369. .hw_params = snd_cs4231_capture_hw_params,
  1370. .hw_free = snd_cs4231_capture_hw_free,
  1371. .prepare = snd_cs4231_capture_prepare,
  1372. .trigger = snd_cs4231_trigger,
  1373. .pointer = snd_cs4231_capture_pointer,
  1374. };
  1375. int snd_cs4231_pcm(struct snd_cs4231 *chip, int device, struct snd_pcm **rpcm)
  1376. {
  1377. struct snd_pcm *pcm;
  1378. int err;
  1379. if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
  1380. return err;
  1381. spin_lock_init(&chip->reg_lock);
  1382. init_MUTEX(&chip->mce_mutex);
  1383. init_MUTEX(&chip->open_mutex);
  1384. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
  1385. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
  1386. /* global setup */
  1387. pcm->private_data = chip;
  1388. pcm->info_flags = 0;
  1389. if (chip->single_dma)
  1390. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1391. if (chip->hardware != CS4231_HW_INTERWAVE)
  1392. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1393. strcpy(pcm->name, snd_cs4231_chip_id(chip));
  1394. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1395. snd_dma_isa_data(),
  1396. 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1397. chip->pcm = pcm;
  1398. if (rpcm)
  1399. *rpcm = pcm;
  1400. return 0;
  1401. }
  1402. static void snd_cs4231_timer_free(struct snd_timer *timer)
  1403. {
  1404. struct snd_cs4231 *chip = timer->private_data;
  1405. chip->timer = NULL;
  1406. }
  1407. int snd_cs4231_timer(struct snd_cs4231 *chip, int device, struct snd_timer **rtimer)
  1408. {
  1409. struct snd_timer *timer;
  1410. struct snd_timer_id tid;
  1411. int err;
  1412. /* Timer initialization */
  1413. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1414. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1415. tid.card = chip->card->number;
  1416. tid.device = device;
  1417. tid.subdevice = 0;
  1418. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1419. return err;
  1420. strcpy(timer->name, snd_cs4231_chip_id(chip));
  1421. timer->private_data = chip;
  1422. timer->private_free = snd_cs4231_timer_free;
  1423. timer->hw = snd_cs4231_timer_table;
  1424. chip->timer = timer;
  1425. if (rtimer)
  1426. *rtimer = timer;
  1427. return 0;
  1428. }
  1429. /*
  1430. * MIXER part
  1431. */
  1432. static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1433. {
  1434. static char *texts[4] = {
  1435. "Line", "Aux", "Mic", "Mix"
  1436. };
  1437. static char *opl3sa_texts[4] = {
  1438. "Line", "CD", "Mic", "Mix"
  1439. };
  1440. static char *gusmax_texts[4] = {
  1441. "Line", "Synth", "Mic", "Mix"
  1442. };
  1443. char **ptexts = texts;
  1444. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1445. snd_assert(chip->card != NULL, return -EINVAL);
  1446. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1447. uinfo->count = 2;
  1448. uinfo->value.enumerated.items = 4;
  1449. if (uinfo->value.enumerated.item > 3)
  1450. uinfo->value.enumerated.item = 3;
  1451. if (!strcmp(chip->card->driver, "GUS MAX"))
  1452. ptexts = gusmax_texts;
  1453. switch (chip->hardware) {
  1454. case CS4231_HW_INTERWAVE: ptexts = gusmax_texts; break;
  1455. case CS4231_HW_OPL3SA2: ptexts = opl3sa_texts; break;
  1456. }
  1457. strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
  1458. return 0;
  1459. }
  1460. static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1461. {
  1462. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1463. unsigned long flags;
  1464. spin_lock_irqsave(&chip->reg_lock, flags);
  1465. ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1466. ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1467. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1468. return 0;
  1469. }
  1470. static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1471. {
  1472. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1473. unsigned long flags;
  1474. unsigned short left, right;
  1475. int change;
  1476. if (ucontrol->value.enumerated.item[0] > 3 ||
  1477. ucontrol->value.enumerated.item[1] > 3)
  1478. return -EINVAL;
  1479. left = ucontrol->value.enumerated.item[0] << 6;
  1480. right = ucontrol->value.enumerated.item[1] << 6;
  1481. spin_lock_irqsave(&chip->reg_lock, flags);
  1482. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1483. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1484. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1485. right != chip->image[CS4231_RIGHT_INPUT];
  1486. snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
  1487. snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
  1488. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1489. return change;
  1490. }
  1491. int snd_cs4231_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1492. {
  1493. int mask = (kcontrol->private_value >> 16) & 0xff;
  1494. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1495. uinfo->count = 1;
  1496. uinfo->value.integer.min = 0;
  1497. uinfo->value.integer.max = mask;
  1498. return 0;
  1499. }
  1500. int snd_cs4231_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1501. {
  1502. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1503. unsigned long flags;
  1504. int reg = kcontrol->private_value & 0xff;
  1505. int shift = (kcontrol->private_value >> 8) & 0xff;
  1506. int mask = (kcontrol->private_value >> 16) & 0xff;
  1507. int invert = (kcontrol->private_value >> 24) & 0xff;
  1508. spin_lock_irqsave(&chip->reg_lock, flags);
  1509. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1510. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1511. if (invert)
  1512. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1513. return 0;
  1514. }
  1515. int snd_cs4231_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1516. {
  1517. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1518. unsigned long flags;
  1519. int reg = kcontrol->private_value & 0xff;
  1520. int shift = (kcontrol->private_value >> 8) & 0xff;
  1521. int mask = (kcontrol->private_value >> 16) & 0xff;
  1522. int invert = (kcontrol->private_value >> 24) & 0xff;
  1523. int change;
  1524. unsigned short val;
  1525. val = (ucontrol->value.integer.value[0] & mask);
  1526. if (invert)
  1527. val = mask - val;
  1528. val <<= shift;
  1529. spin_lock_irqsave(&chip->reg_lock, flags);
  1530. val = (chip->image[reg] & ~(mask << shift)) | val;
  1531. change = val != chip->image[reg];
  1532. snd_cs4231_out(chip, reg, val);
  1533. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1534. return change;
  1535. }
  1536. int snd_cs4231_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1537. {
  1538. int mask = (kcontrol->private_value >> 24) & 0xff;
  1539. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1540. uinfo->count = 2;
  1541. uinfo->value.integer.min = 0;
  1542. uinfo->value.integer.max = mask;
  1543. return 0;
  1544. }
  1545. int snd_cs4231_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1546. {
  1547. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1548. unsigned long flags;
  1549. int left_reg = kcontrol->private_value & 0xff;
  1550. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1551. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1552. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1553. int mask = (kcontrol->private_value >> 24) & 0xff;
  1554. int invert = (kcontrol->private_value >> 22) & 1;
  1555. spin_lock_irqsave(&chip->reg_lock, flags);
  1556. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1557. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1558. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1559. if (invert) {
  1560. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1561. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1562. }
  1563. return 0;
  1564. }
  1565. int snd_cs4231_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1566. {
  1567. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1568. unsigned long flags;
  1569. int left_reg = kcontrol->private_value & 0xff;
  1570. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1571. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1572. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1573. int mask = (kcontrol->private_value >> 24) & 0xff;
  1574. int invert = (kcontrol->private_value >> 22) & 1;
  1575. int change;
  1576. unsigned short val1, val2;
  1577. val1 = ucontrol->value.integer.value[0] & mask;
  1578. val2 = ucontrol->value.integer.value[1] & mask;
  1579. if (invert) {
  1580. val1 = mask - val1;
  1581. val2 = mask - val2;
  1582. }
  1583. val1 <<= shift_left;
  1584. val2 <<= shift_right;
  1585. spin_lock_irqsave(&chip->reg_lock, flags);
  1586. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1587. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1588. change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
  1589. snd_cs4231_out(chip, left_reg, val1);
  1590. snd_cs4231_out(chip, right_reg, val2);
  1591. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1592. return change;
  1593. }
  1594. static struct snd_kcontrol_new snd_cs4231_controls[] = {
  1595. CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1596. CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  1597. CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1598. CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  1599. CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1600. CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  1601. CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1602. CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  1603. CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
  1604. CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
  1605. CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
  1606. CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
  1607. CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1608. {
  1609. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1610. .name = "Capture Source",
  1611. .info = snd_cs4231_info_mux,
  1612. .get = snd_cs4231_get_mux,
  1613. .put = snd_cs4231_put_mux,
  1614. },
  1615. CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1616. CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  1617. CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1)
  1618. };
  1619. int snd_cs4231_mixer(struct snd_cs4231 *chip)
  1620. {
  1621. struct snd_card *card;
  1622. unsigned int idx;
  1623. int err;
  1624. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1625. card = chip->card;
  1626. strcpy(card->mixername, chip->pcm->name);
  1627. for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
  1628. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4231_controls[idx], chip))) < 0)
  1629. return err;
  1630. }
  1631. return 0;
  1632. }
  1633. EXPORT_SYMBOL(snd_cs4231_out);
  1634. EXPORT_SYMBOL(snd_cs4231_in);
  1635. EXPORT_SYMBOL(snd_cs4236_ext_out);
  1636. EXPORT_SYMBOL(snd_cs4236_ext_in);
  1637. EXPORT_SYMBOL(snd_cs4231_mce_up);
  1638. EXPORT_SYMBOL(snd_cs4231_mce_down);
  1639. EXPORT_SYMBOL(snd_cs4231_interrupt);
  1640. EXPORT_SYMBOL(snd_cs4231_chip_id);
  1641. EXPORT_SYMBOL(snd_cs4231_create);
  1642. EXPORT_SYMBOL(snd_cs4231_pcm);
  1643. EXPORT_SYMBOL(snd_cs4231_mixer);
  1644. EXPORT_SYMBOL(snd_cs4231_timer);
  1645. EXPORT_SYMBOL(snd_cs4231_info_single);
  1646. EXPORT_SYMBOL(snd_cs4231_get_single);
  1647. EXPORT_SYMBOL(snd_cs4231_put_single);
  1648. EXPORT_SYMBOL(snd_cs4231_info_double);
  1649. EXPORT_SYMBOL(snd_cs4231_get_double);
  1650. EXPORT_SYMBOL(snd_cs4231_put_double);
  1651. /*
  1652. * INIT part
  1653. */
  1654. static int __init alsa_cs4231_init(void)
  1655. {
  1656. return 0;
  1657. }
  1658. static void __exit alsa_cs4231_exit(void)
  1659. {
  1660. }
  1661. module_init(alsa_cs4231_init)
  1662. module_exit(alsa_cs4231_exit)