ali5451.c 59 KB

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