dm: factor out max_io_len_target_boundary
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / oss / sh_dac_audio.c
blobfdb58eb83d4e15daede90704337ab2da5c2297d6
1 /*
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
10 * for more details.
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>
17 #include <linux/fs.h>
18 #include <linux/smp_lock.h>
19 #include <linux/sound.h>
20 #include <linux/smp_lock.h>
21 #include <linux/soundcard.h>
22 #include <linux/interrupt.h>
23 #include <linux/hrtimer.h>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <asm/irq.h>
27 #include <asm/delay.h>
28 #include <asm/clock.h>
29 #include <cpu/dac.h>
30 #include <asm/machvec.h>
31 #include <mach/hp6xx.h>
32 #include <asm/hd64461.h>
34 #define MODNAME "sh_dac_audio"
36 #define BUFFER_SIZE 48000
38 static int rate;
39 static int empty;
40 static char *data_buffer, *buffer_begin, *buffer_end;
41 static int in_use, device_major;
42 static struct hrtimer hrtimer;
43 static ktime_t wakeups_per_second;
45 static void dac_audio_start_timer(void)
47 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
50 static void dac_audio_stop_timer(void)
52 hrtimer_cancel(&hrtimer);
55 static void dac_audio_reset(void)
57 dac_audio_stop_timer();
58 buffer_begin = buffer_end = data_buffer;
59 empty = 1;
62 static void dac_audio_sync(void)
64 while (!empty)
65 schedule();
68 static void dac_audio_start(void)
70 if (mach_is_hp6xx()) {
71 u16 v = __raw_readw(HD64461_GPADR);
72 v &= ~HD64461_GPADR_SPEAKER;
73 __raw_writew(v, HD64461_GPADR);
76 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
78 static void dac_audio_stop(void)
80 dac_audio_stop_timer();
82 if (mach_is_hp6xx()) {
83 u16 v = __raw_readw(HD64461_GPADR);
84 v |= HD64461_GPADR_SPEAKER;
85 __raw_writew(v, HD64461_GPADR);
88 sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
89 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
92 static void dac_audio_set_rate(void)
94 wakeups_per_second = ktime_set(0, 1000000000 / rate);
97 static int dac_audio_ioctl(struct file *file,
98 unsigned int cmd, unsigned long arg)
100 int val;
102 switch (cmd) {
103 case OSS_GETVERSION:
104 return put_user(SOUND_VERSION, (int *)arg);
106 case SNDCTL_DSP_SYNC:
107 dac_audio_sync();
108 return 0;
110 case SNDCTL_DSP_RESET:
111 dac_audio_reset();
112 return 0;
114 case SNDCTL_DSP_GETFMTS:
115 return put_user(AFMT_U8, (int *)arg);
117 case SNDCTL_DSP_SETFMT:
118 return put_user(AFMT_U8, (int *)arg);
120 case SNDCTL_DSP_NONBLOCK:
121 spin_lock(&file->f_lock);
122 file->f_flags |= O_NONBLOCK;
123 spin_unlock(&file->f_lock);
124 return 0;
126 case SNDCTL_DSP_GETCAPS:
127 return 0;
129 case SOUND_PCM_WRITE_RATE:
130 val = *(int *)arg;
131 if (val > 0) {
132 rate = val;
133 dac_audio_set_rate();
135 return put_user(rate, (int *)arg);
137 case SNDCTL_DSP_STEREO:
138 return put_user(0, (int *)arg);
140 case SOUND_PCM_WRITE_CHANNELS:
141 return put_user(1, (int *)arg);
143 case SNDCTL_DSP_SETDUPLEX:
144 return -EINVAL;
146 case SNDCTL_DSP_PROFILE:
147 return -EINVAL;
149 case SNDCTL_DSP_GETBLKSIZE:
150 return put_user(BUFFER_SIZE, (int *)arg);
152 case SNDCTL_DSP_SETFRAGMENT:
153 return 0;
155 default:
156 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
157 cmd);
158 return -EINVAL;
160 return -EINVAL;
163 static long dac_audio_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
165 int ret;
167 lock_kernel();
168 ret = dac_audio_ioctl(file, cmd, arg);
169 unlock_kernel();
171 return ret;
174 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
175 loff_t * ppos)
177 int free;
178 int nbytes;
180 if (!count) {
181 dac_audio_sync();
182 return 0;
185 free = buffer_begin - buffer_end;
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;
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;
214 if (empty) {
215 empty = 0;
216 dac_audio_start_timer();
219 return count;
222 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
223 loff_t * ppos)
225 return -EINVAL;
228 static int dac_audio_open(struct inode *inode, struct file *file)
230 if (file->f_mode & FMODE_READ)
231 return -ENODEV;
233 lock_kernel();
234 if (in_use) {
235 unlock_kernel();
236 return -EBUSY;
239 in_use = 1;
241 dac_audio_start();
242 unlock_kernel();
243 return 0;
246 static int dac_audio_release(struct inode *inode, struct file *file)
248 dac_audio_sync();
249 dac_audio_stop();
250 in_use = 0;
252 return 0;
255 const struct file_operations dac_audio_fops = {
256 .read = dac_audio_read,
257 .write = dac_audio_write,
258 .unlocked_ioctl = dac_audio_unlocked_ioctl,
259 .open = dac_audio_open,
260 .release = dac_audio_release,
263 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
265 if (!empty) {
266 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
267 buffer_begin++;
269 if (buffer_begin == data_buffer + BUFFER_SIZE)
270 buffer_begin = data_buffer;
271 if (buffer_begin == buffer_end)
272 empty = 1;
275 if (!empty)
276 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
278 return HRTIMER_NORESTART;
281 static int __init dac_audio_init(void)
283 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
284 printk(KERN_ERR "Cannot register dsp device");
285 return device_major;
288 in_use = 0;
290 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
291 if (data_buffer == NULL)
292 return -ENOMEM;
294 dac_audio_reset();
295 rate = 8000;
296 dac_audio_set_rate();
298 /* Today: High Resolution Timer driven DAC playback.
299 * The timer callback gets called once per sample. Ouch.
301 * Future: A much better approach would be to use the
302 * SH7720 CMT+DMAC+DAC hardware combination like this:
303 * - Program sample rate using CMT0 or CMT1
304 * - Program DMAC to use CMT for timing and output to DAC
305 * - Play sound using DMAC, let CPU sleep.
306 * - While at it, rewrite this driver to use ALSA.
309 hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
310 hrtimer.function = sh_dac_audio_timer;
312 return 0;
315 static void __exit dac_audio_exit(void)
317 unregister_sound_dsp(device_major);
318 kfree((void *)data_buffer);
321 module_init(dac_audio_init);
322 module_exit(dac_audio_exit);
324 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
325 MODULE_DESCRIPTION("SH DAC sound driver");
326 MODULE_LICENSE("GPL");