| Andriy Skulysh | 7e27b9b | 2006-09-27 13:48:32 +0900 | [diff] [blame] | 1 | /* | 
 | 2 |  * sound/oss/sh_dac_audio.c | 
 | 3 |  * | 
 | 4 |  * SH DAC based sound :( | 
 | 5 |  * | 
 | 6 |  *  Copyright (C) 2004,2005  Andriy Skulysh | 
 | 7 |  * | 
 | 8 |  * This file is subject to the terms and conditions of the GNU General Public | 
 | 9 |  * License.  See the file "COPYING" in the main directory of this archive | 
 | 10 |  * for more details. | 
 | 11 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> | 
 | 13 | #include <linux/init.h> | 
 | 14 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/linkage.h> | 
 | 16 | #include <linux/slab.h> | 
 | 17 | #include <linux/fs.h> | 
 | 18 | #include <linux/sound.h> | 
| Arnd Bergmann | 90dc763 | 2010-07-11 12:16:36 +0200 | [diff] [blame] | 19 | #include <linux/smp_lock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/soundcard.h> | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 21 | #include <linux/interrupt.h> | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 22 | #include <linux/hrtimer.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/io.h> | 
 | 24 | #include <asm/uaccess.h> | 
 | 25 | #include <asm/irq.h> | 
 | 26 | #include <asm/delay.h> | 
| Andriy Skulysh | 7e27b9b | 2006-09-27 13:48:32 +0900 | [diff] [blame] | 27 | #include <asm/clock.h> | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 28 | #include <cpu/dac.h> | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 29 | #include <asm/machvec.h> | 
| Paul Mundt | 7639a45 | 2008-10-20 13:02:48 +0900 | [diff] [blame] | 30 | #include <mach/hp6xx.h> | 
| Andriy Skulysh | 7e27b9b | 2006-09-27 13:48:32 +0900 | [diff] [blame] | 31 | #include <asm/hd64461.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 |  | 
 | 33 | #define MODNAME "sh_dac_audio" | 
 | 34 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define BUFFER_SIZE 48000 | 
 | 36 |  | 
 | 37 | static int rate; | 
 | 38 | static int empty; | 
 | 39 | static char *data_buffer, *buffer_begin, *buffer_end; | 
 | 40 | static int in_use, device_major; | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 41 | static struct hrtimer hrtimer; | 
 | 42 | static ktime_t wakeups_per_second; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
 | 44 | static void dac_audio_start_timer(void) | 
 | 45 | { | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 46 | 	hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } | 
 | 48 |  | 
 | 49 | static void dac_audio_stop_timer(void) | 
 | 50 | { | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 51 | 	hrtimer_cancel(&hrtimer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } | 
 | 53 |  | 
 | 54 | static void dac_audio_reset(void) | 
 | 55 | { | 
 | 56 | 	dac_audio_stop_timer(); | 
 | 57 | 	buffer_begin = buffer_end = data_buffer; | 
 | 58 | 	empty = 1; | 
 | 59 | } | 
 | 60 |  | 
 | 61 | static void dac_audio_sync(void) | 
 | 62 | { | 
 | 63 | 	while (!empty) | 
 | 64 | 		schedule(); | 
 | 65 | } | 
 | 66 |  | 
 | 67 | static void dac_audio_start(void) | 
 | 68 | { | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 69 | 	if (mach_is_hp6xx()) { | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 70 | 		u16 v = __raw_readw(HD64461_GPADR); | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 71 | 		v &= ~HD64461_GPADR_SPEAKER; | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 72 | 		__raw_writew(v, HD64461_GPADR); | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 73 | 	} | 
 | 74 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 	sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } | 
 | 77 | static void dac_audio_stop(void) | 
 | 78 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | 	dac_audio_stop_timer(); | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 80 |  | 
 | 81 | 	if (mach_is_hp6xx()) { | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 82 | 		u16 v = __raw_readw(HD64461_GPADR); | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 83 | 		v |= HD64461_GPADR_SPEAKER; | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 84 | 		__raw_writew(v, HD64461_GPADR); | 
| Andriy Skulysh | 4bcac20 | 2006-09-27 13:07:38 +0900 | [diff] [blame] | 85 | 	} | 
 | 86 |  | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 87 | 	sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 	sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); | 
 | 89 | } | 
 | 90 |  | 
 | 91 | static void dac_audio_set_rate(void) | 
 | 92 | { | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 93 | 	wakeups_per_second = ktime_set(0, 1000000000 / rate); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } | 
 | 95 |  | 
| Arnd Bergmann | d209974 | 2010-07-12 19:53:18 +0200 | [diff] [blame] | 96 | static int dac_audio_ioctl(struct file *file, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 			   unsigned int cmd, unsigned long arg) | 
 | 98 | { | 
 | 99 | 	int val; | 
 | 100 |  | 
 | 101 | 	switch (cmd) { | 
 | 102 | 	case OSS_GETVERSION: | 
 | 103 | 		return put_user(SOUND_VERSION, (int *)arg); | 
 | 104 |  | 
 | 105 | 	case SNDCTL_DSP_SYNC: | 
 | 106 | 		dac_audio_sync(); | 
 | 107 | 		return 0; | 
 | 108 |  | 
 | 109 | 	case SNDCTL_DSP_RESET: | 
 | 110 | 		dac_audio_reset(); | 
 | 111 | 		return 0; | 
 | 112 |  | 
 | 113 | 	case SNDCTL_DSP_GETFMTS: | 
 | 114 | 		return put_user(AFMT_U8, (int *)arg); | 
 | 115 |  | 
 | 116 | 	case SNDCTL_DSP_SETFMT: | 
 | 117 | 		return put_user(AFMT_U8, (int *)arg); | 
 | 118 |  | 
 | 119 | 	case SNDCTL_DSP_NONBLOCK: | 
| Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 120 | 		spin_lock(&file->f_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 		file->f_flags |= O_NONBLOCK; | 
| Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 122 | 		spin_unlock(&file->f_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | 		return 0; | 
 | 124 |  | 
 | 125 | 	case SNDCTL_DSP_GETCAPS: | 
 | 126 | 		return 0; | 
 | 127 |  | 
 | 128 | 	case SOUND_PCM_WRITE_RATE: | 
 | 129 | 		val = *(int *)arg; | 
 | 130 | 		if (val > 0) { | 
 | 131 | 			rate = val; | 
 | 132 | 			dac_audio_set_rate(); | 
 | 133 | 		} | 
 | 134 | 		return put_user(rate, (int *)arg); | 
 | 135 |  | 
 | 136 | 	case SNDCTL_DSP_STEREO: | 
 | 137 | 		return put_user(0, (int *)arg); | 
 | 138 |  | 
 | 139 | 	case SOUND_PCM_WRITE_CHANNELS: | 
 | 140 | 		return put_user(1, (int *)arg); | 
 | 141 |  | 
 | 142 | 	case SNDCTL_DSP_SETDUPLEX: | 
 | 143 | 		return -EINVAL; | 
 | 144 |  | 
 | 145 | 	case SNDCTL_DSP_PROFILE: | 
 | 146 | 		return -EINVAL; | 
 | 147 |  | 
 | 148 | 	case SNDCTL_DSP_GETBLKSIZE: | 
 | 149 | 		return put_user(BUFFER_SIZE, (int *)arg); | 
 | 150 |  | 
 | 151 | 	case SNDCTL_DSP_SETFRAGMENT: | 
 | 152 | 		return 0; | 
 | 153 |  | 
 | 154 | 	default: | 
 | 155 | 		printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n", | 
 | 156 | 		       cmd); | 
 | 157 | 		return -EINVAL; | 
 | 158 | 	} | 
 | 159 | 	return -EINVAL; | 
 | 160 | } | 
 | 161 |  | 
| Arnd Bergmann | d209974 | 2010-07-12 19:53:18 +0200 | [diff] [blame] | 162 | static long dac_audio_unlocked_ioctl(struct file *file, u_int cmd, u_long arg) | 
 | 163 | { | 
 | 164 | 	int ret; | 
 | 165 |  | 
 | 166 | 	lock_kernel(); | 
 | 167 | 	ret = dac_audio_ioctl(file, cmd, arg); | 
 | 168 | 	unlock_kernel(); | 
 | 169 |  | 
 | 170 | 	return ret; | 
 | 171 | } | 
 | 172 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count, | 
 | 174 | 			       loff_t * ppos) | 
 | 175 | { | 
 | 176 | 	int free; | 
 | 177 | 	int nbytes; | 
 | 178 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | 	if (!count) { | 
 | 180 | 		dac_audio_sync(); | 
 | 181 | 		return 0; | 
 | 182 | 	} | 
 | 183 |  | 
 | 184 | 	free = buffer_begin - buffer_end; | 
 | 185 |  | 
 | 186 | 	if (free < 0) | 
 | 187 | 		free += BUFFER_SIZE; | 
 | 188 | 	if ((free == 0) && (empty)) | 
 | 189 | 		free = BUFFER_SIZE; | 
 | 190 | 	if (count > free) | 
 | 191 | 		count = free; | 
 | 192 | 	if (buffer_begin > buffer_end) { | 
 | 193 | 		if (copy_from_user((void *)buffer_end, buf, count)) | 
 | 194 | 			return -EFAULT; | 
 | 195 |  | 
 | 196 | 		buffer_end += count; | 
 | 197 | 	} else { | 
 | 198 | 		nbytes = data_buffer + BUFFER_SIZE - buffer_end; | 
 | 199 | 		if (nbytes > count) { | 
 | 200 | 			if (copy_from_user((void *)buffer_end, buf, count)) | 
 | 201 | 				return -EFAULT; | 
 | 202 | 			buffer_end += count; | 
 | 203 | 		} else { | 
 | 204 | 			if (copy_from_user((void *)buffer_end, buf, nbytes)) | 
 | 205 | 				return -EFAULT; | 
 | 206 | 			if (copy_from_user | 
 | 207 | 			    ((void *)data_buffer, buf + nbytes, count - nbytes)) | 
 | 208 | 				return -EFAULT; | 
 | 209 | 			buffer_end = data_buffer + count - nbytes; | 
 | 210 | 		} | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	if (empty) { | 
 | 214 | 		empty = 0; | 
 | 215 | 		dac_audio_start_timer(); | 
 | 216 | 	} | 
 | 217 |  | 
 | 218 | 	return count; | 
 | 219 | } | 
 | 220 |  | 
 | 221 | static ssize_t dac_audio_read(struct file *file, char *buf, size_t count, | 
 | 222 | 			      loff_t * ppos) | 
 | 223 | { | 
 | 224 | 	return -EINVAL; | 
 | 225 | } | 
 | 226 |  | 
 | 227 | static int dac_audio_open(struct inode *inode, struct file *file) | 
 | 228 | { | 
 | 229 | 	if (file->f_mode & FMODE_READ) | 
 | 230 | 		return -ENODEV; | 
| Arnd Bergmann | 90dc763 | 2010-07-11 12:16:36 +0200 | [diff] [blame] | 231 |  | 
 | 232 | 	lock_kernel(); | 
 | 233 | 	if (in_use) { | 
 | 234 | 		unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | 		return -EBUSY; | 
| Arnd Bergmann | 90dc763 | 2010-07-11 12:16:36 +0200 | [diff] [blame] | 236 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 |  | 
 | 238 | 	in_use = 1; | 
 | 239 |  | 
 | 240 | 	dac_audio_start(); | 
| Arnd Bergmann | 90dc763 | 2010-07-11 12:16:36 +0200 | [diff] [blame] | 241 | 	unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | 	return 0; | 
 | 243 | } | 
 | 244 |  | 
 | 245 | static int dac_audio_release(struct inode *inode, struct file *file) | 
 | 246 | { | 
 | 247 | 	dac_audio_sync(); | 
 | 248 | 	dac_audio_stop(); | 
 | 249 | 	in_use = 0; | 
 | 250 |  | 
 | 251 | 	return 0; | 
 | 252 | } | 
 | 253 |  | 
| Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 254 | const struct file_operations dac_audio_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 |       .read =		dac_audio_read, | 
| Arnd Bergmann | d209974 | 2010-07-12 19:53:18 +0200 | [diff] [blame] | 256 |       .write =		dac_audio_write, | 
 | 257 |       .unlocked_ioctl =	dac_audio_unlocked_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 |       .open =		dac_audio_open, | 
 | 259 |       .release =	dac_audio_release, | 
 | 260 | }; | 
 | 261 |  | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 262 | static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | 	if (!empty) { | 
 | 265 | 		sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); | 
 | 266 | 		buffer_begin++; | 
 | 267 |  | 
 | 268 | 		if (buffer_begin == data_buffer + BUFFER_SIZE) | 
 | 269 | 			buffer_begin = data_buffer; | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 270 | 		if (buffer_begin == buffer_end) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | 			empty = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | 	} | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 273 |  | 
 | 274 | 	if (!empty) | 
 | 275 | 		hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL); | 
 | 276 |  | 
 | 277 | 	return HRTIMER_NORESTART; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } | 
 | 279 |  | 
 | 280 | static int __init dac_audio_init(void) | 
 | 281 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 	if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) { | 
 | 283 | 		printk(KERN_ERR "Cannot register dsp device"); | 
 | 284 | 		return device_major; | 
 | 285 | 	} | 
 | 286 |  | 
 | 287 | 	in_use = 0; | 
 | 288 |  | 
| Jesper Juhl | 4fa95ef | 2006-03-28 01:56:54 -0800 | [diff] [blame] | 289 | 	data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 	if (data_buffer == NULL) | 
 | 291 | 		return -ENOMEM; | 
 | 292 |  | 
 | 293 | 	dac_audio_reset(); | 
 | 294 | 	rate = 8000; | 
 | 295 | 	dac_audio_set_rate(); | 
 | 296 |  | 
| Magnus Damm | 639138a | 2009-05-15 12:07:17 +0900 | [diff] [blame] | 297 | 	/* Today: High Resolution Timer driven DAC playback. | 
 | 298 | 	 * The timer callback gets called once per sample. Ouch. | 
 | 299 | 	 * | 
 | 300 | 	 * Future: A much better approach would be to use the | 
 | 301 | 	 * SH7720 CMT+DMAC+DAC hardware combination like this: | 
 | 302 | 	 * - Program sample rate using CMT0 or CMT1 | 
 | 303 | 	 * - Program DMAC to use CMT for timing and output to DAC | 
 | 304 | 	 * - Play sound using DMAC, let CPU sleep. | 
 | 305 | 	 * - While at it, rewrite this driver to use ALSA. | 
 | 306 | 	 */ | 
 | 307 |  | 
 | 308 | 	hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
 | 309 | 	hrtimer.function = sh_dac_audio_timer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 |  | 
 | 311 | 	return 0; | 
 | 312 | } | 
 | 313 |  | 
 | 314 | static void __exit dac_audio_exit(void) | 
 | 315 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | 	unregister_sound_dsp(device_major); | 
 | 317 | 	kfree((void *)data_buffer); | 
 | 318 | } | 
 | 319 |  | 
 | 320 | module_init(dac_audio_init); | 
 | 321 | module_exit(dac_audio_exit); | 
 | 322 |  | 
 | 323 | MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua"); | 
 | 324 | MODULE_DESCRIPTION("SH DAC sound driver"); | 
 | 325 | MODULE_LICENSE("GPL"); |