Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * harmony.c - Harmony machine ASoC driver |
| 3 | * |
| 4 | * Author: Stephen Warren <swarren@nvidia.com> |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2010-2011 - NVIDIA, Inc. |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on code copyright/by: |
| 8 | * |
| 9 | * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd. |
| 10 | * |
| 11 | * Copyright 2007 Wolfson Microelectronics PLC. |
| 12 | * Author: Graeme Gregory |
| 13 | * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * version 2 as published by the Free Software Foundation. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, but |
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 27 | * 02110-1301 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <asm/mach-types.h> |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 32 | |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 33 | #include <linux/module.h> |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
| 35 | #include <linux/slab.h> |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 36 | #include <linux/gpio.h> |
| 37 | |
| 38 | #include <mach/harmony_audio.h> |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 39 | |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 40 | #include <sound/core.h> |
| 41 | #include <sound/pcm.h> |
| 42 | #include <sound/pcm_params.h> |
| 43 | #include <sound/soc.h> |
| 44 | |
| 45 | #include "tegra_das.h" |
| 46 | #include "tegra_i2s.h" |
| 47 | #include "tegra_pcm.h" |
| 48 | #include "tegra_asoc_utils.h" |
| 49 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 50 | #define DRV_NAME "tegra-snd-harmony" |
| 51 | #define PREFIX DRV_NAME ": " |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 52 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 53 | struct tegra_harmony { |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 54 | struct harmony_audio_platform_data *pdata; |
| 55 | int gpio_spkr_en_requested; |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 56 | }; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 57 | |
| 58 | static int harmony_asoc_hw_params(struct snd_pcm_substream *substream, |
| 59 | struct snd_pcm_hw_params *params) |
| 60 | { |
| 61 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 62 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 63 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 64 | int srate, mclk, mclk_change; |
| 65 | int err; |
| 66 | |
| 67 | srate = params_rate(params); |
| 68 | switch (srate) { |
| 69 | case 64000: |
| 70 | case 88200: |
| 71 | case 96000: |
| 72 | mclk = 128 * srate; |
| 73 | break; |
| 74 | default: |
| 75 | mclk = 256 * srate; |
| 76 | break; |
| 77 | } |
| 78 | /* FIXME: Codec only requires >= 3MHz if OSR==0 */ |
| 79 | while (mclk < 6000000) |
| 80 | mclk *= 2; |
| 81 | |
| 82 | err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change); |
| 83 | if (err < 0) { |
| 84 | pr_err(PREFIX "Can't configure clocks\n"); |
| 85 | return err; |
| 86 | } |
| 87 | |
| 88 | err = snd_soc_dai_set_fmt(codec_dai, |
| 89 | SND_SOC_DAIFMT_I2S | |
| 90 | SND_SOC_DAIFMT_NB_NF | |
| 91 | SND_SOC_DAIFMT_CBS_CFS); |
| 92 | if (err < 0) { |
| 93 | pr_err(PREFIX "codec_dai fmt not set\n"); |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | err = snd_soc_dai_set_fmt(cpu_dai, |
| 98 | SND_SOC_DAIFMT_I2S | |
| 99 | SND_SOC_DAIFMT_NB_NF | |
| 100 | SND_SOC_DAIFMT_CBS_CFS); |
| 101 | if (err < 0) { |
| 102 | pr_err(PREFIX "cpu_dai fmt not set\n"); |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | if (mclk_change) { |
Stephen Warren | bc72fe0 | 2011-01-28 14:26:38 -0700 | [diff] [blame^] | 107 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 108 | SND_SOC_CLOCK_IN); |
| 109 | if (err < 0) { |
| 110 | pr_err(PREFIX "codec_dai clock not set\n"); |
| 111 | return err; |
| 112 | } |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static struct snd_soc_ops harmony_asoc_ops = { |
| 119 | .hw_params = harmony_asoc_hw_params, |
| 120 | }; |
| 121 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 122 | static int harmony_event_int_spk(struct snd_soc_dapm_widget *w, |
| 123 | struct snd_kcontrol *k, int event) |
| 124 | { |
| 125 | struct snd_soc_codec *codec = w->codec; |
| 126 | struct snd_soc_card *card = codec->card; |
| 127 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
| 128 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
| 129 | |
| 130 | gpio_set_value_cansleep(pdata->gpio_spkr_en, |
| 131 | !!SND_SOC_DAPM_EVENT_ON(event)); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 136 | static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = { |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 137 | SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk), |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 138 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 139 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 140 | }; |
| 141 | |
| 142 | static const struct snd_soc_dapm_route harmony_audio_map[] = { |
| 143 | {"Headphone Jack", NULL, "HPOUTR"}, |
| 144 | {"Headphone Jack", NULL, "HPOUTL"}, |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 145 | {"Int Spk", NULL, "ROP"}, |
| 146 | {"Int Spk", NULL, "RON"}, |
| 147 | {"Int Spk", NULL, "LOP"}, |
| 148 | {"Int Spk", NULL, "LON"}, |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 149 | {"Mic Bias", NULL, "Mic Jack"}, |
| 150 | {"IN1L", NULL, "Mic Bias"}, |
| 151 | }; |
| 152 | |
| 153 | static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd) |
| 154 | { |
| 155 | struct snd_soc_codec *codec = rtd->codec; |
| 156 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 157 | struct snd_soc_card *card = codec->card; |
| 158 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
| 159 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
| 160 | int ret; |
| 161 | |
| 162 | ret = gpio_request(pdata->gpio_spkr_en, "spkr_en"); |
| 163 | if (ret) { |
| 164 | dev_err(card->dev, "cannot get spkr_en gpio\n"); |
| 165 | return ret; |
| 166 | } |
| 167 | harmony->gpio_spkr_en_requested = 1; |
| 168 | |
| 169 | gpio_direction_output(pdata->gpio_spkr_en, 0); |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 170 | |
| 171 | snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets, |
| 172 | ARRAY_SIZE(harmony_dapm_widgets)); |
| 173 | |
| 174 | snd_soc_dapm_add_routes(dapm, harmony_audio_map, |
| 175 | ARRAY_SIZE(harmony_audio_map)); |
| 176 | |
| 177 | snd_soc_dapm_enable_pin(dapm, "Headphone Jack"); |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 178 | snd_soc_dapm_enable_pin(dapm, "Int Spk"); |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 179 | snd_soc_dapm_enable_pin(dapm, "Mic Jack"); |
| 180 | snd_soc_dapm_sync(dapm); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 185 | static struct snd_soc_dai_link harmony_wm8903_dai = { |
| 186 | .name = "WM8903", |
| 187 | .stream_name = "WM8903 PCM", |
| 188 | .codec_name = "wm8903-codec.0-001a", |
| 189 | .platform_name = "tegra-pcm-audio", |
| 190 | .cpu_dai_name = "tegra-i2s.0", |
| 191 | .codec_dai_name = "wm8903-hifi", |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 192 | .init = harmony_asoc_init, |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 193 | .ops = &harmony_asoc_ops, |
| 194 | }; |
| 195 | |
| 196 | static struct snd_soc_card snd_soc_harmony = { |
| 197 | .name = "tegra-harmony", |
| 198 | .dai_link = &harmony_wm8903_dai, |
| 199 | .num_links = 1, |
| 200 | }; |
| 201 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 202 | static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev) |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 203 | { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 204 | struct snd_soc_card *card = &snd_soc_harmony; |
| 205 | struct tegra_harmony *harmony; |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 206 | struct harmony_audio_platform_data *pdata; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 207 | int ret; |
| 208 | |
| 209 | if (!machine_is_harmony()) { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 210 | dev_err(&pdev->dev, "Not running on Tegra Harmony!\n"); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 211 | return -ENODEV; |
| 212 | } |
| 213 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 214 | pdata = pdev->dev.platform_data; |
| 215 | if (!pdata) { |
| 216 | dev_err(&pdev->dev, "no platform data supplied\n"); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 220 | harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL); |
| 221 | if (!harmony) { |
| 222 | dev_err(&pdev->dev, "Can't allocate tegra_harmony\n"); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 226 | harmony->pdata = pdata; |
| 227 | |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 228 | ret = tegra_asoc_utils_init(); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 229 | if (ret) |
| 230 | goto err_free_harmony; |
| 231 | |
| 232 | card->dev = &pdev->dev; |
| 233 | platform_set_drvdata(pdev, card); |
| 234 | snd_soc_card_set_drvdata(card, harmony); |
| 235 | |
| 236 | ret = snd_soc_register_card(card); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 237 | if (ret) { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 238 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 239 | ret); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 240 | goto err_clear_drvdata; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 245 | err_clear_drvdata: |
| 246 | snd_soc_card_set_drvdata(card, NULL); |
| 247 | platform_set_drvdata(pdev, NULL); |
| 248 | card->dev = NULL; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 249 | tegra_asoc_utils_fini(); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 250 | err_free_harmony: |
| 251 | kfree(harmony); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 252 | return ret; |
| 253 | } |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 254 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 255 | static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev) |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 256 | { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 257 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 258 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 259 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 260 | |
| 261 | snd_soc_unregister_card(card); |
| 262 | |
| 263 | snd_soc_card_set_drvdata(card, NULL); |
| 264 | platform_set_drvdata(pdev, NULL); |
| 265 | card->dev = NULL; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 266 | |
| 267 | tegra_asoc_utils_fini(); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 268 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 269 | if (harmony->gpio_spkr_en_requested) |
| 270 | gpio_free(pdata->gpio_spkr_en); |
| 271 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 272 | kfree(harmony); |
| 273 | |
| 274 | return 0; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 275 | } |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 276 | |
| 277 | static struct platform_driver tegra_snd_harmony_driver = { |
| 278 | .driver = { |
| 279 | .name = DRV_NAME, |
| 280 | .owner = THIS_MODULE, |
| 281 | }, |
| 282 | .probe = tegra_snd_harmony_probe, |
| 283 | .remove = __devexit_p(tegra_snd_harmony_remove), |
| 284 | }; |
| 285 | |
| 286 | static int __init snd_tegra_harmony_init(void) |
| 287 | { |
| 288 | return platform_driver_register(&tegra_snd_harmony_driver); |
| 289 | } |
| 290 | module_init(snd_tegra_harmony_init); |
| 291 | |
| 292 | static void __exit snd_tegra_harmony_exit(void) |
| 293 | { |
| 294 | platform_driver_unregister(&tegra_snd_harmony_driver); |
| 295 | } |
| 296 | module_exit(snd_tegra_harmony_exit); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 297 | |
| 298 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 299 | MODULE_DESCRIPTION("Harmony machine ASoC driver"); |
| 300 | MODULE_LICENSE("GPL"); |