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