Path: blob/master/sound/soc/intel/boards/bytcht_da7213.c
26493 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* bytcht-da7213.c - ASoc Machine driver for Intel Baytrail and3* Cherrytrail-based platforms, with Dialog DA7213 codec4*5* Copyright (C) 2017 Intel Corporation6* Author: Pierre-Louis Bossart <[email protected]>7*8* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~9*10* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~11*/1213#include <linux/module.h>14#include <linux/acpi.h>15#include <linux/platform_device.h>16#include <linux/slab.h>17#include <sound/pcm.h>18#include <sound/pcm_params.h>19#include <sound/soc.h>20#include <sound/soc-acpi.h>21#include "../../codecs/da7213.h"22#include "../atom/sst-atom-controls.h"2324static const struct snd_kcontrol_new controls[] = {25SOC_DAPM_PIN_SWITCH("Headphone Jack"),26SOC_DAPM_PIN_SWITCH("Headset Mic"),27SOC_DAPM_PIN_SWITCH("Mic"),28SOC_DAPM_PIN_SWITCH("Aux In"),29};3031static const struct snd_soc_dapm_widget dapm_widgets[] = {32SND_SOC_DAPM_HP("Headphone Jack", NULL),33SND_SOC_DAPM_MIC("Headset Mic", NULL),34SND_SOC_DAPM_MIC("Mic", NULL),35SND_SOC_DAPM_LINE("Aux In", NULL),36};3738static const struct snd_soc_dapm_route audio_map[] = {39{"Headphone Jack", NULL, "HPL"},40{"Headphone Jack", NULL, "HPR"},4142{"AUXL", NULL, "Aux In"},43{"AUXR", NULL, "Aux In"},4445/* Assume Mic1 is linked to Headset and Mic2 to on-board mic */46{"MIC1", NULL, "Headset Mic"},47{"MIC2", NULL, "Mic"},4849/* SOC-codec link */50{"ssp2 Tx", NULL, "codec_out0"},51{"ssp2 Tx", NULL, "codec_out1"},52{"codec_in0", NULL, "ssp2 Rx"},53{"codec_in1", NULL, "ssp2 Rx"},5455{"Playback", NULL, "ssp2 Tx"},56{"ssp2 Rx", NULL, "Capture"},57};5859static int codec_fixup(struct snd_soc_pcm_runtime *rtd,60struct snd_pcm_hw_params *params)61{62int ret;63struct snd_interval *rate = hw_param_interval(params,64SNDRV_PCM_HW_PARAM_RATE);65struct snd_interval *channels = hw_param_interval(params,66SNDRV_PCM_HW_PARAM_CHANNELS);6768/* The DSP will convert the FE rate to 48k, stereo, 24bits */69rate->min = rate->max = 48000;70channels->min = channels->max = 2;7172/* set SSP2 to 24-bit */73params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);7475/*76* Default mode for SSP configuration is TDM 4 slot, override config77* with explicit setting to I2S 2ch 24-bit. The word length is set with78* dai_set_tdm_slot() since there is no other API exposed79*/80ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),81SND_SOC_DAIFMT_I2S |82SND_SOC_DAIFMT_NB_NF |83SND_SOC_DAIFMT_BP_FP);84if (ret < 0) {85dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);86return ret;87}8889ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 24);90if (ret < 0) {91dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);92return ret;93}9495return 0;96}9798static int aif1_startup(struct snd_pcm_substream *substream)99{100return snd_pcm_hw_constraint_single(substream->runtime,101SNDRV_PCM_HW_PARAM_RATE, 48000);102}103104static int aif1_hw_params(struct snd_pcm_substream *substream,105struct snd_pcm_hw_params *params)106{107struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);108struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);109int ret;110111ret = snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK,11219200000, SND_SOC_CLOCK_IN);113if (ret < 0)114dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");115116ret = snd_soc_dai_set_pll(codec_dai, 0,117DA7213_SYSCLK_PLL_SRM, 0, DA7213_PLL_FREQ_OUT_98304000);118if (ret < 0) {119dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);120return -EIO;121}122123return ret;124}125126static int aif1_hw_free(struct snd_pcm_substream *substream)127{128struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);129struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);130int ret;131132ret = snd_soc_dai_set_pll(codec_dai, 0,133DA7213_SYSCLK_MCLK, 0, 0);134if (ret < 0) {135dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);136return -EIO;137}138139return ret;140}141142static const struct snd_soc_ops aif1_ops = {143.startup = aif1_startup,144};145146static const struct snd_soc_ops ssp2_ops = {147.hw_params = aif1_hw_params,148.hw_free = aif1_hw_free,149150};151152SND_SOC_DAILINK_DEF(dummy,153DAILINK_COMP_ARRAY(COMP_DUMMY()));154155SND_SOC_DAILINK_DEF(media,156DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));157158SND_SOC_DAILINK_DEF(deepbuffer,159DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));160161SND_SOC_DAILINK_DEF(ssp2_port,162DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));163SND_SOC_DAILINK_DEF(ssp2_codec,164DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7213:00",165"da7213-hifi")));166167SND_SOC_DAILINK_DEF(platform,168DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));169170static struct snd_soc_dai_link dailink[] = {171[MERR_DPCM_AUDIO] = {172.name = "Audio Port",173.stream_name = "Audio",174.nonatomic = true,175.dynamic = 1,176.ops = &aif1_ops,177SND_SOC_DAILINK_REG(media, dummy, platform),178},179[MERR_DPCM_DEEP_BUFFER] = {180.name = "Deep-Buffer Audio Port",181.stream_name = "Deep-Buffer Audio",182.nonatomic = true,183.dynamic = 1,184.playback_only = 1,185.ops = &aif1_ops,186SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),187},188/* CODEC<->CODEC link */189/* back ends */190{191.name = "SSP2-Codec",192.id = 0,193.no_pcm = 1,194.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF195| SND_SOC_DAIFMT_CBC_CFC,196.be_hw_params_fixup = codec_fixup,197.ops = &ssp2_ops,198SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),199},200};201202/* use space before codec name to simplify card ID, and simplify driver name */203#define SOF_CARD_NAME "bytcht da7213" /* card name will be 'sof-bytcht da7213' */204#define SOF_DRIVER_NAME "SOF"205206#define CARD_NAME "bytcht-da7213"207#define DRIVER_NAME NULL /* card name will be used for driver name */208209/* SoC card */210static struct snd_soc_card bytcht_da7213_card = {211.name = CARD_NAME,212.driver_name = DRIVER_NAME,213.owner = THIS_MODULE,214.dai_link = dailink,215.num_links = ARRAY_SIZE(dailink),216.controls = controls,217.num_controls = ARRAY_SIZE(controls),218.dapm_widgets = dapm_widgets,219.num_dapm_widgets = ARRAY_SIZE(dapm_widgets),220.dapm_routes = audio_map,221.num_dapm_routes = ARRAY_SIZE(audio_map),222};223224static char codec_name[SND_ACPI_I2C_ID_LEN];225226static int bytcht_da7213_probe(struct platform_device *pdev)227{228struct snd_soc_card *card;229struct snd_soc_acpi_mach *mach;230const char *platform_name;231struct acpi_device *adev;232bool sof_parent;233int dai_index = 0;234int ret_val = 0;235int i;236237mach = pdev->dev.platform_data;238card = &bytcht_da7213_card;239card->dev = &pdev->dev;240241/* fix index of codec dai */242for (i = 0; i < ARRAY_SIZE(dailink); i++) {243if (dailink[i].num_codecs &&244!strcmp(dailink[i].codecs->name, "i2c-DLGS7213:00")) {245dai_index = i;246break;247}248}249250/* fixup codec name based on HID */251adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);252if (adev) {253snprintf(codec_name, sizeof(codec_name),254"i2c-%s", acpi_dev_name(adev));255dailink[dai_index].codecs->name = codec_name;256} else {257dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);258return -ENOENT;259}260261acpi_dev_put(adev);262263/* override platform name, if required */264platform_name = mach->mach_params.platform;265266ret_val = snd_soc_fixup_dai_links_platform_name(card, platform_name);267if (ret_val)268return ret_val;269270sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);271272/* set card and driver name */273if (sof_parent) {274bytcht_da7213_card.name = SOF_CARD_NAME;275bytcht_da7213_card.driver_name = SOF_DRIVER_NAME;276} else {277bytcht_da7213_card.name = CARD_NAME;278bytcht_da7213_card.driver_name = DRIVER_NAME;279}280281/* set pm ops */282if (sof_parent)283pdev->dev.driver->pm = &snd_soc_pm_ops;284285ret_val = devm_snd_soc_register_card(&pdev->dev, card);286if (ret_val) {287dev_err(&pdev->dev,288"snd_soc_register_card failed %d\n", ret_val);289return ret_val;290}291platform_set_drvdata(pdev, card);292return ret_val;293}294295static struct platform_driver bytcht_da7213_driver = {296.driver = {297.name = "bytcht_da7213",298},299.probe = bytcht_da7213_probe,300};301module_platform_driver(bytcht_da7213_driver);302303MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail+DA7213 Machine driver");304MODULE_AUTHOR("Pierre-Louis Bossart");305MODULE_LICENSE("GPL v2");306MODULE_ALIAS("platform:bytcht_da7213");307308309