| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
// |
| 3 |
// Tobermory audio support |
| 4 |
// |
| 5 |
// Copyright 2011 Wolfson Microelectronics |
| 6 |
|
| 7 |
#include <sound/soc.h> |
| 8 |
#include <sound/soc-dapm.h> |
| 9 |
#include <sound/jack.h> |
| 10 |
#include <linux/module.h> |
| 11 |
|
| 12 |
#include "../codecs/wm8962.h" |
| 13 |
|
| 14 |
static int sample_rate = 44100; |
| 15 |
|
| 16 |
static int tobermory_set_bias_level(struct snd_soc_card *card, |
| 17 |
struct snd_soc_dapm_context *dapm, |
| 18 |
enum snd_soc_bias_level level) |
| 19 |
{ |
| 20 |
struct snd_soc_pcm_runtime *rtd; |
| 21 |
struct snd_soc_dai *codec_dai; |
| 22 |
int ret; |
| 23 |
|
| 24 |
rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); |
| 25 |
codec_dai = snd_soc_rtd_to_codec(rtd, 0); |
| 26 |
|
| 27 |
if (snd_soc_dapm_to_dev(dapm) != codec_dai->dev) |
| 28 |
return 0; |
| 29 |
|
| 30 |
switch (level) { |
| 31 |
case SND_SOC_BIAS_PREPARE: |
| 32 |
if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_STANDBY) { |
| 33 |
ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 34 |
WM8962_FLL_MCLK, 32768, |
| 35 |
sample_rate * 512); |
| 36 |
if (ret < 0) |
| 37 |
pr_err("Failed to start FLL: %d\n", ret); |
| 38 |
|
| 39 |
ret = snd_soc_dai_set_sysclk(codec_dai, |
| 40 |
WM8962_SYSCLK_FLL, |
| 41 |
sample_rate * 512, |
| 42 |
SND_SOC_CLOCK_IN); |
| 43 |
if (ret < 0) { |
| 44 |
pr_err("Failed to set SYSCLK: %d\n", ret); |
| 45 |
snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 46 |
0, 0, 0); |
| 47 |
return ret; |
| 48 |
} |
| 49 |
} |
| 50 |
break; |
| 51 |
|
| 52 |
default: |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static int tobermory_set_bias_level_post(struct snd_soc_card *card, |
| 60 |
struct snd_soc_dapm_context *dapm, |
| 61 |
enum snd_soc_bias_level level) |
| 62 |
{ |
| 63 |
struct snd_soc_pcm_runtime *rtd; |
| 64 |
struct snd_soc_dai *codec_dai; |
| 65 |
int ret; |
| 66 |
|
| 67 |
rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); |
| 68 |
codec_dai = snd_soc_rtd_to_codec(rtd, 0); |
| 69 |
|
| 70 |
if (snd_soc_dapm_to_dev(dapm) != codec_dai->dev) |
| 71 |
return 0; |
| 72 |
|
| 73 |
switch (level) { |
| 74 |
case SND_SOC_BIAS_STANDBY: |
| 75 |
ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 76 |
32768, SND_SOC_CLOCK_IN); |
| 77 |
if (ret < 0) { |
| 78 |
pr_err("Failed to switch away from FLL: %d\n", ret); |
| 79 |
return ret; |
| 80 |
} |
| 81 |
|
| 82 |
ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 83 |
0, 0, 0); |
| 84 |
if (ret < 0) { |
| 85 |
pr_err("Failed to stop FLL: %d\n", ret); |
| 86 |
return ret; |
| 87 |
} |
| 88 |
break; |
| 89 |
|
| 90 |
default: |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static int tobermory_hw_params(struct snd_pcm_substream *substream, |
| 98 |
struct snd_pcm_hw_params *params) |
| 99 |
{ |
| 100 |
sample_rate = params_rate(params); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static const struct snd_soc_ops tobermory_ops = { |
| 106 |
.hw_params = tobermory_hw_params, |
| 107 |
}; |
| 108 |
|
| 109 |
SND_SOC_DAILINK_DEFS(cpu, |
| 110 |
DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")), |
| 111 |
DAILINK_COMP_ARRAY(COMP_CODEC("wm8962.1-001a", "wm8962")), |
| 112 |
DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0"))); |
| 113 |
|
| 114 |
static struct snd_soc_dai_link tobermory_dai[] = { |
| 115 |
{ |
| 116 |
.name = "CPU", |
| 117 |
.stream_name = "CPU", |
| 118 |
.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
| 119 |
| SND_SOC_DAIFMT_CBP_CFP, |
| 120 |
.ops = &tobermory_ops, |
| 121 |
SND_SOC_DAILINK_REG(cpu), |
| 122 |
}, |
| 123 |
}; |
| 124 |
|
| 125 |
static const struct snd_kcontrol_new controls[] = { |
| 126 |
SOC_DAPM_PIN_SWITCH("Main Speaker"), |
| 127 |
SOC_DAPM_PIN_SWITCH("DMIC"), |
| 128 |
}; |
| 129 |
|
| 130 |
static const struct snd_soc_dapm_widget widgets[] = { |
| 131 |
SND_SOC_DAPM_HP("Headphone", NULL), |
| 132 |
SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 133 |
|
| 134 |
SND_SOC_DAPM_MIC("DMIC", NULL), |
| 135 |
SND_SOC_DAPM_MIC("AMIC", NULL), |
| 136 |
|
| 137 |
SND_SOC_DAPM_SPK("Main Speaker", NULL), |
| 138 |
}; |
| 139 |
|
| 140 |
static const struct snd_soc_dapm_route audio_paths[] = { |
| 141 |
{ "Headphone", NULL, "HPOUTL" }, |
| 142 |
{ "Headphone", NULL, "HPOUTR" }, |
| 143 |
|
| 144 |
{ "Main Speaker", NULL, "SPKOUTL" }, |
| 145 |
{ "Main Speaker", NULL, "SPKOUTR" }, |
| 146 |
|
| 147 |
{ "Headset Mic", NULL, "MICBIAS" }, |
| 148 |
{ "IN4L", NULL, "Headset Mic" }, |
| 149 |
{ "IN4R", NULL, "Headset Mic" }, |
| 150 |
|
| 151 |
{ "AMIC", NULL, "MICBIAS" }, |
| 152 |
{ "IN1L", NULL, "AMIC" }, |
| 153 |
{ "IN1R", NULL, "AMIC" }, |
| 154 |
|
| 155 |
{ "DMIC", NULL, "MICBIAS" }, |
| 156 |
{ "DMICDAT", NULL, "DMIC" }, |
| 157 |
}; |
| 158 |
|
| 159 |
static struct snd_soc_jack tobermory_headset; |
| 160 |
|
| 161 |
/* Headset jack detection DAPM pins */ |
| 162 |
static struct snd_soc_jack_pin tobermory_headset_pins[] = { |
| 163 |
{ |
| 164 |
.pin = "Headset Mic", |
| 165 |
.mask = SND_JACK_MICROPHONE, |
| 166 |
}, |
| 167 |
{ |
| 168 |
.pin = "Headphone", |
| 169 |
.mask = SND_JACK_MICROPHONE, |
| 170 |
}, |
| 171 |
}; |
| 172 |
|
| 173 |
static int tobermory_late_probe(struct snd_soc_card *card) |
| 174 |
{ |
| 175 |
struct snd_soc_pcm_runtime *rtd; |
| 176 |
struct snd_soc_component *component; |
| 177 |
struct snd_soc_dai *codec_dai; |
| 178 |
int ret; |
| 179 |
|
| 180 |
rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); |
| 181 |
component = snd_soc_rtd_to_codec(rtd, 0)->component; |
| 182 |
codec_dai = snd_soc_rtd_to_codec(rtd, 0); |
| 183 |
|
| 184 |
ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 185 |
32768, SND_SOC_CLOCK_IN); |
| 186 |
if (ret < 0) |
| 187 |
return ret; |
| 188 |
|
| 189 |
ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | |
| 190 |
SND_JACK_BTN_0, &tobermory_headset, |
| 191 |
tobermory_headset_pins, |
| 192 |
ARRAY_SIZE(tobermory_headset_pins)); |
| 193 |
if (ret) |
| 194 |
return ret; |
| 195 |
|
| 196 |
wm8962_mic_detect(component, &tobermory_headset); |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
static struct snd_soc_card tobermory = { |
| 202 |
.name = "Tobermory", |
| 203 |
.owner = THIS_MODULE, |
| 204 |
.dai_link = tobermory_dai, |
| 205 |
.num_links = ARRAY_SIZE(tobermory_dai), |
| 206 |
|
| 207 |
.set_bias_level = tobermory_set_bias_level, |
| 208 |
.set_bias_level_post = tobermory_set_bias_level_post, |
| 209 |
|
| 210 |
.controls = controls, |
| 211 |
.num_controls = ARRAY_SIZE(controls), |
| 212 |
.dapm_widgets = widgets, |
| 213 |
.num_dapm_widgets = ARRAY_SIZE(widgets), |
| 214 |
.dapm_routes = audio_paths, |
| 215 |
.num_dapm_routes = ARRAY_SIZE(audio_paths), |
| 216 |
.fully_routed = true, |
| 217 |
|
| 218 |
.late_probe = tobermory_late_probe, |
| 219 |
}; |
| 220 |
|
| 221 |
static int tobermory_probe(struct platform_device *pdev) |
| 222 |
{ |
| 223 |
struct snd_soc_card *card = &tobermory; |
| 224 |
int ret; |
| 225 |
|
| 226 |
card->dev = &pdev->dev; |
| 227 |
|
| 228 |
ret = devm_snd_soc_register_card(&pdev->dev, card); |
| 229 |
if (ret) |
| 230 |
dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n"); |
| 231 |
|
| 232 |
return ret; |
| 233 |
} |
| 234 |
|
| 235 |
static struct platform_driver tobermory_driver = { |
| 236 |
.driver = { |
| 237 |
.name = "tobermory", |
| 238 |
.pm = &snd_soc_pm_ops, |
| 239 |
}, |
| 240 |
.probe = tobermory_probe, |
| 241 |
}; |
| 242 |
|
| 243 |
module_platform_driver(tobermory_driver); |
| 244 |
|
| 245 |
MODULE_DESCRIPTION("Tobermory audio support"); |
| 246 |
MODULE_AUTHOR("Mark Brown <[email protected]>"); |
| 247 |
MODULE_LICENSE("GPL"); |
| 248 |
MODULE_ALIAS("platform:tobermory"); |