es1938.c 52 KB

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