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