xref: /openbmc/linux/sound/soc/codecs/wm8728.c (revision 0bf49ffb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * wm8728.c  --  WM8728 ALSA SoC Audio driver
4  *
5  * Copyright 2008 Wolfson Microelectronics plc
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/pm.h>
15 #include <linux/i2c.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
19 #include <linux/slab.h>
20 #include <linux/of_device.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27 
28 #include "wm8728.h"
29 
30 /*
31  * We can't read the WM8728 register space so we cache them instead.
32  * Note that the defaults here aren't the physical defaults, we latch
33  * the volume update bits, mute the output and enable infinite zero
34  * detect.
35  */
36 static const struct reg_default wm8728_reg_defaults[] = {
37 	{ 0, 0x1ff },
38 	{ 1, 0x1ff },
39 	{ 2, 0x001 },
40 	{ 3, 0x100 },
41 };
42 
43 /* codec private data */
44 struct wm8728_priv {
45 	struct regmap *regmap;
46 };
47 
48 static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
49 
50 static const struct snd_kcontrol_new wm8728_snd_controls[] = {
51 
52 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
53 		 0, 255, 0, wm8728_tlv),
54 
55 SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
56 };
57 
58 /*
59  * DAPM controls.
60  */
61 static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
62 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
63 SND_SOC_DAPM_OUTPUT("VOUTL"),
64 SND_SOC_DAPM_OUTPUT("VOUTR"),
65 };
66 
67 static const struct snd_soc_dapm_route wm8728_intercon[] = {
68 	{"VOUTL", NULL, "DAC"},
69 	{"VOUTR", NULL, "DAC"},
70 };
71 
72 static int wm8728_mute(struct snd_soc_dai *dai, int mute, int direction)
73 {
74 	struct snd_soc_component *component = dai->component;
75 	u16 mute_reg = snd_soc_component_read(component, WM8728_DACCTL);
76 
77 	if (mute)
78 		snd_soc_component_write(component, WM8728_DACCTL, mute_reg | 1);
79 	else
80 		snd_soc_component_write(component, WM8728_DACCTL, mute_reg & ~1);
81 
82 	return 0;
83 }
84 
85 static int wm8728_hw_params(struct snd_pcm_substream *substream,
86 	struct snd_pcm_hw_params *params,
87 	struct snd_soc_dai *dai)
88 {
89 	struct snd_soc_component *component = dai->component;
90 	u16 dac = snd_soc_component_read(component, WM8728_DACCTL);
91 
92 	dac &= ~0x18;
93 
94 	switch (params_width(params)) {
95 	case 16:
96 		break;
97 	case 20:
98 		dac |= 0x10;
99 		break;
100 	case 24:
101 		dac |= 0x08;
102 		break;
103 	default:
104 		return -EINVAL;
105 	}
106 
107 	snd_soc_component_write(component, WM8728_DACCTL, dac);
108 
109 	return 0;
110 }
111 
112 static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
113 		unsigned int fmt)
114 {
115 	struct snd_soc_component *component = codec_dai->component;
116 	u16 iface = snd_soc_component_read(component, WM8728_IFCTL);
117 
118 	/* Currently only I2S is supported by the driver, though the
119 	 * hardware is more flexible.
120 	 */
121 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
122 	case SND_SOC_DAIFMT_I2S:
123 		iface |= 1;
124 		break;
125 	default:
126 		return -EINVAL;
127 	}
128 
129 	/* The hardware only support full slave mode */
130 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
131 	case SND_SOC_DAIFMT_CBS_CFS:
132 		break;
133 	default:
134 		return -EINVAL;
135 	}
136 
137 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
138 	case SND_SOC_DAIFMT_NB_NF:
139 		iface &= ~0x22;
140 		break;
141 	case SND_SOC_DAIFMT_IB_NF:
142 		iface |=  0x20;
143 		iface &= ~0x02;
144 		break;
145 	case SND_SOC_DAIFMT_NB_IF:
146 		iface |= 0x02;
147 		iface &= ~0x20;
148 		break;
149 	case SND_SOC_DAIFMT_IB_IF:
150 		iface |= 0x22;
151 		break;
152 	default:
153 		return -EINVAL;
154 	}
155 
156 	snd_soc_component_write(component, WM8728_IFCTL, iface);
157 	return 0;
158 }
159 
160 static int wm8728_set_bias_level(struct snd_soc_component *component,
161 				 enum snd_soc_bias_level level)
162 {
163 	struct wm8728_priv *wm8728 = snd_soc_component_get_drvdata(component);
164 	u16 reg;
165 
166 	switch (level) {
167 	case SND_SOC_BIAS_ON:
168 	case SND_SOC_BIAS_PREPARE:
169 	case SND_SOC_BIAS_STANDBY:
170 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
171 			/* Power everything up... */
172 			reg = snd_soc_component_read(component, WM8728_DACCTL);
173 			snd_soc_component_write(component, WM8728_DACCTL, reg & ~0x4);
174 
175 			/* ..then sync in the register cache. */
176 			regcache_sync(wm8728->regmap);
177 		}
178 		break;
179 
180 	case SND_SOC_BIAS_OFF:
181 		reg = snd_soc_component_read(component, WM8728_DACCTL);
182 		snd_soc_component_write(component, WM8728_DACCTL, reg | 0x4);
183 		break;
184 	}
185 	return 0;
186 }
187 
188 #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
189 
190 #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
191 	SNDRV_PCM_FMTBIT_S24_LE)
192 
193 static const struct snd_soc_dai_ops wm8728_dai_ops = {
194 	.hw_params	= wm8728_hw_params,
195 	.mute_stream	= wm8728_mute,
196 	.set_fmt	= wm8728_set_dai_fmt,
197 	.no_capture_mute = 1,
198 };
199 
200 static struct snd_soc_dai_driver wm8728_dai = {
201 	.name = "wm8728-hifi",
202 	.playback = {
203 		.stream_name = "Playback",
204 		.channels_min = 2,
205 		.channels_max = 2,
206 		.rates = WM8728_RATES,
207 		.formats = WM8728_FORMATS,
208 	},
209 	.ops = &wm8728_dai_ops,
210 };
211 
212 static const struct snd_soc_component_driver soc_component_dev_wm8728 = {
213 	.set_bias_level		= wm8728_set_bias_level,
214 	.controls		= wm8728_snd_controls,
215 	.num_controls		= ARRAY_SIZE(wm8728_snd_controls),
216 	.dapm_widgets		= wm8728_dapm_widgets,
217 	.num_dapm_widgets	= ARRAY_SIZE(wm8728_dapm_widgets),
218 	.dapm_routes		= wm8728_intercon,
219 	.num_dapm_routes	= ARRAY_SIZE(wm8728_intercon),
220 	.suspend_bias_off	= 1,
221 	.idle_bias_on		= 1,
222 	.use_pmdown_time	= 1,
223 	.endianness		= 1,
224 	.non_legacy_dai_naming	= 1,
225 };
226 
227 static const struct of_device_id wm8728_of_match[] = {
228 	{ .compatible = "wlf,wm8728", },
229 	{ }
230 };
231 MODULE_DEVICE_TABLE(of, wm8728_of_match);
232 
233 static const struct regmap_config wm8728_regmap = {
234 	.reg_bits = 7,
235 	.val_bits = 9,
236 	.max_register = WM8728_IFCTL,
237 
238 	.reg_defaults = wm8728_reg_defaults,
239 	.num_reg_defaults = ARRAY_SIZE(wm8728_reg_defaults),
240 	.cache_type = REGCACHE_RBTREE,
241 };
242 
243 #if defined(CONFIG_SPI_MASTER)
244 static int wm8728_spi_probe(struct spi_device *spi)
245 {
246 	struct wm8728_priv *wm8728;
247 	int ret;
248 
249 	wm8728 = devm_kzalloc(&spi->dev, sizeof(struct wm8728_priv),
250 			      GFP_KERNEL);
251 	if (wm8728 == NULL)
252 		return -ENOMEM;
253 
254 	wm8728->regmap = devm_regmap_init_spi(spi, &wm8728_regmap);
255 	if (IS_ERR(wm8728->regmap))
256 		return PTR_ERR(wm8728->regmap);
257 
258 	spi_set_drvdata(spi, wm8728);
259 
260 	ret = devm_snd_soc_register_component(&spi->dev,
261 			&soc_component_dev_wm8728, &wm8728_dai, 1);
262 
263 	return ret;
264 }
265 
266 static struct spi_driver wm8728_spi_driver = {
267 	.driver = {
268 		.name	= "wm8728",
269 		.of_match_table = wm8728_of_match,
270 	},
271 	.probe		= wm8728_spi_probe,
272 };
273 #endif /* CONFIG_SPI_MASTER */
274 
275 #if IS_ENABLED(CONFIG_I2C)
276 static int wm8728_i2c_probe(struct i2c_client *i2c,
277 			    const struct i2c_device_id *id)
278 {
279 	struct wm8728_priv *wm8728;
280 	int ret;
281 
282 	wm8728 = devm_kzalloc(&i2c->dev, sizeof(struct wm8728_priv),
283 			      GFP_KERNEL);
284 	if (wm8728 == NULL)
285 		return -ENOMEM;
286 
287 	wm8728->regmap = devm_regmap_init_i2c(i2c, &wm8728_regmap);
288 	if (IS_ERR(wm8728->regmap))
289 		return PTR_ERR(wm8728->regmap);
290 
291 	i2c_set_clientdata(i2c, wm8728);
292 
293 	ret =  devm_snd_soc_register_component(&i2c->dev,
294 			&soc_component_dev_wm8728, &wm8728_dai, 1);
295 
296 	return ret;
297 }
298 
299 static const struct i2c_device_id wm8728_i2c_id[] = {
300 	{ "wm8728", 0 },
301 	{ }
302 };
303 MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
304 
305 static struct i2c_driver wm8728_i2c_driver = {
306 	.driver = {
307 		.name = "wm8728",
308 		.of_match_table = wm8728_of_match,
309 	},
310 	.probe =    wm8728_i2c_probe,
311 	.id_table = wm8728_i2c_id,
312 };
313 #endif
314 
315 static int __init wm8728_modinit(void)
316 {
317 	int ret = 0;
318 #if IS_ENABLED(CONFIG_I2C)
319 	ret = i2c_add_driver(&wm8728_i2c_driver);
320 	if (ret != 0) {
321 		printk(KERN_ERR "Failed to register wm8728 I2C driver: %d\n",
322 		       ret);
323 	}
324 #endif
325 #if defined(CONFIG_SPI_MASTER)
326 	ret = spi_register_driver(&wm8728_spi_driver);
327 	if (ret != 0) {
328 		printk(KERN_ERR "Failed to register wm8728 SPI driver: %d\n",
329 		       ret);
330 	}
331 #endif
332 	return ret;
333 }
334 module_init(wm8728_modinit);
335 
336 static void __exit wm8728_exit(void)
337 {
338 #if IS_ENABLED(CONFIG_I2C)
339 	i2c_del_driver(&wm8728_i2c_driver);
340 #endif
341 #if defined(CONFIG_SPI_MASTER)
342 	spi_unregister_driver(&wm8728_spi_driver);
343 #endif
344 }
345 module_exit(wm8728_exit);
346 
347 MODULE_DESCRIPTION("ASoC WM8728 driver");
348 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
349 MODULE_LICENSE("GPL");
350