Jarkko Nikula | 49100c9 | 2010-05-05 11:14:22 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * rx51.c -- SoC audio for Nokia RX-51 |
| 3 | * |
| 4 | * Copyright (C) 2008 - 2009 Nokia Corporation |
| 5 | * |
| 6 | * Contact: Peter Ujfalusi <peter.ujfalusi@nokia.com> |
| 7 | * Eduardo Valentin <eduardo.valentin@nokia.com> |
| 8 | * Jarkko Nikula <jhnikula@gmail.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <sound/core.h> |
| 30 | #include <sound/pcm.h> |
| 31 | #include <sound/soc.h> |
| 32 | #include <sound/soc-dapm.h> |
| 33 | |
| 34 | #include <asm/mach-types.h> |
| 35 | |
| 36 | #include "omap-mcbsp.h" |
| 37 | #include "omap-pcm.h" |
| 38 | #include "../codecs/tlv320aic3x.h" |
| 39 | |
| 40 | /* |
| 41 | * REVISIT: TWL4030 GPIO base in RX-51. Now statically defined to 192. This |
| 42 | * gpio is reserved in arch/arm/mach-omap2/board-rx51-peripherals.c |
| 43 | */ |
| 44 | #define RX51_SPEAKER_AMP_TWL_GPIO (192 + 7) |
| 45 | |
| 46 | static int rx51_spk_func; |
| 47 | static int rx51_dmic_func; |
| 48 | |
| 49 | static void rx51_ext_control(struct snd_soc_codec *codec) |
| 50 | { |
| 51 | if (rx51_spk_func) |
| 52 | snd_soc_dapm_enable_pin(codec, "Ext Spk"); |
| 53 | else |
| 54 | snd_soc_dapm_disable_pin(codec, "Ext Spk"); |
| 55 | if (rx51_dmic_func) |
| 56 | snd_soc_dapm_enable_pin(codec, "DMic"); |
| 57 | else |
| 58 | snd_soc_dapm_disable_pin(codec, "DMic"); |
| 59 | |
| 60 | snd_soc_dapm_sync(codec); |
| 61 | } |
| 62 | |
| 63 | static int rx51_startup(struct snd_pcm_substream *substream) |
| 64 | { |
| 65 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 66 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 67 | struct snd_soc_codec *codec = rtd->socdev->card->codec; |
| 68 | |
| 69 | snd_pcm_hw_constraint_minmax(runtime, |
| 70 | SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2); |
| 71 | rx51_ext_control(codec); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int rx51_hw_params(struct snd_pcm_substream *substream, |
| 77 | struct snd_pcm_hw_params *params) |
| 78 | { |
| 79 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 80 | struct snd_soc_dai *codec_dai = rtd->dai->codec_dai; |
| 81 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
| 82 | int err; |
| 83 | |
| 84 | /* Set codec DAI configuration */ |
| 85 | err = snd_soc_dai_set_fmt(codec_dai, |
| 86 | SND_SOC_DAIFMT_DSP_A | |
| 87 | SND_SOC_DAIFMT_IB_NF | |
| 88 | SND_SOC_DAIFMT_CBM_CFM); |
| 89 | if (err < 0) |
| 90 | return err; |
| 91 | |
| 92 | /* Set cpu DAI configuration */ |
| 93 | err = snd_soc_dai_set_fmt(cpu_dai, |
| 94 | SND_SOC_DAIFMT_DSP_A | |
| 95 | SND_SOC_DAIFMT_IB_NF | |
| 96 | SND_SOC_DAIFMT_CBM_CFM); |
| 97 | if (err < 0) |
| 98 | return err; |
| 99 | |
| 100 | /* Set the codec system clock for DAC and ADC */ |
| 101 | return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000, |
| 102 | SND_SOC_CLOCK_IN); |
| 103 | } |
| 104 | |
| 105 | static struct snd_soc_ops rx51_ops = { |
| 106 | .startup = rx51_startup, |
| 107 | .hw_params = rx51_hw_params, |
| 108 | }; |
| 109 | |
| 110 | static int rx51_get_spk(struct snd_kcontrol *kcontrol, |
| 111 | struct snd_ctl_elem_value *ucontrol) |
| 112 | { |
| 113 | ucontrol->value.integer.value[0] = rx51_spk_func; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int rx51_set_spk(struct snd_kcontrol *kcontrol, |
| 119 | struct snd_ctl_elem_value *ucontrol) |
| 120 | { |
| 121 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 122 | |
| 123 | if (rx51_spk_func == ucontrol->value.integer.value[0]) |
| 124 | return 0; |
| 125 | |
| 126 | rx51_spk_func = ucontrol->value.integer.value[0]; |
| 127 | rx51_ext_control(codec); |
| 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | static int rx51_spk_event(struct snd_soc_dapm_widget *w, |
| 133 | struct snd_kcontrol *k, int event) |
| 134 | { |
| 135 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 136 | gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 1); |
| 137 | else |
| 138 | gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 0); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int rx51_get_input(struct snd_kcontrol *kcontrol, |
| 144 | struct snd_ctl_elem_value *ucontrol) |
| 145 | { |
| 146 | ucontrol->value.integer.value[0] = rx51_dmic_func; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int rx51_set_input(struct snd_kcontrol *kcontrol, |
| 152 | struct snd_ctl_elem_value *ucontrol) |
| 153 | { |
| 154 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 155 | |
| 156 | if (rx51_dmic_func == ucontrol->value.integer.value[0]) |
| 157 | return 0; |
| 158 | |
| 159 | rx51_dmic_func = ucontrol->value.integer.value[0]; |
| 160 | rx51_ext_control(codec); |
| 161 | |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = { |
| 166 | SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event), |
| 167 | SND_SOC_DAPM_MIC("DMic", NULL), |
| 168 | }; |
| 169 | |
| 170 | static const struct snd_soc_dapm_route audio_map[] = { |
| 171 | {"Ext Spk", NULL, "HPLOUT"}, |
| 172 | {"Ext Spk", NULL, "HPROUT"}, |
| 173 | |
| 174 | {"DMic Rate 64", NULL, "Mic Bias 2V"}, |
| 175 | {"Mic Bias 2V", NULL, "DMic"}, |
| 176 | }; |
| 177 | |
| 178 | static const char *spk_function[] = {"Off", "On"}; |
| 179 | static const char *input_function[] = {"ADC", "Digital Mic"}; |
| 180 | |
| 181 | static const struct soc_enum rx51_enum[] = { |
| 182 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function), |
| 183 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function), |
| 184 | }; |
| 185 | |
| 186 | static const struct snd_kcontrol_new aic34_rx51_controls[] = { |
| 187 | SOC_ENUM_EXT("Speaker Function", rx51_enum[0], |
| 188 | rx51_get_spk, rx51_set_spk), |
| 189 | SOC_ENUM_EXT("Input Select", rx51_enum[1], |
| 190 | rx51_get_input, rx51_set_input), |
| 191 | }; |
| 192 | |
| 193 | static int rx51_aic34_init(struct snd_soc_codec *codec) |
| 194 | { |
| 195 | int err; |
| 196 | |
| 197 | /* Set up NC codec pins */ |
| 198 | snd_soc_dapm_nc_pin(codec, "MIC3L"); |
| 199 | snd_soc_dapm_nc_pin(codec, "MIC3R"); |
| 200 | snd_soc_dapm_nc_pin(codec, "LINE1R"); |
| 201 | |
| 202 | /* Add RX-51 specific controls */ |
| 203 | err = snd_soc_add_controls(codec, aic34_rx51_controls, |
| 204 | ARRAY_SIZE(aic34_rx51_controls)); |
| 205 | if (err < 0) |
| 206 | return err; |
| 207 | |
| 208 | /* Add RX-51 specific widgets */ |
| 209 | snd_soc_dapm_new_controls(codec, aic34_dapm_widgets, |
| 210 | ARRAY_SIZE(aic34_dapm_widgets)); |
| 211 | |
| 212 | /* Set up RX-51 specific audio path audio_map */ |
| 213 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 214 | |
| 215 | snd_soc_dapm_sync(codec); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Digital audio interface glue - connects codec <--> CPU */ |
| 221 | static struct snd_soc_dai_link rx51_dai[] = { |
| 222 | { |
| 223 | .name = "TLV320AIC34", |
| 224 | .stream_name = "AIC34", |
| 225 | .cpu_dai = &omap_mcbsp_dai[0], |
| 226 | .codec_dai = &aic3x_dai, |
| 227 | .init = rx51_aic34_init, |
| 228 | .ops = &rx51_ops, |
| 229 | }, |
| 230 | }; |
| 231 | |
| 232 | /* Audio private data */ |
| 233 | static struct aic3x_setup_data rx51_aic34_setup = { |
| 234 | .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED, |
| 235 | .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT, |
| 236 | }; |
| 237 | |
| 238 | /* Audio card */ |
| 239 | static struct snd_soc_card rx51_sound_card = { |
| 240 | .name = "RX-51", |
| 241 | .dai_link = rx51_dai, |
| 242 | .num_links = ARRAY_SIZE(rx51_dai), |
| 243 | .platform = &omap_soc_platform, |
| 244 | }; |
| 245 | |
| 246 | /* Audio subsystem */ |
| 247 | static struct snd_soc_device rx51_snd_devdata = { |
| 248 | .card = &rx51_sound_card, |
| 249 | .codec_dev = &soc_codec_dev_aic3x, |
| 250 | .codec_data = &rx51_aic34_setup, |
| 251 | }; |
| 252 | |
| 253 | static struct platform_device *rx51_snd_device; |
| 254 | |
| 255 | static int __init rx51_soc_init(void) |
| 256 | { |
| 257 | int err; |
| 258 | |
| 259 | if (!machine_is_nokia_rx51()) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | rx51_snd_device = platform_device_alloc("soc-audio", -1); |
| 263 | if (!rx51_snd_device) { |
| 264 | err = -ENOMEM; |
| 265 | goto err1; |
| 266 | } |
| 267 | |
| 268 | platform_set_drvdata(rx51_snd_device, &rx51_snd_devdata); |
| 269 | rx51_snd_devdata.dev = &rx51_snd_device->dev; |
| 270 | *(unsigned int *)rx51_dai[0].cpu_dai->private_data = 1; /* McBSP2 */ |
| 271 | |
| 272 | err = platform_device_add(rx51_snd_device); |
| 273 | if (err) |
| 274 | goto err2; |
| 275 | |
| 276 | return 0; |
| 277 | err2: |
| 278 | platform_device_put(rx51_snd_device); |
| 279 | err1: |
| 280 | |
| 281 | return err; |
| 282 | } |
| 283 | |
| 284 | static void __exit rx51_soc_exit(void) |
| 285 | { |
| 286 | platform_device_unregister(rx51_snd_device); |
| 287 | } |
| 288 | |
| 289 | module_init(rx51_soc_init); |
| 290 | module_exit(rx51_soc_exit); |
| 291 | |
| 292 | MODULE_AUTHOR("Nokia Corporation"); |
| 293 | MODULE_DESCRIPTION("ALSA SoC Nokia RX-51"); |
| 294 | MODULE_LICENSE("GPL"); |