ali5451.c 60 KB

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