]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/fsl/imx-mqs.c
MLK-9723-5: ASoC: imx-mqs: add mqs machine driver
[karo-tx-linux.git] / sound / soc / fsl / imx-mqs.c
1 /*
2  * Copyright 2012, 2014 Freescale Semiconductor, Inc.
3  * Copyright 2012 Linaro Ltd.
4  *
5  * The code contained herein is licensed under the GNU General Public
6  * License. You may obtain a copy of the GNU General Public License
7  * Version 2 or later at the following locations:
8  *
9  * http://www.opensource.org/licenses/gpl-license.html
10  * http://www.gnu.org/copyleft/gpl.html
11  */
12
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <sound/soc.h>
18
19 #define SUPPORT_RATE_NUM    10
20
21 struct imx_priv {
22         unsigned int mclk_freq;
23         struct platform_device *pdev;
24 };
25
26 static struct imx_priv card_priv;
27
28 static int imx_mqs_startup(struct snd_pcm_substream *substream)
29 {
30         struct snd_pcm_runtime *runtime = substream->runtime;
31         static struct snd_pcm_hw_constraint_list constraint_rates;
32         struct imx_priv *priv = &card_priv;
33         struct device *dev = &priv->pdev->dev;
34         static u32 support_rates[SUPPORT_RATE_NUM];
35         int ret;
36
37         if (priv->mclk_freq == 24576000) {
38                 support_rates[0] = 48000;
39                 support_rates[1] = 96000;
40                 support_rates[2] = 192000;
41                 constraint_rates.list = support_rates;
42                 constraint_rates.count = 3;
43
44                 ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
45                                                         &constraint_rates);
46                 if (ret)
47                         return ret;
48         } else
49                 dev_warn(dev, "mclk may be not supported %d\n", priv->mclk_freq);
50
51         return 0;
52 }
53
54 static struct snd_soc_ops imx_mqs_ops = {
55         .startup = imx_mqs_startup,
56 };
57
58
59
60 static struct snd_soc_dai_link imx_mqs_dai = {
61         .name = "HiFi",
62         .stream_name = "HiFi",
63         .dai_fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF |
64                 SND_SOC_DAIFMT_CBS_CFS,
65         .ops = &imx_mqs_ops,
66 };
67
68 static struct snd_soc_card snd_soc_card_imx_mqs = {
69         .name = "mqs-audio",
70         .dai_link = &imx_mqs_dai,
71         .owner = THIS_MODULE,
72         .num_links = 1,
73 };
74
75 static int imx_mqs_probe(struct platform_device *pdev)
76 {
77         struct device_node *cpu_np, *codec_np;
78         struct imx_priv *priv = &card_priv;
79         struct clk *codec_clk = NULL;
80         struct platform_device *codec_dev;
81         int ret;
82
83         priv->pdev = pdev;
84
85         cpu_np = of_parse_phandle(pdev->dev.of_node, "cpu-dai", 0);
86         if (!cpu_np) {
87                 ret = -EINVAL;
88                 goto fail;
89         }
90
91         codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
92         if (!codec_np) {
93                 dev_err(&pdev->dev, "phandle missing or invalid\n");
94                 ret = -EINVAL;
95                 goto fail;
96         }
97
98         codec_dev = of_find_device_by_node(codec_np);
99         if (!codec_dev) {
100                 dev_err(&codec_dev->dev, "failed to find codec device\n");
101                 ret = -EINVAL;
102                 goto fail;
103         }
104
105         codec_clk = devm_clk_get(&codec_dev->dev, NULL);
106         if (IS_ERR(codec_clk)) {
107                 ret = PTR_ERR(codec_clk);
108                 dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
109                 goto fail;
110         }
111         priv->mclk_freq = clk_get_rate(codec_clk);
112
113
114         imx_mqs_dai.cpu_of_node = cpu_np;
115         imx_mqs_dai.platform_of_node = cpu_np;
116         imx_mqs_dai.codec_dai_name = "fsl-mqs-dai";
117         imx_mqs_dai.codec_of_node = codec_np;
118         snd_soc_card_imx_mqs.dev = &pdev->dev;
119
120         ret = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_imx_mqs);
121         if (ret) {
122                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
123                 goto fail;
124         }
125
126 fail:
127         if (cpu_np)
128                 of_node_put(cpu_np);
129
130         return ret;
131 }
132
133 static const struct of_device_id imx_mqs_dt_ids[] = {
134         { .compatible = "fsl,imx-audio-mqs", },
135         { /* sentinel */ }
136 };
137 MODULE_DEVICE_TABLE(of, imx_mqs_dt_ids);
138
139 static struct platform_driver imx_mqs_driver = {
140         .driver = {
141                 .name = "imx-mqs",
142                 .owner = THIS_MODULE,
143                 .of_match_table = imx_mqs_dt_ids,
144         },
145         .probe = imx_mqs_probe,
146 };
147 module_platform_driver(imx_mqs_driver);
148
149 MODULE_AUTHOR("Nicolin Chen <Guangyu.Chen@freescale.com>");
150 MODULE_DESCRIPTION("Freescale i.MX MQS ASoC machine driver");
151 MODULE_ALIAS("platform:imx-mqs");
152 MODULE_LICENSE("GPL v2");