blob: 8f0be4053a5aba62ca96a19e28646ddb1ab69c6d [file] [log] [blame]
Andriy Skulysh7e27b9b2006-09-27 13:48:32 +09001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/linkage.h>
16#include <linux/slab.h>
17#include <linux/fs.h>
18#include <linux/sound.h>
Arnd Bergmann90dc7632010-07-11 12:16:36 +020019#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/soundcard.h>
Andriy Skulysh4bcac202006-09-27 13:07:38 +090021#include <linux/interrupt.h>
Magnus Damm639138a2009-05-15 12:07:17 +090022#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/io.h>
24#include <asm/uaccess.h>
25#include <asm/irq.h>
26#include <asm/delay.h>
Andriy Skulysh7e27b9b2006-09-27 13:48:32 +090027#include <asm/clock.h>
Magnus Damm639138a2009-05-15 12:07:17 +090028#include <cpu/dac.h>
Andriy Skulysh4bcac202006-09-27 13:07:38 +090029#include <asm/machvec.h>
Paul Mundt7639a452008-10-20 13:02:48 +090030#include <mach/hp6xx.h>
Andriy Skulysh7e27b9b2006-09-27 13:48:32 +090031#include <asm/hd64461.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define MODNAME "sh_dac_audio"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define BUFFER_SIZE 48000
36
37static int rate;
38static int empty;
39static char *data_buffer, *buffer_begin, *buffer_end;
40static int in_use, device_major;
Magnus Damm639138a2009-05-15 12:07:17 +090041static struct hrtimer hrtimer;
42static ktime_t wakeups_per_second;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static void dac_audio_start_timer(void)
45{
Magnus Damm639138a2009-05-15 12:07:17 +090046 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49static void dac_audio_stop_timer(void)
50{
Magnus Damm639138a2009-05-15 12:07:17 +090051 hrtimer_cancel(&hrtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54static void dac_audio_reset(void)
55{
56 dac_audio_stop_timer();
57 buffer_begin = buffer_end = data_buffer;
58 empty = 1;
59}
60
61static void dac_audio_sync(void)
62{
63 while (!empty)
64 schedule();
65}
66
67static void dac_audio_start(void)
68{
Andriy Skulysh4bcac202006-09-27 13:07:38 +090069 if (mach_is_hp6xx()) {
Magnus Damm639138a2009-05-15 12:07:17 +090070 u16 v = __raw_readw(HD64461_GPADR);
Andriy Skulysh4bcac202006-09-27 13:07:38 +090071 v &= ~HD64461_GPADR_SPEAKER;
Magnus Damm639138a2009-05-15 12:07:17 +090072 __raw_writew(v, HD64461_GPADR);
Andriy Skulysh4bcac202006-09-27 13:07:38 +090073 }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77static void dac_audio_stop(void)
78{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 dac_audio_stop_timer();
Andriy Skulysh4bcac202006-09-27 13:07:38 +090080
81 if (mach_is_hp6xx()) {
Magnus Damm639138a2009-05-15 12:07:17 +090082 u16 v = __raw_readw(HD64461_GPADR);
Andriy Skulysh4bcac202006-09-27 13:07:38 +090083 v |= HD64461_GPADR_SPEAKER;
Magnus Damm639138a2009-05-15 12:07:17 +090084 __raw_writew(v, HD64461_GPADR);
Andriy Skulysh4bcac202006-09-27 13:07:38 +090085 }
86
Magnus Damm639138a2009-05-15 12:07:17 +090087 sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
89}
90
91static void dac_audio_set_rate(void)
92{
Magnus Damm639138a2009-05-15 12:07:17 +090093 wakeups_per_second = ktime_set(0, 1000000000 / rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static int dac_audio_ioctl(struct inode *inode, struct file *file,
97 unsigned int cmd, unsigned long arg)
98{
99 int val;
100
101 switch (cmd) {
102 case OSS_GETVERSION:
103 return put_user(SOUND_VERSION, (int *)arg);
104
105 case SNDCTL_DSP_SYNC:
106 dac_audio_sync();
107 return 0;
108
109 case SNDCTL_DSP_RESET:
110 dac_audio_reset();
111 return 0;
112
113 case SNDCTL_DSP_GETFMTS:
114 return put_user(AFMT_U8, (int *)arg);
115
116 case SNDCTL_DSP_SETFMT:
117 return put_user(AFMT_U8, (int *)arg);
118
119 case SNDCTL_DSP_NONBLOCK:
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700120 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 file->f_flags |= O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700122 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124
125 case SNDCTL_DSP_GETCAPS:
126 return 0;
127
128 case SOUND_PCM_WRITE_RATE:
129 val = *(int *)arg;
130 if (val > 0) {
131 rate = val;
132 dac_audio_set_rate();
133 }
134 return put_user(rate, (int *)arg);
135
136 case SNDCTL_DSP_STEREO:
137 return put_user(0, (int *)arg);
138
139 case SOUND_PCM_WRITE_CHANNELS:
140 return put_user(1, (int *)arg);
141
142 case SNDCTL_DSP_SETDUPLEX:
143 return -EINVAL;
144
145 case SNDCTL_DSP_PROFILE:
146 return -EINVAL;
147
148 case SNDCTL_DSP_GETBLKSIZE:
149 return put_user(BUFFER_SIZE, (int *)arg);
150
151 case SNDCTL_DSP_SETFRAGMENT:
152 return 0;
153
154 default:
155 printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
156 cmd);
157 return -EINVAL;
158 }
159 return -EINVAL;
160}
161
162static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
163 loff_t * ppos)
164{
165 int free;
166 int nbytes;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!count) {
169 dac_audio_sync();
170 return 0;
171 }
172
173 free = buffer_begin - buffer_end;
174
175 if (free < 0)
176 free += BUFFER_SIZE;
177 if ((free == 0) && (empty))
178 free = BUFFER_SIZE;
179 if (count > free)
180 count = free;
181 if (buffer_begin > buffer_end) {
182 if (copy_from_user((void *)buffer_end, buf, count))
183 return -EFAULT;
184
185 buffer_end += count;
186 } else {
187 nbytes = data_buffer + BUFFER_SIZE - buffer_end;
188 if (nbytes > count) {
189 if (copy_from_user((void *)buffer_end, buf, count))
190 return -EFAULT;
191 buffer_end += count;
192 } else {
193 if (copy_from_user((void *)buffer_end, buf, nbytes))
194 return -EFAULT;
195 if (copy_from_user
196 ((void *)data_buffer, buf + nbytes, count - nbytes))
197 return -EFAULT;
198 buffer_end = data_buffer + count - nbytes;
199 }
200 }
201
202 if (empty) {
203 empty = 0;
204 dac_audio_start_timer();
205 }
206
207 return count;
208}
209
210static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
211 loff_t * ppos)
212{
213 return -EINVAL;
214}
215
216static int dac_audio_open(struct inode *inode, struct file *file)
217{
218 if (file->f_mode & FMODE_READ)
219 return -ENODEV;
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200220
221 lock_kernel();
222 if (in_use) {
223 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EBUSY;
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 in_use = 1;
228
229 dac_audio_start();
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200230 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
234static int dac_audio_release(struct inode *inode, struct file *file)
235{
236 dac_audio_sync();
237 dac_audio_stop();
238 in_use = 0;
239
240 return 0;
241}
242
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800243const struct file_operations dac_audio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .read = dac_audio_read,
245 .write = dac_audio_write,
246 .ioctl = dac_audio_ioctl,
247 .open = dac_audio_open,
248 .release = dac_audio_release,
249};
250
Magnus Damm639138a2009-05-15 12:07:17 +0900251static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!empty) {
254 sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
255 buffer_begin++;
256
257 if (buffer_begin == data_buffer + BUFFER_SIZE)
258 buffer_begin = data_buffer;
Magnus Damm639138a2009-05-15 12:07:17 +0900259 if (buffer_begin == buffer_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 empty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Magnus Damm639138a2009-05-15 12:07:17 +0900262
263 if (!empty)
264 hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
265
266 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static int __init dac_audio_init(void)
270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
272 printk(KERN_ERR "Cannot register dsp device");
273 return device_major;
274 }
275
276 in_use = 0;
277
Jesper Juhl4fa95ef2006-03-28 01:56:54 -0800278 data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (data_buffer == NULL)
280 return -ENOMEM;
281
282 dac_audio_reset();
283 rate = 8000;
284 dac_audio_set_rate();
285
Magnus Damm639138a2009-05-15 12:07:17 +0900286 /* Today: High Resolution Timer driven DAC playback.
287 * The timer callback gets called once per sample. Ouch.
288 *
289 * Future: A much better approach would be to use the
290 * SH7720 CMT+DMAC+DAC hardware combination like this:
291 * - Program sample rate using CMT0 or CMT1
292 * - Program DMAC to use CMT for timing and output to DAC
293 * - Play sound using DMAC, let CPU sleep.
294 * - While at it, rewrite this driver to use ALSA.
295 */
296
297 hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
298 hrtimer.function = sh_dac_audio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
303static void __exit dac_audio_exit(void)
304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 unregister_sound_dsp(device_major);
306 kfree((void *)data_buffer);
307}
308
309module_init(dac_audio_init);
310module_exit(dac_audio_exit);
311
312MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
313MODULE_DESCRIPTION("SH DAC sound driver");
314MODULE_LICENSE("GPL");