| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
| 4 | * Routines for control of ICS 2101 chip and "mixer" in GF1 chip |
| 5 | */ |
| 6 | |
| 7 | #include <linux/time.h> |
| 8 | #include <linux/wait.h> |
| 9 | #include <sound/core.h> |
| 10 | #include <sound/control.h> |
| 11 | #include <sound/gus.h> |
| 12 | |
| 13 | /* |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #define GF1_SINGLE(xname, xindex, shift, invert) \ |
| 18 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 19 | .info = snd_gf1_info_single, \ |
| 20 | .get = snd_gf1_get_single, .put = snd_gf1_put_single, \ |
| 21 | .private_value = shift | (invert << 8) } |
| 22 | |
| 23 | #define snd_gf1_info_single snd_ctl_boolean_mono_info |
| 24 | |
| 25 | static int snd_gf1_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 26 | { |
| 27 | struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); |
| 28 | int shift = kcontrol->private_value & 0xff; |
| 29 | int invert = (kcontrol->private_value >> 8) & 1; |
| 30 | |
| 31 | ucontrol->value.integer.value[0] = (gus->mix_cntrl_reg >> shift) & 1; |
| 32 | if (invert) |
| 33 | ucontrol->value.integer.value[0] ^= 1; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int snd_gf1_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 38 | { |
| 39 | struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); |
| 40 | int shift = kcontrol->private_value & 0xff; |
| 41 | int invert = (kcontrol->private_value >> 8) & 1; |
| 42 | int change; |
| 43 | unsigned char oval, nval; |
| 44 | |
| 45 | nval = ucontrol->value.integer.value[0] & 1; |
| 46 | if (invert) |
| 47 | nval ^= 1; |
| 48 | nval <<= shift; |
| 49 | guard(spinlock_irqsave)(l: &gus->reg_lock); |
| 50 | oval = gus->mix_cntrl_reg; |
| 51 | nval = (oval & ~(1 << shift)) | nval; |
| 52 | change = nval != oval; |
| 53 | outb(value: gus->mix_cntrl_reg = nval, GUSP(gus, MIXCNTRLREG)); |
| 54 | outb(value: gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE)); |
| 55 | return change; |
| 56 | } |
| 57 | |
| 58 | #define ICS_DOUBLE(xname, xindex, addr) \ |
| 59 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 60 | .info = snd_ics_info_double, \ |
| 61 | .get = snd_ics_get_double, .put = snd_ics_put_double, \ |
| 62 | .private_value = addr } |
| 63 | |
| 64 | static int snd_ics_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
| 65 | { |
| 66 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 67 | uinfo->count = 2; |
| 68 | uinfo->value.integer.min = 0; |
| 69 | uinfo->value.integer.max = 127; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int snd_ics_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 74 | { |
| 75 | struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); |
| 76 | int addr = kcontrol->private_value & 0xff; |
| 77 | unsigned char left, right; |
| 78 | |
| 79 | guard(spinlock_irqsave)(l: &gus->reg_lock); |
| 80 | left = gus->gf1.ics_regs[addr][0]; |
| 81 | right = gus->gf1.ics_regs[addr][1]; |
| 82 | ucontrol->value.integer.value[0] = left & 127; |
| 83 | ucontrol->value.integer.value[1] = right & 127; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int snd_ics_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
| 88 | { |
| 89 | struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol); |
| 90 | int addr = kcontrol->private_value & 0xff; |
| 91 | int change; |
| 92 | unsigned char val1, val2, oval1, oval2; |
| 93 | |
| 94 | val1 = ucontrol->value.integer.value[0] & 127; |
| 95 | val2 = ucontrol->value.integer.value[1] & 127; |
| 96 | guard(spinlock_irqsave)(l: &gus->reg_lock); |
| 97 | oval1 = gus->gf1.ics_regs[addr][0]; |
| 98 | oval2 = gus->gf1.ics_regs[addr][1]; |
| 99 | change = val1 != oval1 || val2 != oval2; |
| 100 | gus->gf1.ics_regs[addr][0] = val1; |
| 101 | gus->gf1.ics_regs[addr][1] = val2; |
| 102 | if (gus->ics_flag && gus->ics_flipped && |
| 103 | (addr == SNDRV_ICS_GF1_DEV || addr == SNDRV_ICS_MASTER_DEV)) |
| 104 | swap(val1, val2); |
| 105 | addr <<= 3; |
| 106 | outb(value: addr | 0, GUSP(gus, MIXCNTRLPORT)); |
| 107 | outb(value: 1, GUSP(gus, MIXDATAPORT)); |
| 108 | outb(value: addr | 2, GUSP(gus, MIXCNTRLPORT)); |
| 109 | outb(value: (unsigned char) val1, GUSP(gus, MIXDATAPORT)); |
| 110 | outb(value: addr | 1, GUSP(gus, MIXCNTRLPORT)); |
| 111 | outb(value: 2, GUSP(gus, MIXDATAPORT)); |
| 112 | outb(value: addr | 3, GUSP(gus, MIXCNTRLPORT)); |
| 113 | outb(value: (unsigned char) val2, GUSP(gus, MIXDATAPORT)); |
| 114 | return change; |
| 115 | } |
| 116 | |
| 117 | static const struct snd_kcontrol_new snd_gf1_controls[] = { |
| 118 | GF1_SINGLE("Master Playback Switch" , 0, 1, 1), |
| 119 | GF1_SINGLE("Line Switch" , 0, 0, 1), |
| 120 | GF1_SINGLE("Mic Switch" , 0, 2, 0) |
| 121 | }; |
| 122 | |
| 123 | static const struct snd_kcontrol_new snd_ics_controls[] = { |
| 124 | GF1_SINGLE("Master Playback Switch" , 0, 1, 1), |
| 125 | ICS_DOUBLE("Master Playback Volume" , 0, SNDRV_ICS_MASTER_DEV), |
| 126 | ICS_DOUBLE("Synth Playback Volume" , 0, SNDRV_ICS_GF1_DEV), |
| 127 | GF1_SINGLE("Line Switch" , 0, 0, 1), |
| 128 | ICS_DOUBLE("Line Playback Volume" , 0, SNDRV_ICS_LINE_DEV), |
| 129 | GF1_SINGLE("Mic Switch" , 0, 2, 0), |
| 130 | ICS_DOUBLE("Mic Playback Volume" , 0, SNDRV_ICS_MIC_DEV), |
| 131 | ICS_DOUBLE("CD Playback Volume" , 0, SNDRV_ICS_CD_DEV) |
| 132 | }; |
| 133 | |
| 134 | int snd_gf1_new_mixer(struct snd_gus_card * gus) |
| 135 | { |
| 136 | struct snd_card *card; |
| 137 | unsigned int idx, max; |
| 138 | int err; |
| 139 | |
| 140 | if (snd_BUG_ON(!gus)) |
| 141 | return -EINVAL; |
| 142 | card = gus->card; |
| 143 | if (snd_BUG_ON(!card)) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | if (gus->ics_flag) |
| 147 | snd_component_add(card, component: "ICS2101" ); |
| 148 | if (card->mixername[0] == '\0') { |
| 149 | strscpy(card->mixername, gus->ics_flag ? "GF1,ICS2101" : "GF1" ); |
| 150 | } else { |
| 151 | if (gus->ics_flag) |
| 152 | strcat(p: card->mixername, q: ",ICS2101" ); |
| 153 | strcat(p: card->mixername, q: ",GF1" ); |
| 154 | } |
| 155 | |
| 156 | if (!gus->ics_flag) { |
| 157 | max = gus->ess_flag ? 1 : ARRAY_SIZE(snd_gf1_controls); |
| 158 | for (idx = 0; idx < max; idx++) { |
| 159 | err = snd_ctl_add(card, kcontrol: snd_ctl_new1(kcontrolnew: &snd_gf1_controls[idx], private_data: gus)); |
| 160 | if (err < 0) |
| 161 | return err; |
| 162 | } |
| 163 | } else { |
| 164 | for (idx = 0; idx < ARRAY_SIZE(snd_ics_controls); idx++) { |
| 165 | err = snd_ctl_add(card, kcontrol: snd_ctl_new1(kcontrolnew: &snd_ics_controls[idx], private_data: gus)); |
| 166 | if (err < 0) |
| 167 | return err; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |