blob: 5528dd6a4e4e031e6c3f715c29fedfca34e190d0 [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>
12#include <linux/of.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070013#include <linux/platform_device.h>
14#include <linux/module.h>
15#include <sound/simple_card.h>
16
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080017static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
18 struct asoc_simple_dai *set,
19 unsigned int daifmt)
20{
21 int ret = 0;
22
23 daifmt |= set->fmt;
24
Xiubo Lie874dde2013-12-23 13:24:59 +080025 if (daifmt)
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080026 ret = snd_soc_dai_set_fmt(dai, daifmt);
27
Kuninori Morimotoe244bb92013-10-17 22:46:49 -070028 if (ret == -ENOTSUPP) {
29 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
30 ret = 0;
31 }
32
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080033 if (!ret && set->sysclk)
34 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
35
36 return ret;
37}
38
Kuninori Morimotof2390882012-04-08 21:17:50 -070039static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
40{
Xiubo Liba194a42014-01-13 17:08:08 +080041 struct asoc_simple_card_info *info =
42 snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -070043 struct snd_soc_dai *codec = rtd->codec_dai;
44 struct snd_soc_dai *cpu = rtd->cpu_dai;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080045 unsigned int daifmt = info->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -070046 int ret;
47
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080048 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
49 if (ret < 0)
50 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070051
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080052 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
53 if (ret < 0)
54 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070055
56 return 0;
57}
58
Kuninori Morimotofa558c22013-11-20 15:25:02 +090059static int
60asoc_simple_card_sub_parse_of(struct device_node *np,
61 struct asoc_simple_dai *dai,
62 struct device_node **node)
63{
64 struct clk *clk;
65 int ret;
66
67 /*
68 * get node via "sound-dai = <&phandle port>"
69 * it will be used as xxx_of_node on soc_bind_dai_link()
70 */
71 *node = of_parse_phandle(np, "sound-dai", 0);
72 if (!*node)
73 return -ENODEV;
74
75 /* get dai->name */
76 ret = snd_soc_of_get_dai_name(np, &dai->name);
77 if (ret < 0)
78 goto parse_error;
79
80 /*
81 * bitclock-inversion, frame-inversion
82 * bitclock-master, frame-master
83 * and specific "format" if it has
84 */
85 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
86
87 /*
88 * dai->sysclk come from
89 * "clocks = <&xxx>" (if system has common clock)
90 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +080091 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +090092 */
Xiubo Li71467e42013-12-23 15:25:38 +080093 if (of_property_read_bool(np, "clocks")) {
94 clk = of_clk_get(np, 0);
95 if (IS_ERR(clk)) {
96 ret = PTR_ERR(clk);
97 goto parse_error;
98 }
99
100 dai->sysclk = clk_get_rate(clk);
101 } else if (of_property_read_bool(np, "system-clock-frequency")) {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900102 of_property_read_u32(np,
103 "system-clock-frequency",
104 &dai->sysclk);
Xiubo Li71467e42013-12-23 15:25:38 +0800105 } else {
106 clk = of_clk_get(*node, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800107 if (!IS_ERR(clk))
108 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800109 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900110
111 ret = 0;
112
113parse_error:
114 of_node_put(*node);
115
116 return ret;
117}
118
119static int asoc_simple_card_parse_of(struct device_node *node,
120 struct asoc_simple_card_info *info,
121 struct device *dev,
122 struct device_node **of_cpu,
123 struct device_node **of_codec,
124 struct device_node **of_platform)
125{
126 struct device_node *np;
127 char *name;
Xiubo Lid4c22092013-12-23 12:57:01 +0800128 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900129
130 /* get CPU/CODEC common format via simple-audio-card,format */
131 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
132 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
133
Xiubo Lid4c22092013-12-23 12:57:01 +0800134 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800135 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Xiubo Lif87a3e82014-01-07 09:13:42 +0800136 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800137 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800138 if (ret)
139 return ret;
140 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800141
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900142 /* CPU sub-node */
143 ret = -EINVAL;
144 np = of_get_child_by_name(node, "simple-audio-card,cpu");
145 if (np)
146 ret = asoc_simple_card_sub_parse_of(np,
147 &info->cpu_dai,
148 of_cpu);
149 if (ret < 0)
150 return ret;
151
152 /* CODEC sub-node */
153 ret = -EINVAL;
154 np = of_get_child_by_name(node, "simple-audio-card,codec");
155 if (np)
156 ret = asoc_simple_card_sub_parse_of(np,
157 &info->codec_dai,
158 of_codec);
159 if (ret < 0)
160 return ret;
161
162 /* card name is created from CPU/CODEC dai name */
163 name = devm_kzalloc(dev,
164 strlen(info->cpu_dai.name) +
165 strlen(info->codec_dai.name) + 2,
166 GFP_KERNEL);
167 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
168 info->name = info->card = name;
169
170 /* simple-card assumes platform == cpu */
171 *of_platform = *of_cpu;
172
173 dev_dbg(dev, "card-name : %s\n", info->card);
174 dev_dbg(dev, "platform : %04x\n", info->daifmt);
175 dev_dbg(dev, "cpu : %s / %04x / %d\n",
176 info->cpu_dai.name,
177 info->cpu_dai.fmt,
178 info->cpu_dai.sysclk);
179 dev_dbg(dev, "codec : %s / %04x / %d\n",
180 info->codec_dai.name,
181 info->codec_dai.fmt,
182 info->codec_dai.sysclk);
183
184 return 0;
185}
186
Kuninori Morimotof2390882012-04-08 21:17:50 -0700187static int asoc_simple_card_probe(struct platform_device *pdev)
188{
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900189 struct asoc_simple_card_info *cinfo;
190 struct device_node *np = pdev->dev.of_node;
191 struct device_node *of_cpu, *of_codec, *of_platform;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800192 struct device *dev = &pdev->dev;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700193
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900194 cinfo = NULL;
195 of_cpu = NULL;
196 of_codec = NULL;
197 of_platform = NULL;
198 if (np && of_device_is_available(np)) {
199 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
200 if (cinfo) {
201 int ret;
Xiubo Lid4c22092013-12-23 12:57:01 +0800202 cinfo->snd_card.dev = &pdev->dev;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900203 ret = asoc_simple_card_parse_of(np, cinfo, dev,
204 &of_cpu,
205 &of_codec,
206 &of_platform);
207 if (ret < 0) {
208 if (ret != -EPROBE_DEFER)
209 dev_err(dev, "parse error %d\n", ret);
210 return ret;
211 }
Xiubo Li34787d02014-01-09 17:49:40 +0800212 } else {
213 return -ENOMEM;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900214 }
215 } else {
216 cinfo = pdev->dev.platform_data;
Xiubo Li34787d02014-01-09 17:49:40 +0800217 if (!cinfo) {
218 dev_err(dev, "no info for asoc-simple-card\n");
219 return -EINVAL;
220 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900221
Xiubo Li34787d02014-01-09 17:49:40 +0800222 cinfo->snd_card.dev = &pdev->dev;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700223 }
224
225 if (!cinfo->name ||
226 !cinfo->card ||
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900227 !cinfo->codec_dai.name ||
228 !(cinfo->codec || of_codec) ||
229 !(cinfo->platform || of_platform) ||
230 !(cinfo->cpu_dai.name || of_cpu)) {
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800231 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700232 return -EINVAL;
233 }
234
235 /*
236 * init snd_soc_dai_link
237 */
238 cinfo->snd_link.name = cinfo->name;
239 cinfo->snd_link.stream_name = cinfo->name;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800240 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700241 cinfo->snd_link.platform_name = cinfo->platform;
242 cinfo->snd_link.codec_name = cinfo->codec;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800243 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900244 cinfo->snd_link.cpu_of_node = of_cpu;
245 cinfo->snd_link.codec_of_node = of_codec;
246 cinfo->snd_link.platform_of_node = of_platform;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800247 cinfo->snd_link.init = asoc_simple_card_dai_init;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700248
249 /*
250 * init snd_soc_card
251 */
252 cinfo->snd_card.name = cinfo->card;
253 cinfo->snd_card.owner = THIS_MODULE;
254 cinfo->snd_card.dai_link = &cinfo->snd_link;
255 cinfo->snd_card.num_links = 1;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700256
Xiubo Liba194a42014-01-13 17:08:08 +0800257 snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo);
258
Xiubo Lie1acb402013-12-19 11:59:54 +0800259 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700260}
261
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900262static const struct of_device_id asoc_simple_of_match[] = {
263 { .compatible = "simple-audio-card", },
264 {},
265};
266MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
267
Kuninori Morimotof2390882012-04-08 21:17:50 -0700268static struct platform_driver asoc_simple_card = {
269 .driver = {
270 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300271 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900272 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700273 },
274 .probe = asoc_simple_card_probe,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700275};
276
277module_platform_driver(asoc_simple_card);
278
Fabio Estevamc445be32013-08-23 14:35:17 -0300279MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700280MODULE_LICENSE("GPL");
281MODULE_DESCRIPTION("ASoC Simple Sound Card");
282MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");