Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / oss / sh_dac_audio.c
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/sched.h>
4 #include <linux/linkage.h>
5 #include <linux/slab.h>
6 #include <linux/fs.h>
7 #include <linux/sound.h>
8 #include <linux/soundcard.h>
9 #include <asm/io.h>
10 #include <asm/uaccess.h>
11 #include <asm/irq.h>
12 #include <asm/delay.h>
13 #include <linux/interrupt.h>
14
15 #include <asm/cpu/dac.h>
16
17 #ifdef MACH_HP600
18 #include <asm/hp6xx/hp6xx.h>
19 #include <asm/hd64461/hd64461.h>
20 #endif
21
22 #define MODNAME "sh_dac_audio"
23
24 #define TMU_TOCR_INIT 0x00
25
26 #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
27 #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
28
29 #define TMU_TSTR 0xfffffe92
30 #define TMU1_TCOR 0xfffffea0
31 #define TMU1_TCNT 0xfffffea4
32 #define TMU1_TCR 0xfffffea8
33
34 #define BUFFER_SIZE 48000
35
36 static int rate;
37 static int empty;
38 static char *data_buffer, *buffer_begin, *buffer_end;
39 static int in_use, device_major;
40
41 static void dac_audio_start_timer(void)
42 {
43 u8 tstr;
44
45 tstr = ctrl_inb(TMU_TSTR);
46 tstr |= TMU1_TSTR_INIT;
47 ctrl_outb(tstr, TMU_TSTR);
48 }
49
50 static void dac_audio_stop_timer(void)
51 {
52 u8 tstr;
53
54 tstr = ctrl_inb(TMU_TSTR);
55 tstr &= ~TMU1_TSTR_INIT;
56 ctrl_outb(tstr, TMU_TSTR);
57 }
58
59 static void dac_audio_reset(void)
60 {
61 dac_audio_stop_timer();
62 buffer_begin = buffer_end = data_buffer;
63 empty = 1;
64 }
65
66 static void dac_audio_sync(void)
67 {
68 while (!empty)
69 schedule();
70 }
71
72 static void dac_audio_start(void)
73 {
74 #ifdef MACH_HP600
75 u16 v;
76 v = inw(HD64461_GPADR);
77 v &= ~HD64461_GPADR_SPEAKER;
78 outw(v, HD64461_GPADR);
79 #endif
80 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
81 ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
82 }
83 static void dac_audio_stop(void)
84 {
85 #ifdef MACH_HP600
86 u16 v;
87 #endif
88 dac_audio_stop_timer();
89 #ifdef MACH_HP600
90 v = inw(HD64461_GPADR);
91 v |= HD64461_GPADR_SPEAKER;
92 outw(v, HD64461_GPADR);
93 #endif
94 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
95 }
96
97 static void dac_audio_set_rate(void)
98 {
99 unsigned long interval;
100
101 interval = (current_cpu_data.module_clock / 4) / rate;
102 ctrl_outl(interval, TMU1_TCOR);
103 ctrl_outl(interval, TMU1_TCNT);
104 }
105
106 static int dac_audio_ioctl(struct inode *inode, struct file *file,
107 unsigned int cmd, unsigned long arg)
108 {
109 int val;
110
111 switch (cmd) {
112 case OSS_GETVERSION:
113 return put_user(SOUND_VERSION, (int *)arg);
114
115 case SNDCTL_DSP_SYNC:
116 dac_audio_sync();
117 return 0;
118
119 case SNDCTL_DSP_RESET:
120 dac_audio_reset();
121 return 0;
122
123 case SNDCTL_DSP_GETFMTS:
124 return put_user(AFMT_U8, (int *)arg);
125
126 case SNDCTL_DSP_SETFMT:
127 return put_user(AFMT_U8, (int *)arg);
128
129 case SNDCTL_DSP_NONBLOCK:
130 file->f_flags |= O_NONBLOCK;
131 return 0;
132
133 case SNDCTL_DSP_GETCAPS:
134 return 0;
135
136 case SOUND_PCM_WRITE_RATE:
137 val = *(int *)arg;
138 if (val > 0) {
139 rate = val;
140 dac_audio_set_rate();
141 }
142 return put_user(rate, (int *)arg);
143
144 case SNDCTL_DSP_STEREO:
145 return put_user(0, (int *)arg);
146
147 case SOUND_PCM_WRITE_CHANNELS:
148 return put_user(1, (int *)arg);
149
150 case SNDCTL_DSP_SETDUPLEX:
151 return -EINVAL;
152
153 case SNDCTL_DSP_PROFILE:
154 return -EINVAL;
155
156 case SNDCTL_DSP_GETBLKSIZE:
157 return put_user(BUFFER_SIZE, (int *)arg);
158
159 case SNDCTL_DSP_SETFRAGMENT:
160 return 0;
161
162 default:
163 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
164 cmd);
165 return -EINVAL;
166 }
167 return -EINVAL;
168 }
169
170 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
171 loff_t * ppos)
172 {
173 int free;
174 int nbytes;
175
176 if (count < 0)
177 return -EINVAL;
178
179 if (!count) {
180 dac_audio_sync();
181 return 0;
182 }
183
184 free = buffer_begin - buffer_end;
185
186 if (free < 0)
187 free += BUFFER_SIZE;
188 if ((free == 0) && (empty))
189 free = BUFFER_SIZE;
190 if (count > free)
191 count = free;
192 if (buffer_begin > buffer_end) {
193 if (copy_from_user((void *)buffer_end, buf, count))
194 return -EFAULT;
195
196 buffer_end += count;
197 } else {
198 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
199 if (nbytes > count) {
200 if (copy_from_user((void *)buffer_end, buf, count))
201 return -EFAULT;
202 buffer_end += count;
203 } else {
204 if (copy_from_user((void *)buffer_end, buf, nbytes))
205 return -EFAULT;
206 if (copy_from_user
207 ((void *)data_buffer, buf + nbytes, count - nbytes))
208 return -EFAULT;
209 buffer_end = data_buffer + count - nbytes;
210 }
211 }
212
213 if (empty) {
214 empty = 0;
215 dac_audio_start_timer();
216 }
217
218 return count;
219 }
220
221 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
222 loff_t * ppos)
223 {
224 return -EINVAL;
225 }
226
227 static int dac_audio_open(struct inode *inode, struct file *file)
228 {
229 if (file->f_mode & FMODE_READ)
230 return -ENODEV;
231 if (in_use)
232 return -EBUSY;
233
234 in_use = 1;
235
236 dac_audio_start();
237
238 return 0;
239 }
240
241 static int dac_audio_release(struct inode *inode, struct file *file)
242 {
243 dac_audio_sync();
244 dac_audio_stop();
245 in_use = 0;
246
247 return 0;
248 }
249
250 struct file_operations dac_audio_fops = {
251 .read = dac_audio_read,
252 .write = dac_audio_write,
253 .ioctl = dac_audio_ioctl,
254 .open = dac_audio_open,
255 .release = dac_audio_release,
256 };
257
258 static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
259 {
260 unsigned long timer_status;
261
262 timer_status = ctrl_inw(TMU1_TCR);
263 timer_status &= ~0x100;
264 ctrl_outw(timer_status, TMU1_TCR);
265
266 if (!empty) {
267 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
268 buffer_begin++;
269
270 if (buffer_begin == data_buffer + BUFFER_SIZE)
271 buffer_begin = data_buffer;
272 if (buffer_begin == buffer_end) {
273 empty = 1;
274 dac_audio_stop_timer();
275 }
276 }
277 return IRQ_HANDLED;
278 }
279
280 static int __init dac_audio_init(void)
281 {
282 int retval;
283
284 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
285 printk(KERN_ERR "Cannot register dsp device");
286 return device_major;
287 }
288
289 in_use = 0;
290
291 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
292 if (data_buffer == NULL)
293 return -ENOMEM;
294
295 dac_audio_reset();
296 rate = 8000;
297 dac_audio_set_rate();
298
299 retval =
300 request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
301 if (retval < 0) {
302 printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
303 TIMER1_IRQ);
304 return retval;
305 }
306
307 return 0;
308 }
309
310 static void __exit dac_audio_exit(void)
311 {
312 free_irq(TIMER1_IRQ, 0);
313
314 unregister_sound_dsp(device_major);
315 kfree((void *)data_buffer);
316 }
317
318 module_init(dac_audio_init);
319 module_exit(dac_audio_exit);
320
321 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
322 MODULE_DESCRIPTION("SH DAC sound driver");
323 MODULE_LICENSE("GPL");