ali5451.c 60 KB

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