ali5451.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390
  1. /*
  2. * Matt Wu <Matt_Wu@acersoftech.com.cn>
  3. * Apr 26, 2001
  4. * Routines for control of ALi pci audio M5451
  5. *
  6. * BUGS:
  7. * --
  8. *
  9. * TODO:
  10. * --
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public Lcodecnse as published by
  14. * the Free Software Foundation; either version 2 of the Lcodecnse, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public Lcodecnse for more details.
  21. *
  22. * You should have received a copy of the GNU General Public Lcodecnse
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <sound/driver.h>
  28. #include <asm/io.h>
  29. #include <linux/delay.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/slab.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/dma-mapping.h>
  36. #include <sound/core.h>
  37. #include <sound/pcm.h>
  38. #include <sound/info.h>
  39. #include <sound/ac97_codec.h>
  40. #include <sound/mpu401.h>
  41. #include <sound/initval.h>
  42. MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
  43. MODULE_DESCRIPTION("ALI M5451");
  44. MODULE_LICENSE("GPL");
  45. MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
  46. static int index = SNDRV_DEFAULT_IDX1; /* Index */
  47. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  48. static int pcm_channels = 32;
  49. static int spdif;
  50. module_param(index, int, 0444);
  51. MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
  52. module_param(id, charp, 0444);
  53. MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
  54. module_param(pcm_channels, int, 0444);
  55. MODULE_PARM_DESC(pcm_channels, "PCM Channels");
  56. module_param(spdif, bool, 0444);
  57. MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
  58. /* just for backward compatibility */
  59. static int enable;
  60. module_param(enable, bool, 0444);
  61. /*
  62. * Debug part definitions
  63. */
  64. /* #define ALI_DEBUG */
  65. #ifdef ALI_DEBUG
  66. #define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
  67. #else
  68. #define snd_ali_printk(format, args...)
  69. #endif
  70. /*
  71. * Constants definition
  72. */
  73. #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
  74. #define ALI_CHANNELS 32
  75. #define ALI_PCM_IN_CHANNEL 31
  76. #define ALI_SPDIF_IN_CHANNEL 19
  77. #define ALI_SPDIF_OUT_CHANNEL 15
  78. #define ALI_CENTER_CHANNEL 24
  79. #define ALI_LEF_CHANNEL 23
  80. #define ALI_SURR_LEFT_CHANNEL 26
  81. #define ALI_SURR_RIGHT_CHANNEL 25
  82. #define ALI_MODEM_IN_CHANNEL 21
  83. #define ALI_MODEM_OUT_CHANNEL 20
  84. #define SNDRV_ALI_VOICE_TYPE_PCM 01
  85. #define SNDRV_ALI_VOICE_TYPE_OTH 02
  86. #define ALI_5451_V02 0x02
  87. /*
  88. * Direct Registers
  89. */
  90. #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
  91. #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
  92. #define ALI_LEGACY_DMAR11 0x0b /* MOD */
  93. #define ALI_LEGACY_DMAR15 0x0f /* MMR */
  94. #define ALI_MPUR0 0x20
  95. #define ALI_MPUR1 0x21
  96. #define ALI_MPUR2 0x22
  97. #define ALI_MPUR3 0x23
  98. #define ALI_AC97_WRITE 0x40
  99. #define ALI_AC97_READ 0x44
  100. #define ALI_SCTRL 0x48
  101. #define ALI_SPDIF_OUT_ENABLE 0x20
  102. #define ALI_SCTRL_LINE_IN2 (1 << 9)
  103. #define ALI_SCTRL_GPIO_IN2 (1 << 13)
  104. #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
  105. #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
  106. #define ALI_SCTRL_CODEC1_READY (1 << 24)
  107. #define ALI_SCTRL_CODEC2_READY (1 << 25)
  108. #define ALI_AC97_GPIO 0x4c
  109. #define ALI_AC97_GPIO_ENABLE 0x8000
  110. #define ALI_AC97_GPIO_DATA_SHIFT 16
  111. #define ALI_SPDIF_CS 0x70
  112. #define ALI_SPDIF_CTRL 0x74
  113. #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
  114. #define ALI_SPDIF_IN_CH_STATUS 0x40
  115. #define ALI_SPDIF_OUT_CH_STATUS 0xbf
  116. #define ALI_START 0x80
  117. #define ALI_STOP 0x84
  118. #define ALI_CSPF 0x90
  119. #define ALI_AINT 0x98
  120. #define ALI_GC_CIR 0xa0
  121. #define ENDLP_IE 0x00001000
  122. #define MIDLP_IE 0x00002000
  123. #define ALI_AINTEN 0xa4
  124. #define ALI_VOLUME 0xa8
  125. #define ALI_SBDELTA_DELTA_R 0xac
  126. #define ALI_MISCINT 0xb0
  127. #define ADDRESS_IRQ 0x00000020
  128. #define TARGET_REACHED 0x00008000
  129. #define MIXER_OVERFLOW 0x00000800
  130. #define MIXER_UNDERFLOW 0x00000400
  131. #define GPIO_IRQ 0x01000000
  132. #define ALI_SBBL_SBCL 0xc0
  133. #define ALI_SBCTRL_SBE2R_SBDD 0xc4
  134. #define ALI_STIMER 0xc8
  135. #define ALI_GLOBAL_CONTROL 0xd4
  136. #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
  137. #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
  138. #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
  139. #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
  140. #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
  141. #define ALI_CSO_ALPHA_FMS 0xe0
  142. #define ALI_LBA 0xe4
  143. #define ALI_ESO_DELTA 0xe8
  144. #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
  145. #define ALI_EBUF1 0xf4
  146. #define ALI_EBUF2 0xf8
  147. #define ALI_REG(codec, x) ((codec)->port + x)
  148. #define MAX_CODECS 2
  149. struct snd_ali;
  150. struct snd_ali_voice;
  151. struct snd_ali_channel_control {
  152. /* register data */
  153. struct REGDATA {
  154. unsigned int start;
  155. unsigned int stop;
  156. unsigned int aint;
  157. unsigned int ainten;
  158. } data;
  159. /* register addresses */
  160. struct REGS {
  161. unsigned int start;
  162. unsigned int stop;
  163. unsigned int aint;
  164. unsigned int ainten;
  165. unsigned int ac97read;
  166. unsigned int ac97write;
  167. } regs;
  168. };
  169. struct snd_ali_voice {
  170. unsigned int number;
  171. unsigned int use :1,
  172. pcm :1,
  173. midi :1,
  174. mode :1,
  175. synth :1,
  176. running :1;
  177. /* PCM data */
  178. struct snd_ali *codec;
  179. struct snd_pcm_substream *substream;
  180. struct snd_ali_voice *extra;
  181. int eso; /* final ESO value for channel */
  182. int count; /* runtime->period_size */
  183. /* --- */
  184. void *private_data;
  185. void (*private_free)(void *private_data);
  186. };
  187. struct snd_alidev {
  188. struct snd_ali_voice voices[ALI_CHANNELS];
  189. unsigned int chcnt; /* num of opened channels */
  190. unsigned int chmap; /* bitmap for opened channels */
  191. unsigned int synthcount;
  192. };
  193. #define ALI_GLOBAL_REGS 56
  194. #define ALI_CHANNEL_REGS 8
  195. struct snd_ali_image {
  196. u32 regs[ALI_GLOBAL_REGS];
  197. u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
  198. };
  199. struct snd_ali {
  200. int irq;
  201. unsigned long port;
  202. unsigned char revision;
  203. unsigned int hw_initialized :1;
  204. unsigned int spdif_support :1;
  205. struct pci_dev *pci;
  206. struct pci_dev *pci_m1533;
  207. struct pci_dev *pci_m7101;
  208. struct snd_card *card;
  209. struct snd_pcm *pcm[MAX_CODECS];
  210. struct snd_alidev synth;
  211. struct snd_ali_channel_control chregs;
  212. /* S/PDIF Mask */
  213. unsigned int spdif_mask;
  214. unsigned int spurious_irq_count;
  215. unsigned int spurious_irq_max_delta;
  216. unsigned int num_of_codecs;
  217. struct snd_ac97_bus *ac97_bus;
  218. struct snd_ac97 *ac97[MAX_CODECS];
  219. unsigned short ac97_ext_id;
  220. unsigned short ac97_ext_status;
  221. spinlock_t reg_lock;
  222. spinlock_t voice_alloc;
  223. #ifdef CONFIG_PM
  224. struct snd_ali_image *image;
  225. #endif
  226. };
  227. static struct pci_device_id snd_ali_ids[] = {
  228. {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
  229. {0, }
  230. };
  231. MODULE_DEVICE_TABLE(pci, snd_ali_ids);
  232. static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
  233. static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
  234. static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
  235. unsigned short);
  236. /*
  237. * AC97 ACCESS
  238. */
  239. static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
  240. unsigned int port)
  241. {
  242. return (unsigned int)inl(ALI_REG(codec, port));
  243. }
  244. static inline void snd_ali_5451_poke(struct snd_ali *codec,
  245. unsigned int port,
  246. unsigned int val)
  247. {
  248. outl((unsigned int)val, ALI_REG(codec, port));
  249. }
  250. static int snd_ali_codec_ready(struct snd_ali *codec,
  251. unsigned int port)
  252. {
  253. unsigned long end_time;
  254. unsigned int res;
  255. end_time = jiffies + msecs_to_jiffies(250);
  256. do {
  257. res = snd_ali_5451_peek(codec,port);
  258. if (!(res & 0x8000))
  259. return 0;
  260. schedule_timeout_uninterruptible(1);
  261. } while (time_after_eq(end_time, jiffies));
  262. snd_ali_5451_poke(codec, port, res & ~0x8000);
  263. snd_printdd("ali_codec_ready: codec is not ready.\n ");
  264. return -EIO;
  265. }
  266. static int snd_ali_stimer_ready(struct snd_ali *codec)
  267. {
  268. unsigned long end_time;
  269. unsigned long dwChk1,dwChk2;
  270. dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
  271. dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
  272. end_time = jiffies + msecs_to_jiffies(250);
  273. do {
  274. dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
  275. if (dwChk2 != dwChk1)
  276. return 0;
  277. schedule_timeout_uninterruptible(1);
  278. } while (time_after_eq(end_time, jiffies));
  279. snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
  280. return -EIO;
  281. }
  282. static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
  283. unsigned short reg,
  284. unsigned short val)
  285. {
  286. unsigned int dwVal;
  287. unsigned int port;
  288. if (reg >= 0x80) {
  289. snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
  290. return;
  291. }
  292. port = codec->chregs.regs.ac97write;
  293. if (snd_ali_codec_ready(codec, port) < 0)
  294. return;
  295. if (snd_ali_stimer_ready(codec) < 0)
  296. return;
  297. dwVal = (unsigned int) (reg & 0xff);
  298. dwVal |= 0x8000 | (val << 16);
  299. if (secondary)
  300. dwVal |= 0x0080;
  301. if (codec->revision == ALI_5451_V02)
  302. dwVal |= 0x0100;
  303. snd_ali_5451_poke(codec, port, dwVal);
  304. return ;
  305. }
  306. static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
  307. int secondary,
  308. unsigned short reg)
  309. {
  310. unsigned int dwVal;
  311. unsigned int port;
  312. if (reg >= 0x80) {
  313. snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
  314. return ~0;
  315. }
  316. port = codec->chregs.regs.ac97read;
  317. if (snd_ali_codec_ready(codec, port) < 0)
  318. return ~0;
  319. if (snd_ali_stimer_ready(codec) < 0)
  320. return ~0;
  321. dwVal = (unsigned int) (reg & 0xff);
  322. dwVal |= 0x8000; /* bit 15*/
  323. if (secondary)
  324. dwVal |= 0x0080;
  325. snd_ali_5451_poke(codec, port, dwVal);
  326. if (snd_ali_stimer_ready(codec) < 0)
  327. return ~0;
  328. if (snd_ali_codec_ready(codec, port) < 0)
  329. return ~0;
  330. return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
  331. }
  332. static void snd_ali_codec_write(struct snd_ac97 *ac97,
  333. unsigned short reg,
  334. unsigned short val )
  335. {
  336. struct snd_ali *codec = ac97->private_data;
  337. snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
  338. if (reg == AC97_GPIO_STATUS) {
  339. outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
  340. ALI_REG(codec, ALI_AC97_GPIO));
  341. return;
  342. }
  343. snd_ali_codec_poke(codec, ac97->num, reg, val);
  344. return ;
  345. }
  346. static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
  347. unsigned short reg)
  348. {
  349. struct snd_ali *codec = ac97->private_data;
  350. snd_ali_printk("codec_read reg=%xh.\n", reg);
  351. return snd_ali_codec_peek(codec, ac97->num, reg);
  352. }
  353. /*
  354. * AC97 Reset
  355. */
  356. static int snd_ali_reset_5451(struct snd_ali *codec)
  357. {
  358. struct pci_dev *pci_dev;
  359. unsigned short wCount, wReg;
  360. unsigned int dwVal;
  361. pci_dev = codec->pci_m1533;
  362. if (pci_dev) {
  363. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  364. pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
  365. udelay(5000);
  366. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  367. pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
  368. udelay(5000);
  369. }
  370. pci_dev = codec->pci;
  371. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  372. pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
  373. udelay(500);
  374. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  375. pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
  376. udelay(5000);
  377. wCount = 200;
  378. while(wCount--) {
  379. wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
  380. if ((wReg & 0x000f) == 0x000f)
  381. return 0;
  382. udelay(5000);
  383. }
  384. /* non-fatal if you have a non PM capable codec */
  385. /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
  386. return 0;
  387. }
  388. #ifdef CODEC_RESET
  389. static int snd_ali_reset_codec(struct snd_ali *codec)
  390. {
  391. struct pci_dev *pci_dev;
  392. unsigned char bVal;
  393. unsigned int dwVal;
  394. unsigned short wCount, wReg;
  395. pci_dev = codec->pci_m1533;
  396. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  397. pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
  398. udelay(5000);
  399. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  400. pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
  401. udelay(5000);
  402. bVal = inb(ALI_REG(codec,ALI_SCTRL));
  403. bVal |= 0x02;
  404. outb(ALI_REG(codec,ALI_SCTRL),bVal);
  405. udelay(5000);
  406. bVal = inb(ALI_REG(codec,ALI_SCTRL));
  407. bVal &= 0xfd;
  408. outb(ALI_REG(codec,ALI_SCTRL),bVal);
  409. udelay(15000);
  410. wCount = 200;
  411. while (wCount--) {
  412. wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
  413. if ((wReg & 0x000f) == 0x000f)
  414. return 0;
  415. udelay(5000);
  416. }
  417. return -1;
  418. }
  419. #endif
  420. /*
  421. * ALI 5451 Controller
  422. */
  423. static void snd_ali_enable_special_channel(struct snd_ali *codec,
  424. unsigned int channel)
  425. {
  426. unsigned long dwVal;
  427. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  428. dwVal |= 1 << (channel & 0x0000001f);
  429. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  430. }
  431. static void snd_ali_disable_special_channel(struct snd_ali *codec,
  432. unsigned int channel)
  433. {
  434. unsigned long dwVal;
  435. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  436. dwVal &= ~(1 << (channel & 0x0000001f));
  437. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  438. }
  439. static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
  440. {
  441. unsigned int gc;
  442. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  443. gc |= ENDLP_IE;
  444. gc |= MIDLP_IE;
  445. outl( gc, ALI_REG(codec, ALI_GC_CIR));
  446. }
  447. static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
  448. {
  449. unsigned int gc;
  450. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  451. gc &= ~ENDLP_IE;
  452. gc &= ~MIDLP_IE;
  453. outl(gc, ALI_REG(codec, ALI_GC_CIR));
  454. }
  455. #if 0 /* not used */
  456. static void snd_ali_enable_voice_irq(struct snd_ali *codec,
  457. unsigned int channel)
  458. {
  459. unsigned int mask;
  460. struct snd_ali_channel_control *pchregs = &(codec->chregs);
  461. snd_ali_printk("enable_voice_irq channel=%d\n",channel);
  462. mask = 1 << (channel & 0x1f);
  463. pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
  464. pchregs->data.ainten |= mask;
  465. outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
  466. }
  467. #endif
  468. static void snd_ali_disable_voice_irq(struct snd_ali *codec,
  469. unsigned int channel)
  470. {
  471. unsigned int mask;
  472. struct snd_ali_channel_control *pchregs = &(codec->chregs);
  473. snd_ali_printk("disable_voice_irq channel=%d\n",channel);
  474. mask = 1 << (channel & 0x1f);
  475. pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
  476. pchregs->data.ainten &= ~mask;
  477. outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
  478. }
  479. static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
  480. {
  481. unsigned int idx = channel & 0x1f;
  482. if (codec->synth.chcnt >= ALI_CHANNELS){
  483. snd_printk(KERN_ERR
  484. "ali_alloc_pcm_channel: no free channels.\n");
  485. return -1;
  486. }
  487. if (!(codec->synth.chmap & (1 << idx))) {
  488. codec->synth.chmap |= 1 << idx;
  489. codec->synth.chcnt++;
  490. snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
  491. return idx;
  492. }
  493. return -1;
  494. }
  495. static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
  496. {
  497. int idx;
  498. int result = -1;
  499. snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
  500. /* recording */
  501. if (rec) {
  502. if (codec->spdif_support &&
  503. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  504. ALI_SPDIF_IN_SUPPORT))
  505. idx = ALI_SPDIF_IN_CHANNEL;
  506. else
  507. idx = ALI_PCM_IN_CHANNEL;
  508. result = snd_ali_alloc_pcm_channel(codec, idx);
  509. if (result >= 0)
  510. return result;
  511. else {
  512. snd_printk(KERN_ERR "ali_find_free_channel: "
  513. "record channel is busy now.\n");
  514. return -1;
  515. }
  516. }
  517. /* playback... */
  518. if (codec->spdif_support &&
  519. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  520. ALI_SPDIF_OUT_CH_ENABLE)) {
  521. idx = ALI_SPDIF_OUT_CHANNEL;
  522. result = snd_ali_alloc_pcm_channel(codec, idx);
  523. if (result >= 0)
  524. return result;
  525. else
  526. snd_printk(KERN_ERR "ali_find_free_channel: "
  527. "S/PDIF out channel is in busy now.\n");
  528. }
  529. for (idx = 0; idx < ALI_CHANNELS; idx++) {
  530. result = snd_ali_alloc_pcm_channel(codec, idx);
  531. if (result >= 0)
  532. return result;
  533. }
  534. snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
  535. return -1;
  536. }
  537. static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
  538. {
  539. unsigned int idx = channel & 0x0000001f;
  540. snd_ali_printk("free_channel_pcm channel=%d\n",channel);
  541. if (channel < 0 || channel >= ALI_CHANNELS)
  542. return;
  543. if (!(codec->synth.chmap & (1 << idx))) {
  544. snd_printk(KERN_ERR "ali_free_channel_pcm: "
  545. "channel %d is not in use.\n", channel);
  546. return;
  547. } else {
  548. codec->synth.chmap &= ~(1 << idx);
  549. codec->synth.chcnt--;
  550. }
  551. }
  552. #if 0 /* not used */
  553. static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
  554. {
  555. unsigned int mask = 1 << (channel & 0x1f);
  556. snd_ali_printk("start_voice: channel=%d\n",channel);
  557. outl(mask, ALI_REG(codec,codec->chregs.regs.start));
  558. }
  559. #endif
  560. static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
  561. {
  562. unsigned int mask = 1 << (channel & 0x1f);
  563. snd_ali_printk("stop_voice: channel=%d\n",channel);
  564. outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
  565. }
  566. /*
  567. * S/PDIF Part
  568. */
  569. static void snd_ali_delay(struct snd_ali *codec,int interval)
  570. {
  571. unsigned long begintimer,currenttimer;
  572. begintimer = inl(ALI_REG(codec, ALI_STIMER));
  573. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  574. while (currenttimer < begintimer + interval) {
  575. if (snd_ali_stimer_ready(codec) < 0)
  576. break;
  577. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  578. cpu_relax();
  579. }
  580. }
  581. static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
  582. {
  583. u16 wval;
  584. u16 count = 0;
  585. u8 bval, R1 = 0, R2;
  586. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  587. bval |= 0x1F;
  588. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  589. while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
  590. count ++;
  591. snd_ali_delay(codec, 6);
  592. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  593. R1 = bval & 0x1F;
  594. }
  595. if (count > 50000) {
  596. snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
  597. return;
  598. }
  599. count = 0;
  600. while (count++ <= 50000) {
  601. snd_ali_delay(codec, 6);
  602. bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
  603. R2 = bval & 0x1F;
  604. if (R2 != R1)
  605. R1 = R2;
  606. else
  607. break;
  608. }
  609. if (count > 50000) {
  610. snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
  611. return;
  612. }
  613. if (R2 >= 0x0b && R2 <= 0x0e) {
  614. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  615. wval &= 0xe0f0;
  616. wval |= (0x09 << 8) | 0x05;
  617. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  618. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
  619. outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
  620. } else if (R2 == 0x12) {
  621. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  622. wval &= 0xe0f0;
  623. wval |= (0x0e << 8) | 0x08;
  624. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  625. bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
  626. outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
  627. }
  628. }
  629. static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
  630. {
  631. u32 dwRate;
  632. u8 bval;
  633. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  634. bval &= 0x7f;
  635. bval |= 0x40;
  636. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
  637. snd_ali_detect_spdif_rate(codec);
  638. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
  639. bval &= 0x0f;
  640. switch (bval) {
  641. case 0: dwRate = 44100; break;
  642. case 1: dwRate = 48000; break;
  643. case 2: dwRate = 32000; break;
  644. default: dwRate = 0; break;
  645. }
  646. return dwRate;
  647. }
  648. static void snd_ali_enable_spdif_in(struct snd_ali *codec)
  649. {
  650. unsigned int dwVal;
  651. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  652. dwVal |= ALI_SPDIF_IN_SUPPORT;
  653. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  654. dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  655. dwVal |= 0x02;
  656. outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  657. snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  658. }
  659. static void snd_ali_disable_spdif_in(struct snd_ali *codec)
  660. {
  661. unsigned int dwVal;
  662. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  663. dwVal &= ~ALI_SPDIF_IN_SUPPORT;
  664. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  665. snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  666. }
  667. static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
  668. {
  669. unsigned char bVal;
  670. unsigned int dwRate;
  671. switch (rate) {
  672. case 32000: dwRate = 0x300; break;
  673. case 48000: dwRate = 0x200; break;
  674. default: dwRate = 0; break;
  675. }
  676. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  677. bVal &= (unsigned char)(~(1<<6));
  678. bVal |= 0x80; /* select right */
  679. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  680. outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
  681. bVal &= ~0x80; /* select left */
  682. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  683. outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
  684. }
  685. static void snd_ali_enable_spdif_out(struct snd_ali *codec)
  686. {
  687. unsigned short wVal;
  688. unsigned char bVal;
  689. struct pci_dev *pci_dev;
  690. pci_dev = codec->pci_m1533;
  691. if (pci_dev == NULL)
  692. return;
  693. pci_read_config_byte(pci_dev, 0x61, &bVal);
  694. bVal |= 0x40;
  695. pci_write_config_byte(pci_dev, 0x61, bVal);
  696. pci_read_config_byte(pci_dev, 0x7d, &bVal);
  697. bVal |= 0x01;
  698. pci_write_config_byte(pci_dev, 0x7d, bVal);
  699. pci_read_config_byte(pci_dev, 0x7e, &bVal);
  700. bVal &= (~0x20);
  701. bVal |= 0x10;
  702. pci_write_config_byte(pci_dev, 0x7e, bVal);
  703. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  704. outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  705. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  706. outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
  707. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  708. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  709. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  710. snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  711. }
  712. static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
  713. {
  714. unsigned short wVal;
  715. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  716. wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
  717. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  718. /*
  719. wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
  720. if (flag & ALI_SPDIF_OUT_NON_PCM)
  721. wVal |= 0x0002;
  722. else
  723. wVal &= (~0x0002);
  724. outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
  725. */
  726. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  727. }
  728. static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
  729. {
  730. unsigned short wVal;
  731. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  732. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  733. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  734. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  735. }
  736. static void snd_ali_disable_spdif_out(struct snd_ali *codec)
  737. {
  738. unsigned char bVal;
  739. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  740. outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  741. snd_ali_disable_spdif_chnout(codec);
  742. }
  743. static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
  744. {
  745. struct snd_ali_voice *pvoice;
  746. struct snd_pcm_runtime *runtime;
  747. struct snd_ali_channel_control *pchregs;
  748. unsigned int old, mask;
  749. #ifdef ALI_DEBUG
  750. unsigned int temp, cspf;
  751. #endif
  752. pchregs = &(codec->chregs);
  753. /* check if interrupt occurred for channel */
  754. old = pchregs->data.aint;
  755. mask = 1U << (channel & 0x1f);
  756. if (!(old & mask))
  757. return;
  758. pvoice = &codec->synth.voices[channel];
  759. runtime = pvoice->substream->runtime;
  760. udelay(100);
  761. spin_lock(&codec->reg_lock);
  762. if (pvoice->pcm && pvoice->substream) {
  763. /* pcm interrupt */
  764. #ifdef ALI_DEBUG
  765. outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
  766. temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  767. cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
  768. #endif
  769. if (pvoice->running) {
  770. snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
  771. (u16)temp, cspf);
  772. spin_unlock(&codec->reg_lock);
  773. snd_pcm_period_elapsed(pvoice->substream);
  774. spin_lock(&codec->reg_lock);
  775. } else {
  776. snd_ali_stop_voice(codec, channel);
  777. snd_ali_disable_voice_irq(codec, channel);
  778. }
  779. } else if (codec->synth.voices[channel].synth) {
  780. /* synth interrupt */
  781. } else if (codec->synth.voices[channel].midi) {
  782. /* midi interrupt */
  783. } else {
  784. /* unknown interrupt */
  785. snd_ali_stop_voice(codec, channel);
  786. snd_ali_disable_voice_irq(codec, channel);
  787. }
  788. spin_unlock(&codec->reg_lock);
  789. outl(mask,ALI_REG(codec,pchregs->regs.aint));
  790. pchregs->data.aint = old & (~mask);
  791. }
  792. static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
  793. {
  794. struct snd_ali *codec = dev_id;
  795. int channel;
  796. unsigned int audio_int;
  797. struct snd_ali_channel_control *pchregs;
  798. if (codec == NULL || !codec->hw_initialized)
  799. return IRQ_NONE;
  800. audio_int = inl(ALI_REG(codec, ALI_MISCINT));
  801. if (!audio_int)
  802. return IRQ_NONE;
  803. pchregs = &(codec->chregs);
  804. if (audio_int & ADDRESS_IRQ) {
  805. /* get interrupt status for all channels */
  806. pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
  807. for (channel = 0; channel < ALI_CHANNELS; channel++)
  808. snd_ali_update_ptr(codec, channel);
  809. }
  810. outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
  811. ALI_REG(codec, ALI_MISCINT));
  812. return IRQ_HANDLED;
  813. }
  814. static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
  815. int type, int rec, int channel)
  816. {
  817. struct snd_ali_voice *pvoice;
  818. int idx;
  819. snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
  820. spin_lock_irq(&codec->voice_alloc);
  821. if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
  822. idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
  823. snd_ali_find_free_channel(codec,rec);
  824. if (idx < 0) {
  825. snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
  826. spin_unlock_irq(&codec->voice_alloc);
  827. return NULL;
  828. }
  829. pvoice = &(codec->synth.voices[idx]);
  830. pvoice->codec = codec;
  831. pvoice->use = 1;
  832. pvoice->pcm = 1;
  833. pvoice->mode = rec;
  834. spin_unlock_irq(&codec->voice_alloc);
  835. return pvoice;
  836. }
  837. spin_unlock_irq(&codec->voice_alloc);
  838. return NULL;
  839. }
  840. static void snd_ali_free_voice(struct snd_ali * codec,
  841. struct snd_ali_voice *pvoice)
  842. {
  843. void (*private_free)(void *);
  844. void *private_data;
  845. snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
  846. if (pvoice == NULL || !pvoice->use)
  847. return;
  848. snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
  849. spin_lock_irq(&codec->voice_alloc);
  850. private_free = pvoice->private_free;
  851. private_data = pvoice->private_data;
  852. pvoice->private_free = NULL;
  853. pvoice->private_data = NULL;
  854. if (pvoice->pcm)
  855. snd_ali_free_channel_pcm(codec, pvoice->number);
  856. pvoice->use = pvoice->pcm = pvoice->synth = 0;
  857. pvoice->substream = NULL;
  858. spin_unlock_irq(&codec->voice_alloc);
  859. if (private_free)
  860. private_free(private_data);
  861. }
  862. static void snd_ali_clear_voices(struct snd_ali *codec,
  863. unsigned int v_min,
  864. unsigned int v_max)
  865. {
  866. unsigned int i;
  867. for (i = v_min; i <= v_max; i++) {
  868. snd_ali_stop_voice(codec, i);
  869. snd_ali_disable_voice_irq(codec, i);
  870. }
  871. }
  872. static void snd_ali_write_voice_regs(struct snd_ali *codec,
  873. unsigned int Channel,
  874. unsigned int LBA,
  875. unsigned int CSO,
  876. unsigned int ESO,
  877. unsigned int DELTA,
  878. unsigned int ALPHA_FMS,
  879. unsigned int GVSEL,
  880. unsigned int PAN,
  881. unsigned int VOL,
  882. unsigned int CTRL,
  883. unsigned int EC)
  884. {
  885. unsigned int ctlcmds[4];
  886. outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
  887. ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
  888. ctlcmds[1] = LBA;
  889. ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
  890. ctlcmds[3] = (GVSEL << 31) |
  891. ((PAN & 0x0000007f) << 24) |
  892. ((VOL & 0x000000ff) << 16) |
  893. ((CTRL & 0x0000000f) << 12) |
  894. (EC & 0x00000fff);
  895. outb(Channel, ALI_REG(codec, ALI_GC_CIR));
  896. outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
  897. outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
  898. outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
  899. outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
  900. outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
  901. outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
  902. }
  903. static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
  904. {
  905. unsigned int delta;
  906. if (rate < 4000)
  907. rate = 4000;
  908. if (rate > 48000)
  909. rate = 48000;
  910. if (rec) {
  911. if (rate == 44100)
  912. delta = 0x116a;
  913. else if (rate == 8000)
  914. delta = 0x6000;
  915. else if (rate == 48000)
  916. delta = 0x1000;
  917. else
  918. delta = ((48000 << 12) / rate) & 0x0000ffff;
  919. } else {
  920. if (rate == 44100)
  921. delta = 0xeb3;
  922. else if (rate == 8000)
  923. delta = 0x2ab;
  924. else if (rate == 48000)
  925. delta = 0x1000;
  926. else
  927. delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
  928. }
  929. return delta;
  930. }
  931. static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
  932. {
  933. unsigned int CTRL;
  934. struct snd_pcm_runtime *runtime = substream->runtime;
  935. /* set ctrl mode
  936. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  937. */
  938. CTRL = 0x00000001;
  939. if (snd_pcm_format_width(runtime->format) == 16)
  940. CTRL |= 0x00000008; /* 16-bit data */
  941. if (!snd_pcm_format_unsigned(runtime->format))
  942. CTRL |= 0x00000002; /* signed data */
  943. if (runtime->channels > 1)
  944. CTRL |= 0x00000004; /* stereo data */
  945. return CTRL;
  946. }
  947. /*
  948. * PCM part
  949. */
  950. static int snd_ali_trigger(struct snd_pcm_substream *substream,
  951. int cmd)
  952. {
  953. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  954. struct snd_pcm_substream *s;
  955. unsigned int what, whati, capture_flag;
  956. struct snd_ali_voice *pvoice, *evoice;
  957. unsigned int val;
  958. int do_start;
  959. switch (cmd) {
  960. case SNDRV_PCM_TRIGGER_START:
  961. case SNDRV_PCM_TRIGGER_RESUME:
  962. do_start = 1;
  963. break;
  964. case SNDRV_PCM_TRIGGER_STOP:
  965. case SNDRV_PCM_TRIGGER_SUSPEND:
  966. do_start = 0;
  967. break;
  968. default:
  969. return -EINVAL;
  970. }
  971. what = whati = capture_flag = 0;
  972. snd_pcm_group_for_each_entry(s, substream) {
  973. if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
  974. pvoice = s->runtime->private_data;
  975. evoice = pvoice->extra;
  976. what |= 1 << (pvoice->number & 0x1f);
  977. if (evoice == NULL)
  978. whati |= 1 << (pvoice->number & 0x1f);
  979. else {
  980. whati |= 1 << (evoice->number & 0x1f);
  981. what |= 1 << (evoice->number & 0x1f);
  982. }
  983. if (do_start) {
  984. pvoice->running = 1;
  985. if (evoice != NULL)
  986. evoice->running = 1;
  987. } else {
  988. pvoice->running = 0;
  989. if (evoice != NULL)
  990. evoice->running = 0;
  991. }
  992. snd_pcm_trigger_done(s, substream);
  993. if (pvoice->mode)
  994. capture_flag = 1;
  995. }
  996. }
  997. spin_lock(&codec->reg_lock);
  998. if (!do_start)
  999. outl(what, ALI_REG(codec, ALI_STOP));
  1000. val = inl(ALI_REG(codec, ALI_AINTEN));
  1001. if (do_start)
  1002. val |= whati;
  1003. else
  1004. val &= ~whati;
  1005. outl(val, ALI_REG(codec, ALI_AINTEN));
  1006. if (do_start)
  1007. outl(what, ALI_REG(codec, ALI_START));
  1008. snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
  1009. spin_unlock(&codec->reg_lock);
  1010. return 0;
  1011. }
  1012. static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
  1013. struct snd_pcm_hw_params *hw_params)
  1014. {
  1015. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1016. struct snd_pcm_runtime *runtime = substream->runtime;
  1017. struct snd_ali_voice *pvoice = runtime->private_data;
  1018. struct snd_ali_voice *evoice = pvoice->extra;
  1019. int err;
  1020. err = snd_pcm_lib_malloc_pages(substream,
  1021. params_buffer_bytes(hw_params));
  1022. if (err < 0)
  1023. return err;
  1024. /* voice management */
  1025. if (params_buffer_size(hw_params) / 2 !=
  1026. params_period_size(hw_params)) {
  1027. if (!evoice) {
  1028. evoice = snd_ali_alloc_voice(codec,
  1029. SNDRV_ALI_VOICE_TYPE_PCM,
  1030. 0, -1);
  1031. if (!evoice)
  1032. return -ENOMEM;
  1033. pvoice->extra = evoice;
  1034. evoice->substream = substream;
  1035. }
  1036. } else {
  1037. if (evoice) {
  1038. snd_ali_free_voice(codec, evoice);
  1039. pvoice->extra = evoice = NULL;
  1040. }
  1041. }
  1042. return 0;
  1043. }
  1044. static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
  1045. {
  1046. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1047. struct snd_pcm_runtime *runtime = substream->runtime;
  1048. struct snd_ali_voice *pvoice = runtime->private_data;
  1049. struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
  1050. snd_pcm_lib_free_pages(substream);
  1051. if (evoice) {
  1052. snd_ali_free_voice(codec, evoice);
  1053. pvoice->extra = NULL;
  1054. }
  1055. return 0;
  1056. }
  1057. static int snd_ali_hw_params(struct snd_pcm_substream *substream,
  1058. struct snd_pcm_hw_params *hw_params)
  1059. {
  1060. return snd_pcm_lib_malloc_pages(substream,
  1061. params_buffer_bytes(hw_params));
  1062. }
  1063. static int snd_ali_hw_free(struct snd_pcm_substream *substream)
  1064. {
  1065. return snd_pcm_lib_free_pages(substream);
  1066. }
  1067. static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
  1068. {
  1069. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1070. struct snd_pcm_runtime *runtime = substream->runtime;
  1071. struct snd_ali_voice *pvoice = runtime->private_data;
  1072. struct snd_ali_voice *evoice = pvoice->extra;
  1073. unsigned int LBA;
  1074. unsigned int Delta;
  1075. unsigned int ESO;
  1076. unsigned int CTRL;
  1077. unsigned int GVSEL;
  1078. unsigned int PAN;
  1079. unsigned int VOL;
  1080. unsigned int EC;
  1081. snd_ali_printk("playback_prepare ...\n");
  1082. spin_lock_irq(&codec->reg_lock);
  1083. /* set Delta (rate) value */
  1084. Delta = snd_ali_convert_rate(runtime->rate, 0);
  1085. if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
  1086. pvoice->number == ALI_PCM_IN_CHANNEL)
  1087. snd_ali_disable_special_channel(codec, pvoice->number);
  1088. else if (codec->spdif_support &&
  1089. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  1090. ALI_SPDIF_OUT_CH_ENABLE)
  1091. && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
  1092. snd_ali_set_spdif_out_rate(codec, runtime->rate);
  1093. Delta = 0x1000;
  1094. }
  1095. /* set Loop Back Address */
  1096. LBA = runtime->dma_addr;
  1097. /* set interrupt count size */
  1098. pvoice->count = runtime->period_size;
  1099. /* set target ESO for channel */
  1100. pvoice->eso = runtime->buffer_size;
  1101. snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
  1102. pvoice->eso, pvoice->count);
  1103. /* set ESO to capture first MIDLP interrupt */
  1104. ESO = pvoice->eso -1;
  1105. /* set ctrl mode */
  1106. CTRL = snd_ali_control_mode(substream);
  1107. GVSEL = 1;
  1108. PAN = 0;
  1109. VOL = 0;
  1110. EC = 0;
  1111. snd_ali_printk("playback_prepare:\n");
  1112. snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
  1113. pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
  1114. snd_ali_write_voice_regs(codec,
  1115. pvoice->number,
  1116. LBA,
  1117. 0, /* cso */
  1118. ESO,
  1119. Delta,
  1120. 0, /* alpha */
  1121. GVSEL,
  1122. PAN,
  1123. VOL,
  1124. CTRL,
  1125. EC);
  1126. if (evoice) {
  1127. evoice->count = pvoice->count;
  1128. evoice->eso = pvoice->count << 1;
  1129. ESO = evoice->eso - 1;
  1130. snd_ali_write_voice_regs(codec,
  1131. evoice->number,
  1132. LBA,
  1133. 0, /* cso */
  1134. ESO,
  1135. Delta,
  1136. 0, /* alpha */
  1137. GVSEL,
  1138. 0x7f,
  1139. 0x3ff,
  1140. CTRL,
  1141. EC);
  1142. }
  1143. spin_unlock_irq(&codec->reg_lock);
  1144. return 0;
  1145. }
  1146. static int snd_ali_prepare(struct snd_pcm_substream *substream)
  1147. {
  1148. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1149. struct snd_pcm_runtime *runtime = substream->runtime;
  1150. struct snd_ali_voice *pvoice = runtime->private_data;
  1151. unsigned int LBA;
  1152. unsigned int Delta;
  1153. unsigned int ESO;
  1154. unsigned int CTRL;
  1155. unsigned int GVSEL;
  1156. unsigned int PAN;
  1157. unsigned int VOL;
  1158. unsigned int EC;
  1159. u8 bValue;
  1160. spin_lock_irq(&codec->reg_lock);
  1161. snd_ali_printk("ali_prepare...\n");
  1162. snd_ali_enable_special_channel(codec,pvoice->number);
  1163. Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
  1164. pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
  1165. 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
  1166. /* Prepare capture intr channel */
  1167. if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
  1168. unsigned int rate;
  1169. spin_unlock_irq(&codec->reg_lock);
  1170. if (codec->revision != ALI_5451_V02)
  1171. return -1;
  1172. rate = snd_ali_get_spdif_in_rate(codec);
  1173. if (rate == 0) {
  1174. snd_printk(KERN_WARNING "ali_capture_preapre: "
  1175. "spdif rate detect err!\n");
  1176. rate = 48000;
  1177. }
  1178. spin_lock_irq(&codec->reg_lock);
  1179. bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
  1180. if (bValue & 0x10) {
  1181. outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
  1182. printk(KERN_WARNING "clear SPDIF parity error flag.\n");
  1183. }
  1184. if (rate != 48000)
  1185. Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
  1186. }
  1187. /* set target ESO for channel */
  1188. pvoice->eso = runtime->buffer_size;
  1189. /* set interrupt count size */
  1190. pvoice->count = runtime->period_size;
  1191. /* set Loop Back Address */
  1192. LBA = runtime->dma_addr;
  1193. /* set ESO to capture first MIDLP interrupt */
  1194. ESO = pvoice->eso - 1;
  1195. CTRL = snd_ali_control_mode(substream);
  1196. GVSEL = 0;
  1197. PAN = 0x00;
  1198. VOL = 0x00;
  1199. EC = 0;
  1200. snd_ali_write_voice_regs( codec,
  1201. pvoice->number,
  1202. LBA,
  1203. 0, /* cso */
  1204. ESO,
  1205. Delta,
  1206. 0, /* alpha */
  1207. GVSEL,
  1208. PAN,
  1209. VOL,
  1210. CTRL,
  1211. EC);
  1212. spin_unlock_irq(&codec->reg_lock);
  1213. return 0;
  1214. }
  1215. static snd_pcm_uframes_t
  1216. snd_ali_playback_pointer(struct snd_pcm_substream *substream)
  1217. {
  1218. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1219. struct snd_pcm_runtime *runtime = substream->runtime;
  1220. struct snd_ali_voice *pvoice = runtime->private_data;
  1221. unsigned int cso;
  1222. spin_lock(&codec->reg_lock);
  1223. if (!pvoice->running) {
  1224. spin_unlock(&codec->reg_lock);
  1225. return 0;
  1226. }
  1227. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1228. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1229. spin_unlock(&codec->reg_lock);
  1230. snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
  1231. return cso;
  1232. }
  1233. static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
  1234. {
  1235. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1236. struct snd_pcm_runtime *runtime = substream->runtime;
  1237. struct snd_ali_voice *pvoice = runtime->private_data;
  1238. unsigned int cso;
  1239. spin_lock(&codec->reg_lock);
  1240. if (!pvoice->running) {
  1241. spin_unlock_irq(&codec->reg_lock);
  1242. return 0;
  1243. }
  1244. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1245. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1246. spin_unlock(&codec->reg_lock);
  1247. return cso;
  1248. }
  1249. static struct snd_pcm_hardware snd_ali_playback =
  1250. {
  1251. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1252. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1253. SNDRV_PCM_INFO_MMAP_VALID |
  1254. SNDRV_PCM_INFO_RESUME |
  1255. SNDRV_PCM_INFO_SYNC_START),
  1256. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1257. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1258. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1259. .rate_min = 4000,
  1260. .rate_max = 48000,
  1261. .channels_min = 1,
  1262. .channels_max = 2,
  1263. .buffer_bytes_max = (256*1024),
  1264. .period_bytes_min = 64,
  1265. .period_bytes_max = (256*1024),
  1266. .periods_min = 1,
  1267. .periods_max = 1024,
  1268. .fifo_size = 0,
  1269. };
  1270. /*
  1271. * Capture support device description
  1272. */
  1273. static struct snd_pcm_hardware snd_ali_capture =
  1274. {
  1275. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1276. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1277. SNDRV_PCM_INFO_MMAP_VALID |
  1278. SNDRV_PCM_INFO_RESUME |
  1279. SNDRV_PCM_INFO_SYNC_START),
  1280. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1281. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1282. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1283. .rate_min = 4000,
  1284. .rate_max = 48000,
  1285. .channels_min = 1,
  1286. .channels_max = 2,
  1287. .buffer_bytes_max = (128*1024),
  1288. .period_bytes_min = 64,
  1289. .period_bytes_max = (128*1024),
  1290. .periods_min = 1,
  1291. .periods_max = 1024,
  1292. .fifo_size = 0,
  1293. };
  1294. static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1295. {
  1296. struct snd_ali_voice *pvoice = runtime->private_data;
  1297. struct snd_ali *codec;
  1298. if (pvoice) {
  1299. codec = pvoice->codec;
  1300. snd_ali_free_voice(pvoice->codec, pvoice);
  1301. }
  1302. }
  1303. static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
  1304. int channel, struct snd_pcm_hardware *phw)
  1305. {
  1306. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1307. struct snd_pcm_runtime *runtime = substream->runtime;
  1308. struct snd_ali_voice *pvoice;
  1309. pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
  1310. channel);
  1311. if (!pvoice)
  1312. return -EAGAIN;
  1313. pvoice->substream = substream;
  1314. runtime->private_data = pvoice;
  1315. runtime->private_free = snd_ali_pcm_free_substream;
  1316. runtime->hw = *phw;
  1317. snd_pcm_set_sync(substream);
  1318. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1319. 0, 64*1024);
  1320. return 0;
  1321. }
  1322. static int snd_ali_playback_open(struct snd_pcm_substream *substream)
  1323. {
  1324. return snd_ali_open(substream, 0, -1, &snd_ali_playback);
  1325. }
  1326. static int snd_ali_capture_open(struct snd_pcm_substream *substream)
  1327. {
  1328. return snd_ali_open(substream, 1, -1, &snd_ali_capture);
  1329. }
  1330. static int snd_ali_playback_close(struct snd_pcm_substream *substream)
  1331. {
  1332. return 0;
  1333. }
  1334. static int snd_ali_close(struct snd_pcm_substream *substream)
  1335. {
  1336. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1337. struct snd_ali_voice *pvoice = substream->runtime->private_data;
  1338. snd_ali_disable_special_channel(codec,pvoice->number);
  1339. return 0;
  1340. }
  1341. static struct snd_pcm_ops snd_ali_playback_ops = {
  1342. .open = snd_ali_playback_open,
  1343. .close = snd_ali_playback_close,
  1344. .ioctl = snd_pcm_lib_ioctl,
  1345. .hw_params = snd_ali_playback_hw_params,
  1346. .hw_free = snd_ali_playback_hw_free,
  1347. .prepare = snd_ali_playback_prepare,
  1348. .trigger = snd_ali_trigger,
  1349. .pointer = snd_ali_playback_pointer,
  1350. };
  1351. static struct snd_pcm_ops snd_ali_capture_ops = {
  1352. .open = snd_ali_capture_open,
  1353. .close = snd_ali_close,
  1354. .ioctl = snd_pcm_lib_ioctl,
  1355. .hw_params = snd_ali_hw_params,
  1356. .hw_free = snd_ali_hw_free,
  1357. .prepare = snd_ali_prepare,
  1358. .trigger = snd_ali_trigger,
  1359. .pointer = snd_ali_pointer,
  1360. };
  1361. /*
  1362. * Modem PCM
  1363. */
  1364. static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
  1365. struct snd_pcm_hw_params *hw_params)
  1366. {
  1367. struct snd_ali *chip = snd_pcm_substream_chip(substream);
  1368. unsigned int modem_num = chip->num_of_codecs - 1;
  1369. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
  1370. params_rate(hw_params));
  1371. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
  1372. return snd_ali_hw_params(substream, hw_params);
  1373. }
  1374. static struct snd_pcm_hardware snd_ali_modem =
  1375. {
  1376. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1377. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1378. SNDRV_PCM_INFO_MMAP_VALID |
  1379. SNDRV_PCM_INFO_RESUME |
  1380. SNDRV_PCM_INFO_SYNC_START),
  1381. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1382. .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
  1383. SNDRV_PCM_RATE_16000),
  1384. .rate_min = 8000,
  1385. .rate_max = 16000,
  1386. .channels_min = 1,
  1387. .channels_max = 1,
  1388. .buffer_bytes_max = (256*1024),
  1389. .period_bytes_min = 64,
  1390. .period_bytes_max = (256*1024),
  1391. .periods_min = 1,
  1392. .periods_max = 1024,
  1393. .fifo_size = 0,
  1394. };
  1395. static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
  1396. int channel)
  1397. {
  1398. static unsigned int rates[] = {8000, 9600, 12000, 16000};
  1399. static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
  1400. .count = ARRAY_SIZE(rates),
  1401. .list = rates,
  1402. .mask = 0,
  1403. };
  1404. int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
  1405. if (err)
  1406. return err;
  1407. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  1408. SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
  1409. }
  1410. static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
  1411. {
  1412. return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
  1413. }
  1414. static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
  1415. {
  1416. return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
  1417. }
  1418. static struct snd_pcm_ops snd_ali_modem_playback_ops = {
  1419. .open = snd_ali_modem_playback_open,
  1420. .close = snd_ali_close,
  1421. .ioctl = snd_pcm_lib_ioctl,
  1422. .hw_params = snd_ali_modem_hw_params,
  1423. .hw_free = snd_ali_hw_free,
  1424. .prepare = snd_ali_prepare,
  1425. .trigger = snd_ali_trigger,
  1426. .pointer = snd_ali_pointer,
  1427. };
  1428. static struct snd_pcm_ops snd_ali_modem_capture_ops = {
  1429. .open = snd_ali_modem_capture_open,
  1430. .close = snd_ali_close,
  1431. .ioctl = snd_pcm_lib_ioctl,
  1432. .hw_params = snd_ali_modem_hw_params,
  1433. .hw_free = snd_ali_hw_free,
  1434. .prepare = snd_ali_prepare,
  1435. .trigger = snd_ali_trigger,
  1436. .pointer = snd_ali_pointer,
  1437. };
  1438. struct ali_pcm_description {
  1439. char *name;
  1440. unsigned int playback_num;
  1441. unsigned int capture_num;
  1442. struct snd_pcm_ops *playback_ops;
  1443. struct snd_pcm_ops *capture_ops;
  1444. unsigned short class;
  1445. };
  1446. static void snd_ali_pcm_free(struct snd_pcm *pcm)
  1447. {
  1448. struct snd_ali *codec = pcm->private_data;
  1449. codec->pcm[pcm->device] = NULL;
  1450. }
  1451. static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
  1452. struct ali_pcm_description *desc)
  1453. {
  1454. struct snd_pcm *pcm;
  1455. int err;
  1456. err = snd_pcm_new(codec->card, desc->name, device,
  1457. desc->playback_num, desc->capture_num, &pcm);
  1458. if (err < 0) {
  1459. snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
  1460. return err;
  1461. }
  1462. pcm->private_data = codec;
  1463. pcm->private_free = snd_ali_pcm_free;
  1464. if (desc->playback_ops)
  1465. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1466. desc->playback_ops);
  1467. if (desc->capture_ops)
  1468. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1469. desc->capture_ops);
  1470. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1471. snd_dma_pci_data(codec->pci),
  1472. 64*1024, 128*1024);
  1473. pcm->info_flags = 0;
  1474. pcm->dev_class = desc->class;
  1475. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1476. strcpy(pcm->name, desc->name);
  1477. codec->pcm[0] = pcm;
  1478. return 0;
  1479. }
  1480. static struct ali_pcm_description ali_pcms[] = {
  1481. { .name = "ALI 5451",
  1482. .playback_num = ALI_CHANNELS,
  1483. .capture_num = 1,
  1484. .playback_ops = &snd_ali_playback_ops,
  1485. .capture_ops = &snd_ali_capture_ops
  1486. },
  1487. { .name = "ALI 5451 modem",
  1488. .playback_num = 1,
  1489. .capture_num = 1,
  1490. .playback_ops = &snd_ali_modem_playback_ops,
  1491. .capture_ops = &snd_ali_modem_capture_ops,
  1492. .class = SNDRV_PCM_CLASS_MODEM
  1493. }
  1494. };
  1495. static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
  1496. {
  1497. int i, err;
  1498. for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
  1499. err = snd_ali_pcm(codec, i, &ali_pcms[i]);
  1500. if (err < 0)
  1501. return err;
  1502. }
  1503. return 0;
  1504. }
  1505. #define ALI5451_SPDIF(xname, xindex, value) \
  1506. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  1507. .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
  1508. .put = snd_ali5451_spdif_put, .private_value = value}
  1509. static int snd_ali5451_spdif_info(struct snd_kcontrol *kcontrol,
  1510. struct snd_ctl_elem_info *uinfo)
  1511. {
  1512. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1513. uinfo->count = 1;
  1514. uinfo->value.integer.min = 0;
  1515. uinfo->value.integer.max = 1;
  1516. return 0;
  1517. }
  1518. static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
  1519. struct snd_ctl_elem_value *ucontrol)
  1520. {
  1521. struct snd_ali *codec = kcontrol->private_data;
  1522. unsigned int enable;
  1523. enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1524. spin_lock_irq(&codec->reg_lock);
  1525. switch (kcontrol->private_value) {
  1526. case 0:
  1527. enable = (codec->spdif_mask & 0x02) ? 1 : 0;
  1528. break;
  1529. case 1:
  1530. enable = ((codec->spdif_mask & 0x02) &&
  1531. (codec->spdif_mask & 0x04)) ? 1 : 0;
  1532. break;
  1533. case 2:
  1534. enable = (codec->spdif_mask & 0x01) ? 1 : 0;
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. ucontrol->value.integer.value[0] = enable;
  1540. spin_unlock_irq(&codec->reg_lock);
  1541. return 0;
  1542. }
  1543. static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
  1544. struct snd_ctl_elem_value *ucontrol)
  1545. {
  1546. struct snd_ali *codec = kcontrol->private_data;
  1547. unsigned int change = 0, enable = 0;
  1548. enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1549. spin_lock_irq(&codec->reg_lock);
  1550. switch (kcontrol->private_value) {
  1551. case 0:
  1552. change = (codec->spdif_mask & 0x02) ? 1 : 0;
  1553. change = change ^ enable;
  1554. if (change) {
  1555. if (enable) {
  1556. codec->spdif_mask |= 0x02;
  1557. snd_ali_enable_spdif_out(codec);
  1558. } else {
  1559. codec->spdif_mask &= ~(0x02);
  1560. codec->spdif_mask &= ~(0x04);
  1561. snd_ali_disable_spdif_out(codec);
  1562. }
  1563. }
  1564. break;
  1565. case 1:
  1566. change = (codec->spdif_mask & 0x04) ? 1 : 0;
  1567. change = change ^ enable;
  1568. if (change && (codec->spdif_mask & 0x02)) {
  1569. if (enable) {
  1570. codec->spdif_mask |= 0x04;
  1571. snd_ali_enable_spdif_chnout(codec);
  1572. } else {
  1573. codec->spdif_mask &= ~(0x04);
  1574. snd_ali_disable_spdif_chnout(codec);
  1575. }
  1576. }
  1577. break;
  1578. case 2:
  1579. change = (codec->spdif_mask & 0x01) ? 1 : 0;
  1580. change = change ^ enable;
  1581. if (change) {
  1582. if (enable) {
  1583. codec->spdif_mask |= 0x01;
  1584. snd_ali_enable_spdif_in(codec);
  1585. } else {
  1586. codec->spdif_mask &= ~(0x01);
  1587. snd_ali_disable_spdif_in(codec);
  1588. }
  1589. }
  1590. break;
  1591. default:
  1592. break;
  1593. }
  1594. spin_unlock_irq(&codec->reg_lock);
  1595. return change;
  1596. }
  1597. static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
  1598. /* spdif aplayback switch */
  1599. /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
  1600. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
  1601. /* spdif out to spdif channel */
  1602. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
  1603. /* spdif in from spdif channel */
  1604. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
  1605. };
  1606. static int __devinit snd_ali_mixer(struct snd_ali * codec)
  1607. {
  1608. struct snd_ac97_template ac97;
  1609. unsigned int idx;
  1610. int i, err;
  1611. static struct snd_ac97_bus_ops ops = {
  1612. .write = snd_ali_codec_write,
  1613. .read = snd_ali_codec_read,
  1614. };
  1615. err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
  1616. if (err < 0)
  1617. return err;
  1618. memset(&ac97, 0, sizeof(ac97));
  1619. ac97.private_data = codec;
  1620. for (i = 0; i < codec->num_of_codecs; i++) {
  1621. ac97.num = i;
  1622. err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
  1623. if (err < 0) {
  1624. snd_printk(KERN_ERR
  1625. "ali mixer %d creating error.\n", i);
  1626. if (i == 0)
  1627. return err;
  1628. codec->num_of_codecs = 1;
  1629. break;
  1630. }
  1631. }
  1632. if (codec->spdif_support) {
  1633. for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
  1634. err = snd_ctl_add(codec->card,
  1635. snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
  1636. if (err < 0)
  1637. return err;
  1638. }
  1639. }
  1640. return 0;
  1641. }
  1642. #ifdef CONFIG_PM
  1643. static int ali_suspend(struct pci_dev *pci, pm_message_t state)
  1644. {
  1645. struct snd_card *card = pci_get_drvdata(pci);
  1646. struct snd_ali *chip = card->private_data;
  1647. struct snd_ali_image *im;
  1648. int i, j;
  1649. im = chip->image;
  1650. if (!im)
  1651. return 0;
  1652. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1653. for (i = 0; i < chip->num_of_codecs; i++) {
  1654. snd_pcm_suspend_all(chip->pcm[i]);
  1655. snd_ac97_suspend(chip->ac97[i]);
  1656. }
  1657. spin_lock_irq(&chip->reg_lock);
  1658. im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
  1659. /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
  1660. im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
  1661. /* disable all IRQ bits */
  1662. outl(0, ALI_REG(chip, ALI_MISCINT));
  1663. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1664. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
  1665. continue;
  1666. im->regs[i] = inl(ALI_REG(chip, i*4));
  1667. }
  1668. for (i = 0; i < ALI_CHANNELS; i++) {
  1669. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1670. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1671. im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
  1672. }
  1673. /* stop all HW channel */
  1674. outl(0xffffffff, ALI_REG(chip, ALI_STOP));
  1675. spin_unlock_irq(&chip->reg_lock);
  1676. pci_disable_device(pci);
  1677. pci_save_state(pci);
  1678. pci_set_power_state(pci, pci_choose_state(pci, state));
  1679. return 0;
  1680. }
  1681. static int ali_resume(struct pci_dev *pci)
  1682. {
  1683. struct snd_card *card = pci_get_drvdata(pci);
  1684. struct snd_ali *chip = card->private_data;
  1685. struct snd_ali_image *im;
  1686. int i, j;
  1687. im = chip->image;
  1688. if (!im)
  1689. return 0;
  1690. pci_set_power_state(pci, PCI_D0);
  1691. pci_restore_state(pci);
  1692. if (pci_enable_device(pci) < 0) {
  1693. printk(KERN_ERR "ali5451: pci_enable_device failed, "
  1694. "disabling device\n");
  1695. snd_card_disconnect(card);
  1696. return -EIO;
  1697. }
  1698. pci_set_master(pci);
  1699. spin_lock_irq(&chip->reg_lock);
  1700. for (i = 0; i < ALI_CHANNELS; i++) {
  1701. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1702. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1703. outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
  1704. }
  1705. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1706. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
  1707. (i*4 == ALI_START))
  1708. continue;
  1709. outl(im->regs[i], ALI_REG(chip, i*4));
  1710. }
  1711. /* start HW channel */
  1712. outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
  1713. /* restore IRQ enable bits */
  1714. outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
  1715. spin_unlock_irq(&chip->reg_lock);
  1716. for (i = 0 ; i < chip->num_of_codecs; i++)
  1717. snd_ac97_resume(chip->ac97[i]);
  1718. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1719. return 0;
  1720. }
  1721. #endif /* CONFIG_PM */
  1722. static int snd_ali_free(struct snd_ali * codec)
  1723. {
  1724. if (codec->hw_initialized)
  1725. snd_ali_disable_address_interrupt(codec);
  1726. if (codec->irq >= 0) {
  1727. synchronize_irq(codec->irq);
  1728. free_irq(codec->irq, codec);
  1729. }
  1730. if (codec->port)
  1731. pci_release_regions(codec->pci);
  1732. pci_disable_device(codec->pci);
  1733. #ifdef CONFIG_PM
  1734. kfree(codec->image);
  1735. #endif
  1736. pci_dev_put(codec->pci_m1533);
  1737. pci_dev_put(codec->pci_m7101);
  1738. kfree(codec);
  1739. return 0;
  1740. }
  1741. static int snd_ali_chip_init(struct snd_ali *codec)
  1742. {
  1743. unsigned int legacy;
  1744. unsigned char temp;
  1745. struct pci_dev *pci_dev;
  1746. snd_ali_printk("chip initializing ... \n");
  1747. if (snd_ali_reset_5451(codec)) {
  1748. snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
  1749. return -1;
  1750. }
  1751. if (codec->revision == ALI_5451_V02) {
  1752. pci_dev = codec->pci_m1533;
  1753. pci_read_config_byte(pci_dev, 0x59, &temp);
  1754. temp |= 0x80;
  1755. pci_write_config_byte(pci_dev, 0x59, temp);
  1756. pci_dev = codec->pci_m7101;
  1757. pci_read_config_byte(pci_dev, 0xb8, &temp);
  1758. temp |= 0x20;
  1759. pci_write_config_byte(pci_dev, 0xB8, temp);
  1760. }
  1761. pci_read_config_dword(codec->pci, 0x44, &legacy);
  1762. legacy &= 0xff00ff00;
  1763. legacy |= 0x000800aa;
  1764. pci_write_config_dword(codec->pci, 0x44, legacy);
  1765. outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  1766. outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
  1767. outl(0xffffffff, ALI_REG(codec, ALI_AINT));
  1768. outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
  1769. outb(0x10, ALI_REG(codec, ALI_MPUR2));
  1770. codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
  1771. codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
  1772. AC97_EXTENDED_STATUS);
  1773. if (codec->spdif_support) {
  1774. snd_ali_enable_spdif_out(codec);
  1775. codec->spdif_mask = 0x00000002;
  1776. }
  1777. codec->num_of_codecs = 1;
  1778. /* secondary codec - modem */
  1779. if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
  1780. codec->num_of_codecs++;
  1781. outl(inl(ALI_REG(codec, ALI_SCTRL)) |
  1782. (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
  1783. ALI_SCTRL_LINE_OUT_EN),
  1784. ALI_REG(codec, ALI_SCTRL));
  1785. }
  1786. snd_ali_printk("chip initialize succeed.\n");
  1787. return 0;
  1788. }
  1789. /* proc for register dump */
  1790. static void snd_ali_proc_read(struct snd_info_entry *entry,
  1791. struct snd_info_buffer *buf)
  1792. {
  1793. struct snd_ali *codec = entry->private_data;
  1794. int i;
  1795. for (i = 0; i < 256 ; i+= 4)
  1796. snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
  1797. }
  1798. static void __devinit snd_ali_proc_init(struct snd_ali *codec)
  1799. {
  1800. struct snd_info_entry *entry;
  1801. if (!snd_card_proc_new(codec->card, "ali5451", &entry))
  1802. snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
  1803. }
  1804. static int __devinit snd_ali_resources(struct snd_ali *codec)
  1805. {
  1806. int err;
  1807. snd_ali_printk("resouces allocation ...\n");
  1808. err = pci_request_regions(codec->pci, "ALI 5451");
  1809. if (err < 0)
  1810. return err;
  1811. codec->port = pci_resource_start(codec->pci, 0);
  1812. if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
  1813. IRQF_SHARED, "ALI 5451", codec)) {
  1814. snd_printk(KERN_ERR "Unable to request irq.\n");
  1815. return -EBUSY;
  1816. }
  1817. codec->irq = codec->pci->irq;
  1818. snd_ali_printk("resouces allocated.\n");
  1819. return 0;
  1820. }
  1821. static int snd_ali_dev_free(struct snd_device *device)
  1822. {
  1823. struct snd_ali *codec = device->device_data;
  1824. snd_ali_free(codec);
  1825. return 0;
  1826. }
  1827. static int __devinit snd_ali_create(struct snd_card *card,
  1828. struct pci_dev *pci,
  1829. int pcm_streams,
  1830. int spdif_support,
  1831. struct snd_ali ** r_ali)
  1832. {
  1833. struct snd_ali *codec;
  1834. int i, err;
  1835. unsigned short cmdw;
  1836. static struct snd_device_ops ops = {
  1837. .dev_free = snd_ali_dev_free,
  1838. };
  1839. *r_ali = NULL;
  1840. snd_ali_printk("creating ...\n");
  1841. /* enable PCI device */
  1842. err = pci_enable_device(pci);
  1843. if (err < 0)
  1844. return err;
  1845. /* check, if we can restrict PCI DMA transfers to 31 bits */
  1846. if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
  1847. pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
  1848. snd_printk(KERN_ERR "architecture does not support "
  1849. "31bit PCI busmaster DMA\n");
  1850. pci_disable_device(pci);
  1851. return -ENXIO;
  1852. }
  1853. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1854. if (!codec) {
  1855. pci_disable_device(pci);
  1856. return -ENOMEM;
  1857. }
  1858. spin_lock_init(&codec->reg_lock);
  1859. spin_lock_init(&codec->voice_alloc);
  1860. codec->card = card;
  1861. codec->pci = pci;
  1862. codec->irq = -1;
  1863. codec->revision = pci->revision;
  1864. codec->spdif_support = spdif_support;
  1865. if (pcm_streams < 1)
  1866. pcm_streams = 1;
  1867. if (pcm_streams > 32)
  1868. pcm_streams = 32;
  1869. pci_set_master(pci);
  1870. pci_read_config_word(pci, PCI_COMMAND, &cmdw);
  1871. if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
  1872. cmdw |= PCI_COMMAND_IO;
  1873. pci_write_config_word(pci, PCI_COMMAND, cmdw);
  1874. }
  1875. pci_set_master(pci);
  1876. if (snd_ali_resources(codec)) {
  1877. snd_ali_free(codec);
  1878. return -EBUSY;
  1879. }
  1880. synchronize_irq(pci->irq);
  1881. codec->synth.chmap = 0;
  1882. codec->synth.chcnt = 0;
  1883. codec->spdif_mask = 0;
  1884. codec->synth.synthcount = 0;
  1885. if (codec->revision == ALI_5451_V02)
  1886. codec->chregs.regs.ac97read = ALI_AC97_WRITE;
  1887. else
  1888. codec->chregs.regs.ac97read = ALI_AC97_READ;
  1889. codec->chregs.regs.ac97write = ALI_AC97_WRITE;
  1890. codec->chregs.regs.start = ALI_START;
  1891. codec->chregs.regs.stop = ALI_STOP;
  1892. codec->chregs.regs.aint = ALI_AINT;
  1893. codec->chregs.regs.ainten = ALI_AINTEN;
  1894. codec->chregs.data.start = 0x00;
  1895. codec->chregs.data.stop = 0x00;
  1896. codec->chregs.data.aint = 0x00;
  1897. codec->chregs.data.ainten = 0x00;
  1898. /* M1533: southbridge */
  1899. codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
  1900. if (!codec->pci_m1533) {
  1901. snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
  1902. snd_ali_free(codec);
  1903. return -ENODEV;
  1904. }
  1905. /* M7101: power management */
  1906. codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
  1907. if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
  1908. snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
  1909. snd_ali_free(codec);
  1910. return -ENODEV;
  1911. }
  1912. snd_ali_printk("snd_device_new is called.\n");
  1913. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
  1914. if (err < 0) {
  1915. snd_ali_free(codec);
  1916. return err;
  1917. }
  1918. snd_card_set_dev(card, &pci->dev);
  1919. /* initialise synth voices*/
  1920. for (i = 0; i < ALI_CHANNELS; i++)
  1921. codec->synth.voices[i].number = i;
  1922. err = snd_ali_chip_init(codec);
  1923. if (err < 0) {
  1924. snd_printk(KERN_ERR "ali create: chip init error.\n");
  1925. return err;
  1926. }
  1927. #ifdef CONFIG_PM
  1928. codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
  1929. if (!codec->image)
  1930. snd_printk(KERN_WARNING "can't allocate apm buffer\n");
  1931. #endif
  1932. snd_ali_enable_address_interrupt(codec);
  1933. codec->hw_initialized = 1;
  1934. *r_ali = codec;
  1935. snd_ali_printk("created.\n");
  1936. return 0;
  1937. }
  1938. static int __devinit snd_ali_probe(struct pci_dev *pci,
  1939. const struct pci_device_id *pci_id)
  1940. {
  1941. struct snd_card *card;
  1942. struct snd_ali *codec;
  1943. int err;
  1944. snd_ali_printk("probe ...\n");
  1945. card = snd_card_new(index, id, THIS_MODULE, 0);
  1946. if (!card)
  1947. return -ENOMEM;
  1948. err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
  1949. if (err < 0)
  1950. goto error;
  1951. card->private_data = codec;
  1952. snd_ali_printk("mixer building ...\n");
  1953. err = snd_ali_mixer(codec);
  1954. if (err < 0)
  1955. goto error;
  1956. snd_ali_printk("pcm building ...\n");
  1957. err = snd_ali_build_pcms(codec);
  1958. if (err < 0)
  1959. goto error;
  1960. snd_ali_proc_init(codec);
  1961. strcpy(card->driver, "ALI5451");
  1962. strcpy(card->shortname, "ALI 5451");
  1963. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1964. card->shortname, codec->port, codec->irq);
  1965. snd_ali_printk("register card.\n");
  1966. err = snd_card_register(card);
  1967. if (err < 0)
  1968. goto error;
  1969. pci_set_drvdata(pci, card);
  1970. return 0;
  1971. error:
  1972. snd_card_free(card);
  1973. return err;
  1974. }
  1975. static void __devexit snd_ali_remove(struct pci_dev *pci)
  1976. {
  1977. snd_card_free(pci_get_drvdata(pci));
  1978. pci_set_drvdata(pci, NULL);
  1979. }
  1980. static struct pci_driver driver = {
  1981. .name = "ALI 5451",
  1982. .id_table = snd_ali_ids,
  1983. .probe = snd_ali_probe,
  1984. .remove = __devexit_p(snd_ali_remove),
  1985. #ifdef CONFIG_PM
  1986. .suspend = ali_suspend,
  1987. .resume = ali_resume,
  1988. #endif
  1989. };
  1990. static int __init alsa_card_ali_init(void)
  1991. {
  1992. return pci_register_driver(&driver);
  1993. }
  1994. static void __exit alsa_card_ali_exit(void)
  1995. {
  1996. pci_unregister_driver(&driver);
  1997. }
  1998. module_init(alsa_card_ali_init)
  1999. module_exit(alsa_card_ali_exit)