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> |
| 5 | * Copyright (C) 2010 - NVIDIA, Inc. |
| 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> |
| 32 | #include <linux/module.h> |
| 33 | #include <sound/core.h> |
| 34 | #include <sound/pcm.h> |
| 35 | #include <sound/pcm_params.h> |
| 36 | #include <sound/soc.h> |
| 37 | |
| 38 | #include "tegra_das.h" |
| 39 | #include "tegra_i2s.h" |
| 40 | #include "tegra_pcm.h" |
| 41 | #include "tegra_asoc_utils.h" |
| 42 | |
| 43 | #define PREFIX "ASoC Harmony: " |
| 44 | |
| 45 | static struct platform_device *harmony_snd_device; |
| 46 | |
| 47 | static int harmony_asoc_hw_params(struct snd_pcm_substream *substream, |
| 48 | struct snd_pcm_hw_params *params) |
| 49 | { |
| 50 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 51 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 52 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 53 | int srate, mclk, mclk_change; |
| 54 | int err; |
| 55 | |
| 56 | srate = params_rate(params); |
| 57 | switch (srate) { |
| 58 | case 64000: |
| 59 | case 88200: |
| 60 | case 96000: |
| 61 | mclk = 128 * srate; |
| 62 | break; |
| 63 | default: |
| 64 | mclk = 256 * srate; |
| 65 | break; |
| 66 | } |
| 67 | /* FIXME: Codec only requires >= 3MHz if OSR==0 */ |
| 68 | while (mclk < 6000000) |
| 69 | mclk *= 2; |
| 70 | |
| 71 | err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change); |
| 72 | if (err < 0) { |
| 73 | pr_err(PREFIX "Can't configure clocks\n"); |
| 74 | return err; |
| 75 | } |
| 76 | |
| 77 | err = snd_soc_dai_set_fmt(codec_dai, |
| 78 | SND_SOC_DAIFMT_I2S | |
| 79 | SND_SOC_DAIFMT_NB_NF | |
| 80 | SND_SOC_DAIFMT_CBS_CFS); |
| 81 | if (err < 0) { |
| 82 | pr_err(PREFIX "codec_dai fmt not set\n"); |
| 83 | return err; |
| 84 | } |
| 85 | |
| 86 | err = snd_soc_dai_set_fmt(cpu_dai, |
| 87 | SND_SOC_DAIFMT_I2S | |
| 88 | SND_SOC_DAIFMT_NB_NF | |
| 89 | SND_SOC_DAIFMT_CBS_CFS); |
| 90 | if (err < 0) { |
| 91 | pr_err(PREFIX "cpu_dai fmt not set\n"); |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | if (mclk_change) { |
| 96 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN); |
| 97 | if (err < 0) { |
| 98 | pr_err(PREFIX "codec_dai clock not set\n"); |
| 99 | return err; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static struct snd_soc_ops harmony_asoc_ops = { |
| 107 | .hw_params = harmony_asoc_hw_params, |
| 108 | }; |
| 109 | |
| 110 | static struct snd_soc_dai_link harmony_wm8903_dai = { |
| 111 | .name = "WM8903", |
| 112 | .stream_name = "WM8903 PCM", |
| 113 | .codec_name = "wm8903-codec.0-001a", |
| 114 | .platform_name = "tegra-pcm-audio", |
| 115 | .cpu_dai_name = "tegra-i2s.0", |
| 116 | .codec_dai_name = "wm8903-hifi", |
| 117 | .ops = &harmony_asoc_ops, |
| 118 | }; |
| 119 | |
| 120 | static struct snd_soc_card snd_soc_harmony = { |
| 121 | .name = "tegra-harmony", |
| 122 | .dai_link = &harmony_wm8903_dai, |
| 123 | .num_links = 1, |
| 124 | }; |
| 125 | |
| 126 | static int __init harmony_soc_modinit(void) |
| 127 | { |
| 128 | int ret; |
| 129 | |
| 130 | if (!machine_is_harmony()) { |
| 131 | pr_err(PREFIX "Not running on Tegra Harmony!\n"); |
| 132 | return -ENODEV; |
| 133 | } |
| 134 | |
| 135 | ret = tegra_asoc_utils_init(); |
| 136 | if (ret) { |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Create and register platform device |
| 142 | */ |
| 143 | harmony_snd_device = platform_device_alloc("soc-audio", -1); |
| 144 | if (harmony_snd_device == NULL) { |
| 145 | pr_err(PREFIX "platform_device_alloc failed\n"); |
| 146 | ret = -ENOMEM; |
| 147 | goto err_clock_utils; |
| 148 | } |
| 149 | |
| 150 | platform_set_drvdata(harmony_snd_device, &snd_soc_harmony); |
| 151 | |
| 152 | ret = platform_device_add(harmony_snd_device); |
| 153 | if (ret) { |
| 154 | pr_err(PREFIX "platform_device_add failed (%d)\n", |
| 155 | ret); |
| 156 | goto err_device_put; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | |
| 161 | err_device_put: |
| 162 | platform_device_put(harmony_snd_device); |
| 163 | err_clock_utils: |
| 164 | tegra_asoc_utils_fini(); |
| 165 | return ret; |
| 166 | } |
| 167 | module_init(harmony_soc_modinit); |
| 168 | |
| 169 | static void __exit harmony_soc_modexit(void) |
| 170 | { |
| 171 | platform_device_unregister(harmony_snd_device); |
| 172 | |
| 173 | tegra_asoc_utils_fini(); |
| 174 | } |
| 175 | module_exit(harmony_soc_modexit); |
| 176 | |
| 177 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
| 178 | MODULE_DESCRIPTION("Harmony machine ASoC driver"); |
| 179 | MODULE_LICENSE("GPL"); |