xref: /linux/sound/soc/codecs/max98357a.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
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/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <sound/pcm.h>
17 #include <sound/soc.h>
18 #include <sound/soc-dai.h>
19 #include <sound/soc-dapm.h>
20 
21 struct max98357a_priv {
22 	struct gpio_desc *sdmode;
23 	unsigned int sdmode_delay;
24 	int sdmode_switch;
25 };
26 
max98357a_daiops_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)27 static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
28 		int cmd, struct snd_soc_dai *dai)
29 {
30 	struct snd_soc_component *component = dai->component;
31 	struct max98357a_priv *max98357a =
32 		snd_soc_component_get_drvdata(component);
33 
34 	if (!max98357a->sdmode)
35 		return 0;
36 
37 	switch (cmd) {
38 	case SNDRV_PCM_TRIGGER_START:
39 	case SNDRV_PCM_TRIGGER_RESUME:
40 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
41 		mdelay(max98357a->sdmode_delay);
42 		if (max98357a->sdmode_switch) {
43 			gpiod_set_value(max98357a->sdmode, 1);
44 			dev_dbg(component->dev, "set sdmode to 1");
45 		}
46 		break;
47 	case SNDRV_PCM_TRIGGER_STOP:
48 	case SNDRV_PCM_TRIGGER_SUSPEND:
49 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
50 		gpiod_set_value(max98357a->sdmode, 0);
51 		dev_dbg(component->dev, "set sdmode to 0");
52 		break;
53 	}
54 
55 	return 0;
56 }
57 
max98357a_sdmode_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)58 static int max98357a_sdmode_event(struct snd_soc_dapm_widget *w,
59 		struct snd_kcontrol *kcontrol, int event)
60 {
61 	struct snd_soc_component *component =
62 		snd_soc_dapm_to_component(w->dapm);
63 	struct max98357a_priv *max98357a =
64 		snd_soc_component_get_drvdata(component);
65 
66 	if (event & SND_SOC_DAPM_POST_PMU)
67 		max98357a->sdmode_switch = 1;
68 	else if (event & SND_SOC_DAPM_POST_PMD)
69 		max98357a->sdmode_switch = 0;
70 
71 	return 0;
72 }
73 
74 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
75 	SND_SOC_DAPM_OUTPUT("Speaker"),
76 	SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0,
77 			max98357a_sdmode_event,
78 			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
79 };
80 
81 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
82 	{"SD_MODE", NULL, "HiFi Playback"},
83 	{"Speaker", NULL, "SD_MODE"},
84 };
85 
86 static const struct snd_soc_component_driver max98357a_component_driver = {
87 	.dapm_widgets		= max98357a_dapm_widgets,
88 	.num_dapm_widgets	= ARRAY_SIZE(max98357a_dapm_widgets),
89 	.dapm_routes		= max98357a_dapm_routes,
90 	.num_dapm_routes	= ARRAY_SIZE(max98357a_dapm_routes),
91 	.idle_bias_on		= 1,
92 	.use_pmdown_time	= 1,
93 	.endianness		= 1,
94 };
95 
96 static const struct snd_soc_dai_ops max98357a_dai_ops = {
97 	.trigger        = max98357a_daiops_trigger,
98 };
99 
100 static struct snd_soc_dai_driver max98357a_dai_driver = {
101 	.name = "HiFi",
102 	.playback = {
103 		.stream_name	= "HiFi Playback",
104 		.formats	= SNDRV_PCM_FMTBIT_S16 |
105 					SNDRV_PCM_FMTBIT_S24 |
106 					SNDRV_PCM_FMTBIT_S32,
107 		.rates		= SNDRV_PCM_RATE_8000 |
108 					SNDRV_PCM_RATE_16000 |
109 					SNDRV_PCM_RATE_32000 |
110 					SNDRV_PCM_RATE_44100 |
111 					SNDRV_PCM_RATE_48000 |
112 					SNDRV_PCM_RATE_88200 |
113 					SNDRV_PCM_RATE_96000,
114 		.rate_min	= 8000,
115 		.rate_max	= 96000,
116 		.channels_min	= 1,
117 		.channels_max	= 2,
118 	},
119 	.ops    = &max98357a_dai_ops,
120 };
121 
max98357a_platform_probe(struct platform_device * pdev)122 static int max98357a_platform_probe(struct platform_device *pdev)
123 {
124 	struct max98357a_priv *max98357a;
125 	int ret;
126 
127 	max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL);
128 	if (!max98357a)
129 		return -ENOMEM;
130 
131 	max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev,
132 				"sdmode", GPIOD_OUT_LOW);
133 	if (IS_ERR(max98357a->sdmode))
134 		return PTR_ERR(max98357a->sdmode);
135 
136 	ret = device_property_read_u32(&pdev->dev, "sdmode-delay",
137 					&max98357a->sdmode_delay);
138 	if (ret) {
139 		max98357a->sdmode_delay = 0;
140 		dev_dbg(&pdev->dev,
141 			"no optional property 'sdmode-delay' found, "
142 			"default: no delay\n");
143 	}
144 
145 	dev_set_drvdata(&pdev->dev, max98357a);
146 
147 	return devm_snd_soc_register_component(&pdev->dev,
148 			&max98357a_component_driver,
149 			&max98357a_dai_driver, 1);
150 }
151 
152 #ifdef CONFIG_OF
153 static const struct of_device_id max98357a_device_id[] = {
154 	{ .compatible = "maxim,max98357a" },
155 	{ .compatible = "maxim,max98360a" },
156 	{}
157 };
158 MODULE_DEVICE_TABLE(of, max98357a_device_id);
159 #endif
160 
161 #ifdef CONFIG_ACPI
162 static const struct acpi_device_id max98357a_acpi_match[] = {
163 	{ "MX98357A", 0 },
164 	{ "MX98360A", 0 },
165 	{},
166 };
167 MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
168 #endif
169 
170 static struct platform_driver max98357a_platform_driver = {
171 	.driver = {
172 		.name = "max98357a",
173 		.of_match_table = of_match_ptr(max98357a_device_id),
174 		.acpi_match_table = ACPI_PTR(max98357a_acpi_match),
175 	},
176 	.probe	= max98357a_platform_probe,
177 };
178 module_platform_driver(max98357a_platform_driver);
179 
180 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
181 MODULE_LICENSE("GPL v2");
182