1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
5 * lpass-platform.c -- ALSA SoC platform driver for QTi LPASS
8 #include <linux/dma-mapping.h>
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <sound/pcm_params.h>
14 #include <linux/regmap.h>
15 #include <sound/soc.h>
16 #include "lpass-lpaif-reg.h"
19 #define DRV_NAME "lpass-platform"
21 struct lpass_pcm_data {
26 #define LPASS_PLATFORM_BUFFER_SIZE (16 * 1024)
27 #define LPASS_PLATFORM_PERIODS 2
29 static const struct snd_pcm_hardware lpass_platform_pcm_hardware = {
30 .info = SNDRV_PCM_INFO_MMAP |
31 SNDRV_PCM_INFO_MMAP_VALID |
32 SNDRV_PCM_INFO_INTERLEAVED |
33 SNDRV_PCM_INFO_PAUSE |
34 SNDRV_PCM_INFO_RESUME,
35 .formats = SNDRV_PCM_FMTBIT_S16 |
36 SNDRV_PCM_FMTBIT_S24 |
38 .rates = SNDRV_PCM_RATE_8000_192000,
43 .buffer_bytes_max = LPASS_PLATFORM_BUFFER_SIZE,
44 .period_bytes_max = LPASS_PLATFORM_BUFFER_SIZE /
45 LPASS_PLATFORM_PERIODS,
46 .period_bytes_min = LPASS_PLATFORM_BUFFER_SIZE /
47 LPASS_PLATFORM_PERIODS,
48 .periods_min = LPASS_PLATFORM_PERIODS,
49 .periods_max = LPASS_PLATFORM_PERIODS,
53 static int lpass_platform_alloc_dmactl_fields(struct device *dev,
56 struct lpass_data *drvdata = dev_get_drvdata(dev);
57 struct lpass_variant *v = drvdata->variant;
58 struct lpaif_dmactl *rd_dmactl, *wr_dmactl;
61 drvdata->rd_dmactl = devm_kzalloc(dev, sizeof(struct lpaif_dmactl),
63 if (drvdata->rd_dmactl == NULL)
66 drvdata->wr_dmactl = devm_kzalloc(dev, sizeof(struct lpaif_dmactl),
68 if (drvdata->wr_dmactl == NULL)
71 rd_dmactl = drvdata->rd_dmactl;
72 wr_dmactl = drvdata->wr_dmactl;
74 rval = devm_regmap_field_bulk_alloc(dev, map, &rd_dmactl->bursten,
79 return devm_regmap_field_bulk_alloc(dev, map, &wr_dmactl->bursten,
80 &v->wrdma_bursten, 6);
83 static int lpass_platform_pcmops_open(struct snd_soc_component *component,
84 struct snd_pcm_substream *substream)
86 struct snd_pcm_runtime *runtime = substream->runtime;
87 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
88 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_runtime, 0);
89 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
90 struct lpass_variant *v = drvdata->variant;
91 int ret, dma_ch, dir = substream->stream;
92 struct lpass_pcm_data *data;
94 data = kzalloc(sizeof(*data), GFP_KERNEL);
98 data->i2s_port = cpu_dai->driver->id;
99 runtime->private_data = data;
101 if (v->alloc_dma_channel)
102 dma_ch = v->alloc_dma_channel(drvdata, dir);
109 drvdata->substream[dma_ch] = substream;
111 ret = regmap_write(drvdata->lpaif_map,
112 LPAIF_DMACTL_REG(v, dma_ch, dir), 0);
114 dev_err(soc_runtime->dev,
115 "error writing to rdmactl reg: %d\n", ret);
119 data->dma_ch = dma_ch;
121 snd_soc_set_runtime_hwparams(substream, &lpass_platform_pcm_hardware);
123 runtime->dma_bytes = lpass_platform_pcm_hardware.buffer_bytes_max;
125 ret = snd_pcm_hw_constraint_integer(runtime,
126 SNDRV_PCM_HW_PARAM_PERIODS);
128 dev_err(soc_runtime->dev, "setting constraints failed: %d\n",
133 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
138 static int lpass_platform_pcmops_close(struct snd_soc_component *component,
139 struct snd_pcm_substream *substream)
141 struct snd_pcm_runtime *runtime = substream->runtime;
142 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
143 struct lpass_variant *v = drvdata->variant;
144 struct lpass_pcm_data *data;
146 data = runtime->private_data;
147 drvdata->substream[data->dma_ch] = NULL;
148 if (v->free_dma_channel)
149 v->free_dma_channel(drvdata, data->dma_ch);
155 static int lpass_platform_pcmops_hw_params(struct snd_soc_component *component,
156 struct snd_pcm_substream *substream,
157 struct snd_pcm_hw_params *params)
159 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
160 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
161 struct snd_pcm_runtime *rt = substream->runtime;
162 struct lpass_pcm_data *pcm_data = rt->private_data;
163 struct lpass_variant *v = drvdata->variant;
164 snd_pcm_format_t format = params_format(params);
165 unsigned int channels = params_channels(params);
167 struct lpaif_dmactl *dmactl;
168 int id, dir = substream->stream;
170 int ret, dma_port = pcm_data->i2s_port + v->dmactl_audif_start;
172 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
173 dmactl = drvdata->rd_dmactl;
174 id = pcm_data->dma_ch;
176 dmactl = drvdata->wr_dmactl;
177 id = pcm_data->dma_ch - v->wrdma_channel_start;
180 bitwidth = snd_pcm_format_width(format);
182 dev_err(soc_runtime->dev, "invalid bit width given: %d\n",
187 ret = regmap_fields_write(dmactl->bursten, id, LPAIF_DMACTL_BURSTEN_INCR4);
189 dev_err(soc_runtime->dev, "error updating bursten field: %d\n", ret);
193 regmap_fields_write(dmactl->fifowm, id, LPAIF_DMACTL_FIFOWM_8);
195 dev_err(soc_runtime->dev, "error updating fifowm field: %d\n", ret);
199 regmap_fields_write(dmactl->intf, id, LPAIF_DMACTL_AUDINTF(dma_port));
201 dev_err(soc_runtime->dev, "error updating audintf field: %d\n", ret);
210 regval = LPAIF_DMACTL_WPSCNT_ONE;
213 regval = LPAIF_DMACTL_WPSCNT_TWO;
216 regval = LPAIF_DMACTL_WPSCNT_THREE;
219 regval = LPAIF_DMACTL_WPSCNT_FOUR;
222 dev_err(soc_runtime->dev,
223 "invalid PCM config given: bw=%d, ch=%u\n",
232 regval = LPAIF_DMACTL_WPSCNT_ONE;
235 regval = LPAIF_DMACTL_WPSCNT_TWO;
238 regval = LPAIF_DMACTL_WPSCNT_FOUR;
241 regval = LPAIF_DMACTL_WPSCNT_SIX;
244 regval = LPAIF_DMACTL_WPSCNT_EIGHT;
247 dev_err(soc_runtime->dev,
248 "invalid PCM config given: bw=%d, ch=%u\n",
254 dev_err(soc_runtime->dev, "invalid PCM config given: bw=%d, ch=%u\n",
259 ret = regmap_fields_write(dmactl->wpscnt, id, regval);
261 dev_err(soc_runtime->dev, "error writing to dmactl reg: %d\n",
269 static int lpass_platform_pcmops_hw_free(struct snd_soc_component *component,
270 struct snd_pcm_substream *substream)
272 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
273 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
274 struct snd_pcm_runtime *rt = substream->runtime;
275 struct lpass_pcm_data *pcm_data = rt->private_data;
276 struct lpass_variant *v = drvdata->variant;
280 reg = LPAIF_DMACTL_REG(v, pcm_data->dma_ch, substream->stream);
281 ret = regmap_write(drvdata->lpaif_map, reg, 0);
283 dev_err(soc_runtime->dev, "error writing to rdmactl reg: %d\n",
289 static int lpass_platform_pcmops_prepare(struct snd_soc_component *component,
290 struct snd_pcm_substream *substream)
292 struct snd_pcm_runtime *runtime = substream->runtime;
293 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
294 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
295 struct snd_pcm_runtime *rt = substream->runtime;
296 struct lpass_pcm_data *pcm_data = rt->private_data;
297 struct lpass_variant *v = drvdata->variant;
298 struct lpaif_dmactl *dmactl;
299 int ret, id, ch, dir = substream->stream;
301 ch = pcm_data->dma_ch;
302 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
303 dmactl = drvdata->rd_dmactl;
304 id = pcm_data->dma_ch;
306 dmactl = drvdata->wr_dmactl;
307 id = pcm_data->dma_ch - v->wrdma_channel_start;
310 ret = regmap_write(drvdata->lpaif_map,
311 LPAIF_DMABASE_REG(v, ch, dir),
314 dev_err(soc_runtime->dev, "error writing to rdmabase reg: %d\n",
319 ret = regmap_write(drvdata->lpaif_map,
320 LPAIF_DMABUFF_REG(v, ch, dir),
321 (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1);
323 dev_err(soc_runtime->dev, "error writing to rdmabuff reg: %d\n",
328 ret = regmap_write(drvdata->lpaif_map,
329 LPAIF_DMAPER_REG(v, ch, dir),
330 (snd_pcm_lib_period_bytes(substream) >> 2) - 1);
332 dev_err(soc_runtime->dev, "error writing to rdmaper reg: %d\n",
337 ret = regmap_fields_write(dmactl->enable, id, LPAIF_DMACTL_ENABLE_ON);
339 dev_err(soc_runtime->dev, "error writing to rdmactl reg: %d\n",
347 static int lpass_platform_pcmops_trigger(struct snd_soc_component *component,
348 struct snd_pcm_substream *substream,
351 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
352 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
353 struct snd_pcm_runtime *rt = substream->runtime;
354 struct lpass_pcm_data *pcm_data = rt->private_data;
355 struct lpass_variant *v = drvdata->variant;
356 struct lpaif_dmactl *dmactl;
358 int dir = substream->stream;
360 ch = pcm_data->dma_ch;
361 if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
362 dmactl = drvdata->rd_dmactl;
363 id = pcm_data->dma_ch;
365 dmactl = drvdata->wr_dmactl;
366 id = pcm_data->dma_ch - v->wrdma_channel_start;
370 case SNDRV_PCM_TRIGGER_START:
371 case SNDRV_PCM_TRIGGER_RESUME:
372 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
373 /* clear status before enabling interrupts */
374 ret = regmap_write(drvdata->lpaif_map,
375 LPAIF_IRQCLEAR_REG(v, LPAIF_IRQ_PORT_HOST),
378 dev_err(soc_runtime->dev,
379 "error writing to irqclear reg: %d\n", ret);
383 ret = regmap_update_bits(drvdata->lpaif_map,
384 LPAIF_IRQEN_REG(v, LPAIF_IRQ_PORT_HOST),
388 dev_err(soc_runtime->dev,
389 "error writing to irqen reg: %d\n", ret);
393 ret = regmap_fields_write(dmactl->enable, id,
394 LPAIF_DMACTL_ENABLE_ON);
396 dev_err(soc_runtime->dev,
397 "error writing to rdmactl reg: %d\n", ret);
401 case SNDRV_PCM_TRIGGER_STOP:
402 case SNDRV_PCM_TRIGGER_SUSPEND:
403 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
404 ret = regmap_fields_write(dmactl->enable, id,
405 LPAIF_DMACTL_ENABLE_OFF);
407 dev_err(soc_runtime->dev,
408 "error writing to rdmactl reg: %d\n", ret);
412 ret = regmap_update_bits(drvdata->lpaif_map,
413 LPAIF_IRQEN_REG(v, LPAIF_IRQ_PORT_HOST),
414 LPAIF_IRQ_ALL(ch), 0);
416 dev_err(soc_runtime->dev,
417 "error writing to irqen reg: %d\n", ret);
426 static snd_pcm_uframes_t lpass_platform_pcmops_pointer(
427 struct snd_soc_component *component,
428 struct snd_pcm_substream *substream)
430 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
431 struct lpass_data *drvdata = snd_soc_component_get_drvdata(component);
432 struct snd_pcm_runtime *rt = substream->runtime;
433 struct lpass_pcm_data *pcm_data = rt->private_data;
434 struct lpass_variant *v = drvdata->variant;
435 unsigned int base_addr, curr_addr;
436 int ret, ch, dir = substream->stream;
438 ch = pcm_data->dma_ch;
440 ret = regmap_read(drvdata->lpaif_map,
441 LPAIF_DMABASE_REG(v, ch, dir), &base_addr);
443 dev_err(soc_runtime->dev,
444 "error reading from rdmabase reg: %d\n", ret);
448 ret = regmap_read(drvdata->lpaif_map,
449 LPAIF_DMACURR_REG(v, ch, dir), &curr_addr);
451 dev_err(soc_runtime->dev,
452 "error reading from rdmacurr reg: %d\n", ret);
456 return bytes_to_frames(substream->runtime, curr_addr - base_addr);
459 static int lpass_platform_pcmops_mmap(struct snd_soc_component *component,
460 struct snd_pcm_substream *substream,
461 struct vm_area_struct *vma)
463 struct snd_pcm_runtime *runtime = substream->runtime;
465 return dma_mmap_coherent(component->dev, vma, runtime->dma_area,
466 runtime->dma_addr, runtime->dma_bytes);
469 static irqreturn_t lpass_dma_interrupt_handler(
470 struct snd_pcm_substream *substream,
471 struct lpass_data *drvdata,
472 int chan, u32 interrupts)
474 struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
475 struct lpass_variant *v = drvdata->variant;
476 irqreturn_t ret = IRQ_NONE;
479 if (interrupts & LPAIF_IRQ_PER(chan)) {
480 rv = regmap_write(drvdata->lpaif_map,
481 LPAIF_IRQCLEAR_REG(v, LPAIF_IRQ_PORT_HOST),
482 LPAIF_IRQ_PER(chan));
484 dev_err(soc_runtime->dev,
485 "error writing to irqclear reg: %d\n", rv);
488 snd_pcm_period_elapsed(substream);
492 if (interrupts & LPAIF_IRQ_XRUN(chan)) {
493 rv = regmap_write(drvdata->lpaif_map,
494 LPAIF_IRQCLEAR_REG(v, LPAIF_IRQ_PORT_HOST),
495 LPAIF_IRQ_XRUN(chan));
497 dev_err(soc_runtime->dev,
498 "error writing to irqclear reg: %d\n", rv);
501 dev_warn(soc_runtime->dev, "xrun warning\n");
502 snd_pcm_stop_xrun(substream);
506 if (interrupts & LPAIF_IRQ_ERR(chan)) {
507 rv = regmap_write(drvdata->lpaif_map,
508 LPAIF_IRQCLEAR_REG(v, LPAIF_IRQ_PORT_HOST),
509 LPAIF_IRQ_ERR(chan));
511 dev_err(soc_runtime->dev,
512 "error writing to irqclear reg: %d\n", rv);
515 dev_err(soc_runtime->dev, "bus access error\n");
516 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
523 static irqreturn_t lpass_platform_lpaif_irq(int irq, void *data)
525 struct lpass_data *drvdata = data;
526 struct lpass_variant *v = drvdata->variant;
530 rv = regmap_read(drvdata->lpaif_map,
531 LPAIF_IRQSTAT_REG(v, LPAIF_IRQ_PORT_HOST), &irqs);
533 pr_err("error reading from irqstat reg: %d\n", rv);
537 /* Handle per channel interrupts */
538 for (chan = 0; chan < LPASS_MAX_DMA_CHANNELS; chan++) {
539 if (irqs & LPAIF_IRQ_ALL(chan) && drvdata->substream[chan]) {
540 rv = lpass_dma_interrupt_handler(
541 drvdata->substream[chan],
542 drvdata, chan, irqs);
543 if (rv != IRQ_HANDLED)
551 static int lpass_platform_pcm_new(struct snd_soc_component *component,
552 struct snd_soc_pcm_runtime *soc_runtime)
554 struct snd_pcm *pcm = soc_runtime->pcm;
555 struct snd_pcm_substream *psubstream, *csubstream;
557 size_t size = lpass_platform_pcm_hardware.buffer_bytes_max;
559 psubstream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
561 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
563 size, &psubstream->dma_buffer);
565 dev_err(soc_runtime->dev, "Cannot allocate buffer(s)\n");
570 csubstream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
572 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
574 size, &csubstream->dma_buffer);
576 dev_err(soc_runtime->dev, "Cannot allocate buffer(s)\n");
578 snd_dma_free_pages(&psubstream->dma_buffer);
587 static void lpass_platform_pcm_free(struct snd_soc_component *component,
590 struct snd_pcm_substream *substream;
593 for_each_pcm_streams(i) {
594 substream = pcm->streams[i].substream;
596 snd_dma_free_pages(&substream->dma_buffer);
597 substream->dma_buffer.area = NULL;
598 substream->dma_buffer.addr = 0;
603 static const struct snd_soc_component_driver lpass_component_driver = {
605 .open = lpass_platform_pcmops_open,
606 .close = lpass_platform_pcmops_close,
607 .hw_params = lpass_platform_pcmops_hw_params,
608 .hw_free = lpass_platform_pcmops_hw_free,
609 .prepare = lpass_platform_pcmops_prepare,
610 .trigger = lpass_platform_pcmops_trigger,
611 .pointer = lpass_platform_pcmops_pointer,
612 .mmap = lpass_platform_pcmops_mmap,
613 .pcm_construct = lpass_platform_pcm_new,
614 .pcm_destruct = lpass_platform_pcm_free,
618 int asoc_qcom_lpass_platform_register(struct platform_device *pdev)
620 struct lpass_data *drvdata = platform_get_drvdata(pdev);
621 struct lpass_variant *v = drvdata->variant;
624 drvdata->lpaif_irq = platform_get_irq(pdev, 0);
625 if (drvdata->lpaif_irq < 0)
628 /* ensure audio hardware is disabled */
629 ret = regmap_write(drvdata->lpaif_map,
630 LPAIF_IRQEN_REG(v, LPAIF_IRQ_PORT_HOST), 0);
632 dev_err(&pdev->dev, "error writing to irqen reg: %d\n", ret);
636 ret = devm_request_irq(&pdev->dev, drvdata->lpaif_irq,
637 lpass_platform_lpaif_irq, IRQF_TRIGGER_RISING,
638 "lpass-irq-lpaif", drvdata);
640 dev_err(&pdev->dev, "irq request failed: %d\n", ret);
644 ret = lpass_platform_alloc_dmactl_fields(&pdev->dev,
648 "error initializing dmactl fields: %d\n", ret);
652 return devm_snd_soc_register_component(&pdev->dev,
653 &lpass_component_driver, NULL, 0);
655 EXPORT_SYMBOL_GPL(asoc_qcom_lpass_platform_register);
657 MODULE_DESCRIPTION("QTi LPASS Platform Driver");
658 MODULE_LICENSE("GPL v2");