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