PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sound/pci/ca0106/ca0106_main.c

https://bitbucket.org/abioy/linux
C | 1909 lines | 1337 code | 207 blank | 365 comment | 134 complexity | b4c0926c966e9af12b2af019a1705f56 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
  3. * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
  4. * Version: 0.0.25
  5. *
  6. * FEATURES currently supported:
  7. * Front, Rear and Center/LFE.
  8. * Surround40 and Surround51.
  9. * Capture from MIC an LINE IN input.
  10. * SPDIF digital playback of PCM stereo and AC3/DTS works.
  11. * (One can use a standard mono mini-jack to one RCA plugs cable.
  12. * or one can use a standard stereo mini-jack to two RCA plugs cable.
  13. * Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
  14. * ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
  15. * Notes on how to capture sound:
  16. * The AC97 is used in the PLAYBACK direction.
  17. * The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
  18. * So, to record from the MIC, set the MIC Playback volume to max,
  19. * unmute the MIC and turn up the MASTER Playback volume.
  20. * So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
  21. *
  22. * The only playback controls that currently do anything are: -
  23. * Analog Front
  24. * Analog Rear
  25. * Analog Center/LFE
  26. * SPDIF Front
  27. * SPDIF Rear
  28. * SPDIF Center/LFE
  29. *
  30. * For capture from Mic in or Line in.
  31. * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
  32. *
  33. * CAPTURE feedback into PLAYBACK
  34. *
  35. * Changelog:
  36. * Support interrupts per period.
  37. * Removed noise from Center/LFE channel when in Analog mode.
  38. * Rename and remove mixer controls.
  39. * 0.0.6
  40. * Use separate card based DMA buffer for periods table list.
  41. * 0.0.7
  42. * Change remove and rename ctrls into lists.
  43. * 0.0.8
  44. * Try to fix capture sources.
  45. * 0.0.9
  46. * Fix AC3 output.
  47. * Enable S32_LE format support.
  48. * 0.0.10
  49. * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
  50. * 0.0.11
  51. * Add Model name recognition.
  52. * 0.0.12
  53. * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
  54. * Remove redundent "voice" handling.
  55. * 0.0.13
  56. * Single trigger call for multi channels.
  57. * 0.0.14
  58. * Set limits based on what the sound card hardware can do.
  59. * playback periods_min=2, periods_max=8
  60. * capture hw constraints require period_size = n * 64 bytes.
  61. * playback hw constraints require period_size = n * 64 bytes.
  62. * 0.0.15
  63. * Minor updates.
  64. * 0.0.16
  65. * Implement 192000 sample rate.
  66. * 0.0.17
  67. * Add support for SB0410 and SB0413.
  68. * 0.0.18
  69. * Modified Copyright message.
  70. * 0.0.19
  71. * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
  72. * The output codec needs resetting, otherwise all output is muted.
  73. * 0.0.20
  74. * Merge "pci_disable_device(pci);" fixes.
  75. * 0.0.21
  76. * Add 4 capture channels. (SPDIF only comes in on channel 0. )
  77. * Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
  78. * 0.0.22
  79. * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
  80. * 0.0.23
  81. * Implement support for Line-in capture on SB Live 24bit.
  82. * 0.0.24
  83. * Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
  84. * 0.0.25
  85. * Powerdown SPI DAC channels when not in use
  86. *
  87. * BUGS:
  88. * Some stability problems when unloading the snd-ca0106 kernel module.
  89. * --
  90. *
  91. * TODO:
  92. * 4 Capture channels, only one implemented so far.
  93. * Other capture rates apart from 48khz not implemented.
  94. * MIDI
  95. * --
  96. * GENERAL INFO:
  97. * Model: SB0310
  98. * P17 Chip: CA0106-DAT
  99. * AC97 Codec: STAC 9721
  100. * ADC: Philips 1361T (Stereo 24bit)
  101. * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
  102. *
  103. * GENERAL INFO:
  104. * Model: SB0410
  105. * P17 Chip: CA0106-DAT
  106. * AC97 Codec: None
  107. * ADC: WM8775EDS (4 Channel)
  108. * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
  109. * SPDIF Out control switches between Mic in and SPDIF out.
  110. * No sound out or mic input working yet.
  111. *
  112. * GENERAL INFO:
  113. * Model: SB0413
  114. * P17 Chip: CA0106-DAT
  115. * AC97 Codec: None.
  116. * ADC: Unknown
  117. * DAC: Unknown
  118. * Trying to handle it like the SB0410.
  119. *
  120. * This code was initally based on code from ALSA's emu10k1x.c which is:
  121. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  122. *
  123. * This program is free software; you can redistribute it and/or modify
  124. * it under the terms of the GNU General Public License as published by
  125. * the Free Software Foundation; either version 2 of the License, or
  126. * (at your option) any later version.
  127. *
  128. * This program is distributed in the hope that it will be useful,
  129. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  130. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  131. * GNU General Public License for more details.
  132. *
  133. * You should have received a copy of the GNU General Public License
  134. * along with this program; if not, write to the Free Software
  135. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  136. *
  137. */
  138. #include <linux/delay.h>
  139. #include <linux/init.h>
  140. #include <linux/interrupt.h>
  141. #include <linux/pci.h>
  142. #include <linux/slab.h>
  143. #include <linux/moduleparam.h>
  144. #include <linux/dma-mapping.h>
  145. #include <sound/core.h>
  146. #include <sound/initval.h>
  147. #include <sound/pcm.h>
  148. #include <sound/ac97_codec.h>
  149. #include <sound/info.h>
  150. MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
  151. MODULE_DESCRIPTION("CA0106");
  152. MODULE_LICENSE("GPL");
  153. MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
  154. // module parameters (see "Module Parameters")
  155. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  156. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  157. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  158. static uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
  159. module_param_array(index, int, NULL, 0444);
  160. MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
  161. module_param_array(id, charp, NULL, 0444);
  162. MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
  163. module_param_array(enable, bool, NULL, 0444);
  164. MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
  165. module_param_array(subsystem, uint, NULL, 0444);
  166. MODULE_PARM_DESC(subsystem, "Force card subsystem model.");
  167. #include "ca0106.h"
  168. static struct snd_ca0106_details ca0106_chip_details[] = {
  169. /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
  170. /* It is really just a normal SB Live 24bit. */
  171. /* Tested:
  172. * See ALSA bug#3251
  173. */
  174. { .serial = 0x10131102,
  175. .name = "X-Fi Extreme Audio [SBxxxx]",
  176. .gpio_type = 1,
  177. .i2c_adc = 1 } ,
  178. /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
  179. /* It is really just a normal SB Live 24bit. */
  180. /*
  181. * CTRL:CA0111-WTLF
  182. * ADC: WM8775SEDS
  183. * DAC: CS4382-KQZ
  184. */
  185. /* Tested:
  186. * Playback on front, rear, center/lfe speakers
  187. * Capture from Mic in.
  188. * Not-Tested:
  189. * Capture from Line in.
  190. * Playback to digital out.
  191. */
  192. { .serial = 0x10121102,
  193. .name = "X-Fi Extreme Audio [SB0790]",
  194. .gpio_type = 1,
  195. .i2c_adc = 1 } ,
  196. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  197. /* AudigyLS[SB0310] */
  198. { .serial = 0x10021102,
  199. .name = "AudigyLS [SB0310]",
  200. .ac97 = 1 } ,
  201. /* Unknown AudigyLS that also says SB0310 on it */
  202. { .serial = 0x10051102,
  203. .name = "AudigyLS [SB0310b]",
  204. .ac97 = 1 } ,
  205. /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
  206. { .serial = 0x10061102,
  207. .name = "Live! 7.1 24bit [SB0410]",
  208. .gpio_type = 1,
  209. .i2c_adc = 1 } ,
  210. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  211. { .serial = 0x10071102,
  212. .name = "Live! 7.1 24bit [SB0413]",
  213. .gpio_type = 1,
  214. .i2c_adc = 1 } ,
  215. /* New Audigy SE. Has a different DAC. */
  216. /* SB0570:
  217. * CTRL:CA0106-DAT
  218. * ADC: WM8775EDS
  219. * DAC: WM8768GEDS
  220. */
  221. { .serial = 0x100a1102,
  222. .name = "Audigy SE [SB0570]",
  223. .gpio_type = 1,
  224. .i2c_adc = 1,
  225. .spi_dac = 1 } ,
  226. /* New Audigy LS. Has a different DAC. */
  227. /* SB0570:
  228. * CTRL:CA0106-DAT
  229. * ADC: WM8775EDS
  230. * DAC: WM8768GEDS
  231. */
  232. { .serial = 0x10111102,
  233. .name = "Audigy SE OEM [SB0570a]",
  234. .gpio_type = 1,
  235. .i2c_adc = 1,
  236. .spi_dac = 1 } ,
  237. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  238. /* SB0438
  239. * CTRL:CA0106-DAT
  240. * ADC: WM8775SEDS
  241. * DAC: CS4382-KQZ
  242. */
  243. { .serial = 0x10091462,
  244. .name = "MSI K8N Diamond MB [SB0438]",
  245. .gpio_type = 2,
  246. .i2c_adc = 1 } ,
  247. /* MSI K8N Diamond PLUS MB */
  248. { .serial = 0x10091102,
  249. .name = "MSI K8N Diamond MB",
  250. .gpio_type = 2,
  251. .i2c_adc = 1,
  252. .spi_dac = 1 } ,
  253. /* Giga-byte GA-G1975X mobo
  254. * Novell bnc#395807
  255. */
  256. /* FIXME: the GPIO and I2C setting aren't tested well */
  257. { .serial = 0x1458a006,
  258. .name = "Giga-byte GA-G1975X",
  259. .gpio_type = 1,
  260. .i2c_adc = 1 },
  261. /* Shuttle XPC SD31P which has an onboard Creative Labs
  262. * Sound Blaster Live! 24-bit EAX
  263. * high-definition 7.1 audio processor".
  264. * Added using info from andrewvegan in alsa bug #1298
  265. */
  266. { .serial = 0x30381297,
  267. .name = "Shuttle XPC SD31P [SD31P]",
  268. .gpio_type = 1,
  269. .i2c_adc = 1 } ,
  270. /* Shuttle XPC SD11G5 which has an onboard Creative Labs
  271. * Sound Blaster Live! 24-bit EAX
  272. * high-definition 7.1 audio processor".
  273. * Fixes ALSA bug#1600
  274. */
  275. { .serial = 0x30411297,
  276. .name = "Shuttle XPC SD11G5 [SD11G5]",
  277. .gpio_type = 1,
  278. .i2c_adc = 1 } ,
  279. { .serial = 0,
  280. .name = "AudigyLS [Unknown]" }
  281. };
  282. /* hardware definition */
  283. static struct snd_pcm_hardware snd_ca0106_playback_hw = {
  284. .info = SNDRV_PCM_INFO_MMAP |
  285. SNDRV_PCM_INFO_INTERLEAVED |
  286. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  287. SNDRV_PCM_INFO_MMAP_VALID |
  288. SNDRV_PCM_INFO_SYNC_START,
  289. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  290. .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  291. SNDRV_PCM_RATE_192000),
  292. .rate_min = 48000,
  293. .rate_max = 192000,
  294. .channels_min = 2, //1,
  295. .channels_max = 2, //6,
  296. .buffer_bytes_max = ((65536 - 64) * 8),
  297. .period_bytes_min = 64,
  298. .period_bytes_max = (65536 - 64),
  299. .periods_min = 2,
  300. .periods_max = 8,
  301. .fifo_size = 0,
  302. };
  303. static struct snd_pcm_hardware snd_ca0106_capture_hw = {
  304. .info = (SNDRV_PCM_INFO_MMAP |
  305. SNDRV_PCM_INFO_INTERLEAVED |
  306. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  307. SNDRV_PCM_INFO_MMAP_VALID),
  308. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  309. #if 0 /* FIXME: looks like 44.1kHz capture causes noisy output on 48kHz */
  310. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  311. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  312. .rate_min = 44100,
  313. #else
  314. .rates = (SNDRV_PCM_RATE_48000 |
  315. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  316. .rate_min = 48000,
  317. #endif /* FIXME */
  318. .rate_max = 192000,
  319. .channels_min = 2,
  320. .channels_max = 2,
  321. .buffer_bytes_max = 65536 - 128,
  322. .period_bytes_min = 64,
  323. .period_bytes_max = 32768 - 64,
  324. .periods_min = 2,
  325. .periods_max = 2,
  326. .fifo_size = 0,
  327. };
  328. unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
  329. unsigned int reg,
  330. unsigned int chn)
  331. {
  332. unsigned long flags;
  333. unsigned int regptr, val;
  334. regptr = (reg << 16) | chn;
  335. spin_lock_irqsave(&emu->emu_lock, flags);
  336. outl(regptr, emu->port + PTR);
  337. val = inl(emu->port + DATA);
  338. spin_unlock_irqrestore(&emu->emu_lock, flags);
  339. return val;
  340. }
  341. void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
  342. unsigned int reg,
  343. unsigned int chn,
  344. unsigned int data)
  345. {
  346. unsigned int regptr;
  347. unsigned long flags;
  348. regptr = (reg << 16) | chn;
  349. spin_lock_irqsave(&emu->emu_lock, flags);
  350. outl(regptr, emu->port + PTR);
  351. outl(data, emu->port + DATA);
  352. spin_unlock_irqrestore(&emu->emu_lock, flags);
  353. }
  354. int snd_ca0106_spi_write(struct snd_ca0106 * emu,
  355. unsigned int data)
  356. {
  357. unsigned int reset, set;
  358. unsigned int reg, tmp;
  359. int n, result;
  360. reg = SPI;
  361. if (data > 0xffff) /* Only 16bit values allowed */
  362. return 1;
  363. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  364. reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
  365. set = reset | 0x10000; /* Set xxx1xxxx */
  366. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  367. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
  368. snd_ca0106_ptr_write(emu, reg, 0, set | data);
  369. result = 1;
  370. /* Wait for status bit to return to 0 */
  371. for (n = 0; n < 100; n++) {
  372. udelay(10);
  373. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  374. if (!(tmp & 0x10000)) {
  375. result = 0;
  376. break;
  377. }
  378. }
  379. if (result) /* Timed out */
  380. return 1;
  381. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  382. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
  383. return 0;
  384. }
  385. /* The ADC does not support i2c read, so only write is implemented */
  386. int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
  387. u32 reg,
  388. u32 value)
  389. {
  390. u32 tmp;
  391. int timeout = 0;
  392. int status;
  393. int retry;
  394. if ((reg > 0x7f) || (value > 0x1ff)) {
  395. snd_printk(KERN_ERR "i2c_write: invalid values.\n");
  396. return -EINVAL;
  397. }
  398. tmp = reg << 25 | value << 16;
  399. /*
  400. snd_printk(KERN_DEBUG "I2C-write:reg=0x%x, value=0x%x\n", reg, value);
  401. */
  402. /* Not sure what this I2C channel controls. */
  403. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  404. /* This controls the I2C connected to the WM8775 ADC Codec */
  405. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  406. for (retry = 0; retry < 10; retry++) {
  407. /* Send the data to i2c */
  408. //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  409. //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  410. tmp = 0;
  411. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  412. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  413. /* Wait till the transaction ends */
  414. while (1) {
  415. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  416. /*snd_printk(KERN_DEBUG "I2C:status=0x%x\n", status);*/
  417. timeout++;
  418. if ((status & I2C_A_ADC_START) == 0)
  419. break;
  420. if (timeout > 1000)
  421. break;
  422. }
  423. //Read back and see if the transaction is successful
  424. if ((status & I2C_A_ADC_ABORT) == 0)
  425. break;
  426. }
  427. if (retry == 10) {
  428. snd_printk(KERN_ERR "Writing to ADC failed!\n");
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
  434. {
  435. unsigned long flags;
  436. unsigned int intr_enable;
  437. spin_lock_irqsave(&emu->emu_lock, flags);
  438. intr_enable = inl(emu->port + INTE) | intrenb;
  439. outl(intr_enable, emu->port + INTE);
  440. spin_unlock_irqrestore(&emu->emu_lock, flags);
  441. }
  442. static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
  443. {
  444. unsigned long flags;
  445. unsigned int intr_enable;
  446. spin_lock_irqsave(&emu->emu_lock, flags);
  447. intr_enable = inl(emu->port + INTE) & ~intrenb;
  448. outl(intr_enable, emu->port + INTE);
  449. spin_unlock_irqrestore(&emu->emu_lock, flags);
  450. }
  451. static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
  452. {
  453. kfree(runtime->private_data);
  454. }
  455. static const int spi_dacd_reg[] = {
  456. [PCM_FRONT_CHANNEL] = SPI_DACD4_REG,
  457. [PCM_REAR_CHANNEL] = SPI_DACD0_REG,
  458. [PCM_CENTER_LFE_CHANNEL]= SPI_DACD2_REG,
  459. [PCM_UNKNOWN_CHANNEL] = SPI_DACD1_REG,
  460. };
  461. static const int spi_dacd_bit[] = {
  462. [PCM_FRONT_CHANNEL] = SPI_DACD4_BIT,
  463. [PCM_REAR_CHANNEL] = SPI_DACD0_BIT,
  464. [PCM_CENTER_LFE_CHANNEL]= SPI_DACD2_BIT,
  465. [PCM_UNKNOWN_CHANNEL] = SPI_DACD1_BIT,
  466. };
  467. static void restore_spdif_bits(struct snd_ca0106 *chip, int idx)
  468. {
  469. if (chip->spdif_str_bits[idx] != chip->spdif_bits[idx]) {
  470. chip->spdif_str_bits[idx] = chip->spdif_bits[idx];
  471. snd_ca0106_ptr_write(chip, SPCS0 + idx, 0,
  472. chip->spdif_str_bits[idx]);
  473. }
  474. }
  475. /* open_playback callback */
  476. static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
  477. int channel_id)
  478. {
  479. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  480. struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
  481. struct snd_ca0106_pcm *epcm;
  482. struct snd_pcm_runtime *runtime = substream->runtime;
  483. int err;
  484. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  485. if (epcm == NULL)
  486. return -ENOMEM;
  487. epcm->emu = chip;
  488. epcm->substream = substream;
  489. epcm->channel_id=channel_id;
  490. runtime->private_data = epcm;
  491. runtime->private_free = snd_ca0106_pcm_free_substream;
  492. runtime->hw = snd_ca0106_playback_hw;
  493. channel->emu = chip;
  494. channel->number = channel_id;
  495. channel->use = 1;
  496. /*
  497. printk(KERN_DEBUG "open:channel_id=%d, chip=%p, channel=%p\n",
  498. channel_id, chip, channel);
  499. */
  500. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  501. channel->epcm = epcm;
  502. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  503. return err;
  504. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  505. return err;
  506. snd_pcm_set_sync(substream);
  507. if (chip->details->spi_dac && channel_id != PCM_FRONT_CHANNEL) {
  508. const int reg = spi_dacd_reg[channel_id];
  509. /* Power up dac */
  510. chip->spi_dac_reg[reg] &= ~spi_dacd_bit[channel_id];
  511. err = snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
  512. if (err < 0)
  513. return err;
  514. }
  515. restore_spdif_bits(chip, channel_id);
  516. return 0;
  517. }
  518. /* close callback */
  519. static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
  520. {
  521. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  522. struct snd_pcm_runtime *runtime = substream->runtime;
  523. struct snd_ca0106_pcm *epcm = runtime->private_data;
  524. chip->playback_channels[epcm->channel_id].use = 0;
  525. restore_spdif_bits(chip, epcm->channel_id);
  526. if (chip->details->spi_dac && epcm->channel_id != PCM_FRONT_CHANNEL) {
  527. const int reg = spi_dacd_reg[epcm->channel_id];
  528. /* Power down DAC */
  529. chip->spi_dac_reg[reg] |= spi_dacd_bit[epcm->channel_id];
  530. snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]);
  531. }
  532. /* FIXME: maybe zero others */
  533. return 0;
  534. }
  535. static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
  536. {
  537. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  538. }
  539. static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
  540. {
  541. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  542. }
  543. static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
  544. {
  545. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  546. }
  547. static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
  548. {
  549. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  550. }
  551. /* open_capture callback */
  552. static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
  553. int channel_id)
  554. {
  555. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  556. struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
  557. struct snd_ca0106_pcm *epcm;
  558. struct snd_pcm_runtime *runtime = substream->runtime;
  559. int err;
  560. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  561. if (epcm == NULL) {
  562. snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
  563. return -ENOMEM;
  564. }
  565. epcm->emu = chip;
  566. epcm->substream = substream;
  567. epcm->channel_id=channel_id;
  568. runtime->private_data = epcm;
  569. runtime->private_free = snd_ca0106_pcm_free_substream;
  570. runtime->hw = snd_ca0106_capture_hw;
  571. channel->emu = chip;
  572. channel->number = channel_id;
  573. channel->use = 1;
  574. /*
  575. printk(KERN_DEBUG "open:channel_id=%d, chip=%p, channel=%p\n",
  576. channel_id, chip, channel);
  577. */
  578. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  579. channel->epcm = epcm;
  580. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  581. return err;
  582. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  583. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  584. return err;
  585. return 0;
  586. }
  587. /* close callback */
  588. static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
  589. {
  590. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  591. struct snd_pcm_runtime *runtime = substream->runtime;
  592. struct snd_ca0106_pcm *epcm = runtime->private_data;
  593. chip->capture_channels[epcm->channel_id].use = 0;
  594. /* FIXME: maybe zero others */
  595. return 0;
  596. }
  597. static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
  598. {
  599. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  600. }
  601. static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
  602. {
  603. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  604. }
  605. static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
  606. {
  607. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  608. }
  609. static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
  610. {
  611. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  612. }
  613. /* hw_params callback */
  614. static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  615. struct snd_pcm_hw_params *hw_params)
  616. {
  617. return snd_pcm_lib_malloc_pages(substream,
  618. params_buffer_bytes(hw_params));
  619. }
  620. /* hw_free callback */
  621. static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  622. {
  623. return snd_pcm_lib_free_pages(substream);
  624. }
  625. /* hw_params callback */
  626. static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  627. struct snd_pcm_hw_params *hw_params)
  628. {
  629. return snd_pcm_lib_malloc_pages(substream,
  630. params_buffer_bytes(hw_params));
  631. }
  632. /* hw_free callback */
  633. static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  634. {
  635. return snd_pcm_lib_free_pages(substream);
  636. }
  637. /* prepare playback callback */
  638. static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
  639. {
  640. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  641. struct snd_pcm_runtime *runtime = substream->runtime;
  642. struct snd_ca0106_pcm *epcm = runtime->private_data;
  643. int channel = epcm->channel_id;
  644. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  645. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  646. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  647. u32 hcfg_set = 0x00000000;
  648. u32 hcfg;
  649. u32 reg40_mask = 0x30000 << (channel<<1);
  650. u32 reg40_set = 0;
  651. u32 reg40;
  652. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  653. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  654. u32 reg71_set = 0;
  655. u32 reg71;
  656. int i;
  657. #if 0 /* debug */
  658. snd_printk(KERN_DEBUG
  659. "prepare:channel_number=%d, rate=%d, format=0x%x, "
  660. "channels=%d, buffer_size=%ld, period_size=%ld, "
  661. "periods=%u, frames_to_bytes=%d\n",
  662. channel, runtime->rate, runtime->format,
  663. runtime->channels, runtime->buffer_size,
  664. runtime->period_size, runtime->periods,
  665. frames_to_bytes(runtime, 1));
  666. snd_printk(KERN_DEBUG "dma_addr=%x, dma_area=%p, table_base=%p\n",
  667. runtime->dma_addr, runtime->dma_area, table_base);
  668. snd_printk(KERN_DEBUG "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
  669. emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  670. #endif /* debug */
  671. /* Rate can be set per channel. */
  672. /* reg40 control host to fifo */
  673. /* reg71 controls DAC rate. */
  674. switch (runtime->rate) {
  675. case 44100:
  676. reg40_set = 0x10000 << (channel<<1);
  677. reg71_set = 0x01010000;
  678. break;
  679. case 48000:
  680. reg40_set = 0;
  681. reg71_set = 0;
  682. break;
  683. case 96000:
  684. reg40_set = 0x20000 << (channel<<1);
  685. reg71_set = 0x02020000;
  686. break;
  687. case 192000:
  688. reg40_set = 0x30000 << (channel<<1);
  689. reg71_set = 0x03030000;
  690. break;
  691. default:
  692. reg40_set = 0;
  693. reg71_set = 0;
  694. break;
  695. }
  696. /* Format is a global setting */
  697. /* FIXME: Only let the first channel accessed set this. */
  698. switch (runtime->format) {
  699. case SNDRV_PCM_FORMAT_S16_LE:
  700. hcfg_set = 0;
  701. break;
  702. case SNDRV_PCM_FORMAT_S32_LE:
  703. hcfg_set = HCFG_PLAYBACK_S32_LE;
  704. break;
  705. default:
  706. hcfg_set = 0;
  707. break;
  708. }
  709. hcfg = inl(emu->port + HCFG) ;
  710. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  711. outl(hcfg, emu->port + HCFG);
  712. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  713. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  714. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  715. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  716. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  717. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  718. /* FIXME: Check emu->buffer.size before actually writing to it. */
  719. for(i=0; i < runtime->periods; i++) {
  720. table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
  721. table_base[i*2+1] = period_size_bytes << 16;
  722. }
  723. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  724. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  725. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  726. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  727. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  728. /* FIXME test what 0 bytes does. */
  729. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  730. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  731. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  732. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  733. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  734. #if 0
  735. snd_ca0106_ptr_write(emu, SPCS0, 0,
  736. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  737. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  738. SPCS_GENERATIONSTATUS | 0x00001200 |
  739. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  740. #endif
  741. return 0;
  742. }
  743. /* prepare capture callback */
  744. static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
  745. {
  746. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  747. struct snd_pcm_runtime *runtime = substream->runtime;
  748. struct snd_ca0106_pcm *epcm = runtime->private_data;
  749. int channel = epcm->channel_id;
  750. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  751. u32 hcfg_set = 0x00000000;
  752. u32 hcfg;
  753. u32 over_sampling=0x2;
  754. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  755. u32 reg71_set = 0;
  756. u32 reg71;
  757. #if 0 /* debug */
  758. snd_printk(KERN_DEBUG
  759. "prepare:channel_number=%d, rate=%d, format=0x%x, "
  760. "channels=%d, buffer_size=%ld, period_size=%ld, "
  761. "periods=%u, frames_to_bytes=%d\n",
  762. channel, runtime->rate, runtime->format,
  763. runtime->channels, runtime->buffer_size,
  764. runtime->period_size, runtime->periods,
  765. frames_to_bytes(runtime, 1));
  766. snd_printk(KERN_DEBUG "dma_addr=%x, dma_area=%p, table_base=%p\n",
  767. runtime->dma_addr, runtime->dma_area, table_base);
  768. snd_printk(KERN_DEBUG "dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
  769. emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  770. #endif /* debug */
  771. /* reg71 controls ADC rate. */
  772. switch (runtime->rate) {
  773. case 44100:
  774. reg71_set = 0x00004000;
  775. break;
  776. case 48000:
  777. reg71_set = 0;
  778. break;
  779. case 96000:
  780. reg71_set = 0x00008000;
  781. over_sampling=0xa;
  782. break;
  783. case 192000:
  784. reg71_set = 0x0000c000;
  785. over_sampling=0xa;
  786. break;
  787. default:
  788. reg71_set = 0;
  789. break;
  790. }
  791. /* Format is a global setting */
  792. /* FIXME: Only let the first channel accessed set this. */
  793. switch (runtime->format) {
  794. case SNDRV_PCM_FORMAT_S16_LE:
  795. hcfg_set = 0;
  796. break;
  797. case SNDRV_PCM_FORMAT_S32_LE:
  798. hcfg_set = HCFG_CAPTURE_S32_LE;
  799. break;
  800. default:
  801. hcfg_set = 0;
  802. break;
  803. }
  804. hcfg = inl(emu->port + HCFG) ;
  805. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  806. outl(hcfg, emu->port + HCFG);
  807. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  808. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  809. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  810. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  811. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  812. }
  813. /*
  814. printk(KERN_DEBUG
  815. "prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, "
  816. "buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",
  817. channel, runtime->rate, runtime->format, runtime->channels,
  818. runtime->buffer_size, runtime->period_size,
  819. frames_to_bytes(runtime, 1));
  820. */
  821. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  822. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  823. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  824. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  825. return 0;
  826. }
  827. /* trigger_playback callback */
  828. static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
  829. int cmd)
  830. {
  831. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  832. struct snd_pcm_runtime *runtime;
  833. struct snd_ca0106_pcm *epcm;
  834. int channel;
  835. int result = 0;
  836. struct snd_pcm_substream *s;
  837. u32 basic = 0;
  838. u32 extended = 0;
  839. u32 bits;
  840. int running = 0;
  841. switch (cmd) {
  842. case SNDRV_PCM_TRIGGER_START:
  843. case SNDRV_PCM_TRIGGER_RESUME:
  844. running = 1;
  845. break;
  846. case SNDRV_PCM_TRIGGER_STOP:
  847. case SNDRV_PCM_TRIGGER_SUSPEND:
  848. default:
  849. running = 0;
  850. break;
  851. }
  852. snd_pcm_group_for_each_entry(s, substream) {
  853. if (snd_pcm_substream_chip(s) != emu ||
  854. s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  855. continue;
  856. runtime = s->runtime;
  857. epcm = runtime->private_data;
  858. channel = epcm->channel_id;
  859. /* snd_printk(KERN_DEBUG "channel=%d\n", channel); */
  860. epcm->running = running;
  861. basic |= (0x1 << channel);
  862. extended |= (0x10 << channel);
  863. snd_pcm_trigger_done(s, substream);
  864. }
  865. /* snd_printk(KERN_DEBUG "basic=0x%x, extended=0x%x\n",basic, extended); */
  866. switch (cmd) {
  867. case SNDRV_PCM_TRIGGER_START:
  868. case SNDRV_PCM_TRIGGER_RESUME:
  869. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  870. bits |= extended;
  871. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  872. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  873. bits |= basic;
  874. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  875. break;
  876. case SNDRV_PCM_TRIGGER_STOP:
  877. case SNDRV_PCM_TRIGGER_SUSPEND:
  878. bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
  879. bits &= ~basic;
  880. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
  881. bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
  882. bits &= ~extended;
  883. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
  884. break;
  885. default:
  886. result = -EINVAL;
  887. break;
  888. }
  889. return result;
  890. }
  891. /* trigger_capture callback */
  892. static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
  893. int cmd)
  894. {
  895. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  896. struct snd_pcm_runtime *runtime = substream->runtime;
  897. struct snd_ca0106_pcm *epcm = runtime->private_data;
  898. int channel = epcm->channel_id;
  899. int result = 0;
  900. switch (cmd) {
  901. case SNDRV_PCM_TRIGGER_START:
  902. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  903. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  904. epcm->running = 1;
  905. break;
  906. case SNDRV_PCM_TRIGGER_STOP:
  907. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  908. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  909. epcm->running = 0;
  910. break;
  911. default:
  912. result = -EINVAL;
  913. break;
  914. }
  915. return result;
  916. }
  917. /* pointer_playback callback */
  918. static snd_pcm_uframes_t
  919. snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
  920. {
  921. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  922. struct snd_pcm_runtime *runtime = substream->runtime;
  923. struct snd_ca0106_pcm *epcm = runtime->private_data;
  924. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  925. int channel = epcm->channel_id;
  926. if (!epcm->running)
  927. return 0;
  928. ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  929. ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  930. ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  931. if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  932. ptr2 = bytes_to_frames(runtime, ptr1);
  933. ptr2+= (ptr4 >> 3) * runtime->period_size;
  934. ptr=ptr2;
  935. if (ptr >= runtime->buffer_size)
  936. ptr -= runtime->buffer_size;
  937. /*
  938. printk(KERN_DEBUG "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
  939. "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
  940. ptr1, ptr2, ptr, (int)runtime->buffer_size,
  941. (int)runtime->period_size, (int)runtime->frame_bits,
  942. (int)runtime->rate);
  943. */
  944. return ptr;
  945. }
  946. /* pointer_capture callback */
  947. static snd_pcm_uframes_t
  948. snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
  949. {
  950. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  951. struct snd_pcm_runtime *runtime = substream->runtime;
  952. struct snd_ca0106_pcm *epcm = runtime->private_data;
  953. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  954. int channel = channel=epcm->channel_id;
  955. if (!epcm->running)
  956. return 0;
  957. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  958. ptr2 = bytes_to_frames(runtime, ptr1);
  959. ptr=ptr2;
  960. if (ptr >= runtime->buffer_size)
  961. ptr -= runtime->buffer_size;
  962. /*
  963. printk(KERN_DEBUG "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
  964. "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
  965. ptr1, ptr2, ptr, (int)runtime->buffer_size,
  966. (int)runtime->period_size, (int)runtime->frame_bits,
  967. (int)runtime->rate);
  968. */
  969. return ptr;
  970. }
  971. /* operators */
  972. static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
  973. .open = snd_ca0106_pcm_open_playback_front,
  974. .close = snd_ca0106_pcm_close_playback,
  975. .ioctl = snd_pcm_lib_ioctl,
  976. .hw_params = snd_ca0106_pcm_hw_params_playback,
  977. .hw_free = snd_ca0106_pcm_hw_free_playback,
  978. .prepare = snd_ca0106_pcm_prepare_playback,
  979. .trigger = snd_ca0106_pcm_trigger_playback,
  980. .pointer = snd_ca0106_pcm_pointer_playback,
  981. };
  982. static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
  983. .open = snd_ca0106_pcm_open_0_capture,
  984. .close = snd_ca0106_pcm_close_capture,
  985. .ioctl = snd_pcm_lib_ioctl,
  986. .hw_params = snd_ca0106_pcm_hw_params_capture,
  987. .hw_free = snd_ca0106_pcm_hw_free_capture,
  988. .prepare = snd_ca0106_pcm_prepare_capture,
  989. .trigger = snd_ca0106_pcm_trigger_capture,
  990. .pointer = snd_ca0106_pcm_pointer_capture,
  991. };
  992. static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
  993. .open = snd_ca0106_pcm_open_1_capture,
  994. .close = snd_ca0106_pcm_close_capture,
  995. .ioctl = snd_pcm_lib_ioctl,
  996. .hw_params = snd_ca0106_pcm_hw_params_capture,
  997. .hw_free = snd_ca0106_pcm_hw_free_capture,
  998. .prepare = snd_ca0106_pcm_prepare_capture,
  999. .trigger = snd_ca0106_pcm_trigger_capture,
  1000. .pointer = snd_ca0106_pcm_pointer_capture,
  1001. };
  1002. static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
  1003. .open = snd_ca0106_pcm_open_2_capture,
  1004. .close = snd_ca0106_pcm_close_capture,
  1005. .ioctl = snd_pcm_lib_ioctl,
  1006. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1007. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1008. .prepare = snd_ca0106_pcm_prepare_capture,
  1009. .trigger = snd_ca0106_pcm_trigger_capture,
  1010. .pointer = snd_ca0106_pcm_pointer_capture,
  1011. };
  1012. static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
  1013. .open = snd_ca0106_pcm_open_3_capture,
  1014. .close = snd_ca0106_pcm_close_capture,
  1015. .ioctl = snd_pcm_lib_ioctl,
  1016. .hw_params = snd_ca0106_pcm_hw_params_capture,
  1017. .hw_free = snd_ca0106_pcm_hw_free_capture,
  1018. .prepare = snd_ca0106_pcm_prepare_capture,
  1019. .trigger = snd_ca0106_pcm_trigger_capture,
  1020. .pointer = snd_ca0106_pcm_pointer_capture,
  1021. };
  1022. static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
  1023. .open = snd_ca0106_pcm_open_playback_center_lfe,
  1024. .close = snd_ca0106_pcm_close_playback,
  1025. .ioctl = snd_pcm_lib_ioctl,
  1026. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1027. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1028. .prepare = snd_ca0106_pcm_prepare_playback,
  1029. .trigger = snd_ca0106_pcm_trigger_playback,
  1030. .pointer = snd_ca0106_pcm_pointer_playback,
  1031. };
  1032. static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
  1033. .open = snd_ca0106_pcm_open_playback_unknown,
  1034. .close = snd_ca0106_pcm_close_playback,
  1035. .ioctl = snd_pcm_lib_ioctl,
  1036. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1037. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1038. .prepare = snd_ca0106_pcm_prepare_playback,
  1039. .trigger = snd_ca0106_pcm_trigger_playback,
  1040. .pointer = snd_ca0106_pcm_pointer_playback,
  1041. };
  1042. static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
  1043. .open = snd_ca0106_pcm_open_playback_rear,
  1044. .close = snd_ca0106_pcm_close_playback,
  1045. .ioctl = snd_pcm_lib_ioctl,
  1046. .hw_params = snd_ca0106_pcm_hw_params_playback,
  1047. .hw_free = snd_ca0106_pcm_hw_free_playback,
  1048. .prepare = snd_ca0106_pcm_prepare_playback,
  1049. .trigger = snd_ca0106_pcm_trigger_playback,
  1050. .pointer = snd_ca0106_pcm_pointer_playback,
  1051. };
  1052. static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
  1053. unsigned short reg)
  1054. {
  1055. struct snd_ca0106 *emu = ac97->private_data;
  1056. unsigned long flags;
  1057. unsigned short val;
  1058. spin_lock_irqsave(&emu->emu_lock, flags);
  1059. outb(reg, emu->port + AC97ADDRESS);
  1060. val = inw(emu->port + AC97DATA);
  1061. spin_unlock_irqrestore(&emu->emu_lock, flags);
  1062. return val;
  1063. }
  1064. static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
  1065. unsigned short reg, unsigned short val)
  1066. {
  1067. struct snd_ca0106 *emu = ac97->private_data;
  1068. unsigned long flags;
  1069. spin_lock_irqsave(&emu->emu_lock, flags);
  1070. outb(reg, emu->port + AC97ADDRESS);
  1071. outw(val, emu->port + AC97DATA);
  1072. spin_unlock_irqrestore(&emu->emu_lock, flags);
  1073. }
  1074. static int snd_ca0106_ac97(struct snd_ca0106 *chip)
  1075. {
  1076. struct snd_ac97_bus *pbus;
  1077. struct snd_ac97_template ac97;
  1078. int err;
  1079. static struct snd_ac97_bus_ops ops = {
  1080. .write = snd_ca0106_ac97_write,
  1081. .read = snd_ca0106_ac97_read,
  1082. };
  1083. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  1084. return err;
  1085. pbus->no_vra = 1; /* we don't need VRA */
  1086. memset(&ac97, 0, sizeof(ac97));
  1087. ac97.private_data = chip;
  1088. ac97.scaps = AC97_SCAP_NO_SPDIF;
  1089. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  1090. }
  1091. static void ca0106_stop_chip(struct snd_ca0106 *chip);
  1092. static int snd_ca0106_free(struct snd_ca0106 *chip)
  1093. {
  1094. if (chip->res_port != NULL) {
  1095. /* avoid access to already used hardware */
  1096. ca0106_stop_chip(chip);
  1097. }
  1098. if (chip->irq >= 0)
  1099. free_irq(chip->irq, chip);
  1100. // release the data
  1101. #if 1
  1102. if (chip->buffer.area)
  1103. snd_dma_free_pages(&chip->buffer);
  1104. #endif
  1105. // release the i/o port
  1106. release_and_free_resource(chip->res_port);
  1107. pci_disable_device(chip->pci);
  1108. kfree(chip);
  1109. return 0;
  1110. }
  1111. static int snd_ca0106_dev_free(struct snd_device *device)
  1112. {
  1113. struct snd_ca0106 *chip = device->device_data;
  1114. return snd_ca0106_free(chip);
  1115. }
  1116. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
  1117. {
  1118. unsigned int status;
  1119. struct snd_ca0106 *chip = dev_id;
  1120. int i;
  1121. int mask;
  1122. unsigned int stat76;
  1123. struct snd_ca0106_channel *pchannel;
  1124. status = inl(chip->port + IPR);
  1125. if (! status)
  1126. return IRQ_NONE;
  1127. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  1128. /*
  1129. snd_printk(KERN_DEBUG "interrupt status = 0x%08x, stat76=0x%08x\n",
  1130. status, stat76);
  1131. snd_printk(KERN_DEBUG "ptr=0x%08x\n",
  1132. snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  1133. */
  1134. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  1135. for(i = 0; i < 4; i++) {
  1136. pchannel = &(chip->playback_channels[i]);
  1137. if (stat76 & mask) {
  1138. /* FIXME: Select the correct substream for period elapsed */
  1139. if(pchannel->use) {
  1140. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1141. //printk(KERN_INFO "interrupt [%d] used\n", i);
  1142. }
  1143. }
  1144. //printk(KERN_INFO "channel=%p\n",pchannel);
  1145. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1146. mask <<= 1;
  1147. }
  1148. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  1149. for(i = 0; i < 4; i++) {
  1150. pchannel = &(chip->capture_channels[i]);
  1151. if (stat76 & mask) {
  1152. /* FIXME: Select the correct substream for period elapsed */
  1153. if(pchannel->use) {
  1154. snd_pcm_period_elapsed(pchannel->epcm->substream);
  1155. //printk(KERN_INFO "interrupt [%d] used\n", i);
  1156. }
  1157. }
  1158. //printk(KERN_INFO "channel=%p\n",pchannel);
  1159. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1160. mask <<= 1;
  1161. }
  1162. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  1163. if (chip->midi.dev_id &&
  1164. (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
  1165. if (chip->midi.interrupt)
  1166. chip->midi.interrupt(&chip->midi, status);
  1167. else
  1168. chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
  1169. }
  1170. // acknowledge the interrupt if necessary
  1171. outl(status, chip->port+IPR);
  1172. return IRQ_HANDLED;
  1173. }
  1174. static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device)
  1175. {
  1176. struct snd_pcm *pcm;
  1177. struct snd_pcm_substream *substream;
  1178. int err;
  1179. err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm);
  1180. if (err < 0)
  1181. return err;
  1182. pcm->private_data = emu;
  1183. switch (device) {
  1184. case 0:
  1185. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  1186. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  1187. break;
  1188. case 1:
  1189. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  1190. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  1191. break;
  1192. case 2:
  1193. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  1194. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  1195. break;
  1196. case 3:
  1197. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  1198. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  1199. break;
  1200. }
  1201. pcm->info_flags = 0;
  1202. strcpy(pcm->name, "CA0106");
  1203. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1204. substream;
  1205. substream = substream->next) {
  1206. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1207. SNDRV_DMA_TYPE_DEV,
  1208. snd_dma_pci_data(emu->pci),
  1209. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  1210. return err;
  1211. }
  1212. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1213. substream;
  1214. substream = substream->next) {
  1215. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1216. SNDRV_DMA_TYPE_DEV,
  1217. snd_dma_pci_data(emu->pci),
  1218. 64*1024, 64*1024)) < 0)
  1219. return err;
  1220. }
  1221. emu->pcm[device] = pcm;
  1222. return 0;
  1223. }
  1224. #define SPI_REG(reg, value) (((reg) << SPI_REG_SHIFT) | (value))
  1225. static unsigned int spi_dac_init[] = {
  1226. SPI_REG(SPI_LDA1_REG, SPI_DA_BIT_0dB), /* 0dB dig. attenuation */
  1227. SPI_REG(SPI_RDA1_REG, SPI_DA_BIT_0dB),
  1228. SPI_REG(SPI_PL_REG, SPI_PL_BIT_L_L | SPI_PL_BIT_R_R | SPI_IZD_BIT),
  1229. SPI_REG(SPI_FMT_REG, SPI_FMT_BIT_I2S | SPI_IWL_BIT_24),
  1230. SPI_REG(SPI_LDA2_REG, SPI_DA_BIT_0dB),
  1231. SPI_REG(SPI_RDA2_REG, SPI_DA_BIT_0dB),
  1232. SPI_REG(SPI_LDA3_REG, SPI_DA_BIT_0dB),
  1233. SPI_REG(SPI_RDA3_REG, SPI_DA_BIT_0dB),
  1234. SPI_REG(SPI_MASTDA_REG, SPI_DA_BIT_0dB),
  1235. SPI_REG(9, 0x00),
  1236. SPI_REG(SPI_MS_REG, SPI_DACD0_BIT | SPI_DACD1_BIT | SPI_DACD2_BIT),
  1237. SPI_REG(12, 0x00),
  1238. SPI_REG(SPI_LDA4_REG, SPI_DA_BIT_0dB),
  1239. SPI_REG(SPI_RDA4_REG, SPI_DA_BIT_0dB | SPI_DA_BIT_UPDATE),
  1240. SPI_REG(SPI_DACD4_REG, 0x00),
  1241. };
  1242. static unsigned int i2c_adc_init[][2] = {
  1243. { 0x17, 0x00 }, /* Reset */
  1244. { 0x07, 0x00 }, /* Timeout */
  1245. { 0x0b, 0x22 }, /* Interface control */
  1246. { 0x0c, 0x22 }, /* Master mode control */
  1247. { 0x0d, 0x08 }, /* Powerdown control */
  1248. { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
  1249. { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
  1250. { 0x10, 0x7b }, /* ALC Control 1 */
  1251. { 0x11, 0x00 }, /* ALC Control 2 */
  1252. { 0x12, 0x32 }, /* ALC Control 3 */
  1253. { 0x13, 0x00 }, /* Noise gate control */
  1254. { 0x14, 0xa6 }, /* Limiter control */
  1255. { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
  1256. };
  1257. static void ca0106_init_chip(struct snd_ca0106 *chip, int resume)
  1258. {
  1259. int ch;
  1260. unsigned int def_bits;
  1261. outl(0, chip->port + INTE);
  1262. /*
  1263. * Init to 0x02109204 :
  1264. * Clock accuracy = 0 (1000ppm)
  1265. * Sample Rate = 2 (48kHz)
  1266. * Audio Channel = 1 (Left of 2)
  1267. * Source Number = 0 (Unspecified)
  1268. * Generation Status = 1 (Original for Cat Code 12)
  1269. * Cat Code = 12 (Digital Signal Mixer)
  1270. * Mode = 0 (Mode 0)
  1271. * Emphasis = 0 (None)
  1272. * CP = 1 (Copyright unasserted)
  1273. * AN = 0 (Audio data)
  1274. * P = 0 (Consumer)
  1275. */
  1276. def_bits =
  1277. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1278. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1279. SPCS_GENERATIONSTATUS | 0x00001200 |
  1280. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
  1281. if (!resume) {
  1282. chip->spdif_str_bits[0] = chip->spdif_bits[0] = def_bits;
  1283. chip->spdif_str_bits[1] = chip->spdif_bits[1] = def_bits;
  1284. chip->spdif_str_bits[2] = chip->spdif_bits[2] = def_bits;
  1285. chip->spdif_str_bits[3] = chip->spdif_bits[3] = def_bits;
  1286. }
  1287. /* Only SPCS1 has been tested */
  1288. snd_ca0106_ptr_write(chip, SPCS1, 0, chip->spdif_str_bits[1]);
  1289. snd_ca0106_ptr_write(chip, SPCS0, 0, chip->spdif_str_bits[0]);
  1290. snd_ca0106_ptr_write(chip, SPCS2, 0, chip->spdif_str_bits[2]);
  1291. snd_ca0106_ptr_write(chip, SPCS3, 0, chip->spdif_str_bits[3]);
  1292. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1293. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1294. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1295. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1296. outw(0x8000, chip->port + AC97DATA);
  1297. #if 0 /* FIXME: what are these? */
  1298. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1299. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1300. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1301. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1302. #endif
  1303. /* OSS drivers set this. */
  1304. /* snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); */
  1305. /* Analog or Digital output */
  1306. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1307. /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers.
  1308. * Use 0x000f0000 for surround71
  1309. */
  1310. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000);
  1311. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1312. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0);*/ /* Analogue out */
  1313. /*snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00);*/ /* Digital out */
  1314. /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1315. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000);
  1316. /* (Mute) CAPTURE feedback into PLAYBACK volume.
  1317. * Only lower 16 bits matter.
  1318. */
  1319. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff);
  1320. /* SPDIF IN Volume */
  1321. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000);
  1322. /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1323. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000);
  1324. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1325. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1326. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1327. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1328. for (ch = 0; ch < 4; ch++) {
  1329. /* Only high 16 bits matter */
  1330. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030);
  1331. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1332. #if 0 /* Mute */
  1333. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040);
  1334. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040);
  1335. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff);
  1336. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff);
  1337. #endif
  1338. }
  1339. if (chip->details->i2c_adc == 1) {
  1340. /* Select MIC, Line in, TAD in, AUX in */
  1341. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1342. /* Default to CAPTURE_SOURCE to i2s in */
  1343. if (!resume)
  1344. chip->capture_source = 3;
  1345. } else if (chip->details->ac97 == 1) {
  1346. /* Default to AC97 in */
  1347. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
  1348. /* Default to CAPTURE_SOURCE to AC97 in */
  1349. if (!resume)
  1350. chip->capture_source = 4;
  1351. } else {
  1352. /* Select MIC, Line in, TAD in, AUX in */
  1353. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
  1354. /* Default to Set CAPTURE_SOURCE to i2s in */
  1355. if (!resume)
  1356. chip->capture_source = 3;
  1357. }
  1358. if (chip->details->gpio_type == 2) {
  1359. /* The SB0438 use GPIO differently. */
  1360. /* FIXME: Still need to find out what the other GPIO bits do.
  1361. * E.g. For digital spdif out.
  1362. */
  1363. outl(0x0, chip->port+GPIO);
  1364. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1365. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1366. } else if (chip->details->gpio_type == 1) {
  1367. /* The SB0410 and SB0413 use GPIO differently. */
  1368. /* FIXME: Still need to find out what the other GPIO bits do.
  1369. * E.g. For digital spdif out.
  1370. */
  1371. outl(0x0, chip->port+GPIO);
  1372. /* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
  1373. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1374. } else {
  1375. outl(0x0, chip->port+GPIO);
  1376. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1377. /* outl(0x005f02a2, chip->port+GPIO); */ /* SPDIF */
  1378. }
  1379. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1380. /* outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG); */
  1381. /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1382. /* outl(0x00001409, chip->port+HCFG); */
  1383. /* outl(0x00000009, chip->port+HCFG); */
  1384. /* AC97 2.0, Enable outputs. */
  1385. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG);
  1386. if (chip->details->i2c_adc == 1) {
  1387. /* The SB0410 and SB0413 use I2C to control ADC. */
  1388. int size, n;
  1389. size = ARRAY_SIZE(i2c_adc_init);
  1390. /* snd_printk(KERN_DEBUG "I2C:array size=0x%x\n", size); */
  1391. for (n = 0; n < size; n++)
  1392. snd_ca0106_i2c_write(chip, i2c_adc_init[n][0],
  1393. i2c_adc_init[n][1]);
  1394. for (n = 0; n < 4; n++) {
  1395. chip->i2c_capture_volume[n][0] = 0xcf;
  1396. chip->i2c_capture_volume[n][1] = 0xcf;
  1397. }
  1398. chip->i2c_capture_source = 2; /* Line in */
  1399. /* Enable Line-in capture. MIC in currently untested. */
  1400. /* snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); */
  1401. }
  1402. if (chip->details->spi_dac == 1) {
  1403. /* The SB0570 use SPI to control DAC. */
  1404. int size, n;
  1405. size = ARRAY_SIZE(spi_dac_init);
  1406. for (n = 0; n < size; n++) {
  1407. int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
  1408. snd_ca0106_spi_write(chip, spi_dac_init[n]);
  1409. if (reg < ARRAY_SIZE(chip->spi_dac_reg))
  1410. chip->spi_dac_reg[reg] = spi_dac_init[n];
  1411. }
  1412. }
  1413. }
  1414. static void ca0106_stop_chip(struct snd_ca0106 *chip)
  1415. {
  1416. /* disable interrupts */
  1417. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  1418. outl(0, chip->port + INTE);
  1419. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  1420. udelay(1000);
  1421. /* disable audio */
  1422. /* outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG); */
  1423. outl(0, chip->port + HCFG);
  1424. /* FIXME: We need to stop and DMA transfers here.
  1425. * But as I am not sure how yet, we cannot from the dma pages.
  1426. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  1427. */
  1428. }
  1429. static int __devinit snd_ca0106_create(int dev, struct snd_card *card,
  1430. struct pci_dev *pci,
  1431. struct snd_ca0106 **rchip)
  1432. {
  1433. struct snd_ca0106 *chip;
  1434. struct snd_ca0106_details *c;
  1435. int err;
  1436. static struct snd_device_ops ops = {
  1437. .dev_free = snd_ca0106_dev_free,
  1438. };
  1439. *rchip = NULL;
  1440. err = pci_enable_device(pci);
  1441. if (err < 0)
  1442. return err;
  1443. if (pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0 ||
  1444. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32)) < 0) {
  1445. printk(KERN_ERR "error to set 32bit mask DMA\n");
  1446. pci_disable_device(pci);
  1447. return -ENXIO;
  1448. }
  1449. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1450. if (chip == NULL) {
  1451. pci_disable_device(pci);
  1452. return -ENOMEM;
  1453. }
  1454. chip->card = card;
  1455. chip->pci = pci;
  1456. chip->irq = -1;
  1457. spin_lock_init(&chip->emu_lock);
  1458. chip->port = pci_resource_start(pci, 0);
  1459. chip->res_port = request_region(chip->port, 0x20, "snd_ca0106");
  1460. if (!chip->res_port) {
  1461. snd_ca0106_free(chip);
  1462. printk(KERN_ERR "cannot allocate the port\n");
  1463. return -EBUSY;
  1464. }
  1465. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1466. IRQF_SHARED, "snd_ca0106", chip)) {
  1467. snd_ca0106_free(chip);
  1468. printk(KERN_ERR "cannot grab irq\n");
  1469. return -EBUSY;
  1470. }
  1471. chip->irq = pci->irq;
  1472. /* This stores the periods table. */
  1473. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1474. 1024, &chip->buffer) < 0) {
  1475. snd_ca0106_free(chip);
  1476. return -ENOMEM;
  1477. }
  1478. pci_set_master(pci);
  1479. /* read serial */
  1480. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1481. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1482. printk(KERN_INFO "snd-ca0106: Model %04x Rev %08x Serial %08x\n",
  1483. chip->model, pci->revision, chip->serial);
  1484. strcpy(card->driver, "CA0106");
  1485. strcpy(card->shortname, "CA0106");
  1486. for (c = ca0106_chip_details; c->serial; c++) {
  1487. if (subsystem[dev]) {
  1488. if (c->serial == subsystem[dev])
  1489. break;
  1490. } else if (c->serial == chip->serial)
  1491. break;
  1492. }
  1493. chip->details = c;
  1494. if (subsystem[dev]) {
  1495. printk(KERN_INFO "snd-ca0106: Sound card name=%s, "
  1496. "subsystem=0x%x. Forced to subsystem=0x%x\n",
  1497. c->name, chip->serial, subsystem[dev]);
  1498. }
  1499. sprintf(card->longname, "%s at 0x%lx irq %i",
  1500. c->name, chip->port, chip->irq);
  1501. ca0106_init_chip(chip, 0);
  1502. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1503. if (err < 0) {
  1504. snd_ca0106_free(chip);
  1505. return err;
  1506. }
  1507. *rchip = chip;
  1508. return 0;
  1509. }
  1510. static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
  1511. {
  1512. snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
  1513. }
  1514. static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
  1515. {
  1516. snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
  1517. }
  1518. static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
  1519. {
  1520. return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
  1521. midi->port + idx, 0);
  1522. }
  1523. static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
  1524. {
  1525. snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
  1526. }
  1527. static struct snd_card *ca0106_dev_id_card(void *dev_id)
  1528. {
  1529. return ((struct snd_ca0106 *)dev_id)->card;
  1530. }
  1531. static int ca0106_dev_id_port(void *dev_id)
  1532. {
  1533. return ((struct snd_ca0106 *)dev_id)->port;
  1534. }
  1535. static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
  1536. {
  1537. struct snd_ca_midi *midi;
  1538. char *name;
  1539. int err;
  1540. if (channel == CA0106_MIDI_CHAN_B) {
  1541. name = "CA0106 MPU-401 (UART) B";
  1542. midi = &chip->midi2;
  1543. midi->tx_enable = INTE_MIDI_TX_B;
  1544. midi->rx_enable = INTE_MIDI_RX_B;
  1545. midi->ipr_tx = IPR_MIDI_TX_B;
  1546. midi->ipr_rx = IPR_MIDI_RX_B;
  1547. midi->port = MIDI_UART_B_DATA;
  1548. } else {
  1549. name = "CA0106 MPU-401 (UART)";
  1550. midi = &chip->midi;
  1551. midi->tx_enable = INTE_MIDI_TX_A;
  1552. midi->rx_enable = INTE_MIDI_TX_B;
  1553. midi->ipr_tx = IPR_MIDI_TX_A;
  1554. midi->ipr_rx = IPR_MIDI_RX_A;
  1555. midi->port = MIDI_UART_A_DATA;
  1556. }
  1557. midi->reset = CA0106_MPU401_RESET;
  1558. midi->enter_uart = CA0106_MPU401_ENTER_UART;
  1559. midi->ack = CA0106_MPU401_ACK;
  1560. midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
  1561. midi->output_ready = CA0106_MIDI_OUTPUT_READY;
  1562. midi->channel = channel;
  1563. midi->interrupt_enable = ca0106_midi_interrupt_enable;
  1564. midi->interrupt_disable = ca0106_midi_interrupt_disable;
  1565. midi->read = ca0106_midi_read;
  1566. midi->write = ca0106_midi_write;
  1567. midi->get_dev_id_card = ca0106_dev_id_card;
  1568. midi->get_dev_id_port = ca0106_dev_id_port;
  1569. midi->dev_id = chip;
  1570. if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
  1571. return err;
  1572. return 0;
  1573. }
  1574. static int __devinit snd_ca0106_probe(struct pci_dev *pci,
  1575. const struct pci_device_id *pci_id)
  1576. {
  1577. static int dev;
  1578. struct snd_card *card;
  1579. struct snd_ca0106 *chip;
  1580. int i, err;
  1581. if (dev >= SNDRV_CARDS)
  1582. return -ENODEV;
  1583. if (!enable[dev]) {
  1584. dev++;
  1585. return -ENOENT;
  1586. }
  1587. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1588. if (err < 0)
  1589. return err;
  1590. err = snd_ca0106_create(dev, card, pci, &chip);
  1591. if (err < 0)
  1592. goto error;
  1593. card->private_data = chip;
  1594. for (i = 0; i < 4; i++) {
  1595. err = snd_ca0106_pcm(chip, i);
  1596. if (err < 0)
  1597. goto error;
  1598. }
  1599. if (chip->details->ac97 == 1) {
  1600. /* The SB0410 and SB0413 do not have an AC97 chip. */
  1601. err = snd_ca0106_ac97(chip);
  1602. if (err < 0)
  1603. goto error;
  1604. }
  1605. err = snd_ca0106_mixer(chip);
  1606. if (err < 0)
  1607. goto error;
  1608. snd_printdd("ca0106: probe for MIDI channel A ...");
  1609. err = snd_ca0106_midi(chip, CA0106_MIDI_CHAN_A);
  1610. if (err < 0)
  1611. goto error;
  1612. snd_printdd(" done.\n");
  1613. #ifdef CONFIG_PROC_FS
  1614. snd_ca0106_proc_init(chip);
  1615. #endif
  1616. snd_card_set_dev(card, &pci->dev);
  1617. err = snd_card_register(card);
  1618. if (err < 0)
  1619. goto error;
  1620. pci_set_drvdata(pci, card);
  1621. dev++;
  1622. return 0;
  1623. error:
  1624. snd_card_free(card);
  1625. return err;
  1626. }
  1627. static void __devexit snd_ca0106_remove(struct pci_dev *pci)
  1628. {
  1629. snd_card_free(pci_get_drvdata(pci));
  1630. pci_set_drvdata(pci, NULL);
  1631. }
  1632. #ifdef CONFIG_PM
  1633. static int snd_ca0106_suspend(struct pci_dev *pci, pm_message_t state)
  1634. {
  1635. struct snd_card *card = pci_get_drvdata(pci);
  1636. struct snd_ca0106 *chip = card->private_data;
  1637. int i;
  1638. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1639. for (i = 0; i < 4; i++)
  1640. snd_pcm_suspend_all(chip->pcm[i]);
  1641. if (chip->details->ac97)
  1642. snd_ac97_suspend(chip->ac97);
  1643. snd_ca0106_mixer_suspend(chip);
  1644. ca0106_stop_chip(chip);
  1645. pci_disable_device(pci);
  1646. pci_save_state(pci);
  1647. pci_set_power_state(pci, pci_choose_state(pci, state));
  1648. return 0;
  1649. }
  1650. static int snd_ca0106_resume(struct pci_dev *pci)
  1651. {
  1652. struct snd_card *card = pci_get_drvdata(pci);
  1653. struct snd_ca0106 *chip = card->private_data;
  1654. int i;
  1655. pci_set_power_state(pci, PCI_D0);
  1656. pci_restore_state(pci);
  1657. if (pci_enable_device(pci) < 0) {
  1658. snd_card_disconnect(card);
  1659. return -EIO;
  1660. }
  1661. pci_set_master(pci);
  1662. ca0106_init_chip(chip, 1);
  1663. if (chip->details->ac97)
  1664. snd_ac97_resume(chip->ac97);
  1665. snd_ca0106_mixer_resume(chip);
  1666. if (chip->details->spi_dac) {
  1667. for (i = 0; i < ARRAY_SIZE(chip->spi_dac_reg); i++)
  1668. snd_ca0106_spi_write(chip, chip->spi_dac_reg[i]);
  1669. }
  1670. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1671. return 0;
  1672. }
  1673. #endif
  1674. // PCI IDs
  1675. static DEFINE_PCI_DEVICE_TABLE(snd_ca0106_ids) = {
  1676. { PCI_VDEVICE(CREATIVE, 0x0007), 0 }, /* Audigy LS or Live 24bit */
  1677. { 0, }
  1678. };
  1679. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1680. // pci_driver definition
  1681. static struct pci_driver driver = {
  1682. .name = "CA0106",
  1683. .id_table = snd_ca0106_ids,
  1684. .probe = snd_ca0106_probe,
  1685. .remove = __devexit_p(snd_ca0106_remove),
  1686. #ifdef CONFIG_PM
  1687. .suspend = snd_ca0106_suspend,
  1688. .resume = snd_ca0106_resume,
  1689. #endif
  1690. };
  1691. // initialization of the module
  1692. static int __init alsa_card_ca0106_init(void)
  1693. {
  1694. return pci_register_driver(&driver);
  1695. }
  1696. // clean up the module
  1697. static void __exit alsa_card_ca0106_exit(void)
  1698. {
  1699. pci_unregister_driver(&driver);
  1700. }
  1701. module_init(alsa_card_ca0106_init)
  1702. module_exit(alsa_card_ca0106_exit)