| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
// |
| 3 |
// Copyright(c) 2021-2022 Intel Corporation |
| 4 |
// |
| 5 |
// Author: Cezary Rojewski <[email protected]> |
| 6 |
// |
| 7 |
|
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/platform_data/x86/soc.h> |
| 10 |
#include <linux/platform_device.h> |
| 11 |
#include <sound/jack.h> |
| 12 |
#include <sound/pcm.h> |
| 13 |
#include <sound/pcm_params.h> |
| 14 |
#include <sound/soc.h> |
| 15 |
#include <sound/soc-acpi.h> |
| 16 |
#include <sound/soc-dapm.h> |
| 17 |
#include <uapi/linux/input-event-codes.h> |
| 18 |
#include "../../../codecs/da7219.h" |
| 19 |
#include "../utils.h" |
| 20 |
|
| 21 |
#define DA7219_DAI_NAME "da7219-hifi" |
| 22 |
|
| 23 |
static const struct snd_kcontrol_new card_controls[] = { |
| 24 |
SOC_DAPM_PIN_SWITCH("Headphone Jack"), |
| 25 |
SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 26 |
SOC_DAPM_PIN_SWITCH("Line Out"), |
| 27 |
}; |
| 28 |
|
| 29 |
static int platform_clock_control(struct snd_soc_dapm_widget *w, |
| 30 |
struct snd_kcontrol *k, int event) |
| 31 |
{ |
| 32 |
struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm); |
| 33 |
struct snd_soc_dai *codec_dai; |
| 34 |
int ret = 0; |
| 35 |
|
| 36 |
codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME); |
| 37 |
if (!codec_dai) { |
| 38 |
dev_err(card->dev, "Codec dai not found. Unable to set/unset codec pll\n"); |
| 39 |
return -EIO; |
| 40 |
} |
| 41 |
|
| 42 |
if (SND_SOC_DAPM_EVENT_OFF(event)) { |
| 43 |
ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0); |
| 44 |
if (ret) |
| 45 |
dev_err(card->dev, "failed to stop PLL: %d\n", ret); |
| 46 |
} else if (SND_SOC_DAPM_EVENT_ON(event)) { |
| 47 |
ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM, |
| 48 |
0, DA7219_PLL_FREQ_OUT_98304); |
| 49 |
if (ret) |
| 50 |
dev_err(card->dev, "failed to start PLL: %d\n", ret); |
| 51 |
} |
| 52 |
|
| 53 |
return ret; |
| 54 |
} |
| 55 |
|
| 56 |
static const struct snd_soc_dapm_widget card_widgets[] = { |
| 57 |
SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 58 |
SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 59 |
SND_SOC_DAPM_LINE("Line Out", NULL), |
| 60 |
SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control, |
| 61 |
SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_PRE_PMU), |
| 62 |
}; |
| 63 |
|
| 64 |
static const struct snd_soc_dapm_route card_base_routes[] = { |
| 65 |
/* HP jack connectors - unknown if we have jack detection */ |
| 66 |
{"Headphone Jack", NULL, "HPL"}, |
| 67 |
{"Headphone Jack", NULL, "HPR"}, |
| 68 |
|
| 69 |
{"MIC", NULL, "Headset Mic"}, |
| 70 |
|
| 71 |
{ "Headphone Jack", NULL, "Platform Clock" }, |
| 72 |
{ "Headset Mic", NULL, "Platform Clock" }, |
| 73 |
{ "Line Out", NULL, "Platform Clock" }, |
| 74 |
}; |
| 75 |
|
| 76 |
static const struct snd_soc_jack_pin card_headset_pins[] = { |
| 77 |
{ |
| 78 |
.pin = "Headphone Jack", |
| 79 |
.mask = SND_JACK_HEADPHONE, |
| 80 |
}, |
| 81 |
{ |
| 82 |
.pin = "Headset Mic", |
| 83 |
.mask = SND_JACK_MICROPHONE, |
| 84 |
}, |
| 85 |
{ |
| 86 |
.pin = "Line Out", |
| 87 |
.mask = SND_JACK_LINEOUT, |
| 88 |
}, |
| 89 |
}; |
| 90 |
|
| 91 |
static int avs_da7219_codec_init(struct snd_soc_pcm_runtime *runtime) |
| 92 |
{ |
| 93 |
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0); |
| 94 |
struct snd_soc_component *component = codec_dai->component; |
| 95 |
struct snd_soc_card *card = runtime->card; |
| 96 |
struct snd_soc_jack_pin *pins; |
| 97 |
struct snd_soc_jack *jack; |
| 98 |
int num_pins; |
| 99 |
int clk_freq; |
| 100 |
int ret; |
| 101 |
|
| 102 |
jack = snd_soc_card_get_drvdata(card); |
| 103 |
if (soc_intel_is_apl()) |
| 104 |
clk_freq = 19200000; |
| 105 |
else /* kbl */ |
| 106 |
clk_freq = 24576000; |
| 107 |
|
| 108 |
ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, clk_freq, SND_SOC_CLOCK_IN); |
| 109 |
if (ret) { |
| 110 |
dev_err(card->dev, "can't set codec sysclk configuration\n"); |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
num_pins = ARRAY_SIZE(card_headset_pins); |
| 115 |
pins = devm_kmemdup_array(card->dev, card_headset_pins, num_pins, |
| 116 |
sizeof(card_headset_pins[0]), GFP_KERNEL); |
| 117 |
if (!pins) |
| 118 |
return -ENOMEM; |
| 119 |
|
| 120 |
/* |
| 121 |
* Headset buttons map to the google Reference headset. |
| 122 |
* These can be configured by userspace. |
| 123 |
*/ |
| 124 |
ret = snd_soc_card_jack_new_pins(card, "Headset Jack", |
| 125 |
SND_JACK_HEADSET | SND_JACK_BTN_0 | |
| 126 |
SND_JACK_BTN_1 | SND_JACK_BTN_2 | |
| 127 |
SND_JACK_BTN_3 | SND_JACK_LINEOUT, |
| 128 |
jack, pins, num_pins); |
| 129 |
if (ret) { |
| 130 |
dev_err(card->dev, "Headset Jack creation failed: %d\n", ret); |
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); |
| 135 |
snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP); |
| 136 |
snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); |
| 137 |
snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND); |
| 138 |
|
| 139 |
return snd_soc_component_set_jack(component, jack, NULL); |
| 140 |
} |
| 141 |
|
| 142 |
static void avs_da7219_codec_exit(struct snd_soc_pcm_runtime *rtd) |
| 143 |
{ |
| 144 |
snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL); |
| 145 |
} |
| 146 |
|
| 147 |
static int |
| 148 |
avs_da7219_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params) |
| 149 |
{ |
| 150 |
struct snd_interval *rate, *channels; |
| 151 |
struct snd_mask *fmt; |
| 152 |
|
| 153 |
rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 154 |
channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 155 |
fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 156 |
|
| 157 |
/* The ADSP will convert the FE rate to 48k, stereo */ |
| 158 |
rate->min = rate->max = 48000; |
| 159 |
channels->min = channels->max = 2; |
| 160 |
|
| 161 |
/* set SSP0 to 24 bit */ |
| 162 |
snd_mask_none(fmt); |
| 163 |
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static int avs_create_dai_link(struct device *dev, int ssp_port, int tdm_slot, |
| 168 |
struct snd_soc_dai_link **dai_link) |
| 169 |
{ |
| 170 |
struct snd_soc_dai_link_component *platform; |
| 171 |
struct snd_soc_dai_link *dl; |
| 172 |
|
| 173 |
dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); |
| 174 |
platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); |
| 175 |
if (!dl || !platform) |
| 176 |
return -ENOMEM; |
| 177 |
|
| 178 |
dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port); |
| 179 |
dl->name = devm_kasprintf(dev, GFP_KERNEL, |
| 180 |
AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot)); |
| 181 |
dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); |
| 182 |
dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); |
| 183 |
if (!dl->name || !dl->cpus || !dl->codecs) |
| 184 |
return -ENOMEM; |
| 185 |
|
| 186 |
dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, |
| 187 |
AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot)); |
| 188 |
dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-DLGS7219:00"); |
| 189 |
dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, DA7219_DAI_NAME); |
| 190 |
if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) |
| 191 |
return -ENOMEM; |
| 192 |
|
| 193 |
platform->name = dev_name(dev); |
| 194 |
dl->num_cpus = 1; |
| 195 |
dl->num_codecs = 1; |
| 196 |
dl->platforms = platform; |
| 197 |
dl->num_platforms = 1; |
| 198 |
dl->id = 0; |
| 199 |
dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC; |
| 200 |
dl->be_hw_params_fixup = avs_da7219_be_fixup; |
| 201 |
dl->init = avs_da7219_codec_init; |
| 202 |
dl->exit = avs_da7219_codec_exit; |
| 203 |
dl->nonatomic = 1; |
| 204 |
dl->no_pcm = 1; |
| 205 |
|
| 206 |
*dai_link = dl; |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
static int avs_da7219_probe(struct platform_device *pdev) |
| 212 |
{ |
| 213 |
struct snd_soc_dai_link *dai_link; |
| 214 |
struct snd_soc_acpi_mach *mach; |
| 215 |
struct avs_mach_pdata *pdata; |
| 216 |
struct snd_soc_card *card; |
| 217 |
struct snd_soc_jack *jack; |
| 218 |
struct device *dev = &pdev->dev; |
| 219 |
int ssp_port, tdm_slot, ret; |
| 220 |
|
| 221 |
mach = dev_get_platdata(dev); |
| 222 |
pdata = mach->pdata; |
| 223 |
|
| 224 |
ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot); |
| 225 |
if (ret) |
| 226 |
return ret; |
| 227 |
|
| 228 |
ret = avs_create_dai_link(dev, ssp_port, tdm_slot, &dai_link); |
| 229 |
if (ret) { |
| 230 |
dev_err(dev, "Failed to create dai link: %d", ret); |
| 231 |
return ret; |
| 232 |
} |
| 233 |
|
| 234 |
jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); |
| 235 |
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); |
| 236 |
if (!jack || !card) |
| 237 |
return -ENOMEM; |
| 238 |
|
| 239 |
if (pdata->obsolete_card_names) { |
| 240 |
card->name = "avs_da7219"; |
| 241 |
} else { |
| 242 |
card->driver_name = "avs_da7219"; |
| 243 |
card->long_name = card->name = "AVS I2S DA7219"; |
| 244 |
} |
| 245 |
card->dev = dev; |
| 246 |
card->owner = THIS_MODULE; |
| 247 |
card->dai_link = dai_link; |
| 248 |
card->num_links = 1; |
| 249 |
card->controls = card_controls; |
| 250 |
card->num_controls = ARRAY_SIZE(card_controls); |
| 251 |
card->dapm_widgets = card_widgets; |
| 252 |
card->num_dapm_widgets = ARRAY_SIZE(card_widgets); |
| 253 |
card->dapm_routes = card_base_routes; |
| 254 |
card->num_dapm_routes = ARRAY_SIZE(card_base_routes); |
| 255 |
card->fully_routed = true; |
| 256 |
snd_soc_card_set_drvdata(card, jack); |
| 257 |
|
| 258 |
return devm_snd_soc_register_deferrable_card(dev, card); |
| 259 |
} |
| 260 |
|
| 261 |
static const struct platform_device_id avs_da7219_driver_ids[] = { |
| 262 |
{ |
| 263 |
.name = "avs_da7219", |
| 264 |
}, |
| 265 |
{}, |
| 266 |
}; |
| 267 |
MODULE_DEVICE_TABLE(platform, avs_da7219_driver_ids); |
| 268 |
|
| 269 |
static struct platform_driver avs_da7219_driver = { |
| 270 |
.probe = avs_da7219_probe, |
| 271 |
.driver = { |
| 272 |
.name = "avs_da7219", |
| 273 |
.pm = &snd_soc_pm_ops, |
| 274 |
}, |
| 275 |
.id_table = avs_da7219_driver_ids, |
| 276 |
}; |
| 277 |
|
| 278 |
module_platform_driver(avs_da7219_driver); |
| 279 |
|
| 280 |
MODULE_DESCRIPTION("Intel da7219 machine driver"); |
| 281 |
MODULE_AUTHOR("Cezary Rojewski <[email protected]>"); |
| 282 |
MODULE_LICENSE("GPL"); |