1 /*****************************************************************************
2 * Copyright 2011 Broadcom Corporation. All rights reserved.
4 * Unless you and Broadcom execute a separate written software license
5 * agreement governing use of this software, this software is licensed to you
6 * under the terms of the GNU General Public License version 2, available at
7 * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
9 * Notwithstanding the above, under no circumstances may you combine this
10 * software in any way with any other Broadcom software provided under a
11 * license other than the GPL, without Broadcom's express prior written
13 *****************************************************************************/
15 #include <linux/platform_device.h>
16 #include <linux/init.h>
18 #include <linux/jiffies.h>
19 #include <linux/slab.h>
20 #include <linux/time.h>
21 #include <linux/wait.h>
22 #include <linux/delay.h>
23 #include <linux/moduleparam.h>
24 #include <linux/sched.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/rawmidi.h>
31 #include <sound/initval.h>
32 #include <sound/tlv.h>
33 #include <sound/asoundef.h>
37 /* volume maximum and minimum in terms of 0.01dB */
38 #define CTRL_VOL_MAX 400
39 #define CTRL_VOL_MIN -10239 /* originally -10240 */
41 static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
42 struct snd_ctl_elem_info *uinfo)
44 if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
45 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
47 uinfo->value.integer.min = CTRL_VOL_MIN;
48 uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
49 } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
50 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
52 uinfo->value.integer.min = 0;
53 uinfo->value.integer.max = 1;
54 } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
55 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
57 uinfo->value.integer.min = 0;
58 uinfo->value.integer.max = AUDIO_DEST_MAX - 1;
63 /* toggles mute on or off depending on the value of nmute, and returns
64 * 1 if the mute value was changed, otherwise 0
66 static int toggle_mute(struct bcm2835_chip *chip, int nmute)
68 /* if settings are ok, just return 0 */
69 if (chip->mute == nmute)
72 /* if the sound is muted then we need to unmute */
73 if (chip->mute == CTRL_VOL_MUTE) {
74 chip->volume = chip->old_volume; /* copy the old volume back */
75 audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
76 } else /* otherwise we mute */ {
77 chip->old_volume = chip->volume;
78 chip->volume = 26214; /* set volume to minimum level AKA mute */
79 audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
86 static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_value *ucontrol)
89 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
91 if (mutex_lock_interruptible(&chip->audio_mutex))
94 BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
96 if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
97 ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
98 else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
99 ucontrol->value.integer.value[0] = chip->mute;
100 else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
101 ucontrol->value.integer.value[0] = chip->dest;
103 mutex_unlock(&chip->audio_mutex);
107 static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
110 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
113 if (mutex_lock_interruptible(&chip->audio_mutex))
116 if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
117 audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
118 if (chip->mute == CTRL_VOL_MUTE) {
119 /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
120 changed = 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
123 if (changed || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
124 chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
128 } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
129 /* Now implemented */
130 audio_info(" Mute attempted\n");
131 changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
133 } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
134 if (ucontrol->value.integer.value[0] != chip->dest) {
135 chip->dest = ucontrol->value.integer.value[0];
140 if (changed && bcm2835_audio_set_ctls(chip))
141 dev_err(chip->card->dev, "Failed to set ALSA controls..\n");
144 mutex_unlock(&chip->audio_mutex);
148 static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
150 static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
152 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
153 .name = "PCM Playback Volume",
155 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
156 .private_value = PCM_PLAYBACK_VOLUME,
157 .info = snd_bcm2835_ctl_info,
158 .get = snd_bcm2835_ctl_get,
159 .put = snd_bcm2835_ctl_put,
161 .tlv = {.p = snd_bcm2835_db_scale}
164 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
165 .name = "PCM Playback Switch",
167 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
168 .private_value = PCM_PLAYBACK_MUTE,
169 .info = snd_bcm2835_ctl_info,
170 .get = snd_bcm2835_ctl_get,
171 .put = snd_bcm2835_ctl_put,
175 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
176 .name = "PCM Playback Route",
178 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
179 .private_value = PCM_PLAYBACK_DEVICE,
180 .info = snd_bcm2835_ctl_info,
181 .get = snd_bcm2835_ctl_get,
182 .put = snd_bcm2835_ctl_put,
187 static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
188 struct snd_ctl_elem_info *uinfo)
190 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
195 static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
198 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
201 if (mutex_lock_interruptible(&chip->audio_mutex))
204 for (i = 0; i < 4; i++)
205 ucontrol->value.iec958.status[i] =
206 (chip->spdif_status >> (i * 8)) & 0xff;
208 mutex_unlock(&chip->audio_mutex);
212 static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
215 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
216 unsigned int val = 0;
219 if (mutex_lock_interruptible(&chip->audio_mutex))
222 for (i = 0; i < 4; i++)
223 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
225 change = val != chip->spdif_status;
226 chip->spdif_status = val;
228 mutex_unlock(&chip->audio_mutex);
232 static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
233 struct snd_ctl_elem_info *uinfo)
235 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
240 static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_value *ucontrol)
244 * bcm2835 supports only consumer mode and sets all other format flags
245 * automatically. So the only thing left is signalling non-audio content
247 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
251 static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_info *uinfo)
254 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
259 static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_value *ucontrol)
262 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
265 if (mutex_lock_interruptible(&chip->audio_mutex))
268 for (i = 0; i < 4; i++)
269 ucontrol->value.iec958.status[i] =
270 (chip->spdif_status >> (i * 8)) & 0xff;
272 mutex_unlock(&chip->audio_mutex);
276 static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_value *ucontrol)
279 struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
280 unsigned int val = 0;
283 if (mutex_lock_interruptible(&chip->audio_mutex))
286 for (i = 0; i < 4; i++)
287 val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
288 change = val != chip->spdif_status;
289 chip->spdif_status = val;
291 mutex_unlock(&chip->audio_mutex);
295 static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
297 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
298 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
299 .info = snd_bcm2835_spdif_default_info,
300 .get = snd_bcm2835_spdif_default_get,
301 .put = snd_bcm2835_spdif_default_put
304 .access = SNDRV_CTL_ELEM_ACCESS_READ,
305 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
306 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
307 .info = snd_bcm2835_spdif_mask_info,
308 .get = snd_bcm2835_spdif_mask_get,
311 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
312 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
313 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
314 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
315 .info = snd_bcm2835_spdif_stream_info,
316 .get = snd_bcm2835_spdif_stream_get,
317 .put = snd_bcm2835_spdif_stream_put,
321 int snd_bcm2835_new_ctl(struct bcm2835_chip *chip)
326 strcpy(chip->card->mixername, "Broadcom Mixer");
327 for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
328 err = snd_ctl_add(chip->card,
329 snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
333 for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
334 err = snd_ctl_add(chip->card,
335 snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
342 static struct snd_kcontrol_new snd_bcm2835_headphones_ctl[] = {
344 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
345 .name = "Headphone Playback Volume",
347 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
348 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
349 .private_value = PCM_PLAYBACK_VOLUME,
350 .info = snd_bcm2835_ctl_info,
351 .get = snd_bcm2835_ctl_get,
352 .put = snd_bcm2835_ctl_put,
354 .tlv = {.p = snd_bcm2835_db_scale}
357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
358 .name = "Headphone Playback Switch",
360 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
361 .private_value = PCM_PLAYBACK_MUTE,
362 .info = snd_bcm2835_ctl_info,
363 .get = snd_bcm2835_ctl_get,
364 .put = snd_bcm2835_ctl_put,
369 int snd_bcm2835_new_headphones_ctl(struct bcm2835_chip *chip)
374 strcpy(chip->card->mixername, "Broadcom Mixer");
375 for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_headphones_ctl); idx++) {
376 err = snd_ctl_add(chip->card,
377 snd_ctl_new1(&snd_bcm2835_headphones_ctl[idx],
385 static struct snd_kcontrol_new snd_bcm2835_hdmi[] = {
387 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
388 .name = "HDMI Playback Volume",
390 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
391 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
392 .private_value = PCM_PLAYBACK_VOLUME,
393 .info = snd_bcm2835_ctl_info,
394 .get = snd_bcm2835_ctl_get,
395 .put = snd_bcm2835_ctl_put,
397 .tlv = {.p = snd_bcm2835_db_scale}
400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
401 .name = "HDMI Playback Switch",
403 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
404 .private_value = PCM_PLAYBACK_MUTE,
405 .info = snd_bcm2835_ctl_info,
406 .get = snd_bcm2835_ctl_get,
407 .put = snd_bcm2835_ctl_put,
412 int snd_bcm2835_new_hdmi_ctl(struct bcm2835_chip *chip)
417 strcpy(chip->card->mixername, "Broadcom Mixer");
418 for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_hdmi); idx++) {
419 err = snd_ctl_add(chip->card,
420 snd_ctl_new1(&snd_bcm2835_hdmi[idx], chip));