blob: 2a1b1b5b5221089f32e395087e9c4651ddd00799 [file] [log] [blame]
Kuninori Morimotof2390882012-04-08 21:17:50 -07001/*
2 * ASoC simple sound card support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Kuninori Morimotofa558c22013-11-20 15:25:02 +090011#include <linux/clk.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070012#include <linux/module.h>
Kuninori Morimotofa558c22013-11-20 15:25:02 +090013#include <linux/of.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070014#include <linux/platform_device.h>
Xiubo Lica919fe2014-01-14 12:35:32 +080015#include <linux/string.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070016#include <sound/simple_card.h>
17
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080018static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
19 struct asoc_simple_dai *set,
20 unsigned int daifmt)
21{
22 int ret = 0;
23
24 daifmt |= set->fmt;
25
Xiubo Lie874dde2013-12-23 13:24:59 +080026 if (daifmt)
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080027 ret = snd_soc_dai_set_fmt(dai, daifmt);
28
Kuninori Morimotoe244bb92013-10-17 22:46:49 -070029 if (ret == -ENOTSUPP) {
30 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
31 ret = 0;
32 }
33
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080034 if (!ret && set->sysclk)
35 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
36
37 return ret;
38}
39
Kuninori Morimotof2390882012-04-08 21:17:50 -070040static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
41{
Xiubo Liba194a42014-01-13 17:08:08 +080042 struct asoc_simple_card_info *info =
43 snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -070044 struct snd_soc_dai *codec = rtd->codec_dai;
45 struct snd_soc_dai *cpu = rtd->cpu_dai;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080046 unsigned int daifmt = info->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -070047 int ret;
48
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080049 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
50 if (ret < 0)
51 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070052
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080053 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
54 if (ret < 0)
55 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070056
57 return 0;
58}
59
Kuninori Morimotofa558c22013-11-20 15:25:02 +090060static int
61asoc_simple_card_sub_parse_of(struct device_node *np,
62 struct asoc_simple_dai *dai,
63 struct device_node **node)
64{
65 struct clk *clk;
66 int ret;
67
68 /*
69 * get node via "sound-dai = <&phandle port>"
70 * it will be used as xxx_of_node on soc_bind_dai_link()
71 */
72 *node = of_parse_phandle(np, "sound-dai", 0);
73 if (!*node)
74 return -ENODEV;
75
76 /* get dai->name */
77 ret = snd_soc_of_get_dai_name(np, &dai->name);
78 if (ret < 0)
79 goto parse_error;
80
81 /*
82 * bitclock-inversion, frame-inversion
83 * bitclock-master, frame-master
84 * and specific "format" if it has
85 */
86 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
87
88 /*
89 * dai->sysclk come from
90 * "clocks = <&xxx>" (if system has common clock)
91 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +080092 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +090093 */
Xiubo Li71467e42013-12-23 15:25:38 +080094 if (of_property_read_bool(np, "clocks")) {
95 clk = of_clk_get(np, 0);
96 if (IS_ERR(clk)) {
97 ret = PTR_ERR(clk);
98 goto parse_error;
99 }
100
101 dai->sysclk = clk_get_rate(clk);
102 } else if (of_property_read_bool(np, "system-clock-frequency")) {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900103 of_property_read_u32(np,
104 "system-clock-frequency",
105 &dai->sysclk);
Xiubo Li71467e42013-12-23 15:25:38 +0800106 } else {
107 clk = of_clk_get(*node, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800108 if (!IS_ERR(clk))
109 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800110 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900111
112 ret = 0;
113
114parse_error:
115 of_node_put(*node);
116
117 return ret;
118}
119
120static int asoc_simple_card_parse_of(struct device_node *node,
121 struct asoc_simple_card_info *info,
122 struct device *dev,
123 struct device_node **of_cpu,
124 struct device_node **of_codec,
125 struct device_node **of_platform)
126{
127 struct device_node *np;
128 char *name;
Xiubo Lid4c22092013-12-23 12:57:01 +0800129 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900130
131 /* get CPU/CODEC common format via simple-audio-card,format */
132 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
133 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
134
Xiubo Lid4c22092013-12-23 12:57:01 +0800135 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800136 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Xiubo Lif87a3e82014-01-07 09:13:42 +0800137 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800138 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800139 if (ret)
140 return ret;
141 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800142
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900143 /* CPU sub-node */
144 ret = -EINVAL;
145 np = of_get_child_by_name(node, "simple-audio-card,cpu");
146 if (np)
147 ret = asoc_simple_card_sub_parse_of(np,
148 &info->cpu_dai,
149 of_cpu);
150 if (ret < 0)
151 return ret;
152
153 /* CODEC sub-node */
154 ret = -EINVAL;
155 np = of_get_child_by_name(node, "simple-audio-card,codec");
156 if (np)
157 ret = asoc_simple_card_sub_parse_of(np,
158 &info->codec_dai,
159 of_codec);
160 if (ret < 0)
161 return ret;
162
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800163 if (!info->cpu_dai.name || !info->codec_dai.name)
164 return -EINVAL;
165
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900166 /* card name is created from CPU/CODEC dai name */
167 name = devm_kzalloc(dev,
168 strlen(info->cpu_dai.name) +
169 strlen(info->codec_dai.name) + 2,
170 GFP_KERNEL);
171 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
172 info->name = info->card = name;
173
174 /* simple-card assumes platform == cpu */
175 *of_platform = *of_cpu;
176
177 dev_dbg(dev, "card-name : %s\n", info->card);
178 dev_dbg(dev, "platform : %04x\n", info->daifmt);
179 dev_dbg(dev, "cpu : %s / %04x / %d\n",
180 info->cpu_dai.name,
181 info->cpu_dai.fmt,
182 info->cpu_dai.sysclk);
183 dev_dbg(dev, "codec : %s / %04x / %d\n",
184 info->codec_dai.name,
185 info->codec_dai.fmt,
186 info->codec_dai.sysclk);
187
188 return 0;
189}
190
Kuninori Morimotof2390882012-04-08 21:17:50 -0700191static int asoc_simple_card_probe(struct platform_device *pdev)
192{
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900193 struct asoc_simple_card_info *cinfo;
194 struct device_node *np = pdev->dev.of_node;
195 struct device_node *of_cpu, *of_codec, *of_platform;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800196 struct device *dev = &pdev->dev;
Xiubo Lica919fe2014-01-14 12:35:32 +0800197 int ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700198
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900199 cinfo = NULL;
200 of_cpu = NULL;
201 of_codec = NULL;
202 of_platform = NULL;
Xiubo Lica919fe2014-01-14 12:35:32 +0800203
204 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
205 if (!cinfo)
206 return -ENOMEM;
207
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900208 if (np && of_device_is_available(np)) {
Xiubo Lica919fe2014-01-14 12:35:32 +0800209 cinfo->snd_card.dev = dev;
210
211 ret = asoc_simple_card_parse_of(np, cinfo, dev,
212 &of_cpu,
213 &of_codec,
214 &of_platform);
215 if (ret < 0) {
216 if (ret != -EPROBE_DEFER)
217 dev_err(dev, "parse error %d\n", ret);
218 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900219 }
220 } else {
Xiubo Lica919fe2014-01-14 12:35:32 +0800221 if (!dev->platform_data) {
Xiubo Li34787d02014-01-09 17:49:40 +0800222 dev_err(dev, "no info for asoc-simple-card\n");
223 return -EINVAL;
224 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900225
Xiubo Lica919fe2014-01-14 12:35:32 +0800226 memcpy(cinfo, dev->platform_data, sizeof(*cinfo));
227 cinfo->snd_card.dev = dev;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700228 }
229
230 if (!cinfo->name ||
231 !cinfo->card ||
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900232 !cinfo->codec_dai.name ||
233 !(cinfo->codec || of_codec) ||
234 !(cinfo->platform || of_platform) ||
235 !(cinfo->cpu_dai.name || of_cpu)) {
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800236 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700237 return -EINVAL;
238 }
239
240 /*
241 * init snd_soc_dai_link
242 */
243 cinfo->snd_link.name = cinfo->name;
244 cinfo->snd_link.stream_name = cinfo->name;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800245 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700246 cinfo->snd_link.platform_name = cinfo->platform;
247 cinfo->snd_link.codec_name = cinfo->codec;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800248 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900249 cinfo->snd_link.cpu_of_node = of_cpu;
250 cinfo->snd_link.codec_of_node = of_codec;
251 cinfo->snd_link.platform_of_node = of_platform;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800252 cinfo->snd_link.init = asoc_simple_card_dai_init;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700253
254 /*
255 * init snd_soc_card
256 */
257 cinfo->snd_card.name = cinfo->card;
258 cinfo->snd_card.owner = THIS_MODULE;
259 cinfo->snd_card.dai_link = &cinfo->snd_link;
260 cinfo->snd_card.num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700261
Xiubo Liba194a42014-01-13 17:08:08 +0800262 snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo);
263
Xiubo Lie1acb402013-12-19 11:59:54 +0800264 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700265}
266
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900267static const struct of_device_id asoc_simple_of_match[] = {
268 { .compatible = "simple-audio-card", },
269 {},
270};
271MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
272
Kuninori Morimotof2390882012-04-08 21:17:50 -0700273static struct platform_driver asoc_simple_card = {
274 .driver = {
275 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300276 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900277 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700278 },
279 .probe = asoc_simple_card_probe,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700280};
281
282module_platform_driver(asoc_simple_card);
283
Fabio Estevamc445be32013-08-23 14:35:17 -0300284MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700285MODULE_LICENSE("GPL");
286MODULE_DESCRIPTION("ASoC Simple Sound Card");
287MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");