| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * tegra_wm8753.c - Tegra machine ASoC driver for boards using WM8753 codec. |
| 4 | * |
| 5 | * Author: Stephen Warren <swarren@nvidia.com> |
| 6 | * Copyright (C) 2010-2012 - NVIDIA, Inc. |
| 7 | * |
| 8 | * Based on code copyright/by: |
| 9 | * |
| 10 | * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd. |
| 11 | * |
| 12 | * Copyright 2007 Wolfson Microelectronics PLC. |
| 13 | * Author: Graeme Gregory |
| 14 | * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/gpio.h> |
| 21 | #include <linux/of_gpio.h> |
| 22 | |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/jack.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/soc.h> |
| 28 | |
| 29 | #include "../codecs/wm8753.h" |
| 30 | |
| 31 | #include "tegra_asoc_utils.h" |
| 32 | |
| 33 | #define DRV_NAME "tegra-snd-wm8753" |
| 34 | |
| 35 | struct tegra_wm8753 { |
| 36 | struct tegra_asoc_utils_data util_data; |
| 37 | }; |
| 38 | |
| 39 | static int tegra_wm8753_hw_params(struct snd_pcm_substream *substream, |
| 40 | struct snd_pcm_hw_params *params) |
| 41 | { |
| 42 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 43 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 44 | struct snd_soc_card *card = rtd->card; |
| 45 | struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card); |
| 46 | int srate, mclk; |
| 47 | int err; |
| 48 | |
| 49 | srate = params_rate(params); |
| 50 | switch (srate) { |
| 51 | case 11025: |
| 52 | case 22050: |
| 53 | case 44100: |
| 54 | case 88200: |
| 55 | mclk = 11289600; |
| 56 | break; |
| 57 | default: |
| 58 | mclk = 12288000; |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk); |
| 63 | if (err < 0) { |
| 64 | dev_err(card->dev, "Can't configure clocks\n"); |
| 65 | return err; |
| 66 | } |
| 67 | |
| 68 | err = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, mclk, |
| 69 | SND_SOC_CLOCK_IN); |
| 70 | if (err < 0) { |
| 71 | dev_err(card->dev, "codec_dai clock not set\n"); |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static const struct snd_soc_ops tegra_wm8753_ops = { |
| 79 | .hw_params = tegra_wm8753_hw_params, |
| 80 | }; |
| 81 | |
| 82 | static const struct snd_soc_dapm_widget tegra_wm8753_dapm_widgets[] = { |
| 83 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 84 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 85 | }; |
| 86 | |
| 87 | SND_SOC_DAILINK_DEFS(pcm, |
| 88 | DAILINK_COMP_ARRAY(COMP_EMPTY()), |
| 89 | DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8753-hifi")), |
| 90 | DAILINK_COMP_ARRAY(COMP_EMPTY())); |
| 91 | |
| 92 | static struct snd_soc_dai_link tegra_wm8753_dai = { |
| 93 | .name = "WM8753", |
| 94 | .stream_name = "WM8753 PCM", |
| 95 | .ops = &tegra_wm8753_ops, |
| 96 | .dai_fmt = SND_SOC_DAIFMT_I2S | |
| 97 | SND_SOC_DAIFMT_NB_NF | |
| 98 | SND_SOC_DAIFMT_CBS_CFS, |
| 99 | SND_SOC_DAILINK_REG(pcm), |
| 100 | }; |
| 101 | |
| 102 | static struct snd_soc_card snd_soc_tegra_wm8753 = { |
| 103 | .name = "tegra-wm8753", |
| 104 | .driver_name = "tegra", |
| 105 | .owner = THIS_MODULE, |
| 106 | .dai_link = &tegra_wm8753_dai, |
| 107 | .num_links = 1, |
| 108 | |
| 109 | .dapm_widgets = tegra_wm8753_dapm_widgets, |
| 110 | .num_dapm_widgets = ARRAY_SIZE(tegra_wm8753_dapm_widgets), |
| 111 | .fully_routed = true, |
| 112 | }; |
| 113 | |
| 114 | static int tegra_wm8753_driver_probe(struct platform_device *pdev) |
| 115 | { |
| 116 | struct device_node *np = pdev->dev.of_node; |
| 117 | struct snd_soc_card *card = &snd_soc_tegra_wm8753; |
| 118 | struct tegra_wm8753 *machine; |
| 119 | int ret; |
| 120 | |
| 121 | machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8753), |
| 122 | GFP_KERNEL); |
| 123 | if (!machine) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | card->dev = &pdev->dev; |
| 127 | snd_soc_card_set_drvdata(card, machine); |
| 128 | |
| 129 | ret = snd_soc_of_parse_card_name(card, "nvidia,model"); |
| 130 | if (ret) |
| 131 | goto err; |
| 132 | |
| 133 | ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing"); |
| 134 | if (ret) |
| 135 | goto err; |
| 136 | |
| 137 | tegra_wm8753_dai.codecs->of_node = of_parse_phandle(np, |
| 138 | "nvidia,audio-codec", 0); |
| 139 | if (!tegra_wm8753_dai.codecs->of_node) { |
| 140 | dev_err(&pdev->dev, |
| 141 | "Property 'nvidia,audio-codec' missing or invalid\n"); |
| 142 | ret = -EINVAL; |
| 143 | goto err; |
| 144 | } |
| 145 | |
| 146 | tegra_wm8753_dai.cpus->of_node = of_parse_phandle(np, |
| 147 | "nvidia,i2s-controller", 0); |
| 148 | if (!tegra_wm8753_dai.cpus->of_node) { |
| 149 | dev_err(&pdev->dev, |
| 150 | "Property 'nvidia,i2s-controller' missing or invalid\n"); |
| 151 | ret = -EINVAL; |
| 152 | goto err; |
| 153 | } |
| 154 | |
| 155 | tegra_wm8753_dai.platforms->of_node = tegra_wm8753_dai.cpus->of_node; |
| 156 | |
| 157 | ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev); |
| 158 | if (ret) |
| 159 | goto err; |
| 160 | |
| 161 | ret = snd_soc_register_card(card); |
| 162 | if (ret) { |
| 163 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
| 164 | ret); |
| 165 | goto err_fini_utils; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | |
| 170 | err_fini_utils: |
| 171 | tegra_asoc_utils_fini(&machine->util_data); |
| 172 | err: |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static int tegra_wm8753_driver_remove(struct platform_device *pdev) |
| 177 | { |
| 178 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 179 | struct tegra_wm8753 *machine = snd_soc_card_get_drvdata(card); |
| 180 | |
| 181 | snd_soc_unregister_card(card); |
| 182 | |
| 183 | tegra_asoc_utils_fini(&machine->util_data); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static const struct of_device_id tegra_wm8753_of_match[] = { |
| 189 | { .compatible = "nvidia,tegra-audio-wm8753", }, |
| 190 | {}, |
| 191 | }; |
| 192 | |
| 193 | static struct platform_driver tegra_wm8753_driver = { |
| 194 | .driver = { |
| 195 | .name = DRV_NAME, |
| 196 | .pm = &snd_soc_pm_ops, |
| 197 | .of_match_table = tegra_wm8753_of_match, |
| 198 | }, |
| 199 | .probe = tegra_wm8753_driver_probe, |
| 200 | .remove = tegra_wm8753_driver_remove, |
| 201 | }; |
| 202 | module_platform_driver(tegra_wm8753_driver); |
| 203 | |
| 204 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 205 | MODULE_DESCRIPTION("Tegra+WM8753 machine ASoC driver"); |
| 206 | MODULE_LICENSE("GPL"); |
| 207 | MODULE_ALIAS("platform:" DRV_NAME); |
| 208 | MODULE_DEVICE_TABLE(of, tegra_wm8753_of_match); |