ali5451.c 60 KB

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