ali5451.c 60 KB

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