Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / sound / soc / tegra / tegra_rt5640.c
blob4feb16a99e02d28a10ba7b12149a8d73b6404266
1 /*
2 * tegra_rt5640.c - Tegra machine ASoC driver for boards using WM8903 codec.
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
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.
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.
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/>.
18 * Based on code copyright/by:
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.
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>
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>
38 #include "../codecs/rt5640.h"
40 #include "tegra_asoc_utils.h"
42 #define DRV_NAME "tegra-snd-rt5640"
44 struct tegra_rt5640 {
45 struct tegra_asoc_utils_data util_data;
46 int gpio_hp_det;
49 static int tegra_rt5640_asoc_hw_params(struct snd_pcm_substream *substream,
50 struct snd_pcm_hw_params *params)
52 struct snd_soc_pcm_runtime *rtd = substream->private_data;
53 struct snd_soc_dai *codec_dai = rtd->codec_dai;
54 struct snd_soc_codec *codec = codec_dai->codec;
55 struct snd_soc_card *card = codec->card;
56 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
57 int srate, mclk;
58 int err;
60 srate = params_rate(params);
61 mclk = 256 * srate;
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;
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;
76 return 0;
79 static struct snd_soc_ops tegra_rt5640_ops = {
80 .hw_params = tegra_rt5640_asoc_hw_params,
83 static struct snd_soc_jack tegra_rt5640_hp_jack;
85 static struct snd_soc_jack_pin tegra_rt5640_hp_jack_pins[] = {
87 .pin = "Headphones",
88 .mask = SND_JACK_HEADPHONE,
92 static 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,
99 static const struct snd_soc_dapm_widget tegra_rt5640_dapm_widgets[] = {
100 SND_SOC_DAPM_HP("Headphones", NULL),
101 SND_SOC_DAPM_SPK("Speakers", NULL),
102 SND_SOC_DAPM_MIC("Mic Jack", NULL),
105 static const struct snd_kcontrol_new tegra_rt5640_controls[] = {
106 SOC_DAPM_PIN_SWITCH("Speakers"),
109 static int tegra_rt5640_asoc_init(struct snd_soc_pcm_runtime *rtd)
111 struct snd_soc_dai *codec_dai = rtd->codec_dai;
112 struct snd_soc_codec *codec = codec_dai->codec;
113 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(codec->card);
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);
121 if (gpio_is_valid(machine->gpio_hp_det)) {
122 tegra_rt5640_hp_jack_gpio.gpio = machine->gpio_hp_det;
123 snd_soc_jack_add_gpios(&tegra_rt5640_hp_jack,
125 &tegra_rt5640_hp_jack_gpio);
128 return 0;
131 static int tegra_rt5640_card_remove(struct snd_soc_card *card)
133 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
135 if (gpio_is_valid(machine->gpio_hp_det)) {
136 snd_soc_jack_free_gpios(&tegra_rt5640_hp_jack, 1,
137 &tegra_rt5640_hp_jack_gpio);
140 return 0;
143 static struct snd_soc_dai_link tegra_rt5640_dai = {
144 .name = "RT5640",
145 .stream_name = "RT5640 PCM",
146 .codec_dai_name = "rt5640-aif1",
147 .init = tegra_rt5640_asoc_init,
148 .ops = &tegra_rt5640_ops,
149 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
150 SND_SOC_DAIFMT_CBS_CFS,
153 static struct snd_soc_card snd_soc_tegra_rt5640 = {
154 .name = "tegra-rt5640",
155 .owner = THIS_MODULE,
156 .remove = tegra_rt5640_card_remove,
157 .dai_link = &tegra_rt5640_dai,
158 .num_links = 1,
159 .controls = tegra_rt5640_controls,
160 .num_controls = ARRAY_SIZE(tegra_rt5640_controls),
161 .dapm_widgets = tegra_rt5640_dapm_widgets,
162 .num_dapm_widgets = ARRAY_SIZE(tegra_rt5640_dapm_widgets),
163 .fully_routed = true,
166 static int tegra_rt5640_probe(struct platform_device *pdev)
168 struct device_node *np = pdev->dev.of_node;
169 struct snd_soc_card *card = &snd_soc_tegra_rt5640;
170 struct tegra_rt5640 *machine;
171 int ret;
173 machine = devm_kzalloc(&pdev->dev,
174 sizeof(struct tegra_rt5640), GFP_KERNEL);
175 if (!machine) {
176 dev_err(&pdev->dev, "Can't allocate tegra_rt5640\n");
177 return -ENOMEM;
180 card->dev = &pdev->dev;
181 platform_set_drvdata(pdev, card);
182 snd_soc_card_set_drvdata(card, machine);
184 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
185 if (machine->gpio_hp_det == -EPROBE_DEFER)
186 return -EPROBE_DEFER;
188 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
189 if (ret)
190 goto err;
192 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
193 if (ret)
194 goto err;
196 tegra_rt5640_dai.codec_of_node = of_parse_phandle(np,
197 "nvidia,audio-codec", 0);
198 if (!tegra_rt5640_dai.codec_of_node) {
199 dev_err(&pdev->dev,
200 "Property 'nvidia,audio-codec' missing or invalid\n");
201 ret = -EINVAL;
202 goto err;
205 tegra_rt5640_dai.cpu_of_node = of_parse_phandle(np,
206 "nvidia,i2s-controller", 0);
207 if (!tegra_rt5640_dai.cpu_of_node) {
208 dev_err(&pdev->dev,
209 "Property 'nvidia,i2s-controller' missing or invalid\n");
210 ret = -EINVAL;
211 goto err;
214 tegra_rt5640_dai.platform_of_node = tegra_rt5640_dai.cpu_of_node;
216 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
217 if (ret)
218 goto err;
220 ret = snd_soc_register_card(card);
221 if (ret) {
222 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
223 ret);
224 goto err_fini_utils;
227 return 0;
229 err_fini_utils:
230 tegra_asoc_utils_fini(&machine->util_data);
231 err:
232 return ret;
235 static int tegra_rt5640_remove(struct platform_device *pdev)
237 struct snd_soc_card *card = platform_get_drvdata(pdev);
238 struct tegra_rt5640 *machine = snd_soc_card_get_drvdata(card);
240 snd_soc_unregister_card(card);
242 tegra_asoc_utils_fini(&machine->util_data);
244 return 0;
247 static const struct of_device_id tegra_rt5640_of_match[] = {
248 { .compatible = "nvidia,tegra-audio-rt5640", },
252 static struct platform_driver tegra_rt5640_driver = {
253 .driver = {
254 .name = DRV_NAME,
255 .owner = THIS_MODULE,
256 .pm = &snd_soc_pm_ops,
257 .of_match_table = tegra_rt5640_of_match,
259 .probe = tegra_rt5640_probe,
260 .remove = tegra_rt5640_remove,
262 module_platform_driver(tegra_rt5640_driver);
264 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
265 MODULE_DESCRIPTION("Tegra+RT5640 machine ASoC driver");
266 MODULE_LICENSE("GPL v2");
267 MODULE_ALIAS("platform:" DRV_NAME);
268 MODULE_DEVICE_TABLE(of, tegra_rt5640_of_match);