ASoC: codecs: msm8916-wcd-analog: add MBHC support
[sfrench/cifs-2.6.git] / sound / soc / qcom / apq8016_sbc.c
1 /*
2  * Copyright (c) 2015 The Linux Foundation. All rights reserved.
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 version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/io.h>
19 #include <linux/of.h>
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <dt-bindings/sound/apq8016-lpass.h>
26
27 struct apq8016_sbc_data {
28         void __iomem *mic_iomux;
29         void __iomem *spkr_iomux;
30         struct snd_soc_dai_link dai_link[];     /* dynamically allocated */
31 };
32
33 #define MIC_CTRL_TER_WS_SLAVE_SEL       BIT(21)
34 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10    BIT(17)
35 #define MIC_CTRL_TLMM_SCLK_EN           BIT(1)
36 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11    (BIT(17) | BIT(16))
37 #define DEFAULT_MCLK_RATE               9600000
38
39 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
40 {
41         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
42         struct snd_soc_codec *codec;
43         struct snd_soc_dai_link *dai_link = rtd->dai_link;
44         struct snd_soc_card *card = rtd->card;
45         struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
46         int i, rval;
47
48         switch (cpu_dai->id) {
49         case MI2S_PRIMARY:
50                 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
51                         pdata->spkr_iomux);
52                 break;
53
54         case MI2S_QUATERNARY:
55                 /* Configure the Quat MI2S to TLMM */
56                 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
57                         MIC_CTRL_TLMM_SCLK_EN,
58                         pdata->mic_iomux);
59                 break;
60         case MI2S_TERTIARY:
61                 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
62                         MIC_CTRL_TLMM_SCLK_EN,
63                         pdata->mic_iomux);
64
65                 break;
66
67         default:
68                 dev_err(card->dev, "unsupported cpu dai configuration\n");
69                 return -EINVAL;
70
71         }
72
73         for (i = 0 ; i < dai_link->num_codecs; i++) {
74                 struct snd_soc_dai *dai = rtd->codec_dais[i];
75
76                 codec = dai->codec;
77                 /* Set default mclk for internal codec */
78                 rval = snd_soc_codec_set_sysclk(codec, 0, 0, DEFAULT_MCLK_RATE,
79                                        SND_SOC_CLOCK_IN);
80                 if (rval != 0 && rval != -ENOTSUPP) {
81                         dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
82                         return rval;
83                 }
84         }
85
86         return 0;
87 }
88
89 static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
90 {
91         struct device *dev = card->dev;
92         struct snd_soc_dai_link *link;
93         struct device_node *np, *codec, *cpu, *node  = dev->of_node;
94         struct apq8016_sbc_data *data;
95         int ret, num_links;
96
97         ret = snd_soc_of_parse_card_name(card, "qcom,model");
98         if (ret) {
99                 dev_err(dev, "Error parsing card name: %d\n", ret);
100                 return ERR_PTR(ret);
101         }
102
103         /* DAPM routes */
104         if (of_property_read_bool(node, "qcom,audio-routing")) {
105                 ret = snd_soc_of_parse_audio_routing(card,
106                                         "qcom,audio-routing");
107                 if (ret)
108                         return ERR_PTR(ret);
109         }
110
111
112         /* Populate links */
113         num_links = of_get_child_count(node);
114
115         /* Allocate the private data and the DAI link array */
116         data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
117                             GFP_KERNEL);
118         if (!data)
119                 return ERR_PTR(-ENOMEM);
120
121         card->dai_link  = &data->dai_link[0];
122         card->num_links = num_links;
123
124         link = data->dai_link;
125
126         for_each_child_of_node(node, np) {
127                 cpu = of_get_child_by_name(np, "cpu");
128                 codec = of_get_child_by_name(np, "codec");
129
130                 if (!cpu || !codec) {
131                         dev_err(dev, "Can't find cpu/codec DT node\n");
132                         return ERR_PTR(-EINVAL);
133                 }
134
135                 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
136                 if (!link->cpu_of_node) {
137                         dev_err(card->dev, "error getting cpu phandle\n");
138                         return ERR_PTR(-EINVAL);
139                 }
140
141                 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
142                 if (ret) {
143                         dev_err(card->dev, "error getting cpu dai name\n");
144                         return ERR_PTR(ret);
145                 }
146
147                 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
148
149                 if (ret < 0) {
150                         dev_err(card->dev, "error getting codec dai name\n");
151                         return ERR_PTR(ret);
152                 }
153
154                 link->platform_of_node = link->cpu_of_node;
155                 ret = of_property_read_string(np, "link-name", &link->name);
156                 if (ret) {
157                         dev_err(card->dev, "error getting codec dai_link name\n");
158                         return ERR_PTR(ret);
159                 }
160
161                 link->stream_name = link->name;
162                 link->init = apq8016_sbc_dai_init;
163                 link++;
164         }
165
166         return data;
167 }
168
169 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
170
171         SND_SOC_DAPM_MIC("Handset Mic", NULL),
172         SND_SOC_DAPM_MIC("Headset Mic", NULL),
173         SND_SOC_DAPM_MIC("Secondary Mic", NULL),
174         SND_SOC_DAPM_MIC("Digital Mic1", NULL),
175         SND_SOC_DAPM_MIC("Digital Mic2", NULL),
176 };
177
178 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
179 {
180         struct device *dev = &pdev->dev;
181         struct snd_soc_card *card;
182         struct apq8016_sbc_data *data;
183         struct resource *res;
184
185         card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
186         if (!card)
187                 return -ENOMEM;
188
189         card->dev = dev;
190         card->dapm_widgets = apq8016_sbc_dapm_widgets;
191         card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
192         data = apq8016_sbc_parse_of(card);
193         if (IS_ERR(data)) {
194                 dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
195                         PTR_ERR(data));
196                 return PTR_ERR(data);
197         }
198
199         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
200         data->mic_iomux = devm_ioremap_resource(dev, res);
201         if (IS_ERR(data->mic_iomux))
202                 return PTR_ERR(data->mic_iomux);
203
204         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
205         data->spkr_iomux = devm_ioremap_resource(dev, res);
206         if (IS_ERR(data->spkr_iomux))
207                 return PTR_ERR(data->spkr_iomux);
208
209         snd_soc_card_set_drvdata(card, data);
210
211         return devm_snd_soc_register_card(&pdev->dev, card);
212 }
213
214 static const struct of_device_id apq8016_sbc_device_id[]  = {
215         { .compatible = "qcom,apq8016-sbc-sndcard" },
216         {},
217 };
218 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
219
220 static struct platform_driver apq8016_sbc_platform_driver = {
221         .driver = {
222                 .name = "qcom-apq8016-sbc",
223                 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
224         },
225         .probe = apq8016_sbc_platform_probe,
226 };
227 module_platform_driver(apq8016_sbc_platform_driver);
228
229 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
230 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
231 MODULE_LICENSE("GPL v2");