ASoC: qcom: apq8016-sbc: add board specific dapm mic widgets
[deliverable/linux.git] / sound / soc / qcom / apq8016_sbc.c
CommitLineData
bdb052e8
SK
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
27struct 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
bbedefb9 33#define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
bdb052e8
SK
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
38static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
39{
40 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
41 struct snd_soc_card *card = rtd->card;
42 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
43 int rval = 0;
44
45 switch (cpu_dai->id) {
46 case MI2S_PRIMARY:
47 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
48 pdata->spkr_iomux);
49 break;
50
51 case MI2S_QUATERNARY:
52 /* Configure the Quat MI2S to TLMM */
53 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
54 MIC_CTRL_TLMM_SCLK_EN,
55 pdata->mic_iomux);
56 break;
bbedefb9
SK
57 case MI2S_TERTIARY:
58 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
59 MIC_CTRL_TLMM_SCLK_EN,
60 pdata->mic_iomux);
61
62 break;
bdb052e8
SK
63
64 default:
65 dev_err(card->dev, "unsupported cpu dai configuration\n");
66 rval = -EINVAL;
67 break;
68
69 }
70
71 return rval;
72}
73
74static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
75{
76 struct device *dev = card->dev;
77 struct snd_soc_dai_link *link;
78 struct device_node *np, *codec, *cpu, *node = dev->of_node;
79 struct apq8016_sbc_data *data;
80 int ret, num_links;
81
82 ret = snd_soc_of_parse_card_name(card, "qcom,model");
83 if (ret) {
84 dev_err(dev, "Error parsing card name: %d\n", ret);
85 return ERR_PTR(ret);
86 }
87
88 /* Populate links */
89 num_links = of_get_child_count(node);
90
91 /* Allocate the private data and the DAI link array */
92 data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
93 GFP_KERNEL);
94 if (!data)
95 return ERR_PTR(-ENOMEM);
96
97 card->dai_link = &data->dai_link[0];
98 card->num_links = num_links;
99
100 link = data->dai_link;
101
102 for_each_child_of_node(node, np) {
103 cpu = of_get_child_by_name(np, "cpu");
104 codec = of_get_child_by_name(np, "codec");
105
106 if (!cpu || !codec) {
107 dev_err(dev, "Can't find cpu/codec DT node\n");
108 return ERR_PTR(-EINVAL);
109 }
110
111 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
112 if (!link->cpu_of_node) {
113 dev_err(card->dev, "error getting cpu phandle\n");
114 return ERR_PTR(-EINVAL);
115 }
116
117 link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0);
118 if (!link->codec_of_node) {
119 dev_err(card->dev, "error getting codec phandle\n");
120 return ERR_PTR(-EINVAL);
121 }
122
123 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
124 if (ret) {
125 dev_err(card->dev, "error getting cpu dai name\n");
126 return ERR_PTR(ret);
127 }
128
129 ret = snd_soc_of_get_dai_name(codec, &link->codec_dai_name);
130 if (ret) {
131 dev_err(card->dev, "error getting codec dai name\n");
132 return ERR_PTR(ret);
133 }
134
135 link->platform_of_node = link->cpu_of_node;
bdb052e8
SK
136 ret = of_property_read_string(np, "link-name", &link->name);
137 if (ret) {
138 dev_err(card->dev, "error getting codec dai_link name\n");
139 return ERR_PTR(ret);
140 }
141
142 link->stream_name = link->name;
143 link->init = apq8016_sbc_dai_init;
144 link++;
145 }
146
147 return data;
148}
149
1190300d
SK
150static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
151
152 SND_SOC_DAPM_MIC("Handset Mic", NULL),
153 SND_SOC_DAPM_MIC("Headset Mic", NULL),
154 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
155 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
156 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
157};
158
bdb052e8
SK
159static int apq8016_sbc_platform_probe(struct platform_device *pdev)
160{
161 struct device *dev = &pdev->dev;
162 struct snd_soc_card *card;
163 struct apq8016_sbc_data *data;
164 struct resource *res;
165
166 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
167 if (!card)
168 return -ENOMEM;
169
170 card->dev = dev;
1190300d
SK
171 card->dapm_widgets = apq8016_sbc_dapm_widgets;
172 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
bdb052e8
SK
173 data = apq8016_sbc_parse_of(card);
174 if (IS_ERR(data)) {
175 dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
176 PTR_ERR(data));
177 return PTR_ERR(data);
178 }
179
180 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
181 data->mic_iomux = devm_ioremap_resource(dev, res);
182 if (IS_ERR(data->mic_iomux))
183 return PTR_ERR(data->mic_iomux);
184
185 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
186 data->spkr_iomux = devm_ioremap_resource(dev, res);
187 if (IS_ERR(data->spkr_iomux))
188 return PTR_ERR(data->spkr_iomux);
189
190 platform_set_drvdata(pdev, data);
191 snd_soc_card_set_drvdata(card, data);
192
193 return devm_snd_soc_register_card(&pdev->dev, card);
194}
195
196static const struct of_device_id apq8016_sbc_device_id[] = {
197 { .compatible = "qcom,apq8016-sbc-sndcard" },
198 {},
199};
200MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
201
202static struct platform_driver apq8016_sbc_platform_driver = {
203 .driver = {
204 .name = "qcom-apq8016-sbc",
205 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
206 },
207 .probe = apq8016_sbc_platform_probe,
208};
209module_platform_driver(apq8016_sbc_platform_driver);
210
211MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
212MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
213MODULE_LICENSE("GPL v2");
This page took 0.076264 seconds and 5 git commands to generate.