Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Speyside with WM8962 audio support |
| 3 | * |
| 4 | * Copyright 2011 Wolfson Microelectronics |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <sound/soc.h> |
| 13 | #include <sound/soc-dapm.h> |
| 14 | #include <sound/jack.h> |
| 15 | #include <linux/gpio.h> |
| 16 | |
| 17 | #include "../codecs/wm8962.h" |
| 18 | |
Mark Brown | ef473fe | 2011-09-23 16:05:00 +0100 | [diff] [blame] | 19 | static int sample_rate = 44100; |
| 20 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 21 | static int speyside_wm8962_set_bias_level(struct snd_soc_card *card, |
| 22 | struct snd_soc_dapm_context *dapm, |
| 23 | enum snd_soc_bias_level level) |
| 24 | { |
| 25 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 26 | int ret; |
| 27 | |
Mark Brown | f79e7ff | 2011-08-21 12:20:00 +0100 | [diff] [blame] | 28 | if (dapm->dev != codec_dai->dev) |
| 29 | return 0; |
| 30 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 31 | switch (level) { |
| 32 | case SND_SOC_BIAS_PREPARE: |
| 33 | if (dapm->bias_level == SND_SOC_BIAS_STANDBY) { |
| 34 | ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 35 | WM8962_FLL_MCLK, 32768, |
Mark Brown | ef473fe | 2011-09-23 16:05:00 +0100 | [diff] [blame] | 36 | sample_rate * 512); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 37 | if (ret < 0) |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 38 | pr_err("Failed to start FLL: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 39 | |
| 40 | ret = snd_soc_dai_set_sysclk(codec_dai, |
| 41 | WM8962_SYSCLK_FLL, |
Mark Brown | ef473fe | 2011-09-23 16:05:00 +0100 | [diff] [blame] | 42 | sample_rate * 512, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 43 | SND_SOC_CLOCK_IN); |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 44 | if (ret < 0) { |
Mark Brown | 371e730 | 2011-08-04 10:54:17 +0900 | [diff] [blame] | 45 | pr_err("Failed to set SYSCLK: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 46 | return ret; |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 47 | } |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 48 | } |
| 49 | break; |
| 50 | |
| 51 | default: |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int speyside_wm8962_set_bias_level_post(struct snd_soc_card *card, |
| 59 | struct snd_soc_dapm_context *dapm, |
| 60 | enum snd_soc_bias_level level) |
| 61 | { |
| 62 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 63 | int ret; |
| 64 | |
Mark Brown | f79e7ff | 2011-08-21 12:20:00 +0100 | [diff] [blame] | 65 | if (dapm->dev != codec_dai->dev) |
| 66 | return 0; |
| 67 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 68 | switch (level) { |
| 69 | case SND_SOC_BIAS_STANDBY: |
| 70 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 71 | 32768, SND_SOC_CLOCK_IN); |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 72 | if (ret < 0) { |
| 73 | pr_err("Failed to switch away from FLL: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 74 | return ret; |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 75 | } |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 76 | |
| 77 | ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 78 | 0, 0, 0); |
| 79 | if (ret < 0) { |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 80 | pr_err("Failed to stop FLL: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | break; |
| 84 | |
| 85 | default: |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | dapm->bias_level = level; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int speyside_wm8962_hw_params(struct snd_pcm_substream *substream, |
| 95 | struct snd_pcm_hw_params *params) |
| 96 | { |
| 97 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 98 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 99 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 100 | int ret; |
| 101 | |
| 102 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
| 103 | | SND_SOC_DAIFMT_NB_NF |
| 104 | | SND_SOC_DAIFMT_CBM_CFM); |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | |
| 108 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
| 109 | | SND_SOC_DAIFMT_NB_NF |
| 110 | | SND_SOC_DAIFMT_CBM_CFM); |
| 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
Mark Brown | ef473fe | 2011-09-23 16:05:00 +0100 | [diff] [blame] | 114 | sample_rate = params_rate(params); |
| 115 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static struct snd_soc_ops speyside_wm8962_ops = { |
| 120 | .hw_params = speyside_wm8962_hw_params, |
| 121 | }; |
| 122 | |
| 123 | static struct snd_soc_dai_link speyside_wm8962_dai[] = { |
| 124 | { |
| 125 | .name = "CPU", |
| 126 | .stream_name = "CPU", |
| 127 | .cpu_dai_name = "samsung-i2s.0", |
| 128 | .codec_dai_name = "wm8962", |
| 129 | .platform_name = "samsung-audio", |
| 130 | .codec_name = "wm8962.1-001a", |
| 131 | .ops = &speyside_wm8962_ops, |
| 132 | }, |
| 133 | }; |
| 134 | |
| 135 | static const struct snd_kcontrol_new controls[] = { |
| 136 | SOC_DAPM_PIN_SWITCH("Main Speaker"), |
| 137 | }; |
| 138 | |
| 139 | static struct snd_soc_dapm_widget widgets[] = { |
| 140 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 141 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 142 | |
| 143 | SND_SOC_DAPM_MIC("DMIC", NULL), |
Mark Brown | 7564463 | 2011-09-23 16:23:11 +0100 | [diff] [blame^] | 144 | SND_SOC_DAPM_MIC("AMIC", NULL), |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 145 | |
| 146 | SND_SOC_DAPM_SPK("Main Speaker", NULL), |
| 147 | }; |
| 148 | |
| 149 | static struct snd_soc_dapm_route audio_paths[] = { |
| 150 | { "Headphone", NULL, "HPOUTL" }, |
| 151 | { "Headphone", NULL, "HPOUTR" }, |
| 152 | |
| 153 | { "Main Speaker", NULL, "SPKOUTL" }, |
| 154 | { "Main Speaker", NULL, "SPKOUTR" }, |
| 155 | |
Mark Brown | 086d7f8 | 2011-09-23 16:22:48 +0100 | [diff] [blame] | 156 | { "Headset Mic", NULL, "MICBIAS" }, |
| 157 | { "IN4L", NULL, "Headset Mic" }, |
| 158 | { "IN4R", NULL, "Headset Mic" }, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 159 | |
Mark Brown | 7564463 | 2011-09-23 16:23:11 +0100 | [diff] [blame^] | 160 | { "AMIC", NULL, "MICBIAS" }, |
| 161 | { "IN1L", NULL, "AMIC" }, |
| 162 | { "IN1R", NULL, "AMIC" }, |
| 163 | |
Mark Brown | 086d7f8 | 2011-09-23 16:22:48 +0100 | [diff] [blame] | 164 | { "DMIC", NULL, "MICBIAS" }, |
| 165 | { "DMICDAT", NULL, "DMIC" }, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static struct snd_soc_jack speyside_wm8962_headset; |
| 169 | |
| 170 | /* Headset jack detection DAPM pins */ |
| 171 | static struct snd_soc_jack_pin speyside_wm8962_headset_pins[] = { |
| 172 | { |
| 173 | .pin = "Headset Mic", |
| 174 | .mask = SND_JACK_MICROPHONE, |
| 175 | }, |
| 176 | { |
| 177 | .pin = "Headphone", |
| 178 | .mask = SND_JACK_MICROPHONE, |
| 179 | }, |
| 180 | }; |
| 181 | |
| 182 | static int speyside_wm8962_late_probe(struct snd_soc_card *card) |
| 183 | { |
| 184 | struct snd_soc_codec *codec = card->rtd[0].codec; |
| 185 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 186 | int ret; |
| 187 | |
| 188 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 189 | 32768, SND_SOC_CLOCK_IN); |
| 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | |
| 193 | ret = snd_soc_jack_new(codec, "Headset", |
| 194 | SND_JACK_HEADSET | SND_JACK_BTN_0, |
| 195 | &speyside_wm8962_headset); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | ret = snd_soc_jack_add_pins(&speyside_wm8962_headset, |
| 200 | ARRAY_SIZE(speyside_wm8962_headset_pins), |
| 201 | speyside_wm8962_headset_pins); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | wm8962_mic_detect(codec, &speyside_wm8962_headset); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static struct snd_soc_card speyside_wm8962 = { |
| 211 | .name = "Speyside WM8962", |
| 212 | .dai_link = speyside_wm8962_dai, |
| 213 | .num_links = ARRAY_SIZE(speyside_wm8962_dai), |
| 214 | |
| 215 | .set_bias_level = speyside_wm8962_set_bias_level, |
| 216 | .set_bias_level_post = speyside_wm8962_set_bias_level_post, |
| 217 | |
| 218 | .controls = controls, |
| 219 | .num_controls = ARRAY_SIZE(controls), |
| 220 | .dapm_widgets = widgets, |
| 221 | .num_dapm_widgets = ARRAY_SIZE(widgets), |
| 222 | .dapm_routes = audio_paths, |
| 223 | .num_dapm_routes = ARRAY_SIZE(audio_paths), |
| 224 | |
| 225 | .late_probe = speyside_wm8962_late_probe, |
| 226 | }; |
| 227 | |
| 228 | static __devinit int speyside_wm8962_probe(struct platform_device *pdev) |
| 229 | { |
| 230 | struct snd_soc_card *card = &speyside_wm8962; |
| 231 | int ret; |
| 232 | |
| 233 | card->dev = &pdev->dev; |
| 234 | |
| 235 | ret = snd_soc_register_card(card); |
| 236 | if (ret) { |
| 237 | dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", |
| 238 | ret); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int __devexit speyside_wm8962_remove(struct platform_device *pdev) |
| 246 | { |
| 247 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 248 | |
| 249 | snd_soc_unregister_card(card); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static struct platform_driver speyside_wm8962_driver = { |
| 255 | .driver = { |
| 256 | .name = "speyside-wm8962", |
| 257 | .owner = THIS_MODULE, |
| 258 | .pm = &snd_soc_pm_ops, |
| 259 | }, |
| 260 | .probe = speyside_wm8962_probe, |
| 261 | .remove = __devexit_p(speyside_wm8962_remove), |
| 262 | }; |
| 263 | |
| 264 | static int __init speyside_wm8962_audio_init(void) |
| 265 | { |
| 266 | return platform_driver_register(&speyside_wm8962_driver); |
| 267 | } |
| 268 | module_init(speyside_wm8962_audio_init); |
| 269 | |
| 270 | static void __exit speyside_wm8962_audio_exit(void) |
| 271 | { |
| 272 | platform_driver_unregister(&speyside_wm8962_driver); |
| 273 | } |
| 274 | module_exit(speyside_wm8962_audio_exit); |
| 275 | |
| 276 | MODULE_DESCRIPTION("Speyside WM8962 audio support"); |
| 277 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 278 | MODULE_LICENSE("GPL"); |
| 279 | MODULE_ALIAS("platform:speyside-wm8962"); |