1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/sched.h>
4 #include <linux/linkage.h>
5 #include <linux/slab.h>
7 #include <linux/sound.h>
8 #include <linux/soundcard.h>
10 #include <asm/uaccess.h>
12 #include <asm/delay.h>
13 #include <linux/interrupt.h>
15 #include <asm/cpu/dac.h>
18 #include <asm/hp6xx/hp6xx.h>
19 #include <asm/hd64461/hd64461.h>
22 #define MODNAME "sh_dac_audio"
24 #define TMU_TOCR_INIT 0x00
26 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
27 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
29 #define TMU_TSTR 0xfffffe92
30 #define TMU1_TCOR 0xfffffea0
31 #define TMU1_TCNT 0xfffffea4
32 #define TMU1_TCR 0xfffffea8
34 #define BUFFER_SIZE 48000
38 static char *data_buffer, *buffer_begin, *buffer_end;
39 static int in_use, device_major;
41 static void dac_audio_start_timer(void)
45 tstr = ctrl_inb(TMU_TSTR);
46 tstr |= TMU1_TSTR_INIT;
47 ctrl_outb(tstr, TMU_TSTR);
50 static void dac_audio_stop_timer(void)
54 tstr = ctrl_inb(TMU_TSTR);
55 tstr &= ~TMU1_TSTR_INIT;
56 ctrl_outb(tstr, TMU_TSTR);
59 static void dac_audio_reset(void)
61 dac_audio_stop_timer();
62 buffer_begin = buffer_end = data_buffer;
66 static void dac_audio_sync(void)
72 static void dac_audio_start(void)
76 v = inw(HD64461_GPADR);
77 v &= ~HD64461_GPADR_SPEAKER;
78 outw(v, HD64461_GPADR);
80 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
81 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
83 static void dac_audio_stop(void)
88 dac_audio_stop_timer();
90 v = inw(HD64461_GPADR);
91 v |= HD64461_GPADR_SPEAKER;
92 outw(v, HD64461_GPADR);
94 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
97 static void dac_audio_set_rate(void)
99 unsigned long interval;
101 interval = (current_cpu_data.module_clock / 4) / rate;
102 ctrl_outl(interval, TMU1_TCOR);
103 ctrl_outl(interval, TMU1_TCNT);
106 static int dac_audio_ioctl(struct inode *inode, struct file *file,
107 unsigned int cmd, unsigned long arg)
113 return put_user(SOUND_VERSION, (int *)arg);
115 case SNDCTL_DSP_SYNC:
119 case SNDCTL_DSP_RESET:
123 case SNDCTL_DSP_GETFMTS:
124 return put_user(AFMT_U8, (int *)arg);
126 case SNDCTL_DSP_SETFMT:
127 return put_user(AFMT_U8, (int *)arg);
129 case SNDCTL_DSP_NONBLOCK:
130 file->f_flags |= O_NONBLOCK;
133 case SNDCTL_DSP_GETCAPS:
136 case SOUND_PCM_WRITE_RATE:
140 dac_audio_set_rate();
142 return put_user(rate, (int *)arg);
144 case SNDCTL_DSP_STEREO:
145 return put_user(0, (int *)arg);
147 case SOUND_PCM_WRITE_CHANNELS:
148 return put_user(1, (int *)arg);
150 case SNDCTL_DSP_SETDUPLEX:
153 case SNDCTL_DSP_PROFILE:
156 case SNDCTL_DSP_GETBLKSIZE:
157 return put_user(BUFFER_SIZE, (int *)arg);
159 case SNDCTL_DSP_SETFRAGMENT:
163 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
170 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
184 free = buffer_begin - buffer_end;
188 if ((free == 0) && (empty))
192 if (buffer_begin > buffer_end) {
193 if (copy_from_user((void *)buffer_end, buf, count))
198 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
199 if (nbytes > count) {
200 if (copy_from_user((void *)buffer_end, buf, count))
204 if (copy_from_user((void *)buffer_end, buf, nbytes))
207 ((void *)data_buffer, buf + nbytes, count - nbytes))
209 buffer_end = data_buffer + count - nbytes;
215 dac_audio_start_timer();
221 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
227 static int dac_audio_open(struct inode *inode, struct file *file)
229 if (file->f_mode & FMODE_READ)
241 static int dac_audio_release(struct inode *inode, struct file *file)
250 struct file_operations dac_audio_fops = {
251 .read = dac_audio_read,
252 .write = dac_audio_write,
253 .ioctl = dac_audio_ioctl,
254 .open = dac_audio_open,
255 .release = dac_audio_release,
258 static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
260 unsigned long timer_status;
262 timer_status = ctrl_inw(TMU1_TCR);
263 timer_status &= ~0x100;
264 ctrl_outw(timer_status, TMU1_TCR);
267 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
270 if (buffer_begin == data_buffer + BUFFER_SIZE)
271 buffer_begin = data_buffer;
272 if (buffer_begin == buffer_end) {
274 dac_audio_stop_timer();
280 static int __init dac_audio_init(void)
284 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
285 printk(KERN_ERR "Cannot register dsp device");
291 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
292 if (data_buffer == NULL)
297 dac_audio_set_rate();
300 request_irq(TIMER1_IRQ, timer1_interrupt, SA_INTERRUPT, MODNAME, 0);
302 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
310 static void __exit dac_audio_exit(void)
312 free_irq(TIMER1_IRQ, 0);
314 unregister_sound_dsp(device_major);
315 kfree((void *)data_buffer);
318 module_init(dac_audio_init);
319 module_exit(dac_audio_exit);
321 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
322 MODULE_DESCRIPTION("SH DAC sound driver");
323 MODULE_LICENSE("GPL");