linux/sound/soc/intel/boards/cht_bsw_max98090_ti.c
<<
>>
Prefs
   1/*
   2 *  cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based
   3 *  platforms Cherrytrail and Braswell, with max98090 & TI codec.
   4 *
   5 *  Copyright (C) 2015 Intel Corp
   6 *  Author: Fang, Yang A <yang.a.fang@intel.com>
   7 *  This file is modified from cht_bsw_rt5645.c
   8 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   9 *
  10 *  This program is free software; you can redistribute it and/or modify
  11 *  it under the terms of the GNU General Public License as published by
  12 *  the Free Software Foundation; version 2 of the License.
  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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20 */
  21
  22#include <linux/module.h>
  23#include <linux/platform_device.h>
  24#include <linux/slab.h>
  25#include <linux/acpi.h>
  26#include <linux/clk.h>
  27#include <sound/pcm.h>
  28#include <sound/pcm_params.h>
  29#include <sound/soc.h>
  30#include <sound/jack.h>
  31#include "../../codecs/max98090.h"
  32#include "../atom/sst-atom-controls.h"
  33#include "../../codecs/ts3a227e.h"
  34
  35#define CHT_PLAT_CLK_3_HZ       19200000
  36#define CHT_CODEC_DAI   "HiFi"
  37
  38struct cht_mc_private {
  39        struct clk *mclk;
  40        struct snd_soc_jack jack;
  41        bool ts3a227e_present;
  42};
  43
  44static int platform_clock_control(struct snd_soc_dapm_widget *w,
  45                                          struct snd_kcontrol *k, int  event)
  46{
  47        struct snd_soc_dapm_context *dapm = w->dapm;
  48        struct snd_soc_card *card = dapm->card;
  49        struct snd_soc_dai *codec_dai;
  50        struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  51        int ret;
  52
  53        codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
  54        if (!codec_dai) {
  55                dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  56                return -EIO;
  57        }
  58
  59        if (SND_SOC_DAPM_EVENT_ON(event)) {
  60                ret = clk_prepare_enable(ctx->mclk);
  61                if (ret < 0) {
  62                        dev_err(card->dev,
  63                                "could not configure MCLK state");
  64                        return ret;
  65                }
  66        } else {
  67                clk_disable_unprepare(ctx->mclk);
  68        }
  69
  70        return 0;
  71}
  72
  73static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  74        SND_SOC_DAPM_HP("Headphone", NULL),
  75        SND_SOC_DAPM_MIC("Headset Mic", NULL),
  76        SND_SOC_DAPM_MIC("Int Mic", NULL),
  77        SND_SOC_DAPM_SPK("Ext Spk", NULL),
  78        SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  79                            platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  80                            SND_SOC_DAPM_POST_PMD),
  81};
  82
  83static const struct snd_soc_dapm_route cht_audio_map[] = {
  84        {"IN34", NULL, "Headset Mic"},
  85        {"Headset Mic", NULL, "MICBIAS"},
  86        {"DMICL", NULL, "Int Mic"},
  87        {"Headphone", NULL, "HPL"},
  88        {"Headphone", NULL, "HPR"},
  89        {"Ext Spk", NULL, "SPKL"},
  90        {"Ext Spk", NULL, "SPKR"},
  91        {"HiFi Playback", NULL, "ssp2 Tx"},
  92        {"ssp2 Tx", NULL, "codec_out0"},
  93        {"ssp2 Tx", NULL, "codec_out1"},
  94        {"codec_in0", NULL, "ssp2 Rx" },
  95        {"codec_in1", NULL, "ssp2 Rx" },
  96        {"ssp2 Rx", NULL, "HiFi Capture"},
  97        {"Headphone", NULL, "Platform Clock"},
  98        {"Headset Mic", NULL, "Platform Clock"},
  99        {"Int Mic", NULL, "Platform Clock"},
 100        {"Ext Spk", NULL, "Platform Clock"},
 101};
 102
 103static const struct snd_kcontrol_new cht_mc_controls[] = {
 104        SOC_DAPM_PIN_SWITCH("Headphone"),
 105        SOC_DAPM_PIN_SWITCH("Headset Mic"),
 106        SOC_DAPM_PIN_SWITCH("Int Mic"),
 107        SOC_DAPM_PIN_SWITCH("Ext Spk"),
 108};
 109
 110static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
 111                             struct snd_pcm_hw_params *params)
 112{
 113        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 114        struct snd_soc_dai *codec_dai = rtd->codec_dai;
 115        int ret;
 116
 117        ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK,
 118                                     CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN);
 119        if (ret < 0) {
 120                dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
 121                return ret;
 122        }
 123
 124        return 0;
 125}
 126
 127static int cht_ti_jack_event(struct notifier_block *nb,
 128                unsigned long event, void *data)
 129{
 130        struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
 131        struct snd_soc_dapm_context *dapm = &jack->card->dapm;
 132
 133        if (event & SND_JACK_MICROPHONE) {
 134                snd_soc_dapm_force_enable_pin(dapm, "SHDN");
 135                snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
 136                snd_soc_dapm_sync(dapm);
 137        } else {
 138                snd_soc_dapm_disable_pin(dapm, "MICBIAS");
 139                snd_soc_dapm_disable_pin(dapm, "SHDN");
 140                snd_soc_dapm_sync(dapm);
 141        }
 142
 143        return 0;
 144}
 145
 146static struct notifier_block cht_jack_nb = {
 147        .notifier_call = cht_ti_jack_event,
 148};
 149
 150static struct snd_soc_jack_pin hs_jack_pins[] = {
 151        {
 152                .pin    = "Headphone",
 153                .mask   = SND_JACK_HEADPHONE,
 154        },
 155        {
 156                .pin    = "Headset Mic",
 157                .mask   = SND_JACK_MICROPHONE,
 158        },
 159};
 160
 161static struct snd_soc_jack_gpio hs_jack_gpios[] = {
 162        {
 163                .name           = "hp",
 164                .report         = SND_JACK_HEADPHONE | SND_JACK_LINEOUT,
 165                .debounce_time  = 200,
 166        },
 167        {
 168                .name           = "mic",
 169                .invert         = 1,
 170                .report         = SND_JACK_MICROPHONE,
 171                .debounce_time  = 200,
 172        },
 173};
 174
 175static const struct acpi_gpio_params hp_gpios = { 0, 0, false };
 176static const struct acpi_gpio_params mic_gpios = { 1, 0, false };
 177
 178static const struct acpi_gpio_mapping acpi_max98090_gpios[] = {
 179        { "hp-gpios", &hp_gpios, 1 },
 180        { "mic-gpios", &mic_gpios, 1 },
 181        {},
 182};
 183
 184static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
 185{
 186        int ret;
 187        int jack_type;
 188        struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
 189        struct snd_soc_jack *jack = &ctx->jack;
 190
 191        if (ctx->ts3a227e_present) {
 192                /*
 193                 * The jack has already been created in the
 194                 * cht_max98090_headset_init() function.
 195                 */
 196                snd_soc_jack_notifier_register(jack, &cht_jack_nb);
 197                return 0;
 198        }
 199
 200        jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
 201
 202        ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
 203                                    jack_type, jack,
 204                                    hs_jack_pins, ARRAY_SIZE(hs_jack_pins));
 205        if (ret) {
 206                dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret);
 207                return ret;
 208        }
 209
 210        ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack,
 211                                      ARRAY_SIZE(hs_jack_gpios),
 212                                      hs_jack_gpios);
 213        if (ret) {
 214                /*
 215                 * flag error but don't bail if jack detect is broken
 216                 * due to platform issues or bad BIOS/configuration
 217                 */
 218                dev_err(runtime->dev,
 219                        "jack detection gpios not added, error %d\n", ret);
 220        }
 221
 222        /*
 223         * The firmware might enable the clock at
 224         * boot (this information may or may not
 225         * be reflected in the enable clock register).
 226         * To change the rate we must disable the clock
 227         * first to cover these cases. Due to common
 228         * clock framework restrictions that do not allow
 229         * to disable a clock that has not been enabled,
 230         * we need to enable the clock first.
 231         */
 232        ret = clk_prepare_enable(ctx->mclk);
 233        if (!ret)
 234                clk_disable_unprepare(ctx->mclk);
 235
 236        ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
 237
 238        if (ret)
 239                dev_err(runtime->dev, "unable to set MCLK rate\n");
 240
 241        return ret;
 242}
 243
 244static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
 245                            struct snd_pcm_hw_params *params)
 246{
 247        struct snd_interval *rate = hw_param_interval(params,
 248                        SNDRV_PCM_HW_PARAM_RATE);
 249        struct snd_interval *channels = hw_param_interval(params,
 250                                                SNDRV_PCM_HW_PARAM_CHANNELS);
 251        int ret = 0;
 252        unsigned int fmt = 0;
 253
 254        ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
 255        if (ret < 0) {
 256                dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret);
 257                return ret;
 258        }
 259
 260        fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
 261                                | SND_SOC_DAIFMT_CBS_CFS;
 262
 263        ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt);
 264        if (ret < 0) {
 265                dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret);
 266                return ret;
 267        }
 268
 269        /* The DSP will covert the FE rate to 48k, stereo, 24bits */
 270        rate->min = rate->max = 48000;
 271        channels->min = channels->max = 2;
 272
 273        /* set SSP2 to 16-bit */
 274        params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
 275        return 0;
 276}
 277
 278static int cht_aif1_startup(struct snd_pcm_substream *substream)
 279{
 280        return snd_pcm_hw_constraint_single(substream->runtime,
 281                        SNDRV_PCM_HW_PARAM_RATE, 48000);
 282}
 283
 284static int cht_max98090_headset_init(struct snd_soc_component *component)
 285{
 286        struct snd_soc_card *card = component->card;
 287        struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
 288        struct snd_soc_jack *jack = &ctx->jack;
 289        int jack_type;
 290        int ret;
 291
 292        /*
 293         * TI supports 4 butons headset detection
 294         * KEY_MEDIA
 295         * KEY_VOICECOMMAND
 296         * KEY_VOLUMEUP
 297         * KEY_VOLUMEDOWN
 298         */
 299        jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
 300                    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
 301                    SND_JACK_BTN_2 | SND_JACK_BTN_3;
 302
 303        ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type,
 304                                    jack, NULL, 0);
 305        if (ret) {
 306                dev_err(card->dev, "Headset Jack creation failed %d\n", ret);
 307                return ret;
 308        }
 309
 310        return ts3a227e_enable_jack_detect(component, jack);
 311}
 312
 313static const struct snd_soc_ops cht_aif1_ops = {
 314        .startup = cht_aif1_startup,
 315};
 316
 317static const struct snd_soc_ops cht_be_ssp2_ops = {
 318        .hw_params = cht_aif1_hw_params,
 319};
 320
 321static struct snd_soc_aux_dev cht_max98090_headset_dev = {
 322        .name = "Headset Chip",
 323        .init = cht_max98090_headset_init,
 324        .codec_name = "i2c-104C227E:00",
 325};
 326
 327static struct snd_soc_dai_link cht_dailink[] = {
 328        [MERR_DPCM_AUDIO] = {
 329                .name = "Audio Port",
 330                .stream_name = "Audio",
 331                .cpu_dai_name = "media-cpu-dai",
 332                .codec_dai_name = "snd-soc-dummy-dai",
 333                .codec_name = "snd-soc-dummy",
 334                .platform_name = "sst-mfld-platform",
 335                .nonatomic = true,
 336                .dynamic = 1,
 337                .dpcm_playback = 1,
 338                .dpcm_capture = 1,
 339                .ops = &cht_aif1_ops,
 340        },
 341        [MERR_DPCM_DEEP_BUFFER] = {
 342                .name = "Deep-Buffer Audio Port",
 343                .stream_name = "Deep-Buffer Audio",
 344                .cpu_dai_name = "deepbuffer-cpu-dai",
 345                .codec_dai_name = "snd-soc-dummy-dai",
 346                .codec_name = "snd-soc-dummy",
 347                .platform_name = "sst-mfld-platform",
 348                .nonatomic = true,
 349                .dynamic = 1,
 350                .dpcm_playback = 1,
 351                .ops = &cht_aif1_ops,
 352        },
 353        /* back ends */
 354        {
 355                .name = "SSP2-Codec",
 356                .id = 0,
 357                .cpu_dai_name = "ssp2-port",
 358                .platform_name = "sst-mfld-platform",
 359                .no_pcm = 1,
 360                .codec_dai_name = "HiFi",
 361                .codec_name = "i2c-193C9890:00",
 362                .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
 363                                        | SND_SOC_DAIFMT_CBS_CFS,
 364                .init = cht_codec_init,
 365                .be_hw_params_fixup = cht_codec_fixup,
 366                .dpcm_playback = 1,
 367                .dpcm_capture = 1,
 368                .ops = &cht_be_ssp2_ops,
 369        },
 370};
 371
 372/* SoC card */
 373static struct snd_soc_card snd_soc_card_cht = {
 374        .name = "chtmax98090",
 375        .owner = THIS_MODULE,
 376        .dai_link = cht_dailink,
 377        .num_links = ARRAY_SIZE(cht_dailink),
 378        .aux_dev = &cht_max98090_headset_dev,
 379        .num_aux_devs = 1,
 380        .dapm_widgets = cht_dapm_widgets,
 381        .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
 382        .dapm_routes = cht_audio_map,
 383        .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
 384        .controls = cht_mc_controls,
 385        .num_controls = ARRAY_SIZE(cht_mc_controls),
 386};
 387
 388static int snd_cht_mc_probe(struct platform_device *pdev)
 389{
 390        struct device *dev = &pdev->dev;
 391        int ret_val = 0;
 392        struct cht_mc_private *drv;
 393
 394        drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
 395        if (!drv)
 396                return -ENOMEM;
 397
 398        drv->ts3a227e_present = acpi_dev_found("104C227E");
 399        if (!drv->ts3a227e_present) {
 400                /* no need probe TI jack detection chip */
 401                snd_soc_card_cht.aux_dev = NULL;
 402                snd_soc_card_cht.num_aux_devs = 0;
 403
 404                ret_val = devm_acpi_dev_add_driver_gpios(dev->parent,
 405                                                         acpi_max98090_gpios);
 406                if (ret_val)
 407                        dev_dbg(dev, "Unable to add GPIO mapping table\n");
 408        }
 409
 410        /* register the soc card */
 411        snd_soc_card_cht.dev = &pdev->dev;
 412        snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
 413
 414        drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
 415        if (IS_ERR(drv->mclk)) {
 416                dev_err(&pdev->dev,
 417                        "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
 418                        PTR_ERR(drv->mclk));
 419                return PTR_ERR(drv->mclk);
 420        }
 421
 422        ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
 423        if (ret_val) {
 424                dev_err(&pdev->dev,
 425                        "snd_soc_register_card failed %d\n", ret_val);
 426                return ret_val;
 427        }
 428        platform_set_drvdata(pdev, &snd_soc_card_cht);
 429        return ret_val;
 430}
 431
 432static struct platform_driver snd_cht_mc_driver = {
 433        .driver = {
 434                .name = "cht-bsw-max98090",
 435        },
 436        .probe = snd_cht_mc_probe,
 437};
 438
 439module_platform_driver(snd_cht_mc_driver)
 440
 441MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
 442MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>");
 443MODULE_LICENSE("GPL v2");
 444MODULE_ALIAS("platform:cht-bsw-max98090");
 445