sis7019.c 39 KB

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