Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * zylonite.c -- SoC audio for Zylonite |
| 3 | * |
| 4 | * Copyright 2008 Wolfson Microelectronics PLC. |
| 5 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/device.h> |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 17 | #include <linux/clk.h> |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> |
| 21 | #include <sound/pcm_params.h> |
| 22 | #include <sound/soc.h> |
| 23 | #include <sound/soc-dapm.h> |
| 24 | |
| 25 | #include "../codecs/wm9713.h" |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 26 | #include "pxa2xx-ac97.h" |
| 27 | #include "pxa-ssp.h" |
| 28 | |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 29 | /* |
| 30 | * There is a physical switch SW15 on the board which changes the MCLK |
| 31 | * for the WM9713 between the standard AC97 master clock and the |
| 32 | * output of the CLK_POUT signal from the PXA. |
| 33 | */ |
| 34 | static int clk_pout; |
| 35 | module_param(clk_pout, int, 0); |
| 36 | MODULE_PARM_DESC(clk_pout, "Use CLK_POUT as WM9713 MCLK (SW15 on board)."); |
| 37 | |
| 38 | static struct clk *pout; |
| 39 | |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 40 | static struct snd_soc_card zylonite; |
| 41 | |
| 42 | static const struct snd_soc_dapm_widget zylonite_dapm_widgets[] = { |
| 43 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 44 | SND_SOC_DAPM_MIC("Headset Microphone", NULL), |
| 45 | SND_SOC_DAPM_MIC("Handset Microphone", NULL), |
| 46 | SND_SOC_DAPM_SPK("Multiactor", NULL), |
| 47 | SND_SOC_DAPM_SPK("Headset Earpiece", NULL), |
| 48 | }; |
| 49 | |
| 50 | /* Currently supported audio map */ |
| 51 | static const struct snd_soc_dapm_route audio_map[] = { |
| 52 | |
| 53 | /* Headphone output connected to HPL/HPR */ |
| 54 | { "Headphone", NULL, "HPL" }, |
| 55 | { "Headphone", NULL, "HPR" }, |
| 56 | |
| 57 | /* On-board earpiece */ |
| 58 | { "Headset Earpiece", NULL, "OUT3" }, |
| 59 | |
| 60 | /* Headphone mic */ |
| 61 | { "MIC2A", NULL, "Mic Bias" }, |
| 62 | { "Mic Bias", NULL, "Headset Microphone" }, |
| 63 | |
| 64 | /* On-board mic */ |
| 65 | { "MIC1", NULL, "Mic Bias" }, |
| 66 | { "Mic Bias", NULL, "Handset Microphone" }, |
| 67 | |
| 68 | /* Multiactor differentially connected over SPKL/SPKR */ |
| 69 | { "Multiactor", NULL, "SPKL" }, |
| 70 | { "Multiactor", NULL, "SPKR" }, |
| 71 | }; |
| 72 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 73 | static int zylonite_wm9713_init(struct snd_soc_pcm_runtime *rtd) |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 74 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 75 | struct snd_soc_codec *codec = rtd->codec; |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 76 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 77 | |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 78 | if (clk_pout) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 79 | snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 80 | clk_get_rate(pout), 0); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 81 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 82 | snd_soc_dapm_new_controls(dapm, zylonite_dapm_widgets, |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 83 | ARRAY_SIZE(zylonite_dapm_widgets)); |
| 84 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 85 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 86 | |
| 87 | /* Static setup for now */ |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 88 | snd_soc_dapm_enable_pin(dapm, "Headphone"); |
| 89 | snd_soc_dapm_enable_pin(dapm, "Headset Earpiece"); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 90 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 91 | snd_soc_dapm_sync(dapm); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int zylonite_voice_hw_params(struct snd_pcm_substream *substream, |
| 96 | struct snd_pcm_hw_params *params) |
| 97 | { |
| 98 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 99 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 100 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Mark Brown | a435869 | 2009-01-23 11:49:45 +0000 | [diff] [blame] | 101 | unsigned int pll_out = 0; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 102 | unsigned int wm9713_div = 0; |
| 103 | int ret = 0; |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 104 | int rate = params_rate(params); |
| 105 | int width = snd_pcm_format_physical_width(params_format(params)); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 106 | |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 107 | /* Only support ratios that we can generate neatly from the AC97 |
| 108 | * based master clock - in particular, this excludes 44.1kHz. |
| 109 | * In most applications the voice DAC will be used for telephony |
| 110 | * data so multiples of 8kHz will be the common case. |
| 111 | */ |
| 112 | switch (rate) { |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 113 | case 8000: |
| 114 | wm9713_div = 12; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 115 | break; |
| 116 | case 16000: |
| 117 | wm9713_div = 6; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 118 | break; |
| 119 | case 48000: |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 120 | wm9713_div = 2; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 121 | break; |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 122 | default: |
| 123 | /* Don't support OSS emulation */ |
| 124 | return -EINVAL; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 127 | /* Add 1 to the width for the leading clock cycle */ |
| 128 | pll_out = rate * (width + 1) * 8; |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 129 | |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 130 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 134 | ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, pll_out); |
Mark Brown | a435869 | 2009-01-23 11:49:45 +0000 | [diff] [blame] | 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 138 | if (clk_pout) |
| 139 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV, |
| 140 | WM9713_PCMDIV(wm9713_div)); |
| 141 | else |
| 142 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV, |
| 143 | WM9713_PCMDIV(wm9713_div)); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 144 | if (ret < 0) |
| 145 | return ret; |
| 146 | |
Mark Brown | 85fab78 | 2009-03-13 14:27:08 +0000 | [diff] [blame] | 147 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
| 148 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | |
| 152 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
| 153 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static struct snd_soc_ops zylonite_voice_ops = { |
| 161 | .hw_params = zylonite_voice_hw_params, |
| 162 | }; |
| 163 | |
| 164 | static struct snd_soc_dai_link zylonite_dai[] = { |
| 165 | { |
| 166 | .name = "AC97", |
| 167 | .stream_name = "AC97 HiFi", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 168 | .codec_name = "wm9713-codec", |
| 169 | .platform_name = "pxa-pcm-audio", |
| 170 | .cpu_dai_name = "pxa-ac97.0", |
| 171 | .codec_name = "wm9713-hifi", |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 172 | .init = zylonite_wm9713_init, |
| 173 | }, |
| 174 | { |
| 175 | .name = "AC97 Aux", |
| 176 | .stream_name = "AC97 Aux", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 177 | .codec_name = "wm9713-codec", |
| 178 | .platform_name = "pxa-pcm-audio", |
| 179 | .cpu_dai_name = "pxa-ac97.1", |
| 180 | .codec_name = "wm9713-aux", |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 181 | }, |
| 182 | { |
| 183 | .name = "WM9713 Voice", |
| 184 | .stream_name = "WM9713 Voice", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 185 | .codec_name = "wm9713-codec", |
| 186 | .platform_name = "pxa-pcm-audio", |
| 187 | .cpu_dai_name = "pxa-ssp-dai.2", |
| 188 | .codec_name = "wm9713-voice", |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 189 | .ops = &zylonite_voice_ops, |
| 190 | }, |
| 191 | }; |
| 192 | |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 193 | static int zylonite_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | int ret; |
| 196 | |
| 197 | if (clk_pout) { |
| 198 | pout = clk_get(NULL, "CLK_POUT"); |
| 199 | if (IS_ERR(pout)) { |
| 200 | dev_err(&pdev->dev, "Unable to obtain CLK_POUT: %ld\n", |
| 201 | PTR_ERR(pout)); |
| 202 | return PTR_ERR(pout); |
| 203 | } |
| 204 | |
| 205 | ret = clk_enable(pout); |
| 206 | if (ret != 0) { |
| 207 | dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n", |
| 208 | ret); |
| 209 | clk_put(pout); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | dev_dbg(&pdev->dev, "MCLK enabled at %luHz\n", |
| 214 | clk_get_rate(pout)); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int zylonite_remove(struct platform_device *pdev) |
| 221 | { |
| 222 | if (clk_pout) { |
| 223 | clk_disable(pout); |
| 224 | clk_put(pout); |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int zylonite_suspend_post(struct platform_device *pdev, |
| 231 | pm_message_t state) |
| 232 | { |
| 233 | if (clk_pout) |
| 234 | clk_disable(pout); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int zylonite_resume_pre(struct platform_device *pdev) |
| 240 | { |
| 241 | int ret = 0; |
| 242 | |
| 243 | if (clk_pout) { |
| 244 | ret = clk_enable(pout); |
| 245 | if (ret != 0) |
| 246 | dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n", |
| 247 | ret); |
| 248 | } |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 253 | static struct snd_soc_card zylonite = { |
| 254 | .name = "Zylonite", |
Mark Brown | 2c782f5 | 2009-01-16 16:35:52 +0000 | [diff] [blame] | 255 | .probe = &zylonite_probe, |
| 256 | .remove = &zylonite_remove, |
| 257 | .suspend_post = &zylonite_suspend_post, |
| 258 | .resume_pre = &zylonite_resume_pre, |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 259 | .dai_link = zylonite_dai, |
| 260 | .num_links = ARRAY_SIZE(zylonite_dai), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 261 | .owner = THIS_MODULE, |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | static struct platform_device *zylonite_snd_ac97_device; |
| 265 | |
| 266 | static int __init zylonite_init(void) |
| 267 | { |
| 268 | int ret; |
| 269 | |
| 270 | zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1); |
| 271 | if (!zylonite_snd_ac97_device) |
| 272 | return -ENOMEM; |
| 273 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 274 | platform_set_drvdata(zylonite_snd_ac97_device, &zylonite); |
Mark Brown | 2dac921 | 2008-11-21 14:01:41 +0000 | [diff] [blame] | 275 | |
| 276 | ret = platform_device_add(zylonite_snd_ac97_device); |
| 277 | if (ret != 0) |
| 278 | platform_device_put(zylonite_snd_ac97_device); |
| 279 | |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static void __exit zylonite_exit(void) |
| 284 | { |
| 285 | platform_device_unregister(zylonite_snd_ac97_device); |
| 286 | } |
| 287 | |
| 288 | module_init(zylonite_init); |
| 289 | module_exit(zylonite_exit); |
| 290 | |
| 291 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 292 | MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite"); |
| 293 | MODULE_LICENSE("GPL"); |