blob: 76d759ec40a2adbebac7b750aa9b9e3419931efb [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,
Leon Romanovskyd559f1e2012-02-02 22:13:37 +020098};
99
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200100static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
101 SND_SOC_DAPM_SPK("Int Spk", NULL),
102 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
103 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Leon Romanovskydd7d3a22012-02-13 21:27:36 +0200104 SND_SOC_DAPM_MIC("Digital Mic", NULL),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200105};
106
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200107static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
108 SOC_DAPM_PIN_SWITCH("Int Spk"),
109};
110
111static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
112{
113 struct snd_soc_codec *codec = rtd->codec;
114 struct snd_soc_dapm_context *dapm = &codec->dapm;
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200115 struct device_node *np = codec->card->dev->of_node;
116 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200117
118 snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
119 &tegra_alc5632_hs_jack);
120 snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
121 ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
122 tegra_alc5632_hs_jack_pins);
123
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200124 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
125
126 if (gpio_is_valid(machine->gpio_hp_det)) {
127 tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det;
128 snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack,
129 1,
130 &tegra_alc5632_hp_jack_gpio);
131 machine->gpio_requested |= GPIO_HP_DET;
132 }
133
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200134 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
135
136 return 0;
137}
138
139static struct snd_soc_dai_link tegra_alc5632_dai = {
140 .name = "ALC5632",
141 .stream_name = "ALC5632 PCM",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200142 .platform_name = "tegra-pcm-audio",
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200143 .codec_dai_name = "alc5632-hifi",
144 .init = tegra_alc5632_asoc_init,
145 .ops = &tegra_alc5632_asoc_ops,
146 .dai_fmt = SND_SOC_DAIFMT_I2S
147 | SND_SOC_DAIFMT_NB_NF
148 | SND_SOC_DAIFMT_CBS_CFS,
149};
150
151static struct snd_soc_card snd_soc_tegra_alc5632 = {
152 .name = "tegra-alc5632",
Axel Linb16eaf92011-12-22 21:23:01 +0800153 .owner = THIS_MODULE,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200154 .dai_link = &tegra_alc5632_dai,
155 .num_links = 1,
156 .controls = tegra_alc5632_controls,
157 .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
158 .dapm_widgets = tegra_alc5632_dapm_widgets,
159 .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200160 .fully_routed = true,
161};
162
163static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
164{
165 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");
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200173 ret = -ENOMEM;
174 goto err;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200175 }
176
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200177 card->dev = &pdev->dev;
178 platform_set_drvdata(pdev, card);
179 snd_soc_card_set_drvdata(card, alc5632);
180
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200181 alc5632->pcm_dev = ERR_PTR(-EINVAL);
182
183 if (!(pdev->dev.of_node)) {
184 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
185 ret = -EINVAL;
186 goto err;
187 }
188
189 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
190 if (ret)
191 goto err;
192
193 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
194 if (ret)
195 goto err;
196
197 tegra_alc5632_dai.codec_of_node = of_parse_phandle(
198 pdev->dev.of_node, "nvidia,audio-codec", 0);
199
200 if (!tegra_alc5632_dai.codec_of_node) {
201 dev_err(&pdev->dev,
202 "Property 'nvidia,audio-codec' missing or invalid\n");
203 ret = -EINVAL;
204 goto err;
205 }
206
207 tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
208 pdev->dev.of_node, "nvidia,i2s-controller", 0);
209 if (!tegra_alc5632_dai.cpu_dai_of_node) {
210 dev_err(&pdev->dev,
211 "Property 'nvidia,i2s-controller' missing or invalid\n");
212 ret = -EINVAL;
213 goto err;
214 }
215
216 alc5632->pcm_dev = platform_device_register_simple(
217 "tegra-pcm-audio", -1, NULL, 0);
218 if (IS_ERR(alc5632->pcm_dev)) {
219 dev_err(&pdev->dev,
220 "Can't instantiate tegra-pcm-audio\n");
221 ret = PTR_ERR(alc5632->pcm_dev);
222 goto err;
223 }
224
225 ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
226 if (ret)
227 goto err_unregister;
228
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200229 ret = snd_soc_register_card(card);
230 if (ret) {
231 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
232 ret);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200233 goto err_fini_utils;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200234 }
235
236 return 0;
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200237
238err_fini_utils:
239 tegra_asoc_utils_fini(&alc5632->util_data);
240err_unregister:
241 if (!IS_ERR(alc5632->pcm_dev))
242 platform_device_unregister(alc5632->pcm_dev);
243err:
244 return ret;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200245}
246
247static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
248{
249 struct snd_soc_card *card = platform_get_drvdata(pdev);
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200250 struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card);
251
252 if (machine->gpio_requested & GPIO_HP_DET)
253 snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack,
254 1,
255 &tegra_alc5632_hp_jack_gpio);
256 machine->gpio_requested = 0;
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200257
258 snd_soc_unregister_card(card);
259
Leon Romanovskyd559f1e2012-02-02 22:13:37 +0200260 tegra_asoc_utils_fini(&machine->util_data);
261 if (!IS_ERR(machine->pcm_dev))
262 platform_device_unregister(machine->pcm_dev);
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200263
264 return 0;
265}
266
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200267static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
268 { .compatible = "nvidia,tegra-audio-alc5632", },
269 {},
270};
271
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200272static struct platform_driver tegra_alc5632_driver = {
273 .driver = {
274 .name = DRV_NAME,
275 .owner = THIS_MODULE,
276 .pm = &snd_soc_pm_ops,
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200277 .of_match_table = tegra_alc5632_of_match,
Leon Romanovsky58783fa2011-12-19 21:51:52 +0200278 },
279 .probe = tegra_alc5632_probe,
280 .remove = __devexit_p(tegra_alc5632_remove),
281};
282module_platform_driver(tegra_alc5632_driver);
283
284MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
285MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
286MODULE_LICENSE("GPL");
287MODULE_ALIAS("platform:" DRV_NAME);
Leon Romanovskyb4dc0a72012-01-31 09:26:59 +0200288MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);