es1938.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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. snd_assert(pos + count <= chip->dma1_size, return -EINVAL);
  755. if (pos + count < chip->dma1_size) {
  756. if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
  757. return -EFAULT;
  758. } else {
  759. if (copy_to_user(dst, runtime->dma_area + pos + 1, count - 1))
  760. return -EFAULT;
  761. if (put_user(runtime->dma_area[0], ((unsigned char __user *)dst) + count - 1))
  762. return -EFAULT;
  763. }
  764. return 0;
  765. }
  766. /*
  767. * buffer management
  768. */
  769. static int snd_es1938_pcm_hw_params(struct snd_pcm_substream *substream,
  770. struct snd_pcm_hw_params *hw_params)
  771. {
  772. int err;
  773. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  774. return err;
  775. return 0;
  776. }
  777. static int snd_es1938_pcm_hw_free(struct snd_pcm_substream *substream)
  778. {
  779. return snd_pcm_lib_free_pages(substream);
  780. }
  781. /* ----------------------------------------------------------------------
  782. * Audio1 Capture (ADC)
  783. * ----------------------------------------------------------------------*/
  784. static struct snd_pcm_hardware snd_es1938_capture =
  785. {
  786. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  787. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  788. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  789. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  790. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  791. .rate_min = 6000,
  792. .rate_max = 48000,
  793. .channels_min = 1,
  794. .channels_max = 2,
  795. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  796. .period_bytes_min = 64,
  797. .period_bytes_max = 0x8000,
  798. .periods_min = 1,
  799. .periods_max = 1024,
  800. .fifo_size = 256,
  801. };
  802. /* -----------------------------------------------------------------------
  803. * Audio2 Playback (DAC)
  804. * -----------------------------------------------------------------------*/
  805. static struct snd_pcm_hardware snd_es1938_playback =
  806. {
  807. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  808. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  809. SNDRV_PCM_INFO_MMAP_VALID),
  810. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  811. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  812. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  813. .rate_min = 6000,
  814. .rate_max = 48000,
  815. .channels_min = 1,
  816. .channels_max = 2,
  817. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  818. .period_bytes_min = 64,
  819. .period_bytes_max = 0x8000,
  820. .periods_min = 1,
  821. .periods_max = 1024,
  822. .fifo_size = 256,
  823. };
  824. static int snd_es1938_capture_open(struct snd_pcm_substream *substream)
  825. {
  826. struct es1938 *chip = snd_pcm_substream_chip(substream);
  827. struct snd_pcm_runtime *runtime = substream->runtime;
  828. if (chip->playback2_substream)
  829. return -EAGAIN;
  830. chip->capture_substream = substream;
  831. runtime->hw = snd_es1938_capture;
  832. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  833. &hw_constraints_clocks);
  834. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  835. return 0;
  836. }
  837. static int snd_es1938_playback_open(struct snd_pcm_substream *substream)
  838. {
  839. struct es1938 *chip = snd_pcm_substream_chip(substream);
  840. struct snd_pcm_runtime *runtime = substream->runtime;
  841. switch (substream->number) {
  842. case 0:
  843. chip->playback1_substream = substream;
  844. break;
  845. case 1:
  846. if (chip->capture_substream)
  847. return -EAGAIN;
  848. chip->playback2_substream = substream;
  849. break;
  850. default:
  851. snd_BUG();
  852. return -EINVAL;
  853. }
  854. runtime->hw = snd_es1938_playback;
  855. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  856. &hw_constraints_clocks);
  857. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  858. return 0;
  859. }
  860. static int snd_es1938_capture_close(struct snd_pcm_substream *substream)
  861. {
  862. struct es1938 *chip = snd_pcm_substream_chip(substream);
  863. chip->capture_substream = NULL;
  864. return 0;
  865. }
  866. static int snd_es1938_playback_close(struct snd_pcm_substream *substream)
  867. {
  868. struct es1938 *chip = snd_pcm_substream_chip(substream);
  869. switch (substream->number) {
  870. case 0:
  871. chip->playback1_substream = NULL;
  872. break;
  873. case 1:
  874. chip->playback2_substream = NULL;
  875. break;
  876. default:
  877. snd_BUG();
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. static struct snd_pcm_ops snd_es1938_playback_ops = {
  883. .open = snd_es1938_playback_open,
  884. .close = snd_es1938_playback_close,
  885. .ioctl = snd_pcm_lib_ioctl,
  886. .hw_params = snd_es1938_pcm_hw_params,
  887. .hw_free = snd_es1938_pcm_hw_free,
  888. .prepare = snd_es1938_playback_prepare,
  889. .trigger = snd_es1938_playback_trigger,
  890. .pointer = snd_es1938_playback_pointer,
  891. };
  892. static struct snd_pcm_ops snd_es1938_capture_ops = {
  893. .open = snd_es1938_capture_open,
  894. .close = snd_es1938_capture_close,
  895. .ioctl = snd_pcm_lib_ioctl,
  896. .hw_params = snd_es1938_pcm_hw_params,
  897. .hw_free = snd_es1938_pcm_hw_free,
  898. .prepare = snd_es1938_capture_prepare,
  899. .trigger = snd_es1938_capture_trigger,
  900. .pointer = snd_es1938_capture_pointer,
  901. .copy = snd_es1938_capture_copy,
  902. };
  903. static int __devinit snd_es1938_new_pcm(struct es1938 *chip, int device)
  904. {
  905. struct snd_pcm *pcm;
  906. int err;
  907. if ((err = snd_pcm_new(chip->card, "es-1938-1946", device, 2, 1, &pcm)) < 0)
  908. return err;
  909. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1938_playback_ops);
  910. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1938_capture_ops);
  911. pcm->private_data = chip;
  912. pcm->info_flags = 0;
  913. strcpy(pcm->name, "ESS Solo-1");
  914. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  915. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  916. chip->pcm = pcm;
  917. return 0;
  918. }
  919. /* -------------------------------------------------------------------
  920. *
  921. * *** Mixer part ***
  922. */
  923. static int snd_es1938_info_mux(struct snd_kcontrol *kcontrol,
  924. struct snd_ctl_elem_info *uinfo)
  925. {
  926. static char *texts[8] = {
  927. "Mic", "Mic Master", "CD", "AOUT",
  928. "Mic1", "Mix", "Line", "Master"
  929. };
  930. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  931. uinfo->count = 1;
  932. uinfo->value.enumerated.items = 8;
  933. if (uinfo->value.enumerated.item > 7)
  934. uinfo->value.enumerated.item = 7;
  935. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  936. return 0;
  937. }
  938. static int snd_es1938_get_mux(struct snd_kcontrol *kcontrol,
  939. struct snd_ctl_elem_value *ucontrol)
  940. {
  941. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  942. ucontrol->value.enumerated.item[0] = snd_es1938_mixer_read(chip, 0x1c) & 0x07;
  943. return 0;
  944. }
  945. static int snd_es1938_put_mux(struct snd_kcontrol *kcontrol,
  946. struct snd_ctl_elem_value *ucontrol)
  947. {
  948. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  949. unsigned char val = ucontrol->value.enumerated.item[0];
  950. if (val > 7)
  951. return -EINVAL;
  952. return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
  953. }
  954. #define snd_es1938_info_spatializer_enable snd_ctl_boolean_mono_info
  955. static int snd_es1938_get_spatializer_enable(struct snd_kcontrol *kcontrol,
  956. struct snd_ctl_elem_value *ucontrol)
  957. {
  958. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  959. unsigned char val = snd_es1938_mixer_read(chip, 0x50);
  960. ucontrol->value.integer.value[0] = !!(val & 8);
  961. return 0;
  962. }
  963. static int snd_es1938_put_spatializer_enable(struct snd_kcontrol *kcontrol,
  964. struct snd_ctl_elem_value *ucontrol)
  965. {
  966. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  967. unsigned char oval, nval;
  968. int change;
  969. nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  970. oval = snd_es1938_mixer_read(chip, 0x50) & 0x0c;
  971. change = nval != oval;
  972. if (change) {
  973. snd_es1938_mixer_write(chip, 0x50, nval & ~0x04);
  974. snd_es1938_mixer_write(chip, 0x50, nval);
  975. }
  976. return change;
  977. }
  978. static int snd_es1938_info_hw_volume(struct snd_kcontrol *kcontrol,
  979. struct snd_ctl_elem_info *uinfo)
  980. {
  981. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  982. uinfo->count = 2;
  983. uinfo->value.integer.min = 0;
  984. uinfo->value.integer.max = 63;
  985. return 0;
  986. }
  987. static int snd_es1938_get_hw_volume(struct snd_kcontrol *kcontrol,
  988. struct snd_ctl_elem_value *ucontrol)
  989. {
  990. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  991. ucontrol->value.integer.value[0] = snd_es1938_mixer_read(chip, 0x61) & 0x3f;
  992. ucontrol->value.integer.value[1] = snd_es1938_mixer_read(chip, 0x63) & 0x3f;
  993. return 0;
  994. }
  995. #define snd_es1938_info_hw_switch snd_ctl_boolean_stereo_info
  996. static int snd_es1938_get_hw_switch(struct snd_kcontrol *kcontrol,
  997. struct snd_ctl_elem_value *ucontrol)
  998. {
  999. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1000. ucontrol->value.integer.value[0] = !(snd_es1938_mixer_read(chip, 0x61) & 0x40);
  1001. ucontrol->value.integer.value[1] = !(snd_es1938_mixer_read(chip, 0x63) & 0x40);
  1002. return 0;
  1003. }
  1004. static void snd_es1938_hwv_free(struct snd_kcontrol *kcontrol)
  1005. {
  1006. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1007. chip->master_volume = NULL;
  1008. chip->master_switch = NULL;
  1009. chip->hw_volume = NULL;
  1010. chip->hw_switch = NULL;
  1011. }
  1012. static int snd_es1938_reg_bits(struct es1938 *chip, unsigned char reg,
  1013. unsigned char mask, unsigned char val)
  1014. {
  1015. if (reg < 0xa0)
  1016. return snd_es1938_mixer_bits(chip, reg, mask, val);
  1017. else
  1018. return snd_es1938_bits(chip, reg, mask, val);
  1019. }
  1020. static int snd_es1938_reg_read(struct es1938 *chip, unsigned char reg)
  1021. {
  1022. if (reg < 0xa0)
  1023. return snd_es1938_mixer_read(chip, reg);
  1024. else
  1025. return snd_es1938_read(chip, reg);
  1026. }
  1027. #define ES1938_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
  1028. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1029. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
  1030. .name = xname, .index = xindex, \
  1031. .info = snd_es1938_info_single, \
  1032. .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
  1033. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
  1034. .tlv = { .p = xtlv } }
  1035. #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1036. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1037. .info = snd_es1938_info_single, \
  1038. .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
  1039. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1040. static int snd_es1938_info_single(struct snd_kcontrol *kcontrol,
  1041. struct snd_ctl_elem_info *uinfo)
  1042. {
  1043. int mask = (kcontrol->private_value >> 16) & 0xff;
  1044. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1045. uinfo->count = 1;
  1046. uinfo->value.integer.min = 0;
  1047. uinfo->value.integer.max = mask;
  1048. return 0;
  1049. }
  1050. static int snd_es1938_get_single(struct snd_kcontrol *kcontrol,
  1051. struct snd_ctl_elem_value *ucontrol)
  1052. {
  1053. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1054. int reg = kcontrol->private_value & 0xff;
  1055. int shift = (kcontrol->private_value >> 8) & 0xff;
  1056. int mask = (kcontrol->private_value >> 16) & 0xff;
  1057. int invert = (kcontrol->private_value >> 24) & 0xff;
  1058. int val;
  1059. val = snd_es1938_reg_read(chip, reg);
  1060. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  1061. if (invert)
  1062. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1063. return 0;
  1064. }
  1065. static int snd_es1938_put_single(struct snd_kcontrol *kcontrol,
  1066. struct snd_ctl_elem_value *ucontrol)
  1067. {
  1068. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1069. int reg = kcontrol->private_value & 0xff;
  1070. int shift = (kcontrol->private_value >> 8) & 0xff;
  1071. int mask = (kcontrol->private_value >> 16) & 0xff;
  1072. int invert = (kcontrol->private_value >> 24) & 0xff;
  1073. unsigned char val;
  1074. val = (ucontrol->value.integer.value[0] & mask);
  1075. if (invert)
  1076. val = mask - val;
  1077. mask <<= shift;
  1078. val <<= shift;
  1079. return snd_es1938_reg_bits(chip, reg, mask, val) != val;
  1080. }
  1081. #define ES1938_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
  1082. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1083. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
  1084. .name = xname, .index = xindex, \
  1085. .info = snd_es1938_info_double, \
  1086. .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
  1087. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
  1088. .tlv = { .p = xtlv } }
  1089. #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1090. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1091. .info = snd_es1938_info_double, \
  1092. .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
  1093. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1094. static int snd_es1938_info_double(struct snd_kcontrol *kcontrol,
  1095. struct snd_ctl_elem_info *uinfo)
  1096. {
  1097. int mask = (kcontrol->private_value >> 24) & 0xff;
  1098. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1099. uinfo->count = 2;
  1100. uinfo->value.integer.min = 0;
  1101. uinfo->value.integer.max = mask;
  1102. return 0;
  1103. }
  1104. static int snd_es1938_get_double(struct snd_kcontrol *kcontrol,
  1105. struct snd_ctl_elem_value *ucontrol)
  1106. {
  1107. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1108. int left_reg = kcontrol->private_value & 0xff;
  1109. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1110. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1111. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1112. int mask = (kcontrol->private_value >> 24) & 0xff;
  1113. int invert = (kcontrol->private_value >> 22) & 1;
  1114. unsigned char left, right;
  1115. left = snd_es1938_reg_read(chip, left_reg);
  1116. if (left_reg != right_reg)
  1117. right = snd_es1938_reg_read(chip, right_reg);
  1118. else
  1119. right = left;
  1120. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  1121. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  1122. if (invert) {
  1123. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1124. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1125. }
  1126. return 0;
  1127. }
  1128. static int snd_es1938_put_double(struct snd_kcontrol *kcontrol,
  1129. struct snd_ctl_elem_value *ucontrol)
  1130. {
  1131. struct es1938 *chip = snd_kcontrol_chip(kcontrol);
  1132. int left_reg = kcontrol->private_value & 0xff;
  1133. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1134. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1135. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1136. int mask = (kcontrol->private_value >> 24) & 0xff;
  1137. int invert = (kcontrol->private_value >> 22) & 1;
  1138. int change;
  1139. unsigned char val1, val2, mask1, mask2;
  1140. val1 = ucontrol->value.integer.value[0] & mask;
  1141. val2 = ucontrol->value.integer.value[1] & mask;
  1142. if (invert) {
  1143. val1 = mask - val1;
  1144. val2 = mask - val2;
  1145. }
  1146. val1 <<= shift_left;
  1147. val2 <<= shift_right;
  1148. mask1 = mask << shift_left;
  1149. mask2 = mask << shift_right;
  1150. if (left_reg != right_reg) {
  1151. change = 0;
  1152. if (snd_es1938_reg_bits(chip, left_reg, mask1, val1) != val1)
  1153. change = 1;
  1154. if (snd_es1938_reg_bits(chip, right_reg, mask2, val2) != val2)
  1155. change = 1;
  1156. } else {
  1157. change = (snd_es1938_reg_bits(chip, left_reg, mask1 | mask2,
  1158. val1 | val2) != (val1 | val2));
  1159. }
  1160. return change;
  1161. }
  1162. static unsigned int db_scale_master[] = {
  1163. TLV_DB_RANGE_HEAD(2),
  1164. 0, 54, TLV_DB_SCALE_ITEM(-3600, 50, 1),
  1165. 54, 63, TLV_DB_SCALE_ITEM(-900, 100, 0),
  1166. };
  1167. static unsigned int db_scale_audio1[] = {
  1168. TLV_DB_RANGE_HEAD(2),
  1169. 0, 8, TLV_DB_SCALE_ITEM(-3300, 300, 1),
  1170. 8, 15, TLV_DB_SCALE_ITEM(-900, 150, 0),
  1171. };
  1172. static unsigned int db_scale_audio2[] = {
  1173. TLV_DB_RANGE_HEAD(2),
  1174. 0, 8, TLV_DB_SCALE_ITEM(-3450, 300, 1),
  1175. 8, 15, TLV_DB_SCALE_ITEM(-1050, 150, 0),
  1176. };
  1177. static unsigned int db_scale_mic[] = {
  1178. TLV_DB_RANGE_HEAD(2),
  1179. 0, 8, TLV_DB_SCALE_ITEM(-2400, 300, 1),
  1180. 8, 15, TLV_DB_SCALE_ITEM(0, 150, 0),
  1181. };
  1182. static unsigned int db_scale_line[] = {
  1183. TLV_DB_RANGE_HEAD(2),
  1184. 0, 8, TLV_DB_SCALE_ITEM(-3150, 300, 1),
  1185. 8, 15, TLV_DB_SCALE_ITEM(-750, 150, 0),
  1186. };
  1187. static const DECLARE_TLV_DB_SCALE(db_scale_capture, 0, 150, 0);
  1188. static struct snd_kcontrol_new snd_es1938_controls[] = {
  1189. ES1938_DOUBLE_TLV("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0,
  1190. db_scale_master),
  1191. ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  1192. {
  1193. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1194. .name = "Hardware Master Playback Volume",
  1195. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1196. .info = snd_es1938_info_hw_volume,
  1197. .get = snd_es1938_get_hw_volume,
  1198. },
  1199. {
  1200. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1201. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  1202. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  1203. .name = "Hardware Master Playback Switch",
  1204. .info = snd_es1938_info_hw_switch,
  1205. .get = snd_es1938_get_hw_switch,
  1206. .tlv = { .p = db_scale_master },
  1207. },
  1208. ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
  1209. ES1938_DOUBLE_TLV("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0,
  1210. db_scale_line),
  1211. ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  1212. ES1938_DOUBLE_TLV("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0,
  1213. db_scale_mic),
  1214. ES1938_DOUBLE_TLV("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
  1215. db_scale_line),
  1216. ES1938_DOUBLE_TLV("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0,
  1217. db_scale_mic),
  1218. ES1938_DOUBLE_TLV("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0,
  1219. db_scale_line),
  1220. ES1938_DOUBLE_TLV("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0,
  1221. db_scale_capture),
  1222. ES1938_SINGLE("PC Speaker Volume", 0, 0x3c, 0, 7, 0),
  1223. ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  1224. ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  1225. {
  1226. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1227. .name = "Capture Source",
  1228. .info = snd_es1938_info_mux,
  1229. .get = snd_es1938_get_mux,
  1230. .put = snd_es1938_put_mux,
  1231. },
  1232. ES1938_DOUBLE_TLV("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
  1233. db_scale_line),
  1234. ES1938_DOUBLE_TLV("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0,
  1235. db_scale_audio2),
  1236. ES1938_DOUBLE_TLV("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0,
  1237. db_scale_mic),
  1238. ES1938_DOUBLE_TLV("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0,
  1239. db_scale_line),
  1240. ES1938_DOUBLE_TLV("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0,
  1241. db_scale_mic),
  1242. ES1938_DOUBLE_TLV("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0,
  1243. db_scale_line),
  1244. ES1938_DOUBLE_TLV("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0,
  1245. db_scale_line),
  1246. ES1938_DOUBLE_TLV("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0,
  1247. db_scale_line),
  1248. ES1938_DOUBLE_TLV("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0,
  1249. db_scale_audio2),
  1250. ES1938_DOUBLE_TLV("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0,
  1251. db_scale_audio1),
  1252. ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  1253. {
  1254. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1255. .name = "3D Control - Switch",
  1256. .info = snd_es1938_info_spatializer_enable,
  1257. .get = snd_es1938_get_spatializer_enable,
  1258. .put = snd_es1938_put_spatializer_enable,
  1259. },
  1260. ES1938_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0)
  1261. };
  1262. /* ---------------------------------------------------------------------------- */
  1263. /* ---------------------------------------------------------------------------- */
  1264. /*
  1265. * initialize the chip - used by resume callback, too
  1266. */
  1267. static void snd_es1938_chip_init(struct es1938 *chip)
  1268. {
  1269. /* reset chip */
  1270. snd_es1938_reset(chip);
  1271. /* configure native mode */
  1272. /* enable bus master */
  1273. pci_set_master(chip->pci);
  1274. /* disable legacy audio */
  1275. pci_write_config_word(chip->pci, SL_PCI_LEGACYCONTROL, 0x805f);
  1276. /* set DDMA base */
  1277. pci_write_config_word(chip->pci, SL_PCI_DDMACONTROL, chip->ddma_port | 1);
  1278. /* set DMA/IRQ policy */
  1279. pci_write_config_dword(chip->pci, SL_PCI_CONFIG, 0);
  1280. /* enable Audio 1, Audio 2, MPU401 IRQ and HW volume IRQ*/
  1281. outb(0xf0, SLIO_REG(chip, IRQCONTROL));
  1282. /* reset DMA */
  1283. outb(0, SLDM_REG(chip, DMACLEAR));
  1284. }
  1285. #ifdef CONFIG_PM
  1286. /*
  1287. * PM support
  1288. */
  1289. static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
  1290. 0x14, 0x1a, 0x1c, 0x3a, 0x3c, 0x3e, 0x36, 0x38,
  1291. 0x50, 0x52, 0x60, 0x61, 0x62, 0x63, 0x64, 0x68,
  1292. 0x69, 0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x7c, 0x7d,
  1293. 0xa8, 0xb4,
  1294. };
  1295. static int es1938_suspend(struct pci_dev *pci, pm_message_t state)
  1296. {
  1297. struct snd_card *card = pci_get_drvdata(pci);
  1298. struct es1938 *chip = card->private_data;
  1299. unsigned char *s, *d;
  1300. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1301. snd_pcm_suspend_all(chip->pcm);
  1302. /* save mixer-related registers */
  1303. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++)
  1304. *d = snd_es1938_reg_read(chip, *s);
  1305. outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
  1306. if (chip->irq >= 0) {
  1307. free_irq(chip->irq, chip);
  1308. chip->irq = -1;
  1309. }
  1310. pci_disable_device(pci);
  1311. pci_save_state(pci);
  1312. pci_set_power_state(pci, pci_choose_state(pci, state));
  1313. return 0;
  1314. }
  1315. static int es1938_resume(struct pci_dev *pci)
  1316. {
  1317. struct snd_card *card = pci_get_drvdata(pci);
  1318. struct es1938 *chip = card->private_data;
  1319. unsigned char *s, *d;
  1320. pci_set_power_state(pci, PCI_D0);
  1321. pci_restore_state(pci);
  1322. if (pci_enable_device(pci) < 0) {
  1323. printk(KERN_ERR "es1938: pci_enable_device failed, "
  1324. "disabling device\n");
  1325. snd_card_disconnect(card);
  1326. return -EIO;
  1327. }
  1328. if (request_irq(pci->irq, snd_es1938_interrupt,
  1329. IRQF_SHARED, "ES1938", chip)) {
  1330. printk(KERN_ERR "es1938: unable to grab IRQ %d, "
  1331. "disabling device\n", pci->irq);
  1332. snd_card_disconnect(card);
  1333. return -EIO;
  1334. }
  1335. chip->irq = pci->irq;
  1336. snd_es1938_chip_init(chip);
  1337. /* restore mixer-related registers */
  1338. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++) {
  1339. if (*s < 0xa0)
  1340. snd_es1938_mixer_write(chip, *s, *d);
  1341. else
  1342. snd_es1938_write(chip, *s, *d);
  1343. }
  1344. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1345. return 0;
  1346. }
  1347. #endif /* CONFIG_PM */
  1348. #ifdef SUPPORT_JOYSTICK
  1349. static int __devinit snd_es1938_create_gameport(struct es1938 *chip)
  1350. {
  1351. struct gameport *gp;
  1352. chip->gameport = gp = gameport_allocate_port();
  1353. if (!gp) {
  1354. printk(KERN_ERR "es1938: cannot allocate memory for gameport\n");
  1355. return -ENOMEM;
  1356. }
  1357. gameport_set_name(gp, "ES1938");
  1358. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1359. gameport_set_dev_parent(gp, &chip->pci->dev);
  1360. gp->io = chip->game_port;
  1361. gameport_register_port(gp);
  1362. return 0;
  1363. }
  1364. static void snd_es1938_free_gameport(struct es1938 *chip)
  1365. {
  1366. if (chip->gameport) {
  1367. gameport_unregister_port(chip->gameport);
  1368. chip->gameport = NULL;
  1369. }
  1370. }
  1371. #else
  1372. static inline int snd_es1938_create_gameport(struct es1938 *chip) { return -ENOSYS; }
  1373. static inline void snd_es1938_free_gameport(struct es1938 *chip) { }
  1374. #endif /* SUPPORT_JOYSTICK */
  1375. static int snd_es1938_free(struct es1938 *chip)
  1376. {
  1377. /* disable irqs */
  1378. outb(0x00, SLIO_REG(chip, IRQCONTROL));
  1379. if (chip->rmidi)
  1380. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0);
  1381. snd_es1938_free_gameport(chip);
  1382. if (chip->irq >= 0)
  1383. free_irq(chip->irq, chip);
  1384. pci_release_regions(chip->pci);
  1385. pci_disable_device(chip->pci);
  1386. kfree(chip);
  1387. return 0;
  1388. }
  1389. static int snd_es1938_dev_free(struct snd_device *device)
  1390. {
  1391. struct es1938 *chip = device->device_data;
  1392. return snd_es1938_free(chip);
  1393. }
  1394. static int __devinit snd_es1938_create(struct snd_card *card,
  1395. struct pci_dev * pci,
  1396. struct es1938 ** rchip)
  1397. {
  1398. struct es1938 *chip;
  1399. int err;
  1400. static struct snd_device_ops ops = {
  1401. .dev_free = snd_es1938_dev_free,
  1402. };
  1403. *rchip = NULL;
  1404. /* enable PCI device */
  1405. if ((err = pci_enable_device(pci)) < 0)
  1406. return err;
  1407. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1408. if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
  1409. pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
  1410. snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
  1411. pci_disable_device(pci);
  1412. return -ENXIO;
  1413. }
  1414. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1415. if (chip == NULL) {
  1416. pci_disable_device(pci);
  1417. return -ENOMEM;
  1418. }
  1419. spin_lock_init(&chip->reg_lock);
  1420. spin_lock_init(&chip->mixer_lock);
  1421. chip->card = card;
  1422. chip->pci = pci;
  1423. chip->irq = -1;
  1424. if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
  1425. kfree(chip);
  1426. pci_disable_device(pci);
  1427. return err;
  1428. }
  1429. chip->io_port = pci_resource_start(pci, 0);
  1430. chip->sb_port = pci_resource_start(pci, 1);
  1431. chip->vc_port = pci_resource_start(pci, 2);
  1432. chip->mpu_port = pci_resource_start(pci, 3);
  1433. chip->game_port = pci_resource_start(pci, 4);
  1434. if (request_irq(pci->irq, snd_es1938_interrupt, IRQF_SHARED,
  1435. "ES1938", chip)) {
  1436. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1437. snd_es1938_free(chip);
  1438. return -EBUSY;
  1439. }
  1440. chip->irq = pci->irq;
  1441. #ifdef ES1938_DDEBUG
  1442. snd_printk(KERN_DEBUG "create: io: 0x%lx, sb: 0x%lx, vc: 0x%lx, mpu: 0x%lx, game: 0x%lx\n",
  1443. chip->io_port, chip->sb_port, chip->vc_port, chip->mpu_port, chip->game_port);
  1444. #endif
  1445. chip->ddma_port = chip->vc_port + 0x00; /* fix from Thomas Sailer */
  1446. snd_es1938_chip_init(chip);
  1447. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1448. snd_es1938_free(chip);
  1449. return err;
  1450. }
  1451. snd_card_set_dev(card, &pci->dev);
  1452. *rchip = chip;
  1453. return 0;
  1454. }
  1455. /* --------------------------------------------------------------------
  1456. * Interrupt handler
  1457. * -------------------------------------------------------------------- */
  1458. static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id)
  1459. {
  1460. struct es1938 *chip = dev_id;
  1461. unsigned char status, audiostatus;
  1462. int handled = 0;
  1463. status = inb(SLIO_REG(chip, IRQCONTROL));
  1464. #if 0
  1465. printk("Es1938debug - interrupt status: =0x%x\n", status);
  1466. #endif
  1467. /* AUDIO 1 */
  1468. if (status & 0x10) {
  1469. #if 0
  1470. printk("Es1938debug - AUDIO channel 1 interrupt\n");
  1471. printk("Es1938debug - AUDIO channel 1 DMAC DMA count: %u\n",
  1472. inw(SLDM_REG(chip, DMACOUNT)));
  1473. printk("Es1938debug - AUDIO channel 1 DMAC DMA base: %u\n",
  1474. inl(SLDM_REG(chip, DMAADDR)));
  1475. printk("Es1938debug - AUDIO channel 1 DMAC DMA status: 0x%x\n",
  1476. inl(SLDM_REG(chip, DMASTATUS)));
  1477. #endif
  1478. /* clear irq */
  1479. handled = 1;
  1480. audiostatus = inb(SLSB_REG(chip, STATUS));
  1481. if (chip->active & ADC1)
  1482. snd_pcm_period_elapsed(chip->capture_substream);
  1483. else if (chip->active & DAC1)
  1484. snd_pcm_period_elapsed(chip->playback2_substream);
  1485. }
  1486. /* AUDIO 2 */
  1487. if (status & 0x20) {
  1488. #if 0
  1489. printk("Es1938debug - AUDIO channel 2 interrupt\n");
  1490. printk("Es1938debug - AUDIO channel 2 DMAC DMA count: %u\n",
  1491. inw(SLIO_REG(chip, AUDIO2DMACOUNT)));
  1492. printk("Es1938debug - AUDIO channel 2 DMAC DMA base: %u\n",
  1493. inl(SLIO_REG(chip, AUDIO2DMAADDR)));
  1494. #endif
  1495. /* clear irq */
  1496. handled = 1;
  1497. snd_es1938_mixer_bits(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x80, 0);
  1498. if (chip->active & DAC2)
  1499. snd_pcm_period_elapsed(chip->playback1_substream);
  1500. }
  1501. /* Hardware volume */
  1502. if (status & 0x40) {
  1503. int split = snd_es1938_mixer_read(chip, 0x64) & 0x80;
  1504. handled = 1;
  1505. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
  1506. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
  1507. if (!split) {
  1508. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1509. &chip->master_switch->id);
  1510. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1511. &chip->master_volume->id);
  1512. }
  1513. /* ack interrupt */
  1514. snd_es1938_mixer_write(chip, 0x66, 0x00);
  1515. }
  1516. /* MPU401 */
  1517. if (status & 0x80) {
  1518. // the following line is evil! It switches off MIDI interrupt handling after the first interrupt received.
  1519. // replacing the last 0 by 0x40 works for ESS-Solo1, but just doing nothing works as well!
  1520. // andreas@flying-snail.de
  1521. // snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
  1522. if (chip->rmidi) {
  1523. handled = 1;
  1524. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  1525. }
  1526. }
  1527. return IRQ_RETVAL(handled);
  1528. }
  1529. #define ES1938_DMA_SIZE 64
  1530. static int __devinit snd_es1938_mixer(struct es1938 *chip)
  1531. {
  1532. struct snd_card *card;
  1533. unsigned int idx;
  1534. int err;
  1535. card = chip->card;
  1536. strcpy(card->mixername, "ESS Solo-1");
  1537. for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
  1538. struct snd_kcontrol *kctl;
  1539. kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
  1540. switch (idx) {
  1541. case 0:
  1542. chip->master_volume = kctl;
  1543. kctl->private_free = snd_es1938_hwv_free;
  1544. break;
  1545. case 1:
  1546. chip->master_switch = kctl;
  1547. kctl->private_free = snd_es1938_hwv_free;
  1548. break;
  1549. case 2:
  1550. chip->hw_volume = kctl;
  1551. kctl->private_free = snd_es1938_hwv_free;
  1552. break;
  1553. case 3:
  1554. chip->hw_switch = kctl;
  1555. kctl->private_free = snd_es1938_hwv_free;
  1556. break;
  1557. }
  1558. if ((err = snd_ctl_add(card, kctl)) < 0)
  1559. return err;
  1560. }
  1561. return 0;
  1562. }
  1563. static int __devinit snd_es1938_probe(struct pci_dev *pci,
  1564. const struct pci_device_id *pci_id)
  1565. {
  1566. static int dev;
  1567. struct snd_card *card;
  1568. struct es1938 *chip;
  1569. struct snd_opl3 *opl3;
  1570. int idx, err;
  1571. if (dev >= SNDRV_CARDS)
  1572. return -ENODEV;
  1573. if (!enable[dev]) {
  1574. dev++;
  1575. return -ENOENT;
  1576. }
  1577. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1578. if (card == NULL)
  1579. return -ENOMEM;
  1580. for (idx = 0; idx < 5; idx++) {
  1581. if (pci_resource_start(pci, idx) == 0 ||
  1582. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1583. snd_card_free(card);
  1584. return -ENODEV;
  1585. }
  1586. }
  1587. if ((err = snd_es1938_create(card, pci, &chip)) < 0) {
  1588. snd_card_free(card);
  1589. return err;
  1590. }
  1591. card->private_data = chip;
  1592. strcpy(card->driver, "ES1938");
  1593. strcpy(card->shortname, "ESS ES1938 (Solo-1)");
  1594. sprintf(card->longname, "%s rev %i, irq %i",
  1595. card->shortname,
  1596. chip->revision,
  1597. chip->irq);
  1598. if ((err = snd_es1938_new_pcm(chip, 0)) < 0) {
  1599. snd_card_free(card);
  1600. return err;
  1601. }
  1602. if ((err = snd_es1938_mixer(chip)) < 0) {
  1603. snd_card_free(card);
  1604. return err;
  1605. }
  1606. if (snd_opl3_create(card,
  1607. SLSB_REG(chip, FMLOWADDR),
  1608. SLSB_REG(chip, FMHIGHADDR),
  1609. OPL3_HW_OPL3, 1, &opl3) < 0) {
  1610. printk(KERN_ERR "es1938: OPL3 not detected at 0x%lx\n",
  1611. SLSB_REG(chip, FMLOWADDR));
  1612. } else {
  1613. if ((err = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
  1614. snd_card_free(card);
  1615. return err;
  1616. }
  1617. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1618. snd_card_free(card);
  1619. return err;
  1620. }
  1621. }
  1622. if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  1623. chip->mpu_port, MPU401_INFO_INTEGRATED,
  1624. chip->irq, 0, &chip->rmidi) < 0) {
  1625. printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
  1626. } else {
  1627. // this line is vital for MIDI interrupt handling on ess-solo1
  1628. // andreas@flying-snail.de
  1629. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
  1630. }
  1631. snd_es1938_create_gameport(chip);
  1632. if ((err = snd_card_register(card)) < 0) {
  1633. snd_card_free(card);
  1634. return err;
  1635. }
  1636. pci_set_drvdata(pci, card);
  1637. dev++;
  1638. return 0;
  1639. }
  1640. static void __devexit snd_es1938_remove(struct pci_dev *pci)
  1641. {
  1642. snd_card_free(pci_get_drvdata(pci));
  1643. pci_set_drvdata(pci, NULL);
  1644. }
  1645. static struct pci_driver driver = {
  1646. .name = "ESS ES1938 (Solo-1)",
  1647. .id_table = snd_es1938_ids,
  1648. .probe = snd_es1938_probe,
  1649. .remove = __devexit_p(snd_es1938_remove),
  1650. #ifdef CONFIG_PM
  1651. .suspend = es1938_suspend,
  1652. .resume = es1938_resume,
  1653. #endif
  1654. };
  1655. static int __init alsa_card_es1938_init(void)
  1656. {
  1657. return pci_register_driver(&driver);
  1658. }
  1659. static void __exit alsa_card_es1938_exit(void)
  1660. {
  1661. pci_unregister_driver(&driver);
  1662. }
  1663. module_init(alsa_card_es1938_init)
  1664. module_exit(alsa_card_es1938_exit)