2 * sound/oss/sh_dac_audio.c
4 * SH DAC based sound :(
6 * Copyright (C) 2004,2005 Andriy Skulysh
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
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/linkage.h>
16 #include <linux/slab.h>
18 #include <linux/sound.h>
19 #include <linux/soundcard.h>
20 #include <linux/interrupt.h>
22 #include <asm/uaccess.h>
24 #include <asm/delay.h>
25 #include <asm/clock.h>
26 #include <asm/cpu/dac.h>
27 #include <asm/cpu/timer.h>
28 #include <asm/machvec.h>
29 #include <mach/hp6xx.h>
30 #include <asm/hd64461.h>
32 #define MODNAME "sh_dac_audio"
34 #define TMU_TOCR_INIT 0x00
36 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
37 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
39 #define BUFFER_SIZE 48000
43 static char *data_buffer, *buffer_begin, *buffer_end;
44 static int in_use, device_major;
46 static void dac_audio_start_timer(void)
50 tstr = ctrl_inb(TMU_TSTR);
51 tstr |= TMU1_TSTR_INIT;
52 ctrl_outb(tstr, TMU_TSTR);
55 static void dac_audio_stop_timer(void)
59 tstr = ctrl_inb(TMU_TSTR);
60 tstr &= ~TMU1_TSTR_INIT;
61 ctrl_outb(tstr, TMU_TSTR);
64 static void dac_audio_reset(void)
66 dac_audio_stop_timer();
67 buffer_begin = buffer_end = data_buffer;
71 static void dac_audio_sync(void)
77 static void dac_audio_start(void)
79 if (mach_is_hp6xx()) {
80 u16 v = inw(HD64461_GPADR);
81 v &= ~HD64461_GPADR_SPEAKER;
82 outw(v, HD64461_GPADR);
85 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
86 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
88 static void dac_audio_stop(void)
90 dac_audio_stop_timer();
92 if (mach_is_hp6xx()) {
93 u16 v = inw(HD64461_GPADR);
94 v |= HD64461_GPADR_SPEAKER;
95 outw(v, HD64461_GPADR);
98 sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
99 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
102 static void dac_audio_set_rate(void)
104 unsigned long interval;
107 clk = clk_get(NULL, "module_clk");
108 interval = (clk_get_rate(clk) / 4) / rate;
110 ctrl_outl(interval, TMU1_TCOR);
111 ctrl_outl(interval, TMU1_TCNT);
114 static int dac_audio_ioctl(struct inode *inode, struct file *file,
115 unsigned int cmd, unsigned long arg)
121 return put_user(SOUND_VERSION, (int *)arg);
123 case SNDCTL_DSP_SYNC:
127 case SNDCTL_DSP_RESET:
131 case SNDCTL_DSP_GETFMTS:
132 return put_user(AFMT_U8, (int *)arg);
134 case SNDCTL_DSP_SETFMT:
135 return put_user(AFMT_U8, (int *)arg);
137 case SNDCTL_DSP_NONBLOCK:
138 spin_lock(&file->f_lock);
139 file->f_flags |= O_NONBLOCK;
140 spin_unlock(&file->f_lock);
143 case SNDCTL_DSP_GETCAPS:
146 case SOUND_PCM_WRITE_RATE:
150 dac_audio_set_rate();
152 return put_user(rate, (int *)arg);
154 case SNDCTL_DSP_STEREO:
155 return put_user(0, (int *)arg);
157 case SOUND_PCM_WRITE_CHANNELS:
158 return put_user(1, (int *)arg);
160 case SNDCTL_DSP_SETDUPLEX:
163 case SNDCTL_DSP_PROFILE:
166 case SNDCTL_DSP_GETBLKSIZE:
167 return put_user(BUFFER_SIZE, (int *)arg);
169 case SNDCTL_DSP_SETFRAGMENT:
173 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
180 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
194 free = buffer_begin - buffer_end;
198 if ((free == 0) && (empty))
202 if (buffer_begin > buffer_end) {
203 if (copy_from_user((void *)buffer_end, buf, count))
208 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
209 if (nbytes > count) {
210 if (copy_from_user((void *)buffer_end, buf, count))
214 if (copy_from_user((void *)buffer_end, buf, nbytes))
217 ((void *)data_buffer, buf + nbytes, count - nbytes))
219 buffer_end = data_buffer + count - nbytes;
225 dac_audio_start_timer();
231 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
237 static int dac_audio_open(struct inode *inode, struct file *file)
239 if (file->f_mode & FMODE_READ)
251 static int dac_audio_release(struct inode *inode, struct file *file)
260 const struct file_operations dac_audio_fops = {
261 .read = dac_audio_read,
262 .write = dac_audio_write,
263 .ioctl = dac_audio_ioctl,
264 .open = dac_audio_open,
265 .release = dac_audio_release,
268 static irqreturn_t timer1_interrupt(int irq, void *dev)
270 unsigned long timer_status;
272 timer_status = ctrl_inw(TMU1_TCR);
273 timer_status &= ~0x100;
274 ctrl_outw(timer_status, TMU1_TCR);
277 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
280 if (buffer_begin == data_buffer + BUFFER_SIZE)
281 buffer_begin = data_buffer;
282 if (buffer_begin == buffer_end) {
284 dac_audio_stop_timer();
290 static int __init dac_audio_init(void)
294 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
295 printk(KERN_ERR "Cannot register dsp device");
301 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
302 if (data_buffer == NULL)
307 dac_audio_set_rate();
310 request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
312 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
320 static void __exit dac_audio_exit(void)
322 free_irq(TIMER1_IRQ, 0);
324 unregister_sound_dsp(device_major);
325 kfree((void *)data_buffer);
328 module_init(dac_audio_init);
329 module_exit(dac_audio_exit);
331 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
332 MODULE_DESCRIPTION("SH DAC sound driver");
333 MODULE_LICENSE("GPL");