ali5451.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  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. for (count = 0; count <= 50000; count++) {
  600. snd_ali_delay(codec, 6);
  601. bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
  602. R2 = bval & 0x1F;
  603. if (R2 != R1)
  604. R1 = R2;
  605. else
  606. break;
  607. }
  608. if (count > 50000) {
  609. snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
  610. return;
  611. }
  612. if (R2 >= 0x0b && R2 <= 0x0e) {
  613. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  614. wval &= 0xe0f0;
  615. wval |= (0x09 << 8) | 0x05;
  616. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  617. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
  618. outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
  619. } else if (R2 == 0x12) {
  620. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  621. wval &= 0xe0f0;
  622. wval |= (0x0e << 8) | 0x08;
  623. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  624. bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
  625. outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
  626. }
  627. }
  628. static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
  629. {
  630. u32 dwRate;
  631. u8 bval;
  632. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  633. bval &= 0x7f;
  634. bval |= 0x40;
  635. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
  636. snd_ali_detect_spdif_rate(codec);
  637. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
  638. bval &= 0x0f;
  639. switch (bval) {
  640. case 0: dwRate = 44100; break;
  641. case 1: dwRate = 48000; break;
  642. case 2: dwRate = 32000; break;
  643. default: dwRate = 0; break;
  644. }
  645. return dwRate;
  646. }
  647. static void snd_ali_enable_spdif_in(struct snd_ali *codec)
  648. {
  649. unsigned int dwVal;
  650. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  651. dwVal |= ALI_SPDIF_IN_SUPPORT;
  652. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  653. dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  654. dwVal |= 0x02;
  655. outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  656. snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  657. }
  658. static void snd_ali_disable_spdif_in(struct snd_ali *codec)
  659. {
  660. unsigned int dwVal;
  661. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  662. dwVal &= ~ALI_SPDIF_IN_SUPPORT;
  663. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  664. snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  665. }
  666. static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
  667. {
  668. unsigned char bVal;
  669. unsigned int dwRate;
  670. switch (rate) {
  671. case 32000: dwRate = 0x300; break;
  672. case 48000: dwRate = 0x200; break;
  673. default: dwRate = 0; break;
  674. }
  675. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  676. bVal &= (unsigned char)(~(1<<6));
  677. bVal |= 0x80; /* select right */
  678. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  679. outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
  680. bVal &= ~0x80; /* select left */
  681. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  682. outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
  683. }
  684. static void snd_ali_enable_spdif_out(struct snd_ali *codec)
  685. {
  686. unsigned short wVal;
  687. unsigned char bVal;
  688. struct pci_dev *pci_dev;
  689. pci_dev = codec->pci_m1533;
  690. if (pci_dev == NULL)
  691. return;
  692. pci_read_config_byte(pci_dev, 0x61, &bVal);
  693. bVal |= 0x40;
  694. pci_write_config_byte(pci_dev, 0x61, bVal);
  695. pci_read_config_byte(pci_dev, 0x7d, &bVal);
  696. bVal |= 0x01;
  697. pci_write_config_byte(pci_dev, 0x7d, bVal);
  698. pci_read_config_byte(pci_dev, 0x7e, &bVal);
  699. bVal &= (~0x20);
  700. bVal |= 0x10;
  701. pci_write_config_byte(pci_dev, 0x7e, bVal);
  702. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  703. outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  704. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  705. outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
  706. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  707. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  708. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  709. snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  710. }
  711. static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
  712. {
  713. unsigned short wVal;
  714. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  715. wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
  716. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  717. /*
  718. wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
  719. if (flag & ALI_SPDIF_OUT_NON_PCM)
  720. wVal |= 0x0002;
  721. else
  722. wVal &= (~0x0002);
  723. outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
  724. */
  725. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  726. }
  727. static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
  728. {
  729. unsigned short wVal;
  730. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  731. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  732. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  733. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  734. }
  735. static void snd_ali_disable_spdif_out(struct snd_ali *codec)
  736. {
  737. unsigned char bVal;
  738. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  739. outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  740. snd_ali_disable_spdif_chnout(codec);
  741. }
  742. static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
  743. {
  744. struct snd_ali_voice *pvoice;
  745. struct snd_pcm_runtime *runtime;
  746. struct snd_ali_channel_control *pchregs;
  747. unsigned int old, mask;
  748. #ifdef ALI_DEBUG
  749. unsigned int temp, cspf;
  750. #endif
  751. pchregs = &(codec->chregs);
  752. /* check if interrupt occurred for channel */
  753. old = pchregs->data.aint;
  754. mask = 1U << (channel & 0x1f);
  755. if (!(old & mask))
  756. return;
  757. pvoice = &codec->synth.voices[channel];
  758. runtime = pvoice->substream->runtime;
  759. udelay(100);
  760. spin_lock(&codec->reg_lock);
  761. if (pvoice->pcm && pvoice->substream) {
  762. /* pcm interrupt */
  763. #ifdef ALI_DEBUG
  764. outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
  765. temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  766. cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
  767. #endif
  768. if (pvoice->running) {
  769. snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
  770. (u16)temp, cspf);
  771. spin_unlock(&codec->reg_lock);
  772. snd_pcm_period_elapsed(pvoice->substream);
  773. spin_lock(&codec->reg_lock);
  774. } else {
  775. snd_ali_stop_voice(codec, channel);
  776. snd_ali_disable_voice_irq(codec, channel);
  777. }
  778. } else if (codec->synth.voices[channel].synth) {
  779. /* synth interrupt */
  780. } else if (codec->synth.voices[channel].midi) {
  781. /* midi interrupt */
  782. } else {
  783. /* unknown interrupt */
  784. snd_ali_stop_voice(codec, channel);
  785. snd_ali_disable_voice_irq(codec, channel);
  786. }
  787. spin_unlock(&codec->reg_lock);
  788. outl(mask,ALI_REG(codec,pchregs->regs.aint));
  789. pchregs->data.aint = old & (~mask);
  790. }
  791. static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
  792. {
  793. struct snd_ali *codec = dev_id;
  794. int channel;
  795. unsigned int audio_int;
  796. struct snd_ali_channel_control *pchregs;
  797. if (codec == NULL || !codec->hw_initialized)
  798. return IRQ_NONE;
  799. audio_int = inl(ALI_REG(codec, ALI_MISCINT));
  800. if (!audio_int)
  801. return IRQ_NONE;
  802. pchregs = &(codec->chregs);
  803. if (audio_int & ADDRESS_IRQ) {
  804. /* get interrupt status for all channels */
  805. pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
  806. for (channel = 0; channel < ALI_CHANNELS; channel++)
  807. snd_ali_update_ptr(codec, channel);
  808. }
  809. outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
  810. ALI_REG(codec, ALI_MISCINT));
  811. return IRQ_HANDLED;
  812. }
  813. static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
  814. int type, int rec, int channel)
  815. {
  816. struct snd_ali_voice *pvoice;
  817. int idx;
  818. snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
  819. spin_lock_irq(&codec->voice_alloc);
  820. if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
  821. idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
  822. snd_ali_find_free_channel(codec,rec);
  823. if (idx < 0) {
  824. snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
  825. spin_unlock_irq(&codec->voice_alloc);
  826. return NULL;
  827. }
  828. pvoice = &(codec->synth.voices[idx]);
  829. pvoice->codec = codec;
  830. pvoice->use = 1;
  831. pvoice->pcm = 1;
  832. pvoice->mode = rec;
  833. spin_unlock_irq(&codec->voice_alloc);
  834. return pvoice;
  835. }
  836. spin_unlock_irq(&codec->voice_alloc);
  837. return NULL;
  838. }
  839. static void snd_ali_free_voice(struct snd_ali * codec,
  840. struct snd_ali_voice *pvoice)
  841. {
  842. void (*private_free)(void *);
  843. void *private_data;
  844. snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
  845. if (pvoice == NULL || !pvoice->use)
  846. return;
  847. snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
  848. spin_lock_irq(&codec->voice_alloc);
  849. private_free = pvoice->private_free;
  850. private_data = pvoice->private_data;
  851. pvoice->private_free = NULL;
  852. pvoice->private_data = NULL;
  853. if (pvoice->pcm)
  854. snd_ali_free_channel_pcm(codec, pvoice->number);
  855. pvoice->use = pvoice->pcm = pvoice->synth = 0;
  856. pvoice->substream = NULL;
  857. spin_unlock_irq(&codec->voice_alloc);
  858. if (private_free)
  859. private_free(private_data);
  860. }
  861. static void snd_ali_clear_voices(struct snd_ali *codec,
  862. unsigned int v_min,
  863. unsigned int v_max)
  864. {
  865. unsigned int i;
  866. for (i = v_min; i <= v_max; i++) {
  867. snd_ali_stop_voice(codec, i);
  868. snd_ali_disable_voice_irq(codec, i);
  869. }
  870. }
  871. static void snd_ali_write_voice_regs(struct snd_ali *codec,
  872. unsigned int Channel,
  873. unsigned int LBA,
  874. unsigned int CSO,
  875. unsigned int ESO,
  876. unsigned int DELTA,
  877. unsigned int ALPHA_FMS,
  878. unsigned int GVSEL,
  879. unsigned int PAN,
  880. unsigned int VOL,
  881. unsigned int CTRL,
  882. unsigned int EC)
  883. {
  884. unsigned int ctlcmds[4];
  885. outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
  886. ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
  887. ctlcmds[1] = LBA;
  888. ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
  889. ctlcmds[3] = (GVSEL << 31) |
  890. ((PAN & 0x0000007f) << 24) |
  891. ((VOL & 0x000000ff) << 16) |
  892. ((CTRL & 0x0000000f) << 12) |
  893. (EC & 0x00000fff);
  894. outb(Channel, ALI_REG(codec, ALI_GC_CIR));
  895. outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
  896. outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
  897. outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
  898. outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
  899. outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
  900. outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
  901. }
  902. static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
  903. {
  904. unsigned int delta;
  905. if (rate < 4000)
  906. rate = 4000;
  907. if (rate > 48000)
  908. rate = 48000;
  909. if (rec) {
  910. if (rate == 44100)
  911. delta = 0x116a;
  912. else if (rate == 8000)
  913. delta = 0x6000;
  914. else if (rate == 48000)
  915. delta = 0x1000;
  916. else
  917. delta = ((48000 << 12) / rate) & 0x0000ffff;
  918. } else {
  919. if (rate == 44100)
  920. delta = 0xeb3;
  921. else if (rate == 8000)
  922. delta = 0x2ab;
  923. else if (rate == 48000)
  924. delta = 0x1000;
  925. else
  926. delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
  927. }
  928. return delta;
  929. }
  930. static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
  931. {
  932. unsigned int CTRL;
  933. struct snd_pcm_runtime *runtime = substream->runtime;
  934. /* set ctrl mode
  935. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  936. */
  937. CTRL = 0x00000001;
  938. if (snd_pcm_format_width(runtime->format) == 16)
  939. CTRL |= 0x00000008; /* 16-bit data */
  940. if (!snd_pcm_format_unsigned(runtime->format))
  941. CTRL |= 0x00000002; /* signed data */
  942. if (runtime->channels > 1)
  943. CTRL |= 0x00000004; /* stereo data */
  944. return CTRL;
  945. }
  946. /*
  947. * PCM part
  948. */
  949. static int snd_ali_trigger(struct snd_pcm_substream *substream,
  950. int cmd)
  951. {
  952. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  953. struct snd_pcm_substream *s;
  954. unsigned int what, whati, capture_flag;
  955. struct snd_ali_voice *pvoice, *evoice;
  956. unsigned int val;
  957. int do_start;
  958. switch (cmd) {
  959. case SNDRV_PCM_TRIGGER_START:
  960. case SNDRV_PCM_TRIGGER_RESUME:
  961. do_start = 1;
  962. break;
  963. case SNDRV_PCM_TRIGGER_STOP:
  964. case SNDRV_PCM_TRIGGER_SUSPEND:
  965. do_start = 0;
  966. break;
  967. default:
  968. return -EINVAL;
  969. }
  970. what = whati = capture_flag = 0;
  971. snd_pcm_group_for_each_entry(s, substream) {
  972. if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
  973. pvoice = s->runtime->private_data;
  974. evoice = pvoice->extra;
  975. what |= 1 << (pvoice->number & 0x1f);
  976. if (evoice == NULL)
  977. whati |= 1 << (pvoice->number & 0x1f);
  978. else {
  979. whati |= 1 << (evoice->number & 0x1f);
  980. what |= 1 << (evoice->number & 0x1f);
  981. }
  982. if (do_start) {
  983. pvoice->running = 1;
  984. if (evoice != NULL)
  985. evoice->running = 1;
  986. } else {
  987. pvoice->running = 0;
  988. if (evoice != NULL)
  989. evoice->running = 0;
  990. }
  991. snd_pcm_trigger_done(s, substream);
  992. if (pvoice->mode)
  993. capture_flag = 1;
  994. }
  995. }
  996. spin_lock(&codec->reg_lock);
  997. if (!do_start)
  998. outl(what, ALI_REG(codec, ALI_STOP));
  999. val = inl(ALI_REG(codec, ALI_AINTEN));
  1000. if (do_start)
  1001. val |= whati;
  1002. else
  1003. val &= ~whati;
  1004. outl(val, ALI_REG(codec, ALI_AINTEN));
  1005. if (do_start)
  1006. outl(what, ALI_REG(codec, ALI_START));
  1007. snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
  1008. spin_unlock(&codec->reg_lock);
  1009. return 0;
  1010. }
  1011. static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
  1012. struct snd_pcm_hw_params *hw_params)
  1013. {
  1014. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1015. struct snd_pcm_runtime *runtime = substream->runtime;
  1016. struct snd_ali_voice *pvoice = runtime->private_data;
  1017. struct snd_ali_voice *evoice = pvoice->extra;
  1018. int err;
  1019. err = snd_pcm_lib_malloc_pages(substream,
  1020. params_buffer_bytes(hw_params));
  1021. if (err < 0)
  1022. return err;
  1023. /* voice management */
  1024. if (params_buffer_size(hw_params) / 2 !=
  1025. params_period_size(hw_params)) {
  1026. if (!evoice) {
  1027. evoice = snd_ali_alloc_voice(codec,
  1028. SNDRV_ALI_VOICE_TYPE_PCM,
  1029. 0, -1);
  1030. if (!evoice)
  1031. return -ENOMEM;
  1032. pvoice->extra = evoice;
  1033. evoice->substream = substream;
  1034. }
  1035. } else {
  1036. if (evoice) {
  1037. snd_ali_free_voice(codec, evoice);
  1038. pvoice->extra = evoice = NULL;
  1039. }
  1040. }
  1041. return 0;
  1042. }
  1043. static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
  1044. {
  1045. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1046. struct snd_pcm_runtime *runtime = substream->runtime;
  1047. struct snd_ali_voice *pvoice = runtime->private_data;
  1048. struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
  1049. snd_pcm_lib_free_pages(substream);
  1050. if (evoice) {
  1051. snd_ali_free_voice(codec, evoice);
  1052. pvoice->extra = NULL;
  1053. }
  1054. return 0;
  1055. }
  1056. static int snd_ali_hw_params(struct snd_pcm_substream *substream,
  1057. struct snd_pcm_hw_params *hw_params)
  1058. {
  1059. return snd_pcm_lib_malloc_pages(substream,
  1060. params_buffer_bytes(hw_params));
  1061. }
  1062. static int snd_ali_hw_free(struct snd_pcm_substream *substream)
  1063. {
  1064. return snd_pcm_lib_free_pages(substream);
  1065. }
  1066. static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
  1067. {
  1068. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1069. struct snd_pcm_runtime *runtime = substream->runtime;
  1070. struct snd_ali_voice *pvoice = runtime->private_data;
  1071. struct snd_ali_voice *evoice = pvoice->extra;
  1072. unsigned int LBA;
  1073. unsigned int Delta;
  1074. unsigned int ESO;
  1075. unsigned int CTRL;
  1076. unsigned int GVSEL;
  1077. unsigned int PAN;
  1078. unsigned int VOL;
  1079. unsigned int EC;
  1080. snd_ali_printk("playback_prepare ...\n");
  1081. spin_lock_irq(&codec->reg_lock);
  1082. /* set Delta (rate) value */
  1083. Delta = snd_ali_convert_rate(runtime->rate, 0);
  1084. if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
  1085. pvoice->number == ALI_PCM_IN_CHANNEL)
  1086. snd_ali_disable_special_channel(codec, pvoice->number);
  1087. else if (codec->spdif_support &&
  1088. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  1089. ALI_SPDIF_OUT_CH_ENABLE)
  1090. && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
  1091. snd_ali_set_spdif_out_rate(codec, runtime->rate);
  1092. Delta = 0x1000;
  1093. }
  1094. /* set Loop Back Address */
  1095. LBA = runtime->dma_addr;
  1096. /* set interrupt count size */
  1097. pvoice->count = runtime->period_size;
  1098. /* set target ESO for channel */
  1099. pvoice->eso = runtime->buffer_size;
  1100. snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
  1101. pvoice->eso, pvoice->count);
  1102. /* set ESO to capture first MIDLP interrupt */
  1103. ESO = pvoice->eso -1;
  1104. /* set ctrl mode */
  1105. CTRL = snd_ali_control_mode(substream);
  1106. GVSEL = 1;
  1107. PAN = 0;
  1108. VOL = 0;
  1109. EC = 0;
  1110. snd_ali_printk("playback_prepare:\n");
  1111. snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
  1112. pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
  1113. snd_ali_write_voice_regs(codec,
  1114. pvoice->number,
  1115. LBA,
  1116. 0, /* cso */
  1117. ESO,
  1118. Delta,
  1119. 0, /* alpha */
  1120. GVSEL,
  1121. PAN,
  1122. VOL,
  1123. CTRL,
  1124. EC);
  1125. if (evoice) {
  1126. evoice->count = pvoice->count;
  1127. evoice->eso = pvoice->count << 1;
  1128. ESO = evoice->eso - 1;
  1129. snd_ali_write_voice_regs(codec,
  1130. evoice->number,
  1131. LBA,
  1132. 0, /* cso */
  1133. ESO,
  1134. Delta,
  1135. 0, /* alpha */
  1136. GVSEL,
  1137. 0x7f,
  1138. 0x3ff,
  1139. CTRL,
  1140. EC);
  1141. }
  1142. spin_unlock_irq(&codec->reg_lock);
  1143. return 0;
  1144. }
  1145. static int snd_ali_prepare(struct snd_pcm_substream *substream)
  1146. {
  1147. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1148. struct snd_pcm_runtime *runtime = substream->runtime;
  1149. struct snd_ali_voice *pvoice = runtime->private_data;
  1150. unsigned int LBA;
  1151. unsigned int Delta;
  1152. unsigned int ESO;
  1153. unsigned int CTRL;
  1154. unsigned int GVSEL;
  1155. unsigned int PAN;
  1156. unsigned int VOL;
  1157. unsigned int EC;
  1158. u8 bValue;
  1159. spin_lock_irq(&codec->reg_lock);
  1160. snd_ali_printk("ali_prepare...\n");
  1161. snd_ali_enable_special_channel(codec,pvoice->number);
  1162. Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
  1163. pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
  1164. 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
  1165. /* Prepare capture intr channel */
  1166. if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
  1167. unsigned int rate;
  1168. spin_unlock_irq(&codec->reg_lock);
  1169. if (codec->revision != ALI_5451_V02)
  1170. return -1;
  1171. rate = snd_ali_get_spdif_in_rate(codec);
  1172. if (rate == 0) {
  1173. snd_printk(KERN_WARNING "ali_capture_preapre: "
  1174. "spdif rate detect err!\n");
  1175. rate = 48000;
  1176. }
  1177. spin_lock_irq(&codec->reg_lock);
  1178. bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
  1179. if (bValue & 0x10) {
  1180. outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
  1181. printk(KERN_WARNING "clear SPDIF parity error flag.\n");
  1182. }
  1183. if (rate != 48000)
  1184. Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
  1185. }
  1186. /* set target ESO for channel */
  1187. pvoice->eso = runtime->buffer_size;
  1188. /* set interrupt count size */
  1189. pvoice->count = runtime->period_size;
  1190. /* set Loop Back Address */
  1191. LBA = runtime->dma_addr;
  1192. /* set ESO to capture first MIDLP interrupt */
  1193. ESO = pvoice->eso - 1;
  1194. CTRL = snd_ali_control_mode(substream);
  1195. GVSEL = 0;
  1196. PAN = 0x00;
  1197. VOL = 0x00;
  1198. EC = 0;
  1199. snd_ali_write_voice_regs( codec,
  1200. pvoice->number,
  1201. LBA,
  1202. 0, /* cso */
  1203. ESO,
  1204. Delta,
  1205. 0, /* alpha */
  1206. GVSEL,
  1207. PAN,
  1208. VOL,
  1209. CTRL,
  1210. EC);
  1211. spin_unlock_irq(&codec->reg_lock);
  1212. return 0;
  1213. }
  1214. static snd_pcm_uframes_t
  1215. snd_ali_playback_pointer(struct snd_pcm_substream *substream)
  1216. {
  1217. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1218. struct snd_pcm_runtime *runtime = substream->runtime;
  1219. struct snd_ali_voice *pvoice = runtime->private_data;
  1220. unsigned int cso;
  1221. spin_lock(&codec->reg_lock);
  1222. if (!pvoice->running) {
  1223. spin_unlock(&codec->reg_lock);
  1224. return 0;
  1225. }
  1226. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1227. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1228. spin_unlock(&codec->reg_lock);
  1229. snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
  1230. return cso;
  1231. }
  1232. static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
  1233. {
  1234. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1235. struct snd_pcm_runtime *runtime = substream->runtime;
  1236. struct snd_ali_voice *pvoice = runtime->private_data;
  1237. unsigned int cso;
  1238. spin_lock(&codec->reg_lock);
  1239. if (!pvoice->running) {
  1240. spin_unlock_irq(&codec->reg_lock);
  1241. return 0;
  1242. }
  1243. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1244. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1245. spin_unlock(&codec->reg_lock);
  1246. return cso;
  1247. }
  1248. static struct snd_pcm_hardware snd_ali_playback =
  1249. {
  1250. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1251. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1252. SNDRV_PCM_INFO_MMAP_VALID |
  1253. SNDRV_PCM_INFO_RESUME |
  1254. SNDRV_PCM_INFO_SYNC_START),
  1255. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1256. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1257. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1258. .rate_min = 4000,
  1259. .rate_max = 48000,
  1260. .channels_min = 1,
  1261. .channels_max = 2,
  1262. .buffer_bytes_max = (256*1024),
  1263. .period_bytes_min = 64,
  1264. .period_bytes_max = (256*1024),
  1265. .periods_min = 1,
  1266. .periods_max = 1024,
  1267. .fifo_size = 0,
  1268. };
  1269. /*
  1270. * Capture support device description
  1271. */
  1272. static struct snd_pcm_hardware snd_ali_capture =
  1273. {
  1274. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1275. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1276. SNDRV_PCM_INFO_MMAP_VALID |
  1277. SNDRV_PCM_INFO_RESUME |
  1278. SNDRV_PCM_INFO_SYNC_START),
  1279. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1280. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1281. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1282. .rate_min = 4000,
  1283. .rate_max = 48000,
  1284. .channels_min = 1,
  1285. .channels_max = 2,
  1286. .buffer_bytes_max = (128*1024),
  1287. .period_bytes_min = 64,
  1288. .period_bytes_max = (128*1024),
  1289. .periods_min = 1,
  1290. .periods_max = 1024,
  1291. .fifo_size = 0,
  1292. };
  1293. static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1294. {
  1295. struct snd_ali_voice *pvoice = runtime->private_data;
  1296. struct snd_ali *codec;
  1297. if (pvoice) {
  1298. codec = pvoice->codec;
  1299. snd_ali_free_voice(pvoice->codec, pvoice);
  1300. }
  1301. }
  1302. static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
  1303. int channel, struct snd_pcm_hardware *phw)
  1304. {
  1305. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1306. struct snd_pcm_runtime *runtime = substream->runtime;
  1307. struct snd_ali_voice *pvoice;
  1308. pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
  1309. channel);
  1310. if (!pvoice)
  1311. return -EAGAIN;
  1312. pvoice->substream = substream;
  1313. runtime->private_data = pvoice;
  1314. runtime->private_free = snd_ali_pcm_free_substream;
  1315. runtime->hw = *phw;
  1316. snd_pcm_set_sync(substream);
  1317. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1318. 0, 64*1024);
  1319. return 0;
  1320. }
  1321. static int snd_ali_playback_open(struct snd_pcm_substream *substream)
  1322. {
  1323. return snd_ali_open(substream, 0, -1, &snd_ali_playback);
  1324. }
  1325. static int snd_ali_capture_open(struct snd_pcm_substream *substream)
  1326. {
  1327. return snd_ali_open(substream, 1, -1, &snd_ali_capture);
  1328. }
  1329. static int snd_ali_playback_close(struct snd_pcm_substream *substream)
  1330. {
  1331. return 0;
  1332. }
  1333. static int snd_ali_close(struct snd_pcm_substream *substream)
  1334. {
  1335. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1336. struct snd_ali_voice *pvoice = substream->runtime->private_data;
  1337. snd_ali_disable_special_channel(codec,pvoice->number);
  1338. return 0;
  1339. }
  1340. static struct snd_pcm_ops snd_ali_playback_ops = {
  1341. .open = snd_ali_playback_open,
  1342. .close = snd_ali_playback_close,
  1343. .ioctl = snd_pcm_lib_ioctl,
  1344. .hw_params = snd_ali_playback_hw_params,
  1345. .hw_free = snd_ali_playback_hw_free,
  1346. .prepare = snd_ali_playback_prepare,
  1347. .trigger = snd_ali_trigger,
  1348. .pointer = snd_ali_playback_pointer,
  1349. };
  1350. static struct snd_pcm_ops snd_ali_capture_ops = {
  1351. .open = snd_ali_capture_open,
  1352. .close = snd_ali_close,
  1353. .ioctl = snd_pcm_lib_ioctl,
  1354. .hw_params = snd_ali_hw_params,
  1355. .hw_free = snd_ali_hw_free,
  1356. .prepare = snd_ali_prepare,
  1357. .trigger = snd_ali_trigger,
  1358. .pointer = snd_ali_pointer,
  1359. };
  1360. /*
  1361. * Modem PCM
  1362. */
  1363. static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
  1364. struct snd_pcm_hw_params *hw_params)
  1365. {
  1366. struct snd_ali *chip = snd_pcm_substream_chip(substream);
  1367. unsigned int modem_num = chip->num_of_codecs - 1;
  1368. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
  1369. params_rate(hw_params));
  1370. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
  1371. return snd_ali_hw_params(substream, hw_params);
  1372. }
  1373. static struct snd_pcm_hardware snd_ali_modem =
  1374. {
  1375. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1376. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1377. SNDRV_PCM_INFO_MMAP_VALID |
  1378. SNDRV_PCM_INFO_RESUME |
  1379. SNDRV_PCM_INFO_SYNC_START),
  1380. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1381. .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
  1382. SNDRV_PCM_RATE_16000),
  1383. .rate_min = 8000,
  1384. .rate_max = 16000,
  1385. .channels_min = 1,
  1386. .channels_max = 1,
  1387. .buffer_bytes_max = (256*1024),
  1388. .period_bytes_min = 64,
  1389. .period_bytes_max = (256*1024),
  1390. .periods_min = 1,
  1391. .periods_max = 1024,
  1392. .fifo_size = 0,
  1393. };
  1394. static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
  1395. int channel)
  1396. {
  1397. static unsigned int rates[] = {8000, 9600, 12000, 16000};
  1398. static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
  1399. .count = ARRAY_SIZE(rates),
  1400. .list = rates,
  1401. .mask = 0,
  1402. };
  1403. int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
  1404. if (err)
  1405. return err;
  1406. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  1407. SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
  1408. }
  1409. static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
  1410. {
  1411. return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
  1412. }
  1413. static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
  1414. {
  1415. return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
  1416. }
  1417. static struct snd_pcm_ops snd_ali_modem_playback_ops = {
  1418. .open = snd_ali_modem_playback_open,
  1419. .close = snd_ali_close,
  1420. .ioctl = snd_pcm_lib_ioctl,
  1421. .hw_params = snd_ali_modem_hw_params,
  1422. .hw_free = snd_ali_hw_free,
  1423. .prepare = snd_ali_prepare,
  1424. .trigger = snd_ali_trigger,
  1425. .pointer = snd_ali_pointer,
  1426. };
  1427. static struct snd_pcm_ops snd_ali_modem_capture_ops = {
  1428. .open = snd_ali_modem_capture_open,
  1429. .close = snd_ali_close,
  1430. .ioctl = snd_pcm_lib_ioctl,
  1431. .hw_params = snd_ali_modem_hw_params,
  1432. .hw_free = snd_ali_hw_free,
  1433. .prepare = snd_ali_prepare,
  1434. .trigger = snd_ali_trigger,
  1435. .pointer = snd_ali_pointer,
  1436. };
  1437. struct ali_pcm_description {
  1438. char *name;
  1439. unsigned int playback_num;
  1440. unsigned int capture_num;
  1441. struct snd_pcm_ops *playback_ops;
  1442. struct snd_pcm_ops *capture_ops;
  1443. unsigned short class;
  1444. };
  1445. static void snd_ali_pcm_free(struct snd_pcm *pcm)
  1446. {
  1447. struct snd_ali *codec = pcm->private_data;
  1448. codec->pcm[pcm->device] = NULL;
  1449. }
  1450. static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
  1451. struct ali_pcm_description *desc)
  1452. {
  1453. struct snd_pcm *pcm;
  1454. int err;
  1455. err = snd_pcm_new(codec->card, desc->name, device,
  1456. desc->playback_num, desc->capture_num, &pcm);
  1457. if (err < 0) {
  1458. snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
  1459. return err;
  1460. }
  1461. pcm->private_data = codec;
  1462. pcm->private_free = snd_ali_pcm_free;
  1463. if (desc->playback_ops)
  1464. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1465. desc->playback_ops);
  1466. if (desc->capture_ops)
  1467. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1468. desc->capture_ops);
  1469. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1470. snd_dma_pci_data(codec->pci),
  1471. 64*1024, 128*1024);
  1472. pcm->info_flags = 0;
  1473. pcm->dev_class = desc->class;
  1474. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1475. strcpy(pcm->name, desc->name);
  1476. codec->pcm[0] = pcm;
  1477. return 0;
  1478. }
  1479. static struct ali_pcm_description ali_pcms[] = {
  1480. { .name = "ALI 5451",
  1481. .playback_num = ALI_CHANNELS,
  1482. .capture_num = 1,
  1483. .playback_ops = &snd_ali_playback_ops,
  1484. .capture_ops = &snd_ali_capture_ops
  1485. },
  1486. { .name = "ALI 5451 modem",
  1487. .playback_num = 1,
  1488. .capture_num = 1,
  1489. .playback_ops = &snd_ali_modem_playback_ops,
  1490. .capture_ops = &snd_ali_modem_capture_ops,
  1491. .class = SNDRV_PCM_CLASS_MODEM
  1492. }
  1493. };
  1494. static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
  1495. {
  1496. int i, err;
  1497. for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
  1498. err = snd_ali_pcm(codec, i, &ali_pcms[i]);
  1499. if (err < 0)
  1500. return err;
  1501. }
  1502. return 0;
  1503. }
  1504. #define ALI5451_SPDIF(xname, xindex, value) \
  1505. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  1506. .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
  1507. .put = snd_ali5451_spdif_put, .private_value = value}
  1508. #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
  1509. static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
  1510. struct snd_ctl_elem_value *ucontrol)
  1511. {
  1512. struct snd_ali *codec = kcontrol->private_data;
  1513. unsigned int enable;
  1514. enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1515. spin_lock_irq(&codec->reg_lock);
  1516. switch (kcontrol->private_value) {
  1517. case 0:
  1518. enable = (codec->spdif_mask & 0x02) ? 1 : 0;
  1519. break;
  1520. case 1:
  1521. enable = ((codec->spdif_mask & 0x02) &&
  1522. (codec->spdif_mask & 0x04)) ? 1 : 0;
  1523. break;
  1524. case 2:
  1525. enable = (codec->spdif_mask & 0x01) ? 1 : 0;
  1526. break;
  1527. default:
  1528. break;
  1529. }
  1530. ucontrol->value.integer.value[0] = enable;
  1531. spin_unlock_irq(&codec->reg_lock);
  1532. return 0;
  1533. }
  1534. static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
  1535. struct snd_ctl_elem_value *ucontrol)
  1536. {
  1537. struct snd_ali *codec = kcontrol->private_data;
  1538. unsigned int change = 0, enable = 0;
  1539. enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1540. spin_lock_irq(&codec->reg_lock);
  1541. switch (kcontrol->private_value) {
  1542. case 0:
  1543. change = (codec->spdif_mask & 0x02) ? 1 : 0;
  1544. change = change ^ enable;
  1545. if (change) {
  1546. if (enable) {
  1547. codec->spdif_mask |= 0x02;
  1548. snd_ali_enable_spdif_out(codec);
  1549. } else {
  1550. codec->spdif_mask &= ~(0x02);
  1551. codec->spdif_mask &= ~(0x04);
  1552. snd_ali_disable_spdif_out(codec);
  1553. }
  1554. }
  1555. break;
  1556. case 1:
  1557. change = (codec->spdif_mask & 0x04) ? 1 : 0;
  1558. change = change ^ enable;
  1559. if (change && (codec->spdif_mask & 0x02)) {
  1560. if (enable) {
  1561. codec->spdif_mask |= 0x04;
  1562. snd_ali_enable_spdif_chnout(codec);
  1563. } else {
  1564. codec->spdif_mask &= ~(0x04);
  1565. snd_ali_disable_spdif_chnout(codec);
  1566. }
  1567. }
  1568. break;
  1569. case 2:
  1570. change = (codec->spdif_mask & 0x01) ? 1 : 0;
  1571. change = change ^ enable;
  1572. if (change) {
  1573. if (enable) {
  1574. codec->spdif_mask |= 0x01;
  1575. snd_ali_enable_spdif_in(codec);
  1576. } else {
  1577. codec->spdif_mask &= ~(0x01);
  1578. snd_ali_disable_spdif_in(codec);
  1579. }
  1580. }
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. spin_unlock_irq(&codec->reg_lock);
  1586. return change;
  1587. }
  1588. static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
  1589. /* spdif aplayback switch */
  1590. /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
  1591. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
  1592. /* spdif out to spdif channel */
  1593. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
  1594. /* spdif in from spdif channel */
  1595. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
  1596. };
  1597. static int __devinit snd_ali_mixer(struct snd_ali * codec)
  1598. {
  1599. struct snd_ac97_template ac97;
  1600. unsigned int idx;
  1601. int i, err;
  1602. static struct snd_ac97_bus_ops ops = {
  1603. .write = snd_ali_codec_write,
  1604. .read = snd_ali_codec_read,
  1605. };
  1606. err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
  1607. if (err < 0)
  1608. return err;
  1609. memset(&ac97, 0, sizeof(ac97));
  1610. ac97.private_data = codec;
  1611. for (i = 0; i < codec->num_of_codecs; i++) {
  1612. ac97.num = i;
  1613. err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
  1614. if (err < 0) {
  1615. snd_printk(KERN_ERR
  1616. "ali mixer %d creating error.\n", i);
  1617. if (i == 0)
  1618. return err;
  1619. codec->num_of_codecs = 1;
  1620. break;
  1621. }
  1622. }
  1623. if (codec->spdif_support) {
  1624. for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
  1625. err = snd_ctl_add(codec->card,
  1626. snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
  1627. if (err < 0)
  1628. return err;
  1629. }
  1630. }
  1631. return 0;
  1632. }
  1633. #ifdef CONFIG_PM
  1634. static int ali_suspend(struct pci_dev *pci, pm_message_t state)
  1635. {
  1636. struct snd_card *card = pci_get_drvdata(pci);
  1637. struct snd_ali *chip = card->private_data;
  1638. struct snd_ali_image *im;
  1639. int i, j;
  1640. im = chip->image;
  1641. if (!im)
  1642. return 0;
  1643. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1644. for (i = 0; i < chip->num_of_codecs; i++) {
  1645. snd_pcm_suspend_all(chip->pcm[i]);
  1646. snd_ac97_suspend(chip->ac97[i]);
  1647. }
  1648. spin_lock_irq(&chip->reg_lock);
  1649. im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
  1650. /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
  1651. im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
  1652. /* disable all IRQ bits */
  1653. outl(0, ALI_REG(chip, ALI_MISCINT));
  1654. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1655. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
  1656. continue;
  1657. im->regs[i] = inl(ALI_REG(chip, i*4));
  1658. }
  1659. for (i = 0; i < ALI_CHANNELS; i++) {
  1660. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1661. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1662. im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
  1663. }
  1664. /* stop all HW channel */
  1665. outl(0xffffffff, ALI_REG(chip, ALI_STOP));
  1666. spin_unlock_irq(&chip->reg_lock);
  1667. pci_disable_device(pci);
  1668. pci_save_state(pci);
  1669. pci_set_power_state(pci, pci_choose_state(pci, state));
  1670. return 0;
  1671. }
  1672. static int ali_resume(struct pci_dev *pci)
  1673. {
  1674. struct snd_card *card = pci_get_drvdata(pci);
  1675. struct snd_ali *chip = card->private_data;
  1676. struct snd_ali_image *im;
  1677. int i, j;
  1678. im = chip->image;
  1679. if (!im)
  1680. return 0;
  1681. pci_set_power_state(pci, PCI_D0);
  1682. pci_restore_state(pci);
  1683. if (pci_enable_device(pci) < 0) {
  1684. printk(KERN_ERR "ali5451: pci_enable_device failed, "
  1685. "disabling device\n");
  1686. snd_card_disconnect(card);
  1687. return -EIO;
  1688. }
  1689. pci_set_master(pci);
  1690. spin_lock_irq(&chip->reg_lock);
  1691. for (i = 0; i < ALI_CHANNELS; i++) {
  1692. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1693. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1694. outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
  1695. }
  1696. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1697. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
  1698. (i*4 == ALI_START))
  1699. continue;
  1700. outl(im->regs[i], ALI_REG(chip, i*4));
  1701. }
  1702. /* start HW channel */
  1703. outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
  1704. /* restore IRQ enable bits */
  1705. outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
  1706. spin_unlock_irq(&chip->reg_lock);
  1707. for (i = 0 ; i < chip->num_of_codecs; i++)
  1708. snd_ac97_resume(chip->ac97[i]);
  1709. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1710. return 0;
  1711. }
  1712. #endif /* CONFIG_PM */
  1713. static int snd_ali_free(struct snd_ali * codec)
  1714. {
  1715. if (codec->hw_initialized)
  1716. snd_ali_disable_address_interrupt(codec);
  1717. if (codec->irq >= 0) {
  1718. synchronize_irq(codec->irq);
  1719. free_irq(codec->irq, codec);
  1720. }
  1721. if (codec->port)
  1722. pci_release_regions(codec->pci);
  1723. pci_disable_device(codec->pci);
  1724. #ifdef CONFIG_PM
  1725. kfree(codec->image);
  1726. #endif
  1727. pci_dev_put(codec->pci_m1533);
  1728. pci_dev_put(codec->pci_m7101);
  1729. kfree(codec);
  1730. return 0;
  1731. }
  1732. static int snd_ali_chip_init(struct snd_ali *codec)
  1733. {
  1734. unsigned int legacy;
  1735. unsigned char temp;
  1736. struct pci_dev *pci_dev;
  1737. snd_ali_printk("chip initializing ... \n");
  1738. if (snd_ali_reset_5451(codec)) {
  1739. snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
  1740. return -1;
  1741. }
  1742. if (codec->revision == ALI_5451_V02) {
  1743. pci_dev = codec->pci_m1533;
  1744. pci_read_config_byte(pci_dev, 0x59, &temp);
  1745. temp |= 0x80;
  1746. pci_write_config_byte(pci_dev, 0x59, temp);
  1747. pci_dev = codec->pci_m7101;
  1748. pci_read_config_byte(pci_dev, 0xb8, &temp);
  1749. temp |= 0x20;
  1750. pci_write_config_byte(pci_dev, 0xB8, temp);
  1751. }
  1752. pci_read_config_dword(codec->pci, 0x44, &legacy);
  1753. legacy &= 0xff00ff00;
  1754. legacy |= 0x000800aa;
  1755. pci_write_config_dword(codec->pci, 0x44, legacy);
  1756. outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  1757. outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
  1758. outl(0xffffffff, ALI_REG(codec, ALI_AINT));
  1759. outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
  1760. outb(0x10, ALI_REG(codec, ALI_MPUR2));
  1761. codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
  1762. codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
  1763. AC97_EXTENDED_STATUS);
  1764. if (codec->spdif_support) {
  1765. snd_ali_enable_spdif_out(codec);
  1766. codec->spdif_mask = 0x00000002;
  1767. }
  1768. codec->num_of_codecs = 1;
  1769. /* secondary codec - modem */
  1770. if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
  1771. codec->num_of_codecs++;
  1772. outl(inl(ALI_REG(codec, ALI_SCTRL)) |
  1773. (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
  1774. ALI_SCTRL_LINE_OUT_EN),
  1775. ALI_REG(codec, ALI_SCTRL));
  1776. }
  1777. snd_ali_printk("chip initialize succeed.\n");
  1778. return 0;
  1779. }
  1780. /* proc for register dump */
  1781. static void snd_ali_proc_read(struct snd_info_entry *entry,
  1782. struct snd_info_buffer *buf)
  1783. {
  1784. struct snd_ali *codec = entry->private_data;
  1785. int i;
  1786. for (i = 0; i < 256 ; i+= 4)
  1787. snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
  1788. }
  1789. static void __devinit snd_ali_proc_init(struct snd_ali *codec)
  1790. {
  1791. struct snd_info_entry *entry;
  1792. if (!snd_card_proc_new(codec->card, "ali5451", &entry))
  1793. snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
  1794. }
  1795. static int __devinit snd_ali_resources(struct snd_ali *codec)
  1796. {
  1797. int err;
  1798. snd_ali_printk("resouces allocation ...\n");
  1799. err = pci_request_regions(codec->pci, "ALI 5451");
  1800. if (err < 0)
  1801. return err;
  1802. codec->port = pci_resource_start(codec->pci, 0);
  1803. if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
  1804. IRQF_SHARED, "ALI 5451", codec)) {
  1805. snd_printk(KERN_ERR "Unable to request irq.\n");
  1806. return -EBUSY;
  1807. }
  1808. codec->irq = codec->pci->irq;
  1809. snd_ali_printk("resouces allocated.\n");
  1810. return 0;
  1811. }
  1812. static int snd_ali_dev_free(struct snd_device *device)
  1813. {
  1814. struct snd_ali *codec = device->device_data;
  1815. snd_ali_free(codec);
  1816. return 0;
  1817. }
  1818. static int __devinit snd_ali_create(struct snd_card *card,
  1819. struct pci_dev *pci,
  1820. int pcm_streams,
  1821. int spdif_support,
  1822. struct snd_ali ** r_ali)
  1823. {
  1824. struct snd_ali *codec;
  1825. int i, err;
  1826. unsigned short cmdw;
  1827. static struct snd_device_ops ops = {
  1828. .dev_free = snd_ali_dev_free,
  1829. };
  1830. *r_ali = NULL;
  1831. snd_ali_printk("creating ...\n");
  1832. /* enable PCI device */
  1833. err = pci_enable_device(pci);
  1834. if (err < 0)
  1835. return err;
  1836. /* check, if we can restrict PCI DMA transfers to 31 bits */
  1837. if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
  1838. pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
  1839. snd_printk(KERN_ERR "architecture does not support "
  1840. "31bit PCI busmaster DMA\n");
  1841. pci_disable_device(pci);
  1842. return -ENXIO;
  1843. }
  1844. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1845. if (!codec) {
  1846. pci_disable_device(pci);
  1847. return -ENOMEM;
  1848. }
  1849. spin_lock_init(&codec->reg_lock);
  1850. spin_lock_init(&codec->voice_alloc);
  1851. codec->card = card;
  1852. codec->pci = pci;
  1853. codec->irq = -1;
  1854. codec->revision = pci->revision;
  1855. codec->spdif_support = spdif_support;
  1856. if (pcm_streams < 1)
  1857. pcm_streams = 1;
  1858. if (pcm_streams > 32)
  1859. pcm_streams = 32;
  1860. pci_set_master(pci);
  1861. pci_read_config_word(pci, PCI_COMMAND, &cmdw);
  1862. if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
  1863. cmdw |= PCI_COMMAND_IO;
  1864. pci_write_config_word(pci, PCI_COMMAND, cmdw);
  1865. }
  1866. pci_set_master(pci);
  1867. if (snd_ali_resources(codec)) {
  1868. snd_ali_free(codec);
  1869. return -EBUSY;
  1870. }
  1871. synchronize_irq(pci->irq);
  1872. codec->synth.chmap = 0;
  1873. codec->synth.chcnt = 0;
  1874. codec->spdif_mask = 0;
  1875. codec->synth.synthcount = 0;
  1876. if (codec->revision == ALI_5451_V02)
  1877. codec->chregs.regs.ac97read = ALI_AC97_WRITE;
  1878. else
  1879. codec->chregs.regs.ac97read = ALI_AC97_READ;
  1880. codec->chregs.regs.ac97write = ALI_AC97_WRITE;
  1881. codec->chregs.regs.start = ALI_START;
  1882. codec->chregs.regs.stop = ALI_STOP;
  1883. codec->chregs.regs.aint = ALI_AINT;
  1884. codec->chregs.regs.ainten = ALI_AINTEN;
  1885. codec->chregs.data.start = 0x00;
  1886. codec->chregs.data.stop = 0x00;
  1887. codec->chregs.data.aint = 0x00;
  1888. codec->chregs.data.ainten = 0x00;
  1889. /* M1533: southbridge */
  1890. codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
  1891. if (!codec->pci_m1533) {
  1892. snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
  1893. snd_ali_free(codec);
  1894. return -ENODEV;
  1895. }
  1896. /* M7101: power management */
  1897. codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
  1898. if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
  1899. snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
  1900. snd_ali_free(codec);
  1901. return -ENODEV;
  1902. }
  1903. snd_ali_printk("snd_device_new is called.\n");
  1904. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
  1905. if (err < 0) {
  1906. snd_ali_free(codec);
  1907. return err;
  1908. }
  1909. snd_card_set_dev(card, &pci->dev);
  1910. /* initialise synth voices*/
  1911. for (i = 0; i < ALI_CHANNELS; i++)
  1912. codec->synth.voices[i].number = i;
  1913. err = snd_ali_chip_init(codec);
  1914. if (err < 0) {
  1915. snd_printk(KERN_ERR "ali create: chip init error.\n");
  1916. return err;
  1917. }
  1918. #ifdef CONFIG_PM
  1919. codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
  1920. if (!codec->image)
  1921. snd_printk(KERN_WARNING "can't allocate apm buffer\n");
  1922. #endif
  1923. snd_ali_enable_address_interrupt(codec);
  1924. codec->hw_initialized = 1;
  1925. *r_ali = codec;
  1926. snd_ali_printk("created.\n");
  1927. return 0;
  1928. }
  1929. static int __devinit snd_ali_probe(struct pci_dev *pci,
  1930. const struct pci_device_id *pci_id)
  1931. {
  1932. struct snd_card *card;
  1933. struct snd_ali *codec;
  1934. int err;
  1935. snd_ali_printk("probe ...\n");
  1936. card = snd_card_new(index, id, THIS_MODULE, 0);
  1937. if (!card)
  1938. return -ENOMEM;
  1939. err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
  1940. if (err < 0)
  1941. goto error;
  1942. card->private_data = codec;
  1943. snd_ali_printk("mixer building ...\n");
  1944. err = snd_ali_mixer(codec);
  1945. if (err < 0)
  1946. goto error;
  1947. snd_ali_printk("pcm building ...\n");
  1948. err = snd_ali_build_pcms(codec);
  1949. if (err < 0)
  1950. goto error;
  1951. snd_ali_proc_init(codec);
  1952. strcpy(card->driver, "ALI5451");
  1953. strcpy(card->shortname, "ALI 5451");
  1954. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1955. card->shortname, codec->port, codec->irq);
  1956. snd_ali_printk("register card.\n");
  1957. err = snd_card_register(card);
  1958. if (err < 0)
  1959. goto error;
  1960. pci_set_drvdata(pci, card);
  1961. return 0;
  1962. error:
  1963. snd_card_free(card);
  1964. return err;
  1965. }
  1966. static void __devexit snd_ali_remove(struct pci_dev *pci)
  1967. {
  1968. snd_card_free(pci_get_drvdata(pci));
  1969. pci_set_drvdata(pci, NULL);
  1970. }
  1971. static struct pci_driver driver = {
  1972. .name = "ALI 5451",
  1973. .id_table = snd_ali_ids,
  1974. .probe = snd_ali_probe,
  1975. .remove = __devexit_p(snd_ali_remove),
  1976. #ifdef CONFIG_PM
  1977. .suspend = ali_suspend,
  1978. .resume = ali_resume,
  1979. #endif
  1980. };
  1981. static int __init alsa_card_ali_init(void)
  1982. {
  1983. return pci_register_driver(&driver);
  1984. }
  1985. static void __exit alsa_card_ali_exit(void)
  1986. {
  1987. pci_unregister_driver(&driver);
  1988. }
  1989. module_init(alsa_card_ali_init)
  1990. module_exit(alsa_card_ali_exit)