Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Littlemill 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 | #include <linux/module.h> |
| 17 | |
| 18 | #include "../codecs/wm8994.h" |
| 19 | |
| 20 | static int sample_rate = 44100; |
| 21 | |
| 22 | static int littlemill_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 | |
| 29 | if (dapm->dev != codec_dai->dev) |
| 30 | return 0; |
| 31 | |
| 32 | switch (level) { |
| 33 | case SND_SOC_BIAS_PREPARE: |
| 34 | /* |
| 35 | * If we've not already clocked things via hw_params() |
| 36 | * then do so now, otherwise these are noops. |
| 37 | */ |
| 38 | if (dapm->bias_level == SND_SOC_BIAS_STANDBY) { |
| 39 | ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, |
| 40 | WM8994_FLL_SRC_MCLK2, 32768, |
| 41 | sample_rate * 512); |
| 42 | if (ret < 0) { |
| 43 | pr_err("Failed to start FLL: %d\n", ret); |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | ret = snd_soc_dai_set_sysclk(codec_dai, |
| 48 | WM8994_SYSCLK_FLL1, |
| 49 | sample_rate * 512, |
| 50 | SND_SOC_CLOCK_IN); |
| 51 | if (ret < 0) { |
| 52 | pr_err("Failed to set SYSCLK: %d\n", ret); |
| 53 | return ret; |
| 54 | } |
| 55 | } |
| 56 | break; |
| 57 | |
| 58 | default: |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int littlemill_set_bias_level_post(struct snd_soc_card *card, |
| 66 | struct snd_soc_dapm_context *dapm, |
| 67 | enum snd_soc_bias_level level) |
| 68 | { |
| 69 | struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; |
| 70 | int ret; |
| 71 | |
| 72 | if (dapm->dev != codec_dai->dev) |
| 73 | return 0; |
| 74 | |
| 75 | switch (level) { |
| 76 | case SND_SOC_BIAS_STANDBY: |
| 77 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK2, |
| 78 | 32768, SND_SOC_CLOCK_IN); |
| 79 | if (ret < 0) { |
| 80 | pr_err("Failed to switch away from FLL: %d\n", ret); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, |
| 85 | 0, 0, 0); |
| 86 | if (ret < 0) { |
| 87 | pr_err("Failed to stop FLL: %d\n", ret); |
| 88 | return ret; |
| 89 | } |
| 90 | break; |
| 91 | |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | dapm->bias_level = level; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int littlemill_hw_params(struct snd_pcm_substream *substream, |
| 102 | struct snd_pcm_hw_params *params) |
| 103 | { |
| 104 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 105 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 106 | int ret; |
| 107 | |
| 108 | sample_rate = params_rate(params); |
| 109 | |
| 110 | ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, |
| 111 | WM8994_FLL_SRC_MCLK2, 32768, |
| 112 | sample_rate * 512); |
| 113 | if (ret < 0) { |
| 114 | pr_err("Failed to start FLL: %d\n", ret); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | ret = snd_soc_dai_set_sysclk(codec_dai, |
| 119 | WM8994_SYSCLK_FLL1, |
| 120 | sample_rate * 512, |
| 121 | SND_SOC_CLOCK_IN); |
| 122 | if (ret < 0) { |
| 123 | pr_err("Failed to set SYSCLK: %d\n", ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static struct snd_soc_ops littlemill_ops = { |
| 131 | .hw_params = littlemill_hw_params, |
| 132 | }; |
| 133 | |
| 134 | static struct snd_soc_dai_link littlemill_dai[] = { |
| 135 | { |
| 136 | .name = "CPU", |
| 137 | .stream_name = "CPU", |
| 138 | .cpu_dai_name = "samsung-i2s.0", |
| 139 | .codec_dai_name = "wm8994-aif1", |
| 140 | .platform_name = "samsung-audio", |
| 141 | .codec_name = "wm8994-codec", |
| 142 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
| 143 | | SND_SOC_DAIFMT_CBM_CFM, |
| 144 | .ops = &littlemill_ops, |
| 145 | }, |
| 146 | }; |
| 147 | |
| 148 | static struct snd_soc_dapm_widget widgets[] = { |
| 149 | SND_SOC_DAPM_HP("Headphone", NULL), |
Mark Brown | 8f10316 | 2011-12-02 17:36:06 +0000 | [diff] [blame] | 150 | |
| 151 | SND_SOC_DAPM_MIC("AMIC", NULL), |
| 152 | SND_SOC_DAPM_MIC("DMIC", NULL), |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | static struct snd_soc_dapm_route audio_paths[] = { |
| 156 | { "Headphone", NULL, "HPOUT1L" }, |
| 157 | { "Headphone", NULL, "HPOUT1R" }, |
Mark Brown | 8f10316 | 2011-12-02 17:36:06 +0000 | [diff] [blame] | 158 | |
| 159 | { "AMIC", NULL, "MICBIAS1" }, /* Default for AMICBIAS jumper */ |
| 160 | { "IN1LN", NULL, "AMIC" }, |
| 161 | |
| 162 | { "DMIC", NULL, "MICBIAS2" }, /* Default for DMICBIAS jumper */ |
| 163 | { "DMIC1DAT", NULL, "DMIC" }, |
| 164 | { "DMIC2DAT", NULL, "DMIC" }, |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Mark Brown | f2e2026 | 2011-12-02 15:55:52 +0000 | [diff] [blame] | 167 | static struct snd_soc_jack littlemill_headset; |
| 168 | |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 169 | static int littlemill_late_probe(struct snd_soc_card *card) |
| 170 | { |
Mark Brown | f2e2026 | 2011-12-02 15:55:52 +0000 | [diff] [blame] | 171 | struct snd_soc_codec *codec = card->rtd[0].codec; |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 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, WM8994_SYSCLK_MCLK2, |
| 176 | 32768, SND_SOC_CLOCK_IN); |
| 177 | if (ret < 0) |
| 178 | return ret; |
| 179 | |
Mark Brown | f2e2026 | 2011-12-02 15:55:52 +0000 | [diff] [blame] | 180 | ret = snd_soc_jack_new(codec, "Headset", |
| 181 | SND_JACK_HEADSET | SND_JACK_MECHANICAL | |
| 182 | SND_JACK_BTN_0 | SND_JACK_BTN_1 | |
| 183 | SND_JACK_BTN_2 | SND_JACK_BTN_3 | |
| 184 | SND_JACK_BTN_4 | SND_JACK_BTN_5, |
| 185 | &littlemill_headset); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | /* This will check device compatibility itself */ |
| 190 | wm8958_mic_detect(codec, &littlemill_headset, NULL, NULL); |
| 191 | |
Mark Brown | 654a28c | 2012-02-03 13:15:18 +0000 | [diff] [blame] | 192 | /* As will this */ |
| 193 | wm8994_mic_detect(codec, &littlemill_headset, 1); |
| 194 | |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static struct snd_soc_card littlemill = { |
| 199 | .name = "Littlemill", |
Axel Lin | 095d79d | 2011-12-22 10:53:15 +0800 | [diff] [blame] | 200 | .owner = THIS_MODULE, |
Mark Brown | 0a590b1 | 2011-11-28 22:05:41 +0000 | [diff] [blame] | 201 | .dai_link = littlemill_dai, |
| 202 | .num_links = ARRAY_SIZE(littlemill_dai), |
| 203 | |
| 204 | .set_bias_level = littlemill_set_bias_level, |
| 205 | .set_bias_level_post = littlemill_set_bias_level_post, |
| 206 | |
| 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 = littlemill_late_probe, |
| 213 | }; |
| 214 | |
| 215 | static __devinit int littlemill_probe(struct platform_device *pdev) |
| 216 | { |
| 217 | struct snd_soc_card *card = &littlemill; |
| 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 littlemill_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 littlemill_driver = { |
| 242 | .driver = { |
| 243 | .name = "littlemill", |
| 244 | .owner = THIS_MODULE, |
| 245 | .pm = &snd_soc_pm_ops, |
| 246 | }, |
| 247 | .probe = littlemill_probe, |
| 248 | .remove = __devexit_p(littlemill_remove), |
| 249 | }; |
| 250 | |
| 251 | module_platform_driver(littlemill_driver); |
| 252 | |
| 253 | MODULE_DESCRIPTION("Littlemill audio support"); |
| 254 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 255 | MODULE_LICENSE("GPL"); |
| 256 | MODULE_ALIAS("platform:littlemill"); |