|
| 1 | +/***************************************************************************** |
| 2 | +* Copyright 2011 Broadcom Corporation. All rights reserved. |
| 3 | +* |
| 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"). |
| 8 | +* |
| 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 |
| 12 | +* consent. |
| 13 | +*****************************************************************************/ |
| 14 | + |
| 15 | +#include <linux/platform_device.h> |
| 16 | +#include <linux/init.h> |
| 17 | +#include <linux/io.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> |
| 25 | + |
| 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 | + |
| 34 | +#include "bcm2835.h" |
| 35 | + |
| 36 | +/* volume maximum and minimum in terms of 0.01dB */ |
| 37 | +#define CTRL_VOL_MAX 400 |
| 38 | +#define CTRL_VOL_MIN -10239 /* originally -10240 */ |
| 39 | + |
| 40 | + |
| 41 | +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol, |
| 42 | + struct snd_ctl_elem_info *uinfo) |
| 43 | +{ |
| 44 | + audio_info(" ... IN\n"); |
| 45 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) { |
| 46 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 47 | + uinfo->count = 1; |
| 48 | + uinfo->value.integer.min = CTRL_VOL_MIN; |
| 49 | + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */ |
| 50 | + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) { |
| 51 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 52 | + uinfo->count = 1; |
| 53 | + uinfo->value.integer.min = 0; |
| 54 | + uinfo->value.integer.max = 1; |
| 55 | + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) { |
| 56 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 57 | + uinfo->count = 1; |
| 58 | + uinfo->value.integer.min = 0; |
| 59 | + uinfo->value.integer.max = AUDIO_DEST_MAX-1; |
| 60 | + } |
| 61 | + audio_info(" ... OUT\n"); |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +/* toggles mute on or off depending on the value of nmute, and returns |
| 66 | + * 1 if the mute value was changed, otherwise 0 |
| 67 | + */ |
| 68 | +static int toggle_mute(struct bcm2835_chip *chip, int nmute) |
| 69 | +{ |
| 70 | + /* if settings are ok, just return 0 */ |
| 71 | + if(chip->mute == nmute) |
| 72 | + return 0; |
| 73 | + |
| 74 | + /* if the sound is muted then we need to unmute */ |
| 75 | + if(chip->mute == CTRL_VOL_MUTE) |
| 76 | + { |
| 77 | + chip->volume = chip->old_volume; /* copy the old volume back */ |
| 78 | + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume); |
| 79 | + } |
| 80 | + else /* otherwise we mute */ |
| 81 | + { |
| 82 | + chip->old_volume = chip->volume; |
| 83 | + chip->volume = 26214; /* set volume to minimum level AKA mute */ |
| 84 | + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume); |
| 85 | + } |
| 86 | + |
| 87 | + chip->mute = nmute; |
| 88 | + return 1; |
| 89 | +} |
| 90 | + |
| 91 | +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol, |
| 92 | + struct snd_ctl_elem_value *ucontrol) |
| 93 | +{ |
| 94 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 95 | + |
| 96 | + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK)); |
| 97 | + |
| 98 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) |
| 99 | + ucontrol->value.integer.value[0] = chip2alsa(chip->volume); |
| 100 | + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) |
| 101 | + ucontrol->value.integer.value[0] = chip->mute; |
| 102 | + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) |
| 103 | + ucontrol->value.integer.value[0] = chip->dest; |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol, |
| 109 | + struct snd_ctl_elem_value *ucontrol) |
| 110 | +{ |
| 111 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 112 | + int changed = 0; |
| 113 | + |
| 114 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) { |
| 115 | + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]); |
| 116 | + if (chip->mute == CTRL_VOL_MUTE) { |
| 117 | + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */ |
| 118 | + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */ |
| 119 | + } |
| 120 | + if (changed |
| 121 | + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) { |
| 122 | + |
| 123 | + chip->volume = alsa2chip(ucontrol->value.integer.value[0]); |
| 124 | + changed = 1; |
| 125 | + } |
| 126 | + |
| 127 | + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) { |
| 128 | + /* Now implemented */ |
| 129 | + audio_info(" Mute attempted\n"); |
| 130 | + changed = toggle_mute(chip, ucontrol->value.integer.value[0]); |
| 131 | + |
| 132 | + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) { |
| 133 | + if (ucontrol->value.integer.value[0] != chip->dest) { |
| 134 | + chip->dest = ucontrol->value.integer.value[0]; |
| 135 | + changed = 1; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (changed) { |
| 140 | + if (bcm2835_audio_set_ctls(chip)) |
| 141 | + printk(KERN_ERR "Failed to set ALSA controls..\n"); |
| 142 | + } |
| 143 | + |
| 144 | + return changed; |
| 145 | +} |
| 146 | + |
| 147 | +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1); |
| 148 | + |
| 149 | +static struct snd_kcontrol_new snd_bcm2835_ctl[] = { |
| 150 | + { |
| 151 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 152 | + .name = "PCM Playback Volume", |
| 153 | + .index = 0, |
| 154 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 155 | + .private_value = PCM_PLAYBACK_VOLUME, |
| 156 | + .info = snd_bcm2835_ctl_info, |
| 157 | + .get = snd_bcm2835_ctl_get, |
| 158 | + .put = snd_bcm2835_ctl_put, |
| 159 | + .count = 1, |
| 160 | + .tlv = {.p = snd_bcm2835_db_scale} |
| 161 | + }, |
| 162 | + { |
| 163 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 164 | + .name = "PCM Playback Switch", |
| 165 | + .index = 0, |
| 166 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 167 | + .private_value = PCM_PLAYBACK_MUTE, |
| 168 | + .info = snd_bcm2835_ctl_info, |
| 169 | + .get = snd_bcm2835_ctl_get, |
| 170 | + .put = snd_bcm2835_ctl_put, |
| 171 | + .count = 1, |
| 172 | + }, |
| 173 | + { |
| 174 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 175 | + .name = "PCM Playback Route", |
| 176 | + .index = 0, |
| 177 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 178 | + .private_value = PCM_PLAYBACK_DEVICE, |
| 179 | + .info = snd_bcm2835_ctl_info, |
| 180 | + .get = snd_bcm2835_ctl_get, |
| 181 | + .put = snd_bcm2835_ctl_put, |
| 182 | + .count = 1, |
| 183 | + }, |
| 184 | +}; |
| 185 | + |
| 186 | +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip) |
| 187 | +{ |
| 188 | + int err; |
| 189 | + unsigned int idx; |
| 190 | + |
| 191 | + strcpy(chip->card->mixername, "Broadcom Mixer"); |
| 192 | + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) { |
| 193 | + err = |
| 194 | + snd_ctl_add(chip->card, |
| 195 | + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip)); |
| 196 | + if (err < 0) |
| 197 | + return err; |
| 198 | + } |
| 199 | + return 0; |
| 200 | +} |
0 commit comments