xref: /dragonfly/sys/dev/sound/pci/spicds.c (revision fcf53d9b)
1 /*
2  * Copyright (c) 2006 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
3  * Copyright (c) 2001 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/sound/pci/spicds.c,v 1.5.2.2 2007/06/11 19:33:27 ariff Exp $
28  * $DragonFly: src/sys/dev/sound/pci/spicds.c,v 1.3 2007/06/27 13:26:18 hasso Exp $
29  */
30 
31 #include <dev/sound/pcm/sound.h>
32 
33 #include <dev/sound/pci/spicds.h>
34 
35 MALLOC_DEFINE(M_SPICDS, "spicds", "SPI codec");
36 
37 #define SPICDS_NAMELEN	16
38 struct spicds_info {
39 	device_t dev;
40 	spicds_ctrl ctrl;
41 	void *devinfo;
42 	int num; /* number of this device */
43 	unsigned int type;   /* codec type */
44 	unsigned int cif;    /* Controll data Interface Format (0/1) */
45 	unsigned int format; /* data format and master clock frequency */
46 	unsigned int dvc;    /* De-emphasis and Volume Control */
47 	unsigned int left, right;
48 	char name[SPICDS_NAMELEN];
49 	sndlock_t	lock;
50 };
51 
52 static void
53 spicds_wrbit(struct spicds_info *codec, int bit)
54 {
55 	unsigned int cs, cdti;
56 	if (codec->cif)
57 		cs = 1;
58 	else
59 		cs = 0;
60 	if (bit)
61 		cdti = 1;
62 	else
63 		cdti = 0;
64 	codec->ctrl(codec->devinfo, cs, 0, cdti);
65 	DELAY(1);
66 	codec->ctrl(codec->devinfo, cs, 1, cdti);
67 	DELAY(1);
68 
69 	return;
70 }
71 
72 static void
73 spicds_wrcd(struct spicds_info *codec, int reg, u_int16_t val)
74 {
75 	int mask;
76 
77 #if(0)
78 	device_printf(codec->dev, "spicds_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
79 #endif
80 	/* start */
81 	if (codec->cif)
82 		codec->ctrl(codec->devinfo, 1, 1, 0);
83 	else
84 		codec->ctrl(codec->devinfo, 0, 1, 0);
85 	DELAY(1);
86 	if (codec->type != SPICDS_TYPE_WM8770) {
87 	if (codec->type == SPICDS_TYPE_AK4381) {
88 	/* AK4381 chip address */
89         spicds_wrbit(codec, 0);
90         spicds_wrbit(codec, 1);
91 	}
92 	else if (codec->type == SPICDS_TYPE_AK4396)
93 	{
94 	/* AK4396 chip address */
95         spicds_wrbit(codec, 0);
96         spicds_wrbit(codec, 0);
97 	}
98 	else {
99 	/* chip address */
100 	spicds_wrbit(codec, 1);
101 	spicds_wrbit(codec, 0);
102 	}
103 	/* write */
104 	spicds_wrbit(codec, 1);
105 	/* register address */
106 	for (mask = 0x10; mask != 0; mask >>= 1)
107 		spicds_wrbit(codec, reg & mask);
108 	/* data */
109 	for (mask = 0x80; mask != 0; mask >>= 1)
110 		spicds_wrbit(codec, val & mask);
111 	/* stop */
112 	DELAY(1);
113 	}
114 	else {
115         /* register address */
116         for (mask = 0x40; mask != 0; mask >>= 1)
117                 spicds_wrbit(codec, reg & mask);
118         /* data */
119         for (mask = 0x100; mask != 0; mask >>= 1)
120                 spicds_wrbit(codec, val & mask);
121         /* stop */
122         DELAY(1);
123 	}
124 	if (codec->cif) {
125 		codec->ctrl(codec->devinfo, 0, 1, 0);
126 		DELAY(1);
127 		codec->ctrl(codec->devinfo, 1, 1, 0);
128 	}
129 	else {
130 		codec->ctrl(codec->devinfo, 1, 1, 0);
131 	}
132 
133 	return;
134 }
135 
136 struct spicds_info *
137 spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
138 {
139 	struct spicds_info *codec;
140 
141 #if(0)
142 	device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
143 #endif
144 	codec = kmalloc(sizeof *codec, M_SPICDS, M_WAITOK);
145 
146 	ksnprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
147 	codec->lock = snd_mtxcreate(codec->name, codec->name);
148 	codec->dev = dev;
149 	codec->ctrl = ctrl;
150 	codec->devinfo = devinfo;
151 	codec->num = num;
152 	codec->type = SPICDS_TYPE_AK4524;
153 	codec->cif = 0;
154 	codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
155 	codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
156 
157 	return codec;
158 }
159 
160 void
161 spicds_destroy(struct spicds_info *codec)
162 {
163 	snd_mtxfree(codec->lock);
164 	kfree(codec, M_SPICDS);
165 }
166 
167 void
168 spicds_settype(struct spicds_info *codec, unsigned int type)
169 {
170 	snd_mtxlock(codec->lock);
171 	codec->type = type;
172 	snd_mtxunlock(codec->lock);
173 }
174 
175 void
176 spicds_setcif(struct spicds_info *codec, unsigned int cif)
177 {
178 	snd_mtxlock(codec->lock);
179 	codec->cif = cif;
180 	snd_mtxunlock(codec->lock);
181 }
182 
183 void
184 spicds_setformat(struct spicds_info *codec, unsigned int format)
185 {
186 	snd_mtxlock(codec->lock);
187 	codec->format = format;
188 	snd_mtxunlock(codec->lock);
189 }
190 
191 void
192 spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
193 {
194 	snd_mtxlock(codec->lock);
195 	codec->dvc = dvc;
196 	snd_mtxunlock(codec->lock);
197 }
198 
199 void
200 spicds_init(struct spicds_info *codec)
201 {
202 #if(0)
203 	device_printf(codec->dev, "spicds_init(codec)\n");
204 #endif
205 	snd_mtxlock(codec->lock);
206 	if (codec->type == SPICDS_TYPE_AK4524 ||\
207 	    codec->type == SPICDS_TYPE_AK4528) {
208 	/* power off */
209 	spicds_wrcd(codec, AK4524_POWER, 0);
210 	/* set parameter */
211 	spicds_wrcd(codec, AK4524_FORMAT, codec->format);
212 	spicds_wrcd(codec, AK4524_DVC, codec->dvc);
213 	/* power on */
214 	spicds_wrcd(codec, AK4524_POWER, AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
215 	/* free reset register */
216 	spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
217 	}
218 	if (codec->type == SPICDS_TYPE_WM8770) {
219 	/* WM8770 init values are taken from ALSA */
220         /* These come first to reduce init pop noise */
221 	spicds_wrcd(codec, 0x1b, 0x044);	/* ADC Mux (AC'97 source) */
222 	spicds_wrcd(codec, 0x1c, 0x00B);	/* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
223 	spicds_wrcd(codec, 0x1d, 0x009);	/* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
224 
225 	spicds_wrcd(codec, 0x18, 0x000);	/* All power-up */
226 
227 	spicds_wrcd(codec, 0x16, 0x122);	/* I2S, normal polarity, 24bit */
228 	spicds_wrcd(codec, 0x17, 0x022);	/* 256fs, slave mode */
229 
230 	spicds_wrcd(codec, 0x19, 0x000);	/* -12dB ADC/L */
231 	spicds_wrcd(codec, 0x1a, 0x000);	/* -12dB ADC/R */
232 	}
233 	if (codec->type == SPICDS_TYPE_AK4358)
234 	spicds_wrcd(codec, 0x00, 0x07);		/* I2S, 24bit, power-up */
235 	if (codec->type == SPICDS_TYPE_AK4381)
236 	spicds_wrcd(codec, 0x00, 0x0f);		/* I2S, 24bit, power-up */
237 	if (codec->type == SPICDS_TYPE_AK4396)
238 	spicds_wrcd(codec, 0x00, 0x07);		/* I2S, 24bit, power-up */
239 	snd_mtxunlock(codec->lock);
240 }
241 
242 void
243 spicds_reinit(struct spicds_info *codec)
244 {
245 	snd_mtxlock(codec->lock);
246 	if (codec->type != SPICDS_TYPE_WM8770) {
247 	/* reset */
248 	spicds_wrcd(codec, AK4524_RESET, 0);
249 	/* set parameter */
250 	spicds_wrcd(codec, AK4524_FORMAT, codec->format);
251 	spicds_wrcd(codec, AK4524_DVC, codec->dvc);
252 	/* free reset register */
253 	spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
254 	}
255 	else {
256 	/* WM8770 reinit */
257 	/* AK4358 reinit */
258 	/* AK4381 reinit */
259 	}
260 	snd_mtxunlock(codec->lock);
261 }
262 
263 void
264 spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
265 {
266 #if(0)
267 	device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
268 #endif
269 	snd_mtxlock(codec->lock);
270 	if (left >= 100)
271 		if ((codec->type == SPICDS_TYPE_AK4381) || \
272 		(codec->type == SPICDS_TYPE_AK4396))
273 			left = 255;
274 		else
275 			left = 127;
276 	else
277 		switch (codec->type) {
278 		case SPICDS_TYPE_WM8770:
279 			left = left + 27;
280 			break;
281 		case SPICDS_TYPE_AK4381:
282 		case SPICDS_TYPE_AK4396:
283 			left = left * 255 / 100;
284 			break;
285 		default:
286 			left = left * 127 / 100;
287 		}
288 	if (right >= 100)
289 		if ((codec->type == SPICDS_TYPE_AK4381) || \
290 		(codec->type == SPICDS_TYPE_AK4396))
291                         right = 255;
292                 else
293 			right  = 127;
294 	else
295 		switch (codec->type) {
296 		case SPICDS_TYPE_WM8770:
297                         right = right + 27;
298 			break;
299 		case SPICDS_TYPE_AK4381:
300 		case SPICDS_TYPE_AK4396:
301 			right = right * 255 / 100;
302 			break;
303                 default:
304                         right = right * 127 / 100;
305 		}
306 	if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
307 #if(0)
308 		device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
309 #endif
310 		spicds_wrcd(codec, AK4524_LIPGA, left);
311 		spicds_wrcd(codec, AK4524_RIPGA, right);
312 	}
313 	if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
314 #if(0)
315 		device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
316 #endif
317 		spicds_wrcd(codec, AK4524_LOATT, left);
318 		spicds_wrcd(codec, AK4524_ROATT, right);
319 	}
320 	if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
321 #if(0)
322 		device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
323 #endif
324 		spicds_wrcd(codec, AK4528_LOATT, left);
325 		spicds_wrcd(codec, AK4528_ROATT, right);
326 	}
327         if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
328 #if(0)
329                 device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
330 #endif
331                 spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
332                 spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
333         }
334         if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
335 #if(0)
336                 device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
337 #endif
338                 spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
339                 spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
340         }
341         if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
342 #if(0)
343                 device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
344 #endif
345                 spicds_wrcd(codec, AK4381_LOATT, left);
346                 spicds_wrcd(codec, AK4381_ROATT, right);
347         }
348 
349         if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4396) {
350 #if(0)
351                 device_printf(codec->dev, "spicds_set(): AK4396(PLAY) %d/%d\n", left, right);
352 #endif
353                 spicds_wrcd(codec, AK4396_LOATT, left);
354                 spicds_wrcd(codec, AK4396_ROATT, right);
355         }
356 
357 	snd_mtxunlock(codec->lock);
358 }
359 
360 MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
361 MODULE_VERSION(snd_spicds, 1);
362