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
27 * $FreeBSD: src/sys/dev/sound/pci/spicds.c,v 1.5.2.2 2007/06/11 19:33:27 ariff Exp $
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);
91 else if (codec->type == SPICDS_TYPE_AK4396)
93 /* AK4396 chip address */
94 spicds_wrbit(codec, 0);
95 spicds_wrbit(codec, 0);
99 spicds_wrbit(codec, 1);
100 spicds_wrbit(codec, 0);
103 spicds_wrbit(codec, 1);
104 /* register address */
105 for (mask = 0x10; mask != 0; mask >>= 1)
106 spicds_wrbit(codec, reg & mask);
108 for (mask = 0x80; mask != 0; mask >>= 1)
109 spicds_wrbit(codec, val & mask);
114 /* register address */
115 for (mask = 0x40; mask != 0; mask >>= 1)
116 spicds_wrbit(codec, reg & mask);
118 for (mask = 0x100; mask != 0; mask >>= 1)
119 spicds_wrbit(codec, val & mask);
124 codec->ctrl(codec->devinfo, 0, 1, 0);
126 codec->ctrl(codec->devinfo, 1, 1, 0);
129 codec->ctrl(codec->devinfo, 1, 1, 0);
136 spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
138 struct spicds_info *codec;
141 device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
143 codec = kmalloc(sizeof *codec, M_SPICDS, M_WAITOK);
145 ksnprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
146 codec->lock = snd_mtxcreate(codec->name, codec->name);
149 codec->devinfo = devinfo;
151 codec->type = SPICDS_TYPE_AK4524;
153 codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
154 codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
160 spicds_destroy(struct spicds_info *codec)
162 snd_mtxfree(codec->lock);
163 kfree(codec, M_SPICDS);
167 spicds_settype(struct spicds_info *codec, unsigned int type)
169 snd_mtxlock(codec->lock);
171 snd_mtxunlock(codec->lock);
175 spicds_setcif(struct spicds_info *codec, unsigned int cif)
177 snd_mtxlock(codec->lock);
179 snd_mtxunlock(codec->lock);
183 spicds_setformat(struct spicds_info *codec, unsigned int format)
185 snd_mtxlock(codec->lock);
186 codec->format = format;
187 snd_mtxunlock(codec->lock);
191 spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
193 snd_mtxlock(codec->lock);
195 snd_mtxunlock(codec->lock);
199 spicds_init(struct spicds_info *codec)
202 device_printf(codec->dev, "spicds_init(codec)\n");
204 snd_mtxlock(codec->lock);
205 if (codec->type == SPICDS_TYPE_AK4524 ||\
206 codec->type == SPICDS_TYPE_AK4528) {
208 spicds_wrcd(codec, AK4524_POWER, 0);
210 spicds_wrcd(codec, AK4524_FORMAT, codec->format);
211 spicds_wrcd(codec, AK4524_DVC, codec->dvc);
213 spicds_wrcd(codec, AK4524_POWER, AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
214 /* free reset register */
215 spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
217 if (codec->type == SPICDS_TYPE_WM8770) {
218 /* WM8770 init values are taken from ALSA */
219 /* These come first to reduce init pop noise */
220 spicds_wrcd(codec, 0x1b, 0x044); /* ADC Mux (AC'97 source) */
221 spicds_wrcd(codec, 0x1c, 0x00B); /* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
222 spicds_wrcd(codec, 0x1d, 0x009); /* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
224 spicds_wrcd(codec, 0x18, 0x000); /* All power-up */
226 spicds_wrcd(codec, 0x16, 0x122); /* I2S, normal polarity, 24bit */
227 spicds_wrcd(codec, 0x17, 0x022); /* 256fs, slave mode */
229 spicds_wrcd(codec, 0x19, 0x000); /* -12dB ADC/L */
230 spicds_wrcd(codec, 0x1a, 0x000); /* -12dB ADC/R */
232 if (codec->type == SPICDS_TYPE_AK4358)
233 spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
234 if (codec->type == SPICDS_TYPE_AK4381)
235 spicds_wrcd(codec, 0x00, 0x0f); /* I2S, 24bit, power-up */
236 if (codec->type == SPICDS_TYPE_AK4396)
237 spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
238 snd_mtxunlock(codec->lock);
242 spicds_reinit(struct spicds_info *codec)
244 snd_mtxlock(codec->lock);
245 if (codec->type != SPICDS_TYPE_WM8770) {
247 spicds_wrcd(codec, AK4524_RESET, 0);
249 spicds_wrcd(codec, AK4524_FORMAT, codec->format);
250 spicds_wrcd(codec, AK4524_DVC, codec->dvc);
251 /* free reset register */
252 spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
259 snd_mtxunlock(codec->lock);
263 spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
266 device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
268 snd_mtxlock(codec->lock);
270 if ((codec->type == SPICDS_TYPE_AK4381) || \
271 (codec->type == SPICDS_TYPE_AK4396))
276 switch (codec->type) {
277 case SPICDS_TYPE_WM8770:
280 case SPICDS_TYPE_AK4381:
281 case SPICDS_TYPE_AK4396:
282 left = left * 255 / 100;
285 left = left * 127 / 100;
288 if ((codec->type == SPICDS_TYPE_AK4381) || \
289 (codec->type == SPICDS_TYPE_AK4396))
294 switch (codec->type) {
295 case SPICDS_TYPE_WM8770:
298 case SPICDS_TYPE_AK4381:
299 case SPICDS_TYPE_AK4396:
300 right = right * 255 / 100;
303 right = right * 127 / 100;
305 if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
307 device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
309 spicds_wrcd(codec, AK4524_LIPGA, left);
310 spicds_wrcd(codec, AK4524_RIPGA, right);
312 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
314 device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
316 spicds_wrcd(codec, AK4524_LOATT, left);
317 spicds_wrcd(codec, AK4524_ROATT, right);
319 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
321 device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
323 spicds_wrcd(codec, AK4528_LOATT, left);
324 spicds_wrcd(codec, AK4528_ROATT, right);
326 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
328 device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
330 spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
331 spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
333 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
335 device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
337 spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
338 spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
340 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
342 device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
344 spicds_wrcd(codec, AK4381_LOATT, left);
345 spicds_wrcd(codec, AK4381_ROATT, right);
348 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4396) {
350 device_printf(codec->dev, "spicds_set(): AK4396(PLAY) %d/%d\n", left, right);
352 spicds_wrcd(codec, AK4396_LOATT, left);
353 spicds_wrcd(codec, AK4396_ROATT, right);
356 snd_mtxunlock(codec->lock);
359 MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
360 MODULE_VERSION(snd_spicds, 1);