Merge tag 'driver-core-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / sound / soc / mxs / mxs-sgtl5000.c
1 /*
2  * Copyright 2011 Freescale Semiconductor, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/soc.h>
26 #include <sound/jack.h>
27 #include <sound/soc-dapm.h>
28
29 #include "../codecs/sgtl5000.h"
30 #include "mxs-saif.h"
31
32 static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
33         struct snd_pcm_hw_params *params)
34 {
35         struct snd_soc_pcm_runtime *rtd = substream->private_data;
36         struct snd_soc_dai *codec_dai = rtd->codec_dai;
37         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
38         unsigned int rate = params_rate(params);
39         u32 dai_format, mclk;
40         int ret;
41
42         /* sgtl5000 does not support 512*rate when in 96000 fs */
43         switch (rate) {
44         case 96000:
45                 mclk = 256 * rate;
46                 break;
47         default:
48                 mclk = 512 * rate;
49                 break;
50         }
51
52         /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
53         ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
54         if (ret) {
55                 dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
56                         mclk / 1000000, mclk / 1000 % 1000);
57                 return ret;
58         }
59
60         /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
61         ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
62         if (ret) {
63                 dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
64                         mclk / 1000000, mclk / 1000 % 1000);
65                 return ret;
66         }
67
68         /* set codec to slave mode */
69         dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
70                         SND_SOC_DAIFMT_CBS_CFS;
71
72         /* set codec DAI configuration */
73         ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
74         if (ret) {
75                 dev_err(codec_dai->dev, "Failed to set dai format to %08x\n",
76                         dai_format);
77                 return ret;
78         }
79
80         /* set cpu DAI configuration */
81         ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
82         if (ret) {
83                 dev_err(cpu_dai->dev, "Failed to set dai format to %08x\n",
84                         dai_format);
85                 return ret;
86         }
87
88         return 0;
89 }
90
91 static struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
92         .hw_params = mxs_sgtl5000_hw_params,
93 };
94
95 static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
96         {
97                 .name           = "HiFi Tx",
98                 .stream_name    = "HiFi Playback",
99                 .codec_dai_name = "sgtl5000",
100                 .ops            = &mxs_sgtl5000_hifi_ops,
101                 .playback_only  = true,
102         }, {
103                 .name           = "HiFi Rx",
104                 .stream_name    = "HiFi Capture",
105                 .codec_dai_name = "sgtl5000",
106                 .ops            = &mxs_sgtl5000_hifi_ops,
107                 .capture_only   = true,
108         },
109 };
110
111 static struct snd_soc_card mxs_sgtl5000 = {
112         .name           = "mxs_sgtl5000",
113         .owner          = THIS_MODULE,
114         .dai_link       = mxs_sgtl5000_dai,
115         .num_links      = ARRAY_SIZE(mxs_sgtl5000_dai),
116 };
117
118 static int mxs_sgtl5000_probe(struct platform_device *pdev)
119 {
120         struct snd_soc_card *card = &mxs_sgtl5000;
121         int ret, i;
122         struct device_node *np = pdev->dev.of_node;
123         struct device_node *saif_np[2], *codec_np;
124
125         saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
126         saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
127         codec_np = of_parse_phandle(np, "audio-codec", 0);
128         if (!saif_np[0] || !saif_np[1] || !codec_np) {
129                 dev_err(&pdev->dev, "phandle missing or invalid\n");
130                 return -EINVAL;
131         }
132
133         for (i = 0; i < 2; i++) {
134                 mxs_sgtl5000_dai[i].codec_name = NULL;
135                 mxs_sgtl5000_dai[i].codec_of_node = codec_np;
136                 mxs_sgtl5000_dai[i].cpu_dai_name = NULL;
137                 mxs_sgtl5000_dai[i].cpu_of_node = saif_np[i];
138                 mxs_sgtl5000_dai[i].platform_name = NULL;
139                 mxs_sgtl5000_dai[i].platform_of_node = saif_np[i];
140         }
141
142         of_node_put(codec_np);
143         of_node_put(saif_np[0]);
144         of_node_put(saif_np[1]);
145
146         /*
147          * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
148          * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
149          * should be >= 8MHz and <= 27M.
150          */
151         ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
152         if (ret) {
153                 dev_err(&pdev->dev, "failed to get mclk\n");
154                 return ret;
155         }
156
157         card->dev = &pdev->dev;
158         platform_set_drvdata(pdev, card);
159
160         ret = snd_soc_register_card(card);
161         if (ret) {
162                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
163                         ret);
164                 return ret;
165         }
166
167         return 0;
168 }
169
170 static int mxs_sgtl5000_remove(struct platform_device *pdev)
171 {
172         struct snd_soc_card *card = platform_get_drvdata(pdev);
173
174         mxs_saif_put_mclk(0);
175
176         snd_soc_unregister_card(card);
177
178         return 0;
179 }
180
181 static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
182         { .compatible = "fsl,mxs-audio-sgtl5000", },
183         { /* sentinel */ }
184 };
185 MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
186
187 static struct platform_driver mxs_sgtl5000_audio_driver = {
188         .driver = {
189                 .name = "mxs-sgtl5000",
190                 .of_match_table = mxs_sgtl5000_dt_ids,
191         },
192         .probe = mxs_sgtl5000_probe,
193         .remove = mxs_sgtl5000_remove,
194 };
195
196 module_platform_driver(mxs_sgtl5000_audio_driver);
197
198 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
199 MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
200 MODULE_LICENSE("GPL");
201 MODULE_ALIAS("platform:mxs-sgtl5000");