Linux 4.16.11
[linux/fpc-iii.git] / sound / soc / fsl / imx-wm8962.c
blob206b898e554ce4530013a72ac78e107b06c3a95e
1 /*
2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * Based on imx-sgtl5000.c
5 * Copyright 2012 Freescale Semiconductor, Inc.
6 * Copyright 2012 Linaro Ltd.
8 * The code contained herein is licensed under the GNU General Public
9 * License. You may obtain a copy of the GNU General Public License
10 * Version 2 or later at the following locations:
12 * http://www.opensource.org/licenses/gpl-license.html
13 * http://www.gnu.org/copyleft/gpl.html
16 #include <linux/module.h>
17 #include <linux/of_platform.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/clk.h>
21 #include <sound/soc.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc-dapm.h>
24 #include <linux/pinctrl/consumer.h>
26 #include "../codecs/wm8962.h"
27 #include "imx-audmux.h"
29 #define DAI_NAME_SIZE 32
31 struct imx_wm8962_data {
32 struct snd_soc_dai_link dai;
33 struct snd_soc_card card;
34 char codec_dai_name[DAI_NAME_SIZE];
35 char platform_name[DAI_NAME_SIZE];
36 unsigned int clk_frequency;
39 struct imx_priv {
40 struct platform_device *pdev;
41 int sample_rate;
42 snd_pcm_format_t sample_format;
45 static const struct snd_soc_dapm_widget imx_wm8962_dapm_widgets[] = {
46 SND_SOC_DAPM_HP("Headphone Jack", NULL),
47 SND_SOC_DAPM_SPK("Ext Spk", NULL),
48 SND_SOC_DAPM_MIC("AMIC", NULL),
49 SND_SOC_DAPM_MIC("DMIC", NULL),
52 static int imx_hifi_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params)
55 struct snd_soc_pcm_runtime *rtd = substream->private_data;
56 struct imx_priv *priv = snd_soc_card_get_drvdata(rtd->card);
58 priv->sample_rate = params_rate(params);
59 priv->sample_format = params_format(params);
61 return 0;
64 static const struct snd_soc_ops imx_hifi_ops = {
65 .hw_params = imx_hifi_hw_params,
68 static int imx_wm8962_set_bias_level(struct snd_soc_card *card,
69 struct snd_soc_dapm_context *dapm,
70 enum snd_soc_bias_level level)
72 struct snd_soc_pcm_runtime *rtd;
73 struct snd_soc_dai *codec_dai;
74 struct imx_priv *priv = snd_soc_card_get_drvdata(card);
75 struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
76 struct device *dev = &priv->pdev->dev;
77 unsigned int pll_out;
78 int ret;
80 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
81 codec_dai = rtd->codec_dai;
82 if (dapm->dev != codec_dai->dev)
83 return 0;
85 switch (level) {
86 case SND_SOC_BIAS_PREPARE:
87 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
88 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
89 pll_out = priv->sample_rate * 384;
90 else
91 pll_out = priv->sample_rate * 256;
93 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
94 WM8962_FLL_MCLK, data->clk_frequency,
95 pll_out);
96 if (ret < 0) {
97 dev_err(dev, "failed to start FLL: %d\n", ret);
98 return ret;
101 ret = snd_soc_dai_set_sysclk(codec_dai,
102 WM8962_SYSCLK_FLL, pll_out,
103 SND_SOC_CLOCK_IN);
104 if (ret < 0) {
105 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
106 return ret;
109 break;
111 case SND_SOC_BIAS_STANDBY:
112 if (dapm->bias_level == SND_SOC_BIAS_PREPARE) {
113 ret = snd_soc_dai_set_sysclk(codec_dai,
114 WM8962_SYSCLK_MCLK, data->clk_frequency,
115 SND_SOC_CLOCK_IN);
116 if (ret < 0) {
117 dev_err(dev,
118 "failed to switch away from FLL: %d\n",
119 ret);
120 return ret;
123 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
124 0, 0, 0);
125 if (ret < 0) {
126 dev_err(dev, "failed to stop FLL: %d\n", ret);
127 return ret;
130 break;
132 default:
133 break;
136 return 0;
139 static int imx_wm8962_late_probe(struct snd_soc_card *card)
141 struct snd_soc_pcm_runtime *rtd;
142 struct snd_soc_dai *codec_dai;
143 struct imx_priv *priv = snd_soc_card_get_drvdata(card);
144 struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
145 struct device *dev = &priv->pdev->dev;
146 int ret;
148 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
149 codec_dai = rtd->codec_dai;
150 ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
151 data->clk_frequency, SND_SOC_CLOCK_IN);
152 if (ret < 0)
153 dev_err(dev, "failed to set sysclk in %s\n", __func__);
155 return ret;
158 static int imx_wm8962_probe(struct platform_device *pdev)
160 struct device_node *np = pdev->dev.of_node;
161 struct device_node *ssi_np, *codec_np;
162 struct platform_device *ssi_pdev;
163 struct i2c_client *codec_dev;
164 struct imx_wm8962_data *data;
165 struct imx_priv *priv;
166 struct clk *codec_clk;
167 int int_port, ext_port;
168 int ret;
170 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
171 if (!priv)
172 return -ENOMEM;
174 priv->pdev = pdev;
175 priv->sample_rate = 44100;
176 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
178 ret = of_property_read_u32(np, "mux-int-port", &int_port);
179 if (ret) {
180 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
181 return ret;
183 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
184 if (ret) {
185 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
186 return ret;
190 * The port numbering in the hardware manual starts at 1, while
191 * the audmux API expects it starts at 0.
193 int_port--;
194 ext_port--;
195 ret = imx_audmux_v2_configure_port(int_port,
196 IMX_AUDMUX_V2_PTCR_SYN |
197 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
198 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
199 IMX_AUDMUX_V2_PTCR_TFSDIR |
200 IMX_AUDMUX_V2_PTCR_TCLKDIR,
201 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
202 if (ret) {
203 dev_err(&pdev->dev, "audmux internal port setup failed\n");
204 return ret;
206 ret = imx_audmux_v2_configure_port(ext_port,
207 IMX_AUDMUX_V2_PTCR_SYN,
208 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
209 if (ret) {
210 dev_err(&pdev->dev, "audmux external port setup failed\n");
211 return ret;
214 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
215 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
216 if (!ssi_np || !codec_np) {
217 dev_err(&pdev->dev, "phandle missing or invalid\n");
218 ret = -EINVAL;
219 goto fail;
222 ssi_pdev = of_find_device_by_node(ssi_np);
223 if (!ssi_pdev) {
224 dev_err(&pdev->dev, "failed to find SSI platform device\n");
225 ret = -EINVAL;
226 goto fail;
228 codec_dev = of_find_i2c_device_by_node(codec_np);
229 if (!codec_dev || !codec_dev->dev.driver) {
230 dev_err(&pdev->dev, "failed to find codec platform device\n");
231 ret = -EINVAL;
232 goto fail;
235 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
236 if (!data) {
237 ret = -ENOMEM;
238 goto fail;
241 codec_clk = clk_get(&codec_dev->dev, NULL);
242 if (IS_ERR(codec_clk)) {
243 ret = PTR_ERR(codec_clk);
244 dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
245 goto fail;
248 data->clk_frequency = clk_get_rate(codec_clk);
249 clk_put(codec_clk);
251 data->dai.name = "HiFi";
252 data->dai.stream_name = "HiFi";
253 data->dai.codec_dai_name = "wm8962";
254 data->dai.codec_of_node = codec_np;
255 data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
256 data->dai.platform_of_node = ssi_np;
257 data->dai.ops = &imx_hifi_ops;
258 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
259 SND_SOC_DAIFMT_CBM_CFM;
261 data->card.dev = &pdev->dev;
262 ret = snd_soc_of_parse_card_name(&data->card, "model");
263 if (ret)
264 goto fail;
265 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
266 if (ret)
267 goto fail;
268 data->card.num_links = 1;
269 data->card.owner = THIS_MODULE;
270 data->card.dai_link = &data->dai;
271 data->card.dapm_widgets = imx_wm8962_dapm_widgets;
272 data->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8962_dapm_widgets);
274 data->card.late_probe = imx_wm8962_late_probe;
275 data->card.set_bias_level = imx_wm8962_set_bias_level;
277 platform_set_drvdata(pdev, &data->card);
278 snd_soc_card_set_drvdata(&data->card, data);
280 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
281 if (ret) {
282 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
283 goto fail;
286 fail:
287 of_node_put(ssi_np);
288 of_node_put(codec_np);
290 return ret;
293 static const struct of_device_id imx_wm8962_dt_ids[] = {
294 { .compatible = "fsl,imx-audio-wm8962", },
295 { /* sentinel */ }
297 MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
299 static struct platform_driver imx_wm8962_driver = {
300 .driver = {
301 .name = "imx-wm8962",
302 .pm = &snd_soc_pm_ops,
303 .of_match_table = imx_wm8962_dt_ids,
305 .probe = imx_wm8962_probe,
307 module_platform_driver(imx_wm8962_driver);
309 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
310 MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
311 MODULE_LICENSE("GPL v2");
312 MODULE_ALIAS("platform:imx-wm8962");