ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / ice1712 / ak4xxx.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *   AK4524 / AK4528 / AK4529 / AK4355 / AK4381 interface
5  *
6  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */      
23
24 #include <sound/driver.h>
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include "ice1712.h"
32
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
34 MODULE_DESCRIPTION("ICEnsemble ICE17xx <-> AK4xxx AD/DA chip interface");
35 MODULE_LICENSE("GPL");
36 MODULE_CLASSES("{sound}");
37
38 static void snd_ice1712_akm4xxx_lock(akm4xxx_t *ak, int chip)
39 {
40         ice1712_t *ice = ak->private_data[0];
41
42         snd_ice1712_save_gpio_status(ice);
43 }
44
45 static void snd_ice1712_akm4xxx_unlock(akm4xxx_t *ak, int chip)
46 {
47         ice1712_t *ice = ak->private_data[0];
48
49         snd_ice1712_restore_gpio_status(ice);
50 }
51
52 /*
53  * write AK4xxx register
54  */
55 static void snd_ice1712_akm4xxx_write(akm4xxx_t *ak, int chip,
56                                       unsigned char addr, unsigned char data)
57 {
58         unsigned int tmp;
59         int idx;
60         unsigned int addrdata;
61         struct snd_ak4xxx_private *priv = (void *)ak->private_value[0];
62         ice1712_t *ice = ak->private_data[0];
63
64         snd_assert(chip >= 0 && chip < 4, return);
65
66         tmp = snd_ice1712_gpio_read(ice);
67         tmp |= priv->add_flags;
68         tmp &= ~priv->mask_flags;
69         if (priv->cs_mask == priv->cs_addr) {
70                 if (priv->cif) {
71                         tmp |= priv->cs_mask; /* start without chip select */
72                 }  else {
73                         tmp &= ~priv->cs_mask; /* chip select low */
74                         snd_ice1712_gpio_write(ice, tmp);
75                         udelay(1);
76                 }
77         } else {
78                 /* doesn't handle cf=1 yet */
79                 tmp &= ~priv->cs_mask;
80                 tmp |= priv->cs_addr;
81                 snd_ice1712_gpio_write(ice, tmp);
82                 udelay(1);
83         }
84
85         /* build I2C address + data byte */
86         addrdata = (priv->caddr << 6) | 0x20 | (addr & 0x1f);
87         addrdata = (addrdata << 8) | data;
88         for (idx = 15; idx >= 0; idx--) {
89                 /* drop clock */
90                 tmp &= ~priv->clk_mask;
91                 snd_ice1712_gpio_write(ice, tmp);
92                 udelay(1);
93                 /* set data */
94                 if (addrdata & (1 << idx))
95                         tmp |= priv->data_mask;
96                 else
97                         tmp &= ~priv->data_mask;
98                 snd_ice1712_gpio_write(ice, tmp);
99                 udelay(1);
100                 /* raise clock */
101                 tmp |= priv->clk_mask;
102                 snd_ice1712_gpio_write(ice, tmp);
103                 udelay(1);
104         }
105
106         if (priv->cs_mask == priv->cs_addr) {
107                 if (priv->cif) {
108                         /* assert a cs pulse to trigger */
109                         tmp &= ~priv->cs_mask;
110                         snd_ice1712_gpio_write(ice, tmp);
111                         udelay(1);
112                 }
113                 tmp |= priv->cs_mask; /* chip select high to trigger */
114         } else {
115                 tmp &= ~priv->cs_mask;
116                 tmp |= priv->cs_none; /* deselect address */
117         }
118         snd_ice1712_gpio_write(ice, tmp);
119         udelay(1);
120 }
121
122 /*
123  * initialize the akm4xxx_t record with the template
124  */
125 int snd_ice1712_akm4xxx_init(akm4xxx_t *ak, const akm4xxx_t *temp,
126                              const struct snd_ak4xxx_private *_priv, ice1712_t *ice)
127 {
128         struct snd_ak4xxx_private *priv;
129
130         priv = kmalloc(sizeof(*priv), GFP_KERNEL);
131         if (priv == NULL)
132                 return -ENOMEM;
133         *ak = *temp;
134         ak->card = ice->card;
135         *priv = *_priv;
136         ak->private_value[0] = (unsigned long)priv;
137         ak->private_data[0] = ice;
138         if (ak->ops.lock == NULL)
139                 ak->ops.lock = snd_ice1712_akm4xxx_lock;
140         if (ak->ops.unlock == NULL)
141                 ak->ops.unlock = snd_ice1712_akm4xxx_unlock;
142         if (ak->ops.write == NULL)
143                 ak->ops.write = snd_ice1712_akm4xxx_write;
144         snd_akm4xxx_init(ak);
145         return 0;
146 }
147
148 void snd_ice1712_akm4xxx_free(ice1712_t *ice)
149 {
150         unsigned int akidx;
151         if (ice->akm == NULL)
152                 return;
153         for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
154                 akm4xxx_t *ak = &ice->akm[akidx];
155                 if (ak->private_value[0])
156                         kfree((void *)ak->private_value[0]);
157         }
158         kfree(ice->akm);
159 }
160
161 /*
162  * build AK4xxx controls
163  */
164 int snd_ice1712_akm4xxx_build_controls(ice1712_t *ice)
165 {
166         unsigned int akidx;
167         int err;
168
169         for (akidx = 0; akidx < ice->akm_codecs; akidx++) {
170                 akm4xxx_t *ak = &ice->akm[akidx];
171                 err = snd_akm4xxx_build_controls(ak);
172                 if (err < 0)
173                         return err;
174         }
175         return 0;
176 }
177
178 static int __init alsa_ice1712_akm4xxx_module_init(void)
179 {
180         return 0;
181 }
182         
183 static void __exit alsa_ice1712_akm4xxx_module_exit(void)
184 {
185 }
186         
187 module_init(alsa_ice1712_akm4xxx_module_init)
188 module_exit(alsa_ice1712_akm4xxx_module_exit)
189
190 EXPORT_SYMBOL(snd_ice1712_akm4xxx_init);
191 EXPORT_SYMBOL(snd_ice1712_akm4xxx_free);
192 EXPORT_SYMBOL(snd_ice1712_akm4xxx_build_controls);