Merge git://git.kernel.org/pub/scm/linux/kernel/git/kkeil/ISDN-2.6
[linux-2.6] / sound / i2c / other / pt2258.c
1 /*
2  *   ALSA Driver for the PT2258 volume controller.
3  *
4  *      Copyright (c) 2006  Jochen Voss <voss@seehuhn.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/core.h>
23 #include <sound/control.h>
24 #include <sound/tlv.h>
25 #include <sound/i2c.h>
26 #include <sound/pt2258.h>
27
28 MODULE_AUTHOR("Jochen Voss <voss@seehuhn.de>");
29 MODULE_DESCRIPTION("PT2258 volume controller (Princeton Technology Corp.)");
30 MODULE_LICENSE("GPL");
31
32 #define PT2258_CMD_RESET 0xc0
33 #define PT2258_CMD_UNMUTE 0xf8
34 #define PT2258_CMD_MUTE 0xf9
35
36 static const unsigned char pt2258_channel_code[12] = {
37         0x80, 0x90,             /* channel 1: -10dB, -1dB */
38         0x40, 0x50,             /* channel 2: -10dB, -1dB */
39         0x00, 0x10,             /* channel 3: -10dB, -1dB */
40         0x20, 0x30,             /* channel 4: -10dB, -1dB */
41         0x60, 0x70,             /* channel 5: -10dB, -1dB */
42         0xa0, 0xb0              /* channel 6: -10dB, -1dB */
43 };
44
45 int snd_pt2258_reset(struct snd_pt2258 *pt)
46 {
47         unsigned char bytes[2];
48         int i;
49
50         /* reset chip */
51         bytes[0] = PT2258_CMD_RESET;
52         snd_i2c_lock(pt->i2c_bus);
53         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
54                 goto __error;
55         snd_i2c_unlock(pt->i2c_bus);
56
57         /* mute all channels */
58         pt->mute = 1;
59         bytes[0] = PT2258_CMD_MUTE;
60         snd_i2c_lock(pt->i2c_bus);
61         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
62                 goto __error;
63         snd_i2c_unlock(pt->i2c_bus);
64
65         /* set all channels to 0dB */
66         for (i = 0; i < 6; ++i)
67                 pt->volume[i] = 0;
68         bytes[0] = 0xd0;
69         bytes[1] = 0xe0;
70         snd_i2c_lock(pt->i2c_bus);
71         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
72                 goto __error;
73         snd_i2c_unlock(pt->i2c_bus);
74
75         return 0;
76
77       __error:
78         snd_i2c_unlock(pt->i2c_bus);
79         snd_printk(KERN_ERR "PT2258 reset failed\n");
80         return -EIO;
81 }
82
83 static int pt2258_stereo_volume_info(struct snd_kcontrol *kcontrol,
84                                      struct snd_ctl_elem_info *uinfo)
85 {
86         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
87         uinfo->count = 2;
88         uinfo->value.integer.min = 0;
89         uinfo->value.integer.max = 79;
90         return 0;
91 }
92
93 static int pt2258_stereo_volume_get(struct snd_kcontrol *kcontrol,
94                                     struct snd_ctl_elem_value *ucontrol)
95 {
96         struct snd_pt2258 *pt = kcontrol->private_data;
97         int base = kcontrol->private_value;
98
99         /* chip does not support register reads */
100         ucontrol->value.integer.value[0] = 79 - pt->volume[base];
101         ucontrol->value.integer.value[1] = 79 - pt->volume[base + 1];
102         return 0;
103 }
104
105 static int pt2258_stereo_volume_put(struct snd_kcontrol *kcontrol,
106                                     struct snd_ctl_elem_value *ucontrol)
107 {
108         struct snd_pt2258 *pt = kcontrol->private_data;
109         int base = kcontrol->private_value;
110         unsigned char bytes[2];
111         int val0, val1;
112
113         val0 = 79 - ucontrol->value.integer.value[0];
114         val1 = 79 - ucontrol->value.integer.value[1];
115         if (val0 < 0 || val0 > 79 || val1 < 0 || val1 > 79)
116                 return -EINVAL;
117         if (val0 == pt->volume[base] && val1 == pt->volume[base + 1])
118                 return 0;
119
120         pt->volume[base] = val0;
121         bytes[0] = pt2258_channel_code[2 * base] | (val0 / 10);
122         bytes[1] = pt2258_channel_code[2 * base + 1] | (val0 % 10);
123         snd_i2c_lock(pt->i2c_bus);
124         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
125                 goto __error;
126         snd_i2c_unlock(pt->i2c_bus);
127
128         pt->volume[base + 1] = val1;
129         bytes[0] = pt2258_channel_code[2 * base + 2] | (val1 / 10);
130         bytes[1] = pt2258_channel_code[2 * base + 3] | (val1 % 10);
131         snd_i2c_lock(pt->i2c_bus);
132         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
133                 goto __error;
134         snd_i2c_unlock(pt->i2c_bus);
135
136         return 1;
137
138       __error:
139         snd_i2c_unlock(pt->i2c_bus);
140         snd_printk(KERN_ERR "PT2258 access failed\n");
141         return -EIO;
142 }
143
144 #define pt2258_switch_info      snd_ctl_boolean_mono_info
145
146 static int pt2258_switch_get(struct snd_kcontrol *kcontrol,
147                              struct snd_ctl_elem_value *ucontrol)
148 {
149         struct snd_pt2258 *pt = kcontrol->private_data;
150
151         ucontrol->value.integer.value[0] = !pt->mute;
152         return 0;
153 }
154
155 static int pt2258_switch_put(struct snd_kcontrol *kcontrol,
156                              struct snd_ctl_elem_value *ucontrol)
157 {
158         struct snd_pt2258 *pt = kcontrol->private_data;
159         unsigned char bytes[2];
160         int val;
161
162         val = !ucontrol->value.integer.value[0];
163         if (pt->mute == val)
164                 return 0;
165
166         pt->mute = val;
167         bytes[0] = val ? PT2258_CMD_MUTE : PT2258_CMD_UNMUTE;
168         snd_i2c_lock(pt->i2c_bus);
169         if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
170                 goto __error;
171         snd_i2c_unlock(pt->i2c_bus);
172
173         return 1;
174
175       __error:
176         snd_i2c_unlock(pt->i2c_bus);
177         snd_printk(KERN_ERR "PT2258 access failed 2\n");
178         return -EIO;
179 }
180
181 static const DECLARE_TLV_DB_SCALE(pt2258_db_scale, -7900, 100, 0);
182
183 int snd_pt2258_build_controls(struct snd_pt2258 *pt)
184 {
185         struct snd_kcontrol_new knew;
186         char *names[3] = {
187                 "Mic Loopback Playback Volume",
188                 "Line Loopback Playback Volume",
189                 "CD Loopback Playback Volume"
190         };
191         int i, err;
192
193         for (i = 0; i < 3; ++i) {
194                 memset(&knew, 0, sizeof(knew));
195                 knew.name = names[i];
196                 knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
197                 knew.count = 1;
198                 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
199                     SNDRV_CTL_ELEM_ACCESS_TLV_READ;
200                 knew.private_value = 2 * i;
201                 knew.info = pt2258_stereo_volume_info;
202                 knew.get = pt2258_stereo_volume_get;
203                 knew.put = pt2258_stereo_volume_put;
204                 knew.tlv.p = pt2258_db_scale;
205
206                 err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
207                 if (err < 0)
208                         return err;
209         }
210
211         memset(&knew, 0, sizeof(knew));
212         knew.name = "Loopback Switch";
213         knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
214         knew.info = pt2258_switch_info;
215         knew.get = pt2258_switch_get;
216         knew.put = pt2258_switch_put;
217         knew.access = 0;
218         err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
219         if (err < 0)
220                 return err;
221
222         return 0;
223 }
224
225 EXPORT_SYMBOL(snd_pt2258_reset);
226 EXPORT_SYMBOL(snd_pt2258_build_controls);