1 // SPDX-License-Identifier: GPL-2.0+
3 * Digital Beep Input Interface for HD-audio codec
5 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
6 * Copyright (c) 2008 Embedded Alley Solutions Inc
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>
15 #include "hda_local.h"
18 DIGBEEP_HZ_STEP = 46875, /* 46.875 Hz */
19 DIGBEEP_HZ_MIN = 93750, /* 93.750 Hz */
20 DIGBEEP_HZ_MAX = 12000000, /* 12 KHz */
23 /* generate or stop tone */
24 static void generate_tone(struct hda_beep *beep, int tone)
26 struct hda_codec *codec = beep->codec;
28 if (tone && !beep->playing) {
29 snd_hda_power_up(codec);
31 beep->power_hook(beep, true);
34 snd_hda_codec_write(codec, beep->nid, 0,
35 AC_VERB_SET_BEEP_CONTROL, tone);
36 if (!tone && beep->playing) {
39 beep->power_hook(beep, false);
40 snd_hda_power_down(codec);
44 static void snd_hda_generate_beep(struct work_struct *work)
46 struct hda_beep *beep =
47 container_of(work, struct hda_beep, beep_work);
50 generate_tone(beep, beep->tone);
53 /* (non-standard) Linear beep tone calculation for IDT/STAC codecs
55 * The tone frequency of beep generator on IDT/STAC codecs is
56 * defined from the 8bit tone parameter, in Hz,
57 * freq = 48000 * (257 - tone) / 1024
58 * that is from 12kHz to 93.75Hz in steps of 46.875 Hz
60 static int beep_linear_tone(struct hda_beep *beep, int hz)
64 hz *= 1000; /* fixed point */
65 hz = hz - DIGBEEP_HZ_MIN
66 + DIGBEEP_HZ_STEP / 2; /* round to nearest step */
68 hz = 0; /* turn off PC beep*/
69 else if (hz >= (DIGBEEP_HZ_MAX - DIGBEEP_HZ_MIN))
70 hz = 1; /* max frequency */
72 hz /= DIGBEEP_HZ_STEP;
78 /* HD-audio standard beep tone parameter calculation
80 * The tone frequency in Hz is calculated as
81 * freq = 48000 / (tone * 4)
84 static int beep_standard_tone(struct hda_beep *beep, int hz)
87 return 0; /* disabled */
96 static int snd_hda_beep_event(struct input_dev *dev, unsigned int type,
97 unsigned int code, int hz)
99 struct hda_beep *beep = input_get_drvdata(dev);
107 if (beep->linear_tone)
108 beep->tone = beep_linear_tone(beep, hz);
110 beep->tone = beep_standard_tone(beep, hz);
116 /* schedule beep event */
117 schedule_work(&beep->beep_work);
121 static void turn_on_beep(struct hda_beep *beep)
123 if (beep->keep_power_at_enable)
124 snd_hda_power_up_pm(beep->codec);
127 static void turn_off_beep(struct hda_beep *beep)
129 cancel_work_sync(&beep->beep_work);
132 generate_tone(beep, 0);
134 if (beep->keep_power_at_enable)
135 snd_hda_power_down_pm(beep->codec);
139 * snd_hda_enable_beep_device - Turn on/off beep sound
140 * @codec: the HDA codec
141 * @enable: flag to turn on/off
143 int snd_hda_enable_beep_device(struct hda_codec *codec, int enable)
145 struct hda_beep *beep = codec->beep;
149 if (beep->enabled != enable) {
150 beep->enabled = enable;
159 EXPORT_SYMBOL_GPL(snd_hda_enable_beep_device);
161 static int beep_dev_register(struct snd_device *device)
163 struct hda_beep *beep = device->device_data;
166 err = input_register_device(beep->dev);
168 beep->registered = true;
172 static int beep_dev_disconnect(struct snd_device *device)
174 struct hda_beep *beep = device->device_data;
176 if (beep->registered)
177 input_unregister_device(beep->dev);
179 input_free_device(beep->dev);
185 static int beep_dev_free(struct snd_device *device)
187 struct hda_beep *beep = device->device_data;
189 beep->codec->beep = NULL;
195 * snd_hda_attach_beep_device - Attach a beep input device
196 * @codec: the HDA codec
199 * Attach a beep object to the given widget. If beep hint is turned off
200 * explicitly or beep_mode of the codec is turned off, this doesn't nothing.
202 * Currently, only one beep device is allowed to each codec.
204 int snd_hda_attach_beep_device(struct hda_codec *codec, int nid)
206 static const struct snd_device_ops ops = {
207 .dev_register = beep_dev_register,
208 .dev_disconnect = beep_dev_disconnect,
209 .dev_free = beep_dev_free,
211 struct input_dev *input_dev;
212 struct hda_beep *beep;
215 if (!snd_hda_get_bool_hint(codec, "beep"))
216 return 0; /* disabled explicitly by hints */
217 if (codec->beep_mode == HDA_BEEP_MODE_OFF)
218 return 0; /* disabled by module option */
220 beep = kzalloc(sizeof(*beep), GFP_KERNEL);
223 snprintf(beep->phys, sizeof(beep->phys),
224 "card%d/codec#%d/beep0", codec->card->number, codec->addr);
225 /* enable linear scale */
226 snd_hda_codec_write_cache(codec, nid, 0,
227 AC_VERB_SET_DIGI_CONVERT_2, 0x01);
233 INIT_WORK(&beep->beep_work, &snd_hda_generate_beep);
234 mutex_init(&beep->mutex);
236 input_dev = input_allocate_device();
242 /* setup digital beep device */
243 input_dev->name = "HDA Digital PCBeep";
244 input_dev->phys = beep->phys;
245 input_dev->id.bustype = BUS_PCI;
246 input_dev->dev.parent = &codec->card->card_dev;
248 input_dev->id.vendor = codec->core.vendor_id >> 16;
249 input_dev->id.product = codec->core.vendor_id & 0xffff;
250 input_dev->id.version = 0x01;
252 input_dev->evbit[0] = BIT_MASK(EV_SND);
253 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
254 input_dev->event = snd_hda_beep_event;
255 input_set_drvdata(input_dev, beep);
257 beep->dev = input_dev;
259 err = snd_device_new(codec->card, SNDRV_DEV_JACK, beep, &ops);
266 input_free_device(beep->dev);
272 EXPORT_SYMBOL_GPL(snd_hda_attach_beep_device);
275 * snd_hda_detach_beep_device - Detach the beep device
276 * @codec: the HDA codec
278 void snd_hda_detach_beep_device(struct hda_codec *codec)
280 if (!codec->bus->shutdown && codec->beep)
281 snd_device_free(codec->card, codec->beep);
283 EXPORT_SYMBOL_GPL(snd_hda_detach_beep_device);
285 static bool ctl_has_mute(struct snd_kcontrol *kcontrol)
287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
288 return query_amp_caps(codec, get_amp_nid(kcontrol),
289 get_amp_direction(kcontrol)) & AC_AMPCAP_MUTE;
292 /* get/put callbacks for beep mute mixer switches */
295 * snd_hda_mixer_amp_switch_get_beep - Get callback for beep controls
296 * @kcontrol: ctl element
297 * @ucontrol: pointer to get/store the data
299 int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol,
300 struct snd_ctl_elem_value *ucontrol)
302 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
303 struct hda_beep *beep = codec->beep;
304 int chs = get_amp_channels(kcontrol);
306 if (beep && (!beep->enabled || !ctl_has_mute(kcontrol))) {
308 ucontrol->value.integer.value[0] = beep->enabled;
310 ucontrol->value.integer.value[1] = beep->enabled;
313 return snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
315 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get_beep);
318 * snd_hda_mixer_amp_switch_put_beep - Put callback for beep controls
319 * @kcontrol: ctl element
320 * @ucontrol: pointer to get/store the data
322 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
323 struct snd_ctl_elem_value *ucontrol)
325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
326 struct hda_beep *beep = codec->beep;
328 u8 chs = get_amp_channels(kcontrol);
330 long *valp = ucontrol->value.integer.value;
337 snd_hda_enable_beep_device(codec, enable);
339 if (!ctl_has_mute(kcontrol))
341 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
343 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put_beep);