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>
21 #include <linux/hrtimer.h>
23 #include <asm/uaccess.h>
25 #include <asm/delay.h>
26 #include <asm/clock.h>
28 #include <asm/machvec.h>
29 #include <mach/hp6xx.h>
30 #include <asm/hd64461.h>
32 #define MODNAME "sh_dac_audio"
34 #define BUFFER_SIZE 48000
38 static char *data_buffer, *buffer_begin, *buffer_end;
39 static int in_use, device_major;
40 static struct hrtimer hrtimer;
41 static ktime_t wakeups_per_second;
43 static void dac_audio_start_timer(void)
45 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
48 static void dac_audio_stop_timer(void)
50 hrtimer_cancel(&hrtimer);
53 static void dac_audio_reset(void)
55 dac_audio_stop_timer();
56 buffer_begin = buffer_end = data_buffer;
60 static void dac_audio_sync(void)
66 static void dac_audio_start(void)
68 if (mach_is_hp6xx()) {
69 u16 v = __raw_readw(HD64461_GPADR);
70 v &= ~HD64461_GPADR_SPEAKER;
71 __raw_writew(v, HD64461_GPADR);
74 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
76 static void dac_audio_stop(void)
78 dac_audio_stop_timer();
80 if (mach_is_hp6xx()) {
81 u16 v = __raw_readw(HD64461_GPADR);
82 v |= HD64461_GPADR_SPEAKER;
83 __raw_writew(v, HD64461_GPADR);
86 sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
87 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
90 static void dac_audio_set_rate(void)
92 wakeups_per_second = ktime_set(0, 1000000000 / rate);
95 static int dac_audio_ioctl(struct inode *inode, struct file *file,
96 unsigned int cmd, unsigned long arg)
102 return put_user(SOUND_VERSION, (int *)arg);
104 case SNDCTL_DSP_SYNC:
108 case SNDCTL_DSP_RESET:
112 case SNDCTL_DSP_GETFMTS:
113 return put_user(AFMT_U8, (int *)arg);
115 case SNDCTL_DSP_SETFMT:
116 return put_user(AFMT_U8, (int *)arg);
118 case SNDCTL_DSP_NONBLOCK:
119 spin_lock(&file->f_lock);
120 file->f_flags |= O_NONBLOCK;
121 spin_unlock(&file->f_lock);
124 case SNDCTL_DSP_GETCAPS:
127 case SOUND_PCM_WRITE_RATE:
131 dac_audio_set_rate();
133 return put_user(rate, (int *)arg);
135 case SNDCTL_DSP_STEREO:
136 return put_user(0, (int *)arg);
138 case SOUND_PCM_WRITE_CHANNELS:
139 return put_user(1, (int *)arg);
141 case SNDCTL_DSP_SETDUPLEX:
144 case SNDCTL_DSP_PROFILE:
147 case SNDCTL_DSP_GETBLKSIZE:
148 return put_user(BUFFER_SIZE, (int *)arg);
150 case SNDCTL_DSP_SETFRAGMENT:
154 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
161 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
175 free = buffer_begin - buffer_end;
179 if ((free == 0) && (empty))
183 if (buffer_begin > buffer_end) {
184 if (copy_from_user((void *)buffer_end, buf, count))
189 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
190 if (nbytes > count) {
191 if (copy_from_user((void *)buffer_end, buf, count))
195 if (copy_from_user((void *)buffer_end, buf, nbytes))
198 ((void *)data_buffer, buf + nbytes, count - nbytes))
200 buffer_end = data_buffer + count - nbytes;
206 dac_audio_start_timer();
212 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
218 static int dac_audio_open(struct inode *inode, struct file *file)
220 if (file->f_mode & FMODE_READ)
232 static int dac_audio_release(struct inode *inode, struct file *file)
241 const struct file_operations dac_audio_fops = {
242 .read = dac_audio_read,
243 .write = dac_audio_write,
244 .ioctl = dac_audio_ioctl,
245 .open = dac_audio_open,
246 .release = dac_audio_release,
249 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
252 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
255 if (buffer_begin == data_buffer + BUFFER_SIZE)
256 buffer_begin = data_buffer;
257 if (buffer_begin == buffer_end)
262 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
264 return HRTIMER_NORESTART;
267 static int __init dac_audio_init(void)
269 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
270 printk(KERN_ERR "Cannot register dsp device");
276 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
277 if (data_buffer == NULL)
282 dac_audio_set_rate();
284 /* Today: High Resolution Timer driven DAC playback.
285 * The timer callback gets called once per sample. Ouch.
287 * Future: A much better approach would be to use the
288 * SH7720 CMT+DMAC+DAC hardware combination like this:
289 * - Program sample rate using CMT0 or CMT1
290 * - Program DMAC to use CMT for timing and output to DAC
291 * - Play sound using DMAC, let CPU sleep.
292 * - While at it, rewrite this driver to use ALSA.
295 hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
296 hrtimer.function = sh_dac_audio_timer;
301 static void __exit dac_audio_exit(void)
303 unregister_sound_dsp(device_major);
304 kfree((void *)data_buffer);
307 module_init(dac_audio_init);
308 module_exit(dac_audio_exit);
310 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
311 MODULE_DESCRIPTION("SH DAC sound driver");
312 MODULE_LICENSE("GPL");