/kern_oII/sound/pci/ali5451/ali5451.c

http://omnia2droid.googlecode.com/ · C · 2378 lines · 1875 code · 391 blank · 112 comment · 241 complexity · 1f6f336d573628f0316b6434271d59ae MD5 · raw file

Large files are truncated click here to view the full file

  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 <asm/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/init.h>
  31. #include <linux/pci.h>
  32. #include <linux/slab.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/dma-mapping.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;
  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(KERN_DEBUG 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. struct snd_ali;
  149. struct snd_ali_voice;
  150. 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. };
  168. struct snd_ali_voice {
  169. unsigned int number;
  170. unsigned int use :1,
  171. pcm :1,
  172. midi :1,
  173. mode :1,
  174. synth :1,
  175. running :1;
  176. /* PCM data */
  177. struct snd_ali *codec;
  178. struct snd_pcm_substream *substream;
  179. struct snd_ali_voice *extra;
  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. struct snd_alidev {
  187. struct snd_ali_voice voices[ALI_CHANNELS];
  188. unsigned int chcnt; /* num of opened channels */
  189. unsigned int chmap; /* bitmap for opened channels */
  190. unsigned int synthcount;
  191. };
  192. #define ALI_GLOBAL_REGS 56
  193. #define ALI_CHANNEL_REGS 8
  194. struct snd_ali_image {
  195. u32 regs[ALI_GLOBAL_REGS];
  196. u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
  197. };
  198. struct snd_ali {
  199. int irq;
  200. unsigned long port;
  201. unsigned char revision;
  202. unsigned int hw_initialized :1;
  203. unsigned int spdif_support :1;
  204. struct pci_dev *pci;
  205. struct pci_dev *pci_m1533;
  206. struct pci_dev *pci_m7101;
  207. struct snd_card *card;
  208. struct snd_pcm *pcm[MAX_CODECS];
  209. struct snd_alidev synth;
  210. struct snd_ali_channel_control chregs;
  211. /* S/PDIF Mask */
  212. unsigned int spdif_mask;
  213. unsigned int spurious_irq_count;
  214. unsigned int spurious_irq_max_delta;
  215. unsigned int num_of_codecs;
  216. struct snd_ac97_bus *ac97_bus;
  217. struct snd_ac97 *ac97[MAX_CODECS];
  218. unsigned short ac97_ext_id;
  219. unsigned short ac97_ext_status;
  220. spinlock_t reg_lock;
  221. spinlock_t voice_alloc;
  222. #ifdef CONFIG_PM
  223. struct snd_ali_image *image;
  224. #endif
  225. };
  226. static struct pci_device_id snd_ali_ids[] = {
  227. {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
  228. {0, }
  229. };
  230. MODULE_DEVICE_TABLE(pci, snd_ali_ids);
  231. static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
  232. static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
  233. static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
  234. unsigned short);
  235. /*
  236. * AC97 ACCESS
  237. */
  238. static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
  239. unsigned int port)
  240. {
  241. return (unsigned int)inl(ALI_REG(codec, port));
  242. }
  243. static inline void snd_ali_5451_poke(struct snd_ali *codec,
  244. unsigned int port,
  245. unsigned int val)
  246. {
  247. outl((unsigned int)val, ALI_REG(codec, port));
  248. }
  249. static int snd_ali_codec_ready(struct snd_ali *codec,
  250. unsigned int port)
  251. {
  252. unsigned long end_time;
  253. unsigned int res;
  254. end_time = jiffies + msecs_to_jiffies(250);
  255. do {
  256. res = snd_ali_5451_peek(codec,port);
  257. if (!(res & 0x8000))
  258. return 0;
  259. schedule_timeout_uninterruptible(1);
  260. } while (time_after_eq(end_time, jiffies));
  261. snd_ali_5451_poke(codec, port, res & ~0x8000);
  262. snd_printdd("ali_codec_ready: codec is not ready.\n ");
  263. return -EIO;
  264. }
  265. static int snd_ali_stimer_ready(struct snd_ali *codec)
  266. {
  267. unsigned long end_time;
  268. unsigned long dwChk1,dwChk2;
  269. dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
  270. dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
  271. end_time = jiffies + msecs_to_jiffies(250);
  272. do {
  273. dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
  274. if (dwChk2 != dwChk1)
  275. return 0;
  276. schedule_timeout_uninterruptible(1);
  277. } while (time_after_eq(end_time, jiffies));
  278. snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
  279. return -EIO;
  280. }
  281. static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
  282. unsigned short reg,
  283. unsigned short val)
  284. {
  285. unsigned int dwVal;
  286. unsigned int port;
  287. if (reg >= 0x80) {
  288. snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
  289. return;
  290. }
  291. port = codec->chregs.regs.ac97write;
  292. if (snd_ali_codec_ready(codec, port) < 0)
  293. return;
  294. if (snd_ali_stimer_ready(codec) < 0)
  295. return;
  296. dwVal = (unsigned int) (reg & 0xff);
  297. dwVal |= 0x8000 | (val << 16);
  298. if (secondary)
  299. dwVal |= 0x0080;
  300. if (codec->revision == ALI_5451_V02)
  301. dwVal |= 0x0100;
  302. snd_ali_5451_poke(codec, port, dwVal);
  303. return ;
  304. }
  305. static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
  306. int secondary,
  307. unsigned short reg)
  308. {
  309. unsigned int dwVal;
  310. unsigned int port;
  311. if (reg >= 0x80) {
  312. snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
  313. return ~0;
  314. }
  315. port = codec->chregs.regs.ac97read;
  316. if (snd_ali_codec_ready(codec, port) < 0)
  317. return ~0;
  318. if (snd_ali_stimer_ready(codec) < 0)
  319. return ~0;
  320. dwVal = (unsigned int) (reg & 0xff);
  321. dwVal |= 0x8000; /* bit 15*/
  322. if (secondary)
  323. dwVal |= 0x0080;
  324. snd_ali_5451_poke(codec, port, dwVal);
  325. if (snd_ali_stimer_ready(codec) < 0)
  326. return ~0;
  327. if (snd_ali_codec_ready(codec, port) < 0)
  328. return ~0;
  329. return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
  330. }
  331. static void snd_ali_codec_write(struct snd_ac97 *ac97,
  332. unsigned short reg,
  333. unsigned short val )
  334. {
  335. struct snd_ali *codec = ac97->private_data;
  336. snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
  337. if (reg == AC97_GPIO_STATUS) {
  338. outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
  339. ALI_REG(codec, ALI_AC97_GPIO));
  340. return;
  341. }
  342. snd_ali_codec_poke(codec, ac97->num, reg, val);
  343. return ;
  344. }
  345. static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
  346. unsigned short reg)
  347. {
  348. struct snd_ali *codec = ac97->private_data;
  349. snd_ali_printk("codec_read reg=%xh.\n", reg);
  350. return snd_ali_codec_peek(codec, ac97->num, reg);
  351. }
  352. /*
  353. * AC97 Reset
  354. */
  355. static int snd_ali_reset_5451(struct snd_ali *codec)
  356. {
  357. struct pci_dev *pci_dev;
  358. unsigned short wCount, wReg;
  359. unsigned int dwVal;
  360. pci_dev = codec->pci_m1533;
  361. if (pci_dev) {
  362. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  363. pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
  364. udelay(5000);
  365. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  366. pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
  367. udelay(5000);
  368. }
  369. pci_dev = codec->pci;
  370. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  371. pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
  372. udelay(500);
  373. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  374. pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
  375. udelay(5000);
  376. wCount = 200;
  377. while(wCount--) {
  378. wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
  379. if ((wReg & 0x000f) == 0x000f)
  380. return 0;
  381. udelay(5000);
  382. }
  383. /* non-fatal if you have a non PM capable codec */
  384. /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
  385. return 0;
  386. }
  387. #ifdef CODEC_RESET
  388. static int snd_ali_reset_codec(struct snd_ali *codec)
  389. {
  390. struct pci_dev *pci_dev;
  391. unsigned char bVal;
  392. unsigned int dwVal;
  393. unsigned short wCount, wReg;
  394. pci_dev = codec->pci_m1533;
  395. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  396. pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
  397. udelay(5000);
  398. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  399. pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
  400. udelay(5000);
  401. bVal = inb(ALI_REG(codec,ALI_SCTRL));
  402. bVal |= 0x02;
  403. outb(ALI_REG(codec,ALI_SCTRL),bVal);
  404. udelay(5000);
  405. bVal = inb(ALI_REG(codec,ALI_SCTRL));
  406. bVal &= 0xfd;
  407. outb(ALI_REG(codec,ALI_SCTRL),bVal);
  408. udelay(15000);
  409. wCount = 200;
  410. while (wCount--) {
  411. wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
  412. if ((wReg & 0x000f) == 0x000f)
  413. return 0;
  414. udelay(5000);
  415. }
  416. return -1;
  417. }
  418. #endif
  419. /*
  420. * ALI 5451 Controller
  421. */
  422. static void snd_ali_enable_special_channel(struct snd_ali *codec,
  423. unsigned int channel)
  424. {
  425. unsigned long dwVal;
  426. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  427. dwVal |= 1 << (channel & 0x0000001f);
  428. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  429. }
  430. static void snd_ali_disable_special_channel(struct snd_ali *codec,
  431. unsigned int channel)
  432. {
  433. unsigned long dwVal;
  434. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  435. dwVal &= ~(1 << (channel & 0x0000001f));
  436. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  437. }
  438. static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
  439. {
  440. unsigned int gc;
  441. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  442. gc |= ENDLP_IE;
  443. gc |= MIDLP_IE;
  444. outl( gc, ALI_REG(codec, ALI_GC_CIR));
  445. }
  446. static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
  447. {
  448. unsigned int gc;
  449. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  450. gc &= ~ENDLP_IE;
  451. gc &= ~MIDLP_IE;
  452. outl(gc, ALI_REG(codec, ALI_GC_CIR));
  453. }
  454. #if 0 /* not used */
  455. static void snd_ali_enable_voice_irq(struct snd_ali *codec,
  456. unsigned int channel)
  457. {
  458. unsigned int mask;
  459. struct snd_ali_channel_control *pchregs = &(codec->chregs);
  460. snd_ali_printk("enable_voice_irq channel=%d\n",channel);
  461. mask = 1 << (channel & 0x1f);
  462. pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
  463. pchregs->data.ainten |= mask;
  464. outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
  465. }
  466. #endif
  467. static void snd_ali_disable_voice_irq(struct snd_ali *codec,
  468. unsigned int channel)
  469. {
  470. unsigned int mask;
  471. struct snd_ali_channel_control *pchregs = &(codec->chregs);
  472. snd_ali_printk("disable_voice_irq channel=%d\n",channel);
  473. mask = 1 << (channel & 0x1f);
  474. pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
  475. pchregs->data.ainten &= ~mask;
  476. outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
  477. }
  478. static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
  479. {
  480. unsigned int idx = channel & 0x1f;
  481. if (codec->synth.chcnt >= ALI_CHANNELS){
  482. snd_printk(KERN_ERR
  483. "ali_alloc_pcm_channel: no free channels.\n");
  484. return -1;
  485. }
  486. if (!(codec->synth.chmap & (1 << idx))) {
  487. codec->synth.chmap |= 1 << idx;
  488. codec->synth.chcnt++;
  489. snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
  490. return idx;
  491. }
  492. return -1;
  493. }
  494. static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
  495. {
  496. int idx;
  497. int result = -1;
  498. snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
  499. /* recording */
  500. if (rec) {
  501. if (codec->spdif_support &&
  502. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  503. ALI_SPDIF_IN_SUPPORT))
  504. idx = ALI_SPDIF_IN_CHANNEL;
  505. else
  506. idx = ALI_PCM_IN_CHANNEL;
  507. result = snd_ali_alloc_pcm_channel(codec, idx);
  508. if (result >= 0)
  509. return result;
  510. else {
  511. snd_printk(KERN_ERR "ali_find_free_channel: "
  512. "record channel is busy now.\n");
  513. return -1;
  514. }
  515. }
  516. /* playback... */
  517. if (codec->spdif_support &&
  518. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  519. ALI_SPDIF_OUT_CH_ENABLE)) {
  520. idx = ALI_SPDIF_OUT_CHANNEL;
  521. result = snd_ali_alloc_pcm_channel(codec, idx);
  522. if (result >= 0)
  523. return result;
  524. else
  525. snd_printk(KERN_ERR "ali_find_free_channel: "
  526. "S/PDIF out channel is in busy now.\n");
  527. }
  528. for (idx = 0; idx < ALI_CHANNELS; idx++) {
  529. result = snd_ali_alloc_pcm_channel(codec, idx);
  530. if (result >= 0)
  531. return result;
  532. }
  533. snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
  534. return -1;
  535. }
  536. static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
  537. {
  538. unsigned int idx = channel & 0x0000001f;
  539. snd_ali_printk("free_channel_pcm channel=%d\n",channel);
  540. if (channel < 0 || channel >= ALI_CHANNELS)
  541. return;
  542. if (!(codec->synth.chmap & (1 << idx))) {
  543. snd_printk(KERN_ERR "ali_free_channel_pcm: "
  544. "channel %d is not in use.\n", channel);
  545. return;
  546. } else {
  547. codec->synth.chmap &= ~(1 << idx);
  548. codec->synth.chcnt--;
  549. }
  550. }
  551. #if 0 /* not used */
  552. static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
  553. {
  554. unsigned int mask = 1 << (channel & 0x1f);
  555. snd_ali_printk("start_voice: channel=%d\n",channel);
  556. outl(mask, ALI_REG(codec,codec->chregs.regs.start));
  557. }
  558. #endif
  559. static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
  560. {
  561. unsigned int mask = 1 << (channel & 0x1f);
  562. snd_ali_printk("stop_voice: channel=%d\n",channel);
  563. outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
  564. }
  565. /*
  566. * S/PDIF Part
  567. */
  568. static void snd_ali_delay(struct snd_ali *codec,int interval)
  569. {
  570. unsigned long begintimer,currenttimer;
  571. begintimer = inl(ALI_REG(codec, ALI_STIMER));
  572. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  573. while (currenttimer < begintimer + interval) {
  574. if (snd_ali_stimer_ready(codec) < 0)
  575. break;
  576. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  577. cpu_relax();
  578. }
  579. }
  580. static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
  581. {
  582. u16 wval;
  583. u16 count = 0;
  584. u8 bval, R1 = 0, R2;
  585. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  586. bval |= 0x1F;
  587. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  588. while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
  589. count ++;
  590. snd_ali_delay(codec, 6);
  591. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  592. R1 = bval & 0x1F;
  593. }
  594. if (count > 50000) {
  595. snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
  596. return;
  597. }
  598. for (count = 0; count <= 50000; count++) {
  599. snd_ali_delay(codec, 6);
  600. bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
  601. R2 = bval & 0x1F;
  602. if (R2 != R1)
  603. R1 = R2;
  604. else
  605. break;
  606. }
  607. if (count > 50000) {
  608. snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
  609. return;
  610. }
  611. if (R2 >= 0x0b && R2 <= 0x0e) {
  612. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  613. wval &= 0xe0f0;
  614. wval |= (0x09 << 8) | 0x05;
  615. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  616. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
  617. outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
  618. } else if (R2 == 0x12) {
  619. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  620. wval &= 0xe0f0;
  621. wval |= (0x0e << 8) | 0x08;
  622. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  623. bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
  624. outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
  625. }
  626. }
  627. static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
  628. {
  629. u32 dwRate;
  630. u8 bval;
  631. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  632. bval &= 0x7f;
  633. bval |= 0x40;
  634. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
  635. snd_ali_detect_spdif_rate(codec);
  636. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
  637. bval &= 0x0f;
  638. switch (bval) {
  639. case 0: dwRate = 44100; break;
  640. case 1: dwRate = 48000; break;
  641. case 2: dwRate = 32000; break;
  642. default: dwRate = 0; break;
  643. }
  644. return dwRate;
  645. }
  646. static void snd_ali_enable_spdif_in(struct snd_ali *codec)
  647. {
  648. unsigned int dwVal;
  649. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  650. dwVal |= ALI_SPDIF_IN_SUPPORT;
  651. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  652. dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  653. dwVal |= 0x02;
  654. outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  655. snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  656. }
  657. static void snd_ali_disable_spdif_in(struct snd_ali *codec)
  658. {
  659. unsigned int dwVal;
  660. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  661. dwVal &= ~ALI_SPDIF_IN_SUPPORT;
  662. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  663. snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  664. }
  665. static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
  666. {
  667. unsigned char bVal;
  668. unsigned int dwRate;
  669. switch (rate) {
  670. case 32000: dwRate = 0x300; break;
  671. case 48000: dwRate = 0x200; break;
  672. default: dwRate = 0; break;
  673. }
  674. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  675. bVal &= (unsigned char)(~(1<<6));
  676. bVal |= 0x80; /* select right */
  677. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  678. outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
  679. bVal &= ~0x80; /* select left */
  680. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  681. outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
  682. }
  683. static void snd_ali_enable_spdif_out(struct snd_ali *codec)
  684. {
  685. unsigned short wVal;
  686. unsigned char bVal;
  687. struct pci_dev *pci_dev;
  688. pci_dev = codec->pci_m1533;
  689. if (pci_dev == NULL)
  690. return;
  691. pci_read_config_byte(pci_dev, 0x61, &bVal);
  692. bVal |= 0x40;
  693. pci_write_config_byte(pci_dev, 0x61, bVal);
  694. pci_read_config_byte(pci_dev, 0x7d, &bVal);
  695. bVal |= 0x01;
  696. pci_write_config_byte(pci_dev, 0x7d, bVal);
  697. pci_read_config_byte(pci_dev, 0x7e, &bVal);
  698. bVal &= (~0x20);
  699. bVal |= 0x10;
  700. pci_write_config_byte(pci_dev, 0x7e, bVal);
  701. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  702. outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  703. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  704. outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
  705. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  706. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  707. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  708. snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  709. }
  710. static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
  711. {
  712. unsigned short wVal;
  713. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  714. wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
  715. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  716. /*
  717. wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
  718. if (flag & ALI_SPDIF_OUT_NON_PCM)
  719. wVal |= 0x0002;
  720. else
  721. wVal &= (~0x0002);
  722. outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
  723. */
  724. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  725. }
  726. static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
  727. {
  728. unsigned short wVal;
  729. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  730. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  731. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  732. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  733. }
  734. static void snd_ali_disable_spdif_out(struct snd_ali *codec)
  735. {
  736. unsigned char bVal;
  737. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  738. outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  739. snd_ali_disable_spdif_chnout(codec);
  740. }
  741. static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
  742. {
  743. struct snd_ali_voice *pvoice;
  744. struct snd_pcm_runtime *runtime;
  745. struct snd_ali_channel_control *pchregs;
  746. unsigned int old, mask;
  747. #ifdef ALI_DEBUG
  748. unsigned int temp, cspf;
  749. #endif
  750. pchregs = &(codec->chregs);
  751. /* check if interrupt occurred for channel */
  752. old = pchregs->data.aint;
  753. mask = 1U << (channel & 0x1f);
  754. if (!(old & mask))
  755. return;
  756. pvoice = &codec->synth.voices[channel];
  757. runtime = pvoice->substream->runtime;
  758. udelay(100);
  759. spin_lock(&codec->reg_lock);
  760. if (pvoice->pcm && pvoice->substream) {
  761. /* pcm interrupt */
  762. #ifdef ALI_DEBUG
  763. outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
  764. temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  765. cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
  766. #endif
  767. if (pvoice->running) {
  768. snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
  769. (u16)temp, cspf);
  770. spin_unlock(&codec->reg_lock);
  771. snd_pcm_period_elapsed(pvoice->substream);
  772. spin_lock(&codec->reg_lock);
  773. } else {
  774. snd_ali_stop_voice(codec, channel);
  775. snd_ali_disable_voice_irq(codec, channel);
  776. }
  777. } else if (codec->synth.voices[channel].synth) {
  778. /* synth interrupt */
  779. } else if (codec->synth.voices[channel].midi) {
  780. /* midi interrupt */
  781. } else {
  782. /* unknown interrupt */
  783. snd_ali_stop_voice(codec, channel);
  784. snd_ali_disable_voice_irq(codec, channel);
  785. }
  786. spin_unlock(&codec->reg_lock);
  787. outl(mask,ALI_REG(codec,pchregs->regs.aint));
  788. pchregs->data.aint = old & (~mask);
  789. }
  790. static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
  791. {
  792. struct snd_ali *codec = dev_id;
  793. int channel;
  794. unsigned int audio_int;
  795. struct snd_ali_channel_control *pchregs;
  796. if (codec == NULL || !codec->hw_initialized)
  797. return IRQ_NONE;
  798. audio_int = inl(ALI_REG(codec, ALI_MISCINT));
  799. if (!audio_int)
  800. return IRQ_NONE;
  801. pchregs = &(codec->chregs);
  802. if (audio_int & ADDRESS_IRQ) {
  803. /* get interrupt status for all channels */
  804. pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
  805. for (channel = 0; channel < ALI_CHANNELS; channel++)
  806. snd_ali_update_ptr(codec, channel);
  807. }
  808. outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
  809. ALI_REG(codec, ALI_MISCINT));
  810. return IRQ_HANDLED;
  811. }
  812. static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
  813. int type, int rec, int channel)
  814. {
  815. struct snd_ali_voice *pvoice;
  816. int idx;
  817. snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
  818. spin_lock_irq(&codec->voice_alloc);
  819. if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
  820. idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
  821. snd_ali_find_free_channel(codec,rec);
  822. if (idx < 0) {
  823. snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
  824. spin_unlock_irq(&codec->voice_alloc);
  825. return NULL;
  826. }
  827. pvoice = &(codec->synth.voices[idx]);
  828. pvoice->codec = codec;
  829. pvoice->use = 1;
  830. pvoice->pcm = 1;
  831. pvoice->mode = rec;
  832. spin_unlock_irq(&codec->voice_alloc);
  833. return pvoice;
  834. }
  835. spin_unlock_irq(&codec->voice_alloc);
  836. return NULL;
  837. }
  838. static void snd_ali_free_voice(struct snd_ali * codec,
  839. struct snd_ali_voice *pvoice)
  840. {
  841. void (*private_free)(void *);
  842. void *private_data;
  843. snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
  844. if (pvoice == NULL || !pvoice->use)
  845. return;
  846. snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
  847. spin_lock_irq(&codec->voice_alloc);
  848. private_free = pvoice->private_free;
  849. private_data = pvoice->private_data;
  850. pvoice->private_free = NULL;
  851. pvoice->private_data = NULL;
  852. if (pvoice->pcm)
  853. snd_ali_free_channel_pcm(codec, pvoice->number);
  854. pvoice->use = pvoice->pcm = pvoice->synth = 0;
  855. pvoice->substream = NULL;
  856. spin_unlock_irq(&codec->voice_alloc);
  857. if (private_free)
  858. private_free(private_data);
  859. }
  860. static void snd_ali_clear_voices(struct snd_ali *codec,
  861. unsigned int v_min,
  862. unsigned int v_max)
  863. {
  864. unsigned int i;
  865. for (i = v_min; i <= v_max; i++) {
  866. snd_ali_stop_voice(codec, i);
  867. snd_ali_disable_voice_irq(codec, i);
  868. }
  869. }
  870. static void snd_ali_write_voice_regs(struct snd_ali *codec,
  871. unsigned int Channel,
  872. unsigned int LBA,
  873. unsigned int CSO,
  874. unsigned int ESO,
  875. unsigned int DELTA,
  876. unsigned int ALPHA_FMS,
  877. unsigned int GVSEL,
  878. unsigned int PAN,
  879. unsigned int VOL,
  880. unsigned int CTRL,
  881. unsigned int EC)
  882. {
  883. unsigned int ctlcmds[4];
  884. outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
  885. ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
  886. ctlcmds[1] = LBA;
  887. ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
  888. ctlcmds[3] = (GVSEL << 31) |
  889. ((PAN & 0x0000007f) << 24) |
  890. ((VOL & 0x000000ff) << 16) |
  891. ((CTRL & 0x0000000f) << 12) |
  892. (EC & 0x00000fff);
  893. outb(Channel, ALI_REG(codec, ALI_GC_CIR));
  894. outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
  895. outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
  896. outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
  897. outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
  898. outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
  899. outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
  900. }
  901. static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
  902. {
  903. unsigned int delta;
  904. if (rate < 4000)
  905. rate = 4000;
  906. if (rate > 48000)
  907. rate = 48000;
  908. if (rec) {
  909. if (rate == 44100)
  910. delta = 0x116a;
  911. else if (rate == 8000)
  912. delta = 0x6000;
  913. else if (rate == 48000)
  914. delta = 0x1000;
  915. else
  916. delta = ((48000 << 12) / rate) & 0x0000ffff;
  917. } else {
  918. if (rate == 44100)
  919. delta = 0xeb3;
  920. else if (rate == 8000)
  921. delta = 0x2ab;
  922. else if (rate == 48000)
  923. delta = 0x1000;
  924. else
  925. delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
  926. }
  927. return delta;
  928. }
  929. static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
  930. {
  931. unsigned int CTRL;
  932. struct snd_pcm_runtime *runtime = substream->runtime;
  933. /* set ctrl mode
  934. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  935. */
  936. CTRL = 0x00000001;
  937. if (snd_pcm_format_width(runtime->format) == 16)
  938. CTRL |= 0x00000008; /* 16-bit data */
  939. if (!snd_pcm_format_unsigned(runtime->format))
  940. CTRL |= 0x00000002; /* signed data */
  941. if (runtime->channels > 1)
  942. CTRL |= 0x00000004; /* stereo data */
  943. return CTRL;
  944. }
  945. /*
  946. * PCM part
  947. */
  948. static int snd_ali_trigger(struct snd_pcm_substream *substream,
  949. int cmd)
  950. {
  951. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  952. struct snd_pcm_substream *s;
  953. unsigned int what, whati, capture_flag;
  954. struct snd_ali_voice *pvoice, *evoice;
  955. unsigned int val;
  956. int do_start;
  957. switch (cmd) {
  958. case SNDRV_PCM_TRIGGER_START:
  959. case SNDRV_PCM_TRIGGER_RESUME:
  960. do_start = 1;
  961. break;
  962. case SNDRV_PCM_TRIGGER_STOP:
  963. case SNDRV_PCM_TRIGGER_SUSPEND:
  964. do_start = 0;
  965. break;
  966. default:
  967. return -EINVAL;
  968. }
  969. what = whati = capture_flag = 0;
  970. snd_pcm_group_for_each_entry(s, substream) {
  971. if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
  972. pvoice = s->runtime->private_data;
  973. evoice = pvoice->extra;
  974. what |= 1 << (pvoice->number & 0x1f);
  975. if (evoice == NULL)
  976. whati |= 1 << (pvoice->number & 0x1f);
  977. else {
  978. whati |= 1 << (evoice->number & 0x1f);
  979. what |= 1 << (evoice->number & 0x1f);
  980. }
  981. if (do_start) {
  982. pvoice->running = 1;
  983. if (evoice != NULL)
  984. evoice->running = 1;
  985. } else {
  986. pvoice->running = 0;
  987. if (evoice != NULL)
  988. evoice->running = 0;
  989. }
  990. snd_pcm_trigger_done(s, substream);
  991. if (pvoice->mode)
  992. capture_flag = 1;
  993. }
  994. }
  995. spin_lock(&codec->reg_lock);
  996. if (!do_start)
  997. outl(what, ALI_REG(codec, ALI_STOP));
  998. val = inl(ALI_REG(codec, ALI_AINTEN));
  999. if (do_start)
  1000. val |= whati;
  1001. else
  1002. val &= ~whati;
  1003. outl(val, ALI_REG(codec, ALI_AINTEN));
  1004. if (do_start)
  1005. outl(what, ALI_REG(codec, ALI_START));
  1006. snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
  1007. spin_unlock(&codec->reg_lock);
  1008. return 0;
  1009. }
  1010. static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
  1011. struct snd_pcm_hw_params *hw_params)
  1012. {
  1013. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1014. struct snd_pcm_runtime *runtime = substream->runtime;
  1015. struct snd_ali_voice *pvoice = runtime->private_data;
  1016. struct snd_ali_voice *evoice = pvoice->extra;
  1017. int err;
  1018. err = snd_pcm_lib_malloc_pages(substream,
  1019. params_buffer_bytes(hw_params));
  1020. if (err < 0)
  1021. return err;
  1022. /* voice management */
  1023. if (params_buffer_size(hw_params) / 2 !=
  1024. params_period_size(hw_params)) {
  1025. if (!evoice) {
  1026. evoice = snd_ali_alloc_voice(codec,
  1027. SNDRV_ALI_VOICE_TYPE_PCM,
  1028. 0, -1);
  1029. if (!evoice)
  1030. return -ENOMEM;
  1031. pvoice->extra = evoice;
  1032. evoice->substream = substream;
  1033. }
  1034. } else {
  1035. if (evoice) {
  1036. snd_ali_free_voice(codec, evoice);
  1037. pvoice->extra = evoice = NULL;
  1038. }
  1039. }
  1040. return 0;
  1041. }
  1042. static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
  1043. {
  1044. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1045. struct snd_pcm_runtime *runtime = substream->runtime;
  1046. struct snd_ali_voice *pvoice = runtime->private_data;
  1047. struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
  1048. snd_pcm_lib_free_pages(substream);
  1049. if (evoice) {
  1050. snd_ali_free_voice(codec, evoice);
  1051. pvoice->extra = NULL;
  1052. }
  1053. return 0;
  1054. }
  1055. static int snd_ali_hw_params(struct snd_pcm_substream *substream,
  1056. struct snd_pcm_hw_params *hw_params)
  1057. {
  1058. return snd_pcm_lib_malloc_pages(substream,
  1059. params_buffer_bytes(hw_params));
  1060. }
  1061. static int snd_ali_hw_free(struct snd_pcm_substream *substream)
  1062. {
  1063. return snd_pcm_lib_free_pages(substream);
  1064. }
  1065. static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
  1066. {
  1067. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1068. struct snd_pcm_runtime *runtime = substream->runtime;
  1069. struct snd_ali_voice *pvoice = runtime->private_data;
  1070. struct snd_ali_voice *evoice = pvoice->extra;
  1071. unsigned int LBA;
  1072. unsigned int Delta;
  1073. unsigned int ESO;
  1074. unsigned int CTRL;
  1075. unsigned int GVSEL;
  1076. unsigned int PAN;
  1077. unsigned int VOL;
  1078. unsigned int EC;
  1079. snd_ali_printk("playback_prepare ...\n");
  1080. spin_lock_irq(&codec->reg_lock);
  1081. /* set Delta (rate) value */
  1082. Delta = snd_ali_convert_rate(runtime->rate, 0);
  1083. if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
  1084. pvoice->number == ALI_PCM_IN_CHANNEL)
  1085. snd_ali_disable_special_channel(codec, pvoice->number);
  1086. else if (codec->spdif_support &&
  1087. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  1088. ALI_SPDIF_OUT_CH_ENABLE)
  1089. && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
  1090. snd_ali_set_spdif_out_rate(codec, runtime->rate);
  1091. Delta = 0x1000;
  1092. }
  1093. /* set Loop Back Address */
  1094. LBA = runtime->dma_addr;
  1095. /* set interrupt count size */
  1096. pvoice->count = runtime->period_size;
  1097. /* set target ESO for channel */
  1098. pvoice->eso = runtime->buffer_size;
  1099. snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
  1100. pvoice->eso, pvoice->count);
  1101. /* set ESO to capture first MIDLP interrupt */
  1102. ESO = pvoice->eso -1;
  1103. /* set ctrl mode */
  1104. CTRL = snd_ali_control_mode(substream);
  1105. GVSEL = 1;
  1106. PAN = 0;
  1107. VOL = 0;
  1108. EC = 0;
  1109. snd_ali_printk("playback_prepare:\n");
  1110. snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
  1111. pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
  1112. snd_ali_write_voice_regs(codec,
  1113. pvoice->number,
  1114. LBA,
  1115. 0, /* cso */
  1116. ESO,
  1117. Delta,
  1118. 0, /* alpha */
  1119. GVSEL,
  1120. PAN,
  1121. VOL,
  1122. CTRL,
  1123. EC);
  1124. if (evoice) {
  1125. evoice->count = pvoice->count;
  1126. evoice->eso = pvoice->count << 1;
  1127. ESO = evoice->eso - 1;
  1128. snd_ali_write_voice_regs(codec,
  1129. evoice->number,
  1130. LBA,
  1131. 0, /* cso */
  1132. ESO,
  1133. Delta,
  1134. 0, /* alpha */
  1135. GVSEL,
  1136. 0x7f,
  1137. 0x3ff,
  1138. CTRL,
  1139. EC);
  1140. }
  1141. spin_unlock_irq(&codec->reg_lock);
  1142. return 0;
  1143. }
  1144. static int snd_ali_prepare(struct snd_pcm_substream *substream)
  1145. {
  1146. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1147. struct snd_pcm_runtime *runtime = substream->runtime;
  1148. struct snd_ali_voice *pvoice = runtime->private_data;
  1149. unsigned int LBA;
  1150. unsigned int Delta;
  1151. unsigned int ESO;
  1152. unsigned int CTRL;
  1153. unsigned int GVSEL;
  1154. unsigned int PAN;
  1155. unsigned int VOL;
  1156. unsigned int EC;
  1157. u8 bValue;
  1158. spin_lock_irq(&codec->reg_lock);
  1159. snd_ali_printk("ali_prepare...\n");
  1160. snd_ali_enable_special_channel(codec,pvoice->number);
  1161. Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
  1162. pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
  1163. 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
  1164. /* Prepare capture intr channel */
  1165. if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
  1166. unsigned int rate;
  1167. spin_unlock_irq(&codec->reg_lock);
  1168. if (codec->revision != ALI_5451_V02)
  1169. return -1;
  1170. rate = snd_ali_get_spdif_in_rate(codec);
  1171. if (rate == 0) {
  1172. snd_printk(KERN_WARNING "ali_capture_preapre: "
  1173. "spdif rate detect err!\n");
  1174. rate = 48000;
  1175. }
  1176. spin_lock_irq(&codec->reg_lock);
  1177. bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
  1178. if (bValue & 0x10) {
  1179. outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
  1180. printk(KERN_WARNING "clear SPDIF parity error flag.\n");
  1181. }
  1182. if (rate != 48000)
  1183. Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
  1184. }
  1185. /* set target ESO for channel */
  1186. pvoice->eso = runtime->buffer_size;
  1187. /* set interrupt count size */
  1188. pvoice->count = runtime->period_size;
  1189. /* set Loop Back Address */
  1190. LBA = runtime->dma_addr;
  1191. /* set ESO to capture first MIDLP interrupt */
  1192. ESO = pvoice->eso - 1;
  1193. CTRL = snd_ali_control_mode(substream);
  1194. GVSEL = 0;
  1195. PAN = 0x00;
  1196. VOL = 0x00;
  1197. EC = 0;
  1198. snd_ali_write_voice_regs( codec,
  1199. pvoice->number,
  1200. LBA,
  1201. 0, /* cso */
  1202. ESO,
  1203. Delta,
  1204. 0, /* alpha */
  1205. GVSEL,
  1206. PAN,
  1207. VOL,
  1208. CTRL,
  1209. EC);
  1210. spin_unlock_irq(&codec->reg_lock);
  1211. return 0;
  1212. }
  1213. static snd_pcm_uframes_t
  1214. snd_ali_playback_pointer(struct snd_pcm_substream *substream)
  1215. {
  1216. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1217. struct snd_pcm_runtime *runtime = substream->runtime;
  1218. struct snd_ali_voice *pvoice = runtime->private_data;
  1219. unsigned int cso;
  1220. spin_lock(&codec->reg_lock);
  1221. if (!pvoice->running) {
  1222. spin_unlock(&codec->reg_lock);
  1223. return 0;
  1224. }
  1225. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1226. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1227. spin_unlock(&codec->reg_lock);
  1228. snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
  1229. return cso;
  1230. }
  1231. static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
  1232. {
  1233. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1234. struct snd_pcm_runtime *runtime = substream->runtime;
  1235. struct snd_ali_voice *pvoice = runtime->private_data;
  1236. unsigned int cso;
  1237. spin_lock(&codec->reg_lock);
  1238. if (!pvoice->running) {
  1239. spin_unlock_irq(&codec->reg_lock);
  1240. return 0;
  1241. }
  1242. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1243. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1244. spin_unlock(&codec->reg_lock);
  1245. return cso;
  1246. }
  1247. static struct snd_pcm_hardware snd_ali_playback =
  1248. {
  1249. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1250. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1251. SNDRV_PCM_INFO_MMAP_VALID |
  1252. SNDRV_PCM_INFO_RESUME |
  1253. SNDRV_PCM_INFO_SYNC_START),
  1254. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1255. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1256. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1257. .rate_min = 4000,
  1258. .rate_max = 48000,
  1259. .channels_min = 1,
  1260. .channels_max = 2,
  1261. .buffer_bytes_max = (256*1024),
  1262. .period_bytes_min = 64,
  1263. .period_bytes_max = (256*1024),
  1264. .periods_min = 1,
  1265. .periods_max = 1024,
  1266. .fifo_size = 0,
  1267. };
  1268. /*
  1269. * Capture support device description
  1270. */
  1271. static struct snd_pcm_hardware snd_ali_capture =
  1272. {
  1273. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1274. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1275. SNDRV_PCM_INFO_MMAP_VALID |
  1276. SNDRV_PCM_INFO_RESUME |
  1277. SNDRV_PCM_INFO_SYNC_START),
  1278. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1279. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1280. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1281. .rate_min = 4000,
  1282. .rate_max = 48000,
  1283. .channels_min = 1,
  1284. .channels_max = 2,
  1285. .buffer_bytes_max = (128*1024),
  1286. .period_bytes_min = 64,
  1287. .period_bytes_max = (128*1024),
  1288. .periods_min = 1,
  1289. .periods_max = 1024,
  1290. .fifo_size = 0,
  1291. };
  1292. static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1293. {
  1294. struct snd_ali_voice *pvoice = runtime->private_data;
  1295. struct snd_ali *codec;
  1296. if (pvoice) {
  1297. codec = pvoice->codec;
  1298. snd_ali_free_voice(pvoice->codec, pvoice);
  1299. }
  1300. }
  1301. static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
  1302. int channel, struct snd_pcm_hardware *phw)
  1303. {
  1304. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1305. struct snd_pcm_runtime *runtime = substream->runtime;
  1306. struct snd_ali_voice *pvoice;
  1307. pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
  1308. channel);
  1309. if (!pvoice)
  1310. return -EAGAIN;
  1311. pvoice->substream = substream;
  1312. runtime->private_data = pvoice;
  1313. runtime->private_free = snd_ali_pcm_free_substream;
  1314. runtime->hw = *phw;
  1315. snd_pcm_set_sync(substream);
  1316. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1317. 0, 64*1024);
  1318. return 0;
  1319. }
  1320. static int snd_ali_playback_open(struct snd_pcm_substream *substream)
  1321. {
  1322. return snd_ali_open(substream, 0, -1, &snd_ali_playback);
  1323. }
  1324. static int snd_ali_capture_open(struct snd_pcm_substream *substream)
  1325. {
  1326. return snd_ali_open(substream, 1, -1, &snd_ali_capture);
  1327. }
  1328. static int snd_ali_playback_close(struct snd_pcm_substream *substream)
  1329. {
  1330. return 0;
  1331. }
  1332. static int snd_ali_close(struct snd_pcm_substream *substream)
  1333. {
  1334. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1335. struct snd_ali_voice *pvoice = substream->runtime->private_data;
  1336. snd_ali_disable_special_channel(codec,pvoice->number);
  1337. return 0;
  1338. }
  1339. static struct snd_pcm_ops snd_ali_playback_ops = {
  1340. .open = snd_ali_playback_open,
  1341. .close = snd_ali_playback_close,
  1342. .ioctl = snd_pcm_lib_ioctl,
  1343. .hw_params = snd_ali_playback_hw_params,
  1344. .hw_free = snd_ali_playback_hw_free,
  1345. .prepare = snd_ali_playback_prepare,
  1346. .trigger = snd_ali_trigger,
  1347. .pointer = snd_ali_playback_pointer,
  1348. };
  1349. static struct snd_pcm_ops snd_ali_capture_ops = {
  1350. .open = snd_ali_capture_open,
  1351. .close = snd_ali_close,
  1352. .ioctl = snd_pcm_lib_ioctl,
  1353. .hw_params = snd_ali_hw_params,
  1354. .hw_free = snd_ali_hw_free,
  1355. .prepare = snd_ali_prepare,
  1356. .trigger = snd_ali_trigger,
  1357. .pointer = snd_ali_pointer,
  1358. };
  1359. /*
  1360. * Modem PCM
  1361. */
  1362. static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
  1363. struct snd_pcm_hw_params *hw_params)
  1364. {
  1365. struct snd_ali *chip = snd_pcm_substream_chip(substream);
  1366. unsigned int modem_num = chip->num_of_codecs - 1;
  1367. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
  1368. params_rate(hw_params));
  1369. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
  1370. return snd_ali_hw_params(substream, hw_params);
  1371. }
  1372. static struct snd_pcm_hardware snd_ali_modem =
  1373. {
  1374. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1375. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1376. SNDRV_PCM_INFO_MMAP_VALID |
  1377. SNDRV_PCM_INFO_RESUME |
  1378. SNDRV_PCM_INFO_SYNC_START),
  1379. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1380. .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
  1381. SNDRV_PCM_RATE_16000),
  1382. .rate_min = 8000,
  1383. .rate_max = 16000,
  1384. .channels_min = 1,
  1385. .channels_max = 1,
  1386. .buffer_bytes_max = (256*1024),
  1387. .period_bytes_min = 64,
  1388. .period_bytes_max = (256*1024),
  1389. .periods_min = 1,
  1390. .periods_max = 1024,
  1391. .fifo_size = 0,
  1392. };
  1393. static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
  1394. int channel)
  1395. {
  1396. static unsigned int rates[] = {8000, 9600, 12000, 16000};
  1397. static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
  1398. .count = ARRAY_SIZE(rates),
  1399. .list = rates,
  1400. .mask = 0,
  1401. };
  1402. int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
  1403. if (err)
  1404. return err;
  1405. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  1406. SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
  1407. }
  1408. static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
  1409. {
  1410. return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
  1411. }
  1412. static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
  1413. {
  1414. return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
  1415. }
  1416. static struct snd_pcm_ops snd_ali_modem_playback_ops = {
  1417. .open = snd_ali_modem_playback_open,
  1418. .close = snd_ali_close,
  1419. .ioctl = snd_pcm_lib_ioctl,
  1420. .hw_params = snd_ali_modem_hw_params,
  1421. .hw_free = snd_ali_hw_free,
  1422. .prepare = snd_ali_prepare,
  1423. .trigger = snd_ali_trigger,
  1424. .pointer = snd_ali_pointer,
  1425. };
  1426. static struct snd_pcm_ops snd_ali_modem_capture_ops = {
  1427. .open = snd_ali_modem_capture_open,
  1428. .close = snd_ali_close,
  1429. .ioctl = snd_pcm_lib_ioctl,
  1430. .hw_params = snd_ali_modem_hw_params,
  1431. .hw_free = snd_ali_hw_free,
  1432. .prepare = snd_ali_prepare,
  1433. .trigger = snd_ali_trigger,
  1434. .pointer = snd_ali_pointer,
  1435. };
  1436. struct ali_pcm_description {
  1437. char *name;
  1438. unsigned int playback_num;
  1439. unsigned int capture_num;
  1440. struct snd_pcm_ops *playback_ops;
  1441. struct snd_pcm_ops *capture_ops;
  1442. unsigned short class;
  1443. };
  1444. static void snd_ali_pcm_free(struct snd_pcm *pcm)
  1445. {
  1446. struct snd_ali *codec = pcm->private_data;
  1447. codec->pcm[pcm->device] = NULL;
  1448. }
  1449. static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
  1450. struct ali_pcm_description *desc)
  1451. {
  1452. struct snd_pcm *pcm;
  1453. int err;
  1454. err = snd_pcm_new(codec->card, desc->name, device,
  1455. desc->playback_num, desc->capture_num, &pcm);
  1456. if (err < 0) {
  1457. snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
  1458. return err;
  1459. }
  1460. pcm->private_data = codec;
  1461. pcm->private_free = snd_ali_pcm_free;
  1462. if (desc->playback_ops)
  1463. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1464. desc->playback_ops);
  1465. if (desc->capture_ops)
  1466. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1467. desc->capture_ops);
  1468. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1469. snd_dma_pci_data(codec->pci),
  1470. 64*1024, 128*1024);
  1471. pcm->info_flags = 0;
  1472. pcm->dev_class = desc->class;
  1473. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1474. strcpy(pcm->name, desc->name);
  1475. codec->pcm[0] = pcm;
  1476. return 0;
  1477. }
  1478. static struct ali_pcm_description ali_pcms[] = {
  1479. { .name = "ALI 5451",
  1480. .playback_num = ALI_CHANNELS,
  1481. .capture_num = 1,
  1482. .playback_ops = &snd_ali_playback_ops,
  1483. .capture_ops = &snd_ali_capture_ops
  1484. },
  1485. { .name = "ALI 5451 modem",
  1486. .playback_num = 1,
  1487. .capture_num = 1,
  1488. .playback_ops = &snd_ali_modem_playback_ops,
  1489. .capture_ops = &snd_ali_modem_capture_ops,
  1490. .class = SNDRV_PCM_CLASS_MODEM
  1491. }
  1492. };
  1493. static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
  1494. {
  1495. int i, err;
  1496. for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
  1497. err = snd_ali_pcm(codec, i, &ali_pcms[i]);
  1498. if (err < 0)
  1499. return err;
  1500. }
  1501. return 0;
  1502. }
  1503. #define ALI5451_SPDIF(xname, xindex, value) \
  1504. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  1505. .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
  1506. .put = snd_ali5451_spdif_put, .private_value = value}
  1507. #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
  1508. static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
  1509. struct snd_ctl_elem_value *ucontrol)
  1510. {
  1511. struct snd_ali *codec = kcontrol->private_data;
  1512. unsigned int spdif_enable;
  1513. spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1514. spin_lock_irq(&codec->reg_lock);
  1515. switch (kcontrol->private_value) {
  1516. case 0:
  1517. spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
  1518. break;
  1519. case 1:
  1520. spdif_enable = ((codec->spdif_mask & 0x02) &&
  1521. (codec->spdif_mask & 0x04)) ? 1 : 0;
  1522. break;
  1523. case 2:
  1524. spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
  1525. break;
  1526. default:
  1527. break;
  1528. }
  1529. ucontrol->value.integer.value[0] = spdif_enable;
  1530. spin_unlock_irq(&codec->reg_lock);
  1531. return 0;
  1532. }
  1533. static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
  1534. struct snd_ctl_elem_value *ucontrol)
  1535. {
  1536. struct snd_ali *codec = kcontrol->private_data;
  1537. unsigned int change = 0, spdif_enable = 0;
  1538. spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1539. spin_lock_irq(&codec->reg_lock);
  1540. switch (kcontrol->private_value) {
  1541. case 0:
  1542. change = (codec->spdif_mask & 0x02) ? 1 : 0;
  1543. change = change ^ spdif_enable;
  1544. if (change) {
  1545. if (spdif_enable) {
  1546. codec->spdif_mask |= 0x02;
  1547. snd_ali_enable_spdif_out(codec);
  1548. } else {
  1549. codec->spdif_mask &= ~(0x02);
  1550. codec->spdif_mask &= ~(0x04);
  1551. snd_ali_disable_spdif_out(codec);
  1552. }
  1553. }
  1554. break;
  1555. case 1:
  1556. change = (codec->spdif_mask & 0x04) ? 1 : 0;
  1557. change = change ^ spdif_enable;
  1558. if (change && (codec->spdif_mask & 0x02)) {
  1559. if (spdif_enable) {
  1560. codec->spdif_mask |= 0x04;
  1561. snd_ali_enable_spdif_chnout(codec);
  1562. } else {
  1563. codec->spdif_mask &= ~(0x04);
  1564. snd_ali_disable_spdif_chnout(codec);
  1565. }
  1566. }
  1567. break;
  1568. case 2:
  1569. change = (codec->spdif_mask & 0x01) ? 1 : 0;
  1570. change = change ^ spdif_enable;
  1571. if (change) {
  1572. if (spdif_enable) {
  1573. codec->spdif_mask |= 0x01;
  1574. snd_ali_enable_spdif_in(codec);
  1575. } else {
  1576. codec->spdif_mask &= ~(0x01);
  1577. snd_ali_disable_spdif_in(codec);
  1578. }
  1579. }
  1580. break;
  1581. default:
  1582. break;
  1583. }
  1584. spin_unlock_irq(&codec->reg_lock);
  1585. return change;
  1586. }
  1587. static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
  1588. /* spdif aplayback switch */
  1589. /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
  1590. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
  1591. /* spdif out to spdif channel */
  1592. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
  1593. /* spdif in from spdif channel */
  1594. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
  1595. };
  1596. static int __devinit snd_ali_mixer(struct snd_ali * codec)
  1597. {
  1598. struct snd_ac97_template ac97;
  1599. unsigned int idx;
  1600. int i, err;
  1601. static struct snd_ac97_bus_ops ops = {
  1602. .write = snd_ali_codec_write,
  1603. .read = snd_ali_codec_read,
  1604. };
  1605. err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
  1606. if (err < 0)
  1607. return err;
  1608. memset(&ac97, 0, sizeof(ac97));
  1609. ac97.private_data = codec;
  1610. for (i = 0; i < codec->num_of_codecs; i++) {
  1611. ac97.num = i;
  1612. err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
  1613. if (err < 0) {
  1614. snd_printk(KERN_ERR
  1615. "ali mixer %d creating error.\n", i);
  1616. if (i == 0)
  1617. return err;
  1618. codec->num_of_codecs = 1;
  1619. break;
  1620. }
  1621. }
  1622. if (codec->spdif_support) {
  1623. for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
  1624. err = snd_ctl_add(codec->card,
  1625. snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
  1626. if (err < 0)
  1627. return err;
  1628. }
  1629. }
  1630. return 0;
  1631. }
  1632. #ifdef CONFIG_PM
  1633. static int ali_suspend(struct pci_dev *pci, pm_message_t state)
  1634. {
  1635. struct snd_card *card = pci_get_drvdata(pci);
  1636. struct snd_ali *chip = card->private_data;
  1637. struct snd_ali_image *im;
  1638. int i, j;
  1639. im = chip->image;
  1640. if (!im)
  1641. return 0;
  1642. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1643. for (i = 0; i < chip->num_of_codecs; i++) {
  1644. snd_pcm_suspend_all(chip->pcm[i]);
  1645. snd_ac97_suspend(chip->ac97[i]);
  1646. }
  1647. spin_lock_irq(&chip->reg_lock);
  1648. im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
  1649. /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
  1650. im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
  1651. /* disable all IRQ bits */
  1652. outl(0, ALI_REG(chip, ALI_MISCINT));
  1653. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1654. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
  1655. continue;
  1656. im->regs[i] = inl(ALI_REG(chip, i*4));
  1657. }
  1658. for (i = 0; i < ALI_CHANNELS; i++) {
  1659. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1660. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1661. im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
  1662. }
  1663. /* stop all HW channel */
  1664. outl(0xffffffff, ALI_REG(chip, ALI_STOP));
  1665. spin_unlock_irq(&chip->reg_lock);
  1666. pci_disable_device(pci);
  1667. pci_save_