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