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/smp_lock.h>
20 #include <linux/soundcard.h>
21 #include <linux/interrupt.h>
22 #include <linux/hrtimer.h>
24 #include <asm/uaccess.h>
26 #include <asm/delay.h>
27 #include <asm/clock.h>
29 #include <asm/machvec.h>
30 #include <mach/hp6xx.h>
31 #include <asm/hd64461.h>
33 #define MODNAME "sh_dac_audio"
35 #define BUFFER_SIZE 48000
39 static char *data_buffer
, *buffer_begin
, *buffer_end
;
40 static int in_use
, device_major
;
41 static struct hrtimer hrtimer
;
42 static ktime_t wakeups_per_second
;
44 static void dac_audio_start_timer(void)
46 hrtimer_start(&hrtimer
, wakeups_per_second
, HRTIMER_MODE_REL
);
49 static void dac_audio_stop_timer(void)
51 hrtimer_cancel(&hrtimer
);
54 static void dac_audio_reset(void)
56 dac_audio_stop_timer();
57 buffer_begin
= buffer_end
= data_buffer
;
61 static void dac_audio_sync(void)
67 static void dac_audio_start(void)
69 if (mach_is_hp6xx()) {
70 u16 v
= __raw_readw(HD64461_GPADR
);
71 v
&= ~HD64461_GPADR_SPEAKER
;
72 __raw_writew(v
, HD64461_GPADR
);
75 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
77 static void dac_audio_stop(void)
79 dac_audio_stop_timer();
81 if (mach_is_hp6xx()) {
82 u16 v
= __raw_readw(HD64461_GPADR
);
83 v
|= HD64461_GPADR_SPEAKER
;
84 __raw_writew(v
, HD64461_GPADR
);
87 sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
88 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
91 static void dac_audio_set_rate(void)
93 wakeups_per_second
= ktime_set(0, 1000000000 / rate
);
96 static int dac_audio_ioctl(struct file
*file
,
97 unsigned int cmd
, unsigned long arg
)
103 return put_user(SOUND_VERSION
, (int *)arg
);
105 case SNDCTL_DSP_SYNC
:
109 case SNDCTL_DSP_RESET
:
113 case SNDCTL_DSP_GETFMTS
:
114 return put_user(AFMT_U8
, (int *)arg
);
116 case SNDCTL_DSP_SETFMT
:
117 return put_user(AFMT_U8
, (int *)arg
);
119 case SNDCTL_DSP_NONBLOCK
:
120 spin_lock(&file
->f_lock
);
121 file
->f_flags
|= O_NONBLOCK
;
122 spin_unlock(&file
->f_lock
);
125 case SNDCTL_DSP_GETCAPS
:
128 case SOUND_PCM_WRITE_RATE
:
132 dac_audio_set_rate();
134 return put_user(rate
, (int *)arg
);
136 case SNDCTL_DSP_STEREO
:
137 return put_user(0, (int *)arg
);
139 case SOUND_PCM_WRITE_CHANNELS
:
140 return put_user(1, (int *)arg
);
142 case SNDCTL_DSP_SETDUPLEX
:
145 case SNDCTL_DSP_PROFILE
:
148 case SNDCTL_DSP_GETBLKSIZE
:
149 return put_user(BUFFER_SIZE
, (int *)arg
);
151 case SNDCTL_DSP_SETFRAGMENT
:
155 printk(KERN_ERR
"sh_dac_audio: unimplemented ioctl=0x%x\n",
162 static long dac_audio_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
167 ret
= dac_audio_ioctl(file
, cmd
, arg
);
173 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
)
245 static int dac_audio_release(struct inode
*inode
, struct file
*file
)
254 const struct file_operations dac_audio_fops
= {
255 .read
= dac_audio_read
,
256 .write
= dac_audio_write
,
257 .unlocked_ioctl
= dac_audio_unlocked_ioctl
,
258 .open
= dac_audio_open
,
259 .release
= dac_audio_release
,
262 static enum hrtimer_restart
sh_dac_audio_timer(struct hrtimer
*handle
)
265 sh_dac_output(*buffer_begin
, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
268 if (buffer_begin
== data_buffer
+ BUFFER_SIZE
)
269 buffer_begin
= data_buffer
;
270 if (buffer_begin
== buffer_end
)
275 hrtimer_start(&hrtimer
, wakeups_per_second
, HRTIMER_MODE_REL
);
277 return HRTIMER_NORESTART
;
280 static int __init
dac_audio_init(void)
282 if ((device_major
= register_sound_dsp(&dac_audio_fops
, -1)) < 0) {
283 printk(KERN_ERR
"Cannot register dsp device");
289 data_buffer
= kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
290 if (data_buffer
== NULL
)
295 dac_audio_set_rate();
297 /* Today: High Resolution Timer driven DAC playback.
298 * The timer callback gets called once per sample. Ouch.
300 * Future: A much better approach would be to use the
301 * SH7720 CMT+DMAC+DAC hardware combination like this:
302 * - Program sample rate using CMT0 or CMT1
303 * - Program DMAC to use CMT for timing and output to DAC
304 * - Play sound using DMAC, let CPU sleep.
305 * - While at it, rewrite this driver to use ALSA.
308 hrtimer_init(&hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
309 hrtimer
.function
= sh_dac_audio_timer
;
314 static void __exit
dac_audio_exit(void)
316 unregister_sound_dsp(device_major
);
317 kfree((void *)data_buffer
);
320 module_init(dac_audio_init
);
321 module_exit(dac_audio_exit
);
323 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
324 MODULE_DESCRIPTION("SH DAC sound driver");
325 MODULE_LICENSE("GPL");