Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * spitz.c -- SoC audio for Sharp SL-Cxx00 models Spitz, Borzoi and Akita |
| 3 | * |
| 4 | * Copyright 2005 Wolfson Microelectronics PLC. |
| 5 | * Copyright 2005 Openedhand Ltd. |
| 6 | * |
| 7 | * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com> |
| 8 | * Richard Purdie <richard@openedhand.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 15 | * Revision history |
| 16 | * 30th Nov 2005 Initial version. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/moduleparam.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/platform_device.h> |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/soc.h> |
| 28 | #include <sound/soc-dapm.h> |
| 29 | |
| 30 | #include <asm/mach-types.h> |
| 31 | #include <asm/hardware/scoop.h> |
| 32 | #include <asm/arch/pxa-regs.h> |
| 33 | #include <asm/arch/hardware.h> |
| 34 | #include <asm/arch/akita.h> |
| 35 | #include <asm/arch/spitz.h> |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 36 | #include "../codecs/wm8750.h" |
| 37 | #include "pxa2xx-pcm.h" |
Liam Girdwood | 97952f6 | 2007-02-02 17:22:46 +0100 | [diff] [blame] | 38 | #include "pxa2xx-i2s.h" |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 39 | |
| 40 | #define SPITZ_HP 0 |
| 41 | #define SPITZ_MIC 1 |
| 42 | #define SPITZ_LINE 2 |
| 43 | #define SPITZ_HEADSET 3 |
| 44 | #define SPITZ_HP_OFF 4 |
| 45 | #define SPITZ_SPK_ON 0 |
| 46 | #define SPITZ_SPK_OFF 1 |
| 47 | |
| 48 | /* audio clock in Hz - rounded from 12.235MHz */ |
| 49 | #define SPITZ_AUDIO_CLOCK 12288000 |
| 50 | |
| 51 | static int spitz_jack_func; |
| 52 | static int spitz_spk_func; |
| 53 | |
| 54 | static void spitz_ext_control(struct snd_soc_codec *codec) |
| 55 | { |
| 56 | if (spitz_spk_func == SPITZ_SPK_ON) |
| 57 | snd_soc_dapm_set_endpoint(codec, "Ext Spk", 1); |
| 58 | else |
| 59 | snd_soc_dapm_set_endpoint(codec, "Ext Spk", 0); |
| 60 | |
| 61 | /* set up jack connection */ |
| 62 | switch (spitz_jack_func) { |
| 63 | case SPITZ_HP: |
| 64 | /* enable and unmute hp jack, disable mic bias */ |
| 65 | snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0); |
| 66 | snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0); |
| 67 | snd_soc_dapm_set_endpoint(codec, "Line Jack", 0); |
| 68 | snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1); |
| 69 | set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L); |
| 70 | set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R); |
| 71 | break; |
| 72 | case SPITZ_MIC: |
| 73 | /* enable mic jack and bias, mute hp */ |
| 74 | snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0); |
| 75 | snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0); |
| 76 | snd_soc_dapm_set_endpoint(codec, "Line Jack", 0); |
| 77 | snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1); |
| 78 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L); |
| 79 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R); |
| 80 | break; |
| 81 | case SPITZ_LINE: |
| 82 | /* enable line jack, disable mic bias and mute hp */ |
| 83 | snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0); |
| 84 | snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0); |
| 85 | snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0); |
| 86 | snd_soc_dapm_set_endpoint(codec, "Line Jack", 1); |
| 87 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L); |
| 88 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R); |
| 89 | break; |
| 90 | case SPITZ_HEADSET: |
| 91 | /* enable and unmute headset jack enable mic bias, mute L hp */ |
| 92 | snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0); |
| 93 | snd_soc_dapm_set_endpoint(codec, "Mic Jack", 1); |
| 94 | snd_soc_dapm_set_endpoint(codec, "Line Jack", 0); |
| 95 | snd_soc_dapm_set_endpoint(codec, "Headset Jack", 1); |
| 96 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L); |
| 97 | set_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R); |
| 98 | break; |
| 99 | case SPITZ_HP_OFF: |
| 100 | |
| 101 | /* jack removed, everything off */ |
| 102 | snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0); |
| 103 | snd_soc_dapm_set_endpoint(codec, "Headset Jack", 0); |
| 104 | snd_soc_dapm_set_endpoint(codec, "Mic Jack", 0); |
| 105 | snd_soc_dapm_set_endpoint(codec, "Line Jack", 0); |
| 106 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_L); |
| 107 | reset_scoop_gpio(&spitzscoop_device.dev, SPITZ_SCP_MUTE_R); |
| 108 | break; |
| 109 | } |
| 110 | snd_soc_dapm_sync_endpoints(codec); |
| 111 | } |
| 112 | |
| 113 | static int spitz_startup(struct snd_pcm_substream *substream) |
| 114 | { |
| 115 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 116 | struct snd_soc_codec *codec = rtd->socdev->codec; |
| 117 | |
| 118 | /* check the jack status at stream startup */ |
| 119 | spitz_ext_control(codec); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Liam Girdwood | 97952f6 | 2007-02-02 17:22:46 +0100 | [diff] [blame] | 123 | static int spitz_hw_params(struct snd_pcm_substream *substream, |
| 124 | struct snd_pcm_hw_params *params) |
| 125 | { |
| 126 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 127 | struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai; |
| 128 | struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai; |
| 129 | unsigned int clk = 0; |
| 130 | int ret = 0; |
| 131 | |
| 132 | switch (params_rate(params)) { |
| 133 | case 8000: |
| 134 | case 16000: |
| 135 | case 48000: |
| 136 | case 96000: |
| 137 | clk = 12288000; |
| 138 | break; |
| 139 | case 11025: |
| 140 | case 22050: |
| 141 | case 44100: |
| 142 | clk = 11289600; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | /* set codec DAI configuration */ |
| 147 | ret = codec_dai->dai_ops.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 | /* set cpu DAI configuration */ |
| 153 | ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | |
| 154 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); |
| 155 | if (ret < 0) |
| 156 | return ret; |
| 157 | |
| 158 | /* set the codec system clock for DAC and ADC */ |
| 159 | ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8750_SYSCLK, clk, |
| 160 | SND_SOC_CLOCK_IN); |
| 161 | if (ret < 0) |
| 162 | return ret; |
| 163 | |
| 164 | /* set the I2S system clock as input (unused) */ |
| 165 | ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0, |
| 166 | SND_SOC_CLOCK_IN); |
| 167 | if (ret < 0) |
| 168 | return ret; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 173 | static struct snd_soc_ops spitz_ops = { |
| 174 | .startup = spitz_startup, |
Liam Girdwood | 97952f6 | 2007-02-02 17:22:46 +0100 | [diff] [blame] | 175 | .hw_params = spitz_hw_params, |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static int spitz_get_jack(struct snd_kcontrol *kcontrol, |
| 179 | struct snd_ctl_elem_value *ucontrol) |
| 180 | { |
| 181 | ucontrol->value.integer.value[0] = spitz_jack_func; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int spitz_set_jack(struct snd_kcontrol *kcontrol, |
| 186 | struct snd_ctl_elem_value *ucontrol) |
| 187 | { |
| 188 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 189 | |
| 190 | if (spitz_jack_func == ucontrol->value.integer.value[0]) |
| 191 | return 0; |
| 192 | |
| 193 | spitz_jack_func = ucontrol->value.integer.value[0]; |
| 194 | spitz_ext_control(codec); |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | static int spitz_get_spk(struct snd_kcontrol *kcontrol, |
| 199 | struct snd_ctl_elem_value *ucontrol) |
| 200 | { |
| 201 | ucontrol->value.integer.value[0] = spitz_spk_func; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int spitz_set_spk(struct snd_kcontrol *kcontrol, |
| 206 | struct snd_ctl_elem_value *ucontrol) |
| 207 | { |
| 208 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 209 | |
| 210 | if (spitz_spk_func == ucontrol->value.integer.value[0]) |
| 211 | return 0; |
| 212 | |
| 213 | spitz_spk_func = ucontrol->value.integer.value[0]; |
| 214 | spitz_ext_control(codec); |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | static int spitz_mic_bias(struct snd_soc_dapm_widget *w, int event) |
| 219 | { |
| 220 | if (machine_is_borzoi() || machine_is_spitz()) { |
| 221 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 222 | set_scoop_gpio(&spitzscoop2_device.dev, |
| 223 | SPITZ_SCP2_MIC_BIAS); |
| 224 | else |
| 225 | reset_scoop_gpio(&spitzscoop2_device.dev, |
| 226 | SPITZ_SCP2_MIC_BIAS); |
| 227 | } |
| 228 | |
| 229 | if (machine_is_akita()) { |
| 230 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 231 | akita_set_ioexp(&akitaioexp_device.dev, |
| 232 | AKITA_IOEXP_MIC_BIAS); |
| 233 | else |
| 234 | akita_reset_ioexp(&akitaioexp_device.dev, |
| 235 | AKITA_IOEXP_MIC_BIAS); |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* spitz machine dapm widgets */ |
| 241 | static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = { |
| 242 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 243 | SND_SOC_DAPM_MIC("Mic Jack", spitz_mic_bias), |
| 244 | SND_SOC_DAPM_SPK("Ext Spk", NULL), |
| 245 | SND_SOC_DAPM_LINE("Line Jack", NULL), |
| 246 | |
| 247 | /* headset is a mic and mono headphone */ |
| 248 | SND_SOC_DAPM_HP("Headset Jack", NULL), |
| 249 | }; |
| 250 | |
| 251 | /* Spitz machine audio_map */ |
| 252 | static const char *audio_map[][3] = { |
| 253 | |
| 254 | /* headphone connected to LOUT1, ROUT1 */ |
| 255 | {"Headphone Jack", NULL, "LOUT1"}, |
| 256 | {"Headphone Jack", NULL, "ROUT1"}, |
| 257 | |
| 258 | /* headset connected to ROUT1 and LINPUT1 with bias (def below) */ |
| 259 | {"Headset Jack", NULL, "ROUT1"}, |
| 260 | |
| 261 | /* ext speaker connected to LOUT2, ROUT2 */ |
| 262 | {"Ext Spk", NULL , "ROUT2"}, |
| 263 | {"Ext Spk", NULL , "LOUT2"}, |
| 264 | |
| 265 | /* mic is connected to input 1 - with bias */ |
| 266 | {"LINPUT1", NULL, "Mic Bias"}, |
| 267 | {"Mic Bias", NULL, "Mic Jack"}, |
| 268 | |
| 269 | /* line is connected to input 1 - no bias */ |
| 270 | {"LINPUT1", NULL, "Line Jack"}, |
| 271 | |
| 272 | {NULL, NULL, NULL}, |
| 273 | }; |
| 274 | |
| 275 | static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset", |
| 276 | "Off"}; |
| 277 | static const char *spk_function[] = {"On", "Off"}; |
| 278 | static const struct soc_enum spitz_enum[] = { |
| 279 | SOC_ENUM_SINGLE_EXT(5, jack_function), |
| 280 | SOC_ENUM_SINGLE_EXT(2, spk_function), |
| 281 | }; |
| 282 | |
| 283 | static const struct snd_kcontrol_new wm8750_spitz_controls[] = { |
| 284 | SOC_ENUM_EXT("Jack Function", spitz_enum[0], spitz_get_jack, |
| 285 | spitz_set_jack), |
| 286 | SOC_ENUM_EXT("Speaker Function", spitz_enum[1], spitz_get_spk, |
| 287 | spitz_set_spk), |
| 288 | }; |
| 289 | |
| 290 | /* |
| 291 | * Logic for a wm8750 as connected on a Sharp SL-Cxx00 Device |
| 292 | */ |
| 293 | static int spitz_wm8750_init(struct snd_soc_codec *codec) |
| 294 | { |
| 295 | int i, err; |
| 296 | |
| 297 | /* NC codec pins */ |
| 298 | snd_soc_dapm_set_endpoint(codec, "RINPUT1", 0); |
| 299 | snd_soc_dapm_set_endpoint(codec, "LINPUT2", 0); |
| 300 | snd_soc_dapm_set_endpoint(codec, "RINPUT2", 0); |
| 301 | snd_soc_dapm_set_endpoint(codec, "LINPUT3", 0); |
| 302 | snd_soc_dapm_set_endpoint(codec, "RINPUT3", 0); |
| 303 | snd_soc_dapm_set_endpoint(codec, "OUT3", 0); |
| 304 | snd_soc_dapm_set_endpoint(codec, "MONO", 0); |
| 305 | |
| 306 | /* Add spitz specific controls */ |
| 307 | for (i = 0; i < ARRAY_SIZE(wm8750_spitz_controls); i++) { |
| 308 | err = snd_ctl_add(codec->card, |
| 309 | snd_soc_cnew(&wm8750_spitz_controls[i], codec, NULL)); |
| 310 | if (err < 0) |
| 311 | return err; |
| 312 | } |
| 313 | |
| 314 | /* Add spitz specific widgets */ |
| 315 | for (i = 0; i < ARRAY_SIZE(wm8750_dapm_widgets); i++) { |
| 316 | snd_soc_dapm_new_control(codec, &wm8750_dapm_widgets[i]); |
| 317 | } |
| 318 | |
| 319 | /* Set up spitz specific audio path audio_map */ |
| 320 | for (i = 0; audio_map[i][0] != NULL; i++) { |
| 321 | snd_soc_dapm_connect_input(codec, audio_map[i][0], |
| 322 | audio_map[i][1], audio_map[i][2]); |
| 323 | } |
| 324 | |
| 325 | snd_soc_dapm_sync_endpoints(codec); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 329 | /* spitz digital audio interface glue - connects codec <--> CPU */ |
| 330 | static struct snd_soc_dai_link spitz_dai = { |
| 331 | .name = "wm8750", |
| 332 | .stream_name = "WM8750", |
| 333 | .cpu_dai = &pxa_i2s_dai, |
| 334 | .codec_dai = &wm8750_dai, |
| 335 | .init = spitz_wm8750_init, |
Liam Girdwood | 97952f6 | 2007-02-02 17:22:46 +0100 | [diff] [blame] | 336 | .ops = &spitz_ops, |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* spitz audio machine driver */ |
| 340 | static struct snd_soc_machine snd_soc_machine_spitz = { |
| 341 | .name = "Spitz", |
| 342 | .dai_link = &spitz_dai, |
| 343 | .num_links = 1, |
Liam Girdwood | 7fb290d | 2006-10-12 14:32:13 +0200 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | /* spitz audio private data */ |
| 347 | static struct wm8750_setup_data spitz_wm8750_setup = { |
| 348 | .i2c_address = 0x1b, |
| 349 | }; |
| 350 | |
| 351 | /* spitz audio subsystem */ |
| 352 | static struct snd_soc_device spitz_snd_devdata = { |
| 353 | .machine = &snd_soc_machine_spitz, |
| 354 | .platform = &pxa2xx_soc_platform, |
| 355 | .codec_dev = &soc_codec_dev_wm8750, |
| 356 | .codec_data = &spitz_wm8750_setup, |
| 357 | }; |
| 358 | |
| 359 | static struct platform_device *spitz_snd_device; |
| 360 | |
| 361 | static int __init spitz_init(void) |
| 362 | { |
| 363 | int ret; |
| 364 | |
| 365 | if (!(machine_is_spitz() || machine_is_borzoi() || machine_is_akita())) |
| 366 | return -ENODEV; |
| 367 | |
| 368 | spitz_snd_device = platform_device_alloc("soc-audio", -1); |
| 369 | if (!spitz_snd_device) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | platform_set_drvdata(spitz_snd_device, &spitz_snd_devdata); |
| 373 | spitz_snd_devdata.dev = &spitz_snd_device->dev; |
| 374 | ret = platform_device_add(spitz_snd_device); |
| 375 | |
| 376 | if (ret) |
| 377 | platform_device_put(spitz_snd_device); |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | static void __exit spitz_exit(void) |
| 383 | { |
| 384 | platform_device_unregister(spitz_snd_device); |
| 385 | } |
| 386 | |
| 387 | module_init(spitz_init); |
| 388 | module_exit(spitz_exit); |
| 389 | |
| 390 | MODULE_AUTHOR("Richard Purdie"); |
| 391 | MODULE_DESCRIPTION("ALSA SoC Spitz"); |
| 392 | MODULE_LICENSE("GPL"); |