]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/tegra/tegra_max98090.c
Merge remote-tracking branch 'asoc/topic/component' into asoc-next
[mirror_ubuntu-bionic-kernel.git] / sound / soc / tegra / tegra_max98090.c
1 /*
2 * Tegra machine ASoC driver for boards using a MAX90809 CODEC.
3 *
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Based on code copyright/by:
19 *
20 * Copyright (C) 2010-2012 - NVIDIA, Inc.
21 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
22 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
23 * Copyright 2007 Wolfson Microelectronics PLC.
24 */
25
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
31
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37
38 #include "tegra_asoc_utils.h"
39
40 #define DRV_NAME "tegra-snd-max98090"
41
42 struct tegra_max98090 {
43 struct tegra_asoc_utils_data util_data;
44 int gpio_hp_det;
45 };
46
47 static int tegra_max98090_asoc_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
49 {
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 struct snd_soc_card *card = rtd->card;
53 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
54 int srate, mclk;
55 int err;
56
57 srate = params_rate(params);
58 switch (srate) {
59 case 8000:
60 case 16000:
61 case 24000:
62 case 32000:
63 case 48000:
64 case 64000:
65 case 96000:
66 mclk = 12288000;
67 break;
68 case 11025:
69 case 22050:
70 case 44100:
71 case 88200:
72 mclk = 11289600;
73 break;
74 default:
75 mclk = 12000000;
76 break;
77 }
78
79 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
80 if (err < 0) {
81 dev_err(card->dev, "Can't configure clocks\n");
82 return err;
83 }
84
85 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
86 SND_SOC_CLOCK_IN);
87 if (err < 0) {
88 dev_err(card->dev, "codec_dai clock not set\n");
89 return err;
90 }
91
92 return 0;
93 }
94
95 static struct snd_soc_ops tegra_max98090_ops = {
96 .hw_params = tegra_max98090_asoc_hw_params,
97 };
98
99 static struct snd_soc_jack tegra_max98090_hp_jack;
100
101 static struct snd_soc_jack_pin tegra_max98090_hp_jack_pins[] = {
102 {
103 .pin = "Headphones",
104 .mask = SND_JACK_HEADPHONE,
105 },
106 };
107
108 static struct snd_soc_jack_gpio tegra_max98090_hp_jack_gpio = {
109 .name = "Headphone detection",
110 .report = SND_JACK_HEADPHONE,
111 .debounce_time = 150,
112 .invert = 1,
113 };
114
115 static const struct snd_soc_dapm_widget tegra_max98090_dapm_widgets[] = {
116 SND_SOC_DAPM_HP("Headphones", NULL),
117 SND_SOC_DAPM_SPK("Speakers", NULL),
118 SND_SOC_DAPM_MIC("Mic Jack", NULL),
119 };
120
121 static const struct snd_kcontrol_new tegra_max98090_controls[] = {
122 SOC_DAPM_PIN_SWITCH("Speakers"),
123 };
124
125 static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
126 {
127 struct snd_soc_dai *codec_dai = rtd->codec_dai;
128 struct snd_soc_codec *codec = codec_dai->codec;
129 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
130
131 if (gpio_is_valid(machine->gpio_hp_det)) {
132 snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
133 &tegra_max98090_hp_jack);
134 snd_soc_jack_add_pins(&tegra_max98090_hp_jack,
135 ARRAY_SIZE(tegra_max98090_hp_jack_pins),
136 tegra_max98090_hp_jack_pins);
137
138 tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
139 snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
140 1,
141 &tegra_max98090_hp_jack_gpio);
142 }
143
144 return 0;
145 }
146
147 static int tegra_max98090_card_remove(struct snd_soc_card *card)
148 {
149 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
150
151 if (gpio_is_valid(machine->gpio_hp_det)) {
152 snd_soc_jack_free_gpios(&tegra_max98090_hp_jack, 1,
153 &tegra_max98090_hp_jack_gpio);
154 }
155
156 return 0;
157 }
158
159 static struct snd_soc_dai_link tegra_max98090_dai = {
160 .name = "max98090",
161 .stream_name = "max98090 PCM",
162 .codec_dai_name = "HiFi",
163 .init = tegra_max98090_asoc_init,
164 .ops = &tegra_max98090_ops,
165 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
166 SND_SOC_DAIFMT_CBS_CFS,
167 };
168
169 static struct snd_soc_card snd_soc_tegra_max98090 = {
170 .name = "tegra-max98090",
171 .owner = THIS_MODULE,
172 .remove = tegra_max98090_card_remove,
173 .dai_link = &tegra_max98090_dai,
174 .num_links = 1,
175 .controls = tegra_max98090_controls,
176 .num_controls = ARRAY_SIZE(tegra_max98090_controls),
177 .dapm_widgets = tegra_max98090_dapm_widgets,
178 .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
179 .fully_routed = true,
180 };
181
182 static int tegra_max98090_probe(struct platform_device *pdev)
183 {
184 struct device_node *np = pdev->dev.of_node;
185 struct snd_soc_card *card = &snd_soc_tegra_max98090;
186 struct tegra_max98090 *machine;
187 int ret;
188
189 machine = devm_kzalloc(&pdev->dev,
190 sizeof(struct tegra_max98090), GFP_KERNEL);
191 if (!machine) {
192 dev_err(&pdev->dev, "Can't allocate tegra_max98090\n");
193 return -ENOMEM;
194 }
195
196 card->dev = &pdev->dev;
197 platform_set_drvdata(pdev, card);
198 snd_soc_card_set_drvdata(card, machine);
199
200 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
201 if (machine->gpio_hp_det == -EPROBE_DEFER)
202 return -EPROBE_DEFER;
203
204 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
205 if (ret)
206 goto err;
207
208 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
209 if (ret)
210 goto err;
211
212 tegra_max98090_dai.codec_of_node = of_parse_phandle(np,
213 "nvidia,audio-codec", 0);
214 if (!tegra_max98090_dai.codec_of_node) {
215 dev_err(&pdev->dev,
216 "Property 'nvidia,audio-codec' missing or invalid\n");
217 ret = -EINVAL;
218 goto err;
219 }
220
221 tegra_max98090_dai.cpu_of_node = of_parse_phandle(np,
222 "nvidia,i2s-controller", 0);
223 if (!tegra_max98090_dai.cpu_of_node) {
224 dev_err(&pdev->dev,
225 "Property 'nvidia,i2s-controller' missing or invalid\n");
226 ret = -EINVAL;
227 goto err;
228 }
229
230 tegra_max98090_dai.platform_of_node = tegra_max98090_dai.cpu_of_node;
231
232 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
233 if (ret)
234 goto err;
235
236 ret = snd_soc_register_card(card);
237 if (ret) {
238 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
239 ret);
240 goto err_fini_utils;
241 }
242
243 return 0;
244
245 err_fini_utils:
246 tegra_asoc_utils_fini(&machine->util_data);
247 err:
248 return ret;
249 }
250
251 static int tegra_max98090_remove(struct platform_device *pdev)
252 {
253 struct snd_soc_card *card = platform_get_drvdata(pdev);
254 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
255
256 snd_soc_unregister_card(card);
257
258 tegra_asoc_utils_fini(&machine->util_data);
259
260 return 0;
261 }
262
263 static const struct of_device_id tegra_max98090_of_match[] = {
264 { .compatible = "nvidia,tegra-audio-max98090", },
265 {},
266 };
267
268 static struct platform_driver tegra_max98090_driver = {
269 .driver = {
270 .name = DRV_NAME,
271 .owner = THIS_MODULE,
272 .pm = &snd_soc_pm_ops,
273 .of_match_table = tegra_max98090_of_match,
274 },
275 .probe = tegra_max98090_probe,
276 .remove = tegra_max98090_remove,
277 };
278 module_platform_driver(tegra_max98090_driver);
279
280 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
281 MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
282 MODULE_LICENSE("GPL v2");
283 MODULE_ALIAS("platform:" DRV_NAME);
284 MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);