linux/sound/soc/codecs/max98357a.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
   3 *
   4 * max98357a.c -- MAX98357A ALSA SoC Codec driver
   5 */
   6
   7#include <linux/acpi.h>
   8#include <linux/device.h>
   9#include <linux/err.h>
  10#include <linux/gpio.h>
  11#include <linux/gpio/consumer.h>
  12#include <linux/kernel.h>
  13#include <linux/mod_devicetable.h>
  14#include <linux/module.h>
  15#include <linux/of.h>
  16#include <linux/platform_device.h>
  17#include <sound/pcm.h>
  18#include <sound/soc.h>
  19#include <sound/soc-dai.h>
  20#include <sound/soc-dapm.h>
  21
  22static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
  23                int cmd, struct snd_soc_dai *dai)
  24{
  25        struct gpio_desc *sdmode = snd_soc_dai_get_drvdata(dai);
  26
  27        if (!sdmode)
  28                return 0;
  29
  30        switch (cmd) {
  31        case SNDRV_PCM_TRIGGER_START:
  32        case SNDRV_PCM_TRIGGER_RESUME:
  33        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  34                gpiod_set_value(sdmode, 1);
  35                break;
  36        case SNDRV_PCM_TRIGGER_STOP:
  37        case SNDRV_PCM_TRIGGER_SUSPEND:
  38        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  39                gpiod_set_value(sdmode, 0);
  40                break;
  41        }
  42
  43        return 0;
  44}
  45
  46static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
  47        SND_SOC_DAPM_OUTPUT("Speaker"),
  48};
  49
  50static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
  51        {"Speaker", NULL, "HiFi Playback"},
  52};
  53
  54static int max98357a_component_probe(struct snd_soc_component *component)
  55{
  56        struct gpio_desc *sdmode;
  57
  58        sdmode = devm_gpiod_get_optional(component->dev, "sdmode", GPIOD_OUT_LOW);
  59        if (IS_ERR(sdmode))
  60                return PTR_ERR(sdmode);
  61
  62        snd_soc_component_set_drvdata(component, sdmode);
  63
  64        return 0;
  65}
  66
  67static const struct snd_soc_component_driver max98357a_component_driver = {
  68        .probe                  = max98357a_component_probe,
  69        .dapm_widgets           = max98357a_dapm_widgets,
  70        .num_dapm_widgets       = ARRAY_SIZE(max98357a_dapm_widgets),
  71        .dapm_routes            = max98357a_dapm_routes,
  72        .num_dapm_routes        = ARRAY_SIZE(max98357a_dapm_routes),
  73        .idle_bias_on           = 1,
  74        .use_pmdown_time        = 1,
  75        .endianness             = 1,
  76        .non_legacy_dai_naming  = 1,
  77};
  78
  79static const struct snd_soc_dai_ops max98357a_dai_ops = {
  80        .trigger        = max98357a_daiops_trigger,
  81};
  82
  83static struct snd_soc_dai_driver max98357a_dai_driver = {
  84        .name = "HiFi",
  85        .playback = {
  86                .stream_name    = "HiFi Playback",
  87                .formats        = SNDRV_PCM_FMTBIT_S16 |
  88                                        SNDRV_PCM_FMTBIT_S24 |
  89                                        SNDRV_PCM_FMTBIT_S32,
  90                .rates          = SNDRV_PCM_RATE_8000 |
  91                                        SNDRV_PCM_RATE_16000 |
  92                                        SNDRV_PCM_RATE_32000 |
  93                                        SNDRV_PCM_RATE_44100 |
  94                                        SNDRV_PCM_RATE_48000 |
  95                                        SNDRV_PCM_RATE_88200 |
  96                                        SNDRV_PCM_RATE_96000,
  97                .rate_min       = 8000,
  98                .rate_max       = 96000,
  99                .channels_min   = 1,
 100                .channels_max   = 2,
 101        },
 102        .ops    = &max98357a_dai_ops,
 103};
 104
 105static int max98357a_platform_probe(struct platform_device *pdev)
 106{
 107        return devm_snd_soc_register_component(&pdev->dev,
 108                        &max98357a_component_driver,
 109                        &max98357a_dai_driver, 1);
 110}
 111
 112static int max98357a_platform_remove(struct platform_device *pdev)
 113{
 114        return 0;
 115}
 116
 117#ifdef CONFIG_OF
 118static const struct of_device_id max98357a_device_id[] = {
 119        { .compatible = "maxim,max98357a" },
 120        {}
 121};
 122MODULE_DEVICE_TABLE(of, max98357a_device_id);
 123#endif
 124
 125#ifdef CONFIG_ACPI
 126static const struct acpi_device_id max98357a_acpi_match[] = {
 127        { "MX98357A", 0 },
 128        {},
 129};
 130MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
 131#endif
 132
 133static struct platform_driver max98357a_platform_driver = {
 134        .driver = {
 135                .name = "max98357a",
 136                .of_match_table = of_match_ptr(max98357a_device_id),
 137                .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
 138        },
 139        .probe  = max98357a_platform_probe,
 140        .remove = max98357a_platform_remove,
 141};
 142module_platform_driver(max98357a_platform_driver);
 143
 144MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
 145MODULE_LICENSE("GPL v2");
 146