1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8711.c -- WM8711 ALSA SoC Audio driver
5 * Copyright 2006 Wolfson Microelectronics
7 * Author: Mike Arthur <Mike.Arthur@wolfsonmicro.com>
9 * Based on wm8731.c by Richard Purdie
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/spi/spi.h>
20 #include <linux/slab.h>
21 #include <linux/of_device.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
27 #include <sound/initval.h>
31 /* codec private data */
33 struct regmap
*regmap
;
38 * wm8711 register cache
39 * We can't read the WM8711 register space when we are
40 * using 2 wire for device control, so we cache them instead.
41 * There is no point in caching the reset register
43 static const struct reg_default wm8711_reg_defaults
[] = {
44 { 0, 0x0079 }, { 1, 0x0079 }, { 2, 0x000a }, { 3, 0x0008 },
45 { 4, 0x009f }, { 5, 0x000a }, { 6, 0x0000 }, { 7, 0x0000 },
48 static bool wm8711_volatile(struct device
*dev
, unsigned int reg
)
58 #define wm8711_reset(c) snd_soc_component_write(c, WM8711_RESET, 0)
60 static const DECLARE_TLV_DB_SCALE(out_tlv
, -12100, 100, 1);
62 static const struct snd_kcontrol_new wm8711_snd_controls
[] = {
64 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8711_LOUT1V
, WM8711_ROUT1V
,
66 SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V
, WM8711_ROUT1V
,
72 static const struct snd_kcontrol_new wm8711_output_mixer_controls
[] = {
73 SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA
, 3, 1, 0),
74 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA
, 4, 1, 0),
77 static const struct snd_soc_dapm_widget wm8711_dapm_widgets
[] = {
78 SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR
, 4, 1,
79 &wm8711_output_mixer_controls
[0],
80 ARRAY_SIZE(wm8711_output_mixer_controls
)),
81 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR
, 3, 1),
82 SND_SOC_DAPM_OUTPUT("LOUT"),
83 SND_SOC_DAPM_OUTPUT("LHPOUT"),
84 SND_SOC_DAPM_OUTPUT("ROUT"),
85 SND_SOC_DAPM_OUTPUT("RHPOUT"),
88 static const struct snd_soc_dapm_route wm8711_intercon
[] = {
90 {"Output Mixer", "Line Bypass Switch", "Line Input"},
91 {"Output Mixer", "HiFi Playback Switch", "DAC"},
94 {"RHPOUT", NULL
, "Output Mixer"},
95 {"ROUT", NULL
, "Output Mixer"},
96 {"LHPOUT", NULL
, "Output Mixer"},
97 {"LOUT", NULL
, "Output Mixer"},
109 /* codec mclk clock divider coefficients */
110 static const struct _coeff_div coeff_div
[] = {
112 {12288000, 48000, 256, 0x0, 0x0, 0x0},
113 {18432000, 48000, 384, 0x0, 0x1, 0x0},
114 {12000000, 48000, 250, 0x0, 0x0, 0x1},
117 {12288000, 32000, 384, 0x6, 0x0, 0x0},
118 {18432000, 32000, 576, 0x6, 0x1, 0x0},
119 {12000000, 32000, 375, 0x6, 0x0, 0x1},
122 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
123 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
124 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
125 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
126 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
129 {12288000, 96000, 128, 0x7, 0x0, 0x0},
130 {18432000, 96000, 192, 0x7, 0x1, 0x0},
131 {12000000, 96000, 125, 0x7, 0x0, 0x1},
134 {11289600, 44100, 256, 0x8, 0x0, 0x0},
135 {16934400, 44100, 384, 0x8, 0x1, 0x0},
136 {12000000, 44100, 272, 0x8, 0x1, 0x1},
139 {11289600, 88200, 128, 0xf, 0x0, 0x0},
140 {16934400, 88200, 192, 0xf, 0x1, 0x0},
141 {12000000, 88200, 136, 0xf, 0x1, 0x1},
144 static inline int get_coeff(int mclk
, int rate
)
148 for (i
= 0; i
< ARRAY_SIZE(coeff_div
); i
++) {
149 if (coeff_div
[i
].rate
== rate
&& coeff_div
[i
].mclk
== mclk
)
155 static int wm8711_hw_params(struct snd_pcm_substream
*substream
,
156 struct snd_pcm_hw_params
*params
,
157 struct snd_soc_dai
*dai
)
159 struct snd_soc_component
*component
= dai
->component
;
160 struct wm8711_priv
*wm8711
= snd_soc_component_get_drvdata(component
);
161 u16 iface
= snd_soc_component_read(component
, WM8711_IFACE
) & 0xfff3;
162 int i
= get_coeff(wm8711
->sysclk
, params_rate(params
));
163 u16 srate
= (coeff_div
[i
].sr
<< 2) |
164 (coeff_div
[i
].bosr
<< 1) | coeff_div
[i
].usb
;
166 snd_soc_component_write(component
, WM8711_SRATE
, srate
);
169 switch (params_width(params
)) {
180 snd_soc_component_write(component
, WM8711_IFACE
, iface
);
184 static int wm8711_pcm_prepare(struct snd_pcm_substream
*substream
,
185 struct snd_soc_dai
*dai
)
187 struct snd_soc_component
*component
= dai
->component
;
190 snd_soc_component_write(component
, WM8711_ACTIVE
, 0x0001);
195 static void wm8711_shutdown(struct snd_pcm_substream
*substream
,
196 struct snd_soc_dai
*dai
)
198 struct snd_soc_component
*component
= dai
->component
;
201 if (!snd_soc_component_active(component
)) {
203 snd_soc_component_write(component
, WM8711_ACTIVE
, 0x0);
207 static int wm8711_mute(struct snd_soc_dai
*dai
, int mute
, int direction
)
209 struct snd_soc_component
*component
= dai
->component
;
210 u16 mute_reg
= snd_soc_component_read(component
, WM8711_APDIGI
) & 0xfff7;
213 snd_soc_component_write(component
, WM8711_APDIGI
, mute_reg
| 0x8);
215 snd_soc_component_write(component
, WM8711_APDIGI
, mute_reg
);
220 static int wm8711_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
221 int clk_id
, unsigned int freq
, int dir
)
223 struct snd_soc_component
*component
= codec_dai
->component
;
224 struct wm8711_priv
*wm8711
= snd_soc_component_get_drvdata(component
);
232 wm8711
->sysclk
= freq
;
238 static int wm8711_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
241 struct snd_soc_component
*component
= codec_dai
->component
;
242 u16 iface
= snd_soc_component_read(component
, WM8711_IFACE
) & 0x000c;
244 /* set master/slave audio interface */
245 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
246 case SND_SOC_DAIFMT_CBM_CFM
:
249 case SND_SOC_DAIFMT_CBS_CFS
:
255 /* interface format */
256 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
257 case SND_SOC_DAIFMT_I2S
:
260 case SND_SOC_DAIFMT_RIGHT_J
:
262 case SND_SOC_DAIFMT_LEFT_J
:
265 case SND_SOC_DAIFMT_DSP_A
:
268 case SND_SOC_DAIFMT_DSP_B
:
275 /* clock inversion */
276 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
277 case SND_SOC_DAIFMT_NB_NF
:
279 case SND_SOC_DAIFMT_IB_IF
:
282 case SND_SOC_DAIFMT_IB_NF
:
285 case SND_SOC_DAIFMT_NB_IF
:
293 snd_soc_component_write(component
, WM8711_IFACE
, iface
);
297 static int wm8711_set_bias_level(struct snd_soc_component
*component
,
298 enum snd_soc_bias_level level
)
300 struct wm8711_priv
*wm8711
= snd_soc_component_get_drvdata(component
);
301 u16 reg
= snd_soc_component_read(component
, WM8711_PWR
) & 0xff7f;
304 case SND_SOC_BIAS_ON
:
305 snd_soc_component_write(component
, WM8711_PWR
, reg
);
307 case SND_SOC_BIAS_PREPARE
:
309 case SND_SOC_BIAS_STANDBY
:
310 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
)
311 regcache_sync(wm8711
->regmap
);
313 snd_soc_component_write(component
, WM8711_PWR
, reg
| 0x0040);
315 case SND_SOC_BIAS_OFF
:
316 snd_soc_component_write(component
, WM8711_ACTIVE
, 0x0);
317 snd_soc_component_write(component
, WM8711_PWR
, 0xffff);
323 #define WM8711_RATES SNDRV_PCM_RATE_8000_96000
325 #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
326 SNDRV_PCM_FMTBIT_S24_LE)
328 static const struct snd_soc_dai_ops wm8711_ops
= {
329 .prepare
= wm8711_pcm_prepare
,
330 .hw_params
= wm8711_hw_params
,
331 .shutdown
= wm8711_shutdown
,
332 .mute_stream
= wm8711_mute
,
333 .set_sysclk
= wm8711_set_dai_sysclk
,
334 .set_fmt
= wm8711_set_dai_fmt
,
335 .no_capture_mute
= 1,
338 static struct snd_soc_dai_driver wm8711_dai
= {
339 .name
= "wm8711-hifi",
341 .stream_name
= "Playback",
344 .rates
= WM8711_RATES
,
345 .formats
= WM8711_FORMATS
,
350 static int wm8711_probe(struct snd_soc_component
*component
)
354 ret
= wm8711_reset(component
);
356 dev_err(component
->dev
, "Failed to issue reset\n");
360 /* Latch the update bits */
361 snd_soc_component_update_bits(component
, WM8711_LOUT1V
, 0x0100, 0x0100);
362 snd_soc_component_update_bits(component
, WM8711_ROUT1V
, 0x0100, 0x0100);
368 static const struct snd_soc_component_driver soc_component_dev_wm8711
= {
369 .probe
= wm8711_probe
,
370 .set_bias_level
= wm8711_set_bias_level
,
371 .controls
= wm8711_snd_controls
,
372 .num_controls
= ARRAY_SIZE(wm8711_snd_controls
),
373 .dapm_widgets
= wm8711_dapm_widgets
,
374 .num_dapm_widgets
= ARRAY_SIZE(wm8711_dapm_widgets
),
375 .dapm_routes
= wm8711_intercon
,
376 .num_dapm_routes
= ARRAY_SIZE(wm8711_intercon
),
377 .suspend_bias_off
= 1,
379 .use_pmdown_time
= 1,
381 .non_legacy_dai_naming
= 1,
384 static const struct of_device_id wm8711_of_match
[] = {
385 { .compatible
= "wlf,wm8711", },
388 MODULE_DEVICE_TABLE(of
, wm8711_of_match
);
390 static const struct regmap_config wm8711_regmap
= {
393 .max_register
= WM8711_RESET
,
395 .reg_defaults
= wm8711_reg_defaults
,
396 .num_reg_defaults
= ARRAY_SIZE(wm8711_reg_defaults
),
397 .cache_type
= REGCACHE_RBTREE
,
399 .volatile_reg
= wm8711_volatile
,
402 #if defined(CONFIG_SPI_MASTER)
403 static int wm8711_spi_probe(struct spi_device
*spi
)
405 struct wm8711_priv
*wm8711
;
408 wm8711
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8711_priv
),
413 wm8711
->regmap
= devm_regmap_init_spi(spi
, &wm8711_regmap
);
414 if (IS_ERR(wm8711
->regmap
))
415 return PTR_ERR(wm8711
->regmap
);
417 spi_set_drvdata(spi
, wm8711
);
419 ret
= devm_snd_soc_register_component(&spi
->dev
,
420 &soc_component_dev_wm8711
, &wm8711_dai
, 1);
425 static struct spi_driver wm8711_spi_driver
= {
428 .of_match_table
= wm8711_of_match
,
430 .probe
= wm8711_spi_probe
,
432 #endif /* CONFIG_SPI_MASTER */
434 #if IS_ENABLED(CONFIG_I2C)
435 static int wm8711_i2c_probe(struct i2c_client
*client
,
436 const struct i2c_device_id
*id
)
438 struct wm8711_priv
*wm8711
;
441 wm8711
= devm_kzalloc(&client
->dev
, sizeof(struct wm8711_priv
),
446 wm8711
->regmap
= devm_regmap_init_i2c(client
, &wm8711_regmap
);
447 if (IS_ERR(wm8711
->regmap
))
448 return PTR_ERR(wm8711
->regmap
);
450 i2c_set_clientdata(client
, wm8711
);
452 ret
= devm_snd_soc_register_component(&client
->dev
,
453 &soc_component_dev_wm8711
, &wm8711_dai
, 1);
458 static const struct i2c_device_id wm8711_i2c_id
[] = {
462 MODULE_DEVICE_TABLE(i2c
, wm8711_i2c_id
);
464 static struct i2c_driver wm8711_i2c_driver
= {
467 .of_match_table
= wm8711_of_match
,
469 .probe
= wm8711_i2c_probe
,
470 .id_table
= wm8711_i2c_id
,
474 static int __init
wm8711_modinit(void)
477 #if IS_ENABLED(CONFIG_I2C)
478 ret
= i2c_add_driver(&wm8711_i2c_driver
);
480 printk(KERN_ERR
"Failed to register WM8711 I2C driver: %d\n",
484 #if defined(CONFIG_SPI_MASTER)
485 ret
= spi_register_driver(&wm8711_spi_driver
);
487 printk(KERN_ERR
"Failed to register WM8711 SPI driver: %d\n",
493 module_init(wm8711_modinit
);
495 static void __exit
wm8711_exit(void)
497 #if IS_ENABLED(CONFIG_I2C)
498 i2c_del_driver(&wm8711_i2c_driver
);
500 #if defined(CONFIG_SPI_MASTER)
501 spi_unregister_driver(&wm8711_spi_driver
);
504 module_exit(wm8711_exit
);
506 MODULE_DESCRIPTION("ASoC WM8711 driver");
507 MODULE_AUTHOR("Mike Arthur");
508 MODULE_LICENSE("GPL");