Path: blob/master/sound/soc/intel/boards/cht_bsw_nau8824.c
26493 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* cht-bsw-nau8824.c - ASoc Machine driver for Intel Cherryview-based3* platforms Cherrytrail and Braswell, with nau8824 codec.4*5* Copyright (C) 2018 Intel Corp6* Copyright (C) 2018 Nuvoton Technology Corp7*8* Author: Wang, Joseph C <[email protected]>9* Co-author: John Hsu <[email protected]>10* This file is based on cht_bsw_rt5672.c and cht-bsw-max98090.c11*/1213#include <linux/module.h>14#include <linux/platform_device.h>15#include <linux/slab.h>16#include <sound/pcm.h>17#include <sound/pcm_params.h>18#include <sound/soc.h>19#include <sound/soc-acpi.h>20#include <sound/jack.h>21#include <linux/input.h>22#include "../atom/sst-atom-controls.h"23#include "../../codecs/nau8824.h"2425struct cht_mc_private {26struct snd_soc_jack jack;27};2829static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {30{31.pin = "Headphone",32.mask = SND_JACK_HEADPHONE,33},34{35.pin = "Headset Mic",36.mask = SND_JACK_MICROPHONE,37},38};3940static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {41SND_SOC_DAPM_HP("Headphone", NULL),42SND_SOC_DAPM_MIC("Headset Mic", NULL),43SND_SOC_DAPM_MIC("Int Mic", NULL),44SND_SOC_DAPM_SPK("Ext Spk", NULL),45};4647static const struct snd_soc_dapm_route cht_audio_map[] = {48{"Ext Spk", NULL, "SPKOUTL"},49{"Ext Spk", NULL, "SPKOUTR"},50{"Headphone", NULL, "HPOL"},51{"Headphone", NULL, "HPOR"},52{"MIC1", NULL, "Int Mic"},53{"MIC2", NULL, "Int Mic"},54{"HSMIC1", NULL, "Headset Mic"},55{"HSMIC2", NULL, "Headset Mic"},56{"Playback", NULL, "ssp2 Tx"},57{"ssp2 Tx", NULL, "codec_out0"},58{"ssp2 Tx", NULL, "codec_out1"},59{"codec_in0", NULL, "ssp2 Rx" },60{"codec_in1", NULL, "ssp2 Rx" },61{"ssp2 Rx", NULL, "Capture"},62};6364static const struct snd_kcontrol_new cht_mc_controls[] = {65SOC_DAPM_PIN_SWITCH("Headphone"),66SOC_DAPM_PIN_SWITCH("Headset Mic"),67SOC_DAPM_PIN_SWITCH("Int Mic"),68SOC_DAPM_PIN_SWITCH("Ext Spk"),69};7071static int cht_aif1_hw_params(struct snd_pcm_substream *substream,72struct snd_pcm_hw_params *params)73{74struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);75struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);76int ret;7778ret = snd_soc_dai_set_sysclk(codec_dai, NAU8824_CLK_FLL_FS, 0,79SND_SOC_CLOCK_IN);80if (ret < 0) {81dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);82return ret;83}84ret = snd_soc_dai_set_pll(codec_dai, 0, 0, params_rate(params),85params_rate(params) * 256);86if (ret < 0) {87dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);88return ret;89}9091return 0;92}9394static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)95{96struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);97struct snd_soc_jack *jack = &ctx->jack;98struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);99struct snd_soc_component *component = codec_dai->component;100int ret, jack_type;101102/* NAU88L24 supports 4 buttons headset detection103* KEY_PLAYPAUSE104* KEY_VOICECOMMAND105* KEY_VOLUMEUP106* KEY_VOLUMEDOWN107*/108jack_type = SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |109SND_JACK_BTN_2 | SND_JACK_BTN_3;110ret = snd_soc_card_jack_new_pins(runtime->card, "Headset", jack_type,111jack, cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));112if (ret) {113dev_err(runtime->dev,114"Headset Jack creation failed %d\n", ret);115return ret;116}117snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);118snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);119snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);120snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);121122nau8824_enable_jack_detect(component, jack);123124return ret;125}126127static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,128struct snd_pcm_hw_params *params)129{130struct snd_interval *rate = hw_param_interval(params,131SNDRV_PCM_HW_PARAM_RATE);132struct snd_interval *channels = hw_param_interval(params,133SNDRV_PCM_HW_PARAM_CHANNELS);134struct snd_mask *fmt =135hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);136int ret;137138/* The DSP will convert the FE rate to 48k, stereo, 24bits */139rate->min = rate->max = 48000;140channels->min = channels->max = 2;141142/* set SSP2 to 24-bit */143snd_mask_none(fmt);144params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);145146/* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */147ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_codec(rtd, 0), 0xf, 0x1, 4, 24);148if (ret < 0) {149dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);150return ret;151}152153return 0;154}155156static int cht_aif1_startup(struct snd_pcm_substream *substream)157{158return snd_pcm_hw_constraint_single(substream->runtime,159SNDRV_PCM_HW_PARAM_RATE, 48000);160}161162static const struct snd_soc_ops cht_aif1_ops = {163.startup = cht_aif1_startup,164};165166static const struct snd_soc_ops cht_be_ssp2_ops = {167.hw_params = cht_aif1_hw_params,168};169170SND_SOC_DAILINK_DEF(dummy,171DAILINK_COMP_ARRAY(COMP_DUMMY()));172173SND_SOC_DAILINK_DEF(media,174DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));175176SND_SOC_DAILINK_DEF(deepbuffer,177DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));178179SND_SOC_DAILINK_DEF(ssp2_port,180DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));181SND_SOC_DAILINK_DEF(ssp2_codec,182DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10508824:00",183NAU8824_CODEC_DAI)));184185SND_SOC_DAILINK_DEF(platform,186DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));187188static struct snd_soc_dai_link cht_dailink[] = {189/* Front End DAI links */190[MERR_DPCM_AUDIO] = {191.name = "Audio Port",192.stream_name = "Audio",193.nonatomic = true,194.dynamic = 1,195.ops = &cht_aif1_ops,196SND_SOC_DAILINK_REG(media, dummy, platform),197},198[MERR_DPCM_DEEP_BUFFER] = {199.name = "Deep-Buffer Audio Port",200.stream_name = "Deep-Buffer Audio",201.nonatomic = true,202.dynamic = 1,203.playback_only = 1,204.ops = &cht_aif1_ops,205SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),206},207/* Back End DAI links */208{209/* SSP2 - Codec */210.name = "SSP2-Codec",211.id = 0,212.no_pcm = 1,213.dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF214| SND_SOC_DAIFMT_CBC_CFC,215.init = cht_codec_init,216.be_hw_params_fixup = cht_codec_fixup,217.ops = &cht_be_ssp2_ops,218SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),219},220};221222/* use space before codec name to simplify card ID, and simplify driver name */223#define SOF_CARD_NAME "bytcht nau8824" /* card name will be 'sof-bytcht nau8824 */224#define SOF_DRIVER_NAME "SOF"225226#define CARD_NAME "chtnau8824"227#define DRIVER_NAME NULL /* card name will be used for driver name */228229/* SoC card */230static struct snd_soc_card snd_soc_card_cht = {231.owner = THIS_MODULE,232.dai_link = cht_dailink,233.num_links = ARRAY_SIZE(cht_dailink),234.dapm_widgets = cht_dapm_widgets,235.num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),236.dapm_routes = cht_audio_map,237.num_dapm_routes = ARRAY_SIZE(cht_audio_map),238.controls = cht_mc_controls,239.num_controls = ARRAY_SIZE(cht_mc_controls),240};241242static int snd_cht_mc_probe(struct platform_device *pdev)243{244struct cht_mc_private *drv;245struct snd_soc_acpi_mach *mach;246const char *platform_name;247bool sof_parent;248int ret_val;249250drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);251if (!drv)252return -ENOMEM;253snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);254255/* override platform name, if required */256snd_soc_card_cht.dev = &pdev->dev;257mach = pdev->dev.platform_data;258platform_name = mach->mach_params.platform;259260ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht,261platform_name);262if (ret_val)263return ret_val;264265sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);266267/* set card and driver name */268if (sof_parent) {269snd_soc_card_cht.name = SOF_CARD_NAME;270snd_soc_card_cht.driver_name = SOF_DRIVER_NAME;271} else {272snd_soc_card_cht.name = CARD_NAME;273snd_soc_card_cht.driver_name = DRIVER_NAME;274}275276snd_soc_card_cht.components = nau8824_components();277278/* set pm ops */279if (sof_parent)280pdev->dev.driver->pm = &snd_soc_pm_ops;281282/* register the soc card */283ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);284if (ret_val) {285dev_err(&pdev->dev,286"snd_soc_register_card failed %d\n", ret_val);287return ret_val;288}289platform_set_drvdata(pdev, &snd_soc_card_cht);290291return ret_val;292}293294static struct platform_driver snd_cht_mc_driver = {295.driver = {296.name = "cht-bsw-nau8824",297},298.probe = snd_cht_mc_probe,299};300301module_platform_driver(snd_cht_mc_driver);302303MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");304MODULE_AUTHOR("Wang, Joseph C <[email protected]>");305MODULE_AUTHOR("John Hsu <[email protected]>");306MODULE_LICENSE("GPL v2");307MODULE_ALIAS("platform:cht-bsw-nau8824");308309310