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