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