linux/sound/soc/tegra/tegra_wm8903.c
<<
>>
Prefs
   1/*
   2 * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
   3 *
   4 * Author: Stephen Warren <swarren@nvidia.com>
   5 * Copyright (C) 2010-2012 - 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 <linux/module.h>
  32#include <linux/platform_device.h>
  33#include <linux/slab.h>
  34#include <linux/gpio.h>
  35#include <linux/of_gpio.h>
  36
  37#include <sound/core.h>
  38#include <sound/jack.h>
  39#include <sound/pcm.h>
  40#include <sound/pcm_params.h>
  41#include <sound/soc.h>
  42
  43#include "../codecs/wm8903.h"
  44
  45#include "tegra_asoc_utils.h"
  46
  47#define DRV_NAME "tegra-snd-wm8903"
  48
  49struct tegra_wm8903 {
  50        int gpio_spkr_en;
  51        int gpio_hp_det;
  52        int gpio_hp_mute;
  53        int gpio_int_mic_en;
  54        int gpio_ext_mic_en;
  55        struct tegra_asoc_utils_data util_data;
  56};
  57
  58static int tegra_wm8903_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_codec *codec = codec_dai->codec;
  64        struct snd_soc_card *card = codec->card;
  65        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
  66        int srate, mclk;
  67        int err;
  68
  69        srate = params_rate(params);
  70        switch (srate) {
  71        case 64000:
  72        case 88200:
  73        case 96000:
  74                mclk = 128 * srate;
  75                break;
  76        default:
  77                mclk = 256 * srate;
  78                break;
  79        }
  80        /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  81        while (mclk < 6000000)
  82                mclk *= 2;
  83
  84        err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
  85        if (err < 0) {
  86                dev_err(card->dev, "Can't configure clocks\n");
  87                return err;
  88        }
  89
  90        err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  91                                        SND_SOC_CLOCK_IN);
  92        if (err < 0) {
  93                dev_err(card->dev, "codec_dai clock not set\n");
  94                return err;
  95        }
  96
  97        return 0;
  98}
  99
 100static struct snd_soc_ops tegra_wm8903_ops = {
 101        .hw_params = tegra_wm8903_hw_params,
 102};
 103
 104static struct snd_soc_jack tegra_wm8903_hp_jack;
 105
 106static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
 107        {
 108                .pin = "Headphone Jack",
 109                .mask = SND_JACK_HEADPHONE,
 110        },
 111};
 112
 113static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
 114        .name = "headphone detect",
 115        .report = SND_JACK_HEADPHONE,
 116        .debounce_time = 150,
 117        .invert = 1,
 118};
 119
 120static struct snd_soc_jack tegra_wm8903_mic_jack;
 121
 122static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
 123        {
 124                .pin = "Mic Jack",
 125                .mask = SND_JACK_MICROPHONE,
 126        },
 127};
 128
 129static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
 130                                        struct snd_kcontrol *k, int event)
 131{
 132        struct snd_soc_dapm_context *dapm = w->dapm;
 133        struct snd_soc_card *card = dapm->card;
 134        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
 135
 136        if (!gpio_is_valid(machine->gpio_spkr_en))
 137                return 0;
 138
 139        gpio_set_value_cansleep(machine->gpio_spkr_en,
 140                                SND_SOC_DAPM_EVENT_ON(event));
 141
 142        return 0;
 143}
 144
 145static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
 146                                        struct snd_kcontrol *k, int event)
 147{
 148        struct snd_soc_dapm_context *dapm = w->dapm;
 149        struct snd_soc_card *card = dapm->card;
 150        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
 151
 152        if (!gpio_is_valid(machine->gpio_hp_mute))
 153                return 0;
 154
 155        gpio_set_value_cansleep(machine->gpio_hp_mute,
 156                                !SND_SOC_DAPM_EVENT_ON(event));
 157
 158        return 0;
 159}
 160
 161static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
 162        SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
 163        SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
 164        SND_SOC_DAPM_MIC("Mic Jack", NULL),
 165};
 166
 167static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
 168        SOC_DAPM_PIN_SWITCH("Int Spk"),
 169};
 170
 171static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
 172{
 173        struct snd_soc_dai *codec_dai = rtd->codec_dai;
 174        struct snd_soc_codec *codec = codec_dai->codec;
 175        struct snd_soc_dapm_context *dapm = &codec->dapm;
 176        struct snd_soc_card *card = codec->card;
 177        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
 178
 179        if (gpio_is_valid(machine->gpio_hp_det)) {
 180                tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
 181                snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
 182                                &tegra_wm8903_hp_jack);
 183                snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
 184                                        ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
 185                                        tegra_wm8903_hp_jack_pins);
 186                snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
 187                                        1,
 188                                        &tegra_wm8903_hp_jack_gpio);
 189        }
 190
 191        snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
 192                         &tegra_wm8903_mic_jack);
 193        snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
 194                              ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
 195                              tegra_wm8903_mic_jack_pins);
 196        wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
 197                                0);
 198
 199        snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
 200
 201        return 0;
 202}
 203
 204static int tegra_wm8903_remove(struct snd_soc_card *card)
 205{
 206        struct snd_soc_pcm_runtime *rtd = &(card->rtd[0]);
 207        struct snd_soc_dai *codec_dai = rtd->codec_dai;
 208        struct snd_soc_codec *codec = codec_dai->codec;
 209        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
 210
 211        if (gpio_is_valid(machine->gpio_hp_det)) {
 212                snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1,
 213                                        &tegra_wm8903_hp_jack_gpio);
 214        }
 215
 216        wm8903_mic_detect(codec, NULL, 0, 0);
 217
 218        return 0;
 219}
 220
 221static struct snd_soc_dai_link tegra_wm8903_dai = {
 222        .name = "WM8903",
 223        .stream_name = "WM8903 PCM",
 224        .codec_dai_name = "wm8903-hifi",
 225        .init = tegra_wm8903_init,
 226        .ops = &tegra_wm8903_ops,
 227        .dai_fmt = SND_SOC_DAIFMT_I2S |
 228                   SND_SOC_DAIFMT_NB_NF |
 229                   SND_SOC_DAIFMT_CBS_CFS,
 230};
 231
 232static struct snd_soc_card snd_soc_tegra_wm8903 = {
 233        .name = "tegra-wm8903",
 234        .owner = THIS_MODULE,
 235        .dai_link = &tegra_wm8903_dai,
 236        .num_links = 1,
 237        .remove = tegra_wm8903_remove,
 238        .controls = tegra_wm8903_controls,
 239        .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
 240        .dapm_widgets = tegra_wm8903_dapm_widgets,
 241        .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
 242        .fully_routed = true,
 243};
 244
 245static int tegra_wm8903_driver_probe(struct platform_device *pdev)
 246{
 247        struct device_node *np = pdev->dev.of_node;
 248        struct snd_soc_card *card = &snd_soc_tegra_wm8903;
 249        struct tegra_wm8903 *machine;
 250        int ret;
 251
 252        machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
 253                               GFP_KERNEL);
 254        if (!machine) {
 255                dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
 256                return -ENOMEM;
 257        }
 258
 259        card->dev = &pdev->dev;
 260        platform_set_drvdata(pdev, card);
 261        snd_soc_card_set_drvdata(card, machine);
 262
 263        machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
 264                                                  0);
 265        if (machine->gpio_spkr_en == -EPROBE_DEFER)
 266                return -EPROBE_DEFER;
 267        if (gpio_is_valid(machine->gpio_spkr_en)) {
 268                ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
 269                                            GPIOF_OUT_INIT_LOW, "spkr_en");
 270                if (ret) {
 271                        dev_err(card->dev, "cannot get spkr_en gpio\n");
 272                        return ret;
 273                }
 274        }
 275
 276        machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
 277                                                  0);
 278        if (machine->gpio_hp_mute == -EPROBE_DEFER)
 279                return -EPROBE_DEFER;
 280        if (gpio_is_valid(machine->gpio_hp_mute)) {
 281                ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
 282                                            GPIOF_OUT_INIT_HIGH, "hp_mute");
 283                if (ret) {
 284                        dev_err(card->dev, "cannot get hp_mute gpio\n");
 285                        return ret;
 286                }
 287        }
 288
 289        machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
 290        if (machine->gpio_hp_det == -EPROBE_DEFER)
 291                return -EPROBE_DEFER;
 292
 293        machine->gpio_int_mic_en = of_get_named_gpio(np,
 294                                                "nvidia,int-mic-en-gpios", 0);
 295        if (machine->gpio_int_mic_en == -EPROBE_DEFER)
 296                return -EPROBE_DEFER;
 297        if (gpio_is_valid(machine->gpio_int_mic_en)) {
 298                /* Disable int mic; enable signal is active-high */
 299                ret = devm_gpio_request_one(&pdev->dev,
 300                                            machine->gpio_int_mic_en,
 301                                            GPIOF_OUT_INIT_LOW, "int_mic_en");
 302                if (ret) {
 303                        dev_err(card->dev, "cannot get int_mic_en gpio\n");
 304                        return ret;
 305                }
 306        }
 307
 308        machine->gpio_ext_mic_en = of_get_named_gpio(np,
 309                                                "nvidia,ext-mic-en-gpios", 0);
 310        if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
 311                return -EPROBE_DEFER;
 312        if (gpio_is_valid(machine->gpio_ext_mic_en)) {
 313                /* Enable ext mic; enable signal is active-low */
 314                ret = devm_gpio_request_one(&pdev->dev,
 315                                            machine->gpio_ext_mic_en,
 316                                            GPIOF_OUT_INIT_LOW, "ext_mic_en");
 317                if (ret) {
 318                        dev_err(card->dev, "cannot get ext_mic_en gpio\n");
 319                        return ret;
 320                }
 321        }
 322
 323        ret = snd_soc_of_parse_card_name(card, "nvidia,model");
 324        if (ret)
 325                goto err;
 326
 327        ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
 328        if (ret)
 329                goto err;
 330
 331        tegra_wm8903_dai.codec_of_node = of_parse_phandle(np,
 332                                                "nvidia,audio-codec", 0);
 333        if (!tegra_wm8903_dai.codec_of_node) {
 334                dev_err(&pdev->dev,
 335                        "Property 'nvidia,audio-codec' missing or invalid\n");
 336                ret = -EINVAL;
 337                goto err;
 338        }
 339
 340        tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np,
 341                        "nvidia,i2s-controller", 0);
 342        if (!tegra_wm8903_dai.cpu_of_node) {
 343                dev_err(&pdev->dev,
 344                        "Property 'nvidia,i2s-controller' missing or invalid\n");
 345                ret = -EINVAL;
 346                goto err;
 347        }
 348
 349        tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node;
 350
 351        ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
 352        if (ret)
 353                goto err;
 354
 355        ret = snd_soc_register_card(card);
 356        if (ret) {
 357                dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
 358                        ret);
 359                goto err_fini_utils;
 360        }
 361
 362        return 0;
 363
 364err_fini_utils:
 365        tegra_asoc_utils_fini(&machine->util_data);
 366err:
 367        return ret;
 368}
 369
 370static int tegra_wm8903_driver_remove(struct platform_device *pdev)
 371{
 372        struct snd_soc_card *card = platform_get_drvdata(pdev);
 373        struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
 374
 375        snd_soc_unregister_card(card);
 376
 377        tegra_asoc_utils_fini(&machine->util_data);
 378
 379        return 0;
 380}
 381
 382static const struct of_device_id tegra_wm8903_of_match[] = {
 383        { .compatible = "nvidia,tegra-audio-wm8903", },
 384        {},
 385};
 386
 387static struct platform_driver tegra_wm8903_driver = {
 388        .driver = {
 389                .name = DRV_NAME,
 390                .owner = THIS_MODULE,
 391                .pm = &snd_soc_pm_ops,
 392                .of_match_table = tegra_wm8903_of_match,
 393        },
 394        .probe = tegra_wm8903_driver_probe,
 395        .remove = tegra_wm8903_driver_remove,
 396};
 397module_platform_driver(tegra_wm8903_driver);
 398
 399MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
 400MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
 401MODULE_LICENSE("GPL");
 402MODULE_ALIAS("platform:" DRV_NAME);
 403MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);
 404