Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-block.git] / drivers / staging / goldfish / goldfish_audio.c
CommitLineData
08925742 1// SPDX-License-Identifier: GPL-2.0
ad28497c
SB
2/*
3 * drivers/misc/goldfish_audio.c
2e82b83d
AC
4 *
5 * Copyright (C) 2007 Google, Inc.
6 * Copyright (C) 2012 Intel, Inc.
2e82b83d
AC
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>
45321507 20#include <linux/slab.h>
f6279717 21#include <linux/goldfish.h>
06e526f4 22#include <linux/acpi.h>
2e82b83d
AC
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;
24daa451 32
9b61f085 33 /* lock protects access to buffer_status and to device registers */
2e82b83d
AC
34 spinlock_t lock;
35 wait_queue_head_t wait;
36
82fdb8dd 37 char *buffer_virt; /* combined buffer virtual address */
683a060a 38 unsigned long buffer_phys; /* combined buffer physical address */
2e82b83d 39
82fdb8dd
RRD
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 */
2e82b83d 43 int buffer_status;
683a060a 44 int read_supported; /* true if we have audio input support */
2e82b83d
AC
45};
46
ad28497c
SB
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 */
683a060a
RK
52#define READ_BUFFER_SIZE 16384
53#define WRITE_BUFFER_SIZE 16384
54#define COMBINED_BUFFER_SIZE ((2 * READ_BUFFER_SIZE) + \
2e82b83d
AC
55 (2 * WRITE_BUFFER_SIZE))
56
ad28497c
SB
57/*
58 * temporary variable used between goldfish_audio_probe() and
59 * goldfish_audio_open()
60 */
2e82b83d
AC
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,
c3c1ba66
JT
74 AUDIO_SET_WRITE_BUFFER_1_HIGH = 0x28,
75 AUDIO_SET_WRITE_BUFFER_2_HIGH = 0x30,
2e82b83d
AC
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,
c3c1ba66 81 AUDIO_SET_READ_BUFFER_HIGH = 0x34,
2e82b83d
AC
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,
683a060a 94 AUDIO_INT_READ_BUFFER_FULL = 1U << 2,
2e82b83d 95
683a060a 96 AUDIO_INT_MASK = AUDIO_INT_WRITE_BUFFER_1_EMPTY |
2e82b83d
AC
97 AUDIO_INT_WRITE_BUFFER_2_EMPTY |
98 AUDIO_INT_READ_BUFFER_FULL,
99};
100
2e82b83d
AC
101static atomic_t open_count = ATOMIC_INIT(0);
102
a75647d1
RK
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
2e82b83d 122static ssize_t goldfish_audio_read(struct file *fp, char __user *buf,
8f52e264 123 size_t count, loff_t *pos)
2e82b83d
AC
124{
125 struct goldfish_audio *data = fp->private_data;
3053339b 126 unsigned long irq_flags;
2e82b83d
AC
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);
a75647d1 135 audio_write(data, AUDIO_START_READ, length);
2e82b83d 136
d7d3e898
AM
137 wait_event_interruptible(data->wait, data->buffer_status &
138 AUDIO_INT_READ_BUFFER_FULL);
2e82b83d 139
3053339b
JL
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
a75647d1 144 length = audio_read(data, AUDIO_READ_BUFFER_AVAILABLE);
2e82b83d
AC
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,
8f52e264 158 size_t count, loff_t *pos)
2e82b83d
AC
159{
160 struct goldfish_audio *data = fp->private_data;
161 unsigned long irq_flags;
162 ssize_t result = 0;
82fdb8dd 163 char *kbuf;
2e82b83d
AC
164
165 while (count > 0) {
166 ssize_t copy = count;
ef323812 167
2e82b83d
AC
168 if (copy > WRITE_BUFFER_SIZE)
169 copy = WRITE_BUFFER_SIZE;
d7d3e898 170 wait_event_interruptible(data->wait, data->buffer_status &
2e82b83d 171 (AUDIO_INT_WRITE_BUFFER_1_EMPTY |
d7d3e898 172 AUDIO_INT_WRITE_BUFFER_2_EMPTY));
2e82b83d
AC
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);
ad28497c
SB
186 /*
187 * clear the buffer empty flag, and signal the emulator
188 * to start writing the buffer
189 */
2e82b83d
AC
190 if (kbuf == data->write_buffer1) {
191 data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_1_EMPTY;
a75647d1 192 audio_write(data, AUDIO_WRITE_BUFFER_1, copy);
2e82b83d
AC
193 } else {
194 data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
a75647d1 195 audio_write(data, AUDIO_WRITE_BUFFER_2, copy);
2e82b83d
AC
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);
a75647d1 215 audio_write(audio_data, AUDIO_INT_ENABLE, AUDIO_INT_MASK);
2e82b83d 216 return 0;
2e82b83d 217 }
92271551
HT
218
219 atomic_dec(&open_count);
220 return -EBUSY;
2e82b83d
AC
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 */
a75647d1 227 audio_write(audio_data, AUDIO_INT_ENABLE, 0);
2e82b83d
AC
228 return 0;
229}
230
231static long goldfish_audio_ioctl(struct file *fp, unsigned int cmd,
8f52e264 232 unsigned long arg)
2e82b83d
AC
233{
234 /* temporary workaround, until we switch to the ALSA API */
235 if (cmd == 315)
236 return -1;
92271551
HT
237
238 return 0;
2e82b83d
AC
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 */
a75647d1 250 status = audio_read(data, AUDIO_INT_STATUS);
2e82b83d 251 status &= AUDIO_INT_MASK;
ad28497c
SB
252 /*
253 * if buffers are newly empty, wake up blocked
254 * goldfish_audio_write() call
255 */
2e82b83d
AC
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
85f28332 288 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
6e3f3bb8 289 if (!data)
85f28332 290 return -ENOMEM;
2e82b83d
AC
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);
269ad6e0 296 if (!r) {
2e82b83d 297 dev_err(&pdev->dev, "platform_get_resource failed\n");
85f28332 298 return -ENODEV;
2e82b83d 299 }
85f28332 300 data->reg_base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
269ad6e0 301 if (!data->reg_base)
85f28332 302 return -ENOMEM;
2e82b83d
AC
303
304 data->irq = platform_get_irq(pdev, 0);
305 if (data->irq < 0) {
306 dev_err(&pdev->dev, "platform_get_irq failed\n");
85f28332 307 return -ENODEV;
2e82b83d 308 }
85f28332 309 data->buffer_virt = dmam_alloc_coherent(&pdev->dev,
eee222db
RK
310 COMBINED_BUFFER_SIZE,
311 &buf_addr, GFP_KERNEL);
269ad6e0 312 if (!data->buffer_virt) {
2e82b83d 313 dev_err(&pdev->dev, "allocate buffer failed\n");
85f28332 314 return -ENOMEM;
2e82b83d
AC
315 }
316 data->buffer_phys = buf_addr;
317 data->write_buffer1 = data->buffer_virt;
318 data->write_buffer2 = data->buffer_virt + WRITE_BUFFER_SIZE;
319 data->read_buffer = data->buffer_virt + 2 * WRITE_BUFFER_SIZE;
320
85f28332 321 ret = devm_request_irq(&pdev->dev, data->irq, goldfish_audio_interrupt,
8f52e264 322 IRQF_SHARED, pdev->name, data);
2e82b83d
AC
323 if (ret) {
324 dev_err(&pdev->dev, "request_irq failed\n");
85f28332 325 return ret;
2e82b83d
AC
326 }
327
328 ret = misc_register(&goldfish_audio_device);
329 if (ret) {
330 dev_err(&pdev->dev,
331 "misc_register returned %d in goldfish_audio_init\n",
332 ret);
85f28332 333 return ret;
2e82b83d
AC
334 }
335
a75647d1 336 audio_write64(data, AUDIO_SET_WRITE_BUFFER_1,
8f52e264 337 AUDIO_SET_WRITE_BUFFER_1_HIGH, buf_addr);
f6279717 338 buf_addr += WRITE_BUFFER_SIZE;
c3c1ba66 339
a75647d1 340 audio_write64(data, AUDIO_SET_WRITE_BUFFER_2,
8f52e264 341 AUDIO_SET_WRITE_BUFFER_2_HIGH, buf_addr);
c3c1ba66 342
f6279717 343 buf_addr += WRITE_BUFFER_SIZE;
2e82b83d 344
a75647d1 345 data->read_supported = audio_read(data, AUDIO_READ_SUPPORTED);
2e82b83d 346 if (data->read_supported)
a75647d1 347 audio_write64(data, AUDIO_SET_READ_BUFFER,
8f52e264 348 AUDIO_SET_READ_BUFFER_HIGH, buf_addr);
2e82b83d
AC
349
350 audio_data = data;
351 return 0;
2e82b83d
AC
352}
353
354static int goldfish_audio_remove(struct platform_device *pdev)
355{
2e82b83d 356 misc_deregister(&goldfish_audio_device);
2e82b83d
AC
357 audio_data = NULL;
358 return 0;
359}
360
283ded10
GH
361static const struct of_device_id goldfish_audio_of_match[] = {
362 { .compatible = "google,goldfish-audio", },
363 {},
364};
365MODULE_DEVICE_TABLE(of, goldfish_audio_of_match);
366
06e526f4
YN
367#ifdef CONFIG_ACPI
368static const struct acpi_device_id goldfish_audio_acpi_match[] = {
369 { "GFSH0005", 0 },
370 { },
371};
372MODULE_DEVICE_TABLE(acpi, goldfish_audio_acpi_match);
373#endif
374
2e82b83d
AC
375static struct platform_driver goldfish_audio_driver = {
376 .probe = goldfish_audio_probe,
377 .remove = goldfish_audio_remove,
378 .driver = {
283ded10
GH
379 .name = "goldfish_audio",
380 .of_match_table = goldfish_audio_of_match,
06e526f4 381 .acpi_match_table = ACPI_PTR(goldfish_audio_acpi_match),
2e82b83d
AC
382 }
383};
384
385module_platform_driver(goldfish_audio_driver);