1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA driver for ICEnsemble VT1724 (Envy24HT)
5 * Lowlevel functions for Ego Sys Waveterminal 192M
7 * Copyright (c) 2006 Guedez Clement <klem.dev@gmail.com>
8 * Some functions are taken from the Prodigy192 driver
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <sound/core.h>
18 #include <sound/tlv.h>
19 #include <linux/slab.h>
27 /* rate change needs atomic mute/unmute of all dacs*/
28 struct mutex mute_mutex;
33 * 2*ADC 6*DAC no1 ringbuffer r/w on i2c bus
35 static inline void stac9460_put(struct snd_ice1712 *ice, int reg,
38 snd_vt1724_write_i2c(ice, STAC9460_I2C_ADDR, reg, val);
41 static inline unsigned char stac9460_get(struct snd_ice1712 *ice, int reg)
43 return snd_vt1724_read_i2c(ice, STAC9460_I2C_ADDR, reg);
47 * 2*ADC 2*DAC no2 ringbuffer r/w on i2c bus
49 static inline void stac9460_2_put(struct snd_ice1712 *ice, int reg,
52 snd_vt1724_write_i2c(ice, STAC9460_2_I2C_ADDR, reg, val);
55 static inline unsigned char stac9460_2_get(struct snd_ice1712 *ice, int reg)
57 return snd_vt1724_read_i2c(ice, STAC9460_2_I2C_ADDR, reg);
64 static void stac9460_dac_mute_all(struct snd_ice1712 *ice, unsigned char mute,
65 unsigned short int *change_mask)
67 unsigned char new, old;
71 for (id = 0; id < 7; id++) {
72 if (*change_mask & (0x01 << id)) {
74 idx = STAC946X_MASTER_VOLUME;
76 idx = STAC946X_LF_VOLUME - 1 + id;
77 old = stac9460_get(ice, idx);
78 new = (~mute << 7 & 0x80) | (old & ~0x80);
79 change = (new != old);
81 stac9460_put(ice, idx, new);
82 *change_mask = *change_mask | (0x01 << id);
84 *change_mask = *change_mask & ~(0x01 << id);
90 for (id = 0; id < 3; id++) {
91 if (*change_mask & (0x01 << (id + 7))) {
93 idx = STAC946X_MASTER_VOLUME;
95 idx = STAC946X_LF_VOLUME - 1 + id;
96 old = stac9460_2_get(ice, idx);
97 new = (~mute << 7 & 0x80) | (old & ~0x80);
98 change = (new != old);
100 stac9460_2_put(ice, idx, new);
101 *change_mask = *change_mask | (0x01 << id);
103 *change_mask = *change_mask & ~(0x01 << id);
111 #define stac9460_dac_mute_info snd_ctl_boolean_mono_info
113 static int stac9460_dac_mute_get(struct snd_kcontrol *kcontrol,
114 struct snd_ctl_elem_value *ucontrol)
116 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
117 struct wtm_spec *spec = ice->spec;
121 mutex_lock(&spec->mute_mutex);
123 if (kcontrol->private_value) {
124 idx = STAC946X_MASTER_VOLUME;
127 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
128 idx = id + STAC946X_LF_VOLUME;
131 val = stac9460_get(ice, idx);
133 val = stac9460_2_get(ice, idx - 6);
134 ucontrol->value.integer.value[0] = (~val >> 7) & 0x1;
136 mutex_unlock(&spec->mute_mutex);
140 static int stac9460_dac_mute_put(struct snd_kcontrol *kcontrol,
141 struct snd_ctl_elem_value *ucontrol)
143 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
144 unsigned char new, old;
148 if (kcontrol->private_value) {
149 idx = STAC946X_MASTER_VOLUME;
150 old = stac9460_get(ice, idx);
151 new = (~ucontrol->value.integer.value[0] << 7 & 0x80) |
153 change = (new != old);
155 stac9460_put(ice, idx, new);
156 stac9460_2_put(ice, idx, new);
159 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
160 idx = id + STAC946X_LF_VOLUME;
162 old = stac9460_get(ice, idx);
164 old = stac9460_2_get(ice, idx - 6);
165 new = (~ucontrol->value.integer.value[0] << 7 & 0x80) |
167 change = (new != old);
170 stac9460_put(ice, idx, new);
172 stac9460_2_put(ice, idx - 6, new);
179 * DAC volume attenuation mixer control
181 static int stac9460_dac_vol_info(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_info *uinfo)
184 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
186 uinfo->value.integer.min = 0; /* mute */
187 uinfo->value.integer.max = 0x7f; /* 0dB */
191 static int stac9460_dac_vol_get(struct snd_kcontrol *kcontrol,
192 struct snd_ctl_elem_value *ucontrol)
194 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
198 if (kcontrol->private_value) {
199 idx = STAC946X_MASTER_VOLUME;
202 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
203 idx = id + STAC946X_LF_VOLUME;
206 vol = stac9460_get(ice, idx) & 0x7f;
208 vol = stac9460_2_get(ice, idx - 6) & 0x7f;
209 ucontrol->value.integer.value[0] = 0x7f - vol;
213 static int stac9460_dac_vol_put(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
216 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
218 unsigned char tmp, ovol, nvol;
221 if (kcontrol->private_value) {
222 idx = STAC946X_MASTER_VOLUME;
223 nvol = ucontrol->value.integer.value[0] & 0x7f;
224 tmp = stac9460_get(ice, idx);
225 ovol = 0x7f - (tmp & 0x7f);
226 change = (ovol != nvol);
228 stac9460_put(ice, idx, (0x7f - nvol) | (tmp & 0x80));
229 stac9460_2_put(ice, idx, (0x7f - nvol) | (tmp & 0x80));
232 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
233 idx = id + STAC946X_LF_VOLUME;
234 nvol = ucontrol->value.integer.value[0] & 0x7f;
236 tmp = stac9460_get(ice, idx);
238 tmp = stac9460_2_get(ice, idx - 6);
239 ovol = 0x7f - (tmp & 0x7f);
240 change = (ovol != nvol);
243 stac9460_put(ice, idx, (0x7f - nvol) |
246 stac9460_2_put(ice, idx-6, (0x7f - nvol) |
256 #define stac9460_adc_mute_info snd_ctl_boolean_stereo_info
258 static int stac9460_adc_mute_get(struct snd_kcontrol *kcontrol,
259 struct snd_ctl_elem_value *ucontrol)
261 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
265 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
267 for (i = 0; i < 2; ++i) {
268 val = stac9460_get(ice, STAC946X_MIC_L_VOLUME + i);
269 ucontrol->value.integer.value[i] = ~val>>7 & 0x1;
272 for (i = 0; i < 2; ++i) {
273 val = stac9460_2_get(ice, STAC946X_MIC_L_VOLUME + i);
274 ucontrol->value.integer.value[i] = ~val>>7 & 0x1;
280 static int stac9460_adc_mute_put(struct snd_kcontrol *kcontrol,
281 struct snd_ctl_elem_value *ucontrol)
283 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
284 unsigned char new, old;
288 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
290 for (i = 0; i < 2; ++i) {
291 reg = STAC946X_MIC_L_VOLUME + i;
292 old = stac9460_get(ice, reg);
293 new = (~ucontrol->value.integer.value[i]<<7&0x80) |
295 change = (new != old);
297 stac9460_put(ice, reg, new);
300 for (i = 0; i < 2; ++i) {
301 reg = STAC946X_MIC_L_VOLUME + i;
302 old = stac9460_2_get(ice, reg);
303 new = (~ucontrol->value.integer.value[i]<<7&0x80) |
305 change = (new != old);
307 stac9460_2_put(ice, reg, new);
314 *ADC gain mixer control
316 static int stac9460_adc_vol_info(struct snd_kcontrol *kcontrol,
317 struct snd_ctl_elem_info *uinfo)
319 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
321 uinfo->value.integer.min = 0; /* 0dB */
322 uinfo->value.integer.max = 0x0f; /* 22.5dB */
326 static int stac9460_adc_vol_get(struct snd_kcontrol *kcontrol,
327 struct snd_ctl_elem_value *ucontrol)
329 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
333 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
335 for (i = 0; i < 2; ++i) {
336 reg = STAC946X_MIC_L_VOLUME + i;
337 vol = stac9460_get(ice, reg) & 0x0f;
338 ucontrol->value.integer.value[i] = 0x0f - vol;
341 for (i = 0; i < 2; ++i) {
342 reg = STAC946X_MIC_L_VOLUME + i;
343 vol = stac9460_2_get(ice, reg) & 0x0f;
344 ucontrol->value.integer.value[i] = 0x0f - vol;
350 static int stac9460_adc_vol_put(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_value *ucontrol)
353 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
355 unsigned char ovol, nvol;
358 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
360 for (i = 0; i < 2; ++i) {
361 reg = STAC946X_MIC_L_VOLUME + i;
362 nvol = ucontrol->value.integer.value[i] & 0x0f;
363 ovol = 0x0f - stac9460_get(ice, reg);
364 change = ((ovol & 0x0f) != nvol);
366 stac9460_put(ice, reg, (0x0f - nvol) |
370 for (i = 0; i < 2; ++i) {
371 reg = STAC946X_MIC_L_VOLUME + i;
372 nvol = ucontrol->value.integer.value[i] & 0x0f;
373 ovol = 0x0f - stac9460_2_get(ice, reg);
374 change = ((ovol & 0x0f) != nvol);
376 stac9460_2_put(ice, reg, (0x0f - nvol) |
384 * MIC / LINE switch fonction
386 static int stac9460_mic_sw_info(struct snd_kcontrol *kcontrol,
387 struct snd_ctl_elem_info *uinfo)
389 static const char * const texts[2] = { "Line In", "Mic" };
391 return snd_ctl_enum_info(uinfo, 1, 2, texts);
395 static int stac9460_mic_sw_get(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *ucontrol)
398 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
402 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
404 val = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
406 val = stac9460_2_get(ice, STAC946X_GENERAL_PURPOSE);
407 ucontrol->value.enumerated.item[0] = (val >> 7) & 0x1;
411 static int stac9460_mic_sw_put(struct snd_kcontrol *kcontrol,
412 struct snd_ctl_elem_value *ucontrol)
414 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
415 unsigned char new, old;
418 id = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
420 old = stac9460_get(ice, STAC946X_GENERAL_PURPOSE);
422 old = stac9460_2_get(ice, STAC946X_GENERAL_PURPOSE);
423 new = (ucontrol->value.enumerated.item[0] << 7 & 0x80) | (old & ~0x80);
424 change = (new != old);
427 stac9460_put(ice, STAC946X_GENERAL_PURPOSE, new);
429 stac9460_2_put(ice, STAC946X_GENERAL_PURPOSE, new);
436 * Handler for setting correct codec rate - called when rate change is detected
438 static void stac9460_set_rate_val(struct snd_ice1712 *ice, unsigned int rate)
440 unsigned char old, new;
441 unsigned short int changed;
442 struct wtm_spec *spec = ice->spec;
444 if (rate == 0) /* no hint - S/PDIF input is master, simply return */
446 else if (rate <= 48000)
447 new = 0x08; /* 256x, base rate mode */
448 else if (rate <= 96000)
449 new = 0x11; /* 256x, mid rate mode */
451 new = 0x12; /* 128x, high rate mode */
453 old = stac9460_get(ice, STAC946X_MASTER_CLOCKING);
456 /* change detected, setting master clock, muting first */
457 /* due to possible conflicts with mute controls - mutexing */
458 mutex_lock(&spec->mute_mutex);
459 /* we have to remember current mute status for each DAC */
461 stac9460_dac_mute_all(ice, 0, &changed);
462 /*printk(KERN_DEBUG "Rate change: %d, new MC: 0x%02x\n", rate, new);*/
463 stac9460_put(ice, STAC946X_MASTER_CLOCKING, new);
464 stac9460_2_put(ice, STAC946X_MASTER_CLOCKING, new);
466 /* unmuting - only originally unmuted dacs -
467 * i.e. those changed when muting */
468 stac9460_dac_mute_all(ice, 1, &changed);
469 mutex_unlock(&spec->mute_mutex);
473 /*Limits value in dB for fader*/
474 static const DECLARE_TLV_DB_SCALE(db_scale_dac, -19125, 75, 0);
475 static const DECLARE_TLV_DB_SCALE(db_scale_adc, 0, 150, 0);
480 static struct snd_kcontrol_new stac9640_controls[] = {
482 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
483 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
484 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
485 .name = "Master Playback Switch",
486 .info = stac9460_dac_mute_info,
487 .get = stac9460_dac_mute_get,
488 .put = stac9460_dac_mute_put,
490 .tlv = { .p = db_scale_dac }
493 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
494 .name = "Master Playback Volume",
495 .info = stac9460_dac_vol_info,
496 .get = stac9460_dac_vol_get,
497 .put = stac9460_dac_vol_put,
501 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
502 .name = "MIC/Line Input Enum",
504 .info = stac9460_mic_sw_info,
505 .get = stac9460_mic_sw_get,
506 .put = stac9460_mic_sw_put,
510 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
511 .name = "DAC Switch",
513 .info = stac9460_dac_mute_info,
514 .get = stac9460_dac_mute_get,
515 .put = stac9460_dac_mute_put,
518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
519 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
520 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
522 .name = "DAC Volume",
524 .info = stac9460_dac_vol_info,
525 .get = stac9460_dac_vol_get,
526 .put = stac9460_dac_vol_put,
527 .tlv = { .p = db_scale_dac }
530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
531 .name = "ADC Switch",
533 .info = stac9460_adc_mute_info,
534 .get = stac9460_adc_mute_get,
535 .put = stac9460_adc_mute_put,
538 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
539 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
540 SNDRV_CTL_ELEM_ACCESS_TLV_READ),
542 .name = "ADC Volume",
544 .info = stac9460_adc_vol_info,
545 .get = stac9460_adc_vol_get,
546 .put = stac9460_adc_vol_put,
547 .tlv = { .p = db_scale_adc }
554 static int wtm_add_controls(struct snd_ice1712 *ice)
559 for (i = 0; i < ARRAY_SIZE(stac9640_controls); i++) {
560 err = snd_ctl_add(ice->card,
561 snd_ctl_new1(&stac9640_controls[i], ice));
568 static int wtm_init(struct snd_ice1712 *ice)
570 static unsigned short stac_inits_wtm[] = {
572 STAC946X_MASTER_CLOCKING, 0x11,
576 struct wtm_spec *spec;
579 ice->num_total_dacs = 8;
580 ice->num_total_adcs = 4;
581 ice->force_rdma1 = 1;
583 /*init mutex for dac mute conflict*/
584 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
588 mutex_init(&spec->mute_mutex);
593 for (; *p != (unsigned short)-1; p += 2) {
594 stac9460_put(ice, p[0], p[1]);
595 stac9460_2_put(ice, p[0], p[1]);
597 ice->gpio.set_pro_rate = stac9460_set_rate_val;
602 static unsigned char wtm_eeprom[] = {
603 [ICE_EEP2_SYSCONF] = 0x67, /*SYSCONF: clock 192KHz, mpu401,
605 [ICE_EEP2_ACLINK] = 0x80, /* ACLINK : I2S */
606 [ICE_EEP2_I2S] = 0xf8, /* I2S: vol; 96k, 24bit, 192k */
607 [ICE_EEP2_SPDIF] = 0xc1, /*SPDIF: out-en, spidf ext out*/
608 [ICE_EEP2_GPIO_DIR] = 0x9f,
609 [ICE_EEP2_GPIO_DIR1] = 0xff,
610 [ICE_EEP2_GPIO_DIR2] = 0x7f,
611 [ICE_EEP2_GPIO_MASK] = 0x9f,
612 [ICE_EEP2_GPIO_MASK1] = 0xff,
613 [ICE_EEP2_GPIO_MASK2] = 0x7f,
614 [ICE_EEP2_GPIO_STATE] = 0x16,
615 [ICE_EEP2_GPIO_STATE1] = 0x80,
616 [ICE_EEP2_GPIO_STATE2] = 0x00,
621 struct snd_ice1712_card_info snd_vt1724_wtm_cards[] = {
623 .subvendor = VT1724_SUBDEVICE_WTM,
624 .name = "ESI Waveterminal 192M",
626 .chip_init = wtm_init,
627 .build_controls = wtm_add_controls,
628 .eeprom_size = sizeof(wtm_eeprom),
629 .eeprom_data = wtm_eeprom,