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