blob: 159e517fa09aa6ee5d061e658cafcd9d0f8f12a5 [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>
Xiubo Li6ff62ee2014-02-14 09:34:36 +080012#include <linux/device.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070013#include <linux/module.h>
Kuninori Morimotofa558c22013-11-20 15:25:02 +090014#include <linux/of.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070015#include <linux/platform_device.h>
Xiubo Lica919fe2014-01-14 12:35:32 +080016#include <linux/string.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070017#include <sound/simple_card.h>
Xiubo Li6ff62ee2014-02-14 09:34:36 +080018#include <sound/soc-dai.h>
19#include <sound/soc.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070020
Jean-Francois Moine45fce592014-01-15 16:51:56 +010021struct simple_card_data {
22 struct snd_soc_card snd_card;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010023 struct simple_dai_props {
24 struct asoc_simple_dai cpu_dai;
25 struct asoc_simple_dai codec_dai;
26 } *dai_props;
Andrew Lunn2942a0e2014-05-22 17:31:49 +020027 unsigned int mclk_fs;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +010028 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
Jean-Francois Moine45fce592014-01-15 16:51:56 +010029};
30
Andrew Lunn2942a0e2014-05-22 17:31:49 +020031static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
32 struct snd_pcm_hw_params *params)
33{
34 struct snd_soc_pcm_runtime *rtd = substream->private_data;
35 struct snd_soc_dai *codec_dai = rtd->codec_dai;
36 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
37 unsigned int mclk;
38 int ret = 0;
39
40 if (priv->mclk_fs) {
41 mclk = params_rate(params) * priv->mclk_fs;
42 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
43 SND_SOC_CLOCK_IN);
44 }
45
46 return ret;
47}
48
49static struct snd_soc_ops asoc_simple_card_ops = {
50 .hw_params = asoc_simple_card_hw_params,
51};
52
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080053static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
Xiubo Li30d03412014-01-24 15:43:01 +080054 struct asoc_simple_dai *set)
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080055{
Xiubo Li4763ebe2014-01-24 15:43:00 +080056 int ret;
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080057
Xiubo Li30d03412014-01-24 15:43:01 +080058 if (set->fmt) {
59 ret = snd_soc_dai_set_fmt(dai, set->fmt);
Xiubo Li4763ebe2014-01-24 15:43:00 +080060 if (ret && ret != -ENOTSUPP) {
61 dev_err(dai->dev, "simple-card: set_fmt error\n");
62 goto err;
63 }
Kuninori Morimotoe244bb92013-10-17 22:46:49 -070064 }
65
Xiubo Li4763ebe2014-01-24 15:43:00 +080066 if (set->sysclk) {
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080067 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
Xiubo Li4763ebe2014-01-24 15:43:00 +080068 if (ret && ret != -ENOTSUPP) {
69 dev_err(dai->dev, "simple-card: set_sysclk error\n");
70 goto err;
71 }
72 }
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080073
Xiubo Li6ff62ee2014-02-14 09:34:36 +080074 if (set->slots) {
75 ret = snd_soc_dai_set_tdm_slot(dai, 0, 0,
76 set->slots,
77 set->slot_width);
78 if (ret && ret != -ENOTSUPP) {
79 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
80 goto err;
81 }
82 }
83
Xiubo Li4763ebe2014-01-24 15:43:00 +080084 ret = 0;
85
86err:
Kuninori Morimotoa4a29922013-01-10 16:49:11 -080087 return ret;
88}
89
Kuninori Morimotof2390882012-04-08 21:17:50 -070090static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
91{
Nicolin Chen781cbeb2014-04-24 19:14:00 +080092 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotof2390882012-04-08 21:17:50 -070093 struct snd_soc_dai *codec = rtd->codec_dai;
94 struct snd_soc_dai *cpu = rtd->cpu_dai;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +010095 struct simple_dai_props *dai_props;
96 int num, ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -070097
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +010098 num = rtd - rtd->card->rtd;
99 dai_props = &priv->dai_props[num];
100 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800101 if (ret < 0)
102 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700103
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100104 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
Kuninori Morimotoa4a29922013-01-10 16:49:11 -0800105 if (ret < 0)
106 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700107
108 return 0;
109}
110
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900111static int
112asoc_simple_card_sub_parse_of(struct device_node *np,
113 struct asoc_simple_dai *dai,
Jean-Francois Moine52008472014-01-15 16:51:48 +0100114 const struct device_node **p_node,
115 const char **name)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900116{
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100117 struct device_node *node;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900118 struct clk *clk;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300119 u32 val;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900120 int ret;
121
122 /*
123 * get node via "sound-dai = <&phandle port>"
124 * it will be used as xxx_of_node on soc_bind_dai_link()
125 */
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100126 node = of_parse_phandle(np, "sound-dai", 0);
127 if (!node)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900128 return -ENODEV;
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100129 *p_node = node;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900130
131 /* get dai->name */
Jean-Francois Moine52008472014-01-15 16:51:48 +0100132 ret = snd_soc_of_get_dai_name(np, name);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900133 if (ret < 0)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100134 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900135
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800136 /* parse TDM slot */
137 ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width);
138 if (ret)
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100139 return ret;
Xiubo Li6ff62ee2014-02-14 09:34:36 +0800140
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900141 /*
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900142 * dai->sysclk come from
143 * "clocks = <&xxx>" (if system has common clock)
144 * or "system-clock-frequency = <xxx>"
Xiubo Li71467e42013-12-23 15:25:38 +0800145 * or device's module clock.
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900146 */
Xiubo Li71467e42013-12-23 15:25:38 +0800147 if (of_property_read_bool(np, "clocks")) {
148 clk = of_clk_get(np, 0);
149 if (IS_ERR(clk)) {
150 ret = PTR_ERR(clk);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100151 return ret;
Xiubo Li71467e42013-12-23 15:25:38 +0800152 }
153
154 dai->sysclk = clk_get_rate(clk);
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300155 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
156 dai->sysclk = val;
Xiubo Li71467e42013-12-23 15:25:38 +0800157 } else {
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100158 clk = of_clk_get(node, 0);
Xiubo Lie2a19ac2014-01-06 12:34:36 +0800159 if (!IS_ERR(clk))
160 dai->sysclk = clk_get_rate(clk);
Xiubo Li71467e42013-12-23 15:25:38 +0800161 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900162
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100163 return 0;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900164}
165
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200166static int simple_card_dai_link_of(struct device_node *node,
167 struct device *dev,
168 struct snd_soc_dai_link *dai_link,
Jyri Sarha64872212014-04-24 19:42:00 +0300169 struct simple_dai_props *dai_props,
170 bool is_top_level_node)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100171{
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200172 struct device_node *np = NULL;
173 struct device_node *bitclkmaster = NULL;
174 struct device_node *framemaster = NULL;
175 unsigned int daifmt;
176 char *name;
177 char prop[128];
178 char *prefix = "";
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100179 int ret;
180
Jyri Sarha64872212014-04-24 19:42:00 +0300181 if (is_top_level_node)
182 prefix = "simple-audio-card,";
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100183
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200184 daifmt = snd_soc_of_parse_daifmt(node, prefix,
185 &bitclkmaster, &framemaster);
186 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
187
188 snprintf(prop, sizeof(prop), "%scpu", prefix);
189 np = of_get_child_by_name(node, prop);
190 if (!np) {
191 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800192 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200193 goto dai_link_of_err;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100194 }
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200195
196 ret = asoc_simple_card_sub_parse_of(np, &dai_props->cpu_dai,
197 &dai_link->cpu_of_node,
198 &dai_link->cpu_dai_name);
199 if (ret < 0)
200 goto dai_link_of_err;
201
202 dai_props->cpu_dai.fmt = daifmt;
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800203 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200204 case 0x11:
205 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
206 break;
207 case 0x10:
208 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
209 break;
210 case 0x01:
211 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
212 break;
213 default:
214 dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
215 break;
216 }
217
218 of_node_put(np);
219 snprintf(prop, sizeof(prop), "%scodec", prefix);
220 np = of_get_child_by_name(node, prop);
221 if (!np) {
222 ret = -EINVAL;
Nicolin Chen966b8062014-04-24 19:13:59 +0800223 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200224 goto dai_link_of_err;
225 }
226
227 ret = asoc_simple_card_sub_parse_of(np, &dai_props->codec_dai,
228 &dai_link->codec_of_node,
229 &dai_link->codec_dai_name);
230 if (ret < 0)
231 goto dai_link_of_err;
232
233 if (strlen(prefix) && !bitclkmaster && !framemaster) {
234 /* No dai-link level and master setting was not found from
235 sound node level, revert back to legacy DT parsing and
236 take the settings from codec node. */
237 dev_dbg(dev, "%s: Revert to legacy daifmt parsing\n",
238 __func__);
239 dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt =
240 snd_soc_of_parse_daifmt(np, NULL, NULL, NULL) |
241 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
242 } else {
243 dai_props->codec_dai.fmt = daifmt;
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800244 switch (((np == bitclkmaster) << 4) | (np == framemaster)) {
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200245 case 0x11:
246 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM;
247 break;
248 case 0x10:
249 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS;
250 break;
251 case 0x01:
252 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM;
253 break;
254 default:
255 dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS;
256 break;
257 }
258 }
259
260 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800261 ret = -EINVAL;
262 goto dai_link_of_err;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200263 }
264
265 /* simple-card assumes platform == cpu */
266 dai_link->platform_of_node = dai_link->cpu_of_node;
267
268 /* Link name is created from CPU/CODEC dai name */
269 name = devm_kzalloc(dev,
270 strlen(dai_link->cpu_dai_name) +
271 strlen(dai_link->codec_dai_name) + 2,
272 GFP_KERNEL);
273 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
274 dai_link->codec_dai_name);
275 dai_link->name = dai_link->stream_name = name;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200276 dai_link->ops = &asoc_simple_card_ops;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200277
278 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
279 dev_dbg(dev, "\tcpu : %s / %04x / %d\n",
280 dai_link->cpu_dai_name,
281 dai_props->cpu_dai.fmt,
282 dai_props->cpu_dai.sysclk);
283 dev_dbg(dev, "\tcodec : %s / %04x / %d\n",
284 dai_link->codec_dai_name,
285 dai_props->codec_dai.fmt,
286 dai_props->codec_dai.sysclk);
287
288dai_link_of_err:
289 if (np)
290 of_node_put(np);
291 if (bitclkmaster)
292 of_node_put(bitclkmaster);
293 if (framemaster)
294 of_node_put(framemaster);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100295 return ret;
296}
297
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900298static int asoc_simple_card_parse_of(struct device_node *node,
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100299 struct simple_card_data *priv,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100300 struct device *dev,
301 int multi)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900302{
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100303 struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100304 struct simple_dai_props *dai_props = priv->dai_props;
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300305 u32 val;
Xiubo Lid4c22092013-12-23 12:57:01 +0800306 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900307
Xiubo Li27725552014-01-24 15:43:02 +0800308 /* parsing the card name from DT */
309 snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name");
310
Xiubo Li9d681f52014-02-08 15:59:53 +0800311 /* off-codec widgets */
312 if (of_property_read_bool(node, "simple-audio-card,widgets")) {
313 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
314 "simple-audio-card,widgets");
315 if (ret)
316 return ret;
317 }
318
Xiubo Lid4c22092013-12-23 12:57:01 +0800319 /* DAPM routes */
Xiubo Li8c0b8232014-01-07 09:15:16 +0800320 if (of_property_read_bool(node, "simple-audio-card,routing")) {
Jean-Francois Moineb367a322014-01-15 16:52:00 +0100321 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
Xiubo Li8c0b8232014-01-07 09:15:16 +0800322 "simple-audio-card,routing");
Xiubo Lif87a3e82014-01-07 09:13:42 +0800323 if (ret)
324 return ret;
325 }
Xiubo Lid4c22092013-12-23 12:57:01 +0800326
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200327 /* Factor to mclk, used in hw_params() */
Jyri Sarhac7099eb2014-06-13 13:04:36 +0300328 ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val);
329 if (ret == 0)
330 priv->mclk_fs = val;
Andrew Lunn2942a0e2014-05-22 17:31:49 +0200331
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200332 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
333 priv->snd_card.name : "");
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100334
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200335 if (multi) {
336 struct device_node *np = NULL;
337 int i;
338 for (i = 0; (np = of_get_next_child(node, np)); i++) {
339 dev_dbg(dev, "\tlink %d:\n", i);
340 ret = simple_card_dai_link_of(np, dev, dai_link + i,
Jyri Sarha64872212014-04-24 19:42:00 +0300341 dai_props + i, false);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200342 if (ret < 0) {
343 of_node_put(np);
344 return ret;
345 }
346 }
347 } else {
Jyri Sarha64872212014-04-24 19:42:00 +0300348 ret = simple_card_dai_link_of(node, dev, dai_link, dai_props,
349 true);
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100350 if (ret < 0)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200351 return ret;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100352 }
Xiubo Lidd41e0c2013-12-20 14:39:50 +0800353
Xiubo Li27725552014-01-24 15:43:02 +0800354 if (!priv->snd_card.name)
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200355 priv->snd_card.name = priv->snd_card.dai_link->name;
Kuninori Morimotof687d902014-02-27 18:25:24 -0800356
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900357 return 0;
358}
359
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100360/* update the reference count of the devices nodes at end of probe */
361static int asoc_simple_card_unref(struct platform_device *pdev)
362{
363 struct snd_soc_card *card = platform_get_drvdata(pdev);
364 struct snd_soc_dai_link *dai_link;
365 struct device_node *np;
366 int num_links;
367
368 for (num_links = 0, dai_link = card->dai_link;
369 num_links < card->num_links;
370 num_links++, dai_link++) {
371 np = (struct device_node *) dai_link->cpu_of_node;
372 if (np)
373 of_node_put(np);
374 np = (struct device_node *) dai_link->codec_of_node;
375 if (np)
376 of_node_put(np);
377 }
378 return 0;
379}
380
Kuninori Morimotof2390882012-04-08 21:17:50 -0700381static int asoc_simple_card_probe(struct platform_device *pdev)
382{
Jean-Francois Moine45fce592014-01-15 16:51:56 +0100383 struct simple_card_data *priv;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100384 struct snd_soc_dai_link *dai_link;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900385 struct device_node *np = pdev->dev.of_node;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800386 struct device *dev = &pdev->dev;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100387 int num_links, multi, ret;
388
389 /* get the number of DAI links */
390 if (np && of_get_child_by_name(np, "simple-audio-card,dai-link")) {
391 num_links = of_get_child_count(np);
392 multi = 1;
393 } else {
394 num_links = 1;
395 multi = 0;
396 }
Kuninori Morimotof2390882012-04-08 21:17:50 -0700397
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100398 /* allocate the private data and the DAI link array */
399 priv = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100400 sizeof(*priv) + sizeof(*dai_link) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100401 GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100402 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800403 return -ENOMEM;
404
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100405 /*
406 * init snd_soc_card
407 */
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100408 priv->snd_card.owner = THIS_MODULE;
409 priv->snd_card.dev = dev;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100410 dai_link = priv->dai_link;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100411 priv->snd_card.dai_link = dai_link;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100412 priv->snd_card.num_links = num_links;
Xiubo Lica919fe2014-01-14 12:35:32 +0800413
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100414 /* get room for the other properties */
415 priv->dai_props = devm_kzalloc(dev,
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100416 sizeof(*priv->dai_props) * num_links,
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100417 GFP_KERNEL);
418 if (!priv->dai_props)
419 return -ENOMEM;
420
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100421 if (np && of_device_is_available(np)) {
422
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100423 ret = asoc_simple_card_parse_of(np, priv, dev, multi);
Xiubo Lica919fe2014-01-14 12:35:32 +0800424 if (ret < 0) {
425 if (ret != -EPROBE_DEFER)
426 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100427 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900428 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100429
430 /*
431 * soc_bind_dai_link() will check cpu name
432 * after of_node matching if dai_link has cpu_dai_name.
433 * but, it will never match if name was created by fmt_single_name()
434 * remove cpu_dai_name to escape name matching.
435 * see
436 * fmt_single_name()
437 * fmt_multiple_name()
438 */
439 if (num_links == 1)
440 dai_link->cpu_dai_name = NULL;
441
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900442 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100443 struct asoc_simple_card_info *cinfo;
444
445 cinfo = dev->platform_data;
446 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800447 dev_err(dev, "no info for asoc-simple-card\n");
448 return -EINVAL;
449 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900450
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800451 if (!cinfo->name ||
452 !cinfo->codec_dai.name ||
453 !cinfo->codec ||
454 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100455 !cinfo->cpu_dai.name) {
456 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
457 return -EINVAL;
458 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100459
Kuninori Morimoto12ffa6f2014-03-09 19:37:56 -0700460 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100461 dai_link->name = cinfo->name;
462 dai_link->stream_name = cinfo->name;
463 dai_link->platform_name = cinfo->platform;
464 dai_link->codec_name = cinfo->codec;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100465 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
466 dai_link->codec_dai_name = cinfo->codec_dai.name;
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100467 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
468 sizeof(priv->dai_props->cpu_dai));
469 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
470 sizeof(priv->dai_props->codec_dai));
Kuninori Morimoto81985bd2014-03-02 20:32:43 -0800471
Jean-Francois Moinecf7dc232014-03-20 10:52:41 +0100472 priv->dai_props->cpu_dai.fmt |= cinfo->daifmt;
473 priv->dai_props->codec_dai.fmt |= cinfo->daifmt;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700474 }
475
476 /*
477 * init snd_soc_dai_link
478 */
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100479 dai_link->init = asoc_simple_card_dai_init;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700480
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100481 snd_soc_card_set_drvdata(&priv->snd_card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800482
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100483 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
484
485err:
486 asoc_simple_card_unref(pdev);
487 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700488}
489
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900490static const struct of_device_id asoc_simple_of_match[] = {
491 { .compatible = "simple-audio-card", },
492 {},
493};
494MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
495
Kuninori Morimotof2390882012-04-08 21:17:50 -0700496static struct platform_driver asoc_simple_card = {
497 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800498 .name = "asoc-simple-card",
Fabio Estevamc445be32013-08-23 14:35:17 -0300499 .owner = THIS_MODULE,
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900500 .of_match_table = asoc_simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700501 },
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800502 .probe = asoc_simple_card_probe,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700503};
504
505module_platform_driver(asoc_simple_card);
506
Fabio Estevamc445be32013-08-23 14:35:17 -0300507MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700508MODULE_LICENSE("GPL");
509MODULE_DESCRIPTION("ASoC Simple Sound Card");
510MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");