hda_beep.c 8.7 KB

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