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> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 13 | #include <linux/of.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 15 | #include <linux/string.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 16 | #include <sound/simple_card.h> |
| 17 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 18 | static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai, |
| 19 | struct asoc_simple_dai *set, |
| 20 | unsigned int daifmt) |
| 21 | { |
| 22 | int ret = 0; |
| 23 | |
| 24 | daifmt |= set->fmt; |
| 25 | |
Xiubo Li | e874dde | 2013-12-23 13:24:59 +0800 | [diff] [blame] | 26 | if (daifmt) |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 27 | ret = snd_soc_dai_set_fmt(dai, daifmt); |
| 28 | |
Kuninori Morimoto | e244bb9 | 2013-10-17 22:46:49 -0700 | [diff] [blame] | 29 | if (ret == -ENOTSUPP) { |
| 30 | dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n"); |
| 31 | ret = 0; |
| 32 | } |
| 33 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 34 | if (!ret && set->sysclk) |
| 35 | ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0); |
| 36 | |
| 37 | return ret; |
| 38 | } |
| 39 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 40 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 41 | { |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 42 | struct asoc_simple_card_info *info = |
| 43 | snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 44 | struct snd_soc_dai *codec = rtd->codec_dai; |
| 45 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 46 | unsigned int daifmt = info->daifmt; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 47 | int ret; |
| 48 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 49 | ret = __asoc_simple_card_dai_init(codec, &info->codec_dai, daifmt); |
| 50 | if (ret < 0) |
| 51 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 52 | |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 53 | ret = __asoc_simple_card_dai_init(cpu, &info->cpu_dai, daifmt); |
| 54 | if (ret < 0) |
| 55 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 60 | static int |
| 61 | asoc_simple_card_sub_parse_of(struct device_node *np, |
| 62 | struct asoc_simple_dai *dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 63 | const struct device_node **p_node, |
| 64 | const char **name) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 65 | { |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 66 | struct device_node *node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 67 | struct clk *clk; |
| 68 | int ret; |
| 69 | |
| 70 | /* |
| 71 | * get node via "sound-dai = <&phandle port>" |
| 72 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 73 | */ |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 74 | node = of_parse_phandle(np, "sound-dai", 0); |
| 75 | if (!node) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 76 | return -ENODEV; |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 77 | *p_node = node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 78 | |
| 79 | /* get dai->name */ |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 80 | ret = snd_soc_of_get_dai_name(np, name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 81 | if (ret < 0) |
| 82 | goto parse_error; |
| 83 | |
| 84 | /* |
| 85 | * bitclock-inversion, frame-inversion |
| 86 | * bitclock-master, frame-master |
| 87 | * and specific "format" if it has |
| 88 | */ |
| 89 | dai->fmt = snd_soc_of_parse_daifmt(np, NULL); |
| 90 | |
| 91 | /* |
| 92 | * dai->sysclk come from |
| 93 | * "clocks = <&xxx>" (if system has common clock) |
| 94 | * or "system-clock-frequency = <xxx>" |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 95 | * or device's module clock. |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 96 | */ |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 97 | if (of_property_read_bool(np, "clocks")) { |
| 98 | clk = of_clk_get(np, 0); |
| 99 | if (IS_ERR(clk)) { |
| 100 | ret = PTR_ERR(clk); |
| 101 | goto parse_error; |
| 102 | } |
| 103 | |
| 104 | dai->sysclk = clk_get_rate(clk); |
| 105 | } else if (of_property_read_bool(np, "system-clock-frequency")) { |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 106 | of_property_read_u32(np, |
| 107 | "system-clock-frequency", |
| 108 | &dai->sysclk); |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 109 | } else { |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 110 | clk = of_clk_get(node, 0); |
Xiubo Li | e2a19ac | 2014-01-06 12:34:36 +0800 | [diff] [blame] | 111 | if (!IS_ERR(clk)) |
| 112 | dai->sysclk = clk_get_rate(clk); |
Xiubo Li | 71467e4 | 2013-12-23 15:25:38 +0800 | [diff] [blame] | 113 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 114 | |
| 115 | ret = 0; |
| 116 | |
| 117 | parse_error: |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 118 | of_node_put(node); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static int asoc_simple_card_parse_of(struct device_node *node, |
| 124 | struct asoc_simple_card_info *info, |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 125 | struct device *dev) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 126 | { |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 127 | struct snd_soc_dai_link *dai_link = info->snd_card.dai_link; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 128 | struct device_node *np; |
| 129 | char *name; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 130 | int ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 131 | |
| 132 | /* get CPU/CODEC common format via simple-audio-card,format */ |
| 133 | info->daifmt = snd_soc_of_parse_daifmt(node, "simple-audio-card,") & |
| 134 | (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK); |
| 135 | |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 136 | /* DAPM routes */ |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 137 | if (of_property_read_bool(node, "simple-audio-card,routing")) { |
Xiubo Li | f87a3e8 | 2014-01-07 09:13:42 +0800 | [diff] [blame] | 138 | ret = snd_soc_of_parse_audio_routing(&info->snd_card, |
Xiubo Li | 8c0b823 | 2014-01-07 09:15:16 +0800 | [diff] [blame] | 139 | "simple-audio-card,routing"); |
Xiubo Li | f87a3e8 | 2014-01-07 09:13:42 +0800 | [diff] [blame] | 140 | if (ret) |
| 141 | return ret; |
| 142 | } |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 143 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 144 | /* CPU sub-node */ |
| 145 | ret = -EINVAL; |
| 146 | np = of_get_child_by_name(node, "simple-audio-card,cpu"); |
| 147 | if (np) |
| 148 | ret = asoc_simple_card_sub_parse_of(np, |
| 149 | &info->cpu_dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 150 | &dai_link->cpu_of_node, |
| 151 | &dai_link->cpu_dai_name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 152 | if (ret < 0) |
| 153 | return ret; |
| 154 | |
| 155 | /* CODEC sub-node */ |
| 156 | ret = -EINVAL; |
| 157 | np = of_get_child_by_name(node, "simple-audio-card,codec"); |
| 158 | if (np) |
| 159 | ret = asoc_simple_card_sub_parse_of(np, |
| 160 | &info->codec_dai, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 161 | &dai_link->codec_of_node, |
| 162 | &dai_link->codec_dai_name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 166 | if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) |
Xiubo Li | dd41e0c | 2013-12-20 14:39:50 +0800 | [diff] [blame] | 167 | return -EINVAL; |
| 168 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 169 | /* card name is created from CPU/CODEC dai name */ |
| 170 | name = devm_kzalloc(dev, |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 171 | strlen(dai_link->cpu_dai_name) + |
| 172 | strlen(dai_link->codec_dai_name) + 2, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 173 | GFP_KERNEL); |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 174 | sprintf(name, "%s-%s", dai_link->cpu_dai_name, |
| 175 | dai_link->codec_dai_name); |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 176 | info->snd_card.name = name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 177 | dai_link->name = dai_link->stream_name = name; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 178 | |
| 179 | /* simple-card assumes platform == cpu */ |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 180 | dai_link->platform_of_node = dai_link->cpu_of_node; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 181 | |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 182 | dev_dbg(dev, "card-name : %s\n", name); |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 183 | dev_dbg(dev, "platform : %04x\n", info->daifmt); |
| 184 | dev_dbg(dev, "cpu : %s / %04x / %d\n", |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 185 | dai_link->cpu_dai_name, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 186 | info->cpu_dai.fmt, |
| 187 | info->cpu_dai.sysclk); |
| 188 | dev_dbg(dev, "codec : %s / %04x / %d\n", |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 189 | dai_link->codec_dai_name, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 190 | info->codec_dai.fmt, |
| 191 | info->codec_dai.sysclk); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 196 | static int asoc_simple_card_probe(struct platform_device *pdev) |
| 197 | { |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 198 | struct asoc_simple_card_info *cinfo; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 199 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 200 | struct device_node *np = pdev->dev.of_node; |
Kuninori Morimoto | f89983e | 2012-12-25 22:52:33 -0800 | [diff] [blame] | 201 | struct device *dev = &pdev->dev; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 202 | int ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 203 | |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 204 | cinfo = devm_kzalloc(dev, sizeof(*cinfo), GFP_KERNEL); |
| 205 | if (!cinfo) |
| 206 | return -ENOMEM; |
| 207 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 208 | /* |
| 209 | * init snd_soc_card |
| 210 | */ |
| 211 | cinfo->snd_card.owner = THIS_MODULE; |
| 212 | cinfo->snd_card.dev = dev; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 213 | dai_link = &cinfo->snd_link; |
| 214 | cinfo->snd_card.dai_link = dai_link; |
| 215 | cinfo->snd_card.num_links = 1; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 216 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 217 | if (np && of_device_is_available(np)) { |
| 218 | |
| 219 | ret = asoc_simple_card_parse_of(np, cinfo, dev); |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 220 | if (ret < 0) { |
| 221 | if (ret != -EPROBE_DEFER) |
| 222 | dev_err(dev, "parse error %d\n", ret); |
| 223 | return ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 224 | } |
| 225 | } else { |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 226 | if (!dev->platform_data) { |
Xiubo Li | 34787d0 | 2014-01-09 17:49:40 +0800 | [diff] [blame] | 227 | dev_err(dev, "no info for asoc-simple-card\n"); |
| 228 | return -EINVAL; |
| 229 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 230 | |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 231 | memcpy(cinfo, dev->platform_data, sizeof(*cinfo)); |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 232 | if (!cinfo->name || |
| 233 | !cinfo->card || |
| 234 | !cinfo->codec_dai.name || |
| 235 | !cinfo->codec || |
| 236 | !cinfo->platform || |
| 237 | !cinfo->cpu_dai.name) { |
| 238 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
| 239 | return -EINVAL; |
| 240 | } |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 241 | |
| 242 | cinfo->snd_card.name = cinfo->card; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 243 | dai_link->name = cinfo->name; |
| 244 | dai_link->stream_name = cinfo->name; |
| 245 | dai_link->platform_name = cinfo->platform; |
| 246 | dai_link->codec_name = cinfo->codec; |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame^] | 247 | dai_link->cpu_dai_name = cinfo->cpu_dai.name; |
| 248 | dai_link->codec_dai_name = cinfo->codec_dai.name; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * init snd_soc_dai_link |
| 253 | */ |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 254 | dai_link->init = asoc_simple_card_dai_init; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 255 | |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 256 | snd_soc_card_set_drvdata(&cinfo->snd_card, cinfo); |
| 257 | |
Xiubo Li | e1acb40 | 2013-12-19 11:59:54 +0800 | [diff] [blame] | 258 | return devm_snd_soc_register_card(&pdev->dev, &cinfo->snd_card); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 261 | static const struct of_device_id asoc_simple_of_match[] = { |
| 262 | { .compatible = "simple-audio-card", }, |
| 263 | {}, |
| 264 | }; |
| 265 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 266 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 267 | static struct platform_driver asoc_simple_card = { |
| 268 | .driver = { |
| 269 | .name = "asoc-simple-card", |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 270 | .owner = THIS_MODULE, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 271 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 272 | }, |
| 273 | .probe = asoc_simple_card_probe, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | module_platform_driver(asoc_simple_card); |
| 277 | |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 278 | MODULE_ALIAS("platform:asoc-simple-card"); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 279 | MODULE_LICENSE("GPL"); |
| 280 | MODULE_DESCRIPTION("ASoC Simple Sound Card"); |
| 281 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |