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