| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Beep using pcm | 
 | 3 |  * | 
 | 4 |  * Copyright (c) by Takashi Iwai <tiwai@suse.de> | 
 | 5 |  * | 
 | 6 |  *   This program is free software; you can redistribute it and/or modify | 
 | 7 |  *   it under the terms of the GNU General Public License as published by | 
 | 8 |  *   the Free Software Foundation; either version 2 of the License, or | 
 | 9 |  *   (at your option) any later version. | 
 | 10 |  * | 
 | 11 |  *   This program is distributed in the hope that it will be useful, | 
 | 12 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 13 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 14 |  *   GNU General Public License for more details. | 
 | 15 |  * | 
 | 16 |  *   You should have received a copy of the GNU General Public License | 
 | 17 |  *   along with this program; if not, write to the Free Software | 
 | 18 |  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA | 
 | 19 |  */ | 
 | 20 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/io.h> | 
 | 22 | #include <asm/irq.h> | 
 | 23 | #include <linux/init.h> | 
 | 24 | #include <linux/slab.h> | 
 | 25 | #include <linux/input.h> | 
| Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 26 | #include <linux/pci.h> | 
 | 27 | #include <linux/dma-mapping.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <sound/core.h> | 
 | 29 | #include <sound/control.h> | 
 | 30 | #include "pmac.h" | 
 | 31 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 32 | struct pmac_beep { | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 33 | 	int running;		/* boolean */ | 
 | 34 | 	int volume;		/* mixer volume: 0-100 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | 	int volume_play;	/* currently playing volume */ | 
 | 36 | 	int hz; | 
 | 37 | 	int nsamples; | 
 | 38 | 	short *buf;		/* allocated wave buffer */ | 
| Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 39 | 	dma_addr_t addr;	/* physical address of buffer */ | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 40 | 	struct input_dev *dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | }; | 
 | 42 |  | 
 | 43 | /* | 
 | 44 |  * stop beep if running | 
 | 45 |  */ | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 46 | void snd_pmac_beep_stop(struct snd_pmac *chip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 48 | 	struct pmac_beep *beep = chip->beep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 	if (beep && beep->running) { | 
 | 50 | 		beep->running = 0; | 
 | 51 | 		snd_pmac_beep_dma_stop(chip); | 
 | 52 | 	} | 
 | 53 | } | 
 | 54 |  | 
 | 55 | /* | 
 | 56 |  * Stuff for outputting a beep.  The values range from -327 to +327 | 
 | 57 |  * so we can multiply by an amplitude in the range 0..100 to get a | 
 | 58 |  * signed short value to put in the output buffer. | 
 | 59 |  */ | 
 | 60 | static short beep_wform[256] = { | 
 | 61 | 	0,	40,	79,	117,	153,	187,	218,	245, | 
 | 62 | 	269,	288,	304,	316,	323,	327,	327,	324, | 
 | 63 | 	318,	310,	299,	288,	275,	262,	249,	236, | 
 | 64 | 	224,	213,	204,	196,	190,	186,	183,	182, | 
 | 65 | 	182,	183,	186,	189,	192,	196,	200,	203, | 
 | 66 | 	206,	208,	209,	209,	209,	207,	204,	201, | 
 | 67 | 	197,	193,	188,	183,	179,	174,	170,	166, | 
 | 68 | 	163,	161,	160,	159,	159,	160,	161,	162, | 
 | 69 | 	164,	166,	168,	169,	171,	171,	171,	170, | 
 | 70 | 	169,	167,	163,	159,	155,	150,	144,	139, | 
 | 71 | 	133,	128,	122,	117,	113,	110,	107,	105, | 
 | 72 | 	103,	103,	103,	103,	104,	104,	105,	105, | 
 | 73 | 	105,	103,	101,	97,	92,	86,	78,	68, | 
 | 74 | 	58,	45,	32,	18,	3,	-11,	-26,	-41, | 
 | 75 | 	-55,	-68,	-79,	-88,	-95,	-100,	-102,	-102, | 
 | 76 | 	-99,	-93,	-85,	-75,	-62,	-48,	-33,	-16, | 
 | 77 | 	0,	16,	33,	48,	62,	75,	85,	93, | 
 | 78 | 	99,	102,	102,	100,	95,	88,	79,	68, | 
 | 79 | 	55,	41,	26,	11,	-3,	-18,	-32,	-45, | 
 | 80 | 	-58,	-68,	-78,	-86,	-92,	-97,	-101,	-103, | 
 | 81 | 	-105,	-105,	-105,	-104,	-104,	-103,	-103,	-103, | 
 | 82 | 	-103,	-105,	-107,	-110,	-113,	-117,	-122,	-128, | 
 | 83 | 	-133,	-139,	-144,	-150,	-155,	-159,	-163,	-167, | 
 | 84 | 	-169,	-170,	-171,	-171,	-171,	-169,	-168,	-166, | 
 | 85 | 	-164,	-162,	-161,	-160,	-159,	-159,	-160,	-161, | 
 | 86 | 	-163,	-166,	-170,	-174,	-179,	-183,	-188,	-193, | 
 | 87 | 	-197,	-201,	-204,	-207,	-209,	-209,	-209,	-208, | 
 | 88 | 	-206,	-203,	-200,	-196,	-192,	-189,	-186,	-183, | 
 | 89 | 	-182,	-182,	-183,	-186,	-190,	-196,	-204,	-213, | 
 | 90 | 	-224,	-236,	-249,	-262,	-275,	-288,	-299,	-310, | 
 | 91 | 	-318,	-324,	-327,	-327,	-323,	-316,	-304,	-288, | 
 | 92 | 	-269,	-245,	-218,	-187,	-153,	-117,	-79,	-40, | 
 | 93 | }; | 
 | 94 |  | 
 | 95 | #define BEEP_SRATE	22050	/* 22050 Hz sample rate */ | 
 | 96 | #define BEEP_BUFLEN	512 | 
 | 97 | #define BEEP_VOLUME	15	/* 0 - 100 */ | 
 | 98 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 99 | static int snd_pmac_beep_event(struct input_dev *dev, unsigned int type, | 
 | 100 | 			       unsigned int code, int hz) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 102 | 	struct snd_pmac *chip; | 
 | 103 | 	struct pmac_beep *beep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | 	unsigned long flags; | 
 | 105 | 	int beep_speed = 0; | 
 | 106 | 	int srate; | 
 | 107 | 	int period, ncycles, nsamples; | 
 | 108 | 	int i, j, f; | 
 | 109 | 	short *p; | 
 | 110 |  | 
 | 111 | 	if (type != EV_SND) | 
 | 112 | 		return -1; | 
 | 113 |  | 
 | 114 | 	switch (code) { | 
 | 115 | 	case SND_BELL: if (hz) hz = 1000; | 
 | 116 | 	case SND_TONE: break; | 
 | 117 | 	default: return -1; | 
 | 118 | 	} | 
 | 119 |  | 
