blob: 00c19704057b675bcf6201c61e0fffd6e403d309 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Stephen Warren7637af22013-12-04 15:19:27 -07002/*
3 * Tegra machine ASoC driver for boards using a MAX90809 CODEC.
4 *
5 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
6 *
Stephen Warren7637af22013-12-04 15:19:27 -07007 * 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 "tegra_asoc_utils.h"
28
29#define DRV_NAME "tegra-snd-max98090"
30
31struct tegra_max98090 {
32 struct tegra_asoc_utils_data util_data;
33 int gpio_hp_det;
Dylan Reid9766a1c2014-10-02 09:42:44 -070034 int gpio_mic_det;
Stephen Warren7637af22013-12-04 15:19:27 -070035};
36
37static int tegra_max98090_asoc_hw_params(struct snd_pcm_substream *substream,
38 struct snd_pcm_hw_params *params)
39{
Kuninori Morimoto95ebd022020-07-20 10:17:53 +090040 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0b25cff2020-03-23 14:21:07 +090041 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +020042 struct snd_soc_card *card = rtd->card;
Stephen Warren7637af22013-12-04 15:19:27 -070043 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
44 int srate, mclk;
45 int err;
46
47 srate = params_rate(params);
48 switch (srate) {
49 case 8000:
50 case 16000:
51 case 24000:
52 case 32000:
53 case 48000:
54 case 64000:
55 case 96000:
56 mclk = 12288000;
57 break;
58 case 11025:
59 case 22050:
60 case 44100:
61 case 88200:
62 mclk = 11289600;
63 break;
64 default:
65 mclk = 12000000;
66 break;
67 }
68
69 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
70 if (err < 0) {
71 dev_err(card->dev, "Can't configure clocks\n");
72 return err;
73 }
74
75 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
76 SND_SOC_CLOCK_IN);
77 if (err < 0) {
78 dev_err(card->dev, "codec_dai clock not set\n");
79 return err;
80 }
81
82 return 0;
83}
84
Julia Lawall8a7a2822016-10-15 16:55:51 +020085static const struct snd_soc_ops tegra_max98090_ops = {
Stephen Warren7637af22013-12-04 15:19:27 -070086 .hw_params = tegra_max98090_asoc_hw_params,
87};
88
89static struct snd_soc_jack tegra_max98090_hp_jack;
90
91static struct snd_soc_jack_pin tegra_max98090_hp_jack_pins[] = {
92 {
93 .pin = "Headphones",
94 .mask = SND_JACK_HEADPHONE,
95 },
96};
97
98static struct snd_soc_jack_gpio tegra_max98090_hp_jack_gpio = {
99 .name = "Headphone detection",
100 .report = SND_JACK_HEADPHONE,
101 .debounce_time = 150,
102 .invert = 1,
103};
104
Dylan Reid9766a1c2014-10-02 09:42:44 -0700105static struct snd_soc_jack tegra_max98090_mic_jack;
106
107static struct snd_soc_jack_pin tegra_max98090_mic_jack_pins[] = {
108 {
109 .pin = "Mic Jack",
110 .mask = SND_JACK_MICROPHONE,
111 },
112};
113
114static struct snd_soc_jack_gpio tegra_max98090_mic_jack_gpio = {
115 .name = "Mic detection",
116 .report = SND_JACK_MICROPHONE,
117 .debounce_time = 150,
118 .invert = 1,
119};
120
Stephen Warren7637af22013-12-04 15:19:27 -0700121static const struct snd_soc_dapm_widget tegra_max98090_dapm_widgets[] = {
122 SND_SOC_DAPM_HP("Headphones", NULL),
123 SND_SOC_DAPM_SPK("Speakers", NULL),
124 SND_SOC_DAPM_MIC("Mic Jack", NULL),
Tomeu Vizoso3a4562f2015-02-12 09:41:56 +0100125 SND_SOC_DAPM_MIC("Int Mic", NULL),
Stephen Warren7637af22013-12-04 15:19:27 -0700126};
127
128static const struct snd_kcontrol_new tegra_max98090_controls[] = {
Tomeu Vizosoa0cf43e2015-02-12 09:41:55 +0100129 SOC_DAPM_PIN_SWITCH("Headphones"),
Stephen Warren7637af22013-12-04 15:19:27 -0700130 SOC_DAPM_PIN_SWITCH("Speakers"),
Tomeu Vizosodd300142015-02-12 09:41:57 +0100131 SOC_DAPM_PIN_SWITCH("Mic Jack"),
Tomeu Vizoso3a4562f2015-02-12 09:41:56 +0100132 SOC_DAPM_PIN_SWITCH("Int Mic"),
Stephen Warren7637af22013-12-04 15:19:27 -0700133};
134
135static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
136{
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +0200137 struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
Stephen Warren7637af22013-12-04 15:19:27 -0700138
139 if (gpio_is_valid(machine->gpio_hp_det)) {
Lars-Peter Clausend020e772015-03-04 10:33:41 +0100140 snd_soc_card_jack_new(rtd->card, "Headphones",
141 SND_JACK_HEADPHONE,
142 &tegra_max98090_hp_jack,
143 tegra_max98090_hp_jack_pins,
144 ARRAY_SIZE(tegra_max98090_hp_jack_pins));
Stephen Warren7637af22013-12-04 15:19:27 -0700145
146 tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
147 snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
148 1,
149 &tegra_max98090_hp_jack_gpio);
150 }
151
Dylan Reid9766a1c2014-10-02 09:42:44 -0700152 if (gpio_is_valid(machine->gpio_mic_det)) {
Lars-Peter Clausend020e772015-03-04 10:33:41 +0100153 snd_soc_card_jack_new(rtd->card, "Mic Jack",
154 SND_JACK_MICROPHONE,
155 &tegra_max98090_mic_jack,
156 tegra_max98090_mic_jack_pins,
157 ARRAY_SIZE(tegra_max98090_mic_jack_pins));
Dylan Reid9766a1c2014-10-02 09:42:44 -0700158
159 tegra_max98090_mic_jack_gpio.gpio = machine->gpio_mic_det;
160 snd_soc_jack_add_gpios(&tegra_max98090_mic_jack,
161 1,
162 &tegra_max98090_mic_jack_gpio);
163 }
164
Stephen Warren7637af22013-12-04 15:19:27 -0700165 return 0;
166}
167
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900168SND_SOC_DAILINK_DEFS(pcm,
169 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimotoabe4918b2019-06-28 10:48:45 +0900170 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
171 DAILINK_COMP_ARRAY(COMP_EMPTY()));
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900172
Stephen Warren7637af22013-12-04 15:19:27 -0700173static struct snd_soc_dai_link tegra_max98090_dai = {
174 .name = "max98090",
175 .stream_name = "max98090 PCM",
Stephen Warren7637af22013-12-04 15:19:27 -0700176 .init = tegra_max98090_asoc_init,
177 .ops = &tegra_max98090_ops,
178 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
179 SND_SOC_DAIFMT_CBS_CFS,
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900180 SND_SOC_DAILINK_REG(pcm),
Stephen Warren7637af22013-12-04 15:19:27 -0700181};
182
183static struct snd_soc_card snd_soc_tegra_max98090 = {
184 .name = "tegra-max98090",
185 .owner = THIS_MODULE,
186 .dai_link = &tegra_max98090_dai,
187 .num_links = 1,
188 .controls = tegra_max98090_controls,
189 .num_controls = ARRAY_SIZE(tegra_max98090_controls),
190 .dapm_widgets = tegra_max98090_dapm_widgets,
191 .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
192 .fully_routed = true,
193};
194
195static int tegra_max98090_probe(struct platform_device *pdev)
196{
197 struct device_node *np = pdev->dev.of_node;
198 struct snd_soc_card *card = &snd_soc_tegra_max98090;
199 struct tegra_max98090 *machine;
200 int ret;
201
202 machine = devm_kzalloc(&pdev->dev,
203 sizeof(struct tegra_max98090), GFP_KERNEL);
Codrut Grosue2c187a2017-02-25 13:18:08 +0200204 if (!machine)
Stephen Warren7637af22013-12-04 15:19:27 -0700205 return -ENOMEM;
Stephen Warren7637af22013-12-04 15:19:27 -0700206
207 card->dev = &pdev->dev;
Stephen Warren7637af22013-12-04 15:19:27 -0700208 snd_soc_card_set_drvdata(card, machine);
209
210 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
211 if (machine->gpio_hp_det == -EPROBE_DEFER)
212 return -EPROBE_DEFER;
213
Dylan Reid9766a1c2014-10-02 09:42:44 -0700214 machine->gpio_mic_det =
215 of_get_named_gpio(np, "nvidia,mic-det-gpios", 0);
216 if (machine->gpio_mic_det == -EPROBE_DEFER)
217 return -EPROBE_DEFER;
218
Stephen Warren7637af22013-12-04 15:19:27 -0700219 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
220 if (ret)
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800221 return ret;
Stephen Warren7637af22013-12-04 15:19:27 -0700222
223 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
224 if (ret)
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800225 return ret;
Stephen Warren7637af22013-12-04 15:19:27 -0700226
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900227 tegra_max98090_dai.codecs->of_node = of_parse_phandle(np,
Stephen Warren7637af22013-12-04 15:19:27 -0700228 "nvidia,audio-codec", 0);
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900229 if (!tegra_max98090_dai.codecs->of_node) {
Stephen Warren7637af22013-12-04 15:19:27 -0700230 dev_err(&pdev->dev,
231 "Property 'nvidia,audio-codec' missing or invalid\n");
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800232 return -EINVAL;
Stephen Warren7637af22013-12-04 15:19:27 -0700233 }
234
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900235 tegra_max98090_dai.cpus->of_node = of_parse_phandle(np,
Stephen Warren7637af22013-12-04 15:19:27 -0700236 "nvidia,i2s-controller", 0);
Kuninori Morimotof60adf32019-06-06 13:19:10 +0900237 if (!tegra_max98090_dai.cpus->of_node) {
Stephen Warren7637af22013-12-04 15:19:27 -0700238 dev_err(&pdev->dev,
239 "Property 'nvidia,i2s-controller' missing or invalid\n");
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800240 return -EINVAL;
Stephen Warren7637af22013-12-04 15:19:27 -0700241 }
242
Kuninori Morimotoabe4918b2019-06-28 10:48:45 +0900243 tegra_max98090_dai.platforms->of_node = tegra_max98090_dai.cpus->of_node;
244
Stephen Warren7637af22013-12-04 15:19:27 -0700245 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
246 if (ret)
Sowjanya Komatineni0de6db32020-01-13 23:24:17 -0800247 return ret;
Stephen Warren7637af22013-12-04 15:19:27 -0700248
Qinglang Miaof8eeca92020-09-29 19:29:35 +0800249 ret = devm_snd_soc_register_card(&pdev->dev, card);
Jon Hunter34d3dab2020-11-11 10:32:45 +0000250 if (ret)
251 return dev_err_probe(&pdev->dev, ret,
252 "snd_soc_register_card failed\n");
Stephen Warren7637af22013-12-04 15:19:27 -0700253
254 return 0;
Stephen Warren7637af22013-12-04 15:19:27 -0700255}
256
Stephen Warren7637af22013-12-04 15:19:27 -0700257static const struct of_device_id tegra_max98090_of_match[] = {
258 { .compatible = "nvidia,tegra-audio-max98090", },
259 {},
260};
261
262static struct platform_driver tegra_max98090_driver = {
263 .driver = {
264 .name = DRV_NAME,
Stephen Warren7637af22013-12-04 15:19:27 -0700265 .pm = &snd_soc_pm_ops,
266 .of_match_table = tegra_max98090_of_match,
267 },
268 .probe = tegra_max98090_probe,
Stephen Warren7637af22013-12-04 15:19:27 -0700269};
270module_platform_driver(tegra_max98090_driver);
271
272MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
273MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
274MODULE_LICENSE("GPL v2");
275MODULE_ALIAS("platform:" DRV_NAME);
276MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);