Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Renesas Sampling Rate Convert Sound Card for DPCM |
| 3 | * |
| 4 | * Copyright (C) 2015 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * based on ${LINUX}/sound/soc/generic/simple-card.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <sound/jack.h> |
| 21 | #include <sound/soc.h> |
| 22 | #include <sound/soc-dai.h> |
| 23 | |
| 24 | struct rsrc_card_of_data { |
| 25 | const char *prefix; |
| 26 | const struct snd_soc_dapm_route *routes; |
| 27 | int num_routes; |
| 28 | }; |
| 29 | |
| 30 | static const struct snd_soc_dapm_route routes_ssi0_ak4642[] = { |
| 31 | {"ak4642 Playback", NULL, "DAI0 Playback"}, |
| 32 | {"DAI0 Capture", NULL, "ak4642 Capture"}, |
| 33 | }; |
| 34 | |
| 35 | static const struct rsrc_card_of_data routes_of_ssi0_ak4642 = { |
| 36 | .prefix = "ak4642", |
| 37 | .routes = routes_ssi0_ak4642, |
| 38 | .num_routes = ARRAY_SIZE(routes_ssi0_ak4642), |
| 39 | }; |
| 40 | |
| 41 | static const struct of_device_id rsrc_card_of_match[] = { |
| 42 | { .compatible = "renesas,rsrc-card,lager", .data = &routes_of_ssi0_ak4642 }, |
| 43 | { .compatible = "renesas,rsrc-card,koelsch", .data = &routes_of_ssi0_ak4642 }, |
Kuninori Morimoto | b7419dd | 2015-07-15 07:08:05 +0000 | [diff] [blame] | 44 | { .compatible = "renesas,rsrc-card", }, |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 45 | {}, |
| 46 | }; |
| 47 | MODULE_DEVICE_TABLE(of, rsrc_card_of_match); |
| 48 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 49 | #define DAI_NAME_NUM 32 |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 50 | struct rsrc_card_dai { |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 51 | unsigned int sysclk; |
Kuninori Morimoto | 6dad975 | 2015-12-17 02:48:23 +0000 | [diff] [blame] | 52 | unsigned int tx_slot_mask; |
| 53 | unsigned int rx_slot_mask; |
| 54 | int slots; |
| 55 | int slot_width; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 56 | struct clk *clk; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 57 | char dai_name[DAI_NAME_NUM]; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 60 | #define IDX_CPU 0 |
| 61 | #define IDX_CODEC 1 |
| 62 | struct rsrc_card_priv { |
| 63 | struct snd_soc_card snd_card; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 64 | struct snd_soc_codec_conf codec_conf; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 65 | struct rsrc_card_dai *dai_props; |
Kuninori Morimoto | 3433bf0 | 2015-06-15 06:22:30 +0000 | [diff] [blame] | 66 | struct snd_soc_dai_link *dai_link; |
| 67 | int dai_num; |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 68 | u32 convert_rate; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | #define rsrc_priv_to_dev(priv) ((priv)->snd_card.dev) |
Kuninori Morimoto | 8bd616c | 2015-06-15 06:23:08 +0000 | [diff] [blame] | 72 | #define rsrc_priv_to_link(priv, i) ((priv)->snd_card.dai_link + (i)) |
| 73 | #define rsrc_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 74 | #define rsrc_dev_to_of_data(dev) (of_match_device(rsrc_card_of_match, (dev))->data) |
| 75 | |
| 76 | static int rsrc_card_startup(struct snd_pcm_substream *substream) |
| 77 | { |
| 78 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 79 | struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 80 | struct rsrc_card_dai *dai_props = |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 81 | rsrc_priv_to_props(priv, rtd->num); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 82 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 83 | return clk_prepare_enable(dai_props->clk); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void rsrc_card_shutdown(struct snd_pcm_substream *substream) |
| 87 | { |
| 88 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 89 | struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 90 | struct rsrc_card_dai *dai_props = |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 91 | rsrc_priv_to_props(priv, rtd->num); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 92 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 93 | clk_disable_unprepare(dai_props->clk); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static struct snd_soc_ops rsrc_card_ops = { |
| 97 | .startup = rsrc_card_startup, |
| 98 | .shutdown = rsrc_card_shutdown, |
| 99 | }; |
| 100 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 101 | static int rsrc_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 102 | { |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 103 | struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
| 104 | struct snd_soc_dai *dai; |
| 105 | struct snd_soc_dai_link *dai_link; |
| 106 | struct rsrc_card_dai *dai_props; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 107 | int num = rtd->num; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 108 | int ret; |
| 109 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 110 | dai_link = rsrc_priv_to_link(priv, num); |
| 111 | dai_props = rsrc_priv_to_props(priv, num); |
| 112 | dai = dai_link->dynamic ? |
| 113 | rtd->cpu_dai : |
| 114 | rtd->codec_dai; |
| 115 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 116 | if (dai_props->sysclk) { |
| 117 | ret = snd_soc_dai_set_sysclk(dai, 0, dai_props->sysclk, 0); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 118 | if (ret && ret != -ENOTSUPP) { |
| 119 | dev_err(dai->dev, "set_sysclk error\n"); |
| 120 | goto err; |
| 121 | } |
| 122 | } |
| 123 | |
Kuninori Morimoto | 6dad975 | 2015-12-17 02:48:23 +0000 | [diff] [blame] | 124 | if (dai_props->slots) { |
| 125 | ret = snd_soc_dai_set_tdm_slot(dai, |
| 126 | dai_props->tx_slot_mask, |
| 127 | dai_props->rx_slot_mask, |
| 128 | dai_props->slots, |
| 129 | dai_props->slot_width); |
| 130 | if (ret && ret != -ENOTSUPP) { |
| 131 | dev_err(dai->dev, "set_tdm_slot error\n"); |
| 132 | goto err; |
| 133 | } |
| 134 | } |
| 135 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 136 | ret = 0; |
| 137 | |
| 138 | err: |
| 139 | return ret; |
| 140 | } |
| 141 | |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 142 | static int rsrc_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
| 143 | struct snd_pcm_hw_params *params) |
| 144 | { |
| 145 | struct rsrc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
| 146 | struct snd_interval *rate = hw_param_interval(params, |
| 147 | SNDRV_PCM_HW_PARAM_RATE); |
| 148 | |
| 149 | if (!priv->convert_rate) |
| 150 | return 0; |
| 151 | |
| 152 | rate->min = rate->max = priv->convert_rate; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 157 | static int rsrc_card_parse_daifmt(struct device_node *node, |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 158 | struct device_node *codec, |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 159 | struct rsrc_card_priv *priv, |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 160 | struct snd_soc_dai_link *dai_link, |
| 161 | unsigned int *retfmt) |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 162 | { |
| 163 | struct device_node *bitclkmaster = NULL; |
| 164 | struct device_node *framemaster = NULL; |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 165 | unsigned int daifmt; |
| 166 | |
| 167 | daifmt = snd_soc_of_parse_daifmt(node, NULL, |
| 168 | &bitclkmaster, &framemaster); |
| 169 | daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; |
| 170 | |
| 171 | if (!bitclkmaster && !framemaster) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | if (codec == bitclkmaster) |
| 175 | daifmt |= (codec == framemaster) ? |
| 176 | SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS; |
| 177 | else |
| 178 | daifmt |= (codec == framemaster) ? |
| 179 | SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS; |
| 180 | |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 181 | of_node_put(bitclkmaster); |
| 182 | of_node_put(framemaster); |
| 183 | |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 184 | *retfmt = daifmt; |
| 185 | |
Kuninori Morimoto | fe3bd18 | 2015-06-15 06:23:30 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 189 | static int rsrc_card_parse_links(struct device_node *np, |
| 190 | struct rsrc_card_priv *priv, |
| 191 | int idx, bool is_fe) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 192 | { |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 193 | struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx); |
| 194 | struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 195 | struct of_phandle_args args; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 196 | int ret; |
| 197 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Get node via "sound-dai = <&phandle port>" |
| 200 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 201 | */ |
| 202 | ret = of_parse_phandle_with_args(np, "sound-dai", |
| 203 | "#sound-dai-cells", 0, &args); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
Kuninori Morimoto | 6dad975 | 2015-12-17 02:48:23 +0000 | [diff] [blame] | 207 | /* Parse TDM slot */ |
| 208 | ret = snd_soc_of_parse_tdm_slot(np, |
| 209 | &dai_props->tx_slot_mask, |
| 210 | &dai_props->rx_slot_mask, |
| 211 | &dai_props->slots, |
| 212 | &dai_props->slot_width); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 216 | if (is_fe) { |
| 217 | /* BE is dummy */ |
| 218 | dai_link->codec_of_node = NULL; |
| 219 | dai_link->codec_dai_name = "snd-soc-dummy-dai"; |
| 220 | dai_link->codec_name = "snd-soc-dummy"; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 221 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 222 | /* FE settings */ |
| 223 | dai_link->dynamic = 1; |
| 224 | dai_link->dpcm_merged_format = 1; |
| 225 | dai_link->cpu_of_node = args.np; |
Kuninori Morimoto | 575f1f9 | 2015-12-01 08:33:23 +0000 | [diff] [blame] | 226 | ret = snd_soc_of_get_dai_name(np, &dai_link->cpu_dai_name); |
| 227 | if (ret < 0) |
| 228 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 229 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 230 | /* set dai_name */ |
| 231 | snprintf(dai_props->dai_name, DAI_NAME_NUM, "fe.%s", |
| 232 | dai_link->cpu_dai_name); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 233 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 234 | /* |
| 235 | * In soc_bind_dai_link() will check cpu name after |
| 236 | * of_node matching if dai_link has cpu_dai_name. |
| 237 | * but, it will never match if name was created by |
| 238 | * fmt_single_name() remove cpu_dai_name if cpu_args |
| 239 | * was 0. See: |
| 240 | * fmt_single_name() |
| 241 | * fmt_multiple_name() |
| 242 | */ |
| 243 | if (!args.args_count) |
| 244 | dai_link->cpu_dai_name = NULL; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 245 | } else { |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 246 | struct device *dev = rsrc_priv_to_dev(priv); |
| 247 | const struct rsrc_card_of_data *of_data; |
| 248 | |
| 249 | of_data = rsrc_dev_to_of_data(dev); |
| 250 | |
| 251 | /* FE is dummy */ |
| 252 | dai_link->cpu_of_node = NULL; |
| 253 | dai_link->cpu_dai_name = "snd-soc-dummy-dai"; |
| 254 | dai_link->cpu_name = "snd-soc-dummy"; |
| 255 | |
| 256 | /* BE settings */ |
| 257 | dai_link->no_pcm = 1; |
| 258 | dai_link->be_hw_params_fixup = rsrc_card_be_hw_params_fixup; |
| 259 | dai_link->codec_of_node = args.np; |
Kuninori Morimoto | 575f1f9 | 2015-12-01 08:33:23 +0000 | [diff] [blame] | 260 | ret = snd_soc_of_get_dai_name(np, &dai_link->codec_dai_name); |
| 261 | if (ret < 0) |
| 262 | return ret; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 263 | |
| 264 | /* additional name prefix */ |
Kuninori Morimoto | b7419dd | 2015-07-15 07:08:05 +0000 | [diff] [blame] | 265 | if (of_data) { |
| 266 | priv->codec_conf.of_node = dai_link->codec_of_node; |
| 267 | priv->codec_conf.name_prefix = of_data->prefix; |
| 268 | } else { |
| 269 | snd_soc_of_parse_audio_prefix(&priv->snd_card, |
| 270 | &priv->codec_conf, |
| 271 | dai_link->codec_of_node, |
| 272 | "audio-prefix"); |
| 273 | } |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 274 | |
| 275 | /* set dai_name */ |
| 276 | snprintf(dai_props->dai_name, DAI_NAME_NUM, "be.%s", |
| 277 | dai_link->codec_dai_name); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 280 | /* Simple Card assumes platform == cpu */ |
| 281 | dai_link->platform_of_node = dai_link->cpu_of_node; |
| 282 | dai_link->dpcm_playback = 1; |
| 283 | dai_link->dpcm_capture = 1; |
| 284 | dai_link->name = dai_props->dai_name; |
| 285 | dai_link->stream_name = dai_props->dai_name; |
| 286 | dai_link->ops = &rsrc_card_ops; |
| 287 | dai_link->init = rsrc_card_dai_init; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int rsrc_card_parse_clk(struct device_node *np, |
| 293 | struct rsrc_card_priv *priv, |
| 294 | int idx, bool is_fe) |
| 295 | { |
| 296 | struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx); |
| 297 | struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx); |
| 298 | struct clk *clk; |
| 299 | struct device_node *of_np = is_fe ? dai_link->cpu_of_node : |
| 300 | dai_link->codec_of_node; |
| 301 | u32 val; |
| 302 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 303 | /* |
| 304 | * Parse dai->sysclk come from "clocks = <&xxx>" |
| 305 | * (if system has common clock) |
| 306 | * or "system-clock-frequency = <xxx>" |
| 307 | * or device's module clock. |
| 308 | */ |
| 309 | if (of_property_read_bool(np, "clocks")) { |
| 310 | clk = of_clk_get(np, 0); |
Kuninori Morimoto | 061015f | 2015-06-15 06:23:52 +0000 | [diff] [blame] | 311 | if (IS_ERR(clk)) |
| 312 | return PTR_ERR(clk); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 313 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 314 | dai_props->sysclk = clk_get_rate(clk); |
| 315 | dai_props->clk = clk; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 316 | } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) { |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 317 | dai_props->sysclk = val; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 318 | } else { |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 319 | clk = of_clk_get(of_np, 0); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 320 | if (!IS_ERR(clk)) |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 321 | dai_props->sysclk = clk_get_rate(clk); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 327 | static int rsrc_card_dai_sub_link_of(struct device_node *node, |
| 328 | struct device_node *np, |
| 329 | struct rsrc_card_priv *priv, |
| 330 | int idx, bool is_fe) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 331 | { |
| 332 | struct device *dev = rsrc_priv_to_dev(priv); |
Kuninori Morimoto | ae638b7 | 2015-12-17 02:48:58 +0000 | [diff] [blame] | 333 | struct snd_soc_dai_link *dai_link = rsrc_priv_to_link(priv, idx); |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 334 | struct rsrc_card_dai *dai_props = rsrc_priv_to_props(priv, idx); |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 335 | int ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 336 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 337 | ret = rsrc_card_parse_links(np, priv, idx, is_fe); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 338 | if (ret < 0) |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 339 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 340 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 341 | ret = rsrc_card_parse_clk(np, priv, idx, is_fe); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 342 | if (ret < 0) |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 343 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 344 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 345 | dev_dbg(dev, "\t%s / %04x / %d\n", |
| 346 | dai_props->dai_name, |
Kuninori Morimoto | ae638b7 | 2015-12-17 02:48:58 +0000 | [diff] [blame] | 347 | dai_link->dai_fmt, |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 348 | dai_props->sysclk); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 353 | static int rsrc_card_dai_link_of(struct device_node *node, |
| 354 | struct rsrc_card_priv *priv) |
| 355 | { |
| 356 | struct snd_soc_dai_link *dai_link; |
| 357 | struct device_node *np; |
| 358 | unsigned int daifmt = 0; |
| 359 | int ret, i; |
| 360 | bool is_fe; |
| 361 | |
| 362 | /* find 1st codec */ |
| 363 | i = 0; |
| 364 | for_each_child_of_node(node, np) { |
| 365 | dai_link = rsrc_priv_to_link(priv, i); |
| 366 | |
| 367 | if (strcmp(np->name, "codec") == 0) { |
| 368 | ret = rsrc_card_parse_daifmt(node, np, priv, |
| 369 | dai_link, &daifmt); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | break; |
| 373 | } |
| 374 | i++; |
| 375 | } |
| 376 | |
| 377 | i = 0; |
| 378 | for_each_child_of_node(node, np) { |
| 379 | dai_link = rsrc_priv_to_link(priv, i); |
| 380 | dai_link->dai_fmt = daifmt; |
| 381 | |
| 382 | is_fe = false; |
| 383 | if (strcmp(np->name, "cpu") == 0) |
| 384 | is_fe = true; |
| 385 | |
| 386 | ret = rsrc_card_dai_sub_link_of(node, np, priv, i, is_fe); |
| 387 | if (ret < 0) |
| 388 | return ret; |
| 389 | i++; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 395 | static int rsrc_card_parse_of(struct device_node *node, |
Kuninori Morimoto | 3c7e64d | 2015-06-15 06:22:05 +0000 | [diff] [blame] | 396 | struct rsrc_card_priv *priv, |
| 397 | struct device *dev) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 398 | { |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 399 | const struct rsrc_card_of_data *of_data = rsrc_dev_to_of_data(dev); |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 400 | struct rsrc_card_dai *props; |
Kuninori Morimoto | 3433bf0 | 2015-06-15 06:22:30 +0000 | [diff] [blame] | 401 | struct snd_soc_dai_link *links; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 402 | int ret; |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 403 | int num; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 404 | |
| 405 | if (!node) |
| 406 | return -EINVAL; |
| 407 | |
Kuninori Morimoto | 3433bf0 | 2015-06-15 06:22:30 +0000 | [diff] [blame] | 408 | num = of_get_child_count(node); |
| 409 | props = devm_kzalloc(dev, sizeof(*props) * num, GFP_KERNEL); |
| 410 | links = devm_kzalloc(dev, sizeof(*links) * num, GFP_KERNEL); |
| 411 | if (!props || !links) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | priv->dai_props = props; |
| 415 | priv->dai_link = links; |
| 416 | priv->dai_num = num; |
| 417 | |
Kuninori Morimoto | 3c7e64d | 2015-06-15 06:22:05 +0000 | [diff] [blame] | 418 | /* Init snd_soc_card */ |
| 419 | priv->snd_card.owner = THIS_MODULE; |
| 420 | priv->snd_card.dev = dev; |
| 421 | priv->snd_card.dai_link = priv->dai_link; |
Kuninori Morimoto | 3433bf0 | 2015-06-15 06:22:30 +0000 | [diff] [blame] | 422 | priv->snd_card.num_links = num; |
Kuninori Morimoto | 3c7e64d | 2015-06-15 06:22:05 +0000 | [diff] [blame] | 423 | priv->snd_card.codec_conf = &priv->codec_conf; |
| 424 | priv->snd_card.num_configs = 1; |
Kuninori Morimoto | b7419dd | 2015-07-15 07:08:05 +0000 | [diff] [blame] | 425 | |
| 426 | if (of_data) { |
| 427 | priv->snd_card.of_dapm_routes = of_data->routes; |
| 428 | priv->snd_card.num_of_dapm_routes = of_data->num_routes; |
| 429 | } else { |
| 430 | snd_soc_of_parse_audio_routing(&priv->snd_card, |
| 431 | "audio-routing"); |
| 432 | } |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 433 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 434 | /* Parse the card name from DT */ |
| 435 | snd_soc_of_parse_card_name(&priv->snd_card, "card-name"); |
| 436 | |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 437 | /* sampling rate convert */ |
| 438 | of_property_read_u32(node, "convert-rate", &priv->convert_rate); |
| 439 | |
| 440 | dev_dbg(dev, "New rsrc-audio-card: %s (%d)\n", |
| 441 | priv->snd_card.name ? priv->snd_card.name : "", |
| 442 | priv->convert_rate); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 443 | |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 444 | ret = rsrc_card_dai_link_of(node, priv); |
| 445 | if (ret < 0) |
| 446 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 447 | |
| 448 | if (!priv->snd_card.name) |
| 449 | priv->snd_card.name = priv->snd_card.dai_link->name; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* Decrease the reference count of the device nodes */ |
| 455 | static int rsrc_card_unref(struct snd_soc_card *card) |
| 456 | { |
| 457 | struct snd_soc_dai_link *dai_link; |
| 458 | int num_links; |
| 459 | |
| 460 | for (num_links = 0, dai_link = card->dai_link; |
| 461 | num_links < card->num_links; |
| 462 | num_links++, dai_link++) { |
| 463 | of_node_put(dai_link->cpu_of_node); |
| 464 | of_node_put(dai_link->codec_of_node); |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int rsrc_card_probe(struct platform_device *pdev) |
| 470 | { |
| 471 | struct rsrc_card_priv *priv; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 472 | struct device_node *np = pdev->dev.of_node; |
| 473 | struct device *dev = &pdev->dev; |
| 474 | int ret; |
| 475 | |
| 476 | /* Allocate the private data */ |
| 477 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 478 | if (!priv) |
| 479 | return -ENOMEM; |
| 480 | |
Kuninori Morimoto | 3c7e64d | 2015-06-15 06:22:05 +0000 | [diff] [blame] | 481 | ret = rsrc_card_parse_of(np, priv, dev); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 482 | if (ret < 0) { |
| 483 | if (ret != -EPROBE_DEFER) |
| 484 | dev_err(dev, "parse error %d\n", ret); |
| 485 | goto err; |
| 486 | } |
| 487 | |
| 488 | snd_soc_card_set_drvdata(&priv->snd_card, priv); |
| 489 | |
| 490 | ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card); |
| 491 | if (ret >= 0) |
| 492 | return ret; |
| 493 | err: |
| 494 | rsrc_card_unref(&priv->snd_card); |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | static int rsrc_card_remove(struct platform_device *pdev) |
| 500 | { |
| 501 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 502 | |
| 503 | return rsrc_card_unref(card); |
| 504 | } |
| 505 | |
| 506 | static struct platform_driver rsrc_card = { |
| 507 | .driver = { |
| 508 | .name = "renesas-src-audio-card", |
| 509 | .of_match_table = rsrc_card_of_match, |
| 510 | }, |
| 511 | .probe = rsrc_card_probe, |
| 512 | .remove = rsrc_card_remove, |
| 513 | }; |
| 514 | |
| 515 | module_platform_driver(rsrc_card); |
| 516 | |
| 517 | MODULE_ALIAS("platform:renesas-src-audio-card"); |
| 518 | MODULE_LICENSE("GPL"); |
| 519 | MODULE_DESCRIPTION("Renesas Sampling Rate Convert Sound Card"); |
| 520 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |