18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
48c2ecf20Sopenharmony_ci *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
58c2ecf20Sopenharmony_ci *  Version: 0.0.25
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  FEATURES currently supported:
88c2ecf20Sopenharmony_ci *    Front, Rear and Center/LFE.
98c2ecf20Sopenharmony_ci *    Surround40 and Surround51.
108c2ecf20Sopenharmony_ci *    Capture from MIC an LINE IN input.
118c2ecf20Sopenharmony_ci *    SPDIF digital playback of PCM stereo and AC3/DTS works.
128c2ecf20Sopenharmony_ci *    (One can use a standard mono mini-jack to one RCA plugs cable.
138c2ecf20Sopenharmony_ci *     or one can use a standard stereo mini-jack to two RCA plugs cable.
148c2ecf20Sopenharmony_ci *     Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
158c2ecf20Sopenharmony_ci *    ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
168c2ecf20Sopenharmony_ci *    Notes on how to capture sound:
178c2ecf20Sopenharmony_ci *      The AC97 is used in the PLAYBACK direction.
188c2ecf20Sopenharmony_ci *      The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
198c2ecf20Sopenharmony_ci *      So, to record from the MIC, set the MIC Playback volume to max,
208c2ecf20Sopenharmony_ci *      unmute the MIC and turn up the MASTER Playback volume.
218c2ecf20Sopenharmony_ci *      So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci *    The only playback controls that currently do anything are: -
248c2ecf20Sopenharmony_ci *    Analog Front
258c2ecf20Sopenharmony_ci *    Analog Rear
268c2ecf20Sopenharmony_ci *    Analog Center/LFE
278c2ecf20Sopenharmony_ci *    SPDIF Front
288c2ecf20Sopenharmony_ci *    SPDIF Rear
298c2ecf20Sopenharmony_ci *    SPDIF Center/LFE
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci *    For capture from Mic in or Line in.
328c2ecf20Sopenharmony_ci *    Digital/Analog ( switch must be in Analog mode for CAPTURE. )
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci *    CAPTURE feedback into PLAYBACK
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci *  Changelog:
378c2ecf20Sopenharmony_ci *    Support interrupts per period.
388c2ecf20Sopenharmony_ci *    Removed noise from Center/LFE channel when in Analog mode.
398c2ecf20Sopenharmony_ci *    Rename and remove mixer controls.
408c2ecf20Sopenharmony_ci *  0.0.6
418c2ecf20Sopenharmony_ci *    Use separate card based DMA buffer for periods table list.
428c2ecf20Sopenharmony_ci *  0.0.7
438c2ecf20Sopenharmony_ci *    Change remove and rename ctrls into lists.
448c2ecf20Sopenharmony_ci *  0.0.8
458c2ecf20Sopenharmony_ci *    Try to fix capture sources.
468c2ecf20Sopenharmony_ci *  0.0.9
478c2ecf20Sopenharmony_ci *    Fix AC3 output.
488c2ecf20Sopenharmony_ci *    Enable S32_LE format support.
498c2ecf20Sopenharmony_ci *  0.0.10
508c2ecf20Sopenharmony_ci *    Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
518c2ecf20Sopenharmony_ci *  0.0.11
528c2ecf20Sopenharmony_ci *    Add Model name recognition.
538c2ecf20Sopenharmony_ci *  0.0.12
548c2ecf20Sopenharmony_ci *    Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
558c2ecf20Sopenharmony_ci *    Remove redundent "voice" handling.
568c2ecf20Sopenharmony_ci *  0.0.13
578c2ecf20Sopenharmony_ci *    Single trigger call for multi channels.
588c2ecf20Sopenharmony_ci *  0.0.14
598c2ecf20Sopenharmony_ci *    Set limits based on what the sound card hardware can do.
608c2ecf20Sopenharmony_ci *    playback periods_min=2, periods_max=8
618c2ecf20Sopenharmony_ci *    capture hw constraints require period_size = n * 64 bytes.
628c2ecf20Sopenharmony_ci *    playback hw constraints require period_size = n * 64 bytes.
638c2ecf20Sopenharmony_ci *  0.0.15
648c2ecf20Sopenharmony_ci *    Minor updates.
658c2ecf20Sopenharmony_ci *  0.0.16
668c2ecf20Sopenharmony_ci *    Implement 192000 sample rate.
678c2ecf20Sopenharmony_ci *  0.0.17
688c2ecf20Sopenharmony_ci *    Add support for SB0410 and SB0413.
698c2ecf20Sopenharmony_ci *  0.0.18
708c2ecf20Sopenharmony_ci *    Modified Copyright message.
718c2ecf20Sopenharmony_ci *  0.0.19
728c2ecf20Sopenharmony_ci *    Finally fix support for SB Live 24 bit. SB0410 and SB0413.
738c2ecf20Sopenharmony_ci *    The output codec needs resetting, otherwise all output is muted.
748c2ecf20Sopenharmony_ci *  0.0.20
758c2ecf20Sopenharmony_ci *    Merge "pci_disable_device(pci);" fixes.
768c2ecf20Sopenharmony_ci *  0.0.21
778c2ecf20Sopenharmony_ci *    Add 4 capture channels. (SPDIF only comes in on channel 0. )
788c2ecf20Sopenharmony_ci *    Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
798c2ecf20Sopenharmony_ci *  0.0.22
808c2ecf20Sopenharmony_ci *    Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
818c2ecf20Sopenharmony_ci *  0.0.23
828c2ecf20Sopenharmony_ci *    Implement support for Line-in capture on SB Live 24bit.
838c2ecf20Sopenharmony_ci *  0.0.24
848c2ecf20Sopenharmony_ci *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
858c2ecf20Sopenharmony_ci *  0.0.25
868c2ecf20Sopenharmony_ci *    Powerdown SPI DAC channels when not in use
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci *  BUGS:
898c2ecf20Sopenharmony_ci *    Some stability problems when unloading the snd-ca0106 kernel module.
908c2ecf20Sopenharmony_ci *    --
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci *  TODO:
938c2ecf20Sopenharmony_ci *    4 Capture channels, only one implemented so far.
948c2ecf20Sopenharmony_ci *    Other capture rates apart from 48khz not implemented.
958c2ecf20Sopenharmony_ci *    MIDI
968c2ecf20Sopenharmony_ci *    --
978c2ecf20Sopenharmony_ci *  GENERAL INFO:
988c2ecf20Sopenharmony_ci *    Model: SB0310
998c2ecf20Sopenharmony_ci *    P17 Chip: CA0106-DAT
1008c2ecf20Sopenharmony_ci *    AC97 Codec: STAC 9721
1018c2ecf20Sopenharmony_ci *    ADC: Philips 1361T (Stereo 24bit)
1028c2ecf20Sopenharmony_ci *    DAC: WM8746EDS (6-channel, 24bit, 192Khz)
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci *  GENERAL INFO:
1058c2ecf20Sopenharmony_ci *    Model: SB0410
1068c2ecf20Sopenharmony_ci *    P17 Chip: CA0106-DAT
1078c2ecf20Sopenharmony_ci *    AC97 Codec: None
1088c2ecf20Sopenharmony_ci *    ADC: WM8775EDS (4 Channel)
1098c2ecf20Sopenharmony_ci *    DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
1108c2ecf20Sopenharmony_ci *    SPDIF Out control switches between Mic in and SPDIF out.
1118c2ecf20Sopenharmony_ci *    No sound out or mic input working yet.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci *  GENERAL INFO:
1148c2ecf20Sopenharmony_ci *    Model: SB0413
1158c2ecf20Sopenharmony_ci *    P17 Chip: CA0106-DAT
1168c2ecf20Sopenharmony_ci *    AC97 Codec: None.
1178c2ecf20Sopenharmony_ci *    ADC: Unknown
1188c2ecf20Sopenharmony_ci *    DAC: Unknown
1198c2ecf20Sopenharmony_ci *    Trying to handle it like the SB0410.
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci *  This code was initially based on code from ALSA's emu10k1x.c which is:
1228c2ecf20Sopenharmony_ci *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ci#include <linux/delay.h>
1258c2ecf20Sopenharmony_ci#include <linux/init.h>
1268c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
1278c2ecf20Sopenharmony_ci#include <linux/pci.h>
1288c2ecf20Sopenharmony_ci#include <linux/slab.h>
1298c2ecf20Sopenharmony_ci#include <linux/module.h>
1308c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
1318c2ecf20Sopenharmony_ci#include <sound/core.h>
1328c2ecf20Sopenharmony_ci#include <sound/initval.h>
1338c2ecf20Sopenharmony_ci#include <sound/pcm.h>
1348c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
1358c2ecf20Sopenharmony_ci#include <sound/info.h>
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciMODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
1388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CA0106");
1398c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1408c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci// module parameters (see "Module Parameters")
1438c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
1448c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
1458c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
1468c2ecf20Sopenharmony_cistatic uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
1498c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
1508c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
1518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
1528c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
1538c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
1548c2ecf20Sopenharmony_cimodule_param_array(subsystem, uint, NULL, 0444);
1558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(subsystem, "Force card subsystem model.");
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#include "ca0106.h"
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic const struct snd_ca0106_details ca0106_chip_details[] = {
1608c2ecf20Sopenharmony_ci	 /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
1618c2ecf20Sopenharmony_ci	 /* It is really just a normal SB Live 24bit. */
1628c2ecf20Sopenharmony_ci	 /* Tested:
1638c2ecf20Sopenharmony_ci	  * See ALSA bug#3251
1648c2ecf20Sopenharmony_ci	  */
1658c2ecf20Sopenharmony_ci	 { .serial = 0x10131102,
1668c2ecf20Sopenharmony_ci	   .name   = "X-Fi Extreme Audio [SBxxxx]",
1678c2ecf20Sopenharmony_ci	   .gpio_type = 1,
1688c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
1698c2ecf20Sopenharmony_ci	 /* Sound Blaster X-Fi Extreme Audio. This does not have an AC97. 53SB079000000 */
1708c2ecf20Sopenharmony_ci	 /* It is really just a normal SB Live 24bit. */
1718c2ecf20Sopenharmony_ci	 /*
1728c2ecf20Sopenharmony_ci 	  * CTRL:CA0111-WTLF
1738c2ecf20Sopenharmony_ci	  * ADC: WM8775SEDS
1748c2ecf20Sopenharmony_ci	  * DAC: CS4382-KQZ
1758c2ecf20Sopenharmony_ci	  */
1768c2ecf20Sopenharmony_ci	 /* Tested:
1778c2ecf20Sopenharmony_ci	  * Playback on front, rear, center/lfe speakers
1788c2ecf20Sopenharmony_ci	  * Capture from Mic in.
1798c2ecf20Sopenharmony_ci	  * Not-Tested:
1808c2ecf20Sopenharmony_ci	  * Capture from Line in.
1818c2ecf20Sopenharmony_ci	  * Playback to digital out.
1828c2ecf20Sopenharmony_ci	  */
1838c2ecf20Sopenharmony_ci	 { .serial = 0x10121102,
1848c2ecf20Sopenharmony_ci	   .name   = "X-Fi Extreme Audio [SB0790]",
1858c2ecf20Sopenharmony_ci	   .gpio_type = 1,
1868c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
1878c2ecf20Sopenharmony_ci	 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97.  */
1888c2ecf20Sopenharmony_ci	 /* AudigyLS[SB0310] */
1898c2ecf20Sopenharmony_ci	 { .serial = 0x10021102,
1908c2ecf20Sopenharmony_ci	   .name   = "AudigyLS [SB0310]",
1918c2ecf20Sopenharmony_ci	   .ac97   = 1 } ,
1928c2ecf20Sopenharmony_ci	 /* Unknown AudigyLS that also says SB0310 on it */
1938c2ecf20Sopenharmony_ci	 { .serial = 0x10051102,
1948c2ecf20Sopenharmony_ci	   .name   = "AudigyLS [SB0310b]",
1958c2ecf20Sopenharmony_ci	   .ac97   = 1 } ,
1968c2ecf20Sopenharmony_ci	 /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
1978c2ecf20Sopenharmony_ci	 { .serial = 0x10061102,
1988c2ecf20Sopenharmony_ci	   .name   = "Live! 7.1 24bit [SB0410]",
1998c2ecf20Sopenharmony_ci	   .gpio_type = 1,
2008c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
2018c2ecf20Sopenharmony_ci	 /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97.  */
2028c2ecf20Sopenharmony_ci	 { .serial = 0x10071102,
2038c2ecf20Sopenharmony_ci	   .name   = "Live! 7.1 24bit [SB0413]",
2048c2ecf20Sopenharmony_ci	   .gpio_type = 1,
2058c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
2068c2ecf20Sopenharmony_ci	 /* New Audigy SE. Has a different DAC. */
2078c2ecf20Sopenharmony_ci	 /* SB0570:
2088c2ecf20Sopenharmony_ci	  * CTRL:CA0106-DAT
2098c2ecf20Sopenharmony_ci	  * ADC: WM8775EDS
2108c2ecf20Sopenharmony_ci	  * DAC: WM8768GEDS
2118c2ecf20Sopenharmony_ci	  */
2128c2ecf20Sopenharmony_ci	 { .serial = 0x100a1102,
2138c2ecf20Sopenharmony_ci	   .name   = "Audigy SE [SB0570]",
2148c2ecf20Sopenharmony_ci	   .gpio_type = 1,
2158c2ecf20Sopenharmony_ci	   .i2c_adc = 1,
2168c2ecf20Sopenharmony_ci	   .spi_dac = 0x4021 } ,
2178c2ecf20Sopenharmony_ci	 /* New Audigy LS. Has a different DAC. */
2188c2ecf20Sopenharmony_ci	 /* SB0570:
2198c2ecf20Sopenharmony_ci	  * CTRL:CA0106-DAT
2208c2ecf20Sopenharmony_ci	  * ADC: WM8775EDS
2218c2ecf20Sopenharmony_ci	  * DAC: WM8768GEDS
2228c2ecf20Sopenharmony_ci	  */
2238c2ecf20Sopenharmony_ci	 { .serial = 0x10111102,
2248c2ecf20Sopenharmony_ci	   .name   = "Audigy SE OEM [SB0570a]",
2258c2ecf20Sopenharmony_ci	   .gpio_type = 1,
2268c2ecf20Sopenharmony_ci	   .i2c_adc = 1,
2278c2ecf20Sopenharmony_ci	   .spi_dac = 0x4021 } ,
2288c2ecf20Sopenharmony_ci	/* Sound Blaster 5.1vx
2298c2ecf20Sopenharmony_ci	 * Tested: Playback on front, rear, center/lfe speakers
2308c2ecf20Sopenharmony_ci	 * Not-Tested: Capture
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	{ .serial = 0x10041102,
2338c2ecf20Sopenharmony_ci	  .name   = "Sound Blaster 5.1vx [SB1070]",
2348c2ecf20Sopenharmony_ci	  .gpio_type = 1,
2358c2ecf20Sopenharmony_ci	  .i2c_adc = 0,
2368c2ecf20Sopenharmony_ci	  .spi_dac = 0x0124
2378c2ecf20Sopenharmony_ci	 } ,
2388c2ecf20Sopenharmony_ci	 /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
2398c2ecf20Sopenharmony_ci	 /* SB0438
2408c2ecf20Sopenharmony_ci	  * CTRL:CA0106-DAT
2418c2ecf20Sopenharmony_ci	  * ADC: WM8775SEDS
2428c2ecf20Sopenharmony_ci	  * DAC: CS4382-KQZ
2438c2ecf20Sopenharmony_ci	  */
2448c2ecf20Sopenharmony_ci	 { .serial = 0x10091462,
2458c2ecf20Sopenharmony_ci	   .name   = "MSI K8N Diamond MB [SB0438]",
2468c2ecf20Sopenharmony_ci	   .gpio_type = 2,
2478c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
2488c2ecf20Sopenharmony_ci	 /* MSI K8N Diamond PLUS MB */
2498c2ecf20Sopenharmony_ci	 { .serial = 0x10091102,
2508c2ecf20Sopenharmony_ci	   .name   = "MSI K8N Diamond MB",
2518c2ecf20Sopenharmony_ci	   .gpio_type = 2,
2528c2ecf20Sopenharmony_ci	   .i2c_adc = 1,
2538c2ecf20Sopenharmony_ci	   .spi_dac = 0x4021 } ,
2548c2ecf20Sopenharmony_ci	/* Giga-byte GA-G1975X mobo
2558c2ecf20Sopenharmony_ci	 * Novell bnc#395807
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	/* FIXME: the GPIO and I2C setting aren't tested well */
2588c2ecf20Sopenharmony_ci	{ .serial = 0x1458a006,
2598c2ecf20Sopenharmony_ci	  .name = "Giga-byte GA-G1975X",
2608c2ecf20Sopenharmony_ci	  .gpio_type = 1,
2618c2ecf20Sopenharmony_ci	  .i2c_adc = 1 },
2628c2ecf20Sopenharmony_ci	 /* Shuttle XPC SD31P which has an onboard Creative Labs
2638c2ecf20Sopenharmony_ci	  * Sound Blaster Live! 24-bit EAX
2648c2ecf20Sopenharmony_ci	  * high-definition 7.1 audio processor".
2658c2ecf20Sopenharmony_ci	  * Added using info from andrewvegan in alsa bug #1298
2668c2ecf20Sopenharmony_ci	  */
2678c2ecf20Sopenharmony_ci	 { .serial = 0x30381297,
2688c2ecf20Sopenharmony_ci	   .name   = "Shuttle XPC SD31P [SD31P]",
2698c2ecf20Sopenharmony_ci	   .gpio_type = 1,
2708c2ecf20Sopenharmony_ci	   .i2c_adc = 1 } ,
2718c2ecf20Sopenharmony_ci	/* Shuttle XPC SD11G5 which has an onboard Creative Labs
2728c2ecf20Sopenharmony_ci	 * Sound Blaster Live! 24-bit EAX
2738c2ecf20Sopenharmony_ci	 * high-definition 7.1 audio processor".
2748c2ecf20Sopenharmony_ci	 * Fixes ALSA bug#1600
2758c2ecf20Sopenharmony_ci         */
2768c2ecf20Sopenharmony_ci	{ .serial = 0x30411297,
2778c2ecf20Sopenharmony_ci	  .name = "Shuttle XPC SD11G5 [SD11G5]",
2788c2ecf20Sopenharmony_ci	  .gpio_type = 1,
2798c2ecf20Sopenharmony_ci	  .i2c_adc = 1 } ,
2808c2ecf20Sopenharmony_ci	 { .serial = 0,
2818c2ecf20Sopenharmony_ci	   .name   = "AudigyLS [Unknown]" }
2828c2ecf20Sopenharmony_ci};
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* hardware definition */
2858c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ca0106_playback_hw = {
2868c2ecf20Sopenharmony_ci	.info =			SNDRV_PCM_INFO_MMAP |
2878c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_INTERLEAVED |
2888c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_BLOCK_TRANSFER |
2898c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_MMAP_VALID |
2908c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_SYNC_START,
2918c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
2928c2ecf20Sopenharmony_ci	.rates =		(SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
2938c2ecf20Sopenharmony_ci				 SNDRV_PCM_RATE_192000),
2948c2ecf20Sopenharmony_ci	.rate_min =		48000,
2958c2ecf20Sopenharmony_ci	.rate_max =		192000,
2968c2ecf20Sopenharmony_ci	.channels_min =		2,  //1,
2978c2ecf20Sopenharmony_ci	.channels_max =		2,  //6,
2988c2ecf20Sopenharmony_ci	.buffer_bytes_max =	((65536 - 64) * 8),
2998c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3008c2ecf20Sopenharmony_ci	.period_bytes_max =	(65536 - 64),
3018c2ecf20Sopenharmony_ci	.periods_min =		2,
3028c2ecf20Sopenharmony_ci	.periods_max =		8,
3038c2ecf20Sopenharmony_ci	.fifo_size =		0,
3048c2ecf20Sopenharmony_ci};
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ca0106_capture_hw = {
3078c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP |
3088c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_INTERLEAVED |
3098c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
3108c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID),
3118c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
3128c2ecf20Sopenharmony_ci#if 0 /* FIXME: looks like 44.1kHz capture causes noisy output on 48kHz */
3138c2ecf20Sopenharmony_ci	.rates =		(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
3148c2ecf20Sopenharmony_ci				 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
3158c2ecf20Sopenharmony_ci	.rate_min =		44100,
3168c2ecf20Sopenharmony_ci#else
3178c2ecf20Sopenharmony_ci	.rates =		(SNDRV_PCM_RATE_48000 |
3188c2ecf20Sopenharmony_ci				 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
3198c2ecf20Sopenharmony_ci	.rate_min =		48000,
3208c2ecf20Sopenharmony_ci#endif /* FIXME */
3218c2ecf20Sopenharmony_ci	.rate_max =		192000,
3228c2ecf20Sopenharmony_ci	.channels_min =		2,
3238c2ecf20Sopenharmony_ci	.channels_max =		2,
3248c2ecf20Sopenharmony_ci	.buffer_bytes_max =	65536 - 128,
3258c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3268c2ecf20Sopenharmony_ci	.period_bytes_max =	32768 - 64,
3278c2ecf20Sopenharmony_ci	.periods_min =		2,
3288c2ecf20Sopenharmony_ci	.periods_max =		2,
3298c2ecf20Sopenharmony_ci	.fifo_size =		0,
3308c2ecf20Sopenharmony_ci};
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ciunsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
3338c2ecf20Sopenharmony_ci					  unsigned int reg,
3348c2ecf20Sopenharmony_ci					  unsigned int chn)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	unsigned long flags;
3378c2ecf20Sopenharmony_ci	unsigned int regptr, val;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	regptr = (reg << 16) | chn;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
3428c2ecf20Sopenharmony_ci	outl(regptr, emu->port + PTR);
3438c2ecf20Sopenharmony_ci	val = inl(emu->port + DATA);
3448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
3458c2ecf20Sopenharmony_ci	return val;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_civoid snd_ca0106_ptr_write(struct snd_ca0106 *emu,
3498c2ecf20Sopenharmony_ci				   unsigned int reg,
3508c2ecf20Sopenharmony_ci				   unsigned int chn,
3518c2ecf20Sopenharmony_ci				   unsigned int data)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	unsigned int regptr;
3548c2ecf20Sopenharmony_ci	unsigned long flags;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	regptr = (reg << 16) | chn;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
3598c2ecf20Sopenharmony_ci	outl(regptr, emu->port + PTR);
3608c2ecf20Sopenharmony_ci	outl(data, emu->port + DATA);
3618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ciint snd_ca0106_spi_write(struct snd_ca0106 * emu,
3658c2ecf20Sopenharmony_ci				   unsigned int data)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	unsigned int reset, set;
3688c2ecf20Sopenharmony_ci	unsigned int reg, tmp;
3698c2ecf20Sopenharmony_ci	int n, result;
3708c2ecf20Sopenharmony_ci	reg = SPI;
3718c2ecf20Sopenharmony_ci	if (data > 0xffff) /* Only 16bit values allowed */
3728c2ecf20Sopenharmony_ci		return 1;
3738c2ecf20Sopenharmony_ci	tmp = snd_ca0106_ptr_read(emu, reg, 0);
3748c2ecf20Sopenharmony_ci	reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
3758c2ecf20Sopenharmony_ci	set = reset | 0x10000; /* Set xxx1xxxx */
3768c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, reg, 0, reset | data);
3778c2ecf20Sopenharmony_ci	tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
3788c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, reg, 0, set | data);
3798c2ecf20Sopenharmony_ci	result = 1;
3808c2ecf20Sopenharmony_ci	/* Wait for status bit to return to 0 */
3818c2ecf20Sopenharmony_ci	for (n = 0; n < 100; n++) {
3828c2ecf20Sopenharmony_ci		udelay(10);
3838c2ecf20Sopenharmony_ci		tmp = snd_ca0106_ptr_read(emu, reg, 0);
3848c2ecf20Sopenharmony_ci		if (!(tmp & 0x10000)) {
3858c2ecf20Sopenharmony_ci			result = 0;
3868c2ecf20Sopenharmony_ci			break;
3878c2ecf20Sopenharmony_ci		}
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci	if (result) /* Timed out */
3908c2ecf20Sopenharmony_ci		return 1;
3918c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, reg, 0, reset | data);
3928c2ecf20Sopenharmony_ci	tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
3938c2ecf20Sopenharmony_ci	return 0;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/* The ADC does not support i2c read, so only write is implemented */
3978c2ecf20Sopenharmony_ciint snd_ca0106_i2c_write(struct snd_ca0106 *emu,
3988c2ecf20Sopenharmony_ci				u32 reg,
3998c2ecf20Sopenharmony_ci				u32 value)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	u32 tmp;
4028c2ecf20Sopenharmony_ci	int timeout = 0;
4038c2ecf20Sopenharmony_ci	int status;
4048c2ecf20Sopenharmony_ci	int retry;
4058c2ecf20Sopenharmony_ci	if ((reg > 0x7f) || (value > 0x1ff)) {
4068c2ecf20Sopenharmony_ci		dev_err(emu->card->dev, "i2c_write: invalid values.\n");
4078c2ecf20Sopenharmony_ci		return -EINVAL;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	tmp = reg << 25 | value << 16;
4118c2ecf20Sopenharmony_ci	/*
4128c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev, "I2C-write:reg=0x%x, value=0x%x\n", reg, value);
4138c2ecf20Sopenharmony_ci	*/
4148c2ecf20Sopenharmony_ci	/* Not sure what this I2C channel controls. */
4158c2ecf20Sopenharmony_ci	/* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* This controls the I2C connected to the WM8775 ADC Codec */
4188c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	for (retry = 0; retry < 10; retry++) {
4218c2ecf20Sopenharmony_ci		/* Send the data to i2c */
4228c2ecf20Sopenharmony_ci		//tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
4238c2ecf20Sopenharmony_ci		//tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
4248c2ecf20Sopenharmony_ci		tmp = 0;
4258c2ecf20Sopenharmony_ci		tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
4268c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		/* Wait till the transaction ends */
4298c2ecf20Sopenharmony_ci		while (1) {
4308c2ecf20Sopenharmony_ci			status = snd_ca0106_ptr_read(emu, I2C_A, 0);
4318c2ecf20Sopenharmony_ci			/*dev_dbg(emu->card->dev, "I2C:status=0x%x\n", status);*/
4328c2ecf20Sopenharmony_ci			timeout++;
4338c2ecf20Sopenharmony_ci			if ((status & I2C_A_ADC_START) == 0)
4348c2ecf20Sopenharmony_ci				break;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci			if (timeout > 1000)
4378c2ecf20Sopenharmony_ci				break;
4388c2ecf20Sopenharmony_ci		}
4398c2ecf20Sopenharmony_ci		//Read back and see if the transaction is successful
4408c2ecf20Sopenharmony_ci		if ((status & I2C_A_ADC_ABORT) == 0)
4418c2ecf20Sopenharmony_ci			break;
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if (retry == 10) {
4458c2ecf20Sopenharmony_ci		dev_err(emu->card->dev, "Writing to ADC failed!\n");
4468c2ecf20Sopenharmony_ci		return -EINVAL;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci    	return 0;
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	unsigned long flags;
4568c2ecf20Sopenharmony_ci	unsigned int intr_enable;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
4598c2ecf20Sopenharmony_ci	intr_enable = inl(emu->port + INTE) | intrenb;
4608c2ecf20Sopenharmony_ci	outl(intr_enable, emu->port + INTE);
4618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	unsigned long flags;
4678c2ecf20Sopenharmony_ci	unsigned int intr_enable;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
4708c2ecf20Sopenharmony_ci	intr_enable = inl(emu->port + INTE) & ~intrenb;
4718c2ecf20Sopenharmony_ci	outl(intr_enable, emu->port + INTE);
4728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	kfree(runtime->private_data);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic const int spi_dacd_reg[] = {
4828c2ecf20Sopenharmony_ci	SPI_DACD0_REG,
4838c2ecf20Sopenharmony_ci	SPI_DACD1_REG,
4848c2ecf20Sopenharmony_ci	SPI_DACD2_REG,
4858c2ecf20Sopenharmony_ci	0,
4868c2ecf20Sopenharmony_ci	SPI_DACD4_REG,
4878c2ecf20Sopenharmony_ci};
4888c2ecf20Sopenharmony_cistatic const int spi_dacd_bit[] = {
4898c2ecf20Sopenharmony_ci	SPI_DACD0_BIT,
4908c2ecf20Sopenharmony_ci	SPI_DACD1_BIT,
4918c2ecf20Sopenharmony_ci	SPI_DACD2_BIT,
4928c2ecf20Sopenharmony_ci	0,
4938c2ecf20Sopenharmony_ci	SPI_DACD4_BIT,
4948c2ecf20Sopenharmony_ci};
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic void restore_spdif_bits(struct snd_ca0106 *chip, int idx)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	if (chip->spdif_str_bits[idx] != chip->spdif_bits[idx]) {
4998c2ecf20Sopenharmony_ci		chip->spdif_str_bits[idx] = chip->spdif_bits[idx];
5008c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, SPCS0 + idx, 0,
5018c2ecf20Sopenharmony_ci				     chip->spdif_str_bits[idx]);
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic int snd_ca0106_channel_dac(struct snd_ca0106 *chip,
5068c2ecf20Sopenharmony_ci				  const struct snd_ca0106_details *details,
5078c2ecf20Sopenharmony_ci				  int channel_id)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	switch (channel_id) {
5108c2ecf20Sopenharmony_ci	case PCM_FRONT_CHANNEL:
5118c2ecf20Sopenharmony_ci		return (details->spi_dac & 0xf000) >> (4 * 3);
5128c2ecf20Sopenharmony_ci	case PCM_REAR_CHANNEL:
5138c2ecf20Sopenharmony_ci		return (details->spi_dac & 0x0f00) >> (4 * 2);
5148c2ecf20Sopenharmony_ci	case PCM_CENTER_LFE_CHANNEL:
5158c2ecf20Sopenharmony_ci		return (details->spi_dac & 0x00f0) >> (4 * 1);
5168c2ecf20Sopenharmony_ci	case PCM_UNKNOWN_CHANNEL:
5178c2ecf20Sopenharmony_ci		return (details->spi_dac & 0x000f) >> (4 * 0);
5188c2ecf20Sopenharmony_ci	default:
5198c2ecf20Sopenharmony_ci		dev_dbg(chip->card->dev, "ca0106: unknown channel_id %d\n",
5208c2ecf20Sopenharmony_ci			   channel_id);
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci	return 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_power_dac(struct snd_ca0106 *chip, int channel_id,
5268c2ecf20Sopenharmony_ci				    int power)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	if (chip->details->spi_dac) {
5298c2ecf20Sopenharmony_ci		const int dac = snd_ca0106_channel_dac(chip, chip->details,
5308c2ecf20Sopenharmony_ci						       channel_id);
5318c2ecf20Sopenharmony_ci		const int reg = spi_dacd_reg[dac];
5328c2ecf20Sopenharmony_ci		const int bit = spi_dacd_bit[dac];
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		if (power)
5358c2ecf20Sopenharmony_ci			/* Power up */
5368c2ecf20Sopenharmony_ci			chip->spi_dac_reg[reg] &= ~bit;
5378c2ecf20Sopenharmony_ci		else
5388c2ecf20Sopenharmony_ci			/* Power down */
5398c2ecf20Sopenharmony_ci			chip->spi_dac_reg[reg] |= bit;
5408c2ecf20Sopenharmony_ci		if (snd_ca0106_spi_write(chip, chip->spi_dac_reg[reg]) != 0)
5418c2ecf20Sopenharmony_ci			return -ENXIO;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	return 0;
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci/* open_playback callback */
5478c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
5488c2ecf20Sopenharmony_ci						int channel_id)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
5518c2ecf20Sopenharmony_ci        struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
5528c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm;
5538c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
5548c2ecf20Sopenharmony_ci	int err;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (epcm == NULL)
5598c2ecf20Sopenharmony_ci		return -ENOMEM;
5608c2ecf20Sopenharmony_ci	epcm->emu = chip;
5618c2ecf20Sopenharmony_ci	epcm->substream = substream;
5628c2ecf20Sopenharmony_ci        epcm->channel_id=channel_id;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	runtime->private_data = epcm;
5658c2ecf20Sopenharmony_ci	runtime->private_free = snd_ca0106_pcm_free_substream;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	runtime->hw = snd_ca0106_playback_hw;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci        channel->emu = chip;
5708c2ecf20Sopenharmony_ci        channel->number = channel_id;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	channel->use = 1;
5738c2ecf20Sopenharmony_ci	/*
5748c2ecf20Sopenharmony_ci	dev_dbg(chip->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
5758c2ecf20Sopenharmony_ci	       channel_id, chip, channel);
5768c2ecf20Sopenharmony_ci	*/
5778c2ecf20Sopenharmony_ci        //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
5788c2ecf20Sopenharmony_ci	channel->epcm = epcm;
5798c2ecf20Sopenharmony_ci	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
5808c2ecf20Sopenharmony_ci                return err;
5818c2ecf20Sopenharmony_ci	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
5828c2ecf20Sopenharmony_ci                return err;
5838c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* Front channel dac should already be on */
5868c2ecf20Sopenharmony_ci	if (channel_id != PCM_FRONT_CHANNEL) {
5878c2ecf20Sopenharmony_ci		err = snd_ca0106_pcm_power_dac(chip, channel_id, 1);
5888c2ecf20Sopenharmony_ci		if (err < 0)
5898c2ecf20Sopenharmony_ci			return err;
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	restore_spdif_bits(chip, channel_id);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	return 0;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci/* close callback */
5988c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
5998c2ecf20Sopenharmony_ci{
6008c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
6018c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
6028c2ecf20Sopenharmony_ci        struct snd_ca0106_pcm *epcm = runtime->private_data;
6038c2ecf20Sopenharmony_ci	chip->playback_channels[epcm->channel_id].use = 0;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	restore_spdif_bits(chip, epcm->channel_id);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Front channel dac should stay on */
6088c2ecf20Sopenharmony_ci	if (epcm->channel_id != PCM_FRONT_CHANNEL) {
6098c2ecf20Sopenharmony_ci		int err;
6108c2ecf20Sopenharmony_ci		err = snd_ca0106_pcm_power_dac(chip, epcm->channel_id, 0);
6118c2ecf20Sopenharmony_ci		if (err < 0)
6128c2ecf20Sopenharmony_ci			return err;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	/* FIXME: maybe zero others */
6168c2ecf20Sopenharmony_ci	return 0;
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
6308c2ecf20Sopenharmony_ci{
6318c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci/* open_capture callback */
6408c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
6418c2ecf20Sopenharmony_ci					       int channel_id)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
6448c2ecf20Sopenharmony_ci        struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
6458c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm;
6468c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
6478c2ecf20Sopenharmony_ci	int err;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
6508c2ecf20Sopenharmony_ci	if (!epcm)
6518c2ecf20Sopenharmony_ci		return -ENOMEM;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	epcm->emu = chip;
6548c2ecf20Sopenharmony_ci	epcm->substream = substream;
6558c2ecf20Sopenharmony_ci        epcm->channel_id=channel_id;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	runtime->private_data = epcm;
6588c2ecf20Sopenharmony_ci	runtime->private_free = snd_ca0106_pcm_free_substream;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	runtime->hw = snd_ca0106_capture_hw;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci        channel->emu = chip;
6638c2ecf20Sopenharmony_ci        channel->number = channel_id;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	channel->use = 1;
6668c2ecf20Sopenharmony_ci	/*
6678c2ecf20Sopenharmony_ci	dev_dbg(chip->card->dev, "open:channel_id=%d, chip=%p, channel=%p\n",
6688c2ecf20Sopenharmony_ci	       channel_id, chip, channel);
6698c2ecf20Sopenharmony_ci	*/
6708c2ecf20Sopenharmony_ci        //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
6718c2ecf20Sopenharmony_ci        channel->epcm = epcm;
6728c2ecf20Sopenharmony_ci	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6738c2ecf20Sopenharmony_ci                return err;
6748c2ecf20Sopenharmony_ci	//snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
6758c2ecf20Sopenharmony_ci	if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
6768c2ecf20Sopenharmony_ci                return err;
6778c2ecf20Sopenharmony_ci	return 0;
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci/* close callback */
6818c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
6848c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
6858c2ecf20Sopenharmony_ci        struct snd_ca0106_pcm *epcm = runtime->private_data;
6868c2ecf20Sopenharmony_ci	chip->capture_channels[epcm->channel_id].use = 0;
6878c2ecf20Sopenharmony_ci	/* FIXME: maybe zero others */
6888c2ecf20Sopenharmony_ci	return 0;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_capture_channel(substream, 0);
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_capture_channel(substream, 1);
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
7028c2ecf20Sopenharmony_ci{
7038c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_capture_channel(substream, 2);
7048c2ecf20Sopenharmony_ci}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	return snd_ca0106_pcm_open_capture_channel(substream, 3);
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci/* prepare playback callback */
7128c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
7138c2ecf20Sopenharmony_ci{
7148c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
7158c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
7168c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm = runtime->private_data;
7178c2ecf20Sopenharmony_ci	int channel = epcm->channel_id;
7188c2ecf20Sopenharmony_ci	u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
7198c2ecf20Sopenharmony_ci	u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
7208c2ecf20Sopenharmony_ci	u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
7218c2ecf20Sopenharmony_ci	u32 hcfg_set = 0x00000000;
7228c2ecf20Sopenharmony_ci	u32 hcfg;
7238c2ecf20Sopenharmony_ci	u32 reg40_mask = 0x30000 << (channel<<1);
7248c2ecf20Sopenharmony_ci	u32 reg40_set = 0;
7258c2ecf20Sopenharmony_ci	u32 reg40;
7268c2ecf20Sopenharmony_ci	/* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
7278c2ecf20Sopenharmony_ci	u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
7288c2ecf20Sopenharmony_ci	u32 reg71_set = 0;
7298c2ecf20Sopenharmony_ci	u32 reg71;
7308c2ecf20Sopenharmony_ci	int i;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci#if 0 /* debug */
7338c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
7348c2ecf20Sopenharmony_ci		   "prepare:channel_number=%d, rate=%d, format=0x%x, "
7358c2ecf20Sopenharmony_ci		   "channels=%d, buffer_size=%ld, period_size=%ld, "
7368c2ecf20Sopenharmony_ci		   "periods=%u, frames_to_bytes=%d\n",
7378c2ecf20Sopenharmony_ci		   channel, runtime->rate, runtime->format,
7388c2ecf20Sopenharmony_ci		   runtime->channels, runtime->buffer_size,
7398c2ecf20Sopenharmony_ci		   runtime->period_size, runtime->periods,
7408c2ecf20Sopenharmony_ci		   frames_to_bytes(runtime, 1));
7418c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
7428c2ecf20Sopenharmony_ci		"dma_addr=%x, dma_area=%p, table_base=%p\n",
7438c2ecf20Sopenharmony_ci		   runtime->dma_addr, runtime->dma_area, table_base);
7448c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
7458c2ecf20Sopenharmony_ci		"dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
7468c2ecf20Sopenharmony_ci		   emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
7478c2ecf20Sopenharmony_ci#endif /* debug */
7488c2ecf20Sopenharmony_ci	/* Rate can be set per channel. */
7498c2ecf20Sopenharmony_ci	/* reg40 control host to fifo */
7508c2ecf20Sopenharmony_ci	/* reg71 controls DAC rate. */
7518c2ecf20Sopenharmony_ci	switch (runtime->rate) {
7528c2ecf20Sopenharmony_ci	case 44100:
7538c2ecf20Sopenharmony_ci		reg40_set = 0x10000 << (channel<<1);
7548c2ecf20Sopenharmony_ci		reg71_set = 0x01010000;
7558c2ecf20Sopenharmony_ci		break;
7568c2ecf20Sopenharmony_ci        case 48000:
7578c2ecf20Sopenharmony_ci		reg40_set = 0;
7588c2ecf20Sopenharmony_ci		reg71_set = 0;
7598c2ecf20Sopenharmony_ci		break;
7608c2ecf20Sopenharmony_ci	case 96000:
7618c2ecf20Sopenharmony_ci		reg40_set = 0x20000 << (channel<<1);
7628c2ecf20Sopenharmony_ci		reg71_set = 0x02020000;
7638c2ecf20Sopenharmony_ci		break;
7648c2ecf20Sopenharmony_ci	case 192000:
7658c2ecf20Sopenharmony_ci		reg40_set = 0x30000 << (channel<<1);
7668c2ecf20Sopenharmony_ci		reg71_set = 0x03030000;
7678c2ecf20Sopenharmony_ci		break;
7688c2ecf20Sopenharmony_ci	default:
7698c2ecf20Sopenharmony_ci		reg40_set = 0;
7708c2ecf20Sopenharmony_ci		reg71_set = 0;
7718c2ecf20Sopenharmony_ci		break;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci	/* Format is a global setting */
7748c2ecf20Sopenharmony_ci	/* FIXME: Only let the first channel accessed set this. */
7758c2ecf20Sopenharmony_ci	switch (runtime->format) {
7768c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_LE:
7778c2ecf20Sopenharmony_ci		hcfg_set = 0;
7788c2ecf20Sopenharmony_ci		break;
7798c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S32_LE:
7808c2ecf20Sopenharmony_ci		hcfg_set = HCFG_PLAYBACK_S32_LE;
7818c2ecf20Sopenharmony_ci		break;
7828c2ecf20Sopenharmony_ci	default:
7838c2ecf20Sopenharmony_ci		hcfg_set = 0;
7848c2ecf20Sopenharmony_ci		break;
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci	hcfg = inl(emu->port + HCFG) ;
7878c2ecf20Sopenharmony_ci	hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
7888c2ecf20Sopenharmony_ci	outl(hcfg, emu->port + HCFG);
7898c2ecf20Sopenharmony_ci	reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
7908c2ecf20Sopenharmony_ci	reg40 = (reg40 & ~reg40_mask) | reg40_set;
7918c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
7928c2ecf20Sopenharmony_ci	reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
7938c2ecf20Sopenharmony_ci	reg71 = (reg71 & ~reg71_mask) | reg71_set;
7948c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* FIXME: Check emu->buffer.size before actually writing to it. */
7978c2ecf20Sopenharmony_ci        for(i=0; i < runtime->periods; i++) {
7988c2ecf20Sopenharmony_ci		table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
7998c2ecf20Sopenharmony_ci		table_base[i*2+1] = period_size_bytes << 16;
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
8038c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
8048c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
8058c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
8068c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
8078c2ecf20Sopenharmony_ci	/* FIXME  test what 0 bytes does. */
8088c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
8098c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
8108c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
8118c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x08, channel, 0);
8128c2ecf20Sopenharmony_ci        snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
8138c2ecf20Sopenharmony_ci#if 0
8148c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, SPCS0, 0,
8158c2ecf20Sopenharmony_ci			       SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
8168c2ecf20Sopenharmony_ci			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
8178c2ecf20Sopenharmony_ci			       SPCS_GENERATIONSTATUS | 0x00001200 |
8188c2ecf20Sopenharmony_ci			       0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
8198c2ecf20Sopenharmony_ci#endif
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	return 0;
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci/* prepare capture callback */
8258c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
8288c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
8298c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm = runtime->private_data;
8308c2ecf20Sopenharmony_ci	int channel = epcm->channel_id;
8318c2ecf20Sopenharmony_ci	u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
8328c2ecf20Sopenharmony_ci	u32 hcfg_set = 0x00000000;
8338c2ecf20Sopenharmony_ci	u32 hcfg;
8348c2ecf20Sopenharmony_ci	u32 over_sampling=0x2;
8358c2ecf20Sopenharmony_ci	u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
8368c2ecf20Sopenharmony_ci	u32 reg71_set = 0;
8378c2ecf20Sopenharmony_ci	u32 reg71;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci#if 0 /* debug */
8408c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
8418c2ecf20Sopenharmony_ci		   "prepare:channel_number=%d, rate=%d, format=0x%x, "
8428c2ecf20Sopenharmony_ci		   "channels=%d, buffer_size=%ld, period_size=%ld, "
8438c2ecf20Sopenharmony_ci		   "periods=%u, frames_to_bytes=%d\n",
8448c2ecf20Sopenharmony_ci		   channel, runtime->rate, runtime->format,
8458c2ecf20Sopenharmony_ci		   runtime->channels, runtime->buffer_size,
8468c2ecf20Sopenharmony_ci		   runtime->period_size, runtime->periods,
8478c2ecf20Sopenharmony_ci		   frames_to_bytes(runtime, 1));
8488c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
8498c2ecf20Sopenharmony_ci		"dma_addr=%x, dma_area=%p, table_base=%p\n",
8508c2ecf20Sopenharmony_ci		   runtime->dma_addr, runtime->dma_area, table_base);
8518c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
8528c2ecf20Sopenharmony_ci		"dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",
8538c2ecf20Sopenharmony_ci		   emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
8548c2ecf20Sopenharmony_ci#endif /* debug */
8558c2ecf20Sopenharmony_ci	/* reg71 controls ADC rate. */
8568c2ecf20Sopenharmony_ci	switch (runtime->rate) {
8578c2ecf20Sopenharmony_ci	case 44100:
8588c2ecf20Sopenharmony_ci		reg71_set = 0x00004000;
8598c2ecf20Sopenharmony_ci		break;
8608c2ecf20Sopenharmony_ci        case 48000:
8618c2ecf20Sopenharmony_ci		reg71_set = 0;
8628c2ecf20Sopenharmony_ci		break;
8638c2ecf20Sopenharmony_ci	case 96000:
8648c2ecf20Sopenharmony_ci		reg71_set = 0x00008000;
8658c2ecf20Sopenharmony_ci		over_sampling=0xa;
8668c2ecf20Sopenharmony_ci		break;
8678c2ecf20Sopenharmony_ci	case 192000:
8688c2ecf20Sopenharmony_ci		reg71_set = 0x0000c000;
8698c2ecf20Sopenharmony_ci		over_sampling=0xa;
8708c2ecf20Sopenharmony_ci		break;
8718c2ecf20Sopenharmony_ci	default:
8728c2ecf20Sopenharmony_ci		reg71_set = 0;
8738c2ecf20Sopenharmony_ci		break;
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci	/* Format is a global setting */
8768c2ecf20Sopenharmony_ci	/* FIXME: Only let the first channel accessed set this. */
8778c2ecf20Sopenharmony_ci	switch (runtime->format) {
8788c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_LE:
8798c2ecf20Sopenharmony_ci		hcfg_set = 0;
8808c2ecf20Sopenharmony_ci		break;
8818c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S32_LE:
8828c2ecf20Sopenharmony_ci		hcfg_set = HCFG_CAPTURE_S32_LE;
8838c2ecf20Sopenharmony_ci		break;
8848c2ecf20Sopenharmony_ci	default:
8858c2ecf20Sopenharmony_ci		hcfg_set = 0;
8868c2ecf20Sopenharmony_ci		break;
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci	hcfg = inl(emu->port + HCFG) ;
8898c2ecf20Sopenharmony_ci	hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
8908c2ecf20Sopenharmony_ci	outl(hcfg, emu->port + HCFG);
8918c2ecf20Sopenharmony_ci	reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
8928c2ecf20Sopenharmony_ci	reg71 = (reg71 & ~reg71_mask) | reg71_set;
8938c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
8948c2ecf20Sopenharmony_ci        if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
8958c2ecf20Sopenharmony_ci	        snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/*
9008c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev,
9018c2ecf20Sopenharmony_ci	       "prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, "
9028c2ecf20Sopenharmony_ci	       "buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",
9038c2ecf20Sopenharmony_ci	       channel, runtime->rate, runtime->format, runtime->channels,
9048c2ecf20Sopenharmony_ci	       runtime->buffer_size, runtime->period_size,
9058c2ecf20Sopenharmony_ci	       frames_to_bytes(runtime, 1));
9068c2ecf20Sopenharmony_ci	*/
9078c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, 0x13, channel, 0);
9088c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
9098c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
9108c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	return 0;
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci/* trigger_playback callback */
9168c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
9178c2ecf20Sopenharmony_ci				    int cmd)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
9208c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime;
9218c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm;
9228c2ecf20Sopenharmony_ci	int channel;
9238c2ecf20Sopenharmony_ci	int result = 0;
9248c2ecf20Sopenharmony_ci        struct snd_pcm_substream *s;
9258c2ecf20Sopenharmony_ci	u32 basic = 0;
9268c2ecf20Sopenharmony_ci	u32 extended = 0;
9278c2ecf20Sopenharmony_ci	u32 bits;
9288c2ecf20Sopenharmony_ci	int running = 0;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	switch (cmd) {
9318c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
9328c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
9338c2ecf20Sopenharmony_ci		running = 1;
9348c2ecf20Sopenharmony_ci		break;
9358c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
9368c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
9378c2ecf20Sopenharmony_ci	default:
9388c2ecf20Sopenharmony_ci		running = 0;
9398c2ecf20Sopenharmony_ci		break;
9408c2ecf20Sopenharmony_ci	}
9418c2ecf20Sopenharmony_ci        snd_pcm_group_for_each_entry(s, substream) {
9428c2ecf20Sopenharmony_ci		if (snd_pcm_substream_chip(s) != emu ||
9438c2ecf20Sopenharmony_ci		    s->stream != SNDRV_PCM_STREAM_PLAYBACK)
9448c2ecf20Sopenharmony_ci			continue;
9458c2ecf20Sopenharmony_ci		runtime = s->runtime;
9468c2ecf20Sopenharmony_ci		epcm = runtime->private_data;
9478c2ecf20Sopenharmony_ci		channel = epcm->channel_id;
9488c2ecf20Sopenharmony_ci		/* dev_dbg(emu->card->dev, "channel=%d\n", channel); */
9498c2ecf20Sopenharmony_ci		epcm->running = running;
9508c2ecf20Sopenharmony_ci		basic |= (0x1 << channel);
9518c2ecf20Sopenharmony_ci		extended |= (0x10 << channel);
9528c2ecf20Sopenharmony_ci                snd_pcm_trigger_done(s, substream);
9538c2ecf20Sopenharmony_ci        }
9548c2ecf20Sopenharmony_ci	/* dev_dbg(emu->card->dev, "basic=0x%x, extended=0x%x\n",basic, extended); */
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	switch (cmd) {
9578c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
9588c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
9598c2ecf20Sopenharmony_ci		bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
9608c2ecf20Sopenharmony_ci		bits |= extended;
9618c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
9628c2ecf20Sopenharmony_ci		bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
9638c2ecf20Sopenharmony_ci		bits |= basic;
9648c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
9658c2ecf20Sopenharmony_ci		break;
9668c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
9678c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
9688c2ecf20Sopenharmony_ci		bits = snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0);
9698c2ecf20Sopenharmony_ci		bits &= ~basic;
9708c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, bits);
9718c2ecf20Sopenharmony_ci		bits = snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0);
9728c2ecf20Sopenharmony_ci		bits &= ~extended;
9738c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, bits);
9748c2ecf20Sopenharmony_ci		break;
9758c2ecf20Sopenharmony_ci	default:
9768c2ecf20Sopenharmony_ci		result = -EINVAL;
9778c2ecf20Sopenharmony_ci		break;
9788c2ecf20Sopenharmony_ci	}
9798c2ecf20Sopenharmony_ci	return result;
9808c2ecf20Sopenharmony_ci}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci/* trigger_capture callback */
9838c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
9848c2ecf20Sopenharmony_ci				    int cmd)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
9878c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
9888c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm = runtime->private_data;
9898c2ecf20Sopenharmony_ci	int channel = epcm->channel_id;
9908c2ecf20Sopenharmony_ci	int result = 0;
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	switch (cmd) {
9938c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
9948c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
9958c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
9968c2ecf20Sopenharmony_ci		epcm->running = 1;
9978c2ecf20Sopenharmony_ci		break;
9988c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
9998c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
10008c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
10018c2ecf20Sopenharmony_ci		epcm->running = 0;
10028c2ecf20Sopenharmony_ci		break;
10038c2ecf20Sopenharmony_ci	default:
10048c2ecf20Sopenharmony_ci		result = -EINVAL;
10058c2ecf20Sopenharmony_ci		break;
10068c2ecf20Sopenharmony_ci	}
10078c2ecf20Sopenharmony_ci	return result;
10088c2ecf20Sopenharmony_ci}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci/* pointer_playback callback */
10118c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t
10128c2ecf20Sopenharmony_cisnd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
10158c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
10168c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm = runtime->private_data;
10178c2ecf20Sopenharmony_ci	unsigned int ptr, prev_ptr;
10188c2ecf20Sopenharmony_ci	int channel = epcm->channel_id;
10198c2ecf20Sopenharmony_ci	int timeout = 10;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (!epcm->running)
10228c2ecf20Sopenharmony_ci		return 0;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	prev_ptr = -1;
10258c2ecf20Sopenharmony_ci	do {
10268c2ecf20Sopenharmony_ci		ptr = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
10278c2ecf20Sopenharmony_ci		ptr = (ptr >> 3) * runtime->period_size;
10288c2ecf20Sopenharmony_ci		ptr += bytes_to_frames(runtime,
10298c2ecf20Sopenharmony_ci			snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel));
10308c2ecf20Sopenharmony_ci		if (ptr >= runtime->buffer_size)
10318c2ecf20Sopenharmony_ci			ptr -= runtime->buffer_size;
10328c2ecf20Sopenharmony_ci		if (prev_ptr == ptr)
10338c2ecf20Sopenharmony_ci			return ptr;
10348c2ecf20Sopenharmony_ci		prev_ptr = ptr;
10358c2ecf20Sopenharmony_ci	} while (--timeout);
10368c2ecf20Sopenharmony_ci	dev_warn(emu->card->dev, "ca0106: unstable DMA pointer!\n");
10378c2ecf20Sopenharmony_ci	return 0;
10388c2ecf20Sopenharmony_ci}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci/* pointer_capture callback */
10418c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t
10428c2ecf20Sopenharmony_cisnd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
10458c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
10468c2ecf20Sopenharmony_ci	struct snd_ca0106_pcm *epcm = runtime->private_data;
10478c2ecf20Sopenharmony_ci	snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
10488c2ecf20Sopenharmony_ci	int channel = epcm->channel_id;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	if (!epcm->running)
10518c2ecf20Sopenharmony_ci		return 0;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
10548c2ecf20Sopenharmony_ci	ptr2 = bytes_to_frames(runtime, ptr1);
10558c2ecf20Sopenharmony_ci	ptr=ptr2;
10568c2ecf20Sopenharmony_ci        if (ptr >= runtime->buffer_size)
10578c2ecf20Sopenharmony_ci		ptr -= runtime->buffer_size;
10588c2ecf20Sopenharmony_ci	/*
10598c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev, "ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, "
10608c2ecf20Sopenharmony_ci	       "buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n",
10618c2ecf20Sopenharmony_ci	       ptr1, ptr2, ptr, (int)runtime->buffer_size,
10628c2ecf20Sopenharmony_ci	       (int)runtime->period_size, (int)runtime->frame_bits,
10638c2ecf20Sopenharmony_ci	       (int)runtime->rate);
10648c2ecf20Sopenharmony_ci	*/
10658c2ecf20Sopenharmony_ci	return ptr;
10668c2ecf20Sopenharmony_ci}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci/* operators */
10698c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_playback_front_ops = {
10708c2ecf20Sopenharmony_ci	.open =        snd_ca0106_pcm_open_playback_front,
10718c2ecf20Sopenharmony_ci	.close =       snd_ca0106_pcm_close_playback,
10728c2ecf20Sopenharmony_ci	.prepare =     snd_ca0106_pcm_prepare_playback,
10738c2ecf20Sopenharmony_ci	.trigger =     snd_ca0106_pcm_trigger_playback,
10748c2ecf20Sopenharmony_ci	.pointer =     snd_ca0106_pcm_pointer_playback,
10758c2ecf20Sopenharmony_ci};
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_capture_0_ops = {
10788c2ecf20Sopenharmony_ci	.open =        snd_ca0106_pcm_open_0_capture,
10798c2ecf20Sopenharmony_ci	.close =       snd_ca0106_pcm_close_capture,
10808c2ecf20Sopenharmony_ci	.prepare =     snd_ca0106_pcm_prepare_capture,
10818c2ecf20Sopenharmony_ci	.trigger =     snd_ca0106_pcm_trigger_capture,
10828c2ecf20Sopenharmony_ci	.pointer =     snd_ca0106_pcm_pointer_capture,
10838c2ecf20Sopenharmony_ci};
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_capture_1_ops = {
10868c2ecf20Sopenharmony_ci	.open =        snd_ca0106_pcm_open_1_capture,
10878c2ecf20Sopenharmony_ci	.close =       snd_ca0106_pcm_close_capture,
10888c2ecf20Sopenharmony_ci	.prepare =     snd_ca0106_pcm_prepare_capture,
10898c2ecf20Sopenharmony_ci	.trigger =     snd_ca0106_pcm_trigger_capture,
10908c2ecf20Sopenharmony_ci	.pointer =     snd_ca0106_pcm_pointer_capture,
10918c2ecf20Sopenharmony_ci};
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_capture_2_ops = {
10948c2ecf20Sopenharmony_ci	.open =        snd_ca0106_pcm_open_2_capture,
10958c2ecf20Sopenharmony_ci	.close =       snd_ca0106_pcm_close_capture,
10968c2ecf20Sopenharmony_ci	.prepare =     snd_ca0106_pcm_prepare_capture,
10978c2ecf20Sopenharmony_ci	.trigger =     snd_ca0106_pcm_trigger_capture,
10988c2ecf20Sopenharmony_ci	.pointer =     snd_ca0106_pcm_pointer_capture,
10998c2ecf20Sopenharmony_ci};
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_capture_3_ops = {
11028c2ecf20Sopenharmony_ci	.open =        snd_ca0106_pcm_open_3_capture,
11038c2ecf20Sopenharmony_ci	.close =       snd_ca0106_pcm_close_capture,
11048c2ecf20Sopenharmony_ci	.prepare =     snd_ca0106_pcm_prepare_capture,
11058c2ecf20Sopenharmony_ci	.trigger =     snd_ca0106_pcm_trigger_capture,
11068c2ecf20Sopenharmony_ci	.pointer =     snd_ca0106_pcm_pointer_capture,
11078c2ecf20Sopenharmony_ci};
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
11108c2ecf20Sopenharmony_ci        .open =         snd_ca0106_pcm_open_playback_center_lfe,
11118c2ecf20Sopenharmony_ci        .close =        snd_ca0106_pcm_close_playback,
11128c2ecf20Sopenharmony_ci        .prepare =      snd_ca0106_pcm_prepare_playback,
11138c2ecf20Sopenharmony_ci        .trigger =      snd_ca0106_pcm_trigger_playback,
11148c2ecf20Sopenharmony_ci        .pointer =      snd_ca0106_pcm_pointer_playback,
11158c2ecf20Sopenharmony_ci};
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
11188c2ecf20Sopenharmony_ci        .open =         snd_ca0106_pcm_open_playback_unknown,
11198c2ecf20Sopenharmony_ci        .close =        snd_ca0106_pcm_close_playback,
11208c2ecf20Sopenharmony_ci        .prepare =      snd_ca0106_pcm_prepare_playback,
11218c2ecf20Sopenharmony_ci        .trigger =      snd_ca0106_pcm_trigger_playback,
11228c2ecf20Sopenharmony_ci        .pointer =      snd_ca0106_pcm_pointer_playback,
11238c2ecf20Sopenharmony_ci};
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
11268c2ecf20Sopenharmony_ci        .open =         snd_ca0106_pcm_open_playback_rear,
11278c2ecf20Sopenharmony_ci        .close =        snd_ca0106_pcm_close_playback,
11288c2ecf20Sopenharmony_ci        .prepare =      snd_ca0106_pcm_prepare_playback,
11298c2ecf20Sopenharmony_ci        .trigger =      snd_ca0106_pcm_trigger_playback,
11308c2ecf20Sopenharmony_ci        .pointer =      snd_ca0106_pcm_pointer_playback,
11318c2ecf20Sopenharmony_ci};
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_cistatic unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
11358c2ecf20Sopenharmony_ci					     unsigned short reg)
11368c2ecf20Sopenharmony_ci{
11378c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = ac97->private_data;
11388c2ecf20Sopenharmony_ci	unsigned long flags;
11398c2ecf20Sopenharmony_ci	unsigned short val;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
11428c2ecf20Sopenharmony_ci	outb(reg, emu->port + AC97ADDRESS);
11438c2ecf20Sopenharmony_ci	val = inw(emu->port + AC97DATA);
11448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
11458c2ecf20Sopenharmony_ci	return val;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
11498c2ecf20Sopenharmony_ci				    unsigned short reg, unsigned short val)
11508c2ecf20Sopenharmony_ci{
11518c2ecf20Sopenharmony_ci	struct snd_ca0106 *emu = ac97->private_data;
11528c2ecf20Sopenharmony_ci	unsigned long flags;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->emu_lock, flags);
11558c2ecf20Sopenharmony_ci	outb(reg, emu->port + AC97ADDRESS);
11568c2ecf20Sopenharmony_ci	outw(val, emu->port + AC97DATA);
11578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->emu_lock, flags);
11588c2ecf20Sopenharmony_ci}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_cistatic int snd_ca0106_ac97(struct snd_ca0106 *chip)
11618c2ecf20Sopenharmony_ci{
11628c2ecf20Sopenharmony_ci	struct snd_ac97_bus *pbus;
11638c2ecf20Sopenharmony_ci	struct snd_ac97_template ac97;
11648c2ecf20Sopenharmony_ci	int err;
11658c2ecf20Sopenharmony_ci	static const struct snd_ac97_bus_ops ops = {
11668c2ecf20Sopenharmony_ci		.write = snd_ca0106_ac97_write,
11678c2ecf20Sopenharmony_ci		.read = snd_ca0106_ac97_read,
11688c2ecf20Sopenharmony_ci	};
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
11718c2ecf20Sopenharmony_ci		return err;
11728c2ecf20Sopenharmony_ci	pbus->no_vra = 1; /* we don't need VRA */
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	memset(&ac97, 0, sizeof(ac97));
11758c2ecf20Sopenharmony_ci	ac97.private_data = chip;
11768c2ecf20Sopenharmony_ci	ac97.scaps = AC97_SCAP_NO_SPDIF;
11778c2ecf20Sopenharmony_ci	return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
11788c2ecf20Sopenharmony_ci}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_cistatic void ca0106_stop_chip(struct snd_ca0106 *chip);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic int snd_ca0106_free(struct snd_ca0106 *chip)
11838c2ecf20Sopenharmony_ci{
11848c2ecf20Sopenharmony_ci	if (chip->res_port != NULL) {
11858c2ecf20Sopenharmony_ci		/* avoid access to already used hardware */
11868c2ecf20Sopenharmony_ci		ca0106_stop_chip(chip);
11878c2ecf20Sopenharmony_ci	}
11888c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
11898c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
11908c2ecf20Sopenharmony_ci	// release the data
11918c2ecf20Sopenharmony_ci#if 1
11928c2ecf20Sopenharmony_ci	if (chip->buffer.area)
11938c2ecf20Sopenharmony_ci		snd_dma_free_pages(&chip->buffer);
11948c2ecf20Sopenharmony_ci#endif
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	// release the i/o port
11978c2ecf20Sopenharmony_ci	release_and_free_resource(chip->res_port);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	pci_disable_device(chip->pci);
12008c2ecf20Sopenharmony_ci	kfree(chip);
12018c2ecf20Sopenharmony_ci	return 0;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic int snd_ca0106_dev_free(struct snd_device *device)
12058c2ecf20Sopenharmony_ci{
12068c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = device->device_data;
12078c2ecf20Sopenharmony_ci	return snd_ca0106_free(chip);
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	unsigned int status;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = dev_id;
12158c2ecf20Sopenharmony_ci	int i;
12168c2ecf20Sopenharmony_ci	int mask;
12178c2ecf20Sopenharmony_ci        unsigned int stat76;
12188c2ecf20Sopenharmony_ci	struct snd_ca0106_channel *pchannel;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	status = inl(chip->port + IPR);
12218c2ecf20Sopenharmony_ci	if (! status)
12228c2ecf20Sopenharmony_ci		return IRQ_NONE;
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci        stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
12258c2ecf20Sopenharmony_ci	/*
12268c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev, "interrupt status = 0x%08x, stat76=0x%08x\n",
12278c2ecf20Sopenharmony_ci		   status, stat76);
12288c2ecf20Sopenharmony_ci	dev_dbg(emu->card->dev, "ptr=0x%08x\n",
12298c2ecf20Sopenharmony_ci		   snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
12308c2ecf20Sopenharmony_ci	*/
12318c2ecf20Sopenharmony_ci        mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
12328c2ecf20Sopenharmony_ci	for(i = 0; i < 4; i++) {
12338c2ecf20Sopenharmony_ci		pchannel = &(chip->playback_channels[i]);
12348c2ecf20Sopenharmony_ci		if (stat76 & mask) {
12358c2ecf20Sopenharmony_ci/* FIXME: Select the correct substream for period elapsed */
12368c2ecf20Sopenharmony_ci			if(pchannel->use) {
12378c2ecf20Sopenharmony_ci				snd_pcm_period_elapsed(pchannel->epcm->substream);
12388c2ecf20Sopenharmony_ci				/* dev_dbg(emu->card->dev, "interrupt [%d] used\n", i); */
12398c2ecf20Sopenharmony_ci                        }
12408c2ecf20Sopenharmony_ci		}
12418c2ecf20Sopenharmony_ci		/*
12428c2ecf20Sopenharmony_ci		dev_dbg(emu->card->dev, "channel=%p\n", pchannel);
12438c2ecf20Sopenharmony_ci		dev_dbg(emu->card->dev, "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
12448c2ecf20Sopenharmony_ci		*/
12458c2ecf20Sopenharmony_ci		mask <<= 1;
12468c2ecf20Sopenharmony_ci	}
12478c2ecf20Sopenharmony_ci        mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
12488c2ecf20Sopenharmony_ci	for(i = 0; i < 4; i++) {
12498c2ecf20Sopenharmony_ci		pchannel = &(chip->capture_channels[i]);
12508c2ecf20Sopenharmony_ci		if (stat76 & mask) {
12518c2ecf20Sopenharmony_ci/* FIXME: Select the correct substream for period elapsed */
12528c2ecf20Sopenharmony_ci			if(pchannel->use) {
12538c2ecf20Sopenharmony_ci				snd_pcm_period_elapsed(pchannel->epcm->substream);
12548c2ecf20Sopenharmony_ci				/* dev_dbg(emu->card->dev, "interrupt [%d] used\n", i); */
12558c2ecf20Sopenharmony_ci                        }
12568c2ecf20Sopenharmony_ci		}
12578c2ecf20Sopenharmony_ci		/*
12588c2ecf20Sopenharmony_ci		dev_dbg(emu->card->dev, "channel=%p\n", pchannel);
12598c2ecf20Sopenharmony_ci		dev_dbg(emu->card->dev, "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
12608c2ecf20Sopenharmony_ci		*/
12618c2ecf20Sopenharmony_ci		mask <<= 1;
12628c2ecf20Sopenharmony_ci	}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci        snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	if (chip->midi.dev_id &&
12678c2ecf20Sopenharmony_ci	    (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
12688c2ecf20Sopenharmony_ci		if (chip->midi.interrupt)
12698c2ecf20Sopenharmony_ci			chip->midi.interrupt(&chip->midi, status);
12708c2ecf20Sopenharmony_ci		else
12718c2ecf20Sopenharmony_ci			chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
12728c2ecf20Sopenharmony_ci	}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	// acknowledge the interrupt if necessary
12758c2ecf20Sopenharmony_ci	outl(status, chip->port+IPR);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_cistatic const struct snd_pcm_chmap_elem surround_map[] = {
12818c2ecf20Sopenharmony_ci	{ .channels = 2,
12828c2ecf20Sopenharmony_ci	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12838c2ecf20Sopenharmony_ci	{ }
12848c2ecf20Sopenharmony_ci};
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_cistatic const struct snd_pcm_chmap_elem clfe_map[] = {
12878c2ecf20Sopenharmony_ci	{ .channels = 2,
12888c2ecf20Sopenharmony_ci	  .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
12898c2ecf20Sopenharmony_ci	{ }
12908c2ecf20Sopenharmony_ci};
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_cistatic const struct snd_pcm_chmap_elem side_map[] = {
12938c2ecf20Sopenharmony_ci	{ .channels = 2,
12948c2ecf20Sopenharmony_ci	  .map = { SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
12958c2ecf20Sopenharmony_ci	{ }
12968c2ecf20Sopenharmony_ci};
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_cistatic int snd_ca0106_pcm(struct snd_ca0106 *emu, int device)
12998c2ecf20Sopenharmony_ci{
13008c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
13018c2ecf20Sopenharmony_ci	struct snd_pcm_substream *substream;
13028c2ecf20Sopenharmony_ci	const struct snd_pcm_chmap_elem *map = NULL;
13038c2ecf20Sopenharmony_ci	int err;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm);
13068c2ecf20Sopenharmony_ci	if (err < 0)
13078c2ecf20Sopenharmony_ci		return err;
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	pcm->private_data = emu;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	switch (device) {
13128c2ecf20Sopenharmony_ci	case 0:
13138c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
13148c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
13158c2ecf20Sopenharmony_ci	  map = snd_pcm_std_chmaps;
13168c2ecf20Sopenharmony_ci          break;
13178c2ecf20Sopenharmony_ci	case 1:
13188c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
13198c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
13208c2ecf20Sopenharmony_ci	  map = surround_map;
13218c2ecf20Sopenharmony_ci          break;
13228c2ecf20Sopenharmony_ci	case 2:
13238c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
13248c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
13258c2ecf20Sopenharmony_ci	  map = clfe_map;
13268c2ecf20Sopenharmony_ci          break;
13278c2ecf20Sopenharmony_ci	case 3:
13288c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
13298c2ecf20Sopenharmony_ci	  snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
13308c2ecf20Sopenharmony_ci	  map = side_map;
13318c2ecf20Sopenharmony_ci          break;
13328c2ecf20Sopenharmony_ci        }
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
13358c2ecf20Sopenharmony_ci	strcpy(pcm->name, "CA0106");
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
13388c2ecf20Sopenharmony_ci	    substream;
13398c2ecf20Sopenharmony_ci	    substream = substream->next) {
13408c2ecf20Sopenharmony_ci		snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
13418c2ecf20Sopenharmony_ci					   &emu->pci->dev,
13428c2ecf20Sopenharmony_ci					   64*1024, 64*1024);
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
13468c2ecf20Sopenharmony_ci	      substream;
13478c2ecf20Sopenharmony_ci	      substream = substream->next) {
13488c2ecf20Sopenharmony_ci		snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
13498c2ecf20Sopenharmony_ci					   &emu->pci->dev,
13508c2ecf20Sopenharmony_ci					   64*1024, 64*1024);
13518c2ecf20Sopenharmony_ci	}
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, 2,
13548c2ecf20Sopenharmony_ci				     1 << 2, NULL);
13558c2ecf20Sopenharmony_ci	if (err < 0)
13568c2ecf20Sopenharmony_ci		return err;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	emu->pcm[device] = pcm;
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	return 0;
13618c2ecf20Sopenharmony_ci}
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci#define SPI_REG(reg, value)	(((reg) << SPI_REG_SHIFT) | (value))
13648c2ecf20Sopenharmony_cistatic const unsigned int spi_dac_init[] = {
13658c2ecf20Sopenharmony_ci	SPI_REG(SPI_LDA1_REG,	SPI_DA_BIT_0dB), /* 0dB dig. attenuation */
13668c2ecf20Sopenharmony_ci	SPI_REG(SPI_RDA1_REG,	SPI_DA_BIT_0dB),
13678c2ecf20Sopenharmony_ci	SPI_REG(SPI_PL_REG,	SPI_PL_BIT_L_L | SPI_PL_BIT_R_R | SPI_IZD_BIT),
13688c2ecf20Sopenharmony_ci	SPI_REG(SPI_FMT_REG,	SPI_FMT_BIT_I2S | SPI_IWL_BIT_24),
13698c2ecf20Sopenharmony_ci	SPI_REG(SPI_LDA2_REG,	SPI_DA_BIT_0dB),
13708c2ecf20Sopenharmony_ci	SPI_REG(SPI_RDA2_REG,	SPI_DA_BIT_0dB),
13718c2ecf20Sopenharmony_ci	SPI_REG(SPI_LDA3_REG,	SPI_DA_BIT_0dB),
13728c2ecf20Sopenharmony_ci	SPI_REG(SPI_RDA3_REG,	SPI_DA_BIT_0dB),
13738c2ecf20Sopenharmony_ci	SPI_REG(SPI_MASTDA_REG,	SPI_DA_BIT_0dB),
13748c2ecf20Sopenharmony_ci	SPI_REG(9,		0x00),
13758c2ecf20Sopenharmony_ci	SPI_REG(SPI_MS_REG,	SPI_DACD0_BIT | SPI_DACD1_BIT | SPI_DACD2_BIT),
13768c2ecf20Sopenharmony_ci	SPI_REG(12,		0x00),
13778c2ecf20Sopenharmony_ci	SPI_REG(SPI_LDA4_REG,	SPI_DA_BIT_0dB),
13788c2ecf20Sopenharmony_ci	SPI_REG(SPI_RDA4_REG,	SPI_DA_BIT_0dB | SPI_DA_BIT_UPDATE),
13798c2ecf20Sopenharmony_ci	SPI_REG(SPI_DACD4_REG,	SPI_DACD4_BIT),
13808c2ecf20Sopenharmony_ci};
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_cistatic const unsigned int i2c_adc_init[][2] = {
13838c2ecf20Sopenharmony_ci	{ 0x17, 0x00 }, /* Reset */
13848c2ecf20Sopenharmony_ci	{ 0x07, 0x00 }, /* Timeout */
13858c2ecf20Sopenharmony_ci	{ 0x0b, 0x22 },  /* Interface control */
13868c2ecf20Sopenharmony_ci	{ 0x0c, 0x22 },  /* Master mode control */
13878c2ecf20Sopenharmony_ci	{ 0x0d, 0x08 },  /* Powerdown control */
13888c2ecf20Sopenharmony_ci	{ 0x0e, 0xcf },  /* Attenuation Left  0x01 = -103dB, 0xff = 24dB */
13898c2ecf20Sopenharmony_ci	{ 0x0f, 0xcf },  /* Attenuation Right 0.5dB steps */
13908c2ecf20Sopenharmony_ci	{ 0x10, 0x7b },  /* ALC Control 1 */
13918c2ecf20Sopenharmony_ci	{ 0x11, 0x00 },  /* ALC Control 2 */
13928c2ecf20Sopenharmony_ci	{ 0x12, 0x32 },  /* ALC Control 3 */
13938c2ecf20Sopenharmony_ci	{ 0x13, 0x00 },  /* Noise gate control */
13948c2ecf20Sopenharmony_ci	{ 0x14, 0xa6 },  /* Limiter control */
13958c2ecf20Sopenharmony_ci	{ 0x15, ADC_MUX_LINEIN },  /* ADC Mixer control */
13968c2ecf20Sopenharmony_ci};
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_cistatic void ca0106_init_chip(struct snd_ca0106 *chip, int resume)
13998c2ecf20Sopenharmony_ci{
14008c2ecf20Sopenharmony_ci	int ch;
14018c2ecf20Sopenharmony_ci	unsigned int def_bits;
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	outl(0, chip->port + INTE);
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	/*
14068c2ecf20Sopenharmony_ci	 *  Init to 0x02109204 :
14078c2ecf20Sopenharmony_ci	 *  Clock accuracy    = 0     (1000ppm)
14088c2ecf20Sopenharmony_ci	 *  Sample Rate       = 2     (48kHz)
14098c2ecf20Sopenharmony_ci	 *  Audio Channel     = 1     (Left of 2)
14108c2ecf20Sopenharmony_ci	 *  Source Number     = 0     (Unspecified)
14118c2ecf20Sopenharmony_ci	 *  Generation Status = 1     (Original for Cat Code 12)
14128c2ecf20Sopenharmony_ci	 *  Cat Code          = 12    (Digital Signal Mixer)
14138c2ecf20Sopenharmony_ci	 *  Mode              = 0     (Mode 0)
14148c2ecf20Sopenharmony_ci	 *  Emphasis          = 0     (None)
14158c2ecf20Sopenharmony_ci	 *  CP                = 1     (Copyright unasserted)
14168c2ecf20Sopenharmony_ci	 *  AN                = 0     (Audio data)
14178c2ecf20Sopenharmony_ci	 *  P                 = 0     (Consumer)
14188c2ecf20Sopenharmony_ci	 */
14198c2ecf20Sopenharmony_ci	def_bits =
14208c2ecf20Sopenharmony_ci		SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
14218c2ecf20Sopenharmony_ci		SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
14228c2ecf20Sopenharmony_ci		SPCS_GENERATIONSTATUS | 0x00001200 |
14238c2ecf20Sopenharmony_ci		0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
14248c2ecf20Sopenharmony_ci	if (!resume) {
14258c2ecf20Sopenharmony_ci		chip->spdif_str_bits[0] = chip->spdif_bits[0] = def_bits;
14268c2ecf20Sopenharmony_ci		chip->spdif_str_bits[1] = chip->spdif_bits[1] = def_bits;
14278c2ecf20Sopenharmony_ci		chip->spdif_str_bits[2] = chip->spdif_bits[2] = def_bits;
14288c2ecf20Sopenharmony_ci		chip->spdif_str_bits[3] = chip->spdif_bits[3] = def_bits;
14298c2ecf20Sopenharmony_ci	}
14308c2ecf20Sopenharmony_ci	/* Only SPCS1 has been tested */
14318c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPCS1, 0, chip->spdif_str_bits[1]);
14328c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPCS0, 0, chip->spdif_str_bits[0]);
14338c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPCS2, 0, chip->spdif_str_bits[2]);
14348c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPCS3, 0, chip->spdif_str_bits[3]);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci        snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
14378c2ecf20Sopenharmony_ci        snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci        /* Write 0x8000 to AC97_REC_GAIN to mute it. */
14408c2ecf20Sopenharmony_ci        outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
14418c2ecf20Sopenharmony_ci        outw(0x8000, chip->port + AC97DATA);
14428c2ecf20Sopenharmony_ci#if 0 /* FIXME: what are these? */
14438c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
14448c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
14458c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
14468c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
14478c2ecf20Sopenharmony_ci#endif
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	/* OSS drivers set this. */
14508c2ecf20Sopenharmony_ci	/* snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); */
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	/* Analog or Digital output */
14538c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
14548c2ecf20Sopenharmony_ci	/* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers.
14558c2ecf20Sopenharmony_ci	 * Use 0x000f0000 for surround71
14568c2ecf20Sopenharmony_ci	 */
14578c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	chip->spdif_enable = 0; /* Set digital SPDIF output off */
14608c2ecf20Sopenharmony_ci	/*snd_ca0106_ptr_write(chip, 0x45, 0, 0);*/ /* Analogue out */
14618c2ecf20Sopenharmony_ci	/*snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00);*/ /* Digital out */
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	/* goes to 0x40c80000 when doing SPDIF IN/OUT */
14648c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000);
14658c2ecf20Sopenharmony_ci	/* (Mute) CAPTURE feedback into PLAYBACK volume.
14668c2ecf20Sopenharmony_ci	 * Only lower 16 bits matter.
14678c2ecf20Sopenharmony_ci	 */
14688c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff);
14698c2ecf20Sopenharmony_ci	/* SPDIF IN Volume */
14708c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000);
14718c2ecf20Sopenharmony_ci	/* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
14728c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
14758c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
14768c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
14778c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	for (ch = 0; ch < 4; ch++) {
14808c2ecf20Sopenharmony_ci		/* Only high 16 bits matter */
14818c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030);
14828c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
14838c2ecf20Sopenharmony_ci#if 0 /* Mute */
14848c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040);
14858c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040);
14868c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff);
14878c2ecf20Sopenharmony_ci		snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff);
14888c2ecf20Sopenharmony_ci#endif
14898c2ecf20Sopenharmony_ci	}
14908c2ecf20Sopenharmony_ci	if (chip->details->i2c_adc == 1) {
14918c2ecf20Sopenharmony_ci	        /* Select MIC, Line in, TAD in, AUX in */
14928c2ecf20Sopenharmony_ci	        snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
14938c2ecf20Sopenharmony_ci		/* Default to CAPTURE_SOURCE to i2s in */
14948c2ecf20Sopenharmony_ci		if (!resume)
14958c2ecf20Sopenharmony_ci			chip->capture_source = 3;
14968c2ecf20Sopenharmony_ci	} else if (chip->details->ac97 == 1) {
14978c2ecf20Sopenharmony_ci	        /* Default to AC97 in */
14988c2ecf20Sopenharmony_ci	        snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x444400e4);
14998c2ecf20Sopenharmony_ci		/* Default to CAPTURE_SOURCE to AC97 in */
15008c2ecf20Sopenharmony_ci		if (!resume)
15018c2ecf20Sopenharmony_ci			chip->capture_source = 4;
15028c2ecf20Sopenharmony_ci	} else {
15038c2ecf20Sopenharmony_ci	        /* Select MIC, Line in, TAD in, AUX in */
15048c2ecf20Sopenharmony_ci	        snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4);
15058c2ecf20Sopenharmony_ci		/* Default to Set CAPTURE_SOURCE to i2s in */
15068c2ecf20Sopenharmony_ci		if (!resume)
15078c2ecf20Sopenharmony_ci			chip->capture_source = 3;
15088c2ecf20Sopenharmony_ci	}
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	if (chip->details->gpio_type == 2) {
15118c2ecf20Sopenharmony_ci		/* The SB0438 use GPIO differently. */
15128c2ecf20Sopenharmony_ci		/* FIXME: Still need to find out what the other GPIO bits do.
15138c2ecf20Sopenharmony_ci		 * E.g. For digital spdif out.
15148c2ecf20Sopenharmony_ci		 */
15158c2ecf20Sopenharmony_ci		outl(0x0, chip->port+GPIO);
15168c2ecf20Sopenharmony_ci		/* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
15178c2ecf20Sopenharmony_ci		outl(0x005f5301, chip->port+GPIO); /* Analog */
15188c2ecf20Sopenharmony_ci	} else if (chip->details->gpio_type == 1) {
15198c2ecf20Sopenharmony_ci		/* The SB0410 and SB0413 use GPIO differently. */
15208c2ecf20Sopenharmony_ci		/* FIXME: Still need to find out what the other GPIO bits do.
15218c2ecf20Sopenharmony_ci		 * E.g. For digital spdif out.
15228c2ecf20Sopenharmony_ci		 */
15238c2ecf20Sopenharmony_ci		outl(0x0, chip->port+GPIO);
15248c2ecf20Sopenharmony_ci		/* outl(0x00f0e000, chip->port+GPIO); */ /* Analog */
15258c2ecf20Sopenharmony_ci		outl(0x005f5301, chip->port+GPIO); /* Analog */
15268c2ecf20Sopenharmony_ci	} else {
15278c2ecf20Sopenharmony_ci		outl(0x0, chip->port+GPIO);
15288c2ecf20Sopenharmony_ci		outl(0x005f03a3, chip->port+GPIO); /* Analog */
15298c2ecf20Sopenharmony_ci		/* outl(0x005f02a2, chip->port+GPIO); */ /* SPDIF */
15308c2ecf20Sopenharmony_ci	}
15318c2ecf20Sopenharmony_ci	snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	/* outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG); */
15348c2ecf20Sopenharmony_ci	/* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
15358c2ecf20Sopenharmony_ci	/* outl(0x00001409, chip->port+HCFG); */
15368c2ecf20Sopenharmony_ci	/* outl(0x00000009, chip->port+HCFG); */
15378c2ecf20Sopenharmony_ci	/* AC97 2.0, Enable outputs. */
15388c2ecf20Sopenharmony_ci	outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG);
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci	if (chip->details->i2c_adc == 1) {
15418c2ecf20Sopenharmony_ci		/* The SB0410 and SB0413 use I2C to control ADC. */
15428c2ecf20Sopenharmony_ci		int size, n;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci		size = ARRAY_SIZE(i2c_adc_init);
15458c2ecf20Sopenharmony_ci		/* dev_dbg(emu->card->dev, "I2C:array size=0x%x\n", size); */
15468c2ecf20Sopenharmony_ci		for (n = 0; n < size; n++)
15478c2ecf20Sopenharmony_ci			snd_ca0106_i2c_write(chip, i2c_adc_init[n][0],
15488c2ecf20Sopenharmony_ci					     i2c_adc_init[n][1]);
15498c2ecf20Sopenharmony_ci		for (n = 0; n < 4; n++) {
15508c2ecf20Sopenharmony_ci			chip->i2c_capture_volume[n][0] = 0xcf;
15518c2ecf20Sopenharmony_ci			chip->i2c_capture_volume[n][1] = 0xcf;
15528c2ecf20Sopenharmony_ci		}
15538c2ecf20Sopenharmony_ci		chip->i2c_capture_source = 2; /* Line in */
15548c2ecf20Sopenharmony_ci		/* Enable Line-in capture. MIC in currently untested. */
15558c2ecf20Sopenharmony_ci		/* snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); */
15568c2ecf20Sopenharmony_ci	}
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	if (chip->details->spi_dac) {
15598c2ecf20Sopenharmony_ci		/* The SB0570 use SPI to control DAC. */
15608c2ecf20Sopenharmony_ci		int size, n;
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci		size = ARRAY_SIZE(spi_dac_init);
15638c2ecf20Sopenharmony_ci		for (n = 0; n < size; n++) {
15648c2ecf20Sopenharmony_ci			int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci			snd_ca0106_spi_write(chip, spi_dac_init[n]);
15678c2ecf20Sopenharmony_ci			if (reg < ARRAY_SIZE(chip->spi_dac_reg))
15688c2ecf20Sopenharmony_ci				chip->spi_dac_reg[reg] = spi_dac_init[n];
15698c2ecf20Sopenharmony_ci		}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci		/* Enable front dac only */
15728c2ecf20Sopenharmony_ci		snd_ca0106_pcm_power_dac(chip, PCM_FRONT_CHANNEL, 1);
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci}
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_cistatic void ca0106_stop_chip(struct snd_ca0106 *chip)
15778c2ecf20Sopenharmony_ci{
15788c2ecf20Sopenharmony_ci	/* disable interrupts */
15798c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
15808c2ecf20Sopenharmony_ci	outl(0, chip->port + INTE);
15818c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
15828c2ecf20Sopenharmony_ci	udelay(1000);
15838c2ecf20Sopenharmony_ci	/* disable audio */
15848c2ecf20Sopenharmony_ci	/* outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG); */
15858c2ecf20Sopenharmony_ci	outl(0, chip->port + HCFG);
15868c2ecf20Sopenharmony_ci	/* FIXME: We need to stop and DMA transfers here.
15878c2ecf20Sopenharmony_ci	 *        But as I am not sure how yet, we cannot from the dma pages.
15888c2ecf20Sopenharmony_ci	 * So we can fix: snd-malloc: Memory leak?  pages not freed = 8
15898c2ecf20Sopenharmony_ci	 */
15908c2ecf20Sopenharmony_ci}
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_cistatic int snd_ca0106_create(int dev, struct snd_card *card,
15938c2ecf20Sopenharmony_ci					 struct pci_dev *pci,
15948c2ecf20Sopenharmony_ci					 struct snd_ca0106 **rchip)
15958c2ecf20Sopenharmony_ci{
15968c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip;
15978c2ecf20Sopenharmony_ci	const struct snd_ca0106_details *c;
15988c2ecf20Sopenharmony_ci	int err;
15998c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
16008c2ecf20Sopenharmony_ci		.dev_free = snd_ca0106_dev_free,
16018c2ecf20Sopenharmony_ci	};
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	*rchip = NULL;
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	err = pci_enable_device(pci);
16068c2ecf20Sopenharmony_ci	if (err < 0)
16078c2ecf20Sopenharmony_ci		return err;
16088c2ecf20Sopenharmony_ci	if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0 ||
16098c2ecf20Sopenharmony_ci	    dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
16108c2ecf20Sopenharmony_ci		dev_err(card->dev, "error to set 32bit mask DMA\n");
16118c2ecf20Sopenharmony_ci		pci_disable_device(pci);
16128c2ecf20Sopenharmony_ci		return -ENXIO;
16138c2ecf20Sopenharmony_ci	}
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
16168c2ecf20Sopenharmony_ci	if (chip == NULL) {
16178c2ecf20Sopenharmony_ci		pci_disable_device(pci);
16188c2ecf20Sopenharmony_ci		return -ENOMEM;
16198c2ecf20Sopenharmony_ci	}
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	chip->card = card;
16228c2ecf20Sopenharmony_ci	chip->pci = pci;
16238c2ecf20Sopenharmony_ci	chip->irq = -1;
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	spin_lock_init(&chip->emu_lock);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	chip->port = pci_resource_start(pci, 0);
16288c2ecf20Sopenharmony_ci	chip->res_port = request_region(chip->port, 0x20, "snd_ca0106");
16298c2ecf20Sopenharmony_ci	if (!chip->res_port) {
16308c2ecf20Sopenharmony_ci		snd_ca0106_free(chip);
16318c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot allocate the port\n");
16328c2ecf20Sopenharmony_ci		return -EBUSY;
16338c2ecf20Sopenharmony_ci	}
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	if (request_irq(pci->irq, snd_ca0106_interrupt,
16368c2ecf20Sopenharmony_ci			IRQF_SHARED, KBUILD_MODNAME, chip)) {
16378c2ecf20Sopenharmony_ci		snd_ca0106_free(chip);
16388c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot grab irq\n");
16398c2ecf20Sopenharmony_ci		return -EBUSY;
16408c2ecf20Sopenharmony_ci	}
16418c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
16428c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	/* This stores the periods table. */
16458c2ecf20Sopenharmony_ci	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
16468c2ecf20Sopenharmony_ci				1024, &chip->buffer) < 0) {
16478c2ecf20Sopenharmony_ci		snd_ca0106_free(chip);
16488c2ecf20Sopenharmony_ci		return -ENOMEM;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	pci_set_master(pci);
16528c2ecf20Sopenharmony_ci	/* read serial */
16538c2ecf20Sopenharmony_ci	pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
16548c2ecf20Sopenharmony_ci	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
16558c2ecf20Sopenharmony_ci	dev_info(card->dev, "Model %04x Rev %08x Serial %08x\n",
16568c2ecf20Sopenharmony_ci	       chip->model, pci->revision, chip->serial);
16578c2ecf20Sopenharmony_ci	strcpy(card->driver, "CA0106");
16588c2ecf20Sopenharmony_ci	strcpy(card->shortname, "CA0106");
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_ci	for (c = ca0106_chip_details; c->serial; c++) {
16618c2ecf20Sopenharmony_ci		if (subsystem[dev]) {
16628c2ecf20Sopenharmony_ci			if (c->serial == subsystem[dev])
16638c2ecf20Sopenharmony_ci				break;
16648c2ecf20Sopenharmony_ci		} else if (c->serial == chip->serial)
16658c2ecf20Sopenharmony_ci			break;
16668c2ecf20Sopenharmony_ci	}
16678c2ecf20Sopenharmony_ci	chip->details = c;
16688c2ecf20Sopenharmony_ci	if (subsystem[dev]) {
16698c2ecf20Sopenharmony_ci		dev_info(card->dev, "Sound card name=%s, "
16708c2ecf20Sopenharmony_ci		       "subsystem=0x%x. Forced to subsystem=0x%x\n",
16718c2ecf20Sopenharmony_ci		       c->name, chip->serial, subsystem[dev]);
16728c2ecf20Sopenharmony_ci	}
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at 0x%lx irq %i",
16758c2ecf20Sopenharmony_ci		c->name, chip->port, chip->irq);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	ca0106_init_chip(chip, 0);
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
16808c2ecf20Sopenharmony_ci	if (err < 0) {
16818c2ecf20Sopenharmony_ci		snd_ca0106_free(chip);
16828c2ecf20Sopenharmony_ci		return err;
16838c2ecf20Sopenharmony_ci	}
16848c2ecf20Sopenharmony_ci	*rchip = chip;
16858c2ecf20Sopenharmony_ci	return 0;
16868c2ecf20Sopenharmony_ci}
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_cistatic void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
16908c2ecf20Sopenharmony_ci{
16918c2ecf20Sopenharmony_ci	snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
16928c2ecf20Sopenharmony_ci}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_cistatic void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
16958c2ecf20Sopenharmony_ci{
16968c2ecf20Sopenharmony_ci	snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
16978c2ecf20Sopenharmony_ci}
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_cistatic unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
17028c2ecf20Sopenharmony_ci						  midi->port + idx, 0);
17038c2ecf20Sopenharmony_ci}
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_cistatic void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
17068c2ecf20Sopenharmony_ci{
17078c2ecf20Sopenharmony_ci	snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
17088c2ecf20Sopenharmony_ci}
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_cistatic struct snd_card *ca0106_dev_id_card(void *dev_id)
17118c2ecf20Sopenharmony_ci{
17128c2ecf20Sopenharmony_ci	return ((struct snd_ca0106 *)dev_id)->card;
17138c2ecf20Sopenharmony_ci}
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_cistatic int ca0106_dev_id_port(void *dev_id)
17168c2ecf20Sopenharmony_ci{
17178c2ecf20Sopenharmony_ci	return ((struct snd_ca0106 *)dev_id)->port;
17188c2ecf20Sopenharmony_ci}
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_cistatic int snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
17218c2ecf20Sopenharmony_ci{
17228c2ecf20Sopenharmony_ci	struct snd_ca_midi *midi;
17238c2ecf20Sopenharmony_ci	char *name;
17248c2ecf20Sopenharmony_ci	int err;
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci	if (channel == CA0106_MIDI_CHAN_B) {
17278c2ecf20Sopenharmony_ci		name = "CA0106 MPU-401 (UART) B";
17288c2ecf20Sopenharmony_ci		midi =  &chip->midi2;
17298c2ecf20Sopenharmony_ci		midi->tx_enable = INTE_MIDI_TX_B;
17308c2ecf20Sopenharmony_ci		midi->rx_enable = INTE_MIDI_RX_B;
17318c2ecf20Sopenharmony_ci		midi->ipr_tx = IPR_MIDI_TX_B;
17328c2ecf20Sopenharmony_ci		midi->ipr_rx = IPR_MIDI_RX_B;
17338c2ecf20Sopenharmony_ci		midi->port = MIDI_UART_B_DATA;
17348c2ecf20Sopenharmony_ci	} else {
17358c2ecf20Sopenharmony_ci		name = "CA0106 MPU-401 (UART)";
17368c2ecf20Sopenharmony_ci		midi =  &chip->midi;
17378c2ecf20Sopenharmony_ci		midi->tx_enable = INTE_MIDI_TX_A;
17388c2ecf20Sopenharmony_ci		midi->rx_enable = INTE_MIDI_TX_B;
17398c2ecf20Sopenharmony_ci		midi->ipr_tx = IPR_MIDI_TX_A;
17408c2ecf20Sopenharmony_ci		midi->ipr_rx = IPR_MIDI_RX_A;
17418c2ecf20Sopenharmony_ci		midi->port = MIDI_UART_A_DATA;
17428c2ecf20Sopenharmony_ci	}
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	midi->reset = CA0106_MPU401_RESET;
17458c2ecf20Sopenharmony_ci	midi->enter_uart = CA0106_MPU401_ENTER_UART;
17468c2ecf20Sopenharmony_ci	midi->ack = CA0106_MPU401_ACK;
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
17498c2ecf20Sopenharmony_ci	midi->output_ready = CA0106_MIDI_OUTPUT_READY;
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci	midi->channel = channel;
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	midi->interrupt_enable = ca0106_midi_interrupt_enable;
17548c2ecf20Sopenharmony_ci	midi->interrupt_disable = ca0106_midi_interrupt_disable;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	midi->read = ca0106_midi_read;
17578c2ecf20Sopenharmony_ci	midi->write = ca0106_midi_write;
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	midi->get_dev_id_card = ca0106_dev_id_card;
17608c2ecf20Sopenharmony_ci	midi->get_dev_id_port = ca0106_dev_id_port;
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	midi->dev_id = chip;
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
17658c2ecf20Sopenharmony_ci		return err;
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	return 0;
17688c2ecf20Sopenharmony_ci}
17698c2ecf20Sopenharmony_ci
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_cistatic int snd_ca0106_probe(struct pci_dev *pci,
17728c2ecf20Sopenharmony_ci					const struct pci_device_id *pci_id)
17738c2ecf20Sopenharmony_ci{
17748c2ecf20Sopenharmony_ci	static int dev;
17758c2ecf20Sopenharmony_ci	struct snd_card *card;
17768c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip;
17778c2ecf20Sopenharmony_ci	int i, err;
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
17808c2ecf20Sopenharmony_ci		return -ENODEV;
17818c2ecf20Sopenharmony_ci	if (!enable[dev]) {
17828c2ecf20Sopenharmony_ci		dev++;
17838c2ecf20Sopenharmony_ci		return -ENOENT;
17848c2ecf20Sopenharmony_ci	}
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
17878c2ecf20Sopenharmony_ci			   0, &card);
17888c2ecf20Sopenharmony_ci	if (err < 0)
17898c2ecf20Sopenharmony_ci		return err;
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci	err = snd_ca0106_create(dev, card, pci, &chip);
17928c2ecf20Sopenharmony_ci	if (err < 0)
17938c2ecf20Sopenharmony_ci		goto error;
17948c2ecf20Sopenharmony_ci	card->private_data = chip;
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
17978c2ecf20Sopenharmony_ci		err = snd_ca0106_pcm(chip, i);
17988c2ecf20Sopenharmony_ci		if (err < 0)
17998c2ecf20Sopenharmony_ci			goto error;
18008c2ecf20Sopenharmony_ci	}
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	if (chip->details->ac97 == 1) {
18038c2ecf20Sopenharmony_ci		/* The SB0410 and SB0413 do not have an AC97 chip. */
18048c2ecf20Sopenharmony_ci		err = snd_ca0106_ac97(chip);
18058c2ecf20Sopenharmony_ci		if (err < 0)
18068c2ecf20Sopenharmony_ci			goto error;
18078c2ecf20Sopenharmony_ci	}
18088c2ecf20Sopenharmony_ci	err = snd_ca0106_mixer(chip);
18098c2ecf20Sopenharmony_ci	if (err < 0)
18108c2ecf20Sopenharmony_ci		goto error;
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	dev_dbg(card->dev, "probe for MIDI channel A ...");
18138c2ecf20Sopenharmony_ci	err = snd_ca0106_midi(chip, CA0106_MIDI_CHAN_A);
18148c2ecf20Sopenharmony_ci	if (err < 0)
18158c2ecf20Sopenharmony_ci		goto error;
18168c2ecf20Sopenharmony_ci	dev_dbg(card->dev, " done.\n");
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_PROC_FS
18198c2ecf20Sopenharmony_ci	snd_ca0106_proc_init(chip);
18208c2ecf20Sopenharmony_ci#endif
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci	err = snd_card_register(card);
18238c2ecf20Sopenharmony_ci	if (err < 0)
18248c2ecf20Sopenharmony_ci		goto error;
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
18278c2ecf20Sopenharmony_ci	dev++;
18288c2ecf20Sopenharmony_ci	return 0;
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci error:
18318c2ecf20Sopenharmony_ci	snd_card_free(card);
18328c2ecf20Sopenharmony_ci	return err;
18338c2ecf20Sopenharmony_ci}
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_cistatic void snd_ca0106_remove(struct pci_dev *pci)
18368c2ecf20Sopenharmony_ci{
18378c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
18388c2ecf20Sopenharmony_ci}
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
18418c2ecf20Sopenharmony_cistatic int snd_ca0106_suspend(struct device *dev)
18428c2ecf20Sopenharmony_ci{
18438c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
18448c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = card->private_data;
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
18478c2ecf20Sopenharmony_ci	if (chip->details->ac97)
18488c2ecf20Sopenharmony_ci		snd_ac97_suspend(chip->ac97);
18498c2ecf20Sopenharmony_ci	snd_ca0106_mixer_suspend(chip);
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_ci	ca0106_stop_chip(chip);
18528c2ecf20Sopenharmony_ci	return 0;
18538c2ecf20Sopenharmony_ci}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_cistatic int snd_ca0106_resume(struct device *dev)
18568c2ecf20Sopenharmony_ci{
18578c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
18588c2ecf20Sopenharmony_ci	struct snd_ca0106 *chip = card->private_data;
18598c2ecf20Sopenharmony_ci	int i;
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	ca0106_init_chip(chip, 1);
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci	if (chip->details->ac97)
18648c2ecf20Sopenharmony_ci		snd_ac97_resume(chip->ac97);
18658c2ecf20Sopenharmony_ci	snd_ca0106_mixer_resume(chip);
18668c2ecf20Sopenharmony_ci	if (chip->details->spi_dac) {
18678c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(chip->spi_dac_reg); i++)
18688c2ecf20Sopenharmony_ci			snd_ca0106_spi_write(chip, chip->spi_dac_reg[i]);
18698c2ecf20Sopenharmony_ci	}
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
18728c2ecf20Sopenharmony_ci	return 0;
18738c2ecf20Sopenharmony_ci}
18748c2ecf20Sopenharmony_ci
18758c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(snd_ca0106_pm, snd_ca0106_suspend, snd_ca0106_resume);
18768c2ecf20Sopenharmony_ci#define SND_CA0106_PM_OPS	&snd_ca0106_pm
18778c2ecf20Sopenharmony_ci#else
18788c2ecf20Sopenharmony_ci#define SND_CA0106_PM_OPS	NULL
18798c2ecf20Sopenharmony_ci#endif
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci// PCI IDs
18828c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_ca0106_ids[] = {
18838c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CREATIVE, 0x0007), 0 },	/* Audigy LS or Live 24bit */
18848c2ecf20Sopenharmony_ci	{ 0, }
18858c2ecf20Sopenharmony_ci};
18868c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci// pci_driver definition
18898c2ecf20Sopenharmony_cistatic struct pci_driver ca0106_driver = {
18908c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
18918c2ecf20Sopenharmony_ci	.id_table = snd_ca0106_ids,
18928c2ecf20Sopenharmony_ci	.probe = snd_ca0106_probe,
18938c2ecf20Sopenharmony_ci	.remove = snd_ca0106_remove,
18948c2ecf20Sopenharmony_ci	.driver = {
18958c2ecf20Sopenharmony_ci		.pm = SND_CA0106_PM_OPS,
18968c2ecf20Sopenharmony_ci	},
18978c2ecf20Sopenharmony_ci};
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_cimodule_pci_driver(ca0106_driver);
1900