Merge branch 'for-3.5-take-2' of git://linux-nfs.org/~bfields/linux
[cascardo/linux.git] / sound / soc / tegra / trimslice.c
1 /*
2  * trimslice.c - TrimSlice machine ASoC driver
3  *
4  * Copyright (C) 2011 - CompuLab, Ltd.
5  * Author: Mike Rapoport <mike@compulab.co.il>
6  *
7  * Based on code copyright/by:
8  * Author: Stephen Warren <swarren@nvidia.com>
9  * Copyright (C) 2010-2011 - NVIDIA, Inc.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <asm/mach-types.h>
28
29 #include <linux/module.h>
30 #include <linux/of.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33
34 #include <sound/core.h>
35 #include <sound/jack.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39
40 #include "../codecs/tlv320aic23.h"
41
42 #include "tegra_asoc_utils.h"
43
44 #define DRV_NAME "tegra-snd-trimslice"
45
46 struct tegra_trimslice {
47         struct tegra_asoc_utils_data util_data;
48 };
49
50 static int trimslice_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;
55         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
56         struct snd_soc_codec *codec = rtd->codec;
57         struct snd_soc_card *card = codec->card;
58         struct tegra_trimslice *trimslice = snd_soc_card_get_drvdata(card);
59         int srate, mclk;
60         int err;
61
62         srate = params_rate(params);
63         mclk = 128 * srate;
64
65         err = tegra_asoc_utils_set_rate(&trimslice->util_data, srate, mclk);
66         if (err < 0) {
67                 dev_err(card->dev, "Can't configure clocks\n");
68                 return err;
69         }
70
71         err = snd_soc_dai_set_fmt(codec_dai,
72                                         SND_SOC_DAIFMT_I2S |
73                                         SND_SOC_DAIFMT_NB_NF |
74                                         SND_SOC_DAIFMT_CBS_CFS);
75         if (err < 0) {
76                 dev_err(card->dev, "codec_dai fmt not set\n");
77                 return err;
78         }
79
80         err = snd_soc_dai_set_fmt(cpu_dai,
81                                         SND_SOC_DAIFMT_I2S |
82                                         SND_SOC_DAIFMT_NB_NF |
83                                         SND_SOC_DAIFMT_CBS_CFS);
84         if (err < 0) {
85                 dev_err(card->dev, "cpu_dai fmt not set\n");
86                 return err;
87         }
88
89         err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
90                                         SND_SOC_CLOCK_IN);
91         if (err < 0) {
92                 dev_err(card->dev, "codec_dai clock not set\n");
93                 return err;
94         }
95
96         return 0;
97 }
98
99 static struct snd_soc_ops trimslice_asoc_ops = {
100         .hw_params = trimslice_asoc_hw_params,
101 };
102
103 static const struct snd_soc_dapm_widget trimslice_dapm_widgets[] = {
104         SND_SOC_DAPM_HP("Line Out", NULL),
105         SND_SOC_DAPM_LINE("Line In", NULL),
106 };
107
108 static const struct snd_soc_dapm_route trimslice_audio_map[] = {
109         {"Line Out", NULL, "LOUT"},
110         {"Line Out", NULL, "ROUT"},
111
112         {"LLINEIN", NULL, "Line In"},
113         {"RLINEIN", NULL, "Line In"},
114 };
115
116 static struct snd_soc_dai_link trimslice_tlv320aic23_dai = {
117         .name = "TLV320AIC23",
118         .stream_name = "AIC23",
119         .codec_name = "tlv320aic23-codec.2-001a",
120         .platform_name = "tegra20-i2s.0",
121         .cpu_dai_name = "tegra20-i2s.0",
122         .codec_dai_name = "tlv320aic23-hifi",
123         .ops = &trimslice_asoc_ops,
124 };
125
126 static struct snd_soc_card snd_soc_trimslice = {
127         .name = "tegra-trimslice",
128         .owner = THIS_MODULE,
129         .dai_link = &trimslice_tlv320aic23_dai,
130         .num_links = 1,
131
132         .dapm_widgets = trimslice_dapm_widgets,
133         .num_dapm_widgets = ARRAY_SIZE(trimslice_dapm_widgets),
134         .dapm_routes = trimslice_audio_map,
135         .num_dapm_routes = ARRAY_SIZE(trimslice_audio_map),
136         .fully_routed = true,
137 };
138
139 static __devinit int tegra_snd_trimslice_probe(struct platform_device *pdev)
140 {
141         struct snd_soc_card *card = &snd_soc_trimslice;
142         struct tegra_trimslice *trimslice;
143         int ret;
144
145         trimslice = devm_kzalloc(&pdev->dev, sizeof(struct tegra_trimslice),
146                                  GFP_KERNEL);
147         if (!trimslice) {
148                 dev_err(&pdev->dev, "Can't allocate tegra_trimslice\n");
149                 ret = -ENOMEM;
150                 goto err;
151         }
152
153         if (pdev->dev.of_node) {
154                 trimslice_tlv320aic23_dai.codec_name = NULL;
155                 trimslice_tlv320aic23_dai.codec_of_node = of_parse_phandle(
156                                 pdev->dev.of_node, "nvidia,audio-codec", 0);
157                 if (!trimslice_tlv320aic23_dai.codec_of_node) {
158                         dev_err(&pdev->dev,
159                                 "Property 'nvidia,audio-codec' missing or invalid\n");
160                         ret = -EINVAL;
161                         goto err;
162                 }
163
164                 trimslice_tlv320aic23_dai.cpu_dai_name = NULL;
165                 trimslice_tlv320aic23_dai.cpu_dai_of_node = of_parse_phandle(
166                                 pdev->dev.of_node, "nvidia,i2s-controller", 0);
167                 if (!trimslice_tlv320aic23_dai.cpu_dai_of_node) {
168                         dev_err(&pdev->dev,
169                                 "Property 'nvidia,i2s-controller' missing or invalid\n");
170                         ret = -EINVAL;
171                         goto err;
172                 }
173
174                 trimslice_tlv320aic23_dai.platform_name = NULL;
175                 trimslice_tlv320aic23_dai.platform_of_node =
176                                 trimslice_tlv320aic23_dai.cpu_dai_of_node;
177         }
178
179         ret = tegra_asoc_utils_init(&trimslice->util_data, &pdev->dev);
180         if (ret)
181                 goto err;
182
183         card->dev = &pdev->dev;
184         platform_set_drvdata(pdev, card);
185         snd_soc_card_set_drvdata(card, trimslice);
186
187         ret = snd_soc_register_card(card);
188         if (ret) {
189                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
190                         ret);
191                 goto err_fini_utils;
192         }
193
194         return 0;
195
196 err_fini_utils:
197         tegra_asoc_utils_fini(&trimslice->util_data);
198 err:
199         return ret;
200 }
201
202 static int __devexit tegra_snd_trimslice_remove(struct platform_device *pdev)
203 {
204         struct snd_soc_card *card = platform_get_drvdata(pdev);
205         struct tegra_trimslice *trimslice = snd_soc_card_get_drvdata(card);
206
207         snd_soc_unregister_card(card);
208
209         tegra_asoc_utils_fini(&trimslice->util_data);
210
211         return 0;
212 }
213
214 static const struct of_device_id trimslice_of_match[] __devinitconst = {
215         { .compatible = "nvidia,tegra-audio-trimslice", },
216         {},
217 };
218 MODULE_DEVICE_TABLE(of, trimslice_of_match);
219
220 static struct platform_driver tegra_snd_trimslice_driver = {
221         .driver = {
222                 .name = DRV_NAME,
223                 .owner = THIS_MODULE,
224                 .of_match_table = trimslice_of_match,
225         },
226         .probe = tegra_snd_trimslice_probe,
227         .remove = __devexit_p(tegra_snd_trimslice_remove),
228 };
229 module_platform_driver(tegra_snd_trimslice_driver);
230
231 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
232 MODULE_DESCRIPTION("Trimslice machine ASoC driver");
233 MODULE_LICENSE("GPL");
234 MODULE_ALIAS("platform:" DRV_NAME);