1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2924914eeSMark Brown /*
3924914eeSMark Brown * wm8776.c -- WM8776 ALSA SoC Audio driver
4924914eeSMark Brown *
5656baaebSMark Brown * Copyright 2009-12 Wolfson Microelectronics plc
6924914eeSMark Brown *
7924914eeSMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8924914eeSMark Brown *
9924914eeSMark Brown * TODO: Input ALC/limiter support
10924914eeSMark Brown */
11924914eeSMark Brown
12924914eeSMark Brown #include <linux/module.h>
13924914eeSMark Brown #include <linux/moduleparam.h>
14924914eeSMark Brown #include <linux/init.h>
15924914eeSMark Brown #include <linux/delay.h>
16924914eeSMark Brown #include <linux/pm.h>
17924914eeSMark Brown #include <linux/i2c.h>
18b6de4315SMark Brown #include <linux/of_device.h>
192cbde7abSMark Brown #include <linux/regmap.h>
20924914eeSMark Brown #include <linux/spi/spi.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
22924914eeSMark Brown #include <sound/core.h>
23924914eeSMark Brown #include <sound/pcm.h>
24924914eeSMark Brown #include <sound/pcm_params.h>
25924914eeSMark Brown #include <sound/soc.h>
26924914eeSMark Brown #include <sound/initval.h>
27924914eeSMark Brown #include <sound/tlv.h>
28924914eeSMark Brown
29924914eeSMark Brown #include "wm8776.h"
30924914eeSMark Brown
31fdd48f9dSMark Brown enum wm8776_chip_type {
32fdd48f9dSMark Brown WM8775 = 1,
33fdd48f9dSMark Brown WM8776,
34fdd48f9dSMark Brown };
35fdd48f9dSMark Brown
36924914eeSMark Brown /* codec private data */
37924914eeSMark Brown struct wm8776_priv {
382cbde7abSMark Brown struct regmap *regmap;
39924914eeSMark Brown int sysclk[2];
40924914eeSMark Brown };
41924914eeSMark Brown
422cbde7abSMark Brown static const struct reg_default wm8776_reg_defaults[] = {
432cbde7abSMark Brown { 0, 0x79 },
442cbde7abSMark Brown { 1, 0x79 },
452cbde7abSMark Brown { 2, 0x79 },
462cbde7abSMark Brown { 3, 0xff },
472cbde7abSMark Brown { 4, 0xff },
482cbde7abSMark Brown { 5, 0xff },
492cbde7abSMark Brown { 6, 0x00 },
502cbde7abSMark Brown { 7, 0x90 },
512cbde7abSMark Brown { 8, 0x00 },
522cbde7abSMark Brown { 9, 0x00 },
532cbde7abSMark Brown { 10, 0x22 },
542cbde7abSMark Brown { 11, 0x22 },
552cbde7abSMark Brown { 12, 0x22 },
562cbde7abSMark Brown { 13, 0x08 },
572cbde7abSMark Brown { 14, 0xcf },
582cbde7abSMark Brown { 15, 0xcf },
592cbde7abSMark Brown { 16, 0x7b },
602cbde7abSMark Brown { 17, 0x00 },
612cbde7abSMark Brown { 18, 0x32 },
622cbde7abSMark Brown { 19, 0x00 },
632cbde7abSMark Brown { 20, 0xa6 },
642cbde7abSMark Brown { 21, 0x01 },
652cbde7abSMark Brown { 22, 0x01 },
66924914eeSMark Brown };
67924914eeSMark Brown
wm8776_volatile(struct device * dev,unsigned int reg)682cbde7abSMark Brown static bool wm8776_volatile(struct device *dev, unsigned int reg)
692cbde7abSMark Brown {
702cbde7abSMark Brown switch (reg) {
712cbde7abSMark Brown case WM8776_RESET:
722cbde7abSMark Brown return true;
732cbde7abSMark Brown default:
742cbde7abSMark Brown return false;
752cbde7abSMark Brown }
762cbde7abSMark Brown }
772cbde7abSMark Brown
wm8776_reset(struct snd_soc_component * component)7851e42c95SKuninori Morimoto static int wm8776_reset(struct snd_soc_component *component)
79924914eeSMark Brown {
8051e42c95SKuninori Morimoto return snd_soc_component_write(component, WM8776_RESET, 0);
81924914eeSMark Brown }
82924914eeSMark Brown
83924914eeSMark Brown static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
84924914eeSMark Brown static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
85924914eeSMark Brown static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
86924914eeSMark Brown
87924914eeSMark Brown static const struct snd_kcontrol_new wm8776_snd_controls[] = {
88924914eeSMark Brown SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
89924914eeSMark Brown 0, 127, 0, hp_tlv),
90924914eeSMark Brown SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
91924914eeSMark Brown 0, 255, 0, dac_tlv),
92924914eeSMark Brown SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
93924914eeSMark Brown
94924914eeSMark Brown SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
95924914eeSMark Brown
96924914eeSMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
97924914eeSMark Brown 0, 255, 0, adc_tlv),
98924914eeSMark Brown SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
99924914eeSMark Brown SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
100924914eeSMark Brown SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
101924914eeSMark Brown };
102924914eeSMark Brown
103924914eeSMark Brown static const struct snd_kcontrol_new inmix_controls[] = {
104924914eeSMark Brown SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
105924914eeSMark Brown SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
106924914eeSMark Brown SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
107924914eeSMark Brown SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
108924914eeSMark Brown SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
109924914eeSMark Brown };
110924914eeSMark Brown
111924914eeSMark Brown static const struct snd_kcontrol_new outmix_controls[] = {
112924914eeSMark Brown SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
113924914eeSMark Brown SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
114924914eeSMark Brown SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
115924914eeSMark Brown };
116924914eeSMark Brown
117924914eeSMark Brown static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
118924914eeSMark Brown SND_SOC_DAPM_INPUT("AUX"),
119924914eeSMark Brown
120924914eeSMark Brown SND_SOC_DAPM_INPUT("AIN1"),
121924914eeSMark Brown SND_SOC_DAPM_INPUT("AIN2"),
122924914eeSMark Brown SND_SOC_DAPM_INPUT("AIN3"),
123924914eeSMark Brown SND_SOC_DAPM_INPUT("AIN4"),
124924914eeSMark Brown SND_SOC_DAPM_INPUT("AIN5"),
125924914eeSMark Brown
126924914eeSMark Brown SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
127924914eeSMark Brown inmix_controls, ARRAY_SIZE(inmix_controls)),
128924914eeSMark Brown
129924914eeSMark Brown SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
130924914eeSMark Brown SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
131924914eeSMark Brown
132924914eeSMark Brown SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
133924914eeSMark Brown outmix_controls, ARRAY_SIZE(outmix_controls)),
134924914eeSMark Brown
135924914eeSMark Brown SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
136924914eeSMark Brown
137924914eeSMark Brown SND_SOC_DAPM_OUTPUT("VOUT"),
138924914eeSMark Brown
139924914eeSMark Brown SND_SOC_DAPM_OUTPUT("HPOUTL"),
140924914eeSMark Brown SND_SOC_DAPM_OUTPUT("HPOUTR"),
141924914eeSMark Brown };
142924914eeSMark Brown
143924914eeSMark Brown static const struct snd_soc_dapm_route routes[] = {
144924914eeSMark Brown { "Input Mixer", "AIN1 Switch", "AIN1" },
145924914eeSMark Brown { "Input Mixer", "AIN2 Switch", "AIN2" },
146924914eeSMark Brown { "Input Mixer", "AIN3 Switch", "AIN3" },
147924914eeSMark Brown { "Input Mixer", "AIN4 Switch", "AIN4" },
148924914eeSMark Brown { "Input Mixer", "AIN5 Switch", "AIN5" },
149924914eeSMark Brown
150924914eeSMark Brown { "ADC", NULL, "Input Mixer" },
151924914eeSMark Brown
152924914eeSMark Brown { "Output Mixer", "DAC Switch", "DAC" },
153924914eeSMark Brown { "Output Mixer", "AUX Switch", "AUX" },
154924914eeSMark Brown { "Output Mixer", "Bypass Switch", "Input Mixer" },
155924914eeSMark Brown
156924914eeSMark Brown { "VOUT", NULL, "Output Mixer" },
157924914eeSMark Brown
158924914eeSMark Brown { "Headphone PGA", NULL, "Output Mixer" },
159924914eeSMark Brown
160924914eeSMark Brown { "HPOUTL", NULL, "Headphone PGA" },
161924914eeSMark Brown { "HPOUTR", NULL, "Headphone PGA" },
162924914eeSMark Brown };
163924914eeSMark Brown
wm8776_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)164924914eeSMark Brown static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
165924914eeSMark Brown {
16651e42c95SKuninori Morimoto struct snd_soc_component *component = dai->component;
167924914eeSMark Brown int reg, iface, master;
168924914eeSMark Brown
169f0fba2adSLiam Girdwood switch (dai->driver->id) {
170924914eeSMark Brown case WM8776_DAI_DAC:
171924914eeSMark Brown reg = WM8776_DACIFCTRL;
172924914eeSMark Brown master = 0x80;
173924914eeSMark Brown break;
174924914eeSMark Brown case WM8776_DAI_ADC:
175924914eeSMark Brown reg = WM8776_ADCIFCTRL;
176924914eeSMark Brown master = 0x100;
177924914eeSMark Brown break;
178924914eeSMark Brown default:
179924914eeSMark Brown return -EINVAL;
180924914eeSMark Brown }
181924914eeSMark Brown
182924914eeSMark Brown iface = 0;
183924914eeSMark Brown
184924914eeSMark Brown switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
185924914eeSMark Brown case SND_SOC_DAIFMT_CBM_CFM:
186924914eeSMark Brown break;
187924914eeSMark Brown case SND_SOC_DAIFMT_CBS_CFS:
188924914eeSMark Brown master = 0;
189924914eeSMark Brown break;
190924914eeSMark Brown default:
191924914eeSMark Brown return -EINVAL;
192924914eeSMark Brown }
193924914eeSMark Brown
194924914eeSMark Brown switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
195924914eeSMark Brown case SND_SOC_DAIFMT_I2S:
196924914eeSMark Brown iface |= 0x0002;
197924914eeSMark Brown break;
198924914eeSMark Brown case SND_SOC_DAIFMT_RIGHT_J:
199924914eeSMark Brown break;
200924914eeSMark Brown case SND_SOC_DAIFMT_LEFT_J:
201924914eeSMark Brown iface |= 0x0001;
202924914eeSMark Brown break;
203924914eeSMark Brown default:
204924914eeSMark Brown return -EINVAL;
205924914eeSMark Brown }
206924914eeSMark Brown
207924914eeSMark Brown switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
208924914eeSMark Brown case SND_SOC_DAIFMT_NB_NF:
209924914eeSMark Brown break;
210924914eeSMark Brown case SND_SOC_DAIFMT_IB_IF:
211924914eeSMark Brown iface |= 0x00c;
212924914eeSMark Brown break;
213924914eeSMark Brown case SND_SOC_DAIFMT_IB_NF:
214924914eeSMark Brown iface |= 0x008;
215924914eeSMark Brown break;
216924914eeSMark Brown case SND_SOC_DAIFMT_NB_IF:
217924914eeSMark Brown iface |= 0x004;
218924914eeSMark Brown break;
219924914eeSMark Brown default:
220924914eeSMark Brown return -EINVAL;
221924914eeSMark Brown }
222924914eeSMark Brown
223924914eeSMark Brown /* Finally, write out the values */
22451e42c95SKuninori Morimoto snd_soc_component_update_bits(component, reg, 0xf, iface);
22551e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_MSTRCTRL, 0x180, master);
226924914eeSMark Brown
227924914eeSMark Brown return 0;
228924914eeSMark Brown }
229924914eeSMark Brown
230924914eeSMark Brown static int mclk_ratios[] = {
231924914eeSMark Brown 128,
232924914eeSMark Brown 192,
233924914eeSMark Brown 256,
234924914eeSMark Brown 384,
235924914eeSMark Brown 512,
236924914eeSMark Brown 768,
237924914eeSMark Brown };
238924914eeSMark Brown
wm8776_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)239924914eeSMark Brown static int wm8776_hw_params(struct snd_pcm_substream *substream,
240924914eeSMark Brown struct snd_pcm_hw_params *params,
241924914eeSMark Brown struct snd_soc_dai *dai)
242924914eeSMark Brown {
24351e42c95SKuninori Morimoto struct snd_soc_component *component = dai->component;
24451e42c95SKuninori Morimoto struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
245924914eeSMark Brown int iface_reg, iface;
246924914eeSMark Brown int ratio_shift, master;
247924914eeSMark Brown int i;
248924914eeSMark Brown
249f0fba2adSLiam Girdwood switch (dai->driver->id) {
250924914eeSMark Brown case WM8776_DAI_DAC:
251924914eeSMark Brown iface_reg = WM8776_DACIFCTRL;
252924914eeSMark Brown master = 0x80;
253924914eeSMark Brown ratio_shift = 4;
254924914eeSMark Brown break;
255924914eeSMark Brown case WM8776_DAI_ADC:
256924914eeSMark Brown iface_reg = WM8776_ADCIFCTRL;
257924914eeSMark Brown master = 0x100;
258924914eeSMark Brown ratio_shift = 0;
259924914eeSMark Brown break;
260924914eeSMark Brown default:
261924914eeSMark Brown return -EINVAL;
262924914eeSMark Brown }
263924914eeSMark Brown
264924914eeSMark Brown /* Set word length */
2650a3dcb50SAxel Lin switch (params_width(params)) {
266d1dc698aSTimur Tabi case 16:
267d1dc698aSTimur Tabi iface = 0;
268990fc3d0SJoachim Eastwood break;
269d1dc698aSTimur Tabi case 20:
270d1dc698aSTimur Tabi iface = 0x10;
271924914eeSMark Brown break;
272d1dc698aSTimur Tabi case 24:
273d1dc698aSTimur Tabi iface = 0x20;
274924914eeSMark Brown break;
275d1dc698aSTimur Tabi case 32:
276d1dc698aSTimur Tabi iface = 0x30;
277924914eeSMark Brown break;
278d1dc698aSTimur Tabi default:
27951e42c95SKuninori Morimoto dev_err(component->dev, "Unsupported sample size: %i\n",
2800a3dcb50SAxel Lin params_width(params));
281d1dc698aSTimur Tabi return -EINVAL;
282924914eeSMark Brown }
283924914eeSMark Brown
284924914eeSMark Brown /* Only need to set MCLK/LRCLK ratio if we're master */
2856d75dfc3SKuninori Morimoto if (snd_soc_component_read(component, WM8776_MSTRCTRL) & master) {
286924914eeSMark Brown for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
287f0fba2adSLiam Girdwood if (wm8776->sysclk[dai->driver->id] / params_rate(params)
288924914eeSMark Brown == mclk_ratios[i])
289924914eeSMark Brown break;
290924914eeSMark Brown }
291924914eeSMark Brown
292924914eeSMark Brown if (i == ARRAY_SIZE(mclk_ratios)) {
29351e42c95SKuninori Morimoto dev_err(component->dev,
294924914eeSMark Brown "Unable to configure MCLK ratio %d/%d\n",
295f0fba2adSLiam Girdwood wm8776->sysclk[dai->driver->id], params_rate(params));
296924914eeSMark Brown return -EINVAL;
297924914eeSMark Brown }
298924914eeSMark Brown
29951e42c95SKuninori Morimoto dev_dbg(component->dev, "MCLK is %dfs\n", mclk_ratios[i]);
300924914eeSMark Brown
30151e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_MSTRCTRL,
302924914eeSMark Brown 0x7 << ratio_shift, i << ratio_shift);
303924914eeSMark Brown } else {
30451e42c95SKuninori Morimoto dev_dbg(component->dev, "DAI in slave mode\n");
305924914eeSMark Brown }
306924914eeSMark Brown
30751e42c95SKuninori Morimoto snd_soc_component_update_bits(component, iface_reg, 0x30, iface);
308924914eeSMark Brown
309924914eeSMark Brown return 0;
310924914eeSMark Brown }
311924914eeSMark Brown
wm8776_mute(struct snd_soc_dai * dai,int mute,int direction)31226d3c16eSKuninori Morimoto static int wm8776_mute(struct snd_soc_dai *dai, int mute, int direction)
313924914eeSMark Brown {
31451e42c95SKuninori Morimoto struct snd_soc_component *component = dai->component;
315924914eeSMark Brown
31651e42c95SKuninori Morimoto return snd_soc_component_write(component, WM8776_DACMUTE, !!mute);
317924914eeSMark Brown }
318924914eeSMark Brown
wm8776_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)319924914eeSMark Brown static int wm8776_set_sysclk(struct snd_soc_dai *dai,
320924914eeSMark Brown int clk_id, unsigned int freq, int dir)
321924914eeSMark Brown {
32251e42c95SKuninori Morimoto struct snd_soc_component *component = dai->component;
32351e42c95SKuninori Morimoto struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
324924914eeSMark Brown
32522a7038cSTakashi Iwai if (WARN_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk)))
32622a7038cSTakashi Iwai return -EINVAL;
327924914eeSMark Brown
328f0fba2adSLiam Girdwood wm8776->sysclk[dai->driver->id] = freq;
329924914eeSMark Brown
330924914eeSMark Brown return 0;
331924914eeSMark Brown }
332924914eeSMark Brown
wm8776_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)33351e42c95SKuninori Morimoto static int wm8776_set_bias_level(struct snd_soc_component *component,
334924914eeSMark Brown enum snd_soc_bias_level level)
335924914eeSMark Brown {
33651e42c95SKuninori Morimoto struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
3372cbde7abSMark Brown
338924914eeSMark Brown switch (level) {
339924914eeSMark Brown case SND_SOC_BIAS_ON:
340924914eeSMark Brown break;
341924914eeSMark Brown case SND_SOC_BIAS_PREPARE:
342924914eeSMark Brown break;
343924914eeSMark Brown case SND_SOC_BIAS_STANDBY:
34451e42c95SKuninori Morimoto if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
3452cbde7abSMark Brown regcache_sync(wm8776->regmap);
346abe11d0aSAxel Lin
347924914eeSMark Brown /* Disable the global powerdown; DAPM does the rest */
34851e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_PWRDOWN, 1, 0);
349924914eeSMark Brown }
350924914eeSMark Brown
351924914eeSMark Brown break;
352924914eeSMark Brown case SND_SOC_BIAS_OFF:
35351e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_PWRDOWN, 1, 1);
354924914eeSMark Brown break;
355924914eeSMark Brown }
356924914eeSMark Brown
357924914eeSMark Brown return 0;
358924914eeSMark Brown }
359924914eeSMark Brown
360924914eeSMark Brown #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
361924914eeSMark Brown SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
362924914eeSMark Brown
36385e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8776_dac_ops = {
36426d3c16eSKuninori Morimoto .mute_stream = wm8776_mute,
365924914eeSMark Brown .hw_params = wm8776_hw_params,
366924914eeSMark Brown .set_fmt = wm8776_set_fmt,
367924914eeSMark Brown .set_sysclk = wm8776_set_sysclk,
36826d3c16eSKuninori Morimoto .no_capture_mute = 1,
369924914eeSMark Brown };
370924914eeSMark Brown
37185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8776_adc_ops = {
372924914eeSMark Brown .hw_params = wm8776_hw_params,
373924914eeSMark Brown .set_fmt = wm8776_set_fmt,
374924914eeSMark Brown .set_sysclk = wm8776_set_sysclk,
375924914eeSMark Brown };
376924914eeSMark Brown
377f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm8776_dai[] = {
378924914eeSMark Brown {
379f0fba2adSLiam Girdwood .name = "wm8776-hifi-playback",
380924914eeSMark Brown .id = WM8776_DAI_DAC,
381924914eeSMark Brown .playback = {
382924914eeSMark Brown .stream_name = "Playback",
383924914eeSMark Brown .channels_min = 2,
384924914eeSMark Brown .channels_max = 2,
3850016226dSTimur Tabi .rates = SNDRV_PCM_RATE_CONTINUOUS,
3860016226dSTimur Tabi .rate_min = 32000,
3870016226dSTimur Tabi .rate_max = 192000,
388924914eeSMark Brown .formats = WM8776_FORMATS,
389924914eeSMark Brown },
390924914eeSMark Brown .ops = &wm8776_dac_ops,
391924914eeSMark Brown },
392924914eeSMark Brown {
393f0fba2adSLiam Girdwood .name = "wm8776-hifi-capture",
394924914eeSMark Brown .id = WM8776_DAI_ADC,
395924914eeSMark Brown .capture = {
396924914eeSMark Brown .stream_name = "Capture",
397924914eeSMark Brown .channels_min = 2,
398924914eeSMark Brown .channels_max = 2,
3990016226dSTimur Tabi .rates = SNDRV_PCM_RATE_CONTINUOUS,
4000016226dSTimur Tabi .rate_min = 32000,
4010016226dSTimur Tabi .rate_max = 96000,
402924914eeSMark Brown .formats = WM8776_FORMATS,
403924914eeSMark Brown },
404924914eeSMark Brown .ops = &wm8776_adc_ops,
405924914eeSMark Brown },
406924914eeSMark Brown };
407924914eeSMark Brown
wm8776_probe(struct snd_soc_component * component)40851e42c95SKuninori Morimoto static int wm8776_probe(struct snd_soc_component *component)
409924914eeSMark Brown {
410924914eeSMark Brown int ret = 0;
411924914eeSMark Brown
41251e42c95SKuninori Morimoto ret = wm8776_reset(component);
413924914eeSMark Brown if (ret < 0) {
41451e42c95SKuninori Morimoto dev_err(component->dev, "Failed to issue reset: %d\n", ret);
415f0fba2adSLiam Girdwood return ret;
416924914eeSMark Brown }
417924914eeSMark Brown
418924914eeSMark Brown /* Latch the update bits; right channel only since we always
419924914eeSMark Brown * update both. */
42051e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_HPRVOL, 0x100, 0x100);
42151e42c95SKuninori Morimoto snd_soc_component_update_bits(component, WM8776_DACRVOL, 0x100, 0x100);
422924914eeSMark Brown
423924914eeSMark Brown return ret;
424924914eeSMark Brown }
425924914eeSMark Brown
42651e42c95SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8776 = {
427f0fba2adSLiam Girdwood .probe = wm8776_probe,
428f0fba2adSLiam Girdwood .set_bias_level = wm8776_set_bias_level,
4291db3c98eSMark Brown .controls = wm8776_snd_controls,
4301db3c98eSMark Brown .num_controls = ARRAY_SIZE(wm8776_snd_controls),
4311db3c98eSMark Brown .dapm_widgets = wm8776_dapm_widgets,
4321db3c98eSMark Brown .num_dapm_widgets = ARRAY_SIZE(wm8776_dapm_widgets),
4331db3c98eSMark Brown .dapm_routes = routes,
4341db3c98eSMark Brown .num_dapm_routes = ARRAY_SIZE(routes),
43551e42c95SKuninori Morimoto .suspend_bias_off = 1,
43651e42c95SKuninori Morimoto .idle_bias_on = 1,
43751e42c95SKuninori Morimoto .use_pmdown_time = 1,
43851e42c95SKuninori Morimoto .endianness = 1,
439f0fba2adSLiam Girdwood };
440f0fba2adSLiam Girdwood
441b6de4315SMark Brown static const struct of_device_id wm8776_of_match[] = {
442b6de4315SMark Brown { .compatible = "wlf,wm8776", },
443b6de4315SMark Brown { }
444b6de4315SMark Brown };
445b6de4315SMark Brown MODULE_DEVICE_TABLE(of, wm8776_of_match);
446b6de4315SMark Brown
4472cbde7abSMark Brown static const struct regmap_config wm8776_regmap = {
4482cbde7abSMark Brown .reg_bits = 7,
4492cbde7abSMark Brown .val_bits = 9,
4502cbde7abSMark Brown .max_register = WM8776_RESET,
4512cbde7abSMark Brown
4522cbde7abSMark Brown .reg_defaults = wm8776_reg_defaults,
4532cbde7abSMark Brown .num_reg_defaults = ARRAY_SIZE(wm8776_reg_defaults),
454*080c82a5SMark Brown .cache_type = REGCACHE_MAPLE,
4552cbde7abSMark Brown
4562cbde7abSMark Brown .volatile_reg = wm8776_volatile,
4572cbde7abSMark Brown };
4582cbde7abSMark Brown
459924914eeSMark Brown #if defined(CONFIG_SPI_MASTER)
wm8776_spi_probe(struct spi_device * spi)4607a79e94eSBill Pemberton static int wm8776_spi_probe(struct spi_device *spi)
461924914eeSMark Brown {
462924914eeSMark Brown struct wm8776_priv *wm8776;
463f0fba2adSLiam Girdwood int ret;
464924914eeSMark Brown
465bf97ca9aSMark Brown wm8776 = devm_kzalloc(&spi->dev, sizeof(struct wm8776_priv),
466bf97ca9aSMark Brown GFP_KERNEL);
467924914eeSMark Brown if (wm8776 == NULL)
468924914eeSMark Brown return -ENOMEM;
469924914eeSMark Brown
4702cbde7abSMark Brown wm8776->regmap = devm_regmap_init_spi(spi, &wm8776_regmap);
4712cbde7abSMark Brown if (IS_ERR(wm8776->regmap))
4722cbde7abSMark Brown return PTR_ERR(wm8776->regmap);
4732cbde7abSMark Brown
474f0fba2adSLiam Girdwood spi_set_drvdata(spi, wm8776);
475924914eeSMark Brown
47651e42c95SKuninori Morimoto ret = devm_snd_soc_register_component(&spi->dev,
47751e42c95SKuninori Morimoto &soc_component_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
478bf97ca9aSMark Brown
479f0fba2adSLiam Girdwood return ret;
480924914eeSMark Brown }
481924914eeSMark Brown
482924914eeSMark Brown static struct spi_driver wm8776_spi_driver = {
483924914eeSMark Brown .driver = {
4841e3ad571SMark Brown .name = "wm8776",
485b6de4315SMark Brown .of_match_table = wm8776_of_match,
486924914eeSMark Brown },
487924914eeSMark Brown .probe = wm8776_spi_probe,
488924914eeSMark Brown };
489924914eeSMark Brown #endif /* CONFIG_SPI_MASTER */
490924914eeSMark Brown
4911769267bSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
wm8776_i2c_probe(struct i2c_client * i2c)49297b0b6e3SStephen Kitt static int wm8776_i2c_probe(struct i2c_client *i2c)
493924914eeSMark Brown {
494924914eeSMark Brown struct wm8776_priv *wm8776;
495f0fba2adSLiam Girdwood int ret;
496924914eeSMark Brown
497bf97ca9aSMark Brown wm8776 = devm_kzalloc(&i2c->dev, sizeof(struct wm8776_priv),
498bf97ca9aSMark Brown GFP_KERNEL);
499924914eeSMark Brown if (wm8776 == NULL)
500924914eeSMark Brown return -ENOMEM;
501924914eeSMark Brown
5022cbde7abSMark Brown wm8776->regmap = devm_regmap_init_i2c(i2c, &wm8776_regmap);
5032cbde7abSMark Brown if (IS_ERR(wm8776->regmap))
5042cbde7abSMark Brown return PTR_ERR(wm8776->regmap);
5052cbde7abSMark Brown
506924914eeSMark Brown i2c_set_clientdata(i2c, wm8776);
507924914eeSMark Brown
50851e42c95SKuninori Morimoto ret = devm_snd_soc_register_component(&i2c->dev,
50951e42c95SKuninori Morimoto &soc_component_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
510bf97ca9aSMark Brown
511f0fba2adSLiam Girdwood return ret;
512924914eeSMark Brown }
513924914eeSMark Brown
514924914eeSMark Brown static const struct i2c_device_id wm8776_i2c_id[] = {
515fdd48f9dSMark Brown { "wm8775", WM8775 },
516fdd48f9dSMark Brown { "wm8776", WM8776 },
517924914eeSMark Brown { }
518924914eeSMark Brown };
519924914eeSMark Brown MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
520924914eeSMark Brown
521924914eeSMark Brown static struct i2c_driver wm8776_i2c_driver = {
522924914eeSMark Brown .driver = {
5231e3ad571SMark Brown .name = "wm8776",
524b6de4315SMark Brown .of_match_table = wm8776_of_match,
525924914eeSMark Brown },
5269abcd240SUwe Kleine-König .probe = wm8776_i2c_probe,
527924914eeSMark Brown .id_table = wm8776_i2c_id,
528924914eeSMark Brown };
529924914eeSMark Brown #endif
530924914eeSMark Brown
wm8776_modinit(void)531924914eeSMark Brown static int __init wm8776_modinit(void)
532924914eeSMark Brown {
533f0fba2adSLiam Girdwood int ret = 0;
5341769267bSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
535924914eeSMark Brown ret = i2c_add_driver(&wm8776_i2c_driver);
536924914eeSMark Brown if (ret != 0) {
537f0fba2adSLiam Girdwood printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
538924914eeSMark Brown ret);
539924914eeSMark Brown }
540924914eeSMark Brown #endif
541924914eeSMark Brown #if defined(CONFIG_SPI_MASTER)
542924914eeSMark Brown ret = spi_register_driver(&wm8776_spi_driver);
543924914eeSMark Brown if (ret != 0) {
544f0fba2adSLiam Girdwood printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
545924914eeSMark Brown ret);
546924914eeSMark Brown }
547924914eeSMark Brown #endif
548f0fba2adSLiam Girdwood return ret;
549924914eeSMark Brown }
550924914eeSMark Brown module_init(wm8776_modinit);
551924914eeSMark Brown
wm8776_exit(void)552924914eeSMark Brown static void __exit wm8776_exit(void)
553924914eeSMark Brown {
5541769267bSFabio Estevam #if IS_ENABLED(CONFIG_I2C)
555924914eeSMark Brown i2c_del_driver(&wm8776_i2c_driver);
556924914eeSMark Brown #endif
557924914eeSMark Brown #if defined(CONFIG_SPI_MASTER)
558924914eeSMark Brown spi_unregister_driver(&wm8776_spi_driver);
559924914eeSMark Brown #endif
560924914eeSMark Brown }
561924914eeSMark Brown module_exit(wm8776_exit);
562924914eeSMark Brown
563924914eeSMark Brown MODULE_DESCRIPTION("ASoC WM8776 driver");
564924914eeSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
565924914eeSMark Brown MODULE_LICENSE("GPL");
566