xref: /linux/sound/soc/codecs/wm8782.c (revision 79997eda0d31bc68203c95ecb978773ee6ce7a1f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * sound/soc/codecs/wm8782.c
4  * simple, strap-pin configured 24bit 2ch ADC
5  *
6  * Copyright: 2011 Raumfeld GmbH
7  * Author: Johannes Stezenbach <js@sig21.net>
8  *
9  * based on ad73311.c
10  * Copyright:	Analog Devices Inc.
11  * Author:	Cliff Cai <cliff.cai@analog.com>
12  */
13 
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/device.h>
19 #include <linux/regulator/consumer.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/ac97_codec.h>
23 #include <sound/initval.h>
24 #include <sound/soc.h>
25 
26 /* regulator power supply names */
27 static const char *supply_names[] = {
28 	"Vdda", /* analog supply, 2.7V - 3.6V */
29 	"Vdd",  /* digital supply, 2.7V - 5.5V */
30 };
31 
32 struct wm8782_priv {
33 	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
34 	int max_rate;
35 };
36 
37 static int wm8782_dai_startup(struct snd_pcm_substream *sub, struct snd_soc_dai *dai)
38 {
39 	struct snd_pcm_runtime *runtime = sub->runtime;
40 	struct wm8782_priv *priv =
41 		snd_soc_component_get_drvdata(dai->component);
42 
43 	return snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
44 					   8000, priv->max_rate);
45 }
46 
47 static const struct snd_soc_dapm_widget wm8782_dapm_widgets[] = {
48 SND_SOC_DAPM_INPUT("AINL"),
49 SND_SOC_DAPM_INPUT("AINR"),
50 };
51 
52 static const struct snd_soc_dapm_route wm8782_dapm_routes[] = {
53 	{ "Capture", NULL, "AINL" },
54 	{ "Capture", NULL, "AINR" },
55 };
56 
57 static const struct snd_soc_dai_ops wm8782_dai_ops = {
58 	.startup = &wm8782_dai_startup,
59 };
60 
61 static struct snd_soc_dai_driver wm8782_dai = {
62 	.name = "wm8782",
63 	.capture = {
64 		.stream_name = "Capture",
65 		.channels_min = 2,
66 		.channels_max = 2,
67 		.rates = SNDRV_PCM_RATE_8000_192000,
68 		.formats = SNDRV_PCM_FMTBIT_S16_LE |
69 			   SNDRV_PCM_FMTBIT_S20_3LE |
70 			   SNDRV_PCM_FMTBIT_S24_LE,
71 	},
72 	.ops = &wm8782_dai_ops,
73 };
74 
75 static int wm8782_soc_probe(struct snd_soc_component *component)
76 {
77 	struct wm8782_priv *priv = snd_soc_component_get_drvdata(component);
78 	return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
79 }
80 
81 static void wm8782_soc_remove(struct snd_soc_component *component)
82 {
83 	struct wm8782_priv *priv = snd_soc_component_get_drvdata(component);
84 	regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
85 }
86 
87 #ifdef CONFIG_PM
88 static int wm8782_soc_suspend(struct snd_soc_component *component)
89 {
90 	struct wm8782_priv *priv = snd_soc_component_get_drvdata(component);
91 	regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
92 	return 0;
93 }
94 
95 static int wm8782_soc_resume(struct snd_soc_component *component)
96 {
97 	struct wm8782_priv *priv = snd_soc_component_get_drvdata(component);
98 	return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
99 }
100 #else
101 #define wm8782_soc_suspend      NULL
102 #define wm8782_soc_resume       NULL
103 #endif /* CONFIG_PM */
104 
105 static const struct snd_soc_component_driver soc_component_dev_wm8782 = {
106 	.probe			= wm8782_soc_probe,
107 	.remove			= wm8782_soc_remove,
108 	.suspend		= wm8782_soc_suspend,
109 	.resume			= wm8782_soc_resume,
110 	.dapm_widgets		= wm8782_dapm_widgets,
111 	.num_dapm_widgets	= ARRAY_SIZE(wm8782_dapm_widgets),
112 	.dapm_routes		= wm8782_dapm_routes,
113 	.num_dapm_routes	= ARRAY_SIZE(wm8782_dapm_routes),
114 	.idle_bias_on		= 1,
115 	.use_pmdown_time	= 1,
116 	.endianness		= 1,
117 };
118 
119 static int wm8782_probe(struct platform_device *pdev)
120 {
121 	struct device *dev = &pdev->dev;
122 	struct device_node *np = dev->of_node;
123 	struct wm8782_priv *priv;
124 	int ret, i, fsampen;
125 
126 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
127 	if (!priv)
128 		return -ENOMEM;
129 
130 	dev_set_drvdata(dev, priv);
131 
132 	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
133 		priv->supplies[i].supply = supply_names[i];
134 
135 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
136 				      priv->supplies);
137 	if (ret < 0)
138 		return ret;
139 
140 	// Assume lowest value by default to avoid inadvertent overclocking
141 	fsampen = 0;
142 
143 	if (np)
144 		of_property_read_u32(np, "wlf,fsampen", &fsampen);
145 
146 	switch (fsampen) {
147 	case 0:
148 		priv->max_rate = 48000;
149 		break;
150 	case 1:
151 		priv->max_rate = 96000;
152 		break;
153 	case 2:
154 		priv->max_rate = 192000;
155 		break;
156 	default:
157 		dev_err(dev, "Invalid wlf,fsampen value");
158 		return -EINVAL;
159 	}
160 
161 	return devm_snd_soc_register_component(&pdev->dev,
162 			&soc_component_dev_wm8782, &wm8782_dai, 1);
163 }
164 
165 #ifdef CONFIG_OF
166 static const struct of_device_id wm8782_of_match[] = {
167 	{ .compatible = "wlf,wm8782", },
168 	{ }
169 };
170 MODULE_DEVICE_TABLE(of, wm8782_of_match);
171 #endif
172 
173 static struct platform_driver wm8782_codec_driver = {
174 	.driver = {
175 		.name = "wm8782",
176 		.of_match_table = of_match_ptr(wm8782_of_match),
177 	},
178 	.probe = wm8782_probe,
179 };
180 
181 module_platform_driver(wm8782_codec_driver);
182 
183 MODULE_DESCRIPTION("ASoC WM8782 driver");
184 MODULE_AUTHOR("Johannes Stezenbach <js@sig21.net>");
185 MODULE_LICENSE("GPL");
186