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