18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  The driver for the EMU10K1 (SB Live!) based soundcards
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
78c2ecf20Sopenharmony_ci *      Added support for Audigy 2 Value.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci#include <linux/time.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <sound/core.h>
158c2ecf20Sopenharmony_ci#include <sound/emu10k1.h>
168c2ecf20Sopenharmony_ci#include <sound/initval.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EMU10K1");
208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
218c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Creative Labs,SB Live!/PCI512/E-mu APS},"
228c2ecf20Sopenharmony_ci	       "{Creative Labs,SB Audigy}}");
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SND_SEQUENCER)
258c2ecf20Sopenharmony_ci#define ENABLE_SYNTH
268c2ecf20Sopenharmony_ci#include <sound/emu10k1_synth.h>
278c2ecf20Sopenharmony_ci#endif
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
308c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
318c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
328c2ecf20Sopenharmony_cistatic int extin[SNDRV_CARDS];
338c2ecf20Sopenharmony_cistatic int extout[SNDRV_CARDS];
348c2ecf20Sopenharmony_cistatic int seq_ports[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4};
358c2ecf20Sopenharmony_cistatic int max_synth_voices[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 64};
368c2ecf20Sopenharmony_cistatic int max_buffer_size[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 128};
378c2ecf20Sopenharmony_cistatic bool enable_ir[SNDRV_CARDS];
388c2ecf20Sopenharmony_cistatic uint subsystem[SNDRV_CARDS]; /* Force card subsystem model */
398c2ecf20Sopenharmony_cistatic uint delay_pcm_irq[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for the EMU10K1 soundcard.");
438c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for the EMU10K1 soundcard.");
458c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable the EMU10K1 soundcard.");
478c2ecf20Sopenharmony_cimodule_param_array(extin, int, NULL, 0444);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(extin, "Available external inputs for FX8010. Zero=default.");
498c2ecf20Sopenharmony_cimodule_param_array(extout, int, NULL, 0444);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(extout, "Available external outputs for FX8010. Zero=default.");
518c2ecf20Sopenharmony_cimodule_param_array(seq_ports, int, NULL, 0444);
528c2ecf20Sopenharmony_ciMODULE_PARM_DESC(seq_ports, "Allocated sequencer ports for internal synthesizer.");
538c2ecf20Sopenharmony_cimodule_param_array(max_synth_voices, int, NULL, 0444);
548c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_synth_voices, "Maximum number of voices for WaveTable.");
558c2ecf20Sopenharmony_cimodule_param_array(max_buffer_size, int, NULL, 0444);
568c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_buffer_size, "Maximum sample buffer size in MB.");
578c2ecf20Sopenharmony_cimodule_param_array(enable_ir, bool, NULL, 0444);
588c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable_ir, "Enable IR.");
598c2ecf20Sopenharmony_cimodule_param_array(subsystem, uint, NULL, 0444);
608c2ecf20Sopenharmony_ciMODULE_PARM_DESC(subsystem, "Force card subsystem model.");
618c2ecf20Sopenharmony_cimodule_param_array(delay_pcm_irq, uint, NULL, 0444);
628c2ecf20Sopenharmony_ciMODULE_PARM_DESC(delay_pcm_irq, "Delay PCM interrupt by specified number of samples (default 0).");
638c2ecf20Sopenharmony_ci/*
648c2ecf20Sopenharmony_ci * Class 0401: 1102:0008 (rev 00) Subsystem: 1102:1001 -> Audigy2 Value  Model:SB0400
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_emu10k1_ids[] = {
678c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CREATIVE, 0x0002), 0 },	/* EMU10K1 */
688c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CREATIVE, 0x0004), 1 },	/* Audigy */
698c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CREATIVE, 0x0008), 1 },	/* Audigy 2 Value SB0400 */
708c2ecf20Sopenharmony_ci	{ 0, }
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Audigy 2 Value notes:
758c2ecf20Sopenharmony_ci * A_IOCFG Input (GPIO)
768c2ecf20Sopenharmony_ci * 0x400  = Front analog jack plugged in. (Green socket)
778c2ecf20Sopenharmony_ci * 0x1000 = Read analog jack plugged in. (Black socket)
788c2ecf20Sopenharmony_ci * 0x2000 = Center/LFE analog jack plugged in. (Orange socket)
798c2ecf20Sopenharmony_ci * A_IOCFG Output (GPIO)
808c2ecf20Sopenharmony_ci * 0x60 = Sound out of front Left.
818c2ecf20Sopenharmony_ci * Win sets it to 0xXX61
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_emu10k1_ids);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int snd_card_emu10k1_probe(struct pci_dev *pci,
878c2ecf20Sopenharmony_ci				  const struct pci_device_id *pci_id)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	static int dev;
908c2ecf20Sopenharmony_ci	struct snd_card *card;
918c2ecf20Sopenharmony_ci	struct snd_emu10k1 *emu;
928c2ecf20Sopenharmony_ci#ifdef ENABLE_SYNTH
938c2ecf20Sopenharmony_ci	struct snd_seq_device *wave = NULL;
948c2ecf20Sopenharmony_ci#endif
958c2ecf20Sopenharmony_ci	int err;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
988c2ecf20Sopenharmony_ci        	return -ENODEV;
998c2ecf20Sopenharmony_ci	if (!enable[dev]) {
1008c2ecf20Sopenharmony_ci		dev++;
1018c2ecf20Sopenharmony_ci		return -ENOENT;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1058c2ecf20Sopenharmony_ci			   0, &card);
1068c2ecf20Sopenharmony_ci	if (err < 0)
1078c2ecf20Sopenharmony_ci		return err;
1088c2ecf20Sopenharmony_ci	if (max_buffer_size[dev] < 32)
1098c2ecf20Sopenharmony_ci		max_buffer_size[dev] = 32;
1108c2ecf20Sopenharmony_ci	else if (max_buffer_size[dev] > 1024)
1118c2ecf20Sopenharmony_ci		max_buffer_size[dev] = 1024;
1128c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_create(card, pci, extin[dev], extout[dev],
1138c2ecf20Sopenharmony_ci				      (long)max_buffer_size[dev] * 1024 * 1024,
1148c2ecf20Sopenharmony_ci				      enable_ir[dev], subsystem[dev],
1158c2ecf20Sopenharmony_ci				      &emu)) < 0)
1168c2ecf20Sopenharmony_ci		goto error;
1178c2ecf20Sopenharmony_ci	card->private_data = emu;
1188c2ecf20Sopenharmony_ci	emu->delay_pcm_irq = delay_pcm_irq[dev] & 0x1f;
1198c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_pcm(emu, 0)) < 0)
1208c2ecf20Sopenharmony_ci		goto error;
1218c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_pcm_mic(emu, 1)) < 0)
1228c2ecf20Sopenharmony_ci		goto error;
1238c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_pcm_efx(emu, 2)) < 0)
1248c2ecf20Sopenharmony_ci		goto error;
1258c2ecf20Sopenharmony_ci	/* This stores the periods table. */
1268c2ecf20Sopenharmony_ci	if (emu->card_capabilities->ca0151_chip) { /* P16V */
1278c2ecf20Sopenharmony_ci		err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
1288c2ecf20Sopenharmony_ci					  1024, &emu->p16v_buffer);
1298c2ecf20Sopenharmony_ci		if (err < 0)
1308c2ecf20Sopenharmony_ci			goto error;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_mixer(emu, 0, 3)) < 0)
1348c2ecf20Sopenharmony_ci		goto error;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_timer(emu, 0)) < 0)
1378c2ecf20Sopenharmony_ci		goto error;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_pcm_multi(emu, 3)) < 0)
1408c2ecf20Sopenharmony_ci		goto error;
1418c2ecf20Sopenharmony_ci	if (emu->card_capabilities->ca0151_chip) { /* P16V */
1428c2ecf20Sopenharmony_ci		if ((err = snd_p16v_pcm(emu, 4)) < 0)
1438c2ecf20Sopenharmony_ci			goto error;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	if (emu->audigy) {
1468c2ecf20Sopenharmony_ci		if ((err = snd_emu10k1_audigy_midi(emu)) < 0)
1478c2ecf20Sopenharmony_ci			goto error;
1488c2ecf20Sopenharmony_ci	} else {
1498c2ecf20Sopenharmony_ci		if ((err = snd_emu10k1_midi(emu)) < 0)
1508c2ecf20Sopenharmony_ci			goto error;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci	if ((err = snd_emu10k1_fx8010_new(emu, 0)) < 0)
1538c2ecf20Sopenharmony_ci		goto error;
1548c2ecf20Sopenharmony_ci#ifdef ENABLE_SYNTH
1558c2ecf20Sopenharmony_ci	if (snd_seq_device_new(card, 1, SNDRV_SEQ_DEV_ID_EMU10K1_SYNTH,
1568c2ecf20Sopenharmony_ci			       sizeof(struct snd_emu10k1_synth_arg), &wave) < 0 ||
1578c2ecf20Sopenharmony_ci	    wave == NULL) {
1588c2ecf20Sopenharmony_ci		dev_warn(emu->card->dev,
1598c2ecf20Sopenharmony_ci			 "can't initialize Emu10k1 wavetable synth\n");
1608c2ecf20Sopenharmony_ci	} else {
1618c2ecf20Sopenharmony_ci		struct snd_emu10k1_synth_arg *arg;
1628c2ecf20Sopenharmony_ci		arg = SNDRV_SEQ_DEVICE_ARGPTR(wave);
1638c2ecf20Sopenharmony_ci		strcpy(wave->name, "Emu-10k1 Synth");
1648c2ecf20Sopenharmony_ci		arg->hwptr = emu;
1658c2ecf20Sopenharmony_ci		arg->index = 1;
1668c2ecf20Sopenharmony_ci		arg->seq_ports = seq_ports[dev];
1678c2ecf20Sopenharmony_ci		arg->max_voices = max_synth_voices[dev];
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci#endif
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	strlcpy(card->driver, emu->card_capabilities->driver,
1728c2ecf20Sopenharmony_ci		sizeof(card->driver));
1738c2ecf20Sopenharmony_ci	strlcpy(card->shortname, emu->card_capabilities->name,
1748c2ecf20Sopenharmony_ci		sizeof(card->shortname));
1758c2ecf20Sopenharmony_ci	snprintf(card->longname, sizeof(card->longname),
1768c2ecf20Sopenharmony_ci		 "%s (rev.%d, serial:0x%x) at 0x%lx, irq %i",
1778c2ecf20Sopenharmony_ci		 card->shortname, emu->revision, emu->serial, emu->port, emu->irq);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if ((err = snd_card_register(card)) < 0)
1808c2ecf20Sopenharmony_ci		goto error;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (emu->card_capabilities->emu_model)
1838c2ecf20Sopenharmony_ci		schedule_delayed_work(&emu->emu1010.firmware_work, 0);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
1868c2ecf20Sopenharmony_ci	dev++;
1878c2ecf20Sopenharmony_ci	return 0;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci error:
1908c2ecf20Sopenharmony_ci	snd_card_free(card);
1918c2ecf20Sopenharmony_ci	return err;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic void snd_card_emu10k1_remove(struct pci_dev *pci)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2018c2ecf20Sopenharmony_cistatic int snd_emu10k1_suspend(struct device *dev)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
2048c2ecf20Sopenharmony_ci	struct snd_emu10k1 *emu = card->private_data;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	emu->suspend = 1;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&emu->emu1010.firmware_work);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	snd_ac97_suspend(emu->ac97);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	snd_emu10k1_efx_suspend(emu);
2158c2ecf20Sopenharmony_ci	snd_emu10k1_suspend_regs(emu);
2168c2ecf20Sopenharmony_ci	if (emu->card_capabilities->ca0151_chip)
2178c2ecf20Sopenharmony_ci		snd_p16v_suspend(emu);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	snd_emu10k1_done(emu);
2208c2ecf20Sopenharmony_ci	return 0;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int snd_emu10k1_resume(struct device *dev)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
2268c2ecf20Sopenharmony_ci	struct snd_emu10k1 *emu = card->private_data;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	snd_emu10k1_resume_init(emu);
2298c2ecf20Sopenharmony_ci	snd_emu10k1_efx_resume(emu);
2308c2ecf20Sopenharmony_ci	snd_ac97_resume(emu->ac97);
2318c2ecf20Sopenharmony_ci	snd_emu10k1_resume_regs(emu);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (emu->card_capabilities->ca0151_chip)
2348c2ecf20Sopenharmony_ci		snd_p16v_resume(emu);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	emu->suspend = 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (emu->card_capabilities->emu_model)
2418c2ecf20Sopenharmony_ci		schedule_delayed_work(&emu->emu1010.firmware_work, 0);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(snd_emu10k1_pm, snd_emu10k1_suspend, snd_emu10k1_resume);
2478c2ecf20Sopenharmony_ci#define SND_EMU10K1_PM_OPS	&snd_emu10k1_pm
2488c2ecf20Sopenharmony_ci#else
2498c2ecf20Sopenharmony_ci#define SND_EMU10K1_PM_OPS	NULL
2508c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic struct pci_driver emu10k1_driver = {
2538c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
2548c2ecf20Sopenharmony_ci	.id_table = snd_emu10k1_ids,
2558c2ecf20Sopenharmony_ci	.probe = snd_card_emu10k1_probe,
2568c2ecf20Sopenharmony_ci	.remove = snd_card_emu10k1_remove,
2578c2ecf20Sopenharmony_ci	.driver = {
2588c2ecf20Sopenharmony_ci		.pm = SND_EMU10K1_PM_OPS,
2598c2ecf20Sopenharmony_ci	},
2608c2ecf20Sopenharmony_ci};
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cimodule_pci_driver(emu10k1_driver);
263