sis7019.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * Driver for SiS7019 Audio Accelerator
  3. *
  4. * Copyright (C) 2004-2007, David Dillow
  5. * Written by David Dillow <dave@thedillows.org>
  6. * Inspired by the Trident 4D-WaveDX/NX driver.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, version 2.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/time.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <sound/core.h>
  30. #include <sound/ac97_codec.h>
  31. #include <sound/initval.h>
  32. #include "sis7019.h"
  33. MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
  34. MODULE_DESCRIPTION("SiS7019");
  35. MODULE_LICENSE("GPL");
  36. MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
  37. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  38. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  39. static int enable = 1;
  40. module_param(index, int, 0444);
  41. MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
  42. module_param(id, charp, 0444);
  43. MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
  44. module_param(enable, bool, 0444);
  45. MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
  46. static struct pci_device_id snd_sis7019_ids[] = {
  47. { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
  48. { 0, }
  49. };
  50. MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
  51. /* There are three timing modes for the voices.
  52. *
  53. * For both playback and capture, when the buffer is one or two periods long,
  54. * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
  55. * to let us know when the periods have ended.
  56. *
  57. * When performing playback with more than two periods per buffer, we set
  58. * the "Stop Sample Offset" and tell the hardware to interrupt us when we
  59. * reach it. We then update the offset and continue on until we are
  60. * interrupted for the next period.
  61. *
  62. * Capture channels do not have a SSO, so we allocate a playback channel to
  63. * use as a timer for the capture periods. We use the SSO on the playback
  64. * channel to clock out virtual periods, and adjust the virtual period length
  65. * to maintain synchronization. This algorithm came from the Trident driver.
  66. *
  67. * FIXME: It'd be nice to make use of some of the synth features in the
  68. * hardware, but a woeful lack of documentation is a significant roadblock.
  69. */
  70. struct voice {
  71. u16 flags;
  72. #define VOICE_IN_USE 1
  73. #define VOICE_CAPTURE 2
  74. #define VOICE_SSO_TIMING 4
  75. #define VOICE_SYNC_TIMING 8
  76. u16 sync_cso;
  77. u16 period_size;
  78. u16 buffer_size;
  79. u16 sync_period_size;
  80. u16 sync_buffer_size;
  81. u32 sso;
  82. u32 vperiod;
  83. struct snd_pcm_substream *substream;
  84. struct voice *timing;
  85. void __iomem *ctrl_base;
  86. void __iomem *wave_base;
  87. void __iomem *sync_base;
  88. int num;
  89. };
  90. /* We need four pages to store our wave parameters during a suspend. If
  91. * we're not doing power management, we still need to allocate a page
  92. * for the silence buffer.
  93. */
  94. #ifdef CONFIG_PM
  95. #define SIS_SUSPEND_PAGES 4
  96. #else
  97. #define SIS_SUSPEND_PAGES 1
  98. #endif
  99. struct sis7019 {
  100. unsigned long ioport;
  101. void __iomem *ioaddr;
  102. int irq;
  103. int codecs_present;
  104. struct pci_dev *pci;
  105. struct snd_pcm *pcm;
  106. struct snd_card *card;
  107. struct snd_ac97 *ac97[3];
  108. /* Protect against more than one thread hitting the AC97
  109. * registers (in a more polite manner than pounding the hardware
  110. * semaphore)
  111. */
  112. struct mutex ac97_mutex;
  113. /* voice_lock protects allocation/freeing of the voice descriptions
  114. */
  115. spinlock_t voice_lock;
  116. struct voice voices[64];
  117. struct voice capture_voice;
  118. /* Allocate pages to store the internal wave state during
  119. * suspends. When we're operating, this can be used as a silence
  120. * buffer for a timing channel.
  121. */
  122. void *suspend_state[SIS_SUSPEND_PAGES];
  123. int silence_users;
  124. dma_addr_t silence_dma_addr;
  125. };
  126. #define SIS_PRIMARY_CODEC_PRESENT 0x0001
  127. #define SIS_SECONDARY_CODEC_PRESENT 0x0002
  128. #define SIS_TERTIARY_CODEC_PRESENT 0x0004
  129. /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
  130. * documented range of 8-0xfff8 samples. Given that they are 0-based,
  131. * that places our period/buffer range at 9-0xfff9 samples. That makes the
  132. * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
  133. * max samples / min samples gives us the max periods in a buffer.
  134. *
  135. * We'll add a constraint upon open that limits the period and buffer sample
  136. * size to values that are legal for the hardware.
  137. */
  138. static struct snd_pcm_hardware sis_playback_hw_info = {
  139. .info = (SNDRV_PCM_INFO_MMAP |
  140. SNDRV_PCM_INFO_MMAP_VALID |
  141. SNDRV_PCM_INFO_INTERLEAVED |
  142. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  143. SNDRV_PCM_INFO_SYNC_START |
  144. SNDRV_PCM_INFO_RESUME),
  145. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  146. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  147. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
  148. .rate_min = 4000,
  149. .rate_max = 48000,
  150. .channels_min = 1,
  151. .channels_max = 2,
  152. .buffer_bytes_max = (0xfff9 * 4),
  153. .period_bytes_min = 9,
  154. .period_bytes_max = (0xfff9 * 4),
  155. .periods_min = 1,
  156. .periods_max = (0xfff9 / 9),
  157. };
  158. static struct snd_pcm_hardware sis_capture_hw_info = {
  159. .info = (SNDRV_PCM_INFO_MMAP |
  160. SNDRV_PCM_INFO_MMAP_VALID |
  161. SNDRV_PCM_INFO_INTERLEAVED |
  162. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  163. SNDRV_PCM_INFO_SYNC_START |
  164. SNDRV_PCM_INFO_RESUME),
  165. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  166. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  167. .rates = SNDRV_PCM_RATE_48000,
  168. .rate_min = 4000,
  169. .rate_max = 48000,
  170. .channels_min = 1,
  171. .channels_max = 2,
  172. .buffer_bytes_max = (0xfff9 * 4),
  173. .period_bytes_min = 9,
  174. .period_bytes_max = (0xfff9 * 4),
  175. .periods_min = 1,
  176. .periods_max = (0xfff9 / 9),
  177. };
  178. static void sis_update_sso(struct voice *voice, u16 period)
  179. {
  180. void __iomem *base = voice->ctrl_base;
  181. voice->sso += period;
  182. if (voice->sso >= voice->buffer_size)
  183. voice->sso -= voice->buffer_size;
  184. /* Enforce the documented hardware minimum offset */
  185. if (voice->sso < 8)
  186. voice->sso = 8;
  187. /* The SSO is in the upper 16 bits of the register. */
  188. writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
  189. }
  190. static void sis_update_voice(struct voice *voice)
  191. {
  192. if (voice->flags & VOICE_SSO_TIMING) {
  193. sis_update_sso(voice, voice->period_size);
  194. } else if (voice->flags & VOICE_SYNC_TIMING) {
  195. int sync;
  196. /* If we've not hit the end of the virtual period, update
  197. * our records and keep going.
  198. */
  199. if (voice->vperiod > voice->period_size) {
  200. voice->vperiod -= voice->period_size;
  201. if (voice->vperiod < voice->period_size)
  202. sis_update_sso(voice, voice->vperiod);
  203. else
  204. sis_update_sso(voice, voice->period_size);
  205. return;
  206. }
  207. /* Calculate our relative offset between the target and
  208. * the actual CSO value. Since we're operating in a loop,
  209. * if the value is more than half way around, we can
  210. * consider ourselves wrapped.
  211. */
  212. sync = voice->sync_cso;
  213. sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
  214. if (sync > (voice->sync_buffer_size / 2))
  215. sync -= voice->sync_buffer_size;
  216. /* If sync is positive, then we interrupted too early, and
  217. * we'll need to come back in a few samples and try again.
  218. * There's a minimum wait, as it takes some time for the DMA
  219. * engine to startup, etc...
  220. */
  221. if (sync > 0) {
  222. if (sync < 16)
  223. sync = 16;
  224. sis_update_sso(voice, sync);
  225. return;
  226. }
  227. /* Ok, we interrupted right on time, or (hopefully) just
  228. * a bit late. We'll adjst our next waiting period based
  229. * on how close we got.
  230. *
  231. * We need to stay just behind the actual channel to ensure
  232. * it really is past a period when we get our interrupt --
  233. * otherwise we'll fall into the early code above and have
  234. * a minimum wait time, which makes us quite late here,
  235. * eating into the user's time to refresh the buffer, esp.
  236. * if using small periods.
  237. *
  238. * If we're less than 9 samples behind, we're on target.
  239. */
  240. if (sync > -9)
  241. voice->vperiod = voice->sync_period_size + 1;
  242. else
  243. voice->vperiod = voice->sync_period_size - 4;
  244. if (voice->vperiod < voice->buffer_size) {
  245. sis_update_sso(voice, voice->vperiod);
  246. voice->vperiod = 0;
  247. } else
  248. sis_update_sso(voice, voice->period_size);
  249. sync = voice->sync_cso + voice->sync_period_size;
  250. if (sync >= voice->sync_buffer_size)
  251. sync -= voice->sync_buffer_size;
  252. voice->sync_cso = sync;
  253. }
  254. snd_pcm_period_elapsed(voice->substream);
  255. }
  256. static void sis_voice_irq(u32 status, struct voice *voice)
  257. {
  258. int bit;
  259. while (status) {
  260. bit = __ffs(status);
  261. status >>= bit + 1;
  262. voice += bit;
  263. sis_update_voice(voice);
  264. voice++;
  265. }
  266. }
  267. static irqreturn_t sis_interrupt(int irq, void *dev)
  268. {
  269. struct sis7019 *sis = dev;
  270. unsigned long io = sis->ioport;
  271. struct voice *voice;
  272. u32 intr, status;
  273. /* We only use the DMA interrupts, and we don't enable any other
  274. * source of interrupts. But, it is possible to see an interupt
  275. * status that didn't actually interrupt us, so eliminate anything
  276. * we're not expecting to avoid falsely claiming an IRQ, and an
  277. * ensuing endless loop.
  278. */
  279. intr = inl(io + SIS_GISR);
  280. intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
  281. SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
  282. if (!intr)
  283. return IRQ_NONE;
  284. do {
  285. status = inl(io + SIS_PISR_A);
  286. if (status) {
  287. sis_voice_irq(status, sis->voices);
  288. outl(status, io + SIS_PISR_A);
  289. }
  290. status = inl(io + SIS_PISR_B);
  291. if (status) {
  292. sis_voice_irq(status, &sis->voices[32]);
  293. outl(status, io + SIS_PISR_B);
  294. }
  295. status = inl(io + SIS_RISR);
  296. if (status) {
  297. voice = &sis->capture_voice;
  298. if (!voice->timing)
  299. snd_pcm_period_elapsed(voice->substream);
  300. outl(status, io + SIS_RISR);
  301. }
  302. outl(intr, io + SIS_GISR);
  303. intr = inl(io + SIS_GISR);
  304. intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
  305. SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
  306. } while (intr);
  307. return IRQ_HANDLED;
  308. }
  309. static u32 sis_rate_to_delta(unsigned int rate)
  310. {
  311. u32 delta;
  312. /* This was copied from the trident driver, but it seems its gotten
  313. * around a bit... nevertheless, it works well.
  314. *
  315. * We special case 44100 and 8000 since rounding with the equation
  316. * does not give us an accurate enough value. For 11025 and 22050
  317. * the equation gives us the best answer. All other frequencies will
  318. * also use the equation. JDW
  319. */
  320. if (rate == 44100)
  321. delta = 0xeb3;
  322. else if (rate == 8000)
  323. delta = 0x2ab;
  324. else if (rate == 48000)
  325. delta = 0x1000;
  326. else
  327. delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
  328. return delta;
  329. }
  330. static void __sis_map_silence(struct sis7019 *sis)
  331. {
  332. /* Helper function: must hold sis->voice_lock on entry */
  333. if (!sis->silence_users)
  334. sis->silence_dma_addr = pci_map_single(sis->pci,
  335. sis->suspend_state[0],
  336. 4096, PCI_DMA_TODEVICE);
  337. sis->silence_users++;
  338. }
  339. static void __sis_unmap_silence(struct sis7019 *sis)
  340. {
  341. /* Helper function: must hold sis->voice_lock on entry */
  342. sis->silence_users--;
  343. if (!sis->silence_users)
  344. pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096,
  345. PCI_DMA_TODEVICE);
  346. }
  347. static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
  348. {
  349. unsigned long flags;
  350. spin_lock_irqsave(&sis->voice_lock, flags);
  351. if (voice->timing) {
  352. __sis_unmap_silence(sis);
  353. voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
  354. VOICE_SYNC_TIMING);
  355. voice->timing = NULL;
  356. }
  357. voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
  358. spin_unlock_irqrestore(&sis->voice_lock, flags);
  359. }
  360. static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
  361. {
  362. /* Must hold the voice_lock on entry */
  363. struct voice *voice;
  364. int i;
  365. for (i = 0; i < 64; i++) {
  366. voice = &sis->voices[i];
  367. if (voice->flags & VOICE_IN_USE)
  368. continue;
  369. voice->flags |= VOICE_IN_USE;
  370. goto found_one;
  371. }
  372. voice = NULL;
  373. found_one:
  374. return voice;
  375. }
  376. static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
  377. {
  378. struct voice *voice;
  379. unsigned long flags;
  380. spin_lock_irqsave(&sis->voice_lock, flags);
  381. voice = __sis_alloc_playback_voice(sis);
  382. spin_unlock_irqrestore(&sis->voice_lock, flags);
  383. return voice;
  384. }
  385. static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
  386. struct snd_pcm_hw_params *hw_params)
  387. {
  388. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  389. struct snd_pcm_runtime *runtime = substream->runtime;
  390. struct voice *voice = runtime->private_data;
  391. unsigned int period_size, buffer_size;
  392. unsigned long flags;
  393. int needed;
  394. /* If there are one or two periods per buffer, we don't need a
  395. * timing voice, as we can use the capture channel's interrupts
  396. * to clock out the periods.
  397. */
  398. period_size = params_period_size(hw_params);
  399. buffer_size = params_buffer_size(hw_params);
  400. needed = (period_size != buffer_size &&
  401. period_size != (buffer_size / 2));
  402. if (needed && !voice->timing) {
  403. spin_lock_irqsave(&sis->voice_lock, flags);
  404. voice->timing = __sis_alloc_playback_voice(sis);
  405. if (voice->timing)
  406. __sis_map_silence(sis);
  407. spin_unlock_irqrestore(&sis->voice_lock, flags);
  408. if (!voice->timing)
  409. return -ENOMEM;
  410. voice->timing->substream = substream;
  411. } else if (!needed && voice->timing) {
  412. sis_free_voice(sis, voice);
  413. voice->timing = NULL;
  414. }
  415. return 0;
  416. }
  417. static int sis_playback_open(struct snd_pcm_substream *substream)
  418. {
  419. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  420. struct snd_pcm_runtime *runtime = substream->runtime;
  421. struct voice *voice;
  422. voice = sis_alloc_playback_voice(sis);
  423. if (!voice)
  424. return -EAGAIN;
  425. voice->substream = substream;
  426. runtime->private_data = voice;
  427. runtime->hw = sis_playback_hw_info;
  428. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  429. 9, 0xfff9);
  430. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  431. 9, 0xfff9);
  432. snd_pcm_set_sync(substream);
  433. return 0;
  434. }
  435. static int sis_substream_close(struct snd_pcm_substream *substream)
  436. {
  437. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  438. struct snd_pcm_runtime *runtime = substream->runtime;
  439. struct voice *voice = runtime->private_data;
  440. sis_free_voice(sis, voice);
  441. return 0;
  442. }
  443. static int sis_playback_hw_params(struct snd_pcm_substream *substream,
  444. struct snd_pcm_hw_params *hw_params)
  445. {
  446. return snd_pcm_lib_malloc_pages(substream,
  447. params_buffer_bytes(hw_params));
  448. }
  449. static int sis_hw_free(struct snd_pcm_substream *substream)
  450. {
  451. return snd_pcm_lib_free_pages(substream);
  452. }
  453. static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
  454. {
  455. struct snd_pcm_runtime *runtime = substream->runtime;
  456. struct voice *voice = runtime->private_data;
  457. void __iomem *ctrl_base = voice->ctrl_base;
  458. void __iomem *wave_base = voice->wave_base;
  459. u32 format, dma_addr, control, sso_eso, delta, reg;
  460. u16 leo;
  461. /* We rely on the PCM core to ensure that the parameters for this
  462. * substream do not change on us while we're programming the HW.
  463. */
  464. format = 0;
  465. if (snd_pcm_format_width(runtime->format) == 8)
  466. format |= SIS_PLAY_DMA_FORMAT_8BIT;
  467. if (!snd_pcm_format_signed(runtime->format))
  468. format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
  469. if (runtime->channels == 1)
  470. format |= SIS_PLAY_DMA_FORMAT_MONO;
  471. /* The baseline setup is for a single period per buffer, and
  472. * we add bells and whistles as needed from there.
  473. */
  474. dma_addr = runtime->dma_addr;
  475. leo = runtime->buffer_size - 1;
  476. control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
  477. sso_eso = leo;
  478. if (runtime->period_size == (runtime->buffer_size / 2)) {
  479. control |= SIS_PLAY_DMA_INTR_AT_MLP;
  480. } else if (runtime->period_size != runtime->buffer_size) {
  481. voice->flags |= VOICE_SSO_TIMING;
  482. voice->sso = runtime->period_size - 1;
  483. voice->period_size = runtime->period_size;
  484. voice->buffer_size = runtime->buffer_size;
  485. control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
  486. control |= SIS_PLAY_DMA_INTR_AT_SSO;
  487. sso_eso |= (runtime->period_size - 1) << 16;
  488. }
  489. delta = sis_rate_to_delta(runtime->rate);
  490. /* Ok, we're ready to go, set up the channel.
  491. */
  492. writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
  493. writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
  494. writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
  495. writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
  496. for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
  497. writel(0, wave_base + reg);
  498. writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
  499. writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
  500. writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
  501. SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
  502. SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
  503. wave_base + SIS_WAVE_CHANNEL_CONTROL);
  504. /* Force PCI writes to post. */
  505. readl(ctrl_base);
  506. return 0;
  507. }
  508. static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  509. {
  510. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  511. unsigned long io = sis->ioport;
  512. struct snd_pcm_substream *s;
  513. struct voice *voice;
  514. void *chip;
  515. int starting;
  516. u32 record = 0;
  517. u32 play[2] = { 0, 0 };
  518. /* No locks needed, as the PCM core will hold the locks on the
  519. * substreams, and the HW will only start/stop the indicated voices
  520. * without changing the state of the others.
  521. */
  522. switch (cmd) {
  523. case SNDRV_PCM_TRIGGER_START:
  524. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  525. case SNDRV_PCM_TRIGGER_RESUME:
  526. starting = 1;
  527. break;
  528. case SNDRV_PCM_TRIGGER_STOP:
  529. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  530. case SNDRV_PCM_TRIGGER_SUSPEND:
  531. starting = 0;
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. snd_pcm_group_for_each_entry(s, substream) {
  537. /* Make sure it is for us... */
  538. chip = snd_pcm_substream_chip(s);
  539. if (chip != sis)
  540. continue;
  541. voice = s->runtime->private_data;
  542. if (voice->flags & VOICE_CAPTURE) {
  543. record |= 1 << voice->num;
  544. voice = voice->timing;
  545. }
  546. /* voice could be NULL if this a recording stream, and it
  547. * doesn't have an external timing channel.
  548. */
  549. if (voice)
  550. play[voice->num / 32] |= 1 << (voice->num & 0x1f);
  551. snd_pcm_trigger_done(s, substream);
  552. }
  553. if (starting) {
  554. if (record)
  555. outl(record, io + SIS_RECORD_START_REG);
  556. if (play[0])
  557. outl(play[0], io + SIS_PLAY_START_A_REG);
  558. if (play[1])
  559. outl(play[1], io + SIS_PLAY_START_B_REG);
  560. } else {
  561. if (record)
  562. outl(record, io + SIS_RECORD_STOP_REG);
  563. if (play[0])
  564. outl(play[0], io + SIS_PLAY_STOP_A_REG);
  565. if (play[1])
  566. outl(play[1], io + SIS_PLAY_STOP_B_REG);
  567. }
  568. return 0;
  569. }
  570. static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
  571. {
  572. struct snd_pcm_runtime *runtime = substream->runtime;
  573. struct voice *voice = runtime->private_data;
  574. u32 cso;
  575. cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
  576. cso &= 0xffff;
  577. return cso;
  578. }
  579. static int sis_capture_open(struct snd_pcm_substream *substream)
  580. {
  581. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  582. struct snd_pcm_runtime *runtime = substream->runtime;
  583. struct voice *voice = &sis->capture_voice;
  584. unsigned long flags;
  585. /* FIXME: The driver only supports recording from one channel
  586. * at the moment, but it could support more.
  587. */
  588. spin_lock_irqsave(&sis->voice_lock, flags);
  589. if (voice->flags & VOICE_IN_USE)
  590. voice = NULL;
  591. else
  592. voice->flags |= VOICE_IN_USE;
  593. spin_unlock_irqrestore(&sis->voice_lock, flags);
  594. if (!voice)
  595. return -EAGAIN;
  596. voice->substream = substream;
  597. runtime->private_data = voice;
  598. runtime->hw = sis_capture_hw_info;
  599. runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
  600. snd_pcm_limit_hw_rates(runtime);
  601. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  602. 9, 0xfff9);
  603. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  604. 9, 0xfff9);
  605. snd_pcm_set_sync(substream);
  606. return 0;
  607. }
  608. static int sis_capture_hw_params(struct snd_pcm_substream *substream,
  609. struct snd_pcm_hw_params *hw_params)
  610. {
  611. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  612. int rc;
  613. rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
  614. params_rate(hw_params));
  615. if (rc)
  616. goto out;
  617. rc = snd_pcm_lib_malloc_pages(substream,
  618. params_buffer_bytes(hw_params));
  619. if (rc < 0)
  620. goto out;
  621. rc = sis_alloc_timing_voice(substream, hw_params);
  622. out:
  623. return rc;
  624. }
  625. static void sis_prepare_timing_voice(struct voice *voice,
  626. struct snd_pcm_substream *substream)
  627. {
  628. struct sis7019 *sis = snd_pcm_substream_chip(substream);
  629. struct snd_pcm_runtime *runtime = substream->runtime;
  630. struct voice *timing = voice->timing;
  631. void __iomem *play_base = timing->ctrl_base;
  632. void __iomem *wave_base = timing->wave_base;
  633. u16 buffer_size, period_size;
  634. u32 format, control, sso_eso, delta;
  635. u32 vperiod, sso, reg;
  636. /* Set our initial buffer and period as large as we can given a
  637. * single page of silence.
  638. */
  639. buffer_size = 4096 / runtime->channels;
  640. buffer_size /= snd_pcm_format_size(runtime->format, 1);
  641. period_size = buffer_size;
  642. /* Initially, we want to interrupt just a bit behind the end of
  643. * the period we're clocking out. 10 samples seems to give a good
  644. * delay.
  645. *
  646. * We want to spread our interrupts throughout the virtual period,
  647. * so that we don't end up with two interrupts back to back at the
  648. * end -- this helps minimize the effects of any jitter. Adjust our
  649. * clocking period size so that the last period is at least a fourth
  650. * of a full period.
  651. *
  652. * This is all moot if we don't need to use virtual periods.
  653. */
  654. vperiod = runtime->period_size + 10;
  655. if (vperiod > period_size) {
  656. u16 tail = vperiod % period_size;
  657. u16 quarter_period = period_size / 4;
  658. if (tail && tail < quarter_period) {
  659. u16 loops = vperiod / period_size;
  660. tail = quarter_period - tail;
  661. tail += loops - 1;
  662. tail /= loops;
  663. period_size -= tail;
  664. }
  665. sso = period_size - 1;
  666. } else {
  667. /* The initial period will fit inside the buffer, so we
  668. * don't need to use virtual periods -- disable them.
  669. */
  670. period_size = runtime->period_size;
  671. sso = vperiod - 1;
  672. vperiod = 0;
  673. }
  674. /* The interrupt handler implements the timing syncronization, so
  675. * setup its state.
  676. */
  677. timing->flags |= VOICE_SYNC_TIMING;
  678. timing->sync_base = voice->ctrl_base;
  679. timing->sync_cso = runtime->period_size - 1;
  680. timing->sync_period_size = runtime->period_size;
  681. timing->sync_buffer_size = runtime->buffer_size;
  682. timing->period_size = period_size;
  683. timing->buffer_size = buffer_size;
  684. timing->sso = sso;
  685. timing->vperiod = vperiod;
  686. /* Using unsigned samples with the all-zero silence buffer
  687. * forces the output to the lower rail, killing playback.
  688. * So ignore unsigned vs signed -- it doesn't change the timing.
  689. */
  690. format = 0;
  691. if (snd_pcm_format_width(runtime->format) == 8)
  692. format = SIS_CAPTURE_DMA_FORMAT_8BIT;
  693. if (runtime->channels == 1)
  694. format |= SIS_CAPTURE_DMA_FORMAT_MONO;
  695. control = timing->buffer_size - 1;
  696. control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
  697. sso_eso = timing->buffer_size - 1;
  698. sso_eso |= timing->sso << 16;
  699. delta = sis_rate_to_delta(runtime->rate);
  700. /* We've done the math, now configure the channel.
  701. */
  702. writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
  703. writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
  704. writel(control, play_base + SIS_PLAY_DMA_CONTROL);
  705. writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
  706. for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
  707. writel(0, wave_base + reg);
  708. writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
  709. writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
  710. writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
  711. SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
  712. SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
  713. wave_base + SIS_WAVE_CHANNEL_CONTROL);
  714. }
  715. static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
  716. {
  717. struct snd_pcm_runtime *runtime = substream->runtime;
  718. struct voice *voice = runtime->private_data;
  719. void __iomem *rec_base = voice->ctrl_base;
  720. u32 format, dma_addr, control;
  721. u16 leo;
  722. /* We rely on the PCM core to ensure that the parameters for this
  723. * substream do not change on us while we're programming the HW.
  724. */
  725. format = 0;
  726. if (snd_pcm_format_width(runtime->format) == 8)
  727. format = SIS_CAPTURE_DMA_FORMAT_8BIT;
  728. if (!snd_pcm_format_signed(runtime->format))
  729. format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
  730. if (runtime->channels == 1)
  731. format |= SIS_CAPTURE_DMA_FORMAT_MONO;
  732. dma_addr = runtime->dma_addr;
  733. leo = runtime->buffer_size - 1;
  734. control = leo | SIS_CAPTURE_DMA_LOOP;
  735. /* If we've got more than two periods per buffer, then we have
  736. * use a timing voice to clock out the periods. Otherwise, we can
  737. * use the capture channel's interrupts.
  738. */
  739. if (voice->timing) {
  740. sis_prepare_timing_voice(voice, substream);
  741. } else {
  742. control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
  743. if (runtime->period_size != runtime->buffer_size)
  744. control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
  745. }
  746. writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
  747. writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
  748. writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
  749. /* Force the writes to post. */
  750. readl(rec_base);
  751. return 0;
  752. }
  753. static struct snd_pcm_ops sis_playback_ops = {
  754. .open = sis_playback_open,
  755. .close = sis_substream_close,
  756. .ioctl = snd_pcm_lib_ioctl,
  757. .hw_params = sis_playback_hw_params,
  758. .hw_free = sis_hw_free,
  759. .prepare = sis_pcm_playback_prepare,
  760. .trigger = sis_pcm_trigger,
  761. .pointer = sis_pcm_pointer,
  762. };
  763. static struct snd_pcm_ops sis_capture_ops = {
  764. .open = sis_capture_open,
  765. .close = sis_substream_close,
  766. .ioctl = snd_pcm_lib_ioctl,
  767. .hw_params = sis_capture_hw_params,
  768. .hw_free = sis_hw_free,
  769. .prepare = sis_pcm_capture_prepare,
  770. .trigger = sis_pcm_trigger,
  771. .pointer = sis_pcm_pointer,
  772. };
  773. static int __devinit sis_pcm_create(struct sis7019 *sis)
  774. {
  775. struct snd_pcm *pcm;
  776. int rc;
  777. /* We have 64 voices, and the driver currently records from
  778. * only one channel, though that could change in the future.
  779. */
  780. rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
  781. if (rc)
  782. return rc;
  783. pcm->private_data = sis;
  784. strcpy(pcm->name, "SiS7019");
  785. sis->pcm = pcm;
  786. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
  787. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
  788. /* Try to preallocate some memory, but it's not the end of the
  789. * world if this fails.
  790. */
  791. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  792. snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
  793. return 0;
  794. }
  795. static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
  796. {
  797. unsigned long io = sis->ioport;
  798. unsigned short val = 0xffff;
  799. u16 status;
  800. u16 rdy;
  801. int count;
  802. static const u16 codec_ready[3] = {
  803. SIS_AC97_STATUS_CODEC_READY,
  804. SIS_AC97_STATUS_CODEC2_READY,
  805. SIS_AC97_STATUS_CODEC3_READY,
  806. };
  807. rdy = codec_ready[codec];
  808. /* Get the AC97 semaphore -- software first, so we don't spin
  809. * pounding out IO reads on the hardware semaphore...
  810. */
  811. mutex_lock(&sis->ac97_mutex);
  812. count = 0xffff;
  813. while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
  814. udelay(1);
  815. if (!count)
  816. goto timeout;
  817. /* ... and wait for any outstanding commands to complete ...
  818. */
  819. count = 0xffff;
  820. do {
  821. status = inw(io + SIS_AC97_STATUS);
  822. if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
  823. break;
  824. udelay(1);
  825. } while (--count);
  826. if (!count)
  827. goto timeout_sema;
  828. /* ... before sending our command and waiting for it to finish ...
  829. */
  830. outl(cmd, io + SIS_AC97_CMD);
  831. udelay(10);
  832. count = 0xffff;
  833. while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
  834. udelay(1);
  835. /* ... and reading the results (if any).
  836. */
  837. val = inl(io + SIS_AC97_CMD) >> 16;
  838. timeout_sema:
  839. outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
  840. timeout:
  841. mutex_unlock(&sis->ac97_mutex);
  842. if (!count) {
  843. printk(KERN_ERR "sis7019: ac97 codec %d timeout cmd 0x%08x\n",
  844. codec, cmd);
  845. }
  846. return val;
  847. }
  848. static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  849. unsigned short val)
  850. {
  851. static const u32 cmd[3] = {
  852. SIS_AC97_CMD_CODEC_WRITE,
  853. SIS_AC97_CMD_CODEC2_WRITE,
  854. SIS_AC97_CMD_CODEC3_WRITE,
  855. };
  856. sis_ac97_rw(ac97->private_data, ac97->num,
  857. (val << 16) | (reg << 8) | cmd[ac97->num]);
  858. }
  859. static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  860. {
  861. static const u32 cmd[3] = {
  862. SIS_AC97_CMD_CODEC_READ,
  863. SIS_AC97_CMD_CODEC2_READ,
  864. SIS_AC97_CMD_CODEC3_READ,
  865. };
  866. return sis_ac97_rw(ac97->private_data, ac97->num,
  867. (reg << 8) | cmd[ac97->num]);
  868. }
  869. static int __devinit sis_mixer_create(struct sis7019 *sis)
  870. {
  871. struct snd_ac97_bus *bus;
  872. struct snd_ac97_template ac97;
  873. static struct snd_ac97_bus_ops ops = {
  874. .write = sis_ac97_write,
  875. .read = sis_ac97_read,
  876. };
  877. int rc;
  878. memset(&ac97, 0, sizeof(ac97));
  879. ac97.private_data = sis;
  880. rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
  881. if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
  882. rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
  883. ac97.num = 1;
  884. if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
  885. rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
  886. ac97.num = 2;
  887. if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
  888. rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
  889. /* If we return an error here, then snd_card_free() should
  890. * free up any ac97 codecs that got created, as well as the bus.
  891. */
  892. return rc;
  893. }
  894. static void sis_free_suspend(struct sis7019 *sis)
  895. {
  896. int i;
  897. for (i = 0; i < SIS_SUSPEND_PAGES; i++)
  898. kfree(sis->suspend_state[i]);
  899. }
  900. static int sis_chip_free(struct sis7019 *sis)
  901. {
  902. /* Reset the chip, and disable all interrputs.
  903. */
  904. outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
  905. udelay(10);
  906. outl(0, sis->ioport + SIS_GCR);
  907. outl(0, sis->ioport + SIS_GIER);
  908. /* Now, free everything we allocated.
  909. */
  910. if (sis->irq >= 0)
  911. free_irq(sis->irq, sis);
  912. if (sis->ioaddr)
  913. iounmap(sis->ioaddr);
  914. pci_release_regions(sis->pci);
  915. pci_disable_device(sis->pci);
  916. sis_free_suspend(sis);
  917. return 0;
  918. }
  919. static int sis_dev_free(struct snd_device *dev)
  920. {
  921. struct sis7019 *sis = dev->device_data;
  922. return sis_chip_free(sis);
  923. }
  924. static int sis_chip_init(struct sis7019 *sis)
  925. {
  926. unsigned long io = sis->ioport;
  927. void __iomem *ioaddr = sis->ioaddr;
  928. u16 status;
  929. int count;
  930. int i;
  931. /* Reset the audio controller
  932. */
  933. outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
  934. udelay(10);
  935. outl(0, io + SIS_GCR);
  936. /* Get the AC-link semaphore, and reset the codecs
  937. */
  938. count = 0xffff;
  939. while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
  940. udelay(1);
  941. if (!count)
  942. return -EIO;
  943. outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
  944. udelay(10);
  945. count = 0xffff;
  946. while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
  947. udelay(1);
  948. /* Now that we've finished the reset, find out what's attached.
  949. */
  950. status = inl(io + SIS_AC97_STATUS);
  951. if (status & SIS_AC97_STATUS_CODEC_READY)
  952. sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
  953. if (status & SIS_AC97_STATUS_CODEC2_READY)
  954. sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
  955. if (status & SIS_AC97_STATUS_CODEC3_READY)
  956. sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
  957. /* All done, let go of the semaphore, and check for errors
  958. */
  959. outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
  960. if (!sis->codecs_present || !count)
  961. return -EIO;
  962. /* Let the hardware know that the audio driver is alive,
  963. * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
  964. * record channels. We're going to want to use Variable Rate Audio
  965. * for recording, to avoid needlessly resampling from 48kHZ.
  966. */
  967. outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
  968. outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
  969. SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
  970. SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
  971. SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
  972. /* All AC97 PCM slots should be sourced from sub-mixer 0.
  973. */
  974. outl(0, io + SIS_AC97_PSR);
  975. /* There is only one valid DMA setup for a PCI environment.
  976. */
  977. outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
  978. /* Reset the syncronization groups for all of the channels
  979. * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc.
  980. * we'll need to change how we handle these. Until then, we just
  981. * assign sub-mixer 0 to all playback channels, and avoid any
  982. * attenuation on the audio.
  983. */
  984. outl(0, io + SIS_PLAY_SYNC_GROUP_A);
  985. outl(0, io + SIS_PLAY_SYNC_GROUP_B);
  986. outl(0, io + SIS_PLAY_SYNC_GROUP_C);
  987. outl(0, io + SIS_PLAY_SYNC_GROUP_D);
  988. outl(0, io + SIS_MIXER_SYNC_GROUP);
  989. for (i = 0; i < 64; i++) {
  990. writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
  991. writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
  992. SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
  993. }
  994. /* Don't attenuate any audio set for the wave amplifier.
  995. *
  996. * FIXME: Maximum attenuation is set for the music amp, which will
  997. * need to change if we start using the synth engine.
  998. */
  999. outl(0xffff0000, io + SIS_WEVCR);
  1000. /* Ensure that the wave engine is in normal operating mode.
  1001. */
  1002. outl(0, io + SIS_WECCR);
  1003. /* Go ahead and enable the DMA interrupts. They won't go live
  1004. * until we start a channel.
  1005. */
  1006. outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
  1007. SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
  1008. return 0;
  1009. }
  1010. #ifdef CONFIG_PM
  1011. static int sis_suspend(struct pci_dev *pci, pm_message_t state)
  1012. {
  1013. struct snd_card *card = pci_get_drvdata(pci);
  1014. struct sis7019 *sis = card->private_data;
  1015. void __iomem *ioaddr = sis->ioaddr;
  1016. int i;
  1017. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1018. snd_pcm_suspend_all(sis->pcm);
  1019. if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
  1020. snd_ac97_suspend(sis->ac97[0]);
  1021. if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
  1022. snd_ac97_suspend(sis->ac97[1]);
  1023. if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
  1024. snd_ac97_suspend(sis->ac97[2]);
  1025. /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
  1026. */
  1027. if (sis->irq >= 0) {
  1028. free_irq(sis->irq, sis);
  1029. sis->irq = -1;
  1030. }
  1031. /* Save the internal state away
  1032. */
  1033. for (i = 0; i < 4; i++) {
  1034. memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
  1035. ioaddr += 4096;
  1036. }
  1037. pci_disable_device(pci);
  1038. pci_save_state(pci);
  1039. pci_set_power_state(pci, pci_choose_state(pci, state));
  1040. return 0;
  1041. }
  1042. static int sis_resume(struct pci_dev *pci)
  1043. {
  1044. struct snd_card *card = pci_get_drvdata(pci);
  1045. struct sis7019 *sis = card->private_data;
  1046. void __iomem *ioaddr = sis->ioaddr;
  1047. int i;
  1048. pci_set_power_state(pci, PCI_D0);
  1049. pci_restore_state(pci);
  1050. if (pci_enable_device(pci) < 0) {
  1051. printk(KERN_ERR "sis7019: unable to re-enable device\n");
  1052. goto error;
  1053. }
  1054. if (sis_chip_init(sis)) {
  1055. printk(KERN_ERR "sis7019: unable to re-init controller\n");
  1056. goto error;
  1057. }
  1058. if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
  1059. card->shortname, sis)) {
  1060. printk(KERN_ERR "sis7019: unable to regain IRQ %d\n", pci->irq);
  1061. goto error;
  1062. }
  1063. /* Restore saved state, then clear out the page we use for the
  1064. * silence buffer.
  1065. */
  1066. for (i = 0; i < 4; i++) {
  1067. memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
  1068. ioaddr += 4096;
  1069. }
  1070. memset(sis->suspend_state[0], 0, 4096);
  1071. sis->irq = pci->irq;
  1072. pci_set_master(pci);
  1073. if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
  1074. snd_ac97_resume(sis->ac97[0]);
  1075. if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
  1076. snd_ac97_resume(sis->ac97[1]);
  1077. if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
  1078. snd_ac97_resume(sis->ac97[2]);
  1079. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1080. return 0;
  1081. error:
  1082. snd_card_disconnect(card);
  1083. return -EIO;
  1084. }
  1085. #endif /* CONFIG_PM */
  1086. static int sis_alloc_suspend(struct sis7019 *sis)
  1087. {
  1088. int i;
  1089. /* We need 16K to store the internal wave engine state during a
  1090. * suspend, but we don't need it to be contiguous, so play nice
  1091. * with the memory system. We'll also use this area for a silence
  1092. * buffer.
  1093. */
  1094. for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
  1095. sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
  1096. if (!sis->suspend_state[i])
  1097. return -ENOMEM;
  1098. }
  1099. memset(sis->suspend_state[0], 0, 4096);
  1100. return 0;
  1101. }
  1102. static int __devinit sis_chip_create(struct snd_card *card,
  1103. struct pci_dev *pci)
  1104. {
  1105. struct sis7019 *sis = card->private_data;
  1106. struct voice *voice;
  1107. static struct snd_device_ops ops = {
  1108. .dev_free = sis_dev_free,
  1109. };
  1110. int rc;
  1111. int i;
  1112. rc = pci_enable_device(pci);
  1113. if (rc)
  1114. goto error_out;
  1115. if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0) {
  1116. printk(KERN_ERR "sis7019: architecture does not support "
  1117. "30-bit PCI busmaster DMA");
  1118. goto error_out_enabled;
  1119. }
  1120. memset(sis, 0, sizeof(*sis));
  1121. mutex_init(&sis->ac97_mutex);
  1122. spin_lock_init(&sis->voice_lock);
  1123. sis->card = card;
  1124. sis->pci = pci;
  1125. sis->irq = -1;
  1126. sis->ioport = pci_resource_start(pci, 0);
  1127. rc = pci_request_regions(pci, "SiS7019");
  1128. if (rc) {
  1129. printk(KERN_ERR "sis7019: unable request regions\n");
  1130. goto error_out_enabled;
  1131. }
  1132. rc = -EIO;
  1133. sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
  1134. if (!sis->ioaddr) {
  1135. printk(KERN_ERR "sis7019: unable to remap MMIO, aborting\n");
  1136. goto error_out_cleanup;
  1137. }
  1138. rc = sis_alloc_suspend(sis);
  1139. if (rc < 0) {
  1140. printk(KERN_ERR "sis7019: unable to allocate state storage\n");
  1141. goto error_out_cleanup;
  1142. }
  1143. rc = sis_chip_init(sis);
  1144. if (rc)
  1145. goto error_out_cleanup;
  1146. if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
  1147. card->shortname, sis)) {
  1148. printk(KERN_ERR "unable to allocate irq %d\n", sis->irq);
  1149. goto error_out_cleanup;
  1150. }
  1151. sis->irq = pci->irq;
  1152. pci_set_master(pci);
  1153. for (i = 0; i < 64; i++) {
  1154. voice = &sis->voices[i];
  1155. voice->num = i;
  1156. voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
  1157. voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
  1158. }
  1159. voice = &sis->capture_voice;
  1160. voice->flags = VOICE_CAPTURE;
  1161. voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
  1162. voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
  1163. rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
  1164. if (rc)
  1165. goto error_out_cleanup;
  1166. snd_card_set_dev(card, &pci->dev);
  1167. return 0;
  1168. error_out_cleanup:
  1169. sis_chip_free(sis);
  1170. error_out_enabled:
  1171. pci_disable_device(pci);
  1172. error_out:
  1173. return rc;
  1174. }
  1175. static int __devinit snd_sis7019_probe(struct pci_dev *pci,
  1176. const struct pci_device_id *pci_id)
  1177. {
  1178. struct snd_card *card;
  1179. struct sis7019 *sis;
  1180. int rc;
  1181. rc = -ENOENT;
  1182. if (!enable)
  1183. goto error_out;
  1184. rc = snd_card_create(index, id, THIS_MODULE, sizeof(*sis), &card);
  1185. if (rc < 0)
  1186. goto error_out;
  1187. strcpy(card->driver, "SiS7019");
  1188. strcpy(card->shortname, "SiS7019");
  1189. rc = sis_chip_create(card, pci);
  1190. if (rc)
  1191. goto card_error_out;
  1192. sis = card->private_data;
  1193. rc = sis_mixer_create(sis);
  1194. if (rc)
  1195. goto card_error_out;
  1196. rc = sis_pcm_create(sis);
  1197. if (rc)
  1198. goto card_error_out;
  1199. snprintf(card->longname, sizeof(card->longname),
  1200. "%s Audio Accelerator with %s at 0x%lx, irq %d",
  1201. card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
  1202. sis->ioport, sis->irq);
  1203. rc = snd_card_register(card);
  1204. if (rc)
  1205. goto card_error_out;
  1206. pci_set_drvdata(pci, card);
  1207. return 0;
  1208. card_error_out:
  1209. snd_card_free(card);
  1210. error_out:
  1211. return rc;
  1212. }
  1213. static void __devexit snd_sis7019_remove(struct pci_dev *pci)
  1214. {
  1215. snd_card_free(pci_get_drvdata(pci));
  1216. pci_set_drvdata(pci, NULL);
  1217. }
  1218. static struct pci_driver sis7019_driver = {
  1219. .name = "SiS7019",
  1220. .id_table = snd_sis7019_ids,
  1221. .probe = snd_sis7019_probe,
  1222. .remove = __devexit_p(snd_sis7019_remove),
  1223. #ifdef CONFIG_PM
  1224. .suspend = sis_suspend,
  1225. .resume = sis_resume,
  1226. #endif
  1227. };
  1228. static int __init sis7019_init(void)
  1229. {
  1230. return pci_register_driver(&sis7019_driver);
  1231. }
  1232. static void __exit sis7019_exit(void)
  1233. {
  1234. pci_unregister_driver(&sis7019_driver);
  1235. }
  1236. module_init(sis7019_init);
  1237. module_exit(sis7019_exit);