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