vserver 1.9.3
[linux-2.6.git] / sound / ppc / daca.c
1 /*
2  * PMac DACA lowlevel functions
3  *
4  * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-dev.h>
26 #include <linux/kmod.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include "pmac.h"
30
31 /* i2c address */
32 #define DACA_I2C_ADDR   0x4d
33
34 /* registers */
35 #define DACA_REG_SR     0x01
36 #define DACA_REG_AVOL   0x02
37 #define DACA_REG_GCFG   0x03
38
39 /* maximum volume value */
40 #define DACA_VOL_MAX    0x38
41
42
43 typedef struct pmac_daca_t {
44         pmac_keywest_t i2c;
45         int left_vol, right_vol;
46         unsigned int deemphasis : 1;
47         unsigned int amp_on : 1;
48 } pmac_daca_t;
49
50
51 /*
52  * initialize / detect DACA
53  */
54 static int daca_init_client(pmac_keywest_t *i2c)
55 {
56         unsigned short wdata = 0x00;
57         /* SR: no swap, 1bit delay, 32-48kHz */
58         /* GCFG: power amp inverted, DAC on */
59         if (snd_pmac_keywest_write_byte(i2c, DACA_REG_SR, 0x08) < 0 ||
60             snd_pmac_keywest_write_byte(i2c, DACA_REG_GCFG, 0x05) < 0)
61                 return -EINVAL;
62         return snd_pmac_keywest_write(i2c, DACA_REG_AVOL, 2, (unsigned char*)&wdata);
63 }
64
65 /*
66  * update volume
67  */
68 static int daca_set_volume(pmac_daca_t *mix)
69 {
70         unsigned char data[2];
71   
72         if (! mix->i2c.client)
73                 return -ENODEV;
74   
75         if (mix->left_vol > DACA_VOL_MAX)
76                 data[0] = DACA_VOL_MAX;
77         else
78                 data[0] = mix->left_vol;
79         if (mix->right_vol > DACA_VOL_MAX)
80                 data[1] = DACA_VOL_MAX;
81         else
82                 data[1] = mix->right_vol;
83         data[1] |= mix->deemphasis ? 0x40 : 0;
84         if (snd_pmac_keywest_write(&mix->i2c, DACA_REG_AVOL, 2, data) < 0) {
85                 snd_printk("failed to set volume \n");  
86                 return -EINVAL; 
87         }
88         return 0;
89 }
90
91
92 /* deemphasis switch */
93 static int daca_info_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
94 {
95         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
96         uinfo->count = 1;
97         uinfo->value.integer.min = 0;
98         uinfo->value.integer.max = 1;
99         return 0;
100 }
101
102 static int daca_get_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
103 {
104         pmac_t *chip = snd_kcontrol_chip(kcontrol);
105         pmac_daca_t *mix;
106         if (! (mix = chip->mixer_data))
107                 return -ENODEV;
108         ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
109         return 0;
110 }
111
112 static int daca_put_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
113 {
114         pmac_t *chip = snd_kcontrol_chip(kcontrol);
115         pmac_daca_t *mix;
116         int change;
117
118         if (! (mix = chip->mixer_data))
119                 return -ENODEV;
120         change = mix->deemphasis != ucontrol->value.integer.value[0];
121         if (change) {
122                 mix->deemphasis = ucontrol->value.integer.value[0];
123                 daca_set_volume(mix);
124         }
125         return change;
126 }
127
128 /* output volume */
129 static int daca_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
130 {
131         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
132         uinfo->count = 2;
133         uinfo->value.integer.min = 0;
134         uinfo->value.integer.max = DACA_VOL_MAX;
135         return 0;
136 }
137
138 static int daca_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
139 {
140         pmac_t *chip = snd_kcontrol_chip(kcontrol);
141         pmac_daca_t *mix;
142         if (! (mix = chip->mixer_data))
143                 return -ENODEV;
144         ucontrol->value.integer.value[0] = mix->left_vol;
145         ucontrol->value.integer.value[1] = mix->right_vol;
146         return 0;
147 }
148
149 static int daca_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
150 {
151         pmac_t *chip = snd_kcontrol_chip(kcontrol);
152         pmac_daca_t *mix;
153         int change;
154
155         if (! (mix = chip->mixer_data))
156                 return -ENODEV;
157         change = mix->left_vol != ucontrol->value.integer.value[0] ||
158                 mix->right_vol != ucontrol->value.integer.value[1];
159         if (change) {
160                 mix->left_vol = ucontrol->value.integer.value[0];
161                 mix->right_vol = ucontrol->value.integer.value[1];
162                 daca_set_volume(mix);
163         }
164         return change;
165 }
166
167 /* amplifier switch */
168 #define daca_info_amp   daca_info_deemphasis
169
170 static int daca_get_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
171 {
172         pmac_t *chip = snd_kcontrol_chip(kcontrol);
173         pmac_daca_t *mix;
174         if (! (mix = chip->mixer_data))
175                 return -ENODEV;
176         ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
177         return 0;
178 }
179
180 static int daca_put_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
181 {
182         pmac_t *chip = snd_kcontrol_chip(kcontrol);
183         pmac_daca_t *mix;
184         int change;
185
186         if (! (mix = chip->mixer_data))
187                 return -ENODEV;
188         change = mix->amp_on != ucontrol->value.integer.value[0];
189         if (change) {
190                 mix->amp_on = ucontrol->value.integer.value[0];
191                 snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_GCFG,
192                                             mix->amp_on ? 0x05 : 0x04);
193         }
194         return change;
195 }
196
197 static snd_kcontrol_new_t daca_mixers[] = {
198         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
199           .name = "Deemphasis Switch",
200           .info = daca_info_deemphasis,
201           .get = daca_get_deemphasis,
202           .put = daca_put_deemphasis
203         },
204         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
205           .name = "Master Playback Volume",
206           .info = daca_info_volume,
207           .get = daca_get_volume,
208           .put = daca_put_volume
209         },
210         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
211           .name = "Power Amplifier Switch",
212           .info = daca_info_amp,
213           .get = daca_get_amp,
214           .put = daca_put_amp
215         },
216 };
217
218
219 #ifdef CONFIG_PMAC_PBOOK
220 static void daca_resume(pmac_t *chip)
221 {
222         pmac_daca_t *mix = chip->mixer_data;
223         snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_SR, 0x08);
224         snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_GCFG,
225                                     mix->amp_on ? 0x05 : 0x04);
226         daca_set_volume(mix);
227 }
228 #endif /* CONFIG_PMAC_PBOOK */
229
230
231 static void daca_cleanup(pmac_t *chip)
232 {
233         pmac_daca_t *mix = chip->mixer_data;
234         if (! mix)
235                 return;
236         snd_pmac_keywest_cleanup(&mix->i2c);
237         kfree(mix);
238         chip->mixer_data = NULL;
239 }
240
241 /* exported */
242 int __init snd_pmac_daca_init(pmac_t *chip)
243 {
244         int i, err;
245         pmac_daca_t *mix;
246
247 #ifdef CONFIG_KMOD
248         if (current->fs->root)
249                 request_module("i2c-keywest");
250 #endif /* CONFIG_KMOD */        
251
252         mix = kmalloc(sizeof(*mix), GFP_KERNEL);
253         if (! mix)
254                 return -ENOMEM;
255         memset(mix, 0, sizeof(*mix));
256         chip->mixer_data = mix;
257         chip->mixer_free = daca_cleanup;
258         mix->amp_on = 1; /* default on */
259
260         mix->i2c.addr = DACA_I2C_ADDR;
261         mix->i2c.init_client = daca_init_client;
262         mix->i2c.name = "DACA";
263         if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
264                 return err;
265
266         /*
267          * build mixers
268          */
269         strcpy(chip->card->mixername, "PowerMac DACA");
270
271         for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
272                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
273                         return err;
274         }
275
276 #ifdef CONFIG_PMAC_PBOOK
277         chip->resume = daca_resume;
278 #endif
279
280         return 0;
281 }