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