Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * tosa.c -- SoC audio for Tosa |
| 3 | * |
| 4 | * Copyright 2005 Wolfson Microelectronics PLC. |
| 5 | * Copyright 2005 Openedhand Ltd. |
| 6 | * |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 7 | * Authors: Liam Girdwood <lrg@slimlogic.co.uk> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 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 | * |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 15 | * GPIO's |
| 16 | * 1 - Jack Insertion |
| 17 | * 5 - Hookswitch (headset answer/hang up switch) |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/device.h> |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 24 | #include <linux/gpio.h> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 25 | |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 26 | #include <sound/core.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | |
| 31 | #include <asm/mach-types.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 32 | #include <mach/tosa.h> |
| 33 | #include <mach/pxa-regs.h> |
| 34 | #include <mach/hardware.h> |
| 35 | #include <mach/audio.h> |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 36 | |
| 37 | #include "../codecs/wm9712.h" |
| 38 | #include "pxa2xx-pcm.h" |
Liam Girdwood | cb4c048 | 2007-02-02 17:23:11 +0100 | [diff] [blame] | 39 | #include "pxa2xx-ac97.h" |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 40 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 41 | static struct snd_soc_card tosa; |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 42 | |
| 43 | #define TOSA_HP 0 |
| 44 | #define TOSA_MIC_INT 1 |
| 45 | #define TOSA_HEADSET 2 |
| 46 | #define TOSA_HP_OFF 3 |
| 47 | #define TOSA_SPK_ON 0 |
| 48 | #define TOSA_SPK_OFF 1 |
| 49 | |
| 50 | static int tosa_jack_func; |
| 51 | static int tosa_spk_func; |
| 52 | |
| 53 | static void tosa_ext_control(struct snd_soc_codec *codec) |
| 54 | { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 55 | /* set up jack connection */ |
| 56 | switch (tosa_jack_func) { |
| 57 | case TOSA_HP: |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 58 | snd_soc_dapm_disable_pin(codec, "Mic (Internal)"); |
| 59 | snd_soc_dapm_enable_pin(codec, "Headphone Jack"); |
| 60 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 61 | break; |
| 62 | case TOSA_MIC_INT: |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 63 | snd_soc_dapm_enable_pin(codec, "Mic (Internal)"); |
| 64 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
| 65 | snd_soc_dapm_disable_pin(codec, "Headset Jack"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 66 | break; |
| 67 | case TOSA_HEADSET: |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 68 | snd_soc_dapm_disable_pin(codec, "Mic (Internal)"); |
| 69 | snd_soc_dapm_disable_pin(codec, "Headphone Jack"); |
| 70 | snd_soc_dapm_enable_pin(codec, "Headset Jack"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (tosa_spk_func == TOSA_SPK_ON) |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 75 | snd_soc_dapm_enable_pin(codec, "Speaker"); |
| 76 | else |
| 77 | snd_soc_dapm_disable_pin(codec, "Speaker"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 78 | |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 79 | snd_soc_dapm_sync(codec); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int tosa_startup(struct snd_pcm_substream *substream) |
| 83 | { |
| 84 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 85 | struct snd_soc_codec *codec = rtd->socdev->codec; |
| 86 | |
| 87 | /* check the jack status at stream startup */ |
| 88 | tosa_ext_control(codec); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static struct snd_soc_ops tosa_ops = { |
| 93 | .startup = tosa_startup, |
| 94 | }; |
| 95 | |
| 96 | static int tosa_get_jack(struct snd_kcontrol *kcontrol, |
| 97 | struct snd_ctl_elem_value *ucontrol) |
| 98 | { |
| 99 | ucontrol->value.integer.value[0] = tosa_jack_func; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int tosa_set_jack(struct snd_kcontrol *kcontrol, |
| 104 | struct snd_ctl_elem_value *ucontrol) |
| 105 | { |
| 106 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 107 | |
| 108 | if (tosa_jack_func == ucontrol->value.integer.value[0]) |
| 109 | return 0; |
| 110 | |
| 111 | tosa_jack_func = ucontrol->value.integer.value[0]; |
| 112 | tosa_ext_control(codec); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static int tosa_get_spk(struct snd_kcontrol *kcontrol, |
| 117 | struct snd_ctl_elem_value *ucontrol) |
| 118 | { |
| 119 | ucontrol->value.integer.value[0] = tosa_spk_func; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int tosa_set_spk(struct snd_kcontrol *kcontrol, |
| 124 | struct snd_ctl_elem_value *ucontrol) |
| 125 | { |
| 126 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 127 | |
| 128 | if (tosa_spk_func == ucontrol->value.integer.value[0]) |
| 129 | return 0; |
| 130 | |
| 131 | tosa_spk_func = ucontrol->value.integer.value[0]; |
| 132 | tosa_ext_control(codec); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | /* tosa dapm event handlers */ |
Jarkko Nikula | 338c7ed | 2008-02-28 12:34:48 +0100 | [diff] [blame] | 137 | static int tosa_hp_event(struct snd_soc_dapm_widget *w, |
| 138 | struct snd_kcontrol *k, int event) |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 139 | { |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 140 | gpio_set_value(TOSA_GPIO_L_MUTE, SND_SOC_DAPM_EVENT_ON(event) ? 1 :0); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* tosa machine dapm widgets */ |
| 145 | static const struct snd_soc_dapm_widget tosa_dapm_widgets[] = { |
| 146 | SND_SOC_DAPM_HP("Headphone Jack", tosa_hp_event), |
| 147 | SND_SOC_DAPM_HP("Headset Jack", NULL), |
| 148 | SND_SOC_DAPM_MIC("Mic (Internal)", NULL), |
| 149 | SND_SOC_DAPM_SPK("Speaker", NULL), |
| 150 | }; |
| 151 | |
| 152 | /* tosa audio map */ |
Dmitry Baryshkov | 76d39d0 | 2008-07-08 19:45:20 +0400 | [diff] [blame] | 153 | static const struct snd_soc_dapm_route audio_map[] = { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 154 | |
| 155 | /* headphone connected to HPOUTL, HPOUTR */ |
| 156 | {"Headphone Jack", NULL, "HPOUTL"}, |
| 157 | {"Headphone Jack", NULL, "HPOUTR"}, |
| 158 | |
| 159 | /* ext speaker connected to LOUT2, ROUT2 */ |
| 160 | {"Speaker", NULL, "LOUT2"}, |
| 161 | {"Speaker", NULL, "ROUT2"}, |
| 162 | |
| 163 | /* internal mic is connected to mic1, mic2 differential - with bias */ |
| 164 | {"MIC1", NULL, "Mic Bias"}, |
| 165 | {"MIC2", NULL, "Mic Bias"}, |
| 166 | {"Mic Bias", NULL, "Mic (Internal)"}, |
| 167 | |
| 168 | /* headset is connected to HPOUTR, and LINEINR with bias */ |
| 169 | {"Headset Jack", NULL, "HPOUTR"}, |
| 170 | {"LINEINR", NULL, "Mic Bias"}, |
| 171 | {"Mic Bias", NULL, "Headset Jack"}, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static const char *jack_function[] = {"Headphone", "Mic", "Line", "Headset", |
| 175 | "Off"}; |
| 176 | static const char *spk_function[] = {"On", "Off"}; |
| 177 | static const struct soc_enum tosa_enum[] = { |
| 178 | SOC_ENUM_SINGLE_EXT(5, jack_function), |
| 179 | SOC_ENUM_SINGLE_EXT(2, spk_function), |
| 180 | }; |
| 181 | |
| 182 | static const struct snd_kcontrol_new tosa_controls[] = { |
| 183 | SOC_ENUM_EXT("Jack Function", tosa_enum[0], tosa_get_jack, |
| 184 | tosa_set_jack), |
| 185 | SOC_ENUM_EXT("Speaker Function", tosa_enum[1], tosa_get_spk, |
| 186 | tosa_set_spk), |
| 187 | }; |
| 188 | |
| 189 | static int tosa_ac97_init(struct snd_soc_codec *codec) |
| 190 | { |
| 191 | int i, err; |
| 192 | |
Mark Brown | 35f5e54 | 2008-09-27 10:45:09 +0100 | [diff] [blame] | 193 | snd_soc_dapm_nc_pin(codec, "OUT3"); |
| 194 | snd_soc_dapm_nc_pin(codec, "MONOOUT"); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 195 | |
| 196 | /* add tosa specific controls */ |
| 197 | for (i = 0; i < ARRAY_SIZE(tosa_controls); i++) { |
| 198 | err = snd_ctl_add(codec->card, |
| 199 | snd_soc_cnew(&tosa_controls[i],codec, NULL)); |
| 200 | if (err < 0) |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | /* add tosa specific widgets */ |
Dmitry Baryshkov | 76d39d0 | 2008-07-08 19:45:20 +0400 | [diff] [blame] | 205 | snd_soc_dapm_new_controls(codec, tosa_dapm_widgets, |
Mark Brown | 25191c4 | 2008-05-13 14:55:48 +0200 | [diff] [blame] | 206 | ARRAY_SIZE(tosa_dapm_widgets)); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 207 | |
| 208 | /* set up tosa specific audio path audio_map */ |
Mark Brown | 25191c4 | 2008-05-13 14:55:48 +0200 | [diff] [blame] | 209 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 210 | |
Liam Girdwood | a530218 | 2008-07-07 13:35:17 +0100 | [diff] [blame] | 211 | snd_soc_dapm_sync(codec); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static struct snd_soc_dai_link tosa_dai[] = { |
| 216 | { |
| 217 | .name = "AC97", |
| 218 | .stream_name = "AC97 HiFi", |
| 219 | .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_HIFI], |
| 220 | .codec_dai = &wm9712_dai[WM9712_DAI_AC97_HIFI], |
| 221 | .init = tosa_ac97_init, |
Liam Girdwood | cb4c048 | 2007-02-02 17:23:11 +0100 | [diff] [blame] | 222 | .ops = &tosa_ops, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 223 | }, |
| 224 | { |
| 225 | .name = "AC97 Aux", |
| 226 | .stream_name = "AC97 Aux", |
| 227 | .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_AUX], |
| 228 | .codec_dai = &wm9712_dai[WM9712_DAI_AC97_AUX], |
Liam Girdwood | cb4c048 | 2007-02-02 17:23:11 +0100 | [diff] [blame] | 229 | .ops = &tosa_ops, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 230 | }, |
| 231 | }; |
| 232 | |
Dmitry Baryshkov | 5c0d7bb | 2008-11-25 09:35:21 +0300 | [diff] [blame] | 233 | static int tosa_probe(struct platform_device *dev) |
| 234 | { |
| 235 | int ret; |
| 236 | |
| 237 | ret = gpio_request(TOSA_GPIO_L_MUTE, "Headphone Jack"); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | ret = gpio_direction_output(TOSA_GPIO_L_MUTE, 0); |
| 241 | if (ret) |
| 242 | gpio_free(TOSA_GPIO_L_MUTE); |
| 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static int tosa_remove(struct platform_device *dev) |
| 248 | { |
| 249 | gpio_free(TOSA_GPIO_L_MUTE); |
| 250 | return 0; |
| 251 | } |
| 252 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 253 | static struct snd_soc_card tosa = { |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 254 | .name = "Tosa", |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 255 | .platform = &pxa2xx_soc_platform, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 256 | .dai_link = tosa_dai, |
| 257 | .num_links = ARRAY_SIZE(tosa_dai), |
Dmitry Baryshkov | 5c0d7bb | 2008-11-25 09:35:21 +0300 | [diff] [blame] | 258 | .probe = tosa_probe, |
| 259 | .remove = tosa_remove, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | static struct snd_soc_device tosa_snd_devdata = { |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 263 | .card = &tosa, |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 264 | .codec_dev = &soc_codec_dev_wm9712, |
| 265 | }; |
| 266 | |
| 267 | static struct platform_device *tosa_snd_device; |
| 268 | |
| 269 | static int __init tosa_init(void) |
| 270 | { |
| 271 | int ret; |
| 272 | |
| 273 | if (!machine_is_tosa()) |
| 274 | return -ENODEV; |
| 275 | |
| 276 | tosa_snd_device = platform_device_alloc("soc-audio", -1); |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 277 | if (!tosa_snd_device) { |
| 278 | ret = -ENOMEM; |
| 279 | goto err_alloc; |
| 280 | } |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 281 | |
| 282 | platform_set_drvdata(tosa_snd_device, &tosa_snd_devdata); |
| 283 | tosa_snd_devdata.dev = &tosa_snd_device->dev; |
| 284 | ret = platform_device_add(tosa_snd_device); |
| 285 | |
Dmitry Baryshkov | 4440cbd | 2008-06-14 11:42:57 +0100 | [diff] [blame] | 286 | if (!ret) |
| 287 | return 0; |
| 288 | |
| 289 | platform_device_put(tosa_snd_device); |
| 290 | |
| 291 | err_alloc: |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static void __exit tosa_exit(void) |
| 296 | { |
| 297 | platform_device_unregister(tosa_snd_device); |
Liam Girdwood | 1b49cb9 | 2006-10-12 14:33:09 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | module_init(tosa_init); |
| 301 | module_exit(tosa_exit); |
| 302 | |
| 303 | /* Module information */ |
| 304 | MODULE_AUTHOR("Richard Purdie"); |
| 305 | MODULE_DESCRIPTION("ALSA SoC Tosa"); |
| 306 | MODULE_LICENSE("GPL"); |