spicds.c revision 162874
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: head/sys/dev/sound/pci/spicds.c 162874 2006-09-30 16:53:40Z netchild $
28 */
29
30#include <dev/sound/pcm/sound.h>
31
32#include <dev/sound/pci/spicds.h>
33
34MALLOC_DEFINE(M_SPICDS, "spicds", "SPI codec");
35
36#define SPICDS_NAMELEN	16
37struct spicds_info {
38	device_t dev;
39	spicds_ctrl ctrl;
40	void *devinfo;
41	int num; /* number of this device */
42	unsigned int type;   /* codec type */
43	unsigned int cif;    /* Controll data Interface Format (0/1) */
44	unsigned int format; /* data format and master clock frequency */
45	unsigned int dvc;    /* De-emphasis and Volume Control */
46	unsigned int left, right;
47	char name[SPICDS_NAMELEN];
48	void *lock;
49};
50
51static void
52spicds_wrbit(struct spicds_info *codec, int bit)
53{
54	unsigned int cs, cdti;
55	if (codec->cif)
56		cs = 1;
57	else
58		cs = 0;
59	if (bit)
60		cdti = 1;
61	else
62		cdti = 0;
63	codec->ctrl(codec->devinfo, cs, 0, cdti);
64	DELAY(1);
65	codec->ctrl(codec->devinfo, cs, 1, cdti);
66	DELAY(1);
67
68	return;
69}
70
71static void
72spicds_wrcd(struct spicds_info *codec, int reg, u_int16_t val)
73{
74	int mask;
75
76#if(0)
77	device_printf(codec->dev, "spicds_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
78#endif
79	/* start */
80	if (codec->cif)
81		codec->ctrl(codec->devinfo, 1, 1, 0);
82	else
83		codec->ctrl(codec->devinfo, 0, 1, 0);
84	DELAY(1);
85	if (codec->type != SPICDS_TYPE_WM8770) {
86	if (codec->type == SPICDS_TYPE_AK4381) {
87	/* AK4381 chip address */
88        spicds_wrbit(codec, 0);
89        spicds_wrbit(codec, 1);
90	}
91	else {
92	/* chip address */
93	spicds_wrbit(codec, 1);
94	spicds_wrbit(codec, 0);
95	}
96	/* write */
97	spicds_wrbit(codec, 1);
98	/* register address */
99	for (mask = 0x10; mask != 0; mask >>= 1)
100		spicds_wrbit(codec, reg & mask);
101	/* data */
102	for (mask = 0x80; mask != 0; mask >>= 1)
103		spicds_wrbit(codec, val & mask);
104	/* stop */
105	DELAY(1);
106	}
107	else {
108        /* register address */
109        for (mask = 0x40; mask != 0; mask >>= 1)
110                spicds_wrbit(codec, reg & mask);
111        /* data */
112        for (mask = 0x100; mask != 0; mask >>= 1)
113                spicds_wrbit(codec, val & mask);
114        /* stop */
115        DELAY(1);
116	}
117	if (codec->cif) {
118		codec->ctrl(codec->devinfo, 0, 1, 0);
119		DELAY(1);
120		codec->ctrl(codec->devinfo, 1, 1, 0);
121	}
122	else {
123		codec->ctrl(codec->devinfo, 1, 1, 0);
124	}
125
126	return;
127}
128
129struct spicds_info *
130spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
131{
132	struct spicds_info *codec;
133
134#if(0)
135	device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
136#endif
137	codec = (struct spicds_info *)malloc(sizeof *codec, M_SPICDS, M_NOWAIT);
138	if (codec == NULL)
139		return NULL;
140
141	snprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
142	codec->lock = snd_mtxcreate(codec->name, codec->name);
143	codec->dev = dev;
144	codec->ctrl = ctrl;
145	codec->devinfo = devinfo;
146	codec->num = num;
147	codec->type = SPICDS_TYPE_AK4524;
148	codec->cif = 0;
149	codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
150	codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
151
152	return codec;
153}
154
155void
156spicds_destroy(struct spicds_info *codec)
157{
158	snd_mtxfree(codec->lock);
159	free(codec, M_SPICDS);
160}
161
162void
163spicds_settype(struct spicds_info *codec, unsigned int type)
164{
165	snd_mtxlock(codec->lock);
166	codec->type = type;
167	snd_mtxunlock(codec->lock);
168}
169
170void
171spicds_setcif(struct spicds_info *codec, unsigned int cif)
172{
173	snd_mtxlock(codec->lock);
174	codec->cif = cif;
175	snd_mtxunlock(codec->lock);
176}
177
178void
179spicds_setformat(struct spicds_info *codec, unsigned int format)
180{
181	snd_mtxlock(codec->lock);
182	codec->format = format;
183	snd_mtxunlock(codec->lock);
184}
185
186void
187spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
188{
189	snd_mtxlock(codec->lock);
190	codec->dvc = dvc;
191	snd_mtxunlock(codec->lock);
192}
193
194void
195spicds_init(struct spicds_info *codec)
196{
197#if(0)
198	device_printf(codec->dev, "spicds_init(codec)\n");
199#endif
200	snd_mtxlock(codec->lock);
201	if (codec->type == SPICDS_TYPE_AK4524 ||\
202	    codec->type == SPICDS_TYPE_AK4528) {
203	/* power off */
204	spicds_wrcd(codec, AK4524_POWER, 0);
205	/* set parameter */
206	spicds_wrcd(codec, AK4524_FORMAT, codec->format);
207	spicds_wrcd(codec, AK4524_DVC, codec->dvc);
208	/* power on */
209	spicds_wrcd(codec, AK4524_POWER, AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
210	/* free reset register */
211	spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
212	}
213	if (codec->type == SPICDS_TYPE_WM8770) {
214	/* WM8770 init values are taken from ALSA */
215        /* These come first to reduce init pop noise */
216	spicds_wrcd(codec, 0x1b, 0x044);	/* ADC Mux (AC'97 source) */
217	spicds_wrcd(codec, 0x1c, 0x00B);	/* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
218	spicds_wrcd(codec, 0x1d, 0x009);	/* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
219
220	spicds_wrcd(codec, 0x18, 0x000);	/* All power-up */
221
222	spicds_wrcd(codec, 0x16, 0x122);	/* I2S, normal polarity, 24bit */
223	spicds_wrcd(codec, 0x17, 0x022);	/* 256fs, slave mode */
224
225	spicds_wrcd(codec, 0x19, 0x000);	/* -12dB ADC/L */
226	spicds_wrcd(codec, 0x1a, 0x000);	/* -12dB ADC/R */
227	}
228	if (codec->type == SPICDS_TYPE_AK4358)
229	spicds_wrcd(codec, 0x00, 0x07);		/* I2S, 24bit, power-up */
230	if (codec->type == SPICDS_TYPE_AK4381)
231	spicds_wrcd(codec, 0x00, 0x0f);		/* I2S, 24bit, power-up */
232	snd_mtxunlock(codec->lock);
233}
234
235void
236spicds_reinit(struct spicds_info *codec)
237{
238	snd_mtxlock(codec->lock);
239	if (codec->type != SPICDS_TYPE_WM8770) {
240	/* reset */
241	spicds_wrcd(codec, AK4524_RESET, 0);
242	/* set parameter */
243	spicds_wrcd(codec, AK4524_FORMAT, codec->format);
244	spicds_wrcd(codec, AK4524_DVC, codec->dvc);
245	/* free reset register */
246	spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
247	}
248	else {
249	/* WM8770 reinit */
250	/* AK4358 reinit */
251	/* AK4381 reinit */
252	}
253	snd_mtxunlock(codec->lock);
254}
255
256void
257spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
258{
259#if(0)
260	device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
261#endif
262	snd_mtxlock(codec->lock);
263	if (left >= 100)
264		if (codec->type == SPICDS_TYPE_AK4381)
265			left = 255;
266		else
267			left = 127;
268	else
269		switch (codec->type) {
270		case SPICDS_TYPE_WM8770:
271			left = left + 27;
272			break;
273		case SPICDS_TYPE_AK4381:
274			left = left * 255 / 100;
275			break;
276		default:
277			left = left * 127 / 100;
278		}
279	if (right >= 100)
280		if (codec->type == SPICDS_TYPE_AK4381)
281                        right = 255;
282                else
283			right  = 127;
284	else
285		switch (codec->type) {
286		case SPICDS_TYPE_WM8770:
287                        right = right + 27;
288			break;
289		case SPICDS_TYPE_AK4381:
290			right = right * 255 / 100;
291			break;
292                default:
293                        right = right * 127 / 100;
294		}
295	if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
296#if(0)
297		device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
298#endif
299		spicds_wrcd(codec, AK4524_LIPGA, left);
300		spicds_wrcd(codec, AK4524_RIPGA, right);
301	}
302	if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
303#if(0)
304		device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
305#endif
306		spicds_wrcd(codec, AK4524_LOATT, left);
307		spicds_wrcd(codec, AK4524_ROATT, right);
308	}
309	if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
310#if(0)
311		device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
312#endif
313		spicds_wrcd(codec, AK4528_LOATT, left);
314		spicds_wrcd(codec, AK4528_ROATT, right);
315	}
316        if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
317#if(0)
318                device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
319#endif
320                spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
321                spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
322        }
323        if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
324#if(0)
325                device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
326#endif
327                spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
328                spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
329        }
330        if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
331#if(0)
332                device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
333#endif
334                spicds_wrcd(codec, AK4381_LOATT, left);
335                spicds_wrcd(codec, AK4381_ROATT, right);
336        }
337
338	snd_mtxunlock(codec->lock);
339}
340
341MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
342MODULE_VERSION(snd_spicds, 1);
343