es1938.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /*
  2. * Driver for ESS Solo-1 (ES1938, ES1946, ES1969) soundcard
  3. * Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
  4. * Jaroslav Kysela <perex@suse.cz>,
  5. * Thomas Sailer <sailer@ife.ee.ethz.ch>,
  6. * Abramo Bagnara <abramo@alsa-project.org>,
  7. * Markus Gruber <gruber@eikon.tum.de>
  8. *
  9. * Rewritten from sonicvibes.c source.
  10. *
  11. * TODO:
  12. * Rewrite better spinlocks
  13. *
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. /*
  31. NOTES:
  32. - Capture data is written unaligned starting from dma_base + 1 so I need to
  33. disable mmap and to add a copy callback.
  34. - After several cycle of the following:
  35. while : ; do arecord -d1 -f cd -t raw | aplay -f cd ; done
  36. a "playback write error (DMA or IRQ trouble?)" may happen.
  37. This is due to playback interrupts not generated.
  38. I suspect a timing issue.
  39. - Sometimes the interrupt handler is invoked wrongly during playback.
  40. This generates some harmless "Unexpected hw_pointer: wrong interrupt
  41. acknowledge".
  42. I've seen that using small period sizes.
  43. Reproducible with:
  44. mpg123 test.mp3 &
  45. hdparm -t -T /dev/hda
  46. */
  47. #include <sound/driver.h>
  48. #include <linux/init.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/pci.h>
  51. #include <linux/slab.h>
  52. #include <linux/gameport.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/delay.h>
  55. #include <sound/core.h>
  56. #include <sound/control.h>
  57. #include <sound/pcm.h>
  58. #include <sound/opl3.h>
  59. #include <sound/mpu401.h>
  60. #include <sound/initval.h>
  61. #include <asm/io.h>
  62. MODULE_AUTHOR("Jaromir Koutek <miri@punknet.cz>");
  63. MODULE_DESCRIPTION("ESS Solo-1");
  64. MODULE_LICENSE("GPL");
  65. MODULE_SUPPORTED_DEVICE("{{ESS,ES1938},"
  66. "{ESS,ES1946},"
  67. "{ESS,ES1969},"
  68. "{TerraTec,128i PCI}}");
  69. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  70. #define SUPPORT_JOYSTICK 1
  71. #endif
  72. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  73. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  74. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  75. module_param_array(index, int, NULL, 0444);
  76. MODULE_PARM_DESC(index, "Index value for ESS Solo-1 soundcard.");
  77. module_param_array(id, charp, NULL, 0444);
  78. MODULE_PARM_DESC(id, "ID string for ESS Solo-1 soundcard.");
  79. module_param_array(enable, bool, NULL, 0444);
  80. MODULE_PARM_DESC(enable, "Enable ESS Solo-1 soundcard.");
  81. #define SLIO_REG(chip, x) ((chip)->io_port + ESSIO_REG_##x)
  82. #define SLDM_REG(chip, x) ((chip)->ddma_port + ESSDM_REG_##x)
  83. #define SLSB_REG(chip, x) ((chip)->sb_port + ESSSB_REG_##x)
  84. #define SL_PCI_LEGACYCONTROL 0x40
  85. #define SL_PCI_CONFIG 0x50
  86. #define SL_PCI_DDMACONTROL 0x60
  87. #define ESSIO_REG_AUDIO2DMAADDR 0
  88. #define ESSIO_REG_AUDIO2DMACOUNT 4
  89. #define ESSIO_REG_AUDIO2MODE 6
  90. #define ESSIO_REG_IRQCONTROL 7
  91. #define ESSDM_REG_DMAADDR 0x00
  92. #define ESSDM_REG_DMACOUNT 0x04
  93. #define ESSDM_REG_DMACOMMAND 0x08
  94. #define ESSDM_REG_DMASTATUS 0x08
  95. #define ESSDM_REG_DMAMODE 0x0b
  96. #define ESSDM_REG_DMACLEAR 0x0d
  97. #define ESSDM_REG_DMAMASK 0x0f
  98. #define ESSSB_REG_FMLOWADDR 0x00
  99. #define ESSSB_REG_FMHIGHADDR 0x02
  100. #define ESSSB_REG_MIXERADDR 0x04
  101. #define ESSSB_REG_MIXERDATA 0x05
  102. #define ESSSB_IREG_AUDIO1 0x14
  103. #define ESSSB_IREG_MICMIX 0x1a
  104. #define ESSSB_IREG_RECSRC 0x1c
  105. #define ESSSB_IREG_MASTER 0x32
  106. #define ESSSB_IREG_FM 0x36
  107. #define ESSSB_IREG_AUXACD 0x38
  108. #define ESSSB_IREG_AUXB 0x3a
  109. #define ESSSB_IREG_PCSPEAKER 0x3c
  110. #define ESSSB_IREG_LINE 0x3e
  111. #define ESSSB_IREG_SPATCONTROL 0x50
  112. #define ESSSB_IREG_SPATLEVEL 0x52
  113. #define ESSSB_IREG_MASTER_LEFT 0x60
  114. #define ESSSB_IREG_MASTER_RIGHT 0x62
  115. #define ESSSB_IREG_MPU401CONTROL 0x64
  116. #define ESSSB_IREG_MICMIXRECORD 0x68
  117. #define ESSSB_IREG_AUDIO2RECORD 0x69
  118. #define ESSSB_IREG_AUXACDRECORD 0x6a
  119. #define ESSSB_IREG_FMRECORD 0x6b
  120. #define ESSSB_IREG_AUXBRECORD 0x6c
  121. #define ESSSB_IREG_MONO 0x6d
  122. #define ESSSB_IREG_LINERECORD 0x6e
  123. #define ESSSB_IREG_MONORECORD 0x6f
  124. #define ESSSB_IREG_AUDIO2SAMPLE 0x70
  125. #define ESSSB_IREG_AUDIO2MODE 0x71
  126. #define ESSSB_IREG_AUDIO2FILTER 0x72
  127. #define ESSSB_IREG_AUDIO2TCOUNTL 0x74
  128. #define ESSSB_IREG_AUDIO2TCOUNTH 0x76
  129. #define ESSSB_IREG_AUDIO2CONTROL1 0x78
  130. #define ESSSB_IREG_AUDIO2CONTROL2 0x7a
  131. #define ESSSB_IREG_AUDIO2 0x7c
  132. #define ESSSB_REG_RESET 0x06
  133. #define ESSSB_REG_READDATA 0x0a
  134. #define ESSSB_REG_WRITEDATA 0x0c
  135. #define ESSSB_REG_READSTATUS 0x0c
  136. #define ESSSB_REG_STATUS 0x0e
  137. #define ESS_CMD_EXTSAMPLERATE 0xa1
  138. #define ESS_CMD_FILTERDIV 0xa2
  139. #define ESS_CMD_DMACNTRELOADL 0xa4
  140. #define ESS_CMD_DMACNTRELOADH 0xa5
  141. #define ESS_CMD_ANALOGCONTROL 0xa8
  142. #define ESS_CMD_IRQCONTROL 0xb1
  143. #define ESS_CMD_DRQCONTROL 0xb2
  144. #define ESS_CMD_RECLEVEL 0xb4
  145. #define ESS_CMD_SETFORMAT 0xb6
  146. #define ESS_CMD_SETFORMAT2 0xb7
  147. #define ESS_CMD_DMACONTROL 0xb8
  148. #define ESS_CMD_DMATYPE 0xb9
  149. #define ESS_CMD_OFFSETLEFT 0xba
  150. #define ESS_CMD_OFFSETRIGHT 0xbb
  151. #define ESS_CMD_READREG 0xc0
  152. #define ESS_CMD_ENABLEEXT 0xc6
  153. #define ESS_CMD_PAUSEDMA 0xd0
  154. #define ESS_CMD_ENABLEAUDIO1 0xd1
  155. #define ESS_CMD_STOPAUDIO1 0xd3
  156. #define ESS_CMD_AUDIO1STATUS 0xd8
  157. #define ESS_CMD_CONTDMA 0xd4
  158. #define ESS_CMD_TESTIRQ 0xf2
  159. #define ESS_RECSRC_MIC 0
  160. #define ESS_RECSRC_AUXACD 2
  161. #define ESS_RECSRC_AUXB 5
  162. #define ESS_RECSRC_LINE 6
  163. #define ESS_RECSRC_NONE 7
  164. #define DAC1 0x01
  165. #define ADC1 0x02
  166. #define DAC2 0x04
  167. /*
  168. */
  169. #define SAVED_REG_SIZE 32 /* max. number of registers to save */
  170. struct es1938 {
  171. int irq;
  172. unsigned long io_port;
  173. unsigned long sb_port;
  174. unsigned long vc_port;
  175. unsigned long mpu_port;
  176. unsigned long game_port;
  177. unsigned long ddma_port;
  178. unsigned char irqmask;
  179. unsigned char revision;
  180. struct snd_kcontrol *hw_volume;
  181. struct snd_kcontrol *hw_switch;
  182. struct snd_kcontrol *master_volume;
  183. struct snd_kcontrol *master_switch;
  184. struct pci_dev *pci;
  185. struct snd_card *card;
  186. struct snd_pcm *pcm;
  187. struct snd_pcm_substream *capture_substream;
  188. struct snd_pcm_substream *playback1_substream;
  189. struct snd_pcm_substream *playback2_substream;
  190. struct snd_rawmidi *rmidi;
  191. unsigned int dma1_size;
  192. unsigned int dma2_size;
  193. unsigned int dma1_start;
  194. unsigned int dma2_start;
  195. unsigned int dma1_shift;
  196. unsigned int dma2_shift;
  197. unsigned int active;
  198. spinlock_t reg_lock;
  199. spinlock_t mixer_lock;
  200. struct snd_info_entry *proc_entry;
  201. #ifdef SUPPORT_JOYSTICK
  202. struct gameport *gameport;
  203. #endif
  204. #ifdef CONFIG_PM
  205. unsigned char saved_regs[SAVED_REG_SIZE];
  206. #endif
  207. };
  208. static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  209. static struct pci_device_id snd_es1938_ids[] = {
  210. { 0x125d, 0x1969, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* Solo-1 */
  211. { 0, }
  212. };
  213. MODULE_DEVICE_TABLE(pci, snd_es1938_ids);
  214. #define RESET_LOOP_TIMEOUT 0x10000
  215. #define WRITE_LOOP_TIMEOUT 0x10000
  216. #define GET_LOOP_TIMEOUT 0x01000
  217. #undef REG_DEBUG
  218. /* -----------------------------------------------------------------
  219. * Write to a mixer register
  220. * -----------------------------------------------------------------*/
  221. static void snd_es1938_mixer_write(struct es1938 *chip, unsigned char reg, unsigned char val)
  222. {
  223. unsigned long flags;
  224. spin_lock_irqsave(&chip->mixer_lock, flags);
  225. outb(reg, SLSB_REG(chip, MIXERADDR));
  226. outb(val, SLSB_REG(chip, MIXERDATA));
  227. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  228. #ifdef REG_DEBUG
  229. snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, val);
  230. #endif
  231. }
  232. /* -----------------------------------------------------------------
  233. * Read from a mixer register
  234. * -----------------------------------------------------------------*/
  235. static int snd_es1938_mixer_read(struct es1938 *chip, unsigned char reg)
  236. {
  237. int data;
  238. unsigned long flags;
  239. spin_lock_irqsave(&chip->mixer_lock, flags);
  240. outb(reg, SLSB_REG(chip, MIXERADDR));
  241. data = inb(SLSB_REG(chip, MIXERDATA));
  242. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  243. #ifdef REG_DEBUG
  244. snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
  245. #endif
  246. return data;
  247. }
  248. /* -----------------------------------------------------------------
  249. * Write to some bits of a mixer register (return old value)
  250. * -----------------------------------------------------------------*/
  251. static int snd_es1938_mixer_bits(struct es1938 *chip, unsigned char reg,
  252. unsigned char mask, unsigned char val)
  253. {
  254. unsigned long flags;
  255. unsigned char old, new, oval;
  256. spin_lock_irqsave(&chip->mixer_lock, flags);
  257. outb(reg, SLSB_REG(chip, MIXERADDR));
  258. old = inb(SLSB_REG(chip, MIXERDATA));
  259. oval = old & mask;
  260. if (val != oval) {
  261. new = (old & ~mask) | (val & mask);
  262. outb(new, SLSB_REG(chip, MIXERDATA));
  263. #ifdef REG_DEBUG
  264. snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
  265. reg, old, new);
  266. #endif
  267. }
  268. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  269. return oval;
  270. }
  271. /* -----------------------------------------------------------------
  272. * Write command to Controller Registers
  273. * -----------------------------------------------------------------*/
  274. static void snd_es1938_write_cmd(struct es1938 *chip, unsigned char cmd)
  275. {
  276. int i;
  277. unsigned char v;
  278. for (i = 0; i < WRITE_LOOP_TIMEOUT; i++) {
  279. if (!(v = inb(SLSB_REG(chip, READSTATUS)) & 0x80)) {
  280. outb(cmd, SLSB_REG(chip, WRITEDATA));
  281. return;
  282. }
  283. }
  284. printk(KERN_ERR "snd_es1938_write_cmd timeout (0x02%x/0x02%x)\n", cmd, v);
  285. }
  286. /* -----------------------------------------------------------------
  287. * Read the Read Data Buffer
  288. * -----------------------------------------------------------------*/
  289. static int snd_es1938_get_byte(struct es1938 *chip)
  290. {
  291. int i;
  292. unsigned char v;
  293. for (i = GET_LOOP_TIMEOUT; i; i--)
  294. if ((v = inb(SLSB_REG(chip, STATUS))) & 0x80)
  295. return inb(SLSB_REG(chip, READDATA));
  296. snd_printk(KERN_ERR "get_byte timeout: status 0x02%x\n", v);
  297. return -ENODEV;
  298. }
  299. /* -----------------------------------------------------------------
  300. * Write value cmd register
  301. * -----------------------------------------------------------------*/
  302. static void snd_es1938_write(struct es1938 *chip, unsigned char reg, unsigned char val)
  303. {
  304. unsigned long flags;
  305. spin_lock_irqsave(&chip->reg_lock, flags);
  306. snd_es1938_write_cmd(chip, reg);
  307. snd_es1938_write_cmd(chip, val);
  308. spin_unlock_irqrestore(&chip->reg_lock, flags);
  309. #ifdef REG_DEBUG
  310. snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, val);
  311. #endif
  312. }
  313. /* -----------------------------------------------------------------
  314. * Read data from cmd register and return it
  315. * -----------------------------------------------------------------*/
  316. static unsigned char snd_es1938_read(struct es1938 *chip, unsigned char reg)
  317. {
  318. unsigned char val;
  319. unsigned long flags;
  320. spin_lock_irqsave(&chip->reg_lock, flags);
  321. snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  322. snd_es1938_write_cmd(chip, reg);
  323. val = snd_es1938_get_byte(chip);
  324. spin_unlock_irqrestore(&chip->reg_lock, flags);
  325. #ifdef REG_DEBUG
  326. snd_printk(KERN_DEBUG "Reg %02x now is %02x\n", reg, val);
  327. #endif
  328. return val;
  329. }
  330. /* -----------------------------------------------------------------
  331. * Write data to cmd register and return old value
  332. * -----------------------------------------------------------------*/
  333. static int snd_es1938_bits(struct es1938 *chip, unsigned char reg, unsigned char mask,
  334. unsigned char val)
  335. {
  336. unsigned long flags;
  337. unsigned char old, new, oval;
  338. spin_lock_irqsave(&chip->reg_lock, flags);
  339. snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  340. snd_es1938_write_cmd(chip, reg);
  341. old = snd_es1938_get_byte(chip);
  342. oval = old & mask;
  343. if (val != oval) {
  344. snd_es1938_write_cmd(chip, reg);
  345. new = (old & ~mask) | (val & mask);
  346. snd_es1938_write_cmd(chip, new);
  347. #ifdef REG_DEBUG
  348. snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x\n",
  349. reg, old, new);
  350. #endif
  351. }
  352. spin_unlock_irqrestore(&chip->reg_lock, flags);
  353. return oval;
  354. }
  355. /* --------------------------------------------------------------------
  356. * Reset the chip
  357. * --------------------------------------------------------------------*/
  358. static void snd_es1938_reset(struct es1938 *chip)
  359. {
  360. int i;
  361. outb(3, SLSB_REG(chip, RESET));
  362. inb(SLSB_REG(chip, RESET));
  363. outb(0, SLSB_REG(chip, RESET));
  364. for (i = 0; i < RESET_LOOP_TIMEOUT; i++) {
  365. if (inb(SLSB_REG(chip, STATUS)) & 0x80) {
  366. if (inb(SLSB_REG(chip, READDATA)) == 0xaa)
  367. goto __next;
  368. }
  369. }
  370. snd_printk(KERN_ERR "ESS Solo-1 reset failed\n");
  371. __next:
  372. snd_es1938_write_cmd(chip, ESS_CMD_ENABLEEXT);
  373. /* Demand transfer DMA: 4 bytes per DMA request */
  374. snd_es1938_write(chip, ESS_CMD_DMATYPE, 2);
  375. /* Change behaviour of register A1
  376. 4x oversampling
  377. 2nd channel DAC asynchronous */
  378. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2MODE, 0x32);
  379. /* enable/select DMA channel and IRQ channel */
  380. snd_es1938_bits(chip, ESS_CMD_IRQCONTROL, 0xf0, 0x50);
  381. snd_es1938_bits(chip, ESS_CMD_DRQCONTROL, 0xf0, 0x50);
  382. snd_es1938_write_cmd(chip, ESS_CMD_ENABLEAUDIO1);
  383. /* Set spatializer parameters to recommended values */
  384. snd_es1938_mixer_write(chip, 0x54, 0x8f);
  385. snd_es1938_mixer_write(chip, 0x56, 0x95);
  386. snd_es1938_mixer_write(chip, 0x58, 0x94);
  387. snd_es1938_mixer_write(chip, 0x5a, 0x80);
  388. }
  389. /* --------------------------------------------------------------------
  390. * Reset the FIFOs
  391. * --------------------------------------------------------------------*/
  392. static void snd_es1938_reset_fifo(struct es1938 *chip)
  393. {
  394. outb(2, SLSB_REG(chip, RESET));
  395. outb(0, SLSB_REG(chip, RESET));
  396. }
  397. static struct snd_ratnum clocks[2] = {
  398. {
  399. .num = 793800,
  400. .den_min = 1,
  401. .den_max = 128,
  402. .den_step = 1,
  403. },
  404. {
  405. .num = 768000,
  406. .den_min = 1,
  407. .den_max = 128,
  408. .den_step = 1,
  409. }
  410. };
  411. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  412. .nrats = 2,
  413. .rats = clocks,
  414. };
  415. static void snd_es1938_rate_set(struct es1938 *chip,
  416. struct snd_pcm_substream *substream,
  417. int mode)
  418. {
  419. unsigned int bits, div0;
  420. struct snd_pcm_runtime *runtime = substream->runtime;
  421. if (runtime->rate_num == clocks[0].num)
  422. bits = 128 - runtime->rate_den;
  423. else
  424. bits = 256 - runtime->rate_den;
  425. /* set filter register */
  426. div0 = 256 - 7160000*20/(8*82*runtime->rate);
  427. if (mode == DAC2) {
  428. snd_es1938_mixer_write(chip, 0x70, bits);
  429. snd_es1938_mixer_write(chip, 0x72, div0);
  430. } else {
  431. snd_es1938_write(chip, 0xA1, bits);
  432. snd_es1938_write(chip, 0xA2, div0);
  433. }
  434. }
  435. /* --------------------------------------------------------------------
  436. * Configure Solo1 builtin DMA Controller
  437. * --------------------------------------------------------------------*/
  438. static void snd_es1938_playback1_setdma(struct es1938 *chip)
  439. {
  440. outb(0x00, SLIO_REG(chip, AUDIO2MODE));
  441. outl(chip->dma2_start, SLIO_REG(chip, AUDIO2DMAADDR));
  442. outw(0, SLIO_REG(chip, AUDIO2DMACOUNT));
  443. outw(chip->dma2_size, SLIO_REG(chip, AUDIO2DMACOUNT));
  444. }
  445. static void snd_es1938_playback2_setdma(struct es1938 *chip)
  446. {
  447. /* Enable DMA controller */
  448. outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  449. /* 1. Master reset */
  450. outb(0, SLDM_REG(chip, DMACLEAR));
  451. /* 2. Mask DMA */
  452. outb(1, SLDM_REG(chip, DMAMASK));
  453. outb(0x18, SLDM_REG(chip, DMAMODE));
  454. outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
  455. outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  456. /* 3. Unmask DMA */
  457. outb(0, SLDM_REG(chip, DMAMASK));
  458. }
  459. static void snd_es1938_capture_setdma(struct es1938 *chip)
  460. {
  461. /* Enable DMA controller */
  462. outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  463. /* 1. Master reset */
  464. outb(0, SLDM_REG(chip, DMACLEAR));
  465. /* 2. Mask DMA */
  466. outb(1, SLDM_REG(chip, DMAMASK));
  467. outb(0x14, SLDM_REG(chip, DMAMODE));
  468. outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
  469. outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  470. /* 3. Unmask DMA */
  471. outb(0, SLDM_REG(chip, DMAMASK));
  472. }
  473. /* ----------------------------------------------------------------------
  474. *
  475. * *** PCM part ***
  476. */
  477. static int snd_es1938_capture_trigger(struct snd_pcm_substream *substream,
  478. int cmd)
  479. {
  480. struct es1938 *chip = snd_pcm_substream_chip(substream);
  481. int val;
  482. switch (cmd) {
  483. case SNDRV_PCM_TRIGGER_START:
  484. case SNDRV_PCM_TRIGGER_RESUME:
  485. val = 0x0f;
  486. chip->active |= ADC1;
  487. break;
  488. case SNDRV_PCM_TRIGGER_STOP:
  489. case SNDRV_PCM_TRIGGER_SUSPEND:
  490. val = 0x00;
  491. chip->active &= ~ADC1;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  497. return 0;
  498. }
  499. static int snd_es1938_playback1_trigger(struct snd_pcm_substream *substream,
  500. int cmd)
  501. {
  502. struct es1938 *chip = snd_pcm_substream_chip(substream);
  503. switch (cmd) {
  504. case SNDRV_PCM_TRIGGER_START:
  505. case SNDRV_PCM_TRIGGER_RESUME:
  506. /* According to the documentation this should be:
  507. 0x13 but that value may randomly swap stereo channels */
  508. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x92);
  509. udelay(10);
  510. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x93);
  511. /* This two stage init gives the FIFO -> DAC connection time to
  512. * settle before first data from DMA flows in. This should ensure
  513. * no swapping of stereo channels. Report a bug if otherwise :-) */
  514. outb(0x0a, SLIO_REG(chip, AUDIO2MODE));
  515. chip->active |= DAC2;
  516. break;
  517. case SNDRV_PCM_TRIGGER_STOP:
  518. case SNDRV_PCM_TRIGGER_SUSPEND:
  519. outb(0, SLIO_REG(chip, AUDIO2MODE));
  520. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0);
  521. chip->active &= ~DAC2;
  522. break;
  523. default:
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. static int snd_es1938_playback2_trigger(struct snd_pcm_substream *substream,
  529. int cmd)
  530. {
  531. struct es1938 *chip = snd_pcm_substream_chip(substream);
  532. int val;
  533. switch (cmd) {
  534. case SNDRV_PCM_TRIGGER_START:
  535. case SNDRV_PCM_TRIGGER_RESUME:
  536. val = 5;
  537. chip->active |= DAC1;
  538. break;
  539. case SNDRV_PCM_TRIGGER_STOP:
  540. case SNDRV_PCM_TRIGGER_SUSPEND:
  541. val = 0;
  542. chip->active &= ~DAC1;
  543. break;
  544. default:
  545. return -EINVAL;
  546. }
  547. snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  548. return 0;
  549. }
  550. static int snd_es1938_playback_trigger(struct snd_pcm_substream *substream,
  551. int cmd)
  552. {
  553. switch (substream->number) {
  554. case 0:
  555. return snd_es1938_playback1_trigger(substream, cmd);
  556. case 1:
  557. return snd_es1938_playback2_trigger(substream, cmd);
  558. }
  559. snd_BUG();
  560. return -EINVAL;
  561. }
  562. /* --------------------------------------------------------------------
  563. * First channel for Extended Mode Audio 1 ADC Operation
  564. * --------------------------------------------------------------------*/
  565. static int snd_es1938_capture_prepare(struct snd_pcm_substream *substream)
  566. {
  567. struct es1938 *chip = snd_pcm_substream_chip(substream);
  568. struct snd_pcm_runtime *runtime = substream->runtime;
  569. int u, is8, mono;
  570. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  571. unsigned int count = snd_pcm_lib_period_bytes(substream);
  572. chip->dma1_size = size;
  573. chip->dma1_start = runtime->dma_addr;
  574. mono = (runtime->channels > 1) ? 0 : 1;
  575. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  576. u = snd_pcm_format_unsigned(runtime->format);
  577. chip->dma1_shift = 2 - mono - is8;
  578. snd_es1938_reset_fifo(chip);
  579. /* program type */
  580. snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  581. /* set clock and counters */
  582. snd_es1938_rate_set(chip, substream, ADC1);
  583. count = 0x10000 - count;
  584. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  585. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  586. /* initialize and configure ADC */
  587. snd_es1938_write(chip, ESS_CMD_SETFORMAT2, u ? 0x51 : 0x71);
  588. snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 0x90 |
  589. (u ? 0x00 : 0x20) |
  590. (is8 ? 0x00 : 0x04) |
  591. (mono ? 0x40 : 0x08));
  592. // snd_es1938_reset_fifo(chip);
  593. /* 11. configure system interrupt controller and DMA controller */
  594. snd_es1938_capture_setdma(chip);
  595. return 0;
  596. }
  597. /* ------------------------------------------------------------------------------
  598. * Second Audio channel DAC Operation
  599. * ------------------------------------------------------------------------------*/
  600. static int snd_es1938_playback1_prepare(struct snd_pcm_substream *substream)
  601. {
  602. struct es1938 *chip = snd_pcm_substream_chip(substream);
  603. struct snd_pcm_runtime *runtime = substream->runtime;
  604. int u, is8, mono;
  605. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  606. unsigned int count = snd_pcm_lib_period_bytes(substream);
  607. chip->dma2_size = size;
  608. chip->dma2_start = runtime->dma_addr;
  609. mono = (runtime->channels > 1) ? 0 : 1;
  610. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  611. u = snd_pcm_format_unsigned(runtime->format);
  612. chip->dma2_shift = 2 - mono - is8;
  613. snd_es1938_reset_fifo(chip);
  614. /* set clock and counters */
  615. snd_es1938_rate_set(chip, substream, DAC2);
  616. count >>= 1;
  617. count = 0x10000 - count;
  618. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTL, count & 0xff);
  619. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTH, count >> 8);
  620. /* initialize and configure Audio 2 DAC */
  621. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x40 | (u ? 0 : 4) |
  622. (mono ? 0 : 2) | (is8 ? 0 : 1));
  623. /* program DMA */
  624. snd_es1938_playback1_setdma(chip);
  625. return 0;
  626. }
  627. static int snd_es1938_playback2_prepare(struct snd_pcm_substream *substream)
  628. {
  629. struct es1938 *chip = snd_pcm_substream_chip(substream);
  630. struct snd_pcm_runtime *runtime = substream->runtime;
  631. int u, is8, mono;
  632. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  633. unsigned int count = snd_pcm_lib_period_bytes(substream);
  634. chip->dma1_size = size;
  635. chip->dma1_start = runtime->dma_addr;
  636. mono = (runtime->channels > 1) ? 0 : 1;
  637. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  638. u = snd_pcm_format_unsigned(runtime->format);
  639. chip->dma1_shift = 2 - mono - is8;
  640. count = 0x10000 - count;
  641. /* reset */
  642. snd_es1938_reset_fifo(chip);
  643. snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  644. /* set clock and counters */
  645. snd_es1938_rate_set(chip, substream, DAC1);
  646. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  647. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  648. /* initialized and configure DAC */
  649. snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x80 : 0x00);
  650. snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x51 : 0x71);
  651. snd_es1938_write(chip, ESS_CMD_SETFORMAT2,
  652. 0x90 | (mono ? 0x40 : 0x08) |
  653. (is8 ? 0x00 : 0x04) | (u ? 0x00 : 0x20));
  654. /* program DMA */
  655. snd_es1938_playback2_setdma(chip);
  656. return 0;
  657. }
  658. static int snd_es1938_playback_prepare(struct snd_pcm_substream *substream)
  659. {
  660. switch (substream->number) {
  661. case 0:
  662. return snd_es1938_playback1_prepare(substream);
  663. case 1:
  664. return snd_es1938_playback2_prepare(substream);
  665. }
  666. snd_BUG();
  667. return -EINVAL;
  668. }
  669. static snd_pcm_uframes_t snd_es1938_capture_pointer(struct snd_pcm_substream *substream)
  670. {
  671. struct es1938 *chip = snd_pcm_substream_chip(substream);
  672. size_t ptr;
  673. size_t old, new;
  674. #if 1
  675. /* This stuff is *needed*, don't ask why - AB */
  676. old = inw(SLDM_REG(chip, DMACOUNT));
  677. while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  678. old = new;
  679. ptr = chip->dma1_size - 1 - new;
  680. #else
  681. ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
  682. #endif
  683. return ptr >> chip->dma1_shift;
  684. }
  685. static snd_pcm_uframes_t snd_es1938_playback1_pointer(struct snd_pcm_substream *substream)
  686. {
  687. struct es1938 *chip = snd_pcm_substream_chip(substream);
  688. size_t ptr;
  689. #if 1
  690. ptr = chip->dma2_size - inw(SLIO_REG(chip, AUDIO2DMACOUNT));
  691. #else
  692. ptr = inl(SLIO_REG(chip, AUDIO2DMAADDR)) - chip->dma2_start;
  693. #endif
  694. return ptr >> chip->dma2_shift;
  695. }
  696. static snd_pcm_uframes_t snd_es1938_playback2_pointer(struct snd_pcm_substream *substream)
  697. {
  698. struct es1938 *chip = snd_pcm_substream_chip(substream);
  699. size_t ptr;
  700. size_t old, new;
  701. #if 1
  702. /* This stuff is *needed*, don't ask why - AB */
  703. old = inw(SLDM_REG(chip, DMACOUNT));
  704. while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  705. old = new;
  706. ptr = chip->dma1_size - 1 - new;
  707. #else
  708. ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
  709. #endif
  710. return ptr >> chip->dma1_shift;
  711. }
  712. static snd_pcm_uframes_t snd_es1938_playback_pointer(struct snd_pcm_substream *substream)
  713. {
  714. switch (substream->number) {
  715. case 0:
  716. return snd_es1938_playback1_pointer(substream);
  717. case 1:
  718. return snd_es1938_playback2_pointer(substream);
  719. }
  720. snd_BUG();
  721. return -EINVAL;
  722. }
  723. static int snd_es1938_capture_copy(struct snd_pcm_substream *substream,
  724. int channel,
  725. snd_pcm_uframes_t pos,
  726. void __user *dst,
  727. snd_pcm_uframes_t count)
  728. {
  729. struct snd_pcm_runtime *runtime = substream->runtime;
  730. struct es1938 *chip = snd_pcm_substream_chip(substream);
  731. pos <<= chip->dma1_shift;
  732. count <<= chip->dma1_shift;
  733. snd_assert(pos + count <= chip->dma1_size, return -EINVAL);
  734. if (pos + count < chip->dma1_size) {
  735. if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
  736. return -EFAULT;
  737. } else {
  738. if (copy_to_user(dst, runtime->dma_area + pos + 1, count - 1))
  739. return -EFAULT;
  740. if (put_user(runtime->dma_area[0], ((unsigned char __user *)dst) + count - 1))
  741. return -EFAULT;
  742. }
  743. return 0;
  744. }
  745. /*
  746. * buffer management
  747. */
  748. static int snd_es1938_pcm_hw_params(struct snd_pcm_substream *substream,
  749. struct snd_pcm_hw_params *hw_params)
  750. {
  751. int err;
  752. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  753. return err;
  754. return 0;
  755. }
  756. static int snd_es1938_pcm_hw_free(struct snd_pcm_substream *substream)
  757. {
  758. return snd_pcm_lib_free_pages(substream);
  759. }
  760. /* ----------------------------------------------------------------------
  761. * Audio1 Capture (ADC)
  762. * ----------------------------------------------------------------------*/
  763. static struct snd_pcm_hardware snd_es1938_capture =
  764. {
  765. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  766. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  767. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  768. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  769. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  770. .rate_min = 6000,
  771. .rate_max = 48000,
  772. .channels_min = 1,
  773. .channels_max = 2,
  774. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  775. .period_bytes_min = 64,
  776. .period_bytes_max = 0x8000,
  777. .periods_min = 1,
  778. .periods_max = 1024,
  779. .fifo_size = 256,
  780. };
  781. /* -----------------------------------------------------------------------
  782. * Audio2 Playback (DAC)
  783. * -----------------------------------------------------------------------*/
  784. static struct snd_pcm_hardware snd_es1938_playback =
  785. {
  786. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  787. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  788. SNDRV_PCM_INFO_MMAP_VALID),
  789. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  790. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  791. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  792. .rate_min = 6000,
  793. .rate_max = 48000,
  794. .channels_min = 1,
  795. .channels_max = 2,
  796. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  797. .period_bytes_min = 64,
  798. .period_bytes_max = 0x8000,
  799. .periods_min = 1,
  800. .periods_max = 1024,
  801. .fifo_size = 256,
  802. };
  803. static int snd_es1938_capture_open(struct snd_pcm_substream *substream)
  804. {
  805. struct es1938 *chip = snd_pcm_substream_chip(substream);
  806. struct snd_pcm_runtime *runtime = substream->runtime;
  807. if (chip->playback2_substream)
  808. return -EAGAIN;
  809. chip->capture_substream = substream;
  810. runtime->hw = snd_es1938_capture;
  811. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  812. &hw_constraints_clocks);
  813. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  814. return 0;
  815. }
  816. static int snd_es1938_playback_open(struct snd_pcm_substream *substream)
  817. {
  818. struct es1938 *chip = snd_pcm_substream_chip(substream);
  819. struct snd_pcm_runtime *runtime = substream->runtime;
  820. switch (substream->number) {
  821. case 0:
  822. chip->playback1_substream = substream;
  823. break;
  824. case 1:
  825. if (chip->capture_substream)
  826. return -EAGAIN;
  827. chip->playback2_substream = substream;
  828. break;
  829. default:
  830. snd_BUG();
  831. return -EINVAL;
  832. }
  833. runtime->hw = snd_es1938_playback;
  834. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  835. &hw_constraints_clocks);
  836. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  837. return 0;
  838. }
  839. static int snd_es1938_capture_close(struct snd_pcm_substream *substream)
  840. {
  841. struct es1938 *chip = snd_pcm_substream_chip(substream);
  842. chip->capture_substream = NULL;
  843. return 0;
  844. }
  845. static int snd_es1938_playback_close(struct snd_pcm_substream *substream)
  846. {
  847. struct es1938 *chip = snd_pcm_substream_chip(substream);
  848. switch (substream->number) {
  849. case 0:
  850. chip->playback1_substream = NULL;
  851. break;
  852. case 1:
  853. chip->playback2_substream = NULL;
  854. break;
  855. default:
  856. snd_BUG();
  857. return -EINVAL;
  858. }
  859. return 0;
  860. }
  861. static struct snd_pcm_ops snd_es1938_playback_ops = {
  862. .open = snd_es1938_playback_open,
  863. .close = snd_es1938_playback_close,
  864. .ioctl = snd_pcm_lib_ioctl,
  865. .hw_params = snd_es1938_pcm_hw_params,
  866. .hw_free = snd_es1938_pcm_hw_free,
  867. .prepare = snd_es1938_playback_prepare,
  868. .trigger = snd_es1938_playback_trigger,
  869. .pointer = snd_es1938_playback_pointer,
  870. };
  871. static struct snd_pcm_ops snd_es1938_capture_ops = {
  872. .open = snd_es1938_capture_open,
  873. .close = snd_es1938_capture_close,
  874. .ioctl = snd_pcm_lib_ioctl,
  875. .hw_params = snd_es1938_pcm_hw_params,
  876. .hw_free = snd_es1938_pcm_hw_free,
  877. .prepare = snd_es1938_capture_prepare,
  878. .trigger = snd_es1938_capture_trigger,
  879. .pointer = snd_es1938_capture_pointer,
  880. .copy = snd_es1938_capture_copy,
  881. };
  882. static int __devinit snd_es1938_new_pcm(struct es1938 *chip, int device)
  883. {
  884. struct snd_pcm *pcm;
  885. int err;
  886. if ((err = snd_pcm_new(chip->card, "es-1938-1946", device, 2, 1, &pcm)) < 0)
  887. return err;
  888. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1938_playback_ops);
  889. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1938_capture_ops);
  890. pcm->private_data = chip;
  891. pcm->info_flags = 0;
  892. strcpy(pcm->name, "ESS Solo-1");
  893. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  894. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  895. chip->pcm = pcm;
  896. return 0;
  897. }
  898. /* -------------------------------------------------------------------
  899. *
  900. * *** Mixer part ***
  901. */
  902. static int snd_es1938_info_mux(struct snd_kcontrol *kcontrol,
  903. struct snd_ctl_elem_info *uinfo)
  904. {
  905. static char *texts[8] = {
  906. "Mic", "Mic Master", "CD", "AOUT",
  907. "Mic1", "Mix", "Line", "Master"
  908. };
  909. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  910. uinfo->count = 1;
  911. uinfo->value.enumerated.items = 8;
  912. if (uinfo->value.enumerated.item > 7)
  913. uinfo->value.enumerated.item = 7;
  914. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  915. return 0;
  916. }
  917. static int snd_es1938_get_mux(struct snd_kcontrol *kcontrol,
  918. struct snd_ctl_elem_value *ucontrol)
  919. {
  920. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  921. ucontrol->value.enumerated.item[0] = snd_es1938_mixer_read(chip, 0x1c) & 0x07;
  922. return 0;
  923. }
  924. static int snd_es1938_put_mux(struct snd_kcontrol *kcontrol,
  925. struct snd_ctl_elem_value *ucontrol)
  926. {
  927. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  928. unsigned char val = ucontrol->value.enumerated.item[0];
  929. if (val > 7)
  930. return -EINVAL;
  931. return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
  932. }
  933. static int snd_es1938_info_spatializer_enable(struct snd_kcontrol *kcontrol,
  934. struct snd_ctl_elem_info *uinfo)
  935. {
  936. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  937. uinfo->count = 1;
  938. uinfo->value.integer.min = 0;
  939. uinfo->value.integer.max = 1;
  940. return 0;
  941. }
  942. static int snd_es1938_get_spatializer_enable(struct snd_kcontrol *kcontrol,
  943. struct snd_ctl_elem_value *ucontrol)
  944. {
  945. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  946. unsigned char val = snd_es1938_mixer_read(chip, 0x50);
  947. ucontrol->value.integer.value[0] = !!(val & 8);
  948. return 0;
  949. }
  950. static int snd_es1938_put_spatializer_enable(struct snd_kcontrol *kcontrol,
  951. struct snd_ctl_elem_value *ucontrol)
  952. {
  953. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  954. unsigned char oval, nval;
  955. int change;
  956. nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  957. oval = snd_es1938_mixer_read(chip, 0x50) & 0x0c;
  958. change = nval != oval;
  959. if (change) {
  960. snd_es1938_mixer_write(chip, 0x50, nval & ~0x04);
  961. snd_es1938_mixer_write(chip, 0x50, nval);
  962. }
  963. return change;
  964. }
  965. static int snd_es1938_info_hw_volume(struct snd_kcontrol *kcontrol,
  966. struct snd_ctl_elem_info *uinfo)
  967. {
  968. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  969. uinfo->count = 2;
  970. uinfo->value.integer.min = 0;
  971. uinfo->value.integer.max = 63;
  972. return 0;
  973. }
  974. static int snd_es1938_get_hw_volume(struct snd_kcontrol *kcontrol,
  975. struct snd_ctl_elem_value *ucontrol)
  976. {
  977. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  978. ucontrol->value.integer.value[0] = snd_es1938_mixer_read(chip, 0x61) & 0x3f;
  979. ucontrol->value.integer.value[1] = snd_es1938_mixer_read(chip, 0x63) & 0x3f;
  980. return 0;
  981. }
  982. static int snd_es1938_info_hw_switch(struct snd_kcontrol *kcontrol,
  983. struct snd_ctl_elem_info *uinfo)
  984. {
  985. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  986. uinfo->count = 2;
  987. uinfo->value.integer.min = 0;
  988. uinfo->value.integer.max = 1;
  989. return 0;
  990. }
  991. static int snd_es1938_get_hw_switch(struct snd_kcontrol *kcontrol,
  992. struct snd_ctl_elem_value *ucontrol)
  993. {
  994. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  995. ucontrol->value.integer.value[0] = !(snd_es1938_mixer_read(chip, 0x61) & 0x40);
  996. ucontrol->value.integer.value[1] = !(snd_es1938_mixer_read(chip, 0x63) & 0x40);
  997. return 0;
  998. }
  999. static void snd_es1938_hwv_free(struct snd_kcontrol *kcontrol)
  1000. {
  1001. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1002. chip->master_volume = NULL;
  1003. chip->master_switch = NULL;
  1004. chip->hw_volume = NULL;
  1005. chip->hw_switch = NULL;
  1006. }
  1007. static int snd_es1938_reg_bits(struct es1938 *chip, unsigned char reg,
  1008. unsigned char mask, unsigned char val)
  1009. {
  1010. if (reg < 0xa0)
  1011. return snd_es1938_mixer_bits(chip, reg, mask, val);
  1012. else
  1013. return snd_es1938_bits(chip, reg, mask, val);
  1014. }
  1015. static int snd_es1938_reg_read(struct es1938 *chip, unsigned char reg)
  1016. {
  1017. if (reg < 0xa0)
  1018. return snd_es1938_mixer_read(chip, reg);
  1019. else
  1020. return snd_es1938_read(chip, reg);
  1021. }
  1022. #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1023. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1024. .info = snd_es1938_info_single, \
  1025. .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
  1026. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1027. static int snd_es1938_info_single(struct snd_kcontrol *kcontrol,
  1028. struct snd_ctl_elem_info *uinfo)
  1029. {
  1030. int mask = (kcontrol->private_value >> 16) & 0xff;
  1031. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1032. uinfo->count = 1;
  1033. uinfo->value.integer.min = 0;
  1034. uinfo->value.integer.max = mask;
  1035. return 0;
  1036. }
  1037. static int snd_es1938_get_single(struct snd_kcontrol *kcontrol,
  1038. struct snd_ctl_elem_value *ucontrol)
  1039. {
  1040. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1041. int reg = kcontrol->private_value & 0xff;
  1042. int shift = (kcontrol->private_value >> 8) & 0xff;
  1043. int mask = (kcontrol->private_value >> 16) & 0xff;
  1044. int invert = (kcontrol->private_value >> 24) & 0xff;
  1045. int val;
  1046. val = snd_es1938_reg_read(chip, reg);
  1047. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  1048. if (invert)
  1049. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1050. return 0;
  1051. }
  1052. static int snd_es1938_put_single(struct snd_kcontrol *kcontrol,
  1053. struct snd_ctl_elem_value *ucontrol)
  1054. {
  1055. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1056. int reg = kcontrol->private_value & 0xff;
  1057. int shift = (kcontrol->private_value >> 8) & 0xff;
  1058. int mask = (kcontrol->private_value >> 16) & 0xff;
  1059. int invert = (kcontrol->private_value >> 24) & 0xff;
  1060. unsigned char val;
  1061. val = (ucontrol->value.integer.value[0] & mask);
  1062. if (invert)
  1063. val = mask - val;
  1064. mask <<= shift;
  1065. val <<= shift;
  1066. return snd_es1938_reg_bits(chip, reg, mask, val) != val;
  1067. }
  1068. #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1069. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1070. .info = snd_es1938_info_double, \
  1071. .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
  1072. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1073. static int snd_es1938_info_double(struct snd_kcontrol *kcontrol,
  1074. struct snd_ctl_elem_info *uinfo)
  1075. {
  1076. int mask = (kcontrol->private_value >> 24) & 0xff;
  1077. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1078. uinfo->count = 2;
  1079. uinfo->value.integer.min = 0;
  1080. uinfo->value.integer.max = mask;
  1081. return 0;
  1082. }
  1083. static int snd_es1938_get_double(struct snd_kcontrol *kcontrol,
  1084. struct snd_ctl_elem_value *ucontrol)
  1085. {
  1086. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1087. int left_reg = kcontrol->private_value & 0xff;
  1088. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1089. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1090. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1091. int mask = (kcontrol->private_value >> 24) & 0xff;
  1092. int invert = (kcontrol->private_value >> 22) & 1;
  1093. unsigned char left, right;
  1094. left = snd_es1938_reg_read(chip, left_reg);
  1095. if (left_reg != right_reg)
  1096. right = snd_es1938_reg_read(chip, right_reg);
  1097. else
  1098. right = left;
  1099. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  1100. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  1101. if (invert) {
  1102. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1103. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1104. }
  1105. return 0;
  1106. }
  1107. static int snd_es1938_put_double(struct snd_kcontrol *kcontrol,
  1108. struct snd_ctl_elem_value *ucontrol)
  1109. {
  1110. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1111. int left_reg = kcontrol->private_value & 0xff;
  1112. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1113. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1114. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1115. int mask = (kcontrol->private_value >> 24) & 0xff;
  1116. int invert = (kcontrol->private_value >> 22) & 1;
  1117. int change;
  1118. unsigned char val1, val2, mask1, mask2;
  1119. val1 = ucontrol->value.integer.value[0] & mask;
  1120. val2 = ucontrol->value.integer.value[1] & mask;
  1121. if (invert) {
  1122. val1 = mask - val1;
  1123. val2 = mask - val2;
  1124. }
  1125. val1 <<= shift_left;
  1126. val2 <<= shift_right;
  1127. mask1 = mask << shift_left;
  1128. mask2 = mask << shift_right;
  1129. if (left_reg != right_reg) {
  1130. change = 0;
  1131. if (snd_es1938_reg_bits(chip, left_reg, mask1, val1) != val1)
  1132. change = 1;
  1133. if (snd_es1938_reg_bits(chip, right_reg, mask2, val2) != val2)
  1134. change = 1;
  1135. } else {
  1136. change = (snd_es1938_reg_bits(chip, left_reg, mask1 | mask2,
  1137. val1 | val2) != (val1 | val2));
  1138. }
  1139. return change;
  1140. }
  1141. static struct snd_kcontrol_new snd_es1938_controls[] = {
  1142. ES1938_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
  1143. ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  1144. {
  1145. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1146. .name = "Hardware Master Playback Volume",
  1147. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1148. .info = snd_es1938_info_hw_volume,
  1149. .get = snd_es1938_get_hw_volume,
  1150. },
  1151. {
  1152. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1153. .name = "Hardware Master Playback Switch",
  1154. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1155. .info = snd_es1938_info_hw_switch,
  1156. .get = snd_es1938_get_hw_switch,
  1157. },
  1158. ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
  1159. ES1938_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
  1160. ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  1161. ES1938_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
  1162. ES1938_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1163. ES1938_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
  1164. ES1938_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
  1165. ES1938_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
  1166. ES1938_SINGLE("PC Speaker Volume", 0, 0x3c, 0, 7, 0),
  1167. ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  1168. ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  1169. {
  1170. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1171. .name = "Capture Source",
  1172. .info = snd_es1938_info_mux,
  1173. .get = snd_es1938_get_mux,
  1174. .put = snd_es1938_put_mux,
  1175. },
  1176. ES1938_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1177. ES1938_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
  1178. ES1938_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
  1179. ES1938_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
  1180. ES1938_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
  1181. ES1938_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
  1182. ES1938_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
  1183. ES1938_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0),
  1184. ES1938_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
  1185. ES1938_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0),
  1186. ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  1187. {
  1188. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1189. .name = "3D Control - Switch",
  1190. .info = snd_es1938_info_spatializer_enable,
  1191. .get = snd_es1938_get_spatializer_enable,
  1192. .put = snd_es1938_put_spatializer_enable,
  1193. },
  1194. ES1938_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0)
  1195. };
  1196. /* ---------------------------------------------------------------------------- */
  1197. /* ---------------------------------------------------------------------------- */
  1198. /*
  1199. * initialize the chip - used by resume callback, too
  1200. */
  1201. static void snd_es1938_chip_init(struct es1938 *chip)
  1202. {
  1203. /* reset chip */
  1204. snd_es1938_reset(chip);
  1205. /* configure native mode */
  1206. /* enable bus master */
  1207. pci_set_master(chip->pci);
  1208. /* disable legacy audio */
  1209. pci_write_config_word(chip->pci, SL_PCI_LEGACYCONTROL, 0x805f);
  1210. /* set DDMA base */
  1211. pci_write_config_word(chip->pci, SL_PCI_DDMACONTROL, chip->ddma_port | 1);
  1212. /* set DMA/IRQ policy */
  1213. pci_write_config_dword(chip->pci, SL_PCI_CONFIG, 0);
  1214. /* enable Audio 1, Audio 2, MPU401 IRQ and HW volume IRQ*/
  1215. outb(0xf0, SLIO_REG(chip, IRQCONTROL));
  1216. /* reset DMA */
  1217. outb(0, SLDM_REG(chip, DMACLEAR));
  1218. }
  1219. #ifdef CONFIG_PM
  1220. /*
  1221. * PM support
  1222. */
  1223. static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
  1224. 0x14, 0x1a, 0x1c, 0x3a, 0x3c, 0x3e, 0x36, 0x38,
  1225. 0x50, 0x52, 0x60, 0x61, 0x62, 0x63, 0x64, 0x68,
  1226. 0x69, 0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x7c, 0x7d,
  1227. 0xa8, 0xb4,
  1228. };
  1229. static int es1938_suspend(struct pci_dev *pci, pm_message_t state)
  1230. {
  1231. struct snd_card *card = pci_get_drvdata(pci);
  1232. struct es1938 *chip = card->private_data;
  1233. unsigned char *s, *d;
  1234. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1235. snd_pcm_suspend_all(chip->pcm);
  1236. /* save mixer-related registers */
  1237. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++)
  1238. *d = snd_es1938_reg_read(chip, *s);
  1239. outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
  1240. if (chip->irq >= 0)
  1241. free_irq(chip->irq, chip);
  1242. pci_disable_device(pci);
  1243. pci_save_state(pci);
  1244. return 0;
  1245. }
  1246. static int es1938_resume(struct pci_dev *pci)
  1247. {
  1248. struct snd_card *card = pci_get_drvdata(pci);
  1249. struct es1938 *chip = card->private_data;
  1250. unsigned char *s, *d;
  1251. pci_restore_state(pci);
  1252. pci_enable_device(pci);
  1253. request_irq(pci->irq, snd_es1938_interrupt,
  1254. SA_INTERRUPT|SA_SHIRQ, "ES1938", chip);
  1255. chip->irq = pci->irq;
  1256. snd_es1938_chip_init(chip);
  1257. /* restore mixer-related registers */
  1258. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++) {
  1259. if (*s < 0xa0)
  1260. snd_es1938_mixer_write(chip, *s, *d);
  1261. else
  1262. snd_es1938_write(chip, *s, *d);
  1263. }
  1264. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1265. return 0;
  1266. }
  1267. #endif /* CONFIG_PM */
  1268. #ifdef SUPPORT_JOYSTICK
  1269. static int __devinit snd_es1938_create_gameport(struct es1938 *chip)
  1270. {
  1271. struct gameport *gp;
  1272. chip->gameport = gp = gameport_allocate_port();
  1273. if (!gp) {
  1274. printk(KERN_ERR "es1938: cannot allocate memory for gameport\n");
  1275. return -ENOMEM;
  1276. }
  1277. gameport_set_name(gp, "ES1938");
  1278. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1279. gameport_set_dev_parent(gp, &chip->pci->dev);
  1280. gp->io = chip->game_port;
  1281. gameport_register_port(gp);
  1282. return 0;
  1283. }
  1284. static void snd_es1938_free_gameport(struct es1938 *chip)
  1285. {
  1286. if (chip->gameport) {
  1287. gameport_unregister_port(chip->gameport);
  1288. chip->gameport = NULL;
  1289. }
  1290. }
  1291. #else
  1292. static inline int snd_es1938_create_gameport(struct es1938 *chip) { return -ENOSYS; }
  1293. static inline void snd_es1938_free_gameport(struct es1938 *chip) { }
  1294. #endif /* SUPPORT_JOYSTICK */
  1295. static int snd_es1938_free(struct es1938 *chip)
  1296. {
  1297. /* disable irqs */
  1298. outb(0x00, SLIO_REG(chip, IRQCONTROL));
  1299. if (chip->rmidi)
  1300. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0);
  1301. snd_es1938_free_gameport(chip);
  1302. if (chip->irq >= 0)
  1303. free_irq(chip->irq, chip);
  1304. pci_release_regions(chip->pci);
  1305. pci_disable_device(chip->pci);
  1306. kfree(chip);
  1307. return 0;
  1308. }
  1309. static int snd_es1938_dev_free(struct snd_device *device)
  1310. {
  1311. struct es1938 *chip = device->device_data;
  1312. return snd_es1938_free(chip);
  1313. }
  1314. static int __devinit snd_es1938_create(struct snd_card *card,
  1315. struct pci_dev * pci,
  1316. struct es1938 ** rchip)
  1317. {
  1318. struct es1938 *chip;
  1319. int err;
  1320. static struct snd_device_ops ops = {
  1321. .dev_free = snd_es1938_dev_free,
  1322. };
  1323. *rchip = NULL;
  1324. /* enable PCI device */
  1325. if ((err = pci_enable_device(pci)) < 0)
  1326. return err;
  1327. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1328. if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
  1329. pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
  1330. snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
  1331. pci_disable_device(pci);
  1332. return -ENXIO;
  1333. }
  1334. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1335. if (chip == NULL) {
  1336. pci_disable_device(pci);
  1337. return -ENOMEM;
  1338. }
  1339. spin_lock_init(&chip->reg_lock);
  1340. spin_lock_init(&chip->mixer_lock);
  1341. chip->card = card;
  1342. chip->pci = pci;
  1343. if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
  1344. kfree(chip);
  1345. pci_disable_device(pci);
  1346. return err;
  1347. }
  1348. chip->io_port = pci_resource_start(pci, 0);
  1349. chip->sb_port = pci_resource_start(pci, 1);
  1350. chip->vc_port = pci_resource_start(pci, 2);
  1351. chip->mpu_port = pci_resource_start(pci, 3);
  1352. chip->game_port = pci_resource_start(pci, 4);
  1353. if (request_irq(pci->irq, snd_es1938_interrupt, SA_INTERRUPT|SA_SHIRQ,
  1354. "ES1938", chip)) {
  1355. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1356. snd_es1938_free(chip);
  1357. return -EBUSY;
  1358. }
  1359. chip->irq = pci->irq;
  1360. #ifdef ES1938_DDEBUG
  1361. snd_printk(KERN_DEBUG "create: io: 0x%lx, sb: 0x%lx, vc: 0x%lx, mpu: 0x%lx, game: 0x%lx\n",
  1362. chip->io_port, chip->sb_port, chip->vc_port, chip->mpu_port, chip->game_port);
  1363. #endif
  1364. chip->ddma_port = chip->vc_port + 0x00; /* fix from Thomas Sailer */
  1365. snd_es1938_chip_init(chip);
  1366. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1367. snd_es1938_free(chip);
  1368. return err;
  1369. }
  1370. snd_card_set_dev(card, &pci->dev);
  1371. *rchip = chip;
  1372. return 0;
  1373. }
  1374. /* --------------------------------------------------------------------
  1375. * Interrupt handler
  1376. * -------------------------------------------------------------------- */
  1377. static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1378. {
  1379. struct es1938 *chip = dev_id;
  1380. unsigned char status, audiostatus;
  1381. int handled = 0;
  1382. status = inb(SLIO_REG(chip, IRQCONTROL));
  1383. #if 0
  1384. printk("Es1938debug - interrupt status: =0x%x\n", status);
  1385. #endif
  1386. /* AUDIO 1 */
  1387. if (status & 0x10) {
  1388. #if 0
  1389. printk("Es1938debug - AUDIO channel 1 interrupt\n");
  1390. printk("Es1938debug - AUDIO channel 1 DMAC DMA count: %u\n",
  1391. inw(SLDM_REG(chip, DMACOUNT)));
  1392. printk("Es1938debug - AUDIO channel 1 DMAC DMA base: %u\n",
  1393. inl(SLDM_REG(chip, DMAADDR)));
  1394. printk("Es1938debug - AUDIO channel 1 DMAC DMA status: 0x%x\n",
  1395. inl(SLDM_REG(chip, DMASTATUS)));
  1396. #endif
  1397. /* clear irq */
  1398. handled = 1;
  1399. audiostatus = inb(SLSB_REG(chip, STATUS));
  1400. if (chip->active & ADC1)
  1401. snd_pcm_period_elapsed(chip->capture_substream);
  1402. else if (chip->active & DAC1)
  1403. snd_pcm_period_elapsed(chip->playback2_substream);
  1404. }
  1405. /* AUDIO 2 */
  1406. if (status & 0x20) {
  1407. #if 0
  1408. printk("Es1938debug - AUDIO channel 2 interrupt\n");
  1409. printk("Es1938debug - AUDIO channel 2 DMAC DMA count: %u\n",
  1410. inw(SLIO_REG(chip, AUDIO2DMACOUNT)));
  1411. printk("Es1938debug - AUDIO channel 2 DMAC DMA base: %u\n",
  1412. inl(SLIO_REG(chip, AUDIO2DMAADDR)));
  1413. #endif
  1414. /* clear irq */
  1415. handled = 1;
  1416. snd_es1938_mixer_bits(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x80, 0);
  1417. if (chip->active & DAC2)
  1418. snd_pcm_period_elapsed(chip->playback1_substream);
  1419. }
  1420. /* Hardware volume */
  1421. if (status & 0x40) {
  1422. int split = snd_es1938_mixer_read(chip, 0x64) & 0x80;
  1423. handled = 1;
  1424. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
  1425. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
  1426. if (!split) {
  1427. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1428. &chip->master_switch->id);
  1429. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1430. &chip->master_volume->id);
  1431. }
  1432. /* ack interrupt */
  1433. snd_es1938_mixer_write(chip, 0x66, 0x00);
  1434. }
  1435. /* MPU401 */
  1436. if (status & 0x80) {
  1437. // the following line is evil! It switches off MIDI interrupt handling after the first interrupt received.
  1438. // replacing the last 0 by 0x40 works for ESS-Solo1, but just doing nothing works as well!
  1439. // andreas@flying-snail.de
  1440. // snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
  1441. if (chip->rmidi) {
  1442. handled = 1;
  1443. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  1444. }
  1445. }
  1446. return IRQ_RETVAL(handled);
  1447. }
  1448. #define ES1938_DMA_SIZE 64
  1449. static int __devinit snd_es1938_mixer(struct es1938 *chip)
  1450. {
  1451. struct snd_card *card;
  1452. unsigned int idx;
  1453. int err;
  1454. card = chip->card;
  1455. strcpy(card->mixername, "ESS Solo-1");
  1456. for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
  1457. struct snd_kcontrol *kctl;
  1458. kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
  1459. switch (idx) {
  1460. case 0:
  1461. chip->master_volume = kctl;
  1462. kctl->private_free = snd_es1938_hwv_free;
  1463. break;
  1464. case 1:
  1465. chip->master_switch = kctl;
  1466. kctl->private_free = snd_es1938_hwv_free;
  1467. break;
  1468. case 2:
  1469. chip->hw_volume = kctl;
  1470. kctl->private_free = snd_es1938_hwv_free;
  1471. break;
  1472. case 3:
  1473. chip->hw_switch = kctl;
  1474. kctl->private_free = snd_es1938_hwv_free;
  1475. break;
  1476. }
  1477. if ((err = snd_ctl_add(card, kctl)) < 0)
  1478. return err;
  1479. }
  1480. return 0;
  1481. }
  1482. static int __devinit snd_es1938_probe(struct pci_dev *pci,
  1483. const struct pci_device_id *pci_id)
  1484. {
  1485. static int dev;
  1486. struct snd_card *card;
  1487. struct es1938 *chip;
  1488. struct snd_opl3 *opl3;
  1489. int idx, err;
  1490. if (dev >= SNDRV_CARDS)
  1491. return -ENODEV;
  1492. if (!enable[dev]) {
  1493. dev++;
  1494. return -ENOENT;
  1495. }
  1496. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1497. if (card == NULL)
  1498. return -ENOMEM;
  1499. for (idx = 0; idx < 5; idx++) {
  1500. if (pci_resource_start(pci, idx) == 0 ||
  1501. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1502. snd_card_free(card);
  1503. return -ENODEV;
  1504. }
  1505. }
  1506. if ((err = snd_es1938_create(card, pci, &chip)) < 0) {
  1507. snd_card_free(card);
  1508. return err;
  1509. }
  1510. card->private_data = chip;
  1511. strcpy(card->driver, "ES1938");
  1512. strcpy(card->shortname, "ESS ES1938 (Solo-1)");
  1513. sprintf(card->longname, "%s rev %i, irq %i",
  1514. card->shortname,
  1515. chip->revision,
  1516. chip->irq);
  1517. if ((err = snd_es1938_new_pcm(chip, 0)) < 0) {
  1518. snd_card_free(card);
  1519. return err;
  1520. }
  1521. if ((err = snd_es1938_mixer(chip)) < 0) {
  1522. snd_card_free(card);
  1523. return err;
  1524. }
  1525. if (snd_opl3_create(card,
  1526. SLSB_REG(chip, FMLOWADDR),
  1527. SLSB_REG(chip, FMHIGHADDR),
  1528. OPL3_HW_OPL3, 1, &opl3) < 0) {
  1529. printk(KERN_ERR "es1938: OPL3 not detected at 0x%lx\n",
  1530. SLSB_REG(chip, FMLOWADDR));
  1531. } else {
  1532. if ((err = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
  1533. snd_card_free(card);
  1534. return err;
  1535. }
  1536. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1537. snd_card_free(card);
  1538. return err;
  1539. }
  1540. }
  1541. if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  1542. chip->mpu_port, 1, chip->irq, 0, &chip->rmidi) < 0) {
  1543. printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
  1544. } else {
  1545. // this line is vital for MIDI interrupt handling on ess-solo1
  1546. // andreas@flying-snail.de
  1547. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
  1548. }
  1549. snd_es1938_create_gameport(chip);
  1550. if ((err = snd_card_register(card)) < 0) {
  1551. snd_card_free(card);
  1552. return err;
  1553. }
  1554. pci_set_drvdata(pci, card);
  1555. dev++;
  1556. return 0;
  1557. }
  1558. static void __devexit snd_es1938_remove(struct pci_dev *pci)
  1559. {
  1560. snd_card_free(pci_get_drvdata(pci));
  1561. pci_set_drvdata(pci, NULL);
  1562. }
  1563. static struct pci_driver driver = {
  1564. .name = "ESS ES1938 (Solo-1)",
  1565. .id_table = snd_es1938_ids,
  1566. .probe = snd_es1938_probe,
  1567. .remove = __devexit_p(snd_es1938_remove),
  1568. #ifdef CONFIG_PM
  1569. .suspend = es1938_suspend,
  1570. .resume = es1938_resume,
  1571. #endif
  1572. };
  1573. static int __init alsa_card_es1938_init(void)
  1574. {
  1575. return pci_register_driver(&driver);
  1576. }
  1577. static void __exit alsa_card_es1938_exit(void)
  1578. {
  1579. pci_unregister_driver(&driver);
  1580. }
  1581. module_init(alsa_card_es1938_init)
  1582. module_exit(alsa_card_es1938_exit)