ali5451.c 53 KB

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