OSDN Git Service

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