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>
8 #include <linux/sound.h>
9 #include <linux/soundcard.h>
11 #include <asm/uaccess.h>
13 #include <asm/delay.h>
14 #include <linux/interrupt.h>
16 #include <asm/cpu/dac.h>
19 #include <asm/hp6xx/hp6xx.h>
20 #include <asm/hd64461/hd64461.h>
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
39 static char *data_buffer
, *buffer_begin
, *buffer_end
;
40 static int in_use
, device_major
;
42 static void dac_audio_start_timer(void)
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)
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
;
67 static void dac_audio_sync(void)
73 static void dac_audio_start(void)
77 v
= inw(HD64461_GPADR
);
78 v
&= ~HD64461_GPADR_SPEAKER
;
79 outw(v
, HD64461_GPADR
);
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)
89 dac_audio_stop_timer();
91 v
= inw(HD64461_GPADR
);
92 v
|= HD64461_GPADR_SPEAKER
;
93 outw(v
, HD64461_GPADR
);
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
)
114 return put_user(SOUND_VERSION
, (int *)arg
);
116 case SNDCTL_DSP_SYNC
:
120 case SNDCTL_DSP_RESET
:
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
;
134 case SNDCTL_DSP_GETCAPS
:
137 case SOUND_PCM_WRITE_RATE
:
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
:
154 case SNDCTL_DSP_PROFILE
:
157 case SNDCTL_DSP_GETBLKSIZE
:
158 return put_user(BUFFER_SIZE
, (int *)arg
);
160 case SNDCTL_DSP_SETFRAGMENT
:
164 printk(KERN_ERR
"sh_dac_audio: unimplemented ioctl=0x%x\n",
171 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
)
242 static int dac_audio_release(struct inode
*inode
, struct file
*file
)
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
);
268 sh_dac_output(*buffer_begin
, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL
);
271 if (buffer_begin
== data_buffer
+ BUFFER_SIZE
)
272 buffer_begin
= data_buffer
;
273 if (buffer_begin
== buffer_end
) {
275 dac_audio_stop_timer();
281 static int __init
dac_audio_init(void)
285 if ((device_major
= register_sound_dsp(&dac_audio_fops
, -1)) < 0) {
286 printk(KERN_ERR
"Cannot register dsp device");
292 data_buffer
= (char *)kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
293 if (data_buffer
== NULL
)
298 dac_audio_set_rate();
301 request_irq(TIMER1_IRQ
, timer1_interrupt
, SA_INTERRUPT
, MODNAME
, 0);
303 printk(KERN_ERR
"sh_dac_audio: IRQ %d request failed\n",
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");