Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 - 2016 Samsung Electronics Co., Ltd. |
| 3 | * |
| 4 | * Authors: Inha Song <ideal.song@samsung.com> |
| 5 | * Sylwester Nawrocki <s.nawrocki@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/gpio.h> |
Fabian Frederick | 5d079fd | 2016-12-09 19:12:50 +0100 | [diff] [blame] | 15 | #include <linux/gpio/consumer.h> |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <sound/pcm_params.h> |
| 19 | #include <sound/soc.h> |
| 20 | |
| 21 | #include "i2s.h" |
| 22 | #include "../codecs/wm5110.h" |
| 23 | |
| 24 | /* |
| 25 | * The source clock is XCLKOUT with its mux set to the external fixed rate |
| 26 | * oscillator (XXTI). |
| 27 | */ |
| 28 | #define MCLK_RATE 24000000U |
| 29 | |
| 30 | #define TM2_DAI_AIF1 0 |
| 31 | #define TM2_DAI_AIF2 1 |
| 32 | |
| 33 | struct tm2_machine_priv { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 34 | struct snd_soc_component *component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 35 | unsigned int sysclk_rate; |
| 36 | struct gpio_desc *gpio_mic_bias; |
| 37 | }; |
| 38 | |
| 39 | static int tm2_start_sysclk(struct snd_soc_card *card) |
| 40 | { |
| 41 | struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card); |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 42 | struct snd_soc_component *component = priv->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 43 | int ret; |
| 44 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 45 | ret = snd_soc_component_set_pll(component, WM5110_FLL1_REFCLK, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 46 | ARIZONA_FLL_SRC_MCLK1, |
| 47 | MCLK_RATE, |
| 48 | priv->sysclk_rate); |
| 49 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 50 | dev_err(component->dev, "Failed to set FLL1 source: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 51 | return ret; |
| 52 | } |
| 53 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 54 | ret = snd_soc_component_set_pll(component, WM5110_FLL1, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 55 | ARIZONA_FLL_SRC_MCLK1, |
| 56 | MCLK_RATE, |
| 57 | priv->sysclk_rate); |
| 58 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 59 | dev_err(component->dev, "Failed to start FLL1: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 63 | ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 64 | ARIZONA_CLK_SRC_FLL1, |
| 65 | priv->sysclk_rate, |
| 66 | SND_SOC_CLOCK_IN); |
| 67 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 68 | dev_err(component->dev, "Failed to set SYSCLK source: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int tm2_stop_sysclk(struct snd_soc_card *card) |
| 76 | { |
| 77 | struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card); |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 78 | struct snd_soc_component *component = priv->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 79 | int ret; |
| 80 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 81 | ret = snd_soc_component_set_pll(component, WM5110_FLL1, 0, 0, 0); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 82 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 83 | dev_err(component->dev, "Failed to stop FLL1: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 87 | ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 88 | ARIZONA_CLK_SRC_FLL1, 0, 0); |
| 89 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 90 | dev_err(component->dev, "Failed to stop SYSCLK: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int tm2_aif1_hw_params(struct snd_pcm_substream *substream, |
| 98 | struct snd_pcm_hw_params *params) |
| 99 | { |
| 100 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 101 | struct snd_soc_component *component = rtd->codec_dai->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 102 | struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(rtd->card); |
| 103 | |
| 104 | switch (params_rate(params)) { |
| 105 | case 4000: |
| 106 | case 8000: |
| 107 | case 12000: |
| 108 | case 16000: |
| 109 | case 24000: |
| 110 | case 32000: |
| 111 | case 48000: |
| 112 | case 96000: |
| 113 | case 192000: |
| 114 | /* Highest possible SYSCLK frequency: 147.456MHz */ |
| 115 | priv->sysclk_rate = 147456000U; |
| 116 | break; |
| 117 | case 11025: |
| 118 | case 22050: |
| 119 | case 44100: |
| 120 | case 88200: |
| 121 | case 176400: |
| 122 | /* Highest possible SYSCLK frequency: 135.4752 MHz */ |
| 123 | priv->sysclk_rate = 135475200U; |
| 124 | break; |
| 125 | default: |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 126 | dev_err(component->dev, "Not supported sample rate: %d\n", |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 127 | params_rate(params)); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | return tm2_start_sysclk(rtd->card); |
| 132 | } |
| 133 | |
| 134 | static struct snd_soc_ops tm2_aif1_ops = { |
| 135 | .hw_params = tm2_aif1_hw_params, |
| 136 | }; |
| 137 | |
| 138 | static int tm2_aif2_hw_params(struct snd_pcm_substream *substream, |
| 139 | struct snd_pcm_hw_params *params) |
| 140 | { |
| 141 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 142 | struct snd_soc_component *component = rtd->codec_dai->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 143 | unsigned int asyncclk_rate; |
| 144 | int ret; |
| 145 | |
| 146 | switch (params_rate(params)) { |
| 147 | case 8000: |
| 148 | case 12000: |
| 149 | case 16000: |
| 150 | /* Highest possible ASYNCCLK frequency: 49.152MHz */ |
| 151 | asyncclk_rate = 49152000U; |
| 152 | break; |
| 153 | case 11025: |
| 154 | /* Highest possible ASYNCCLK frequency: 45.1584 MHz */ |
| 155 | asyncclk_rate = 45158400U; |
| 156 | break; |
| 157 | default: |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 158 | dev_err(component->dev, "Not supported sample rate: %d\n", |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 159 | params_rate(params)); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 163 | ret = snd_soc_component_set_pll(component, WM5110_FLL2_REFCLK, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 164 | ARIZONA_FLL_SRC_MCLK1, |
| 165 | MCLK_RATE, |
| 166 | asyncclk_rate); |
| 167 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 168 | dev_err(component->dev, "Failed to set FLL2 source: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 172 | ret = snd_soc_component_set_pll(component, WM5110_FLL2, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 173 | ARIZONA_FLL_SRC_MCLK1, |
| 174 | MCLK_RATE, |
| 175 | asyncclk_rate); |
| 176 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 177 | dev_err(component->dev, "Failed to start FLL2: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 181 | ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_ASYNCCLK, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 182 | ARIZONA_CLK_SRC_FLL2, |
| 183 | asyncclk_rate, |
| 184 | SND_SOC_CLOCK_IN); |
| 185 | if (ret < 0) { |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 186 | dev_err(component->dev, "Failed to set ASYNCCLK source: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int tm2_aif2_hw_free(struct snd_pcm_substream *substream) |
| 194 | { |
| 195 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 196 | struct snd_soc_component *component = rtd->codec_dai->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 197 | int ret; |
| 198 | |
| 199 | /* disable FLL2 */ |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 200 | ret = snd_soc_component_set_pll(component, WM5110_FLL2, ARIZONA_FLL_SRC_MCLK1, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 201 | 0, 0); |
| 202 | if (ret < 0) |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 203 | dev_err(component->dev, "Failed to stop FLL2: %d\n", ret); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static struct snd_soc_ops tm2_aif2_ops = { |
| 209 | .hw_params = tm2_aif2_hw_params, |
| 210 | .hw_free = tm2_aif2_hw_free, |
| 211 | }; |
| 212 | |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 213 | static int tm2_hdmi_hw_params(struct snd_pcm_substream *substream, |
| 214 | struct snd_pcm_hw_params *params) |
| 215 | { |
| 216 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 217 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 218 | unsigned int bfs; |
| 219 | int bitwidth, ret; |
| 220 | |
| 221 | bitwidth = snd_pcm_format_width(params_format(params)); |
| 222 | if (bitwidth < 0) { |
| 223 | dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth); |
| 224 | return bitwidth; |
| 225 | } |
| 226 | |
| 227 | switch (bitwidth) { |
| 228 | case 48: |
| 229 | bfs = 64; |
| 230 | break; |
| 231 | case 16: |
| 232 | bfs = 32; |
| 233 | break; |
| 234 | default: |
| 235 | dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth); |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | switch (params_rate(params)) { |
| 240 | case 48000: |
| 241 | case 96000: |
| 242 | case 192000: |
| 243 | break; |
| 244 | default: |
| 245 | dev_err(rtd->card->dev, "Unsupported sample rate: %d\n", |
| 246 | params_rate(params)); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK, |
| 251 | 0, SAMSUNG_I2S_OPCLK_PCLK); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | |
| 255 | ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs); |
| 256 | if (ret < 0) |
| 257 | return ret; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static struct snd_soc_ops tm2_hdmi_ops = { |
| 263 | .hw_params = tm2_hdmi_hw_params, |
| 264 | }; |
| 265 | |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 266 | static int tm2_mic_bias(struct snd_soc_dapm_widget *w, |
| 267 | struct snd_kcontrol *kcontrol, int event) |
| 268 | { |
| 269 | struct snd_soc_card *card = w->dapm->card; |
| 270 | struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card); |
| 271 | |
| 272 | switch (event) { |
| 273 | case SND_SOC_DAPM_PRE_PMU: |
| 274 | gpiod_set_value_cansleep(priv->gpio_mic_bias, 1); |
| 275 | break; |
| 276 | case SND_SOC_DAPM_POST_PMD: |
| 277 | gpiod_set_value_cansleep(priv->gpio_mic_bias, 0); |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int tm2_set_bias_level(struct snd_soc_card *card, |
| 285 | struct snd_soc_dapm_context *dapm, |
| 286 | enum snd_soc_bias_level level) |
| 287 | { |
| 288 | struct snd_soc_pcm_runtime *rtd; |
| 289 | |
| 290 | rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name); |
| 291 | |
| 292 | if (dapm->dev != rtd->codec_dai->dev) |
| 293 | return 0; |
| 294 | |
| 295 | switch (level) { |
| 296 | case SND_SOC_BIAS_STANDBY: |
| 297 | if (card->dapm.bias_level == SND_SOC_BIAS_OFF) |
| 298 | tm2_start_sysclk(card); |
| 299 | break; |
| 300 | case SND_SOC_BIAS_OFF: |
| 301 | tm2_stop_sysclk(card); |
| 302 | break; |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static struct snd_soc_aux_dev tm2_speaker_amp_dev; |
| 311 | |
| 312 | static int tm2_late_probe(struct snd_soc_card *card) |
| 313 | { |
| 314 | struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card); |
| 315 | struct snd_soc_dai_link_component dlc = { 0 }; |
| 316 | unsigned int ch_map[] = { 0, 1 }; |
| 317 | struct snd_soc_dai *amp_pdm_dai; |
| 318 | struct snd_soc_pcm_runtime *rtd; |
| 319 | struct snd_soc_dai *aif1_dai; |
| 320 | struct snd_soc_dai *aif2_dai; |
| 321 | int ret; |
| 322 | |
| 323 | rtd = snd_soc_get_pcm_runtime(card, card->dai_link[TM2_DAI_AIF1].name); |
| 324 | aif1_dai = rtd->codec_dai; |
Kuninori Morimoto | 0fe1daa | 2018-02-13 02:03:12 +0000 | [diff] [blame] | 325 | priv->component = rtd->codec_dai->component; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 326 | |
| 327 | ret = snd_soc_dai_set_sysclk(aif1_dai, ARIZONA_CLK_SYSCLK, 0, 0); |
| 328 | if (ret < 0) { |
| 329 | dev_err(aif1_dai->dev, "Failed to set SYSCLK: %d\n", ret); |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | rtd = snd_soc_get_pcm_runtime(card, card->dai_link[TM2_DAI_AIF2].name); |
| 334 | aif2_dai = rtd->codec_dai; |
| 335 | |
| 336 | ret = snd_soc_dai_set_sysclk(aif2_dai, ARIZONA_CLK_ASYNCCLK, 0, 0); |
| 337 | if (ret < 0) { |
| 338 | dev_err(aif2_dai->dev, "Failed to set ASYNCCLK: %d\n", ret); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | dlc.of_node = tm2_speaker_amp_dev.codec_of_node; |
| 343 | amp_pdm_dai = snd_soc_find_dai(&dlc); |
| 344 | if (!amp_pdm_dai) |
| 345 | return -ENODEV; |
| 346 | |
| 347 | /* Set the MAX98504 V/I sense PDM Tx DAI channel mapping */ |
| 348 | ret = snd_soc_dai_set_channel_map(amp_pdm_dai, ARRAY_SIZE(ch_map), |
| 349 | ch_map, 0, NULL); |
| 350 | if (ret < 0) |
| 351 | return ret; |
| 352 | |
| 353 | ret = snd_soc_dai_set_tdm_slot(amp_pdm_dai, 0x3, 0x0, 2, 16); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static const struct snd_kcontrol_new tm2_controls[] = { |
| 361 | SOC_DAPM_PIN_SWITCH("HP"), |
| 362 | SOC_DAPM_PIN_SWITCH("SPK"), |
| 363 | SOC_DAPM_PIN_SWITCH("RCV"), |
| 364 | SOC_DAPM_PIN_SWITCH("VPS"), |
| 365 | SOC_DAPM_PIN_SWITCH("HDMI"), |
| 366 | |
| 367 | SOC_DAPM_PIN_SWITCH("Main Mic"), |
| 368 | SOC_DAPM_PIN_SWITCH("Sub Mic"), |
| 369 | SOC_DAPM_PIN_SWITCH("Third Mic"), |
| 370 | |
| 371 | SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 372 | }; |
| 373 | |
Colin Ian King | fa99125 | 2017-08-13 16:37:15 +0100 | [diff] [blame] | 374 | static const struct snd_soc_dapm_widget tm2_dapm_widgets[] = { |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 375 | SND_SOC_DAPM_HP("HP", NULL), |
| 376 | SND_SOC_DAPM_SPK("SPK", NULL), |
| 377 | SND_SOC_DAPM_SPK("RCV", NULL), |
| 378 | SND_SOC_DAPM_LINE("VPS", NULL), |
| 379 | SND_SOC_DAPM_LINE("HDMI", NULL), |
| 380 | |
| 381 | SND_SOC_DAPM_MIC("Main Mic", tm2_mic_bias), |
| 382 | SND_SOC_DAPM_MIC("Sub Mic", NULL), |
| 383 | SND_SOC_DAPM_MIC("Third Mic", NULL), |
| 384 | |
| 385 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 386 | }; |
| 387 | |
| 388 | static const struct snd_soc_component_driver tm2_component = { |
| 389 | .name = "tm2-audio", |
| 390 | }; |
| 391 | |
| 392 | static struct snd_soc_dai_driver tm2_ext_dai[] = { |
| 393 | { |
| 394 | .name = "Voice call", |
| 395 | .playback = { |
| 396 | .channels_min = 1, |
| 397 | .channels_max = 4, |
| 398 | .rate_min = 8000, |
| 399 | .rate_max = 48000, |
| 400 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | |
| 401 | SNDRV_PCM_RATE_48000), |
| 402 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 403 | }, |
| 404 | .capture = { |
| 405 | .channels_min = 1, |
| 406 | .channels_max = 4, |
| 407 | .rate_min = 8000, |
| 408 | .rate_max = 48000, |
| 409 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | |
| 410 | SNDRV_PCM_RATE_48000), |
| 411 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 412 | }, |
| 413 | }, |
| 414 | { |
| 415 | .name = "Bluetooth", |
| 416 | .playback = { |
| 417 | .channels_min = 1, |
| 418 | .channels_max = 4, |
| 419 | .rate_min = 8000, |
| 420 | .rate_max = 16000, |
| 421 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000), |
| 422 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 423 | }, |
| 424 | .capture = { |
| 425 | .channels_min = 1, |
| 426 | .channels_max = 2, |
| 427 | .rate_min = 8000, |
| 428 | .rate_max = 16000, |
| 429 | .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000), |
| 430 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 431 | }, |
| 432 | }, |
| 433 | }; |
| 434 | |
| 435 | static struct snd_soc_dai_link tm2_dai_links[] = { |
| 436 | { |
| 437 | .name = "WM5110 AIF1", |
| 438 | .stream_name = "HiFi Primary", |
Jaechul Lee | e8d9320 | 2017-09-06 10:04:14 +0900 | [diff] [blame] | 439 | .cpu_dai_name = SAMSUNG_I2S_DAI, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 440 | .codec_dai_name = "wm5110-aif1", |
| 441 | .ops = &tm2_aif1_ops, |
| 442 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 443 | SND_SOC_DAIFMT_CBM_CFM, |
| 444 | }, { |
| 445 | .name = "WM5110 Voice", |
| 446 | .stream_name = "Voice call", |
Jaechul Lee | e8d9320 | 2017-09-06 10:04:14 +0900 | [diff] [blame] | 447 | .cpu_dai_name = SAMSUNG_I2S_DAI, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 448 | .codec_dai_name = "wm5110-aif2", |
| 449 | .ops = &tm2_aif2_ops, |
| 450 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 451 | SND_SOC_DAIFMT_CBM_CFM, |
| 452 | .ignore_suspend = 1, |
| 453 | }, { |
| 454 | .name = "WM5110 BT", |
| 455 | .stream_name = "Bluetooth", |
Jaechul Lee | e8d9320 | 2017-09-06 10:04:14 +0900 | [diff] [blame] | 456 | .cpu_dai_name = SAMSUNG_I2S_DAI, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 457 | .codec_dai_name = "wm5110-aif3", |
| 458 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 459 | SND_SOC_DAIFMT_CBM_CFM, |
| 460 | .ignore_suspend = 1, |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 461 | }, { |
| 462 | .name = "HDMI", |
| 463 | .stream_name = "i2s1", |
| 464 | .ops = &tm2_hdmi_ops, |
| 465 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
| 466 | SND_SOC_DAIFMT_CBS_CFS, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 467 | } |
| 468 | }; |
| 469 | |
| 470 | static struct snd_soc_card tm2_card = { |
| 471 | .owner = THIS_MODULE, |
| 472 | |
| 473 | .dai_link = tm2_dai_links, |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 474 | .controls = tm2_controls, |
| 475 | .num_controls = ARRAY_SIZE(tm2_controls), |
| 476 | .dapm_widgets = tm2_dapm_widgets, |
| 477 | .num_dapm_widgets = ARRAY_SIZE(tm2_dapm_widgets), |
| 478 | .aux_dev = &tm2_speaker_amp_dev, |
| 479 | .num_aux_devs = 1, |
| 480 | |
| 481 | .late_probe = tm2_late_probe, |
| 482 | .set_bias_level = tm2_set_bias_level, |
| 483 | }; |
| 484 | |
| 485 | static int tm2_probe(struct platform_device *pdev) |
| 486 | { |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 487 | struct device_node *cpu_dai_node[2] = {}; |
| 488 | struct device_node *codec_dai_node[2] = {}; |
| 489 | const char *cells_name = NULL; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 490 | struct device *dev = &pdev->dev; |
| 491 | struct snd_soc_card *card = &tm2_card; |
| 492 | struct tm2_machine_priv *priv; |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 493 | struct of_phandle_args args; |
Kuninori Morimoto | 7fe072b | 2018-09-18 01:28:49 +0000 | [diff] [blame^] | 494 | struct snd_soc_dai_link *dai_link; |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 495 | int num_codecs, ret, i; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 496 | |
| 497 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 498 | if (!priv) |
| 499 | return -ENOMEM; |
| 500 | |
| 501 | snd_soc_card_set_drvdata(card, priv); |
| 502 | card->dev = dev; |
| 503 | |
Jaechul Lee | 975b6a9 | 2017-09-06 10:04:15 +0900 | [diff] [blame] | 504 | priv->gpio_mic_bias = devm_gpiod_get(dev, "mic-bias", GPIOD_OUT_HIGH); |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 505 | if (IS_ERR(priv->gpio_mic_bias)) { |
| 506 | dev_err(dev, "Failed to get mic bias gpio\n"); |
| 507 | return PTR_ERR(priv->gpio_mic_bias); |
| 508 | } |
| 509 | |
| 510 | ret = snd_soc_of_parse_card_name(card, "model"); |
| 511 | if (ret < 0) { |
| 512 | dev_err(dev, "Card name is not specified\n"); |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing"); |
| 517 | if (ret < 0) { |
| 518 | dev_err(dev, "Audio routing is not specified or invalid\n"); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | card->aux_dev[0].codec_of_node = of_parse_phandle(dev->of_node, |
| 523 | "audio-amplifier", 0); |
| 524 | if (!card->aux_dev[0].codec_of_node) { |
| 525 | dev_err(dev, "audio-amplifier property invalid or missing\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 529 | num_codecs = of_count_phandle_with_args(dev->of_node, "audio-codec", |
| 530 | NULL); |
| 531 | |
| 532 | /* Skip the HDMI link if not specified in DT */ |
| 533 | if (num_codecs > 1) { |
| 534 | card->num_links = ARRAY_SIZE(tm2_dai_links); |
| 535 | cells_name = "#sound-dai-cells"; |
| 536 | } else { |
| 537 | card->num_links = ARRAY_SIZE(tm2_dai_links) - 1; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 540 | for (i = 0; i < num_codecs; i++) { |
| 541 | struct of_phandle_args args; |
| 542 | |
| 543 | ret = of_parse_phandle_with_args(dev->of_node, "i2s-controller", |
| 544 | cells_name, i, &args); |
| 545 | if (!args.np) { |
| 546 | dev_err(dev, "i2s-controller property parse error: %d\n", i); |
| 547 | ret = -EINVAL; |
| 548 | goto dai_node_put; |
| 549 | } |
| 550 | cpu_dai_node[i] = args.np; |
| 551 | |
| 552 | codec_dai_node[i] = of_parse_phandle(dev->of_node, |
| 553 | "audio-codec", i); |
| 554 | if (!codec_dai_node[i]) { |
| 555 | dev_err(dev, "audio-codec property parse error\n"); |
| 556 | ret = -EINVAL; |
| 557 | goto dai_node_put; |
| 558 | } |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 561 | /* Initialize WM5110 - I2S and HDMI - I2S1 DAI links */ |
Kuninori Morimoto | 7fe072b | 2018-09-18 01:28:49 +0000 | [diff] [blame^] | 562 | for_each_card_prelinks(card, i, dai_link) { |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 563 | unsigned int dai_index = 0; /* WM5110 */ |
| 564 | |
Kuninori Morimoto | 7fe072b | 2018-09-18 01:28:49 +0000 | [diff] [blame^] | 565 | dai_link->cpu_name = NULL; |
| 566 | dai_link->platform_name = NULL; |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 567 | |
| 568 | if (num_codecs > 1 && i == card->num_links - 1) |
| 569 | dai_index = 1; /* HDMI */ |
| 570 | |
Kuninori Morimoto | 7fe072b | 2018-09-18 01:28:49 +0000 | [diff] [blame^] | 571 | dai_link->codec_of_node = codec_dai_node[dai_index]; |
| 572 | dai_link->cpu_of_node = cpu_dai_node[dai_index]; |
| 573 | dai_link->platform_of_node = cpu_dai_node[dai_index]; |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | if (num_codecs > 1) { |
| 577 | /* HDMI DAI link (I2S1) */ |
| 578 | i = card->num_links - 1; |
| 579 | |
| 580 | ret = of_parse_phandle_with_fixed_args(dev->of_node, |
| 581 | "audio-codec", 0, 1, &args); |
| 582 | if (ret) { |
| 583 | dev_err(dev, "audio-codec property parse error\n"); |
| 584 | goto dai_node_put; |
| 585 | } |
| 586 | |
| 587 | ret = snd_soc_get_dai_name(&args, &card->dai_link[i].codec_dai_name); |
| 588 | if (ret) { |
| 589 | dev_err(dev, "Unable to get codec_dai_name\n"); |
| 590 | goto dai_node_put; |
| 591 | } |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | ret = devm_snd_soc_register_component(dev, &tm2_component, |
| 595 | tm2_ext_dai, ARRAY_SIZE(tm2_ext_dai)); |
| 596 | if (ret < 0) { |
| 597 | dev_err(dev, "Failed to register component: %d\n", ret); |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 598 | goto dai_node_put; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | ret = devm_snd_soc_register_card(dev, card); |
| 602 | if (ret < 0) { |
| 603 | dev_err(dev, "Failed to register card: %d\n", ret); |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 604 | goto dai_node_put; |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 607 | dai_node_put: |
| 608 | for (i = 0; i < num_codecs; i++) { |
| 609 | of_node_put(codec_dai_node[i]); |
| 610 | of_node_put(cpu_dai_node[i]); |
| 611 | } |
| 612 | |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 613 | of_node_put(card->aux_dev[0].codec_of_node); |
Sylwester Nawrocki | 8d1513c | 2018-02-12 17:15:37 +0100 | [diff] [blame] | 614 | |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static int tm2_pm_prepare(struct device *dev) |
| 619 | { |
| 620 | struct snd_soc_card *card = dev_get_drvdata(dev); |
| 621 | |
| 622 | return tm2_stop_sysclk(card); |
| 623 | } |
| 624 | |
| 625 | static void tm2_pm_complete(struct device *dev) |
| 626 | { |
| 627 | struct snd_soc_card *card = dev_get_drvdata(dev); |
| 628 | |
| 629 | tm2_start_sysclk(card); |
| 630 | } |
| 631 | |
Colin Ian King | fa99125 | 2017-08-13 16:37:15 +0100 | [diff] [blame] | 632 | static const struct dev_pm_ops tm2_pm_ops = { |
Sylwester Nawrocki | 1bfbc26 | 2016-11-02 17:05:45 +0100 | [diff] [blame] | 633 | .prepare = tm2_pm_prepare, |
| 634 | .suspend = snd_soc_suspend, |
| 635 | .resume = snd_soc_resume, |
| 636 | .complete = tm2_pm_complete, |
| 637 | .freeze = snd_soc_suspend, |
| 638 | .thaw = snd_soc_resume, |
| 639 | .poweroff = snd_soc_poweroff, |
| 640 | .restore = snd_soc_resume, |
| 641 | }; |
| 642 | |
| 643 | static const struct of_device_id tm2_of_match[] = { |
| 644 | { .compatible = "samsung,tm2-audio" }, |
| 645 | { }, |
| 646 | }; |
| 647 | MODULE_DEVICE_TABLE(of, tm2_of_match); |
| 648 | |
| 649 | static struct platform_driver tm2_driver = { |
| 650 | .driver = { |
| 651 | .name = "tm2-audio", |
| 652 | .pm = &tm2_pm_ops, |
| 653 | .of_match_table = tm2_of_match, |
| 654 | }, |
| 655 | .probe = tm2_probe, |
| 656 | }; |
| 657 | module_platform_driver(tm2_driver); |
| 658 | |
| 659 | MODULE_AUTHOR("Inha Song <ideal.song@samsung.com>"); |
| 660 | MODULE_DESCRIPTION("ALSA SoC Exynos TM2 Audio Support"); |
| 661 | MODULE_LICENSE("GPL v2"); |