Kuninori Morimoto | decd896 | 2018-07-02 06:31:16 +0000 | [diff] [blame] | 1 | // 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 Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/gpio.h> |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 13 | #include <linux/gpio/consumer.h> |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 14 | #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 Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 21 | #include <sound/simple_card_utils.h> |
| 22 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 23 | struct graph_priv { |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 24 | struct snd_soc_card snd_card; |
| 25 | struct graph_dai_props { |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 26 | struct asoc_simple_dai *cpu_dai; |
| 27 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | 8e6746d | 2018-08-31 03:09:05 +0000 | [diff] [blame] | 28 | struct snd_soc_dai_link_component codecs; /* single codec */ |
Kuninori Morimoto | 910fdca | 2019-01-21 09:32:32 +0900 | [diff] [blame] | 29 | struct snd_soc_dai_link_component platforms; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 30 | struct asoc_simple_card_data adata; |
| 31 | struct snd_soc_codec_conf *codec_conf; |
Olivier Moysan | 757652d | 2017-11-09 15:07:58 +0100 | [diff] [blame] | 32 | unsigned int mclk_fs; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 33 | } *dai_props; |
Katsuhiro Suzuki | f6de35c | 2018-06-11 17:32:14 +0900 | [diff] [blame] | 34 | struct asoc_simple_jack hp_jack; |
| 35 | struct asoc_simple_jack mic_jack; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 36 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 37 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 38 | struct snd_soc_codec_conf *codec_conf; |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 39 | struct gpio_desc *pa_gpio; |
| 40 | }; |
| 41 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 42 | struct link_info { |
| 43 | int dais; /* number of dai */ |
| 44 | int link; /* number of link */ |
| 45 | int conf; /* number of codec_conf */ |
| 46 | int cpu; /* turn for CPU / Codec */ |
| 47 | }; |
| 48 | |
Kuninori Morimoto | 64ef081 | 2018-11-22 00:57:40 +0000 | [diff] [blame] | 49 | #define graph_priv_to_card(priv) (&(priv)->snd_card) |
| 50 | #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
| 51 | #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev) |
| 52 | #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i)) |
| 53 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 54 | #define PREFIX "audio-graph-card," |
| 55 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 56 | static int graph_outdrv_event(struct snd_soc_dapm_widget *w, |
| 57 | struct snd_kcontrol *kcontrol, |
| 58 | int event) |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 59 | { |
| 60 | struct snd_soc_dapm_context *dapm = w->dapm; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 61 | struct graph_priv *priv = snd_soc_card_get_drvdata(dapm->card); |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 62 | |
| 63 | switch (event) { |
| 64 | case SND_SOC_DAPM_POST_PMU: |
| 65 | gpiod_set_value_cansleep(priv->pa_gpio, 1); |
| 66 | break; |
| 67 | case SND_SOC_DAPM_PRE_PMD: |
| 68 | gpiod_set_value_cansleep(priv->pa_gpio, 0); |
| 69 | break; |
| 70 | default: |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 77 | static const struct snd_soc_dapm_widget graph_dapm_widgets[] = { |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 78 | SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM, |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 79 | 0, 0, NULL, 0, graph_outdrv_event, |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 80 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 83 | static int graph_startup(struct snd_pcm_substream *substream) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 84 | { |
| 85 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 86 | struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 87 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 88 | int ret; |
| 89 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 90 | ret = asoc_simple_card_clk_enable(dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 91 | if (ret) |
| 92 | return ret; |
| 93 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 94 | ret = asoc_simple_card_clk_enable(dai_props->codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 95 | if (ret) |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 96 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 101 | static void graph_shutdown(struct snd_pcm_substream *substream) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 102 | { |
| 103 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 104 | struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 105 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 106 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 107 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 108 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 109 | asoc_simple_card_clk_disable(dai_props->codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 112 | static int graph_hw_params(struct snd_pcm_substream *substream, |
| 113 | struct snd_pcm_hw_params *params) |
Olivier Moysan | 757652d | 2017-11-09 15:07:58 +0100 | [diff] [blame] | 114 | { |
| 115 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 116 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 117 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 118 | struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Olivier Moysan | 757652d | 2017-11-09 15:07:58 +0100 | [diff] [blame] | 119 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 120 | unsigned int mclk, mclk_fs = 0; |
| 121 | int ret = 0; |
| 122 | |
Kuninori Morimoto | 56eb818 | 2018-12-14 11:32:30 +0900 | [diff] [blame] | 123 | if (dai_props->mclk_fs) |
Olivier Moysan | 757652d | 2017-11-09 15:07:58 +0100 | [diff] [blame] | 124 | mclk_fs = dai_props->mclk_fs; |
| 125 | |
| 126 | if (mclk_fs) { |
| 127 | mclk = params_rate(params) * mclk_fs; |
| 128 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 129 | SND_SOC_CLOCK_IN); |
| 130 | if (ret && ret != -ENOTSUPP) |
| 131 | goto err; |
| 132 | |
| 133 | ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, |
| 134 | SND_SOC_CLOCK_OUT); |
| 135 | if (ret && ret != -ENOTSUPP) |
| 136 | goto err; |
| 137 | } |
| 138 | return 0; |
| 139 | err: |
| 140 | return ret; |
| 141 | } |
| 142 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 143 | static const struct snd_soc_ops graph_ops = { |
| 144 | .startup = graph_startup, |
| 145 | .shutdown = graph_shutdown, |
| 146 | .hw_params = graph_hw_params, |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 149 | static int graph_dai_init(struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 150 | { |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 151 | struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 152 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 153 | int ret = 0; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 154 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 155 | ret = asoc_simple_card_init_dai(rtd->codec_dai, |
| 156 | dai_props->codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 160 | ret = asoc_simple_card_init_dai(rtd->cpu_dai, |
| 161 | dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 168 | static int graph_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
| 169 | struct snd_pcm_hw_params *params) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 170 | { |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 171 | struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 172 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 173 | |
| 174 | asoc_simple_card_convert_fixup(&dai_props->adata, params); |
| 175 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 179 | static void graph_get_conversion(struct device *dev, |
| 180 | struct device_node *ep, |
| 181 | struct asoc_simple_card_data *adata) |
Kuninori Morimoto | 40dfae1 | 2018-12-20 10:45:48 +0900 | [diff] [blame] | 182 | { |
| 183 | struct device_node *top = dev->of_node; |
| 184 | struct device_node *port = of_get_parent(ep); |
| 185 | struct device_node *ports = of_get_parent(port); |
| 186 | struct device_node *node = of_graph_get_port_parent(ep); |
| 187 | |
| 188 | asoc_simple_card_parse_convert(dev, top, NULL, adata); |
| 189 | asoc_simple_card_parse_convert(dev, node, PREFIX, adata); |
| 190 | asoc_simple_card_parse_convert(dev, ports, NULL, adata); |
| 191 | asoc_simple_card_parse_convert(dev, port, NULL, adata); |
| 192 | asoc_simple_card_parse_convert(dev, ep, NULL, adata); |
| 193 | } |
| 194 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 195 | static int graph_dai_link_of_dpcm(struct graph_priv *priv, |
| 196 | struct device_node *cpu_ep, |
| 197 | struct device_node *codec_ep, |
| 198 | struct link_info *li, |
| 199 | int dup_codec) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 200 | { |
| 201 | struct device *dev = graph_priv_to_dev(priv); |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 202 | struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link); |
| 203 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link); |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 204 | struct device_node *top = dev->of_node; |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 205 | struct device_node *ep = li->cpu ? cpu_ep : codec_ep; |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 206 | struct device_node *port; |
| 207 | struct device_node *ports; |
| 208 | struct device_node *node; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 209 | struct asoc_simple_dai *dai; |
Kuninori Morimoto | 66164a4 | 2018-12-14 11:32:43 +0900 | [diff] [blame] | 210 | struct snd_soc_dai_link_component *codecs = dai_link->codecs; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 211 | int ret; |
| 212 | |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 213 | /* Do it all CPU endpoint, and 1st Codec endpoint */ |
| 214 | if (!li->cpu && dup_codec) |
| 215 | return 0; |
| 216 | |
| 217 | port = of_get_parent(ep); |
| 218 | ports = of_get_parent(port); |
| 219 | node = of_graph_get_port_parent(ep); |
| 220 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 221 | li->link++; |
| 222 | |
| 223 | dev_dbg(dev, "link_of DPCM (%pOF)\n", ep); |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 224 | |
Kuninori Morimoto | 56eb818 | 2018-12-14 11:32:30 +0900 | [diff] [blame] | 225 | of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs); |
| 226 | of_property_read_u32(ports, "mclk-fs", &dai_props->mclk_fs); |
| 227 | of_property_read_u32(port, "mclk-fs", &dai_props->mclk_fs); |
| 228 | of_property_read_u32(ep, "mclk-fs", &dai_props->mclk_fs); |
Kuninori Morimoto | 7ced65f | 2018-12-14 11:32:36 +0900 | [diff] [blame] | 229 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 230 | graph_get_conversion(dev, ep, &dai_props->adata); |
Kuninori Morimoto | 7ced65f | 2018-12-14 11:32:36 +0900 | [diff] [blame] | 231 | |
Kuninori Morimoto | 56eb818 | 2018-12-14 11:32:30 +0900 | [diff] [blame] | 232 | of_node_put(ports); |
| 233 | of_node_put(port); |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 234 | of_node_put(node); |
Kuninori Morimoto | 56eb818 | 2018-12-14 11:32:30 +0900 | [diff] [blame] | 235 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 236 | if (li->cpu) { |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 237 | |
| 238 | /* BE is dummy */ |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 239 | codecs->of_node = NULL; |
| 240 | codecs->dai_name = "snd-soc-dummy-dai"; |
| 241 | codecs->name = "snd-soc-dummy"; |
| 242 | |
| 243 | /* FE settings */ |
| 244 | dai_link->dynamic = 1; |
| 245 | dai_link->dpcm_merged_format = 1; |
| 246 | |
| 247 | dai = |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 248 | dai_props->cpu_dai = &priv->dais[li->dais++]; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 249 | |
| 250 | ret = asoc_simple_card_parse_graph_cpu(ep, dai_link); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai); |
| 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
| 258 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 259 | "fe.%s", |
| 260 | dai_link->cpu_dai_name); |
| 261 | if (ret < 0) |
| 262 | return ret; |
| 263 | |
| 264 | /* card->num_links includes Codec */ |
| 265 | asoc_simple_card_canonicalize_cpu(dai_link, |
| 266 | of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1); |
| 267 | } else { |
| 268 | struct snd_soc_codec_conf *cconf; |
| 269 | |
| 270 | /* FE is dummy */ |
| 271 | dai_link->cpu_of_node = NULL; |
| 272 | dai_link->cpu_dai_name = "snd-soc-dummy-dai"; |
| 273 | dai_link->cpu_name = "snd-soc-dummy"; |
| 274 | |
| 275 | /* BE settings */ |
| 276 | dai_link->no_pcm = 1; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 277 | dai_link->be_hw_params_fixup = graph_be_hw_params_fixup; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 278 | |
| 279 | dai = |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 280 | dai_props->codec_dai = &priv->dais[li->dais++]; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 281 | |
| 282 | cconf = |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 283 | dai_props->codec_conf = &priv->codec_conf[li->conf++]; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 284 | |
| 285 | ret = asoc_simple_card_parse_graph_codec(ep, dai_link); |
| 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
| 289 | ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai); |
| 290 | if (ret < 0) |
| 291 | return ret; |
| 292 | |
| 293 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 294 | "be.%s", |
Kuninori Morimoto | 66164a4 | 2018-12-14 11:32:43 +0900 | [diff] [blame] | 295 | codecs->dai_name); |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 296 | if (ret < 0) |
| 297 | return ret; |
| 298 | |
| 299 | /* check "prefix" from top node */ |
Kuninori Morimoto | 66164a4 | 2018-12-14 11:32:43 +0900 | [diff] [blame] | 300 | snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node, |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 301 | "prefix"); |
Kuninori Morimoto | 66164a4 | 2018-12-14 11:32:43 +0900 | [diff] [blame] | 302 | snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node, |
| 303 | PREFIX "prefix"); |
| 304 | snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, |
| 305 | "prefix"); |
| 306 | snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node, |
| 307 | "prefix"); |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Kuninori Morimoto | fe7ed4d | 2019-01-21 16:40:59 +0900 | [diff] [blame^] | 310 | asoc_simple_card_canonicalize_platform(dai_link); |
| 311 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 312 | ret = asoc_simple_card_of_parse_tdm(ep, dai); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 316 | ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep, |
| 317 | NULL, &dai_link->dai_fmt); |
| 318 | if (ret < 0) |
| 319 | return ret; |
| 320 | |
| 321 | dai_link->dpcm_playback = 1; |
| 322 | dai_link->dpcm_capture = 1; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 323 | dai_link->ops = &graph_ops; |
| 324 | dai_link->init = graph_dai_init; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 329 | static int graph_dai_link_of(struct graph_priv *priv, |
| 330 | struct device_node *cpu_ep, |
| 331 | struct device_node *codec_ep, |
| 332 | struct link_info *li) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 333 | { |
| 334 | struct device *dev = graph_priv_to_dev(priv); |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 335 | struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link); |
| 336 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link); |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 337 | struct device_node *top = dev->of_node; |
| 338 | struct device_node *cpu_port; |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 339 | struct device_node *cpu_ports; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 340 | struct device_node *codec_port; |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 341 | struct device_node *codec_ports; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 342 | struct asoc_simple_dai *cpu_dai; |
| 343 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 344 | int ret; |
| 345 | |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 346 | /* Do it only CPU turn */ |
| 347 | if (!li->cpu) |
| 348 | return 0; |
| 349 | |
| 350 | cpu_port = of_get_parent(cpu_ep); |
| 351 | cpu_ports = of_get_parent(cpu_port); |
| 352 | codec_port = of_get_parent(codec_ep); |
| 353 | codec_ports = of_get_parent(codec_port); |
| 354 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 355 | dev_dbg(dev, "link_of (%pOF)\n", cpu_ep); |
| 356 | |
| 357 | li->link++; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 358 | |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 359 | cpu_dai = |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 360 | dai_props->cpu_dai = &priv->dais[li->dais++]; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 361 | codec_dai = |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 362 | dai_props->codec_dai = &priv->dais[li->dais++]; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 363 | |
Kuninori Morimoto | 56eb818 | 2018-12-14 11:32:30 +0900 | [diff] [blame] | 364 | /* Factor to mclk, used in hw_params() */ |
| 365 | of_property_read_u32(top, "mclk-fs", &dai_props->mclk_fs); |
| 366 | of_property_read_u32(cpu_ports, "mclk-fs", &dai_props->mclk_fs); |
| 367 | of_property_read_u32(codec_ports, "mclk-fs", &dai_props->mclk_fs); |
| 368 | of_property_read_u32(cpu_port, "mclk-fs", &dai_props->mclk_fs); |
| 369 | of_property_read_u32(codec_port, "mclk-fs", &dai_props->mclk_fs); |
| 370 | of_property_read_u32(cpu_ep, "mclk-fs", &dai_props->mclk_fs); |
| 371 | of_property_read_u32(codec_ep, "mclk-fs", &dai_props->mclk_fs); |
| 372 | of_node_put(cpu_port); |
| 373 | of_node_put(cpu_ports); |
| 374 | of_node_put(codec_port); |
| 375 | of_node_put(codec_ports); |
| 376 | |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 377 | ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep, |
| 378 | NULL, &dai_link->dai_fmt); |
| 379 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 380 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 381 | |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 382 | ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link); |
| 383 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 384 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 385 | |
| 386 | ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link); |
| 387 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 388 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 389 | |
Kuninori Morimoto | c98907d | 2017-06-14 00:35:30 +0000 | [diff] [blame] | 390 | ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 391 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 392 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 393 | |
Kuninori Morimoto | c98907d | 2017-06-14 00:35:30 +0000 | [diff] [blame] | 394 | ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 395 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 396 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 397 | |
| 398 | ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai); |
| 399 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 400 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 401 | |
| 402 | ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai); |
| 403 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 404 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 405 | |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 406 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 407 | "%s-%s", |
| 408 | dai_link->cpu_dai_name, |
Kuninori Morimoto | 8e6746d | 2018-08-31 03:09:05 +0000 | [diff] [blame] | 409 | dai_link->codecs->dai_name); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 410 | if (ret < 0) |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 411 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 412 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 413 | dai_link->ops = &graph_ops; |
| 414 | dai_link->init = graph_dai_init; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 415 | |
Kuninori Morimoto | fe7ed4d | 2019-01-21 16:40:59 +0900 | [diff] [blame^] | 416 | asoc_simple_card_canonicalize_platform(dai_link); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 417 | asoc_simple_card_canonicalize_cpu(dai_link, |
Kuninori Morimoto | 47ca959 | 2017-06-22 06:21:49 +0000 | [diff] [blame] | 418 | of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 419 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 420 | return 0; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 423 | static int graph_for_each_link(struct graph_priv *priv, |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 424 | struct link_info *li, |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 425 | int (*func_noml)(struct graph_priv *priv, |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 426 | struct device_node *cpu_ep, |
| 427 | struct device_node *codec_ep, |
| 428 | struct link_info *li), |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 429 | int (*func_dpcm)(struct graph_priv *priv, |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 430 | struct device_node *cpu_ep, |
| 431 | struct device_node *codec_ep, |
| 432 | struct link_info *li, int dup_codec)) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 433 | { |
| 434 | struct of_phandle_iterator it; |
| 435 | struct device *dev = graph_priv_to_dev(priv); |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 436 | struct device_node *node = dev->of_node; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 437 | struct device_node *cpu_port; |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 438 | struct device_node *cpu_ep; |
| 439 | struct device_node *codec_ep; |
| 440 | struct device_node *codec_port; |
| 441 | struct device_node *codec_port_old = NULL; |
Kuninori Morimoto | de2949f | 2018-12-20 10:45:54 +0900 | [diff] [blame] | 442 | struct asoc_simple_card_data adata; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 443 | int rc, ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 444 | |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 445 | /* loop for all listed CPU port */ |
| 446 | of_for_each_phandle(&it, rc, node, "dais", NULL, 0) { |
| 447 | cpu_port = it.node; |
| 448 | cpu_ep = NULL; |
| 449 | |
| 450 | /* loop for all CPU endpoint */ |
| 451 | while (1) { |
| 452 | cpu_ep = of_get_next_child(cpu_port, cpu_ep); |
| 453 | if (!cpu_ep) |
| 454 | break; |
| 455 | |
| 456 | /* get codec */ |
| 457 | codec_ep = of_graph_get_remote_endpoint(cpu_ep); |
| 458 | codec_port = of_get_parent(codec_ep); |
| 459 | |
| 460 | of_node_put(codec_ep); |
| 461 | of_node_put(codec_port); |
| 462 | |
| 463 | /* get convert-xxx property */ |
| 464 | memset(&adata, 0, sizeof(adata)); |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 465 | graph_get_conversion(dev, codec_ep, &adata); |
| 466 | graph_get_conversion(dev, cpu_ep, &adata); |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * It is DPCM |
| 470 | * if Codec port has many endpoints, |
| 471 | * or has convert-xxx property |
| 472 | */ |
| 473 | if ((of_get_child_count(codec_port) > 1) || |
| 474 | adata.convert_rate || adata.convert_channels) |
| 475 | ret = func_dpcm(priv, cpu_ep, codec_ep, li, |
| 476 | (codec_port_old == codec_port)); |
| 477 | /* else normal sound */ |
| 478 | else |
| 479 | ret = func_noml(priv, cpu_ep, codec_ep, li); |
| 480 | |
| 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | |
| 484 | codec_port_old = codec_port; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 491 | static int graph_parse_of(struct graph_priv *priv) |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 492 | { |
| 493 | struct snd_soc_card *card = graph_priv_to_card(priv); |
| 494 | struct link_info li; |
| 495 | int ret; |
| 496 | |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 497 | ret = asoc_simple_card_of_parse_widgets(card, NULL); |
| 498 | if (ret < 0) |
| 499 | return ret; |
| 500 | |
Kuninori Morimoto | 33404f3 | 2018-11-21 02:11:13 +0000 | [diff] [blame] | 501 | ret = asoc_simple_card_of_parse_routing(card, NULL); |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 502 | if (ret < 0) |
| 503 | return ret; |
| 504 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 505 | memset(&li, 0, sizeof(li)); |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 506 | for (li.cpu = 1; li.cpu >= 0; li.cpu--) { |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 507 | /* |
| 508 | * Detect all CPU first, and Detect all Codec 2nd. |
| 509 | * |
| 510 | * In Normal sound case, all DAIs are detected |
| 511 | * as "CPU-Codec". |
| 512 | * |
| 513 | * In DPCM sound case, |
| 514 | * all CPUs are detected as "CPU-dummy", and |
| 515 | * all Codecs are detected as "dummy-Codec". |
| 516 | * To avoid random sub-device numbering, |
| 517 | * detect "dummy-Codec" in last; |
| 518 | */ |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 519 | ret = graph_for_each_link(priv, &li, |
| 520 | graph_dai_link_of, |
| 521 | graph_dai_link_of_dpcm); |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 522 | if (ret < 0) |
| 523 | return ret; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return asoc_simple_card_parse_card_name(card, NULL); |
| 527 | } |
| 528 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 529 | static int graph_count_noml(struct graph_priv *priv, |
| 530 | struct device_node *cpu_ep, |
| 531 | struct device_node *codec_ep, |
| 532 | struct link_info *li) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 533 | { |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 534 | struct device *dev = graph_priv_to_dev(priv); |
| 535 | |
| 536 | li->link += 1; /* 1xCPU-Codec */ |
| 537 | li->dais += 2; /* 1xCPU + 1xCodec */ |
| 538 | |
| 539 | dev_dbg(dev, "Count As Normal\n"); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 544 | static int graph_count_dpcm(struct graph_priv *priv, |
| 545 | struct device_node *cpu_ep, |
| 546 | struct device_node *codec_ep, |
| 547 | struct link_info *li, |
| 548 | int dup_codec) |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 549 | { |
| 550 | struct device *dev = graph_priv_to_dev(priv); |
| 551 | |
| 552 | li->link++; /* 1xCPU-dummy */ |
| 553 | li->dais++; /* 1xCPU */ |
| 554 | |
| 555 | if (!dup_codec) { |
| 556 | li->link++; /* 1xdummy-Codec */ |
| 557 | li->conf++; /* 1xdummy-Codec */ |
| 558 | li->dais++; /* 1xCodec */ |
| 559 | } |
| 560 | |
| 561 | dev_dbg(dev, "Count As DPCM\n"); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 566 | static void graph_get_dais_count(struct graph_priv *priv, |
| 567 | struct link_info *li) |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 568 | { |
| 569 | struct device *dev = graph_priv_to_dev(priv); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 570 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 571 | /* |
| 572 | * link_num : number of links. |
| 573 | * CPU-Codec / CPU-dummy / dummy-Codec |
| 574 | * dais_num : number of DAIs |
| 575 | * ccnf_num : number of codec_conf |
| 576 | * same number for "dummy-Codec" |
| 577 | * |
| 578 | * ex1) |
| 579 | * CPU0 --- Codec0 link : 5 |
| 580 | * CPU1 --- Codec1 dais : 7 |
| 581 | * CPU2 -/ ccnf : 1 |
| 582 | * CPU3 --- Codec2 |
| 583 | * |
| 584 | * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec |
| 585 | * => 7 DAIs = 4xCPU + 3xCodec |
| 586 | * => 1 ccnf = 1xdummy-Codec |
| 587 | * |
| 588 | * ex2) |
| 589 | * CPU0 --- Codec0 link : 5 |
| 590 | * CPU1 --- Codec1 dais : 6 |
| 591 | * CPU2 -/ ccnf : 1 |
| 592 | * CPU3 -/ |
| 593 | * |
| 594 | * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec |
| 595 | * => 6 DAIs = 4xCPU + 2xCodec |
| 596 | * => 1 ccnf = 1xdummy-Codec |
| 597 | * |
| 598 | * ex3) |
| 599 | * CPU0 --- Codec0 link : 6 |
| 600 | * CPU1 -/ dais : 6 |
| 601 | * CPU2 --- Codec1 ccnf : 2 |
| 602 | * CPU3 -/ |
| 603 | * |
| 604 | * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec |
| 605 | * => 6 DAIs = 4xCPU + 2xCodec |
| 606 | * => 2 ccnf = 2xdummy-Codec |
Kuninori Morimoto | de2949f | 2018-12-20 10:45:54 +0900 | [diff] [blame] | 607 | * |
| 608 | * ex4) |
| 609 | * CPU0 --- Codec0 (convert-rate) link : 3 |
| 610 | * CPU1 --- Codec1 dais : 4 |
| 611 | * ccnf : 1 |
| 612 | * |
| 613 | * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec |
| 614 | * => 4 DAIs = 2xCPU + 2xCodec |
| 615 | * => 1 ccnf = 1xdummy-Codec |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 616 | */ |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 617 | graph_for_each_link(priv, li, |
| 618 | graph_count_noml, |
| 619 | graph_count_dpcm); |
Kuninori Morimoto | fce9b90 | 2018-12-20 10:46:20 +0900 | [diff] [blame] | 620 | dev_dbg(dev, "link %d, dais %d, ccnf %d\n", |
| 621 | li->link, li->dais, li->conf); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 624 | static int graph_card_probe(struct snd_soc_card *card) |
Katsuhiro Suzuki | f6de35c | 2018-06-11 17:32:14 +0900 | [diff] [blame] | 625 | { |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 626 | struct graph_priv *priv = snd_soc_card_get_drvdata(card); |
Katsuhiro Suzuki | f6de35c | 2018-06-11 17:32:14 +0900 | [diff] [blame] | 627 | int ret; |
| 628 | |
| 629 | ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL); |
| 630 | if (ret < 0) |
| 631 | return ret; |
| 632 | |
| 633 | ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL); |
| 634 | if (ret < 0) |
| 635 | return ret; |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 640 | static int graph_probe(struct platform_device *pdev) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 641 | { |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 642 | struct graph_priv *priv; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 643 | struct snd_soc_dai_link *dai_link; |
| 644 | struct graph_dai_props *dai_props; |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 645 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 646 | struct device *dev = &pdev->dev; |
| 647 | struct snd_soc_card *card; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 648 | struct snd_soc_codec_conf *cconf; |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 649 | struct link_info li; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 650 | int ret, i; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 651 | |
| 652 | /* Allocate the private data and the DAI link array */ |
| 653 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 654 | if (!priv) |
| 655 | return -ENOMEM; |
| 656 | |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 657 | card = graph_priv_to_card(priv); |
| 658 | card->owner = THIS_MODULE; |
| 659 | card->dev = dev; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 660 | card->dapm_widgets = graph_dapm_widgets; |
| 661 | card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets); |
| 662 | card->probe = graph_card_probe; |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 663 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 664 | memset(&li, 0, sizeof(li)); |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 665 | graph_get_dais_count(priv, &li); |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 666 | if (!li.link || !li.dais) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 667 | return -EINVAL; |
| 668 | |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 669 | dai_props = devm_kcalloc(dev, li.link, sizeof(*dai_props), GFP_KERNEL); |
| 670 | dai_link = devm_kcalloc(dev, li.link, sizeof(*dai_link), GFP_KERNEL); |
| 671 | dais = devm_kcalloc(dev, li.dais, sizeof(*dais), GFP_KERNEL); |
| 672 | cconf = devm_kcalloc(dev, li.conf, sizeof(*cconf), GFP_KERNEL); |
Kuninori Morimoto | 0e3460b | 2018-11-30 02:07:48 +0000 | [diff] [blame] | 673 | if (!dai_props || !dai_link || !dais) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 674 | return -ENOMEM; |
| 675 | |
Kuninori Morimoto | 8e6746d | 2018-08-31 03:09:05 +0000 | [diff] [blame] | 676 | /* |
| 677 | * Use snd_soc_dai_link_component instead of legacy style |
| 678 | * It is codec only. but cpu/platform will be supported in the future. |
| 679 | * see |
| 680 | * soc-core.c :: snd_soc_init_multicodec() |
| 681 | */ |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 682 | for (i = 0; i < li.link; i++) { |
Kuninori Morimoto | 8e6746d | 2018-08-31 03:09:05 +0000 | [diff] [blame] | 683 | dai_link[i].codecs = &dai_props[i].codecs; |
| 684 | dai_link[i].num_codecs = 1; |
Kuninori Morimoto | 910fdca | 2019-01-21 09:32:32 +0900 | [diff] [blame] | 685 | dai_link[i].platforms = &dai_props[i].platforms; |
| 686 | dai_link[i].num_platforms = 1; |
Kuninori Morimoto | 8e6746d | 2018-08-31 03:09:05 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Shawn Guo | f986907 | 2017-06-29 21:26:38 +0800 | [diff] [blame] | 689 | priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW); |
| 690 | if (IS_ERR(priv->pa_gpio)) { |
| 691 | ret = PTR_ERR(priv->pa_gpio); |
| 692 | dev_err(dev, "failed to get amplifier gpio: %d\n", ret); |
| 693 | return ret; |
| 694 | } |
| 695 | |
Kuninori Morimoto | dd98fbc | 2018-12-20 10:46:05 +0900 | [diff] [blame] | 696 | priv->dai_props = dai_props; |
| 697 | priv->dai_link = dai_link; |
| 698 | priv->dais = dais; |
| 699 | priv->codec_conf = cconf; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 700 | |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 701 | card->dai_link = dai_link; |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 702 | card->num_links = li.link; |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 703 | card->codec_conf = cconf; |
Kuninori Morimoto | 1e4771a | 2018-12-20 10:45:59 +0900 | [diff] [blame] | 704 | card->num_configs = li.conf; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 705 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 706 | ret = graph_parse_of(priv); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 707 | if (ret < 0) { |
| 708 | if (ret != -EPROBE_DEFER) |
| 709 | dev_err(dev, "parse error %d\n", ret); |
| 710 | goto err; |
| 711 | } |
| 712 | |
| 713 | snd_soc_card_set_drvdata(card, priv); |
| 714 | |
| 715 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | ecea931 | 2017-05-19 00:58:00 +0000 | [diff] [blame] | 716 | if (ret < 0) |
| 717 | goto err; |
| 718 | |
| 719 | return 0; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 720 | err: |
| 721 | asoc_simple_card_clean_reference(card); |
| 722 | |
| 723 | return ret; |
| 724 | } |
| 725 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 726 | static int graph_remove(struct platform_device *pdev) |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 727 | { |
| 728 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 729 | |
| 730 | return asoc_simple_card_clean_reference(card); |
| 731 | } |
| 732 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 733 | static const struct of_device_id graph_of_match[] = { |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 734 | { .compatible = "audio-graph-card", }, |
Kuninori Morimoto | ae3cb57 | 2018-12-14 11:32:25 +0900 | [diff] [blame] | 735 | { .compatible = "audio-graph-scu-card", }, |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 736 | {}, |
| 737 | }; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 738 | MODULE_DEVICE_TABLE(of, graph_of_match); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 739 | |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 740 | static struct platform_driver graph_card = { |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 741 | .driver = { |
| 742 | .name = "asoc-audio-graph-card", |
Kuninori Morimoto | 7b828a3 | 2017-08-22 04:56:44 +0000 | [diff] [blame] | 743 | .pm = &snd_soc_pm_ops, |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 744 | .of_match_table = graph_of_match, |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 745 | }, |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 746 | .probe = graph_probe, |
| 747 | .remove = graph_remove, |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 748 | }; |
Kuninori Morimoto | 97fe6ca | 2018-12-20 10:46:33 +0900 | [diff] [blame] | 749 | module_platform_driver(graph_card); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 750 | |
| 751 | MODULE_ALIAS("platform:asoc-audio-graph-card"); |
| 752 | MODULE_LICENSE("GPL v2"); |
| 753 | MODULE_DESCRIPTION("ASoC Audio Graph Sound Card"); |
| 754 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |