2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
5 * Copyright (c) 2001 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include <dev/sound/pcm/sound.h>
38 #include <dev/sound/pci/spicds.h>
40 static MALLOC_DEFINE(M_SPICDS, "spicds", "SPI codec");
42 #define SPICDS_NAMELEN 16
47 int num; /* number of this device */
48 unsigned int type; /* codec type */
49 unsigned int cif; /* Controll data Interface Format (0/1) */
50 unsigned int format; /* data format and master clock frequency */
51 unsigned int dvc; /* De-emphasis and Volume Control */
52 unsigned int left, right;
53 char name[SPICDS_NAMELEN];
58 spicds_wrbit(struct spicds_info *codec, int bit)
60 unsigned int cs, cdti;
69 codec->ctrl(codec->devinfo, cs, 0, cdti);
71 codec->ctrl(codec->devinfo, cs, 1, cdti);
78 spicds_wrcd(struct spicds_info *codec, int reg, u_int16_t val)
83 device_printf(codec->dev, "spicds_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
87 codec->ctrl(codec->devinfo, 1, 1, 0);
89 codec->ctrl(codec->devinfo, 0, 1, 0);
91 if (codec->type != SPICDS_TYPE_WM8770) {
92 if (codec->type == SPICDS_TYPE_AK4381) {
93 /* AK4381 chip address */
94 spicds_wrbit(codec, 0);
95 spicds_wrbit(codec, 1);
97 else if (codec->type == SPICDS_TYPE_AK4396)
99 /* AK4396 chip address */
100 spicds_wrbit(codec, 0);
101 spicds_wrbit(codec, 0);
105 spicds_wrbit(codec, 1);
106 spicds_wrbit(codec, 0);
109 spicds_wrbit(codec, 1);
110 /* register address */
111 for (mask = 0x10; mask != 0; mask >>= 1)
112 spicds_wrbit(codec, reg & mask);
114 for (mask = 0x80; mask != 0; mask >>= 1)
115 spicds_wrbit(codec, val & mask);
120 /* register address */
121 for (mask = 0x40; mask != 0; mask >>= 1)
122 spicds_wrbit(codec, reg & mask);
124 for (mask = 0x100; mask != 0; mask >>= 1)
125 spicds_wrbit(codec, val & mask);
130 codec->ctrl(codec->devinfo, 0, 1, 0);
132 codec->ctrl(codec->devinfo, 1, 1, 0);
135 codec->ctrl(codec->devinfo, 1, 1, 0);
142 spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
144 struct spicds_info *codec;
147 device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
149 codec = (struct spicds_info *)malloc(sizeof *codec, M_SPICDS, M_NOWAIT);
153 snprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
154 codec->lock = snd_mtxcreate(codec->name, codec->name);
157 codec->devinfo = devinfo;
159 codec->type = SPICDS_TYPE_AK4524;
161 codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
162 codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
168 spicds_destroy(struct spicds_info *codec)
170 snd_mtxfree(codec->lock);
171 free(codec, M_SPICDS);
175 spicds_settype(struct spicds_info *codec, unsigned int type)
177 snd_mtxlock(codec->lock);
179 snd_mtxunlock(codec->lock);
183 spicds_setcif(struct spicds_info *codec, unsigned int cif)
185 snd_mtxlock(codec->lock);
187 snd_mtxunlock(codec->lock);
191 spicds_setformat(struct spicds_info *codec, unsigned int format)
193 snd_mtxlock(codec->lock);
194 codec->format = format;
195 snd_mtxunlock(codec->lock);
199 spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
201 snd_mtxlock(codec->lock);
203 snd_mtxunlock(codec->lock);
207 spicds_init(struct spicds_info *codec)
210 device_printf(codec->dev, "spicds_init(codec)\n");
212 snd_mtxlock(codec->lock);
213 if (codec->type == SPICDS_TYPE_AK4524 ||\
214 codec->type == SPICDS_TYPE_AK4528) {
216 spicds_wrcd(codec, AK4524_POWER, 0);
218 spicds_wrcd(codec, AK4524_FORMAT, codec->format);
219 spicds_wrcd(codec, AK4524_DVC, codec->dvc);
221 spicds_wrcd(codec, AK4524_POWER,
222 AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
223 /* free reset register */
224 spicds_wrcd(codec, AK4524_RESET,
225 AK452X_RESET_RSDA | AK452X_RESET_RSAD);
227 if (codec->type == SPICDS_TYPE_WM8770) {
228 /* WM8770 init values are taken from ALSA */
230 /* These come first to reduce init pop noise */
231 spicds_wrcd(codec, 0x1b, 0x044); /* ADC Mux (AC'97 source) */
232 spicds_wrcd(codec, 0x1c, 0x00B); /* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
233 spicds_wrcd(codec, 0x1d, 0x009); /* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
235 spicds_wrcd(codec, 0x18, 0x000); /* All power-up */
237 spicds_wrcd(codec, 0x16, 0x122); /* I2S, normal polarity, 24bit */
238 spicds_wrcd(codec, 0x17, 0x022); /* 256fs, slave mode */
240 spicds_wrcd(codec, 0x19, 0x000); /* -12dB ADC/L */
241 spicds_wrcd(codec, 0x1a, 0x000); /* -12dB ADC/R */
243 if (codec->type == SPICDS_TYPE_AK4358)
244 spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
245 if (codec->type == SPICDS_TYPE_AK4381)
246 spicds_wrcd(codec, 0x00, 0x8f); /* I2S, 24bit, power-up */
247 if (codec->type == SPICDS_TYPE_AK4396)
248 spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
249 snd_mtxunlock(codec->lock);
253 spicds_reinit(struct spicds_info *codec)
255 snd_mtxlock(codec->lock);
256 if (codec->type != SPICDS_TYPE_WM8770) {
258 spicds_wrcd(codec, AK4524_RESET, 0);
260 spicds_wrcd(codec, AK4524_FORMAT, codec->format);
261 spicds_wrcd(codec, AK4524_DVC, codec->dvc);
262 /* free reset register */
263 spicds_wrcd(codec, AK4524_RESET,
264 AK452X_RESET_RSDA | AK452X_RESET_RSAD);
270 snd_mtxunlock(codec->lock);
274 spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
277 device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
279 snd_mtxlock(codec->lock);
281 if ((codec->type == SPICDS_TYPE_AK4381) || \
282 (codec->type == SPICDS_TYPE_AK4396))
287 switch (codec->type) {
288 case SPICDS_TYPE_WM8770:
291 case SPICDS_TYPE_AK4381:
292 case SPICDS_TYPE_AK4396:
293 left = left * 255 / 100;
296 left = left * 127 / 100;
299 if ((codec->type == SPICDS_TYPE_AK4381) || \
300 (codec->type == SPICDS_TYPE_AK4396))
305 switch (codec->type) {
306 case SPICDS_TYPE_WM8770:
309 case SPICDS_TYPE_AK4381:
310 case SPICDS_TYPE_AK4396:
311 right = right * 255 / 100;
314 right = right * 127 / 100;
316 if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
318 device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
320 spicds_wrcd(codec, AK4524_LIPGA, left);
321 spicds_wrcd(codec, AK4524_RIPGA, right);
323 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
325 device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
327 spicds_wrcd(codec, AK4524_LOATT, left);
328 spicds_wrcd(codec, AK4524_ROATT, right);
330 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
332 device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
334 spicds_wrcd(codec, AK4528_LOATT, left);
335 spicds_wrcd(codec, AK4528_ROATT, right);
337 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
339 device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
341 spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
342 spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
344 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
346 device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
348 spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
349 spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
351 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
353 device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
355 spicds_wrcd(codec, AK4381_LOATT, left);
356 spicds_wrcd(codec, AK4381_ROATT, right);
359 if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4396) {
361 device_printf(codec->dev, "spicds_set(): AK4396(PLAY) %d/%d\n", left, right);
363 spicds_wrcd(codec, AK4396_LOATT, left);
364 spicds_wrcd(codec, AK4396_ROATT, right);
367 snd_mtxunlock(codec->lock);
370 MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
371 MODULE_VERSION(snd_spicds, 1);