xref: /linux/sound/soc/codecs/nau8315.c (revision f9e437cddf6cf9e603bdaefe148c1f4792aaf39c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // nau8315.c  --  NAU8315 ALSA SoC Audio Amplifier Driver
4 //
5 // Copyright 2020 Nuvoton Technology Crop.
6 //
7 // Author: David Lin <ctlin0@nuvoton.com>
8 //
9 // Based on MAX98357A.c
10 
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dai.h>
22 #include <sound/soc-dapm.h>
23 
24 struct nau8315_priv {
25 	struct gpio_desc *enable;
26 	int enpin_switch;
27 };
28 
29 static int nau8315_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 nau8315_priv *nau8315 =
34 		snd_soc_component_get_drvdata(component);
35 
36 	if (!nau8315->enable)
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 		if (nau8315->enpin_switch) {
44 			gpiod_set_value(nau8315->enable, 1);
45 			dev_dbg(component->dev, "set enable to 1");
46 		}
47 		break;
48 	case SNDRV_PCM_TRIGGER_STOP:
49 	case SNDRV_PCM_TRIGGER_SUSPEND:
50 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
51 		gpiod_set_value(nau8315->enable, 0);
52 		dev_dbg(component->dev, "set enable to 0");
53 		break;
54 	}
55 
56 	return 0;
57 }
58 
59 static int nau8315_enpin_event(struct snd_soc_dapm_widget *w,
60 		struct snd_kcontrol *kcontrol, int event)
61 {
62 	struct snd_soc_component *component =
63 		snd_soc_dapm_to_component(w->dapm);
64 	struct nau8315_priv *nau8315 =
65 		snd_soc_component_get_drvdata(component);
66 
67 	if (event & SND_SOC_DAPM_PRE_PMU)
68 		nau8315->enpin_switch = 1;
69 	else if (event & SND_SOC_DAPM_POST_PMD)
70 		nau8315->enpin_switch = 0;
71 
72 	return 0;
73 }
74 
75 static const struct snd_soc_dapm_widget nau8315_dapm_widgets[] = {
76 	SND_SOC_DAPM_OUTPUT("Speaker"),
77 	SND_SOC_DAPM_OUT_DRV_E("EN_Pin", SND_SOC_NOPM, 0, 0, NULL, 0,
78 			nau8315_enpin_event,
79 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
80 };
81 
82 static const struct snd_soc_dapm_route nau8315_dapm_routes[] = {
83 	{"EN_Pin", NULL, "HiFi Playback"},
84 	{"Speaker", NULL, "EN_Pin"},
85 };
86 
87 static const struct snd_soc_component_driver nau8315_component_driver = {
88 	.dapm_widgets		= nau8315_dapm_widgets,
89 	.num_dapm_widgets	= ARRAY_SIZE(nau8315_dapm_widgets),
90 	.dapm_routes		= nau8315_dapm_routes,
91 	.num_dapm_routes	= ARRAY_SIZE(nau8315_dapm_routes),
92 	.idle_bias_on		= 1,
93 	.use_pmdown_time	= 1,
94 	.endianness		= 1,
95 };
96 
97 static const struct snd_soc_dai_ops nau8315_dai_ops = {
98 	.trigger	= nau8315_daiops_trigger,
99 };
100 
101 #define NAU8315_RATES SNDRV_PCM_RATE_8000_96000
102 #define NAU8315_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE)
103 
104 static struct snd_soc_dai_driver nau8315_dai_driver = {
105 	.name = "nau8315-hifi",
106 	.playback = {
107 		.stream_name	= "HiFi Playback",
108 		.formats	= NAU8315_FORMATS,
109 		.rates		= NAU8315_RATES,
110 		.channels_min	= 1,
111 		.channels_max	= 2,
112 	},
113 	.ops    = &nau8315_dai_ops,
114 };
115 
116 static int nau8315_platform_probe(struct platform_device *pdev)
117 {
118 	struct nau8315_priv *nau8315;
119 
120 	nau8315 = devm_kzalloc(&pdev->dev, sizeof(*nau8315), GFP_KERNEL);
121 	if (!nau8315)
122 		return -ENOMEM;
123 
124 	nau8315->enable = devm_gpiod_get_optional(&pdev->dev,
125 				"enable", GPIOD_OUT_LOW);
126 	if (IS_ERR(nau8315->enable))
127 		return PTR_ERR(nau8315->enable);
128 
129 	dev_set_drvdata(&pdev->dev, nau8315);
130 
131 	return devm_snd_soc_register_component(&pdev->dev,
132 			&nau8315_component_driver,
133 			&nau8315_dai_driver, 1);
134 }
135 
136 #ifdef CONFIG_OF
137 static const struct of_device_id nau8315_device_id[] = {
138 	{ .compatible = "nuvoton,nau8315" },
139 	{ .compatible = "nuvoton,nau8318" },
140 	{}
141 };
142 MODULE_DEVICE_TABLE(of, nau8315_device_id);
143 #endif
144 
145 #ifdef CONFIG_ACPI
146 static const struct acpi_device_id nau8315_acpi_match[] = {
147 	{ "NVTN2010", 0 },
148 	{ "NVTN2012", 0},
149 	{},
150 };
151 MODULE_DEVICE_TABLE(acpi, nau8315_acpi_match);
152 #endif
153 
154 static struct platform_driver nau8315_platform_driver = {
155 	.driver = {
156 		.name = "nau8315",
157 		.of_match_table = of_match_ptr(nau8315_device_id),
158 		.acpi_match_table = ACPI_PTR(nau8315_acpi_match),
159 	},
160 	.probe	= nau8315_platform_probe,
161 };
162 module_platform_driver(nau8315_platform_driver);
163 
164 MODULE_DESCRIPTION("ASoC NAU8315 Mono Class-D Amplifier Driver");
165 MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
166 MODULE_LICENSE("GPL v2");
167