sis7019.c 39 KB

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