1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Digital Beep Input Interface for HD-audio codec 4 * 5 * Author: Matt Ranostay <matt.ranostay@konsulko.com> 6 * Copyright (c) 2008 Embedded Alley Solutions Inc 7 */ 8 9 #include <linux/input.h> 10 #include <linux/slab.h> 11 #include <linux/workqueue.h> 12 #include <linux/export.h> 13 #include <sound/core.h> 14 #include "hda_beep.h" 15 #include "hda_local.h" 16 17 enum { 18 DIGBEEP_HZ_STEP = 46875, /* 46.875 Hz */ 19 DIGBEEP_HZ_MIN = 93750, /* 93.750 Hz */ 20 DIGBEEP_HZ_MAX = 12000000, /* 12 KHz */ 21 }; 22 23 /* generate or stop tone */ 24 static void generate_tone(struct hda_beep *beep, int tone) 25 { 26 struct hda_codec *codec = beep->codec; 27 28 if (tone && !beep->playing) { 29 snd_hda_power_up(codec); 30 if (beep->power_hook) 31 beep->power_hook(beep, true); 32 beep->playing = 1; 33 } 34 if (!codec->beep_just_power_on) 35 snd_hda_codec_write(codec, beep->nid, 0, 36 AC_VERB_SET_BEEP_CONTROL, tone); 37 if (!tone && beep->playing) { 38 beep->playing = 0; 39 if (beep->power_hook) 40 beep->power_hook(beep, false); 41 snd_hda_power_down(codec); 42 } 43 } 44 45 static void snd_hda_generate_beep(struct work_struct *work) 46 { 47 struct hda_beep *beep = 48 container_of(work, struct hda_beep, beep_work); 49 50 if (beep->enabled) 51 generate_tone(beep, beep->tone); 52 } 53 54 /* (non-standard) Linear beep tone calculation for IDT/STAC codecs 55 * 56 * The tone frequency of beep generator on IDT/STAC codecs is 57 * defined from the 8bit tone parameter, in Hz, 58 * freq = 48000 * (257 - tone) / 1024 59 * that is from 12kHz to 93.75Hz in steps of 46.875 Hz 60 */ 61 static int beep_linear_tone(struct hda_beep *beep, int hz) 62 { 63 if (hz <= 0) 64 return 0; 65 hz *= 1000; /* fixed point */ 66 hz = hz - DIGBEEP_HZ_MIN 67 + DIGBEEP_HZ_STEP / 2; /* round to nearest step */ 68 if (hz < 0) 69 hz = 0; /* turn off PC beep*/ 70 else if (hz >= (DIGBEEP_HZ_MAX - DIGBEEP_HZ_MIN)) 71 hz = 1; /* max frequency */ 72 else { 73 hz /= DIGBEEP_HZ_STEP; 74 hz = 255 - hz; 75 } 76 return hz; 77 } 78 79 /* HD-audio standard beep tone parameter calculation 80 * 81 * The tone frequency in Hz is calculated as 82 * freq = 48000 / (tone * 4) 83 * from 47Hz to 12kHz 84 */ 85 static int beep_standard_tone(struct hda_beep *beep, int hz) 86 { 87 if (hz <= 0) 88 return 0; /* disabled */ 89 hz = 12000 / hz; 90 if (hz > 0xff) 91 return 0xff; 92 if (hz <= 0) 93 return 1; 94 return hz; 95 } 96 97 static int snd_hda_beep_event(struct input_dev *dev, unsigned int type, 98 unsigned int code, int hz) 99 { 100 struct hda_beep *beep = input_get_drvdata(dev); 101 102 switch (code) { 103 case SND_BELL: 104 if (hz) 105 hz = 1000; 106 fallthrough; 107 case SND_TONE: 108 if (beep->linear_tone) 109 beep->tone = beep_linear_tone(beep, hz); 110 else 111 beep->tone = beep_standard_tone(beep, hz); 112 break; 113 default: 114 return -1; 115 } 116 117 /* schedule beep event */ 118 schedule_work(&beep->beep_work); 119 return 0; 120 } 121 122 static void turn_on_beep(struct hda_beep *beep) 123 { 124 if (beep->keep_power_at_enable) 125 snd_hda_power_up_pm(beep->codec); 126 } 127 128 static void turn_off_beep(struct hda_beep *beep) 129 { 130 cancel_work_sync(&beep->beep_work); 131 if (beep->playing) { 132 /* turn off beep */ 133 generate_tone(beep, 0); 134 } 135 if (beep->keep_power_at_enable) 136 snd_hda_power_down_pm(beep->codec); 137 } 138 139 /** 140 * snd_hda_enable_beep_device - Turn on/off beep sound 141 * @codec: the HDA codec 142 * @enable: flag to turn on/off 143 */ 144 int snd_hda_enable_beep_device(struct hda_codec *codec, int enable) 145 { 146 struct hda_beep *beep = codec->beep; 147 if (!beep) 148 return 0; 149 enable = !!enable; 150 if (beep->enabled != enable) { 151 beep->enabled = enable; 152 if (enable) 153 turn_on_beep(beep); 154 else 155 turn_off_beep(beep); 156 return 1; 157 } 158 return 0; 159 } 160 EXPORT_SYMBOL_GPL(snd_hda_enable_beep_device); 161 162 static int beep_dev_register(struct snd_device *device) 163 { 164 struct hda_beep *beep = device->device_data; 165 int err; 166 167 err = input_register_device(beep->dev); 168 if (!err) 169 beep->registered = true; 170 return err; 171 } 172 173 static int beep_dev_disconnect(struct snd_device *device) 174 { 175 struct hda_beep *beep = device->device_data; 176 177 if (beep->registered) 178 input_unregister_device(beep->dev); 179 else 180 input_free_device(beep->dev); 181 if (beep->enabled) 182 turn_off_beep(beep); 183 return 0; 184 } 185 186 static int beep_dev_free(struct snd_device *device) 187 { 188 struct hda_beep *beep = device->device_data; 189 190 beep->codec->beep = NULL; 191 kfree(beep); 192 return 0; 193 } 194 195 /** 196 * snd_hda_attach_beep_device - Attach a beep input device 197 * @codec: the HDA codec 198 * @nid: beep NID 199 * 200 * Attach a beep object to the given widget. If beep hint is turned off 201 * explicitly or beep_mode of the codec is turned off, this doesn't nothing. 202 * 203 * Currently, only one beep device is allowed to each codec. 204 */ 205 int snd_hda_attach_beep_device(struct hda_codec *codec, int nid) 206 { 207 static const struct snd_device_ops ops = { 208 .dev_register = beep_dev_register, 209 .dev_disconnect = beep_dev_disconnect, 210 .dev_free = beep_dev_free, 211 }; 212 struct input_dev *input_dev; 213 struct hda_beep *beep; 214 int err; 215 216 if (!codec->beep_just_power_on) { 217 if (!snd_hda_get_bool_hint(codec, "beep")) 218 return 0; /* disabled explicitly by hints */ 219 if (codec->beep_mode == HDA_BEEP_MODE_OFF) 220 return 0; /* disabled by module option */ 221 } 222 223 beep = kzalloc(sizeof(*beep), GFP_KERNEL); 224 if (beep == NULL) 225 return -ENOMEM; 226 snprintf(beep->phys, sizeof(beep->phys), 227 "card%d/codec#%d/beep0", codec->card->number, codec->addr); 228 /* enable linear scale */ 229 snd_hda_codec_write_cache(codec, nid, 0, 230 AC_VERB_SET_DIGI_CONVERT_2, 0x01); 231 232 beep->nid = nid; 233 beep->codec = codec; 234 codec->beep = beep; 235 236 INIT_WORK(&beep->beep_work, &snd_hda_generate_beep); 237 238 input_dev = input_allocate_device(); 239 if (!input_dev) { 240 err = -ENOMEM; 241 goto err_free; 242 } 243 244 /* setup digital beep device */ 245 input_dev->name = "HDA Digital PCBeep"; 246 input_dev->phys = beep->phys; 247 input_dev->id.bustype = BUS_PCI; 248 input_dev->dev.parent = &codec->card->card_dev; 249 250 input_dev->id.vendor = codec->core.vendor_id >> 16; 251 input_dev->id.product = codec->core.vendor_id & 0xffff; 252 input_dev->id.version = 0x01; 253 254 input_dev->evbit[0] = BIT_MASK(EV_SND); 255 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); 256 input_dev->event = snd_hda_beep_event; 257 input_set_drvdata(input_dev, beep); 258 259 beep->dev = input_dev; 260 261 err = snd_device_new(codec->card, SNDRV_DEV_JACK, beep, &ops); 262 if (err < 0) 263 goto err_input; 264 265 return 0; 266 267 err_input: 268 input_free_device(beep->dev); 269 err_free: 270 kfree(beep); 271 codec->beep = NULL; 272 return err; 273 } 274 EXPORT_SYMBOL_GPL(snd_hda_attach_beep_device); 275 276 /** 277 * snd_hda_detach_beep_device - Detach the beep device 278 * @codec: the HDA codec 279 */ 280 void snd_hda_detach_beep_device(struct hda_codec *codec) 281 { 282 if (!codec->bus->shutdown && codec->beep) 283 snd_device_free(codec->card, codec->beep); 284 } 285 EXPORT_SYMBOL_GPL(snd_hda_detach_beep_device); 286 287 static bool ctl_has_mute(struct snd_kcontrol *kcontrol) 288 { 289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 290 return query_amp_caps(codec, get_amp_nid(kcontrol), 291 get_amp_direction(kcontrol)) & AC_AMPCAP_MUTE; 292 } 293 294 /* get/put callbacks for beep mute mixer switches */ 295 296 /** 297 * snd_hda_mixer_amp_switch_get_beep - Get callback for beep controls 298 * @kcontrol: ctl element 299 * @ucontrol: pointer to get/store the data 300 */ 301 int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol, 302 struct snd_ctl_elem_value *ucontrol) 303 { 304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 305 struct hda_beep *beep = codec->beep; 306 int chs = get_amp_channels(kcontrol); 307 308 if (beep && (!beep->enabled || !ctl_has_mute(kcontrol))) { 309 if (chs & 1) 310 ucontrol->value.integer.value[0] = beep->enabled; 311 if (chs & 2) 312 ucontrol->value.integer.value[1] = beep->enabled; 313 return 0; 314 } 315 return snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); 316 } 317 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get_beep); 318 319 /** 320 * snd_hda_mixer_amp_switch_put_beep - Put callback for beep controls 321 * @kcontrol: ctl element 322 * @ucontrol: pointer to get/store the data 323 */ 324 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol, 325 struct snd_ctl_elem_value *ucontrol) 326 { 327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 328 struct hda_beep *beep = codec->beep; 329 if (beep) { 330 u8 chs = get_amp_channels(kcontrol); 331 int enable = 0; 332 long *valp = ucontrol->value.integer.value; 333 if (chs & 1) { 334 enable |= *valp; 335 valp++; 336 } 337 if (chs & 2) 338 enable |= *valp; 339 snd_hda_enable_beep_device(codec, enable); 340 } 341 if (!ctl_has_mute(kcontrol)) 342 return 0; 343 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); 344 } 345 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put_beep); 346