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 snd_pcm_substream *s;
  372. int do_start;
  373. #if 0
  374. printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, cs4231_inb(chip, CS4231P(STATUS)));
  375. #endif
  376. switch (cmd) {
  377. case SNDRV_PCM_TRIGGER_START:
  378. case SNDRV_PCM_TRIGGER_RESUME:
  379. do_start = 1; break;
  380. case SNDRV_PCM_TRIGGER_STOP:
  381. case SNDRV_PCM_TRIGGER_SUSPEND:
  382. do_start = 0; break;
  383. default:
  384. return -EINVAL;
  385. }
  386. what = 0;
  387. snd_pcm_group_for_each_entry(s, substream) {
  388. if (s == chip->playback_substream) {
  389. what |= CS4231_PLAYBACK_ENABLE;
  390. snd_pcm_trigger_done(s, substream);
  391. } else if (s == chip->capture_substream) {
  392. what |= CS4231_RECORD_ENABLE;
  393. snd_pcm_trigger_done(s, substream);
  394. }
  395. }
  396. spin_lock(&chip->reg_lock);
  397. if (do_start) {
  398. chip->image[CS4231_IFACE_CTRL] |= what;
  399. if (chip->trigger)
  400. chip->trigger(chip, what, 1);
  401. } else {
  402. chip->image[CS4231_IFACE_CTRL] &= ~what;
  403. if (chip->trigger)
  404. chip->trigger(chip, what, 0);
  405. }
  406. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  407. spin_unlock(&chip->reg_lock);
  408. #if 0
  409. snd_cs4231_debug(chip);
  410. #endif
  411. return result;
  412. }
  413. /*
  414. * CODEC I/O
  415. */
  416. static unsigned char snd_cs4231_get_rate(unsigned int rate)
  417. {
  418. int i;
  419. for (i = 0; i < 14; i++)
  420. if (rate == rates[i])
  421. return freq_bits[i];
  422. // snd_BUG();
  423. return freq_bits[13];
  424. }
  425. static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip,
  426. int format,
  427. int channels)
  428. {
  429. unsigned char rformat;
  430. rformat = CS4231_LINEAR_8;
  431. switch (format) {
  432. case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
  433. case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
  434. case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
  435. case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
  436. case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
  437. }
  438. if (channels > 1)
  439. rformat |= CS4231_STEREO;
  440. #if 0
  441. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  442. #endif
  443. return rformat;
  444. }
  445. static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
  446. {
  447. unsigned long flags;
  448. mute = mute ? 1 : 0;
  449. spin_lock_irqsave(&chip->reg_lock, flags);
  450. if (chip->calibrate_mute == mute) {
  451. spin_unlock_irqrestore(&chip->reg_lock, flags);
  452. return;
  453. }
  454. if (!mute) {
  455. snd_cs4231_dout(chip, CS4231_LEFT_INPUT, chip->image[CS4231_LEFT_INPUT]);
  456. snd_cs4231_dout(chip, CS4231_RIGHT_INPUT, chip->image[CS4231_RIGHT_INPUT]);
  457. snd_cs4231_dout(chip, CS4231_LOOPBACK, chip->image[CS4231_LOOPBACK]);
  458. }
  459. snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
  460. snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
  461. snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
  462. snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
  463. snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
  464. snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
  465. snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN, mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
  466. snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN, mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
  467. snd_cs4231_dout(chip, CS4231_MONO_CTRL, mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
  468. if (chip->hardware == CS4231_HW_INTERWAVE) {
  469. snd_cs4231_dout(chip, CS4231_LEFT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
  470. snd_cs4231_dout(chip, CS4231_RIGHT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
  471. snd_cs4231_dout(chip, CS4231_LINE_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
  472. snd_cs4231_dout(chip, CS4231_LINE_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
  473. }
  474. chip->calibrate_mute = mute;
  475. spin_unlock_irqrestore(&chip->reg_lock, flags);
  476. }
  477. static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
  478. struct snd_pcm_hw_params *params,
  479. unsigned char pdfr)
  480. {
  481. unsigned long flags;
  482. int full_calib = 1;
  483. mutex_lock(&chip->mce_mutex);
  484. snd_cs4231_calibrate_mute(chip, 1);
  485. if (chip->hardware == CS4231_HW_CS4231A ||
  486. (chip->hardware & CS4231_HW_CS4232_MASK)) {
  487. spin_lock_irqsave(&chip->reg_lock, flags);
  488. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
  489. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10);
  490. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
  491. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
  492. udelay(100); /* Fixes audible clicks at least on GUS MAX */
  493. full_calib = 0;
  494. }
  495. spin_unlock_irqrestore(&chip->reg_lock, flags);
  496. }
  497. if (full_calib) {
  498. snd_cs4231_mce_up(chip);
  499. spin_lock_irqsave(&chip->reg_lock, flags);
  500. if (chip->hardware != CS4231_HW_INTERWAVE && !chip->single_dma) {
  501. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  502. (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
  503. (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
  504. pdfr);
  505. } else {
  506. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
  507. }
  508. spin_unlock_irqrestore(&chip->reg_lock, flags);
  509. snd_cs4231_mce_down(chip);
  510. }
  511. snd_cs4231_calibrate_mute(chip, 0);
  512. mutex_unlock(&chip->mce_mutex);
  513. }
  514. static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
  515. struct snd_pcm_hw_params *params,
  516. unsigned char cdfr)
  517. {
  518. unsigned long flags;
  519. int full_calib = 1;
  520. mutex_lock(&chip->mce_mutex);
  521. snd_cs4231_calibrate_mute(chip, 1);
  522. if (chip->hardware == CS4231_HW_CS4231A ||
  523. (chip->hardware & CS4231_HW_CS4232_MASK)) {
  524. spin_lock_irqsave(&chip->reg_lock, flags);
  525. if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
  526. (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  527. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20);
  528. snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT] = cdfr);
  529. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
  530. full_calib = 0;
  531. }
  532. spin_unlock_irqrestore(&chip->reg_lock, flags);
  533. }
  534. if (full_calib) {
  535. snd_cs4231_mce_up(chip);
  536. spin_lock_irqsave(&chip->reg_lock, flags);
  537. if (chip->hardware != CS4231_HW_INTERWAVE) {
  538. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  539. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  540. ((chip->single_dma ? cdfr : chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
  541. (cdfr & 0x0f));
  542. spin_unlock_irqrestore(&chip->reg_lock, flags);
  543. snd_cs4231_mce_down(chip);
  544. snd_cs4231_mce_up(chip);
  545. spin_lock_irqsave(&chip->reg_lock, flags);
  546. }
  547. }
  548. snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
  549. spin_unlock_irqrestore(&chip->reg_lock, flags);
  550. snd_cs4231_mce_down(chip);
  551. }
  552. snd_cs4231_calibrate_mute(chip, 0);
  553. mutex_unlock(&chip->mce_mutex);
  554. }
  555. /*
  556. * Timer interface
  557. */
  558. static unsigned long snd_cs4231_timer_resolution(struct snd_timer * timer)
  559. {
  560. struct snd_cs4231 *chip = snd_timer_chip(timer);
  561. if (chip->hardware & CS4231_HW_CS4236B_MASK)
  562. return 14467;
  563. else
  564. return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
  565. }
  566. static int snd_cs4231_timer_start(struct snd_timer * timer)
  567. {
  568. unsigned long flags;
  569. unsigned int ticks;
  570. struct snd_cs4231 *chip = snd_timer_chip(timer);
  571. spin_lock_irqsave(&chip->reg_lock, flags);
  572. ticks = timer->sticks;
  573. if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
  574. (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
  575. (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
  576. snd_cs4231_out(chip, CS4231_TIMER_HIGH, chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8));
  577. snd_cs4231_out(chip, CS4231_TIMER_LOW, chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks);
  578. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
  579. }
  580. spin_unlock_irqrestore(&chip->reg_lock, flags);
  581. return 0;
  582. }
  583. static int snd_cs4231_timer_stop(struct snd_timer * timer)
  584. {
  585. unsigned long flags;
  586. struct snd_cs4231 *chip = snd_timer_chip(timer);
  587. spin_lock_irqsave(&chip->reg_lock, flags);
  588. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
  589. spin_unlock_irqrestore(&chip->reg_lock, flags);
  590. return 0;
  591. }
  592. static void snd_cs4231_init(struct snd_cs4231 *chip)
  593. {
  594. unsigned long flags;
  595. snd_cs4231_mce_down(chip);
  596. #ifdef SNDRV_DEBUG_MCE
  597. snd_printk("init: (1)\n");
  598. #endif
  599. snd_cs4231_mce_up(chip);
  600. spin_lock_irqsave(&chip->reg_lock, flags);
  601. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  602. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
  603. CS4231_CALIB_MODE);
  604. chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
  605. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  606. spin_unlock_irqrestore(&chip->reg_lock, flags);
  607. snd_cs4231_mce_down(chip);
  608. #ifdef SNDRV_DEBUG_MCE
  609. snd_printk("init: (2)\n");
  610. #endif
  611. snd_cs4231_mce_up(chip);
  612. spin_lock_irqsave(&chip->reg_lock, flags);
  613. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
  614. spin_unlock_irqrestore(&chip->reg_lock, flags);
  615. snd_cs4231_mce_down(chip);
  616. #ifdef SNDRV_DEBUG_MCE
  617. snd_printk("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
  618. #endif
  619. spin_lock_irqsave(&chip->reg_lock, flags);
  620. snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
  621. spin_unlock_irqrestore(&chip->reg_lock, flags);
  622. snd_cs4231_mce_up(chip);
  623. spin_lock_irqsave(&chip->reg_lock, flags);
  624. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
  625. spin_unlock_irqrestore(&chip->reg_lock, flags);
  626. snd_cs4231_mce_down(chip);
  627. #ifdef SNDRV_DEBUG_MCE
  628. snd_printk("init: (4)\n");
  629. #endif
  630. snd_cs4231_mce_up(chip);
  631. spin_lock_irqsave(&chip->reg_lock, flags);
  632. snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
  633. spin_unlock_irqrestore(&chip->reg_lock, flags);
  634. snd_cs4231_mce_down(chip);
  635. #ifdef SNDRV_DEBUG_MCE
  636. snd_printk("init: (5)\n");
  637. #endif
  638. }
  639. static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
  640. {
  641. unsigned long flags;
  642. mutex_lock(&chip->open_mutex);
  643. if ((chip->mode & mode) ||
  644. ((chip->mode & CS4231_MODE_OPEN) && chip->single_dma)) {
  645. mutex_unlock(&chip->open_mutex);
  646. return -EAGAIN;
  647. }
  648. if (chip->mode & CS4231_MODE_OPEN) {
  649. chip->mode |= mode;
  650. mutex_unlock(&chip->open_mutex);
  651. return 0;
  652. }
  653. /* ok. now enable and ack CODEC IRQ */
  654. spin_lock_irqsave(&chip->reg_lock, flags);
  655. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  656. CS4231_RECORD_IRQ |
  657. CS4231_TIMER_IRQ);
  658. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  659. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  660. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  661. chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
  662. snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  663. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  664. CS4231_RECORD_IRQ |
  665. CS4231_TIMER_IRQ);
  666. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  667. spin_unlock_irqrestore(&chip->reg_lock, flags);
  668. chip->mode = mode;
  669. mutex_unlock(&chip->open_mutex);
  670. return 0;
  671. }
  672. static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
  673. {
  674. unsigned long flags;
  675. mutex_lock(&chip->open_mutex);
  676. chip->mode &= ~mode;
  677. if (chip->mode & CS4231_MODE_OPEN) {
  678. mutex_unlock(&chip->open_mutex);
  679. return;
  680. }
  681. snd_cs4231_calibrate_mute(chip, 1);
  682. /* disable IRQ */
  683. spin_lock_irqsave(&chip->reg_lock, flags);
  684. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  685. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  686. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  687. chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
  688. snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
  689. /* now disable record & playback */
  690. if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  691. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
  692. spin_unlock_irqrestore(&chip->reg_lock, flags);
  693. snd_cs4231_mce_up(chip);
  694. spin_lock_irqsave(&chip->reg_lock, flags);
  695. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  696. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  697. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  698. spin_unlock_irqrestore(&chip->reg_lock, flags);
  699. snd_cs4231_mce_down(chip);
  700. spin_lock_irqsave(&chip->reg_lock, flags);
  701. }
  702. /* clear IRQ again */
  703. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  704. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  705. cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
  706. spin_unlock_irqrestore(&chip->reg_lock, flags);
  707. snd_cs4231_calibrate_mute(chip, 0);
  708. chip->mode = 0;
  709. mutex_unlock(&chip->open_mutex);
  710. }
  711. /*
  712. * timer open/close
  713. */
  714. static int snd_cs4231_timer_open(struct snd_timer * timer)
  715. {
  716. struct snd_cs4231 *chip = snd_timer_chip(timer);
  717. snd_cs4231_open(chip, CS4231_MODE_TIMER);
  718. return 0;
  719. }
  720. static int snd_cs4231_timer_close(struct snd_timer * timer)
  721. {
  722. struct snd_cs4231 *chip = snd_timer_chip(timer);
  723. snd_cs4231_close(chip, CS4231_MODE_TIMER);
  724. return 0;
  725. }
  726. static struct snd_timer_hardware snd_cs4231_timer_table =
  727. {
  728. .flags = SNDRV_TIMER_HW_AUTO,
  729. .resolution = 9945,
  730. .ticks = 65535,
  731. .open = snd_cs4231_timer_open,
  732. .close = snd_cs4231_timer_close,
  733. .c_resolution = snd_cs4231_timer_resolution,
  734. .start = snd_cs4231_timer_start,
  735. .stop = snd_cs4231_timer_stop,
  736. };
  737. /*
  738. * ok.. exported functions..
  739. */
  740. static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
  741. struct snd_pcm_hw_params *hw_params)
  742. {
  743. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  744. unsigned char new_pdfr;
  745. int err;
  746. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  747. return err;
  748. new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
  749. snd_cs4231_get_rate(params_rate(hw_params));
  750. chip->set_playback_format(chip, hw_params, new_pdfr);
  751. return 0;
  752. }
  753. static int snd_cs4231_playback_hw_free(struct snd_pcm_substream *substream)
  754. {
  755. return snd_pcm_lib_free_pages(substream);
  756. }
  757. static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
  758. {
  759. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  760. struct snd_pcm_runtime *runtime = substream->runtime;
  761. unsigned long flags;
  762. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  763. unsigned int count = snd_pcm_lib_period_bytes(substream);
  764. spin_lock_irqsave(&chip->reg_lock, flags);
  765. chip->p_dma_size = size;
  766. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
  767. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  768. count = snd_cs4231_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
  769. snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  770. snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  771. spin_unlock_irqrestore(&chip->reg_lock, flags);
  772. #if 0
  773. snd_cs4231_debug(chip);
  774. #endif
  775. return 0;
  776. }
  777. static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
  778. struct snd_pcm_hw_params *hw_params)
  779. {
  780. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  781. unsigned char new_cdfr;
  782. int err;
  783. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  784. return err;
  785. new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
  786. snd_cs4231_get_rate(params_rate(hw_params));
  787. chip->set_capture_format(chip, hw_params, new_cdfr);
  788. return 0;
  789. }
  790. static int snd_cs4231_capture_hw_free(struct snd_pcm_substream *substream)
  791. {
  792. return snd_pcm_lib_free_pages(substream);
  793. }
  794. static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
  795. {
  796. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  797. struct snd_pcm_runtime *runtime = substream->runtime;
  798. unsigned long flags;
  799. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  800. unsigned int count = snd_pcm_lib_period_bytes(substream);
  801. spin_lock_irqsave(&chip->reg_lock, flags);
  802. chip->c_dma_size = size;
  803. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  804. snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  805. count = snd_cs4231_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
  806. if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
  807. snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
  808. snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
  809. } else {
  810. snd_cs4231_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
  811. snd_cs4231_out(chip, CS4231_REC_UPR_CNT, (unsigned char) (count >> 8));
  812. }
  813. spin_unlock_irqrestore(&chip->reg_lock, flags);
  814. return 0;
  815. }
  816. static void snd_cs4231_overrange(struct snd_cs4231 *chip)
  817. {
  818. unsigned long flags;
  819. unsigned char res;
  820. spin_lock_irqsave(&chip->reg_lock, flags);
  821. res = snd_cs4231_in(chip, CS4231_TEST_INIT);
  822. spin_unlock_irqrestore(&chip->reg_lock, flags);
  823. if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
  824. chip->capture_substream->runtime->overrange++;
  825. }
  826. irqreturn_t snd_cs4231_interrupt(int irq, void *dev_id)
  827. {
  828. struct snd_cs4231 *chip = dev_id;
  829. unsigned char status;
  830. status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
  831. if (status & CS4231_TIMER_IRQ) {
  832. if (chip->timer)
  833. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  834. }
  835. if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
  836. if (status & CS4231_PLAYBACK_IRQ) {
  837. if (chip->mode & CS4231_MODE_PLAY) {
  838. if (chip->playback_substream)
  839. snd_pcm_period_elapsed(chip->playback_substream);
  840. }
  841. if (chip->mode & CS4231_MODE_RECORD) {
  842. if (chip->capture_substream) {
  843. snd_cs4231_overrange(chip);
  844. snd_pcm_period_elapsed(chip->capture_substream);
  845. }
  846. }
  847. }
  848. } else {
  849. if (status & CS4231_PLAYBACK_IRQ) {
  850. if (chip->playback_substream)
  851. snd_pcm_period_elapsed(chip->playback_substream);
  852. }
  853. if (status & CS4231_RECORD_IRQ) {
  854. if (chip->capture_substream) {
  855. snd_cs4231_overrange(chip);
  856. snd_pcm_period_elapsed(chip->capture_substream);
  857. }
  858. }
  859. }
  860. spin_lock(&chip->reg_lock);
  861. snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
  862. spin_unlock(&chip->reg_lock);
  863. return IRQ_HANDLED;
  864. }
  865. static snd_pcm_uframes_t snd_cs4231_playback_pointer(struct snd_pcm_substream *substream)
  866. {
  867. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  868. size_t ptr;
  869. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
  870. return 0;
  871. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  872. return bytes_to_frames(substream->runtime, ptr);
  873. }
  874. static snd_pcm_uframes_t snd_cs4231_capture_pointer(struct snd_pcm_substream *substream)
  875. {
  876. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  877. size_t ptr;
  878. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
  879. return 0;
  880. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  881. return bytes_to_frames(substream->runtime, ptr);
  882. }
  883. /*
  884. */
  885. static int snd_cs4231_probe(struct snd_cs4231 *chip)
  886. {
  887. unsigned long flags;
  888. int i, id, rev;
  889. unsigned char *ptr;
  890. unsigned int hw;
  891. #if 0
  892. snd_cs4231_debug(chip);
  893. #endif
  894. id = 0;
  895. for (i = 0; i < 50; i++) {
  896. mb();
  897. if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
  898. udelay(2000);
  899. else {
  900. spin_lock_irqsave(&chip->reg_lock, flags);
  901. snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
  902. id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
  903. spin_unlock_irqrestore(&chip->reg_lock, flags);
  904. if (id == 0x0a)
  905. break; /* this is valid value */
  906. }
  907. }
  908. snd_printdd("cs4231: port = 0x%lx, id = 0x%x\n", chip->port, id);
  909. if (id != 0x0a)
  910. return -ENODEV; /* no valid device found */
  911. if (((hw = chip->hardware) & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
  912. rev = snd_cs4231_in(chip, CS4231_VERSION) & 0xe7;
  913. snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
  914. if (rev == 0x80) {
  915. unsigned char tmp = snd_cs4231_in(chip, 23);
  916. snd_cs4231_out(chip, 23, ~tmp);
  917. if (snd_cs4231_in(chip, 23) != tmp)
  918. chip->hardware = CS4231_HW_AD1845;
  919. else
  920. chip->hardware = CS4231_HW_CS4231;
  921. } else if (rev == 0xa0) {
  922. chip->hardware = CS4231_HW_CS4231A;
  923. } else if (rev == 0xa2) {
  924. chip->hardware = CS4231_HW_CS4232;
  925. } else if (rev == 0xb2) {
  926. chip->hardware = CS4231_HW_CS4232A;
  927. } else if (rev == 0x83) {
  928. chip->hardware = CS4231_HW_CS4236;
  929. } else if (rev == 0x03) {
  930. chip->hardware = CS4231_HW_CS4236B;
  931. } else {
  932. snd_printk("unknown CS chip with version 0x%x\n", rev);
  933. return -ENODEV; /* unknown CS4231 chip? */
  934. }
  935. }
  936. spin_lock_irqsave(&chip->reg_lock, flags);
  937. cs4231_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
  938. cs4231_outb(chip, CS4231P(STATUS), 0);
  939. mb();
  940. spin_unlock_irqrestore(&chip->reg_lock, flags);
  941. chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
  942. switch (chip->hardware) {
  943. case CS4231_HW_INTERWAVE:
  944. chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
  945. break;
  946. case CS4231_HW_CS4235:
  947. case CS4231_HW_CS4236B:
  948. case CS4231_HW_CS4237B:
  949. case CS4231_HW_CS4238B:
  950. case CS4231_HW_CS4239:
  951. if (hw == CS4231_HW_DETECT3)
  952. chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
  953. else
  954. chip->hardware = CS4231_HW_CS4236;
  955. break;
  956. }
  957. chip->image[CS4231_IFACE_CTRL] =
  958. (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
  959. (chip->single_dma ? CS4231_SINGLE_DMA : 0);
  960. chip->image[CS4231_ALT_FEATURE_1] = 0x80;
  961. chip->image[CS4231_ALT_FEATURE_2] = chip->hardware == CS4231_HW_INTERWAVE ? 0xc2 : 0x01;
  962. ptr = (unsigned char *) &chip->image;
  963. snd_cs4231_mce_down(chip);
  964. spin_lock_irqsave(&chip->reg_lock, flags);
  965. for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
  966. snd_cs4231_out(chip, i, *ptr++);
  967. spin_unlock_irqrestore(&chip->reg_lock, flags);
  968. snd_cs4231_mce_up(chip);
  969. snd_cs4231_mce_down(chip);
  970. mdelay(2);
  971. /* ok.. try check hardware version for CS4236+ chips */
  972. if ((hw & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
  973. if (chip->hardware == CS4231_HW_CS4236B) {
  974. rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
  975. snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
  976. id = snd_cs4236_ext_in(chip, CS4236_VERSION);
  977. snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
  978. snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
  979. if ((id & 0x1f) == 0x1d) { /* CS4235 */
  980. chip->hardware = CS4231_HW_CS4235;
  981. switch (id >> 5) {
  982. case 4:
  983. case 5:
  984. case 6:
  985. break;
  986. default:
  987. snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
  988. }
  989. } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
  990. switch (id >> 5) {
  991. case 4:
  992. case 5:
  993. case 6:
  994. case 7:
  995. chip->hardware = CS4231_HW_CS4236B;
  996. break;
  997. default:
  998. snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
  999. }
  1000. } else if ((id & 0x1f) == 0x08) { /* CS4237B */
  1001. chip->hardware = CS4231_HW_CS4237B;
  1002. switch (id >> 5) {
  1003. case 4:
  1004. case 5:
  1005. case 6:
  1006. case 7:
  1007. break;
  1008. default:
  1009. snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
  1010. }
  1011. } else if ((id & 0x1f) == 0x09) { /* CS4238B */
  1012. chip->hardware = CS4231_HW_CS4238B;
  1013. switch (id >> 5) {
  1014. case 5:
  1015. case 6:
  1016. case 7:
  1017. break;
  1018. default:
  1019. snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
  1020. }
  1021. } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
  1022. chip->hardware = CS4231_HW_CS4239;
  1023. switch (id >> 5) {
  1024. case 4:
  1025. case 5:
  1026. case 6:
  1027. break;
  1028. default:
  1029. snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
  1030. }
  1031. } else {
  1032. snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
  1033. }
  1034. }
  1035. }
  1036. return 0; /* all things are ok.. */
  1037. }
  1038. /*
  1039. */
  1040. static struct snd_pcm_hardware snd_cs4231_playback =
  1041. {
  1042. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1043. SNDRV_PCM_INFO_MMAP_VALID |
  1044. SNDRV_PCM_INFO_RESUME |
  1045. SNDRV_PCM_INFO_SYNC_START),
  1046. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1047. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1048. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1049. .rate_min = 5510,
  1050. .rate_max = 48000,
  1051. .channels_min = 1,
  1052. .channels_max = 2,
  1053. .buffer_bytes_max = (128*1024),
  1054. .period_bytes_min = 64,
  1055. .period_bytes_max = (128*1024),
  1056. .periods_min = 1,
  1057. .periods_max = 1024,
  1058. .fifo_size = 0,
  1059. };
  1060. static struct snd_pcm_hardware snd_cs4231_capture =
  1061. {
  1062. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1063. SNDRV_PCM_INFO_MMAP_VALID |
  1064. SNDRV_PCM_INFO_RESUME |
  1065. SNDRV_PCM_INFO_SYNC_START),
  1066. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1067. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  1068. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1069. .rate_min = 5510,
  1070. .rate_max = 48000,
  1071. .channels_min = 1,
  1072. .channels_max = 2,
  1073. .buffer_bytes_max = (128*1024),
  1074. .period_bytes_min = 64,
  1075. .period_bytes_max = (128*1024),
  1076. .periods_min = 1,
  1077. .periods_max = 1024,
  1078. .fifo_size = 0,
  1079. };
  1080. /*
  1081. */
  1082. static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
  1083. {
  1084. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1085. struct snd_pcm_runtime *runtime = substream->runtime;
  1086. int err;
  1087. runtime->hw = snd_cs4231_playback;
  1088. /* hardware bug in InterWave chipset */
  1089. if (chip->hardware == CS4231_HW_INTERWAVE && chip->dma1 > 3)
  1090. runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
  1091. /* hardware limitation of cheap chips */
  1092. if (chip->hardware == CS4231_HW_CS4235 ||
  1093. chip->hardware == CS4231_HW_CS4239)
  1094. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1095. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  1096. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  1097. if (chip->claim_dma) {
  1098. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
  1099. return err;
  1100. }
  1101. if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
  1102. if (chip->release_dma)
  1103. chip->release_dma(chip, chip->dma_private_data, chip->dma1);
  1104. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1105. return err;
  1106. }
  1107. chip->playback_substream = substream;
  1108. snd_pcm_set_sync(substream);
  1109. chip->rate_constraint(runtime);
  1110. return 0;
  1111. }
  1112. static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
  1113. {
  1114. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1115. struct snd_pcm_runtime *runtime = substream->runtime;
  1116. int err;
  1117. runtime->hw = snd_cs4231_capture;
  1118. /* hardware limitation of cheap chips */
  1119. if (chip->hardware == CS4231_HW_CS4235 ||
  1120. chip->hardware == CS4231_HW_CS4239)
  1121. runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
  1122. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  1123. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  1124. if (chip->claim_dma) {
  1125. if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
  1126. return err;
  1127. }
  1128. if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
  1129. if (chip->release_dma)
  1130. chip->release_dma(chip, chip->dma_private_data, chip->dma2);
  1131. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1132. return err;
  1133. }
  1134. chip->capture_substream = substream;
  1135. snd_pcm_set_sync(substream);
  1136. chip->rate_constraint(runtime);
  1137. return 0;
  1138. }
  1139. static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
  1140. {
  1141. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1142. chip->playback_substream = NULL;
  1143. snd_cs4231_close(chip, CS4231_MODE_PLAY);
  1144. return 0;
  1145. }
  1146. static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
  1147. {
  1148. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1149. chip->capture_substream = NULL;
  1150. snd_cs4231_close(chip, CS4231_MODE_RECORD);
  1151. return 0;
  1152. }
  1153. #ifdef CONFIG_PM
  1154. /* lowlevel suspend callback for CS4231 */
  1155. static void snd_cs4231_suspend(struct snd_cs4231 *chip)
  1156. {
  1157. int reg;
  1158. unsigned long flags;
  1159. snd_pcm_suspend_all(chip->pcm);
  1160. spin_lock_irqsave(&chip->reg_lock, flags);
  1161. for (reg = 0; reg < 32; reg++)
  1162. chip->image[reg] = snd_cs4231_in(chip, reg);
  1163. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1164. }
  1165. /* lowlevel resume callback for CS4231 */
  1166. static void snd_cs4231_resume(struct snd_cs4231 *chip)
  1167. {
  1168. int reg;
  1169. unsigned long flags;
  1170. /* int timeout; */
  1171. snd_cs4231_mce_up(chip);
  1172. spin_lock_irqsave(&chip->reg_lock, flags);
  1173. for (reg = 0; reg < 32; reg++) {
  1174. switch (reg) {
  1175. case CS4231_VERSION:
  1176. break;
  1177. default:
  1178. snd_cs4231_out(chip, reg, chip->image[reg]);
  1179. break;
  1180. }
  1181. }
  1182. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1183. #if 1
  1184. snd_cs4231_mce_down(chip);
  1185. #else
  1186. /* The following is a workaround to avoid freeze after resume on TP600E.
  1187. This is the first half of copy of snd_cs4231_mce_down(), but doesn't
  1188. include rescheduling. -- iwai
  1189. */
  1190. snd_cs4231_busy_wait(chip);
  1191. spin_lock_irqsave(&chip->reg_lock, flags);
  1192. chip->mce_bit &= ~CS4231_MCE;
  1193. timeout = cs4231_inb(chip, CS4231P(REGSEL));
  1194. cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
  1195. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1196. if (timeout == 0x80)
  1197. snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  1198. if ((timeout & CS4231_MCE) == 0 ||
  1199. !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
  1200. return;
  1201. }
  1202. snd_cs4231_busy_wait(chip);
  1203. #endif
  1204. }
  1205. #endif /* CONFIG_PM */
  1206. static int snd_cs4231_free(struct snd_cs4231 *chip)
  1207. {
  1208. release_and_free_resource(chip->res_port);
  1209. release_and_free_resource(chip->res_cport);
  1210. if (chip->irq >= 0) {
  1211. disable_irq(chip->irq);
  1212. if (!(chip->hwshare & CS4231_HWSHARE_IRQ))
  1213. free_irq(chip->irq, (void *) chip);
  1214. }
  1215. if (!(chip->hwshare & CS4231_HWSHARE_DMA1) && chip->dma1 >= 0) {
  1216. snd_dma_disable(chip->dma1);
  1217. free_dma(chip->dma1);
  1218. }
  1219. if (!(chip->hwshare & CS4231_HWSHARE_DMA2) && chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
  1220. snd_dma_disable(chip->dma2);
  1221. free_dma(chip->dma2);
  1222. }
  1223. if (chip->timer)
  1224. snd_device_free(chip->card, chip->timer);
  1225. kfree(chip);
  1226. return 0;
  1227. }
  1228. static int snd_cs4231_dev_free(struct snd_device *device)
  1229. {
  1230. struct snd_cs4231 *chip = device->device_data;
  1231. return snd_cs4231_free(chip);
  1232. }
  1233. const char *snd_cs4231_chip_id(struct snd_cs4231 *chip)
  1234. {
  1235. switch (chip->hardware) {
  1236. case CS4231_HW_CS4231: return "CS4231";
  1237. case CS4231_HW_CS4231A: return "CS4231A";
  1238. case CS4231_HW_CS4232: return "CS4232";
  1239. case CS4231_HW_CS4232A: return "CS4232A";
  1240. case CS4231_HW_CS4235: return "CS4235";
  1241. case CS4231_HW_CS4236: return "CS4236";
  1242. case CS4231_HW_CS4236B: return "CS4236B";
  1243. case CS4231_HW_CS4237B: return "CS4237B";
  1244. case CS4231_HW_CS4238B: return "CS4238B";
  1245. case CS4231_HW_CS4239: return "CS4239";
  1246. case CS4231_HW_INTERWAVE: return "AMD InterWave";
  1247. case CS4231_HW_OPL3SA2: return chip->card->shortname;
  1248. case CS4231_HW_AD1845: return "AD1845";
  1249. default: return "???";
  1250. }
  1251. }
  1252. static int snd_cs4231_new(struct snd_card *card,
  1253. unsigned short hardware,
  1254. unsigned short hwshare,
  1255. struct snd_cs4231 ** rchip)
  1256. {
  1257. struct snd_cs4231 *chip;
  1258. *rchip = NULL;
  1259. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1260. if (chip == NULL)
  1261. return -ENOMEM;
  1262. chip->hardware = hardware;
  1263. chip->hwshare = hwshare;
  1264. spin_lock_init(&chip->reg_lock);
  1265. mutex_init(&chip->mce_mutex);
  1266. mutex_init(&chip->open_mutex);
  1267. chip->card = card;
  1268. chip->rate_constraint = snd_cs4231_xrate;
  1269. chip->set_playback_format = snd_cs4231_playback_format;
  1270. chip->set_capture_format = snd_cs4231_capture_format;
  1271. memcpy(&chip->image, &snd_cs4231_original_image, sizeof(snd_cs4231_original_image));
  1272. *rchip = chip;
  1273. return 0;
  1274. }
  1275. int snd_cs4231_create(struct snd_card *card,
  1276. unsigned long port,
  1277. unsigned long cport,
  1278. int irq, int dma1, int dma2,
  1279. unsigned short hardware,
  1280. unsigned short hwshare,
  1281. struct snd_cs4231 ** rchip)
  1282. {
  1283. static struct snd_device_ops ops = {
  1284. .dev_free = snd_cs4231_dev_free,
  1285. };
  1286. struct snd_cs4231 *chip;
  1287. int err;
  1288. err = snd_cs4231_new(card, hardware, hwshare, &chip);
  1289. if (err < 0)
  1290. return err;
  1291. chip->irq = -1;
  1292. chip->dma1 = -1;
  1293. chip->dma2 = -1;
  1294. if ((chip->res_port = request_region(port, 4, "CS4231")) == NULL) {
  1295. snd_printk(KERN_ERR "cs4231: can't grab port 0x%lx\n", port);
  1296. snd_cs4231_free(chip);
  1297. return -EBUSY;
  1298. }
  1299. chip->port = port;
  1300. if ((long)cport >= 0 && (chip->res_cport = request_region(cport, 8, "CS4232 Control")) == NULL) {
  1301. snd_printk(KERN_ERR "cs4231: can't grab control port 0x%lx\n", cport);
  1302. snd_cs4231_free(chip);
  1303. return -ENODEV;
  1304. }
  1305. chip->cport = cport;
  1306. if (!(hwshare & CS4231_HWSHARE_IRQ) && request_irq(irq, snd_cs4231_interrupt, IRQF_DISABLED, "CS4231", (void *) chip)) {
  1307. snd_printk(KERN_ERR "cs4231: can't grab IRQ %d\n", irq);
  1308. snd_cs4231_free(chip);
  1309. return -EBUSY;
  1310. }
  1311. chip->irq = irq;
  1312. if (!(hwshare & CS4231_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
  1313. snd_printk(KERN_ERR "cs4231: can't grab DMA1 %d\n", dma1);
  1314. snd_cs4231_free(chip);
  1315. return -EBUSY;
  1316. }
  1317. chip->dma1 = dma1;
  1318. if (!(hwshare & CS4231_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
  1319. snd_printk(KERN_ERR "cs4231: can't grab DMA2 %d\n", dma2);
  1320. snd_cs4231_free(chip);
  1321. return -EBUSY;
  1322. }
  1323. if (dma1 == dma2 || dma2 < 0) {
  1324. chip->single_dma = 1;
  1325. chip->dma2 = chip->dma1;
  1326. } else
  1327. chip->dma2 = dma2;
  1328. /* global setup */
  1329. if (snd_cs4231_probe(chip) < 0) {
  1330. snd_cs4231_free(chip);
  1331. return -ENODEV;
  1332. }
  1333. snd_cs4231_init(chip);
  1334. #if 0
  1335. if (chip->hardware & CS4231_HW_CS4232_MASK) {
  1336. if (chip->res_cport == NULL)
  1337. snd_printk("CS4232 control port features are not accessible\n");
  1338. }
  1339. #endif
  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. mutex_init(&chip->mce_mutex);
  1381. mutex_init(&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)