1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA Driver for the PT2258 volume controller.
5 * Copyright (c) 2006 Jochen Voss <voss@seehuhn.de>
8 #include <sound/core.h>
9 #include <sound/control.h>
10 #include <sound/tlv.h>
11 #include <sound/i2c.h>
12 #include <sound/pt2258.h>
13 #include <linux/module.h>
15 MODULE_AUTHOR("Jochen Voss <voss@seehuhn.de>");
16 MODULE_DESCRIPTION("PT2258 volume controller (Princeton Technology Corp.)");
17 MODULE_LICENSE("GPL");
19 #define PT2258_CMD_RESET 0xc0
20 #define PT2258_CMD_UNMUTE 0xf8
21 #define PT2258_CMD_MUTE 0xf9
23 static const unsigned char pt2258_channel_code[12] = {
24 0x80, 0x90, /* channel 1: -10dB, -1dB */
25 0x40, 0x50, /* channel 2: -10dB, -1dB */
26 0x00, 0x10, /* channel 3: -10dB, -1dB */
27 0x20, 0x30, /* channel 4: -10dB, -1dB */
28 0x60, 0x70, /* channel 5: -10dB, -1dB */
29 0xa0, 0xb0 /* channel 6: -10dB, -1dB */
32 int snd_pt2258_reset(struct snd_pt2258 *pt)
34 unsigned char bytes[2];
38 bytes[0] = PT2258_CMD_RESET;
39 snd_i2c_lock(pt->i2c_bus);
40 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
42 snd_i2c_unlock(pt->i2c_bus);
44 /* mute all channels */
46 bytes[0] = PT2258_CMD_MUTE;
47 snd_i2c_lock(pt->i2c_bus);
48 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
50 snd_i2c_unlock(pt->i2c_bus);
52 /* set all channels to 0dB */
53 for (i = 0; i < 6; ++i)
57 snd_i2c_lock(pt->i2c_bus);
58 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
60 snd_i2c_unlock(pt->i2c_bus);
65 snd_i2c_unlock(pt->i2c_bus);
66 snd_printk(KERN_ERR "PT2258 reset failed\n");
70 static int pt2258_stereo_volume_info(struct snd_kcontrol *kcontrol,
71 struct snd_ctl_elem_info *uinfo)
73 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
75 uinfo->value.integer.min = 0;
76 uinfo->value.integer.max = 79;
80 static int pt2258_stereo_volume_get(struct snd_kcontrol *kcontrol,
81 struct snd_ctl_elem_value *ucontrol)
83 struct snd_pt2258 *pt = kcontrol->private_data;
84 int base = kcontrol->private_value;
86 /* chip does not support register reads */
87 ucontrol->value.integer.value[0] = 79 - pt->volume[base];
88 ucontrol->value.integer.value[1] = 79 - pt->volume[base + 1];
92 static int pt2258_stereo_volume_put(struct snd_kcontrol *kcontrol,
93 struct snd_ctl_elem_value *ucontrol)
95 struct snd_pt2258 *pt = kcontrol->private_data;
96 int base = kcontrol->private_value;
97 unsigned char bytes[2];
100 val0 = 79 - ucontrol->value.integer.value[0];
101 val1 = 79 - ucontrol->value.integer.value[1];
102 if (val0 < 0 || val0 > 79 || val1 < 0 || val1 > 79)
104 if (val0 == pt->volume[base] && val1 == pt->volume[base + 1])
107 pt->volume[base] = val0;
108 bytes[0] = pt2258_channel_code[2 * base] | (val0 / 10);
109 bytes[1] = pt2258_channel_code[2 * base + 1] | (val0 % 10);
110 snd_i2c_lock(pt->i2c_bus);
111 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
113 snd_i2c_unlock(pt->i2c_bus);
115 pt->volume[base + 1] = val1;
116 bytes[0] = pt2258_channel_code[2 * base + 2] | (val1 / 10);
117 bytes[1] = pt2258_channel_code[2 * base + 3] | (val1 % 10);
118 snd_i2c_lock(pt->i2c_bus);
119 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 2) != 2)
121 snd_i2c_unlock(pt->i2c_bus);
126 snd_i2c_unlock(pt->i2c_bus);
127 snd_printk(KERN_ERR "PT2258 access failed\n");
131 #define pt2258_switch_info snd_ctl_boolean_mono_info
133 static int pt2258_switch_get(struct snd_kcontrol *kcontrol,
134 struct snd_ctl_elem_value *ucontrol)
136 struct snd_pt2258 *pt = kcontrol->private_data;
138 ucontrol->value.integer.value[0] = !pt->mute;
142 static int pt2258_switch_put(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value *ucontrol)
145 struct snd_pt2258 *pt = kcontrol->private_data;
146 unsigned char bytes[2];
149 val = !ucontrol->value.integer.value[0];
154 bytes[0] = val ? PT2258_CMD_MUTE : PT2258_CMD_UNMUTE;
155 snd_i2c_lock(pt->i2c_bus);
156 if (snd_i2c_sendbytes(pt->i2c_dev, bytes, 1) != 1)
158 snd_i2c_unlock(pt->i2c_bus);
163 snd_i2c_unlock(pt->i2c_bus);
164 snd_printk(KERN_ERR "PT2258 access failed 2\n");
168 static const DECLARE_TLV_DB_SCALE(pt2258_db_scale, -7900, 100, 0);
170 int snd_pt2258_build_controls(struct snd_pt2258 *pt)
172 struct snd_kcontrol_new knew;
174 "Mic Loopback Playback Volume",
175 "Line Loopback Playback Volume",
176 "CD Loopback Playback Volume"
180 for (i = 0; i < 3; ++i) {
181 memset(&knew, 0, sizeof(knew));
182 knew.name = names[i];
183 knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
185 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
186 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
187 knew.private_value = 2 * i;
188 knew.info = pt2258_stereo_volume_info;
189 knew.get = pt2258_stereo_volume_get;
190 knew.put = pt2258_stereo_volume_put;
191 knew.tlv.p = pt2258_db_scale;
193 err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
198 memset(&knew, 0, sizeof(knew));
199 knew.name = "Loopback Switch";
200 knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
201 knew.info = pt2258_switch_info;
202 knew.get = pt2258_switch_get;
203 knew.put = pt2258_switch_put;
205 err = snd_ctl_add(pt->card, snd_ctl_new1(&knew, pt));
212 EXPORT_SYMBOL(snd_pt2258_reset);
213 EXPORT_SYMBOL(snd_pt2258_build_controls);