Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
  4 *
  5 *   Lowlevel functions for ESI Juli@ cards
  6 *
  7 *	Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
  8 *	              2008 Pavel Hofman <dustin@seznam.cz>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#include <linux/delay.h>
 12#include <linux/interrupt.h>
 13#include <linux/init.h>
 14#include <linux/slab.h>
 15#include <linux/string.h>
 16#include <sound/core.h>
 17#include <sound/tlv.h>
 18
 19#include "ice1712.h"
 20#include "envy24ht.h"
 21#include "juli.h"
 22
 23struct juli_spec {
 24	struct ak4114 *ak4114;
 25	unsigned int analog:1;
 26};
 27
 28/*
 29 * chip addresses on I2C bus
 30 */
 31#define AK4114_ADDR		0x20		/* S/PDIF receiver */
 32#define AK4358_ADDR		0x22		/* DAC */
 33
 34/*
 35 * Juli does not use the standard ICE1724 clock scheme. Juli's ice1724 chip is
 36 * supplied by external clock provided by Xilinx array and MK73-1 PLL frequency
 37 * multiplier. Actual frequency is set by ice1724 GPIOs hooked to the Xilinx.
 38 *
 39 * The clock circuitry is supplied by the two ice1724 crystals. This
 40 * arrangement allows to generate independent clock signal for AK4114's input
 41 * rate detection circuit. As a result, Juli, unlike most other
 42 * ice1724+ak4114-based cards, detects spdif input rate correctly.
 43 * This fact is applied in the driver, allowing to modify PCM stream rate
 44 * parameter according to the actual input rate.
 45 *
 46 * Juli uses the remaining three stereo-channels of its DAC to optionally
 47 * monitor analog input, digital input, and digital output. The corresponding
 48 * I2S signals are routed by Xilinx, controlled by GPIOs.
 49 *
 50 * The master mute is implemented using output muting transistors (GPIO) in
 51 * combination with smuting the DAC.
 52 *
 53 * The card itself has no HW master volume control, implemented using the
 54 * vmaster control.
 55 *
 56 * TODO:
 57 * researching and fixing the input monitors
 58 */
 59
 60/*
 61 * GPIO pins
 62 */
 63#define GPIO_FREQ_MASK		(3<<0)
 64#define GPIO_FREQ_32KHZ		(0<<0)
 65#define GPIO_FREQ_44KHZ		(1<<0)
 66#define GPIO_FREQ_48KHZ		(2<<0)
 67#define GPIO_MULTI_MASK		(3<<2)
 68#define GPIO_MULTI_4X		(0<<2)
 69#define GPIO_MULTI_2X		(1<<2)
 70#define GPIO_MULTI_1X		(2<<2)		/* also external */
 71#define GPIO_MULTI_HALF		(3<<2)
 72#define GPIO_INTERNAL_CLOCK	(1<<4)		/* 0 = external, 1 = internal */
 73#define GPIO_CLOCK_MASK		(1<<4)
 74#define GPIO_ANALOG_PRESENT	(1<<5)		/* RO only: 0 = present */
 75#define GPIO_RXMCLK_SEL		(1<<7)		/* must be 0 */
 76#define GPIO_AK5385A_CKS0	(1<<8)
 77#define GPIO_AK5385A_DFS1	(1<<9)
 78#define GPIO_AK5385A_DFS0	(1<<10)
 79#define GPIO_DIGOUT_MONITOR	(1<<11)		/* 1 = active */
 80#define GPIO_DIGIN_MONITOR	(1<<12)		/* 1 = active */
 81#define GPIO_ANAIN_MONITOR	(1<<13)		/* 1 = active */
 82#define GPIO_AK5385A_CKS1	(1<<14)		/* must be 0 */
 83#define GPIO_MUTE_CONTROL	(1<<15)		/* output mute, 1 = muted */
 84
 85#define GPIO_RATE_MASK		(GPIO_FREQ_MASK | GPIO_MULTI_MASK | \
 86		GPIO_CLOCK_MASK)
 87#define GPIO_AK5385A_MASK	(GPIO_AK5385A_CKS0 | GPIO_AK5385A_DFS0 | \
 88		GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS1)
 89
 90#define JULI_PCM_RATE	(SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
 91		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
 92		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
 93		SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
 94		SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
 95
 96#define GPIO_RATE_16000		(GPIO_FREQ_32KHZ | GPIO_MULTI_HALF | \
 97		GPIO_INTERNAL_CLOCK)
 98#define GPIO_RATE_22050		(GPIO_FREQ_44KHZ | GPIO_MULTI_HALF | \
 99		GPIO_INTERNAL_CLOCK)
