18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci// Copyright (c) 2020 Intel Corporation
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci *  sof_sdw_rt5682 - Helpers to handle RT5682 from generic machine driver
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/device.h>
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/input.h>
118c2ecf20Sopenharmony_ci#include <linux/soundwire/sdw.h>
128c2ecf20Sopenharmony_ci#include <linux/soundwire/sdw_type.h>
138c2ecf20Sopenharmony_ci#include <sound/control.h>
148c2ecf20Sopenharmony_ci#include <sound/soc.h>
158c2ecf20Sopenharmony_ci#include <sound/soc-acpi.h>
168c2ecf20Sopenharmony_ci#include <sound/soc-dapm.h>
178c2ecf20Sopenharmony_ci#include <sound/jack.h>
188c2ecf20Sopenharmony_ci#include "sof_sdw_common.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget rt5682_widgets[] = {
218c2ecf20Sopenharmony_ci	SND_SOC_DAPM_HP("Headphone", NULL),
228c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Headset Mic", NULL),
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route rt5682_map[] = {
268c2ecf20Sopenharmony_ci	/*Headphones*/
278c2ecf20Sopenharmony_ci	{ "Headphone", NULL, "rt5682 HPOL" },
288c2ecf20Sopenharmony_ci	{ "Headphone", NULL, "rt5682 HPOR" },
298c2ecf20Sopenharmony_ci	{ "rt5682 IN1P", NULL, "Headset Mic" },
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new rt5682_controls[] = {
338c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Headphone"),
348c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Headset Mic"),
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic struct snd_soc_jack_pin rt5682_jack_pins[] = {
388c2ecf20Sopenharmony_ci	{
398c2ecf20Sopenharmony_ci		.pin    = "Headphone",
408c2ecf20Sopenharmony_ci		.mask   = SND_JACK_HEADPHONE,
418c2ecf20Sopenharmony_ci	},
428c2ecf20Sopenharmony_ci	{
438c2ecf20Sopenharmony_ci		.pin    = "Headset Mic",
448c2ecf20Sopenharmony_ci		.mask   = SND_JACK_MICROPHONE,
458c2ecf20Sopenharmony_ci	},
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic int rt5682_rtd_init(struct snd_soc_pcm_runtime *rtd)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct snd_soc_card *card = rtd->card;
518c2ecf20Sopenharmony_ci	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
528c2ecf20Sopenharmony_ci	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
538c2ecf20Sopenharmony_ci	struct snd_soc_component *component = codec_dai->component;
548c2ecf20Sopenharmony_ci	struct snd_soc_jack *jack;
558c2ecf20Sopenharmony_ci	int ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
588c2ecf20Sopenharmony_ci					  "%s hs:rt5682",
598c2ecf20Sopenharmony_ci					  card->components);
608c2ecf20Sopenharmony_ci	if (!card->components)
618c2ecf20Sopenharmony_ci		return -ENOMEM;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = snd_soc_add_card_controls(card, rt5682_controls,
648c2ecf20Sopenharmony_ci					ARRAY_SIZE(rt5682_controls));
658c2ecf20Sopenharmony_ci	if (ret) {
668c2ecf20Sopenharmony_ci		dev_err(card->dev, "rt5682 control addition failed: %d\n", ret);
678c2ecf20Sopenharmony_ci		return ret;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	ret = snd_soc_dapm_new_controls(&card->dapm, rt5682_widgets,
718c2ecf20Sopenharmony_ci					ARRAY_SIZE(rt5682_widgets));
728c2ecf20Sopenharmony_ci	if (ret) {
738c2ecf20Sopenharmony_ci		dev_err(card->dev, "rt5682 widgets addition failed: %d\n", ret);
748c2ecf20Sopenharmony_ci		return ret;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	ret = snd_soc_dapm_add_routes(&card->dapm, rt5682_map,
788c2ecf20Sopenharmony_ci				      ARRAY_SIZE(rt5682_map));
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (ret) {
818c2ecf20Sopenharmony_ci		dev_err(card->dev, "rt5682 map addition failed: %d\n", ret);
828c2ecf20Sopenharmony_ci		return ret;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
868c2ecf20Sopenharmony_ci				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
878c2ecf20Sopenharmony_ci				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
888c2ecf20Sopenharmony_ci				    SND_JACK_BTN_3,
898c2ecf20Sopenharmony_ci				    &ctx->sdw_headset,
908c2ecf20Sopenharmony_ci				    rt5682_jack_pins,
918c2ecf20Sopenharmony_ci				    ARRAY_SIZE(rt5682_jack_pins));
928c2ecf20Sopenharmony_ci	if (ret) {
938c2ecf20Sopenharmony_ci		dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n",
948c2ecf20Sopenharmony_ci			ret);
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	jack = &ctx->sdw_headset;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
1018c2ecf20Sopenharmony_ci	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
1028c2ecf20Sopenharmony_ci	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
1038c2ecf20Sopenharmony_ci	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	ret = snd_soc_component_set_jack(component, jack, NULL);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (ret)
1088c2ecf20Sopenharmony_ci		dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n",
1098c2ecf20Sopenharmony_ci			ret);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciint sof_sdw_rt5682_init(const struct snd_soc_acpi_link_adr *link,
1158c2ecf20Sopenharmony_ci			struct snd_soc_dai_link *dai_links,
1168c2ecf20Sopenharmony_ci			struct sof_sdw_codec_info *info,
1178c2ecf20Sopenharmony_ci			bool playback)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * headset should be initialized once.
1218c2ecf20Sopenharmony_ci	 * Do it with dai link for playback.
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	if (!playback)
1248c2ecf20Sopenharmony_ci		return 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	dai_links->init = rt5682_rtd_init;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
130