xref: /openbmc/linux/sound/pci/ice1712/psc724.c (revision 9fb29c73)
1 /*
2  *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
3  *
4  *   Lowlevel functions for Philips PSC724 Ultimate Edge
5  *
6  *	Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <sound/core.h>
28 
29 #include "ice1712.h"
30 #include "envy24ht.h"
31 #include "psc724.h"
32 #include "wm8766.h"
33 #include "wm8776.h"
34 
35 struct psc724_spec {
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;
41 	bool hp_connected;
42 };
43 
44 /****************************************************************************/
45 /*  PHILIPS PSC724 ULTIMATE EDGE                                            */
46 /****************************************************************************/
47 /*
48  *  VT1722 (Envy24GT) - 6 outputs, 4 inputs (only 2 used), 24-bit/96kHz
49  *
50  *  system configuration ICE_EEP2_SYSCONF=0x42
51  *    XIN1 49.152MHz
52  *    no MPU401
53  *    one stereo ADC, no S/PDIF receiver
54  *    three stereo DACs (FRONT, REAR, CENTER+LFE)
55  *
56  *  AC-Link configuration ICE_EEP2_ACLINK=0x80
57  *    use I2S, not AC97
58  *
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!)
62  *    I2S codec 24bits
63  *
64  *  S/PDIF configuration ICE_EEP2_SPDIF=0xc1
65  *    Enable integrated S/PDIF transmitter
66  *    internal S/PDIF out implemented
67  *    No S/PDIF input
68  *    External S/PDIF out implemented
69  *
70  *
71  * ** connected chips **
72  *
73  *  WM8776
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:
77  *          MODE (pin16) -- GND
78  *          CE   (pin17) -- GND  I2C mode (address=0x34)
79  *          DI   (pin18) -- SDA  (VT1722 pin70)
80  *          CL   (pin19) -- SCLK (VT1722 pin71)
81  *
82  *  WM8766
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)
90  *
91  *  GPIO14 is used as input for headphone jack detection (1 = connected)
92  *  GPIO22 is used as MUTE ALL output, grounding all 6 channels
93  *
94  * ** output pins and device names **
95  *
96  *   5.1ch name -- output connector color -- device (-D option)
97  *
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
101  */
102 
103 /* codec access low-level functions */
104 
105 #define GPIO_HP_JACK	(1 << 14)
106 #define GPIO_MUTE_SUR	(1 << 20)
107 #define GPIO_MUTE_ALL	(1 << 22)
108 
109 #define JACK_INTERVAL	1000
110 
111 #define PSC724_SPI_DELAY 1
112 
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)
117 
118 static void psc724_wm8766_write(struct snd_wm8766 *wm, u16 addr, u16 data)
119 {
120 	struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8766);
121 	struct snd_ice1712 *ice = spec->ice;
122 	u32 st, bits;
123 	int i;
124 
125 	snd_ice1712_save_gpio_status(ice);
126 
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);
132 
133 	for (i = 0; i < 16; i++) {
134 		udelay(PSC724_SPI_DELAY);
135 		bits &= ~PSC724_SPI_CLK;
136 		/* MSB first */
137 		st <<= 1;
138 		if (st & 0x10000)
139 			bits |= PSC724_SPI_DATA;
140 		else
141 			bits &= ~PSC724_SPI_DATA;
142 		snd_ice1712_gpio_write(ice, bits);
143 		/* CLOCK high */
144 		udelay(PSC724_SPI_DELAY);
145 		bits |= PSC724_SPI_CLK;
146 		snd_ice1712_gpio_write(ice, bits);
147 	}
148 	/* LOAD high */
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);
156 
157 	snd_ice1712_restore_gpio_status(ice);
158 }
159 
160 static void psc724_wm8776_write(struct snd_wm8776 *wm, u8 addr, u8 data)
161 {
162 	struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8776);
163 
164 	snd_vt1724_write_i2c(spec->ice, 0x34, addr, data);
165 }
166 
167 /* mute all */
168 
169 static void psc724_set_master_switch(struct snd_ice1712 *ice, bool on)
170 {
171 	unsigned int bits = snd_ice1712_gpio_read(ice);
172 	struct psc724_spec *spec = ice->spec;
173 
174 	spec->mute_all = !on;
175 	if (on)
176 		bits &= ~(GPIO_MUTE_ALL | GPIO_MUTE_SUR);
177 	else
178 		bits |= GPIO_MUTE_ALL | GPIO_MUTE_SUR;
179 	snd_ice1712_gpio_write(ice, bits);
180 }
181 
182 static bool psc724_get_master_switch(struct snd_ice1712 *ice)
183 {
184 	struct psc724_spec *spec = ice->spec;
185 
186 	return !spec->mute_all;
187 }
188 
189 /* jack detection */
190 
191 static void psc724_set_jack_state(struct snd_ice1712 *ice, bool hp_connected)
192 {
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;
197 
198 	psc724_set_master_switch(ice, !hp_connected);
199 	if (!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);
215 }
216 
217 static void psc724_update_hp_jack_state(struct work_struct *work)
218 {
219 	struct psc724_spec *spec = container_of(work, struct psc724_spec,
220 						hp_work.work);
221 	struct snd_ice1712 *ice = spec->ice;
222 	bool hp_connected = snd_ice1712_gpio_read(ice) & GPIO_HP_JACK;
223 
224 	schedule_delayed_work(&spec->hp_work, msecs_to_jiffies(JACK_INTERVAL));
225 	if (hp_connected == spec->hp_connected)
226 		return;
227 	psc724_set_jack_state(ice, hp_connected);
228 }
229 
230 static void psc724_set_jack_detection(struct snd_ice1712 *ice, bool on)
231 {
232 	struct psc724_spec *spec = ice->spec;
233 
234 	if (spec->jack_detect == on)
235 		return;
236 
237 	spec->jack_detect = on;
238 	if (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));
243 	} else
244 		cancel_delayed_work_sync(&spec->hp_work);
245 }
246 
247 static bool psc724_get_jack_detection(struct snd_ice1712 *ice)
248 {
249 	struct psc724_spec *spec = ice->spec;
250 
251 	return spec->jack_detect;
252 }
253 
254 /* mixer controls */
255 
256 struct psc724_control {
257 	const char *name;
258 	void (*set)(struct snd_ice1712 *ice, bool on);
259 	bool (*get)(struct snd_ice1712 *ice);
260 };
261 
262 static const struct psc724_control psc724_cont[] = {
263 	{
264 		.name = "Master Speakers Playback Switch",
265 		.set = psc724_set_master_switch,
266 		.get = psc724_get_master_switch,
267 	},
268 	{
269 		.name = "Headphone Jack Detection Playback Switch",
270 		.set = psc724_set_jack_detection,
271 		.get = psc724_get_jack_detection,
272 	},
273 };
274 
275 static int psc724_ctl_get(struct snd_kcontrol *kcontrol,
276 				  struct snd_ctl_elem_value *ucontrol)
277 {
278 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
279 	int n = kcontrol->private_value;
280 
281 	ucontrol->value.integer.value[0] = psc724_cont[n].get(ice);
282 
283 	return 0;
284 }
285 
286 static int psc724_ctl_put(struct snd_kcontrol *kcontrol,
287 				  struct snd_ctl_elem_value *ucontrol)
288 {
289 	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
290 	int n = kcontrol->private_value;
291 
292 	psc724_cont[n].set(ice, ucontrol->value.integer.value[0]);
293 
294 	return 0;
295 }
296 
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";
318 
319 static int psc724_add_controls(struct snd_ice1712 *ice)
320 {
321 	struct snd_kcontrol_new cont;
322 	struct snd_kcontrol *ctl;
323 	int err, i;
324 	struct psc724_spec *spec = ice->spec;
325 
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);
354 
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);
365 		if (!ctl)
366 			return -ENOMEM;
367 		err = snd_ctl_add(ice->card, ctl);
368 		if (err < 0)
369 			return err;
370 	}
371 
372 	return 0;
373 }
374 
375 static void psc724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate)
376 {
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);
381 }
382 
383 /* power management */
384 
385 #ifdef CONFIG_PM_SLEEP
386 static int psc724_resume(struct snd_ice1712 *ice)
387 {
388 	struct psc724_spec *spec = ice->spec;
389 
390 	snd_wm8776_resume(&spec->wm8776);
391 	snd_wm8766_resume(&spec->wm8766);
392 
393 	return 0;
394 }
395 #endif
396 
397 /* init */
398 
399 static int psc724_init(struct snd_ice1712 *ice)
400 {
401 	struct psc724_spec *spec;
402 
403 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
404 	if (!spec)
405 		return -ENOMEM;
406 	ice->spec = spec;
407 	spec->ice = ice;
408 
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;
419 #endif
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);
426 	return 0;
427 }
428 
429 static void psc724_exit(struct snd_ice1712 *ice)
430 {
431 	struct psc724_spec *spec = ice->spec;
432 
433 	cancel_delayed_work_sync(&spec->hp_work);
434 }
435 
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 */
442 	/* GPIO outputs */
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 */
450 };
451 
452 struct snd_ice1712_card_info snd_vt1724_psc724_cards[] = {
453 	{
454 		.subvendor = VT1724_SUBDEVICE_PSC724,
455 		.name = "Philips PSC724 Ultimate Edge",
456 		.model = "psc724",
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,
462 	},
463 	{} /*terminator*/
464 };
465