es1938.c 56 KB

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