100#define GPIO_RATE_24000		(GPIO_FREQ_48KHZ | GPIO_MULTI_HALF | \
101		GPIO_INTERNAL_CLOCK)
102#define GPIO_RATE_32000		(GPIO_FREQ_32KHZ | GPIO_MULTI_1X | \
103		GPIO_INTERNAL_CLOCK)
104#define GPIO_RATE_44100		(GPIO_FREQ_44KHZ | GPIO_MULTI_1X | \
105		GPIO_INTERNAL_CLOCK)
106#define GPIO_RATE_48000		(GPIO_FREQ_48KHZ | GPIO_MULTI_1X | \
107		GPIO_INTERNAL_CLOCK)
108#define GPIO_RATE_64000		(GPIO_FREQ_32KHZ | GPIO_MULTI_2X | \
109		GPIO_INTERNAL_CLOCK)
110#define GPIO_RATE_88200		(GPIO_FREQ_44KHZ | GPIO_MULTI_2X | \
111		GPIO_INTERNAL_CLOCK)
112#define GPIO_RATE_96000		(GPIO_FREQ_48KHZ | GPIO_MULTI_2X | \
113		GPIO_INTERNAL_CLOCK)
114#define GPIO_RATE_176400	(GPIO_FREQ_44KHZ | GPIO_MULTI_4X | \
115		GPIO_INTERNAL_CLOCK)
116#define GPIO_RATE_192000	(GPIO_FREQ_48KHZ | GPIO_MULTI_4X | \
117		GPIO_INTERNAL_CLOCK)
118
119/*
120 * Initial setup of the conversion array GPIO <-> rate
121 */
122static const unsigned int juli_rates[] = {
123	16000, 22050, 24000, 32000,
124	44100, 48000, 64000, 88200,
125	96000, 176400, 192000,
126};
127
128static const unsigned int gpio_vals[] = {
129	GPIO_RATE_16000, GPIO_RATE_22050, GPIO_RATE_24000, GPIO_RATE_32000,
130	GPIO_RATE_44100, GPIO_RATE_48000, GPIO_RATE_64000, GPIO_RATE_88200,
131	GPIO_RATE_96000, GPIO_RATE_176400, GPIO_RATE_192000,
132};
133
134static const struct snd_pcm_hw_constraint_list juli_rates_info = {
135	.count = ARRAY_SIZE(juli_rates),
136	.list = juli_rates,
137	.mask = 0,
138};
139
140static int get_gpio_val(int rate)
141{
142	int i;
143	for (i = 0; i < ARRAY_SIZE(juli_rates); i++)
144		if (juli_rates[i] == rate)
145			return gpio_vals[i];
146	return 0;
147}
148
149static void juli_ak4114_write(void *private_data, unsigned char reg,
150				unsigned char val)
151{
152	snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR,
153				reg, val);
154}
155
156static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
157{
158	return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data,
159					AK4114_ADDR, reg);
160}
161
162/*
163 * If SPDIF capture and slaved to SPDIF-IN, setting runtime rate
164 * to the external rate
165 */
166static void juli_spdif_in_open(struct snd_ice1712 *ice,
167				struct snd_pcm_substream *substream)
168{
169	struct juli_spec *spec = ice->spec;
170	struct snd_pcm_runtime *runtime = substream->runtime;
171	int rate;
172
173	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
174			!ice->is_spdif_master(ice))
175		return;
176	rate = snd_ak4114_external_rate(spec->ak4114);
177	if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
178		runtime->hw.rate_min = rate;
179		runtime->hw.rate_max = rate;
180	}
181}
182
183/*
184 * AK4358 section
185 */
186
187static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
188{
189}
190
191static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
192{
193}
194
195static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
196			   unsigned char addr, unsigned char data)
197{
198	struct snd_ice1712 *ice = ak->private_data[0];
199	 
200	if (snd_BUG_ON(chip))
201		return;
202	snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
203}
204
205/*
206 * change the rate of envy24HT, AK4358, AK5385
207 */
208static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
209{
210	unsigned char old, tmp, ak4358_dfs;
211	unsigned int ak5385_pins, old_gpio, new_gpio;
212	struct snd_ice1712 *ice = ak->private_data[0];
213	struct juli_spec *spec = ice->spec;
214
215	if (rate == 0)  /* no hint - S/PDIF input is master or the new spdif
216			   input rate undetected, simply return */
217		return;
218
219	/* adjust DFS on codecs */
220	if (rate > 96000)  {
221		ak4358_dfs = 2;
222		ak5385_pins = GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS0;
223	} else if (rate > 48000) {
224		ak4358_dfs = 1;
225		ak5385_pins = GPIO_AK5385A_DFS0;
226	} else {
227		ak4358_dfs = 0;
228		ak5385_pins = 0;
229	}
230	/* AK5385 first, since it requires cold reset affecting both codecs */
231	old_gpio = ice->gpio.get_data(ice);
232	new_gpio =  (old_gpio & ~GPIO_AK5385A_MASK) | ak5385_pins;
233	/* dev_dbg(ice->card->dev, "JULI - ak5385 set_rate_val: new gpio 0x%x\n",
234		new_gpio); */
235	ice->gpio.set_data(ice, new_gpio);
236
237	/* cold reset */
238	old = inb(ICEMT1724(ice, AC97_CMD));
239	outb(old | VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
240	udelay(1);
241	outb(old & ~VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
242
243	/* AK4358 */
244	/* set new value, reset DFS */
245	tmp = snd_akm4xxx_get(ak, 0, 2);
246	snd_akm4xxx_reset(ak, 1);
247	tmp = snd_akm4xxx_get(ak, 0, 2);
248	tmp &= ~(0x03 << 4);
249	tmp |= ak4358_dfs << 4;
250	snd_akm4xxx_set(ak, 0, 2, tmp);
251	snd_akm4xxx_reset(ak, 0);
252
253	/* reinit ak4114 */
254	snd_ak4114_reinit(spec->ak4114);
255}
256
257#define AK_DAC(xname, xch)	{ .name = xname, .num_channels = xch }
258#define PCM_VOLUME		"PCM Playback Volume"
259#define MONITOR_AN_IN_VOLUME	"Monitor Analog In Volume"
260#define MONITOR_DIG_IN_VOLUME	"Monitor Digital In Volume"
261#define MONITOR_DIG_OUT_VOLUME	"Monitor Digital Out Volume"
262
263static const struct snd_akm4xxx_dac_channel juli_dac[] = {
264	AK_DAC(PCM_VOLUME, 2),
265	AK_DAC(MONITOR_AN_IN_VOLUME, 2),
266	AK_DAC(MONITOR_DIG_OUT_VOLUME, 2),
267	AK_DAC(MONITOR_DIG_IN_VOLUME, 2),
268};
269
270
271static const struct snd_akm4xxx akm_juli_dac = {
272	.type = SND_AK4358,
273	.num_dacs = 8,	/* DAC1 - analog out
274			   DAC2 - analog in monitor
275			   DAC3 - digital out monitor
276			   DAC4 - digital in monitor
277			 */
278	.ops = {
279		.lock = juli_akm_lock,
280		.unlock = juli_akm_unlock,
281		.write = juli_akm_write,
282		.set_rate_val = juli_akm_set_rate_val
283	},
284	.dac_info = juli_dac,
285};
286
287#define juli_mute_info		snd_ctl_boolean_mono_info
288
289static int juli_mute_get(struct snd_kcontrol *kcontrol,
290		struct snd_ctl_elem_value *ucontrol)
291{
292	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
293	unsigned int val;
294	val = ice->gpio.get_data(ice) & (unsigned int) kcontrol->private_value;
295	if (kcontrol->private_value == GPIO_MUTE_CONTROL)
296		/* val 0 = signal on */
297		ucontrol->value.integer.value[0] = (val) ? 0 : 1;
298	else
299		/* val 1 = signal on */
300		ucontrol->value.integer.value[0] = (val) ? 1 : 0;
301	return 0;
302}
303
304static int juli_mute_put(struct snd_kcontrol *kcontrol,
305		struct snd_ctl_elem_value *ucontrol)
306{
307	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
308	unsigned int old_gpio, new_gpio;
309	old_gpio = ice->gpio.get_data(ice);
310	if (ucontrol->value.integer.value[0]) {
311		/* unmute */
312		if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
313			/* 0 = signal on */
314			new_gpio = old_gpio & ~GPIO_MUTE_CONTROL;
315			/* un-smuting DAC */
316			snd_akm4xxx_write(ice->akm, 0, 0x01, 0x01);
317		} else
318			/* 1 = signal on */
319			new_gpio =  old_gpio |
320				(unsigned int) kcontrol->private_value;
321	} else {
322		/* mute */
323		if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
324			/* 1 = signal off */
325			new_gpio = old_gpio | GPIO_MUTE_CONTROL;
326			/* smuting DAC */
327			snd_akm4xxx_write(ice->akm, 0, 0x01, 0x03);
328		} else
329			/* 0 = signal off */
330			new_gpio =  old_gpio &
331				~((unsigned int) kcontrol->private_value);
332	}
333	/* dev_dbg(ice->card->dev,
334		"JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, "
335		"new_gpio 0x%x\n",
336		(unsigned int)ucontrol->value.integer.value[0], old_gpio,
337		new_gpio); */
338	if (old_gpio != new_gpio) {
339		ice->gpio.set_data(ice, new_gpio);
340		return 1;
341	}
342	/* no change */
343	return 0;
344}
345
346static const struct snd_kcontrol_new juli_mute_controls[] = {
347	{
348		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
349		.name = "Master Playback Switch",
350		.info = juli_mute_info,
351		.get = juli_mute_get,
352		.put = juli_mute_put,
353		.private_value = GPIO_MUTE_CONTROL,
354	},
355	/* Although the following functionality respects the succint NDA'd
356	 * documentation from the card manufacturer, and the same way of
357	 * operation is coded in OSS Juli driver, only Digital Out monitor
358	 * seems to work. Surprisingly, Analog input monitor outputs Digital
359	 * output data. The two are independent, as enabling both doubles
360	 * volume of the monitor sound.
361	 *
362	 * Checking traces on the board suggests the functionality described
363	 * by the manufacturer is correct - I2S from ADC and AK4114
364	 * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
365	 * inputs) are fed from Xilinx.
366	 *
367	 * I even checked traces on board and coded a support in driver for
368	 * an alternative possibility - the unused I2S ICE output channels
369	 * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
370	 * the DAC - to no avail. The I2S outputs seem to be unconnected.
371	 *
372	 * The windows driver supports the monitoring correctly.
373	 */
374	{
375		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
376		.name = "Monitor Analog In Switch",
377		.info = juli_mute_info,
378		.get = juli_mute_get,
379		.put = juli_mute_put,
380		.private_value = GPIO_ANAIN_MONITOR,
381	},
382	{
383		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
384		.name = "Monitor Digital Out Switch",
385		.info = juli_mute_info,
386		.get = juli_mute_get,
387		.put = juli_mute_put,
388		.private_value = GPIO_DIGOUT_MONITOR,
389	},
390	{
391		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
392		.name = "Monitor Digital In Switch",
393		.info = juli_mute_info,
394		.get = juli_mute_get,
395		.put = juli_mute_put,
396		.private_value = GPIO_DIGIN_MONITOR,
397	},
398};
399
400static const char * const follower_vols[] = {
401	PCM_VOLUME,
402	MONITOR_AN_IN_VOLUME,
403	MONITOR_DIG_IN_VOLUME,
404	MONITOR_DIG_OUT_VOLUME,
405	NULL
406};
407
408static
409DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
410
411static struct snd_kcontrol *ctl_find(struct snd_card *card,
412				     const char *name)
413{
414	struct snd_ctl_elem_id sid = {0};
415
416	strlcpy(sid.name, name, sizeof(sid.name));
 
417	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
418	return snd_ctl_find_id(card, &sid);
419}
420
421static void add_followers(struct snd_card *card,
422			  struct snd_kcontrol *master,
423			  const char * const *list)
424{
425	for (; *list; list++) {
426		struct snd_kcontrol *follower = ctl_find(card, *list);
427		/* dev_dbg(card->dev, "add_followers - %s\n", *list); */
428		if (follower) {
429			/* dev_dbg(card->dev, "follower %s found\n", *list); */
430			snd_ctl_add_follower(master, follower);
431		}
432	}
433}
434
435static int juli_add_controls(struct snd_ice1712 *ice)
436{
437	struct juli_spec *spec = ice->spec;
438	int err;
439	unsigned int i;
440	struct snd_kcontrol *vmaster;
441
442	err = snd_ice1712_akm4xxx_build_controls(ice);
443	if (err < 0)
444		return err;
445
446	for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
447		err = snd_ctl_add(ice->card,
448				snd_ctl_new1(&juli_mute_controls[i], ice));
449		if (err < 0)
450			return err;
451	}
452	/* Create virtual master control */
453	vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
454					      juli_master_db_scale);
455	if (!vmaster)
456		return -ENOMEM;
457	add_followers(ice->card, vmaster, follower_vols);
458	err = snd_ctl_add(ice->card, vmaster);
459	if (err < 0)
460		return err;
461
462	/* only capture SPDIF over AK4114 */
463	return snd_ak4114_build(spec->ak4114, NULL,
464			ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
 
 
 
465}
466
467/*
468 * suspend/resume
469 * */
470
471#ifdef CONFIG_PM_SLEEP
472static int juli_resume(struct snd_ice1712 *ice)
473{
474	struct snd_akm4xxx *ak = ice->akm;
475	struct juli_spec *spec = ice->spec;
476	/* akm4358 un-reset, un-mute */
477	snd_akm4xxx_reset(ak, 0);
478	/* reinit ak4114 */
479	snd_ak4114_resume(spec->ak4114);
480	return 0;
481}
482
483static int juli_suspend(struct snd_ice1712 *ice)
484{
485	struct snd_akm4xxx *ak = ice->akm;
486	struct juli_spec *spec = ice->spec;
487	/* akm4358 reset and soft-mute */
488	snd_akm4xxx_reset(ak, 1);
489	snd_ak4114_suspend(spec->ak4114);
490	return 0;
491}
492#endif
493
494/*
495 * initialize the chip
496 */
497
498static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
499{
500	return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
501}
502
503static unsigned int juli_get_rate(struct snd_ice1712 *ice)
504{
505	int i;
506	unsigned char result;
507
508	result =  ice->gpio.get_data(ice) & GPIO_RATE_MASK;
509	for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
510		if (gpio_vals[i] == result)
511			return juli_rates[i];
512	return 0;
513}
514
515/* setting new rate */
516static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
517{
518	unsigned int old, new;
519	unsigned char val;
520
521	old = ice->gpio.get_data(ice);
522	new =  (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
523	/* dev_dbg(ice->card->dev, "JULI - set_rate: old %x, new %x\n",
524			old & GPIO_RATE_MASK,
525			new & GPIO_RATE_MASK); */
526
527	ice->gpio.set_data(ice, new);
528	/* switching to external clock - supplied by external circuits */
529	val = inb(ICEMT1724(ice, RATE));
530	outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
531}
532
533static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
534					  unsigned int rate)
535{
536	/* no change in master clock */
537	return 0;
538}
539
540/* setting clock to external - SPDIF */
541static int juli_set_spdif_clock(struct snd_ice1712 *ice, int type)
542{
543	unsigned int old;
544	old = ice->gpio.get_data(ice);
545	/* external clock (= 0), multiply 1x, 48kHz */
546	ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
547			GPIO_FREQ_48KHZ);
548	return 0;
549}
550
551/* Called when ak4114 detects change in the input SPDIF stream */
552static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
553			       unsigned char c1)
554{
555	struct snd_ice1712 *ice = ak4114->change_callback_private;
556	int rate;
557	if (ice->is_spdif_master(ice) && c1) {
558		/* only for SPDIF master mode, rate was changed */
559		rate = snd_ak4114_external_rate(ak4114);
560		/* dev_dbg(ice->card->dev, "ak4114 - input rate changed to %d\n",
561				rate); */
562		juli_akm_set_rate_val(ice->akm, rate);
563	}
564}
565
566static int juli_init(struct snd_ice1712 *ice)
567{
568	static const unsigned char ak4114_init_vals[] = {
569		/* AK4117_REG_PWRDN */	AK4114_RST | AK4114_PWN |
570					AK4114_OCKS0 | AK4114_OCKS1,
571		/* AK4114_REQ_FORMAT */	AK4114_DIF_I24I2S,
572		/* AK4114_REG_IO0 */	AK4114_TX1E,
573		/* AK4114_REG_IO1 */	AK4114_EFH_1024 | AK4114_DIT |
574					AK4114_IPS(1),
575		/* AK4114_REG_INT0_MASK */ 0,
576		/* AK4114_REG_INT1_MASK */ 0
577	};
578	static const unsigned char ak4114_init_txcsb[] = {
579		0x41, 0x02, 0x2c, 0x00, 0x00
580	};
581	int err;
582	struct juli_spec *spec;
583	struct snd_akm4xxx *ak;
584
585	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
586	if (!spec)
587		return -ENOMEM;
588	ice->spec = spec;
589
590	err = snd_ak4114_create(ice->card,
591				juli_ak4114_read,
592				juli_ak4114_write,
593				ak4114_init_vals, ak4114_init_txcsb,
594				ice, &spec->ak4114);
595	if (err < 0)
596		return err;
597	/* callback for codecs rate setting */
598	spec->ak4114->change_callback = juli_ak4114_change;
599	spec->ak4114->change_callback_private = ice;
600	/* AK4114 in Juli can detect external rate correctly */
601	spec->ak4114->check_flags = 0;
602
603#if 0
604/*
605 * it seems that the analog doughter board detection does not work reliably, so
606 * force the analog flag; it should be very rare (if ever) to come at Juli@
607 * used without the analog daughter board
608 */
609	spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
610#else
611	spec->analog = 1;
612#endif
613
614	if (spec->analog) {
615		dev_info(ice->card->dev, "juli@: analog I/O detected\n");
616		ice->num_total_dacs = 2;
617		ice->num_total_adcs = 2;
618
619		ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
620		ak = ice->akm;
621		if (!ak)
622			return -ENOMEM;
623		ice->akm_codecs = 1;
624		err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
625		if (err < 0)
626			return err;
627	}
628
629	/* juli is clocked by Xilinx array */
630	ice->hw_rates = &juli_rates_info;
631	ice->is_spdif_master = juli_is_spdif_master;
632	ice->get_rate = juli_get_rate;
633	ice->set_rate = juli_set_rate;
634	ice->set_mclk = juli_set_mclk;
635	ice->set_spdif_clock = juli_set_spdif_clock;
636
637	ice->spdif.ops.open = juli_spdif_in_open;
638
639#ifdef CONFIG_PM_SLEEP
640	ice->pm_resume = juli_resume;
641	ice->pm_suspend = juli_suspend;
642	ice->pm_suspend_enabled = 1;
643#endif
644
645	return 0;
646}
647
648
649/*
650 * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
651 * hence the driver needs to sets up it properly.
652 */
653
654static const unsigned char juli_eeprom[] = {
655	[ICE_EEP2_SYSCONF]     = 0x2b,	/* clock 512, mpu401, 1xADC, 1xDACs,
656					   SPDIF in */
657	[ICE_EEP2_ACLINK]      = 0x80,	/* I2S */
658	[ICE_EEP2_I2S]         = 0xf8,	/* vol, 96k, 24bit, 192k */
659	[ICE_EEP2_SPDIF]       = 0xc3,	/* out-en, out-int, spdif-in */
660	[ICE_EEP2_GPIO_DIR]    = 0x9f,	/* 5, 6:inputs; 7, 4-0 outputs*/
661	[ICE_EEP2_GPIO_DIR1]   = 0xff,
662	[ICE_EEP2_GPIO_DIR2]   = 0x7f,
663	[ICE_EEP2_GPIO_MASK]   = 0x60,	/* 5, 6: locked; 7, 4-0 writable */
664	[ICE_EEP2_GPIO_MASK1]  = 0x00,  /* 0-7 writable */
665	[ICE_EEP2_GPIO_MASK2]  = 0x7f,
666	[ICE_EEP2_GPIO_STATE]  = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
667	       GPIO_INTERNAL_CLOCK,	/* internal clock, multiple 1x, 48kHz*/
668	[ICE_EEP2_GPIO_STATE1] = 0x00,	/* unmuted */
669	[ICE_EEP2_GPIO_STATE2] = 0x00,
670};
671
672/* entry point */
673struct snd_ice1712_card_info snd_vt1724_juli_cards[] = {
674	{
675		.subvendor = VT1724_SUBDEVICE_JULI,
676		.name = "ESI Juli@",
677		.model = "juli",
678		.chip_init = juli_init,
679		.build_controls = juli_add_controls,
680		.eeprom_size = sizeof(juli_eeprom),
681		.eeprom_data = juli_eeprom,
682	},
683	{ } /* terminator */
684};
v3.15
 
  1/*
  2 *   ALSA driver for ICEnsemble VT1724 (Envy24HT)
  3 *
  4 *   Lowlevel functions for ESI Juli@ cards
  5 *
  6 *	Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
  7 *	              2008 Pavel Hofman <dustin@seznam.cz>
  8 *
  9 *
 10 *   This program is free software; you can redistribute it and/or modify
 11 *   it under the terms of the GNU General Public License as published by
 12 *   the Free Software Foundation; either version 2 of the License, or
 13 *   (at your option) any later version.
 14 *
 15 *   This program is distributed in the hope that it will be useful,
 16 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 *   GNU General Public License for more details.
 19 *
 20 *   You should have received a copy of the GNU General Public License
 21 *   along with this program; if not, write to the Free Software
 22 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 23 *
 24 */
 25
 26#include <linux/delay.h>
 27#include <linux/interrupt.h>
 28#include <linux/init.h>
 29#include <linux/slab.h>
 
 30#include <sound/core.h>
 31#include <sound/tlv.h>
 32
 33#include "ice1712.h"
 34#include "envy24ht.h"
 35#include "juli.h"
 36
 37struct juli_spec {
 38	struct ak4114 *ak4114;
 39	unsigned int analog:1;
 40};
 41
 42/*
 43 * chip addresses on I2C bus
 44 */
 45#define AK4114_ADDR		0x20		/* S/PDIF receiver */
 46#define AK4358_ADDR		0x22		/* DAC */
 47
 48/*
 49 * Juli does not use the standard ICE1724 clock scheme. Juli's ice1724 chip is
 50 * supplied by external clock provided by Xilinx array and MK73-1 PLL frequency
 51 * multiplier. Actual frequency is set by ice1724 GPIOs hooked to the Xilinx.
 52 *
 53 * The clock circuitry is supplied by the two ice1724 crystals. This
 54 * arrangement allows to generate independent clock signal for AK4114's input
 55 * rate detection circuit. As a result, Juli, unlike most other
 56 * ice1724+ak4114-based cards, detects spdif input rate correctly.
 57 * This fact is applied in the driver, allowing to modify PCM stream rate
 58 * parameter according to the actual input rate.
 59 *
 60 * Juli uses the remaining three stereo-channels of its DAC to optionally
 61 * monitor analog input, digital input, and digital output. The corresponding
 62 * I2S signals are routed by Xilinx, controlled by GPIOs.
 63 *
 64 * The master mute is implemented using output muting transistors (GPIO) in
 65 * combination with smuting the DAC.
 66 *
 67 * The card itself has no HW master volume control, implemented using the
 68 * vmaster control.
 69 *
 70 * TODO:
 71 * researching and fixing the input monitors
 72 */
 73
 74/*
 75 * GPIO pins
 76 */
 77#define GPIO_FREQ_MASK		(3<<0)
 78#define GPIO_FREQ_32KHZ		(0<<0)
 79#define GPIO_FREQ_44KHZ		(1<<0)
 80#define GPIO_FREQ_48KHZ		(2<<0)
 81#define GPIO_MULTI_MASK		(3<<2)
 82#define GPIO_MULTI_4X		(0<<2)
 83#define GPIO_MULTI_2X		(1<<2)
 84#define GPIO_MULTI_1X		(2<<2)		/* also external */
 85#define GPIO_MULTI_HALF		(3<<2)
 86#define GPIO_INTERNAL_CLOCK	(1<<4)		/* 0 = external, 1 = internal */
 87#define GPIO_CLOCK_MASK		(1<<4)
 88#define GPIO_ANALOG_PRESENT	(1<<5)		/* RO only: 0 = present */
 89#define GPIO_RXMCLK_SEL		(1<<7)		/* must be 0 */
 90#define GPIO_AK5385A_CKS0	(1<<8)
 91#define GPIO_AK5385A_DFS1	(1<<9)
 92#define GPIO_AK5385A_DFS0	(1<<10)
 93#define GPIO_DIGOUT_MONITOR	(1<<11)		/* 1 = active */
 94#define GPIO_DIGIN_MONITOR	(1<<12)		/* 1 = active */
 95#define GPIO_ANAIN_MONITOR	(1<<13)		/* 1 = active */
 96#define GPIO_AK5385A_CKS1	(1<<14)		/* must be 0 */
 97#define GPIO_MUTE_CONTROL	(1<<15)		/* output mute, 1 = muted */
 98
 99#define GPIO_RATE_MASK		(GPIO_FREQ_MASK | GPIO_MULTI_MASK | \
