cs4231_lib.c 57 KB

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