]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/tegra/tegra_max98090.c
8df71a436f11998e40dd62f2c0c32594f378cc16
[karo-tx-linux.git] / sound / soc / tegra / tegra_max98090.c
1 /*
2  * Tegra machine ASoC driver for boards using a MAX90809 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 "tegra_asoc_utils.h"
39
40 #define DRV_NAME "tegra-snd-max98090"
41
42 struct tegra_max98090 {
43         struct tegra_asoc_utils_data util_data;
44         int gpio_hp_det;
45         int gpio_mic_det;
46 };
47
48 static int tegra_max98090_asoc_hw_params(struct snd_pcm_substream *substream,
49                                         struct snd_pcm_hw_params *params)
50 {
51         struct snd_soc_pcm_runtime *rtd = substream->private_data;
52         struct snd_soc_dai *codec_dai = rtd->codec_dai;
53         struct snd_soc_card *card = rtd->card;
54         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
55         int srate, mclk;
56         int err;
57
58         srate = params_rate(params);
59         switch (srate) {
60         case 8000:
61         case 16000:
62         case 24000:
63         case 32000:
64         case 48000:
65         case 64000:
66         case 96000:
67                 mclk = 12288000;
68                 break;
69         case 11025:
70         case 22050:
71         case 44100:
72         case 88200:
73                 mclk = 11289600;
74                 break;
75         default:
76                 mclk = 12000000;
77                 break;
78         }
79
80         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
81         if (err < 0) {
82                 dev_err(card->dev, "Can't configure clocks\n");
83                 return err;
84         }
85
86         err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
87                                         SND_SOC_CLOCK_IN);
88         if (err < 0) {
89                 dev_err(card->dev, "codec_dai clock not set\n");
90                 return err;
91         }
92
93         return 0;
94 }
95
96 static struct snd_soc_ops tegra_max98090_ops = {
97         .hw_params = tegra_max98090_asoc_hw_params,
98 };
99
100 static struct snd_soc_jack tegra_max98090_hp_jack;
101
102 static struct snd_soc_jack_pin tegra_max98090_hp_jack_pins[] = {
103         {
104                 .pin = "Headphones",
105                 .mask = SND_JACK_HEADPHONE,
106         },
107 };
108
109 static struct snd_soc_jack_gpio tegra_max98090_hp_jack_gpio = {
110         .name = "Headphone detection",
111         .report = SND_JACK_HEADPHONE,
112         .debounce_time = 150,
113         .invert = 1,
114 };
115
116 static struct snd_soc_jack tegra_max98090_mic_jack;
117
118 static struct snd_soc_jack_pin tegra_max98090_mic_jack_pins[] = {
119         {
120                 .pin = "Mic Jack",
121                 .mask = SND_JACK_MICROPHONE,
122         },
123 };
124
125 static struct snd_soc_jack_gpio tegra_max98090_mic_jack_gpio = {
126         .name = "Mic detection",
127         .report = SND_JACK_MICROPHONE,
128         .debounce_time = 150,
129         .invert = 1,
130 };
131
132 static const struct snd_soc_dapm_widget tegra_max98090_dapm_widgets[] = {
133         SND_SOC_DAPM_HP("Headphones", NULL),
134         SND_SOC_DAPM_SPK("Speakers", NULL),
135         SND_SOC_DAPM_MIC("Mic Jack", NULL),
136 };
137
138 static const struct snd_kcontrol_new tegra_max98090_controls[] = {
139         SOC_DAPM_PIN_SWITCH("Headphones"),
140         SOC_DAPM_PIN_SWITCH("Speakers"),
141 };
142
143 static int tegra_max98090_asoc_init(struct snd_soc_pcm_runtime *rtd)
144 {
145         struct snd_soc_dai *codec_dai = rtd->codec_dai;
146         struct snd_soc_codec *codec = codec_dai->codec;
147         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(rtd->card);
148
149         if (gpio_is_valid(machine->gpio_hp_det)) {
150                 snd_soc_jack_new(codec, "Headphones", SND_JACK_HEADPHONE,
151                                 &tegra_max98090_hp_jack);
152                 snd_soc_jack_add_pins(&tegra_max98090_hp_jack,
153                                 ARRAY_SIZE(tegra_max98090_hp_jack_pins),
154                                 tegra_max98090_hp_jack_pins);
155
156                 tegra_max98090_hp_jack_gpio.gpio = machine->gpio_hp_det;
157                 snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
158                                         1,
159                                         &tegra_max98090_hp_jack_gpio);
160         }
161
162         if (gpio_is_valid(machine->gpio_mic_det)) {
163                 snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
164                                  &tegra_max98090_mic_jack);
165                 snd_soc_jack_add_pins(&tegra_max98090_mic_jack,
166                                       ARRAY_SIZE(tegra_max98090_mic_jack_pins),
167                                       tegra_max98090_mic_jack_pins);
168
169                 tegra_max98090_mic_jack_gpio.gpio = machine->gpio_mic_det;
170                 snd_soc_jack_add_gpios(&tegra_max98090_mic_jack,
171                                        1,
172                                        &tegra_max98090_mic_jack_gpio);
173         }
174
175         return 0;
176 }
177
178 static int tegra_max98090_card_remove(struct snd_soc_card *card)
179 {
180         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
181
182         if (gpio_is_valid(machine->gpio_hp_det)) {
183                 snd_soc_jack_free_gpios(&tegra_max98090_hp_jack, 1,
184                                         &tegra_max98090_hp_jack_gpio);
185         }
186
187         if (gpio_is_valid(machine->gpio_mic_det)) {
188                 snd_soc_jack_free_gpios(&tegra_max98090_mic_jack, 1,
189                                         &tegra_max98090_mic_jack_gpio);
190         }
191
192         return 0;
193 }
194
195 static struct snd_soc_dai_link tegra_max98090_dai = {
196         .name = "max98090",
197         .stream_name = "max98090 PCM",
198         .codec_dai_name = "HiFi",
199         .init = tegra_max98090_asoc_init,
200         .ops = &tegra_max98090_ops,
201         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
202                         SND_SOC_DAIFMT_CBS_CFS,
203 };
204
205 static struct snd_soc_card snd_soc_tegra_max98090 = {
206         .name = "tegra-max98090",
207         .owner = THIS_MODULE,
208         .remove = tegra_max98090_card_remove,
209         .dai_link = &tegra_max98090_dai,
210         .num_links = 1,
211         .controls = tegra_max98090_controls,
212         .num_controls = ARRAY_SIZE(tegra_max98090_controls),
213         .dapm_widgets = tegra_max98090_dapm_widgets,
214         .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
215         .fully_routed = true,
216 };
217
218 static int tegra_max98090_probe(struct platform_device *pdev)
219 {
220         struct device_node *np = pdev->dev.of_node;
221         struct snd_soc_card *card = &snd_soc_tegra_max98090;
222         struct tegra_max98090 *machine;
223         int ret;
224
225         machine = devm_kzalloc(&pdev->dev,
226                         sizeof(struct tegra_max98090), GFP_KERNEL);
227         if (!machine) {
228                 dev_err(&pdev->dev, "Can't allocate tegra_max98090\n");
229                 return -ENOMEM;
230         }
231
232         card->dev = &pdev->dev;
233         platform_set_drvdata(pdev, card);
234         snd_soc_card_set_drvdata(card, machine);
235
236         machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
237         if (machine->gpio_hp_det == -EPROBE_DEFER)
238                 return -EPROBE_DEFER;
239
240         machine->gpio_mic_det =
241                         of_get_named_gpio(np, "nvidia,mic-det-gpios", 0);
242         if (machine->gpio_mic_det == -EPROBE_DEFER)
243                 return -EPROBE_DEFER;
244
245         ret = snd_soc_of_parse_card_name(card, "nvidia,model");
246         if (ret)
247                 goto err;
248
249         ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
250         if (ret)
251                 goto err;
252
253         tegra_max98090_dai.codec_of_node = of_parse_phandle(np,
254                         "nvidia,audio-codec", 0);
255         if (!tegra_max98090_dai.codec_of_node) {
256                 dev_err(&pdev->dev,
257                         "Property 'nvidia,audio-codec' missing or invalid\n");
258                 ret = -EINVAL;
259                 goto err;
260         }
261
262         tegra_max98090_dai.cpu_of_node = of_parse_phandle(np,
263                         "nvidia,i2s-controller", 0);
264         if (!tegra_max98090_dai.cpu_of_node) {
265                 dev_err(&pdev->dev,
266                         "Property 'nvidia,i2s-controller' missing or invalid\n");
267                 ret = -EINVAL;
268                 goto err;
269         }
270
271         tegra_max98090_dai.platform_of_node = tegra_max98090_dai.cpu_of_node;
272
273         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
274         if (ret)
275                 goto err;
276
277         ret = snd_soc_register_card(card);
278         if (ret) {
279                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
280                         ret);
281                 goto err_fini_utils;
282         }
283
284         return 0;
285
286 err_fini_utils:
287         tegra_asoc_utils_fini(&machine->util_data);
288 err:
289         return ret;
290 }
291
292 static int tegra_max98090_remove(struct platform_device *pdev)
293 {
294         struct snd_soc_card *card = platform_get_drvdata(pdev);
295         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
296
297         snd_soc_unregister_card(card);
298
299         tegra_asoc_utils_fini(&machine->util_data);
300
301         return 0;
302 }
303
304 static const struct of_device_id tegra_max98090_of_match[] = {
305         { .compatible = "nvidia,tegra-audio-max98090", },
306         {},
307 };
308
309 static struct platform_driver tegra_max98090_driver = {
310         .driver = {
311                 .name = DRV_NAME,
312                 .pm = &snd_soc_pm_ops,
313                 .of_match_table = tegra_max98090_of_match,
314         },
315         .probe = tegra_max98090_probe,
316         .remove = tegra_max98090_remove,
317 };
318 module_platform_driver(tegra_max98090_driver);
319
320 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
321 MODULE_DESCRIPTION("Tegra max98090 machine ASoC driver");
322 MODULE_LICENSE("GPL v2");
323 MODULE_ALIAS("platform:" DRV_NAME);
324 MODULE_DEVICE_TABLE(of, tegra_max98090_of_match);