ali5451.c 60 KB

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