blob: 201a70d1027a4a91bddd6dbc6e8d304071b3dd8d [file] [log] [blame]
Nicolin Chen8de2ae22013-06-11 02:43:30 +08001/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
4 * Based on imx-sgtl5000.c
5 * Copyright 2012 Freescale Semiconductor, Inc.
6 * Copyright 2012 Linaro Ltd.
7 *
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:
11 *
12 * http://www.opensource.org/licenses/gpl-license.html
13 * http://www.gnu.org/copyleft/gpl.html
14 */
15
16#include <linux/module.h>
17#include <linux/of_platform.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010018#include <linux/i2c.h>
Nicolin Chen8de2ae22013-06-11 02:43:30 +080019#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>
25
26#include "../codecs/wm8962.h"
27#include "imx-audmux.h"
28
29#define DAI_NAME_SIZE 32
30
31struct 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;
38};
39
40struct imx_priv {
41 struct platform_device *pdev;
42};
43static struct imx_priv card_priv;
44
45static 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),
50};
51
52static int sample_rate = 44100;
53static snd_pcm_format_t sample_format = SNDRV_PCM_FORMAT_S16_LE;
54
55static int imx_hifi_hw_params(struct snd_pcm_substream *substream,
56 struct snd_pcm_hw_params *params)
57{
58 sample_rate = params_rate(params);
59 sample_format = params_format(params);
60
61 return 0;
62}
63
64static struct snd_soc_ops imx_hifi_ops = {
65 .hw_params = imx_hifi_hw_params,
66};
67
68static 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)
71{
Mengdong Lin50159202015-11-18 02:34:01 -050072 struct snd_soc_pcm_runtime *rtd;
73 struct snd_soc_dai *codec_dai;
Nicolin Chen8de2ae22013-06-11 02:43:30 +080074 struct imx_priv *priv = &card_priv;
Shawn Guo47cf84e2014-02-08 13:20:35 +080075 struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
Nicolin Chen8de2ae22013-06-11 02:43:30 +080076 struct device *dev = &priv->pdev->dev;
77 unsigned int pll_out;
78 int ret;
79
Mengdong Lin50159202015-11-18 02:34:01 -050080 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
81 codec_dai = rtd->codec_dai;
Nicolin Chen8de2ae22013-06-11 02:43:30 +080082 if (dapm->dev != codec_dai->dev)
83 return 0;
84
85 switch (level) {
86 case SND_SOC_BIAS_PREPARE:
87 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
88 if (sample_format == SNDRV_PCM_FORMAT_S24_LE)
89 pll_out = sample_rate * 384;
90 else
91 pll_out = sample_rate * 256;
92
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;
99 }
100
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;
107 }
108 }
109 break;
110
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;
121 }
122
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;
128 }
129 }
130 break;
131
132 default:
133 break;
134 }
135
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800136 return 0;
137}
138
139static int imx_wm8962_late_probe(struct snd_soc_card *card)
140{
Mengdong Lin50159202015-11-18 02:34:01 -0500141 struct snd_soc_pcm_runtime *rtd;
142 struct snd_soc_dai *codec_dai;
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800143 struct imx_priv *priv = &card_priv;
Shawn Guo47cf84e2014-02-08 13:20:35 +0800144 struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800145 struct device *dev = &priv->pdev->dev;
146 int ret;
147
Mengdong Lin50159202015-11-18 02:34:01 -0500148 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
149 codec_dai = rtd->codec_dai;
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800150 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__);
154
155 return ret;
156}
157
158static int imx_wm8962_probe(struct platform_device *pdev)
159{
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 imx_priv *priv = &card_priv;
164 struct i2c_client *codec_dev;
165 struct imx_wm8962_data *data;
166 int int_port, ext_port;
167 int ret;
168
169 priv->pdev = pdev;
170
171 ret = of_property_read_u32(np, "mux-int-port", &int_port);
172 if (ret) {
173 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
174 return ret;
175 }
176 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
177 if (ret) {
178 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
179 return ret;
180 }
181
182 /*
183 * The port numbering in the hardware manual starts at 1, while
184 * the audmux API expects it starts at 0.
185 */
186 int_port--;
187 ext_port--;
188 ret = imx_audmux_v2_configure_port(int_port,
189 IMX_AUDMUX_V2_PTCR_SYN |
190 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
191 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
192 IMX_AUDMUX_V2_PTCR_TFSDIR |
193 IMX_AUDMUX_V2_PTCR_TCLKDIR,
194 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
195 if (ret) {
196 dev_err(&pdev->dev, "audmux internal port setup failed\n");
197 return ret;
198 }
Dan Carpenter474ff0a2015-06-10 18:37:23 +0300199 ret = imx_audmux_v2_configure_port(ext_port,
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800200 IMX_AUDMUX_V2_PTCR_SYN,
201 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
202 if (ret) {
203 dev_err(&pdev->dev, "audmux external port setup failed\n");
204 return ret;
205 }
206
207 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
208 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
209 if (!ssi_np || !codec_np) {
210 dev_err(&pdev->dev, "phandle missing or invalid\n");
211 ret = -EINVAL;
212 goto fail;
213 }
214
215 ssi_pdev = of_find_device_by_node(ssi_np);
216 if (!ssi_pdev) {
217 dev_err(&pdev->dev, "failed to find SSI platform device\n");
218 ret = -EINVAL;
219 goto fail;
220 }
221 codec_dev = of_find_i2c_device_by_node(codec_np);
Lars-Peter Clausen40df2492013-09-29 10:51:05 +0200222 if (!codec_dev || !codec_dev->dev.driver) {
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800223 dev_err(&pdev->dev, "failed to find codec platform device\n");
Fabio Estevamd8a14e32013-07-18 15:07:48 -0300224 ret = -EINVAL;
225 goto fail;
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800226 }
227
228 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
229 if (!data) {
230 ret = -ENOMEM;
231 goto fail;
232 }
233
234 data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
235 if (IS_ERR(data->codec_clk)) {
236 ret = PTR_ERR(data->codec_clk);
237 dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
238 goto fail;
239 }
240
241 data->clk_frequency = clk_get_rate(data->codec_clk);
242 ret = clk_prepare_enable(data->codec_clk);
243 if (ret) {
244 dev_err(&codec_dev->dev, "failed to enable codec clk: %d\n", ret);
245 goto fail;
246 }
247
248 data->dai.name = "HiFi";
249 data->dai.stream_name = "HiFi";
250 data->dai.codec_dai_name = "wm8962";
251 data->dai.codec_of_node = codec_np;
252 data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
253 data->dai.platform_of_node = ssi_np;
254 data->dai.ops = &imx_hifi_ops;
255 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
256 SND_SOC_DAIFMT_CBM_CFM;
257
258 data->card.dev = &pdev->dev;
259 ret = snd_soc_of_parse_card_name(&data->card, "model");
260 if (ret)
261 goto clk_fail;
262 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
263 if (ret)
264 goto clk_fail;
265 data->card.num_links = 1;
Fabio Estevam0984f342015-01-14 11:56:22 -0200266 data->card.owner = THIS_MODULE;
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800267 data->card.dai_link = &data->dai;
268 data->card.dapm_widgets = imx_wm8962_dapm_widgets;
269 data->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8962_dapm_widgets);
270
271 data->card.late_probe = imx_wm8962_late_probe;
272 data->card.set_bias_level = imx_wm8962_set_bias_level;
273
Shawn Guo47cf84e2014-02-08 13:20:35 +0800274 platform_set_drvdata(pdev, &data->card);
275 snd_soc_card_set_drvdata(&data->card, data);
276
Sachin Kamateafbae22013-09-17 09:42:48 +0530277 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800278 if (ret) {
279 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
280 goto clk_fail;
281 }
282
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800283 of_node_put(ssi_np);
284 of_node_put(codec_np);
285
286 return 0;
287
288clk_fail:
Dan Carpenterea8e5e52013-11-08 12:54:44 +0300289 clk_disable_unprepare(data->codec_clk);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800290fail:
Fabio Estevam051c77e2014-10-07 15:13:24 -0300291 of_node_put(ssi_np);
292 of_node_put(codec_np);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800293
294 return ret;
295}
296
297static int imx_wm8962_remove(struct platform_device *pdev)
298{
Shawn Guo47cf84e2014-02-08 13:20:35 +0800299 struct snd_soc_card *card = platform_get_drvdata(pdev);
300 struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800301
302 if (!IS_ERR(data->codec_clk))
303 clk_disable_unprepare(data->codec_clk);
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800304
305 return 0;
306}
307
308static const struct of_device_id imx_wm8962_dt_ids[] = {
309 { .compatible = "fsl,imx-audio-wm8962", },
310 { /* sentinel */ }
311};
312MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
313
314static struct platform_driver imx_wm8962_driver = {
315 .driver = {
316 .name = "imx-wm8962",
Nicolin Chen1abe7292013-10-24 18:15:29 +0800317 .pm = &snd_soc_pm_ops,
Nicolin Chen8de2ae22013-06-11 02:43:30 +0800318 .of_match_table = imx_wm8962_dt_ids,
319 },
320 .probe = imx_wm8962_probe,
321 .remove = imx_wm8962_remove,
322};
323module_platform_driver(imx_wm8962_driver);
324
325MODULE_AUTHOR("Freescale Semiconductor, Inc.");
326MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
327MODULE_LICENSE("GPL v2");
328MODULE_ALIAS("platform:imx-wm8962");