Path: blob/master/sound/soc/intel/boards/bytcht_cx2072x.c
54336 views
// SPDX-License-Identifier: GPL-2.0-only1//2// ASoC DPCM Machine driver for Baytrail / Cherrytrail platforms with3// CX2072X codec4//56#include <linux/acpi.h>7#include <linux/device.h>8#include <linux/gpio/consumer.h>9#include <linux/module.h>10#include <linux/platform_device.h>11#include <linux/slab.h>12#include <sound/pcm.h>13#include <sound/pcm_params.h>14#include <sound/jack.h>15#include <sound/soc.h>16#include <sound/soc-acpi.h>17#include "../../codecs/cx2072x.h"18#include "../atom/sst-atom-controls.h"1920static const struct snd_soc_dapm_widget byt_cht_cx2072x_widgets[] = {21SND_SOC_DAPM_HP("Headphone", NULL),22SND_SOC_DAPM_MIC("Headset Mic", NULL),23SND_SOC_DAPM_MIC("Int Mic", NULL),24SND_SOC_DAPM_SPK("Ext Spk", NULL),25};2627static const struct snd_soc_dapm_route byt_cht_cx2072x_audio_map[] = {28/* External Speakers: HFL, HFR */29{"Headphone", NULL, "PORTA"},30{"Ext Spk", NULL, "PORTG"},31{"PORTC", NULL, "Int Mic"},32{"PORTD", NULL, "Headset Mic"},3334{"Playback", NULL, "ssp2 Tx"},35{"ssp2 Tx", NULL, "codec_out0"},36{"ssp2 Tx", NULL, "codec_out1"},37{"codec_in0", NULL, "ssp2 Rx"},38{"codec_in1", NULL, "ssp2 Rx"},39{"ssp2 Rx", NULL, "Capture"},40};4142static const struct snd_kcontrol_new byt_cht_cx2072x_controls[] = {43SOC_DAPM_PIN_SWITCH("Headphone"),44SOC_DAPM_PIN_SWITCH("Headset Mic"),45SOC_DAPM_PIN_SWITCH("Int Mic"),46SOC_DAPM_PIN_SWITCH("Ext Spk"),47};4849static struct snd_soc_jack byt_cht_cx2072x_headset;5051/* Headset jack detection DAPM pins */52static struct snd_soc_jack_pin byt_cht_cx2072x_headset_pins[] = {53{54.pin = "Headset Mic",55.mask = SND_JACK_MICROPHONE,56},57{58.pin = "Headphone",59.mask = SND_JACK_HEADPHONE,60},61};6263static const struct acpi_gpio_params byt_cht_cx2072x_headset_gpios;64static const struct acpi_gpio_mapping byt_cht_cx2072x_acpi_gpios[] = {65{ "headset-gpios", &byt_cht_cx2072x_headset_gpios, 1 },66{},67};6869static int byt_cht_cx2072x_init(struct snd_soc_pcm_runtime *rtd)70{71struct snd_soc_card *card = rtd->card;72struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);73struct snd_soc_component *codec = snd_soc_rtd_to_codec(rtd, 0)->component;74int ret;7576if (devm_acpi_dev_add_driver_gpios(codec->dev,77byt_cht_cx2072x_acpi_gpios))78dev_warn(rtd->dev, "Unable to add GPIO mapping table\n");7980snd_soc_dapm_set_idle_bias(dapm, false);8182/* set the default PLL rate, the clock is handled by the codec driver */83ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(rtd, 0), CX2072X_MCLK_EXTERNAL_PLL,8419200000, SND_SOC_CLOCK_IN);85if (ret) {86dev_err(rtd->dev, "Could not set sysclk\n");87return ret;88}8990ret = snd_soc_card_jack_new_pins(card, "Headset",91SND_JACK_HEADSET | SND_JACK_BTN_0,92&byt_cht_cx2072x_headset,93byt_cht_cx2072x_headset_pins,94ARRAY_SIZE(byt_cht_cx2072x_headset_pins));95if (ret)96return ret;9798snd_soc_component_set_jack(codec, &byt_cht_cx2072x_headset, NULL);99100snd_soc_dai_set_bclk_ratio(snd_soc_rtd_to_codec(rtd, 0), 50);101102return 0;103}104105static int byt_cht_cx2072x_fixup(struct snd_soc_pcm_runtime *rtd,106struct snd_pcm_hw_params *params)107{108struct snd_interval *rate =109hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);110struct snd_interval *channels =111hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);112int ret;113114/* The DSP will convert the FE rate to 48k, stereo, 24bits */115rate->min = rate->max = 48000;116channels->min = channels->max = 2;117118/* set SSP2 to 24-bit */119params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);120121/*122* Default mode for SSP configuration is TDM 4 slot, override config123* with explicit setting to I2S 2ch 24-bit. The word length is set with124* dai_set_tdm_slot() since there is no other API exposed125*/126ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),127SND_SOC_DAIFMT_I2S |128SND_SOC_DAIFMT_NB_NF |129SND_SOC_DAIFMT_BP_FP);130if (ret < 0) {131dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);132return ret;133}134135ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 24);136if (ret < 0) {137dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);138return ret;139}140141return 0;142}143144static int byt_cht_cx2072x_aif1_startup(struct snd_pcm_substream *substream)145{146return snd_pcm_hw_constraint_single(substream->runtime,147SNDRV_PCM_HW_PARAM_RATE, 48000);148}149150static const struct snd_soc_ops byt_cht_cx2072x_aif1_ops = {151.startup = byt_cht_cx2072x_aif1_startup,152};153154SND_SOC_DAILINK_DEF(dummy,155DAILINK_COMP_ARRAY(COMP_DUMMY()));156157SND_SOC_DAILINK_DEF(media,158DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));159160SND_SOC_DAILINK_DEF(deepbuffer,161DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));162163SND_SOC_DAILINK_DEF(ssp2,164DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));165166SND_SOC_DAILINK_DEF(cx2072x,167DAILINK_COMP_ARRAY(COMP_CODEC("i2c-14F10720:00", "cx2072x-hifi")));168169SND_SOC_DAILINK_DEF(platform,170DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));171172static struct snd_soc_dai_link byt_cht_cx2072x_dais[] = {173[MERR_DPCM_AUDIO] = {174.name = "Audio Port",175.stream_name = "Audio",176.nonatomic = true,177.dynamic = 1,178.ops = &byt_cht_cx2072x_aif1_ops,179SND_SOC_DAILINK_REG(media, dummy, platform),180},181[MERR_DPCM_DEEP_BUFFER] = {182.name = "Deep-Buffer Audio Port",183.stream_name = "Deep-Buffer Audio",184.nonatomic = true,185.dynamic = 1,186.playback_only = 1,187.ops = &byt_cht_cx2072x_aif1_ops,188SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),189},190/* back ends */191{192.name = "SSP2-Codec",193.id = 0,194.no_pcm = 1,195.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF196| SND_SOC_DAIFMT_CBC_CFC,197.init = byt_cht_cx2072x_init,198.be_hw_params_fixup = byt_cht_cx2072x_fixup,199SND_SOC_DAILINK_REG(ssp2, cx2072x, platform),200},201};202203/* use space before codec name to simplify card ID, and simplify driver name */204#define SOF_CARD_NAME "bytcht cx2072x" /* card name will be 'sof-bytcht cx2072x' */205#define SOF_DRIVER_NAME "SOF"206207#define CARD_NAME "bytcht-cx2072x"208#define DRIVER_NAME NULL /* card name will be used for driver name */209210/* SoC card */211static struct snd_soc_card byt_cht_cx2072x_card = {212.name = CARD_NAME,213.driver_name = DRIVER_NAME,214.owner = THIS_MODULE,215.dai_link = byt_cht_cx2072x_dais,216.num_links = ARRAY_SIZE(byt_cht_cx2072x_dais),217.dapm_widgets = byt_cht_cx2072x_widgets,218.num_dapm_widgets = ARRAY_SIZE(byt_cht_cx2072x_widgets),219.dapm_routes = byt_cht_cx2072x_audio_map,220.num_dapm_routes = ARRAY_SIZE(byt_cht_cx2072x_audio_map),221.controls = byt_cht_cx2072x_controls,222.num_controls = ARRAY_SIZE(byt_cht_cx2072x_controls),223};224225static char codec_name[SND_ACPI_I2C_ID_LEN];226227static int snd_byt_cht_cx2072x_probe(struct platform_device *pdev)228{229struct snd_soc_acpi_mach *mach;230struct acpi_device *adev;231int dai_index = 0;232bool sof_parent;233int i, ret;234235byt_cht_cx2072x_card.dev = &pdev->dev;236mach = dev_get_platdata(&pdev->dev);237238/* fix index of codec dai */239for (i = 0; i < ARRAY_SIZE(byt_cht_cx2072x_dais); i++) {240if (byt_cht_cx2072x_dais[i].num_codecs &&241!strcmp(byt_cht_cx2072x_dais[i].codecs->name,242"i2c-14F10720:00")) {243dai_index = i;244break;245}246}247248/* fixup codec name based on HID */249adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);250if (adev) {251snprintf(codec_name, sizeof(codec_name), "i2c-%s",252acpi_dev_name(adev));253byt_cht_cx2072x_dais[dai_index].codecs->name = codec_name;254} else {255dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);256return -ENOENT;257}258259acpi_dev_put(adev);260261/* override platform name, if required */262ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_cx2072x_card,263mach->mach_params.platform);264if (ret)265return ret;266267sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);268269/* set card and driver name */270if (sof_parent) {271byt_cht_cx2072x_card.name = SOF_CARD_NAME;272byt_cht_cx2072x_card.driver_name = SOF_DRIVER_NAME;273} else {274byt_cht_cx2072x_card.name = CARD_NAME;275byt_cht_cx2072x_card.driver_name = DRIVER_NAME;276}277278/* set pm ops */279if (sof_parent)280pdev->dev.driver->pm = &snd_soc_pm_ops;281282return devm_snd_soc_register_card(&pdev->dev, &byt_cht_cx2072x_card);283}284285static struct platform_driver snd_byt_cht_cx2072x_driver = {286.driver = {287.name = "bytcht_cx2072x",288},289.probe = snd_byt_cht_cx2072x_probe,290};291module_platform_driver(snd_byt_cht_cx2072x_driver);292293MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");294MODULE_LICENSE("GPL v2");295MODULE_ALIAS("platform:bytcht_cx2072x");296297298