18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Generic driver for CS4231 chips
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
58c2ecf20Sopenharmony_ci *  Originally the CS4232/CS4232A driver, modified for use on CS4231 by
68c2ecf20Sopenharmony_ci *  Tugrul Galatali <galatalt@stuy.edu>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/isa.h>
128c2ecf20Sopenharmony_ci#include <linux/time.h>
138c2ecf20Sopenharmony_ci#include <linux/wait.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <sound/core.h>
168c2ecf20Sopenharmony_ci#include <sound/wss.h>
178c2ecf20Sopenharmony_ci#include <sound/mpu401.h>
188c2ecf20Sopenharmony_ci#include <sound/initval.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define CRD_NAME "Generic CS4231"
218c2ecf20Sopenharmony_ci#define DEV_NAME "cs4231"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(CRD_NAME);
248c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
268c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Crystal Semiconductors,CS4231}}");
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
298c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
308c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;	/* Enable this card */
318c2ecf20Sopenharmony_cistatic long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
328c2ecf20Sopenharmony_cistatic long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
338c2ecf20Sopenharmony_cistatic int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,11,12,15 */
348c2ecf20Sopenharmony_cistatic int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 9,11,12,15 */
358c2ecf20Sopenharmony_cistatic int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3,5,6,7 */
368c2ecf20Sopenharmony_cistatic int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3,5,6,7 */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
398c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
408c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
418c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
428c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
438c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
448c2ecf20Sopenharmony_cimodule_param_hw_array(port, long, ioport, NULL, 0444);
458c2ecf20Sopenharmony_ciMODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
468c2ecf20Sopenharmony_cimodule_param_hw_array(mpu_port, long, ioport, NULL, 0444);
478c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
488c2ecf20Sopenharmony_cimodule_param_hw_array(irq, int, irq, NULL, 0444);
498c2ecf20Sopenharmony_ciMODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
508c2ecf20Sopenharmony_cimodule_param_hw_array(mpu_irq, int, irq, NULL, 0444);
518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
528c2ecf20Sopenharmony_cimodule_param_hw_array(dma1, int, dma, NULL, 0444);
538c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dma1, "DMA1 # for " CRD_NAME " driver.");
548c2ecf20Sopenharmony_cimodule_param_hw_array(dma2, int, dma, NULL, 0444);
558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dma2, "DMA2 # for " CRD_NAME " driver.");
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int snd_cs4231_match(struct device *dev, unsigned int n)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	if (!enable[n])
608c2ecf20Sopenharmony_ci		return 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (port[n] == SNDRV_AUTO_PORT) {
638c2ecf20Sopenharmony_ci		dev_err(dev, "please specify port\n");
648c2ecf20Sopenharmony_ci		return 0;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci	if (irq[n] == SNDRV_AUTO_IRQ) {
678c2ecf20Sopenharmony_ci		dev_err(dev, "please specify irq\n");
688c2ecf20Sopenharmony_ci		return 0;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci	if (dma1[n] == SNDRV_AUTO_DMA) {
718c2ecf20Sopenharmony_ci		dev_err(dev, "please specify dma1\n");
728c2ecf20Sopenharmony_ci		return 0;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci	return 1;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int snd_cs4231_probe(struct device *dev, unsigned int n)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct snd_card *card;
808c2ecf20Sopenharmony_ci	struct snd_wss *chip;
818c2ecf20Sopenharmony_ci	int error;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	error = snd_card_new(dev, index[n], id[n], THIS_MODULE, 0, &card);
848c2ecf20Sopenharmony_ci	if (error < 0)
858c2ecf20Sopenharmony_ci		return error;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	error = snd_wss_create(card, port[n], -1, irq[n], dma1[n], dma2[n],
888c2ecf20Sopenharmony_ci			WSS_HW_DETECT, 0, &chip);
898c2ecf20Sopenharmony_ci	if (error < 0)
908c2ecf20Sopenharmony_ci		goto out;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	card->private_data = chip;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	error = snd_wss_pcm(chip, 0);
958c2ecf20Sopenharmony_ci	if (error < 0)
968c2ecf20Sopenharmony_ci		goto out;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	strlcpy(card->driver, "CS4231", sizeof(card->driver));
998c2ecf20Sopenharmony_ci	strlcpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (dma2[n] < 0)
1028c2ecf20Sopenharmony_ci		snprintf(card->longname, sizeof(card->longname),
1038c2ecf20Sopenharmony_ci			 "%s at 0x%lx, irq %d, dma %d",
1048c2ecf20Sopenharmony_ci			 chip->pcm->name, chip->port, irq[n], dma1[n]);
1058c2ecf20Sopenharmony_ci	else
1068c2ecf20Sopenharmony_ci		snprintf(card->longname, sizeof(card->longname),
1078c2ecf20Sopenharmony_ci			 "%s at 0x%lx, irq %d, dma %d&%d",
1088c2ecf20Sopenharmony_ci			 chip->pcm->name, chip->port, irq[n], dma1[n], dma2[n]);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	error = snd_wss_mixer(chip);
1118c2ecf20Sopenharmony_ci	if (error < 0)
1128c2ecf20Sopenharmony_ci		goto out;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	error = snd_wss_timer(chip, 0);
1158c2ecf20Sopenharmony_ci	if (error < 0)
1168c2ecf20Sopenharmony_ci		goto out;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (mpu_port[n] > 0 && mpu_port[n] != SNDRV_AUTO_PORT) {
1198c2ecf20Sopenharmony_ci		if (mpu_irq[n] == SNDRV_AUTO_IRQ)
1208c2ecf20Sopenharmony_ci			mpu_irq[n] = -1;
1218c2ecf20Sopenharmony_ci		if (snd_mpu401_uart_new(card, 0, MPU401_HW_CS4232,
1228c2ecf20Sopenharmony_ci					mpu_port[n], 0, mpu_irq[n],
1238c2ecf20Sopenharmony_ci					NULL) < 0)
1248c2ecf20Sopenharmony_ci			dev_warn(dev, "MPU401 not detected\n");
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	error = snd_card_register(card);
1288c2ecf20Sopenharmony_ci	if (error < 0)
1298c2ecf20Sopenharmony_ci		goto out;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, card);
1328c2ecf20Sopenharmony_ci	return 0;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciout:	snd_card_free(card);
1358c2ecf20Sopenharmony_ci	return error;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int snd_cs4231_remove(struct device *dev, unsigned int n)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	snd_card_free(dev_get_drvdata(dev));
1418c2ecf20Sopenharmony_ci	return 0;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
1458c2ecf20Sopenharmony_cistatic int snd_cs4231_suspend(struct device *dev, unsigned int n, pm_message_t state)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
1488c2ecf20Sopenharmony_ci	struct snd_wss *chip = card->private_data;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1518c2ecf20Sopenharmony_ci	chip->suspend(chip);
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int snd_cs4231_resume(struct device *dev, unsigned int n)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
1588c2ecf20Sopenharmony_ci	struct snd_wss *chip = card->private_data;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	chip->resume(chip);
1618c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci#endif
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic struct isa_driver snd_cs4231_driver = {
1678c2ecf20Sopenharmony_ci	.match		= snd_cs4231_match,
1688c2ecf20Sopenharmony_ci	.probe		= snd_cs4231_probe,
1698c2ecf20Sopenharmony_ci	.remove		= snd_cs4231_remove,
1708c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
1718c2ecf20Sopenharmony_ci	.suspend	= snd_cs4231_suspend,
1728c2ecf20Sopenharmony_ci	.resume		= snd_cs4231_resume,
1738c2ecf20Sopenharmony_ci#endif
1748c2ecf20Sopenharmony_ci	.driver		= {
1758c2ecf20Sopenharmony_ci		.name	= DEV_NAME
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cimodule_isa_driver(snd_cs4231_driver, SNDRV_CARDS);
180