blob: 8a357fdf1077c91822d6699dcf79e7aa96574809 [file] [log] [blame]
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +00001/*
2 * Renesas Sampling Rate Convert Sound Card for DPCM
3 *
4 * Copyright (C) 2015 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * based on ${LINUX}/sound/soc/generic/simple-card.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/clk.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/string.h>
20#include <sound/jack.h>
21#include <sound/soc.h>
22#include <sound/soc-dai.h>
23
24struct rsrc_card_of_data {
25 const char *prefix;
26 const struct snd_soc_dapm_route *routes;
27 int num_routes;
28};
29
30static const struct snd_soc_dapm_route routes_ssi0_ak4642[] = {
31 {"ak4642 Playback", NULL, "DAI0 Playback"},
32 {"DAI0 Capture", NULL, "ak4642 Capture"},
33};
34
35static const struct rsrc_card_of_data routes_of_ssi0_ak4642 = {
36 .prefix = "ak4642",
37 .routes = routes_ssi0_ak4642,
38 .num_routes = ARRAY_SIZE(routes_ssi0_ak4642),
39};
40
41static const struct of_device_id rsrc_card_of_match[] = {
42 { .compatible = "renesas,rsrc-card,lager", .data = &routes_of_ssi0_ak4642 },
43 { .compatible = "renesas,rsrc-card,koelsch", .data = &routes_of_ssi0_ak4642 },
Kuninori Morimotob7419dd2015-07-15 07:08:05 +000044 { .compatible = "renesas,rsrc-card", },
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000045 {},
46};
47MODULE_DEVICE_TABLE(of, rsrc_card_of_match);
48
Kuninori Morimoto04700022015-06-15 06:24:15 +000049#define DAI_NAME_NUM 32
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000050struct rsrc_card_dai {
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000051 unsigned int sysclk;
Kuninori Morimoto6dad9752015-12-17 02:48:23 +000052 unsigned int tx_slot_mask;
53 unsigned int rx_slot_mask;
54 int slots;
55 int slot_width;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000056 struct clk *clk;
Kuninori Morimoto04700022015-06-15 06:24:15 +000057 char dai_name[DAI_NAME_NUM];
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000058};
59
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000060#define IDX_CPU 0
61#define IDX_CODEC 1
62struct rsrc_card_priv {
63 struct snd_soc_card snd_card;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000064 struct snd_soc_codec_conf codec_conf;
Kuninori Morimoto04700022015-06-15 06:24:15 +000065 struct rsrc_card_dai *dai_props;
Kuninori Morimoto3433bf02015-06-15 06:22:30 +000066 struct snd_soc_dai_link *dai_link;
67 int dai_num;
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +000068 u32 convert_rate;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000069};
70
71#define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev)
Kuninori Morimoto8bd616c2015-06-15 06:23:08 +000072#define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i))
73#define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i))
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000074#define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data)
75
76static int rsrc_card_startup(struct snd_pcm_substream *substream)
77{
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
79 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto04700022015-06-15 06:24:15 +000080 struct rsrc_card_dai *dai_props =
Mengdong Lin1a497982015-11-18 02:34:11 -050081 rsrc_priv_to_props(priv, rtd->num);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000082
Kuninori Morimoto04700022015-06-15 06:24:15 +000083 return clk_prepare_enable(dai_props->clk);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000084}
85
86static void rsrc_card_shutdown(struct snd_pcm_substream *substream)
87{
88 struct snd_soc_pcm_runtime *rtd = substream->private_data;
89 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto04700022015-06-15 06:24:15 +000090 struct rsrc_card_dai *dai_props =
Mengdong Lin1a497982015-11-18 02:34:11 -050091 rsrc_priv_to_props(priv, rtd->num);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000092
Kuninori Morimoto04700022015-06-15 06:24:15 +000093 clk_disable_unprepare(dai_props->clk);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000094}
95
96static struct snd_soc_ops rsrc_card_ops = {
97 .startup = rsrc_card_startup,
98 .shutdown = rsrc_card_shutdown,
99};
100
Kuninori Morimoto04700022015-06-15 06:24:15 +0000101static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000102{
Kuninori Morimoto04700022015-06-15 06:24:15 +0000103 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
104 struct snd_soc_dai *dai;
105 struct snd_soc_dai_link *dai_link;
106 struct rsrc_card_dai *dai_props;
Mengdong Lin1a497982015-11-18 02:34:11 -0500107 int num = rtd->num;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000108 int ret;
109
Kuninori Morimoto04700022015-06-15 06:24:15 +0000110 dai_link = rsrc_priv_to_link(priv, num);
111 dai_props = rsrc_priv_to_props(priv, num);
112 dai = dai_link->dynamic ?
113 rtd->cpu_dai :
114 rtd->codec_dai;
115
Kuninori Morimoto04700022015-06-15 06:24:15 +0000116 if (dai_props->sysclk) {
117 ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000118 if (ret && ret != -ENOTSUPP) {
119 dev_err(dai->dev, "set_sysclk error\n");
120 goto err;
121 }
122 }
123
Kuninori Morimoto6dad9752015-12-17 02:48:23 +0000124 if (dai_props->slots) {
125 ret = snd_soc_dai_set_tdm_slot(dai,
126 dai_props->tx_slot_mask,
127 dai_props->rx_slot_mask,
128 dai_props->slots,
129 dai_props->slot_width);
130 if (ret && ret != -ENOTSUPP) {
131 dev_err(dai->dev, "set_tdm_slot error\n");
132 goto err;
133 }
134 }
135
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000136 ret = 0;
137
138err:
139 return ret;
140}
141
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +0000142static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
143 struct snd_pcm_hw_params *params)
144{
145 struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
146 struct snd_interval *rate = hw_param_interval(params,
147 SNDRV_PCM_HW_PARAM_RATE);
148
149 if (!priv->convert_rate)
150 return 0;
151
152 rate->min = rate->max = priv->convert_rate;
153
154 return 0;
155}
156
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000157static int rsrc_card_parse_daifmt(struct device_node *node,
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000158 struct device_node *codec,
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000159 struct rsrc_card_priv *priv,
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000160 struct snd_soc_dai_link *dai_link,
161 unsigned int *retfmt)
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000162{
163 struct device_node *bitclkmaster = NULL;
164 struct device_node *framemaster = NULL;
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000165 unsigned int daifmt;
166
167 daifmt = snd_soc_of_parse_daifmt(node, NULL,
168 &bitclkmaster, &framemaster);
169 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
170
171 if (!bitclkmaster && !framemaster)
172 return -EINVAL;
173
174 if (codec == bitclkmaster)
175 daifmt |= (codec == framemaster) ?
176 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
177 else
178 daifmt |= (codec == framemaster) ?
179 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
180
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000181 of_node_put(bitclkmaster);
182 of_node_put(framemaster);
183
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000184 *retfmt = daifmt;
185
Kuninori Morimotofe3bd182015-06-15 06:23:30 +0000186 return 0;
187}
188
Kuninori Morimoto04700022015-06-15 06:24:15 +0000189static int rsrc_card_parse_links(struct device_node *np,
190 struct rsrc_card_priv *priv,
191 int idx, bool is_fe)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000192{
Kuninori Morimoto04700022015-06-15 06:24:15 +0000193 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
194 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000195 struct of_phandle_args args;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000196 int ret;
197
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000198 /*
199 * Get node via "sound-dai = <&phandle port>"
200 * it will be used as xxx_of_node on soc_bind_dai_link()
201 */
202 ret = of_parse_phandle_with_args(np, "sound-dai",
203 "#sound-dai-cells", 0, &args);
204 if (ret)
205 return ret;
206
Kuninori Morimoto6dad9752015-12-17 02:48:23 +0000207 /* Parse TDM slot */
208 ret = snd_soc_of_parse_tdm_slot(np,
209 &dai_props->tx_slot_mask,
210 &dai_props->rx_slot_mask,
211 &dai_props->slots,
212 &dai_props->slot_width);
213 if (ret)
214 return ret;
215
Kuninori Morimoto04700022015-06-15 06:24:15 +0000216 if (is_fe) {
217 /* BE is dummy */
218 dai_link->codec_of_node = NULL;
219 dai_link->codec_dai_name = "snd-soc-dummy-dai";
220 dai_link->codec_name = "snd-soc-dummy";
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000221
Kuninori Morimoto04700022015-06-15 06:24:15 +0000222 /* FE settings */
223 dai_link->dynamic = 1;
224 dai_link->dpcm_merged_format = 1;
225 dai_link->cpu_of_node = args.np;
Kuninori Morimoto575f1f92015-12-01 08:33:23 +0000226 ret = snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name);
227 if (ret < 0)
228 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000229
Kuninori Morimoto04700022015-06-15 06:24:15 +0000230 /* set dai_name */
231 snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s",
232 dai_link->cpu_dai_name);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000233
Kuninori Morimoto04700022015-06-15 06:24:15 +0000234 /*
235 * In soc_bind_dai_link() will check cpu name after
236 * of_node matching if dai_link has cpu_dai_name.
237 * but, it will never match if name was created by
238 * fmt_single_name() remove cpu_dai_name if cpu_args
239 * was 0. See:
240 * fmt_single_name()
241 * fmt_multiple_name()
242 */
243 if (!args.args_count)
244 dai_link->cpu_dai_name = NULL;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000245 } else {
Kuninori Morimoto04700022015-06-15 06:24:15 +0000246 struct device *dev = rsrc_priv_to_dev(priv);
247 const struct rsrc_card_of_data *of_data;
248
249 of_data = rsrc_dev_to_of_data(dev);
250
251 /* FE is dummy */
252 dai_link->cpu_of_node = NULL;
253 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
254 dai_link->cpu_name = "snd-soc-dummy";
255
256 /* BE settings */
257 dai_link->no_pcm = 1;
258 dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup;
259 dai_link->codec_of_node = args.np;
Kuninori Morimoto575f1f92015-12-01 08:33:23 +0000260 ret = snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name);
261 if (ret < 0)
262 return ret;
Kuninori Morimoto04700022015-06-15 06:24:15 +0000263
264 /* additional name prefix */
Kuninori Morimotob7419dd2015-07-15 07:08:05 +0000265 if (of_data) {
266 priv->codec_conf.of_node = dai_link->codec_of_node;
267 priv->codec_conf.name_prefix = of_data->prefix;
268 } else {
269 snd_soc_of_parse_audio_prefix(&priv->snd_card,
270 &priv->codec_conf,
271 dai_link->codec_of_node,
272 "audio-prefix");
273 }
Kuninori Morimoto04700022015-06-15 06:24:15 +0000274
275 /* set dai_name */
276 snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s",
277 dai_link->codec_dai_name);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000278 }
279
Kuninori Morimoto04700022015-06-15 06:24:15 +0000280 /* Simple Card assumes platform == cpu */
281 dai_link->platform_of_node = dai_link->cpu_of_node;
282 dai_link->dpcm_playback = 1;
283 dai_link->dpcm_capture = 1;
284 dai_link->name = dai_props->dai_name;
285 dai_link->stream_name = dai_props->dai_name;
286 dai_link->ops = &rsrc_card_ops;
287 dai_link->init = rsrc_card_dai_init;
288
289 return 0;
290}
291
292static int rsrc_card_parse_clk(struct device_node *np,
293 struct rsrc_card_priv *priv,
294 int idx, bool is_fe)
295{
296 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
297 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
298 struct clk *clk;
299 struct device_node *of_np = is_fe ? dai_link->cpu_of_node :
300 dai_link->codec_of_node;
301 u32 val;
302
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000303 /*
304 * Parse dai->sysclk come from "clocks = <&xxx>"
305 * (if system has common clock)
306 * or "system-clock-frequency = <xxx>"
307 * or device's module clock.
308 */
309 if (of_property_read_bool(np, "clocks")) {
310 clk = of_clk_get(np, 0);
Kuninori Morimoto061015f2015-06-15 06:23:52 +0000311 if (IS_ERR(clk))
312 return PTR_ERR(clk);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000313
Kuninori Morimoto04700022015-06-15 06:24:15 +0000314 dai_props->sysclk = clk_get_rate(clk);
315 dai_props->clk = clk;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000316 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
Kuninori Morimoto04700022015-06-15 06:24:15 +0000317 dai_props->sysclk = val;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000318 } else {
Kuninori Morimoto04700022015-06-15 06:24:15 +0000319 clk = of_clk_get(of_np, 0);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000320 if (!IS_ERR(clk))
Kuninori Morimoto04700022015-06-15 06:24:15 +0000321 dai_props->sysclk = clk_get_rate(clk);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000322 }
323
324 return 0;
325}
326
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000327static int rsrc_card_dai_sub_link_of(struct device_node *node,
328 struct device_node *np,
329 struct rsrc_card_priv *priv,
330 int idx, bool is_fe)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000331{
332 struct device *dev = rsrc_priv_to_dev(priv);
Kuninori Morimotoae638b72015-12-17 02:48:58 +0000333 struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx);
Kuninori Morimoto04700022015-06-15 06:24:15 +0000334 struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx);
Kuninori Morimoto04700022015-06-15 06:24:15 +0000335 int ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000336
Kuninori Morimoto04700022015-06-15 06:24:15 +0000337 ret = rsrc_card_parse_links(np, priv, idx, is_fe);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000338 if (ret < 0)
Kuninori Morimoto04700022015-06-15 06:24:15 +0000339 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000340
Kuninori Morimoto04700022015-06-15 06:24:15 +0000341 ret = rsrc_card_parse_clk(np, priv, idx, is_fe);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000342 if (ret < 0)
Kuninori Morimoto04700022015-06-15 06:24:15 +0000343 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000344
Kuninori Morimoto04700022015-06-15 06:24:15 +0000345 dev_dbg(dev, "\t%s / %04x / %d\n",
346 dai_props->dai_name,
Kuninori Morimotoae638b72015-12-17 02:48:58 +0000347 dai_link->dai_fmt,
Kuninori Morimoto04700022015-06-15 06:24:15 +0000348 dai_props->sysclk);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000349
350 return ret;
351}
352
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000353static int rsrc_card_dai_link_of(struct device_node *node,
354 struct rsrc_card_priv *priv)
355{
356 struct snd_soc_dai_link *dai_link;
357 struct device_node *np;
358 unsigned int daifmt = 0;
359 int ret, i;
360 bool is_fe;
361
362 /* find 1st codec */
363 i = 0;
364 for_each_child_of_node(node, np) {
365 dai_link = rsrc_priv_to_link(priv, i);
366
367 if (strcmp(np->name, "codec") == 0) {
368 ret = rsrc_card_parse_daifmt(node, np, priv,
369 dai_link, &daifmt);
370 if (ret < 0)
371 return ret;
372 break;
373 }
374 i++;
375 }
376
377 i = 0;
378 for_each_child_of_node(node, np) {
379 dai_link = rsrc_priv_to_link(priv, i);
380 dai_link->dai_fmt = daifmt;
381
382 is_fe = false;
383 if (strcmp(np->name, "cpu") == 0)
384 is_fe = true;
385
386 ret = rsrc_card_dai_sub_link_of(node, np, priv, i, is_fe);
387 if (ret < 0)
388 return ret;
389 i++;
390 }
391
392 return 0;
393}
394
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000395static int rsrc_card_parse_of(struct device_node *node,
Kuninori Morimoto3c7e64d2015-06-15 06:22:05 +0000396 struct rsrc_card_priv *priv,
397 struct device *dev)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000398{
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000399 const struct rsrc_card_of_data *of_data = rsrc_dev_to_of_data(dev);
Kuninori Morimoto04700022015-06-15 06:24:15 +0000400 struct rsrc_card_dai *props;
Kuninori Morimoto3433bf02015-06-15 06:22:30 +0000401 struct snd_soc_dai_link *links;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000402 int ret;
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000403 int num;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000404
405 if (!node)
406 return -EINVAL;
407
Kuninori Morimoto3433bf02015-06-15 06:22:30 +0000408 num = of_get_child_count(node);
409 props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL);
410 links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL);
411 if (!props || !links)
412 return -ENOMEM;
413
414 priv->dai_props = props;
415 priv->dai_link = links;
416 priv->dai_num = num;
417
Kuninori Morimoto3c7e64d2015-06-15 06:22:05 +0000418 /* Init snd_soc_card */
419 priv->snd_card.owner = THIS_MODULE;
420 priv->snd_card.dev = dev;
421 priv->snd_card.dai_link = priv->dai_link;
Kuninori Morimoto3433bf02015-06-15 06:22:30 +0000422 priv->snd_card.num_links = num;
Kuninori Morimoto3c7e64d2015-06-15 06:22:05 +0000423 priv->snd_card.codec_conf = &priv->codec_conf;
424 priv->snd_card.num_configs = 1;
Kuninori Morimotob7419dd2015-07-15 07:08:05 +0000425
426 if (of_data) {
427 priv->snd_card.of_dapm_routes = of_data->routes;
428 priv->snd_card.num_of_dapm_routes = of_data->num_routes;
429 } else {
430 snd_soc_of_parse_audio_routing(&priv->snd_card,
431 "audio-routing");
432 }
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000433
Kuninori Morimoto04700022015-06-15 06:24:15 +0000434 /* Parse the card name from DT */
435 snd_soc_of_parse_card_name(&priv->snd_card, "card-name");
436
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +0000437 /* sampling rate convert */
438 of_property_read_u32(node, "convert-rate", &priv->convert_rate);
439
440 dev_dbg(dev, "New rsrc-audio-card: %s (%d)\n",
441 priv->snd_card.name ? priv->snd_card.name : "",
442 priv->convert_rate);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000443
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000444 ret = rsrc_card_dai_link_of(node, priv);
445 if (ret < 0)
446 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000447
448 if (!priv->snd_card.name)
449 priv->snd_card.name = priv->snd_card.dai_link->name;
450
451 return 0;
452}
453
454/* Decrease the reference count of the device nodes */
455static int rsrc_card_unref(struct snd_soc_card *card)
456{
457 struct snd_soc_dai_link *dai_link;
458 int num_links;
459
460 for (num_links = 0, dai_link = card->dai_link;
461 num_links < card->num_links;
462 num_links++, dai_link++) {
463 of_node_put(dai_link->cpu_of_node);
464 of_node_put(dai_link->codec_of_node);
465 }
466 return 0;
467}
468
469static int rsrc_card_probe(struct platform_device *pdev)
470{
471 struct rsrc_card_priv *priv;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000472 struct device_node *np = pdev->dev.of_node;
473 struct device *dev = &pdev->dev;
474 int ret;
475
476 /* Allocate the private data */
477 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
478 if (!priv)
479 return -ENOMEM;
480
Kuninori Morimoto3c7e64d2015-06-15 06:22:05 +0000481 ret = rsrc_card_parse_of(np, priv, dev);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000482 if (ret < 0) {
483 if (ret != -EPROBE_DEFER)
484 dev_err(dev, "parse error %d\n", ret);
485 goto err;
486 }
487
488 snd_soc_card_set_drvdata(&priv->snd_card, priv);
489
490 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
491 if (ret >= 0)
492 return ret;
493err:
494 rsrc_card_unref(&priv->snd_card);
495
496 return ret;
497}
498
499static int rsrc_card_remove(struct platform_device *pdev)
500{
501 struct snd_soc_card *card = platform_get_drvdata(pdev);
502
503 return rsrc_card_unref(card);
504}
505
506static struct platform_driver rsrc_card = {
507 .driver = {
508 .name = "renesas-src-audio-card",
509 .of_match_table = rsrc_card_of_match,
510 },
511 .probe = rsrc_card_probe,
512 .remove = rsrc_card_remove,
513};
514
515module_platform_driver(rsrc_card);
516
517MODULE_ALIAS("platform:renesas-src-audio-card");
518MODULE_LICENSE("GPL");
519MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card");
520MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");