18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
48c2ecf20Sopenharmony_ci *  Routines for control of ICS 2101 chip and "mixer" in GF1 chip
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/time.h>
88c2ecf20Sopenharmony_ci#include <linux/wait.h>
98c2ecf20Sopenharmony_ci#include <sound/core.h>
108c2ecf20Sopenharmony_ci#include <sound/control.h>
118c2ecf20Sopenharmony_ci#include <sound/gus.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define GF1_SINGLE(xname, xindex, shift, invert) \
188c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
198c2ecf20Sopenharmony_ci  .info = snd_gf1_info_single, \
208c2ecf20Sopenharmony_ci  .get = snd_gf1_get_single, .put = snd_gf1_put_single, \
218c2ecf20Sopenharmony_ci  .private_value = shift | (invert << 8) }
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define snd_gf1_info_single	snd_ctl_boolean_mono_info
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int snd_gf1_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
288c2ecf20Sopenharmony_ci	int shift = kcontrol->private_value & 0xff;
298c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 8) & 1;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (gus->mix_cntrl_reg >> shift) & 1;
328c2ecf20Sopenharmony_ci	if (invert)
338c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[0] ^= 1;
348c2ecf20Sopenharmony_ci	return 0;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int snd_gf1_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
408c2ecf20Sopenharmony_ci	unsigned long flags;
418c2ecf20Sopenharmony_ci	int shift = kcontrol->private_value & 0xff;
428c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 8) & 1;
438c2ecf20Sopenharmony_ci	int change;
448c2ecf20Sopenharmony_ci	unsigned char oval, nval;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	nval = ucontrol->value.integer.value[0] & 1;
478c2ecf20Sopenharmony_ci	if (invert)
488c2ecf20Sopenharmony_ci		nval ^= 1;
498c2ecf20Sopenharmony_ci	nval <<= shift;
508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
518c2ecf20Sopenharmony_ci	oval = gus->mix_cntrl_reg;
528c2ecf20Sopenharmony_ci	nval = (oval & ~(1 << shift)) | nval;
538c2ecf20Sopenharmony_ci	change = nval != oval;
548c2ecf20Sopenharmony_ci	outb(gus->mix_cntrl_reg = nval, GUSP(gus, MIXCNTRLREG));
558c2ecf20Sopenharmony_ci	outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
578c2ecf20Sopenharmony_ci	return change;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define ICS_DOUBLE(xname, xindex, addr) \
618c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
628c2ecf20Sopenharmony_ci  .info = snd_ics_info_double, \
638c2ecf20Sopenharmony_ci  .get = snd_ics_get_double, .put = snd_ics_put_double, \
648c2ecf20Sopenharmony_ci  .private_value = addr }
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int snd_ics_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
698c2ecf20Sopenharmony_ci	uinfo->count = 2;
708c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
718c2ecf20Sopenharmony_ci	uinfo->value.integer.max = 127;
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int snd_ics_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
788c2ecf20Sopenharmony_ci	unsigned long flags;
798c2ecf20Sopenharmony_ci	int addr = kcontrol->private_value & 0xff;
808c2ecf20Sopenharmony_ci	unsigned char left, right;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
838c2ecf20Sopenharmony_ci	left = gus->gf1.ics_regs[addr][0];
848c2ecf20Sopenharmony_ci	right = gus->gf1.ics_regs[addr][1];
858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
868c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = left & 127;
878c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[1] = right & 127;
888c2ecf20Sopenharmony_ci	return 0;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic int snd_ics_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
948c2ecf20Sopenharmony_ci	unsigned long flags;
958c2ecf20Sopenharmony_ci	int addr = kcontrol->private_value & 0xff;
968c2ecf20Sopenharmony_ci	int change;
978c2ecf20Sopenharmony_ci	unsigned char val1, val2, oval1, oval2;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	val1 = ucontrol->value.integer.value[0] & 127;
1008c2ecf20Sopenharmony_ci	val2 = ucontrol->value.integer.value[1] & 127;
1018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&gus->reg_lock, flags);
1028c2ecf20Sopenharmony_ci	oval1 = gus->gf1.ics_regs[addr][0];
1038c2ecf20Sopenharmony_ci	oval2 = gus->gf1.ics_regs[addr][1];
1048c2ecf20Sopenharmony_ci	change = val1 != oval1 || val2 != oval2;
1058c2ecf20Sopenharmony_ci	gus->gf1.ics_regs[addr][0] = val1;
1068c2ecf20Sopenharmony_ci	gus->gf1.ics_regs[addr][1] = val2;
1078c2ecf20Sopenharmony_ci	if (gus->ics_flag && gus->ics_flipped &&
1088c2ecf20Sopenharmony_ci	    (addr == SNDRV_ICS_GF1_DEV || addr == SNDRV_ICS_MASTER_DEV))
1098c2ecf20Sopenharmony_ci		swap(val1, val2);
1108c2ecf20Sopenharmony_ci	addr <<= 3;
1118c2ecf20Sopenharmony_ci	outb(addr | 0, GUSP(gus, MIXCNTRLPORT));
1128c2ecf20Sopenharmony_ci	outb(1, GUSP(gus, MIXDATAPORT));
1138c2ecf20Sopenharmony_ci	outb(addr | 2, GUSP(gus, MIXCNTRLPORT));
1148c2ecf20Sopenharmony_ci	outb((unsigned char) val1, GUSP(gus, MIXDATAPORT));
1158c2ecf20Sopenharmony_ci	outb(addr | 1, GUSP(gus, MIXCNTRLPORT));
1168c2ecf20Sopenharmony_ci	outb(2, GUSP(gus, MIXDATAPORT));
1178c2ecf20Sopenharmony_ci	outb(addr | 3, GUSP(gus, MIXCNTRLPORT));
1188c2ecf20Sopenharmony_ci	outb((unsigned char) val2, GUSP(gus, MIXDATAPORT));
1198c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&gus->reg_lock, flags);
1208c2ecf20Sopenharmony_ci	return change;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_gf1_controls[] = {
1248c2ecf20Sopenharmony_ciGF1_SINGLE("Master Playback Switch", 0, 1, 1),
1258c2ecf20Sopenharmony_ciGF1_SINGLE("Line Switch", 0, 0, 1),
1268c2ecf20Sopenharmony_ciGF1_SINGLE("Mic Switch", 0, 2, 0)
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_ics_controls[] = {
1308c2ecf20Sopenharmony_ciGF1_SINGLE("Master Playback Switch", 0, 1, 1),
1318c2ecf20Sopenharmony_ciICS_DOUBLE("Master Playback Volume", 0, SNDRV_ICS_MASTER_DEV),
1328c2ecf20Sopenharmony_ciICS_DOUBLE("Synth Playback Volume", 0, SNDRV_ICS_GF1_DEV),
1338c2ecf20Sopenharmony_ciGF1_SINGLE("Line Switch", 0, 0, 1),
1348c2ecf20Sopenharmony_ciICS_DOUBLE("Line Playback Volume", 0, SNDRV_ICS_LINE_DEV),
1358c2ecf20Sopenharmony_ciGF1_SINGLE("Mic Switch", 0, 2, 0),
1368c2ecf20Sopenharmony_ciICS_DOUBLE("Mic Playback Volume", 0, SNDRV_ICS_MIC_DEV),
1378c2ecf20Sopenharmony_ciICS_DOUBLE("CD Playback Volume", 0, SNDRV_ICS_CD_DEV)
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciint snd_gf1_new_mixer(struct snd_gus_card * gus)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct snd_card *card;
1438c2ecf20Sopenharmony_ci	unsigned int idx, max;
1448c2ecf20Sopenharmony_ci	int err;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!gus))
1478c2ecf20Sopenharmony_ci		return -EINVAL;
1488c2ecf20Sopenharmony_ci	card = gus->card;
1498c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!card))
1508c2ecf20Sopenharmony_ci		return -EINVAL;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (gus->ics_flag)
1538c2ecf20Sopenharmony_ci		snd_component_add(card, "ICS2101");
1548c2ecf20Sopenharmony_ci	if (card->mixername[0] == '\0') {
1558c2ecf20Sopenharmony_ci		strcpy(card->mixername, gus->ics_flag ? "GF1,ICS2101" : "GF1");
1568c2ecf20Sopenharmony_ci	} else {
1578c2ecf20Sopenharmony_ci		if (gus->ics_flag)
1588c2ecf20Sopenharmony_ci			strcat(card->mixername, ",ICS2101");
1598c2ecf20Sopenharmony_ci		strcat(card->mixername, ",GF1");
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (!gus->ics_flag) {
1638c2ecf20Sopenharmony_ci		max = gus->ess_flag ? 1 : ARRAY_SIZE(snd_gf1_controls);
1648c2ecf20Sopenharmony_ci		for (idx = 0; idx < max; idx++) {
1658c2ecf20Sopenharmony_ci			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_gf1_controls[idx], gus))) < 0)
1668c2ecf20Sopenharmony_ci				return err;
1678c2ecf20Sopenharmony_ci		}
1688c2ecf20Sopenharmony_ci	} else {
1698c2ecf20Sopenharmony_ci		for (idx = 0; idx < ARRAY_SIZE(snd_ics_controls); idx++) {
1708c2ecf20Sopenharmony_ci			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ics_controls[idx], gus))) < 0)
1718c2ecf20Sopenharmony_ci				return err;
1728c2ecf20Sopenharmony_ci		}
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci}
176