ali5451.c 60 KB

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