blob: 21d6e03df3d771e46b601a52744f249afc72c354 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -08002/* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
3 *
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -08004 * max98357a.c -- MAX98357A ALSA SoC Codec driver
5 */
6
Rohit Ainapure5c270872015-12-11 11:29:06 -08007#include <linux/acpi.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -08008#include <linux/device.h>
9#include <linux/err.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080010#include <linux/gpio.h>
Vincent Stehlé5c8be982015-02-11 23:08:59 +010011#include <linux/gpio/consumer.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080012#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>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080018#include <sound/soc.h>
Kenneth Westfield08d0a552015-02-17 00:53:11 -080019#include <sound/soc-dai.h>
20#include <sound/soc-dapm.h>
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080021
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080022static 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
Anatol Pomozov51192222015-07-12 08:14:19 -070027 if (!sdmode)
28 return 0;
29
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080030 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[] = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080047 SND_SOC_DAPM_OUTPUT("Speaker"),
48};
49
50static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
Anatol Pomozov508e6192015-07-12 08:14:20 -070051 {"Speaker", NULL, "HiFi Playback"},
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080052};
53
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000054static int max98357a_component_probe(struct snd_soc_component *component)
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080055{
56 struct gpio_desc *sdmode;
57
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000058 sdmode = devm_gpiod_get_optional(component->dev, "sdmode", GPIOD_OUT_LOW);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070059 if (IS_ERR(sdmode))
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080060 return PTR_ERR(sdmode);
Anatol Pomozov4ab0c5912015-07-12 08:14:21 -070061
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000062 snd_soc_component_set_drvdata(component, sdmode);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080063
64 return 0;
65}
66
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +000067static 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,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080077};
78
Axel Lin64793042015-07-15 15:38:14 +080079static const struct snd_soc_dai_ops max98357a_dai_ops = {
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080080 .trigger = max98357a_daiops_trigger,
81};
82
83static struct snd_soc_dai_driver max98357a_dai_driver = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080084 .name = "HiFi",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080085 .playback = {
Kenneth Westfield92b2ad22015-02-24 22:39:04 -080086 .stream_name = "HiFi Playback",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080087 .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 |
Jerome Brunetfdf34362019-04-04 13:50:15 +020092 SNDRV_PCM_RATE_32000 |
93 SNDRV_PCM_RATE_44100 |
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080094 SNDRV_PCM_RATE_48000 |
Jerome Brunetfdf34362019-04-04 13:50:15 +020095 SNDRV_PCM_RATE_88200 |
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -080096 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{
Kuninori Morimoto4b51e9f2018-01-29 04:09:20 +0000107 return devm_snd_soc_register_component(&pdev->dev,
108 &max98357a_component_driver,
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800109 &max98357a_dai_driver, 1);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800110}
111
112static int max98357a_platform_remove(struct platform_device *pdev)
113{
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800114 return 0;
115}
116
117#ifdef CONFIG_OF
118static const struct of_device_id max98357a_device_id[] = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800119 { .compatible = "maxim,max98357a" },
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800120 {}
121};
Mark Brown3efa1302015-02-09 14:36:47 +0800122MODULE_DEVICE_TABLE(of, max98357a_device_id);
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800123#endif
124
Rohit Ainapure5c270872015-12-11 11:29:06 -0800125#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
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800133static struct platform_driver max98357a_platform_driver = {
134 .driver = {
Kenneth Westfield7ff5eab2015-02-17 00:53:12 -0800135 .name = "max98357a",
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800136 .of_match_table = of_match_ptr(max98357a_device_id),
Rohit Ainapure5c270872015-12-11 11:29:06 -0800137 .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
Kenneth Westfieldaf5adf12015-02-05 12:53:40 -0800138 },
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");