18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  The driver for the Cirrus Logic's Sound Fusion CS46XX based soundcards
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci  NOTES:
98c2ecf20Sopenharmony_ci  - sometimes the sound is metallic and sibilant, unloading and
108c2ecf20Sopenharmony_ci    reloading the module may solve this.
118c2ecf20Sopenharmony_ci*/
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/time.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <sound/core.h>
188c2ecf20Sopenharmony_ci#include "cs46xx.h"
198c2ecf20Sopenharmony_ci#include <sound/initval.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
228c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cirrus Logic Sound Fusion CS46XX");
238c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
248c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Cirrus Logic,Sound Fusion (CS4280)},"
258c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4610)},"
268c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4612)},"
278c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4615)},"
288c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4622)},"
298c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4624)},"
308c2ecf20Sopenharmony_ci		"{Cirrus Logic,Sound Fusion (CS4630)}}");
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
338c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
348c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
358c2ecf20Sopenharmony_cistatic bool external_amp[SNDRV_CARDS];
368c2ecf20Sopenharmony_cistatic bool thinkpad[SNDRV_CARDS];
378c2ecf20Sopenharmony_cistatic bool mmap_valid[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for the CS46xx soundcard.");
418c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for the CS46xx soundcard.");
438c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable CS46xx soundcard.");
458c2ecf20Sopenharmony_cimodule_param_array(external_amp, bool, NULL, 0444);
468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(external_amp, "Force to enable external amplifier.");
478c2ecf20Sopenharmony_cimodule_param_array(thinkpad, bool, NULL, 0444);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(thinkpad, "Force to enable Thinkpad's CLKRUN control.");
498c2ecf20Sopenharmony_cimodule_param_array(mmap_valid, bool, NULL, 0444);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mmap_valid, "Support OSS mmap.");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_cs46xx_ids[] = {
538c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CIRRUS, 0x6001), 0, },   /* CS4280 */
548c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CIRRUS, 0x6003), 0, },   /* CS4612 */
558c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CIRRUS, 0x6004), 0, },   /* CS4615 */
568c2ecf20Sopenharmony_ci	{ 0, }
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_cs46xx_ids);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int snd_card_cs46xx_probe(struct pci_dev *pci,
628c2ecf20Sopenharmony_ci				 const struct pci_device_id *pci_id)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	static int dev;
658c2ecf20Sopenharmony_ci	struct snd_card *card;
668c2ecf20Sopenharmony_ci	struct snd_cs46xx *chip;
678c2ecf20Sopenharmony_ci	int err;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
708c2ecf20Sopenharmony_ci		return -ENODEV;
718c2ecf20Sopenharmony_ci	if (!enable[dev]) {
728c2ecf20Sopenharmony_ci		dev++;
738c2ecf20Sopenharmony_ci		return -ENOENT;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
778c2ecf20Sopenharmony_ci			   0, &card);
788c2ecf20Sopenharmony_ci	if (err < 0)
798c2ecf20Sopenharmony_ci		return err;
808c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_create(card, pci,
818c2ecf20Sopenharmony_ci				     external_amp[dev], thinkpad[dev],
828c2ecf20Sopenharmony_ci				     &chip)) < 0) {
838c2ecf20Sopenharmony_ci		snd_card_free(card);
848c2ecf20Sopenharmony_ci		return err;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci	card->private_data = chip;
878c2ecf20Sopenharmony_ci	chip->accept_valid = mmap_valid[dev];
888c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_pcm(chip, 0)) < 0) {
898c2ecf20Sopenharmony_ci		snd_card_free(card);
908c2ecf20Sopenharmony_ci		return err;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_CS46XX_NEW_DSP
938c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_pcm_rear(chip, 1)) < 0) {
948c2ecf20Sopenharmony_ci		snd_card_free(card);
958c2ecf20Sopenharmony_ci		return err;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_pcm_iec958(chip, 2)) < 0) {
988c2ecf20Sopenharmony_ci		snd_card_free(card);
998c2ecf20Sopenharmony_ci		return err;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_mixer(chip, 2)) < 0) {
1038c2ecf20Sopenharmony_ci		snd_card_free(card);
1048c2ecf20Sopenharmony_ci		return err;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_CS46XX_NEW_DSP
1078c2ecf20Sopenharmony_ci	if (chip->nr_ac97_codecs ==2) {
1088c2ecf20Sopenharmony_ci		if ((err = snd_cs46xx_pcm_center_lfe(chip, 3)) < 0) {
1098c2ecf20Sopenharmony_ci			snd_card_free(card);
1108c2ecf20Sopenharmony_ci			return err;
1118c2ecf20Sopenharmony_ci		}
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci#endif
1148c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_midi(chip, 0)) < 0) {
1158c2ecf20Sopenharmony_ci		snd_card_free(card);
1168c2ecf20Sopenharmony_ci		return err;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	if ((err = snd_cs46xx_start_dsp(chip)) < 0) {
1198c2ecf20Sopenharmony_ci		snd_card_free(card);
1208c2ecf20Sopenharmony_ci		return err;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	snd_cs46xx_gameport(chip);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	strcpy(card->driver, "CS46xx");
1278c2ecf20Sopenharmony_ci	strcpy(card->shortname, "Sound Fusion CS46xx");
1288c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at 0x%lx/0x%lx, irq %i",
1298c2ecf20Sopenharmony_ci		card->shortname,
1308c2ecf20Sopenharmony_ci		chip->ba0_addr,
1318c2ecf20Sopenharmony_ci		chip->ba1_addr,
1328c2ecf20Sopenharmony_ci		chip->irq);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if ((err = snd_card_register(card)) < 0) {
1358c2ecf20Sopenharmony_ci		snd_card_free(card);
1368c2ecf20Sopenharmony_ci		return err;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
1408c2ecf20Sopenharmony_ci	dev++;
1418c2ecf20Sopenharmony_ci	return 0;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic void snd_card_cs46xx_remove(struct pci_dev *pci)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic struct pci_driver cs46xx_driver = {
1508c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
1518c2ecf20Sopenharmony_ci	.id_table = snd_cs46xx_ids,
1528c2ecf20Sopenharmony_ci	.probe = snd_card_cs46xx_probe,
1538c2ecf20Sopenharmony_ci	.remove = snd_card_cs46xx_remove,
1548c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
1558c2ecf20Sopenharmony_ci	.driver = {
1568c2ecf20Sopenharmony_ci		.pm = &snd_cs46xx_pm,
1578c2ecf20Sopenharmony_ci	},
1588c2ecf20Sopenharmony_ci#endif
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cimodule_pci_driver(cs46xx_driver);
162