linux/drivers/staging/goldfish/goldfish_audio.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * drivers/misc/goldfish_audio.c
   4 *
   5 * Copyright (C) 2007 Google, Inc.
   6 * Copyright (C) 2012 Intel, Inc.
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/miscdevice.h>
  11#include <linux/fs.h>
  12#include <linux/platform_device.h>
  13#include <linux/types.h>
  14#include <linux/pci.h>
  15#include <linux/interrupt.h>
  16#include <linux/io.h>
  17#include <linux/sched.h>
  18#include <linux/dma-mapping.h>
  19#include <linux/uaccess.h>
  20#include <linux/slab.h>
  21#include <linux/goldfish.h>
  22#include <linux/acpi.h>
  23
  24MODULE_AUTHOR("Google, Inc.");
  25MODULE_DESCRIPTION("Android QEMU Audio Driver");
  26MODULE_LICENSE("GPL");
  27MODULE_VERSION("1.0");
  28
  29struct goldfish_audio {
  30        char __iomem *reg_base;
  31        int irq;
  32
  33        /* lock protects access to buffer_status and to device registers */
  34        spinlock_t lock;
  35        wait_queue_head_t wait;
  36
  37        char *buffer_virt;              /* combined buffer virtual address */
  38        unsigned long buffer_phys;      /* combined buffer physical address */
  39
  40        char *write_buffer1;            /* write buffer 1 virtual address */
  41        char *write_buffer2;            /* write buffer 2 virtual address */
  42        char *read_buffer;              /* read buffer virtual address */
  43        int buffer_status;
  44        int read_supported;     /* true if we have audio input support */
  45};
  46
  47/*
  48 *  We will allocate two read buffers and two write buffers.
  49 *  Having two read buffers facilitate stereo -> mono conversion.
  50 *  Having two write buffers facilitate interleaved IO.
  51 */
  52#define READ_BUFFER_SIZE        16384
  53#define WRITE_BUFFER_SIZE       16384
  54#define COMBINED_BUFFER_SIZE    ((2 * READ_BUFFER_SIZE) + \
  55                                        (2 * WRITE_BUFFER_SIZE))
  56
  57/*
  58 *  temporary variable used between goldfish_audio_probe() and
  59 *  goldfish_audio_open()
  60 */
  61static struct goldfish_audio *audio_data;
  62
  63enum {
  64        /* audio status register */
  65        AUDIO_INT_STATUS        = 0x00,
  66        /* set this to enable IRQ */
  67        AUDIO_INT_ENABLE        = 0x04,
  68        /* set these to specify buffer addresses */
  69        AUDIO_SET_WRITE_BUFFER_1 = 0x08,
  70        AUDIO_SET_WRITE_BUFFER_2 = 0x0C,
  71        /* set number of bytes in buffer to write */
  72        AUDIO_WRITE_BUFFER_1  = 0x10,
  73        AUDIO_WRITE_BUFFER_2  = 0x14,
  74        AUDIO_SET_WRITE_BUFFER_1_HIGH = 0x28,
  75        AUDIO_SET_WRITE_BUFFER_2_HIGH = 0x30,
  76
  77        /* true if audio input is supported */
  78        AUDIO_READ_SUPPORTED = 0x18,
  79        /* buffer to use for audio input */
  80        AUDIO_SET_READ_BUFFER = 0x1C,
  81        AUDIO_SET_READ_BUFFER_HIGH = 0x34,
  82
  83        /* driver writes number of bytes to read */
  84        AUDIO_START_READ  = 0x20,
  85
  86        /* number of bytes available in read buffer */
  87        AUDIO_READ_BUFFER_AVAILABLE  = 0x24,
  88
  89        /* AUDIO_INT_STATUS bits */
  90
  91        /* this bit set when it is safe to write more bytes to the buffer */
  92        AUDIO_INT_WRITE_BUFFER_1_EMPTY  = 1U << 0,
  93        AUDIO_INT_WRITE_BUFFER_2_EMPTY  = 1U << 1,
  94        AUDIO_INT_READ_BUFFER_FULL      = 1U << 2,
  95
  96        AUDIO_INT_MASK                  = AUDIO_INT_WRITE_BUFFER_1_EMPTY |
  97                                          AUDIO_INT_WRITE_BUFFER_2_EMPTY |
  98                                          AUDIO_INT_READ_BUFFER_FULL,
  99};
 100
 101static atomic_t open_count = ATOMIC_INIT(0);
 102
 103static unsigned int audio_read(const struct goldfish_audio *data, int addr)
 104{
 105        return readl(data->reg_base + addr);
 106}
 107
 108static void audio_write(const struct goldfish_audio *data,
 109                        int addr, unsigned int x)
 110{
 111        writel(x, data->reg_base + addr);
 112}
 113
 114static void audio_write64(const struct goldfish_audio *data,
 115                          int addr_lo, int addr_hi, unsigned int x)
 116{
 117        char __iomem *reg_base = data->reg_base;
 118
 119        gf_write_dma_addr(x, reg_base + addr_lo, reg_base + addr_hi);
 120}
 121
 122static ssize_t goldfish_audio_read(struct file *fp, char __user *buf,
 123                                   size_t count, loff_t *pos)
 124{
 125        struct goldfish_audio *data = fp->private_data;
 126        unsigned long irq_flags;
 127        int length;
 128        int result = 0;
 129
 130        if (!data->read_supported)
 131                return -ENODEV;
 132
 133        while (count > 0) {
 134                length = (count > READ_BUFFER_SIZE ? READ_BUFFER_SIZE : count);
 135                audio_write(data, AUDIO_START_READ, length);
 136
 137                wait_event_interruptible(data->wait, data->buffer_status &
 138                                         AUDIO_INT_READ_BUFFER_FULL);
 139
 140                spin_lock_irqsave(&data->lock, irq_flags);
 141                data->buffer_status &= ~AUDIO_INT_READ_BUFFER_FULL;
 142                spin_unlock_irqrestore(&data->lock, irq_flags);
 143
 144                length = audio_read(data, AUDIO_READ_BUFFER_AVAILABLE);
 145
 146                /* copy data to user space */
 147                if (copy_to_user(buf, data->read_buffer, length))
 148                        return -EFAULT;
 149
 150                result += length;
 151                buf += length;
 152                count -= length;
 153        }
 154        return result;
 155}
 156
 157static ssize_t goldfish_audio_write(struct file *fp, const char __user *buf,
 158                                    size_t count, loff_t *pos)
 159{
 160        struct goldfish_audio *data = fp->private_data;
 161        unsigned long irq_flags;
 162        ssize_t result = 0;
 163        char *kbuf;
 164
 165        while (count > 0) {
 166                ssize_t copy = count;
 167
 168                if (copy > WRITE_BUFFER_SIZE)
 169                        copy = WRITE_BUFFER_SIZE;
 170                wait_event_interruptible(data->wait, data->buffer_status &
 171                                        (AUDIO_INT_WRITE_BUFFER_1_EMPTY |
 172                                        AUDIO_INT_WRITE_BUFFER_2_EMPTY));
 173
 174                if ((data->buffer_status & AUDIO_INT_WRITE_BUFFER_1_EMPTY) != 0)
 175                        kbuf = data->write_buffer1;
 176                else
 177                        kbuf = data->write_buffer2;
 178
 179                /* copy from user space to the appropriate buffer */
 180                if (copy_from_user(kbuf, buf, copy)) {
 181                        result = -EFAULT;
 182                        break;
 183                }
 184
 185                spin_lock_irqsave(&data->lock, irq_flags);
 186                /*
 187                 *  clear the buffer empty flag, and signal the emulator
 188                 *  to start writing the buffer
 189                 */
 190                if (kbuf == data->write_buffer1) {
 191                        data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_1_EMPTY;
 192                        audio_write(data, AUDIO_WRITE_BUFFER_1, copy);
 193                } else {
 194                        data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
 195                        audio_write(data, AUDIO_WRITE_BUFFER_2, copy);
 196                }
 197                spin_unlock_irqrestore(&data->lock, irq_flags);
 198
 199                buf += copy;
 200                result += copy;
 201                count -= copy;
 202        }
 203        return result;
 204}
 205
 206static int goldfish_audio_open(struct inode *ip, struct file *fp)
 207{
 208        if (!audio_data)
 209                return -ENODEV;
 210
 211        if (atomic_inc_return(&open_count) == 1) {
 212                fp->private_data = audio_data;
 213                audio_data->buffer_status = (AUDIO_INT_WRITE_BUFFER_1_EMPTY |
 214                                             AUDIO_INT_WRITE_BUFFER_2_EMPTY);
 215                audio_write(audio_data, AUDIO_INT_ENABLE, AUDIO_INT_MASK);
 216                return 0;
 217        }
 218
 219        atomic_dec(&open_count);
 220        return -EBUSY;
 221}
 222
 223static int goldfish_audio_release(struct inode *ip, struct file *fp)
 224{
 225        atomic_dec(&open_count);
 226        /* FIXME: surely this is wrong for the multi-opened case */
 227        audio_write(audio_data, AUDIO_INT_ENABLE, 0);
 228        return 0;
 229}
 230
 231static long goldfish_audio_ioctl(struct file *fp, unsigned int cmd,
 232                                 unsigned long arg)
 233{
 234        /* temporary workaround, until we switch to the ALSA API */
 235        if (cmd == 315)
 236                return -1;
 237
 238        return 0;
 239}
 240
 241static irqreturn_t goldfish_audio_interrupt(int irq, void *dev_id)
 242{
 243        unsigned long irq_flags;
 244        struct goldfish_audio   *data = dev_id;
 245        u32 status;
 246
 247        spin_lock_irqsave(&data->lock, irq_flags);
 248
 249        /* read buffer status flags */
 250        status = audio_read(data, AUDIO_INT_STATUS);
 251        status &= AUDIO_INT_MASK;
 252        /*
 253         *  if buffers are newly empty, wake up blocked
 254         *  goldfish_audio_write() call
 255         */
 256        if (status) {
 257                data->buffer_status = status;
 258                wake_up(&data->wait);
 259        }
 260
 261        spin_unlock_irqrestore(&data->lock, irq_flags);
 262        return status ? IRQ_HANDLED : IRQ_NONE;
 263}
 264
 265/* file operations for /dev/eac */
 266static const struct file_operations goldfish_audio_fops = {
 267        .owner = THIS_MODULE,
 268        .read = goldfish_audio_read,
 269        .write = goldfish_audio_write,
 270        .open = goldfish_audio_open,
 271        .release = goldfish_audio_release,
 272        .unlocked_ioctl = goldfish_audio_ioctl,
 273};
 274
 275static struct miscdevice goldfish_audio_device = {
 276        .minor = MISC_DYNAMIC_MINOR,
 277        .name = "eac",
 278        .fops = &goldfish_audio_fops,
 279};
 280
 281static int goldfish_audio_probe(struct platform_device *pdev)
 282{
 283        int ret;
 284        struct resource *r;
 285        struct goldfish_audio *data;
 286        dma_addr_t buf_addr;
 287
 288        data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
 289        if (!data)
 290                return -ENOMEM;
 291        spin_lock_init(&data->lock);
 292        init_waitqueue_head(&data->wait);
 293        platform_set_drvdata(pdev, data);
 294
 295        r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 296        if (!r) {
 297                dev_err(&pdev->dev, "platform_get_resource failed\n");
 298                return -ENODEV;
 299        }
 300        data->reg_base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
 301        if (!data->reg_base)
 302                return -ENOMEM;
 303
 304        data->irq = platform_get_irq(pdev, 0);
 305        if (data->irq < 0)
 306                return -ENODEV;
 307        data->buffer_virt = dmam_alloc_coherent(&pdev->dev,
 308                                                COMBINED_BUFFER_SIZE,
 309                                                &buf_addr, GFP_KERNEL);
 310        if (!data->buffer_virt) {
 311                dev_err(&pdev->dev, "allocate buffer failed\n");
 312                return -ENOMEM;
 313        }
 314        data->buffer_phys = buf_addr;
 315        data->write_buffer1 = data->buffer_virt;
 316        data->write_buffer2 = data->buffer_virt + WRITE_BUFFER_SIZE;
 317        data->read_buffer = data->buffer_virt + 2 * WRITE_BUFFER_SIZE;
 318
 319        ret = devm_request_irq(&pdev->dev, data->irq, goldfish_audio_interrupt,
 320                               IRQF_SHARED, pdev->name, data);
 321        if (ret) {
 322                dev_err(&pdev->dev, "request_irq failed\n");
 323                return ret;
 324        }
 325
 326        ret = misc_register(&goldfish_audio_device);
 327        if (ret) {
 328                dev_err(&pdev->dev,
 329                        "misc_register returned %d in goldfish_audio_init\n",
 330                                                                ret);
 331                return ret;
 332        }
 333
 334        audio_write64(data, AUDIO_SET_WRITE_BUFFER_1,
 335                      AUDIO_SET_WRITE_BUFFER_1_HIGH, buf_addr);
 336        buf_addr += WRITE_BUFFER_SIZE;
 337
 338        audio_write64(data, AUDIO_SET_WRITE_BUFFER_2,
 339                      AUDIO_SET_WRITE_BUFFER_2_HIGH, buf_addr);
 340
 341        buf_addr += WRITE_BUFFER_SIZE;
 342
 343        data->read_supported = audio_read(data, AUDIO_READ_SUPPORTED);
 344        if (data->read_supported)
 345                audio_write64(data, AUDIO_SET_READ_BUFFER,
 346                              AUDIO_SET_READ_BUFFER_HIGH, buf_addr);
 347
 348        audio_data = data;
 349        return 0;
 350}
 351
 352static int goldfish_audio_remove(struct platform_device *pdev)
 353{
 354        misc_deregister(&goldfish_audio_device);
 355        audio_data = NULL;
 356        return 0;
 357}
 358
 359static const struct of_device_id goldfish_audio_of_match[] = {
 360        { .compatible = "google,goldfish-audio", },
 361        {},
 362};
 363MODULE_DEVICE_TABLE(of, goldfish_audio_of_match);
 364
 365#ifdef CONFIG_ACPI
 366static const struct acpi_device_id goldfish_audio_acpi_match[] = {
 367        { "GFSH0005", 0 },
 368        { },
 369};
 370MODULE_DEVICE_TABLE(acpi, goldfish_audio_acpi_match);
 371#endif
 372
 373static struct platform_driver goldfish_audio_driver = {
 374        .probe          = goldfish_audio_probe,
 375        .remove         = goldfish_audio_remove,
 376        .driver = {
 377                .name = "goldfish_audio",
 378                .of_match_table = goldfish_audio_of_match,
 379                .acpi_match_table = ACPI_PTR(goldfish_audio_acpi_match),
 380        }
 381};
 382
 383module_platform_driver(goldfish_audio_driver);
 384