~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/sis7019.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php