hda_beep.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. case SND_TONE:
  105. if (beep->linear_tone)
  106. beep->tone = beep_linear_tone(beep, hz);
  107. else
  108. beep->tone = beep_standard_tone(beep, hz);
  109. break;
  110. default:
  111. return -1;
  112. }
  113. /* schedule beep event */
  114. schedule_work(&beep->beep_work);
  115. return 0;
  116. }
  117. static void turn_off_beep(struct hda_beep *beep)
  118. {
  119. cancel_work_sync(&beep->beep_work);
  120. if (beep->playing) {
  121. /* turn off beep */
  122. snd_hda_codec_write(beep->codec, beep->nid, 0,
  123. AC_VERB_SET_BEEP_CONTROL, 0);
  124. beep->playing = 0;
  125. snd_hda_power_down(beep->codec);
  126. }
  127. }
  128. static void snd_hda_do_detach(struct hda_beep *beep)
  129. {
  130. input_unregister_device(beep->dev);
  131. beep->dev = NULL;
  132. turn_off_beep(beep);
  133. }
  134. static int snd_hda_do_attach(struct hda_beep *beep)
  135. {
  136. struct input_dev *input_dev;
  137. struct hda_codec *codec = beep->codec;
  138. int err;
  139. input_dev = input_allocate_device();
  140. if (!input_dev) {
  141. printk(KERN_INFO "hda_beep: unable to allocate input device\n");
  142. return -ENOMEM;
  143. }
  144. /* setup digital beep device */
  145. input_dev->name = "HDA Digital PCBeep";
  146. input_dev->phys = beep->phys;
  147. input_dev->id.bustype = BUS_PCI;
  148. input_dev->id.vendor = codec->vendor_id >> 16;
  149. input_dev->id.product = codec->vendor_id & 0xffff;
  150. input_dev->id.version = 0x01;
  151. input_dev->evbit[0] = BIT_MASK(EV_SND);
  152. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  153. input_dev->event = snd_hda_beep_event;
  154. input_dev->dev.parent = &codec->bus->pci->dev;
  155. input_set_drvdata(input_dev, beep);
  156. err = input_register_device(input_dev);
  157. if (err < 0) {
  158. input_free_device(input_dev);
  159. printk(KERN_INFO "hda_beep: unable to register input device\n");
  160. return err;
  161. }
  162. beep->dev = input_dev;
  163. return 0;
  164. }
  165. int snd_hda_enable_beep_device(struct hda_codec *codec, int enable)
  166. {
  167. struct hda_beep *beep = codec->beep;
  168. if (!beep)
  169. return 0;
  170. enable = !!enable;
  171. if (beep->enabled != enable) {
  172. beep->enabled = enable;
  173. if (!enable)
  174. turn_off_beep(beep);
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL_HDA(snd_hda_enable_beep_device);
  180. int snd_hda_attach_beep_device(struct hda_codec *codec, int nid)
  181. {
  182. struct hda_beep *beep;
  183. int err;
  184. if (!snd_hda_get_bool_hint(codec, "beep"))
  185. return 0; /* disabled explicitly by hints */
  186. if (codec->beep_mode == HDA_BEEP_MODE_OFF)
  187. return 0; /* disabled by module option */
  188. beep = kzalloc(sizeof(*beep), GFP_KERNEL);
  189. if (beep == NULL)
  190. return -ENOMEM;
  191. snprintf(beep->phys, sizeof(beep->phys),
  192. "card%d/codec#%d/beep0", codec->bus->card->number, codec->addr);
  193. /* enable linear scale */
  194. snd_hda_codec_write_cache(codec, nid, 0,
  195. AC_VERB_SET_DIGI_CONVERT_2, 0x01);
  196. beep->nid = nid;
  197. beep->codec = codec;
  198. codec->beep = beep;
  199. INIT_WORK(&beep->beep_work, &snd_hda_generate_beep);
  200. mutex_init(&beep->mutex);
  201. err = snd_hda_do_attach(beep);
  202. if (err < 0) {
  203. kfree(beep);
  204. codec->beep = NULL;
  205. return err;
  206. }
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_HDA(snd_hda_attach_beep_device);
  210. void snd_hda_detach_beep_device(struct hda_codec *codec)
  211. {
  212. struct hda_beep *beep = codec->beep;
  213. if (beep) {
  214. if (beep->dev)
  215. snd_hda_do_detach(beep);
  216. codec->beep = NULL;
  217. kfree(beep);
  218. }
  219. }
  220. EXPORT_SYMBOL_HDA(snd_hda_detach_beep_device);
  221. static bool ctl_has_mute(struct snd_kcontrol *kcontrol)
  222. {
  223. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  224. return query_amp_caps(codec, get_amp_nid(kcontrol),
  225. get_amp_direction(kcontrol)) & AC_AMPCAP_MUTE;
  226. }
  227. /* get/put callbacks for beep mute mixer switches */
  228. int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol,
  229. struct snd_ctl_elem_value *ucontrol)
  230. {
  231. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  232. struct hda_beep *beep = codec->beep;
  233. if (beep && (!beep->enabled || !ctl_has_mute(kcontrol))) {
  234. ucontrol->value.integer.value[0] =
  235. ucontrol->value.integer.value[1] = beep->enabled;
  236. return 0;
  237. }
  238. return snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  239. }
  240. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get_beep);
  241. int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_value *ucontrol)
  243. {
  244. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  245. struct hda_beep *beep = codec->beep;
  246. if (beep) {
  247. u8 chs = get_amp_channels(kcontrol);
  248. int enable = 0;
  249. long *valp = ucontrol->value.integer.value;
  250. if (chs & 1) {
  251. enable |= *valp;
  252. valp++;
  253. }
  254. if (chs & 2)
  255. enable |= *valp;
  256. snd_hda_enable_beep_device(codec, enable);
  257. }
  258. if (!ctl_has_mute(kcontrol))
  259. return 0;
  260. return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  261. }
  262. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);