1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/sched.h>
5 #include <linux/version.h>
6 #include <linux/linkage.h>
7 #include <linux/slab.h>
9 #include <linux/sound.h>
10 #include <linux/soundcard.h>
12 #include <asm/uaccess.h>
14 #include <asm/delay.h>
15 #include <linux/interrupt.h>
17 #include <asm/cpu/dac.h>
20 #include <asm/hp6xx/hp6xx.h>
21 #include <asm/hd64461/hd64461.h>
24 #define MODNAME "sh_dac_audio"
26 #define TMU_TOCR_INIT 0x00
28 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
29 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
31 #define TMU_TSTR 0xfffffe92
32 #define TMU1_TCOR 0xfffffea0
33 #define TMU1_TCNT 0xfffffea4
34 #define TMU1_TCR 0xfffffea8
36 #define BUFFER_SIZE 48000
40 static char *data_buffer
, *buffer_begin
, *buffer_end
;
41 static int in_use
, device_major
;
43 static void dac_audio_start_timer(void)
47 tstr
= ctrl_inb(TMU_TSTR
);
48 tstr
|= TMU1_TSTR_INIT
;
49 ctrl_outb(tstr
, TMU_TSTR
);
52 static void dac_audio_stop_timer(void)
56 tstr
= ctrl_inb(TMU_TSTR
);
57 tstr
&= ~TMU1_TSTR_INIT
;
58 ctrl_outb(tstr
, TMU_TSTR
);
61 static void dac_audio_reset(void)
63 dac_audio_stop_timer();
64 buffer_begin
= buffer_end
= data_buffer
;
68 static void dac_audio_sync(void)
74 static void dac_audio_start(void)
78 v
= inw(HD64461_GPADR
);
79 v
&= ~HD64461_GPADR_SPEAKER
;
80 outw(v
, HD64461_GPADR
);
82 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
83 ctrl_outw(TMU1_TCR_INIT
, TMU1_TCR
);
85 static void dac_audio_stop(void)
90 dac_audio_stop_timer();
92 v
= inw(HD64461_GPADR
);
93 v
|= HD64461_GPADR_SPEAKER
;
94 outw(v
, HD64461_GPADR
);
96 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
99 static void dac_audio_set_rate(void)
101 unsigned long interval
;
103 interval
= (current_cpu_data
.module_clock
/ 4) / rate
;
104 ctrl_outl(interval
, TMU1_TCOR
);
105 ctrl_outl(interval
, TMU1_TCNT
);
108 static int dac_audio_ioctl(struct inode
*inode
, struct file
*file
,
109 unsigned int cmd
, unsigned long arg
)
115 return put_user(SOUND_VERSION
, (int *)arg
);
117 case SNDCTL_DSP_SYNC
:
121 case SNDCTL_DSP_RESET
:
125 case SNDCTL_DSP_GETFMTS
:
126 return put_user(AFMT_U8
, (int *)arg
);
128 case SNDCTL_DSP_SETFMT
:
129 return put_user(AFMT_U8
, (int *)arg
);
131 case SNDCTL_DSP_NONBLOCK
:
132 file
->f_flags
|= O_NONBLOCK
;
135 case SNDCTL_DSP_GETCAPS
:
138 case SOUND_PCM_WRITE_RATE
:
142 dac_audio_set_rate();
144 return put_user(rate
, (int *)arg
);
146 case SNDCTL_DSP_STEREO
:
147 return put_user(0, (int *)arg
);
149 case SOUND_PCM_WRITE_CHANNELS
:
150 return put_user(1, (int *)arg
);
152 case SNDCTL_DSP_SETDUPLEX
:
155 case SNDCTL_DSP_PROFILE
:
158 case SNDCTL_DSP_GETBLKSIZE
:
159 return put_user(BUFFER_SIZE
, (int *)arg
);
161 case SNDCTL_DSP_SETFRAGMENT
:
165 printk(KERN_ERR
"sh_dac_audio: unimplemented ioctl=0x%x\n",
172 static ssize_t
dac_audio_write(struct file
*file
, const char *buf
, size_t count
,
186 free
= buffer_begin
- buffer_end
;
190 if ((free
== 0) && (empty
))
194 if (buffer_begin
> buffer_end
) {
195 if (copy_from_user((void *)buffer_end
, buf
, count
))
200 nbytes
= data_buffer
+ BUFFER_SIZE
- buffer_end
;
201 if (nbytes
> count
) {
202 if (copy_from_user((void *)buffer_end
, buf
, count
))
206 if (copy_from_user((void *)buffer_end
, buf
, nbytes
))
209 ((void *)data_buffer
, buf
+ nbytes
, count
- nbytes
))
211 buffer_end
= data_buffer
+ count
- nbytes
;
217 dac_audio_start_timer();
223 static ssize_t
dac_audio_read(struct file
*file
, char *buf
, size_t count
,
229 static int dac_audio_open(struct inode
*inode
, struct file
*file
)
231 if (file
->f_mode
& FMODE_READ
)
243 static int dac_audio_release(struct inode
*inode
, struct file
*file
)
252 struct file_operations dac_audio_fops
= {
253 .read
= dac_audio_read
,
254 .write
= dac_audio_write
,
255 .ioctl
= dac_audio_ioctl
,
256 .open
= dac_audio_open
,
257 .release
= dac_audio_release
,
260 static irqreturn_t
timer1_interrupt(int irq
, void *dev
, struct pt_regs
*regs
)
262 unsigned long timer_status
;
264 timer_status
= ctrl_inw(TMU1_TCR
);
265 timer_status
&= ~0x100;
266 ctrl_outw(timer_status
, TMU1_TCR
);
269 sh_dac_output(*buffer_begin
, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
272 if (buffer_begin
== data_buffer
+ BUFFER_SIZE
)
273 buffer_begin
= data_buffer
;
274 if (buffer_begin
== buffer_end
) {
276 dac_audio_stop_timer();
282 static int __init
dac_audio_init(void)
286 if ((device_major
= register_sound_dsp(&dac_audio_fops
, -1)) < 0) {
287 printk(KERN_ERR
"Cannot register dsp device");
293 data_buffer
= (char *)kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
294 if (data_buffer
== NULL
)
299 dac_audio_set_rate();
302 request_irq(TIMER1_IRQ
, timer1_interrupt
, SA_INTERRUPT
, MODNAME
, 0);
304 printk(KERN_ERR
"sh_dac_audio: IRQ %d request failed\n",
312 static void __exit
dac_audio_exit(void)
314 free_irq(TIMER1_IRQ
, 0);
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");