Path: blob/master/sound/soc/mediatek/mt2701/mt2701-wm8960.c
26488 views
// SPDX-License-Identifier: GPL-2.01/*2* mt2701-wm8960.c -- MT2701 WM8960 ALSA SoC machine driver3*4* Copyright (c) 2017 MediaTek Inc.5* Author: Ryder Lee <[email protected]>6*/78#include <linux/module.h>9#include <sound/soc.h>1011#include "mt2701-afe-common.h"1213static const struct snd_soc_dapm_widget mt2701_wm8960_widgets[] = {14SND_SOC_DAPM_HP("Headphone", NULL),15SND_SOC_DAPM_MIC("AMIC", NULL),16};1718static const struct snd_kcontrol_new mt2701_wm8960_controls[] = {19SOC_DAPM_PIN_SWITCH("Headphone"),20SOC_DAPM_PIN_SWITCH("AMIC"),21};2223static int mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream *substream,24struct snd_pcm_hw_params *params)25{26struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);27struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);28struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);29unsigned int mclk_rate;30unsigned int rate = params_rate(params);31unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;32unsigned int div_bck_over_lrck = 64;3334mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;3536snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);37snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);3839return 0;40}4142static const struct snd_soc_ops mt2701_wm8960_be_ops = {43.hw_params = mt2701_wm8960_be_ops_hw_params44};4546SND_SOC_DAILINK_DEFS(playback,47DAILINK_COMP_ARRAY(COMP_CPU("PCMO0")),48DAILINK_COMP_ARRAY(COMP_DUMMY()),49DAILINK_COMP_ARRAY(COMP_EMPTY()));5051SND_SOC_DAILINK_DEFS(capture,52DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),53DAILINK_COMP_ARRAY(COMP_DUMMY()),54DAILINK_COMP_ARRAY(COMP_EMPTY()));5556SND_SOC_DAILINK_DEFS(codec,57DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),58DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8960-hifi")),59DAILINK_COMP_ARRAY(COMP_EMPTY()));6061static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {62/* FE */63{64.name = "wm8960-playback",65.stream_name = "wm8960-playback",66.trigger = {SND_SOC_DPCM_TRIGGER_POST,67SND_SOC_DPCM_TRIGGER_POST},68.dynamic = 1,69.playback_only = 1,70SND_SOC_DAILINK_REG(playback),71},72{73.name = "wm8960-capture",74.stream_name = "wm8960-capture",75.trigger = {SND_SOC_DPCM_TRIGGER_POST,76SND_SOC_DPCM_TRIGGER_POST},77.dynamic = 1,78.capture_only = 1,79SND_SOC_DAILINK_REG(capture),80},81/* BE */82{83.name = "wm8960-codec",84.no_pcm = 1,85.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBC_CFC86| SND_SOC_DAIFMT_GATED,87.ops = &mt2701_wm8960_be_ops,88SND_SOC_DAILINK_REG(codec),89},90};9192static struct snd_soc_card mt2701_wm8960_card = {93.name = "mt2701-wm8960",94.owner = THIS_MODULE,95.dai_link = mt2701_wm8960_dai_links,96.num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),97.controls = mt2701_wm8960_controls,98.num_controls = ARRAY_SIZE(mt2701_wm8960_controls),99.dapm_widgets = mt2701_wm8960_widgets,100.num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),101};102103static int mt2701_wm8960_machine_probe(struct platform_device *pdev)104{105struct snd_soc_card *card = &mt2701_wm8960_card;106struct device_node *platform_node, *codec_node;107struct snd_soc_dai_link *dai_link;108int ret, i;109110platform_node = of_parse_phandle(pdev->dev.of_node,111"mediatek,platform", 0);112if (!platform_node) {113dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");114return -EINVAL;115}116for_each_card_prelinks(card, i, dai_link) {117if (dai_link->platforms->name)118continue;119dai_link->platforms->of_node = platform_node;120}121122card->dev = &pdev->dev;123124codec_node = of_parse_phandle(pdev->dev.of_node,125"mediatek,audio-codec", 0);126if (!codec_node) {127dev_err(&pdev->dev,128"Property 'audio-codec' missing or invalid\n");129ret = -EINVAL;130goto put_platform_node;131}132for_each_card_prelinks(card, i, dai_link) {133if (dai_link->codecs->name)134continue;135dai_link->codecs->of_node = codec_node;136}137138ret = snd_soc_of_parse_audio_routing(card, "audio-routing");139if (ret) {140dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);141goto put_codec_node;142}143144ret = devm_snd_soc_register_card(&pdev->dev, card);145if (ret)146dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",147__func__, ret);148149put_codec_node:150of_node_put(codec_node);151put_platform_node:152of_node_put(platform_node);153return ret;154}155156#ifdef CONFIG_OF157static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {158{.compatible = "mediatek,mt2701-wm8960-machine",},159{}160};161MODULE_DEVICE_TABLE(of, mt2701_wm8960_machine_dt_match);162#endif163164static struct platform_driver mt2701_wm8960_machine = {165.driver = {166.name = "mt2701-wm8960",167#ifdef CONFIG_OF168.of_match_table = mt2701_wm8960_machine_dt_match,169#endif170},171.probe = mt2701_wm8960_machine_probe,172};173174module_platform_driver(mt2701_wm8960_machine);175176/* Module information */177MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");178MODULE_AUTHOR("Ryder Lee <[email protected]>");179MODULE_LICENSE("GPL v2");180MODULE_ALIAS("mt2701 wm8960 soc card");181182183184