ASoC: tegra_max98090: Register jacks at the card level
[deliverable/linux.git] / sound / soc / tegra / tegra_rt5640.c
CommitLineData
040a62cf
SW
1/*
2* tegra_rt5640.c - Tegra machine ASoC driver for boards using WM8903 codec.
3 *
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Based on code copyright/by:
19 *
20 * Copyright (C) 2010-2012 - NVIDIA, Inc.
21 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
22 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
23 * Copyright 2007 Wolfson Microelectronics PLC.
24 */
25
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29#include <linux/gpio.h>
30#include <linux/of_gpio.h>
31
32#include <sound/core.h>
33#include <sound/jack.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/soc.h>
37
38#include "../codecs/rt5640.h"
39
40#include "tegra_asoc_utils.h"
41
42#define DRV_NAME "tegra-snd-rt5640"
43
44struct tegra_rt5640 {
45 struct tegra_asoc_utils_data util_data;
46 int gpio_hp_det;
469cda29 47 enum of_gpio_flags gpio_hp_det_flags;
040a62cf
SW
48};
49
50static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
51 struct snd_pcm_hw_params *params)
52{
53 struct snd_soc_pcm_runtime *rtd = substream->private_data;
54 struct snd_soc_dai *codec_dai = rtd->codec_dai;
093c4e5c 55 struct snd_soc_card *card = rtd->card;
040a62cf
SW
56 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
57 int srate, mclk;
58 int err;
59
60 srate = params_rate(params);
61 mclk = 256 * srate;
62
63 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
64 if (err < 0) {
65 dev_err(card->dev, "Can't configure clocks\n");
66 return err;
67 }
68
69 err = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, mclk,
70 SND_SOC_CLOCK_IN);
71 if (err < 0) {
72 dev_err(card->dev, "codec_dai clock not set\n");
73 return err;
74 }
75
76 return 0;
77}
78
79static struct snd_soc_ops tegra_rt5640_ops = {
80 .hw_params = tegra_rt5640_asoc_hw_params,
81};
82
83static struct snd_soc_jack tegra_rt5640_hp_jack;
84
85static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
86 {
87 .pin = "Headphones",
88 .mask = SND_JACK_HEADPHONE,
89 },
90};
91
92static struct snd_soc_jack_gpio tegra_rt5640_hp_jack_gpio = {
93 .name = "Headphone detection",
94 .report = SND_JACK_HEADPHONE,
95 .debounce_time = 150,
96 .invert = 1,
97};
98
99static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
100 SND_SOC_DAPM_HP("Headphones", NULL),
101 SND_SOC_DAPM_SPK("Speakers", NULL),
7ac0da8c 102 SND_SOC_DAPM_MIC("Mic Jack", NULL),
040a62cf
SW
103};
104
105static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
106 SOC_DAPM_PIN_SWITCH("Speakers"),
107};
108
109static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
110{
111 struct snd_soc_dai *codec_dai = rtd->codec_dai;
112 struct snd_soc_codec *codec = codec_dai->codec;
093c4e5c 113 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(rtd->card);
040a62cf
SW
114
115 snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
116 &tegra_rt5640_hp_jack);
117 snd_soc_jack_add_pins(&tegra_rt5640_hp_jack,
118 ARRAY_SIZE(tegra_rt5640_hp_jack_pins),
119 tegra_rt5640_hp_jack_pins);
120
121 if (gpio_is_valid(machine->gpio_hp_det)) {
122 tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
469cda29
AB
123 tegra_rt5640_hp_jack_gpio.invert =
124 !!(machine->gpio_hp_det_flags & OF_GPIO_ACTIVE_LOW);
040a62cf
SW
125 snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
126 1,
127 &tegra_rt5640_hp_jack_gpio);
128 }
129
130 return 0;
131}
132
fb6b8e71
SW
133static int tegra_rt5640_card_remove(struct snd_soc_card *card)
134{
135 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
136
137 if (gpio_is_valid(machine->gpio_hp_det)) {
138 snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
139 &tegra_rt5640_hp_jack_gpio);
140 }
141
142 return 0;
143}
144
040a62cf
SW
145static struct snd_soc_dai_link tegra_rt5640_dai = {
146 .name = "RT5640",
147 .stream_name = "RT5640 PCM",
148 .codec_dai_name = "rt5640-aif1",
149 .init = tegra_rt5640_asoc_init,
150 .ops = &tegra_rt5640_ops,
151 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
152 SND_SOC_DAIFMT_CBS_CFS,
153};
154
155static struct snd_soc_card snd_soc_tegra_rt5640 = {
156 .name = "tegra-rt5640",
157 .owner = THIS_MODULE,
fb6b8e71 158 .remove = tegra_rt5640_card_remove,
040a62cf
SW
159 .dai_link = &tegra_rt5640_dai,
160 .num_links = 1,
161 .controls = tegra_rt5640_controls,
162 .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
163 .dapm_widgets = tegra_rt5640_dapm_widgets,
164 .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
165 .fully_routed = true,
166};
167
168static int tegra_rt5640_probe(struct platform_device *pdev)
169{
170 struct device_node *np = pdev->dev.of_node;
171 struct snd_soc_card *card = &snd_soc_tegra_rt5640;
172 struct tegra_rt5640 *machine;
173 int ret;
174
175 machine = devm_kzalloc(&pdev->dev,
176 sizeof(struct tegra_rt5640), GFP_KERNEL);
177 if (!machine) {
178 dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
179 return -ENOMEM;
180 }
181
182 card->dev = &pdev->dev;
183 platform_set_drvdata(pdev, card);
184 snd_soc_card_set_drvdata(card, machine);
185
469cda29
AB
186 machine->gpio_hp_det = of_get_named_gpio_flags(
187 np, "nvidia,hp-det-gpios", 0, &machine->gpio_hp_det_flags);
040a62cf
SW
188 if (machine->gpio_hp_det == -EPROBE_DEFER)
189 return -EPROBE_DEFER;
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_rt5640_dai.codec_of_node = of_parse_phandle(np,
200 "nvidia,audio-codec", 0);
201 if (!tegra_rt5640_dai.codec_of_node) {
202 dev_err(&pdev->dev,
203 "Property 'nvidia,audio-codec' missing or invalid\n");
204 ret = -EINVAL;
205 goto err;
206 }
207
208 tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
209 "nvidia,i2s-controller", 0);
210 if (!tegra_rt5640_dai.cpu_of_node) {
211 dev_err(&pdev->dev,
212 "Property 'nvidia,i2s-controller' missing or invalid\n");
213 ret = -EINVAL;
214 goto err;
215 }
216
217 tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
218
219 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
220 if (ret)
221 goto err;
222
223 ret = snd_soc_register_card(card);
224 if (ret) {
225 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
226 ret);
227 goto err_fini_utils;
228 }
229
230 return 0;
231
232err_fini_utils:
233 tegra_asoc_utils_fini(&machine->util_data);
234err:
235 return ret;
236}
237
238static int tegra_rt5640_remove(struct platform_device *pdev)
239{
240 struct snd_soc_card *card = platform_get_drvdata(pdev);
241 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
242
040a62cf
SW
243 snd_soc_unregister_card(card);
244
245 tegra_asoc_utils_fini(&machine->util_data);
246
247 return 0;
248}
249
250static const struct of_device_id tegra_rt5640_of_match[] = {
251 { .compatible = "nvidia,tegra-audio-rt5640", },
252 {},
253};
254
255static struct platform_driver tegra_rt5640_driver = {
256 .driver = {
257 .name = DRV_NAME,
040a62cf
SW
258 .pm = &snd_soc_pm_ops,
259 .of_match_table = tegra_rt5640_of_match,
260 },
261 .probe = tegra_rt5640_probe,
262 .remove = tegra_rt5640_remove,
263};
264module_platform_driver(tegra_rt5640_driver);
265
266MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
267MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
268MODULE_LICENSE("GPL v2");
269MODULE_ALIAS("platform:" DRV_NAME);
270MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);
This page took 0.087473 seconds and 5 git commands to generate.