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