xref: /openbmc/linux/sound/pci/hda/hda_beep.c (revision 7dd65feb)
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 
22 #include <linux/input.h>
23 #include <linux/pci.h>
24 #include <linux/workqueue.h>
25 #include <sound/core.h>
26 #include "hda_beep.h"
27 #include "hda_local.h"
28 
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 
35 static void snd_hda_generate_beep(struct work_struct *work)
36 {
37 	struct hda_beep *beep =
38 		container_of(work, struct hda_beep, beep_work);
39 	struct hda_codec *codec = beep->codec;
40 
41 	if (!beep->enabled)
42 		return;
43 
44 	/* generate tone */
45 	snd_hda_codec_write(codec, beep->nid, 0,
46 			AC_VERB_SET_BEEP_CONTROL, beep->tone);
47 }
48 
49 /* (non-standard) Linear beep tone calculation for IDT/STAC codecs
50  *
51  * The tone frequency of beep generator on IDT/STAC codecs is
52  * defined from the 8bit tone parameter, in Hz,
53  *    freq = 48000 * (257 - tone) / 1024
54  * that is from 12kHz to 93.75Hz in steps of 46.875 Hz
55  */
56 static int beep_linear_tone(struct hda_beep *beep, int hz)
57 {
58 	if (hz <= 0)
59 		return 0;
60 	hz *= 1000; /* fixed point */
61 	hz = hz - DIGBEEP_HZ_MIN
62 		+ DIGBEEP_HZ_STEP / 2; /* round to nearest step */
63 	if (hz < 0)
64 		hz = 0; /* turn off PC beep*/
65 	else if (hz >= (DIGBEEP_HZ_MAX - DIGBEEP_HZ_MIN))
66 		hz = 1; /* max frequency */
67 	else {
68 		hz /= DIGBEEP_HZ_STEP;
69 		hz = 255 - hz;
70 	}
71 	return hz;
72 }
73 
74 /* HD-audio standard beep tone parameter calculation
75  *
76  * The tone frequency in Hz is calculated as
77  *   freq = 48000 / (tone * 4)
78  * from 47Hz to 12kHz
79  */
80 static int beep_standard_tone(struct hda_beep *beep, int hz)
81 {
82 	if (hz <= 0)
83 		return 0; /* disabled */
84 	hz = 12000 / hz;
85 	if (hz > 0xff)
86 		return 0xff;
87 	if (hz <= 0)
88 		return 1;
89 	return hz;
90 }
91 
92 static int snd_hda_beep_event(struct input_dev *dev, unsigned int type,
93 				unsigned int code, int hz)
94 {
95 	struct hda_beep *beep = input_get_drvdata(dev);
96 
97 	switch (code) {
98 	case SND_BELL:
99 		if (hz)
100 			hz = 1000;
101 	case SND_TONE:
102 		if (beep->linear_tone)
103 			beep->tone = beep_linear_tone(beep, hz);
104 		else
105 			beep->tone = beep_standard_tone(beep, hz);
106 		break;
107 	default:
108 		return -1;
109 	}
110 
111 	/* schedule beep event */
112 	schedule_work(&beep->beep_work);
113 	return 0;
114 }
115 
116 static void snd_hda_do_detach(struct hda_beep *beep)
117 {
118 	input_unregister_device(beep->dev);
119 	beep->dev = NULL;
120 	cancel_work_sync(&beep->beep_work);
121 	/* turn off beep for sure */
122 	snd_hda_codec_write(beep->codec, beep->nid, 0,
123 				  AC_VERB_SET_BEEP_CONTROL, 0);
124 }
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 	int err;
131 
132 	input_dev = input_allocate_device();
133 	if (!input_dev) {
134 		printk(KERN_INFO "hda_beep: unable to allocate input device\n");
135 		return -ENOMEM;
136 	}
137 
138 	/* setup digital beep device */
139 	input_dev->name = "HDA Digital PCBeep";
140 	input_dev->phys = beep->phys;
141 	input_dev->id.bustype = BUS_PCI;
142 
143 	input_dev->id.vendor = codec->vendor_id >> 16;
144 	input_dev->id.product = codec->vendor_id & 0xffff;
145 	input_dev->id.version = 0x01;
146 
147 	input_dev->evbit[0] = BIT_MASK(EV_SND);
148 	input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
149 	input_dev->event = snd_hda_beep_event;
150 	input_dev->dev.parent = &codec->bus->pci->dev;
151 	input_set_drvdata(input_dev, beep);
152 
153 	err = input_register_device(input_dev);
154 	if (err < 0) {
155 		input_free_device(input_dev);
156 		printk(KERN_INFO "hda_beep: unable to register input device\n");
157 		return err;
158 	}
159 	beep->dev = input_dev;
160 	return 0;
161 }
162 
163 static void snd_hda_do_register(struct work_struct *work)
164 {
165 	struct hda_beep *beep =
166 		container_of(work, struct hda_beep, register_work);
167 
168 	mutex_lock(&beep->mutex);
169 	if (beep->enabled && !beep->dev)
170 		snd_hda_do_attach(beep);
171 	mutex_unlock(&beep->mutex);
172 }
173 
174 static void snd_hda_do_unregister(struct work_struct *work)
175 {
176 	struct hda_beep *beep =
177 		container_of(work, struct hda_beep, unregister_work.work);
178 
179 	mutex_lock(&beep->mutex);
180 	if (!beep->enabled && beep->dev)
181 		snd_hda_do_detach(beep);
182 	mutex_unlock(&beep->mutex);
183 }
184 
185 int snd_hda_enable_beep_device(struct hda_codec *codec, int enable)
186 {
187 	struct hda_beep *beep = codec->beep;
188 	enable = !!enable;
189 	if (beep == NULL)
190 		return 0;
191 	if (beep->enabled != enable) {
192 		beep->enabled = enable;
193 		if (!enable) {
194 			/* turn off beep */
195 			snd_hda_codec_write(beep->codec, beep->nid, 0,
196 						  AC_VERB_SET_BEEP_CONTROL, 0);
197 		}
198 		if (beep->mode == HDA_BEEP_MODE_SWREG) {
199 			if (enable) {
200 				cancel_delayed_work(&beep->unregister_work);
201 				schedule_work(&beep->register_work);
202 			} else {
203 				schedule_delayed_work(&beep->unregister_work,
204 									   HZ);
205 			}
206 		}
207 		return 1;
208 	}
209 	return 0;
210 }
211 EXPORT_SYMBOL_HDA(snd_hda_enable_beep_device);
212 
213 int snd_hda_attach_beep_device(struct hda_codec *codec, int nid)
214 {
215 	struct hda_beep *beep;
216 
217 	if (!snd_hda_get_bool_hint(codec, "beep"))
218 		return 0; /* disabled explicitly by hints */
219 	if (codec->beep_mode == HDA_BEEP_MODE_OFF)
220 		return 0; /* disabled by module option */
221 
222 	beep = kzalloc(sizeof(*beep), GFP_KERNEL);
223 	if (beep == NULL)
224 		return -ENOMEM;
225 	snprintf(beep->phys, sizeof(beep->phys),
226 		"card%d/codec#%d/beep0", codec->bus->card->number, codec->addr);
227 	/* enable linear scale */
228 	snd_hda_codec_write(codec, nid, 0,
229 		AC_VERB_SET_DIGI_CONVERT_2, 0x01);
230 
231 	beep->nid = nid;
232 	beep->codec = codec;
233 	beep->mode = codec->beep_mode;
234 	codec->beep = beep;
235 
236 	INIT_WORK(&beep->register_work, &snd_hda_do_register);
237 	INIT_DELAYED_WORK(&beep->unregister_work, &snd_hda_do_unregister);
238 	INIT_WORK(&beep->beep_work, &snd_hda_generate_beep);
239 	mutex_init(&beep->mutex);
240 
241 	if (beep->mode == HDA_BEEP_MODE_ON) {
242 		int err = snd_hda_do_attach(beep);
243 		if (err < 0) {
244 			kfree(beep);
245 			codec->beep = NULL;
246 			return err;
247 		}
248 	}
249 
250 	return 0;
251 }
252 EXPORT_SYMBOL_HDA(snd_hda_attach_beep_device);
253 
254 void snd_hda_detach_beep_device(struct hda_codec *codec)
255 {
256 	struct hda_beep *beep = codec->beep;
257 	if (beep) {
258 		cancel_work_sync(&beep->register_work);
259 		cancel_delayed_work(&beep->unregister_work);
260 		if (beep->dev)
261 			snd_hda_do_detach(beep);
262 		codec->beep = NULL;
263 		kfree(beep);
264 	}
265 }
266 EXPORT_SYMBOL_HDA(snd_hda_detach_beep_device);
267