Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ASoC simple sound card support |
| 3 | * |
| 4 | * Copyright (C) 2012 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 11 | #include <linux/clk.h> |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 12 | #include <linux/device.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 14 | #include <linux/of.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 16 | #include <linux/string.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 17 | #include <sound/simple_card.h> |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 18 | #include <sound/soc-dai.h> |
| 19 | #include <sound/soc.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 20 | |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 21 | struct simple_card_data { |
| 22 | struct snd_soc_card snd_card; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 23 | struct simple_dai_props { |
| 24 | struct asoc_simple_dai cpu_dai; |
| 25 | struct asoc_simple_dai codec_dai; |
| 26 | } *dai_props; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 27 | unsigned int mclk_fs; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 28 | struct snd_soc_dai_link dai_link[]; /* dynamically allocated */ |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 31 | static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream, |
| 32 | struct snd_pcm_hw_params *params) |
| 33 | { |
| 34 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 35 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 36 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 37 | unsigned int mclk; |
| 38 | int ret = 0; |
| 39 | |
| 40 | if (priv->mclk_fs) { |
| 41 | mclk = params_rate(params) * priv->mclk_fs; |
| 42 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 43 | SND_SOC_CLOCK_IN); |
| 44 | } |
| 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | static struct snd_soc_ops asoc_simple_card_ops = { |
| 50 | .hw_params = asoc_simple_card_hw_params, |
| 51 | }; |
| 52 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 53 | static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 54 | struct asoc_simple_dai *set) |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 55 | { |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 56 | int ret; |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 57 | |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 58 | if (set->fmt) { |
| 59 | ret = snd_soc_dai_set_fmt(dai, set->fmt); |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 60 | if (ret && ret != -ENOTSUPP) { |
| 61 | dev_err(dai->dev, "simple-card: set_fmt error\n"); |
| 62 | goto err; |
| 63 | } |
Kuninori Morimoto | e244bb9 | 2013-10-17 22:46:49 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 66 | if (set->sysclk) { |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 67 | ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0); |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 68 | if (ret && ret != -ENOTSUPP) { |
| 69 | dev_err(dai->dev, "simple-card: set_sysclk error\n"); |
| 70 | goto err; |
| 71 | } |
| 72 | } |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 73 | |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 74 | if (set->slots) { |
| 75 | ret = snd_soc_dai_set_tdm_slot(dai, 0, 0, |
| 76 | set->slots, |
| 77 | set->slot_width); |
| 78 | if (ret && ret != -ENOTSUPP) { |
| 79 | dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); |
| 80 | goto err; |
| 81 | } |
| 82 | } |
| 83 | |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 84 | ret = 0; |
| 85 | |
| 86 | err: |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 87 | return ret; |
| 88 | } |
| 89 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 90 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 91 | { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 92 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 93 | struct snd_soc_dai *codec = rtd->codec_dai; |
| 94 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 95 | struct simple_dai_props *dai_props; |
| 96 | int num, ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 97 | |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 98 | num = rtd - rtd->card->rtd; |
| 99 | dai_props = &priv->dai_props[num]; |
| 100 | ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 101 | if (ret < 0) |
| 102 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 103 | |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 104 | ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 105 | if (ret < 0) |
| 106 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 111 | static int |
| 112 | asoc_simple_card_sub_parse_of(struct device_node *np, |
| 113 | struct asoc_simple_dai *dai, |
Xiubo Li | 8ea2134 | 2014-08-18 15:00:15 +0800 | [diff] [blame] | 114 | struct device_node **p_node, |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 115 | const char **name, |
| 116 | int *args_count) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 117 | { |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 118 | struct of_phandle_args args; |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 119 | struct device_node *node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 120 | struct clk *clk; |
Jyri Sarha | c7099eb | 2014-06-13 13:04:36 +0300 | [diff] [blame] | 121 | u32 val; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 122 | int ret; |
| 123 | |
| 124 | /* |
| 125 | * get node via "sound-dai = <&phandle port>" |
| 126 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 127 | */ |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 128 | ret = of_parse_phandle_with_args(np, "sound-dai", |
| 129 | "#sound-dai-cells", 0, &args); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | *p_node = args.np; |
| 134 | |
| 135 | if (args_count) |
| 136 | *args_count = args.args_count; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 137 | |
| 138 | /* get dai->name */ |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 139 | ret = snd_soc_of_get_dai_name(np, name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 140 | if (ret < 0) |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 141 | return ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 142 | |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 143 | /* parse TDM slot */ |
| 144 | ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width); |
| 145 | if (ret) |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 146 | return ret; |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 147 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 148 | /* |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 149 | * dai->sysclk come from |
| 150 | * "clocks = <&xxx>" (if system has common clock) |
| 151 | * or "system-clock-frequency = <xxx>" |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 152 | * or device's module clock. |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 153 | */ |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 154 | if (of_property_read_bool(np, "clocks")) { |
| 155 | clk = of_clk_get(np, 0); |
| 156 | if (IS_ERR(clk)) { |
| 157 | ret = PTR_ERR(clk); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 158 | return ret; |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | dai->sysclk = clk_get_rate(clk); |
Jyri Sarha | c7099eb | 2014-06-13 13:04:36 +0300 | [diff] [blame] | 162 | } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) { |
| 163 | dai->sysclk = val; |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 164 | } else { |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 165 | clk = of_clk_get(node, 0); |
Xiubo Li | e2a19ac | 2014-01-06 12:34:36 +0800 | [diff] [blame] | 166 | if (!IS_ERR(clk)) |
| 167 | dai->sysclk = clk_get_rate(clk); |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 168 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 169 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 170 | return 0; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 173 | static int asoc_simple_card_dai_link_of(struct device_node *node, |
| 174 | struct device *dev, |
| 175 | struct snd_soc_dai_link *dai_link, |
| 176 | struct simple_dai_props *dai_props, |
| 177 | bool is_top_level_node) |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 178 | { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 179 | struct device_node *np = NULL; |
| 180 | struct device_node *bitclkmaster = NULL; |
| 181 | struct device_node *framemaster = NULL; |
| 182 | unsigned int daifmt; |
| 183 | char *name; |
| 184 | char prop[128]; |
| 185 | char *prefix = ""; |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 186 | int ret, cpu_args; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 187 | |
Jyri Sarha | 6487221 | 2014-04-24 19:42:00 +0300 | [diff] [blame] | 188 | if (is_top_level_node) |
| 189 | prefix = "simple-audio-card,"; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 190 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 191 | daifmt = snd_soc_of_parse_daifmt(node, prefix, |
| 192 | &bitclkmaster, &framemaster); |
| 193 | daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; |
| 194 | |
| 195 | snprintf(prop, sizeof(prop), "%scpu", prefix); |
| 196 | np = of_get_child_by_name(node, prop); |
| 197 | if (!np) { |
| 198 | ret = -EINVAL; |
Nicolin Chen | 966b806 | 2014-04-24 19:13:59 +0800 | [diff] [blame] | 199 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 200 | goto dai_link_of_err; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 201 | } |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 202 | |
| 203 | ret = asoc_simple_card_sub_parse_of(np, &dai_props->cpu_dai, |
| 204 | &dai_link->cpu_of_node, |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 205 | &dai_link->cpu_dai_name, |
| 206 | &cpu_args); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 207 | if (ret < 0) |
| 208 | goto dai_link_of_err; |
| 209 | |
| 210 | dai_props->cpu_dai.fmt = daifmt; |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 211 | switch (((np == bitclkmaster) << 4) | (np == framemaster)) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 212 | case 0x11: |
| 213 | dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 214 | break; |
| 215 | case 0x10: |
| 216 | dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 217 | break; |
| 218 | case 0x01: |
| 219 | dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 220 | break; |
| 221 | default: |
| 222 | dai_props->cpu_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | of_node_put(np); |
| 227 | snprintf(prop, sizeof(prop), "%scodec", prefix); |
| 228 | np = of_get_child_by_name(node, prop); |
| 229 | if (!np) { |
| 230 | ret = -EINVAL; |
Nicolin Chen | 966b806 | 2014-04-24 19:13:59 +0800 | [diff] [blame] | 231 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 232 | goto dai_link_of_err; |
| 233 | } |
| 234 | |
| 235 | ret = asoc_simple_card_sub_parse_of(np, &dai_props->codec_dai, |
| 236 | &dai_link->codec_of_node, |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 237 | &dai_link->codec_dai_name, NULL); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 238 | if (ret < 0) |
| 239 | goto dai_link_of_err; |
| 240 | |
| 241 | if (strlen(prefix) && !bitclkmaster && !framemaster) { |
| 242 | /* No dai-link level and master setting was not found from |
| 243 | sound node level, revert back to legacy DT parsing and |
| 244 | take the settings from codec node. */ |
| 245 | dev_dbg(dev, "%s: Revert to legacy daifmt parsing\n", |
| 246 | __func__); |
| 247 | dai_props->cpu_dai.fmt = dai_props->codec_dai.fmt = |
| 248 | snd_soc_of_parse_daifmt(np, NULL, NULL, NULL) | |
| 249 | (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK); |
| 250 | } else { |
| 251 | dai_props->codec_dai.fmt = daifmt; |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 252 | switch (((np == bitclkmaster) << 4) | (np == framemaster)) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 253 | case 0x11: |
| 254 | dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFM; |
| 255 | break; |
| 256 | case 0x10: |
| 257 | dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 258 | break; |
| 259 | case 0x01: |
| 260 | dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 261 | break; |
| 262 | default: |
| 263 | dai_props->codec_dai.fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 269 | ret = -EINVAL; |
| 270 | goto dai_link_of_err; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* simple-card assumes platform == cpu */ |
| 274 | dai_link->platform_of_node = dai_link->cpu_of_node; |
| 275 | |
| 276 | /* Link name is created from CPU/CODEC dai name */ |
| 277 | name = devm_kzalloc(dev, |
| 278 | strlen(dai_link->cpu_dai_name) + |
| 279 | strlen(dai_link->codec_dai_name) + 2, |
| 280 | GFP_KERNEL); |
| 281 | sprintf(name, "%s-%s", dai_link->cpu_dai_name, |
| 282 | dai_link->codec_dai_name); |
| 283 | dai_link->name = dai_link->stream_name = name; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 284 | dai_link->ops = &asoc_simple_card_ops; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 285 | dai_link->init = asoc_simple_card_dai_init; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 286 | |
| 287 | dev_dbg(dev, "\tname : %s\n", dai_link->stream_name); |
| 288 | dev_dbg(dev, "\tcpu : %s / %04x / %d\n", |
| 289 | dai_link->cpu_dai_name, |
| 290 | dai_props->cpu_dai.fmt, |
| 291 | dai_props->cpu_dai.sysclk); |
| 292 | dev_dbg(dev, "\tcodec : %s / %04x / %d\n", |
| 293 | dai_link->codec_dai_name, |
| 294 | dai_props->codec_dai.fmt, |
| 295 | dai_props->codec_dai.sysclk); |
| 296 | |
Kuninori Morimoto | 179949b | 2014-08-27 20:08:06 -0700 | [diff] [blame] | 297 | /* |
| 298 | * soc_bind_dai_link() will check cpu name |
| 299 | * after of_node matching if dai_link has cpu_dai_name. |
| 300 | * but, it will never match if name was created by fmt_single_name() |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 301 | * remove cpu_dai_name if cpu_args was 0. |
Kuninori Morimoto | 179949b | 2014-08-27 20:08:06 -0700 | [diff] [blame] | 302 | * see |
| 303 | * fmt_single_name() |
| 304 | * fmt_multiple_name() |
| 305 | */ |
Kuninori Morimoto | 7c7b9cf | 2014-09-02 04:05:30 -0700 | [diff] [blame^] | 306 | if (!cpu_args) |
| 307 | dai_link->cpu_dai_name = NULL; |
Kuninori Morimoto | 179949b | 2014-08-27 20:08:06 -0700 | [diff] [blame] | 308 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 309 | dai_link_of_err: |
| 310 | if (np) |
| 311 | of_node_put(np); |
| 312 | if (bitclkmaster) |
| 313 | of_node_put(bitclkmaster); |
| 314 | if (framemaster) |
| 315 | of_node_put(framemaster); |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 319 | static int asoc_simple_card_parse_of(struct device_node *node, |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 320 | struct simple_card_data *priv, |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 321 | struct device *dev, |
| 322 | int multi) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 323 | { |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 324 | struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 325 | struct simple_dai_props *dai_props = priv->dai_props; |
Jyri Sarha | c7099eb | 2014-06-13 13:04:36 +0300 | [diff] [blame] | 326 | u32 val; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 327 | int ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 328 | |
Xiubo Li | 2772555 | 2014-01-24 15:43:02 +0800 | [diff] [blame] | 329 | /* parsing the card name from DT */ |
| 330 | snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name"); |
| 331 | |
Xiubo Li | 9d681f5 | 2014-02-08 15:59:53 +0800 | [diff] [blame] | 332 | /* off-codec widgets */ |
| 333 | if (of_property_read_bool(node, "simple-audio-card,widgets")) { |
| 334 | ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card, |
| 335 | "simple-audio-card,widgets"); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | } |
| 339 | |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 340 | /* DAPM routes */ |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 341 | if (of_property_read_bool(node, "simple-audio-card,routing")) { |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 342 | ret = snd_soc_of_parse_audio_routing(&priv->snd_card, |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 343 | "simple-audio-card,routing"); |
Xiubo Li | f87a3e8 | 2014-01-07 09:13:42 +0800 | [diff] [blame] | 344 | if (ret) |
| 345 | return ret; |
| 346 | } |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 347 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 348 | /* Factor to mclk, used in hw_params() */ |
Jyri Sarha | c7099eb | 2014-06-13 13:04:36 +0300 | [diff] [blame] | 349 | ret = of_property_read_u32(node, "simple-audio-card,mclk-fs", &val); |
| 350 | if (ret == 0) |
| 351 | priv->mclk_fs = val; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 352 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 353 | dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ? |
| 354 | priv->snd_card.name : ""); |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 355 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 356 | if (multi) { |
| 357 | struct device_node *np = NULL; |
Kuninori Morimoto | a44a750 | 2014-08-27 20:08:47 -0700 | [diff] [blame] | 358 | int i = 0; |
| 359 | |
| 360 | for_each_child_of_node(node, np) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 361 | dev_dbg(dev, "\tlink %d:\n", i); |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 362 | ret = asoc_simple_card_dai_link_of(np, dev, |
| 363 | dai_link + i, |
| 364 | dai_props + i, |
| 365 | false); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 366 | if (ret < 0) { |
| 367 | of_node_put(np); |
| 368 | return ret; |
| 369 | } |
Kuninori Morimoto | a44a750 | 2014-08-27 20:08:47 -0700 | [diff] [blame] | 370 | i++; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 371 | } |
| 372 | } else { |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 373 | ret = asoc_simple_card_dai_link_of(node, dev, |
| 374 | dai_link, dai_props, true); |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 375 | if (ret < 0) |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 376 | return ret; |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 377 | } |
Xiubo Li | dd41e0c | 2013-12-20 14:39:50 +0800 | [diff] [blame] | 378 | |
Xiubo Li | 2772555 | 2014-01-24 15:43:02 +0800 | [diff] [blame] | 379 | if (!priv->snd_card.name) |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 380 | priv->snd_card.name = priv->snd_card.dai_link->name; |
Kuninori Morimoto | f687d90 | 2014-02-27 18:25:24 -0800 | [diff] [blame] | 381 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 385 | /* update the reference count of the devices nodes at end of probe */ |
| 386 | static int asoc_simple_card_unref(struct platform_device *pdev) |
| 387 | { |
| 388 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 389 | struct snd_soc_dai_link *dai_link; |
| 390 | struct device_node *np; |
| 391 | int num_links; |
| 392 | |
| 393 | for (num_links = 0, dai_link = card->dai_link; |
| 394 | num_links < card->num_links; |
| 395 | num_links++, dai_link++) { |
| 396 | np = (struct device_node *) dai_link->cpu_of_node; |
| 397 | if (np) |
| 398 | of_node_put(np); |
| 399 | np = (struct device_node *) dai_link->codec_of_node; |
| 400 | if (np) |
| 401 | of_node_put(np); |
| 402 | } |
| 403 | return 0; |
| 404 | } |
| 405 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 406 | static int asoc_simple_card_probe(struct platform_device *pdev) |
| 407 | { |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 408 | struct simple_card_data *priv; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 409 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 410 | struct device_node *np = pdev->dev.of_node; |
Kuninori Morimoto | f89983e | 2012-12-25 22:52:33 -0800 | [diff] [blame] | 411 | struct device *dev = &pdev->dev; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 412 | int num_links, multi, ret; |
| 413 | |
| 414 | /* get the number of DAI links */ |
| 415 | if (np && of_get_child_by_name(np, "simple-audio-card,dai-link")) { |
| 416 | num_links = of_get_child_count(np); |
| 417 | multi = 1; |
| 418 | } else { |
| 419 | num_links = 1; |
| 420 | multi = 0; |
| 421 | } |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 422 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 423 | /* allocate the private data and the DAI link array */ |
| 424 | priv = devm_kzalloc(dev, |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 425 | sizeof(*priv) + sizeof(*dai_link) * num_links, |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 426 | GFP_KERNEL); |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 427 | if (!priv) |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 428 | return -ENOMEM; |
| 429 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 430 | /* |
| 431 | * init snd_soc_card |
| 432 | */ |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 433 | priv->snd_card.owner = THIS_MODULE; |
| 434 | priv->snd_card.dev = dev; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 435 | dai_link = priv->dai_link; |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 436 | priv->snd_card.dai_link = dai_link; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 437 | priv->snd_card.num_links = num_links; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 438 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 439 | /* get room for the other properties */ |
| 440 | priv->dai_props = devm_kzalloc(dev, |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 441 | sizeof(*priv->dai_props) * num_links, |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 442 | GFP_KERNEL); |
| 443 | if (!priv->dai_props) |
| 444 | return -ENOMEM; |
| 445 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 446 | if (np && of_device_is_available(np)) { |
| 447 | |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 448 | ret = asoc_simple_card_parse_of(np, priv, dev, multi); |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 449 | if (ret < 0) { |
| 450 | if (ret != -EPROBE_DEFER) |
| 451 | dev_err(dev, "parse error %d\n", ret); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 452 | goto err; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 453 | } |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 454 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 455 | } else { |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 456 | struct asoc_simple_card_info *cinfo; |
| 457 | |
| 458 | cinfo = dev->platform_data; |
| 459 | if (!cinfo) { |
Xiubo Li | 34787d0a | 2014-01-09 17:49:40 +0800 | [diff] [blame] | 460 | dev_err(dev, "no info for asoc-simple-card\n"); |
| 461 | return -EINVAL; |
| 462 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 463 | |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 464 | if (!cinfo->name || |
| 465 | !cinfo->codec_dai.name || |
| 466 | !cinfo->codec || |
| 467 | !cinfo->platform || |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 468 | !cinfo->cpu_dai.name) { |
| 469 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
| 470 | return -EINVAL; |
| 471 | } |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 472 | |
Kuninori Morimoto | 12ffa6f | 2014-03-09 19:37:56 -0700 | [diff] [blame] | 473 | priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 474 | dai_link->name = cinfo->name; |
| 475 | dai_link->stream_name = cinfo->name; |
| 476 | dai_link->platform_name = cinfo->platform; |
| 477 | dai_link->codec_name = cinfo->codec; |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 478 | dai_link->cpu_dai_name = cinfo->cpu_dai.name; |
| 479 | dai_link->codec_dai_name = cinfo->codec_dai.name; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 480 | dai_link->init = asoc_simple_card_dai_init; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 481 | memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai, |
| 482 | sizeof(priv->dai_props->cpu_dai)); |
| 483 | memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai, |
| 484 | sizeof(priv->dai_props->codec_dai)); |
Kuninori Morimoto | 81985bd | 2014-03-02 20:32:43 -0800 | [diff] [blame] | 485 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 486 | priv->dai_props->cpu_dai.fmt |= cinfo->daifmt; |
| 487 | priv->dai_props->codec_dai.fmt |= cinfo->daifmt; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 490 | snd_soc_card_set_drvdata(&priv->snd_card, priv); |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 491 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 492 | ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card); |
| 493 | |
| 494 | err: |
| 495 | asoc_simple_card_unref(pdev); |
| 496 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 499 | static const struct of_device_id asoc_simple_of_match[] = { |
| 500 | { .compatible = "simple-audio-card", }, |
| 501 | {}, |
| 502 | }; |
| 503 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 504 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 505 | static struct platform_driver asoc_simple_card = { |
| 506 | .driver = { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 507 | .name = "asoc-simple-card", |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 508 | .owner = THIS_MODULE, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 509 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 510 | }, |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 511 | .probe = asoc_simple_card_probe, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | module_platform_driver(asoc_simple_card); |
| 515 | |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 516 | MODULE_ALIAS("platform:asoc-simple-card"); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 517 | MODULE_LICENSE("GPL"); |
| 518 | MODULE_DESCRIPTION("ASoC Simple Sound Card"); |
| 519 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |