xref: /openbmc/linux/sound/soc/codecs/max98357a.c (revision 78bb17f7)
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 };
27 
28 static int max98357a_sdmode_event(struct snd_soc_dapm_widget *w,
29 		struct snd_kcontrol *kcontrol, int event)
30 {
31 	struct snd_soc_component *component =
32 		snd_soc_dapm_to_component(w->dapm);
33 	struct max98357a_priv *max98357a =
34 		snd_soc_component_get_drvdata(component);
35 
36 	if (!max98357a->sdmode)
37 		return 0;
38 
39 	if (event & SND_SOC_DAPM_POST_PMU) {
40 		msleep(max98357a->sdmode_delay);
41 		gpiod_set_value(max98357a->sdmode, 1);
42 		dev_dbg(component->dev, "set sdmode to 1");
43 	} else if (event & SND_SOC_DAPM_PRE_PMD) {
44 		gpiod_set_value(max98357a->sdmode, 0);
45 		dev_dbg(component->dev, "set sdmode to 0");
46 	}
47 
48 	return 0;
49 }
50 
51 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
52 	SND_SOC_DAPM_OUTPUT("Speaker"),
53 	SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0,
54 			max98357a_sdmode_event,
55 			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
56 };
57 
58 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
59 	{"SD_MODE", NULL, "HiFi Playback"},
60 	{"Speaker", NULL, "SD_MODE"},
61 };
62 
63 static const struct snd_soc_component_driver max98357a_component_driver = {
64 	.dapm_widgets		= max98357a_dapm_widgets,
65 	.num_dapm_widgets	= ARRAY_SIZE(max98357a_dapm_widgets),
66 	.dapm_routes		= max98357a_dapm_routes,
67 	.num_dapm_routes	= ARRAY_SIZE(max98357a_dapm_routes),
68 	.idle_bias_on		= 1,
69 	.use_pmdown_time	= 1,
70 	.endianness		= 1,
71 	.non_legacy_dai_naming	= 1,
72 };
73 
74 static struct snd_soc_dai_driver max98357a_dai_driver = {
75 	.name = "HiFi",
76 	.playback = {
77 		.stream_name	= "HiFi Playback",
78 		.formats	= SNDRV_PCM_FMTBIT_S16 |
79 					SNDRV_PCM_FMTBIT_S24 |
80 					SNDRV_PCM_FMTBIT_S32,
81 		.rates		= SNDRV_PCM_RATE_8000 |
82 					SNDRV_PCM_RATE_16000 |
83 					SNDRV_PCM_RATE_32000 |
84 					SNDRV_PCM_RATE_44100 |
85 					SNDRV_PCM_RATE_48000 |
86 					SNDRV_PCM_RATE_88200 |
87 					SNDRV_PCM_RATE_96000,
88 		.rate_min	= 8000,
89 		.rate_max	= 96000,
90 		.channels_min	= 1,
91 		.channels_max	= 2,
92 	},
93 };
94 
95 static int max98357a_platform_probe(struct platform_device *pdev)
96 {
97 	struct max98357a_priv *max98357a;
98 	int ret;
99 
100 	max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL);
101 	if (!max98357a)
102 		return -ENOMEM;
103 
104 	max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev,
105 				"sdmode", GPIOD_OUT_LOW);
106 	if (IS_ERR(max98357a->sdmode))
107 		return PTR_ERR(max98357a->sdmode);
108 
109 	ret = device_property_read_u32(&pdev->dev, "sdmode-delay",
110 					&max98357a->sdmode_delay);
111 	if (ret) {
112 		max98357a->sdmode_delay = 0;
113 		dev_dbg(&pdev->dev,
114 			"no optional property 'sdmode-delay' found, "
115 			"default: no delay\n");
116 	}
117 
118 	dev_set_drvdata(&pdev->dev, max98357a);
119 
120 	return devm_snd_soc_register_component(&pdev->dev,
121 			&max98357a_component_driver,
122 			&max98357a_dai_driver, 1);
123 }
124 
125 #ifdef CONFIG_OF
126 static const struct of_device_id max98357a_device_id[] = {
127 	{ .compatible = "maxim,max98357a" },
128 	{}
129 };
130 MODULE_DEVICE_TABLE(of, max98357a_device_id);
131 #endif
132 
133 #ifdef CONFIG_ACPI
134 static const struct acpi_device_id max98357a_acpi_match[] = {
135 	{ "MX98357A", 0 },
136 	{ "MX98360A", 0 },
137 	{},
138 };
139 MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
140 #endif
141 
142 static struct platform_driver max98357a_platform_driver = {
143 	.driver = {
144 		.name = "max98357a",
145 		.of_match_table = of_match_ptr(max98357a_device_id),
146 		.acpi_match_table = ACPI_PTR(max98357a_acpi_match),
147 	},
148 	.probe	= max98357a_platform_probe,
149 };
150 module_platform_driver(max98357a_platform_driver);
151 
152 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
153 MODULE_LICENSE("GPL v2");
154