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