ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #define chip_t pmac_t
32
33 /* i2c address */
34 #define DACA_I2C_ADDR   0x4d
35
36 /* registers */
37 #define DACA_REG_SR     0x01
38 #define DACA_REG_AVOL   0x02
39 #define DACA_REG_GCFG   0x03
40
41 /* maximum volume value */
42 #define DACA_VOL_MAX    0x38
43
44
45 typedef struct pmac_daca_t {
46         pmac_keywest_t i2c;
47         int left_vol, right_vol;
48         unsigned int deemphasis : 1;
49         unsigned int amp_on : 1;
50 } pmac_daca_t;
51
52
53 /*
54  * initialize / detect DACA
55  */
56 static int daca_init_client(pmac_keywest_t *i2c)
57 {
58         unsigned short wdata = 0x00;
59         /* SR: no swap, 1bit delay, 32-48kHz */
60         /* GCFG: power amp inverted, DAC on */
61         if (snd_pmac_keywest_write_byte(i2c, DACA_REG_SR, 0x08) < 0 ||
62             snd_pmac_keywest_write_byte(i2c, DACA_REG_GCFG, 0x05) < 0)
63                 return -EINVAL;
64         return snd_pmac_keywest_write(i2c, DACA_REG_AVOL, 2, (unsigned char*)&wdata);
65 }
66
67 /*
68  * update volume
69  */
70 static int daca_set_volume(pmac_daca_t *mix)
71 {
72         unsigned char data[2];
73   
74         if (! mix->i2c.client)
75                 return -ENODEV;
76   
77         if (mix->left_vol > DACA_VOL_MAX)
78                 data[0] = DACA_VOL_MAX;
79         else
80                 data[0] = mix->left_vol;
81         if (mix->right_vol > DACA_VOL_MAX)
82                 data[1] = DACA_VOL_MAX;
83         else
84                 data[1] = mix->right_vol;
85         data[1] |= mix->deemphasis ? 0x40 : 0;
86         if (snd_pmac_keywest_write(&mix->i2c, DACA_REG_AVOL, 2, data) < 0) {
87                 snd_printk("failed to set volume \n");  
88                 return -EINVAL; 
89         }
90         return 0;
91 }
92
93
94 /* deemphasis switch */
95 static int daca_info_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
96 {
97         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
98         uinfo->count = 1;
99         uinfo->value.integer.min = 0;
100         uinfo->value.integer.max = 1;
101         return 0;
102 }
103
104 static int daca_get_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
105 {
106         pmac_t *chip = snd_kcontrol_chip(kcontrol);
107         pmac_daca_t *mix;
108         if (! (mix = chip->mixer_data))
109                 return -ENODEV;
110         ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
111         return 0;
112 }
113
114 static int daca_put_deemphasis(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
115 {
116         pmac_t *chip = snd_kcontrol_chip(kcontrol);
117         pmac_daca_t *mix;
118         int change;
119
120         if (! (mix = chip->mixer_data))
121                 return -ENODEV;
122         change = mix->deemphasis != ucontrol->value.integer.value[0];
123         if (change) {
124                 mix->deemphasis = ucontrol->value.integer.value[0];
125                 daca_set_volume(mix);
126         }
127         return change;
128 }
129
130 /* output volume */
131 static int daca_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
132 {
133         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
134         uinfo->count = 2;
135         uinfo->value.integer.min = 0;
136         uinfo->value.integer.max = DACA_VOL_MAX;
137         return 0;
138 }
139
140 static int daca_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
141 {
142         pmac_t *chip = snd_kcontrol_chip(kcontrol);
143         pmac_daca_t *mix;
144         if (! (mix = chip->mixer_data))
145                 return -ENODEV;
146         ucontrol->value.integer.value[0] = mix->left_vol;
147         ucontrol->value.integer.value[1] = mix->right_vol;
148         return 0;
149 }
150
151 static int daca_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
152 {
153         pmac_t *chip = snd_kcontrol_chip(kcontrol);
154         pmac_daca_t *mix;
155         int change;
156
157         if (! (mix = chip->mixer_data))
158                 return -ENODEV;
159         change = mix->left_vol != ucontrol->value.integer.value[0] ||
160                 mix->right_vol != ucontrol->value.integer.value[1];
161         if (change) {
162                 mix->left_vol = ucontrol->value.integer.value[0];
163                 mix->right_vol = ucontrol->value.integer.value[1];
164                 daca_set_volume(mix);
165         }
166         return change;
167 }
168
169 /* amplifier switch */
170 #define daca_info_amp   daca_info_deemphasis
171
172 static int daca_get_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
173 {
174         pmac_t *chip = snd_kcontrol_chip(kcontrol);
175         pmac_daca_t *mix;
176         if (! (mix = chip->mixer_data))
177                 return -ENODEV;
178         ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
179         return 0;
180 }
181
182 static int daca_put_amp(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
183 {
184         pmac_t *chip = snd_kcontrol_chip(kcontrol);
185         pmac_daca_t *mix;
186         int change;
187
188         if (! (mix = chip->mixer_data))
189                 return -ENODEV;
190         change = mix->amp_on != ucontrol->value.integer.value[0];
191         if (change) {
192                 mix->amp_on = ucontrol->value.integer.value[0];
193                 snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_GCFG,
194                                             mix->amp_on ? 0x05 : 0x04);
195         }
196         return change;
197 }
198
199 static snd_kcontrol_new_t daca_mixers[] = {
200         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
201           .name = "Deemphasis Switch",
202           .info = daca_info_deemphasis,
203           .get = daca_get_deemphasis,
204           .put = daca_put_deemphasis
205         },
206         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
207           .name = "Master Playback Volume",
208           .info = daca_info_volume,
209           .get = daca_get_volume,
210           .put = daca_put_volume
211         },
212         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
213           .name = "Power Amplifier Switch",
214           .info = daca_info_amp,
215           .get = daca_get_amp,
216           .put = daca_put_amp
217         },
218 };
219
220 #define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
221
222
223 #ifdef CONFIG_PMAC_PBOOK
224 static void daca_resume(pmac_t *chip)
225 {
226         pmac_daca_t *mix = chip->mixer_data;
227         snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_SR, 0x08);
228         snd_pmac_keywest_write_byte(&mix->i2c, DACA_REG_GCFG,
229                                     mix->amp_on ? 0x05 : 0x04);
230         daca_set_volume(mix);
231 }
232 #endif /* CONFIG_PMAC_PBOOK */
233
234
235 static void daca_cleanup(pmac_t *chip)
236 {
237         pmac_daca_t *mix = chip->mixer_data;
238         if (! mix)
239                 return;
240         snd_pmac_keywest_cleanup(&mix->i2c);
241         kfree(mix);
242         chip->mixer_data = NULL;
243 }
244
245 /* exported */
246 int __init snd_pmac_daca_init(pmac_t *chip)
247 {
248         int i, err;
249         pmac_daca_t *mix;
250
251 #ifdef CONFIG_KMOD
252         if (current->fs->root)
253                 request_module("i2c-keywest");
254 #endif /* CONFIG_KMOD */        
255
256         mix = kmalloc(sizeof(*mix), GFP_KERNEL);
257         if (! mix)
258                 return -ENOMEM;
259         memset(mix, 0, sizeof(*mix));
260         chip->mixer_data = mix;
261         chip->mixer_free = daca_cleanup;
262         mix->amp_on = 1; /* default on */
263
264         mix->i2c.addr = DACA_I2C_ADDR;
265         mix->i2c.init_client = daca_init_client;
266         mix->i2c.name = "DACA";
267         if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
268                 return err;
269
270         /*
271          * build mixers
272          */
273         strcpy(chip->card->mixername, "PowerMac DACA");
274
275         for (i = 0; i < num_controls(daca_mixers); i++) {
276                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
277                         return err;
278         }
279
280 #ifdef CONFIG_PMAC_PBOOK
281         chip->resume = daca_resume;
282 #endif
283
284         return 0;
285 }