GNU Linux-libre 5.4.200-gnu1
[releases.git] / sound / soc / tegra / tegra_rt5640.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * tegra_rt5640.c - Tegra machine ASoC driver for boards using RT5640 codec.
4  *
5  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
6  *
7  * Based on code copyright/by:
8  *
9  * Copyright (C) 2010-2012 - NVIDIA, Inc.
10  * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
11  * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
12  * Copyright 2007 Wolfson Microelectronics PLC.
13  */
14
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
20
21 #include <sound/core.h>
22 #include <sound/jack.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26
27 #include "../codecs/rt5640.h"
28
29 #include "tegra_asoc_utils.h"
30
31 #define DRV_NAME "tegra-snd-rt5640"
32
33 struct tegra_rt5640 {
34         struct tegra_asoc_utils_data util_data;
35         int gpio_hp_det;
36         enum of_gpio_flags gpio_hp_det_flags;
37 };
38
39 static int tegra_rt5640_asoc_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_rt5640 *machine = snd_soc_card_get_drvdata(card);
46         int srate, mclk;
47         int err;
48
49         srate = params_rate(params);
50         mclk = 256 * srate;
51
52         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
53         if (err < 0) {
54                 dev_err(card->dev, "Can't configure clocks\n");
55                 return err;
56         }
57
58         err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
59                                         SND_SOC_CLOCK_IN);
60         if (err < 0) {
61                 dev_err(card->dev, "codec_dai clock not set\n");
62                 return err;
63         }
64
65         return 0;
66 }
67
68 static const struct snd_soc_ops tegra_rt5640_ops = {
69         .hw_params = tegra_rt5640_asoc_hw_params,
70 };
71
72 static struct snd_soc_jack tegra_rt5640_hp_jack;
73
74 static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
75         {
76                 .pin = "Headphones",
77                 .mask = SND_JACK_HEADPHONE,
78         },
79 };
80
81 static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
82         .name = "Headphone detection",
83         .report = SND_JACK_HEADPHONE,
84         .debounce_time = 150,
85         .invert = 1,
86 };
87
88 static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
89         SND_SOC_DAPM_HP("Headphones", NULL),
90         SND_SOC_DAPM_SPK("Speakers", NULL),
91         SND_SOC_DAPM_MIC("Mic Jack", NULL),
92 };
93
94 static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
95         SOC_DAPM_PIN_SWITCH("Speakers"),
96 };
97
98 static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
99 {
100         struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
101
102         snd_soc_card_jack_new(rtd->card, "Headphones", SND_JACK_HEADPHONE,
103                               &tegra_rt5640_hp_jack, tegra_rt5640_hp_jack_pins,
104                               ARRAY_SIZE(tegra_rt5640_hp_jack_pins));
105
106         if (gpio_is_valid(machine->gpio_hp_det)) {
107                 tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
108                 tegra_rt5640_hp_jack_gpio.invert =
109                         !!(machine->gpio_hp_det_flags & OF_GPIO_ACTIVE_LOW);
110                 snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
111                                                 1,
112                                                 &tegra_rt5640_hp_jack_gpio);
113         }
114
115         return 0;
116 }
117
118 SND_SOC_DAILINK_DEFS(aif1,
119         DAILINK_COMP_ARRAY(COMP_EMPTY()),
120         DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5640-aif1")),
121         DAILINK_COMP_ARRAY(COMP_EMPTY()));
122
123 static struct snd_soc_dai_link tegra_rt5640_dai = {
124         .name = "RT5640",
125         .stream_name = "RT5640 PCM",
126         .init = tegra_rt5640_asoc_init,
127         .ops = &tegra_rt5640_ops,
128         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
129                         SND_SOC_DAIFMT_CBS_CFS,
130         SND_SOC_DAILINK_REG(aif1),
131 };
132
133 static struct snd_soc_card snd_soc_tegra_rt5640 = {
134         .name = "tegra-rt5640",
135         .driver_name = "tegra",
136         .owner = THIS_MODULE,
137         .dai_link = &tegra_rt5640_dai,
138         .num_links = 1,
139         .controls = tegra_rt5640_controls,
140         .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
141         .dapm_widgets = tegra_rt5640_dapm_widgets,
142         .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
143         .fully_routed = true,
144 };
145
146 static int tegra_rt5640_probe(struct platform_device *pdev)
147 {
148         struct device_node *np = pdev->dev.of_node;
149         struct snd_soc_card *card = &snd_soc_tegra_rt5640;
150         struct tegra_rt5640 *machine;
151         int ret;
152
153         machine = devm_kzalloc(&pdev->dev,
154                         sizeof(struct tegra_rt5640), GFP_KERNEL);
155         if (!machine)
156                 return -ENOMEM;
157
158         card->dev = &pdev->dev;
159         snd_soc_card_set_drvdata(card, machine);
160
161         machine->gpio_hp_det = of_get_named_gpio_flags(
162                 np, "nvidia,hp-det-gpios", 0, &machine->gpio_hp_det_flags);
163         if (machine->gpio_hp_det == -EPROBE_DEFER)
164                 return -EPROBE_DEFER;
165
166         ret = snd_soc_of_parse_card_name(card, "nvidia,model");
167         if (ret)
168                 goto err;
169
170         ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
171         if (ret)
172                 goto err;
173
174         tegra_rt5640_dai.codecs->of_node = of_parse_phandle(np,
175                         "nvidia,audio-codec", 0);
176         if (!tegra_rt5640_dai.codecs->of_node) {
177                 dev_err(&pdev->dev,
178                         "Property 'nvidia,audio-codec' missing or invalid\n");
179                 ret = -EINVAL;
180                 goto err;
181         }
182
183         tegra_rt5640_dai.cpus->of_node = of_parse_phandle(np,
184                         "nvidia,i2s-controller", 0);
185         if (!tegra_rt5640_dai.cpus->of_node) {
186                 dev_err(&pdev->dev,
187                         "Property 'nvidia,i2s-controller' missing or invalid\n");
188                 ret = -EINVAL;
189                 goto err;
190         }
191
192         tegra_rt5640_dai.platforms->of_node = tegra_rt5640_dai.cpus->of_node;
193
194         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
195         if (ret)
196                 goto err;
197
198         ret = snd_soc_register_card(card);
199         if (ret) {
200                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
201                         ret);
202                 goto err_fini_utils;
203         }
204
205         return 0;
206
207 err_fini_utils:
208         tegra_asoc_utils_fini(&machine->util_data);
209 err:
210         return ret;
211 }
212
213 static int tegra_rt5640_remove(struct platform_device *pdev)
214 {
215         struct snd_soc_card *card = platform_get_drvdata(pdev);
216         struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
217
218         snd_soc_unregister_card(card);
219
220         tegra_asoc_utils_fini(&machine->util_data);
221
222         return 0;
223 }
224
225 static const struct of_device_id tegra_rt5640_of_match[] = {
226         { .compatible = "nvidia,tegra-audio-rt5640", },
227         {},
228 };
229
230 static struct platform_driver tegra_rt5640_driver = {
231         .driver = {
232                 .name = DRV_NAME,
233                 .pm = &snd_soc_pm_ops,
234                 .of_match_table = tegra_rt5640_of_match,
235         },
236         .probe = tegra_rt5640_probe,
237         .remove = tegra_rt5640_remove,
238 };
239 module_platform_driver(tegra_rt5640_driver);
240
241 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
242 MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
243 MODULE_LICENSE("GPL v2");
244 MODULE_ALIAS("platform:" DRV_NAME);
245 MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);