2 * rt5514-spi.c -- RT5514 SPI driver
4 * Copyright 2015 Realtek Semiconductor Corp.
5 * Author: Oder Chiou <oder_chiou@realtek.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/input.h>
14 #include <linux/spi/spi.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_qos.h>
26 #include <linux/sysfs.h>
27 #include <linux/clk.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/initval.h>
34 #include <sound/tlv.h>
36 #include "rt5514-spi.h"
38 #define DRV_NAME "rt5514-spi"
40 static struct spi_device *rt5514_spi;
44 struct delayed_work copy_work;
45 struct mutex dma_lock;
46 struct snd_pcm_substream *substream;
47 unsigned int buf_base, buf_limit, buf_rp;
48 size_t buf_size, get_size, dma_offset;
51 static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = {
52 .info = SNDRV_PCM_INFO_MMAP |
53 SNDRV_PCM_INFO_MMAP_VALID |
54 SNDRV_PCM_INFO_INTERLEAVED,
55 .formats = SNDRV_PCM_FMTBIT_S16_LE,
56 .period_bytes_min = PAGE_SIZE,
57 .period_bytes_max = 0x20000 / 8,
62 .buffer_bytes_max = 0x20000,
65 static struct snd_soc_dai_driver rt5514_spi_dai = {
66 .name = "rt5514-dsp-cpu-dai",
69 .stream_name = "DSP Capture",
72 .rates = SNDRV_PCM_RATE_16000,
73 .formats = SNDRV_PCM_FMTBIT_S16_LE,
77 static void rt5514_spi_copy_work(struct work_struct *work)
79 struct rt5514_dsp *rt5514_dsp =
80 container_of(work, struct rt5514_dsp, copy_work.work);
81 struct snd_pcm_runtime *runtime;
82 size_t period_bytes, truncated_bytes = 0;
83 unsigned int cur_wp, remain_data;
86 mutex_lock(&rt5514_dsp->dma_lock);
87 if (!rt5514_dsp->substream) {
88 dev_err(rt5514_dsp->dev, "No pcm substream\n");
92 runtime = rt5514_dsp->substream->runtime;
93 period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream);
95 schedule_delayed_work(&rt5514_dsp->copy_work, 5);
99 if (rt5514_dsp->buf_size % period_bytes)
100 rt5514_dsp->buf_size = (rt5514_dsp->buf_size / period_bytes) *
103 if (rt5514_dsp->get_size >= rt5514_dsp->buf_size) {
104 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf,
106 cur_wp = buf[0] | buf[1] << 8 | buf[2] << 16 |
109 if (cur_wp >= rt5514_dsp->buf_rp)
110 remain_data = (cur_wp - rt5514_dsp->buf_rp);
113 (rt5514_dsp->buf_limit - rt5514_dsp->buf_rp) +
114 (cur_wp - rt5514_dsp->buf_base);
116 if (remain_data < period_bytes) {
117 schedule_delayed_work(&rt5514_dsp->copy_work, 5);
122 if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) {
123 rt5514_spi_burst_read(rt5514_dsp->buf_rp,
124 runtime->dma_area + rt5514_dsp->dma_offset,
127 if (rt5514_dsp->buf_rp + period_bytes == rt5514_dsp->buf_limit)
128 rt5514_dsp->buf_rp = rt5514_dsp->buf_base;
130 rt5514_dsp->buf_rp += period_bytes;
132 truncated_bytes = rt5514_dsp->buf_limit - rt5514_dsp->buf_rp;
133 rt5514_spi_burst_read(rt5514_dsp->buf_rp,
134 runtime->dma_area + rt5514_dsp->dma_offset,
137 rt5514_spi_burst_read(rt5514_dsp->buf_base,
138 runtime->dma_area + rt5514_dsp->dma_offset +
139 truncated_bytes, period_bytes - truncated_bytes);
141 rt5514_dsp->buf_rp = rt5514_dsp->buf_base + period_bytes -
145 rt5514_dsp->get_size += period_bytes;
146 rt5514_dsp->dma_offset += period_bytes;
147 if (rt5514_dsp->dma_offset >= runtime->dma_bytes)
148 rt5514_dsp->dma_offset = 0;
150 snd_pcm_period_elapsed(rt5514_dsp->substream);
152 schedule_delayed_work(&rt5514_dsp->copy_work, 5);
155 mutex_unlock(&rt5514_dsp->dma_lock);
158 static void rt5514_schedule_copy(struct rt5514_dsp *rt5514_dsp)
162 if (!rt5514_dsp->substream)
165 rt5514_dsp->get_size = 0;
168 * The address area x1800XXXX is the register address, and it cannot
169 * support spi burst read perfectly. So we use the spi burst read
170 * individually to make sure the data correctly.
172 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf,
174 rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 |
177 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf,
179 rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 |
182 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf,
184 rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 |
187 if (rt5514_dsp->buf_rp % 8)
188 rt5514_dsp->buf_rp = (rt5514_dsp->buf_rp / 8) * 8;
190 rt5514_dsp->buf_size = rt5514_dsp->buf_limit - rt5514_dsp->buf_base;
192 if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit &&
193 rt5514_dsp->buf_rp && rt5514_dsp->buf_size)
194 schedule_delayed_work(&rt5514_dsp->copy_work, 0);
197 static irqreturn_t rt5514_spi_irq(int irq, void *data)
199 struct rt5514_dsp *rt5514_dsp = data;
201 rt5514_schedule_copy(rt5514_dsp);
206 /* PCM for streaming audio from the DSP buffer */
207 static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream)
209 snd_soc_set_runtime_hwparams(substream, &rt5514_spi_pcm_hardware);
214 static int rt5514_spi_hw_params(struct snd_pcm_substream *substream,
215 struct snd_pcm_hw_params *hw_params)
217 struct snd_soc_pcm_runtime *rtd = substream->private_data;
218 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
219 struct rt5514_dsp *rt5514_dsp =
220 snd_soc_component_get_drvdata(component);
224 mutex_lock(&rt5514_dsp->dma_lock);
225 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
226 params_buffer_bytes(hw_params));
227 rt5514_dsp->substream = substream;
228 rt5514_dsp->dma_offset = 0;
230 /* Read IRQ status and schedule copy accordingly. */
231 rt5514_spi_burst_read(RT5514_IRQ_CTRL, (u8 *)&buf, sizeof(buf));
232 if (buf[0] & RT5514_IRQ_STATUS_BIT)
233 rt5514_schedule_copy(rt5514_dsp);
235 mutex_unlock(&rt5514_dsp->dma_lock);
240 static int rt5514_spi_hw_free(struct snd_pcm_substream *substream)
242 struct snd_soc_pcm_runtime *rtd = substream->private_data;
243 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
244 struct rt5514_dsp *rt5514_dsp =
245 snd_soc_component_get_drvdata(component);
247 mutex_lock(&rt5514_dsp->dma_lock);
248 rt5514_dsp->substream = NULL;
249 mutex_unlock(&rt5514_dsp->dma_lock);
251 cancel_delayed_work_sync(&rt5514_dsp->copy_work);
253 return snd_pcm_lib_free_vmalloc_buffer(substream);
256 static snd_pcm_uframes_t rt5514_spi_pcm_pointer(
257 struct snd_pcm_substream *substream)
259 struct snd_pcm_runtime *runtime = substream->runtime;
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
262 struct rt5514_dsp *rt5514_dsp =
263 snd_soc_component_get_drvdata(component);
265 return bytes_to_frames(runtime, rt5514_dsp->dma_offset);
268 static const struct snd_pcm_ops rt5514_spi_pcm_ops = {
269 .open = rt5514_spi_pcm_open,
270 .hw_params = rt5514_spi_hw_params,
271 .hw_free = rt5514_spi_hw_free,
272 .pointer = rt5514_spi_pcm_pointer,
273 .mmap = snd_pcm_lib_mmap_vmalloc,
274 .page = snd_pcm_lib_get_vmalloc_page,
277 static int rt5514_spi_pcm_probe(struct snd_soc_component *component)
279 struct rt5514_dsp *rt5514_dsp;
282 rt5514_dsp = devm_kzalloc(component->dev, sizeof(*rt5514_dsp),
285 rt5514_dsp->dev = &rt5514_spi->dev;
286 mutex_init(&rt5514_dsp->dma_lock);
287 INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work);
288 snd_soc_component_set_drvdata(component, rt5514_dsp);
290 if (rt5514_spi->irq) {
291 ret = devm_request_threaded_irq(&rt5514_spi->dev,
292 rt5514_spi->irq, NULL, rt5514_spi_irq,
293 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "rt5514-spi",
296 dev_err(&rt5514_spi->dev,
297 "%s Failed to reguest IRQ: %d\n", __func__,
300 device_init_wakeup(rt5514_dsp->dev, true);
306 static const struct snd_soc_component_driver rt5514_spi_component = {
308 .probe = rt5514_spi_pcm_probe,
309 .ops = &rt5514_spi_pcm_ops,
313 * rt5514_spi_burst_read - Read data from SPI by rt5514 address.
314 * @addr: Start address.
315 * @rxbuf: Data Buffer for reading.
316 * @len: Data length, it must be a multiple of 8.
319 * Returns true for success.
321 int rt5514_spi_burst_read(unsigned int addr, u8 *rxbuf, size_t len)
323 u8 spi_cmd = RT5514_SPI_CMD_BURST_READ;
326 unsigned int i, end, offset = 0;
328 struct spi_message message;
329 struct spi_transfer x[3];
331 while (offset < len) {
332 if (offset + RT5514_SPI_BUF_LEN <= len)
333 end = RT5514_SPI_BUF_LEN;
335 end = len % RT5514_SPI_BUF_LEN;
337 write_buf[0] = spi_cmd;
338 write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
339 write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
340 write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
341 write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
343 spi_message_init(&message);
344 memset(x, 0, sizeof(x));
347 x[0].tx_buf = write_buf;
348 spi_message_add_tail(&x[0], &message);
351 x[1].tx_buf = write_buf;
352 spi_message_add_tail(&x[1], &message);
355 x[2].rx_buf = rxbuf + offset;
356 spi_message_add_tail(&x[2], &message);
358 status = spi_sync(rt5514_spi, &message);
363 offset += RT5514_SPI_BUF_LEN;
366 for (i = 0; i < len; i += 8) {
367 write_buf[0] = rxbuf[i + 0];
368 write_buf[1] = rxbuf[i + 1];
369 write_buf[2] = rxbuf[i + 2];
370 write_buf[3] = rxbuf[i + 3];
371 write_buf[4] = rxbuf[i + 4];
372 write_buf[5] = rxbuf[i + 5];
373 write_buf[6] = rxbuf[i + 6];
374 write_buf[7] = rxbuf[i + 7];
376 rxbuf[i + 0] = write_buf[7];
377 rxbuf[i + 1] = write_buf[6];
378 rxbuf[i + 2] = write_buf[5];
379 rxbuf[i + 3] = write_buf[4];
380 rxbuf[i + 4] = write_buf[3];
381 rxbuf[i + 5] = write_buf[2];
382 rxbuf[i + 6] = write_buf[1];
383 rxbuf[i + 7] = write_buf[0];
388 EXPORT_SYMBOL_GPL(rt5514_spi_burst_read);
391 * rt5514_spi_burst_write - Write data to SPI by rt5514 address.
392 * @addr: Start address.
393 * @txbuf: Data Buffer for writng.
394 * @len: Data length, it must be a multiple of 8.
397 * Returns true for success.
399 int rt5514_spi_burst_write(u32 addr, const u8 *txbuf, size_t len)
401 u8 spi_cmd = RT5514_SPI_CMD_BURST_WRITE;
403 unsigned int i, end, offset = 0;
405 write_buf = kmalloc(RT5514_SPI_BUF_LEN + 6, GFP_KERNEL);
407 if (write_buf == NULL)
410 while (offset < len) {
411 if (offset + RT5514_SPI_BUF_LEN <= len)
412 end = RT5514_SPI_BUF_LEN;
414 end = len % RT5514_SPI_BUF_LEN;
416 write_buf[0] = spi_cmd;
417 write_buf[1] = ((addr + offset) & 0xff000000) >> 24;
418 write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16;
419 write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8;
420 write_buf[4] = ((addr + offset) & 0x000000ff) >> 0;
422 for (i = 0; i < end; i += 8) {
423 write_buf[i + 12] = txbuf[offset + i + 0];
424 write_buf[i + 11] = txbuf[offset + i + 1];
425 write_buf[i + 10] = txbuf[offset + i + 2];
426 write_buf[i + 9] = txbuf[offset + i + 3];
427 write_buf[i + 8] = txbuf[offset + i + 4];
428 write_buf[i + 7] = txbuf[offset + i + 5];
429 write_buf[i + 6] = txbuf[offset + i + 6];
430 write_buf[i + 5] = txbuf[offset + i + 7];
433 write_buf[end + 5] = spi_cmd;
435 spi_write(rt5514_spi, write_buf, end + 6);
437 offset += RT5514_SPI_BUF_LEN;
444 EXPORT_SYMBOL_GPL(rt5514_spi_burst_write);
446 static int rt5514_spi_probe(struct spi_device *spi)
452 ret = devm_snd_soc_register_component(&spi->dev,
453 &rt5514_spi_component,
456 dev_err(&spi->dev, "Failed to register component.\n");
463 static int __maybe_unused rt5514_suspend(struct device *dev)
465 int irq = to_spi_device(dev)->irq;
467 if (device_may_wakeup(dev))
468 enable_irq_wake(irq);
473 static int __maybe_unused rt5514_resume(struct device *dev)
475 struct snd_soc_component *component = snd_soc_lookup_component(dev, DRV_NAME);
476 struct rt5514_dsp *rt5514_dsp =
477 snd_soc_component_get_drvdata(component);
478 int irq = to_spi_device(dev)->irq;
481 if (device_may_wakeup(dev))
482 disable_irq_wake(irq);
485 if (rt5514_dsp->substream) {
486 rt5514_spi_burst_read(RT5514_IRQ_CTRL, (u8 *)&buf,
488 if (buf[0] & RT5514_IRQ_STATUS_BIT)
489 rt5514_schedule_copy(rt5514_dsp);
496 static const struct dev_pm_ops rt5514_pm_ops = {
497 SET_SYSTEM_SLEEP_PM_OPS(rt5514_suspend, rt5514_resume)
500 static const struct of_device_id rt5514_of_match[] = {
501 { .compatible = "realtek,rt5514", },
504 MODULE_DEVICE_TABLE(of, rt5514_of_match);
506 static struct spi_driver rt5514_spi_driver = {
509 .pm = &rt5514_pm_ops,
510 .of_match_table = of_match_ptr(rt5514_of_match),
512 .probe = rt5514_spi_probe,
514 module_spi_driver(rt5514_spi_driver);
516 MODULE_DESCRIPTION("RT5514 SPI driver");
517 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
518 MODULE_LICENSE("GPL v2");