hda_beep.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #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. /* fallthru */
  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_off_beep(struct hda_beep *beep)
  110. {
  111. cancel_work_sync(&beep->beep_work);
  112. if (beep->playing) {
  113. /* turn off beep */
  114. generate_tone(beep, 0);
  115. }
  116. }
  117. static void snd_hda_do_detach(struct hda_beep *beep)
  118. {
  119. if (beep->registered)
  120. input_unregister_device(beep->dev);
  121. else
  122. input_free_device(beep->dev);
  123. beep->dev = NULL;
  124. turn_off_beep(beep);
  125. }
  126. static int snd_hda_do_attach(struct hda_beep *beep)
  127. {
  128. struct input_dev *input_dev;
  129. struct hda_codec *codec = beep->codec;
  130. input_dev = input_allocate_device();
  131. if (!input_dev)
  132. return -ENOMEM;
  133. /* setup digital beep device */
  134. input_dev->name = "HDA Digital PCBeep";
  135. input_dev->phys = beep->phys;
  136. input_dev->id.bustype = BUS_PCI;
  137. input_dev->dev.parent = &codec->card->card_dev;
  138. input_dev->id.vendor = codec->core.vendor_id >> 16;
  139. input_dev->id.product = codec->core.vendor_id & 0xffff;
  140. input_dev->id.version = 0x01;
  141. input_dev->evbit[0] = BIT_MASK(EV_SND);
  142. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  143. input_dev->event = snd_hda_beep_event;
  144. input_set_drvdata(input_dev, beep);
  145. beep->dev = input_dev;
  146. return 0;
  147. }
  148. /**
  149. * snd_hda_enable_beep_device - Turn on/off beep sound
  150. * @codec: the HDA codec
  151. * @enable: flag to turn on/off
  152. */
  153. int snd_hda_enable_beep_device(struct hda_codec *codec, int enable)
  154. {
  155. struct hda_beep *beep = codec->beep;
  156. if (!beep)
  157. return 0;
  158. enable = !!enable;
  159. if (beep->enabled != enable) {
  160. beep->enabled = enable;
  161. if (!enable)
  162. turn_off_beep(beep);
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. EXPORT_SYMBOL_GPL(snd_hda_enable_beep_device);
  168. /**
  169. * snd_hda_attach_beep_device - Attach a beep input device
  170. * @codec: the HDA codec
  171. * @nid: beep NID
  172. *
  173. * Attach a beep object to the given widget. If beep hint is turned off
  174. * explicitly or beep_mode of the codec is turned off, this doesn't nothing.
  175. *
  176. * The attached beep device has to be registered via
  177. * snd_hda_register_beep_device() and released via snd_hda_detach_beep_device()
  178. * appropriately.
  179. *
  180. * Currently, only one beep device is allowed to each codec.
  181. */
  182. int snd_hda_attach_beep_device(struct hda_codec *codec, int nid)
  183. {
  184. struct hda_beep *beep;
  185. int err;
  186. if (!snd_hda_get_bool_hint(codec, "beep"))
  187. return 0; /* disabled explicitly by hints */
  188. if (codec->beep_mode == HDA_BEEP_MODE_OFF)
  189. return 0; /* disabled by module option */
  190. beep = kzalloc(sizeof(*beep), GFP_KERNEL);
  191. if (beep == NULL)
  192. return -ENOMEM;
  193. snprintf(beep->phys, sizeof(beep->phys),
  194. "card%d/codec#%d/beep0", codec->card->number, codec->addr);
  195. /* enable linear scale */
  196. snd_hda_codec_write_cache(codec, nid, 0,
  197. AC_VERB_SET_DIGI_CONVERT_2, 0x01);
  198. beep->nid = nid;
  199. beep->codec = codec;
  200. codec->beep = beep;
  201. INIT_WORK(&beep->beep_work, &snd_hda_generate_beep);
  202. mutex_init(&beep->mutex);
  203. err = snd_hda_do_attach(beep);
  204. if (err < 0) {
  205. kfree(beep);
  206. codec->beep = NULL;
  207. return err;
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(snd_hda_attach_beep_device);
  212. /**
  213. * snd_hda_detach_beep_device - Detach the beep device
  214. * @codec: the HDA codec
  215. */
  216. void snd_hda_detach_beep_device(struct hda_codec *codec)
  217. {
  218. struct hda_beep *beep = codec->beep;
  219. if (beep) {
  220. if (beep->dev)
  221. snd_hda_do_detach(beep);
  222. codec->beep = NULL;
  223. kfree(beep);
  224. }
  225. }
  226. EXPORT_SYMBOL_GPL(snd_hda_detach_beep_device);
  227. /**
  228. * snd_hda_register_beep_device - Register the beep device
  229. * @codec: the HDA codec
  230. */
  231. int snd_hda_register_beep_device(struct hda_codec *codec)
  232. {
  233. struct hda_beep *beep = codec->beep;
  234. int err;
  235. if (!beep || !beep->dev)
  236. return 0;
  237. err = input_register_device(beep->dev);
  238. if (err < 0) {
  239. codec_err(codec, "hda_beep: unable to register input device\n");
  240. input_free_device(beep->dev);
  241. codec->beep = NULL;
  242. kfree(beep);
  243. return err;
  244. }
  245. beep->registered = true;
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(snd_hda_register_beep_device);
  249. static bool ctl_has_mute(struct snd_kcontrol *kcontrol)
  250. {
  251. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  252. return query_amp_caps(codec, get_amp_nid(kcontrol),
  253. get_amp_direction(kcontrol)) & AC_AMPCAP_MUTE;
  254. }
  255. /* get/put callbacks for beep mute mixer switches */
  256. /**
  257. * snd_hda_mixer_amp_switch_get_beep - Get callback for beep controls
  258. * @kcontrol: ctl element
  259. * @ucontrol: pointer to get/store the data
  260. */
  261. int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol,
  262. struct snd_ctl_elem_value *ucontrol)
  263. {
  264. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  265. struct hda_beep *beep = codec->beep;
  266. int chs = get_amp_channels(kcontrol);
  267. if (beep && (!beep->enabled || !ctl_has_mute(kcontrol))) {
  268. if (chs & 1)
  269. ucontrol->value.integer.value[0] = beep->enabled;
  270. if (chs & 2)
  271. ucontrol->value.integer.value[1] = beep->enabled;
  272. return 0;
  273. }
  274. return snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  275. }
  276. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get_beep);
  277. /**
  278. * snd_hda_mixer_amp_switch_put_beep - Put callback for beep controls
  279. * @kcontrol: ctl element
  280. * @ucontrol: pointer to get/store the data
  281. */
  282. int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
  283. struct snd_ctl_elem_value *ucontrol)
  284. {
  285. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  286. struct hda_beep *beep = codec->beep;
  287. if (beep) {
  288. u8 chs = get_amp_channels(kcontrol);
  289. int enable = 0;
  290. long *valp = ucontrol->value.integer.value;
  291. if (chs & 1) {
  292. enable |= *valp;
  293. valp++;
  294. }
  295. if (chs & 2)
  296. enable |= *valp;
  297. snd_hda_enable_beep_device(codec, enable);
  298. }
  299. if (!ctl_has_mute(kcontrol))
  300. return 0;
  301. return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  302. }
  303. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put_beep);