1// SPDX-License-Identifier: GPL-2.0-only
2//
3// rt9123p.c -- RT9123 (HW Mode) ALSA SoC Codec driver
4//
5// Author: ChiYuan Huang <cy_huang@richtek.com>
6
7#include <linux/acpi.h>
8#include <linux/delay.h>
9#include <linux/err.h>
10#include <linux/gpio/consumer.h>
11#include <linux/kernel.h>
12#include <linux/mod_devicetable.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/property.h>
17#include <sound/pcm.h>
18#include <sound/soc.h>
19#include <sound/soc-dai.h>
20#include <sound/soc-dapm.h>
21
22struct rt9123p_priv {
23 struct gpio_desc *enable;
24 unsigned int enable_delay;
25 int enable_switch;
26};
27
28static int rt9123p_daiops_trigger(struct snd_pcm_substream *substream, int cmd,
29 struct snd_soc_dai *dai)
30{
31 struct snd_soc_component *comp = dai->component;
32 struct rt9123p_priv *rt9123p = snd_soc_component_get_drvdata(c: comp);
33
34 if (!rt9123p->enable)
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(rt9123p->enable_delay);
42 if (rt9123p->enable_switch) {
43 gpiod_set_value(desc: rt9123p->enable, value: 1);
44 dev_dbg(comp->dev, "set enable 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(desc: rt9123p->enable, value: 0);
51 dev_dbg(comp->dev, "set enable to 0");
52 break;
53 default:
54 break;
55 }
56
57 return 0;
58}
59
60static int rt9123p_enable_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kcontrol,
61 int event)
62{
63 struct snd_soc_component *comp = snd_soc_dapm_to_component(dapm: w->dapm);
64 struct rt9123p_priv *rt9123p = snd_soc_component_get_drvdata(c: comp);
65
66 if (event & SND_SOC_DAPM_POST_PMU)
67 rt9123p->enable_switch = 1;
68 else if (event & SND_SOC_DAPM_POST_PMD)
69 rt9123p->enable_switch = 0;
70
71 return 0;
72}
73
74static const struct snd_soc_dapm_widget rt9123p_dapm_widgets[] = {
75 SND_SOC_DAPM_OUTPUT("SPK"),
76 SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0, rt9123p_enable_event,
77 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
78};
79
80static const struct snd_soc_dapm_route rt9123p_dapm_routes[] = {
81 {"Amp Drv", NULL, "HiFi Playback"},
82 {"SPK", NULL, "Amp Drv"},
83};
84
85static const struct snd_soc_component_driver rt9123p_comp_driver = {
86 .dapm_widgets = rt9123p_dapm_widgets,
87 .num_dapm_widgets = ARRAY_SIZE(rt9123p_dapm_widgets),
88 .dapm_routes = rt9123p_dapm_routes,
89 .num_dapm_routes = ARRAY_SIZE(rt9123p_dapm_routes),
90 .idle_bias_on = 1,
91 .use_pmdown_time = 1,
92 .endianness = 1,
93};
94
95static const struct snd_soc_dai_ops rt9123p_dai_ops = {
96 .trigger = rt9123p_daiops_trigger,
97};
98
99static struct snd_soc_dai_driver rt9123p_dai_driver = {
100 .name = "HiFi",
101 .playback = {
102 .stream_name = "HiFi Playback",
103 .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S24 |
104 SNDRV_PCM_FMTBIT_S32,
105 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
106 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_24000 |
107 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
108 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
109 SNDRV_PCM_RATE_96000,
110 .rate_min = 8000,
111 .rate_max = 96000,
112 .channels_min = 1,
113 .channels_max = 2,
114 },
115 .ops = &rt9123p_dai_ops,
116};
117
118static int rt9123p_platform_probe(struct platform_device *pdev)
119{
120 struct device *dev = &pdev->dev;
121 struct rt9123p_priv *rt9123p;
122 int ret;
123
124 rt9123p = devm_kzalloc(dev, size: sizeof(*rt9123p), GFP_KERNEL);
125 if (!rt9123p)
126 return -ENOMEM;
127
128 rt9123p->enable = devm_gpiod_get_optional(dev, con_id: "enable", flags: GPIOD_OUT_LOW);
129 if (IS_ERR(ptr: rt9123p->enable))
130 return PTR_ERR(ptr: rt9123p->enable);
131
132 ret = device_property_read_u32(dev, propname: "enable-delay-ms", val: &rt9123p->enable_delay);
133 if (ret) {
134 rt9123p->enable_delay = 0;
135 dev_dbg(dev, "no optional property 'enable-delay-ms' found, default: no delay\n");
136 }
137
138 platform_set_drvdata(pdev, data: rt9123p);
139
140 return devm_snd_soc_register_component(dev, component_driver: &rt9123p_comp_driver, dai_drv: &rt9123p_dai_driver, num_dai: 1);
141}
142
143#ifdef CONFIG_OF
144static const struct of_device_id rt9123p_device_id[] = {
145 { .compatible = "richtek,rt9123p" },
146 {}
147};
148MODULE_DEVICE_TABLE(of, rt9123p_device_id);
149#endif
150
151#ifdef CONFIG_ACPI
152static const struct acpi_device_id rt9123p_acpi_match[] = {
153 { "RT9123P", 0 },
154 {}
155};
156MODULE_DEVICE_TABLE(acpi, rt9123p_acpi_match);
157#endif
158
159static struct platform_driver rt9123p_platform_driver = {
160 .driver = {
161 .name = "rt9123p",
162 .of_match_table = of_match_ptr(rt9123p_device_id),
163 .acpi_match_table = ACPI_PTR(rt9123p_acpi_match),
164 },
165 .probe = rt9123p_platform_probe,
166};
167module_platform_driver(rt9123p_platform_driver);
168
169MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
170MODULE_DESCRIPTION("ASoC rt9123p Driver");
171MODULE_LICENSE("GPL");
172

source code of linux/sound/soc/codecs/rt9123p.c