|
| 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 | +#include <sound/asoundef.h> |
| 34 | + |
| 35 | +#include "bcm2835.h" |
| 36 | + |
| 37 | +/* volume maximum and minimum in terms of 0.01dB */ |
| 38 | +#define CTRL_VOL_MAX 400 |
| 39 | +#define CTRL_VOL_MIN -10239 /* originally -10240 */ |
| 40 | + |
| 41 | + |
| 42 | +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol, |
| 43 | + struct snd_ctl_elem_info *uinfo) |
| 44 | +{ |
| 45 | + audio_info(" ... IN\n"); |
| 46 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) { |
| 47 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 48 | + uinfo->count = 1; |
| 49 | + uinfo->value.integer.min = CTRL_VOL_MIN; |
| 50 | + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */ |
| 51 | + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) { |
| 52 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 53 | + uinfo->count = 1; |
| 54 | + uinfo->value.integer.min = 0; |
| 55 | + uinfo->value.integer.max = 1; |
| 56 | + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) { |
| 57 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 58 | + uinfo->count = 1; |
| 59 | + uinfo->value.integer.min = 0; |
| 60 | + uinfo->value.integer.max = AUDIO_DEST_MAX-1; |
| 61 | + } |
| 62 | + audio_info(" ... OUT\n"); |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +/* toggles mute on or off depending on the value of nmute, and returns |
| 67 | + * 1 if the mute value was changed, otherwise 0 |
| 68 | + */ |
| 69 | +static int toggle_mute(struct bcm2835_chip *chip, int nmute) |
| 70 | +{ |
| 71 | + /* if settings are ok, just return 0 */ |
| 72 | + if(chip->mute == nmute) |
| 73 | + return 0; |
| 74 | + |
| 75 | + /* if the sound is muted then we need to unmute */ |
| 76 | + if(chip->mute == CTRL_VOL_MUTE) |
| 77 | + { |
| 78 | + chip->volume = chip->old_volume; /* copy the old volume back */ |
| 79 | + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume); |
| 80 | + } |
| 81 | + else /* otherwise we mute */ |
| 82 | + { |
| 83 | + chip->old_volume = chip->volume; |
| 84 | + chip->volume = 26214; /* set volume to minimum level AKA mute */ |
| 85 | + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume); |
| 86 | + } |
| 87 | + |
| 88 | + chip->mute = nmute; |
| 89 | + return 1; |
| 90 | +} |
| 91 | + |
| 92 | +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol, |
| 93 | + struct snd_ctl_elem_value *ucontrol) |
| 94 | +{ |
| 95 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 96 | + |
| 97 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 98 | + return -EINTR; |
| 99 | + |
| 100 | + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK)); |
| 101 | + |
| 102 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) |
| 103 | + ucontrol->value.integer.value[0] = chip2alsa(chip->volume); |
| 104 | + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) |
| 105 | + ucontrol->value.integer.value[0] = chip->mute; |
| 106 | + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) |
| 107 | + ucontrol->value.integer.value[0] = chip->dest; |
| 108 | + |
| 109 | + mutex_unlock(&chip->audio_mutex); |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol, |
| 114 | + struct snd_ctl_elem_value *ucontrol) |
| 115 | +{ |
| 116 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 117 | + int changed = 0; |
| 118 | + |
| 119 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 120 | + return -EINTR; |
| 121 | + |
| 122 | + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) { |
| 123 | + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]); |
| 124 | + if (chip->mute == CTRL_VOL_MUTE) { |
| 125 | + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */ |
| 126 | + changed = 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */ |
| 127 | + goto unlock; |
| 128 | + } |
| 129 | + if (changed |
| 130 | + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) { |
| 131 | + |
| 132 | + chip->volume = alsa2chip(ucontrol->value.integer.value[0]); |
| 133 | + changed = 1; |
| 134 | + } |
| 135 | + |
| 136 | + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) { |
| 137 | + /* Now implemented */ |
| 138 | + audio_info(" Mute attempted\n"); |
| 139 | + changed = toggle_mute(chip, ucontrol->value.integer.value[0]); |
| 140 | + |
| 141 | + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) { |
| 142 | + if (ucontrol->value.integer.value[0] != chip->dest) { |
| 143 | + chip->dest = ucontrol->value.integer.value[0]; |
| 144 | + changed = 1; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (changed) { |
| 149 | + if (bcm2835_audio_set_ctls(chip)) |
| 150 | + printk(KERN_ERR "Failed to set ALSA controls..\n"); |
| 151 | + } |
| 152 | + |
| 153 | +unlock: |
| 154 | + mutex_unlock(&chip->audio_mutex); |
| 155 | + return changed; |
| 156 | +} |
| 157 | + |
| 158 | +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1); |
| 159 | + |
| 160 | +static struct snd_kcontrol_new snd_bcm2835_ctl[] = { |
| 161 | + { |
| 162 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 163 | + .name = "PCM Playback Volume", |
| 164 | + .index = 0, |
| 165 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, |
| 166 | + .private_value = PCM_PLAYBACK_VOLUME, |
| 167 | + .info = snd_bcm2835_ctl_info, |
| 168 | + .get = snd_bcm2835_ctl_get, |
| 169 | + .put = snd_bcm2835_ctl_put, |
| 170 | + .count = 1, |
| 171 | + .tlv = {.p = snd_bcm2835_db_scale} |
| 172 | + }, |
| 173 | + { |
| 174 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 175 | + .name = "PCM Playback Switch", |
| 176 | + .index = 0, |
| 177 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 178 | + .private_value = PCM_PLAYBACK_MUTE, |
| 179 | + .info = snd_bcm2835_ctl_info, |
| 180 | + .get = snd_bcm2835_ctl_get, |
| 181 | + .put = snd_bcm2835_ctl_put, |
| 182 | + .count = 1, |
| 183 | + }, |
| 184 | + { |
| 185 | + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 186 | + .name = "PCM Playback Route", |
| 187 | + .index = 0, |
| 188 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 189 | + .private_value = PCM_PLAYBACK_DEVICE, |
| 190 | + .info = snd_bcm2835_ctl_info, |
| 191 | + .get = snd_bcm2835_ctl_get, |
| 192 | + .put = snd_bcm2835_ctl_put, |
| 193 | + .count = 1, |
| 194 | + }, |
| 195 | +}; |
| 196 | + |
| 197 | +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol, |
| 198 | + struct snd_ctl_elem_info *uinfo) |
| 199 | +{ |
| 200 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 201 | + uinfo->count = 1; |
| 202 | + return 0; |
| 203 | +} |
| 204 | + |
| 205 | +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol, |
| 206 | + struct snd_ctl_elem_value *ucontrol) |
| 207 | +{ |
| 208 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 209 | + int i; |
| 210 | + |
| 211 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 212 | + return -EINTR; |
| 213 | + |
| 214 | + for (i = 0; i < 4; i++) |
| 215 | + ucontrol->value.iec958.status[i] = |
| 216 | + (chip->spdif_status >> (i * 8)) && 0xff; |
| 217 | + |
| 218 | + mutex_unlock(&chip->audio_mutex); |
| 219 | + return 0; |
| 220 | +} |
| 221 | + |
| 222 | +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol, |
| 223 | + struct snd_ctl_elem_value *ucontrol) |
| 224 | +{ |
| 225 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 226 | + unsigned int val = 0; |
| 227 | + int i, change; |
| 228 | + |
| 229 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 230 | + return -EINTR; |
| 231 | + |
| 232 | + for (i = 0; i < 4; i++) |
| 233 | + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8); |
| 234 | + |
| 235 | + change = val != chip->spdif_status; |
| 236 | + chip->spdif_status = val; |
| 237 | + |
| 238 | + mutex_unlock(&chip->audio_mutex); |
| 239 | + return change; |
| 240 | +} |
| 241 | + |
| 242 | +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol, |
| 243 | + struct snd_ctl_elem_info *uinfo) |
| 244 | +{ |
| 245 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 246 | + uinfo->count = 1; |
| 247 | + return 0; |
| 248 | +} |
| 249 | + |
| 250 | +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol, |
| 251 | + struct snd_ctl_elem_value *ucontrol) |
| 252 | +{ |
| 253 | + /* bcm2835 supports only consumer mode and sets all other format flags |
| 254 | + * automatically. So the only thing left is signalling non-audio |
| 255 | + * content */ |
| 256 | + ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO; |
| 257 | + return 0; |
| 258 | +} |
| 259 | + |
| 260 | +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol, |
| 261 | + struct snd_ctl_elem_info *uinfo) |
| 262 | +{ |
| 263 | + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 264 | + uinfo->count = 1; |
| 265 | + return 0; |
| 266 | +} |
| 267 | + |
| 268 | +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol, |
| 269 | + struct snd_ctl_elem_value *ucontrol) |
| 270 | +{ |
| 271 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 272 | + int i; |
| 273 | + |
| 274 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 275 | + return -EINTR; |
| 276 | + |
| 277 | + for (i = 0; i < 4; i++) |
| 278 | + ucontrol->value.iec958.status[i] = |
| 279 | + (chip->spdif_status >> (i * 8)) & 0xff; |
| 280 | + |
| 281 | + mutex_unlock(&chip->audio_mutex); |
| 282 | + return 0; |
| 283 | +} |
| 284 | + |
| 285 | +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol, |
| 286 | + struct snd_ctl_elem_value *ucontrol) |
| 287 | +{ |
| 288 | + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol); |
| 289 | + unsigned int val = 0; |
| 290 | + int i, change; |
| 291 | + |
| 292 | + if (mutex_lock_interruptible(&chip->audio_mutex)) |
| 293 | + return -EINTR; |
| 294 | + |
| 295 | + for (i = 0; i < 4; i++) |
| 296 | + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8); |
| 297 | + change = val != chip->spdif_status; |
| 298 | + chip->spdif_status = val; |
| 299 | + |
| 300 | + mutex_unlock(&chip->audio_mutex); |
| 301 | + return change; |
| 302 | +} |
| 303 | + |
| 304 | +static struct snd_kcontrol_new snd_bcm2835_spdif[] = { |
| 305 | + { |
| 306 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 307 | + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 308 | + .info = snd_bcm2835_spdif_default_info, |
| 309 | + .get = snd_bcm2835_spdif_default_get, |
| 310 | + .put = snd_bcm2835_spdif_default_put |
| 311 | + }, |
| 312 | + { |
| 313 | + .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 314 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 315 | + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), |
| 316 | + .info = snd_bcm2835_spdif_mask_info, |
| 317 | + .get = snd_bcm2835_spdif_mask_get, |
| 318 | + }, |
| 319 | + { |
| 320 | + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | |
| 321 | + SNDRV_CTL_ELEM_ACCESS_INACTIVE, |
| 322 | + .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 323 | + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM), |
| 324 | + .info = snd_bcm2835_spdif_stream_info, |
| 325 | + .get = snd_bcm2835_spdif_stream_get, |
| 326 | + .put = snd_bcm2835_spdif_stream_put, |
| 327 | + }, |
| 328 | +}; |
| 329 | + |
| 330 | +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip) |
| 331 | +{ |
| 332 | + int err; |
| 333 | + unsigned int idx; |
| 334 | + |
| 335 | + strcpy(chip->card->mixername, "Broadcom Mixer"); |
| 336 | + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) { |
| 337 | + err = |
| 338 | + snd_ctl_add(chip->card, |
| 339 | + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip)); |
| 340 | + if (err < 0) |
| 341 | + return err; |
| 342 | + } |
| 343 | + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) { |
| 344 | + err = snd_ctl_add(chip->card, |
| 345 | + snd_ctl_new1(&snd_bcm2835_spdif[idx], chip)); |
| 346 | + if (err < 0) |
| 347 | + return err; |
| 348 | + } |
| 349 | + return 0; |
| 350 | +} |
0 commit comments