1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
4 *
5 *   Lowlevel functions for Philips PSC724 Ultimate Edge
6 *
7 *	Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
8 */
9
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <sound/core.h>
14
15#include "ice1712.h"
16#include "envy24ht.h"
17#include "psc724.h"
18#include "wm8766.h"
19#include "wm8776.h"
20
21struct psc724_spec {
22	struct snd_wm8766 wm8766;
23	struct snd_wm8776 wm8776;
24	bool mute_all, jack_detect;
25	struct snd_ice1712 *ice;
26	struct delayed_work hp_work;
27	bool hp_connected;
28};
29
30/****************************************************************************/
31/*  PHILIPS PSC724 ULTIMATE EDGE                                            */
32/****************************************************************************/
33/*
34 *  VT1722 (Envy24GT) - 6 outputs, 4 inputs (only 2 used), 24-bit/96kHz
35 *
36 *  system configuration ICE_EEP2_SYSCONF=0x42
37 *    XIN1 49.152MHz
38 *    no MPU401
39 *    one stereo ADC, no S/PDIF receiver
40 *    three stereo DACs (FRONT, REAR, CENTER+LFE)
41 *
42 *  AC-Link configuration ICE_EEP2_ACLINK=0x80
43 *    use I2S, not AC97
44 *
45 *  I2S converters feature ICE_EEP2_I2S=0x30
46 *    I2S codec has no volume/mute control feature (bug!)
47 *    I2S codec does not support 96KHz or 192KHz (bug!)
48 *    I2S codec 24bits
49 *
50 *  S/PDIF configuration ICE_EEP2_SPDIF=0xc1
51 *    Enable integrated S/PDIF transmitter
52 *    internal S/PDIF out implemented
53 *    No S/PDIF input
54 *    External S/PDIF out implemented
55 *
56 *
57 * ** connected chips **
58 *
59 *  WM8776
60 *     2-channel DAC used for main output and stereo ADC (with 10-channel MUX)
61 *     AIN1: LINE IN, AIN2: CD/VIDEO, AIN3: AUX, AIN4: Front MIC, AIN5: Rear MIC
62 *     Controlled by I2C using VT1722 I2C interface:
63 *          MODE (pin16) -- GND
64 *          CE   (pin17) -- GND  I2C mode (address=0x34)
65 *          DI   (pin18) -- SDA  (VT1722 pin70)
66 *          CL   (pin19) -- SCLK (VT1722 pin71)
67 *
68 *  WM8766
69 *      6-channel DAC used for rear & center/LFE outputs (only 4 channels used)
70 *      Controlled by SPI using VT1722 GPIO pins:
71 *          MODE   (pin 1) -- GPIO19 (VT1722 pin99)
72 *          ML/I2S (pin11) -- GPIO18 (VT1722 pin98)
73 *          MC/IWL (pin12) -- GPIO17 (VT1722 pin97)
74 *          MD/DM  (pin13) -- GPIO16 (VT1722 pin96)
75 *          MUTE   (pin14) -- GPIO20 (VT1722 pin101)
76 *
77 *  GPIO14 is used as input for headphone jack detection (1 = connected)
78 *  GPIO22 is used as MUTE ALL output, grounding all 6 channels
79 *
80 * ** output pins and device names **
81 *
82 *   5.1ch name -- output connector color -- device (-D option)
83 *
84 *      FRONT 2ch                  -- green  -- plughw:0,0
85 *      CENTER(Lch) SUBWOOFER(Rch) -- orange -- plughw:0,2,0
86 *      REAR 2ch                   -- black  -- plughw:0,2,1
87 */
88
89/* codec access low-level functions */
90
91#define GPIO_HP_JACK	(1 << 14)
92#define GPIO_MUTE_SUR	(1 << 20)
93#define GPIO_MUTE_ALL	(1 << 22)
94
95#define JACK_INTERVAL	1000
96
97#define PSC724_SPI_DELAY 1
98
99#define PSC724_SPI_DATA	(1 << 16)
100#define PSC724_SPI_CLK	(1 << 17)
101#define PSC724_SPI_LOAD	(1 << 18)
102#define PSC724_SPI_MASK	(PSC724_SPI_DATA | PSC724_SPI_CLK | PSC724_SPI_LOAD)
103
104static void psc724_wm8766_write(struct snd_wm8766 *wm, u16 addr, u16 data)
105{
106	struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8766);
107	struct snd_ice1712 *ice = spec->ice;
108	u32 st, bits;
109	int i;
110
111	snd_ice1712_save_gpio_status(ice);
112
113	st = ((addr & 0x7f) << 9) | (data & 0x1ff);
114	snd_ice1712_gpio_set_dir(ice, ice->gpio.direction | PSC724_SPI_MASK);
115	snd_ice1712_gpio_set_mask(ice, ice->gpio.write_mask & ~PSC724_SPI_MASK);
116	bits = snd_ice1712_gpio_read(ice) & ~PSC724_SPI_MASK;
117	snd_ice1712_gpio_write(ice, bits);
118
119	for (i = 0; i < 16; i++) {
120		udelay(PSC724_SPI_DELAY);
121		bits &= ~PSC724_SPI_CLK;
122		/* MSB first */
123		st <<= 1;
124		if (st & 0x10000)
125			bits |= PSC724_SPI_DATA;
126		else
127			bits &= ~PSC724_SPI_DATA;
128		snd_ice1712_gpio_write(ice, bits);
129		/* CLOCK high */
130		udelay(PSC724_SPI_DELAY);
131		bits |= PSC724_SPI_CLK;
132		snd_ice1712_gpio_write(ice, bits);
133	}
134	/* LOAD high */
135	udelay(PSC724_SPI_DELAY);
136	bits |= PSC724_SPI_LOAD;
137	snd_ice1712_gpio_write(ice, bits);
138	/* LOAD low, DATA and CLOCK high */
139	udelay(PSC724_SPI_DELAY);
140	bits |= (PSC724_SPI_DATA | PSC724_SPI_CLK);
141	snd_ice1712_gpio_write(ice, bits);
142
143	snd_ice1712_restore_gpio_status(ice);
144}
145
146static void psc724_wm8776_write(struct snd_wm8776 *wm, u8 addr, u8 data)
147{
148	struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8776);
149
150	snd_vt1724_write_i2c(spec->ice, 0x34, addr, data);
151}
152
153/* mute all */
154
155static void psc724_set_master_switch(struct snd_ice1712 *ice, bool on)
156{
157	unsigned int bits = snd_ice1712_gpio_read(ice);
158	struct psc724_spec *spec = ice->spec;
159
160	spec->mute_all = !on;
161	if (on)
162		bits &= ~(GPIO_MUTE_ALL | GPIO_MUTE_SUR);
163	else
164		bits |= GPIO_MUTE_ALL | GPIO_MUTE_SUR;
165	snd_ice1712_gpio_write(ice, bits);
166}
167
168static bool psc724_get_master_switch(struct snd_ice1712 *ice)
169{
170	struct psc724_spec *spec = ice->spec;
171
172	return !spec->mute_all;
173}
174
175/* jack detection */
176
177static void psc724_set_jack_state(struct snd_ice1712 *ice, bool hp_connected)
178{
179	struct psc724_spec *spec = ice->spec;
180	struct snd_ctl_elem_id elem_id;
181	struct snd_kcontrol *kctl;
182	u16 power = spec->wm8776.regs[WM8776_REG_PWRDOWN] & ~WM8776_PWR_HPPD;
183
184	psc724_set_master_switch(ice, !hp_connected);
185	if (!hp_connected)
186		power |= WM8776_PWR_HPPD;
187	snd_wm8776_set_power(&spec->wm8776, power);
188	spec->hp_connected = hp_connected;
189	/* notify about master speaker mute change */
190	memset(&elem_id, 0, sizeof(elem_id));
191	elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
192	strlcpy(elem_id.name, "Master Speakers Playback Switch",
193						sizeof(elem_id.name));
194	kctl = snd_ctl_find_id(ice->card, &elem_id);
195	snd_ctl_notify(ice->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
196	/* and headphone mute change */
197	strlcpy(elem_id.name, spec->wm8776.ctl[WM8776_CTL_HP_SW].name,
198						sizeof(elem_id.name));
199	kctl = snd_ctl_find_id(ice->card, &elem_id);
200	snd_ctl_notify(ice->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
201}
202
203static void psc724_update_hp_jack_state(struct work_struct *work)
204{
205	struct psc724_spec *spec = container_of(work, struct psc724_spec,
206						hp_work.work);
207	struct snd_ice1712 *ice = spec->ice;
208	bool hp_connected = snd_ice1712_gpio_read(ice) & GPIO_HP_JACK;
209
210	schedule_delayed_work(&spec->hp_work, msecs_to_jiffies(JACK_INTERVAL));
211	if (hp_connected == spec->hp_connected)
212		return;
213	psc724_set_jack_state(ice, hp_connected);
214}
215
216static void psc724_set_jack_detection(struct snd_ice1712 *ice, bool on)
217{
218	struct psc724_spec *spec = ice->spec;
219
220	if (spec->jack_detect == on)
221		return;
222
223	spec->jack_detect = on;
224	if (on) {
225		bool hp_connected = snd_ice1712_gpio_read(ice) & GPIO_HP_JACK;
226		psc724_set_jack_state(ice, hp_connected);
227		schedule_delayed_work(&spec->hp_work,
228					msecs_to_jiffies(JACK_INTERVAL));
229	} else
230		cancel_delayed_work_sync(&spec->hp_work);
231}
232
233static bool psc724_get_jack_detection(struct snd_ice1712 *ice)
234{
235	struct psc724_spec *spec = ice->spec;
236
237	return spec->jack_detect;
238}
239
240/* mixer controls */
241
242struct psc724_control {
243	const char *name;
244	void (*set)(struct snd_ice1712 *ice, bool on);
245	bool (*get)(struct snd_ice1712 *ice);
246};
247
248static const struct psc724_control psc724_cont[] = {
249	{
250		.name = "Master Speakers Playback Switch",
251		.set = psc724_set_master_switch,
252		.get = psc724_get_master_switch,
253	},
254	{
255		.name = "Headphone Jack Detection Playback Switch",
256		.set = psc724_set_jack_detection,
257		.get = psc724_get_jack_detection,
258	},
259};
260
261static int psc724_ctl_get(struct snd_kcontrol *kcontrol,
262				  struct snd_ctl_elem_value *ucontrol)
263{
264	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
265	int n = kcontrol->private_value;
266
267	ucontrol->value.integer.value[0] = psc724_cont[n].get(ice);
268
269	return 0;
270}
271
272static int psc724_ctl_put(struct snd_kcontrol *kcontrol,
273				  struct snd_ctl_elem_value *ucontrol)
274{
275	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
276	int n = kcontrol->private_value;
277
278	psc724_cont[n].set(ice, ucontrol->value.integer.value[0]);
279
280	return 0;
281}
282
283static const char *front_volume	= "Front Playback Volume";
284static const char *front_switch	= "Front Playback Switch";
285static const char *front_zc	= "Front Zero Cross Detect Playback Switch";
286static const char *front_izd	= "Front Infinite Zero Detect Playback Switch";
287static const char *front_phase	= "Front Phase Invert Playback Switch";
288static const char *front_deemph	= "Front Deemphasis Playback Switch";
289static const char *ain1_switch	= "Line Capture Switch";
290static const char *ain2_switch	= "CD Capture Switch";
291static const char *ain3_switch	= "AUX Capture Switch";
292static const char *ain4_switch	= "Front Mic Capture Switch";
293static const char *ain5_switch	= "Rear Mic Capture Switch";
294static const char *rear_volume	= "Surround Playback Volume";
295static const char *clfe_volume	= "CLFE Playback Volume";
296static const char *rear_switch	= "Surround Playback Switch";
297static const char *clfe_switch	= "CLFE Playback Switch";
298static const char *rear_phase	= "Surround Phase Invert Playback Switch";
299static const char *clfe_phase	= "CLFE Phase Invert Playback Switch";
300static const char *rear_deemph	= "Surround Deemphasis Playback Switch";
301static const char *clfe_deemph	= "CLFE Deemphasis Playback Switch";
302static const char *rear_clfe_izd = "Rear Infinite Zero Detect Playback Switch";
303static const char *rear_clfe_zc	= "Rear Zero Cross Detect Playback Switch";
304
305static int psc724_add_controls(struct snd_ice1712 *ice)
306{
307	struct snd_kcontrol_new cont;
308	struct snd_kcontrol *ctl;
309	int err, i;
310	struct psc724_spec *spec = ice->spec;
311
312	spec->wm8776.ctl[WM8776_CTL_DAC_VOL].name = front_volume;
313	spec->wm8776.ctl[WM8776_CTL_DAC_SW].name = front_switch;
314	spec->wm8776.ctl[WM8776_CTL_DAC_ZC_SW].name = front_zc;
315	spec->wm8776.ctl[WM8776_CTL_AUX_SW].name = NULL;
316	spec->wm8776.ctl[WM8776_CTL_DAC_IZD_SW].name = front_izd;
317	spec->wm8776.ctl[WM8776_CTL_PHASE_SW].name = front_phase;
318	spec->wm8776.ctl[WM8776_CTL_DEEMPH_SW].name = front_deemph;
319	spec->wm8776.ctl[WM8776_CTL_INPUT1_SW].name = ain1_switch;
320	spec->wm8776.ctl[WM8776_CTL_INPUT2_SW].name = ain2_switch;
321	spec->wm8776.ctl[WM8776_CTL_INPUT3_SW].name = ain3_switch;
322	spec->wm8776.ctl[WM8776_CTL_INPUT4_SW].name = ain4_switch;
323	spec->wm8776.ctl[WM8776_CTL_INPUT5_SW].name = ain5_switch;
324	snd_wm8776_build_controls(&spec->wm8776);
325	spec->wm8766.ctl[WM8766_CTL_CH1_VOL].name = rear_volume;
326	spec->wm8766.ctl[WM8766_CTL_CH2_VOL].name = clfe_volume;
327	spec->wm8766.ctl[WM8766_CTL_CH3_VOL].name = NULL;
328	spec->wm8766.ctl[WM8766_CTL_CH1_SW].name = rear_switch;
329	spec->wm8766.ctl[WM8766_CTL_CH2_SW].name = clfe_switch;
330	spec->wm8766.ctl[WM8766_CTL_CH3_SW].name = NULL;
331	spec->wm8766.ctl[WM8766_CTL_PHASE1_SW].name = rear_phase;
332	spec->wm8766.ctl[WM8766_CTL_PHASE2_SW].name = clfe_phase;
333	spec->wm8766.ctl[WM8766_CTL_PHASE3_SW].name = NULL;
334	spec->wm8766.ctl[WM8766_CTL_DEEMPH1_SW].name = rear_deemph;
335	spec->wm8766.ctl[WM8766_CTL_DEEMPH2_SW].name = clfe_deemph;
336	spec->wm8766.ctl[WM8766_CTL_DEEMPH3_SW].name = NULL;
337	spec->wm8766.ctl[WM8766_CTL_IZD_SW].name = rear_clfe_izd;
338	spec->wm8766.ctl[WM8766_CTL_ZC_SW].name = rear_clfe_zc;
339	snd_wm8766_build_controls(&spec->wm8766);
340
341	memset(&cont, 0, sizeof(cont));
342	cont.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
343	for (i = 0; i < ARRAY_SIZE(psc724_cont); i++) {
344		cont.private_value = i;
345		cont.name = psc724_cont[i].name;
346		cont.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
347		cont.info = snd_ctl_boolean_mono_info;
348		cont.get = psc724_ctl_get;
349		cont.put = psc724_ctl_put;
350		ctl = snd_ctl_new1(&cont, ice);
351		if (!ctl)
352			return -ENOMEM;
353		err = snd_ctl_add(ice->card, ctl);
354		if (err < 0)
355			return err;
356	}
357
358	return 0;
359}
360
361static void psc724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate)
362{
363	struct psc724_spec *spec = ice->spec;
364	/* restore codec volume settings after rate change (PMCLK stop) */
365	snd_wm8776_volume_restore(&spec->wm8776);
366	snd_wm8766_volume_restore(&spec->wm8766);
367}
368
369/* power management */
370
371#ifdef CONFIG_PM_SLEEP
372static int psc724_resume(struct snd_ice1712 *ice)
373{
374	struct psc724_spec *spec = ice->spec;
375
376	snd_wm8776_resume(&spec->wm8776);
377	snd_wm8766_resume(&spec->wm8766);
378
379	return 0;
380}
381#endif
382
383/* init */
384
385static int psc724_init(struct snd_ice1712 *ice)
386{
387	struct psc724_spec *spec;
388
389	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
390	if (!spec)
391		return -ENOMEM;
392	ice->spec = spec;
393	spec->ice = ice;
394
395	ice->num_total_dacs = 6;
396	ice->num_total_adcs = 2;
397	spec->wm8776.ops.write = psc724_wm8776_write;
398	spec->wm8776.card = ice->card;
399	snd_wm8776_init(&spec->wm8776);
400	spec->wm8766.ops.write = psc724_wm8766_write;
401	spec->wm8766.card = ice->card;
402#ifdef CONFIG_PM_SLEEP
403	ice->pm_resume = psc724_resume;
404	ice->pm_suspend_enabled = 1;
405#endif
406	snd_wm8766_init(&spec->wm8766);
407	snd_wm8766_set_if(&spec->wm8766,
408			WM8766_IF_FMT_I2S | WM8766_IF_IWL_24BIT);
409	ice->gpio.set_pro_rate = psc724_set_pro_rate;
410	INIT_DELAYED_WORK(&spec->hp_work, psc724_update_hp_jack_state);
411	psc724_set_jack_detection(ice, true);
412	return 0;
413}
414
415static void psc724_exit(struct snd_ice1712 *ice)
416{
417	struct psc724_spec *spec = ice->spec;
418
419	cancel_delayed_work_sync(&spec->hp_work);
420}
421
422/* PSC724 has buggy EEPROM (no 96&192kHz, all FFh GPIOs), so override it here */
423static const unsigned char psc724_eeprom[] = {
424	[ICE_EEP2_SYSCONF]	= 0x42,	/* 49.152MHz, 1 ADC, 3 DACs */
425	[ICE_EEP2_ACLINK]	= 0x80,	/* I2S */
426	[ICE_EEP2_I2S]		= 0xf0,	/* I2S volume, 96kHz, 24bit */
427	[ICE_EEP2_SPDIF]	= 0xc1,	/* spdif out-en, out-int, no input */
428	/* GPIO outputs */
429	[ICE_EEP2_GPIO_DIR2]	= 0x5f, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
430	/* GPIO write enable */
431	[ICE_EEP2_GPIO_MASK]	= 0xff, /* read-only */
432	[ICE_EEP2_GPIO_MASK1]	= 0xff, /* read-only */
433	[ICE_EEP2_GPIO_MASK2]	= 0xa0, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
434	/* GPIO initial state */
435	[ICE_EEP2_GPIO_STATE2]	= 0x20,	/* unmuted, all WM8766 pins low */
436};
437
438struct snd_ice1712_card_info snd_vt1724_psc724_cards[] = {
439	{
440		.subvendor = VT1724_SUBDEVICE_PSC724,
441		.name = "Philips PSC724 Ultimate Edge",
442		.model = "psc724",
443		.chip_init = psc724_init,
444		.chip_exit = psc724_exit,
445		.build_controls = psc724_add_controls,
446		.eeprom_size = sizeof(psc724_eeprom),
447		.eeprom_data = psc724_eeprom,
448	},
449	{} /*terminator*/
450};
451