Merge branch 'for-2.6.36' of git://git.kernel.dk/linux-2.6-block
[sfrench/cifs-2.6.git] / sound / oss / sh_dac_audio.c
1 /*
2  * sound/oss/sh_dac_audio.c
3  *
4  * SH DAC based sound :(
5  *
6  *  Copyright (C) 2004,2005  Andriy Skulysh
7  *
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
10  * for more details.
11  */
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>
17 #include <linux/fs.h>
18 #include <linux/smp_lock.h>
19 #include <linux/sound.h>
20 #include <linux/smp_lock.h>
21 #include <linux/soundcard.h>
22 #include <linux/interrupt.h>
23 #include <linux/hrtimer.h>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26 #include <asm/irq.h>
27 #include <asm/delay.h>
28 #include <asm/clock.h>
29 #include <cpu/dac.h>
30 #include <asm/machvec.h>
31 #include <mach/hp6xx.h>
32 #include <asm/hd64461.h>
33
34 #define MODNAME "sh_dac_audio"
35
36 #define BUFFER_SIZE 48000
37
38 static int rate;
39 static int empty;
40 static char *data_buffer, *buffer_begin, *buffer_end;
41 static int in_use, device_major;
42 static struct hrtimer hrtimer;
43 static ktime_t wakeups_per_second;
44
45 static void dac_audio_start_timer(void)
46 {
47         hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
48 }
49
50 static void dac_audio_stop_timer(void)
51 {
52         hrtimer_cancel(&hrtimer);
53 }
54
55 static void dac_audio_reset(void)
56 {
57         dac_audio_stop_timer();
58         buffer_begin = buffer_end = data_buffer;
59         empty = 1;
60 }
61
62 static void dac_audio_sync(void)
63 {
64         while (!empty)
65                 schedule();
66 }
67
68 static void dac_audio_start(void)
69 {
70         if (mach_is_hp6xx()) {
71                 u16 v = __raw_readw(HD64461_GPADR);
72                 v &= ~HD64461_GPADR_SPEAKER;
73                 __raw_writew(v, HD64461_GPADR);
74         }
75
76         sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
77 }
78 static void dac_audio_stop(void)
79 {
80         dac_audio_stop_timer();
81
82         if (mach_is_hp6xx()) {
83                 u16 v = __raw_readw(HD64461_GPADR);
84                 v |= HD64461_GPADR_SPEAKER;
85                 __raw_writew(v, HD64461_GPADR);
86         }
87
88         sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
89         sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
90 }
91
92 static void dac_audio_set_rate(void)
93 {
94         wakeups_per_second = ktime_set(0, 1000000000 / rate);
95 }
96
97 static int dac_audio_ioctl(struct file *file,
98                            unsigned int cmd, unsigned long arg)
99 {
100         int val;
101
102         switch (cmd) {
103         case OSS_GETVERSION:
104                 return put_user(SOUND_VERSION, (int *)arg);
105
106         case SNDCTL_DSP_SYNC:
107                 dac_audio_sync();
108                 return 0;
109
110         case SNDCTL_DSP_RESET:
111                 dac_audio_reset();
112                 return 0;
113
114         case SNDCTL_DSP_GETFMTS:
115                 return put_user(AFMT_U8, (int *)arg);
116
117         case SNDCTL_DSP_SETFMT:
118                 return put_user(AFMT_U8, (int *)arg);
119
120         case SNDCTL_DSP_NONBLOCK:
121                 spin_lock(&file->f_lock);
122                 file->f_flags |= O_NONBLOCK;
123                 spin_unlock(&file->f_lock);
124                 return 0;
125
126         case SNDCTL_DSP_GETCAPS:
127                 return 0;
128
129         case SOUND_PCM_WRITE_RATE:
130                 val = *(int *)arg;
131                 if (val > 0) {
132                         rate = val;
133                         dac_audio_set_rate();
134                 }
135                 return put_user(rate, (int *)arg);
136
137         case SNDCTL_DSP_STEREO:
138                 return put_user(0, (int *)arg);
139
140         case SOUND_PCM_WRITE_CHANNELS:
141                 return put_user(1, (int *)arg);
142
143         case SNDCTL_DSP_SETDUPLEX:
144                 return -EINVAL;
145
146         case SNDCTL_DSP_PROFILE:
147                 return -EINVAL;
148
149         case SNDCTL_DSP_GETBLKSIZE:
150                 return put_user(BUFFER_SIZE, (int *)arg);
151
152         case SNDCTL_DSP_SETFRAGMENT:
153                 return 0;
154
155         default:
156                 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
157                        cmd);
158                 return -EINVAL;
159         }
160         return -EINVAL;
161 }
162
163 static long dac_audio_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
164 {
165         int ret;
166
167         lock_kernel();
168         ret = dac_audio_ioctl(file, cmd, arg);
169         unlock_kernel();
170
171         return ret;
172 }
173
174 static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
175                                loff_t * ppos)
176 {
177         int free;
178         int nbytes;
179
180         if (!count) {
181                 dac_audio_sync();
182                 return 0;
183         }
184
185         free = buffer_begin - buffer_end;
186
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;
196
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;
211                 }
212         }
213
214         if (empty) {
215                 empty = 0;
216                 dac_audio_start_timer();
217         }
218
219         return count;
220 }
221
222 static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
223                               loff_t * ppos)
224 {
225         return -EINVAL;
226 }
227
228 static int dac_audio_open(struct inode *inode, struct file *file)
229 {
230         if (file->f_mode & FMODE_READ)
231                 return -ENODEV;
232
233         lock_kernel();
234         if (in_use) {
235                 unlock_kernel();
236                 return -EBUSY;
237         }
238
239         in_use = 1;
240
241         dac_audio_start();
242         unlock_kernel();
243         return 0;
244 }
245
246 static int dac_audio_release(struct inode *inode, struct file *file)
247 {
248         dac_audio_sync();
249         dac_audio_stop();
250         in_use = 0;
251
252         return 0;
253 }
254
255 const struct file_operations dac_audio_fops = {
256       .read =           dac_audio_read,
257       .write =          dac_audio_write,
258       .unlocked_ioctl = dac_audio_unlocked_ioctl,
259       .open =           dac_audio_open,
260       .release =        dac_audio_release,
261 };
262
263 static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
264 {
265         if (!empty) {
266                 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
267                 buffer_begin++;
268
269                 if (buffer_begin == data_buffer + BUFFER_SIZE)
270                         buffer_begin = data_buffer;
271                 if (buffer_begin == buffer_end)
272                         empty = 1;
273         }
274
275         if (!empty)
276                 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
277
278         return HRTIMER_NORESTART;
279 }
280
281 static int __init dac_audio_init(void)
282 {
283         if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
284                 printk(KERN_ERR "Cannot register dsp device");
285                 return device_major;
286         }
287
288         in_use = 0;
289
290         data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
291         if (data_buffer == NULL)
292                 return -ENOMEM;
293
294         dac_audio_reset();
295         rate = 8000;
296         dac_audio_set_rate();
297
298         /* Today: High Resolution Timer driven DAC playback.
299          * The timer callback gets called once per sample. Ouch.
300          *
301          * Future: A much better approach would be to use the
302          * SH7720 CMT+DMAC+DAC hardware combination like this:
303          * - Program sample rate using CMT0 or CMT1
304          * - Program DMAC to use CMT for timing and output to DAC
305          * - Play sound using DMAC, let CPU sleep.
306          * - While at it, rewrite this driver to use ALSA.
307          */
308
309         hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
310         hrtimer.function = sh_dac_audio_timer;
311
312         return 0;
313 }
314
315 static void __exit dac_audio_exit(void)
316 {
317         unregister_sound_dsp(device_major);
318         kfree((void *)data_buffer);
319 }
320
321 module_init(dac_audio_init);
322 module_exit(dac_audio_exit);
323
324 MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
325 MODULE_DESCRIPTION("SH DAC sound driver");
326 MODULE_LICENSE("GPL");