blob: be7c1db5388faddcb4f6477b345ed5efba4abc9e [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
28 if (!ret && daifmt)
29 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>"
93 */
94 clk = of_clk_get(np, 0);
95 if (IS_ERR(clk))
96 of_property_read_u32(np,
97 "system-clock-frequency",
98 &dai->sysclk);
99 else
100 dai->sysclk = clk_get_rate(clk);
101
102 ret = 0;
103
104parse_error:
105 of_node_put(*node);
106
107 return ret;
108}
109
110static int asoc_simple_card_parse_of(struct device_node *node,
111 struct asoc_simple_card_info *info,
112 struct device *dev,
113 struct device_node **of_cpu,
114 struct device_node **of_codec,
115 struct device_node **of_platform)
116{
117 struct device_node *np;
118 char *name;
119 int ret = 0;
120
121 /* get CPU/CODEC common format via simple-audio-card,format */
122 info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") &
123 (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK);
124
125 /* CPU sub-node */
126 ret = -EINVAL;
127 np = of_get_child_by_name(node, "simple-audio-card,cpu");
128 if (np)
129 ret = asoc_simple_card_sub_parse_of(np,
130 &info->cpu_dai,
131 of_cpu);
132 if (ret < 0)
133 return ret;
134
135 /* CODEC sub-node */
136 ret = -EINVAL;
137 np = of_get_child_by_name(node, "simple-audio-card,codec");
138 if (np)
139 ret = asoc_simple_card_sub_parse_of(np,
140 &info->codec_dai,
141 of_codec);
142 if (ret < 0)
143 return ret;
144
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800145 if (!info->cpu_dai.name || !info->codec_dai.name)
146 return -EINVAL;
147
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900148 /* card name is created from CPU/CODEC dai name */
149 name = devm_kzalloc(dev,
150 strlen(info->cpu_dai.name) +
151 strlen(info->codec_dai.name) + 2,
152 GFP_KERNEL);
153 sprintf(name, "%s-%s", info->cpu_dai.name, info->codec_dai.name);
154 info->name = info->card = name;
155
156 /* simple-card assumes platform == cpu */
157 *of_platform = *of_cpu;
158
159 dev_dbg(dev, "card-name : %s\n", info->card);
160 dev_dbg(dev, "platform : %04x\n", info->daifmt);
161 dev_dbg(dev, "cpu : %s / %04x / %d\n",
162 info->cpu_dai.name,
163 info->cpu_dai.fmt,
164 info->cpu_dai.sysclk);
165 dev_dbg(dev, "codec : %s / %04x / %d\n",
166 info->codec_dai.name,
167 info->codec_dai.fmt,
168 info->codec_dai.sysclk);
169
170 return 0;
171}
172
Kuninori Morimotof2390882012-04-08 21:17:50 -0700173static int asoc_simple_card_probe(struct platform_device *pdev)
174{
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900175 struct asoc_simple_card_info *cinfo;
176 struct device_node *np = pdev->dev.of_node;
177 struct device_node *of_cpu, *of_codec, *of_platform;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800178 struct device *dev = &pdev->dev;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700179
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900180 cinfo = NULL;
181 of_cpu = NULL;
182 of_codec = NULL;
183 of_platform = NULL;
184 if (np && of_device_is_available(np)) {
185 cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL);
186 if (cinfo) {
187 int ret;
188 ret = asoc_simple_card_parse_of(np, cinfo, dev,
189 &of_cpu,
190 &of_codec,
191 &of_platform);
192 if (ret < 0) {
193 if (ret != -EPROBE_DEFER)
194 dev_err(dev, "parse error %d\n", ret);
195 return ret;
196 }
197 }
198 } else {
199 cinfo = pdev->dev.platform_data;
200 }
201
Kuninori Morimotof2390882012-04-08 21:17:50 -0700202 if (!cinfo) {
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800203 dev_err(dev, "no info for asoc-simple-card\n");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700204 return -EINVAL;
205 }
206
207 if (!cinfo->name ||
208 !cinfo->card ||
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900209 !cinfo->codec_dai.name ||
210 !(cinfo->codec || of_codec) ||
211 !(cinfo->platform || of_platform) ||
212 !(cinfo->cpu_dai.name || of_cpu)) {
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800213 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700214 return -EINVAL;
215 }
216
217 /*
218 * init snd_soc_dai_link
219 */
220 cinfo->snd_link.name = cinfo->name;
221 cinfo->snd_link.stream_name = cinfo->name;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800222 cinfo->snd_link.cpu_dai_name = cinfo->cpu_dai.name;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700223 cinfo->snd_link.platform_name = cinfo->platform;
224 cinfo->snd_link.codec_name = cinfo->codec;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800225 cinfo->snd_link.codec_dai_name = cinfo->codec_dai.name;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900226 cinfo->snd_link.cpu_of_node = of_cpu;
227 cinfo->snd_link.codec_of_node = of_codec;
228 cinfo->snd_link.platform_of_node = of_platform;
Kuninori Morimotoa4a2992c2013-01-10 16:49:11 -0800229 cinfo->snd_link.init = asoc_simple_card_dai_init;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700230
231 /*
232 * init snd_soc_card
233 */
234 cinfo->snd_card.name = cinfo->card;
235 cinfo->snd_card.owner = THIS_MODULE;
236 cinfo->snd_card.dai_link = &cinfo->snd_link;
237 cinfo->snd_card.num_links = 1;
238 cinfo->snd_card.dev = &pdev->dev;
239
240 return snd_soc_register_card(&cinfo->snd_card);
241}
242
243static int asoc_simple_card_remove(struct platform_device *pdev)
244{
245 struct asoc_simple_card_info *cinfo = pdev->dev.platform_data;
246
247 return snd_soc_unregister_card(&cinfo->snd_card);
248}
249
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900250static const struct of_device_id asoc_simple_of_match[] = {
251 { .compatible = "simple-audio-card", },
252 {},
253};
254MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
255
Kuninori Morimotof2390882012-04-08 21:17:50 -0700256static struct platform_driver asoc_simple_card = {
257 .driver = {
258 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300259 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900260 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700261 },
262 .probe = asoc_simple_card_probe,
263 .remove = asoc_simple_card_remove,
264};
265
266module_platform_driver(asoc_simple_card);
267
Fabio Estevamc445be32013-08-23 14:35:17 -0300268MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700269MODULE_LICENSE("GPL");
270MODULE_DESCRIPTION("ASoC Simple Sound Card");
271MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");