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