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 | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 23 | struct simple_dai_props { |
| 24 | struct asoc_simple_dai cpu_dai; |
| 25 | struct asoc_simple_dai codec_dai; |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 26 | unsigned int mclk_fs; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 27 | } *dai_props; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 28 | unsigned int mclk_fs; |
Kuninori Morimoto | 9eac361 | 2016-05-31 08:59:46 +0000 | [diff] [blame] | 29 | struct asoc_simple_jack hp_jack; |
| 30 | struct asoc_simple_jack mic_jack; |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 31 | struct snd_soc_dai_link *dai_link; |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 34 | #define simple_priv_to_card(priv) (&(priv)->snd_card) |
Kuninori Morimoto | 7e3353d | 2016-08-26 03:06:23 +0000 | [diff] [blame] | 35 | #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 36 | #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev) |
| 37 | #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i)) |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 38 | |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 39 | #define DAI "sound-dai" |
| 40 | #define CELL "#sound-dai-cells" |
Kuninori Morimoto | 548563f | 2016-05-31 08:59:01 +0000 | [diff] [blame] | 41 | #define PREFIX "simple-audio-card," |
| 42 | |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 43 | static int asoc_simple_card_startup(struct snd_pcm_substream *substream) |
| 44 | { |
| 45 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 46 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 47 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 48 | simple_priv_to_props(priv, rtd->num); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 49 | int ret; |
| 50 | |
Kuninori Morimoto | 3ab50c4 | 2017-06-09 00:44:16 +0000 | [diff] [blame] | 51 | ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 52 | if (ret) |
| 53 | return ret; |
Kuninori Morimoto | 387f582 | 2016-08-26 03:07:59 +0000 | [diff] [blame] | 54 | |
Kuninori Morimoto | 3ab50c4 | 2017-06-09 00:44:16 +0000 | [diff] [blame] | 55 | ret = asoc_simple_card_clk_enable(&dai_props->codec_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 56 | if (ret) |
Kuninori Morimoto | 3ab50c4 | 2017-06-09 00:44:16 +0000 | [diff] [blame] | 57 | asoc_simple_card_clk_disable(&dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream) |
| 63 | { |
| 64 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 65 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 66 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 67 | simple_priv_to_props(priv, rtd->num); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 68 | |
Kuninori Morimoto | 3ab50c4 | 2017-06-09 00:44:16 +0000 | [diff] [blame] | 69 | asoc_simple_card_clk_disable(&dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 70 | |
Kuninori Morimoto | 3ab50c4 | 2017-06-09 00:44:16 +0000 | [diff] [blame] | 71 | asoc_simple_card_clk_disable(&dai_props->codec_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 74 | static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai, |
| 75 | unsigned long rate) |
| 76 | { |
| 77 | if (!simple_dai->clk) |
| 78 | return 0; |
| 79 | |
| 80 | if (clk_get_rate(simple_dai->clk) == rate) |
| 81 | return 0; |
| 82 | |
| 83 | return clk_set_rate(simple_dai->clk, rate); |
| 84 | } |
| 85 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 86 | static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream, |
| 87 | struct snd_pcm_hw_params *params) |
| 88 | { |
| 89 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 90 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 91 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 92 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 93 | struct simple_dai_props *dai_props = |
| 94 | simple_priv_to_props(priv, rtd->num); |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 95 | unsigned int mclk, mclk_fs = 0; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 96 | int ret = 0; |
| 97 | |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 98 | if (priv->mclk_fs) |
| 99 | mclk_fs = priv->mclk_fs; |
| 100 | else if (dai_props->mclk_fs) |
| 101 | mclk_fs = dai_props->mclk_fs; |
| 102 | |
| 103 | if (mclk_fs) { |
| 104 | mclk = params_rate(params) * mclk_fs; |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 105 | |
| 106 | ret = asoc_simple_set_clk_rate(&dai_props->codec_dai, mclk); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | ret = asoc_simple_set_clk_rate(&dai_props->cpu_dai, mclk); |
| 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 114 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 115 | SND_SOC_CLOCK_IN); |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 116 | if (ret && ret != -ENOTSUPP) |
| 117 | goto err; |
| 118 | |
| 119 | ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, |
| 120 | SND_SOC_CLOCK_OUT); |
| 121 | if (ret && ret != -ENOTSUPP) |
| 122 | goto err; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 123 | } |
Aaro Koskinen | ee43a1a | 2016-01-24 00:36:40 +0200 | [diff] [blame] | 124 | return 0; |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 125 | err: |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
Julia Lawall | 9b6fdef | 2016-10-15 16:55:49 +0200 | [diff] [blame] | 129 | static const struct snd_soc_ops asoc_simple_card_ops = { |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 130 | .startup = asoc_simple_card_startup, |
| 131 | .shutdown = asoc_simple_card_shutdown, |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 132 | .hw_params = asoc_simple_card_hw_params, |
| 133 | }; |
| 134 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 135 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 136 | { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 137 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 138 | struct snd_soc_dai *codec = rtd->codec_dai; |
| 139 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 140 | struct simple_dai_props *dai_props = |
| 141 | simple_priv_to_props(priv, rtd->num); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 142 | int ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 143 | |
Kuninori Morimoto | d8cb935 | 2016-08-09 05:48:53 +0000 | [diff] [blame] | 144 | ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 145 | if (ret < 0) |
| 146 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 147 | |
Kuninori Morimoto | d8cb935 | 2016-08-09 05:48:53 +0000 | [diff] [blame] | 148 | ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 149 | if (ret < 0) |
| 150 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 155 | static int asoc_simple_card_dai_link_of(struct device_node *node, |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 156 | struct simple_card_data *priv, |
Kuninori Morimoto | 9810f53 | 2014-09-09 21:38:24 -0700 | [diff] [blame] | 157 | int idx, |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 158 | bool is_top_level_node) |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 159 | { |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 160 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | 9810f53 | 2014-09-09 21:38:24 -0700 | [diff] [blame] | 161 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx); |
| 162 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx); |
Kuninori Morimoto | 28abd99 | 2016-07-19 02:53:13 +0000 | [diff] [blame] | 163 | struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai; |
| 164 | struct asoc_simple_dai *codec_dai = &dai_props->codec_dai; |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 165 | struct device_node *cpu = NULL; |
Jun Nie | e0ae225 | 2015-04-29 18:11:07 +0800 | [diff] [blame] | 166 | struct device_node *plat = NULL; |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 167 | struct device_node *codec = NULL; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 168 | char prop[128]; |
| 169 | char *prefix = ""; |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 170 | int ret, single_cpu; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 171 | |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 172 | /* For single DAI link & old style of DT node */ |
Jyri Sarha | 6487221 | 2014-04-24 19:42:00 +0300 | [diff] [blame] | 173 | if (is_top_level_node) |
Kuninori Morimoto | 548563f | 2016-05-31 08:59:01 +0000 | [diff] [blame] | 174 | prefix = PREFIX; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 175 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 176 | snprintf(prop, sizeof(prop), "%scpu", prefix); |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 177 | cpu = of_get_child_by_name(node, prop); |
| 178 | |
Julian Scheel | 7ac45d1 | 2017-05-24 12:28:23 +0200 | [diff] [blame] | 179 | if (!cpu) { |
| 180 | ret = -EINVAL; |
| 181 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
| 182 | goto dai_link_of_err; |
| 183 | } |
| 184 | |
Jun Nie | e0ae225 | 2015-04-29 18:11:07 +0800 | [diff] [blame] | 185 | snprintf(prop, sizeof(prop), "%splat", prefix); |
| 186 | plat = of_get_child_by_name(node, prop); |
| 187 | |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 188 | snprintf(prop, sizeof(prop), "%scodec", prefix); |
| 189 | codec = of_get_child_by_name(node, prop); |
| 190 | |
Julian Scheel | 7ac45d1 | 2017-05-24 12:28:23 +0200 | [diff] [blame] | 191 | if (!codec) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 192 | ret = -EINVAL; |
Nicolin Chen | 966b806 | 2014-04-24 19:13:59 +0800 | [diff] [blame] | 193 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 194 | goto dai_link_of_err; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 195 | } |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 196 | |
Kuninori Morimoto | cecdef3 | 2016-06-30 06:02:46 +0000 | [diff] [blame] | 197 | ret = asoc_simple_card_parse_daifmt(dev, node, codec, |
| 198 | prefix, &dai_link->dai_fmt); |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 199 | if (ret < 0) |
| 200 | goto dai_link_of_err; |
| 201 | |
Kuninori Morimoto | f93b646 | 2016-08-26 03:05:58 +0000 | [diff] [blame] | 202 | of_property_read_u32(node, "mclk-fs", &dai_props->mclk_fs); |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 203 | |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 204 | ret = asoc_simple_card_parse_cpu(cpu, dai_link, |
| 205 | DAI, CELL, &single_cpu); |
| 206 | if (ret < 0) |
| 207 | goto dai_link_of_err; |
| 208 | |
| 209 | ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL); |
| 210 | if (ret < 0) |
| 211 | goto dai_link_of_err; |
| 212 | |
| 213 | ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL); |
| 214 | if (ret < 0) |
| 215 | goto dai_link_of_err; |
| 216 | |
Kuninori Morimoto | b93d2cf | 2017-06-14 00:34:53 +0000 | [diff] [blame] | 217 | ret = asoc_simple_card_of_parse_tdm(cpu, cpu_dai); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 218 | if (ret < 0) |
| 219 | goto dai_link_of_err; |
| 220 | |
Kuninori Morimoto | b93d2cf | 2017-06-14 00:34:53 +0000 | [diff] [blame] | 221 | ret = asoc_simple_card_of_parse_tdm(codec, codec_dai); |
Kuninori Morimoto | 5fb9cb1 | 2016-05-20 09:40:41 +0000 | [diff] [blame] | 222 | if (ret < 0) |
| 223 | goto dai_link_of_err; |
| 224 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 225 | ret = asoc_simple_card_parse_clk_cpu(dev, cpu, dai_link, cpu_dai); |
Kuninori Morimoto | 28abd99 | 2016-07-19 02:53:13 +0000 | [diff] [blame] | 226 | if (ret < 0) |
| 227 | goto dai_link_of_err; |
| 228 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 229 | ret = asoc_simple_card_parse_clk_codec(dev, codec, dai_link, codec_dai); |
Kuninori Morimoto | 28abd99 | 2016-07-19 02:53:13 +0000 | [diff] [blame] | 230 | if (ret < 0) |
| 231 | goto dai_link_of_err; |
| 232 | |
Kuninori Morimoto | c958374 | 2016-08-09 05:50:02 +0000 | [diff] [blame] | 233 | ret = asoc_simple_card_canonicalize_dailink(dai_link); |
| 234 | if (ret < 0) |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 235 | goto dai_link_of_err; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 236 | |
Kuninori Morimoto | 2e8d1c7 | 2016-07-11 23:57:34 +0000 | [diff] [blame] | 237 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 238 | "%s-%s", |
| 239 | dai_link->cpu_dai_name, |
| 240 | dai_link->codec_dai_name); |
| 241 | if (ret < 0) |
Vishal Thanki | 31f3032 | 2015-03-03 18:59:00 +0530 | [diff] [blame] | 242 | goto dai_link_of_err; |
Vishal Thanki | 31f3032 | 2015-03-03 18:59:00 +0530 | [diff] [blame] | 243 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 244 | dai_link->ops = &asoc_simple_card_ops; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 245 | dai_link->init = asoc_simple_card_dai_init; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 246 | |
Kuninori Morimoto | 16f1de6 | 2016-08-10 02:20:43 +0000 | [diff] [blame] | 247 | asoc_simple_card_canonicalize_cpu(dai_link, single_cpu); |
Kuninori Morimoto | 179949b | 2014-08-27 20:08:06 -0700 | [diff] [blame] | 248 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 249 | dai_link_of_err: |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 250 | of_node_put(cpu); |
| 251 | of_node_put(codec); |
| 252 | |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 253 | return ret; |
| 254 | } |
| 255 | |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 256 | static int asoc_simple_card_parse_aux_devs(struct device_node *node, |
| 257 | struct simple_card_data *priv) |
| 258 | { |
| 259 | struct device *dev = simple_priv_to_dev(priv); |
| 260 | struct device_node *aux_node; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 261 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 262 | int i, n, len; |
| 263 | |
| 264 | if (!of_find_property(node, PREFIX "aux-devs", &len)) |
| 265 | return 0; /* Ok to have no aux-devs */ |
| 266 | |
| 267 | n = len / sizeof(__be32); |
| 268 | if (n <= 0) |
| 269 | return -EINVAL; |
| 270 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 271 | card->aux_dev = devm_kcalloc(dev, |
| 272 | n, sizeof(*card->aux_dev), GFP_KERNEL); |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 273 | if (!card->aux_dev) |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 274 | return -ENOMEM; |
| 275 | |
| 276 | for (i = 0; i < n; i++) { |
| 277 | aux_node = of_parse_phandle(node, PREFIX "aux-devs", i); |
| 278 | if (!aux_node) |
| 279 | return -EINVAL; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 280 | card->aux_dev[i].codec_of_node = aux_node; |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 281 | } |
| 282 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 283 | card->num_aux_devs = n; |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Kuninori Morimoto | d2fdcc2 | 2017-06-07 00:37:05 +0000 | [diff] [blame] | 287 | static int asoc_simple_card_parse_of(struct simple_card_data *priv) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 288 | { |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 289 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 290 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 291 | struct device_node *dai_link; |
Kuninori Morimoto | d2fdcc2 | 2017-06-07 00:37:05 +0000 | [diff] [blame] | 292 | struct device_node *node = dev->of_node; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 293 | int ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 294 | |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 295 | if (!node) |
| 296 | return -EINVAL; |
| 297 | |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 298 | dai_link = of_get_child_by_name(node, PREFIX "dai-link"); |
| 299 | |
Kuninori Morimoto | fa2760dd | 2017-06-16 01:39:11 +0000 | [diff] [blame] | 300 | ret = asoc_simple_card_of_parse_widgets(card, PREFIX); |
| 301 | if (ret < 0) |
| 302 | goto card_parse_end; |
Xiubo Li | 9d681f5 | 2014-02-08 15:59:53 +0800 | [diff] [blame] | 303 | |
Kuninori Morimoto | 1fdb5d2 | 2017-06-15 00:25:17 +0000 | [diff] [blame] | 304 | ret = asoc_simple_card_of_parse_routing(card, PREFIX, 1); |
| 305 | if (ret < 0) |
| 306 | goto card_parse_end; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 307 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 308 | /* Factor to mclk, used in hw_params() */ |
Kuninori Morimoto | f93b646 | 2016-08-26 03:05:58 +0000 | [diff] [blame] | 309 | of_property_read_u32(node, PREFIX "mclk-fs", &priv->mclk_fs); |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 310 | |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 311 | /* Single/Muti DAI link(s) & New style of DT node */ |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 312 | if (dai_link) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 313 | struct device_node *np = NULL; |
Kuninori Morimoto | a44a750 | 2014-08-27 20:08:47 -0700 | [diff] [blame] | 314 | int i = 0; |
| 315 | |
| 316 | for_each_child_of_node(node, np) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 317 | dev_dbg(dev, "\tlink %d:\n", i); |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 318 | ret = asoc_simple_card_dai_link_of(np, priv, |
Kuninori Morimoto | 9810f53 | 2014-09-09 21:38:24 -0700 | [diff] [blame] | 319 | i, false); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 320 | if (ret < 0) { |
| 321 | of_node_put(np); |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 322 | goto card_parse_end; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 323 | } |
Kuninori Morimoto | a44a750 | 2014-08-27 20:08:47 -0700 | [diff] [blame] | 324 | i++; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 325 | } |
| 326 | } else { |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 327 | /* For single DAI link & old style of DT node */ |
Kuninori Morimoto | 9810f53 | 2014-09-09 21:38:24 -0700 | [diff] [blame] | 328 | ret = asoc_simple_card_dai_link_of(node, priv, 0, true); |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 329 | if (ret < 0) |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 330 | goto card_parse_end; |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 331 | } |
Xiubo Li | dd41e0c | 2013-12-20 14:39:50 +0800 | [diff] [blame] | 332 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 333 | ret = asoc_simple_card_parse_card_name(card, PREFIX); |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 334 | if (ret < 0) |
| 335 | goto card_parse_end; |
| 336 | |
| 337 | ret = asoc_simple_card_parse_aux_devs(node, priv); |
Kuninori Morimoto | f687d90 | 2014-02-27 18:25:24 -0800 | [diff] [blame] | 338 | |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 339 | card_parse_end: |
| 340 | of_node_put(dai_link); |
| 341 | |
| 342 | return ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 343 | } |
| 344 | |
Katsuhiro Suzuki | 8d1bd11 | 2018-06-11 17:32:13 +0900 | [diff] [blame^] | 345 | static int asoc_simple_soc_card_probe(struct snd_soc_card *card) |
| 346 | { |
| 347 | struct simple_card_data *priv = snd_soc_card_get_drvdata(card); |
| 348 | int ret; |
| 349 | |
| 350 | ret = asoc_simple_card_init_hp(card, &priv->hp_jack, PREFIX); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
| 354 | ret = asoc_simple_card_init_mic(card, &priv->mic_jack, PREFIX); |
| 355 | if (ret < 0) |
| 356 | return ret; |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 361 | static int asoc_simple_card_probe(struct platform_device *pdev) |
| 362 | { |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 363 | struct simple_card_data *priv; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 364 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 365 | struct simple_dai_props *dai_props; |
Kuninori Morimoto | f89983e | 2012-12-25 22:52:33 -0800 | [diff] [blame] | 366 | struct device *dev = &pdev->dev; |
Kuninori Morimoto | dcee9bfe | 2017-03-15 04:43:21 +0000 | [diff] [blame] | 367 | struct device_node *np = dev->of_node; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 368 | struct snd_soc_card *card; |
Kuninori Morimoto | 2c86dda | 2016-08-26 03:09:38 +0000 | [diff] [blame] | 369 | int num, ret; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 370 | |
Xiubo Li | 0dd4fc3 | 2014-09-10 09:59:55 +0800 | [diff] [blame] | 371 | /* Get the number of DAI links */ |
Kuninori Morimoto | 548563f | 2016-05-31 08:59:01 +0000 | [diff] [blame] | 372 | if (np && of_get_child_by_name(np, PREFIX "dai-link")) |
Kuninori Morimoto | 2c86dda | 2016-08-26 03:09:38 +0000 | [diff] [blame] | 373 | num = of_get_child_count(np); |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 374 | else |
Kuninori Morimoto | 2c86dda | 2016-08-26 03:09:38 +0000 | [diff] [blame] | 375 | num = 1; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 376 | |
Xiubo Li | 0dd4fc3 | 2014-09-10 09:59:55 +0800 | [diff] [blame] | 377 | /* Allocate the private data and the DAI link array */ |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 378 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 379 | if (!priv) |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 380 | return -ENOMEM; |
| 381 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 382 | dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL); |
| 383 | dai_link = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL); |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 384 | if (!dai_props || !dai_link) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | priv->dai_props = dai_props; |
| 388 | priv->dai_link = dai_link; |
| 389 | |
Xiubo Li | 0dd4fc3 | 2014-09-10 09:59:55 +0800 | [diff] [blame] | 390 | /* Init snd_soc_card */ |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 391 | card = simple_priv_to_card(priv); |
| 392 | card->owner = THIS_MODULE; |
| 393 | card->dev = dev; |
| 394 | card->dai_link = priv->dai_link; |
| 395 | card->num_links = num; |
Katsuhiro Suzuki | 8d1bd11 | 2018-06-11 17:32:13 +0900 | [diff] [blame^] | 396 | card->probe = asoc_simple_soc_card_probe; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 397 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 398 | if (np && of_device_is_available(np)) { |
| 399 | |
Kuninori Morimoto | d2fdcc2 | 2017-06-07 00:37:05 +0000 | [diff] [blame] | 400 | ret = asoc_simple_card_parse_of(priv); |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 401 | if (ret < 0) { |
| 402 | if (ret != -EPROBE_DEFER) |
| 403 | dev_err(dev, "parse error %d\n", ret); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 404 | goto err; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 405 | } |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 406 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 407 | } else { |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 408 | struct asoc_simple_card_info *cinfo; |
| 409 | |
| 410 | cinfo = dev->platform_data; |
| 411 | if (!cinfo) { |
Xiubo Li | 34787d0a | 2014-01-09 17:49:40 +0800 | [diff] [blame] | 412 | dev_err(dev, "no info for asoc-simple-card\n"); |
| 413 | return -EINVAL; |
| 414 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 415 | |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 416 | if (!cinfo->name || |
| 417 | !cinfo->codec_dai.name || |
| 418 | !cinfo->codec || |
| 419 | !cinfo->platform || |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 420 | !cinfo->cpu_dai.name) { |
| 421 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
| 422 | return -EINVAL; |
| 423 | } |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 424 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 425 | card->name = (cinfo->card) ? cinfo->card : cinfo->name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 426 | dai_link->name = cinfo->name; |
| 427 | dai_link->stream_name = cinfo->name; |
| 428 | dai_link->platform_name = cinfo->platform; |
| 429 | dai_link->codec_name = cinfo->codec; |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 430 | dai_link->cpu_dai_name = cinfo->cpu_dai.name; |
| 431 | dai_link->codec_dai_name = cinfo->codec_dai.name; |
Lars-Peter Clausen | 1efb53a | 2015-03-24 01:07:08 +0000 | [diff] [blame] | 432 | dai_link->dai_fmt = cinfo->daifmt; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 433 | dai_link->init = asoc_simple_card_dai_init; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 434 | memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai, |
| 435 | sizeof(priv->dai_props->cpu_dai)); |
| 436 | memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai, |
| 437 | sizeof(priv->dai_props->codec_dai)); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 440 | snd_soc_card_set_drvdata(card, priv); |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 441 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 442 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | c6d112e | 2017-05-19 00:57:21 +0000 | [diff] [blame] | 443 | if (ret < 0) |
| 444 | goto err; |
| 445 | |
| 446 | return 0; |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 447 | err: |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 448 | asoc_simple_card_clean_reference(card); |
Kuninori Morimoto | 387f582 | 2016-08-26 03:07:59 +0000 | [diff] [blame] | 449 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 450 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 453 | static int asoc_simple_card_remove(struct platform_device *pdev) |
| 454 | { |
Dylan Reid | 3fe2403 | 2014-10-01 14:25:20 -0700 | [diff] [blame] | 455 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Dylan Reid | 3fe2403 | 2014-10-01 14:25:20 -0700 | [diff] [blame] | 456 | |
Kuninori Morimoto | 885fc05 | 2016-08-10 02:21:42 +0000 | [diff] [blame] | 457 | return asoc_simple_card_clean_reference(card); |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 458 | } |
| 459 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 460 | static const struct of_device_id asoc_simple_of_match[] = { |
| 461 | { .compatible = "simple-audio-card", }, |
| 462 | {}, |
| 463 | }; |
| 464 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 465 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 466 | static struct platform_driver asoc_simple_card = { |
| 467 | .driver = { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 468 | .name = "asoc-simple-card", |
Peter Ujfalusi | 7c37671 | 2016-05-09 13:38:10 +0300 | [diff] [blame] | 469 | .pm = &snd_soc_pm_ops, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 470 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 471 | }, |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 472 | .probe = asoc_simple_card_probe, |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 473 | .remove = asoc_simple_card_remove, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | module_platform_driver(asoc_simple_card); |
| 477 | |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 478 | MODULE_ALIAS("platform:asoc-simple-card"); |
Kuninori Morimoto | a1dbfd0 | 2016-08-26 03:07:28 +0000 | [diff] [blame] | 479 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 480 | MODULE_DESCRIPTION("ASoC Simple Sound Card"); |
| 481 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |