ali5451.c 58 KB

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