hda_beep.c 8.2 KB

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