]>
Commit | Line | Data |
---|---|---|
58783faf | 1 | /* |
1ae93b9d SW |
2 | * tegra_alc5632.c -- Toshiba AC100(PAZ00) machine ASoC driver |
3 | * | |
4 | * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net> | |
518de86b | 5 | * Copyright (C) 2012 - NVIDIA, Inc. |
1ae93b9d SW |
6 | * |
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 | */ | |
58783faf LR |
15 | |
16 | #include <asm/mach-types.h> | |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/gpio.h> | |
d559f1e5 | 22 | #include <linux/of_gpio.h> |
58783faf LR |
23 | |
24 | #include <sound/core.h> | |
25 | #include <sound/jack.h> | |
26 | #include <sound/pcm.h> | |
27 | #include <sound/pcm_params.h> | |
28 | #include <sound/soc.h> | |
29 | ||
30 | #include "../codecs/alc5632.h" | |
31 | ||
58783faf LR |
32 | #include "tegra_asoc_utils.h" |
33 | ||
34 | #define DRV_NAME "tegra-alc5632" | |
35 | ||
d559f1e5 LR |
36 | #define GPIO_HP_DET BIT(0) |
37 | ||
58783faf LR |
38 | struct tegra_alc5632 { |
39 | struct tegra_asoc_utils_data util_data; | |
d559f1e5 LR |
40 | int gpio_requested; |
41 | int gpio_hp_det; | |
58783faf LR |
42 | }; |
43 | ||
44 | static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream, | |
45 | struct snd_pcm_hw_params *params) | |
46 | { | |
47 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
48 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
49 | struct snd_soc_codec *codec = rtd->codec; | |
50 | struct snd_soc_card *card = codec->card; | |
51 | struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card); | |
52 | int srate, mclk; | |
53 | int err; | |
54 | ||
55 | srate = params_rate(params); | |
56 | mclk = 512 * srate; | |
57 | ||
58 | err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk); | |
59 | if (err < 0) { | |
60 | dev_err(card->dev, "Can't configure clocks\n"); | |
61 | return err; | |
62 | } | |
63 | ||
64 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, | |
65 | SND_SOC_CLOCK_IN); | |
66 | if (err < 0) { | |
67 | dev_err(card->dev, "codec_dai clock not set\n"); | |
68 | return err; | |
69 | } | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | static struct snd_soc_ops tegra_alc5632_asoc_ops = { | |
75 | .hw_params = tegra_alc5632_asoc_hw_params, | |
76 | }; | |
77 | ||
78 | static struct snd_soc_jack tegra_alc5632_hs_jack; | |
79 | ||
80 | static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = { | |
81 | { | |
82 | .pin = "Headset Mic", | |
83 | .mask = SND_JACK_MICROPHONE, | |
84 | }, | |
85 | { | |
86 | .pin = "Headset Stereophone", | |
87 | .mask = SND_JACK_HEADPHONE, | |
88 | }, | |
89 | }; | |
90 | ||
d559f1e5 LR |
91 | static struct snd_soc_jack_gpio tegra_alc5632_hp_jack_gpio = { |
92 | .name = "Headset detection", | |
93 | .report = SND_JACK_HEADSET, | |
94 | .debounce_time = 150, | |
95 | .invert = 1, | |
96 | }; | |
97 | ||
58783faf LR |
98 | static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = { |
99 | SND_SOC_DAPM_SPK("Int Spk", NULL), | |
100 | SND_SOC_DAPM_HP("Headset Stereophone", NULL), | |
101 | SND_SOC_DAPM_MIC("Headset Mic", NULL), | |
dd7d3a24 | 102 | SND_SOC_DAPM_MIC("Digital Mic", NULL), |
58783faf LR |
103 | }; |
104 | ||
58783faf LR |
105 | static const struct snd_kcontrol_new tegra_alc5632_controls[] = { |
106 | SOC_DAPM_PIN_SWITCH("Int Spk"), | |
107 | }; | |
108 | ||
109 | static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd) | |
110 | { | |
111 | struct snd_soc_codec *codec = rtd->codec; | |
112 | struct snd_soc_dapm_context *dapm = &codec->dapm; | |
d559f1e5 LR |
113 | struct device_node *np = codec->card->dev->of_node; |
114 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(codec->card); | |
58783faf LR |
115 | |
116 | snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET, | |
117 | &tegra_alc5632_hs_jack); | |
118 | snd_soc_jack_add_pins(&tegra_alc5632_hs_jack, | |
119 | ARRAY_SIZE(tegra_alc5632_hs_jack_pins), | |
120 | tegra_alc5632_hs_jack_pins); | |
121 | ||
d559f1e5 LR |
122 | machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0); |
123 | ||
124 | if (gpio_is_valid(machine->gpio_hp_det)) { | |
125 | tegra_alc5632_hp_jack_gpio.gpio = machine->gpio_hp_det; | |
126 | snd_soc_jack_add_gpios(&tegra_alc5632_hs_jack, | |
127 | 1, | |
128 | &tegra_alc5632_hp_jack_gpio); | |
129 | machine->gpio_requested |= GPIO_HP_DET; | |
130 | } | |
131 | ||
58783faf LR |
132 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static struct snd_soc_dai_link tegra_alc5632_dai = { | |
138 | .name = "ALC5632", | |
139 | .stream_name = "ALC5632 PCM", | |
58783faf LR |
140 | .codec_dai_name = "alc5632-hifi", |
141 | .init = tegra_alc5632_asoc_init, | |
142 | .ops = &tegra_alc5632_asoc_ops, | |
143 | .dai_fmt = SND_SOC_DAIFMT_I2S | |
144 | | SND_SOC_DAIFMT_NB_NF | |
145 | | SND_SOC_DAIFMT_CBS_CFS, | |
146 | }; | |
147 | ||
148 | static struct snd_soc_card snd_soc_tegra_alc5632 = { | |
149 | .name = "tegra-alc5632", | |
b16eaf9f | 150 | .owner = THIS_MODULE, |
58783faf LR |
151 | .dai_link = &tegra_alc5632_dai, |
152 | .num_links = 1, | |
153 | .controls = tegra_alc5632_controls, | |
154 | .num_controls = ARRAY_SIZE(tegra_alc5632_controls), | |
155 | .dapm_widgets = tegra_alc5632_dapm_widgets, | |
156 | .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets), | |
58783faf LR |
157 | .fully_routed = true, |
158 | }; | |
159 | ||
160 | static __devinit int tegra_alc5632_probe(struct platform_device *pdev) | |
161 | { | |
162 | struct snd_soc_card *card = &snd_soc_tegra_alc5632; | |
163 | struct tegra_alc5632 *alc5632; | |
164 | int ret; | |
165 | ||
166 | alc5632 = devm_kzalloc(&pdev->dev, | |
167 | sizeof(struct tegra_alc5632), GFP_KERNEL); | |
168 | if (!alc5632) { | |
169 | dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n"); | |
b4dc0a75 LR |
170 | ret = -ENOMEM; |
171 | goto err; | |
58783faf LR |
172 | } |
173 | ||
58783faf LR |
174 | card->dev = &pdev->dev; |
175 | platform_set_drvdata(pdev, card); | |
176 | snd_soc_card_set_drvdata(card, alc5632); | |
177 | ||
b4dc0a75 LR |
178 | if (!(pdev->dev.of_node)) { |
179 | dev_err(&pdev->dev, "Must be instantiated using device tree\n"); | |
180 | ret = -EINVAL; | |
181 | goto err; | |
182 | } | |
183 | ||
184 | 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 | ||
202 | tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle( | |
203 | pdev->dev.of_node, "nvidia,i2s-controller", 0); | |
204 | if (!tegra_alc5632_dai.cpu_dai_of_node) { | |
205 | dev_err(&pdev->dev, | |
206 | "Property 'nvidia,i2s-controller' missing or invalid\n"); | |
207 | ret = -EINVAL; | |
208 | goto err; | |
209 | } | |
210 | ||
518de86b | 211 | tegra_alc5632_dai.platform_of_node = tegra_alc5632_dai.cpu_dai_of_node; |
b4dc0a75 LR |
212 | |
213 | ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev); | |
214 | if (ret) | |
518de86b | 215 | goto err; |
b4dc0a75 | 216 | |
58783faf LR |
217 | ret = snd_soc_register_card(card); |
218 | if (ret) { | |
219 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", | |
220 | ret); | |
b4dc0a75 | 221 | goto err_fini_utils; |
58783faf LR |
222 | } |
223 | ||
224 | return 0; | |
b4dc0a75 LR |
225 | |
226 | err_fini_utils: | |
227 | tegra_asoc_utils_fini(&alc5632->util_data); | |
b4dc0a75 LR |
228 | err: |
229 | return ret; | |
58783faf LR |
230 | } |
231 | ||
232 | static int __devexit tegra_alc5632_remove(struct platform_device *pdev) | |
233 | { | |
234 | struct snd_soc_card *card = platform_get_drvdata(pdev); | |
d559f1e5 LR |
235 | struct tegra_alc5632 *machine = snd_soc_card_get_drvdata(card); |
236 | ||
237 | if (machine->gpio_requested & GPIO_HP_DET) | |
238 | snd_soc_jack_free_gpios(&tegra_alc5632_hs_jack, | |
239 | 1, | |
240 | &tegra_alc5632_hp_jack_gpio); | |
241 | machine->gpio_requested = 0; | |
58783faf LR |
242 | |
243 | snd_soc_unregister_card(card); | |
244 | ||
d559f1e5 | 245 | tegra_asoc_utils_fini(&machine->util_data); |
58783faf LR |
246 | |
247 | return 0; | |
248 | } | |
249 | ||
b4dc0a75 LR |
250 | static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = { |
251 | { .compatible = "nvidia,tegra-audio-alc5632", }, | |
252 | {}, | |
253 | }; | |
254 | ||
58783faf LR |
255 | static struct platform_driver tegra_alc5632_driver = { |
256 | .driver = { | |
257 | .name = DRV_NAME, | |
258 | .owner = THIS_MODULE, | |
259 | .pm = &snd_soc_pm_ops, | |
b4dc0a75 | 260 | .of_match_table = tegra_alc5632_of_match, |
58783faf LR |
261 | }, |
262 | .probe = tegra_alc5632_probe, | |
263 | .remove = __devexit_p(tegra_alc5632_remove), | |
264 | }; | |
265 | module_platform_driver(tegra_alc5632_driver); | |
266 | ||
267 | MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>"); | |
268 | MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver"); | |
269 | MODULE_LICENSE("GPL"); | |
270 | MODULE_ALIAS("platform:" DRV_NAME); | |
b4dc0a75 | 271 | MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match); |