xref: /linux/sound/soc/codecs/wm8985.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26d6f8b83SDimitris Papastamos /*
3811e66deSPetr Kulhavy  * wm8985.c  --  WM8985 / WM8758 ALSA SoC Audio driver
46d6f8b83SDimitris Papastamos  *
56d6f8b83SDimitris Papastamos  * Copyright 2010 Wolfson Microelectronics plc
66d6f8b83SDimitris Papastamos  * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
76d6f8b83SDimitris Papastamos  *
8811e66deSPetr Kulhavy  * WM8758 support:
9811e66deSPetr Kulhavy  * Copyright: 2016 Barix AG
10811e66deSPetr Kulhavy  * Author: Petr Kulhavy <petr@barix.com>
11811e66deSPetr Kulhavy  *
126d6f8b83SDimitris Papastamos  * TODO:
136d6f8b83SDimitris Papastamos  *  o Add OUT3/OUT4 mixer controls.
146d6f8b83SDimitris Papastamos  */
156d6f8b83SDimitris Papastamos 
166d6f8b83SDimitris Papastamos #include <linux/module.h>
176d6f8b83SDimitris Papastamos #include <linux/moduleparam.h>
186d6f8b83SDimitris Papastamos #include <linux/init.h>
196d6f8b83SDimitris Papastamos #include <linux/delay.h>
206d6f8b83SDimitris Papastamos #include <linux/pm.h>
216d6f8b83SDimitris Papastamos #include <linux/i2c.h>
22411a3450SMark Brown #include <linux/regmap.h>
236d6f8b83SDimitris Papastamos #include <linux/regulator/consumer.h>
246d6f8b83SDimitris Papastamos #include <linux/spi/spi.h>
256d6f8b83SDimitris Papastamos #include <linux/slab.h>
266d6f8b83SDimitris Papastamos #include <sound/core.h>
276d6f8b83SDimitris Papastamos #include <sound/pcm.h>
286d6f8b83SDimitris Papastamos #include <sound/pcm_params.h>
296d6f8b83SDimitris Papastamos #include <sound/soc.h>
306d6f8b83SDimitris Papastamos #include <sound/initval.h>
316d6f8b83SDimitris Papastamos #include <sound/tlv.h>
326d6f8b83SDimitris Papastamos 
336d6f8b83SDimitris Papastamos #include "wm8985.h"
346d6f8b83SDimitris Papastamos 
356d6f8b83SDimitris Papastamos #define WM8985_NUM_SUPPLIES 4
366d6f8b83SDimitris Papastamos static const char *wm8985_supply_names[WM8985_NUM_SUPPLIES] = {
376d6f8b83SDimitris Papastamos 	"DCVDD",
386d6f8b83SDimitris Papastamos 	"DBVDD",
396d6f8b83SDimitris Papastamos 	"AVDD1",
406d6f8b83SDimitris Papastamos 	"AVDD2"
416d6f8b83SDimitris Papastamos };
426d6f8b83SDimitris Papastamos 
43811e66deSPetr Kulhavy enum wm8985_type {
44811e66deSPetr Kulhavy 	WM8985,
45811e66deSPetr Kulhavy 	WM8758,
46811e66deSPetr Kulhavy };
47811e66deSPetr Kulhavy 
48411a3450SMark Brown static const struct reg_default wm8985_reg_defaults[] = {
49411a3450SMark Brown 	{ 1,  0x0000 },     /* R1  - Power management 1 */
50411a3450SMark Brown 	{ 2,  0x0000 },     /* R2  - Power management 2 */
51411a3450SMark Brown 	{ 3,  0x0000 },     /* R3  - Power management 3 */
52411a3450SMark Brown 	{ 4,  0x0050 },     /* R4  - Audio Interface */
53411a3450SMark Brown 	{ 5,  0x0000 },     /* R5  - Companding control */
54411a3450SMark Brown 	{ 6,  0x0140 },     /* R6  - Clock Gen control */
55411a3450SMark Brown 	{ 7,  0x0000 },     /* R7  - Additional control */
56411a3450SMark Brown 	{ 8,  0x0000 },     /* R8  - GPIO Control */
57411a3450SMark Brown 	{ 9,  0x0000 },     /* R9  - Jack Detect Control 1 */
58411a3450SMark Brown 	{ 10, 0x0000 },     /* R10 - DAC Control */
59411a3450SMark Brown 	{ 11, 0x00FF },     /* R11 - Left DAC digital Vol */
60411a3450SMark Brown 	{ 12, 0x00FF },     /* R12 - Right DAC digital vol */
61411a3450SMark Brown 	{ 13, 0x0000 },     /* R13 - Jack Detect Control 2 */
62411a3450SMark Brown 	{ 14, 0x0100 },     /* R14 - ADC Control */
63411a3450SMark Brown 	{ 15, 0x00FF },     /* R15 - Left ADC Digital Vol */
64411a3450SMark Brown 	{ 16, 0x00FF },     /* R16 - Right ADC Digital Vol */
65411a3450SMark Brown 	{ 18, 0x012C },     /* R18 - EQ1 - low shelf */
66411a3450SMark Brown 	{ 19, 0x002C },     /* R19 - EQ2 - peak 1 */
67411a3450SMark Brown 	{ 20, 0x002C },     /* R20 - EQ3 - peak 2 */
68411a3450SMark Brown 	{ 21, 0x002C },     /* R21 - EQ4 - peak 3 */
69411a3450SMark Brown 	{ 22, 0x002C },     /* R22 - EQ5 - high shelf */
70411a3450SMark Brown 	{ 24, 0x0032 },     /* R24 - DAC Limiter 1 */
71411a3450SMark Brown 	{ 25, 0x0000 },     /* R25 - DAC Limiter 2 */
72411a3450SMark Brown 	{ 27, 0x0000 },     /* R27 - Notch Filter 1 */
73411a3450SMark Brown 	{ 28, 0x0000 },     /* R28 - Notch Filter 2 */
74411a3450SMark Brown 	{ 29, 0x0000 },     /* R29 - Notch Filter 3 */
75411a3450SMark Brown 	{ 30, 0x0000 },     /* R30 - Notch Filter 4 */
76411a3450SMark Brown 	{ 32, 0x0038 },     /* R32 - ALC control 1 */
77411a3450SMark Brown 	{ 33, 0x000B },     /* R33 - ALC control 2 */
78411a3450SMark Brown 	{ 34, 0x0032 },     /* R34 - ALC control 3 */
79411a3450SMark Brown 	{ 35, 0x0000 },     /* R35 - Noise Gate */
80411a3450SMark Brown 	{ 36, 0x0008 },     /* R36 - PLL N */
81411a3450SMark Brown 	{ 37, 0x000C },     /* R37 - PLL K 1 */
82411a3450SMark Brown 	{ 38, 0x0093 },     /* R38 - PLL K 2 */
83411a3450SMark Brown 	{ 39, 0x00E9 },     /* R39 - PLL K 3 */
84411a3450SMark Brown 	{ 41, 0x0000 },     /* R41 - 3D control */
85411a3450SMark Brown 	{ 42, 0x0000 },     /* R42 - OUT4 to ADC */
86411a3450SMark Brown 	{ 43, 0x0000 },     /* R43 - Beep control */
87411a3450SMark Brown 	{ 44, 0x0033 },     /* R44 - Input ctrl */
88411a3450SMark Brown 	{ 45, 0x0010 },     /* R45 - Left INP PGA gain ctrl */
89411a3450SMark Brown 	{ 46, 0x0010 },     /* R46 - Right INP PGA gain ctrl */
90411a3450SMark Brown 	{ 47, 0x0100 },     /* R47 - Left ADC BOOST ctrl */
91411a3450SMark Brown 	{ 48, 0x0100 },     /* R48 - Right ADC BOOST ctrl */
92411a3450SMark Brown 	{ 49, 0x0002 },     /* R49 - Output ctrl */
93411a3450SMark Brown 	{ 50, 0x0001 },     /* R50 - Left mixer ctrl */
94411a3450SMark Brown 	{ 51, 0x0001 },     /* R51 - Right mixer ctrl */
95411a3450SMark Brown 	{ 52, 0x0039 },     /* R52 - LOUT1 (HP) volume ctrl */
96411a3450SMark Brown 	{ 53, 0x0039 },     /* R53 - ROUT1 (HP) volume ctrl */
97411a3450SMark Brown 	{ 54, 0x0039 },     /* R54 - LOUT2 (SPK) volume ctrl */
98411a3450SMark Brown 	{ 55, 0x0039 },     /* R55 - ROUT2 (SPK) volume ctrl */
99411a3450SMark Brown 	{ 56, 0x0001 },     /* R56 - OUT3 mixer ctrl */
100411a3450SMark Brown 	{ 57, 0x0001 },     /* R57 - OUT4 (MONO) mix ctrl */
101411a3450SMark Brown 	{ 60, 0x0004 },     /* R60 - OUTPUT ctrl */
102411a3450SMark Brown 	{ 61, 0x0000 },     /* R61 - BIAS CTRL */
1036d6f8b83SDimitris Papastamos };
1046d6f8b83SDimitris Papastamos 
105411a3450SMark Brown static bool wm8985_writeable(struct device *dev, unsigned int reg)
106411a3450SMark Brown {
107411a3450SMark Brown 	switch (reg) {
108411a3450SMark Brown 	case WM8985_SOFTWARE_RESET:
109411a3450SMark Brown 	case WM8985_POWER_MANAGEMENT_1:
110411a3450SMark Brown 	case WM8985_POWER_MANAGEMENT_2:
111411a3450SMark Brown 	case WM8985_POWER_MANAGEMENT_3:
112411a3450SMark Brown 	case WM8985_AUDIO_INTERFACE:
113411a3450SMark Brown 	case WM8985_COMPANDING_CONTROL:
114411a3450SMark Brown 	case WM8985_CLOCK_GEN_CONTROL:
115411a3450SMark Brown 	case WM8985_ADDITIONAL_CONTROL:
116411a3450SMark Brown 	case WM8985_GPIO_CONTROL:
117411a3450SMark Brown 	case WM8985_JACK_DETECT_CONTROL_1:
118411a3450SMark Brown 	case WM8985_DAC_CONTROL:
119411a3450SMark Brown 	case WM8985_LEFT_DAC_DIGITAL_VOL:
120411a3450SMark Brown 	case WM8985_RIGHT_DAC_DIGITAL_VOL:
121411a3450SMark Brown 	case WM8985_JACK_DETECT_CONTROL_2:
122411a3450SMark Brown 	case WM8985_ADC_CONTROL:
123411a3450SMark Brown 	case WM8985_LEFT_ADC_DIGITAL_VOL:
124411a3450SMark Brown 	case WM8985_RIGHT_ADC_DIGITAL_VOL:
125411a3450SMark Brown 	case WM8985_EQ1_LOW_SHELF:
126411a3450SMark Brown 	case WM8985_EQ2_PEAK_1:
127411a3450SMark Brown 	case WM8985_EQ3_PEAK_2:
128411a3450SMark Brown 	case WM8985_EQ4_PEAK_3:
129411a3450SMark Brown 	case WM8985_EQ5_HIGH_SHELF:
130411a3450SMark Brown 	case WM8985_DAC_LIMITER_1:
131411a3450SMark Brown 	case WM8985_DAC_LIMITER_2:
132411a3450SMark Brown 	case WM8985_NOTCH_FILTER_1:
133411a3450SMark Brown 	case WM8985_NOTCH_FILTER_2:
134411a3450SMark Brown 	case WM8985_NOTCH_FILTER_3:
135411a3450SMark Brown 	case WM8985_NOTCH_FILTER_4:
136411a3450SMark Brown 	case WM8985_ALC_CONTROL_1:
137411a3450SMark Brown 	case WM8985_ALC_CONTROL_2:
138411a3450SMark Brown 	case WM8985_ALC_CONTROL_3:
139411a3450SMark Brown 	case WM8985_NOISE_GATE:
140411a3450SMark Brown 	case WM8985_PLL_N:
141411a3450SMark Brown 	case WM8985_PLL_K_1:
142411a3450SMark Brown 	case WM8985_PLL_K_2:
143411a3450SMark Brown 	case WM8985_PLL_K_3:
144411a3450SMark Brown 	case WM8985_3D_CONTROL:
145411a3450SMark Brown 	case WM8985_OUT4_TO_ADC:
146411a3450SMark Brown 	case WM8985_BEEP_CONTROL:
147411a3450SMark Brown 	case WM8985_INPUT_CTRL:
148411a3450SMark Brown 	case WM8985_LEFT_INP_PGA_GAIN_CTRL:
149411a3450SMark Brown 	case WM8985_RIGHT_INP_PGA_GAIN_CTRL:
150411a3450SMark Brown 	case WM8985_LEFT_ADC_BOOST_CTRL:
151411a3450SMark Brown 	case WM8985_RIGHT_ADC_BOOST_CTRL:
152411a3450SMark Brown 	case WM8985_OUTPUT_CTRL0:
153411a3450SMark Brown 	case WM8985_LEFT_MIXER_CTRL:
154411a3450SMark Brown 	case WM8985_RIGHT_MIXER_CTRL:
155411a3450SMark Brown 	case WM8985_LOUT1_HP_VOLUME_CTRL:
156411a3450SMark Brown 	case WM8985_ROUT1_HP_VOLUME_CTRL:
157411a3450SMark Brown 	case WM8985_LOUT2_SPK_VOLUME_CTRL:
158411a3450SMark Brown 	case WM8985_ROUT2_SPK_VOLUME_CTRL:
159411a3450SMark Brown 	case WM8985_OUT3_MIXER_CTRL:
160411a3450SMark Brown 	case WM8985_OUT4_MONO_MIX_CTRL:
161411a3450SMark Brown 	case WM8985_OUTPUT_CTRL1:
162411a3450SMark Brown 	case WM8985_BIAS_CTRL:
163411a3450SMark Brown 		return true;
164411a3450SMark Brown 	default:
165411a3450SMark Brown 		return false;
166411a3450SMark Brown 	}
167411a3450SMark Brown }
168411a3450SMark Brown 
1696d6f8b83SDimitris Papastamos /*
1706d6f8b83SDimitris Papastamos  * latch bit 8 of these registers to ensure instant
1716d6f8b83SDimitris Papastamos  * volume updates
1726d6f8b83SDimitris Papastamos  */
1736d6f8b83SDimitris Papastamos static const int volume_update_regs[] = {
1746d6f8b83SDimitris Papastamos 	WM8985_LEFT_DAC_DIGITAL_VOL,
1756d6f8b83SDimitris Papastamos 	WM8985_RIGHT_DAC_DIGITAL_VOL,
1766d6f8b83SDimitris Papastamos 	WM8985_LEFT_ADC_DIGITAL_VOL,
1776d6f8b83SDimitris Papastamos 	WM8985_RIGHT_ADC_DIGITAL_VOL,
1786d6f8b83SDimitris Papastamos 	WM8985_LOUT2_SPK_VOLUME_CTRL,
1796d6f8b83SDimitris Papastamos 	WM8985_ROUT2_SPK_VOLUME_CTRL,
1806d6f8b83SDimitris Papastamos 	WM8985_LOUT1_HP_VOLUME_CTRL,
1816d6f8b83SDimitris Papastamos 	WM8985_ROUT1_HP_VOLUME_CTRL,
1826d6f8b83SDimitris Papastamos 	WM8985_LEFT_INP_PGA_GAIN_CTRL,
1836d6f8b83SDimitris Papastamos 	WM8985_RIGHT_INP_PGA_GAIN_CTRL
1846d6f8b83SDimitris Papastamos };
1856d6f8b83SDimitris Papastamos 
1866d6f8b83SDimitris Papastamos struct wm8985_priv {
187411a3450SMark Brown 	struct regmap *regmap;
1886d6f8b83SDimitris Papastamos 	struct regulator_bulk_data supplies[WM8985_NUM_SUPPLIES];
189811e66deSPetr Kulhavy 	enum wm8985_type dev_type;
1906d6f8b83SDimitris Papastamos 	unsigned int sysclk;
1916d6f8b83SDimitris Papastamos 	unsigned int bclk;
1926d6f8b83SDimitris Papastamos };
1936d6f8b83SDimitris Papastamos 
1946d6f8b83SDimitris Papastamos static const struct {
1956d6f8b83SDimitris Papastamos 	int div;
1966d6f8b83SDimitris Papastamos 	int ratio;
1976d6f8b83SDimitris Papastamos } fs_ratios[] = {
1986d6f8b83SDimitris Papastamos 	{ 10, 128 },
1996d6f8b83SDimitris Papastamos 	{ 15, 192 },
2006d6f8b83SDimitris Papastamos 	{ 20, 256 },
2016d6f8b83SDimitris Papastamos 	{ 30, 384 },
2026d6f8b83SDimitris Papastamos 	{ 40, 512 },
2036d6f8b83SDimitris Papastamos 	{ 60, 768 },
2046d6f8b83SDimitris Papastamos 	{ 80, 1024 },
2056d6f8b83SDimitris Papastamos 	{ 120, 1536 }
2066d6f8b83SDimitris Papastamos };
2076d6f8b83SDimitris Papastamos 
2086d6f8b83SDimitris Papastamos static const int srates[] = { 48000, 32000, 24000, 16000, 12000, 8000 };
2096d6f8b83SDimitris Papastamos 
2106d6f8b83SDimitris Papastamos static const int bclk_divs[] = {
2116d6f8b83SDimitris Papastamos 	1, 2, 4, 8, 16, 32
2126d6f8b83SDimitris Papastamos };
2136d6f8b83SDimitris Papastamos 
2146d6f8b83SDimitris Papastamos static int eqmode_get(struct snd_kcontrol *kcontrol,
2156d6f8b83SDimitris Papastamos 		      struct snd_ctl_elem_value *ucontrol);
2166d6f8b83SDimitris Papastamos static int eqmode_put(struct snd_kcontrol *kcontrol,
2176d6f8b83SDimitris Papastamos 		      struct snd_ctl_elem_value *ucontrol);
2186d6f8b83SDimitris Papastamos 
2196d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1);
2206d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(adc_tlv, -12700, 50, 1);
2216d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
2226d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(lim_thresh_tlv, -600, 100, 0);
2236d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(lim_boost_tlv, 0, 100, 0);
2246d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(alc_min_tlv, -1200, 600, 0);
2256d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -675, 600, 0);
2266d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(alc_tar_tlv, -2250, 150, 0);
2276d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(pga_vol_tlv, -1200, 75, 0);
2286d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(boost_tlv, -1200, 300, 1);
2296d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
2306d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(aux_tlv, -1500, 300, 0);
2316d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
2326d6f8b83SDimitris Papastamos static const DECLARE_TLV_DB_SCALE(pga_boost_tlv, 0, 2000, 0);
2336d6f8b83SDimitris Papastamos 
2346d6f8b83SDimitris Papastamos static const char *alc_sel_text[] = { "Off", "Right", "Left", "Stereo" };
235d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_sel, WM8985_ALC_CONTROL_1, 7, alc_sel_text);
2366d6f8b83SDimitris Papastamos 
2376d6f8b83SDimitris Papastamos static const char *alc_mode_text[] = { "ALC", "Limiter" };
238d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(alc_mode, WM8985_ALC_CONTROL_3, 8, alc_mode_text);
2396d6f8b83SDimitris Papastamos 
2406d6f8b83SDimitris Papastamos static const char *filter_mode_text[] = { "Audio", "Application" };
241d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(filter_mode, WM8985_ADC_CONTROL, 7,
2426d6f8b83SDimitris Papastamos 			    filter_mode_text);
2436d6f8b83SDimitris Papastamos 
2446d6f8b83SDimitris Papastamos static const char *eq_bw_text[] = { "Narrow", "Wide" };
2456d6f8b83SDimitris Papastamos static const char *eqmode_text[] = { "Capture", "Playback" };
246d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_EXT_DECL(eqmode, eqmode_text);
247d1454e6fSDimitris Papastamos 
2486d6f8b83SDimitris Papastamos static const char *eq1_cutoff_text[] = {
2496d6f8b83SDimitris Papastamos 	"80Hz", "105Hz", "135Hz", "175Hz"
2506d6f8b83SDimitris Papastamos };
251d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq1_cutoff, WM8985_EQ1_LOW_SHELF, 5,
2526d6f8b83SDimitris Papastamos 			    eq1_cutoff_text);
2536d6f8b83SDimitris Papastamos static const char *eq2_cutoff_text[] = {
2546d6f8b83SDimitris Papastamos 	"230Hz", "300Hz", "385Hz", "500Hz"
2556d6f8b83SDimitris Papastamos };
256d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq2_bw, WM8985_EQ2_PEAK_1, 8, eq_bw_text);
257d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq2_cutoff, WM8985_EQ2_PEAK_1, 5, eq2_cutoff_text);
2586d6f8b83SDimitris Papastamos static const char *eq3_cutoff_text[] = {
2596d6f8b83SDimitris Papastamos 	"650Hz", "850Hz", "1.1kHz", "1.4kHz"
2606d6f8b83SDimitris Papastamos };
261d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq3_bw, WM8985_EQ3_PEAK_2, 8, eq_bw_text);
262d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq3_cutoff, WM8985_EQ3_PEAK_2, 5,
2636d6f8b83SDimitris Papastamos 			    eq3_cutoff_text);
2646d6f8b83SDimitris Papastamos static const char *eq4_cutoff_text[] = {
2656d6f8b83SDimitris Papastamos 	"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"
2666d6f8b83SDimitris Papastamos };
267d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq4_bw, WM8985_EQ4_PEAK_3, 8, eq_bw_text);
268d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq4_cutoff, WM8985_EQ4_PEAK_3, 5, eq4_cutoff_text);
2696d6f8b83SDimitris Papastamos static const char *eq5_cutoff_text[] = {
2706d6f8b83SDimitris Papastamos 	"5.3kHz", "6.9kHz", "9kHz", "11.7kHz"
2716d6f8b83SDimitris Papastamos };
272d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(eq5_cutoff, WM8985_EQ5_HIGH_SHELF, 5,
2736d6f8b83SDimitris Papastamos 				  eq5_cutoff_text);
2746d6f8b83SDimitris Papastamos 
2756d6f8b83SDimitris Papastamos static const char *speaker_mode_text[] = { "Class A/B", "Class D" };
276d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(speaker_mode, 0x17, 8, speaker_mode_text);
2776d6f8b83SDimitris Papastamos 
2786d6f8b83SDimitris Papastamos static const char *depth_3d_text[] = {
2796d6f8b83SDimitris Papastamos 	"Off",
2806d6f8b83SDimitris Papastamos 	"6.67%",
2816d6f8b83SDimitris Papastamos 	"13.3%",
2826d6f8b83SDimitris Papastamos 	"20%",
2836d6f8b83SDimitris Papastamos 	"26.7%",
2846d6f8b83SDimitris Papastamos 	"33.3%",
2856d6f8b83SDimitris Papastamos 	"40%",
2866d6f8b83SDimitris Papastamos 	"46.6%",
2876d6f8b83SDimitris Papastamos 	"53.3%",
2886d6f8b83SDimitris Papastamos 	"60%",
2896d6f8b83SDimitris Papastamos 	"66.7%",
2906d6f8b83SDimitris Papastamos 	"73.3%",
2916d6f8b83SDimitris Papastamos 	"80%",
2926d6f8b83SDimitris Papastamos 	"86.7%",
2936d6f8b83SDimitris Papastamos 	"93.3%",
2946d6f8b83SDimitris Papastamos 	"100%"
2956d6f8b83SDimitris Papastamos };
296d0a4eec1STakashi Iwai static SOC_ENUM_SINGLE_DECL(depth_3d, WM8985_3D_CONTROL, 0, depth_3d_text);
2976d6f8b83SDimitris Papastamos 
298811e66deSPetr Kulhavy static const struct snd_kcontrol_new wm8985_common_snd_controls[] = {
2996d6f8b83SDimitris Papastamos 	SOC_SINGLE("Digital Loopback Switch", WM8985_COMPANDING_CONTROL,
3006d6f8b83SDimitris Papastamos 		0, 1, 0),
3016d6f8b83SDimitris Papastamos 
3026d6f8b83SDimitris Papastamos 	SOC_ENUM("ALC Capture Function", alc_sel),
3036d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("ALC Capture Max Volume", WM8985_ALC_CONTROL_1,
3046d6f8b83SDimitris Papastamos 		3, 7, 0, alc_max_tlv),
3056d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("ALC Capture Min Volume", WM8985_ALC_CONTROL_1,
3066d6f8b83SDimitris Papastamos 		0, 7, 0, alc_min_tlv),
3076d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("ALC Capture Target Volume", WM8985_ALC_CONTROL_2,
3086d6f8b83SDimitris Papastamos 		0, 15, 0, alc_tar_tlv),
3096d6f8b83SDimitris Papastamos 	SOC_SINGLE("ALC Capture Attack", WM8985_ALC_CONTROL_3, 0, 10, 0),
3106d6f8b83SDimitris Papastamos 	SOC_SINGLE("ALC Capture Hold", WM8985_ALC_CONTROL_2, 4, 10, 0),
3116d6f8b83SDimitris Papastamos 	SOC_SINGLE("ALC Capture Decay", WM8985_ALC_CONTROL_3, 4, 10, 0),
3126d6f8b83SDimitris Papastamos 	SOC_ENUM("ALC Mode", alc_mode),
3136d6f8b83SDimitris Papastamos 	SOC_SINGLE("ALC Capture NG Switch", WM8985_NOISE_GATE,
3146d6f8b83SDimitris Papastamos 		3, 1, 0),
3156d6f8b83SDimitris Papastamos 	SOC_SINGLE("ALC Capture NG Threshold", WM8985_NOISE_GATE,
3166d6f8b83SDimitris Papastamos 		0, 7, 1),
3176d6f8b83SDimitris Papastamos 
3186d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Capture Volume", WM8985_LEFT_ADC_DIGITAL_VOL,
3196d6f8b83SDimitris Papastamos 		WM8985_RIGHT_ADC_DIGITAL_VOL, 0, 255, 0, adc_tlv),
3206d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R("Capture PGA ZC Switch", WM8985_LEFT_INP_PGA_GAIN_CTRL,
3216d6f8b83SDimitris Papastamos 		WM8985_RIGHT_INP_PGA_GAIN_CTRL, 7, 1, 0),
3226d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Capture PGA Volume", WM8985_LEFT_INP_PGA_GAIN_CTRL,
3236d6f8b83SDimitris Papastamos 		WM8985_RIGHT_INP_PGA_GAIN_CTRL, 0, 63, 0, pga_vol_tlv),
3246d6f8b83SDimitris Papastamos 
3256d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Capture PGA Boost Volume",
3266d6f8b83SDimitris Papastamos 		WM8985_LEFT_ADC_BOOST_CTRL, WM8985_RIGHT_ADC_BOOST_CTRL,
3276d6f8b83SDimitris Papastamos 		8, 1, 0, pga_boost_tlv),
3286d6f8b83SDimitris Papastamos 
3296d6f8b83SDimitris Papastamos 	SOC_DOUBLE("ADC Inversion Switch", WM8985_ADC_CONTROL, 0, 1, 1, 0),
3306d6f8b83SDimitris Papastamos 	SOC_SINGLE("ADC 128x Oversampling Switch", WM8985_ADC_CONTROL, 8, 1, 0),
3316d6f8b83SDimitris Papastamos 
3326d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Playback Volume", WM8985_LEFT_DAC_DIGITAL_VOL,
3336d6f8b83SDimitris Papastamos 		WM8985_RIGHT_DAC_DIGITAL_VOL, 0, 255, 0, dac_tlv),
3346d6f8b83SDimitris Papastamos 
3356d6f8b83SDimitris Papastamos 	SOC_SINGLE("DAC Playback Limiter Switch", WM8985_DAC_LIMITER_1, 8, 1, 0),
3366d6f8b83SDimitris Papastamos 	SOC_SINGLE("DAC Playback Limiter Decay", WM8985_DAC_LIMITER_1, 4, 10, 0),
3376d6f8b83SDimitris Papastamos 	SOC_SINGLE("DAC Playback Limiter Attack", WM8985_DAC_LIMITER_1, 0, 11, 0),
3386d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8985_DAC_LIMITER_2,
3396d6f8b83SDimitris Papastamos 		4, 7, 1, lim_thresh_tlv),
3406d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("DAC Playback Limiter Boost Volume", WM8985_DAC_LIMITER_2,
3416d6f8b83SDimitris Papastamos 		0, 12, 0, lim_boost_tlv),
3426d6f8b83SDimitris Papastamos 	SOC_DOUBLE("DAC Inversion Switch", WM8985_DAC_CONTROL, 0, 1, 1, 0),
3436d6f8b83SDimitris Papastamos 	SOC_SINGLE("DAC Auto Mute Switch", WM8985_DAC_CONTROL, 2, 1, 0),
3446d6f8b83SDimitris Papastamos 	SOC_SINGLE("DAC 128x Oversampling Switch", WM8985_DAC_CONTROL, 3, 1, 0),
3456d6f8b83SDimitris Papastamos 
3466d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8985_LOUT1_HP_VOLUME_CTRL,
3476d6f8b83SDimitris Papastamos 		WM8985_ROUT1_HP_VOLUME_CTRL, 0, 63, 0, out_tlv),
3486d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8985_LOUT1_HP_VOLUME_CTRL,
3496d6f8b83SDimitris Papastamos 		WM8985_ROUT1_HP_VOLUME_CTRL, 7, 1, 0),
3506d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R("Headphone Switch", WM8985_LOUT1_HP_VOLUME_CTRL,
3516d6f8b83SDimitris Papastamos 		WM8985_ROUT1_HP_VOLUME_CTRL, 6, 1, 1),
3526d6f8b83SDimitris Papastamos 
3536d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8985_LOUT2_SPK_VOLUME_CTRL,
3546d6f8b83SDimitris Papastamos 		WM8985_ROUT2_SPK_VOLUME_CTRL, 0, 63, 0, out_tlv),
3556d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8985_LOUT2_SPK_VOLUME_CTRL,
3566d6f8b83SDimitris Papastamos 		WM8985_ROUT2_SPK_VOLUME_CTRL, 7, 1, 0),
3576d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R("Speaker Switch", WM8985_LOUT2_SPK_VOLUME_CTRL,
3586d6f8b83SDimitris Papastamos 		WM8985_ROUT2_SPK_VOLUME_CTRL, 6, 1, 1),
3596d6f8b83SDimitris Papastamos 
3606d6f8b83SDimitris Papastamos 	SOC_SINGLE("High Pass Filter Switch", WM8985_ADC_CONTROL, 8, 1, 0),
3616d6f8b83SDimitris Papastamos 	SOC_ENUM("High Pass Filter Mode", filter_mode),
3626d6f8b83SDimitris Papastamos 	SOC_SINGLE("High Pass Filter Cutoff", WM8985_ADC_CONTROL, 4, 7, 0),
3636d6f8b83SDimitris Papastamos 
3646d6f8b83SDimitris Papastamos 	SOC_DOUBLE_R_TLV("Input PGA Bypass Volume",
3656d6f8b83SDimitris Papastamos 		WM8985_LEFT_MIXER_CTRL, WM8985_RIGHT_MIXER_CTRL, 2, 7, 0,
3666d6f8b83SDimitris Papastamos 		bypass_tlv),
3676d6f8b83SDimitris Papastamos 
3686d6f8b83SDimitris Papastamos 	SOC_ENUM_EXT("Equalizer Function", eqmode, eqmode_get, eqmode_put),
3696d6f8b83SDimitris Papastamos 	SOC_ENUM("EQ1 Cutoff", eq1_cutoff),
3706d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("EQ1 Volume", WM8985_EQ1_LOW_SHELF,  0, 24, 1, eq_tlv),
371c46d5c04SMasanari Iida 	SOC_ENUM("EQ2 Bandwidth", eq2_bw),
3726d6f8b83SDimitris Papastamos 	SOC_ENUM("EQ2 Cutoff", eq2_cutoff),
3736d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("EQ2 Volume", WM8985_EQ2_PEAK_1, 0, 24, 1, eq_tlv),
374c46d5c04SMasanari Iida 	SOC_ENUM("EQ3 Bandwidth", eq3_bw),
3756d6f8b83SDimitris Papastamos 	SOC_ENUM("EQ3 Cutoff", eq3_cutoff),
3766d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("EQ3 Volume", WM8985_EQ3_PEAK_2, 0, 24, 1, eq_tlv),
377c46d5c04SMasanari Iida 	SOC_ENUM("EQ4 Bandwidth", eq4_bw),
3786d6f8b83SDimitris Papastamos 	SOC_ENUM("EQ4 Cutoff", eq4_cutoff),
3796d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("EQ4 Volume", WM8985_EQ4_PEAK_3, 0, 24, 1, eq_tlv),
3806d6f8b83SDimitris Papastamos 	SOC_ENUM("EQ5 Cutoff", eq5_cutoff),
3816d6f8b83SDimitris Papastamos 	SOC_SINGLE_TLV("EQ5 Volume", WM8985_EQ5_HIGH_SHELF, 0, 24, 1, eq_tlv),
3826d6f8b83SDimitris Papastamos 
3836d6f8b83SDimitris Papastamos 	SOC_ENUM("3D Depth", depth_3d),
384811e66deSPetr Kulhavy };
385811e66deSPetr Kulhavy 
386811e66deSPetr Kulhavy static const struct snd_kcontrol_new wm8985_specific_snd_controls[] = {
387811e66deSPetr Kulhavy 	SOC_DOUBLE_R_TLV("Aux Bypass Volume",
388811e66deSPetr Kulhavy 		WM8985_LEFT_MIXER_CTRL, WM8985_RIGHT_MIXER_CTRL, 6, 7, 0,
389811e66deSPetr Kulhavy 		aux_tlv),
3906d6f8b83SDimitris Papastamos 
3916d6f8b83SDimitris Papastamos 	SOC_ENUM("Speaker Mode", speaker_mode)
3926d6f8b83SDimitris Papastamos };
3936d6f8b83SDimitris Papastamos 
3946d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new left_out_mixer[] = {
3956d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("Line Switch", WM8985_LEFT_MIXER_CTRL, 1, 1, 0),
3966d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("PCM Switch", WM8985_LEFT_MIXER_CTRL, 0, 1, 0),
397811e66deSPetr Kulhavy 
398811e66deSPetr Kulhavy 	/* --- WM8985 only --- */
399811e66deSPetr Kulhavy 	SOC_DAPM_SINGLE("Aux Switch", WM8985_LEFT_MIXER_CTRL, 5, 1, 0),
4006d6f8b83SDimitris Papastamos };
4016d6f8b83SDimitris Papastamos 
4026d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new right_out_mixer[] = {
4036d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("Line Switch", WM8985_RIGHT_MIXER_CTRL, 1, 1, 0),
4046d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("PCM Switch", WM8985_RIGHT_MIXER_CTRL, 0, 1, 0),
405811e66deSPetr Kulhavy 
406811e66deSPetr Kulhavy 	/* --- WM8985 only --- */
407811e66deSPetr Kulhavy 	SOC_DAPM_SINGLE("Aux Switch", WM8985_RIGHT_MIXER_CTRL, 5, 1, 0),
4086d6f8b83SDimitris Papastamos };
4096d6f8b83SDimitris Papastamos 
4106d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new left_input_mixer[] = {
4116d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("L2 Switch", WM8985_INPUT_CTRL, 2, 1, 0),
4126d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("MicN Switch", WM8985_INPUT_CTRL, 1, 1, 0),
4136d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("MicP Switch", WM8985_INPUT_CTRL, 0, 1, 0),
4146d6f8b83SDimitris Papastamos };
4156d6f8b83SDimitris Papastamos 
4166d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new right_input_mixer[] = {
4176d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("R2 Switch", WM8985_INPUT_CTRL, 6, 1, 0),
4186d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("MicN Switch", WM8985_INPUT_CTRL, 5, 1, 0),
4196d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE("MicP Switch", WM8985_INPUT_CTRL, 4, 1, 0),
4206d6f8b83SDimitris Papastamos };
4216d6f8b83SDimitris Papastamos 
4226d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new left_boost_mixer[] = {
4236d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE_TLV("L2 Volume", WM8985_LEFT_ADC_BOOST_CTRL,
4246d6f8b83SDimitris Papastamos 		4, 7, 0, boost_tlv),
425811e66deSPetr Kulhavy 
426811e66deSPetr Kulhavy 	/* --- WM8985 only --- */
4276d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE_TLV("AUXL Volume", WM8985_LEFT_ADC_BOOST_CTRL,
4286d6f8b83SDimitris Papastamos 		0, 7, 0, boost_tlv)
4296d6f8b83SDimitris Papastamos };
4306d6f8b83SDimitris Papastamos 
4316d6f8b83SDimitris Papastamos static const struct snd_kcontrol_new right_boost_mixer[] = {
4326d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE_TLV("R2 Volume", WM8985_RIGHT_ADC_BOOST_CTRL,
4336d6f8b83SDimitris Papastamos 		4, 7, 0, boost_tlv),
434811e66deSPetr Kulhavy 
435811e66deSPetr Kulhavy 	/* --- WM8985 only --- */
4366d6f8b83SDimitris Papastamos 	SOC_DAPM_SINGLE_TLV("AUXR Volume", WM8985_RIGHT_ADC_BOOST_CTRL,
4376d6f8b83SDimitris Papastamos 		0, 7, 0, boost_tlv)
4386d6f8b83SDimitris Papastamos };
4396d6f8b83SDimitris Papastamos 
440811e66deSPetr Kulhavy static const struct snd_soc_dapm_widget wm8985_common_dapm_widgets[] = {
4416d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8985_POWER_MANAGEMENT_3,
4426d6f8b83SDimitris Papastamos 		0, 0),
4436d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8985_POWER_MANAGEMENT_3,
4446d6f8b83SDimitris Papastamos 		1, 0),
4456d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8985_POWER_MANAGEMENT_2,
4466d6f8b83SDimitris Papastamos 		0, 0),
4476d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8985_POWER_MANAGEMENT_2,
4486d6f8b83SDimitris Papastamos 		1, 0),
4496d6f8b83SDimitris Papastamos 
4506d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_MIXER("Left Input Mixer", WM8985_POWER_MANAGEMENT_2,
4516d6f8b83SDimitris Papastamos 		2, 0, left_input_mixer, ARRAY_SIZE(left_input_mixer)),
4526d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_MIXER("Right Input Mixer", WM8985_POWER_MANAGEMENT_2,
4536d6f8b83SDimitris Papastamos 		3, 0, right_input_mixer, ARRAY_SIZE(right_input_mixer)),
4546d6f8b83SDimitris Papastamos 
4556d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Left Capture PGA", WM8985_LEFT_INP_PGA_GAIN_CTRL,
4566d6f8b83SDimitris Papastamos 		6, 1, NULL, 0),
4576d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Right Capture PGA", WM8985_RIGHT_INP_PGA_GAIN_CTRL,
4586d6f8b83SDimitris Papastamos 		6, 1, NULL, 0),
4596d6f8b83SDimitris Papastamos 
4606d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Left Headphone Out", WM8985_POWER_MANAGEMENT_2,
4616d6f8b83SDimitris Papastamos 		7, 0, NULL, 0),
4626d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Right Headphone Out", WM8985_POWER_MANAGEMENT_2,
4636d6f8b83SDimitris Papastamos 		8, 0, NULL, 0),
4646d6f8b83SDimitris Papastamos 
4656d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Left Speaker Out", WM8985_POWER_MANAGEMENT_3,
4666d6f8b83SDimitris Papastamos 		5, 0, NULL, 0),
4676d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_PGA("Right Speaker Out", WM8985_POWER_MANAGEMENT_3,
4686d6f8b83SDimitris Papastamos 		6, 0, NULL, 0),
4696d6f8b83SDimitris Papastamos 
470812f8a35SMark Brown 	SND_SOC_DAPM_SUPPLY("Mic Bias", WM8985_POWER_MANAGEMENT_1, 4, 0,
471812f8a35SMark Brown 			    NULL, 0),
4726d6f8b83SDimitris Papastamos 
4736d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("LIN"),
4746d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("LIP"),
4756d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("RIN"),
4766d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("RIP"),
4776d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("L2"),
4786d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_INPUT("R2"),
4796d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_OUTPUT("HPL"),
4806d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_OUTPUT("HPR"),
4816d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_OUTPUT("SPKL"),
4826d6f8b83SDimitris Papastamos 	SND_SOC_DAPM_OUTPUT("SPKR")
4836d6f8b83SDimitris Papastamos };
4846d6f8b83SDimitris Papastamos 
485811e66deSPetr Kulhavy static const struct snd_soc_dapm_widget wm8985_dapm_widgets[] = {
486811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Left Output Mixer", WM8985_POWER_MANAGEMENT_3,
487811e66deSPetr Kulhavy 		2, 0, left_out_mixer, ARRAY_SIZE(left_out_mixer)),
488811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Right Output Mixer", WM8985_POWER_MANAGEMENT_3,
489811e66deSPetr Kulhavy 		3, 0, right_out_mixer, ARRAY_SIZE(right_out_mixer)),
490811e66deSPetr Kulhavy 
491811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8985_POWER_MANAGEMENT_2,
492811e66deSPetr Kulhavy 		4, 0, left_boost_mixer, ARRAY_SIZE(left_boost_mixer)),
493811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8985_POWER_MANAGEMENT_2,
494811e66deSPetr Kulhavy 		5, 0, right_boost_mixer, ARRAY_SIZE(right_boost_mixer)),
495811e66deSPetr Kulhavy 
496811e66deSPetr Kulhavy 	SND_SOC_DAPM_INPUT("AUXL"),
497811e66deSPetr Kulhavy 	SND_SOC_DAPM_INPUT("AUXR"),
498811e66deSPetr Kulhavy };
499811e66deSPetr Kulhavy 
500811e66deSPetr Kulhavy static const struct snd_soc_dapm_widget wm8758_dapm_widgets[] = {
501811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Left Output Mixer", WM8985_POWER_MANAGEMENT_3,
502811e66deSPetr Kulhavy 		2, 0, left_out_mixer,
503811e66deSPetr Kulhavy 		ARRAY_SIZE(left_out_mixer) - 1),
504811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Right Output Mixer", WM8985_POWER_MANAGEMENT_3,
505811e66deSPetr Kulhavy 		3, 0, right_out_mixer,
506811e66deSPetr Kulhavy 		ARRAY_SIZE(right_out_mixer) - 1),
507811e66deSPetr Kulhavy 
508811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8985_POWER_MANAGEMENT_2,
509811e66deSPetr Kulhavy 		4, 0, left_boost_mixer,
510811e66deSPetr Kulhavy 		ARRAY_SIZE(left_boost_mixer) - 1),
511811e66deSPetr Kulhavy 	SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8985_POWER_MANAGEMENT_2,
512811e66deSPetr Kulhavy 		5, 0, right_boost_mixer,
513811e66deSPetr Kulhavy 		ARRAY_SIZE(right_boost_mixer) - 1),
514811e66deSPetr Kulhavy };
515811e66deSPetr Kulhavy 
516811e66deSPetr Kulhavy static const struct snd_soc_dapm_route wm8985_common_dapm_routes[] = {
5176d6f8b83SDimitris Papastamos 	{ "Right Output Mixer", "PCM Switch", "Right DAC" },
5186d6f8b83SDimitris Papastamos 	{ "Right Output Mixer", "Line Switch", "Right Boost Mixer" },
5196d6f8b83SDimitris Papastamos 
5206d6f8b83SDimitris Papastamos 	{ "Left Output Mixer", "PCM Switch", "Left DAC" },
5216d6f8b83SDimitris Papastamos 	{ "Left Output Mixer", "Line Switch", "Left Boost Mixer" },
5226d6f8b83SDimitris Papastamos 
5236d6f8b83SDimitris Papastamos 	{ "Right Headphone Out", NULL, "Right Output Mixer" },
5246d6f8b83SDimitris Papastamos 	{ "HPR", NULL, "Right Headphone Out" },
5256d6f8b83SDimitris Papastamos 
5266d6f8b83SDimitris Papastamos 	{ "Left Headphone Out", NULL, "Left Output Mixer" },
5276d6f8b83SDimitris Papastamos 	{ "HPL", NULL, "Left Headphone Out" },
5286d6f8b83SDimitris Papastamos 
5296d6f8b83SDimitris Papastamos 	{ "Right Speaker Out", NULL, "Right Output Mixer" },
5306d6f8b83SDimitris Papastamos 	{ "SPKR", NULL, "Right Speaker Out" },
5316d6f8b83SDimitris Papastamos 
5326d6f8b83SDimitris Papastamos 	{ "Left Speaker Out", NULL, "Left Output Mixer" },
5336d6f8b83SDimitris Papastamos 	{ "SPKL", NULL, "Left Speaker Out" },
5346d6f8b83SDimitris Papastamos 
5356d6f8b83SDimitris Papastamos 	{ "Right ADC", NULL, "Right Boost Mixer" },
5366d6f8b83SDimitris Papastamos 
5376d6f8b83SDimitris Papastamos 	{ "Right Boost Mixer", NULL, "Right Capture PGA" },
5386d6f8b83SDimitris Papastamos 	{ "Right Boost Mixer", "R2 Volume", "R2" },
5396d6f8b83SDimitris Papastamos 
5406d6f8b83SDimitris Papastamos 	{ "Left ADC", NULL, "Left Boost Mixer" },
5416d6f8b83SDimitris Papastamos 
5426d6f8b83SDimitris Papastamos 	{ "Left Boost Mixer", NULL, "Left Capture PGA" },
5436d6f8b83SDimitris Papastamos 	{ "Left Boost Mixer", "L2 Volume", "L2" },
5446d6f8b83SDimitris Papastamos 
5456d6f8b83SDimitris Papastamos 	{ "Right Capture PGA", NULL, "Right Input Mixer" },
5466d6f8b83SDimitris Papastamos 	{ "Left Capture PGA", NULL, "Left Input Mixer" },
5476d6f8b83SDimitris Papastamos 
5486d6f8b83SDimitris Papastamos 	{ "Right Input Mixer", "R2 Switch", "R2" },
5496d6f8b83SDimitris Papastamos 	{ "Right Input Mixer", "MicN Switch", "RIN" },
5506d6f8b83SDimitris Papastamos 	{ "Right Input Mixer", "MicP Switch", "RIP" },
5516d6f8b83SDimitris Papastamos 
5526d6f8b83SDimitris Papastamos 	{ "Left Input Mixer", "L2 Switch", "L2" },
5536d6f8b83SDimitris Papastamos 	{ "Left Input Mixer", "MicN Switch", "LIN" },
5546d6f8b83SDimitris Papastamos 	{ "Left Input Mixer", "MicP Switch", "LIP" },
5556d6f8b83SDimitris Papastamos };
556811e66deSPetr Kulhavy static const struct snd_soc_dapm_route wm8985_aux_dapm_routes[] = {
557811e66deSPetr Kulhavy 	{ "Right Output Mixer", "Aux Switch", "AUXR" },
558811e66deSPetr Kulhavy 	{ "Left Output Mixer", "Aux Switch", "AUXL" },
559811e66deSPetr Kulhavy 
560811e66deSPetr Kulhavy 	{ "Right Boost Mixer", "AUXR Volume", "AUXR" },
561811e66deSPetr Kulhavy 	{ "Left Boost Mixer", "AUXL Volume", "AUXL" },
562811e66deSPetr Kulhavy };
563811e66deSPetr Kulhavy 
564cbc23b74SKuninori Morimoto static int wm8985_add_widgets(struct snd_soc_component *component)
565811e66deSPetr Kulhavy {
566cbc23b74SKuninori Morimoto 	struct wm8985_priv *wm8985 = snd_soc_component_get_drvdata(component);
567cbc23b74SKuninori Morimoto 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
568811e66deSPetr Kulhavy 
569811e66deSPetr Kulhavy 	switch (wm8985->dev_type) {
570811e66deSPetr Kulhavy 	case WM8758:
571811e66deSPetr Kulhavy 		snd_soc_dapm_new_controls(dapm, wm8758_dapm_widgets,
572811e66deSPetr Kulhavy 					  ARRAY_SIZE(wm8758_dapm_widgets));
573811e66deSPetr Kulhavy 		break;
574811e66deSPetr Kulhavy 
575811e66deSPetr Kulhavy 	case WM8985:
576cbc23b74SKuninori Morimoto 		snd_soc_add_component_controls(component, wm8985_specific_snd_controls,
577811e66deSPetr Kulhavy 			ARRAY_SIZE(wm8985_specific_snd_controls));
578811e66deSPetr Kulhavy 
579811e66deSPetr Kulhavy 		snd_soc_dapm_new_controls(dapm, wm8985_dapm_widgets,
580811e66deSPetr Kulhavy 			ARRAY_SIZE(wm8985_dapm_widgets));
581811e66deSPetr Kulhavy 		snd_soc_dapm_add_routes(dapm, wm8985_aux_dapm_routes,
582811e66deSPetr Kulhavy 			ARRAY_SIZE(wm8985_aux_dapm_routes));
583811e66deSPetr Kulhavy 		break;
584811e66deSPetr Kulhavy 	}
585811e66deSPetr Kulhavy 
586811e66deSPetr Kulhavy 	return 0;
587811e66deSPetr Kulhavy }
5886d6f8b83SDimitris Papastamos 
5896d6f8b83SDimitris Papastamos static int eqmode_get(struct snd_kcontrol *kcontrol,
5906d6f8b83SDimitris Papastamos 		      struct snd_ctl_elem_value *ucontrol)
5916d6f8b83SDimitris Papastamos {
592cbc23b74SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
5936d6f8b83SDimitris Papastamos 	unsigned int reg;
5946d6f8b83SDimitris Papastamos 
595cbc23b74SKuninori Morimoto 	reg = snd_soc_component_read32(component, WM8985_EQ1_LOW_SHELF);
5966d6f8b83SDimitris Papastamos 	if (reg & WM8985_EQ3DMODE)
597251d6047STakashi Iwai 		ucontrol->value.enumerated.item[0] = 1;
5986d6f8b83SDimitris Papastamos 	else
599251d6047STakashi Iwai 		ucontrol->value.enumerated.item[0] = 0;
6006d6f8b83SDimitris Papastamos 
6016d6f8b83SDimitris Papastamos 	return 0;
6026d6f8b83SDimitris Papastamos }
6036d6f8b83SDimitris Papastamos 
6046d6f8b83SDimitris Papastamos static int eqmode_put(struct snd_kcontrol *kcontrol,
6056d6f8b83SDimitris Papastamos 		      struct snd_ctl_elem_value *ucontrol)
6066d6f8b83SDimitris Papastamos {
607cbc23b74SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
6086d6f8b83SDimitris Papastamos 	unsigned int regpwr2, regpwr3;
6096d6f8b83SDimitris Papastamos 	unsigned int reg_eq;
6106d6f8b83SDimitris Papastamos 
611251d6047STakashi Iwai 	if (ucontrol->value.enumerated.item[0] != 0
612251d6047STakashi Iwai 			&& ucontrol->value.enumerated.item[0] != 1)
6136d6f8b83SDimitris Papastamos 		return -EINVAL;
6146d6f8b83SDimitris Papastamos 
615cbc23b74SKuninori Morimoto 	reg_eq = snd_soc_component_read32(component, WM8985_EQ1_LOW_SHELF);
6166d6f8b83SDimitris Papastamos 	switch ((reg_eq & WM8985_EQ3DMODE) >> WM8985_EQ3DMODE_SHIFT) {
6176d6f8b83SDimitris Papastamos 	case 0:
618251d6047STakashi Iwai 		if (!ucontrol->value.enumerated.item[0])
6196d6f8b83SDimitris Papastamos 			return 0;
6206d6f8b83SDimitris Papastamos 		break;
6216d6f8b83SDimitris Papastamos 	case 1:
622251d6047STakashi Iwai 		if (ucontrol->value.enumerated.item[0])
6236d6f8b83SDimitris Papastamos 			return 0;
6246d6f8b83SDimitris Papastamos 		break;
6256d6f8b83SDimitris Papastamos 	}
6266d6f8b83SDimitris Papastamos 
627cbc23b74SKuninori Morimoto 	regpwr2 = snd_soc_component_read32(component, WM8985_POWER_MANAGEMENT_2);
628cbc23b74SKuninori Morimoto 	regpwr3 = snd_soc_component_read32(component, WM8985_POWER_MANAGEMENT_3);
6296d6f8b83SDimitris Papastamos 	/* disable the DACs and ADCs */
630cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_2,
6316d6f8b83SDimitris Papastamos 			    WM8985_ADCENR_MASK | WM8985_ADCENL_MASK, 0);
632cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_3,
6336d6f8b83SDimitris Papastamos 			    WM8985_DACENR_MASK | WM8985_DACENL_MASK, 0);
634cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_ADDITIONAL_CONTROL,
6356d6f8b83SDimitris Papastamos 			    WM8985_M128ENB_MASK, WM8985_M128ENB);
6366d6f8b83SDimitris Papastamos 	/* set the desired eqmode */
637cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_EQ1_LOW_SHELF,
6386d6f8b83SDimitris Papastamos 			    WM8985_EQ3DMODE_MASK,
639251d6047STakashi Iwai 			    ucontrol->value.enumerated.item[0]
6406d6f8b83SDimitris Papastamos 			    << WM8985_EQ3DMODE_SHIFT);
6416d6f8b83SDimitris Papastamos 	/* restore DAC/ADC configuration */
642cbc23b74SKuninori Morimoto 	snd_soc_component_write(component, WM8985_POWER_MANAGEMENT_2, regpwr2);
643cbc23b74SKuninori Morimoto 	snd_soc_component_write(component, WM8985_POWER_MANAGEMENT_3, regpwr3);
6446d6f8b83SDimitris Papastamos 	return 0;
6456d6f8b83SDimitris Papastamos }
6466d6f8b83SDimitris Papastamos 
647cbc23b74SKuninori Morimoto static int wm8985_reset(struct snd_soc_component *component)
6486d6f8b83SDimitris Papastamos {
649cbc23b74SKuninori Morimoto 	return snd_soc_component_write(component, WM8985_SOFTWARE_RESET, 0x0);
6506d6f8b83SDimitris Papastamos }
6516d6f8b83SDimitris Papastamos 
6526d6f8b83SDimitris Papastamos static int wm8985_dac_mute(struct snd_soc_dai *dai, int mute)
6536d6f8b83SDimitris Papastamos {
654cbc23b74SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
6556d6f8b83SDimitris Papastamos 
656cbc23b74SKuninori Morimoto 	return snd_soc_component_update_bits(component, WM8985_DAC_CONTROL,
6576d6f8b83SDimitris Papastamos 				   WM8985_SOFTMUTE_MASK,
6586d6f8b83SDimitris Papastamos 				   !!mute << WM8985_SOFTMUTE_SHIFT);
6596d6f8b83SDimitris Papastamos }
6606d6f8b83SDimitris Papastamos 
6616d6f8b83SDimitris Papastamos static int wm8985_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
6626d6f8b83SDimitris Papastamos {
663cbc23b74SKuninori Morimoto 	struct snd_soc_component *component;
6646d6f8b83SDimitris Papastamos 	u16 format, master, bcp, lrp;
6656d6f8b83SDimitris Papastamos 
666cbc23b74SKuninori Morimoto 	component = dai->component;
6676d6f8b83SDimitris Papastamos 
6686d6f8b83SDimitris Papastamos 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6696d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_I2S:
6706d6f8b83SDimitris Papastamos 		format = 0x2;
6716d6f8b83SDimitris Papastamos 		break;
6726d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_RIGHT_J:
6736d6f8b83SDimitris Papastamos 		format = 0x0;
6746d6f8b83SDimitris Papastamos 		break;
6756d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_LEFT_J:
6766d6f8b83SDimitris Papastamos 		format = 0x1;
6776d6f8b83SDimitris Papastamos 		break;
6786d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_DSP_A:
6796d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_DSP_B:
6806d6f8b83SDimitris Papastamos 		format = 0x3;
6816d6f8b83SDimitris Papastamos 		break;
6826d6f8b83SDimitris Papastamos 	default:
6836d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unknown dai format\n");
6846d6f8b83SDimitris Papastamos 		return -EINVAL;
6856d6f8b83SDimitris Papastamos 	}
6866d6f8b83SDimitris Papastamos 
687cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_AUDIO_INTERFACE,
6886d6f8b83SDimitris Papastamos 			    WM8985_FMT_MASK, format << WM8985_FMT_SHIFT);
6896d6f8b83SDimitris Papastamos 
6906d6f8b83SDimitris Papastamos 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6916d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_CBM_CFM:
6926d6f8b83SDimitris Papastamos 		master = 1;
6936d6f8b83SDimitris Papastamos 		break;
6946d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_CBS_CFS:
6956d6f8b83SDimitris Papastamos 		master = 0;
6966d6f8b83SDimitris Papastamos 		break;
6976d6f8b83SDimitris Papastamos 	default:
6986d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unknown master/slave configuration\n");
6996d6f8b83SDimitris Papastamos 		return -EINVAL;
7006d6f8b83SDimitris Papastamos 	}
7016d6f8b83SDimitris Papastamos 
702cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
7036d6f8b83SDimitris Papastamos 			    WM8985_MS_MASK, master << WM8985_MS_SHIFT);
7046d6f8b83SDimitris Papastamos 
7056d6f8b83SDimitris Papastamos 	/* frame inversion is not valid for dsp modes */
7066d6f8b83SDimitris Papastamos 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
7076d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_DSP_A:
7086d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_DSP_B:
7096d6f8b83SDimitris Papastamos 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
7106d6f8b83SDimitris Papastamos 		case SND_SOC_DAIFMT_IB_IF:
7116d6f8b83SDimitris Papastamos 		case SND_SOC_DAIFMT_NB_IF:
7126d6f8b83SDimitris Papastamos 			return -EINVAL;
7136d6f8b83SDimitris Papastamos 		default:
7146d6f8b83SDimitris Papastamos 			break;
7156d6f8b83SDimitris Papastamos 		}
7166d6f8b83SDimitris Papastamos 		break;
7176d6f8b83SDimitris Papastamos 	default:
7186d6f8b83SDimitris Papastamos 		break;
7196d6f8b83SDimitris Papastamos 	}
7206d6f8b83SDimitris Papastamos 
7216d6f8b83SDimitris Papastamos 	bcp = lrp = 0;
7226d6f8b83SDimitris Papastamos 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
7236d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_NB_NF:
7246d6f8b83SDimitris Papastamos 		break;
7256d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_IB_IF:
7266d6f8b83SDimitris Papastamos 		bcp = lrp = 1;
7276d6f8b83SDimitris Papastamos 		break;
7286d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_IB_NF:
7296d6f8b83SDimitris Papastamos 		bcp = 1;
7306d6f8b83SDimitris Papastamos 		break;
7316d6f8b83SDimitris Papastamos 	case SND_SOC_DAIFMT_NB_IF:
7326d6f8b83SDimitris Papastamos 		lrp = 1;
7336d6f8b83SDimitris Papastamos 		break;
7346d6f8b83SDimitris Papastamos 	default:
7356d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unknown polarity configuration\n");
7366d6f8b83SDimitris Papastamos 		return -EINVAL;
7376d6f8b83SDimitris Papastamos 	}
7386d6f8b83SDimitris Papastamos 
739cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_AUDIO_INTERFACE,
7406d6f8b83SDimitris Papastamos 			    WM8985_LRP_MASK, lrp << WM8985_LRP_SHIFT);
741cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_AUDIO_INTERFACE,
7426d6f8b83SDimitris Papastamos 			    WM8985_BCP_MASK, bcp << WM8985_BCP_SHIFT);
7436d6f8b83SDimitris Papastamos 	return 0;
7446d6f8b83SDimitris Papastamos }
7456d6f8b83SDimitris Papastamos 
7466d6f8b83SDimitris Papastamos static int wm8985_hw_params(struct snd_pcm_substream *substream,
7476d6f8b83SDimitris Papastamos 			    struct snd_pcm_hw_params *params,
7486d6f8b83SDimitris Papastamos 			    struct snd_soc_dai *dai)
7496d6f8b83SDimitris Papastamos {
750cf1ff501STakashi Iwai 	int i;
751cbc23b74SKuninori Morimoto 	struct snd_soc_component *component;
7526d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
7536d6f8b83SDimitris Papastamos 	u16 blen, srate_idx;
7546d6f8b83SDimitris Papastamos 	unsigned int tmp;
7556d6f8b83SDimitris Papastamos 	int srate_best;
7566d6f8b83SDimitris Papastamos 
757cbc23b74SKuninori Morimoto 	component = dai->component;
758cbc23b74SKuninori Morimoto 	wm8985 = snd_soc_component_get_drvdata(component);
7596d6f8b83SDimitris Papastamos 
7606d6f8b83SDimitris Papastamos 	wm8985->bclk = snd_soc_params_to_bclk(params);
7616d6f8b83SDimitris Papastamos 	if ((int)wm8985->bclk < 0)
7626d6f8b83SDimitris Papastamos 		return wm8985->bclk;
7636d6f8b83SDimitris Papastamos 
764c37642c7SMark Brown 	switch (params_width(params)) {
765c37642c7SMark Brown 	case 16:
7666d6f8b83SDimitris Papastamos 		blen = 0x0;
7676d6f8b83SDimitris Papastamos 		break;
768c37642c7SMark Brown 	case 20:
7696d6f8b83SDimitris Papastamos 		blen = 0x1;
7706d6f8b83SDimitris Papastamos 		break;
771c37642c7SMark Brown 	case 24:
7726d6f8b83SDimitris Papastamos 		blen = 0x2;
7736d6f8b83SDimitris Papastamos 		break;
774c37642c7SMark Brown 	case 32:
7756d6f8b83SDimitris Papastamos 		blen = 0x3;
7766d6f8b83SDimitris Papastamos 		break;
7776d6f8b83SDimitris Papastamos 	default:
7786d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unsupported word length %u\n",
779c37642c7SMark Brown 			params_width(params));
7806d6f8b83SDimitris Papastamos 		return -EINVAL;
7816d6f8b83SDimitris Papastamos 	}
7826d6f8b83SDimitris Papastamos 
783cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_AUDIO_INTERFACE,
7846d6f8b83SDimitris Papastamos 			    WM8985_WL_MASK, blen << WM8985_WL_SHIFT);
7856d6f8b83SDimitris Papastamos 
7866d6f8b83SDimitris Papastamos 	/*
7876d6f8b83SDimitris Papastamos 	 * match to the nearest possible sample rate and rely
7886d6f8b83SDimitris Papastamos 	 * on the array index to configure the SR register
7896d6f8b83SDimitris Papastamos 	 */
7906d6f8b83SDimitris Papastamos 	srate_idx = 0;
7916d6f8b83SDimitris Papastamos 	srate_best = abs(srates[0] - params_rate(params));
7926d6f8b83SDimitris Papastamos 	for (i = 1; i < ARRAY_SIZE(srates); ++i) {
7936d6f8b83SDimitris Papastamos 		if (abs(srates[i] - params_rate(params)) >= srate_best)
7946d6f8b83SDimitris Papastamos 			continue;
7956d6f8b83SDimitris Papastamos 		srate_idx = i;
7966d6f8b83SDimitris Papastamos 		srate_best = abs(srates[i] - params_rate(params));
7976d6f8b83SDimitris Papastamos 	}
7986d6f8b83SDimitris Papastamos 
7996d6f8b83SDimitris Papastamos 	dev_dbg(dai->dev, "Selected SRATE = %d\n", srates[srate_idx]);
800cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_ADDITIONAL_CONTROL,
8016d6f8b83SDimitris Papastamos 			    WM8985_SR_MASK, srate_idx << WM8985_SR_SHIFT);
8026d6f8b83SDimitris Papastamos 
8036d6f8b83SDimitris Papastamos 	dev_dbg(dai->dev, "Target BCLK = %uHz\n", wm8985->bclk);
8046d6f8b83SDimitris Papastamos 	dev_dbg(dai->dev, "SYSCLK = %uHz\n", wm8985->sysclk);
8056d6f8b83SDimitris Papastamos 
8066d6f8b83SDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(fs_ratios); ++i) {
8076d6f8b83SDimitris Papastamos 		if (wm8985->sysclk / params_rate(params)
8086d6f8b83SDimitris Papastamos 				== fs_ratios[i].ratio)
8096d6f8b83SDimitris Papastamos 			break;
8106d6f8b83SDimitris Papastamos 	}
8116d6f8b83SDimitris Papastamos 
8126d6f8b83SDimitris Papastamos 	if (i == ARRAY_SIZE(fs_ratios)) {
8136d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unable to configure MCLK ratio %u/%u\n",
8146d6f8b83SDimitris Papastamos 			wm8985->sysclk, params_rate(params));
8156d6f8b83SDimitris Papastamos 		return -EINVAL;
8166d6f8b83SDimitris Papastamos 	}
8176d6f8b83SDimitris Papastamos 
8186d6f8b83SDimitris Papastamos 	dev_dbg(dai->dev, "MCLK ratio = %dfs\n", fs_ratios[i].ratio);
819cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
8206d6f8b83SDimitris Papastamos 			    WM8985_MCLKDIV_MASK, i << WM8985_MCLKDIV_SHIFT);
8216d6f8b83SDimitris Papastamos 
8226d6f8b83SDimitris Papastamos 	/* select the appropriate bclk divider */
8236d6f8b83SDimitris Papastamos 	tmp = (wm8985->sysclk / fs_ratios[i].div) * 10;
8246d6f8b83SDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(bclk_divs); ++i) {
8256d6f8b83SDimitris Papastamos 		if (wm8985->bclk == tmp / bclk_divs[i])
8266d6f8b83SDimitris Papastamos 			break;
8276d6f8b83SDimitris Papastamos 	}
8286d6f8b83SDimitris Papastamos 
8296d6f8b83SDimitris Papastamos 	if (i == ARRAY_SIZE(bclk_divs)) {
8306d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "No matching BCLK divider found\n");
8316d6f8b83SDimitris Papastamos 		return -EINVAL;
8326d6f8b83SDimitris Papastamos 	}
8336d6f8b83SDimitris Papastamos 
8346d6f8b83SDimitris Papastamos 	dev_dbg(dai->dev, "BCLK div = %d\n", i);
835cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
8366d6f8b83SDimitris Papastamos 			    WM8985_BCLKDIV_MASK, i << WM8985_BCLKDIV_SHIFT);
8376d6f8b83SDimitris Papastamos 	return 0;
8386d6f8b83SDimitris Papastamos }
8396d6f8b83SDimitris Papastamos 
8406d6f8b83SDimitris Papastamos struct pll_div {
8416d6f8b83SDimitris Papastamos 	u32 div2:1;
8426d6f8b83SDimitris Papastamos 	u32 n:4;
8436d6f8b83SDimitris Papastamos 	u32 k:24;
8446d6f8b83SDimitris Papastamos };
8456d6f8b83SDimitris Papastamos 
8466d6f8b83SDimitris Papastamos #define FIXED_PLL_SIZE ((1ULL << 24) * 10)
8476d6f8b83SDimitris Papastamos static int pll_factors(struct pll_div *pll_div, unsigned int target,
8486d6f8b83SDimitris Papastamos 		       unsigned int source)
8496d6f8b83SDimitris Papastamos {
8506d6f8b83SDimitris Papastamos 	u64 Kpart;
8516d6f8b83SDimitris Papastamos 	unsigned long int K, Ndiv, Nmod;
8526d6f8b83SDimitris Papastamos 
8536d6f8b83SDimitris Papastamos 	pll_div->div2 = 0;
8546d6f8b83SDimitris Papastamos 	Ndiv = target / source;
8556d6f8b83SDimitris Papastamos 	if (Ndiv < 6) {
8566d6f8b83SDimitris Papastamos 		source >>= 1;
8576d6f8b83SDimitris Papastamos 		pll_div->div2 = 1;
8586d6f8b83SDimitris Papastamos 		Ndiv = target / source;
8596d6f8b83SDimitris Papastamos 	}
8606d6f8b83SDimitris Papastamos 
8616d6f8b83SDimitris Papastamos 	if (Ndiv < 6 || Ndiv > 12) {
8626d6f8b83SDimitris Papastamos 		printk(KERN_ERR "%s: WM8985 N value is not within"
8636d6f8b83SDimitris Papastamos 		       " the recommended range: %lu\n", __func__, Ndiv);
8646d6f8b83SDimitris Papastamos 		return -EINVAL;
8656d6f8b83SDimitris Papastamos 	}
8666d6f8b83SDimitris Papastamos 	pll_div->n = Ndiv;
8676d6f8b83SDimitris Papastamos 
8686d6f8b83SDimitris Papastamos 	Nmod = target % source;
8696d6f8b83SDimitris Papastamos 	Kpart = FIXED_PLL_SIZE * (u64)Nmod;
8706d6f8b83SDimitris Papastamos 
8716d6f8b83SDimitris Papastamos 	do_div(Kpart, source);
8726d6f8b83SDimitris Papastamos 
8736d6f8b83SDimitris Papastamos 	K = Kpart & 0xffffffff;
8746d6f8b83SDimitris Papastamos 	if ((K % 10) >= 5)
8756d6f8b83SDimitris Papastamos 		K += 5;
8766d6f8b83SDimitris Papastamos 	K /= 10;
8776d6f8b83SDimitris Papastamos 	pll_div->k = K;
8786d6f8b83SDimitris Papastamos 
8796d6f8b83SDimitris Papastamos 	return 0;
8806d6f8b83SDimitris Papastamos }
8816d6f8b83SDimitris Papastamos 
8826d6f8b83SDimitris Papastamos static int wm8985_set_pll(struct snd_soc_dai *dai, int pll_id,
8836d6f8b83SDimitris Papastamos 			  int source, unsigned int freq_in,
8846d6f8b83SDimitris Papastamos 			  unsigned int freq_out)
8856d6f8b83SDimitris Papastamos {
8866d6f8b83SDimitris Papastamos 	int ret;
887cbc23b74SKuninori Morimoto 	struct snd_soc_component *component;
8886d6f8b83SDimitris Papastamos 	struct pll_div pll_div;
8896d6f8b83SDimitris Papastamos 
890cbc23b74SKuninori Morimoto 	component = dai->component;
8915f3d25c0SFabio Estevam 	if (!freq_in || !freq_out) {
8925f3d25c0SFabio Estevam 		/* disable the PLL */
893cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
8945f3d25c0SFabio Estevam 				    WM8985_PLLEN_MASK, 0);
8955f3d25c0SFabio Estevam 	} else {
8966d6f8b83SDimitris Papastamos 		ret = pll_factors(&pll_div, freq_out * 4 * 2, freq_in);
8976d6f8b83SDimitris Papastamos 		if (ret)
8986d6f8b83SDimitris Papastamos 			return ret;
8996d6f8b83SDimitris Papastamos 
9006d6f8b83SDimitris Papastamos 		/* set PLLN and PRESCALE */
901cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_PLL_N,
9026d6f8b83SDimitris Papastamos 			      (pll_div.div2 << WM8985_PLL_PRESCALE_SHIFT)
9036d6f8b83SDimitris Papastamos 			      | pll_div.n);
9046d6f8b83SDimitris Papastamos 		/* set PLLK */
905cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_PLL_K_3, pll_div.k & 0x1ff);
906cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_PLL_K_2, (pll_div.k >> 9) & 0x1ff);
907cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_PLL_K_1, (pll_div.k >> 18));
9086d6f8b83SDimitris Papastamos 		/* set the source of the clock to be the PLL */
909cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
9106d6f8b83SDimitris Papastamos 				    WM8985_CLKSEL_MASK, WM8985_CLKSEL);
9116d6f8b83SDimitris Papastamos 		/* enable the PLL */
912cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9136d6f8b83SDimitris Papastamos 				    WM8985_PLLEN_MASK, WM8985_PLLEN);
9145f3d25c0SFabio Estevam 	}
9156d6f8b83SDimitris Papastamos 	return 0;
9166d6f8b83SDimitris Papastamos }
9176d6f8b83SDimitris Papastamos 
9186d6f8b83SDimitris Papastamos static int wm8985_set_sysclk(struct snd_soc_dai *dai,
9196d6f8b83SDimitris Papastamos 			     int clk_id, unsigned int freq, int dir)
9206d6f8b83SDimitris Papastamos {
921cbc23b74SKuninori Morimoto 	struct snd_soc_component *component;
9226d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
9236d6f8b83SDimitris Papastamos 
924cbc23b74SKuninori Morimoto 	component = dai->component;
925cbc23b74SKuninori Morimoto 	wm8985 = snd_soc_component_get_drvdata(component);
9266d6f8b83SDimitris Papastamos 
9276d6f8b83SDimitris Papastamos 	switch (clk_id) {
9286d6f8b83SDimitris Papastamos 	case WM8985_CLKSRC_MCLK:
929cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
9306d6f8b83SDimitris Papastamos 				    WM8985_CLKSEL_MASK, 0);
931cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9326d6f8b83SDimitris Papastamos 				    WM8985_PLLEN_MASK, 0);
9336d6f8b83SDimitris Papastamos 		break;
9346d6f8b83SDimitris Papastamos 	case WM8985_CLKSRC_PLL:
935cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_CLOCK_GEN_CONTROL,
9366d6f8b83SDimitris Papastamos 				    WM8985_CLKSEL_MASK, WM8985_CLKSEL);
9376d6f8b83SDimitris Papastamos 		break;
9386d6f8b83SDimitris Papastamos 	default:
9396d6f8b83SDimitris Papastamos 		dev_err(dai->dev, "Unknown clock source %d\n", clk_id);
9406d6f8b83SDimitris Papastamos 		return -EINVAL;
9416d6f8b83SDimitris Papastamos 	}
9426d6f8b83SDimitris Papastamos 
9436d6f8b83SDimitris Papastamos 	wm8985->sysclk = freq;
9446d6f8b83SDimitris Papastamos 	return 0;
9456d6f8b83SDimitris Papastamos }
9466d6f8b83SDimitris Papastamos 
947cbc23b74SKuninori Morimoto static int wm8985_set_bias_level(struct snd_soc_component *component,
9486d6f8b83SDimitris Papastamos 				 enum snd_soc_bias_level level)
9496d6f8b83SDimitris Papastamos {
9506d6f8b83SDimitris Papastamos 	int ret;
9516d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
9526d6f8b83SDimitris Papastamos 
953cbc23b74SKuninori Morimoto 	wm8985 = snd_soc_component_get_drvdata(component);
9546d6f8b83SDimitris Papastamos 	switch (level) {
9556d6f8b83SDimitris Papastamos 	case SND_SOC_BIAS_ON:
9566d6f8b83SDimitris Papastamos 	case SND_SOC_BIAS_PREPARE:
9576d6f8b83SDimitris Papastamos 		/* VMID at 75k */
958cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9596d6f8b83SDimitris Papastamos 				    WM8985_VMIDSEL_MASK,
9606d6f8b83SDimitris Papastamos 				    1 << WM8985_VMIDSEL_SHIFT);
9616d6f8b83SDimitris Papastamos 		break;
9626d6f8b83SDimitris Papastamos 	case SND_SOC_BIAS_STANDBY:
963cbc23b74SKuninori Morimoto 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
9646d6f8b83SDimitris Papastamos 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8985->supplies),
9656d6f8b83SDimitris Papastamos 						    wm8985->supplies);
9666d6f8b83SDimitris Papastamos 			if (ret) {
967cbc23b74SKuninori Morimoto 				dev_err(component->dev,
9686d6f8b83SDimitris Papastamos 					"Failed to enable supplies: %d\n",
9696d6f8b83SDimitris Papastamos 					ret);
9706d6f8b83SDimitris Papastamos 				return ret;
9716d6f8b83SDimitris Papastamos 			}
9726d6f8b83SDimitris Papastamos 
973411a3450SMark Brown 			regcache_sync(wm8985->regmap);
9746d6f8b83SDimitris Papastamos 
9756d6f8b83SDimitris Papastamos 			/* enable anti-pop features */
976cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_OUT4_TO_ADC,
9776d6f8b83SDimitris Papastamos 					    WM8985_POBCTRL_MASK,
9786d6f8b83SDimitris Papastamos 					    WM8985_POBCTRL);
9796d6f8b83SDimitris Papastamos 			/* enable thermal shutdown */
980cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_OUTPUT_CTRL0,
9816d6f8b83SDimitris Papastamos 					    WM8985_TSDEN_MASK, WM8985_TSDEN);
982cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_OUTPUT_CTRL0,
9836d6f8b83SDimitris Papastamos 					    WM8985_TSOPCTRL_MASK,
9846d6f8b83SDimitris Papastamos 					    WM8985_TSOPCTRL);
9856d6f8b83SDimitris Papastamos 			/* enable BIASEN */
986cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9876d6f8b83SDimitris Papastamos 					    WM8985_BIASEN_MASK, WM8985_BIASEN);
9886d6f8b83SDimitris Papastamos 			/* VMID at 75k */
989cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9906d6f8b83SDimitris Papastamos 					    WM8985_VMIDSEL_MASK,
9916d6f8b83SDimitris Papastamos 					    1 << WM8985_VMIDSEL_SHIFT);
9926d6f8b83SDimitris Papastamos 			msleep(500);
9936d6f8b83SDimitris Papastamos 			/* disable anti-pop features */
994cbc23b74SKuninori Morimoto 			snd_soc_component_update_bits(component, WM8985_OUT4_TO_ADC,
9956d6f8b83SDimitris Papastamos 					    WM8985_POBCTRL_MASK, 0);
9966d6f8b83SDimitris Papastamos 		}
9976d6f8b83SDimitris Papastamos 		/* VMID at 300k */
998cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
9996d6f8b83SDimitris Papastamos 				    WM8985_VMIDSEL_MASK,
10006d6f8b83SDimitris Papastamos 				    2 << WM8985_VMIDSEL_SHIFT);
10016d6f8b83SDimitris Papastamos 		break;
10026d6f8b83SDimitris Papastamos 	case SND_SOC_BIAS_OFF:
10036d6f8b83SDimitris Papastamos 		/* disable thermal shutdown */
1004cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_OUTPUT_CTRL0,
10056d6f8b83SDimitris Papastamos 				    WM8985_TSOPCTRL_MASK, 0);
1006cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_OUTPUT_CTRL0,
10076d6f8b83SDimitris Papastamos 				    WM8985_TSDEN_MASK, 0);
10086d6f8b83SDimitris Papastamos 		/* disable VMIDSEL and BIASEN */
1009cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, WM8985_POWER_MANAGEMENT_1,
10106d6f8b83SDimitris Papastamos 				    WM8985_VMIDSEL_MASK | WM8985_BIASEN_MASK,
10116d6f8b83SDimitris Papastamos 				    0);
1012cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_POWER_MANAGEMENT_1, 0);
1013cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_POWER_MANAGEMENT_2, 0);
1014cbc23b74SKuninori Morimoto 		snd_soc_component_write(component, WM8985_POWER_MANAGEMENT_3, 0);
10156d6f8b83SDimitris Papastamos 
1016411a3450SMark Brown 		regcache_mark_dirty(wm8985->regmap);
10176d6f8b83SDimitris Papastamos 
10186d6f8b83SDimitris Papastamos 		regulator_bulk_disable(ARRAY_SIZE(wm8985->supplies),
10196d6f8b83SDimitris Papastamos 				       wm8985->supplies);
10206d6f8b83SDimitris Papastamos 		break;
10216d6f8b83SDimitris Papastamos 	}
10226d6f8b83SDimitris Papastamos 
10236d6f8b83SDimitris Papastamos 	return 0;
10246d6f8b83SDimitris Papastamos }
10256d6f8b83SDimitris Papastamos 
1026cbc23b74SKuninori Morimoto static int wm8985_probe(struct snd_soc_component *component)
10276d6f8b83SDimitris Papastamos {
10286d6f8b83SDimitris Papastamos 	size_t i;
10296d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
10306d6f8b83SDimitris Papastamos 	int ret;
10316d6f8b83SDimitris Papastamos 
1032cbc23b74SKuninori Morimoto 	wm8985 = snd_soc_component_get_drvdata(component);
10336d6f8b83SDimitris Papastamos 
10346d6f8b83SDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(wm8985->supplies); i++)
10356d6f8b83SDimitris Papastamos 		wm8985->supplies[i].supply = wm8985_supply_names[i];
10366d6f8b83SDimitris Papastamos 
1037cbc23b74SKuninori Morimoto 	ret = devm_regulator_bulk_get(component->dev, ARRAY_SIZE(wm8985->supplies),
10386d6f8b83SDimitris Papastamos 				 wm8985->supplies);
10396d6f8b83SDimitris Papastamos 	if (ret) {
1040cbc23b74SKuninori Morimoto 		dev_err(component->dev, "Failed to request supplies: %d\n", ret);
10416d6f8b83SDimitris Papastamos 		return ret;
10426d6f8b83SDimitris Papastamos 	}
10436d6f8b83SDimitris Papastamos 
10446d6f8b83SDimitris Papastamos 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8985->supplies),
10456d6f8b83SDimitris Papastamos 				    wm8985->supplies);
10466d6f8b83SDimitris Papastamos 	if (ret) {
1047cbc23b74SKuninori Morimoto 		dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
1048a0b148b4SFabio Estevam 		return ret;
10496d6f8b83SDimitris Papastamos 	}
10506d6f8b83SDimitris Papastamos 
1051cbc23b74SKuninori Morimoto 	ret = wm8985_reset(component);
10526d6f8b83SDimitris Papastamos 	if (ret < 0) {
1053cbc23b74SKuninori Morimoto 		dev_err(component->dev, "Failed to issue reset: %d\n", ret);
10546d6f8b83SDimitris Papastamos 		goto err_reg_enable;
10556d6f8b83SDimitris Papastamos 	}
10566d6f8b83SDimitris Papastamos 
10576d6f8b83SDimitris Papastamos 	/* latch volume update bits */
10586d6f8b83SDimitris Papastamos 	for (i = 0; i < ARRAY_SIZE(volume_update_regs); ++i)
1059cbc23b74SKuninori Morimoto 		snd_soc_component_update_bits(component, volume_update_regs[i],
10609f8cbae4SMark Brown 				    0x100, 0x100);
10616d6f8b83SDimitris Papastamos 	/* enable BIASCUT */
1062cbc23b74SKuninori Morimoto 	snd_soc_component_update_bits(component, WM8985_BIAS_CTRL, WM8985_BIASCUT,
10639f8cbae4SMark Brown 			    WM8985_BIASCUT);
10646d6f8b83SDimitris Papastamos 
1065cbc23b74SKuninori Morimoto 	wm8985_add_widgets(component);
1066811e66deSPetr Kulhavy 
10676d6f8b83SDimitris Papastamos 	return 0;
10686d6f8b83SDimitris Papastamos 
10696d6f8b83SDimitris Papastamos err_reg_enable:
10706d6f8b83SDimitris Papastamos 	regulator_bulk_disable(ARRAY_SIZE(wm8985->supplies), wm8985->supplies);
10716d6f8b83SDimitris Papastamos 	return ret;
10726d6f8b83SDimitris Papastamos }
10736d6f8b83SDimitris Papastamos 
107485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm8985_dai_ops = {
10756d6f8b83SDimitris Papastamos 	.digital_mute = wm8985_dac_mute,
10766d6f8b83SDimitris Papastamos 	.hw_params = wm8985_hw_params,
10776d6f8b83SDimitris Papastamos 	.set_fmt = wm8985_set_fmt,
10786d6f8b83SDimitris Papastamos 	.set_sysclk = wm8985_set_sysclk,
10796d6f8b83SDimitris Papastamos 	.set_pll = wm8985_set_pll
10806d6f8b83SDimitris Papastamos };
10816d6f8b83SDimitris Papastamos 
10826d6f8b83SDimitris Papastamos #define WM8985_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
10836d6f8b83SDimitris Papastamos 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
10846d6f8b83SDimitris Papastamos 
10856d6f8b83SDimitris Papastamos static struct snd_soc_dai_driver wm8985_dai = {
10866d6f8b83SDimitris Papastamos 	.name = "wm8985-hifi",
10876d6f8b83SDimitris Papastamos 	.playback = {
10886d6f8b83SDimitris Papastamos 		.stream_name = "Playback",
10896d6f8b83SDimitris Papastamos 		.channels_min = 2,
10906d6f8b83SDimitris Papastamos 		.channels_max = 2,
10916d6f8b83SDimitris Papastamos 		.rates = SNDRV_PCM_RATE_8000_48000,
10926d6f8b83SDimitris Papastamos 		.formats = WM8985_FORMATS,
10936d6f8b83SDimitris Papastamos 	},
10946d6f8b83SDimitris Papastamos 	.capture = {
10956d6f8b83SDimitris Papastamos 		.stream_name = "Capture",
10966d6f8b83SDimitris Papastamos 		.channels_min = 2,
10976d6f8b83SDimitris Papastamos 		.channels_max = 2,
10986d6f8b83SDimitris Papastamos 		.rates = SNDRV_PCM_RATE_8000_48000,
10996d6f8b83SDimitris Papastamos 		.formats = WM8985_FORMATS,
11006d6f8b83SDimitris Papastamos 	},
11016d6f8b83SDimitris Papastamos 	.ops = &wm8985_dai_ops,
11026d6f8b83SDimitris Papastamos 	.symmetric_rates = 1
11036d6f8b83SDimitris Papastamos };
11046d6f8b83SDimitris Papastamos 
1105cbc23b74SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm8985 = {
11066d6f8b83SDimitris Papastamos 	.probe			= wm8985_probe,
11076d6f8b83SDimitris Papastamos 	.set_bias_level		= wm8985_set_bias_level,
1108811e66deSPetr Kulhavy 	.controls		= wm8985_common_snd_controls,
1109811e66deSPetr Kulhavy 	.num_controls		= ARRAY_SIZE(wm8985_common_snd_controls),
1110811e66deSPetr Kulhavy 	.dapm_widgets		= wm8985_common_dapm_widgets,
1111811e66deSPetr Kulhavy 	.num_dapm_widgets	= ARRAY_SIZE(wm8985_common_dapm_widgets),
1112811e66deSPetr Kulhavy 	.dapm_routes		= wm8985_common_dapm_routes,
1113811e66deSPetr Kulhavy 	.num_dapm_routes	= ARRAY_SIZE(wm8985_common_dapm_routes),
1114cbc23b74SKuninori Morimoto 	.suspend_bias_off	= 1,
1115cbc23b74SKuninori Morimoto 	.idle_bias_on		= 1,
1116cbc23b74SKuninori Morimoto 	.use_pmdown_time	= 1,
1117cbc23b74SKuninori Morimoto 	.endianness		= 1,
1118cbc23b74SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
11196d6f8b83SDimitris Papastamos };
11206d6f8b83SDimitris Papastamos 
1121411a3450SMark Brown static const struct regmap_config wm8985_regmap = {
1122411a3450SMark Brown 	.reg_bits = 7,
1123411a3450SMark Brown 	.val_bits = 9,
1124411a3450SMark Brown 
1125411a3450SMark Brown 	.max_register = WM8985_MAX_REGISTER,
1126411a3450SMark Brown 	.writeable_reg = wm8985_writeable,
1127411a3450SMark Brown 
1128411a3450SMark Brown 	.cache_type = REGCACHE_RBTREE,
1129411a3450SMark Brown 	.reg_defaults = wm8985_reg_defaults,
1130411a3450SMark Brown 	.num_reg_defaults = ARRAY_SIZE(wm8985_reg_defaults),
1131411a3450SMark Brown };
1132411a3450SMark Brown 
11336d6f8b83SDimitris Papastamos #if defined(CONFIG_SPI_MASTER)
11347a79e94eSBill Pemberton static int wm8985_spi_probe(struct spi_device *spi)
11356d6f8b83SDimitris Papastamos {
11366d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
11376d6f8b83SDimitris Papastamos 	int ret;
11386d6f8b83SDimitris Papastamos 
1139a1fea940SMark Brown 	wm8985 = devm_kzalloc(&spi->dev, sizeof *wm8985, GFP_KERNEL);
1140fe3e2e7fSDan Carpenter 	if (!wm8985)
1141fe3e2e7fSDan Carpenter 		return -ENOMEM;
11426d6f8b83SDimitris Papastamos 
11436d6f8b83SDimitris Papastamos 	spi_set_drvdata(spi, wm8985);
11446d6f8b83SDimitris Papastamos 
1145811e66deSPetr Kulhavy 	wm8985->dev_type = WM8985;
1146811e66deSPetr Kulhavy 
1147f911fa82STushar Behera 	wm8985->regmap = devm_regmap_init_spi(spi, &wm8985_regmap);
1148411a3450SMark Brown 	if (IS_ERR(wm8985->regmap)) {
1149411a3450SMark Brown 		ret = PTR_ERR(wm8985->regmap);
1150411a3450SMark Brown 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1151411a3450SMark Brown 			ret);
1152f911fa82STushar Behera 		return ret;
1153411a3450SMark Brown 	}
1154411a3450SMark Brown 
1155cbc23b74SKuninori Morimoto 	ret = devm_snd_soc_register_component(&spi->dev,
1156cbc23b74SKuninori Morimoto 				     &soc_component_dev_wm8985, &wm8985_dai, 1);
11576d6f8b83SDimitris Papastamos 	return ret;
11586d6f8b83SDimitris Papastamos }
11596d6f8b83SDimitris Papastamos 
11606d6f8b83SDimitris Papastamos static struct spi_driver wm8985_spi_driver = {
11616d6f8b83SDimitris Papastamos 	.driver = {
11626d6f8b83SDimitris Papastamos 		.name = "wm8985",
11636d6f8b83SDimitris Papastamos 	},
11646d6f8b83SDimitris Papastamos 	.probe = wm8985_spi_probe,
11656d6f8b83SDimitris Papastamos };
11666d6f8b83SDimitris Papastamos #endif
11676d6f8b83SDimitris Papastamos 
116850c96973SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
11697a79e94eSBill Pemberton static int wm8985_i2c_probe(struct i2c_client *i2c,
11706d6f8b83SDimitris Papastamos 			    const struct i2c_device_id *id)
11716d6f8b83SDimitris Papastamos {
11726d6f8b83SDimitris Papastamos 	struct wm8985_priv *wm8985;
11736d6f8b83SDimitris Papastamos 	int ret;
11746d6f8b83SDimitris Papastamos 
1175a1fea940SMark Brown 	wm8985 = devm_kzalloc(&i2c->dev, sizeof *wm8985, GFP_KERNEL);
1176fe3e2e7fSDan Carpenter 	if (!wm8985)
1177fe3e2e7fSDan Carpenter 		return -ENOMEM;
11786d6f8b83SDimitris Papastamos 
11796d6f8b83SDimitris Papastamos 	i2c_set_clientdata(i2c, wm8985);
11806d6f8b83SDimitris Papastamos 
1181811e66deSPetr Kulhavy 	wm8985->dev_type = id->driver_data;
1182811e66deSPetr Kulhavy 
1183f911fa82STushar Behera 	wm8985->regmap = devm_regmap_init_i2c(i2c, &wm8985_regmap);
1184411a3450SMark Brown 	if (IS_ERR(wm8985->regmap)) {
1185411a3450SMark Brown 		ret = PTR_ERR(wm8985->regmap);
1186411a3450SMark Brown 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1187411a3450SMark Brown 			ret);
1188f911fa82STushar Behera 		return ret;
1189411a3450SMark Brown 	}
1190411a3450SMark Brown 
1191cbc23b74SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev,
1192cbc23b74SKuninori Morimoto 				     &soc_component_dev_wm8985, &wm8985_dai, 1);
11936d6f8b83SDimitris Papastamos 	return ret;
11946d6f8b83SDimitris Papastamos }
11956d6f8b83SDimitris Papastamos 
11966d6f8b83SDimitris Papastamos static const struct i2c_device_id wm8985_i2c_id[] = {
1197811e66deSPetr Kulhavy 	{ "wm8985", WM8985 },
1198811e66deSPetr Kulhavy 	{ "wm8758", WM8758 },
11996d6f8b83SDimitris Papastamos 	{ }
12006d6f8b83SDimitris Papastamos };
12016d6f8b83SDimitris Papastamos MODULE_DEVICE_TABLE(i2c, wm8985_i2c_id);
12026d6f8b83SDimitris Papastamos 
12036d6f8b83SDimitris Papastamos static struct i2c_driver wm8985_i2c_driver = {
12046d6f8b83SDimitris Papastamos 	.driver = {
12056d6f8b83SDimitris Papastamos 		.name = "wm8985",
12066d6f8b83SDimitris Papastamos 	},
12076d6f8b83SDimitris Papastamos 	.probe = wm8985_i2c_probe,
12086d6f8b83SDimitris Papastamos 	.id_table = wm8985_i2c_id
12096d6f8b83SDimitris Papastamos };
12106d6f8b83SDimitris Papastamos #endif
12116d6f8b83SDimitris Papastamos 
12126d6f8b83SDimitris Papastamos static int __init wm8985_modinit(void)
12136d6f8b83SDimitris Papastamos {
12146d6f8b83SDimitris Papastamos 	int ret = 0;
12156d6f8b83SDimitris Papastamos 
121650c96973SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
12176d6f8b83SDimitris Papastamos 	ret = i2c_add_driver(&wm8985_i2c_driver);
12186d6f8b83SDimitris Papastamos 	if (ret) {
12196d6f8b83SDimitris Papastamos 		printk(KERN_ERR "Failed to register wm8985 I2C driver: %d\n",
12206d6f8b83SDimitris Papastamos 		       ret);
12216d6f8b83SDimitris Papastamos 	}
12226d6f8b83SDimitris Papastamos #endif
12236d6f8b83SDimitris Papastamos #if defined(CONFIG_SPI_MASTER)
12246d6f8b83SDimitris Papastamos 	ret = spi_register_driver(&wm8985_spi_driver);
12256d6f8b83SDimitris Papastamos 	if (ret != 0) {
12266d6f8b83SDimitris Papastamos 		printk(KERN_ERR "Failed to register wm8985 SPI driver: %d\n",
12276d6f8b83SDimitris Papastamos 		       ret);
12286d6f8b83SDimitris Papastamos 	}
12296d6f8b83SDimitris Papastamos #endif
12306d6f8b83SDimitris Papastamos 	return ret;
12316d6f8b83SDimitris Papastamos }
12326d6f8b83SDimitris Papastamos module_init(wm8985_modinit);
12336d6f8b83SDimitris Papastamos 
12346d6f8b83SDimitris Papastamos static void __exit wm8985_exit(void)
12356d6f8b83SDimitris Papastamos {
123650c96973SFabio Estevam #if IS_ENABLED(CONFIG_I2C)
12376d6f8b83SDimitris Papastamos 	i2c_del_driver(&wm8985_i2c_driver);
12386d6f8b83SDimitris Papastamos #endif
12396d6f8b83SDimitris Papastamos #if defined(CONFIG_SPI_MASTER)
12406d6f8b83SDimitris Papastamos 	spi_unregister_driver(&wm8985_spi_driver);
12416d6f8b83SDimitris Papastamos #endif
12426d6f8b83SDimitris Papastamos }
12436d6f8b83SDimitris Papastamos module_exit(wm8985_exit);
12446d6f8b83SDimitris Papastamos 
1245811e66deSPetr Kulhavy MODULE_DESCRIPTION("ASoC WM8985 / WM8758 driver");
12466d6f8b83SDimitris Papastamos MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
12476d6f8b83SDimitris Papastamos MODULE_LICENSE("GPL");
1248