]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - sound/soc/mxs/mxs-sgtl5000.c
Merge remote-tracking branches 'asoc/topic/mtk', 'asoc/topic/mxs', 'asoc/topic/mxs...
[mirror_ubuntu-disco-kernel.git] / sound / soc / mxs / mxs-sgtl5000.c
CommitLineData
fcb5e47e
DA
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>
e968194b
SG
21#include <linux/of.h>
22#include <linux/of_device.h>
fcb5e47e
DA
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>
fcb5e47e
DA
28
29#include "../codecs/sgtl5000.h"
30#include "mxs-saif.h"
31
32static 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);
bc6a5d64 39 u32 mclk;
fcb5e47e
DA
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
fcb5e47e
DA
52 /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
53 ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
d66a5b9c
LW
54 if (ret) {
55 dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
56 mclk / 1000000, mclk / 1000 % 1000);
fcb5e47e 57 return ret;
d66a5b9c 58 }
fcb5e47e
DA
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);
d66a5b9c
LW
62 if (ret) {
63 dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
64 mclk / 1000000, mclk / 1000 % 1000);
fcb5e47e 65 return ret;
d66a5b9c 66 }
fcb5e47e 67
fcb5e47e
DA
68 return 0;
69}
70
9b6fdef6 71static const struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
fcb5e47e
DA
72 .hw_params = mxs_sgtl5000_hw_params,
73};
74
bc6a5d64
LPC
75#define MXS_SGTL5000_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
76 SND_SOC_DAIFMT_CBS_CFS)
77
fcb5e47e
DA
78static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
79 {
78a262c8 80 .name = "HiFi Tx",
fcb5e47e
DA
81 .stream_name = "HiFi Playback",
82 .codec_dai_name = "sgtl5000",
bc6a5d64 83 .dai_fmt = MXS_SGTL5000_DAI_FMT,
fcb5e47e 84 .ops = &mxs_sgtl5000_hifi_ops,
a90e6053 85 .playback_only = true,
78a262c8
DA
86 }, {
87 .name = "HiFi Rx",
88 .stream_name = "HiFi Capture",
89 .codec_dai_name = "sgtl5000",
bc6a5d64 90 .dai_fmt = MXS_SGTL5000_DAI_FMT,
78a262c8 91 .ops = &mxs_sgtl5000_hifi_ops,
a90e6053 92 .capture_only = true,
fcb5e47e
DA
93 },
94};
95
949293d4
CF
96static const struct snd_soc_dapm_widget mxs_sgtl5000_dapm_widgets[] = {
97 SND_SOC_DAPM_MIC("Mic Jack", NULL),
98 SND_SOC_DAPM_LINE("Line In Jack", NULL),
99 SND_SOC_DAPM_HP("Headphone Jack", NULL),
100 SND_SOC_DAPM_SPK("Line Out Jack", NULL),
101 SND_SOC_DAPM_SPK("Ext Spk", NULL),
102};
103
fcb5e47e
DA
104static struct snd_soc_card mxs_sgtl5000 = {
105 .name = "mxs_sgtl5000",
a5b68348 106 .owner = THIS_MODULE,
fcb5e47e
DA
107 .dai_link = mxs_sgtl5000_dai,
108 .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
109};
110
38f2b8cb 111static int mxs_sgtl5000_probe(struct platform_device *pdev)
e968194b 112{
38f2b8cb
FE
113 struct snd_soc_card *card = &mxs_sgtl5000;
114 int ret, i;
e968194b
SG
115 struct device_node *np = pdev->dev.of_node;
116 struct device_node *saif_np[2], *codec_np;
e968194b
SG
117
118 saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
119 saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
120 codec_np = of_parse_phandle(np, "audio-codec", 0);
121 if (!saif_np[0] || !saif_np[1] || !codec_np) {
122 dev_err(&pdev->dev, "phandle missing or invalid\n");
123 return -EINVAL;
124 }
125
126 for (i = 0; i < 2; i++) {
127 mxs_sgtl5000_dai[i].codec_name = NULL;
128 mxs_sgtl5000_dai[i].codec_of_node = codec_np;
129 mxs_sgtl5000_dai[i].cpu_dai_name = NULL;
bc92657a 130 mxs_sgtl5000_dai[i].cpu_of_node = saif_np[i];
e968194b
SG
131 mxs_sgtl5000_dai[i].platform_name = NULL;
132 mxs_sgtl5000_dai[i].platform_of_node = saif_np[i];
133 }
134
135 of_node_put(codec_np);
136 of_node_put(saif_np[0]);
137 of_node_put(saif_np[1]);
138
fcb5e47e
DA
139 /*
140 * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
141 * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
142 * should be >= 8MHz and <= 27M.
143 */
144 ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
d66a5b9c
LW
145 if (ret) {
146 dev_err(&pdev->dev, "failed to get mclk\n");
fcb5e47e 147 return ret;
d66a5b9c 148 }
fcb5e47e
DA
149
150 card->dev = &pdev->dev;
fcb5e47e 151
949293d4
CF
152 if (of_find_property(np, "audio-routing", NULL)) {
153 card->dapm_widgets = mxs_sgtl5000_dapm_widgets;
154 card->num_dapm_widgets = ARRAY_SIZE(mxs_sgtl5000_dapm_widgets);
155
156 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
157 if (ret) {
158 dev_err(&pdev->dev, "failed to parse audio-routing (%d)\n",
159 ret);
160 return ret;
161 }
162 }
163
5b73de19 164 ret = devm_snd_soc_register_card(&pdev->dev, card);
fcb5e47e 165 if (ret) {
d04c413f
FE
166 if (ret != -EPROBE_DEFER)
167 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
168 ret);
fcb5e47e
DA
169 return ret;
170 }
171
172 return 0;
173}
174
fd582736 175static int mxs_sgtl5000_remove(struct platform_device *pdev)
fcb5e47e 176{
fcb5e47e
DA
177 mxs_saif_put_mclk(0);
178
fcb5e47e
DA
179 return 0;
180}
181
e968194b
SG
182static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
183 { .compatible = "fsl,mxs-audio-sgtl5000", },
184 { /* sentinel */ }
185};
186MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
187
fcb5e47e
DA
188static struct platform_driver mxs_sgtl5000_audio_driver = {
189 .driver = {
190 .name = "mxs-sgtl5000",
e968194b 191 .of_match_table = mxs_sgtl5000_dt_ids,
fcb5e47e
DA
192 },
193 .probe = mxs_sgtl5000_probe,
fd582736 194 .remove = mxs_sgtl5000_remove,
fcb5e47e
DA
195};
196
85aa0960 197module_platform_driver(mxs_sgtl5000_audio_driver);
fcb5e47e
DA
198
199MODULE_AUTHOR("Freescale Semiconductor, Inc.");
200MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
201MODULE_LICENSE("GPL");
9fd369b1 202MODULE_ALIAS("platform:mxs-sgtl5000");