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/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>
25 #include <asm/uaccess.h>
27 #include <asm/delay.h>
28 #include <asm/clock.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
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
;
62 static void dac_audio_sync(void)
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
)
104 return put_user(SOUND_VERSION
, (int *)arg
);
106 case SNDCTL_DSP_SYNC
:
110 case SNDCTL_DSP_RESET
:
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
);
126 case SNDCTL_DSP_GETCAPS
:
129 case SOUND_PCM_WRITE_RATE
:
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
:
146 case SNDCTL_DSP_PROFILE
:
149 case SNDCTL_DSP_GETBLKSIZE
:
150 return put_user(BUFFER_SIZE
, (int *)arg
);
152 case SNDCTL_DSP_SETFRAGMENT
:
156 printk(KERN_ERR
"sh_dac_audio: unimplemented ioctl=0x%x\n",
163 static long dac_audio_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
168 ret
= dac_audio_ioctl(file
, cmd
, arg
);
174 static ssize_t
dac_audio_write(struct file
*file
, const char *buf
, size_t count
,
185 free
= buffer_begin
- buffer_end
;
189 if ((free
== 0) && (empty
))
193 if (buffer_begin
> buffer_end
) {
194 if (copy_from_user((void *)buffer_end
, buf
, count
))
199 nbytes
= data_buffer
+ BUFFER_SIZE
- buffer_end
;
200 if (nbytes
> count
) {
201 if (copy_from_user((void *)buffer_end
, buf
, count
))
205 if (copy_from_user((void *)buffer_end
, buf
, nbytes
))
208 ((void *)data_buffer
, buf
+ nbytes
, count
- nbytes
))
210 buffer_end
= data_buffer
+ count
- nbytes
;
216 dac_audio_start_timer();
222 static ssize_t
dac_audio_read(struct file
*file
, char *buf
, size_t count
,
228 static int dac_audio_open(struct inode
*inode
, struct file
*file
)
230 if (file
->f_mode
& FMODE_READ
)
246 static int dac_audio_release(struct inode
*inode
, struct file
*file
)
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
)
266 sh_dac_output(*buffer_begin
, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
269 if (buffer_begin
== data_buffer
+ BUFFER_SIZE
)
270 buffer_begin
= data_buffer
;
271 if (buffer_begin
== buffer_end
)
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");
290 data_buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
291 if (data_buffer
== NULL
)
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
;
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");