blob: 6dcd06a966c7c6149b8020f806e3bbb9c09af976 [file] [log] [blame]
Leon Romanovsky58783fa2011-12-19 21:51:52 +02001/*
Stephen Warrenaef9a372012-05-22 16:09:51 -06002* tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
Stephen Warren1ae93b92012-03-20 14:55:48 -06003 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
Stephen Warren518de862012-03-20 14:55:49 -06005 * Copyright (C) 2012 - NVIDIA, Inc.
Stephen Warren1ae93b92012-03-20 14:55:48 -06006 *
7 * Authors: Leon Romanovsky <leon@leon.nu>
8 * Andrey Danin <danindrey@mail.ru>
9 * Marc Dietrich <marvin24@gmx.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
Leon Romanovsky58783fa2011-12-19 21:51:52 +020015
Leon Romanovsky58783fa2011-12-19 21:51:52 +020016#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/gpio.h>
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020020#include <linux/of_gpio.h>
Leon Romanovsky58783fa2011-12-19 21:51:52 +020021
22#include <sound/core.h>
23#include <sound/jack.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27
28#include "../codecs/alc5632.h"
29
Leon Romanovsky58783fa2011-12-19 21:51:52 +020030#include "tegra_asoc_utils.h"
31
32#define DRV_NAME "tegra-alc5632"
33
34struct tegra_alc5632 {
35 struct tegra_asoc_utils_data util_data;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020036 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020037};
38
39static int tegra_alc5632_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;
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +020044 struct snd_soc_card *card = rtd->card;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020045 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
46 int srate, mclk;
47 int err;
48
49 srate = params_rate(params);
50 mclk = 512 * srate;
51
52 err = tegra_asoc_utils_set_rate(&alc5632->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, 0, 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
68static struct snd_soc_ops tegra_alc5632_asoc_ops = {
69 .hw_params = tegra_alc5632_asoc_hw_params,
70};
71
72static struct snd_soc_jack tegra_alc5632_hs_jack;
73
74static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
75 {
76 .pin = "Headset Mic",
77 .mask = SND_JACK_MICROPHONE,
78 },
79 {
80 .pin = "Headset Stereophone",
81 .mask = SND_JACK_HEADPHONE,
82 },
83};
84
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020085static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
86 .name = "Headset detection",
87 .report = SND_JACK_HEADSET,
88 .debounce_time = 150,
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020089};
90
Leon Romanovsky58783fa2011-12-19 21:51:52 +020091static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
92 SND_SOC_DAPM_SPK("Int Spk", NULL),
93 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
94 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +020095 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +020096};
97
Leon Romanovsky58783fa2011-12-19 21:51:52 +020098static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
99 SOC_DAPM_PIN_SWITCH("Int Spk"),
100};
101
102static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
103{
Stephen Warren40db77a2012-06-06 17:15:49 -0600104 struct snd_soc_dai *codec_dai = rtd->codec_dai;
105 struct snd_soc_codec *codec = codec_dai->codec;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200106 struct snd_soc_dapm_context *dapm = &codec->dapm;
Lars-Peter Clausen093c4e52014-07-17 22:01:06 +0200107 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(rtd->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200108
Lars-Peter Clausen12cc6d12015-03-04 10:33:40 +0100109 snd_soc_card_jack_new(rtd->card, "Headset Jack", SND_JACK_HEADSET,
110 &tegra_alc5632_hs_jack,
111 tegra_alc5632_hs_jack_pins,
112 ARRAY_SIZE(tegra_alc5632_hs_jack_pins));
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200113
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200114 if (gpio_is_valid(machine->gpio_hp_det)) {
115 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
116 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
117 1,
118 &tegra_alc5632_hp_jack_gpio);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200119 }
120
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200121 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
122
123 return 0;
124}
125
Stephen Warrenfb6b8e72014-05-22 16:14:53 -0600126static int tegra_alc5632_card_remove(struct snd_soc_card *card)
127{
128 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
129
130 if (gpio_is_valid(machine->gpio_hp_det)) {
131 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack, 1,
132 &tegra_alc5632_hp_jack_gpio);
133 }
134
135 return 0;
136}
137
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200138static struct snd_soc_dai_link tegra_alc5632_dai = {
139 .name = "ALC5632",
140 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200141 .codec_dai_name = "alc5632-hifi",
142 .init = tegra_alc5632_asoc_init,
143 .ops = &tegra_alc5632_asoc_ops,
144 .dai_fmt = SND_SOC_DAIFMT_I2S
145 | SND_SOC_DAIFMT_NB_NF
146 | SND_SOC_DAIFMT_CBS_CFS,
147};
148
149static struct snd_soc_card snd_soc_tegra_alc5632 = {
150 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800151 .owner = THIS_MODULE,
Stephen Warrenfb6b8e72014-05-22 16:14:53 -0600152 .remove = tegra_alc5632_card_remove,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200153 .dai_link = &tegra_alc5632_dai,
154 .num_links = 1,
155 .controls = tegra_alc5632_controls,
156 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
157 .dapm_widgets = tegra_alc5632_dapm_widgets,
158 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200159 .fully_routed = true,
160};
161
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500162static int tegra_alc5632_probe(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200163{
Stephen Warrenaef9a372012-05-22 16:09:51 -0600164 struct device_node *np = pdev->dev.of_node;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200165 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
166 struct tegra_alc5632 *alc5632;
167 int ret;
168
169 alc5632 = devm_kzalloc(&pdev->dev,
170 sizeof(struct tegra_alc5632), GFP_KERNEL);
171 if (!alc5632) {
172 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Stephen Warrenf7265362013-02-15 17:07:33 -0700173 return -ENOMEM;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200174 }
175
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200176 card->dev = &pdev->dev;
177 platform_set_drvdata(pdev, card);
178 snd_soc_card_set_drvdata(card, alc5632);
179
Stephen Warrenaef9a372012-05-22 16:09:51 -0600180 alc5632->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
Roland Stiggebbfc3282012-06-18 18:42:22 +0200181 if (alc5632->gpio_hp_det == -EPROBE_DEFER)
Stephen Warrenaef9a372012-05-22 16:09:51 -0600182 return -EPROBE_DEFER;
183
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200184 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
185 if (ret)
186 goto err;
187
188 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
189 if (ret)
190 goto err;
191
192 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
193 pdev->dev.of_node, "nvidia,audio-codec", 0);
194
195 if (!tegra_alc5632_dai.codec_of_node) {
196 dev_err(&pdev->dev,
197 "Property 'nvidia,audio-codec' missing or invalid\n");
198 ret = -EINVAL;
199 goto err;
200 }
201
Stephen Warrenf7265362013-02-15 17:07:33 -0700202 tegra_alc5632_dai.cpu_of_node = of_parse_phandle(np,
203 "nvidia,i2s-controller", 0);
Stephen Warrenbc926572012-05-25 18:22:11 -0600204 if (!tegra_alc5632_dai.cpu_of_node) {
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200205 dev_err(&pdev->dev,
Stephen Warrenf7265362013-02-15 17:07:33 -0700206 "Property 'nvidia,i2s-controller' missing or invalid\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200207 ret = -EINVAL;
208 goto err;
209 }
210
Stephen Warrenbc926572012-05-25 18:22:11 -0600211 tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_of_node;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200212
213 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
214 if (ret)
Stephen Warren518de862012-03-20 14:55:49 -0600215 goto err;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200216
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200217 ret = snd_soc_register_card(card);
218 if (ret) {
219 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
220 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200221 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200222 }
223
224 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200225
226err_fini_utils:
227 tegra_asoc_utils_fini(&alc5632->util_data);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200228err:
229 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200230}
231
Bill Pemberton4652a0d2012-12-07 09:26:33 -0500232static int tegra_alc5632_remove(struct platform_device *pdev)
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200233{
234 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200235 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
236
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200237 snd_soc_unregister_card(card);
238
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200239 tegra_asoc_utils_fini(&machine->util_data);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200240
241 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);