100		GPIO_CLOCK_MASK)
101#define GPIO_AK5385A_MASK	(GPIO_AK5385A_CKS0 | GPIO_AK5385A_DFS0 | \
102		GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS1)
103
104#define JULI_PCM_RATE	(SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
105		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
106		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
107		SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
108		SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
109
110#define GPIO_RATE_16000		(GPIO_FREQ_32KHZ | GPIO_MULTI_HALF | \
111		GPIO_INTERNAL_CLOCK)
112#define GPIO_RATE_22050		(GPIO_FREQ_44KHZ | GPIO_MULTI_HALF | \
113		GPIO_INTERNAL_CLOCK)
114#define GPIO_RATE_24000		(GPIO_FREQ_48KHZ | GPIO_MULTI_HALF | \
115		GPIO_INTERNAL_CLOCK)
116#define GPIO_RATE_32000		(GPIO_FREQ_32KHZ | GPIO_MULTI_1X | \
117		GPIO_INTERNAL_CLOCK)
118#define GPIO_RATE_44100		(GPIO_FREQ_44KHZ | GPIO_MULTI_1X | \
119		GPIO_INTERNAL_CLOCK)
120#define GPIO_RATE_48000		(GPIO_FREQ_48KHZ | GPIO_MULTI_1X | \
121		GPIO_INTERNAL_CLOCK)
122#define GPIO_RATE_64000		(GPIO_FREQ_32KHZ | GPIO_MULTI_2X | \
123		GPIO_INTERNAL_CLOCK)
124#define GPIO_RATE_88200		(GPIO_FREQ_44KHZ | GPIO_MULTI_2X | \
125		GPIO_INTERNAL_CLOCK)
126#define GPIO_RATE_96000		(GPIO_FREQ_48KHZ | GPIO_MULTI_2X | \
127		GPIO_INTERNAL_CLOCK)
128#define GPIO_RATE_176400	(GPIO_FREQ_44KHZ | GPIO_MULTI_4X | \
129		GPIO_INTERNAL_CLOCK)
130#define GPIO_RATE_192000	(GPIO_FREQ_48KHZ | GPIO_MULTI_4X | \
131		GPIO_INTERNAL_CLOCK)
132
133/*
134 * Initial setup of the conversion array GPIO <-> rate
135 */
136static unsigned int juli_rates[] = {
137	16000, 22050, 24000, 32000,
138	44100, 48000, 64000, 88200,
139	96000, 176400, 192000,
140};
141
142static unsigned int gpio_vals[] = {
143	GPIO_RATE_16000, GPIO_RATE_22050, GPIO_RATE_24000, GPIO_RATE_32000,
144	GPIO_RATE_44100, GPIO_RATE_48000, GPIO_RATE_64000, GPIO_RATE_88200,
145	GPIO_RATE_96000, GPIO_RATE_176400, GPIO_RATE_192000,
146};
147
148static struct snd_pcm_hw_constraint_list juli_rates_info = {
149	.count = ARRAY_SIZE(juli_rates),
150	.list = juli_rates,
151	.mask = 0,
152};
153
154static int get_gpio_val(int rate)
155{
156	int i;
157	for (i = 0; i < ARRAY_SIZE(juli_rates); i++)
158		if (juli_rates[i] == rate)
159			return gpio_vals[i];
160	return 0;
161}
162
163static void juli_ak4114_write(void *private_data, unsigned char reg,
164				unsigned char val)
165{
166	snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR,
167				reg, val);
168}
169
170static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
171{
172	return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data,
173					AK4114_ADDR, reg);
174}
175
176/*
177 * If SPDIF capture and slaved to SPDIF-IN, setting runtime rate
178 * to the external rate
179 */
180static void juli_spdif_in_open(struct snd_ice1712 *ice,
181				struct snd_pcm_substream *substream)
182{
183	struct juli_spec *spec = ice->spec;
184	struct snd_pcm_runtime *runtime = substream->runtime;
185	int rate;
186
187	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
188			!ice->is_spdif_master(ice))
189		return;
190	rate = snd_ak4114_external_rate(spec->ak4114);
191	if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
192		runtime->hw.rate_min = rate;
193		runtime->hw.rate_max = rate;
194	}
195}
196
197/*
198 * AK4358 section
199 */
200
201static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
202{
203}
204
205static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
206{
207}
208
209static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
210			   unsigned char addr, unsigned char data)
211{
212	struct snd_ice1712 *ice = ak->private_data[0];
213	 
214	if (snd_BUG_ON(chip))
215		return;
216	snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
217}
218
219/*
220 * change the rate of envy24HT, AK4358, AK5385
221 */
222static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
223{
224	unsigned char old, tmp, ak4358_dfs;
225	unsigned int ak5385_pins, old_gpio, new_gpio;
226	struct snd_ice1712 *ice = ak->private_data[0];
227	struct juli_spec *spec = ice->spec;
228
229	if (rate == 0)  /* no hint - S/PDIF input is master or the new spdif
230			   input rate undetected, simply return */
231		return;
232
233	/* adjust DFS on codecs */
234	if (rate > 96000)  {
235		ak4358_dfs = 2;
236		ak5385_pins = GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS0;
237	} else if (rate > 48000) {
238		ak4358_dfs = 1;
239		ak5385_pins = GPIO_AK5385A_DFS0;
240	} else {
241		ak4358_dfs = 0;
242		ak5385_pins = 0;
243	}
244	/* AK5385 first, since it requires cold reset affecting both codecs */
245	old_gpio = ice->gpio.get_data(ice);
246	new_gpio =  (old_gpio & ~GPIO_AK5385A_MASK) | ak5385_pins;
247	/* dev_dbg(ice->card->dev, "JULI - ak5385 set_rate_val: new gpio 0x%x\n",
248		new_gpio); */
249	ice->gpio.set_data(ice, new_gpio);
250
251	/* cold reset */
252	old = inb(ICEMT1724(ice, AC97_CMD));
253	outb(old | VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
254	udelay(1);
255	outb(old & ~VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
256
257	/* AK4358 */
258	/* set new value, reset DFS */
259	tmp = snd_akm4xxx_get(ak, 0, 2);
260	snd_akm4xxx_reset(ak, 1);
261	tmp = snd_akm4xxx_get(ak, 0, 2);
262	tmp &= ~(0x03 << 4);
263	tmp |= ak4358_dfs << 4;
264	snd_akm4xxx_set(ak, 0, 2, tmp);
265	snd_akm4xxx_reset(ak, 0);
266
267	/* reinit ak4114 */
268	snd_ak4114_reinit(spec->ak4114);
269}
270
271#define AK_DAC(xname, xch)	{ .name = xname, .num_channels = xch }
272#define PCM_VOLUME		"PCM Playback Volume"
273#define MONITOR_AN_IN_VOLUME	"Monitor Analog In Volume"
274#define MONITOR_DIG_IN_VOLUME	"Monitor Digital In Volume"
275#define MONITOR_DIG_OUT_VOLUME	"Monitor Digital Out Volume"
276
277static const struct snd_akm4xxx_dac_channel juli_dac[] = {
278	AK_DAC(PCM_VOLUME, 2),
279	AK_DAC(MONITOR_AN_IN_VOLUME, 2),
280	AK_DAC(MONITOR_DIG_OUT_VOLUME, 2),
281	AK_DAC(MONITOR_DIG_IN_VOLUME, 2),
282};
283
284
285static struct snd_akm4xxx akm_juli_dac = {
286	.type = SND_AK4358,
287	.num_dacs = 8,	/* DAC1 - analog out
288			   DAC2 - analog in monitor
289			   DAC3 - digital out monitor
290			   DAC4 - digital in monitor
291			 */
292	.ops = {
293		.lock = juli_akm_lock,
294		.unlock = juli_akm_unlock,
295		.write = juli_akm_write,
296		.set_rate_val = juli_akm_set_rate_val
297	},
298	.dac_info = juli_dac,
299};
300
301#define juli_mute_info		snd_ctl_boolean_mono_info
302
303static int juli_mute_get(struct snd_kcontrol *kcontrol,
304		struct snd_ctl_elem_value *ucontrol)
305{
306	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
307	unsigned int val;
308	val = ice->gpio.get_data(ice) & (unsigned int) kcontrol->private_value;
309	if (kcontrol->private_value == GPIO_MUTE_CONTROL)
310		/* val 0 = signal on */
311		ucontrol->value.integer.value[0] = (val) ? 0 : 1;
312	else
313		/* val 1 = signal on */
314		ucontrol->value.integer.value[0] = (val) ? 1 : 0;
315	return 0;
316}
317
318static int juli_mute_put(struct snd_kcontrol *kcontrol,
319		struct snd_ctl_elem_value *ucontrol)
320{
321	struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
322	unsigned int old_gpio, new_gpio;
323	old_gpio = ice->gpio.get_data(ice);
324	if (ucontrol->value.integer.value[0]) {
325		/* unmute */
326		if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
327			/* 0 = signal on */
328			new_gpio = old_gpio & ~GPIO_MUTE_CONTROL;
329			/* un-smuting DAC */
330			snd_akm4xxx_write(ice->akm, 0, 0x01, 0x01);
331		} else
332			/* 1 = signal on */
333			new_gpio =  old_gpio |
334				(unsigned int) kcontrol->private_value;
335	} else {
336		/* mute */
337		if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
338			/* 1 = signal off */
339			new_gpio = old_gpio | GPIO_MUTE_CONTROL;
340			/* smuting DAC */
341			snd_akm4xxx_write(ice->akm, 0, 0x01, 0x03);
342		} else
343			/* 0 = signal off */
344			new_gpio =  old_gpio &
345				~((unsigned int) kcontrol->private_value);
346	}
347	/* dev_dbg(ice->card->dev,
348		"JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, "
349		"new_gpio 0x%x\n",
350		(unsigned int)ucontrol->value.integer.value[0], old_gpio,
351		new_gpio); */
352	if (old_gpio != new_gpio) {
353		ice->gpio.set_data(ice, new_gpio);
354		return 1;
355	}
356	/* no change */
357	return 0;
358}
359
360static struct snd_kcontrol_new juli_mute_controls[] = {
361	{
362		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
363		.name = "Master Playback Switch",
364		.info = juli_mute_info,
365		.get = juli_mute_get,
366		.put = juli_mute_put,
367		.private_value = GPIO_MUTE_CONTROL,
368	},
369	/* Although the following functionality respects the succint NDA'd
370	 * documentation from the card manufacturer, and the same way of
371	 * operation is coded in OSS Juli driver, only Digital Out monitor
372	 * seems to work. Surprisingly, Analog input monitor outputs Digital
373	 * output data. The two are independent, as enabling both doubles
374	 * volume of the monitor sound.
375	 *
376	 * Checking traces on the board suggests the functionality described
377	 * by the manufacturer is correct - I2S from ADC and AK4114
378	 * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
379	 * inputs) are fed from Xilinx.
380	 *
381	 * I even checked traces on board and coded a support in driver for
382	 * an alternative possibility - the unused I2S ICE output channels
383	 * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
384	 * the DAC - to no avail. The I2S outputs seem to be unconnected.
385	 *
386	 * The windows driver supports the monitoring correctly.
387	 */
388	{
389		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
390		.name = "Monitor Analog In Switch",
391		.info = juli_mute_info,
392		.get = juli_mute_get,
393		.put = juli_mute_put,
394		.private_value = GPIO_ANAIN_MONITOR,
395	},
396	{
397		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
398		.name = "Monitor Digital Out Switch",
399		.info = juli_mute_info,
400		.get = juli_mute_get,
401		.put = juli_mute_put,
402		.private_value = GPIO_DIGOUT_MONITOR,
403	},
404	{
405		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
406		.name = "Monitor Digital In Switch",
407		.info = juli_mute_info,
408		.get = juli_mute_get,
409		.put = juli_mute_put,
410		.private_value = GPIO_DIGIN_MONITOR,
411	},
412};
413
414static char *slave_vols[] = {
415	PCM_VOLUME,
416	MONITOR_AN_IN_VOLUME,
417	MONITOR_DIG_IN_VOLUME,
418	MONITOR_DIG_OUT_VOLUME,
419	NULL
420};
421
422static
423DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
424
425static struct snd_kcontrol *ctl_find(struct snd_card *card,
426				     const char *name)
427{
428	struct snd_ctl_elem_id sid;
429	memset(&sid, 0, sizeof(sid));
430	/* FIXME: strcpy is bad. */
431	strcpy(sid.name, name);
432	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
433	return snd_ctl_find_id(card, &sid);
434}
435
436static void add_slaves(struct snd_card *card,
437		       struct snd_kcontrol *master,
438		       char * const *list)
439{
440	for (; *list; list++) {
441		struct snd_kcontrol *slave = ctl_find(card, *list);
442		/* dev_dbg(card->dev, "add_slaves - %s\n", *list); */
443		if (slave) {
444			/* dev_dbg(card->dev, "slave %s found\n", *list); */
445			snd_ctl_add_slave(master, slave);
446		}
447	}
448}
449
450static int juli_add_controls(struct snd_ice1712 *ice)
451{
452	struct juli_spec *spec = ice->spec;
453	int err;
454	unsigned int i;
455	struct snd_kcontrol *vmaster;
456
457	err = snd_ice1712_akm4xxx_build_controls(ice);
458	if (err < 0)
459		return err;
460
461	for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
462		err = snd_ctl_add(ice->card,
463				snd_ctl_new1(&juli_mute_controls[i], ice));
464		if (err < 0)
465			return err;
466	}
467	/* Create virtual master control */
468	vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
469					      juli_master_db_scale);
470	if (!vmaster)
471		return -ENOMEM;
472	add_slaves(ice->card, vmaster, slave_vols);
473	err = snd_ctl_add(ice->card, vmaster);
474	if (err < 0)
475		return err;
476
477	/* only capture SPDIF over AK4114 */
478	err = snd_ak4114_build(spec->ak4114, NULL,
479			ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
480	if (err < 0)
481		return err;
482	return 0;
483}
484
485/*
486 * suspend/resume
487 * */
488
489#ifdef CONFIG_PM_SLEEP
490static int juli_resume(struct snd_ice1712 *ice)
491{
492	struct snd_akm4xxx *ak = ice->akm;
493	struct juli_spec *spec = ice->spec;
494	/* akm4358 un-reset, un-mute */
495	snd_akm4xxx_reset(ak, 0);
496	/* reinit ak4114 */
497	snd_ak4114_reinit(spec->ak4114);
498	return 0;
499}
500
501static int juli_suspend(struct snd_ice1712 *ice)
502{
503	struct snd_akm4xxx *ak = ice->akm;
 
504	/* akm4358 reset and soft-mute */
505	snd_akm4xxx_reset(ak, 1);
 
506	return 0;
507}
508#endif
509
510/*
511 * initialize the chip
512 */
513
514static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
515{
516	return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
517}
518
519static unsigned int juli_get_rate(struct snd_ice1712 *ice)
520{
521	int i;
522	unsigned char result;
523
524	result =  ice->gpio.get_data(ice) & GPIO_RATE_MASK;
525	for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
526		if (gpio_vals[i] == result)
527			return juli_rates[i];
528	return 0;
529}
530
531/* setting new rate */
532static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
533{
534	unsigned int old, new;
535	unsigned char val;
536
537	old = ice->gpio.get_data(ice);
538	new =  (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
539	/* dev_dbg(ice->card->dev, "JULI - set_rate: old %x, new %x\n",
540			old & GPIO_RATE_MASK,
541			new & GPIO_RATE_MASK); */
542
543	ice->gpio.set_data(ice, new);
544	/* switching to external clock - supplied by external circuits */
545	val = inb(ICEMT1724(ice, RATE));
546	outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
547}
548
549static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
550					  unsigned int rate)
551{
552	/* no change in master clock */
553	return 0;
554}
555
556/* setting clock to external - SPDIF */
557static int juli_set_spdif_clock(struct snd_ice1712 *ice, int type)
558{
559	unsigned int old;
560	old = ice->gpio.get_data(ice);
561	/* external clock (= 0), multiply 1x, 48kHz */
562	ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
563			GPIO_FREQ_48KHZ);
564	return 0;
565}
566
567/* Called when ak4114 detects change in the input SPDIF stream */
568static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
569			       unsigned char c1)
570{
571	struct snd_ice1712 *ice = ak4114->change_callback_private;
572	int rate;
573	if (ice->is_spdif_master(ice) && c1) {
574		/* only for SPDIF master mode, rate was changed */
575		rate = snd_ak4114_external_rate(ak4114);
576		/* dev_dbg(ice->card->dev, "ak4114 - input rate changed to %d\n",
577				rate); */
578		juli_akm_set_rate_val(ice->akm, rate);
579	}
580}
581
582static int juli_init(struct snd_ice1712 *ice)
583{
584	static const unsigned char ak4114_init_vals[] = {
585		/* AK4117_REG_PWRDN */	AK4114_RST | AK4114_PWN |
586					AK4114_OCKS0 | AK4114_OCKS1,
587		/* AK4114_REQ_FORMAT */	AK4114_DIF_I24I2S,
588		/* AK4114_REG_IO0 */	AK4114_TX1E,
589		/* AK4114_REG_IO1 */	AK4114_EFH_1024 | AK4114_DIT |
590					AK4114_IPS(1),
591		/* AK4114_REG_INT0_MASK */ 0,
592		/* AK4114_REG_INT1_MASK */ 0
593	};
594	static const unsigned char ak4114_init_txcsb[] = {
595		0x41, 0x02, 0x2c, 0x00, 0x00
596	};
597	int err;
598	struct juli_spec *spec;
599	struct snd_akm4xxx *ak;
600
601	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
602	if (!spec)
603		return -ENOMEM;
604	ice->spec = spec;
605
606	err = snd_ak4114_create(ice->card,
607				juli_ak4114_read,
608				juli_ak4114_write,
609				ak4114_init_vals, ak4114_init_txcsb,
610				ice, &spec->ak4114);
611	if (err < 0)
612		return err;
613	/* callback for codecs rate setting */
614	spec->ak4114->change_callback = juli_ak4114_change;
615	spec->ak4114->change_callback_private = ice;
616	/* AK4114 in Juli can detect external rate correctly */
617	spec->ak4114->check_flags = 0;
618
619#if 0
620/*
621 * it seems that the analog doughter board detection does not work reliably, so
622 * force the analog flag; it should be very rare (if ever) to come at Juli@
623 * used without the analog daughter board
624 */
625	spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
626#else
627	spec->analog = 1;
628#endif
629
630	if (spec->analog) {
631		dev_info(ice->card->dev, "juli@: analog I/O detected\n");
632		ice->num_total_dacs = 2;
633		ice->num_total_adcs = 2;
634
635		ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
636		ak = ice->akm;
637		if (!ak)
638			return -ENOMEM;
639		ice->akm_codecs = 1;
640		err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
641		if (err < 0)
642			return err;
643	}
644
645	/* juli is clocked by Xilinx array */
646	ice->hw_rates = &juli_rates_info;
647	ice->is_spdif_master = juli_is_spdif_master;
648	ice->get_rate = juli_get_rate;
649	ice->set_rate = juli_set_rate;
650	ice->set_mclk = juli_set_mclk;
651	ice->set_spdif_clock = juli_set_spdif_clock;
652
653	ice->spdif.ops.open = juli_spdif_in_open;
654
655#ifdef CONFIG_PM_SLEEP
656	ice->pm_resume = juli_resume;
657	ice->pm_suspend = juli_suspend;
658	ice->pm_suspend_enabled = 1;
659#endif
660
661	return 0;
662}
663
664
665/*
666 * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
667 * hence the driver needs to sets up it properly.
668 */
669
670static unsigned char juli_eeprom[] = {
671	[ICE_EEP2_SYSCONF]     = 0x2b,	/* clock 512, mpu401, 1xADC, 1xDACs,
672					   SPDIF in */
673	[ICE_EEP2_ACLINK]      = 0x80,	/* I2S */
674	[ICE_EEP2_I2S]         = 0xf8,	/* vol, 96k, 24bit, 192k */
675	[ICE_EEP2_SPDIF]       = 0xc3,	/* out-en, out-int, spdif-in */
676	[ICE_EEP2_GPIO_DIR]    = 0x9f,	/* 5, 6:inputs; 7, 4-0 outputs*/
677	[ICE_EEP2_GPIO_DIR1]   = 0xff,
678	[ICE_EEP2_GPIO_DIR2]   = 0x7f,
679	[ICE_EEP2_GPIO_MASK]   = 0x60,	/* 5, 6: locked; 7, 4-0 writable */
680	[ICE_EEP2_GPIO_MASK1]  = 0x00,  /* 0-7 writable */
681	[ICE_EEP2_GPIO_MASK2]  = 0x7f,
682	[ICE_EEP2_GPIO_STATE]  = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
683	       GPIO_INTERNAL_CLOCK,	/* internal clock, multiple 1x, 48kHz*/
684	[ICE_EEP2_GPIO_STATE1] = 0x00,	/* unmuted */
685	[ICE_EEP2_GPIO_STATE2] = 0x00,
686};
687
688/* entry point */
689struct snd_ice1712_card_info snd_vt1724_juli_cards[] = {
690	{
691		.subvendor = VT1724_SUBDEVICE_JULI,
692		.name = "ESI Juli@",
693		.model = "juli",
694		.chip_init = juli_init,
695		.build_controls = juli_add_controls,
696		.eeprom_size = sizeof(juli_eeprom),
697		.eeprom_data = juli_eeprom,
698	},
699	{ } /* terminator */
700};