blob: eb95beb25d43d60f92a4dff742b804e1e1c21c21 [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
17#define asoc_simple_get_card_info(p) \
18 container_of(p->dai_link, struct asoc_simple_card_info, snd_link)
19
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080020static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
21 struct asoc_simple_dai *set,
22 unsigned int daifmt)
23{
24 int ret = 0;
25
26 daifmt |= set->fmt;
27
Xiubo Lie874dde2013-12-23 13:24:59 +080028 if (daifmt)
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080029 ret = snd_soc_dai_set_fmt(dai, daifmt);
30
Kuninori Morimotoe244bb92013-10-17 22:46:49 -070031 if (ret == -ENOTSUPP) {
32 dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
33 ret = 0;
34 }
35
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080036 if (!ret && set->sysclk)
37 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
38
39 return ret;
40}
41
Kuninori Morimotof2390882012-04-08 21:17:50 -070042static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
43{
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080044 struct asoc_simple_card_info *info = asoc_simple_get_card_info(rtd);
Kuninori Morimotof2390882012-04-08 21:17:50 -070045 struct snd_soc_dai *codec = rtd->codec_dai;
46 struct snd_soc_dai *cpu = rtd->cpu_dai;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080047 unsigned int daifmt = info->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -070048 int ret;
49
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080050 ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt);
51 if (ret < 0)
52 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070053
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -080054 ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt);
55 if (ret < 0)
56 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070057
58 return 0;
59}
60
Kuninori Morimotofa558c22013-11-20 15:25:02 +090061static int
62asoc_simple_card_sub_parse_of(struct device_node *np,
63 struct asoc_simple_dai *dai,
64 struct device_node **node)
65{
66 struct clk *clk;
67 int ret;
68
69 /*
70 * get node via "sound-dai = <&phandle port>"
71 * it will be used as xxx_of_node on soc_bind_dai_link()
72 */
73 *node = of_parse_phandle(np, "sound-dai", 0);
74 if (!*node)
75 return -ENODEV;
76
77 /* get dai->name */
78 ret = snd_soc_of_get_dai_name(np, &dai->name);
79 if (ret < 0)
80 goto parse_error;
81
82 /*
83 * bitclock-inversion, frame-inversion
84 * bitclock-master, frame-master
85 * and specific "format" if it has
86 */
87 dai->fmt = snd_soc_of_parse_daifmt(np, NULL);
88
89 /*
90 * dai->sysclk come from
91 * "clocks = <&xxx>" (if system has common clock)
92 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +080093 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +090094 */
Xiubo Li71467e42013-12-23 15:25:38 +080095 if (of_property_read_bool(np, "clocks")) {
96 clk = of_clk_get(np, 0);
97 if (IS_ERR(clk)) {
98 ret = PTR_ERR(clk);
99 goto parse_error;
100 }
101
102 dai->sysclk = clk_get_rate(clk);
103 } else if (of_property_read_bool(np, "system-clock-frequency")) {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900104 of_property_read_u32(np,
105 "system-clock-frequency",
106 &dai->sysclk);
Xiubo Li71467e42013-12-23 15:25:38 +0800107 } else {
108 clk = of_clk_get(*node, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800109 if (!IS_ERR(clk))
110 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800111 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900112
113 ret = 0;
114
115parse_error:
116 of_node_put(*node);
117
118 return ret;
119}
120
121static int asoc_simple_card_parse_of(struct device_node *node,
122 struct asoc_simple_card_info *info,
123 struct device *dev,
124 struct device_node **of_cpu,
125 struct device_node **of_codec,
126 struct device_node **of_platform)
127{
128 struct device_node *np;
129 char *name;
Xiubo Lid4c22092013-12-23 12:57:01 +0800130 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900131
132 /* get CPU/CODEC common format via simple-audio-card,format */
133 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
134 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
135
Xiubo Lid4c22092013-12-23 12:57:01 +0800136 /* DAPM routes */
Xiubo Lif87a3e82014-01-07 09:13:42 +0800137 if (of_property_read_bool(node, "simple-audio-routing")) {
138 ret = snd_soc_of_parse_audio_routing(&info->snd_card,
Xiubo Lid4c22092013-12-23 12:57:01 +0800139 "simple-audio-routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800140 if (ret)
141 return ret;
142 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800143
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900144 /* CPU sub-node */
145 ret = -EINVAL;
146 np = of_get_child_by_name(node, "simple-audio-card,cpu");
147 if (np)
148 ret = asoc_simple_card_sub_parse_of(np,
149 &info->cpu_dai,
150 of_cpu);
151 if (ret < 0)
152 return ret;
153
154 /* CODEC sub-node */
155 ret = -EINVAL;
156 np = of_get_child_by_name(node, "simple-audio-card,codec");
157 if (np)
158 ret = asoc_simple_card_sub_parse_of(np,
159 &info->codec_dai,
160 of_codec);
161 if (ret < 0)
162 return ret;
163
164 /* card name is created from CPU/CODEC dai name */
165 name = devm_kzalloc(dev,
166 strlen(info->cpu_dai.name) +
167 strlen(info->codec_dai.name) + 2,
168 GFP_KERNEL);
169 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
170 info->name = info->card = name;
171
172 /* simple-card assumes platform == cpu */
173 *of_platform = *of_cpu;
174
175 dev_dbg(dev, "card-name : %s\n", info->card);
176 dev_dbg(dev, "platform : %04x\n", info->daifmt);
177 dev_dbg(dev, "cpu : %s / %04x / %d\n",
178 info->cpu_dai.name,
179 info->cpu_dai.fmt,
180 info->cpu_dai.sysclk);
181 dev_dbg(dev, "codec : %s / %04x / %d\n",
182 info->codec_dai.name,
183 info->codec_dai.fmt,
184 info->codec_dai.sysclk);
185
186 return 0;
187}
188
Kuninori Morimotof2390882012-04-08 21:17:50 -0700189static int asoc_simple_card_probe(struct platform_device *pdev)
190{
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900191 struct asoc_simple_card_info *cinfo;
192 struct device_node *np = pdev->dev.of_node;
193 struct device_node *of_cpu, *of_codec, *of_platform;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800194 struct device *dev = &pdev->dev;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700195
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900196 cinfo = NULL;
197 of_cpu = NULL;
198 of_codec = NULL;
199 of_platform = NULL;
200 if (np && of_device_is_available(np)) {
201 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
202 if (cinfo) {
203 int ret;
Xiubo Lid4c22092013-12-23 12:57:01 +0800204 cinfo->snd_card.dev = &pdev->dev;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900205 ret = asoc_simple_card_parse_of(np, cinfo, dev,
206 &of_cpu,
207 &of_codec,
208 &of_platform);
209 if (ret < 0) {
210 if (ret != -EPROBE_DEFER)
211 dev_err(dev, "parse error %d\n", ret);
212 return ret;
213 }
214 }
215 } else {
Xiubo Lid4c22092013-12-23 12:57:01 +0800216 cinfo->snd_card.dev = &pdev->dev;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900217 cinfo = pdev->dev.platform_data;
218 }
219
Kuninori Morimotof2390882012-04-08 21:17:50 -0700220 if (!cinfo) {
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800221 dev_err(dev, "no info for asoc-simple-card\n");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700222 return -EINVAL;
223 }
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 Lie1acb402013-12-19 11:59:54 +0800257 return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card);
Kuninori Morimotof2390882012-04-08 21:17:50 -0700258}
259
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900260static const struct of_device_id asoc_simple_of_match[] = {
261 { .compatible = "simple-audio-card", },
262 {},
263};
264MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
265
Kuninori Morimotof2390882012-04-08 21:17:50 -0700266static struct platform_driver asoc_simple_card = {
267 .driver = {
268 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300269 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900270 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700271 },
272 .probe = asoc_simple_card_probe,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700273};
274
275module_platform_driver(asoc_simple_card);
276
Fabio Estevamc445be32013-08-23 14:35:17 -0300277MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700278MODULE_LICENSE("GPL");
279MODULE_DESCRIPTION("ASoC Simple Sound Card");
280MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");