es1938.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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/module.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 bool 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 DEFINE_PCI_DEVICE_TABLE(snd_es1938_ids) = {
  212. { PCI_VDEVICE(ESS, 0x1969), 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("Beep 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, KBUILD_MODNAME, 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. KBUILD_MODNAME, 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,
  1632. MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
  1633. -1, &chip->rmidi) < 0) {
  1634. printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
  1635. } else {
  1636. // this line is vital for MIDI interrupt handling on ess-solo1
  1637. // andreas@flying-snail.de
  1638. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
  1639. }
  1640. snd_es1938_create_gameport(chip);
  1641. if ((err = snd_card_register(card)) < 0) {
  1642. snd_card_free(card);
  1643. return err;
  1644. }
  1645. pci_set_drvdata(pci, card);
  1646. dev++;
  1647. return 0;
  1648. }
  1649. static void __devexit snd_es1938_remove(struct pci_dev *pci)
  1650. {
  1651. snd_card_free(pci_get_drvdata(pci));
  1652. pci_set_drvdata(pci, NULL);
  1653. }
  1654. static struct pci_driver driver = {
  1655. .name = KBUILD_MODNAME,
  1656. .id_table = snd_es1938_ids,
  1657. .probe = snd_es1938_probe,
  1658. .remove = __devexit_p(snd_es1938_remove),
  1659. #ifdef CONFIG_PM
  1660. .suspend = es1938_suspend,
  1661. .resume = es1938_resume,
  1662. #endif
  1663. };
  1664. static int __init alsa_card_es1938_init(void)
  1665. {
  1666. return pci_register_driver(&driver);
  1667. }
  1668. static void __exit alsa_card_es1938_exit(void)
  1669. {
  1670. pci_unregister_driver(&driver);
  1671. }
  1672. module_init(alsa_card_es1938_init)
  1673. module_exit(alsa_card_es1938_exit)