cs4231_lib.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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 0
  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 (chip->hardware & CS4231_HW_CS4232_MASK) {
  1337. if (chip->res_cport == NULL)
  1338. snd_printk("CS4232 control port features are not accessible\n");
  1339. }
  1340. /* Register device */
  1341. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1342. snd_cs4231_free(chip);
  1343. return err;
  1344. }
  1345. #ifdef CONFIG_PM
  1346. /* Power Management */
  1347. chip->suspend = snd_cs4231_suspend;
  1348. chip->resume = snd_cs4231_resume;
  1349. #endif
  1350. *rchip = chip;
  1351. return 0;
  1352. }
  1353. static struct snd_pcm_ops snd_cs4231_playback_ops = {
  1354. .open = snd_cs4231_playback_open,
  1355. .close = snd_cs4231_playback_close,
  1356. .ioctl = snd_pcm_lib_ioctl,
  1357. .hw_params = snd_cs4231_playback_hw_params,
  1358. .hw_free = snd_cs4231_playback_hw_free,
  1359. .prepare = snd_cs4231_playback_prepare,
  1360. .trigger = snd_cs4231_trigger,
  1361. .pointer = snd_cs4231_playback_pointer,
  1362. };
  1363. static struct snd_pcm_ops snd_cs4231_capture_ops = {
  1364. .open = snd_cs4231_capture_open,
  1365. .close = snd_cs4231_capture_close,
  1366. .ioctl = snd_pcm_lib_ioctl,
  1367. .hw_params = snd_cs4231_capture_hw_params,
  1368. .hw_free = snd_cs4231_capture_hw_free,
  1369. .prepare = snd_cs4231_capture_prepare,
  1370. .trigger = snd_cs4231_trigger,
  1371. .pointer = snd_cs4231_capture_pointer,
  1372. };
  1373. int snd_cs4231_pcm(struct snd_cs4231 *chip, int device, struct snd_pcm **rpcm)
  1374. {
  1375. struct snd_pcm *pcm;
  1376. int err;
  1377. if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
  1378. return err;
  1379. spin_lock_init(&chip->reg_lock);
  1380. init_MUTEX(&chip->mce_mutex);
  1381. init_MUTEX(&chip->open_mutex);
  1382. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
  1383. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
  1384. /* global setup */
  1385. pcm->private_data = chip;
  1386. pcm->info_flags = 0;
  1387. if (chip->single_dma)
  1388. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1389. if (chip->hardware != CS4231_HW_INTERWAVE)
  1390. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1391. strcpy(pcm->name, snd_cs4231_chip_id(chip));
  1392. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1393. snd_dma_isa_data(),
  1394. 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1395. chip->pcm = pcm;
  1396. if (rpcm)
  1397. *rpcm = pcm;
  1398. return 0;
  1399. }
  1400. static void snd_cs4231_timer_free(struct snd_timer *timer)
  1401. {
  1402. struct snd_cs4231 *chip = timer->private_data;
  1403. chip->timer = NULL;
  1404. }
  1405. int snd_cs4231_timer(struct snd_cs4231 *chip, int device, struct snd_timer **rtimer)
  1406. {
  1407. struct snd_timer *timer;
  1408. struct snd_timer_id tid;
  1409. int err;
  1410. /* Timer initialization */
  1411. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1412. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1413. tid.card = chip->card->number;
  1414. tid.device = device;
  1415. tid.subdevice = 0;
  1416. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1417. return err;
  1418. strcpy(timer->name, snd_cs4231_chip_id(chip));
  1419. timer->private_data = chip;
  1420. timer->private_free = snd_cs4231_timer_free;
  1421. timer->hw = snd_cs4231_timer_table;
  1422. chip->timer = timer;
  1423. if (rtimer)
  1424. *rtimer = timer;
  1425. return 0;
  1426. }
  1427. /*
  1428. * MIXER part
  1429. */
  1430. static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1431. {
  1432. static char *texts[4] = {
  1433. "Line", "Aux", "Mic", "Mix"
  1434. };
  1435. static char *opl3sa_texts[4] = {
  1436. "Line", "CD", "Mic", "Mix"
  1437. };
  1438. static char *gusmax_texts[4] = {
  1439. "Line", "Synth", "Mic", "Mix"
  1440. };
  1441. char **ptexts = texts;
  1442. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1443. snd_assert(chip->card != NULL, return -EINVAL);
  1444. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1445. uinfo->count = 2;
  1446. uinfo->value.enumerated.items = 4;
  1447. if (uinfo->value.enumerated.item > 3)
  1448. uinfo->value.enumerated.item = 3;
  1449. if (!strcmp(chip->card->driver, "GUS MAX"))
  1450. ptexts = gusmax_texts;
  1451. switch (chip->hardware) {
  1452. case CS4231_HW_INTERWAVE: ptexts = gusmax_texts; break;
  1453. case CS4231_HW_OPL3SA2: ptexts = opl3sa_texts; break;
  1454. }
  1455. strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
  1456. return 0;
  1457. }
  1458. static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1459. {
  1460. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1461. unsigned long flags;
  1462. spin_lock_irqsave(&chip->reg_lock, flags);
  1463. ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1464. ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1465. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1466. return 0;
  1467. }
  1468. static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1469. {
  1470. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1471. unsigned long flags;
  1472. unsigned short left, right;
  1473. int change;
  1474. if (ucontrol->value.enumerated.item[0] > 3 ||
  1475. ucontrol->value.enumerated.item[1] > 3)
  1476. return -EINVAL;
  1477. left = ucontrol->value.enumerated.item[0] << 6;
  1478. right = ucontrol->value.enumerated.item[1] << 6;
  1479. spin_lock_irqsave(&chip->reg_lock, flags);
  1480. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1481. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1482. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1483. right != chip->image[CS4231_RIGHT_INPUT];
  1484. snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
  1485. snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
  1486. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1487. return change;
  1488. }
  1489. int snd_cs4231_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1490. {
  1491. int mask = (kcontrol->private_value >> 16) & 0xff;
  1492. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1493. uinfo->count = 1;
  1494. uinfo->value.integer.min = 0;
  1495. uinfo->value.integer.max = mask;
  1496. return 0;
  1497. }
  1498. int snd_cs4231_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1499. {
  1500. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1501. unsigned long flags;
  1502. int reg = kcontrol->private_value & 0xff;
  1503. int shift = (kcontrol->private_value >> 8) & 0xff;
  1504. int mask = (kcontrol->private_value >> 16) & 0xff;
  1505. int invert = (kcontrol->private_value >> 24) & 0xff;
  1506. spin_lock_irqsave(&chip->reg_lock, flags);
  1507. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1508. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1509. if (invert)
  1510. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1511. return 0;
  1512. }
  1513. int snd_cs4231_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1514. {
  1515. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1516. unsigned long flags;
  1517. int reg = kcontrol->private_value & 0xff;
  1518. int shift = (kcontrol->private_value >> 8) & 0xff;
  1519. int mask = (kcontrol->private_value >> 16) & 0xff;
  1520. int invert = (kcontrol->private_value >> 24) & 0xff;
  1521. int change;
  1522. unsigned short val;
  1523. val = (ucontrol->value.integer.value[0] & mask);
  1524. if (invert)
  1525. val = mask - val;
  1526. val <<= shift;
  1527. spin_lock_irqsave(&chip->reg_lock, flags);
  1528. val = (chip->image[reg] & ~(mask << shift)) | val;
  1529. change = val != chip->image[reg];
  1530. snd_cs4231_out(chip, reg, val);
  1531. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1532. return change;
  1533. }
  1534. int snd_cs4231_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1535. {
  1536. int mask = (kcontrol->private_value >> 24) & 0xff;
  1537. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1538. uinfo->count = 2;
  1539. uinfo->value.integer.min = 0;
  1540. uinfo->value.integer.max = mask;
  1541. return 0;
  1542. }
  1543. int snd_cs4231_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1544. {
  1545. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1546. unsigned long flags;
  1547. int left_reg = kcontrol->private_value & 0xff;
  1548. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1549. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1550. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1551. int mask = (kcontrol->private_value >> 24) & 0xff;
  1552. int invert = (kcontrol->private_value >> 22) & 1;
  1553. spin_lock_irqsave(&chip->reg_lock, flags);
  1554. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1555. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1556. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1557. if (invert) {
  1558. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1559. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1560. }
  1561. return 0;
  1562. }
  1563. int snd_cs4231_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1564. {
  1565. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1566. unsigned long flags;
  1567. int left_reg = kcontrol->private_value & 0xff;
  1568. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1569. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1570. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1571. int mask = (kcontrol->private_value >> 24) & 0xff;
  1572. int invert = (kcontrol->private_value >> 22) & 1;
  1573. int change;
  1574. unsigned short val1, val2;
  1575. val1 = ucontrol->value.integer.value[0] & mask;
  1576. val2 = ucontrol->value.integer.value[1] & mask;
  1577. if (invert) {
  1578. val1 = mask - val1;
  1579. val2 = mask - val2;
  1580. }
  1581. val1 <<= shift_left;
  1582. val2 <<= shift_right;
  1583. spin_lock_irqsave(&chip->reg_lock, flags);
  1584. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1585. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1586. change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
  1587. snd_cs4231_out(chip, left_reg, val1);
  1588. snd_cs4231_out(chip, right_reg, val2);
  1589. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1590. return change;
  1591. }
  1592. static struct snd_kcontrol_new snd_cs4231_controls[] = {
  1593. CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1594. CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  1595. CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1596. CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  1597. CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1598. CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  1599. CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1600. CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  1601. CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
  1602. CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
  1603. CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
  1604. CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
  1605. CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1606. {
  1607. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1608. .name = "Capture Source",
  1609. .info = snd_cs4231_info_mux,
  1610. .get = snd_cs4231_get_mux,
  1611. .put = snd_cs4231_put_mux,
  1612. },
  1613. CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1614. CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  1615. CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1)
  1616. };
  1617. int snd_cs4231_mixer(struct snd_cs4231 *chip)
  1618. {
  1619. struct snd_card *card;
  1620. unsigned int idx;
  1621. int err;
  1622. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1623. card = chip->card;
  1624. strcpy(card->mixername, chip->pcm->name);
  1625. for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
  1626. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4231_controls[idx], chip))) < 0)
  1627. return err;
  1628. }
  1629. return 0;
  1630. }
  1631. EXPORT_SYMBOL(snd_cs4231_out);
  1632. EXPORT_SYMBOL(snd_cs4231_in);
  1633. EXPORT_SYMBOL(snd_cs4236_ext_out);
  1634. EXPORT_SYMBOL(snd_cs4236_ext_in);
  1635. EXPORT_SYMBOL(snd_cs4231_mce_up);
  1636. EXPORT_SYMBOL(snd_cs4231_mce_down);
  1637. EXPORT_SYMBOL(snd_cs4231_interrupt);
  1638. EXPORT_SYMBOL(snd_cs4231_chip_id);
  1639. EXPORT_SYMBOL(snd_cs4231_create);
  1640. EXPORT_SYMBOL(snd_cs4231_pcm);
  1641. EXPORT_SYMBOL(snd_cs4231_mixer);
  1642. EXPORT_SYMBOL(snd_cs4231_timer);
  1643. EXPORT_SYMBOL(snd_cs4231_info_single);
  1644. EXPORT_SYMBOL(snd_cs4231_get_single);
  1645. EXPORT_SYMBOL(snd_cs4231_put_single);
  1646. EXPORT_SYMBOL(snd_cs4231_info_double);
  1647. EXPORT_SYMBOL(snd_cs4231_get_double);
  1648. EXPORT_SYMBOL(snd_cs4231_put_double);
  1649. /*
  1650. * INIT part
  1651. */
  1652. static int __init alsa_cs4231_init(void)
  1653. {
  1654. return 0;
  1655. }
  1656. static void __exit alsa_cs4231_exit(void)
  1657. {
  1658. }
  1659. module_init(alsa_cs4231_init)
  1660. module_exit(alsa_cs4231_exit)