2 * Copyright (c) 2006 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
3 * Copyright (c) 2001 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include <dev/sound/pcm/sound.h>
32 #include <dev/sound/pci/spicds.h>
34 MALLOC_DEFINE(M_SPICDS, "spicds", "SPI codec");
36 #define SPICDS_NAMELEN 16
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];
52 spicds_wrbit(struct spicds_info *codec, int bit)
54 unsigned int cs, cdti;
63 codec->ctrl(codec->devinfo, cs, 0, cdti);
65 codec->ctrl(codec->devinfo, cs, 1, cdti);
72 spicds_wrcd(struct spicds_info *codec, int reg, u_int16_t val)
77 device_printf(codec->dev, "spicds_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
81 codec->ctrl(codec->devinfo, 1, 1, 0);
83 codec->ctrl(codec->devinfo, 0, 1, 0);
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);
93 spicds_wrbit(codec, 1);
94 spicds_wrbit(codec, 0);
97 spicds_wrbit(codec, 1);
98 /* register address */
99 for (mask = 0x10; mask != 0; mask >>= 1)
100 spicds_wrbit(codec, reg & mask);
102 for (mask = 0x80; mask != 0; mask >>= 1)
103 spicds_wrbit(codec, val & mask);
108 /* register address */
109 for (mask = 0x40; mask != 0; mask >>= 1)
110 spicds_wrbit(codec, reg & mask);
112 for (mask = 0x100; mask != 0; mask >>= 1)
113 spicds_wrbit(codec, val & mask);
118 codec->ctrl(codec->devinfo, 0, 1, 0);
120 codec->ctrl(codec->devinfo, 1, 1, 0);
123 codec->ctrl(codec->devinfo, 1, 1, 0);
130 spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
132 struct spicds_info *codec;
135 device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
137 codec = (struct spicds_info *)malloc(sizeof *codec, M_SPICDS, M_NOWAIT);
141 snprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
142 codec->lock = snd_mtxcreate(codec->name, codec->name);
145 codec->devinfo = devinfo;
147 codec->type = SPICDS_TYPE_AK4524;
149 codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
150 codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
156 spicds_destroy(struct spicds_info *codec)
158 snd_mtxfree(codec->lock);
159 free(codec, M_SPICDS);
163 spicds_settype(struct spicds_info *codec, unsigned int type)
165 snd_mtxlock(codec->lock);
167 snd_mtxunlock(codec->lock);
171 spicds_setcif(struct spicds_info *codec, unsigned int cif)
173 snd_mtxlock(codec->lock);
175 snd_mtxunlock(codec->lock);
179 spicds_setformat(struct spicds_info *codec, unsigned int format)
181 snd_mtxlock(codec->lock);
182 codec->format = format;
183 snd_mtxunlock(codec->lock);
187 spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
189 snd_mtxlock(codec->lock);
191 snd_mtxunlock(codec->lock);
195 spicds_init(struct spicds_info *codec)
198 device_printf(codec->dev, "spicds_init(codec)\n");
200 snd_mtxlock(codec->lock);
201 if (codec->type == SPICDS_TYPE_AK4524 ||\
202 codec->type == SPICDS_TYPE_AK4528) {
204 spicds_wrcd(codec, AK4524_POWER, 0);
206 spicds_wrcd(codec, AK4524_FORMAT, codec->format);
207 spicds_wrcd(codec, AK4524_DVC, codec->dvc);
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);
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) */
220 spicds_wrcd(codec, 0x18, 0x000); /* All power-up */
222 spicds_wrcd(codec, 0x16, 0x122); /* I2S, normal polarity, 24bit */
223 spicds_wrcd(codec, 0x17, 0x022); /* 256fs, slave mode */
225 spicds_wrcd(codec, 0x19, 0x000); /* -12dB ADC/L */
226 spicds_wrcd(codec, 0x1a, 0x000); /* -12dB ADC/R */
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);
236 spicds_reinit(struct spicds_info *codec)
238 snd_mtxlock(codec->lock);
239 if (codec->type != SPICDS_TYPE_WM8770) {
241 spicds_wrcd(codec, AK4524_RESET, 0);
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);
253 snd_mtxunlock(codec->lock);
257 spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
260 device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
262 snd_mtxlock(codec->lock);
264 if (codec->type == SPICDS_TYPE_AK4381)
269 switch (codec->type) {
270 case SPICDS_TYPE_WM8770:
273 case SPICDS_TYPE_AK4381:
274 left = left * 255 / 100;
277 left = left * 127 / 100;
280 if (codec->type == SPICDS_TYPE_AK4381)
285 switch (codec->type) {
286 case SPICDS_TYPE_WM8770:
289 case SPICDS_TYPE_AK4381:
290 right = right * 255 / 100;
293 right = right * 127 / 100;
295 if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
297 device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
299 spicds_wrcd(codec, AK4524_LIPGA, left);
300 spicds_wrcd(codec, AK4524_RIPGA, right);
302 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
304 device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
306 spicds_wrcd(codec, AK4524_LOATT, left);
307 spicds_wrcd(codec, AK4524_ROATT, right);
309 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
311 device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
313 spicds_wrcd(codec, AK4528_LOATT, left);
314 spicds_wrcd(codec, AK4528_ROATT, right);
316 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
318 device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
320 spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
321 spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
323 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
325 device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
327 spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
328 spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
330 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
332 device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
334 spicds_wrcd(codec, AK4381_LOATT, left);
335 spicds_wrcd(codec, AK4381_ROATT, right);
338 snd_mtxunlock(codec->lock);
341 MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
342 MODULE_VERSION(snd_spicds, 1);