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; |
| 27 | struct snd_soc_dai_link dai_link[]; /* dynamically allocated */ |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 30 | static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 31 | struct asoc_simple_dai *set) |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 32 | { |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 33 | int ret; |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 34 | |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 35 | if (set->fmt) { |
| 36 | ret = snd_soc_dai_set_fmt(dai, set->fmt); |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 37 | if (ret && ret != -ENOTSUPP) { |
| 38 | dev_err(dai->dev, "simple-card: set_fmt error\n"); |
| 39 | goto err; |
| 40 | } |
Kuninori Morimoto | e244bb9 | 2013-10-17 22:46:49 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 43 | if (set->sysclk) { |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 44 | ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0); |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 45 | if (ret && ret != -ENOTSUPP) { |
| 46 | dev_err(dai->dev, "simple-card: set_sysclk error\n"); |
| 47 | goto err; |
| 48 | } |
| 49 | } |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 50 | |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 51 | if (set->slots) { |
| 52 | ret = snd_soc_dai_set_tdm_slot(dai, 0, 0, |
| 53 | set->slots, |
| 54 | set->slot_width); |
| 55 | if (ret && ret != -ENOTSUPP) { |
| 56 | dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); |
| 57 | goto err; |
| 58 | } |
| 59 | } |
| 60 | |
Xiubo Li | 4763ebe | 2014-01-24 15:43:00 +0800 | [diff] [blame] | 61 | ret = 0; |
| 62 | |
| 63 | err: |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 67 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 68 | { |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 69 | struct simple_card_data *priv = |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 70 | snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 71 | struct snd_soc_dai *codec = rtd->codec_dai; |
| 72 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 73 | int ret; |
| 74 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 75 | ret = __asoc_simple_card_dai_init(codec, &priv->dai_props->codec_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 76 | if (ret < 0) |
| 77 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 78 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 79 | ret = __asoc_simple_card_dai_init(cpu, &priv->dai_props->cpu_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 80 | if (ret < 0) |
| 81 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 86 | static int |
| 87 | asoc_simple_card_sub_parse_of(struct device_node *np, |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 88 | unsigned int daifmt, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 89 | struct asoc_simple_dai *dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 90 | const struct device_node **p_node, |
| 91 | const char **name) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 92 | { |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 93 | struct device_node *node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 94 | struct clk *clk; |
| 95 | int ret; |
| 96 | |
| 97 | /* |
| 98 | * get node via "sound-dai = <&phandle port>" |
| 99 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 100 | */ |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 101 | node = of_parse_phandle(np, "sound-dai", 0); |
| 102 | if (!node) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 103 | return -ENODEV; |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 104 | *p_node = node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 105 | |
| 106 | /* get dai->name */ |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 107 | ret = snd_soc_of_get_dai_name(np, name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 108 | if (ret < 0) |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 109 | return ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 110 | |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 111 | /* parse TDM slot */ |
| 112 | ret = snd_soc_of_parse_tdm_slot(np, &dai->slots, &dai->slot_width); |
| 113 | if (ret) |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 114 | return ret; |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 115 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 116 | /* |
| 117 | * bitclock-inversion, frame-inversion |
| 118 | * bitclock-master, frame-master |
| 119 | * and specific "format" if it has |
| 120 | */ |
| 121 | dai->fmt = snd_soc_of_parse_daifmt(np, NULL); |
Xiubo Li | 30d0341 | 2014-01-24 15:43:01 +0800 | [diff] [blame] | 122 | dai->fmt |= daifmt; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * dai->sysclk come from |
| 126 | * "clocks = <&xxx>" (if system has common clock) |
| 127 | * or "system-clock-frequency = <xxx>" |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 128 | * or device's module clock. |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 129 | */ |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 130 | if (of_property_read_bool(np, "clocks")) { |
| 131 | clk = of_clk_get(np, 0); |
| 132 | if (IS_ERR(clk)) { |
| 133 | ret = PTR_ERR(clk); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 134 | return ret; |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | dai->sysclk = clk_get_rate(clk); |
| 138 | } else if (of_property_read_bool(np, "system-clock-frequency")) { |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 139 | of_property_read_u32(np, |
| 140 | "system-clock-frequency", |
| 141 | &dai->sysclk); |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 142 | } else { |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 143 | clk = of_clk_get(node, 0); |
Xiubo Li | e2a19ac | 2014-01-06 12:34:36 +0800 | [diff] [blame] | 144 | if (!IS_ERR(clk)) |
| 145 | dai->sysclk = clk_get_rate(clk); |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 146 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 147 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 148 | return 0; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int asoc_simple_card_parse_of(struct device_node *node, |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 152 | struct simple_card_data *priv, |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 153 | struct device *dev) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 154 | { |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 155 | struct snd_soc_dai_link *dai_link = priv->snd_card.dai_link; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 156 | struct asoc_simple_dai *codec_dai = &priv->dai_props->codec_dai; |
| 157 | struct asoc_simple_dai *cpu_dai = &priv->dai_props->cpu_dai; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 158 | struct device_node *np; |
| 159 | char *name; |
Jean-Francois Moine | c56c4d7 | 2014-03-15 11:32:42 +0100 | [diff] [blame] | 160 | unsigned int daifmt; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 161 | int ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 162 | |
Xiubo Li | 2772555 | 2014-01-24 15:43:02 +0800 | [diff] [blame] | 163 | /* parsing the card name from DT */ |
| 164 | snd_soc_of_parse_card_name(&priv->snd_card, "simple-audio-card,name"); |
| 165 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 166 | /* get CPU/CODEC common format via simple-audio-card,format */ |
Jean-Francois Moine | c56c4d7 | 2014-03-15 11:32:42 +0100 | [diff] [blame] | 167 | daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") & |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 168 | (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK); |
| 169 | |
Xiubo Li | 9d681f5 | 2014-02-08 15:59:53 +0800 | [diff] [blame] | 170 | /* off-codec widgets */ |
| 171 | if (of_property_read_bool(node, "simple-audio-card,widgets")) { |
| 172 | ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card, |
| 173 | "simple-audio-card,widgets"); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | } |
| 177 | |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 178 | /* DAPM routes */ |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 179 | if (of_property_read_bool(node, "simple-audio-card,routing")) { |
Jean-Francois Moine | b367a32 | 2014-01-15 16:52:00 +0100 | [diff] [blame] | 180 | ret = snd_soc_of_parse_audio_routing(&priv->snd_card, |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 181 | "simple-audio-card,routing"); |
Xiubo Li | f87a3e8 | 2014-01-07 09:13:42 +0800 | [diff] [blame] | 182 | if (ret) |
| 183 | return ret; |
| 184 | } |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 185 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 186 | /* CPU sub-node */ |
| 187 | ret = -EINVAL; |
| 188 | np = of_get_child_by_name(node, "simple-audio-card,cpu"); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 189 | if (np) { |
Jean-Francois Moine | c56c4d7 | 2014-03-15 11:32:42 +0100 | [diff] [blame] | 190 | ret = asoc_simple_card_sub_parse_of(np, daifmt, |
Nicolin Chen | 46c39ca | 2014-03-12 11:02:11 +0800 | [diff] [blame] | 191 | cpu_dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 192 | &dai_link->cpu_of_node, |
| 193 | &dai_link->cpu_dai_name); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 194 | of_node_put(np); |
| 195 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 196 | if (ret < 0) |
| 197 | return ret; |
| 198 | |
| 199 | /* CODEC sub-node */ |
| 200 | ret = -EINVAL; |
| 201 | np = of_get_child_by_name(node, "simple-audio-card,codec"); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 202 | if (np) { |
Jean-Francois Moine | c56c4d7 | 2014-03-15 11:32:42 +0100 | [diff] [blame] | 203 | ret = asoc_simple_card_sub_parse_of(np, daifmt, |
Nicolin Chen | 46c39ca | 2014-03-12 11:02:11 +0800 | [diff] [blame] | 204 | codec_dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 205 | &dai_link->codec_of_node, |
| 206 | &dai_link->codec_dai_name); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 207 | of_node_put(np); |
| 208 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 209 | if (ret < 0) |
| 210 | return ret; |
| 211 | |
Nicolin Chen | 46c39ca | 2014-03-12 11:02:11 +0800 | [diff] [blame] | 212 | /* |
| 213 | * overwrite cpu_dai->fmt as its DAIFMT_MASTER bit is based on CODEC |
| 214 | * while the other bits should be identical unless buggy SW/HW design. |
| 215 | */ |
| 216 | cpu_dai->fmt = codec_dai->fmt; |
| 217 | |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 218 | if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) |
Xiubo Li | dd41e0c | 2013-12-20 14:39:50 +0800 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 221 | /* card name is created from CPU/CODEC dai name */ |
| 222 | name = devm_kzalloc(dev, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 223 | strlen(dai_link->cpu_dai_name) + |
| 224 | strlen(dai_link->codec_dai_name) + 2, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 225 | GFP_KERNEL); |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 226 | sprintf(name, "%s-%s", dai_link->cpu_dai_name, |
| 227 | dai_link->codec_dai_name); |
Xiubo Li | 2772555 | 2014-01-24 15:43:02 +0800 | [diff] [blame] | 228 | if (!priv->snd_card.name) |
| 229 | priv->snd_card.name = name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 230 | dai_link->name = dai_link->stream_name = name; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 231 | |
| 232 | /* simple-card assumes platform == cpu */ |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 233 | dai_link->platform_of_node = dai_link->cpu_of_node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 234 | |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 235 | dev_dbg(dev, "card-name : %s\n", name); |
Jean-Francois Moine | c56c4d7 | 2014-03-15 11:32:42 +0100 | [diff] [blame] | 236 | dev_dbg(dev, "platform : %04x\n", daifmt); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 237 | dev_dbg(dev, "cpu : %s / %04x / %d\n", |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 238 | dai_link->cpu_dai_name, |
Nicolin Chen | 46c39ca | 2014-03-12 11:02:11 +0800 | [diff] [blame] | 239 | cpu_dai->fmt, |
| 240 | cpu_dai->sysclk); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 241 | dev_dbg(dev, "codec : %s / %04x / %d\n", |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 242 | dai_link->codec_dai_name, |
Nicolin Chen | 46c39ca | 2014-03-12 11:02:11 +0800 | [diff] [blame] | 243 | codec_dai->fmt, |
| 244 | codec_dai->sysclk); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 245 | |
Kuninori Morimoto | f687d90 | 2014-02-27 18:25:24 -0800 | [diff] [blame] | 246 | /* |
| 247 | * soc_bind_dai_link() will check cpu name |
| 248 | * after of_node matching if dai_link has cpu_dai_name. |
| 249 | * but, it will never match if name was created by fmt_single_name() |
| 250 | * remove cpu_dai_name to escape name matching. |
| 251 | * see |
| 252 | * fmt_single_name() |
| 253 | * fmt_multiple_name() |
| 254 | */ |
| 255 | dai_link->cpu_dai_name = NULL; |
| 256 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 260 | /* update the reference count of the devices nodes at end of probe */ |
| 261 | static int asoc_simple_card_unref(struct platform_device *pdev) |
| 262 | { |
| 263 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 264 | struct snd_soc_dai_link *dai_link; |
| 265 | struct device_node *np; |
| 266 | int num_links; |
| 267 | |
| 268 | for (num_links = 0, dai_link = card->dai_link; |
| 269 | num_links < card->num_links; |
| 270 | num_links++, dai_link++) { |
| 271 | np = (struct device_node *) dai_link->cpu_of_node; |
| 272 | if (np) |
| 273 | of_node_put(np); |
| 274 | np = (struct device_node *) dai_link->codec_of_node; |
| 275 | if (np) |
| 276 | of_node_put(np); |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 281 | static int asoc_simple_card_probe(struct platform_device *pdev) |
| 282 | { |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 283 | struct simple_card_data *priv; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 284 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 285 | struct device_node *np = pdev->dev.of_node; |
Kuninori Morimoto | f89983e | 2012-12-25 22:52:33 -0800 | [diff] [blame] | 286 | struct device *dev = &pdev->dev; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 287 | int ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 288 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 289 | /* allocate the private data and the DAI link array */ |
| 290 | priv = devm_kzalloc(dev, |
| 291 | sizeof(*priv) + sizeof(*dai_link), |
| 292 | GFP_KERNEL); |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 293 | if (!priv) |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 294 | return -ENOMEM; |
| 295 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 296 | /* |
| 297 | * init snd_soc_card |
| 298 | */ |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 299 | priv->snd_card.owner = THIS_MODULE; |
| 300 | priv->snd_card.dev = dev; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 301 | dai_link = priv->dai_link; |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 302 | priv->snd_card.dai_link = dai_link; |
| 303 | priv->snd_card.num_links = 1; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 304 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 305 | /* get room for the other properties */ |
| 306 | priv->dai_props = devm_kzalloc(dev, |
| 307 | sizeof(*priv->dai_props), |
| 308 | GFP_KERNEL); |
| 309 | if (!priv->dai_props) |
| 310 | return -ENOMEM; |
| 311 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 312 | if (np && of_device_is_available(np)) { |
| 313 | |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 314 | ret = asoc_simple_card_parse_of(np, priv, dev); |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 315 | if (ret < 0) { |
| 316 | if (ret != -EPROBE_DEFER) |
| 317 | dev_err(dev, "parse error %d\n", ret); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 318 | goto err; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 319 | } |
| 320 | } else { |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 321 | struct asoc_simple_card_info *cinfo; |
| 322 | |
| 323 | cinfo = dev->platform_data; |
| 324 | if (!cinfo) { |
Xiubo Li | 34787d0a | 2014-01-09 17:49:40 +0800 | [diff] [blame] | 325 | dev_err(dev, "no info for asoc-simple-card\n"); |
| 326 | return -EINVAL; |
| 327 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 328 | |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 329 | if (!cinfo->name || |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 330 | !cinfo->codec_dai.name || |
| 331 | !cinfo->codec || |
| 332 | !cinfo->platform || |
| 333 | !cinfo->cpu_dai.name) { |
| 334 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
| 335 | return -EINVAL; |
| 336 | } |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 337 | |
Kuninori Morimoto | 12ffa6f | 2014-03-09 19:37:56 -0700 | [diff] [blame] | 338 | priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 339 | dai_link->name = cinfo->name; |
| 340 | dai_link->stream_name = cinfo->name; |
| 341 | dai_link->platform_name = cinfo->platform; |
| 342 | dai_link->codec_name = cinfo->codec; |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 343 | dai_link->cpu_dai_name = cinfo->cpu_dai.name; |
| 344 | dai_link->codec_dai_name = cinfo->codec_dai.name; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 345 | memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai, |
| 346 | sizeof(priv->dai_props->cpu_dai)); |
| 347 | memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai, |
| 348 | sizeof(priv->dai_props->codec_dai)); |
Kuninori Morimoto | 81985bd | 2014-03-02 20:32:43 -0800 | [diff] [blame] | 349 | |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame^] | 350 | priv->dai_props->cpu_dai.fmt |= cinfo->daifmt; |
| 351 | priv->dai_props->codec_dai.fmt |= cinfo->daifmt; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * init snd_soc_dai_link |
| 356 | */ |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 357 | dai_link->init = asoc_simple_card_dai_init; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 358 | |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 359 | snd_soc_card_set_drvdata(&priv->snd_card, priv); |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 360 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 361 | ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card); |
| 362 | |
| 363 | err: |
| 364 | asoc_simple_card_unref(pdev); |
| 365 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 368 | static const struct of_device_id asoc_simple_of_match[] = { |
| 369 | { .compatible = "simple-audio-card", }, |
| 370 | {}, |
| 371 | }; |
| 372 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 373 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 374 | static struct platform_driver asoc_simple_card = { |
| 375 | .driver = { |
| 376 | .name = "asoc-simple-card", |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 377 | .owner = THIS_MODULE, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 378 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 379 | }, |
| 380 | .probe = asoc_simple_card_probe, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
| 383 | module_platform_driver(asoc_simple_card); |
| 384 | |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 385 | MODULE_ALIAS("platform:asoc-simple-card"); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 386 | MODULE_LICENSE("GPL"); |
| 387 | MODULE_DESCRIPTION("ASoC Simple Sound Card"); |
| 388 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |