Path: blob/master/sound/soc/qcom/qdsp6/q6apm-lpass-dais.c
29268 views
// SPDX-License-Identifier: GPL-2.01// Copyright (c) 2021, Linaro Limited23#include <dt-bindings/sound/qcom,q6dsp-lpass-ports.h>4#include <linux/err.h>5#include <linux/init.h>6#include <linux/module.h>7#include <linux/device.h>8#include <linux/platform_device.h>9#include <linux/slab.h>10#include <sound/pcm.h>11#include <sound/soc.h>12#include <sound/pcm_params.h>13#include "q6dsp-lpass-ports.h"14#include "q6dsp-common.h"15#include "audioreach.h"16#include "q6apm.h"1718#define AUDIOREACH_BE_PCM_BASE 161920struct q6apm_lpass_dai_data {21struct q6apm_graph *graph[APM_PORT_MAX];22bool is_port_started[APM_PORT_MAX];23struct audioreach_module_config module_config[APM_PORT_MAX];24};2526static int q6dma_set_channel_map(struct snd_soc_dai *dai,27unsigned int tx_num,28const unsigned int *tx_ch_mask,29unsigned int rx_num,30const unsigned int *rx_ch_mask)31{3233struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);34struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];35int i;3637switch (dai->id) {38case WSA_CODEC_DMA_TX_0:39case WSA_CODEC_DMA_TX_1:40case WSA_CODEC_DMA_TX_2:41case VA_CODEC_DMA_TX_0:42case VA_CODEC_DMA_TX_1:43case VA_CODEC_DMA_TX_2:44case TX_CODEC_DMA_TX_0:45case TX_CODEC_DMA_TX_1:46case TX_CODEC_DMA_TX_2:47case TX_CODEC_DMA_TX_3:48case TX_CODEC_DMA_TX_4:49case TX_CODEC_DMA_TX_5:50if (!tx_ch_mask) {51dev_err(dai->dev, "tx slot not found\n");52return -EINVAL;53}5455if (tx_num > AR_PCM_MAX_NUM_CHANNEL) {56dev_err(dai->dev, "invalid tx num %d\n",57tx_num);58return -EINVAL;59}60for (i = 0; i < tx_num; i++)61cfg->channel_map[i] = tx_ch_mask[i];6263break;64case WSA_CODEC_DMA_RX_0:65case WSA_CODEC_DMA_RX_1:66case RX_CODEC_DMA_RX_0:67case RX_CODEC_DMA_RX_1:68case RX_CODEC_DMA_RX_2:69case RX_CODEC_DMA_RX_3:70case RX_CODEC_DMA_RX_4:71case RX_CODEC_DMA_RX_5:72case RX_CODEC_DMA_RX_6:73case RX_CODEC_DMA_RX_7:74/* rx */75if (!rx_ch_mask) {76dev_err(dai->dev, "rx slot not found\n");77return -EINVAL;78}79if (rx_num > APM_PORT_MAX_AUDIO_CHAN_CNT) {80dev_err(dai->dev, "invalid rx num %d\n",81rx_num);82return -EINVAL;83}84for (i = 0; i < rx_num; i++)85cfg->channel_map[i] = rx_ch_mask[i];8687break;88default:89dev_err(dai->dev, "%s: invalid dai id 0x%x\n",90__func__, dai->id);91return -EINVAL;92}9394return 0;95}9697static int q6hdmi_hw_params(struct snd_pcm_substream *substream,98struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)99{100struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);101struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];102int channels = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max;103int ret;104105cfg->bit_width = params_width(params);106cfg->sample_rate = params_rate(params);107cfg->num_channels = channels;108audioreach_set_default_channel_mapping(cfg->channel_map, channels);109110switch (dai->id) {111case DISPLAY_PORT_RX_0:112cfg->dp_idx = 0;113break;114case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:115cfg->dp_idx = dai->id - DISPLAY_PORT_RX_1 + 1;116break;117}118119ret = q6dsp_get_channel_allocation(channels);120if (ret < 0)121return ret;122123cfg->channel_allocation = ret;124125return 0;126}127128static int q6dma_hw_params(struct snd_pcm_substream *substream,129struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)130{131struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);132struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];133int channels = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max;134135cfg->bit_width = params_width(params);136cfg->sample_rate = params_rate(params);137cfg->num_channels = channels;138audioreach_set_default_channel_mapping(cfg->channel_map, channels);139140return 0;141}142143static void q6apm_lpass_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)144{145struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);146int rc;147148if (dai_data->is_port_started[dai->id]) {149rc = q6apm_graph_stop(dai_data->graph[dai->id]);150dai_data->is_port_started[dai->id] = false;151if (rc < 0)152dev_err(dai->dev, "fail to close APM port (%d)\n", rc);153}154155if (dai_data->graph[dai->id]) {156q6apm_graph_close(dai_data->graph[dai->id]);157dai_data->graph[dai->id] = NULL;158}159}160161static int q6apm_lpass_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)162{163struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);164struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];165struct q6apm_graph *graph;166int graph_id = dai->id;167int rc;168169if (dai_data->is_port_started[dai->id]) {170q6apm_graph_stop(dai_data->graph[dai->id]);171dai_data->is_port_started[dai->id] = false;172173if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {174q6apm_graph_close(dai_data->graph[dai->id]);175dai_data->graph[dai->id] = NULL;176}177}178179/**180* It is recommend to load DSP with source graph first and then sink181* graph, so sequence for playback and capture will be different182*/183if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {184graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);185if (IS_ERR(graph)) {186dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);187rc = PTR_ERR(graph);188return rc;189}190dai_data->graph[graph_id] = graph;191}192193cfg->direction = substream->stream;194rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);195if (rc) {196dev_err(dai->dev, "Failed to set media format %d\n", rc);197goto err;198}199200rc = q6apm_graph_prepare(dai_data->graph[dai->id]);201if (rc) {202dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);203goto err;204}205206rc = q6apm_graph_start(dai_data->graph[dai->id]);207if (rc < 0) {208dev_err(dai->dev, "Failed to start APM port %d\n", dai->id);209goto err;210}211dai_data->is_port_started[dai->id] = true;212213return 0;214err:215if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {216q6apm_graph_close(dai_data->graph[dai->id]);217dai_data->graph[dai->id] = NULL;218}219return rc;220}221222static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)223{224struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);225struct q6apm_graph *graph;226int graph_id = dai->id;227228if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {229graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);230if (IS_ERR(graph)) {231dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);232return PTR_ERR(graph);233}234dai_data->graph[graph_id] = graph;235}236237return 0;238}239240static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)241{242struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);243struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];244245cfg->fmt = fmt;246247return 0;248}249250static const struct snd_soc_dai_ops q6dma_ops = {251.prepare = q6apm_lpass_dai_prepare,252.startup = q6apm_lpass_dai_startup,253.shutdown = q6apm_lpass_dai_shutdown,254.set_channel_map = q6dma_set_channel_map,255.hw_params = q6dma_hw_params,256};257258static const struct snd_soc_dai_ops q6i2s_ops = {259.prepare = q6apm_lpass_dai_prepare,260.startup = q6apm_lpass_dai_startup,261.shutdown = q6apm_lpass_dai_shutdown,262.set_channel_map = q6dma_set_channel_map,263.hw_params = q6dma_hw_params,264.set_fmt = q6i2s_set_fmt,265};266267static const struct snd_soc_dai_ops q6hdmi_ops = {268.prepare = q6apm_lpass_dai_prepare,269.startup = q6apm_lpass_dai_startup,270.shutdown = q6apm_lpass_dai_shutdown,271.hw_params = q6hdmi_hw_params,272.set_fmt = q6i2s_set_fmt,273};274275static const struct snd_soc_component_driver q6apm_lpass_dai_component = {276.name = "q6apm-be-dai-component",277.of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,278.be_pcm_base = AUDIOREACH_BE_PCM_BASE,279.use_dai_pcm_id = true,280};281282static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)283{284struct q6dsp_audio_port_dai_driver_config cfg;285struct q6apm_lpass_dai_data *dai_data;286struct snd_soc_dai_driver *dais;287struct device *dev = &pdev->dev;288int num_dais;289290dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);291if (!dai_data)292return -ENOMEM;293294dev_set_drvdata(dev, dai_data);295296memset(&cfg, 0, sizeof(cfg));297cfg.q6i2s_ops = &q6i2s_ops;298cfg.q6dma_ops = &q6dma_ops;299cfg.q6hdmi_ops = &q6hdmi_ops;300dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);301302return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);303}304305#ifdef CONFIG_OF306static const struct of_device_id q6apm_lpass_dai_device_id[] = {307{ .compatible = "qcom,q6apm-lpass-dais" },308{},309};310MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);311#endif312313static struct platform_driver q6apm_lpass_dai_platform_driver = {314.driver = {315.name = "q6apm-lpass-dais",316.of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),317},318.probe = q6apm_lpass_dai_dev_probe,319};320module_platform_driver(q6apm_lpass_dai_platform_driver);321322MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");323MODULE_LICENSE("GPL");324325326