blob: 48606c63562a7694a83fe7b4d38210908d38ad4e [file] [log] [blame]
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +00001/*
Kuninori Morimoto53e682b2016-08-23 01:34:17 +00002 * ASoC simple SCU sound card support
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +00003 *
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>
Kuninori Morimotod6a4a9a42016-06-30 06:03:13 +000023#include <sound/simple_card_utils.h>
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000024
Kuninori Morimoto6910e862016-10-28 03:37:44 +000025struct simple_card_data {
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000026 struct snd_soc_card snd_card;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000027 struct snd_soc_codec_conf codec_conf;
Kuninori Morimoto303c3be2016-07-11 23:58:50 +000028 struct asoc_simple_dai *dai_props;
Kuninori Morimoto3433bf02015-06-15 06:22:30 +000029 struct snd_soc_dai_link *dai_link;
Kuninori Morimotocd8957f2017-06-15 00:24:28 +000030 struct asoc_simple_card_data adata;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000031};
32
Kuninori Morimotod27f3b42017-03-15 04:44:16 +000033#define simple_priv_to_card(priv) (&(priv)->snd_card)
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000034#define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
Kuninori Morimotod27f3b42017-03-15 04:44:16 +000035#define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
36#define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000037
Kuninori Morimoto5bbf38662016-08-08 06:02:31 +000038#define DAI "sound-dai"
39#define CELL "#sound-dai-cells"
Kuninori Morimoto64df0e62016-08-23 01:34:43 +000040#define PREFIX "simple-audio-card,"
Kuninori Morimoto5bbf38662016-08-08 06:02:31 +000041
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000042static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000043{
44 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto6910e862016-10-28 03:37:44 +000045 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto303c3be2016-07-11 23:58:50 +000046 struct asoc_simple_dai *dai_props =
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000047 simple_priv_to_props(priv, rtd->num);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000048
Kuninori Morimotobb24a3b2017-06-09 00:44:40 +000049 return asoc_simple_card_clk_enable(dai_props);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000050}
51
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000052static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000053{
54 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto6910e862016-10-28 03:37:44 +000055 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto303c3be2016-07-11 23:58:50 +000056 struct asoc_simple_dai *dai_props =
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000057 simple_priv_to_props(priv, rtd->num);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000058
Kuninori Morimotobb24a3b2017-06-09 00:44:40 +000059 asoc_simple_card_clk_disable(dai_props);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000060}
61
Julia Lawall9b6fdef2016-10-15 16:55:49 +020062static const struct snd_soc_ops asoc_simple_card_ops = {
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000063 .startup = asoc_simple_card_startup,
64 .shutdown = asoc_simple_card_shutdown,
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000065};
66
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000067static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000068{
Kuninori Morimoto6910e862016-10-28 03:37:44 +000069 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimoto04700022015-06-15 06:24:15 +000070 struct snd_soc_dai *dai;
71 struct snd_soc_dai_link *dai_link;
Kuninori Morimoto303c3be2016-07-11 23:58:50 +000072 struct asoc_simple_dai *dai_props;
Mengdong Lin1a497982015-11-18 02:34:11 -050073 int num = rtd->num;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000074
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000075 dai_link = simple_priv_to_link(priv, num);
76 dai_props = simple_priv_to_props(priv, num);
Kuninori Morimoto04700022015-06-15 06:24:15 +000077 dai = dai_link->dynamic ?
78 rtd->cpu_dai :
79 rtd->codec_dai;
80
Kuninori Morimoto600ee202016-08-09 05:49:21 +000081 return asoc_simple_card_init_dai(dai, dai_props);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000082}
83
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000084static int asoc_simple_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +000085 struct snd_pcm_hw_params *params)
86{
Kuninori Morimoto6910e862016-10-28 03:37:44 +000087 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +000088
Kuninori Morimotocd8957f2017-06-15 00:24:28 +000089 asoc_simple_card_convert_fixup(&priv->adata, params);
Kuninori Morimotoaf7e2be2015-03-26 04:01:46 +000090
91 return 0;
92}
93
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +000094static int asoc_simple_card_dai_link_of(struct device_node *np,
Kuninori Morimoto6910e862016-10-28 03:37:44 +000095 struct simple_card_data *priv,
Kuninori Morimoto83216f32016-08-25 01:57:30 +000096 unsigned int daifmt,
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000097 int idx, bool is_fe)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +000098{
Kuninori Morimoto53e682b2016-08-23 01:34:17 +000099 struct device *dev = simple_priv_to_dev(priv);
100 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
101 struct asoc_simple_dai *dai_props = simple_priv_to_props(priv, idx);
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000102 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000103 int ret;
104
Kuninori Morimoto04700022015-06-15 06:24:15 +0000105 if (is_fe) {
Kuninori Morimoto27b01082016-08-10 02:21:03 +0000106 int is_single_links = 0;
107
Kuninori Morimoto04700022015-06-15 06:24:15 +0000108 /* BE is dummy */
109 dai_link->codec_of_node = NULL;
110 dai_link->codec_dai_name = "snd-soc-dummy-dai";
111 dai_link->codec_name = "snd-soc-dummy";
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000112
Kuninori Morimoto04700022015-06-15 06:24:15 +0000113 /* FE settings */
114 dai_link->dynamic = 1;
115 dai_link->dpcm_merged_format = 1;
Kuninori Morimoto5bbf38662016-08-08 06:02:31 +0000116
117 ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL,
118 &is_single_links);
119 if (ret)
Kuninori Morimoto575f1f92015-12-01 08:33:23 +0000120 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000121
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000122 ret = asoc_simple_card_parse_clk_cpu(dev, np, dai_link, dai_props);
Kuninori Morimotoc9a235d2016-07-19 02:53:32 +0000123 if (ret < 0)
124 return ret;
125
Kuninori Morimoto8a99a6b2016-07-11 23:58:25 +0000126 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
127 "fe.%s",
128 dai_link->cpu_dai_name);
129 if (ret < 0)
130 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000131
Kuninori Morimoto27b01082016-08-10 02:21:03 +0000132 asoc_simple_card_canonicalize_cpu(dai_link, is_single_links);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000133 } else {
Kuninori Morimoto04700022015-06-15 06:24:15 +0000134 /* FE is dummy */
135 dai_link->cpu_of_node = NULL;
136 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
137 dai_link->cpu_name = "snd-soc-dummy";
138
139 /* BE settings */
140 dai_link->no_pcm = 1;
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000141 dai_link->be_hw_params_fixup = asoc_simple_card_be_hw_params_fixup;
Kuninori Morimoto5bbf38662016-08-08 06:02:31 +0000142
143 ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL);
Kuninori Morimoto575f1f92015-12-01 08:33:23 +0000144 if (ret < 0)
145 return ret;
Kuninori Morimoto04700022015-06-15 06:24:15 +0000146
Kuninori Morimotoe984fd62017-01-23 07:29:42 +0000147 ret = asoc_simple_card_parse_clk_codec(dev, np, dai_link, dai_props);
Kuninori Morimotoc9a235d2016-07-19 02:53:32 +0000148 if (ret < 0)
149 return ret;
150
Kuninori Morimoto8a99a6b2016-07-11 23:58:25 +0000151 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
152 "be.%s",
153 dai_link->codec_dai_name);
154 if (ret < 0)
155 return ret;
156
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000157 snd_soc_of_parse_audio_prefix(card,
Kuninori Morimoto64df0e62016-08-23 01:34:43 +0000158 &priv->codec_conf,
159 dai_link->codec_of_node,
160 PREFIX "prefix");
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000161 }
162
Kuninori Morimoto77b713b2017-06-14 00:35:13 +0000163 ret = asoc_simple_card_of_parse_tdm(np, dai_props);
Kuninori Morimoto9f645422016-08-25 01:58:55 +0000164 if (ret)
165 return ret;
166
Kuninori Morimotoa09f3832016-08-09 05:50:19 +0000167 ret = asoc_simple_card_canonicalize_dailink(dai_link);
168 if (ret < 0)
169 return ret;
170
Kuninori Morimoto83216f32016-08-25 01:57:30 +0000171 dai_link->dai_fmt = daifmt;
Kuninori Morimoto04700022015-06-15 06:24:15 +0000172 dai_link->dpcm_playback = 1;
173 dai_link->dpcm_capture = 1;
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000174 dai_link->ops = &asoc_simple_card_ops;
175 dai_link->init = asoc_simple_card_dai_init;
Kuninori Morimoto04700022015-06-15 06:24:15 +0000176
Kuninori Morimotoc9a235d2016-07-19 02:53:32 +0000177 return 0;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000178}
179
Kuninori Morimoto0d6b3522017-06-07 00:37:30 +0000180static int asoc_simple_card_parse_of(struct simple_card_data *priv)
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000181
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000182{
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000183 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000184 struct device_node *np;
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000185 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimoto0d6b3522017-06-07 00:37:30 +0000186 struct device_node *node = dev->of_node;
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000187 unsigned int daifmt = 0;
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000188 bool is_fe;
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000189 int ret, i;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000190
191 if (!node)
192 return -EINVAL;
193
Daniel Balutad0148eb2017-08-11 17:12:19 +0300194 ret = asoc_simple_card_of_parse_widgets(card, PREFIX);
195 if (ret < 0)
196 return ret;
197
Kuninori Morimotobfe6b582017-06-15 00:25:33 +0000198 ret = asoc_simple_card_of_parse_routing(card, PREFIX, 0);
Kuninori Morimoto64df0e62016-08-23 01:34:43 +0000199 if (ret < 0)
200 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000201
Kuninori Morimotocd8957f2017-06-15 00:24:28 +0000202 asoc_simple_card_parse_convert(dev, PREFIX, &priv->adata);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000203
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000204 /* find 1st codec */
205 np = of_get_child_by_name(node, PREFIX "codec");
206 if (!np)
207 return -ENODEV;
208
209 ret = asoc_simple_card_parse_daifmt(dev, node, np, PREFIX, &daifmt);
Kuninori Morimotoaf998f82015-12-17 02:49:43 +0000210 if (ret < 0)
211 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000212
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000213 i = 0;
214 for_each_child_of_node(node, np) {
215 is_fe = false;
216 if (strcmp(np->name, PREFIX "cpu") == 0)
217 is_fe = true;
218
219 ret = asoc_simple_card_dai_link_of(np, priv, daifmt, i, is_fe);
220 if (ret < 0)
221 return ret;
222 i++;
223 }
224
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000225 ret = asoc_simple_card_parse_card_name(card, PREFIX);
Kuninori Morimoto53ae9182016-07-12 00:00:00 +0000226 if (ret < 0)
227 return ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000228
229 return 0;
230}
231
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000232static int asoc_simple_card_probe(struct platform_device *pdev)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000233{
Kuninori Morimoto6910e862016-10-28 03:37:44 +0000234 struct simple_card_data *priv;
Kuninori Morimoto19359922016-10-28 03:38:00 +0000235 struct snd_soc_dai_link *dai_link;
236 struct asoc_simple_dai *dai_props;
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000237 struct snd_soc_card *card;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000238 struct device *dev = &pdev->dev;
Kuninori Morimoto40b68da2017-03-15 04:43:40 +0000239 struct device_node *np = dev->of_node;
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000240 int num, ret;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000241
242 /* Allocate the private data */
243 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
244 if (!priv)
245 return -ENOMEM;
246
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000247 num = of_get_child_count(np);
248
Kuninori Morimoto19359922016-10-28 03:38:00 +0000249 dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL);
250 dai_link = devm_kzalloc(dev, sizeof(*dai_link) * num, GFP_KERNEL);
251 if (!dai_props || !dai_link)
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000252 return -ENOMEM;
253
Kuninori Morimoto19359922016-10-28 03:38:00 +0000254 priv->dai_props = dai_props;
255 priv->dai_link = dai_link;
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000256
257 /* Init snd_soc_card */
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000258 card = simple_priv_to_card(priv);
259 card->owner = THIS_MODULE;
260 card->dev = dev;
261 card->dai_link = priv->dai_link;
262 card->num_links = num;
263 card->codec_conf = &priv->codec_conf;
264 card->num_configs = 1;
Kuninori Morimoto15a190ff2016-10-28 03:37:26 +0000265
Kuninori Morimoto0d6b3522017-06-07 00:37:30 +0000266 ret = asoc_simple_card_parse_of(priv);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000267 if (ret < 0) {
268 if (ret != -EPROBE_DEFER)
269 dev_err(dev, "parse error %d\n", ret);
270 goto err;
271 }
272
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000273 snd_soc_card_set_drvdata(card, priv);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000274
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000275 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoc73df772017-05-19 00:57:44 +0000276 if (ret < 0)
277 goto err;
278
279 return 0;
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000280err:
Kuninori Morimotod27f3b42017-03-15 04:44:16 +0000281 asoc_simple_card_clean_reference(card);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000282
283 return ret;
284}
285
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000286static int asoc_simple_card_remove(struct platform_device *pdev)
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000287{
288 struct snd_soc_card *card = platform_get_drvdata(pdev);
289
Kuninori Morimoto239486b2016-08-10 02:22:01 +0000290 return asoc_simple_card_clean_reference(card);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000291}
292
Kuninori Morimotof4d707092016-08-25 01:58:31 +0000293static const struct of_device_id asoc_simple_of_match[] = {
294 { .compatible = "renesas,rsrc-card", },
295 { .compatible = "simple-scu-audio-card", },
296 {},
297};
298MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
299
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000300static struct platform_driver asoc_simple_card = {
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000301 .driver = {
Kuninori Morimoto64df0e62016-08-23 01:34:43 +0000302 .name = "simple-scu-audio-card",
Kuninori Morimotob4e01802017-08-22 04:56:06 +0000303 .pm = &snd_soc_pm_ops,
Kuninori Morimotof4d707092016-08-25 01:58:31 +0000304 .of_match_table = asoc_simple_of_match,
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000305 },
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000306 .probe = asoc_simple_card_probe,
307 .remove = asoc_simple_card_remove,
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000308};
309
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000310module_platform_driver(asoc_simple_card);
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000311
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000312MODULE_ALIAS("platform:asoc-simple-scu-card");
Kuninori Morimoto93bc0472016-08-25 01:56:38 +0000313MODULE_LICENSE("GPL v2");
Kuninori Morimoto53e682b2016-08-23 01:34:17 +0000314MODULE_DESCRIPTION("ASoC Simple SCU Sound Card");
Kuninori Morimoto415f1cb2015-03-26 04:01:27 +0000315MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");