Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/config.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/linkage.h> |
| 6 | #include <linux/slab.h> |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/sound.h> |
| 9 | #include <linux/soundcard.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/uaccess.h> |
| 12 | #include <asm/irq.h> |
| 13 | #include <asm/delay.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | |
| 16 | #include <asm/cpu/dac.h> |
| 17 | |
| 18 | #ifdef MACH_HP600 |
| 19 | #include <asm/hp6xx/hp6xx.h> |
| 20 | #include <asm/hd64461/hd64461.h> |
| 21 | #endif |
| 22 | |
| 23 | #define MODNAME "sh_dac_audio" |
| 24 | |
| 25 | #define TMU_TOCR_INIT 0x00 |
| 26 | |
| 27 | #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */ |
| 28 | #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */ |
| 29 | |
| 30 | #define TMU_TSTR 0xfffffe92 |
| 31 | #define TMU1_TCOR 0xfffffea0 |
| 32 | #define TMU1_TCNT 0xfffffea4 |
| 33 | #define TMU1_TCR 0xfffffea8 |
| 34 | |
| 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; |
| 41 | |
| 42 | static void dac_audio_start_timer(void) |
| 43 | { |
| 44 | u8 tstr; |
| 45 | |
| 46 | tstr = ctrl_inb(TMU_TSTR); |
| 47 | tstr |= TMU1_TSTR_INIT; |
| 48 | ctrl_outb(tstr, TMU_TSTR); |
| 49 | } |
| 50 | |
| 51 | static void dac_audio_stop_timer(void) |
| 52 | { |
| 53 | u8 tstr; |
| 54 | |
| 55 | tstr = ctrl_inb(TMU_TSTR); |
| 56 | tstr &= ~TMU1_TSTR_INIT; |
| 57 | ctrl_outb(tstr, TMU_TSTR); |
| 58 | } |
| 59 | |
| 60 | static void dac_audio_reset(void) |
| 61 | { |
| 62 | dac_audio_stop_timer(); |
| 63 | buffer_begin = buffer_end = data_buffer; |
| 64 | empty = 1; |
| 65 | } |
| 66 | |
| 67 | static void dac_audio_sync(void) |
| 68 | { |
| 69 | while (!empty) |
| 70 | schedule(); |
| 71 | } |
| 72 | |
| 73 | static void dac_audio_start(void) |
| 74 | { |
| 75 | #ifdef MACH_HP600 |
| 76 | u16 v; |
| 77 | v = inw(HD64461_GPADR); |
| 78 | v &= ~HD64461_GPADR_SPEAKER; |
| 79 | outw(v, HD64461_GPADR); |
| 80 | #endif |
| 81 | sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); |
| 82 | ctrl_outw(TMU1_TCR_INIT, TMU1_TCR); |
| 83 | } |
| 84 | static void dac_audio_stop(void) |
| 85 | { |
| 86 | #ifdef MACH_HP600 |
| 87 | u16 v; |
| 88 | #endif |
| 89 | dac_audio_stop_timer(); |
| 90 | #ifdef MACH_HP600 |
| 91 | v = inw(HD64461_GPADR); |
| 92 | v |= HD64461_GPADR_SPEAKER; |
| 93 | outw(v, HD64461_GPADR); |
| 94 | #endif |
| 95 | sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); |
| 96 | } |
| 97 | |
| 98 | static void dac_audio_set_rate(void) |
| 99 | { |
| 100 | unsigned long interval; |
| 101 | |
| 102 | interval = (current_cpu_data.module_clock / 4) / rate; |
| 103 | ctrl_outl(interval, TMU1_TCOR); |
| 104 | ctrl_outl(interval, TMU1_TCNT); |
| 105 | } |
| 106 | |
| 107 | static int dac_audio_ioctl(struct inode *inode, struct file *file, |
| 108 | unsigned int cmd, unsigned long arg) |
| 109 | { |
| 110 | int val; |
| 111 | |
| 112 | switch (cmd) { |
| 113 | case OSS_GETVERSION: |
| 114 | return put_user(SOUND_VERSION, (int *)arg); |
| 115 | |
| 116 | case SNDCTL_DSP_SYNC: |
| 117 | dac_audio_sync(); |
| 118 | return 0; |
| 119 | |
| 120 | case SNDCTL_DSP_RESET: |
| 121 | dac_audio_reset(); |
| 122 | return 0; |
| 123 | |
| 124 | case SNDCTL_DSP_GETFMTS: |
| 125 | return put_user(AFMT_U8, (int *)arg); |
| 126 | |
| 127 | case SNDCTL_DSP_SETFMT: |
| 128 | return put_user(AFMT_U8, (int *)arg); |
| 129 | |
| 130 | case SNDCTL_DSP_NONBLOCK: |
| 131 | file->f_flags |= O_NONBLOCK; |
| 132 | return 0; |
| 133 | |
| 134 | case SNDCTL_DSP_GETCAPS: |
| 135 | return 0; |
| 136 | |
| 137 | case SOUND_PCM_WRITE_RATE: |
| 138 | val = *(int *)arg; |
| 139 | if (val > 0) { |
| 140 | rate = val; |
| 141 | dac_audio_set_rate(); |
| 142 | } |
| 143 | return put_user(rate, (int *)arg); |
| 144 | |
| 145 | case SNDCTL_DSP_STEREO: |
| 146 | return put_user(0, (int *)arg); |
| 147 | |
| 148 | case SOUND_PCM_WRITE_CHANNELS: |
| 149 | return put_user(1, (int *)arg); |
| 150 | |
| 151 | case SNDCTL_DSP_SETDUPLEX: |
| 152 | return -EINVAL; |
| 153 | |
| 154 | case SNDCTL_DSP_PROFILE: |
| 155 | return -EINVAL; |
| 156 | |
| 157 | case SNDCTL_DSP_GETBLKSIZE: |
| 158 | return put_user(BUFFER_SIZE, (int *)arg); |
| 159 | |
| 160 | case SNDCTL_DSP_SETFRAGMENT: |
| 161 | return 0; |
| 162 | |
| 163 | default: |
| 164 | printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n", |
| 165 | cmd); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
| 171 | static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count, |
| 172 | loff_t * ppos) |
| 173 | { |
| 174 | int free; |
| 175 | int nbytes; |
| 176 | |
| 177 | if (count < 0) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | if (!count) { |
| 181 | dac_audio_sync(); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | free = buffer_begin - buffer_end; |
| 186 | |
| 187 | if (free < 0) |
| 188 | free += BUFFER_SIZE; |
| 189 | if ((free == 0) && (empty)) |
| 190 | free = BUFFER_SIZE; |
| 191 | if (count > free) |
| 192 | count = free; |
| 193 | if (buffer_begin > buffer_end) { |
| 194 | if (copy_from_user((void *)buffer_end, buf, count)) |
| 195 | return -EFAULT; |
| 196 | |
| 197 | buffer_end += count; |
| 198 | } else { |
| 199 | nbytes = data_buffer + BUFFER_SIZE - buffer_end; |
| 200 | if (nbytes > count) { |
| 201 | if (copy_from_user((void *)buffer_end, buf, count)) |
| 202 | return -EFAULT; |
| 203 | buffer_end += count; |
| 204 | } else { |
| 205 | if (copy_from_user((void *)buffer_end, buf, nbytes)) |
| 206 | return -EFAULT; |
| 207 | if (copy_from_user |
| 208 | ((void *)data_buffer, buf + nbytes, count - nbytes)) |
| 209 | return -EFAULT; |
| 210 | buffer_end = data_buffer + count - nbytes; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (empty) { |
| 215 | empty = 0; |
| 216 | dac_audio_start_timer(); |
| 217 | } |
| 218 | |
| 219 | return count; |
| 220 | } |
| 221 | |
| 222 | static ssize_t dac_audio_read(struct file *file, char *buf, size_t count, |
| 223 | loff_t * ppos) |
| 224 | { |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | static int dac_audio_open(struct inode *inode, struct file *file) |
| 229 | { |
| 230 | if (file->f_mode & FMODE_READ) |
| 231 | return -ENODEV; |
| 232 | if (in_use) |
| 233 | return -EBUSY; |
| 234 | |
| 235 | in_use = 1; |
| 236 | |
| 237 | dac_audio_start(); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int dac_audio_release(struct inode *inode, struct file *file) |
| 243 | { |
| 244 | dac_audio_sync(); |
| 245 | dac_audio_stop(); |
| 246 | in_use = 0; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | struct file_operations dac_audio_fops = { |
| 252 | .read = dac_audio_read, |
| 253 | .write = dac_audio_write, |
| 254 | .ioctl = dac_audio_ioctl, |
| 255 | .open = dac_audio_open, |
| 256 | .release = dac_audio_release, |
| 257 | }; |
| 258 | |
| 259 | static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs) |
| 260 | { |
| 261 | unsigned long timer_status; |
| 262 | |
| 263 | timer_status = ctrl_inw(TMU1_TCR); |
| 264 | timer_status &= ~0x100; |
| 265 | ctrl_outw(timer_status, TMU1_TCR); |
| 266 | |
| 267 | if (!empty) { |
| 268 | sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL); |
| 269 | buffer_begin++; |
| 270 | |
| 271 | if (buffer_begin == data_buffer + BUFFER_SIZE) |
| 272 | buffer_begin = data_buffer; |
| 273 | if (buffer_begin == buffer_end) { |
| 274 | empty = 1; |
| 275 | dac_audio_stop_timer(); |
| 276 | } |
| 277 | } |
| 278 | return IRQ_HANDLED; |
| 279 | } |
| 280 | |
| 281 | static int __init dac_audio_init(void) |
| 282 | { |
| 283 | int retval; |
| 284 | |
| 285 | if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) { |
| 286 | printk(KERN_ERR "Cannot register dsp device"); |
| 287 | return device_major; |
| 288 | } |
| 289 | |
| 290 | in_use = 0; |
| 291 | |
Jesper Juhl | 4fa95ef | 2006-03-28 01:56:54 -0800 | [diff] [blame^] | 292 | data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | if (data_buffer == NULL) |
| 294 | return -ENOMEM; |
| 295 | |
| 296 | dac_audio_reset(); |
| 297 | rate = 8000; |
| 298 | dac_audio_set_rate(); |
| 299 | |
| 300 | retval = |
| 301 | request_irq(TIMER1_IRQ, timer1_interrupt, SA_INTERRUPT, MODNAME, 0); |
| 302 | if (retval < 0) { |
| 303 | printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n", |
| 304 | TIMER1_IRQ); |
| 305 | return retval; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static void __exit dac_audio_exit(void) |
| 312 | { |
| 313 | free_irq(TIMER1_IRQ, 0); |
| 314 | |
| 315 | unregister_sound_dsp(device_major); |
| 316 | kfree((void *)data_buffer); |
| 317 | } |
| 318 | |
| 319 | module_init(dac_audio_init); |
| 320 | module_exit(dac_audio_exit); |
| 321 | |
| 322 | MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua"); |
| 323 | MODULE_DESCRIPTION("SH DAC sound driver"); |
| 324 | MODULE_LICENSE("GPL"); |