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