blob: 6760f0ebc1338881cd243b658315a57f8b814da9 [file] [log] [blame]
Stephen Warren7637af22013-12-04 15:19:27 -07001/*
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
42struct tegra_max98090 {
43 struct tegra_asoc_utils_data util_data;
44 int gpio_hp_det;
Dylan Reid9766a1c2014-10-02 09:42:44 -070045 int gpio_mic_det;
Stephen Warren7637af22013-12-04 15:19:27 -070046};
47
48static int tegra_max98090_asoc_hw_params(struct snd_pcm_substream *substream,
49 struct snd_pcm_hw_params *params)
50{
51 struct snd_soc_pcm_runtime *rtd = substream->private_data;
52 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +020053 struct snd_soc_card *card = rtd->card;
Stephen Warren7637af22013-12-04 15:19:27 -070054 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
55 int srate, mclk;
56 int err;
57
58 srate = params_rate(params);
59 switch (srate) {
60 case 8000:
61 case 16000:
62 case 24000:
63 case 32000:
64 case 48000:
65 case 64000:
66 case 96000:
67 mclk = 12288000;
68 break;
69 case 11025:
70 case 22050:
71 case 44100:
72 case 88200:
73 mclk = 11289600;
74 break;
75 default:
76 mclk = 12000000;
77 break;
78 }
79
80 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
81 if (err < 0) {
82 dev_err(card->dev, "Can't configure clocks\n");
83 return err;
84 }
85
86 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
87 SND_SOC_CLOCK_IN);
88 if (err < 0) {
89 dev_err(card->dev, "codec_dai clock not set\n");
90 return err;
91 }
92
93 return 0;
94}
95
96static struct snd_soc_ops tegra_max98090_ops = {
97 .hw_params = tegra_max98090_asoc_hw_params,
98};
99
100static struct snd_soc_jack tegra_max98090_hp_jack;
101
102static struct snd_soc_jack_pin tegra_max98090_hp_jack_pins[] = {
103 {
104 .pin = "Headphones",
105 .mask = SND_JACK_HEADPHONE,
106 },
107};
108
109static struct snd_soc_jack_gpio tegra_max98090_hp_jack_gpio = {
110 .name = "Headphone detection",
111 .report = SND_JACK_HEADPHONE,
112 .debounce_time = 150,
113 .invert = 1,
114};
115
Dylan Reid9766a1c2014-10-02 09:42:44 -0700116static struct snd_soc_jack tegra_max98090_mic_jack;
117
118static struct snd_soc_jack_pin tegra_max98090_mic_jack_pins[] = {
119 {
120 .pin = "Mic Jack",
121 .mask = SND_JACK_MICROPHONE,
122 },
123};
124
125static struct snd_soc_jack_gpio tegra_max98090_mic_jack_gpio = {
126 .name = "Mic detection",
127 .report = SND_JACK_MICROPHONE,
128 .debounce_time = 150,
129 .invert = 1,
130};
131
Stephen Warren7637af22013-12-04 15:19:27 -0700132static const struct snd_soc_dapm_widget tegra_max98090_dapm_widgets[] = {
133 SND_SOC_DAPM_HP("Headphones", NULL),
134 SND_SOC_DAPM_SPK("Speakers", NULL),
135 SND_SOC_DAPM_MIC("Mic Jack", NULL),
136};
137
138static const struct snd_kcontrol_new tegra_max98090_controls[] = {
139 SOC_DAPM_PIN_SWITCH("Speakers"),
140};
141
142static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
143{
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +0200144 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
Stephen Warren7637af22013-12-04 15:19:27 -0700145
146 if (gpio_is_valid(machine->gpio_hp_det)) {
Lars-Peter Clausend020e772015-03-04 10:33:41 +0100147 snd_soc_card_jack_new(rtd->card, "Headphones",
148 SND_JACK_HEADPHONE,
149 &tegra_max98090_hp_jack,
150 tegra_max98090_hp_jack_pins,
151 ARRAY_SIZE(tegra_max98090_hp_jack_pins));
Stephen Warren7637af22013-12-04 15:19:27 -0700152
153 tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
154 snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
155 1,
156 &tegra_max98090_hp_jack_gpio);
157 }
158
Dylan Reid9766a1c2014-10-02 09:42:44 -0700159 if (gpio_is_valid(machine->gpio_mic_det)) {
Lars-Peter Clausend020e772015-03-04 10:33:41 +0100160 snd_soc_card_jack_new(rtd->card, "Mic Jack",
161 SND_JACK_MICROPHONE,
162 &tegra_max98090_mic_jack,
163 tegra_max98090_mic_jack_pins,
164 ARRAY_SIZE(tegra_max98090_mic_jack_pins));
Dylan Reid9766a1c2014-10-02 09:42:44 -0700165
166 tegra_max98090_mic_jack_gpio.gpio = machine->gpio_mic_det;
167 snd_soc_jack_add_gpios(&tegra_max98090_mic_jack,
168 1,
169 &tegra_max98090_mic_jack_gpio);
170 }
171
Stephen Warren7637af22013-12-04 15:19:27 -0700172 return 0;
173}
174
Stephen Warrenfb6b8e72014-05-22 16:14:53 -0600175static int tegra_max98090_card_remove(struct snd_soc_card *card)
176{
177 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
178
179 if (gpio_is_valid(machine->gpio_hp_det)) {
180 snd_soc_jack_free_gpios(&tegra_max98090_hp_jack, 1,
181 &tegra_max98090_hp_jack_gpio);
182 }
183
Dylan Reid9766a1c2014-10-02 09:42:44 -0700184 if (gpio_is_valid(machine->gpio_mic_det)) {
185 snd_soc_jack_free_gpios(&tegra_max98090_mic_jack, 1,
186 &tegra_max98090_mic_jack_gpio);
187 }
188
Stephen Warrenfb6b8e72014-05-22 16:14:53 -0600189 return 0;
190}
191
Stephen Warren7637af22013-12-04 15:19:27 -0700192static struct snd_soc_dai_link tegra_max98090_dai = {
193 .name = "max98090",
194 .stream_name = "max98090 PCM",
195 .codec_dai_name = "HiFi",
196 .init = tegra_max98090_asoc_init,
197 .ops = &tegra_max98090_ops,
198 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
199 SND_SOC_DAIFMT_CBS_CFS,
200};
201
202static struct snd_soc_card snd_soc_tegra_max98090 = {
203 .name = "tegra-max98090",
204 .owner = THIS_MODULE,
Stephen Warrenfb6b8e72014-05-22 16:14:53 -0600205 .remove = tegra_max98090_card_remove,
Stephen Warren7637af22013-12-04 15:19:27 -0700206 .dai_link = &tegra_max98090_dai,
207 .num_links = 1,
208 .controls = tegra_max98090_controls,
209 .num_controls = ARRAY_SIZE(tegra_max98090_controls),
210 .dapm_widgets = tegra_max98090_dapm_widgets,
211 .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
212 .fully_routed = true,
213};
214
215static int tegra_max98090_probe(struct platform_device *pdev)
216{
217 struct device_node *np = pdev->dev.of_node;
218 struct snd_soc_card *card = &snd_soc_tegra_max98090;
219 struct tegra_max98090 *machine;
220 int ret;
221
222 machine = devm_kzalloc(&pdev->dev,
223 sizeof(struct tegra_max98090), GFP_KERNEL);
224 if (!machine) {
225 dev_err(&pdev->dev, "Can't allocate tegra_max98090\n");
226 return -ENOMEM;
227 }
228
229 card->dev = &pdev->dev;
230 platform_set_drvdata(pdev, card);
231 snd_soc_card_set_drvdata(card, machine);
232
233 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
234 if (machine->gpio_hp_det == -EPROBE_DEFER)
235 return -EPROBE_DEFER;
236
Dylan Reid9766a1c2014-10-02 09:42:44 -0700237 machine->gpio_mic_det =
238 of_get_named_gpio(np, "nvidia,mic-det-gpios", 0);
239 if (machine->gpio_mic_det == -EPROBE_DEFER)
240 return -EPROBE_DEFER;
241
Stephen Warren7637af22013-12-04 15:19:27 -0700242 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
243 if (ret)
244 goto err;
245
246 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
247 if (ret)
248 goto err;
249
250 tegra_max98090_dai.codec_of_node = of_parse_phandle(np,
251 "nvidia,audio-codec", 0);
252 if (!tegra_max98090_dai.codec_of_node) {
253 dev_err(&pdev->dev,
254 "Property 'nvidia,audio-codec' missing or invalid\n");
255 ret = -EINVAL;
256 goto err;
257 }
258
259 tegra_max98090_dai.cpu_of_node = of_parse_phandle(np,
260 "nvidia,i2s-controller", 0);
261 if (!tegra_max98090_dai.cpu_of_node) {
262 dev_err(&pdev->dev,
263 "Property 'nvidia,i2s-controller' missing or invalid\n");
264 ret = -EINVAL;
265 goto err;
266 }
267
268 tegra_max98090_dai.platform_of_node = tegra_max98090_dai.cpu_of_node;
269
270 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
271 if (ret)
272 goto err;
273
274 ret = snd_soc_register_card(card);
275 if (ret) {
276 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
277 ret);
278 goto err_fini_utils;
279 }
280
281 return 0;
282
283err_fini_utils:
284 tegra_asoc_utils_fini(&machine->util_data);
285err:
286 return ret;
287}
288
289static int tegra_max98090_remove(struct platform_device *pdev)
290{
291 struct snd_soc_card *card = platform_get_drvdata(pdev);
292 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
293
Stephen Warren7637af22013-12-04 15:19:27 -0700294 snd_soc_unregister_card(card);
295
296 tegra_asoc_utils_fini(&machine->util_data);
297
298 return 0;
299}
300
301static const struct of_device_id tegra_max98090_of_match[] = {
302 { .compatible = "nvidia,tegra-audio-max98090", },
303 {},
304};
305
306static struct platform_driver tegra_max98090_driver = {
307 .driver = {
308 .name = DRV_NAME,
Stephen Warren7637af22013-12-04 15:19:27 -0700309 .pm = &snd_soc_pm_ops,
310 .of_match_table = tegra_max98090_of_match,
311 },
312 .probe = tegra_max98090_probe,
313 .remove = tegra_max98090_remove,
314};
315module_platform_driver(tegra_max98090_driver);
316
317MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
318MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
319MODULE_LICENSE("GPL v2");
320MODULE_ALIAS("platform:" DRV_NAME);
321MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);