upgrade iop32x to 2.6.30
[openwrt.git] / target / linux / goldfish / patches-2.6.30 / 0120--ARM-goldfish-Add-audio-driver-for-goldfish.patch
1 From aefe5257bf3ed9bf950ad962a63e5315566c2f97 Mon Sep 17 00:00:00 2001
2 From: Mike Lockwood <lockwood@google.com>
3 Date: Mon, 23 Jul 2007 09:31:54 -0400
4 Subject: [PATCH 120/134] [ARM] goldfish: Add audio driver for goldfish.
5
6 Signed-off-by: Mike A. Chan <mikechan@google.com>
7 ---
8 arch/arm/mach-goldfish/Makefile | 2 +-
9 arch/arm/mach-goldfish/audio.c | 379 +++++++++++++++++++++++++++++++++++++++
10 2 files changed, 380 insertions(+), 1 deletions(-)
11 create mode 100644 arch/arm/mach-goldfish/audio.c
12
13 --- a/arch/arm/mach-goldfish/Makefile
14 +++ b/arch/arm/mach-goldfish/Makefile
15 @@ -4,6 +4,6 @@
16
17 # Object file lists.
18
19 -obj-y := pdev_bus.o timer.o
20 +obj-y := pdev_bus.o timer.o audio.o
21 obj-$(CONFIG_MACH_GOLDFISH) += board-goldfish.o
22
23 --- /dev/null
24 +++ b/arch/arm/mach-goldfish/audio.c
25 @@ -0,0 +1,379 @@
26 +/* arch/arm/mach-goldfish/audio.c
27 +**
28 +** Copyright (C) 2007 Google, Inc.
29 +**
30 +** This software is licensed under the terms of the GNU General Public
31 +** License version 2, as published by the Free Software Foundation, and
32 +** may be copied, distributed, and modified under those terms.
33 +**
34 +** This program is distributed in the hope that it will be useful,
35 +** but WITHOUT ANY WARRANTY; without even the implied warranty of
36 +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 +** GNU General Public License for more details.
38 +**
39 +*/
40 +
41 +#include <linux/module.h>
42 +#include <linux/miscdevice.h>
43 +#include <linux/fs.h>
44 +#include <linux/platform_device.h>
45 +
46 +#include <linux/types.h>
47 +#include <linux/pci.h>
48 +#include <linux/interrupt.h>
49 +
50 +#include <asm/types.h>
51 +#include <asm/io.h>
52 +#include <asm/uaccess.h>
53 +
54 +
55 +MODULE_AUTHOR("Google, Inc.");
56 +MODULE_DESCRIPTION("Android QEMU Audio Driver");
57 +MODULE_LICENSE("GPL");
58 +MODULE_VERSION("1.0");
59 +
60 +struct goldfish_audio {
61 + uint32_t reg_base;
62 + int irq;
63 + spinlock_t lock;
64 + wait_queue_head_t wait;
65 +
66 + char __iomem *buffer_virt; /* combined buffer virtual address */
67 + unsigned long buffer_phys; /* combined buffer physical address */
68 +
69 + char __iomem *write_buffer1; /* write buffer 1 virtual address */
70 + char __iomem *write_buffer2; /* write buffer 2 virtual address */
71 + char __iomem *read_buffer; /* read buffer virtual address */
72 + int buffer_status;
73 + int read_supported; /* true if we have audio input support */
74 +};
75 +
76 +/* We will allocate two read buffers and two write buffers.
77 + Having two read buffers facilitate stereo -> mono conversion.
78 + Having two write buffers facilitate interleaved IO.
79 +*/
80 +#define READ_BUFFER_SIZE 16384
81 +#define WRITE_BUFFER_SIZE 16384
82 +#define COMBINED_BUFFER_SIZE ((2 * READ_BUFFER_SIZE) + (2 * WRITE_BUFFER_SIZE))
83 +
84 +#define GOLDFISH_AUDIO_READ(data, addr) (readl(data->reg_base + addr))
85 +#define GOLDFISH_AUDIO_WRITE(data, addr, x) (writel(x, data->reg_base + addr))
86 +
87 +/* temporary variable used between goldfish_audio_probe() and goldfish_audio_open() */
88 +static struct goldfish_audio* audio_data;
89 +
90 +enum {
91 + /* audio status register */
92 + AUDIO_INT_STATUS = 0x00,
93 + /* set this to enable IRQ */
94 + AUDIO_INT_ENABLE = 0x04,
95 + /* set these to specify buffer addresses */
96 + AUDIO_SET_WRITE_BUFFER_1 = 0x08,
97 + AUDIO_SET_WRITE_BUFFER_2 = 0x0C,
98 + /* set number of bytes in buffer to write */
99 + AUDIO_WRITE_BUFFER_1 = 0x10,
100 + AUDIO_WRITE_BUFFER_2 = 0x14,
101 +
102 + /* true if audio input is supported */
103 + AUDIO_READ_SUPPORTED = 0x18,
104 + /* buffer to use for audio input */
105 + AUDIO_SET_READ_BUFFER = 0x1C,
106 +
107 + /* driver writes number of bytes to read */
108 + AUDIO_START_READ = 0x20,
109 +
110 + /* number of bytes available in read buffer */
111 + AUDIO_READ_BUFFER_AVAILABLE = 0x24,
112 +
113 + /* AUDIO_INT_STATUS bits */
114 +
115 + /* this bit set when it is safe to write more bytes to the buffer */
116 + AUDIO_INT_WRITE_BUFFER_1_EMPTY = 1U << 0,
117 + AUDIO_INT_WRITE_BUFFER_2_EMPTY = 1U << 1,
118 + AUDIO_INT_READ_BUFFER_FULL = 1U << 2,
119 +
120 + AUDIO_INT_MASK = AUDIO_INT_WRITE_BUFFER_1_EMPTY |
121 + AUDIO_INT_WRITE_BUFFER_2_EMPTY |
122 + AUDIO_INT_READ_BUFFER_FULL,
123 +};
124 +
125 +
126 +static atomic_t open_count = ATOMIC_INIT(0);
127 +
128 +
129 +static ssize_t goldfish_audio_read(struct file *fp, char __user *buf,
130 + size_t count, loff_t *pos)
131 +{
132 + struct goldfish_audio* data = fp->private_data;
133 + int length;
134 + int result = 0;
135 +
136 + if (!data->read_supported)
137 + return -ENODEV;
138 +
139 + while (count > 0) {
140 + length = (count > READ_BUFFER_SIZE ? READ_BUFFER_SIZE : count);
141 + GOLDFISH_AUDIO_WRITE(data, AUDIO_START_READ, length);
142 +
143 + wait_event_interruptible(data->wait, (data->buffer_status & AUDIO_INT_READ_BUFFER_FULL));
144 +
145 + length = GOLDFISH_AUDIO_READ(data, AUDIO_READ_BUFFER_AVAILABLE);
146 +
147 + /* copy data to user space */
148 + if (copy_to_user(buf, data->read_buffer, length))
149 + {
150 + printk("copy_from_user failed!\n");
151 + return -EFAULT;
152 + }
153 +
154 + result += length;
155 + buf += length;
156 + count -= length;
157 + }
158 +
159 + return result;
160 +}
161 +
162 +static ssize_t goldfish_audio_write(struct file *fp, const char __user *buf,
163 + size_t count, loff_t *pos)
164 +{
165 + struct goldfish_audio* data = fp->private_data;
166 + unsigned long irq_flags;
167 + ssize_t result = 0;
168 + char __iomem *kbuf;
169 +
170 + while (count > 0)
171 + {
172 + ssize_t copy = count;
173 + if (copy > WRITE_BUFFER_SIZE)
174 + copy = WRITE_BUFFER_SIZE;
175 + wait_event_interruptible(data->wait,
176 + (data->buffer_status & (AUDIO_INT_WRITE_BUFFER_1_EMPTY | AUDIO_INT_WRITE_BUFFER_2_EMPTY)));
177 +
178 + if ((data->buffer_status & AUDIO_INT_WRITE_BUFFER_1_EMPTY) != 0) {
179 + kbuf = data->write_buffer1;
180 + } else {
181 + kbuf = data->write_buffer2;
182 + }
183 +
184 + /* copy from user space to the appropriate buffer */
185 + if (copy_from_user(kbuf, buf, copy))
186 + {
187 + printk("copy_from_user failed!\n");
188 + result = -EFAULT;
189 + break;
190 + }
191 + else
192 + {
193 + spin_lock_irqsave(&data->lock, irq_flags);
194 +
195 + /* clear the buffer empty flag, and signal the emulator to start writing the buffer */
196 + if (kbuf == data->write_buffer1) {
197 + data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_1_EMPTY;
198 + GOLDFISH_AUDIO_WRITE(data, AUDIO_WRITE_BUFFER_1, copy);
199 + } else {
200 + data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
201 + GOLDFISH_AUDIO_WRITE(data, AUDIO_WRITE_BUFFER_2, copy);
202 + }
203 +
204 + spin_unlock_irqrestore(&data->lock, irq_flags);
205 + }
206 +
207 + buf += copy;
208 + result += copy;
209 + count -= copy;
210 + }
211 +
212 + return result;
213 +}
214 +
215 +static int goldfish_audio_open(struct inode *ip, struct file *fp)
216 +{
217 + if (!audio_data)
218 + return -ENODEV;
219 +
220 + if (atomic_inc_return(&open_count) == 1)
221 + {
222 + fp->private_data = audio_data;
223 + audio_data->buffer_status = (AUDIO_INT_WRITE_BUFFER_1_EMPTY | AUDIO_INT_WRITE_BUFFER_2_EMPTY);
224 + GOLDFISH_AUDIO_WRITE(audio_data, AUDIO_INT_ENABLE, AUDIO_INT_MASK);
225 + return 0;
226 + }
227 + else
228 + {
229 + atomic_dec(&open_count);
230 + return -EBUSY;
231 + }
232 +}
233 +
234 +static int goldfish_audio_release(struct inode *ip, struct file* fp)
235 +{
236 + atomic_dec(&open_count);
237 + GOLDFISH_AUDIO_WRITE(audio_data, AUDIO_INT_ENABLE, 0);
238 + return 0;
239 +}
240 +
241 +static int goldfish_audio_ioctl(struct inode* ip, struct file* fp, unsigned int cmd, unsigned long arg)
242 +{
243 + /* temporary workaround, until we switch to the ALSA API */
244 + if (cmd == 315)
245 + return -1;
246 + else
247 + return 0;
248 +}
249 +
250 +static irqreturn_t
251 +goldfish_audio_interrupt(int irq, void *dev_id)
252 +{
253 + unsigned long irq_flags;
254 + struct goldfish_audio *data = dev_id;
255 + uint32_t status;
256 +
257 + spin_lock_irqsave(&data->lock, irq_flags);
258 +
259 + /* read buffer status flags */
260 + status = GOLDFISH_AUDIO_READ(data, AUDIO_INT_STATUS);
261 + status &= AUDIO_INT_MASK;
262 + /* if buffers are newly empty, wake up blocked goldfish_audio_write() call */
263 + if(status) {
264 + data->buffer_status = status;
265 + wake_up(&data->wait);
266 + }
267 +
268 + spin_unlock_irqrestore(&data->lock, irq_flags);
269 + return status ? IRQ_HANDLED : IRQ_NONE;
270 +}
271 +
272 +/* file operations for /dev/eac */
273 +static struct file_operations goldfish_audio_fops = {
274 + .owner = THIS_MODULE,
275 + .read = goldfish_audio_read,
276 + .write = goldfish_audio_write,
277 + .open = goldfish_audio_open,
278 + .release = goldfish_audio_release,
279 + .ioctl = goldfish_audio_ioctl,
280 +
281 +};
282 +
283 +static struct miscdevice goldfish_audio_device = {
284 + .minor = MISC_DYNAMIC_MINOR,
285 + .name = "eac",
286 + .fops = &goldfish_audio_fops,
287 +};
288 +
289 +static int goldfish_audio_probe(struct platform_device *pdev)
290 +{
291 + int ret;
292 + struct resource *r;
293 + struct goldfish_audio *data;
294 + dma_addr_t buf_addr;
295 +
296 +printk("goldfish_audio_probe\n");
297 + data = kzalloc(sizeof(*data), GFP_KERNEL);
298 + if(data == NULL) {
299 + ret = -ENOMEM;
300 + goto err_data_alloc_failed;
301 + }
302 + spin_lock_init(&data->lock);
303 + init_waitqueue_head(&data->wait);
304 + platform_set_drvdata(pdev, data);
305 +
306 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
307 + if(r == NULL) {
308 + printk("platform_get_resource failed\n");
309 + ret = -ENODEV;
310 + goto err_no_io_base;
311 + }
312 + data->reg_base = IO_ADDRESS(r->start - IO_START);
313 +
314 + data->irq = platform_get_irq(pdev, 0);
315 + if(data->irq < 0) {
316 + printk("platform_get_irq failed\n");
317 + ret = -ENODEV;
318 + goto err_no_irq;
319 + }
320 +
321 + data->buffer_virt = dma_alloc_writecombine(&pdev->dev, COMBINED_BUFFER_SIZE,
322 + &buf_addr, GFP_KERNEL);
323 + if(data->buffer_virt == 0) {
324 + ret = -ENOMEM;
325 + goto err_alloc_write_buffer_failed;
326 + }
327 + data->buffer_phys = buf_addr;
328 + data->write_buffer1 = data->buffer_virt;
329 + data->write_buffer2 = data->buffer_virt + WRITE_BUFFER_SIZE;
330 + data->read_buffer = data->buffer_virt + 2 * WRITE_BUFFER_SIZE;
331 +
332 + ret = request_irq(data->irq, goldfish_audio_interrupt, IRQF_SHARED, pdev->name, data);
333 + if(ret)
334 + goto err_request_irq_failed;
335 +
336 + if((ret = misc_register(&goldfish_audio_device)))
337 + {
338 + printk("misc_register returned %d in goldfish_audio_init\n", ret);
339 + goto err_misc_register_failed;
340 + }
341 +
342 +
343 + GOLDFISH_AUDIO_WRITE(data, AUDIO_SET_WRITE_BUFFER_1, buf_addr);
344 + GOLDFISH_AUDIO_WRITE(data, AUDIO_SET_WRITE_BUFFER_2, buf_addr + WRITE_BUFFER_SIZE);
345 +
346 + data->read_supported = GOLDFISH_AUDIO_READ(data, AUDIO_READ_SUPPORTED);
347 + if (data->read_supported)
348 + GOLDFISH_AUDIO_WRITE(data, AUDIO_SET_READ_BUFFER, buf_addr + 2 * WRITE_BUFFER_SIZE);
349 +
350 + audio_data = data;
351 + return 0;
352 +
353 +err_misc_register_failed:
354 +err_request_irq_failed:
355 + dma_free_writecombine(&pdev->dev, COMBINED_BUFFER_SIZE, data->buffer_virt, data->buffer_phys);
356 +err_alloc_write_buffer_failed:
357 +err_no_irq:
358 +err_no_io_base:
359 + kfree(data);
360 +err_data_alloc_failed:
361 + return ret;
362 +}
363 +
364 +static int goldfish_audio_remove(struct platform_device *pdev)
365 +{
366 + struct goldfish_audio *data = platform_get_drvdata(pdev);
367 +
368 + misc_deregister(&goldfish_audio_device);
369 + free_irq(data->irq, data);
370 + dma_free_writecombine(&pdev->dev, COMBINED_BUFFER_SIZE, data->buffer_virt, data->buffer_phys);
371 + kfree(data);
372 + audio_data = NULL;
373 + return 0;
374 +}
375 +
376 +static struct platform_driver goldfish_audio_driver = {
377 + .probe = goldfish_audio_probe,
378 + .remove = goldfish_audio_remove,
379 + .driver = {
380 + .name = "goldfish_audio"
381 + }
382 +};
383 +
384 +static int __init goldfish_audio_init(void)
385 +{
386 + int ret;
387 +
388 + ret = platform_driver_register(&goldfish_audio_driver);
389 + if (ret < 0)
390 + {
391 + printk("platform_driver_register returned %d\n", ret);
392 + return ret;
393 + }
394 +
395 + return ret;
396 +}
397 +
398 +static void __exit goldfish_audio_exit(void)
399 +{
400 + platform_driver_unregister(&goldfish_audio_driver);
401 +}
402 +
403 +module_init(goldfish_audio_init);
404 +module_exit(goldfish_audio_exit);
This page took 0.063058 seconds and 5 git commands to generate.