blob: 2a27725cc9b1687a9a7ee4d8f40d953c9eb3e7ec [file] [log] [blame]
Leon Romanovsky58783fa2011-12-19 21:51:52 +02001/*
2* tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver
3*
4* Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
5*
6* Authors: Leon Romanovsky <leon@leon.nu>
7* Andrey Danin <danindrey@mail.ru>
8* Marc Dietrich <marvin24@gmx.de>
9*
10* This program is free software; you can redistribute it and/or modify
11* it under the terms of the GNU General Public License version 2 as
12* published by the Free Software Foundation.
13*/
14
15#include <asm/mach-types.h>
16
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/gpio.h>
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020021#include <linux/of_gpio.h>
Leon Romanovsky58783fa2011-12-19 21:51:52 +020022
23#include <sound/core.h>
24#include <sound/jack.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28
29#include "../codecs/alc5632.h"
30
31#include "tegra_das.h"
32#include "tegra_i2s.h"
33#include "tegra_pcm.h"
34#include "tegra_asoc_utils.h"
35
36#define DRV_NAME "tegra-alc5632"
37
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020038#define GPIO_HP_DET BIT(0)
39
Leon Romanovsky58783fa2011-12-19 21:51:52 +020040struct tegra_alc5632 {
41 struct tegra_asoc_utils_data util_data;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +020042 struct platform_device *pcm_dev;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020043 int gpio_requested;
44 int gpio_hp_det;
Leon Romanovsky58783fa2011-12-19 21:51:52 +020045};
46
47static int tegra_alc5632_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_codec *codec = rtd->codec;
53 struct snd_soc_card *card = codec->card;
54 struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
55 int srate, mclk;
56 int err;
57
58 srate = params_rate(params);
59 mclk = 512 * srate;
60
61 err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
62 if (err < 0) {
63 dev_err(card->dev, "Can't configure clocks\n");
64 return err;
65 }
66
67 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
68 SND_SOC_CLOCK_IN);
69 if (err < 0) {
70 dev_err(card->dev, "codec_dai clock not set\n");
71 return err;
72 }
73
74 return 0;
75}
76
77static struct snd_soc_ops tegra_alc5632_asoc_ops = {
78 .hw_params = tegra_alc5632_asoc_hw_params,
79};
80
81static struct snd_soc_jack tegra_alc5632_hs_jack;
82
83static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
84 {
85 .pin = "Headset Mic",
86 .mask = SND_JACK_MICROPHONE,
87 },
88 {
89 .pin = "Headset Stereophone",
90 .mask = SND_JACK_HEADPHONE,
91 },
92};
93
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020094static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = {
95 .name = "Headset detection",
96 .report = SND_JACK_HEADSET,
97 .debounce_time = 150,
98 .invert = 1,
99};
100
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200101static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
102 SND_SOC_DAPM_SPK("Int Spk", NULL),
103 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
104 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +0200105 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200106};
107
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200108static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
109 SOC_DAPM_PIN_SWITCH("Int Spk"),
110};
111
112static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
113{
114 struct snd_soc_codec *codec = rtd->codec;
115 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200116 struct device_node *np = codec->card->dev->of_node;
117 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
118 int ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200119
120 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
121 &tegra_alc5632_hs_jack);
122 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
123 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
124 tegra_alc5632_hs_jack_pins);
125
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200126 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
127
128 if (gpio_is_valid(machine->gpio_hp_det)) {
129 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
130 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
131 1,
132 &tegra_alc5632_hp_jack_gpio);
133 machine->gpio_requested |= GPIO_HP_DET;
134 }
135
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200136 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
137
138 return 0;
139}
140
141static struct snd_soc_dai_link tegra_alc5632_dai = {
142 .name = "ALC5632",
143 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200144 .platform_name = "tegra-pcm-audio",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200145 .codec_dai_name = "alc5632-hifi",
146 .init = tegra_alc5632_asoc_init,
147 .ops = &tegra_alc5632_asoc_ops,
148 .dai_fmt = SND_SOC_DAIFMT_I2S
149 | SND_SOC_DAIFMT_NB_NF
150 | SND_SOC_DAIFMT_CBS_CFS,
151};
152
153static struct snd_soc_card snd_soc_tegra_alc5632 = {
154 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800155 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200156 .dai_link = &tegra_alc5632_dai,
157 .num_links = 1,
158 .controls = tegra_alc5632_controls,
159 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
160 .dapm_widgets = tegra_alc5632_dapm_widgets,
161 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200162 .fully_routed = true,
163};
164
165static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
166{
167 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
168 struct tegra_alc5632 *alc5632;
169 int ret;
170
171 alc5632 = devm_kzalloc(&pdev->dev,
172 sizeof(struct tegra_alc5632), GFP_KERNEL);
173 if (!alc5632) {
174 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200175 ret = -ENOMEM;
176 goto err;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200177 }
178
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200179 card->dev = &pdev->dev;
180 platform_set_drvdata(pdev, card);
181 snd_soc_card_set_drvdata(card, alc5632);
182
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200183 alc5632->pcm_dev = ERR_PTR(-EINVAL);
184
185 if (!(pdev->dev.of_node)) {
186 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
187 ret = -EINVAL;
188 goto err;
189 }
190
191 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
192 if (ret)
193 goto err;
194
195 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
196 if (ret)
197 goto err;
198
199 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
200 pdev->dev.of_node, "nvidia,audio-codec", 0);
201
202 if (!tegra_alc5632_dai.codec_of_node) {
203 dev_err(&pdev->dev,
204 "Property 'nvidia,audio-codec' missing or invalid\n");
205 ret = -EINVAL;
206 goto err;
207 }
208
209 tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
210 pdev->dev.of_node, "nvidia,i2s-controller", 0);
211 if (!tegra_alc5632_dai.cpu_dai_of_node) {
212 dev_err(&pdev->dev,
213 "Property 'nvidia,i2s-controller' missing or invalid\n");
214 ret = -EINVAL;
215 goto err;
216 }
217
218 alc5632->pcm_dev = platform_device_register_simple(
219 "tegra-pcm-audio", -1, NULL, 0);
220 if (IS_ERR(alc5632->pcm_dev)) {
221 dev_err(&pdev->dev,
222 "Can't instantiate tegra-pcm-audio\n");
223 ret = PTR_ERR(alc5632->pcm_dev);
224 goto err;
225 }
226
227 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
228 if (ret)
229 goto err_unregister;
230
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200231 ret = snd_soc_register_card(card);
232 if (ret) {
233 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
234 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200235 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200236 }
237
238 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200239
240err_fini_utils:
241 tegra_asoc_utils_fini(&alc5632->util_data);
242err_unregister:
243 if (!IS_ERR(alc5632->pcm_dev))
244 platform_device_unregister(alc5632->pcm_dev);
245err:
246 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200247}
248
249static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
250{
251 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200252 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
253
254 if (machine->gpio_requested & GPIO_HP_DET)
255 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
256 1,
257 &tegra_alc5632_hp_jack_gpio);
258 machine->gpio_requested = 0;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200259
260 snd_soc_unregister_card(card);
261
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200262 tegra_asoc_utils_fini(&machine->util_data);
263 if (!IS_ERR(machine->pcm_dev))
264 platform_device_unregister(machine->pcm_dev);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200265
266 return 0;
267}
268
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200269static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
270 { .compatible = "nvidia,tegra-audio-alc5632", },
271 {},
272};
273
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200274static struct platform_driver tegra_alc5632_driver = {
275 .driver = {
276 .name = DRV_NAME,
277 .owner = THIS_MODULE,
278 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200279 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200280 },
281 .probe = tegra_alc5632_probe,
282 .remove = __devexit_p(tegra_alc5632_remove),
283};
284module_platform_driver(tegra_alc5632_driver);
285
286MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
287MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
288MODULE_LICENSE("GPL");
289MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200290MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);