sis7019.c 38 KB

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