sis7019.c 38 KB

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