Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 1 | /* |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 2 | * Tobermory audio support |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 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 | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 22 | static int tobermory_set_bias_level(struct snd_soc_card *card, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 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 | 631f8e9 | 2014-01-30 18:26:07 +0000 | [diff] [blame^] | 47 | snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 48 | 0, 0, 0); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 49 | return ret; |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 50 | } |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 51 | } |
| 52 | break; |
| 53 | |
| 54 | default: |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 61 | static int tobermory_set_bias_level_post(struct snd_soc_card *card, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 62 | struct snd_soc_dapm_context *dapm, |
| 63 | enum snd_soc_bias_level level) |
| 64 | { |
| 65 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 66 | int ret; |
| 67 | |
Mark Brown | f79e7ff | 2011-08-21 12:20:00 +0100 | [diff] [blame] | 68 | if (dapm->dev != codec_dai->dev) |
| 69 | return 0; |
| 70 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 71 | switch (level) { |
| 72 | case SND_SOC_BIAS_STANDBY: |
| 73 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 74 | 32768, SND_SOC_CLOCK_IN); |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 75 | if (ret < 0) { |
| 76 | pr_err("Failed to switch away from FLL: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 77 | return ret; |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 78 | } |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 79 | |
| 80 | ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL, |
| 81 | 0, 0, 0); |
| 82 | if (ret < 0) { |
Mark Brown | 67d0c47 | 2011-06-29 14:07:24 -0700 | [diff] [blame] | 83 | pr_err("Failed to stop FLL: %d\n", ret); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | break; |
| 87 | |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | dapm->bias_level = level; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 97 | static int tobermory_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 98 | struct snd_pcm_hw_params *params) |
| 99 | { |
Mark Brown | ef473fe | 2011-09-23 16:05:00 +0100 | [diff] [blame] | 100 | sample_rate = params_rate(params); |
| 101 | |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 105 | static struct snd_soc_ops tobermory_ops = { |
| 106 | .hw_params = tobermory_hw_params, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 109 | static struct snd_soc_dai_link tobermory_dai[] = { |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 110 | { |
| 111 | .name = "CPU", |
| 112 | .stream_name = "CPU", |
| 113 | .cpu_dai_name = "samsung-i2s.0", |
| 114 | .codec_dai_name = "wm8962", |
Padmavathi Venna | a08485d | 2012-12-07 13:59:21 +0530 | [diff] [blame] | 115 | .platform_name = "samsung-i2s.0", |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 116 | .codec_name = "wm8962.1-001a", |
Mark Brown | 44fdd43 | 2011-09-27 16:42:27 +0100 | [diff] [blame] | 117 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
| 118 | | SND_SOC_DAIFMT_CBM_CFM, |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 119 | .ops = &tobermory_ops, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 120 | }, |
| 121 | }; |
| 122 | |
| 123 | static const struct snd_kcontrol_new controls[] = { |
| 124 | SOC_DAPM_PIN_SWITCH("Main Speaker"), |
Mark Brown | 8c75615 | 2011-09-23 16:46:24 +0100 | [diff] [blame] | 125 | SOC_DAPM_PIN_SWITCH("DMIC"), |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static struct snd_soc_dapm_widget widgets[] = { |
| 129 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 130 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 131 | |
| 132 | SND_SOC_DAPM_MIC("DMIC", NULL), |
Mark Brown | 7564463 | 2011-09-23 16:23:11 +0100 | [diff] [blame] | 133 | SND_SOC_DAPM_MIC("AMIC", NULL), |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 134 | |
| 135 | SND_SOC_DAPM_SPK("Main Speaker", NULL), |
| 136 | }; |
| 137 | |
| 138 | static struct snd_soc_dapm_route audio_paths[] = { |
| 139 | { "Headphone", NULL, "HPOUTL" }, |
| 140 | { "Headphone", NULL, "HPOUTR" }, |
| 141 | |
| 142 | { "Main Speaker", NULL, "SPKOUTL" }, |
| 143 | { "Main Speaker", NULL, "SPKOUTR" }, |
| 144 | |
Mark Brown | 086d7f8 | 2011-09-23 16:22:48 +0100 | [diff] [blame] | 145 | { "Headset Mic", NULL, "MICBIAS" }, |
| 146 | { "IN4L", NULL, "Headset Mic" }, |
| 147 | { "IN4R", NULL, "Headset Mic" }, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 148 | |
Mark Brown | 7564463 | 2011-09-23 16:23:11 +0100 | [diff] [blame] | 149 | { "AMIC", NULL, "MICBIAS" }, |
| 150 | { "IN1L", NULL, "AMIC" }, |
| 151 | { "IN1R", NULL, "AMIC" }, |
| 152 | |
Mark Brown | 086d7f8 | 2011-09-23 16:22:48 +0100 | [diff] [blame] | 153 | { "DMIC", NULL, "MICBIAS" }, |
| 154 | { "DMICDAT", NULL, "DMIC" }, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 157 | static struct snd_soc_jack tobermory_headset; |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 158 | |
| 159 | /* Headset jack detection DAPM pins */ |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 160 | static struct snd_soc_jack_pin tobermory_headset_pins[] = { |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 161 | { |
| 162 | .pin = "Headset Mic", |
| 163 | .mask = SND_JACK_MICROPHONE, |
| 164 | }, |
| 165 | { |
| 166 | .pin = "Headphone", |
| 167 | .mask = SND_JACK_MICROPHONE, |
| 168 | }, |
| 169 | }; |
| 170 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 171 | static int tobermory_late_probe(struct snd_soc_card *card) |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 172 | { |
| 173 | struct snd_soc_codec *codec = card->rtd[0].codec; |
| 174 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 175 | int ret; |
| 176 | |
| 177 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK, |
| 178 | 32768, SND_SOC_CLOCK_IN); |
| 179 | if (ret < 0) |
| 180 | return ret; |
| 181 | |
| 182 | ret = snd_soc_jack_new(codec, "Headset", |
| 183 | SND_JACK_HEADSET | SND_JACK_BTN_0, |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 184 | &tobermory_headset); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 185 | if (ret) |
| 186 | return ret; |
| 187 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 188 | ret = snd_soc_jack_add_pins(&tobermory_headset, |
| 189 | ARRAY_SIZE(tobermory_headset_pins), |
| 190 | tobermory_headset_pins); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 191 | if (ret) |
| 192 | return ret; |
| 193 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 194 | wm8962_mic_detect(codec, &tobermory_headset); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 199 | static struct snd_soc_card tobermory = { |
| 200 | .name = "Tobermory", |
Axel Lin | 095d79d | 2011-12-22 10:53:15 +0800 | [diff] [blame] | 201 | .owner = THIS_MODULE, |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 202 | .dai_link = tobermory_dai, |
| 203 | .num_links = ARRAY_SIZE(tobermory_dai), |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 204 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 205 | .set_bias_level = tobermory_set_bias_level, |
| 206 | .set_bias_level_post = tobermory_set_bias_level_post, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 207 | |
| 208 | .controls = controls, |
| 209 | .num_controls = ARRAY_SIZE(controls), |
| 210 | .dapm_widgets = widgets, |
| 211 | .num_dapm_widgets = ARRAY_SIZE(widgets), |
| 212 | .dapm_routes = audio_paths, |
| 213 | .num_dapm_routes = ARRAY_SIZE(audio_paths), |
Mark Brown | 39afd66 | 2011-11-23 21:33:07 +0000 | [diff] [blame] | 214 | .fully_routed = true, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 215 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 216 | .late_probe = tobermory_late_probe, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 217 | }; |
| 218 | |
Bill Pemberton | fdca21a | 2012-12-07 09:26:15 -0500 | [diff] [blame] | 219 | static int tobermory_probe(struct platform_device *pdev) |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 220 | { |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 221 | struct snd_soc_card *card = &tobermory; |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 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 | |
Bill Pemberton | fdca21a | 2012-12-07 09:26:15 -0500 | [diff] [blame] | 236 | static int tobermory_remove(struct platform_device *pdev) |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 237 | { |
| 238 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 239 | |
| 240 | snd_soc_unregister_card(card); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 245 | static struct platform_driver tobermory_driver = { |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 246 | .driver = { |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 247 | .name = "tobermory", |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 248 | .owner = THIS_MODULE, |
| 249 | .pm = &snd_soc_pm_ops, |
| 250 | }, |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 251 | .probe = tobermory_probe, |
Bill Pemberton | fdca21a | 2012-12-07 09:26:15 -0500 | [diff] [blame] | 252 | .remove = tobermory_remove, |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 253 | }; |
| 254 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 255 | module_platform_driver(tobermory_driver); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 256 | |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 257 | MODULE_DESCRIPTION("Tobermory audio support"); |
Mark Brown | 22cb839 | 2011-04-25 18:30:45 +0100 | [diff] [blame] | 258 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 259 | MODULE_LICENSE("GPL"); |
Mark Brown | 6414261 | 2011-11-30 13:30:27 +0000 | [diff] [blame] | 260 | MODULE_ALIAS("platform:tobermory"); |