sis7019.c 40 KB

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