sis7019.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 const struct pci_device_id 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_SLEEP
  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 const struct snd_pcm_hardware sis_playback_hw_info = {
  146. .info = (SNDRV_PCM_INFO_MMAP |
  147. SNDRV_PCM_INFO_MMAP_VALID |
  148. SNDRV_PCM_INFO_INTERLEAVED |
  149. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  150. SNDRV_PCM_INFO_SYNC_START |
  151. SNDRV_PCM_INFO_RESUME),
  152. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  153. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  154. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
  155. .rate_min = 4000,
  156. .rate_max = 48000,
  157. .channels_min = 1,
  158. .channels_max = 2,
  159. .buffer_bytes_max = (0xfff9 * 4),
  160. .period_bytes_min = 9,
  161. .period_bytes_max = (0xfff9 * 4),
  162. .periods_min = 1,
  163. .periods_max = (0xfff9 / 9),
  164. };
  165. static const 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 = dma_map_single(&sis->pci->dev,
  344. sis->suspend_state[0],
  345. 4096, DMA_TO_DEVICE);
  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. dma_unmap_single(&sis->pci->dev, sis->silence_dma_addr, 4096,
  354. DMA_TO_DEVICE);
  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 const 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 const 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 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 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. iounmap(sis->ioaddr);
  922. pci_release_regions(sis->pci);
  923. pci_disable_device(sis->pci);
  924. sis_free_suspend(sis);
  925. return 0;
  926. }
  927. static int sis_dev_free(struct snd_device *dev)
  928. {
  929. struct sis7019 *sis = dev->device_data;
  930. return sis_chip_free(sis);
  931. }
  932. static int sis_chip_init(struct sis7019 *sis)
  933. {
  934. unsigned long io = sis->ioport;
  935. void __iomem *ioaddr = sis->ioaddr;
  936. unsigned long timeout;
  937. u16 status;
  938. int count;
  939. int i;
  940. /* Reset the audio controller
  941. */
  942. outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
  943. udelay(25);
  944. outl(0, io + SIS_GCR);
  945. /* Get the AC-link semaphore, and reset the codecs
  946. */
  947. count = 0xffff;
  948. while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
  949. udelay(1);
  950. if (!count)
  951. return -EIO;
  952. outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
  953. udelay(250);
  954. count = 0xffff;
  955. while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
  956. udelay(1);
  957. /* Command complete, we can let go of the semaphore now.
  958. */
  959. outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
  960. if (!count)
  961. return -EIO;
  962. /* Now that we've finished the reset, find out what's attached.
  963. * There are some codec/board combinations that take an extremely
  964. * long time to come up. 350+ ms has been observed in the field,
  965. * so we'll give them up to 500ms.
  966. */
  967. sis->codecs_present = 0;
  968. timeout = msecs_to_jiffies(500) + jiffies;
  969. while (time_before_eq(jiffies, timeout)) {
  970. status = inl(io + SIS_AC97_STATUS);
  971. if (status & SIS_AC97_STATUS_CODEC_READY)
  972. sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
  973. if (status & SIS_AC97_STATUS_CODEC2_READY)
  974. sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
  975. if (status & SIS_AC97_STATUS_CODEC3_READY)
  976. sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
  977. if (sis->codecs_present == codecs)
  978. break;
  979. msleep(1);
  980. }
  981. /* All done, check for errors.
  982. */
  983. if (!sis->codecs_present) {
  984. dev_err(&sis->pci->dev, "could not find any codecs\n");
  985. return -EIO;
  986. }
  987. if (sis->codecs_present != codecs) {
  988. dev_warn(&sis->pci->dev, "missing codecs, found %0x, expected %0x\n",
  989. sis->codecs_present, codecs);
  990. }
  991. /* Let the hardware know that the audio driver is alive,
  992. * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
  993. * record channels. We're going to want to use Variable Rate Audio
  994. * for recording, to avoid needlessly resampling from 48kHZ.
  995. */
  996. outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
  997. outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
  998. SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
  999. SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
  1000. SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
  1001. /* All AC97 PCM slots should be sourced from sub-mixer 0.
  1002. */
  1003. outl(0, io + SIS_AC97_PSR);
  1004. /* There is only one valid DMA setup for a PCI environment.
  1005. */
  1006. outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
  1007. /* Reset the synchronization groups for all of the channels
  1008. * to be asynchronous. If we start doing SPDIF or 5.1 sound, etc.
  1009. * we'll need to change how we handle these. Until then, we just
  1010. * assign sub-mixer 0 to all playback channels, and avoid any
  1011. * attenuation on the audio.
  1012. */
  1013. outl(0, io + SIS_PLAY_SYNC_GROUP_A);
  1014. outl(0, io + SIS_PLAY_SYNC_GROUP_B);
  1015. outl(0, io + SIS_PLAY_SYNC_GROUP_C);
  1016. outl(0, io + SIS_PLAY_SYNC_GROUP_D);
  1017. outl(0, io + SIS_MIXER_SYNC_GROUP);
  1018. for (i = 0; i < 64; i++) {
  1019. writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
  1020. writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
  1021. SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
  1022. }
  1023. /* Don't attenuate any audio set for the wave amplifier.
  1024. *
  1025. * FIXME: Maximum attenuation is set for the music amp, which will
  1026. * need to change if we start using the synth engine.
  1027. */
  1028. outl(0xffff0000, io + SIS_WEVCR);
  1029. /* Ensure that the wave engine is in normal operating mode.
  1030. */
  1031. outl(0, io + SIS_WECCR);
  1032. /* Go ahead and enable the DMA interrupts. They won't go live
  1033. * until we start a channel.
  1034. */
  1035. outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
  1036. SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
  1037. return 0;
  1038. }
  1039. #ifdef CONFIG_PM_SLEEP
  1040. static int sis_suspend(struct device *dev)
  1041. {
  1042. struct snd_card *card = dev_get_drvdata(dev);
  1043. struct sis7019 *sis = card->private_data;
  1044. void __iomem *ioaddr = sis->ioaddr;
  1045. int i;
  1046. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1047. snd_pcm_suspend_all(sis->pcm);
  1048. if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
  1049. snd_ac97_suspend(sis->ac97[0]);
  1050. if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
  1051. snd_ac97_suspend(sis->ac97[1]);
  1052. if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
  1053. snd_ac97_suspend(sis->ac97[2]);
  1054. /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
  1055. */
  1056. if (sis->irq >= 0) {
  1057. free_irq(sis->irq, sis);
  1058. sis->irq = -1;
  1059. }
  1060. /* Save the internal state away
  1061. */
  1062. for (i = 0; i < 4; i++) {
  1063. memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
  1064. ioaddr += 4096;
  1065. }
  1066. return 0;
  1067. }
  1068. static int sis_resume(struct device *dev)
  1069. {
  1070. struct pci_dev *pci = to_pci_dev(dev);
  1071. struct snd_card *card = dev_get_drvdata(dev);
  1072. struct sis7019 *sis = card->private_data;
  1073. void __iomem *ioaddr = sis->ioaddr;
  1074. int i;
  1075. if (sis_chip_init(sis)) {
  1076. dev_err(&pci->dev, "unable to re-init controller\n");
  1077. goto error;
  1078. }
  1079. if (request_irq(pci->irq, sis_interrupt, IRQF_SHARED,
  1080. KBUILD_MODNAME, sis)) {
  1081. dev_err(&pci->dev, "unable to regain IRQ %d\n", pci->irq);
  1082. goto error;
  1083. }
  1084. /* Restore saved state, then clear out the page we use for the
  1085. * silence buffer.
  1086. */
  1087. for (i = 0; i < 4; i++) {
  1088. memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
  1089. ioaddr += 4096;
  1090. }
  1091. memset(sis->suspend_state[0], 0, 4096);
  1092. sis->irq = pci->irq;
  1093. if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
  1094. snd_ac97_resume(sis->ac97[0]);
  1095. if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
  1096. snd_ac97_resume(sis->ac97[1]);
  1097. if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
  1098. snd_ac97_resume(sis->ac97[2]);
  1099. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1100. return 0;
  1101. error:
  1102. snd_card_disconnect(card);
  1103. return -EIO;
  1104. }
  1105. static SIMPLE_DEV_PM_OPS(sis_pm, sis_suspend, sis_resume);
  1106. #define SIS_PM_OPS &sis_pm
  1107. #else
  1108. #define SIS_PM_OPS NULL
  1109. #endif /* CONFIG_PM_SLEEP */
  1110. static int sis_alloc_suspend(struct sis7019 *sis)
  1111. {
  1112. int i;
  1113. /* We need 16K to store the internal wave engine state during a
  1114. * suspend, but we don't need it to be contiguous, so play nice
  1115. * with the memory system. We'll also use this area for a silence
  1116. * buffer.
  1117. */
  1118. for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
  1119. sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
  1120. if (!sis->suspend_state[i])
  1121. return -ENOMEM;
  1122. }
  1123. memset(sis->suspend_state[0], 0, 4096);
  1124. return 0;
  1125. }
  1126. static int sis_chip_create(struct snd_card *card,
  1127. struct pci_dev *pci)
  1128. {
  1129. struct sis7019 *sis = card->private_data;
  1130. struct voice *voice;
  1131. static struct snd_device_ops ops = {
  1132. .dev_free = sis_dev_free,
  1133. };
  1134. int rc;
  1135. int i;
  1136. rc = pci_enable_device(pci);
  1137. if (rc)
  1138. goto error_out;
  1139. rc = dma_set_mask(&pci->dev, DMA_BIT_MASK(30));
  1140. if (rc < 0) {
  1141. dev_err(&pci->dev, "architecture does not support 30-bit PCI busmaster DMA");
  1142. goto error_out_enabled;
  1143. }
  1144. memset(sis, 0, sizeof(*sis));
  1145. mutex_init(&sis->ac97_mutex);
  1146. spin_lock_init(&sis->voice_lock);
  1147. sis->card = card;
  1148. sis->pci = pci;
  1149. sis->irq = -1;
  1150. sis->ioport = pci_resource_start(pci, 0);
  1151. rc = pci_request_regions(pci, "SiS7019");
  1152. if (rc) {
  1153. dev_err(&pci->dev, "unable request regions\n");
  1154. goto error_out_enabled;
  1155. }
  1156. rc = -EIO;
  1157. sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
  1158. if (!sis->ioaddr) {
  1159. dev_err(&pci->dev, "unable to remap MMIO, aborting\n");
  1160. goto error_out_cleanup;
  1161. }
  1162. rc = sis_alloc_suspend(sis);
  1163. if (rc < 0) {
  1164. dev_err(&pci->dev, "unable to allocate state storage\n");
  1165. goto error_out_cleanup;
  1166. }
  1167. rc = sis_chip_init(sis);
  1168. if (rc)
  1169. goto error_out_cleanup;
  1170. rc = request_irq(pci->irq, sis_interrupt, IRQF_SHARED, KBUILD_MODNAME,
  1171. sis);
  1172. if (rc) {
  1173. dev_err(&pci->dev, "unable to allocate irq %d\n", sis->irq);
  1174. goto error_out_cleanup;
  1175. }
  1176. sis->irq = pci->irq;
  1177. pci_set_master(pci);
  1178. for (i = 0; i < 64; i++) {
  1179. voice = &sis->voices[i];
  1180. voice->num = i;
  1181. voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
  1182. voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
  1183. }
  1184. voice = &sis->capture_voice;
  1185. voice->flags = VOICE_CAPTURE;
  1186. voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
  1187. voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
  1188. rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
  1189. if (rc)
  1190. goto error_out_cleanup;
  1191. return 0;
  1192. error_out_cleanup:
  1193. sis_chip_free(sis);
  1194. error_out_enabled:
  1195. pci_disable_device(pci);
  1196. error_out:
  1197. return rc;
  1198. }
  1199. static int snd_sis7019_probe(struct pci_dev *pci,
  1200. const struct pci_device_id *pci_id)
  1201. {
  1202. struct snd_card *card;
  1203. struct sis7019 *sis;
  1204. int rc;
  1205. rc = -ENOENT;
  1206. if (!enable)
  1207. goto error_out;
  1208. /* The user can specify which codecs should be present so that we
  1209. * can wait for them to show up if they are slow to recover from
  1210. * the AC97 cold reset. We default to a single codec, the primary.
  1211. *
  1212. * We assume that SIS_PRIMARY_*_PRESENT matches bits 0-2.
  1213. */
  1214. codecs &= SIS_PRIMARY_CODEC_PRESENT | SIS_SECONDARY_CODEC_PRESENT |
  1215. SIS_TERTIARY_CODEC_PRESENT;
  1216. if (!codecs)
  1217. codecs = SIS_PRIMARY_CODEC_PRESENT;
  1218. rc = snd_card_new(&pci->dev, index, id, THIS_MODULE,
  1219. sizeof(*sis), &card);
  1220. if (rc < 0)
  1221. goto error_out;
  1222. strcpy(card->driver, "SiS7019");
  1223. strcpy(card->shortname, "SiS7019");
  1224. rc = sis_chip_create(card, pci);
  1225. if (rc)
  1226. goto card_error_out;
  1227. sis = card->private_data;
  1228. rc = sis_mixer_create(sis);
  1229. if (rc)
  1230. goto card_error_out;
  1231. rc = sis_pcm_create(sis);
  1232. if (rc)
  1233. goto card_error_out;
  1234. snprintf(card->longname, sizeof(card->longname),
  1235. "%s Audio Accelerator with %s at 0x%lx, irq %d",
  1236. card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
  1237. sis->ioport, sis->irq);
  1238. rc = snd_card_register(card);
  1239. if (rc)
  1240. goto card_error_out;
  1241. pci_set_drvdata(pci, card);
  1242. return 0;
  1243. card_error_out:
  1244. snd_card_free(card);
  1245. error_out:
  1246. return rc;
  1247. }
  1248. static void snd_sis7019_remove(struct pci_dev *pci)
  1249. {
  1250. snd_card_free(pci_get_drvdata(pci));
  1251. }
  1252. static struct pci_driver sis7019_driver = {
  1253. .name = KBUILD_MODNAME,
  1254. .id_table = snd_sis7019_ids,
  1255. .probe = snd_sis7019_probe,
  1256. .remove = snd_sis7019_remove,
  1257. .driver = {
  1258. .pm = SIS_PM_OPS,
  1259. },
  1260. };
  1261. module_pci_driver(sis7019_driver);