xref: /openbmc/linux/sound/pci/ice1712/juli.c (revision c93f5a1eca1f6d662d791c14c469b6962e05a08f)
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  *
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 <asm/io.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <sound/core.h>
30 
31 #include "ice1712.h"
32 #include "envy24ht.h"
33 #include "juli.h"
34 
35 struct juli_spec {
36 	struct ak4114 *ak4114;
37 	unsigned int analog: 1;
38 };
39 
40 /*
41  * chip addresses on I2C bus
42  */
43 #define AK4114_ADDR		0x20		/* S/PDIF receiver */
44 #define AK4358_ADDR		0x22		/* DAC */
45 
46 /*
47  * GPIO pins
48  */
49 #define GPIO_FREQ_MASK		(3<<0)
50 #define GPIO_FREQ_32KHZ		(0<<0)
51 #define GPIO_FREQ_44KHZ		(1<<0)
52 #define GPIO_FREQ_48KHZ		(2<<0)
53 #define GPIO_MULTI_MASK		(3<<2)
54 #define GPIO_MULTI_4X		(0<<2)
55 #define GPIO_MULTI_2X		(1<<2)
56 #define GPIO_MULTI_1X		(2<<2)		/* also external */
57 #define GPIO_MULTI_HALF		(3<<2)
58 #define GPIO_INTERNAL_CLOCK	(1<<4)
59 #define GPIO_ANALOG_PRESENT	(1<<5)		/* RO only: 0 = present */
60 #define GPIO_RXMCLK_SEL		(1<<7)		/* must be 0 */
61 #define GPIO_AK5385A_CKS0	(1<<8)
62 #define GPIO_AK5385A_DFS0	(1<<9)		/* swapped with DFS1 according doc? */
63 #define GPIO_AK5385A_DFS1	(1<<10)
64 #define GPIO_DIGOUT_MONITOR	(1<<11)		/* 1 = active */
65 #define GPIO_DIGIN_MONITOR	(1<<12)		/* 1 = active */
66 #define GPIO_ANAIN_MONITOR	(1<<13)		/* 1 = active */
67 #define GPIO_AK5385A_MCLK	(1<<14)		/* must be 0 */
68 #define GPIO_MUTE_CONTROL	(1<<15)		/* 0 = off, 1 = on */
69 
70 static void juli_ak4114_write(void *private_data, unsigned char reg, unsigned char val)
71 {
72 	snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR, reg, val);
73 }
74 
75 static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
76 {
77 	return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR, reg);
78 }
79 
80 static void juli_spdif_in_open(struct snd_ice1712 *ice,
81 			       struct snd_pcm_substream *substream)
82 {
83 	struct juli_spec *spec = ice->spec;
84 	struct snd_pcm_runtime *runtime = substream->runtime;
85 	int rate;
86 
87 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
88 		return;
89 	rate = snd_ak4114_external_rate(spec->ak4114);
90 	if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
91 		runtime->hw.rate_min = rate;
92 		runtime->hw.rate_max = rate;
93 	}
94 }
95 
96 /*
97  * AK4358 section
98  */
99 
100 static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
101 {
102 }
103 
104 static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
105 {
106 }
107 
108 static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
109 			   unsigned char addr, unsigned char data)
110 {
111 	struct snd_ice1712 *ice = ak->private_data[0];
112 
113 	snd_assert(chip == 0, return);
114 	snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
115 }
116 
117 /*
118  * change the rate of envy24HT, AK4358
119  */
120 static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
121 {
122 	unsigned char old, tmp, dfs;
123 
124 	if (rate == 0)  /* no hint - S/PDIF input is master, simply return */
125 		return;
126 
127 	/* adjust DFS on codecs */
128 	if (rate > 96000)
129 		dfs = 2;
130 	else if (rate > 48000)
131 		dfs = 1;
132 	else
133 		dfs = 0;
134 
135 	tmp = snd_akm4xxx_get(ak, 0, 2);
136 	old = (tmp >> 4) & 0x03;
137 	if (old == dfs)
138 		return;
139 	/* reset DFS */
140 	snd_akm4xxx_reset(ak, 1);
141 	tmp = snd_akm4xxx_get(ak, 0, 2);
142 	tmp &= ~(0x03 << 4);
143 	tmp |= dfs << 4;
144 	snd_akm4xxx_set(ak, 0, 2, tmp);
145 	snd_akm4xxx_reset(ak, 0);
146 }
147 
148 static struct snd_akm4xxx akm_juli_dac __devinitdata = {
149 	.type = SND_AK4358,
150 	.num_dacs = 2,
151 	.ops = {
152 		.lock = juli_akm_lock,
153 		.unlock = juli_akm_unlock,
154 		.write = juli_akm_write,
155 		.set_rate_val = juli_akm_set_rate_val
156 	}
157 };
158 
159 static int __devinit juli_add_controls(struct snd_ice1712 *ice)
160 {
161 	struct juli_spec *spec = ice->spec;
162 	int err;
163 	err = snd_ice1712_akm4xxx_build_controls(ice);
164 	if (err < 0)
165 		return err;
166 	/* only capture SPDIF over AK4114 */
167 	err = snd_ak4114_build(spec->ak4114, NULL,
168 			       ice->pcm_pro->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
169 	if (err < 0)
170 		return err;
171 	return 0;
172 }
173 
174 /*
175  * initialize the chip
176  */
177 static int __devinit juli_init(struct snd_ice1712 *ice)
178 {
179 	static const unsigned char ak4114_init_vals[] = {
180 		/* AK4117_REG_PWRDN */	AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
181 		/* AK4114_REQ_FORMAT */	AK4114_DIF_I24I2S,
182 		/* AK4114_REG_IO0 */	AK4114_TX1E,
183 		/* AK4114_REG_IO1 */	AK4114_EFH_1024 | AK4114_DIT | AK4114_IPS(1),
184 		/* AK4114_REG_INT0_MASK */ 0,
185 		/* AK4114_REG_INT1_MASK */ 0
186 	};
187 	static const unsigned char ak4114_init_txcsb[] = {
188 		0x41, 0x02, 0x2c, 0x00, 0x00
189 	};
190 	int err;
191 	struct juli_spec *spec;
192 	struct snd_akm4xxx *ak;
193 
194 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
195 	if (!spec)
196 		return -ENOMEM;
197 	ice->spec = spec;
198 
199 	err = snd_ak4114_create(ice->card,
200 				juli_ak4114_read,
201 				juli_ak4114_write,
202 				ak4114_init_vals, ak4114_init_txcsb,
203 				ice, &spec->ak4114);
204 	if (err < 0)
205 		return err;
206 
207 #if 0
208         /* it seems that the analog doughter board detection does not work
209            reliably, so force the analog flag; it should be very rare
210            to use Juli@ without the analog doughter board */
211 	spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
212 #else
213         spec->analog = 1;
214 #endif
215 
216 	if (spec->analog) {
217 		printk(KERN_INFO "juli@: analog I/O detected\n");
218 		ice->num_total_dacs = 2;
219 		ice->num_total_adcs = 2;
220 
221 		ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
222 		if (! ak)
223 			return -ENOMEM;
224 		ice->akm_codecs = 1;
225 		if ((err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice)) < 0)
226 			return err;
227 	}
228 
229 	ice->spdif.ops.open = juli_spdif_in_open;
230 	return 0;
231 }
232 
233 
234 /*
235  * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
236  * hence the driver needs to sets up it properly.
237  */
238 
239 static unsigned char juli_eeprom[] __devinitdata = {
240 	[ICE_EEP2_SYSCONF]     = 0x20,	/* clock 512, mpu401, 1xADC, 1xDACs */
241 	[ICE_EEP2_ACLINK]      = 0x80,	/* I2S */
242 	[ICE_EEP2_I2S]         = 0xf8,	/* vol, 96k, 24bit, 192k */
243 	[ICE_EEP2_SPDIF]       = 0xc3,	/* out-en, out-int, spdif-in */
244 	[ICE_EEP2_GPIO_DIR]    = 0x9f,
245 	[ICE_EEP2_GPIO_DIR1]   = 0xff,
246 	[ICE_EEP2_GPIO_DIR2]   = 0x7f,
247 	[ICE_EEP2_GPIO_MASK]   = 0x9f,
248 	[ICE_EEP2_GPIO_MASK1]  = 0xff,
249 	[ICE_EEP2_GPIO_MASK2]  = 0x7f,
250 	[ICE_EEP2_GPIO_STATE]  = 0x16,	/* internal clock, multiple 1x, 48kHz */
251 	[ICE_EEP2_GPIO_STATE1] = 0x80,	/* mute */
252 	[ICE_EEP2_GPIO_STATE2] = 0x00,
253 };
254 
255 /* entry point */
256 struct snd_ice1712_card_info snd_vt1724_juli_cards[] __devinitdata = {
257 	{
258 		.subvendor = VT1724_SUBDEVICE_JULI,
259 		.name = "ESI Juli@",
260 		.model = "juli",
261 		.chip_init = juli_init,
262 		.build_controls = juli_add_controls,
263 		.eeprom_size = sizeof(juli_eeprom),
264 		.eeprom_data = juli_eeprom,
265 	},
266 	{ } /* terminator */
267 };
268