linux/sound/soc/intel/boards/sof_rt5682.c

// SPDX-License-Identifier: GPL-2.0-only
// Copyright(c) 2019-2020 Intel Corporation.

/*
 * Intel SOF Machine Driver with Realtek rt5682 Codec
 * and speaker codec MAX98357A or RT1015.
 */
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/dmi.h>
#include <sound/core.h>
#include <sound/jack.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/sof.h>
#include <sound/rt5682.h>
#include <sound/rt5682s.h>
#include <sound/soc-acpi.h>
#include "../../codecs/rt5682.h"
#include "../../codecs/rt5682s.h"
#include "../../codecs/rt5645.h"
#include "../common/soc-intel-quirks.h"
#include "sof_board_helpers.h"
#include "sof_maxim_common.h"
#include "sof_realtek_common.h"

/* Driver-specific board quirks: from bit 0 to 7 */
#define SOF_RT5682_MCLK_EN

/* Default: MCLK on, MCLK 19.2M, SSP0  */
static unsigned long sof_rt5682_quirk =;

static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
{}

static const struct dmi_system_id sof_rt5682_quirk_table[] =;

static struct snd_soc_jack_pin jack_pins[] =;

static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
{
	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
	struct snd_soc_jack *jack = &ctx->headset_jack;
	int extra_jack_data;
	int ret, mclk_freq;

	if (ctx->rt5682.mclk_en) {
		mclk_freq = sof_dai_get_mclk(rtd);
		if (mclk_freq <= 0) {
			dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_freq);
			return -EINVAL;
		}

		/* need to enable ASRC function for 24MHz mclk rate */
		if (mclk_freq == 24000000) {
			dev_info(rtd->dev, "enable ASRC\n");

			switch (ctx->codec_type) {
			case CODEC_RT5650:
				rt5645_sel_asrc_clk_src(component,
							RT5645_DA_STEREO_FILTER |
							RT5645_AD_STEREO_FILTER,
							RT5645_CLK_SEL_I2S1_ASRC);
				rt5645_sel_asrc_clk_src(component,
							RT5645_DA_MONO_L_FILTER |
							RT5645_DA_MONO_R_FILTER,
							RT5645_CLK_SEL_I2S2_ASRC);
				break;
			case CODEC_RT5682:
				rt5682_sel_asrc_clk_src(component,
							RT5682_DA_STEREO1_FILTER |
							RT5682_AD_STEREO1_FILTER,
							RT5682_CLK_SEL_I2S1_ASRC);
				break;
			case CODEC_RT5682S:
				rt5682s_sel_asrc_clk_src(component,
							 RT5682S_DA_STEREO1_FILTER |
							 RT5682S_AD_STEREO1_FILTER,
							 RT5682S_CLK_SEL_I2S1_ASRC);
				break;
			default:
				dev_err(rtd->dev, "invalid codec type %d\n",
					ctx->codec_type);
				return -EINVAL;
			}
		}

		if (ctx->rt5682.is_legacy_cpu) {
			/*
			 * The firmware might enable the clock at
			 * boot (this information may or may not
			 * be reflected in the enable clock register).
			 * To change the rate we must disable the clock
			 * first to cover these cases. Due to common
			 * clock framework restrictions that do not allow
			 * to disable a clock that has not been enabled,
			 * we need to enable the clock first.
			 */
			ret = clk_prepare_enable(ctx->rt5682.mclk);
			if (!ret)
				clk_disable_unprepare(ctx->rt5682.mclk);

			ret = clk_set_rate(ctx->rt5682.mclk, 19200000);

			if (ret)
				dev_err(rtd->dev, "unable to set MCLK rate\n");
		}
	}

	/*
	 * Headset buttons map to the google Reference headset.
	 * These can be configured by userspace.
	 */
	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
					 SND_JACK_BTN_3,
					 jack,
					 jack_pins,
					 ARRAY_SIZE(jack_pins));
	if (ret) {
		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
		return ret;
	}

	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);

	if (ctx->codec_type == CODEC_RT5650) {
		extra_jack_data = SND_JACK_MICROPHONE | SND_JACK_BTN_0;
		ret = snd_soc_component_set_jack(component, jack, &extra_jack_data);
	} else
		ret = snd_soc_component_set_jack(component, jack, NULL);

	if (ret) {
		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
		return ret;
	}

	return ret;
};

static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
{}

static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params)
{}

static const struct snd_soc_ops sof_rt5682_ops =;

static int sof_card_late_probe(struct snd_soc_card *card)
{}

static const struct snd_kcontrol_new sof_controls[] =;

static const struct snd_soc_dapm_widget sof_widgets[] =;

static const struct snd_soc_dapm_route sof_map[] =;

static const struct snd_kcontrol_new rt5650_spk_kcontrols[] =;

static const struct snd_soc_dapm_widget rt5650_spk_widgets[] =;

static const struct snd_soc_dapm_route rt5650_spk_dapm_routes[] =;

static int rt5650_spk_init(struct snd_soc_pcm_runtime *rtd)
{}

/* sof audio machine driver for rt5682 codec */
static struct snd_soc_card sof_audio_card_rt5682 =;

static struct snd_soc_dai_link_component rt5682_component[] =;

static struct snd_soc_dai_link_component rt5682s_component[] =;

static struct snd_soc_dai_link_component rt5650_components[] =;

static int
sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card,
			  struct sof_card_private *ctx)
{}

#define GLK_LINK_ORDER

static int sof_audio_probe(struct platform_device *pdev)
{}

static const struct platform_device_id board_ids[] =;
MODULE_DEVICE_TABLE(platform, board_ids);

static struct platform_driver sof_audio =;
module_platform_driver()

/* Module information */
MODULE_DESCRIPTION();
MODULE_AUTHOR();
MODULE_AUTHOR();
MODULE_AUTHOR();
MODULE_AUTHOR();
MODULE_LICENSE();
MODULE_IMPORT_NS();
MODULE_IMPORT_NS();
MODULE_IMPORT_NS();