2 * ALSA driver for ICEnsemble VT1724 (Envy24HT)
4 * Lowlevel functions for Philips PSC724 Ultimate Edge
6 * Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
36 struct snd_wm8766 wm8766
;
37 struct snd_wm8776 wm8776
;
38 bool mute_all
, jack_detect
;
39 struct snd_ice1712
*ice
;
40 struct delayed_work hp_work
;
44 /****************************************************************************/
45 /* PHILIPS PSC724 ULTIMATE EDGE */
46 /****************************************************************************/
48 * VT1722 (Envy24GT) - 6 outputs, 4 inputs (only 2 used), 24-bit/96kHz
50 * system configuration ICE_EEP2_SYSCONF=0x42
53 * one stereo ADC, no S/PDIF receiver
54 * three stereo DACs (FRONT, REAR, CENTER+LFE)
56 * AC-Link configuration ICE_EEP2_ACLINK=0x80
59 * I2S converters feature ICE_EEP2_I2S=0x30
60 * I2S codec has no volume/mute control feature (bug!)
61 * I2S codec does not support 96KHz or 192KHz (bug!)
64 * S/PDIF configuration ICE_EEP2_SPDIF=0xc1
65 * Enable integrated S/PDIF transmitter
66 * internal S/PDIF out implemented
68 * External S/PDIF out implemented
71 * ** connected chips **
74 * 2-channel DAC used for main output and stereo ADC (with 10-channel MUX)
75 * AIN1: LINE IN, AIN2: CD/VIDEO, AIN3: AUX, AIN4: Front MIC, AIN5: Rear MIC
76 * Controlled by I2C using VT1722 I2C interface:
78 * CE (pin17) -- GND I2C mode (address=0x34)
79 * DI (pin18) -- SDA (VT1722 pin70)
80 * CL (pin19) -- SCLK (VT1722 pin71)
83 * 6-channel DAC used for rear & center/LFE outputs (only 4 channels used)
84 * Controlled by SPI using VT1722 GPIO pins:
85 * MODE (pin 1) -- GPIO19 (VT1722 pin99)
86 * ML/I2S (pin11) -- GPIO18 (VT1722 pin98)
87 * MC/IWL (pin12) -- GPIO17 (VT1722 pin97)
88 * MD/DM (pin13) -- GPIO16 (VT1722 pin96)
89 * MUTE (pin14) -- GPIO20 (VT1722 pin101)
91 * GPIO14 is used as input for headphone jack detection (1 = connected)
92 * GPIO22 is used as MUTE ALL output, grounding all 6 channels
94 * ** output pins and device names **
96 * 5.1ch name -- output connector color -- device (-D option)
98 * FRONT 2ch -- green -- plughw:0,0
99 * CENTER(Lch) SUBWOOFER(Rch) -- orange -- plughw:0,2,0
100 * REAR 2ch -- black -- plughw:0,2,1
103 /* codec access low-level functions */
105 #define GPIO_HP_JACK (1 << 14)
106 #define GPIO_MUTE_SUR (1 << 20)
107 #define GPIO_MUTE_ALL (1 << 22)
109 #define JACK_INTERVAL 1000
111 #define PSC724_SPI_DELAY 1
113 #define PSC724_SPI_DATA (1 << 16)
114 #define PSC724_SPI_CLK (1 << 17)
115 #define PSC724_SPI_LOAD (1 << 18)
116 #define PSC724_SPI_MASK (PSC724_SPI_DATA | PSC724_SPI_CLK | PSC724_SPI_LOAD)
118 static void psc724_wm8766_write(struct snd_wm8766
*wm
, u16 addr
, u16 data
)
120 struct psc724_spec
*spec
= container_of(wm
, struct psc724_spec
, wm8766
);
121 struct snd_ice1712
*ice
= spec
->ice
;
125 snd_ice1712_save_gpio_status(ice
);
127 st
= ((addr
& 0x7f) << 9) | (data
& 0x1ff);
128 snd_ice1712_gpio_set_dir(ice
, ice
->gpio
.direction
| PSC724_SPI_MASK
);
129 snd_ice1712_gpio_set_mask(ice
, ice
->gpio
.write_mask
& ~PSC724_SPI_MASK
);
130 bits
= snd_ice1712_gpio_read(ice
) & ~PSC724_SPI_MASK
;
131 snd_ice1712_gpio_write(ice
, bits
);
133 for (i
= 0; i
< 16; i
++) {
134 udelay(PSC724_SPI_DELAY
);
135 bits
&= ~PSC724_SPI_CLK
;
139 bits
|= PSC724_SPI_DATA
;
141 bits
&= ~PSC724_SPI_DATA
;
142 snd_ice1712_gpio_write(ice
, bits
);
144 udelay(PSC724_SPI_DELAY
);
145 bits
|= PSC724_SPI_CLK
;
146 snd_ice1712_gpio_write(ice
, bits
);
149 udelay(PSC724_SPI_DELAY
);
150 bits
|= PSC724_SPI_LOAD
;
151 snd_ice1712_gpio_write(ice
, bits
);
152 /* LOAD low, DATA and CLOCK high */
153 udelay(PSC724_SPI_DELAY
);
154 bits
|= (PSC724_SPI_DATA
| PSC724_SPI_CLK
);
155 snd_ice1712_gpio_write(ice
, bits
);
157 snd_ice1712_restore_gpio_status(ice
);
160 static void psc724_wm8776_write(struct snd_wm8776
*wm
, u8 addr
, u8 data
)
162 struct psc724_spec
*spec
= container_of(wm
, struct psc724_spec
, wm8776
);
164 snd_vt1724_write_i2c(spec
->ice
, 0x34, addr
, data
);
169 static void psc724_set_master_switch(struct snd_ice1712
*ice
, bool on
)
171 unsigned int bits
= snd_ice1712_gpio_read(ice
);
172 struct psc724_spec
*spec
= ice
->spec
;
174 spec
->mute_all
= !on
;
176 bits
&= ~(GPIO_MUTE_ALL
| GPIO_MUTE_SUR
);
178 bits
|= GPIO_MUTE_ALL
| GPIO_MUTE_SUR
;
179 snd_ice1712_gpio_write(ice
, bits
);
182 static bool psc724_get_master_switch(struct snd_ice1712
*ice
)
184 struct psc724_spec
*spec
= ice
->spec
;
186 return !spec
->mute_all
;
191 static void psc724_set_jack_state(struct snd_ice1712
*ice
, bool hp_connected
)
193 struct psc724_spec
*spec
= ice
->spec
;
194 struct snd_ctl_elem_id elem_id
;
195 struct snd_kcontrol
*kctl
;
196 u16 power
= spec
->wm8776
.regs
[WM8776_REG_PWRDOWN
] & ~WM8776_PWR_HPPD
;
198 psc724_set_master_switch(ice
, !hp_connected
);
200 power
|= WM8776_PWR_HPPD
;
201 snd_wm8776_set_power(&spec
->wm8776
, power
);
202 spec
->hp_connected
= hp_connected
;
203 /* notify about master speaker mute change */
204 memset(&elem_id
, 0, sizeof(elem_id
));
205 elem_id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
206 strlcpy(elem_id
.name
, "Master Speakers Playback Switch",
207 sizeof(elem_id
.name
));
208 kctl
= snd_ctl_find_id(ice
->card
, &elem_id
);
209 snd_ctl_notify(ice
->card
, SNDRV_CTL_EVENT_MASK_VALUE
, &kctl
->id
);
210 /* and headphone mute change */
211 strlcpy(elem_id
.name
, spec
->wm8776
.ctl
[WM8776_CTL_HP_SW
].name
,
212 sizeof(elem_id
.name
));
213 kctl
= snd_ctl_find_id(ice
->card
, &elem_id
);
214 snd_ctl_notify(ice
->card
, SNDRV_CTL_EVENT_MASK_VALUE
, &kctl
->id
);
217 static void psc724_update_hp_jack_state(struct work_struct
*work
)
219 struct psc724_spec
*spec
= container_of(work
, struct psc724_spec
,
221 struct snd_ice1712
*ice
= spec
->ice
;
222 bool hp_connected
= snd_ice1712_gpio_read(ice
) & GPIO_HP_JACK
;
224 schedule_delayed_work(&spec
->hp_work
, msecs_to_jiffies(JACK_INTERVAL
));
225 if (hp_connected
== spec
->hp_connected
)
227 psc724_set_jack_state(ice
, hp_connected
);
230 static void psc724_set_jack_detection(struct snd_ice1712
*ice
, bool on
)
232 struct psc724_spec
*spec
= ice
->spec
;
234 if (spec
->jack_detect
== on
)
237 spec
->jack_detect
= on
;
239 bool hp_connected
= snd_ice1712_gpio_read(ice
) & GPIO_HP_JACK
;
240 psc724_set_jack_state(ice
, hp_connected
);
241 schedule_delayed_work(&spec
->hp_work
,
242 msecs_to_jiffies(JACK_INTERVAL
));
244 cancel_delayed_work_sync(&spec
->hp_work
);
247 static bool psc724_get_jack_detection(struct snd_ice1712
*ice
)
249 struct psc724_spec
*spec
= ice
->spec
;
251 return spec
->jack_detect
;
256 struct psc724_control
{
258 void (*set
)(struct snd_ice1712
*ice
, bool on
);
259 bool (*get
)(struct snd_ice1712
*ice
);
262 static const struct psc724_control psc724_cont
[] = {
264 .name
= "Master Speakers Playback Switch",
265 .set
= psc724_set_master_switch
,
266 .get
= psc724_get_master_switch
,
269 .name
= "Headphone Jack Detection Playback Switch",
270 .set
= psc724_set_jack_detection
,
271 .get
= psc724_get_jack_detection
,
275 static int psc724_ctl_get(struct snd_kcontrol
*kcontrol
,
276 struct snd_ctl_elem_value
*ucontrol
)
278 struct snd_ice1712
*ice
= snd_kcontrol_chip(kcontrol
);
279 int n
= kcontrol
->private_value
;
281 ucontrol
->value
.integer
.value
[0] = psc724_cont
[n
].get(ice
);
286 static int psc724_ctl_put(struct snd_kcontrol
*kcontrol
,
287 struct snd_ctl_elem_value
*ucontrol
)
289 struct snd_ice1712
*ice
= snd_kcontrol_chip(kcontrol
);
290 int n
= kcontrol
->private_value
;
292 psc724_cont
[n
].set(ice
, ucontrol
->value
.integer
.value
[0]);
297 static const char *front_volume
= "Front Playback Volume";
298 static const char *front_switch
= "Front Playback Switch";
299 static const char *front_zc
= "Front Zero Cross Detect Playback Switch";
300 static const char *front_izd
= "Front Infinite Zero Detect Playback Switch";
301 static const char *front_phase
= "Front Phase Invert Playback Switch";
302 static const char *front_deemph
= "Front Deemphasis Playback Switch";
303 static const char *ain1_switch
= "Line Capture Switch";
304 static const char *ain2_switch
= "CD Capture Switch";
305 static const char *ain3_switch
= "AUX Capture Switch";
306 static const char *ain4_switch
= "Front Mic Capture Switch";
307 static const char *ain5_switch
= "Rear Mic Capture Switch";
308 static const char *rear_volume
= "Surround Playback Volume";
309 static const char *clfe_volume
= "CLFE Playback Volume";
310 static const char *rear_switch
= "Surround Playback Switch";
311 static const char *clfe_switch
= "CLFE Playback Switch";
312 static const char *rear_phase
= "Surround Phase Invert Playback Switch";
313 static const char *clfe_phase
= "CLFE Phase Invert Playback Switch";
314 static const char *rear_deemph
= "Surround Deemphasis Playback Switch";
315 static const char *clfe_deemph
= "CLFE Deemphasis Playback Switch";
316 static const char *rear_clfe_izd
= "Rear Infinite Zero Detect Playback Switch";
317 static const char *rear_clfe_zc
= "Rear Zero Cross Detect Playback Switch";
319 static int psc724_add_controls(struct snd_ice1712
*ice
)
321 struct snd_kcontrol_new cont
;
322 struct snd_kcontrol
*ctl
;
324 struct psc724_spec
*spec
= ice
->spec
;
326 spec
->wm8776
.ctl
[WM8776_CTL_DAC_VOL
].name
= front_volume
;
327 spec
->wm8776
.ctl
[WM8776_CTL_DAC_SW
].name
= front_switch
;
328 spec
->wm8776
.ctl
[WM8776_CTL_DAC_ZC_SW
].name
= front_zc
;
329 spec
->wm8776
.ctl
[WM8776_CTL_AUX_SW
].name
= NULL
;
330 spec
->wm8776
.ctl
[WM8776_CTL_DAC_IZD_SW
].name
= front_izd
;
331 spec
->wm8776
.ctl
[WM8776_CTL_PHASE_SW
].name
= front_phase
;
332 spec
->wm8776
.ctl
[WM8776_CTL_DEEMPH_SW
].name
= front_deemph
;
333 spec
->wm8776
.ctl
[WM8776_CTL_INPUT1_SW
].name
= ain1_switch
;
334 spec
->wm8776
.ctl
[WM8776_CTL_INPUT2_SW
].name
= ain2_switch
;
335 spec
->wm8776
.ctl
[WM8776_CTL_INPUT3_SW
].name
= ain3_switch
;
336 spec
->wm8776
.ctl
[WM8776_CTL_INPUT4_SW
].name
= ain4_switch
;
337 spec
->wm8776
.ctl
[WM8776_CTL_INPUT5_SW
].name
= ain5_switch
;
338 snd_wm8776_build_controls(&spec
->wm8776
);
339 spec
->wm8766
.ctl
[WM8766_CTL_CH1_VOL
].name
= rear_volume
;
340 spec
->wm8766
.ctl
[WM8766_CTL_CH2_VOL
].name
= clfe_volume
;
341 spec
->wm8766
.ctl
[WM8766_CTL_CH3_VOL
].name
= NULL
;
342 spec
->wm8766
.ctl
[WM8766_CTL_CH1_SW
].name
= rear_switch
;
343 spec
->wm8766
.ctl
[WM8766_CTL_CH2_SW
].name
= clfe_switch
;
344 spec
->wm8766
.ctl
[WM8766_CTL_CH3_SW
].name
= NULL
;
345 spec
->wm8766
.ctl
[WM8766_CTL_PHASE1_SW
].name
= rear_phase
;
346 spec
->wm8766
.ctl
[WM8766_CTL_PHASE2_SW
].name
= clfe_phase
;
347 spec
->wm8766
.ctl
[WM8766_CTL_PHASE3_SW
].name
= NULL
;
348 spec
->wm8766
.ctl
[WM8766_CTL_DEEMPH1_SW
].name
= rear_deemph
;
349 spec
->wm8766
.ctl
[WM8766_CTL_DEEMPH2_SW
].name
= clfe_deemph
;
350 spec
->wm8766
.ctl
[WM8766_CTL_DEEMPH3_SW
].name
= NULL
;
351 spec
->wm8766
.ctl
[WM8766_CTL_IZD_SW
].name
= rear_clfe_izd
;
352 spec
->wm8766
.ctl
[WM8766_CTL_ZC_SW
].name
= rear_clfe_zc
;
353 snd_wm8766_build_controls(&spec
->wm8766
);
355 memset(&cont
, 0, sizeof(cont
));
356 cont
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
357 for (i
= 0; i
< ARRAY_SIZE(psc724_cont
); i
++) {
358 cont
.private_value
= i
;
359 cont
.name
= psc724_cont
[i
].name
;
360 cont
.access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
361 cont
.info
= snd_ctl_boolean_mono_info
;
362 cont
.get
= psc724_ctl_get
;
363 cont
.put
= psc724_ctl_put
;
364 ctl
= snd_ctl_new1(&cont
, ice
);
367 err
= snd_ctl_add(ice
->card
, ctl
);
375 static void psc724_set_pro_rate(struct snd_ice1712
*ice
, unsigned int rate
)
377 struct psc724_spec
*spec
= ice
->spec
;
378 /* restore codec volume settings after rate change (PMCLK stop) */
379 snd_wm8776_volume_restore(&spec
->wm8776
);
380 snd_wm8766_volume_restore(&spec
->wm8766
);
383 /* power management */
385 #ifdef CONFIG_PM_SLEEP
386 static int psc724_resume(struct snd_ice1712
*ice
)
388 struct psc724_spec
*spec
= ice
->spec
;
390 snd_wm8776_resume(&spec
->wm8776
);
391 snd_wm8766_resume(&spec
->wm8766
);
399 static int psc724_init(struct snd_ice1712
*ice
)
401 struct psc724_spec
*spec
;
403 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
409 ice
->num_total_dacs
= 6;
410 ice
->num_total_adcs
= 2;
411 spec
->wm8776
.ops
.write
= psc724_wm8776_write
;
412 spec
->wm8776
.card
= ice
->card
;
413 snd_wm8776_init(&spec
->wm8776
);
414 spec
->wm8766
.ops
.write
= psc724_wm8766_write
;
415 spec
->wm8766
.card
= ice
->card
;
416 #ifdef CONFIG_PM_SLEEP
417 ice
->pm_resume
= psc724_resume
;
418 ice
->pm_suspend_enabled
= 1;
420 snd_wm8766_init(&spec
->wm8766
);
421 snd_wm8766_set_if(&spec
->wm8766
,
422 WM8766_IF_FMT_I2S
| WM8766_IF_IWL_24BIT
);
423 ice
->gpio
.set_pro_rate
= psc724_set_pro_rate
;
424 INIT_DELAYED_WORK(&spec
->hp_work
, psc724_update_hp_jack_state
);
425 psc724_set_jack_detection(ice
, true);
429 static void psc724_exit(struct snd_ice1712
*ice
)
431 struct psc724_spec
*spec
= ice
->spec
;
433 cancel_delayed_work_sync(&spec
->hp_work
);
436 /* PSC724 has buggy EEPROM (no 96&192kHz, all FFh GPIOs), so override it here */
437 static unsigned char psc724_eeprom
[] = {
438 [ICE_EEP2_SYSCONF
] = 0x42, /* 49.152MHz, 1 ADC, 3 DACs */
439 [ICE_EEP2_ACLINK
] = 0x80, /* I2S */
440 [ICE_EEP2_I2S
] = 0xf0, /* I2S volume, 96kHz, 24bit */
441 [ICE_EEP2_SPDIF
] = 0xc1, /* spdif out-en, out-int, no input */
443 [ICE_EEP2_GPIO_DIR2
] = 0x5f, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
444 /* GPIO write enable */
445 [ICE_EEP2_GPIO_MASK
] = 0xff, /* read-only */
446 [ICE_EEP2_GPIO_MASK1
] = 0xff, /* read-only */
447 [ICE_EEP2_GPIO_MASK2
] = 0xa0, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
448 /* GPIO initial state */
449 [ICE_EEP2_GPIO_STATE2
] = 0x20, /* unmuted, all WM8766 pins low */
452 struct snd_ice1712_card_info snd_vt1724_psc724_cards
[] = {
454 .subvendor
= VT1724_SUBDEVICE_PSC724
,
455 .name
= "Philips PSC724 Ultimate Edge",
457 .chip_init
= psc724_init
,
458 .chip_exit
= psc724_exit
,
459 .build_controls
= psc724_add_controls
,
460 .eeprom_size
= sizeof(psc724_eeprom
),
461 .eeprom_data
= psc724_eeprom
,