ali5451.c 60 KB

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