| Dmitry Torokhov | 1e2831d | 2007-06-14 23:58:51 -0400 | [diff] [blame] | 120 | 	chip = input_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 	if (! chip || (beep = chip->beep) == NULL) | 
 | 122 | 		return -1; | 
 | 123 |  | 
 | 124 | 	if (! hz) { | 
 | 125 | 		spin_lock_irqsave(&chip->reg_lock, flags); | 
 | 126 | 		if (beep->running) | 
 | 127 | 			snd_pmac_beep_stop(chip); | 
 | 128 | 		spin_unlock_irqrestore(&chip->reg_lock, flags); | 
 | 129 | 		return 0; | 
 | 130 | 	} | 
 | 131 |  | 
 | 132 | 	beep_speed = snd_pmac_rate_index(chip, &chip->playback, BEEP_SRATE); | 
 | 133 | 	srate = chip->freq_table[beep_speed]; | 
 | 134 |  | 
 | 135 | 	if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) | 
 | 136 | 		hz = 1000; | 
 | 137 |  | 
 | 138 | 	spin_lock_irqsave(&chip->reg_lock, flags); | 
 | 139 | 	if (chip->playback.running || chip->capture.running || beep->running) { | 
 | 140 | 		spin_unlock_irqrestore(&chip->reg_lock, flags); | 
 | 141 | 		return 0; | 
 | 142 | 	} | 
 | 143 | 	beep->running = 1; | 
 | 144 | 	spin_unlock_irqrestore(&chip->reg_lock, flags); | 
 | 145 |  | 
 | 146 | 	if (hz == beep->hz && beep->volume == beep->volume_play) { | 
 | 147 | 		nsamples = beep->nsamples; | 
 | 148 | 	} else { | 
 | 149 | 		period = srate * 256 / hz;	/* fixed point */ | 
 | 150 | 		ncycles = BEEP_BUFLEN * 256 / period; | 
 | 151 | 		nsamples = (period * ncycles) >> 8; | 
 | 152 | 		f = ncycles * 65536 / nsamples; | 
 | 153 | 		j = 0; | 
 | 154 | 		p = beep->buf; | 
 | 155 | 		for (i = 0; i < nsamples; ++i, p += 2) { | 
 | 156 | 			p[0] = p[1] = beep_wform[j >> 8] * beep->volume; | 
 | 157 | 			j = (j + f) & 0xffff; | 
 | 158 | 		} | 
 | 159 | 		beep->hz = hz; | 
 | 160 | 		beep->volume_play = beep->volume; | 
 | 161 | 		beep->nsamples = nsamples; | 
 | 162 | 	} | 
 | 163 |  | 
 | 164 | 	spin_lock_irqsave(&chip->reg_lock, flags); | 
 | 165 | 	snd_pmac_beep_dma_start(chip, beep->nsamples * 4, beep->addr, beep_speed); | 
 | 166 | 	spin_unlock_irqrestore(&chip->reg_lock, flags); | 
 | 167 | 	return 0; | 
 | 168 | } | 
 | 169 |  | 
 | 170 | /* | 
 | 171 |  * beep volume mixer | 
 | 172 |  */ | 
 | 173 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 174 | static int snd_pmac_info_beep(struct snd_kcontrol *kcontrol, | 
 | 175 | 			      struct snd_ctl_elem_info *uinfo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { | 
 | 177 | 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | 
 | 178 | 	uinfo->count = 1; | 
 | 179 | 	uinfo->value.integer.min = 0; | 
 | 180 | 	uinfo->value.integer.max = 100; | 
 | 181 | 	return 0; | 
 | 182 | } | 
 | 183 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 184 | static int snd_pmac_get_beep(struct snd_kcontrol *kcontrol, | 
 | 185 | 			     struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 187 | 	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); | 
| Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 188 | 	if (snd_BUG_ON(!chip->beep)) | 
 | 189 | 		return -ENXIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | 	ucontrol->value.integer.value[0] = chip->beep->volume; | 
 | 191 | 	return 0; | 
 | 192 | } | 
 | 193 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 194 | static int snd_pmac_put_beep(struct snd_kcontrol *kcontrol, | 
 | 195 | 			     struct snd_ctl_elem_value *ucontrol) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 197 | 	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); | 
