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 struct clk *codec_clk;
37 unsigned int clk_frequency;
41 struct platform_device *pdev;
43 static struct imx_priv card_priv;
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 sample_rate = 44100;
53 static snd_pcm_format_t sample_format = SNDRV_PCM_FORMAT_S16_LE;
55 static int imx_hifi_hw_params(struct snd_pcm_substream *substream,
56 struct snd_pcm_hw_params *params)
58 sample_rate = params_rate(params);
59 sample_format = params_format(params);
64 static 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_dai *codec_dai = card->rtd[0].codec_dai;
73 struct imx_priv *priv = &card_priv;
74 struct imx_wm8962_data *data = platform_get_drvdata(priv->pdev);
75 struct device *dev = &priv->pdev->dev;
79 if (dapm->dev != codec_dai->dev)
83 case SND_SOC_BIAS_PREPARE:
84 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
85 if (sample_format == SNDRV_PCM_FORMAT_S24_LE)
86 pll_out = sample_rate * 384;
88 pll_out = sample_rate * 256;
90 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
91 WM8962_FLL_MCLK, data->clk_frequency,
94 dev_err(dev, "failed to start FLL: %d\n", ret);
98 ret = snd_soc_dai_set_sysclk(codec_dai,
99 WM8962_SYSCLK_FLL, pll_out,
102 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
108 case SND_SOC_BIAS_STANDBY:
109 if (dapm->bias_level == SND_SOC_BIAS_PREPARE) {
110 ret = snd_soc_dai_set_sysclk(codec_dai,
111 WM8962_SYSCLK_MCLK, data->clk_frequency,
115 "failed to switch away from FLL: %d\n",
120 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
123 dev_err(dev, "failed to stop FLL: %d\n", ret);
133 dapm->bias_level = level;
138 static int imx_wm8962_late_probe(struct snd_soc_card *card)
140 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
141 struct imx_priv *priv = &card_priv;
142 struct imx_wm8962_data *data = platform_get_drvdata(priv->pdev);
143 struct device *dev = &priv->pdev->dev;
146 ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
147 data->clk_frequency, SND_SOC_CLOCK_IN);
149 dev_err(dev, "failed to set sysclk in %s\n", __func__);
154 static int imx_wm8962_probe(struct platform_device *pdev)
156 struct device_node *np = pdev->dev.of_node;
157 struct device_node *ssi_np, *codec_np;
158 struct platform_device *ssi_pdev;
159 struct imx_priv *priv = &card_priv;
160 struct i2c_client *codec_dev;
161 struct imx_wm8962_data *data;
162 int int_port, ext_port;
167 ret = of_property_read_u32(np, "mux-int-port", &int_port);
169 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
172 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
174 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
179 * The port numbering in the hardware manual starts at 1, while
180 * the audmux API expects it starts at 0.
184 ret = imx_audmux_v2_configure_port(int_port,
185 IMX_AUDMUX_V2_PTCR_SYN |
186 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
187 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
188 IMX_AUDMUX_V2_PTCR_TFSDIR |
189 IMX_AUDMUX_V2_PTCR_TCLKDIR,
190 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
192 dev_err(&pdev->dev, "audmux internal port setup failed\n");
195 imx_audmux_v2_configure_port(ext_port,
196 IMX_AUDMUX_V2_PTCR_SYN,
197 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
199 dev_err(&pdev->dev, "audmux external port setup failed\n");
203 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
204 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
205 if (!ssi_np || !codec_np) {
206 dev_err(&pdev->dev, "phandle missing or invalid\n");
211 ssi_pdev = of_find_device_by_node(ssi_np);
213 dev_err(&pdev->dev, "failed to find SSI platform device\n");
217 codec_dev = of_find_i2c_device_by_node(codec_np);
218 if (!codec_dev || !codec_dev->driver) {
219 dev_err(&pdev->dev, "failed to find codec platform device\n");
224 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
230 data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
231 if (IS_ERR(data->codec_clk)) {
232 ret = PTR_ERR(data->codec_clk);
233 dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
237 data->clk_frequency = clk_get_rate(data->codec_clk);
238 ret = clk_prepare_enable(data->codec_clk);
240 dev_err(&codec_dev->dev, "failed to enable codec clk: %d\n", ret);
244 data->dai.name = "HiFi";
245 data->dai.stream_name = "HiFi";
246 data->dai.codec_dai_name = "wm8962";
247 data->dai.codec_of_node = codec_np;
248 data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
249 data->dai.platform_of_node = ssi_np;
250 data->dai.ops = &imx_hifi_ops;
251 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
252 SND_SOC_DAIFMT_CBM_CFM;
254 data->card.dev = &pdev->dev;
255 ret = snd_soc_of_parse_card_name(&data->card, "model");
258 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
261 data->card.num_links = 1;
262 data->card.dai_link = &data->dai;
263 data->card.dapm_widgets = imx_wm8962_dapm_widgets;
264 data->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8962_dapm_widgets);
266 data->card.late_probe = imx_wm8962_late_probe;
267 data->card.set_bias_level = imx_wm8962_set_bias_level;
269 ret = snd_soc_register_card(&data->card);
271 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
275 platform_set_drvdata(pdev, data);
277 of_node_put(codec_np);
282 if (!IS_ERR(data->codec_clk))
283 clk_disable_unprepare(data->codec_clk);
288 of_node_put(codec_np);
293 static int imx_wm8962_remove(struct platform_device *pdev)
295 struct imx_wm8962_data *data = platform_get_drvdata(pdev);
297 if (!IS_ERR(data->codec_clk))
298 clk_disable_unprepare(data->codec_clk);
299 snd_soc_unregister_card(&data->card);
304 static const struct of_device_id imx_wm8962_dt_ids[] = {
305 { .compatible = "fsl,imx-audio-wm8962", },
308 MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
310 static struct platform_driver imx_wm8962_driver = {
312 .name = "imx-wm8962",
313 .owner = THIS_MODULE,
314 .of_match_table = imx_wm8962_dt_ids,
316 .probe = imx_wm8962_probe,
317 .remove = imx_wm8962_remove,
319 module_platform_driver(imx_wm8962_driver);
321 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
322 MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
323 MODULE_LICENSE("GPL v2");
324 MODULE_ALIAS("platform:imx-wm8962");