| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// Copyright (c) 2020, Linaro Limited |
| 3 |
|
| 4 |
#include <dt-bindings/sound/qcom,q6afe.h> |
| 5 |
#include <linux/module.h> |
| 6 |
#include <linux/platform_device.h> |
| 7 |
#include <sound/soc.h> |
| 8 |
#include <sound/soc-dapm.h> |
| 9 |
#include <sound/pcm.h> |
| 10 |
#include <sound/pcm_params.h> |
| 11 |
#include <linux/soundwire/sdw.h> |
| 12 |
#include <sound/jack.h> |
| 13 |
#include <linux/input-event-codes.h> |
| 14 |
#include "qdsp6/q6afe.h" |
| 15 |
#include "common.h" |
| 16 |
#include "usb_offload_utils.h" |
| 17 |
#include "sdw.h" |
| 18 |
|
| 19 |
#define MI2S_BCLK_RATE 1536000 |
| 20 |
|
| 21 |
struct sm8250_snd_data { |
| 22 |
bool stream_prepared[AFE_PORT_MAX]; |
| 23 |
struct snd_soc_card *card; |
| 24 |
struct snd_soc_jack jack; |
| 25 |
struct snd_soc_jack usb_offload_jack; |
| 26 |
bool usb_offload_jack_setup; |
| 27 |
struct snd_soc_jack dp_jack; |
| 28 |
bool jack_setup; |
| 29 |
}; |
| 30 |
|
| 31 |
static int sm8250_snd_init(struct snd_soc_pcm_runtime *rtd) |
| 32 |
{ |
| 33 |
struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card); |
| 34 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 35 |
|
| 36 |
switch (cpu_dai->id) { |
| 37 |
case DISPLAY_PORT_RX: |
| 38 |
return qcom_snd_dp_jack_setup(rtd, &data->dp_jack, 0); |
| 39 |
case USB_RX: |
| 40 |
return qcom_snd_usb_offload_jack_setup(rtd, &data->usb_offload_jack, |
| 41 |
&data->usb_offload_jack_setup); |
| 42 |
default: |
| 43 |
return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
static void sm8250_snd_exit(struct snd_soc_pcm_runtime *rtd) |
| 48 |
{ |
| 49 |
struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card); |
| 50 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 51 |
|
| 52 |
if (cpu_dai->id == USB_RX) |
| 53 |
qcom_snd_usb_offload_jack_remove(rtd, |
| 54 |
&data->usb_offload_jack_setup); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
static int sm8250_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
| 59 |
struct snd_pcm_hw_params *params) |
| 60 |
{ |
| 61 |
struct snd_interval *rate = hw_param_interval(params, |
| 62 |
SNDRV_PCM_HW_PARAM_RATE); |
| 63 |
struct snd_interval *channels = hw_param_interval(params, |
| 64 |
SNDRV_PCM_HW_PARAM_CHANNELS); |
| 65 |
struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 66 |
|
| 67 |
rate->min = rate->max = 48000; |
| 68 |
channels->min = channels->max = 2; |
| 69 |
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static int sm8250_snd_startup(struct snd_pcm_substream *substream) |
| 75 |
{ |
| 76 |
unsigned int fmt = SND_SOC_DAIFMT_BP_FP; |
| 77 |
unsigned int codec_dai_fmt = SND_SOC_DAIFMT_BC_FC; |
| 78 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 79 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 80 |
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); |
| 81 |
|
| 82 |
switch (cpu_dai->id) { |
| 83 |
case PRIMARY_MI2S_RX: |
| 84 |
codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S; |
| 85 |
snd_soc_dai_set_sysclk(cpu_dai, |
| 86 |
Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT, |
| 87 |
MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK); |
| 88 |
snd_soc_dai_set_fmt(cpu_dai, fmt); |
| 89 |
snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt); |
| 90 |
break; |
| 91 |
case SECONDARY_MI2S_RX: |
| 92 |
codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S; |
| 93 |
snd_soc_dai_set_sysclk(cpu_dai, |
| 94 |
Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT, |
| 95 |
MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK); |
| 96 |
snd_soc_dai_set_fmt(cpu_dai, fmt); |
| 97 |
snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt); |
| 98 |
break; |
| 99 |
case TERTIARY_MI2S_RX: |
| 100 |
codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S; |
| 101 |
snd_soc_dai_set_sysclk(cpu_dai, |
| 102 |
Q6AFE_LPASS_CLK_ID_TER_MI2S_IBIT, |
| 103 |
MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK); |
| 104 |
snd_soc_dai_set_fmt(cpu_dai, fmt); |
| 105 |
snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt); |
| 106 |
break; |
| 107 |
case QUINARY_MI2S_RX: |
| 108 |
codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S; |
| 109 |
snd_soc_dai_set_sysclk(cpu_dai, |
| 110 |
Q6AFE_LPASS_CLK_ID_QUI_MI2S_IBIT, |
| 111 |
MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK); |
| 112 |
snd_soc_dai_set_fmt(cpu_dai, fmt); |
| 113 |
snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt); |
| 114 |
break; |
| 115 |
default: |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
return qcom_snd_sdw_startup(substream); |
| 120 |
} |
| 121 |
|
| 122 |
static int sm8250_snd_prepare(struct snd_pcm_substream *substream) |
| 123 |
{ |
| 124 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 125 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 126 |
struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card); |
| 127 |
|
| 128 |
return qcom_snd_sdw_prepare(substream, &data->stream_prepared[cpu_dai->id]); |
| 129 |
} |
| 130 |
|
| 131 |
static int sm8250_snd_hw_free(struct snd_pcm_substream *substream) |
| 132 |
{ |
| 133 |
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); |
| 134 |
struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card); |
| 135 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 136 |
|
| 137 |
return qcom_snd_sdw_hw_free(substream, &data->stream_prepared[cpu_dai->id]); |
| 138 |
} |
| 139 |
|
| 140 |
static const struct snd_soc_ops sm8250_be_ops = { |
| 141 |
.startup = sm8250_snd_startup, |
| 142 |
.shutdown = qcom_snd_sdw_shutdown, |
| 143 |
.hw_free = sm8250_snd_hw_free, |
| 144 |
.prepare = sm8250_snd_prepare, |
| 145 |
}; |
| 146 |
|
| 147 |
static void sm8250_add_be_ops(struct snd_soc_card *card) |
| 148 |
{ |
| 149 |
struct snd_soc_dai_link *link; |
| 150 |
int i; |
| 151 |
|
| 152 |
for_each_card_prelinks(card, i, link) { |
| 153 |
if (link->no_pcm == 1) { |
| 154 |
link->init = sm8250_snd_init; |
| 155 |
link->exit = sm8250_snd_exit; |
| 156 |
link->be_hw_params_fixup = sm8250_be_hw_params_fixup; |
| 157 |
link->ops = &sm8250_be_ops; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
static int sm8250_platform_probe(struct platform_device *pdev) |
| 163 |
{ |
| 164 |
struct snd_soc_card *card; |
| 165 |
struct sm8250_snd_data *data; |
| 166 |
struct device *dev = &pdev->dev; |
| 167 |
int ret; |
| 168 |
|
| 169 |
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); |
| 170 |
if (!card) |
| 171 |
return -ENOMEM; |
| 172 |
|
| 173 |
card->owner = THIS_MODULE; |
| 174 |
/* Allocate the private data */ |
| 175 |
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 176 |
if (!data) |
| 177 |
return -ENOMEM; |
| 178 |
|
| 179 |
card->dev = dev; |
| 180 |
dev_set_drvdata(dev, card); |
| 181 |
snd_soc_card_set_drvdata(card, data); |
| 182 |
ret = qcom_snd_parse_of(card); |
| 183 |
if (ret) |
| 184 |
return ret; |
| 185 |
|
| 186 |
card->driver_name = of_device_get_match_data(dev); |
| 187 |
sm8250_add_be_ops(card); |
| 188 |
return devm_snd_soc_register_card(dev, card); |
| 189 |
} |
| 190 |
|
| 191 |
static const struct of_device_id snd_sm8250_dt_match[] = { |
| 192 |
{ .compatible = "fairphone,fp4-sndcard", .data = "sm7225" }, |
| 193 |
{ .compatible = "fairphone,fp5-sndcard", .data = "qcm6490" }, |
| 194 |
{ .compatible = "qcom,qrb2210-sndcard", .data = "qcm2290" }, |
| 195 |
{ .compatible = "qcom,qrb4210-rb2-sndcard", .data = "sm4250" }, |
| 196 |
{ .compatible = "qcom,qrb5165-rb5-sndcard", .data = "sm8250" }, |
| 197 |
{ .compatible = "qcom,sm8250-sndcard", .data = "sm8250" }, |
| 198 |
{} |
| 199 |
}; |
| 200 |
|
| 201 |
MODULE_DEVICE_TABLE(of, snd_sm8250_dt_match); |
| 202 |
|
| 203 |
static struct platform_driver snd_sm8250_driver = { |
| 204 |
.probe = sm8250_platform_probe, |
| 205 |
.driver = { |
| 206 |
.name = "snd-sm8250", |
| 207 |
.of_match_table = snd_sm8250_dt_match, |
| 208 |
}, |
| 209 |
}; |
| 210 |
module_platform_driver(snd_sm8250_driver); |
| 211 |
MODULE_AUTHOR("Srinivas Kandagatla <[email protected]"); |
| 212 |
MODULE_DESCRIPTION("SM8250 ASoC Machine Driver"); |
| 213 |
MODULE_LICENSE("GPL"); |