| Takashi Iwai | d4079ac | 2007-11-15 16:14:12 +0100 | [diff] [blame] | 198 | 	unsigned int oval, nval; | 
| Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 199 | 	if (snd_BUG_ON(!chip->beep)) | 
 | 200 | 		return -ENXIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | 	oval = chip->beep->volume; | 
| Takashi Iwai | d4079ac | 2007-11-15 16:14:12 +0100 | [diff] [blame] | 202 | 	nval = ucontrol->value.integer.value[0]; | 
 | 203 | 	if (nval > 100) | 
 | 204 | 		return -EINVAL; | 
 | 205 | 	chip->beep->volume = nval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | 	return oval != chip->beep->volume; | 
 | 207 | } | 
 | 208 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 209 | static struct snd_kcontrol_new snd_pmac_beep_mixer = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, | 
 | 211 | 	.name = "Beep Playback Volume", | 
 | 212 | 	.info = snd_pmac_info_beep, | 
 | 213 | 	.get = snd_pmac_get_beep, | 
 | 214 | 	.put = snd_pmac_put_beep, | 
 | 215 | }; | 
 | 216 |  | 
 | 217 | /* Initialize beep stuff */ | 
| Stephen Rothwell | 5c9b6e9 | 2009-06-03 15:35:19 +1000 | [diff] [blame] | 218 | int __devinit snd_pmac_attach_beep(struct snd_pmac *chip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 220 | 	struct pmac_beep *beep; | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 221 | 	struct input_dev *input_dev; | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 222 | 	struct snd_kcontrol *beep_ctl; | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 223 | 	void *dmabuf; | 
 | 224 | 	int err = -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 |  | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 226 | 	beep = kzalloc(sizeof(*beep), GFP_KERNEL); | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 227 | 	if (! beep) | 
 | 228 | 		return -ENOMEM; | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 229 | 	dmabuf = dma_alloc_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4, | 
 | 230 | 				    &beep->addr, GFP_KERNEL); | 
 | 231 | 	input_dev = input_allocate_device(); | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 232 | 	if (! dmabuf || ! input_dev) | 
 | 233 | 		goto fail1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 |  | 
 | 235 | 	/* FIXME: set more better values */ | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 236 | 	input_dev->name = "PowerMac Beep"; | 
 | 237 | 	input_dev->phys = "powermac/beep"; | 
 | 238 | 	input_dev->id.bustype = BUS_ADB; | 
 | 239 | 	input_dev->id.vendor = 0x001f; | 
 | 240 | 	input_dev->id.product = 0x0001; | 
 | 241 | 	input_dev->id.version = 0x0100; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 |  | 
| Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 243 | 	input_dev->evbit[0] = BIT_MASK(EV_SND); | 
 | 244 | 	input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 245 | 	input_dev->event = snd_pmac_beep_event; | 
| Dmitry Torokhov | 1e2831d | 2007-06-14 23:58:51 -0400 | [diff] [blame] | 246 | 	input_dev->dev.parent = &chip->pdev->dev; | 
 | 247 | 	input_set_drvdata(input_dev, chip); | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 248 |  | 
 | 249 | 	beep->dev = input_dev; | 
 | 250 | 	beep->buf = dmabuf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | 	beep->volume = BEEP_VOLUME; | 
 | 252 | 	beep->running = 0; | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 253 |  | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 254 | 	beep_ctl = snd_ctl_new1(&snd_pmac_beep_mixer, chip); | 
 | 255 | 	err = snd_ctl_add(chip->card, beep_ctl); | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 256 | 	if (err < 0) | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 257 | 		goto fail1; | 
| Dmitry Torokhov | 1e2831d | 2007-06-14 23:58:51 -0400 | [diff] [blame] | 258 |  | 
 | 259 | 	chip->beep = beep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 |  | 
| Dmitry Torokhov | f03d68f | 2006-08-03 15:06:14 +0200 | [diff] [blame] | 261 | 	err = input_register_device(beep->dev); | 
 | 262 | 	if (err) | 
 | 263 | 		goto fail2; | 
 | 264 |   | 
 | 265 |  	return 0; | 
 | 266 |   | 
 | 267 |  fail2:	snd_ctl_remove(chip->card, beep_ctl); | 
 | 268 |  fail1:	input_free_device(input_dev); | 
 | 269 | 	if (dmabuf) | 
 | 270 | 		dma_free_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4, | 
 | 271 | 				  dmabuf, beep->addr); | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 272 | 	kfree(beep); | 
 | 273 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } | 
 | 275 |  | 
| Takashi Iwai | 65b29f5 | 2005-11-17 15:09:46 +0100 | [diff] [blame] | 276 | void snd_pmac_detach_beep(struct snd_pmac *chip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { | 
 | 278 | 	if (chip->beep) { | 
| Dmitry Torokhov | 5ebdcbc | 2005-09-15 02:01:49 -0500 | [diff] [blame] | 279 | 		input_unregister_device(chip->beep->dev); | 
| Benjamin Herrenschmidt | 7bbd827 | 2005-04-16 15:24:32 -0700 | [diff] [blame] | 280 | 		dma_free_coherent(&chip->pdev->dev, BEEP_BUFLEN * 4, | 
 | 281 | 				  chip->beep->buf, chip->beep->addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 		kfree(chip->beep); | 
 | 283 | 		chip->beep = NULL; | 
 | 284 | 	} | 
 | 285 | } |