Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6.git] / sound / oss / sh_dac_audio.c
blob8a9917c919c2e60cc00b06908f61d855bb184f3c
1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/sched.h>
5 #include <linux/linkage.h>
6 #include <linux/slab.h>
7 #include <linux/fs.h>
8 #include <linux/sound.h>
9 #include <linux/soundcard.h>
10 #include <asm/io.h>
11 #include <asm/uaccess.h>
12 #include <asm/irq.h>
13 #include <asm/delay.h>
14 #include <linux/interrupt.h>
16 #include <asm/cpu/dac.h>
18 #ifdef MACH_HP600
19 #include <asm/hp6xx/hp6xx.h>
20 #include <asm/hd64461/hd64461.h>
21 #endif
23 #define MODNAME "sh_dac_audio"
25 #define TMU_TOCR_INIT 0x00
27 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
28 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
30 #define TMU_TSTR 0xfffffe92
31 #define TMU1_TCOR 0xfffffea0
32 #define TMU1_TCNT 0xfffffea4
33 #define TMU1_TCR 0xfffffea8
35 #define BUFFER_SIZE 48000
37 static int rate;
38 static int empty;
39 static char *data_buffer, *buffer_begin, *buffer_end;
40 static int in_use, device_major;
42 static void dac_audio_start_timer(void)
44 u8 tstr;
46 tstr = ctrl_inb(TMU_TSTR);
47 tstr |= TMU1_TSTR_INIT;
48 ctrl_outb(tstr, TMU_TSTR);
51 static void dac_audio_stop_timer(void)
53 u8 tstr;
55 tstr = ctrl_inb(TMU_TSTR);
56 tstr &= ~TMU1_TSTR_INIT;
57 ctrl_outb(tstr, TMU_TSTR);
60 static void dac_audio_reset(void)
62 dac_audio_stop_timer();
63 buffer_begin = buffer_end = data_buffer;
64 empty = 1;
67 static void dac_audio_sync(void)
69 while (!empty)
70 schedule();
73 static void dac_audio_start(void)
75 #ifdef MACH_HP600
76 u16 v;
77 v = inw(HD64461_GPADR);
78 v &= ~HD64461_GPADR_SPEAKER;
79 outw(v, HD64461_GPADR);
80 #endif
81 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
82 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
84 static void dac_audio_stop(void)
86 #ifdef MACH_HP600
87 u16 v;
88 #endif
89 dac_audio_stop_timer();
90 #ifdef MACH_HP600
91 v = inw(HD64461_GPADR);
92 v |= HD64461_GPADR_SPEAKER;
93 outw(v, HD64461_GPADR);
94 #endif
95 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
98 static void dac_audio_set_rate(void)
100 unsigned long interval;
102 interval = (current_cpu_data.module_clock / 4) / rate;
103 ctrl_outl(interval, TMU1_TCOR);
104 ctrl_outl(interval, TMU1_TCNT);
107 static int dac_audio_ioctl(struct inode *inode, struct file *file,
108 unsigned int cmd, unsigned long arg)
110 int val;
112 switch (cmd) {
113 case OSS_GETVERSION:
114 return put_user(SOUND_VERSION, (int *)arg);
116 case SNDCTL_DSP_SYNC:
117 dac_audio_sync();
118 return 0;
120 case SNDCTL_DSP_RESET:
121 dac_audio_reset();
122 return 0;
124 case SNDCTL_DSP_GETFMTS:
125 return put_user(AFMT_U8, (int *)arg);
127 case SNDCTL_DSP_SETFMT:
128 return put_user(AFMT_U8, (int *)arg);
130 case SNDCTL_DSP_NONBLOCK:
131 file->f_flags |= O_NONBLOCK;
132 return 0;
134 case SNDCTL_DSP_GETCAPS:
135 return 0;
137 case SOUND_PCM_WRITE_RATE:
138 val = *(int *)arg;
139 if (val > 0) {
140 rate = val;
141 dac_audio_set_rate();
143 return put_user(rate, (int *)arg);
145 case SNDCTL_DSP_STEREO:
146 return put_user(0, (int *)arg);
148 case SOUND_PCM_WRITE_CHANNELS:
149 return put_user(1, (int *)arg);
151 case SNDCTL_DSP_SETDUPLEX:
152 return -EINVAL;
154 case SNDCTL_DSP_PROFILE:
155 return -EINVAL;
157 case SNDCTL_DSP_GETBLKSIZE:
158 return put_user(BUFFER_SIZE, (int *)arg);
160 case SNDCTL_DSP_SETFRAGMENT:
161 return 0;
163 default:
164 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
165 cmd);
166 return -EINVAL;
168 return -EINVAL;
171 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
172 loff_t * ppos)
174 int free;
175 int nbytes;
177 if (count < 0)
178 return -EINVAL;
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;
232 if (in_use)
233 return -EBUSY;
235 in_use = 1;
237 dac_audio_start();
239 return 0;
242 static int dac_audio_release(struct inode *inode, struct file *file)
244 dac_audio_sync();
245 dac_audio_stop();
246 in_use = 0;
248 return 0;
251 struct file_operations dac_audio_fops = {
252 .read = dac_audio_read,
253 .write = dac_audio_write,
254 .ioctl = dac_audio_ioctl,
255 .open = dac_audio_open,
256 .release = dac_audio_release,
259 static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
261 unsigned long timer_status;
263 timer_status = ctrl_inw(TMU1_TCR);
264 timer_status &= ~0x100;
265 ctrl_outw(timer_status, TMU1_TCR);
267 if (!empty) {
268 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
269 buffer_begin++;
271 if (buffer_begin == data_buffer + BUFFER_SIZE)
272 buffer_begin = data_buffer;
273 if (buffer_begin == buffer_end) {
274 empty = 1;
275 dac_audio_stop_timer();
278 return IRQ_HANDLED;
281 static int __init dac_audio_init(void)
283 int retval;
285 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
286 printk(KERN_ERR "Cannot register dsp device");
287 return device_major;
290 in_use = 0;
292 data_buffer = (char *)kmalloc(BUFFER_SIZE, GFP_KERNEL);
293 if (data_buffer == NULL)
294 return -ENOMEM;
296 dac_audio_reset();
297 rate = 8000;
298 dac_audio_set_rate();
300 retval =
301 request_irq(TIMER1_IRQ, timer1_interrupt, SA_INTERRUPT, MODNAME, 0);
302 if (retval < 0) {
303 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
304 TIMER1_IRQ);
305 return retval;
308 return 0;
311 static void __exit dac_audio_exit(void)
313 free_irq(TIMER1_IRQ, 0);
315 unregister_sound_dsp(device_major);
316 kfree((void *)data_buffer);
319 module_init(dac_audio_init);
320 module_exit(dac_audio_exit);
322 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
323 MODULE_DESCRIPTION("SH DAC sound driver");
324 MODULE_LICENSE("GPL");