blob: 9e8b1497efd3e543e8e495f4d8a8aae391897bc6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Leon Romanovsky58783fa2011-12-19 21:51:52 +02002/*
Stephen Warrenaef9a372012-05-22 16:09:51 -06003* tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
Stephen Warren1ae93b92012-03-20 14:55:48 -06004 *
5 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
Stephen Warren518de862012-03-20 14:55:49 -06006 * Copyright (C) 2012 - NVIDIA, Inc.
Stephen Warren1ae93b92012-03-20 14:55:48 -06007 *
8 * Authors: Leon Romanovsky <leon@leon.nu>
9 * Andrey Danin <danindrey@mail.ru>
10 * Marc Dietrich <marvin24@gmx.de>
Stephen Warren1ae93b92012-03-20 14:55:48 -060011 */
Leon Romanovsky58783fa2011-12-19 21:51:52 +020012
Leon Romanovsky58783fa2011-12-19 21:51:52 +020013#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
16#include <linux/gpio.h>
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020017#include <linux/of_gpio.h>
Leon Romanovsky58783fa2011-12-19 21:51:52 +020018
19#include <sound/core.h>
20#include <sound/jack.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
24
25#include "../codecs/alc5632.h"
26
Leon Romanovsky58783fa2011-12-19 21:51:52 +020027#include "tegra_asoc_utils.h"
28
29#define DRV_NAME "tegra-alc5632"
30
31struct tegra_alc5632 {
32 struct tegra_asoc_utils_data util_data;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020033 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020034};
35
36static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
37 struct snd_pcm_hw_params *params)
38{
39 struct snd_soc_pcm_runtime *rtd = substream->private_data;
40 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +020041 struct snd_soc_card *card = rtd->card;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020042 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
43 int srate, mclk;
44 int err;
45
46 srate = params_rate(params);
47 mclk = 512 * srate;
48
49 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
50 if (err < 0) {
51 dev_err(card->dev, "Can't configure clocks\n");
52 return err;
53 }
54
55 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
56 SND_SOC_CLOCK_IN);
57 if (err < 0) {
58 dev_err(card->dev, "codec_dai clock not set\n");
59 return err;
60 }
61
62 return 0;
63}
64
Julia Lawall8a7a2822016-10-15 16:55:51 +020065static const struct snd_soc_ops tegra_alc5632_asoc_ops = {
Leon Romanovsky58783fa2011-12-19 21:51:52 +020066 .hw_params = tegra_alc5632_asoc_hw_params,
67};
68
69static struct snd_soc_jack tegra_alc5632_hs_jack;
70
71static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
72 {
73 .pin = "Headset Mic",
74 .mask = SND_JACK_MICROPHONE,
75 },
76 {
77 .pin = "Headset Stereophone",
78 .mask = SND_JACK_HEADPHONE,
79 },
80};
81
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020082static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
83 .name = "Headset detection",
84 .report = SND_JACK_HEADSET,
85 .debounce_time = 150,
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020086};
87
Leon Romanovsky58783fa2011-12-19 21:51:52 +020088static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
89 SND_SOC_DAPM_SPK("Int Spk", NULL),
90 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
91 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +020092 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +020093};
94
Leon Romanovsky58783fa2011-12-19 21:51:52 +020095static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
96 SOC_DAPM_PIN_SWITCH("Int Spk"),
97};
98
99static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
100{
Sudip Mukherjee319c3252015-12-01 16:09:51 +0530101 int ret;
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +0200102 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(rtd->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200103
Sudip Mukherjee319c3252015-12-01 16:09:51 +0530104 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
105 SND_JACK_HEADSET,
106 &tegra_alc5632_hs_jack,
107 tegra_alc5632_hs_jack_pins,
108 ARRAY_SIZE(tegra_alc5632_hs_jack_pins));
109 if (ret)
110 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200111
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200112 if (gpio_is_valid(machine->gpio_hp_det)) {
113 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
114 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
115 1,
116 &tegra_alc5632_hp_jack_gpio);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200117 }
118
Lars-Peter Clausen884c0f52015-04-03 13:04:05 +0200119 snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "MICBIAS1");
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200120
121 return 0;
122}
123
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900124SND_SOC_DAILINK_DEFS(pcm,
125 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimotobae05f22019-06-28 10:48:40 +0900126 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "alc5632-hifi")),
127 DAILINK_COMP_ARRAY(COMP_EMPTY()));
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900128
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200129static struct snd_soc_dai_link tegra_alc5632_dai = {
130 .name = "ALC5632",
131 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200132 .init = tegra_alc5632_asoc_init,
133 .ops = &tegra_alc5632_asoc_ops,
134 .dai_fmt = SND_SOC_DAIFMT_I2S
135 | SND_SOC_DAIFMT_NB_NF
136 | SND_SOC_DAIFMT_CBS_CFS,
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900137 SND_SOC_DAILINK_REG(pcm),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200138};
139
140static struct snd_soc_card snd_soc_tegra_alc5632 = {
141 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800142 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200143 .dai_link = &tegra_alc5632_dai,
144 .num_links = 1,
145 .controls = tegra_alc5632_controls,
146 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
147 .dapm_widgets = tegra_alc5632_dapm_widgets,
148 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200149 .fully_routed = true,
150};
151
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500152static int tegra_alc5632_probe(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200153{
Stephen Warrenaef9a372012-05-22 16:09:51 -0600154 struct device_node *np = pdev->dev.of_node;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200155 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
156 struct tegra_alc5632 *alc5632;
157 int ret;
158
159 alc5632 = devm_kzalloc(&pdev->dev,
160 sizeof(struct tegra_alc5632), GFP_KERNEL);
Codrut Grosue2c187a2017-02-25 13:18:08 +0200161 if (!alc5632)
Stephen Warrenf7265362013-02-15 17:07:33 -0700162 return -ENOMEM;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200163
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200164 card->dev = &pdev->dev;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200165 snd_soc_card_set_drvdata(card, alc5632);
166
Stephen Warrenaef9a372012-05-22 16:09:51 -0600167 alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
Roland Stiggebbfc3282012-06-18 18:42:22 +0200168 if (alc5632->gpio_hp_det == -EPROBE_DEFER)
Stephen Warrenaef9a372012-05-22 16:09:51 -0600169 return -EPROBE_DEFER;
170
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200171 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
172 if (ret)
173 goto err;
174
175 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
176 if (ret)
177 goto err;
178
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900179 tegra_alc5632_dai.codecs->of_node = of_parse_phandle(
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200180 pdev->dev.of_node, "nvidia,audio-codec", 0);
181
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900182 if (!tegra_alc5632_dai.codecs->of_node) {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200183 dev_err(&pdev->dev,
184 "Property 'nvidia,audio-codec' missing or invalid\n");
185 ret = -EINVAL;
186 goto err;
187 }
188
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900189 tegra_alc5632_dai.cpus->of_node = of_parse_phandle(np,
Stephen Warrenf7265362013-02-15 17:07:33 -0700190 "nvidia,i2s-controller", 0);
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900191 if (!tegra_alc5632_dai.cpus->of_node) {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200192 dev_err(&pdev->dev,
Stephen Warrenf7265362013-02-15 17:07:33 -0700193 "Property 'nvidia,i2s-controller' missing or invalid\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200194 ret = -EINVAL;
Alexey Khoroshilov43217232018-07-28 00:06:59 +0300195 goto err_put_codec_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200196 }
197
Kuninori Morimotobae05f22019-06-28 10:48:40 +0900198 tegra_alc5632_dai.platforms->of_node = tegra_alc5632_dai.cpus->of_node;
199
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200200 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
201 if (ret)
Alexey Khoroshilov43217232018-07-28 00:06:59 +0300202 goto err_put_cpu_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200203
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200204 ret = snd_soc_register_card(card);
205 if (ret) {
206 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
207 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200208 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200209 }
210
211 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200212
213err_fini_utils:
214 tegra_asoc_utils_fini(&alc5632->util_data);
Alexey Khoroshilov43217232018-07-28 00:06:59 +0300215err_put_cpu_of_node:
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900216 of_node_put(tegra_alc5632_dai.cpus->of_node);
217 tegra_alc5632_dai.cpus->of_node = NULL;
Kuninori Morimotobae05f22019-06-28 10:48:40 +0900218 tegra_alc5632_dai.platforms->of_node = NULL;
Alexey Khoroshilov43217232018-07-28 00:06:59 +0300219err_put_codec_of_node:
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900220 of_node_put(tegra_alc5632_dai.codecs->of_node);
221 tegra_alc5632_dai.codecs->of_node = NULL;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200222err:
223 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200224}
225
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500226static int tegra_alc5632_remove(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200227{
228 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200229 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
230
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200231 snd_soc_unregister_card(card);
232
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200233 tegra_asoc_utils_fini(&machine->util_data);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200234
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900235 of_node_put(tegra_alc5632_dai.cpus->of_node);
236 tegra_alc5632_dai.cpus->of_node = NULL;
Kuninori Morimotobae05f22019-06-28 10:48:40 +0900237 tegra_alc5632_dai.platforms->of_node = NULL;
Kuninori Morimotoaf297e62019-06-06 13:18:46 +0900238 of_node_put(tegra_alc5632_dai.codecs->of_node);
239 tegra_alc5632_dai.codecs->of_node = NULL;
Alexey Khoroshilov43217232018-07-28 00:06:59 +0300240
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200241 return 0;
242}
243
Bill Pembertonf6e65742012-11-19 13:25:33 -0500244static const struct of_device_id tegra_alc5632_of_match[] = {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200245 { .compatible = "nvidia,tegra-audio-alc5632", },
246 {},
247};
248
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200249static struct platform_driver tegra_alc5632_driver = {
250 .driver = {
251 .name = DRV_NAME,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200252 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200253 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200254 },
255 .probe = tegra_alc5632_probe,
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500256 .remove = tegra_alc5632_remove,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200257};
258module_platform_driver(tegra_alc5632_driver);
259
260MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
261MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
262MODULE_LICENSE("GPL");
263MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200264MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);