hda_beep.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Digital Beep Input Interface for HD-audio codec
  3. *
  4. * Author: Matthew Ranostay <mranostay@embeddedalley.com>
  5. * Copyright (c) 2008 Embedded Alley Solutions Inc
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/input.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/export.h>
  26. #include <sound/core.h>
  27. #include "hda_beep.h"
  28. #include "hda_local.h"
  29. enum {
  30. DIGBEEP_HZ_STEP = 46875, /* 46.875 Hz */
  31. DIGBEEP_HZ_MIN = 93750, /* 93.750 Hz */
  32. DIGBEEP_HZ_MAX = 12000000, /* 12 KHz */
  33. };
  34. static void snd_hda_generate_beep(struct work_struct *work)
  35. {
  36. struct hda_beep *beep =
  37. container_of(work, struct hda_beep, beep_work);
  38. struct hda_codec *codec = beep->codec;
  39. int tone;
  40. if (!beep->enabled)
  41. return;
  42. tone = beep->tone;
  43. if (tone && !beep->playing) {
  44. snd_hda_power_up(codec);
  45. beep->playing = 1;
  46. }
  47. /* generate tone */
  48. snd_hda_codec_write(codec, beep->nid, 0,
  49. AC_VERB_SET_BEEP_CONTROL, tone);
  50. if (!tone && beep->playing) {
  51. beep->playing = 0;
  52. snd_hda_power_down(codec);
  53. }
  54. }
  55. /* (non-standard) Linear beep tone calculation for IDT/STAC codecs
  56. *
  57. * The tone frequency of beep generator on IDT/STAC codecs is
  58. * defined from the 8bit tone parameter, in Hz,
  59. * freq = 48000 * (257 - tone) / 1024
  60. * that is from 12kHz to 93.75Hz in steps of 46.875 Hz
  61. */
  62. static int beep_linear_tone(struct hda_beep *beep, int hz)
  63. {
  64. if (hz <= 0)
  65. return 0;
  66. hz *= 1000; /* fixed point */
  67. hz = hz - DIGBEEP_HZ_MIN
  68. + DIGBEEP_HZ_STEP / 2; /* round to nearest step */
  69. if (hz < 0)
  70. hz = 0; /* turn off PC beep*/
  71. else if (hz >= (DIGBEEP_HZ_MAX - DIGBEEP_HZ_MIN))
  72. hz = 1; /* max frequency */
  73. else {
  74. hz /= DIGBEEP_HZ_STEP;
  75. hz = 255 - hz;
  76. }
  77. return hz;
  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. 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. switch (code) {
  101. case SND_BELL:
  102. if (hz)
  103. hz = 1000;
  104. /* fallthru */
  105. case SND_TONE:
  106. if (beep->linear_tone)
  107. beep->tone = beep_linear_tone(beep, hz);
  108. else
  109. beep->tone = beep_standard_tone(beep, hz);
  110. break;
  111. default:
  112. return -1;
  113. }
  114. /* schedule beep event */
  115. schedule_work(&beep->beep_work);
  116. return 0;
  117. }
  118. static void turn_off_beep(struct hda_beep *beep)
  119. {
  120. cancel_work_sync(&beep->beep_work);
  121. if (beep->playing) {
  122. /* turn off beep */
  123. snd_hda_codec_write(beep->codec, beep->nid, 0,
  124. AC_VERB_SET_BEEP_CONTROL, 0);
  125. beep->playing = 0;
  126. snd_hda_power_down(beep->codec);
  127. }
  128. }
  129. static void snd_hda_do_detach(struct hda_beep *beep)
  130. {
  131. input_unregister_device(beep->dev);
  132. beep->dev = NULL;
  133. turn_off_beep(beep);
  134. }
  135. static int snd_hda_do_attach(struct hda_beep *beep)
  136. {
  137. struct input_dev *input_dev;
  138. struct hda_codec *codec = beep->codec;
  139. int err;
  140. input_dev = input_allocate_device();
  141. if (!input_dev)
  142. return -ENOMEM;
  143. /* setup digital beep device */
  144. input_dev->name = "HDA Digital PCBeep";
  145. input_dev->phys = beep->phys;
  146. input_dev->id.bustype = BUS_PCI;
  147. input_dev->id.vendor = codec->vendor_id >> 16;
  148. input_dev->id.product = codec->vendor_id & 0xffff;
  149. input_dev->id.version = 0x01;
  150. input_dev->evbit[0] = BIT_MASK(EV_SND);
  151. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  152. input_dev->event = snd_hda_beep_event;
  153. input_dev->dev.parent = &codec->bus->pci->dev;
  154. input_set_drvdata(input_dev, beep);
  155. err = input_register_device(input_dev);
  156. if (err < 0) {
  157. input_free_device(input_dev);
  158. printk(KERN_INFO "hda_beep: unable to register input device\n");
  159. return err;
  160. }
  161. beep->dev = input_dev;
  162. return 0;
  163. }
  164. int snd_hda_enable_beep_device(struct hda_codec *codec, int enable)
  165. {
  166. struct hda_beep *beep = codec->beep;
  167. if (!beep)
  168. return 0;
  169. enable = !!enable;
  170. if (beep->enabled != enable) {
  171. beep->enabled = enable;
  172. if (!enable)
  173. turn_off_beep(beep);
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_HDA(snd_hda_enable_beep_device);
  179. int snd_hda_attach_beep_device(struct hda_codec *codec, int nid)
  180. {
  181. struct hda_beep *beep;
  182. int err;
  183. if (!snd_hda_get_bool_hint(codec, "beep"))
  184. return 0; /* disabled explicitly by hints */
  185. if (codec->beep_mode == HDA_BEEP_MODE_OFF)
  186. return 0; /* disabled by module option */
  187. beep = kzalloc(sizeof(*beep), GFP_KERNEL);
  188. if (beep == NULL)
  189. return -ENOMEM;
  190. snprintf(beep->phys, sizeof(beep->phys),
  191. "card%d/codec#%d/beep0", codec->bus->card->number, codec->addr);
  192. /* enable linear scale */
  193. snd_hda_codec_write_cache(codec, nid, 0,
  194. AC_VERB_SET_DIGI_CONVERT_2, 0x01);
  195. beep->nid = nid;
  196. beep->codec = codec;
  197. codec->beep = beep;
  198. INIT_WORK(&beep->beep_work, &snd_hda_generate_beep);
  199. mutex_init(&beep->mutex);
  200. err = snd_hda_do_attach(beep);
  201. if (err < 0) {
  202. kfree(beep);
  203. codec->beep = NULL;
  204. return err;
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_HDA(snd_hda_attach_beep_device);
  209. void snd_hda_detach_beep_device(struct hda_codec *codec)
  210. {
  211. struct hda_beep *beep = codec->beep;
  212. if (beep) {
  213. if (beep->dev)
  214. snd_hda_do_detach(beep);
  215. codec->beep = NULL;
  216. kfree(beep);
  217. }
  218. }
  219. EXPORT_SYMBOL_HDA(snd_hda_detach_beep_device);
  220. static bool ctl_has_mute(struct snd_kcontrol *kcontrol)
  221. {
  222. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  223. return query_amp_caps(codec, get_amp_nid(kcontrol),
  224. get_amp_direction(kcontrol)) & AC_AMPCAP_MUTE;
  225. }
  226. /* get/put callbacks for beep mute mixer switches */
  227. int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol,
  228. struct snd_ctl_elem_value *ucontrol)
  229. {
  230. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  231. struct hda_beep *beep = codec->beep;
  232. if (beep && (!beep->enabled || !ctl_has_mute(kcontrol))) {
  233. ucontrol->value.integer.value[0] =
  234. ucontrol->value.integer.value[1] = beep->enabled;
  235. return 0;
  236. }
  237. return snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  238. }
  239. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get_beep);
  240. int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol)
  242. {
  243. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  244. struct hda_beep *beep = codec->beep;
  245. if (beep) {
  246. u8 chs = get_amp_channels(kcontrol);
  247. int enable = 0;
  248. long *valp = ucontrol->value.integer.value;
  249. if (chs & 1) {
  250. enable |= *valp;
  251. valp++;
  252. }
  253. if (chs & 2)
  254. enable |= *valp;
  255. snd_hda_enable_beep_device(codec, enable);
  256. }
  257. if (!ctl_has_mute(kcontrol))
  258. return 0;
  259. return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  260. }
  261. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);