blob: 288df245b2f07e16d42329a8da6cdb50b0a8fcd5 [file] [log] [blame]
Kuninori Morimotodecd8962018-07-02 06:31:16 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// ASoC audio graph sound card support
4//
5// Copyright (C) 2016 Renesas Solutions Corp.
6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7//
8// based on ${LINUX}/sound/soc/generic/simple-card.c
9
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000010#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
Shawn Guof9869072017-06-29 21:26:38 +080013#include <linux/gpio/consumer.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000014#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/of_gpio.h>
18#include <linux/of_graph.h>
19#include <linux/platform_device.h>
20#include <linux/string.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000021#include <sound/simple_card_utils.h>
22
Kuninori Morimotoc2c61602019-04-04 09:52:15 +090023#define DPCM_SELECTABLE 1
24
Kuninori Morimotoae3cb572018-12-14 11:32:25 +090025#define PREFIX "audio-graph-card,"
26
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090027static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
28 struct snd_kcontrol *kcontrol,
29 int event)
Shawn Guof9869072017-06-29 21:26:38 +080030{
31 struct snd_soc_dapm_context *dapm = w->dapm;
Kuninori Morimotoe59289c2019-03-20 13:54:59 +090032 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
Shawn Guof9869072017-06-29 21:26:38 +080033
34 switch (event) {
35 case SND_SOC_DAPM_POST_PMU:
36 gpiod_set_value_cansleep(priv->pa_gpio, 1);
37 break;
38 case SND_SOC_DAPM_PRE_PMD:
39 gpiod_set_value_cansleep(priv->pa_gpio, 0);
40 break;
41 default:
42 return -EINVAL;
43 }
44
45 return 0;
46}
47
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090048static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
Shawn Guof9869072017-06-29 21:26:38 +080049 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090050 0, 0, NULL, 0, graph_outdrv_event,
Shawn Guof9869072017-06-29 21:26:38 +080051 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000052};
53
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090054static const struct snd_soc_ops graph_ops = {
Kuninori Morimotof38df5b2019-03-20 13:55:14 +090055 .startup = asoc_simple_startup,
Kuninori Morimoto686911b2019-03-20 13:55:27 +090056 .shutdown = asoc_simple_shutdown,
Kuninori Morimotof48dcbb2019-03-20 13:55:39 +090057 .hw_params = asoc_simple_hw_params,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000058};
59
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090060static int graph_get_dai_id(struct device_node *ep)
61{
62 struct device_node *node;
63 struct device_node *endpoint;
64 struct of_endpoint info;
65 int i, id;
Kuninori Morimotoec3042a2019-07-11 13:10:45 +090066 const u32 *reg;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090067 int ret;
68
69 /* use driver specified DAI ID if exist */
70 ret = snd_soc_get_dai_id(ep);
71 if (ret != -ENOTSUPP)
72 return ret;
73
74 /* use endpoint/port reg if exist */
75 ret = of_graph_parse_endpoint(ep, &info);
76 if (ret == 0) {
77 /*
78 * Because it will count port/endpoint if it doesn't have "reg".
79 * But, we can't judge whether it has "no reg", or "reg = <0>"
80 * only of_graph_parse_endpoint().
81 * We need to check "reg" property
82 */
83 if (of_get_property(ep, "reg", NULL))
84 return info.id;
85
86 node = of_get_parent(ep);
Wen Yangc152f842019-07-10 15:25:09 +080087 reg = of_get_property(node, "reg", NULL);
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090088 of_node_put(node);
Wen Yangc152f842019-07-10 15:25:09 +080089 if (reg)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090090 return info.port;
91 }
92 node = of_graph_get_port_parent(ep);
93
94 /*
95 * Non HDMI sound case, counting port/endpoint on its DT
96 * is enough. Let's count it.
97 */
98 i = 0;
99 id = -1;
100 for_each_endpoint_of_node(node, endpoint) {
101 if (endpoint == ep)
102 id = i;
103 i++;
104 }
105
106 of_node_put(node);
107
108 if (id < 0)
109 return -ENODEV;
110
111 return id;
112}
113
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900114static int asoc_simple_parse_dai(struct device_node *ep,
115 struct snd_soc_dai_link_component *dlc,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900116 int *is_single_link)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900117{
118 struct device_node *node;
119 struct of_phandle_args args;
120 int ret;
121
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900122 if (!ep)
123 return 0;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900124
125 node = of_graph_get_port_parent(ep);
126
127 /* Get dai->name */
128 args.np = node;
129 args.args[0] = graph_get_dai_id(ep);
130 args.args_count = (of_graph_get_endpoint_count(node) > 1);
131
Kuninori Morimotof1072942019-06-06 13:07:35 +0900132 ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900133 if (ret < 0)
134 return ret;
135
Kuninori Morimotof1072942019-06-06 13:07:35 +0900136 dlc->of_node = node;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900137
138 if (is_single_link)
139 *is_single_link = of_graph_get_endpoint_count(node) == 1;
140
141 return 0;
142}
143
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900144static void graph_parse_convert(struct device *dev,
145 struct device_node *ep,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900146 struct asoc_simple_data *adata)
Kuninori Morimoto40dfae12018-12-20 10:45:48 +0900147{
148 struct device_node *top = dev->of_node;
149 struct device_node *port = of_get_parent(ep);
150 struct device_node *ports = of_get_parent(port);
151 struct device_node *node = of_graph_get_port_parent(ep);
152
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900153 asoc_simple_parse_convert(dev, top, NULL, adata);
154 asoc_simple_parse_convert(dev, node, PREFIX, adata);
155 asoc_simple_parse_convert(dev, ports, NULL, adata);
156 asoc_simple_parse_convert(dev, port, NULL, adata);
157 asoc_simple_parse_convert(dev, ep, NULL, adata);
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900158
159 of_node_put(port);
160 of_node_put(ports);
161 of_node_put(node);
Kuninori Morimoto40dfae12018-12-20 10:45:48 +0900162}
163
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900164static void graph_parse_mclk_fs(struct device_node *top,
165 struct device_node *ep,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900166 struct simple_dai_props *props)
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900167{
168 struct device_node *port = of_get_parent(ep);
169 struct device_node *ports = of_get_parent(port);
170 struct device_node *node = of_graph_get_port_parent(ep);
171
172 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
173 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
174 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
175 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
176
177 of_node_put(port);
178 of_node_put(ports);
179 of_node_put(node);
180}
181
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900182static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900183 struct device_node *cpu_ep,
184 struct device_node *codec_ep,
185 struct link_info *li,
186 int dup_codec)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900187{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900188 struct device *dev = simple_priv_to_dev(priv);
189 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
190 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900191 struct device_node *top = dev->of_node;
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900192 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900193 struct device_node *port;
194 struct device_node *ports;
195 struct device_node *node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900196 struct asoc_simple_dai *dai;
Kuninori Morimotof1072942019-06-06 13:07:35 +0900197 struct snd_soc_dai_link_component *cpus = dai_link->cpus;
Kuninori Morimoto66164a42018-12-14 11:32:43 +0900198 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900199 int ret;
200
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900201 /* Do it all CPU endpoint, and 1st Codec endpoint */
202 if (!li->cpu && dup_codec)
203 return 0;
204
205 port = of_get_parent(ep);
206 ports = of_get_parent(port);
207 node = of_graph_get_port_parent(ep);
208
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900209 li->link++;
210
211 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900212
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900213 if (li->cpu) {
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900214 int is_single_links = 0;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900215
216 /* BE is dummy */
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900217 codecs->of_node = NULL;
218 codecs->dai_name = "snd-soc-dummy-dai";
219 codecs->name = "snd-soc-dummy";
220
221 /* FE settings */
222 dai_link->dynamic = 1;
223 dai_link->dpcm_merged_format = 1;
224
225 dai =
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900226 dai_props->cpu_dai = &priv->dais[li->dais++];
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900227
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900228 ret = asoc_simple_parse_cpu(ep, dai_link, &is_single_links);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900229 if (ret)
Wen Yangaa2e3622019-07-10 15:25:08 +0800230 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900231
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900232 ret = asoc_simple_parse_clk_cpu(dev, ep, dai_link, dai);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900233 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800234 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900235
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900236 ret = asoc_simple_set_dailink_name(dev, dai_link,
237 "fe.%s",
Kuninori Morimotof1072942019-06-06 13:07:35 +0900238 cpus->dai_name);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900239 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800240 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900241
242 /* card->num_links includes Codec */
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900243 asoc_simple_canonicalize_cpu(dai_link, is_single_links);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900244 } else {
245 struct snd_soc_codec_conf *cconf;
246
247 /* FE is dummy */
Kuninori Morimotof1072942019-06-06 13:07:35 +0900248 cpus->of_node = NULL;
249 cpus->dai_name = "snd-soc-dummy-dai";
250 cpus->name = "snd-soc-dummy";
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900251
252 /* BE settings */
253 dai_link->no_pcm = 1;
Kuninori Morimoto629f7542019-03-20 13:56:06 +0900254 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900255
256 dai =
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900257 dai_props->codec_dai = &priv->dais[li->dais++];
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900258
259 cconf =
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900260 dai_props->codec_conf = &priv->codec_conf[li->conf++];
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900261
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900262 ret = asoc_simple_parse_codec(ep, dai_link);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900263 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800264 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900265
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900266 ret = asoc_simple_parse_clk_codec(dev, ep, dai_link, dai);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900267 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800268 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900269
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900270 ret = asoc_simple_set_dailink_name(dev, dai_link,
271 "be.%s",
272 codecs->dai_name);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900273 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800274 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900275
276 /* check "prefix" from top node */
Kuninori Morimoto66164a42018-12-14 11:32:43 +0900277 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900278 "prefix");
Kuninori Morimoto66164a42018-12-14 11:32:43 +0900279 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
280 PREFIX "prefix");
281 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
282 "prefix");
283 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
284 "prefix");
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900285 }
286
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900287 graph_parse_convert(dev, ep, &dai_props->adata);
288 graph_parse_mclk_fs(top, ep, dai_props);
289
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900290 asoc_simple_canonicalize_platform(dai_link);
Kuninori Morimotofe7ed4d2019-01-21 16:40:59 +0900291
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900292 ret = asoc_simple_parse_tdm(ep, dai);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900293 if (ret)
Wen Yangaa2e3622019-07-10 15:25:08 +0800294 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900295
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900296 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
297 NULL, &dai_link->dai_fmt);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900298 if (ret < 0)
Wen Yangaa2e3622019-07-10 15:25:08 +0800299 goto out_put_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900300
301 dai_link->dpcm_playback = 1;
302 dai_link->dpcm_capture = 1;
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900303 dai_link->ops = &graph_ops;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900304 dai_link->init = asoc_simple_dai_init;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900305
Wen Yangaa2e3622019-07-10 15:25:08 +0800306out_put_node:
307 of_node_put(ports);
308 of_node_put(port);
309 of_node_put(node);
310 return ret;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900311}
312
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900313static int graph_dai_link_of(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900314 struct device_node *cpu_ep,
315 struct device_node *codec_ep,
316 struct link_info *li)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000317{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900318 struct device *dev = simple_priv_to_dev(priv);
319 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
320 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900321 struct device_node *top = dev->of_node;
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000322 struct asoc_simple_dai *cpu_dai;
323 struct asoc_simple_dai *codec_dai;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900324 int ret, single_cpu;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000325
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900326 /* Do it only CPU turn */
327 if (!li->cpu)
328 return 0;
329
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900330 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
331
332 li->link++;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900333
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000334 cpu_dai =
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900335 dai_props->cpu_dai = &priv->dais[li->dais++];
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000336 codec_dai =
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900337 dai_props->codec_dai = &priv->dais[li->dais++];
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000338
Kuninori Morimoto56eb8182018-12-14 11:32:30 +0900339 /* Factor to mclk, used in hw_params() */
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900340 graph_parse_mclk_fs(top, cpu_ep, dai_props);
341 graph_parse_mclk_fs(top, codec_ep, dai_props);
Kuninori Morimoto56eb8182018-12-14 11:32:30 +0900342
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900343 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
344 NULL, &dai_link->dai_fmt);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000345 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900346 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000347
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900348 ret = asoc_simple_parse_cpu(cpu_ep, dai_link, &single_cpu);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000349 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900350 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000351
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900352 ret = asoc_simple_parse_codec(codec_ep, dai_link);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000353 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900354 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000355
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900356 ret = asoc_simple_parse_tdm(cpu_ep, cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000357 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900358 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000359
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900360 ret = asoc_simple_parse_tdm(codec_ep, codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000361 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900362 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000363
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900364 ret = asoc_simple_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000365 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900366 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000367
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900368 ret = asoc_simple_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000369 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900370 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000371
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900372 ret = asoc_simple_set_dailink_name(dev, dai_link,
373 "%s-%s",
Kuninori Morimotof1072942019-06-06 13:07:35 +0900374 dai_link->cpus->dai_name,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900375 dai_link->codecs->dai_name);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000376 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900377 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000378
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900379 dai_link->ops = &graph_ops;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900380 dai_link->init = asoc_simple_dai_init;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000381
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900382 asoc_simple_canonicalize_cpu(dai_link, single_cpu);
383 asoc_simple_canonicalize_platform(dai_link);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000384
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900385 return 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000386}
387
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900388static int graph_for_each_link(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900389 struct link_info *li,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900390 int (*func_noml)(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900391 struct device_node *cpu_ep,
392 struct device_node *codec_ep,
393 struct link_info *li),
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900394 int (*func_dpcm)(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900395 struct device_node *cpu_ep,
396 struct device_node *codec_ep,
397 struct link_info *li, int dup_codec))
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000398{
399 struct of_phandle_iterator it;
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900400 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900401 struct device_node *node = dev->of_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900402 struct device_node *cpu_port;
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900403 struct device_node *cpu_ep;
404 struct device_node *codec_ep;
405 struct device_node *codec_port;
406 struct device_node *codec_port_old = NULL;
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900407 struct asoc_simple_data adata;
Kuninori Morimotoc2c61602019-04-04 09:52:15 +0900408 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
Kuninori Morimoto0e3460b2018-11-30 02:07:48 +0000409 int rc, ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000410
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900411 /* loop for all listed CPU port */
412 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
413 cpu_port = it.node;
414 cpu_ep = NULL;
415
416 /* loop for all CPU endpoint */
417 while (1) {
418 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
419 if (!cpu_ep)
420 break;
421
422 /* get codec */
423 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
424 codec_port = of_get_parent(codec_ep);
425
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900426 /* get convert-xxx property */
427 memset(&adata, 0, sizeof(adata));
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900428 graph_parse_convert(dev, codec_ep, &adata);
429 graph_parse_convert(dev, cpu_ep, &adata);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900430
431 /*
432 * It is DPCM
433 * if Codec port has many endpoints,
434 * or has convert-xxx property
435 */
Kuninori Morimotoc2c61602019-04-04 09:52:15 +0900436 if (dpcm_selectable &&
437 ((of_get_child_count(codec_port) > 1) ||
438 adata.convert_rate || adata.convert_channels))
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900439 ret = func_dpcm(priv, cpu_ep, codec_ep, li,
440 (codec_port_old == codec_port));
441 /* else normal sound */
442 else
443 ret = func_noml(priv, cpu_ep, codec_ep, li);
444
Wen Yang1bcc1fd2019-07-04 16:38:50 +0800445 of_node_put(codec_ep);
446 of_node_put(codec_port);
447
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900448 if (ret < 0)
449 return ret;
450
451 codec_port_old = codec_port;
452 }
453 }
454
455 return 0;
456}
457
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900458static int graph_parse_of(struct asoc_simple_priv *priv)
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900459{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900460 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900461 struct link_info li;
462 int ret;
463
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900464 ret = asoc_simple_parse_widgets(card, NULL);
Shawn Guof9869072017-06-29 21:26:38 +0800465 if (ret < 0)
466 return ret;
467
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900468 ret = asoc_simple_parse_routing(card, NULL);
Shawn Guof9869072017-06-29 21:26:38 +0800469 if (ret < 0)
470 return ret;
471
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900472 memset(&li, 0, sizeof(li));
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900473 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900474 /*
475 * Detect all CPU first, and Detect all Codec 2nd.
476 *
477 * In Normal sound case, all DAIs are detected
478 * as "CPU-Codec".
479 *
480 * In DPCM sound case,
481 * all CPUs are detected as "CPU-dummy", and
482 * all Codecs are detected as "dummy-Codec".
483 * To avoid random sub-device numbering,
484 * detect "dummy-Codec" in last;
485 */
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900486 ret = graph_for_each_link(priv, &li,
487 graph_dai_link_of,
488 graph_dai_link_of_dpcm);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900489 if (ret < 0)
490 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000491 }
492
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900493 return asoc_simple_parse_card_name(card, NULL);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000494}
495
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900496static int graph_count_noml(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900497 struct device_node *cpu_ep,
498 struct device_node *codec_ep,
499 struct link_info *li)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000500{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900501 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900502
503 li->link += 1; /* 1xCPU-Codec */
504 li->dais += 2; /* 1xCPU + 1xCodec */
505
506 dev_dbg(dev, "Count As Normal\n");
507
508 return 0;
509}
510
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900511static int graph_count_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900512 struct device_node *cpu_ep,
513 struct device_node *codec_ep,
514 struct link_info *li,
515 int dup_codec)
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900516{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900517 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900518
519 li->link++; /* 1xCPU-dummy */
520 li->dais++; /* 1xCPU */
521
522 if (!dup_codec) {
523 li->link++; /* 1xdummy-Codec */
524 li->conf++; /* 1xdummy-Codec */
525 li->dais++; /* 1xCodec */
526 }
527
528 dev_dbg(dev, "Count As DPCM\n");
529
530 return 0;
531}
532
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900533static void graph_get_dais_count(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900534 struct link_info *li)
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900535{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900536 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000537
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900538 /*
539 * link_num : number of links.
540 * CPU-Codec / CPU-dummy / dummy-Codec
541 * dais_num : number of DAIs
542 * ccnf_num : number of codec_conf
543 * same number for "dummy-Codec"
544 *
545 * ex1)
546 * CPU0 --- Codec0 link : 5
547 * CPU1 --- Codec1 dais : 7
548 * CPU2 -/ ccnf : 1
549 * CPU3 --- Codec2
550 *
551 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
552 * => 7 DAIs = 4xCPU + 3xCodec
553 * => 1 ccnf = 1xdummy-Codec
554 *
555 * ex2)
556 * CPU0 --- Codec0 link : 5
557 * CPU1 --- Codec1 dais : 6
558 * CPU2 -/ ccnf : 1
559 * CPU3 -/
560 *
561 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
562 * => 6 DAIs = 4xCPU + 2xCodec
563 * => 1 ccnf = 1xdummy-Codec
564 *
565 * ex3)
566 * CPU0 --- Codec0 link : 6
567 * CPU1 -/ dais : 6
568 * CPU2 --- Codec1 ccnf : 2
569 * CPU3 -/
570 *
571 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
572 * => 6 DAIs = 4xCPU + 2xCodec
573 * => 2 ccnf = 2xdummy-Codec
Kuninori Morimotode2949f2018-12-20 10:45:54 +0900574 *
575 * ex4)
576 * CPU0 --- Codec0 (convert-rate) link : 3
577 * CPU1 --- Codec1 dais : 4
578 * ccnf : 1
579 *
580 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
581 * => 4 DAIs = 2xCPU + 2xCodec
582 * => 1 ccnf = 1xdummy-Codec
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900583 */
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900584 graph_for_each_link(priv, li,
585 graph_count_noml,
586 graph_count_dpcm);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900587 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
588 li->link, li->dais, li->conf);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000589}
590
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900591static int graph_card_probe(struct snd_soc_card *card)
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900592{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900593 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900594 int ret;
595
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900596 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900597 if (ret < 0)
598 return ret;
599
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900600 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
Katsuhiro Suzukif6de35c2018-06-11 17:32:14 +0900601 if (ret < 0)
602 return ret;
603
604 return 0;
605}
606
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900607static int graph_probe(struct platform_device *pdev)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000608{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900609 struct asoc_simple_priv *priv;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000610 struct device *dev = &pdev->dev;
611 struct snd_soc_card *card;
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900612 struct link_info li;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900613 int ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000614
615 /* Allocate the private data and the DAI link array */
616 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
617 if (!priv)
618 return -ENOMEM;
619
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900620 card = simple_priv_to_card(priv);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900621 card->owner = THIS_MODULE;
622 card->dev = dev;
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900623 card->dapm_widgets = graph_dapm_widgets;
624 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
625 card->probe = graph_card_probe;
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900626
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900627 memset(&li, 0, sizeof(li));
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900628 graph_get_dais_count(priv, &li);
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900629 if (!li.link || !li.dais)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000630 return -EINVAL;
631
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900632 ret = asoc_simple_init_priv(priv, &li);
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900633 if (ret < 0)
634 return ret;
Kuninori Morimoto8e6746d2018-08-31 03:09:05 +0000635
Shawn Guof9869072017-06-29 21:26:38 +0800636 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
637 if (IS_ERR(priv->pa_gpio)) {
638 ret = PTR_ERR(priv->pa_gpio);
639 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
640 return ret;
641 }
642
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900643 ret = graph_parse_of(priv);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000644 if (ret < 0) {
645 if (ret != -EPROBE_DEFER)
646 dev_err(dev, "parse error %d\n", ret);
647 goto err;
648 }
649
650 snd_soc_card_set_drvdata(card, priv);
651
Kuninori Morimoto0580dde2019-03-20 13:54:42 +0900652 asoc_simple_debug_info(priv);
653
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000654 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoecea9312017-05-19 00:58:00 +0000655 if (ret < 0)
656 goto err;
657
658 return 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000659err:
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900660 asoc_simple_clean_reference(card);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000661
662 return ret;
663}
664
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900665static int graph_remove(struct platform_device *pdev)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000666{
667 struct snd_soc_card *card = platform_get_drvdata(pdev);
668
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900669 return asoc_simple_clean_reference(card);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000670}
671
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900672static const struct of_device_id graph_of_match[] = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000673 { .compatible = "audio-graph-card", },
Kuninori Morimotoc2c61602019-04-04 09:52:15 +0900674 { .compatible = "audio-graph-scu-card",
675 .data = (void *)DPCM_SELECTABLE },
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000676 {},
677};
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900678MODULE_DEVICE_TABLE(of, graph_of_match);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000679
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900680static struct platform_driver graph_card = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000681 .driver = {
682 .name = "asoc-audio-graph-card",
Kuninori Morimoto7b828a32017-08-22 04:56:44 +0000683 .pm = &snd_soc_pm_ops,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900684 .of_match_table = graph_of_match,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000685 },
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900686 .probe = graph_probe,
687 .remove = graph_remove,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000688};
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900689module_platform_driver(graph_card);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000690
691MODULE_ALIAS("platform:asoc-audio-graph-card");
692MODULE_LICENSE("GPL v2");
693MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
694MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");