blob: 17941392fd704c27a3e3faaa81236fcbb3b235b3 [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),
105};
106
107static const struct snd_soc_dapm_route tegra_alc5632_audio_map[] = {
108 /* Internal Speaker */
109 {"Int Spk", NULL, "SPKOUT"},
110 {"Int Spk", NULL, "SPKOUTN"},
111
112 /* Headset Mic */
113 {"MIC1", NULL, "MICBIAS1"},
114 {"MICBIAS1", NULL, "Headset Mic"},
115
116 /* Headset Stereophone */
117 {"Headset Stereophone", NULL, "HPR"},
118 {"Headset Stereophone", NULL, "HPL"},
119};
120
121static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
122 SOC_DAPM_PIN_SWITCH("Int Spk"),
123};
124
125static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
126{
127 struct snd_soc_codec *codec = rtd->codec;
128 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200129 struct device_node *np = codec->card->dev->of_node;
130 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
131 int ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200132
133 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
134 &tegra_alc5632_hs_jack);
135 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
136 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
137 tegra_alc5632_hs_jack_pins);
138
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200139 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
140
141 if (gpio_is_valid(machine->gpio_hp_det)) {
142 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
143 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
144 1,
145 &tegra_alc5632_hp_jack_gpio);
146 machine->gpio_requested |= GPIO_HP_DET;
147 }
148
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200149 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
150
151 return 0;
152}
153
154static struct snd_soc_dai_link tegra_alc5632_dai = {
155 .name = "ALC5632",
156 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200157 .platform_name = "tegra-pcm-audio",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200158 .codec_dai_name = "alc5632-hifi",
159 .init = tegra_alc5632_asoc_init,
160 .ops = &tegra_alc5632_asoc_ops,
161 .dai_fmt = SND_SOC_DAIFMT_I2S
162 | SND_SOC_DAIFMT_NB_NF
163 | SND_SOC_DAIFMT_CBS_CFS,
164};
165
166static struct snd_soc_card snd_soc_tegra_alc5632 = {
167 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800168 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200169 .dai_link = &tegra_alc5632_dai,
170 .num_links = 1,
171 .controls = tegra_alc5632_controls,
172 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
173 .dapm_widgets = tegra_alc5632_dapm_widgets,
174 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
175 .dapm_routes = tegra_alc5632_audio_map,
176 .num_dapm_routes = ARRAY_SIZE(tegra_alc5632_audio_map),
177 .fully_routed = true,
178};
179
180static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
181{
182 struct snd_soc_card *card = &snd_soc_tegra_alc5632;
183 struct tegra_alc5632 *alc5632;
184 int ret;
185
186 alc5632 = devm_kzalloc(&pdev->dev,
187 sizeof(struct tegra_alc5632), GFP_KERNEL);
188 if (!alc5632) {
189 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200190 ret = -ENOMEM;
191 goto err;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200192 }
193
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200194 card->dev = &pdev->dev;
195 platform_set_drvdata(pdev, card);
196 snd_soc_card_set_drvdata(card, alc5632);
197
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200198 alc5632->pcm_dev = ERR_PTR(-EINVAL);
199
200 if (!(pdev->dev.of_node)) {
201 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
202 ret = -EINVAL;
203 goto err;
204 }
205
206 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
207 if (ret)
208 goto err;
209
210 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
211 if (ret)
212 goto err;
213
214 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
215 pdev->dev.of_node, "nvidia,audio-codec", 0);
216
217 if (!tegra_alc5632_dai.codec_of_node) {
218 dev_err(&pdev->dev,
219 "Property 'nvidia,audio-codec' missing or invalid\n");
220 ret = -EINVAL;
221 goto err;
222 }
223
224 tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
225 pdev->dev.of_node, "nvidia,i2s-controller", 0);
226 if (!tegra_alc5632_dai.cpu_dai_of_node) {
227 dev_err(&pdev->dev,
228 "Property 'nvidia,i2s-controller' missing or invalid\n");
229 ret = -EINVAL;
230 goto err;
231 }
232
233 alc5632->pcm_dev = platform_device_register_simple(
234 "tegra-pcm-audio", -1, NULL, 0);
235 if (IS_ERR(alc5632->pcm_dev)) {
236 dev_err(&pdev->dev,
237 "Can't instantiate tegra-pcm-audio\n");
238 ret = PTR_ERR(alc5632->pcm_dev);
239 goto err;
240 }
241
242 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
243 if (ret)
244 goto err_unregister;
245
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200246 ret = snd_soc_register_card(card);
247 if (ret) {
248 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
249 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200250 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200251 }
252
253 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200254
255err_fini_utils:
256 tegra_asoc_utils_fini(&alc5632->util_data);
257err_unregister:
258 if (!IS_ERR(alc5632->pcm_dev))
259 platform_device_unregister(alc5632->pcm_dev);
260err:
261 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200262}
263
264static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
265{
266 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200267 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
268
269 if (machine->gpio_requested & GPIO_HP_DET)
270 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
271 1,
272 &tegra_alc5632_hp_jack_gpio);
273 machine->gpio_requested = 0;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200274
275 snd_soc_unregister_card(card);
276
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200277 tegra_asoc_utils_fini(&machine->util_data);
278 if (!IS_ERR(machine->pcm_dev))
279 platform_device_unregister(machine->pcm_dev);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200280
281 return 0;
282}
283
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200284static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
285 { .compatible = "nvidia,tegra-audio-alc5632", },
286 {},
287};
288
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200289static struct platform_driver tegra_alc5632_driver = {
290 .driver = {
291 .name = DRV_NAME,
292 .owner = THIS_MODULE,
293 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200294 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200295 },
296 .probe = tegra_alc5632_probe,
297 .remove = __devexit_p(tegra_alc5632_remove),
298};
299module_platform_driver(tegra_alc5632_driver);
300
301MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
302MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
303MODULE_LICENSE("GPL");
304MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200305MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);