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