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