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.
6 Signed-off-by: Mike A. Chan <mikechan@google.com>
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
13 --- a/arch/arm/mach-goldfish/Makefile
14 +++ b/arch/arm/mach-goldfish/Makefile
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
24 +++ b/arch/arm/mach-goldfish/audio.c
26 +/* arch/arm/mach-goldfish/audio.c
28 +** Copyright (C) 2007 Google, Inc.
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.
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.
41 +#include <linux/module.h>
42 +#include <linux/miscdevice.h>
43 +#include <linux/fs.h>
44 +#include <linux/platform_device.h>
46 +#include <linux/types.h>
47 +#include <linux/pci.h>
48 +#include <linux/interrupt.h>
50 +#include <asm/types.h>
52 +#include <asm/uaccess.h>
55 +MODULE_AUTHOR("Google, Inc.");
56 +MODULE_DESCRIPTION("Android QEMU Audio Driver");
57 +MODULE_LICENSE("GPL");
58 +MODULE_VERSION("1.0");
60 +struct goldfish_audio {
64 + wait_queue_head_t wait;
66 + char __iomem *buffer_virt; /* combined buffer virtual address */
67 + unsigned long buffer_phys; /* combined buffer physical address */
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 */
73 + int read_supported; /* true if we have audio input support */
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.
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))
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))
87 +/* temporary variable used between goldfish_audio_probe() and goldfish_audio_open() */
88 +static struct goldfish_audio* audio_data;
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,
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,
107 + /* driver writes number of bytes to read */
108 + AUDIO_START_READ = 0x20,
110 + /* number of bytes available in read buffer */
111 + AUDIO_READ_BUFFER_AVAILABLE = 0x24,
113 + /* AUDIO_INT_STATUS bits */
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,
120 + AUDIO_INT_MASK = AUDIO_INT_WRITE_BUFFER_1_EMPTY |
121 + AUDIO_INT_WRITE_BUFFER_2_EMPTY |
122 + AUDIO_INT_READ_BUFFER_FULL,
126 +static atomic_t open_count = ATOMIC_INIT(0);
129 +static ssize_t goldfish_audio_read(struct file *fp, char __user *buf,
130 + size_t count, loff_t *pos)
132 + struct goldfish_audio* data = fp->private_data;
136 + if (!data->read_supported)
139 + while (count > 0) {
140 + length = (count > READ_BUFFER_SIZE ? READ_BUFFER_SIZE : count);
141 + GOLDFISH_AUDIO_WRITE(data, AUDIO_START_READ, length);
143 + wait_event_interruptible(data->wait, (data->buffer_status & AUDIO_INT_READ_BUFFER_FULL));
145 + length = GOLDFISH_AUDIO_READ(data, AUDIO_READ_BUFFER_AVAILABLE);
147 + /* copy data to user space */
148 + if (copy_to_user(buf, data->read_buffer, length))
150 + printk("copy_from_user failed!\n");
162 +static ssize_t goldfish_audio_write(struct file *fp, const char __user *buf,
163 + size_t count, loff_t *pos)
165 + struct goldfish_audio* data = fp->private_data;
166 + unsigned long irq_flags;
167 + ssize_t result = 0;
168 + char __iomem *kbuf;
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)));
178 + if ((data->buffer_status & AUDIO_INT_WRITE_BUFFER_1_EMPTY) != 0) {
179 + kbuf = data->write_buffer1;
181 + kbuf = data->write_buffer2;
184 + /* copy from user space to the appropriate buffer */
185 + if (copy_from_user(kbuf, buf, copy))
187 + printk("copy_from_user failed!\n");
193 + spin_lock_irqsave(&data->lock, irq_flags);
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);
200 + data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
201 + GOLDFISH_AUDIO_WRITE(data, AUDIO_WRITE_BUFFER_2, copy);
204 + spin_unlock_irqrestore(&data->lock, irq_flags);
215 +static int goldfish_audio_open(struct inode *ip, struct file *fp)
220 + if (atomic_inc_return(&open_count) == 1)
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);
229 + atomic_dec(&open_count);
234 +static int goldfish_audio_release(struct inode *ip, struct file* fp)
236 + atomic_dec(&open_count);
237 + GOLDFISH_AUDIO_WRITE(audio_data, AUDIO_INT_ENABLE, 0);
241 +static int goldfish_audio_ioctl(struct inode* ip, struct file* fp, unsigned int cmd, unsigned long arg)
243 + /* temporary workaround, until we switch to the ALSA API */
251 +goldfish_audio_interrupt(int irq, void *dev_id)
253 + unsigned long irq_flags;
254 + struct goldfish_audio *data = dev_id;
257 + spin_lock_irqsave(&data->lock, irq_flags);
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 */
264 + data->buffer_status = status;
265 + wake_up(&data->wait);
268 + spin_unlock_irqrestore(&data->lock, irq_flags);
269 + return status ? IRQ_HANDLED : IRQ_NONE;
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,
283 +static struct miscdevice goldfish_audio_device = {
284 + .minor = MISC_DYNAMIC_MINOR,
286 + .fops = &goldfish_audio_fops,
289 +static int goldfish_audio_probe(struct platform_device *pdev)
292 + struct resource *r;
293 + struct goldfish_audio *data;
294 + dma_addr_t buf_addr;
296 +printk("goldfish_audio_probe\n");
297 + data = kzalloc(sizeof(*data), GFP_KERNEL);
300 + goto err_data_alloc_failed;
302 + spin_lock_init(&data->lock);
303 + init_waitqueue_head(&data->wait);
304 + platform_set_drvdata(pdev, data);
306 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
308 + printk("platform_get_resource failed\n");
310 + goto err_no_io_base;
312 + data->reg_base = IO_ADDRESS(r->start - IO_START);
314 + data->irq = platform_get_irq(pdev, 0);
315 + if(data->irq < 0) {
316 + printk("platform_get_irq failed\n");
321 + data->buffer_virt = dma_alloc_writecombine(&pdev->dev, COMBINED_BUFFER_SIZE,
322 + &buf_addr, GFP_KERNEL);
323 + if(data->buffer_virt == 0) {
325 + goto err_alloc_write_buffer_failed;
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;
332 + ret = request_irq(data->irq, goldfish_audio_interrupt, IRQF_SHARED, pdev->name, data);
334 + goto err_request_irq_failed;
336 + if((ret = misc_register(&goldfish_audio_device)))
338 + printk("misc_register returned %d in goldfish_audio_init\n", ret);
339 + goto err_misc_register_failed;
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);
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);
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:
360 +err_data_alloc_failed:
364 +static int goldfish_audio_remove(struct platform_device *pdev)
366 + struct goldfish_audio *data = platform_get_drvdata(pdev);
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);
376 +static struct platform_driver goldfish_audio_driver = {
377 + .probe = goldfish_audio_probe,
378 + .remove = goldfish_audio_remove,
380 + .name = "goldfish_audio"
384 +static int __init goldfish_audio_init(void)
388 + ret = platform_driver_register(&goldfish_audio_driver);
391 + printk("platform_driver_register returned %d\n", ret);
398 +static void __exit goldfish_audio_exit(void)
400 + platform_driver_unregister(&goldfish_audio_driver);
403 +module_init(goldfish_audio_init);
404 +module_exit(goldfish_audio_exit);