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" |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 51 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 52 | struct tegra_harmony { |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 53 | struct tegra_asoc_utils_data util_data; |
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; |
Stephen Warren | c244d47 | 2011-01-28 14:26:39 -0700 | [diff] [blame] | 64 | struct snd_soc_codec *codec = rtd->codec; |
| 65 | struct snd_soc_card *card = codec->card; |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 66 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 67 | int srate, mclk, mclk_change; |
| 68 | int err; |
| 69 | |
| 70 | srate = params_rate(params); |
| 71 | switch (srate) { |
| 72 | case 64000: |
| 73 | case 88200: |
| 74 | case 96000: |
| 75 | mclk = 128 * srate; |
| 76 | break; |
| 77 | default: |
| 78 | mclk = 256 * srate; |
| 79 | break; |
| 80 | } |
| 81 | /* FIXME: Codec only requires >= 3MHz if OSR==0 */ |
| 82 | while (mclk < 6000000) |
| 83 | mclk *= 2; |
| 84 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 85 | err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk, |
| 86 | &mclk_change); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 87 | if (err < 0) { |
Stephen Warren | c244d47 | 2011-01-28 14:26:39 -0700 | [diff] [blame] | 88 | dev_err(card->dev, "Can't configure clocks\n"); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 89 | return err; |
| 90 | } |
| 91 | |
| 92 | err = snd_soc_dai_set_fmt(codec_dai, |
| 93 | SND_SOC_DAIFMT_I2S | |
| 94 | SND_SOC_DAIFMT_NB_NF | |
| 95 | SND_SOC_DAIFMT_CBS_CFS); |
| 96 | if (err < 0) { |
Stephen Warren | c244d47 | 2011-01-28 14:26:39 -0700 | [diff] [blame] | 97 | dev_err(card->dev, "codec_dai fmt not set\n"); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 98 | return err; |
| 99 | } |
| 100 | |
| 101 | err = snd_soc_dai_set_fmt(cpu_dai, |
| 102 | SND_SOC_DAIFMT_I2S | |
| 103 | SND_SOC_DAIFMT_NB_NF | |
| 104 | SND_SOC_DAIFMT_CBS_CFS); |
| 105 | if (err < 0) { |
Stephen Warren | c244d47 | 2011-01-28 14:26:39 -0700 | [diff] [blame] | 106 | dev_err(card->dev, "cpu_dai fmt not set\n"); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 107 | return err; |
| 108 | } |
| 109 | |
| 110 | if (mclk_change) { |
Stephen Warren | bc72fe0 | 2011-01-28 14:26:38 -0700 | [diff] [blame] | 111 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 112 | SND_SOC_CLOCK_IN); |
| 113 | if (err < 0) { |
Stephen Warren | c244d47 | 2011-01-28 14:26:39 -0700 | [diff] [blame] | 114 | dev_err(card->dev, "codec_dai clock not set\n"); |
Stephen Warren | bc72fe0 | 2011-01-28 14:26:38 -0700 | [diff] [blame] | 115 | return err; |
| 116 | } |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static struct snd_soc_ops harmony_asoc_ops = { |
| 123 | .hw_params = harmony_asoc_hw_params, |
| 124 | }; |
| 125 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 126 | static int harmony_event_int_spk(struct snd_soc_dapm_widget *w, |
| 127 | struct snd_kcontrol *k, int event) |
| 128 | { |
| 129 | struct snd_soc_codec *codec = w->codec; |
| 130 | struct snd_soc_card *card = codec->card; |
| 131 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
| 132 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
| 133 | |
| 134 | gpio_set_value_cansleep(pdata->gpio_spkr_en, |
Stephen Warren | f9eabc3 | 2011-01-31 11:00:17 -0700 | [diff] [blame] | 135 | SND_SOC_DAPM_EVENT_ON(event)); |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 140 | static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = { |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 141 | SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk), |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 142 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 143 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
| 144 | }; |
| 145 | |
| 146 | static const struct snd_soc_dapm_route harmony_audio_map[] = { |
| 147 | {"Headphone Jack", NULL, "HPOUTR"}, |
| 148 | {"Headphone Jack", NULL, "HPOUTL"}, |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 149 | {"Int Spk", NULL, "ROP"}, |
| 150 | {"Int Spk", NULL, "RON"}, |
| 151 | {"Int Spk", NULL, "LOP"}, |
| 152 | {"Int Spk", NULL, "LON"}, |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 153 | {"Mic Bias", NULL, "Mic Jack"}, |
| 154 | {"IN1L", NULL, "Mic Bias"}, |
| 155 | }; |
| 156 | |
| 157 | static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd) |
| 158 | { |
| 159 | struct snd_soc_codec *codec = rtd->codec; |
| 160 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 161 | struct snd_soc_card *card = codec->card; |
| 162 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
| 163 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
| 164 | int ret; |
| 165 | |
| 166 | ret = gpio_request(pdata->gpio_spkr_en, "spkr_en"); |
| 167 | if (ret) { |
| 168 | dev_err(card->dev, "cannot get spkr_en gpio\n"); |
| 169 | return ret; |
| 170 | } |
| 171 | harmony->gpio_spkr_en_requested = 1; |
| 172 | |
| 173 | gpio_direction_output(pdata->gpio_spkr_en, 0); |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 174 | |
| 175 | snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets, |
| 176 | ARRAY_SIZE(harmony_dapm_widgets)); |
| 177 | |
| 178 | snd_soc_dapm_add_routes(dapm, harmony_audio_map, |
| 179 | ARRAY_SIZE(harmony_audio_map)); |
| 180 | |
| 181 | snd_soc_dapm_enable_pin(dapm, "Headphone Jack"); |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 182 | snd_soc_dapm_enable_pin(dapm, "Int Spk"); |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 183 | snd_soc_dapm_enable_pin(dapm, "Mic Jack"); |
| 184 | snd_soc_dapm_sync(dapm); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 189 | static struct snd_soc_dai_link harmony_wm8903_dai = { |
| 190 | .name = "WM8903", |
| 191 | .stream_name = "WM8903 PCM", |
| 192 | .codec_name = "wm8903-codec.0-001a", |
| 193 | .platform_name = "tegra-pcm-audio", |
| 194 | .cpu_dai_name = "tegra-i2s.0", |
| 195 | .codec_dai_name = "wm8903-hifi", |
Stephen Warren | 62ffac4 | 2011-01-13 15:22:08 -0700 | [diff] [blame] | 196 | .init = harmony_asoc_init, |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 197 | .ops = &harmony_asoc_ops, |
| 198 | }; |
| 199 | |
| 200 | static struct snd_soc_card snd_soc_harmony = { |
| 201 | .name = "tegra-harmony", |
| 202 | .dai_link = &harmony_wm8903_dai, |
| 203 | .num_links = 1, |
| 204 | }; |
| 205 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 206 | static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev) |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 207 | { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 208 | struct snd_soc_card *card = &snd_soc_harmony; |
| 209 | struct tegra_harmony *harmony; |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 210 | struct harmony_audio_platform_data *pdata; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 211 | int ret; |
| 212 | |
| 213 | if (!machine_is_harmony()) { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 214 | dev_err(&pdev->dev, "Not running on Tegra Harmony!\n"); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 215 | return -ENODEV; |
| 216 | } |
| 217 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 218 | pdata = pdev->dev.platform_data; |
| 219 | if (!pdata) { |
| 220 | dev_err(&pdev->dev, "no platform data supplied\n"); |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 224 | harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL); |
| 225 | if (!harmony) { |
| 226 | dev_err(&pdev->dev, "Can't allocate tegra_harmony\n"); |
| 227 | return -ENOMEM; |
| 228 | } |
| 229 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 230 | harmony->pdata = pdata; |
| 231 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 232 | ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 233 | if (ret) |
| 234 | goto err_free_harmony; |
| 235 | |
| 236 | card->dev = &pdev->dev; |
| 237 | platform_set_drvdata(pdev, card); |
| 238 | snd_soc_card_set_drvdata(card, harmony); |
| 239 | |
| 240 | ret = snd_soc_register_card(card); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 241 | if (ret) { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 242 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 243 | ret); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 244 | goto err_clear_drvdata; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 249 | err_clear_drvdata: |
| 250 | snd_soc_card_set_drvdata(card, NULL); |
| 251 | platform_set_drvdata(pdev, NULL); |
| 252 | card->dev = NULL; |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 253 | tegra_asoc_utils_fini(&harmony->util_data); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 254 | err_free_harmony: |
| 255 | kfree(harmony); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 256 | return ret; |
| 257 | } |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 258 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 259 | static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev) |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 260 | { |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 261 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 262 | struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card); |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 263 | struct harmony_audio_platform_data *pdata = harmony->pdata; |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 264 | |
| 265 | snd_soc_unregister_card(card); |
| 266 | |
| 267 | snd_soc_card_set_drvdata(card, NULL); |
| 268 | platform_set_drvdata(pdev, NULL); |
| 269 | card->dev = NULL; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 270 | |
Stephen Warren | d64e57c | 2011-01-28 14:26:40 -0700 | [diff] [blame] | 271 | tegra_asoc_utils_fini(&harmony->util_data); |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 272 | |
Stephen Warren | 6e26764 | 2011-01-28 14:26:37 -0700 | [diff] [blame] | 273 | if (harmony->gpio_spkr_en_requested) |
| 274 | gpio_free(pdata->gpio_spkr_en); |
| 275 | |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 276 | kfree(harmony); |
| 277 | |
| 278 | return 0; |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 279 | } |
Stephen Warren | 72de2b1 | 2011-01-28 14:26:36 -0700 | [diff] [blame] | 280 | |
| 281 | static struct platform_driver tegra_snd_harmony_driver = { |
| 282 | .driver = { |
| 283 | .name = DRV_NAME, |
| 284 | .owner = THIS_MODULE, |
| 285 | }, |
| 286 | .probe = tegra_snd_harmony_probe, |
| 287 | .remove = __devexit_p(tegra_snd_harmony_remove), |
| 288 | }; |
| 289 | |
| 290 | static int __init snd_tegra_harmony_init(void) |
| 291 | { |
| 292 | return platform_driver_register(&tegra_snd_harmony_driver); |
| 293 | } |
| 294 | module_init(snd_tegra_harmony_init); |
| 295 | |
| 296 | static void __exit snd_tegra_harmony_exit(void) |
| 297 | { |
| 298 | platform_driver_unregister(&tegra_snd_harmony_driver); |
| 299 | } |
| 300 | module_exit(snd_tegra_harmony_exit); |
Stephen Warren | a8bf1ba | 2011-01-07 22:36:16 -0700 | [diff] [blame] | 301 | |
| 302 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 303 | MODULE_DESCRIPTION("Harmony machine ASoC driver"); |
| 304 | MODULE_LICENSE("GPL"); |