xref: /openbmc/linux/sound/soc/codecs/cx2072x.c (revision a497a436)
1a497a436SSimon Ho // SPDX-License-Identifier: GPL-2.0
2a497a436SSimon Ho //
3a497a436SSimon Ho // ALSA SoC CX20721/CX20723 codec driver
4a497a436SSimon Ho //
5a497a436SSimon Ho // Copyright:	(C) 2017 Conexant Systems, Inc.
6a497a436SSimon Ho // Author:	Simon Ho, <Simon.ho@conexant.com>
7a497a436SSimon Ho //
8a497a436SSimon Ho // TODO: add support for TDM mode.
9a497a436SSimon Ho //
10a497a436SSimon Ho 
11a497a436SSimon Ho #include <linux/acpi.h>
12a497a436SSimon Ho #include <linux/clk.h>
13a497a436SSimon Ho #include <linux/delay.h>
14a497a436SSimon Ho #include <linux/gpio.h>
15a497a436SSimon Ho #include <linux/init.h>
16a497a436SSimon Ho #include <linux/i2c.h>
17a497a436SSimon Ho #include <linux/module.h>
18a497a436SSimon Ho #include <linux/platform_device.h>
19a497a436SSimon Ho #include <linux/pm.h>
20a497a436SSimon Ho #include <linux/pm_runtime.h>
21a497a436SSimon Ho #include <linux/regmap.h>
22a497a436SSimon Ho #include <linux/slab.h>
23a497a436SSimon Ho #include <sound/core.h>
24a497a436SSimon Ho #include <sound/initval.h>
25a497a436SSimon Ho #include <sound/jack.h>
26a497a436SSimon Ho #include <sound/pcm.h>
27a497a436SSimon Ho #include <sound/pcm_params.h>
28a497a436SSimon Ho #include <sound/tlv.h>
29a497a436SSimon Ho #include <sound/soc.h>
30a497a436SSimon Ho #include <sound/soc-dapm.h>
31a497a436SSimon Ho #include "cx2072x.h"
32a497a436SSimon Ho 
33a497a436SSimon Ho #define PLL_OUT_HZ_48	(1024 * 3 * 48000)
34a497a436SSimon Ho #define BITS_PER_SLOT	8
35a497a436SSimon Ho 
36a497a436SSimon Ho /* codec private data */
37a497a436SSimon Ho struct cx2072x_priv {
38a497a436SSimon Ho 	struct regmap *regmap;
39a497a436SSimon Ho 	struct clk *mclk;
40a497a436SSimon Ho 	unsigned int mclk_rate;
41a497a436SSimon Ho 	struct device *dev;
42a497a436SSimon Ho 	struct snd_soc_component *codec;
43a497a436SSimon Ho 	struct snd_soc_jack_gpio jack_gpio;
44a497a436SSimon Ho 	struct mutex lock;
45a497a436SSimon Ho 	unsigned int bclk_ratio;
46a497a436SSimon Ho 	bool pll_changed;
47a497a436SSimon Ho 	bool i2spcm_changed;
48a497a436SSimon Ho 	int sample_size;
49a497a436SSimon Ho 	int frame_size;
50a497a436SSimon Ho 	int sample_rate;
51a497a436SSimon Ho 	unsigned int dai_fmt;
52a497a436SSimon Ho 	bool en_aec_ref;
53a497a436SSimon Ho };
54a497a436SSimon Ho 
55a497a436SSimon Ho /*
56a497a436SSimon Ho  * DAC/ADC Volume
57a497a436SSimon Ho  *
58a497a436SSimon Ho  * max : 74 : 0 dB
59a497a436SSimon Ho  *	 ( in 1 dB  step )
60a497a436SSimon Ho  * min : 0 : -74 dB
61a497a436SSimon Ho  */
62a497a436SSimon Ho static const DECLARE_TLV_DB_SCALE(adc_tlv, -7400, 100, 0);
63a497a436SSimon Ho static const DECLARE_TLV_DB_SCALE(dac_tlv, -7400, 100, 0);
64a497a436SSimon Ho static const DECLARE_TLV_DB_SCALE(boost_tlv, 0, 1200, 0);
65a497a436SSimon Ho 
66a497a436SSimon Ho struct cx2072x_eq_ctrl {
67a497a436SSimon Ho 	u8 ch;
68a497a436SSimon Ho 	u8 band;
69a497a436SSimon Ho };
70a497a436SSimon Ho 
71a497a436SSimon Ho static const DECLARE_TLV_DB_RANGE(hpf_tlv,
72a497a436SSimon Ho 	0, 0, TLV_DB_SCALE_ITEM(120, 0, 0),
73a497a436SSimon Ho 	1, 63, TLV_DB_SCALE_ITEM(30, 30, 0)
74a497a436SSimon Ho );
75a497a436SSimon Ho 
76a497a436SSimon Ho /* Lookup table for PRE_DIV */
77a497a436SSimon Ho static const struct {
78a497a436SSimon Ho 	unsigned int mclk;
79a497a436SSimon Ho 	unsigned int div;
80a497a436SSimon Ho } mclk_pre_div[] = {
81a497a436SSimon Ho 	{ 6144000, 1 },
82a497a436SSimon Ho 	{ 12288000, 2 },
83a497a436SSimon Ho 	{ 19200000, 3 },
84a497a436SSimon Ho 	{ 26000000, 4 },
85a497a436SSimon Ho 	{ 28224000, 5 },
86a497a436SSimon Ho 	{ 36864000, 6 },
87a497a436SSimon Ho 	{ 36864000, 7 },
88a497a436SSimon Ho 	{ 48000000, 8 },
89a497a436SSimon Ho 	{ 49152000, 8 },
90a497a436SSimon Ho };
91a497a436SSimon Ho 
92a497a436SSimon Ho /*
93a497a436SSimon Ho  * cx2072x register cache.
94a497a436SSimon Ho  */
95a497a436SSimon Ho static const struct reg_default cx2072x_reg_defaults[] = {
96a497a436SSimon Ho 	{ CX2072X_AFG_POWER_STATE, 0x00000003 },
97a497a436SSimon Ho 	{ CX2072X_UM_RESPONSE, 0x00000000 },
98a497a436SSimon Ho 	{ CX2072X_GPIO_DATA, 0x00000000 },
99a497a436SSimon Ho 	{ CX2072X_GPIO_ENABLE, 0x00000000 },
100a497a436SSimon Ho 	{ CX2072X_GPIO_DIRECTION, 0x00000000 },
101a497a436SSimon Ho 	{ CX2072X_GPIO_WAKE, 0x00000000 },
102a497a436SSimon Ho 	{ CX2072X_GPIO_UM_ENABLE, 0x00000000 },
103a497a436SSimon Ho 	{ CX2072X_GPIO_STICKY_MASK, 0x00000000 },
104a497a436SSimon Ho 	{ CX2072X_DAC1_CONVERTER_FORMAT, 0x00000031 },
105a497a436SSimon Ho 	{ CX2072X_DAC1_AMP_GAIN_RIGHT, 0x0000004a },
106a497a436SSimon Ho 	{ CX2072X_DAC1_AMP_GAIN_LEFT, 0x0000004a },
107a497a436SSimon Ho 	{ CX2072X_DAC1_POWER_STATE, 0x00000433 },
108a497a436SSimon Ho 	{ CX2072X_DAC1_CONVERTER_STREAM_CHANNEL, 0x00000000 },
109a497a436SSimon Ho 	{ CX2072X_DAC1_EAPD_ENABLE, 0x00000000 },
110a497a436SSimon Ho 	{ CX2072X_DAC2_CONVERTER_FORMAT, 0x00000031 },
111a497a436SSimon Ho 	{ CX2072X_DAC2_AMP_GAIN_RIGHT, 0x0000004a },
112a497a436SSimon Ho 	{ CX2072X_DAC2_AMP_GAIN_LEFT, 0x0000004a },
113a497a436SSimon Ho 	{ CX2072X_DAC2_POWER_STATE, 0x00000433 },
114a497a436SSimon Ho 	{ CX2072X_DAC2_CONVERTER_STREAM_CHANNEL, 0x00000000 },
115a497a436SSimon Ho 	{ CX2072X_ADC1_CONVERTER_FORMAT, 0x00000031 },
116a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_0, 0x0000004a },
117a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_0, 0x0000004a },
118a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_1, 0x0000004a },
119a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_1, 0x0000004a },
120a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_2, 0x0000004a },
121a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_2, 0x0000004a },
122a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_3, 0x0000004a },
123a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_3, 0x0000004a },
124a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_4, 0x0000004a },
125a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_4, 0x0000004a },
126a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_5, 0x0000004a },
127a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_5, 0x0000004a },
128a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_RIGHT_6, 0x0000004a },
129a497a436SSimon Ho 	{ CX2072X_ADC1_AMP_GAIN_LEFT_6, 0x0000004a },
130a497a436SSimon Ho 	{ CX2072X_ADC1_CONNECTION_SELECT_CONTROL, 0x00000000 },
131a497a436SSimon Ho 	{ CX2072X_ADC1_POWER_STATE, 0x00000433 },
132a497a436SSimon Ho 	{ CX2072X_ADC1_CONVERTER_STREAM_CHANNEL, 0x00000000 },
133a497a436SSimon Ho 	{ CX2072X_ADC2_CONVERTER_FORMAT, 0x00000031 },
134a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_RIGHT_0, 0x0000004a },
135a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_LEFT_0, 0x0000004a },
136a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_RIGHT_1, 0x0000004a },
137a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_LEFT_1, 0x0000004a },
138a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_RIGHT_2, 0x0000004a },
139a497a436SSimon Ho 	{ CX2072X_ADC2_AMP_GAIN_LEFT_2, 0x0000004a },
140a497a436SSimon Ho 	{ CX2072X_ADC2_CONNECTION_SELECT_CONTROL, 0x00000000 },
141a497a436SSimon Ho 	{ CX2072X_ADC2_POWER_STATE, 0x00000433 },
142a497a436SSimon Ho 	{ CX2072X_ADC2_CONVERTER_STREAM_CHANNEL, 0x00000000 },
143a497a436SSimon Ho 	{ CX2072X_PORTA_CONNECTION_SELECT_CTRL, 0x00000000 },
144a497a436SSimon Ho 	{ CX2072X_PORTA_POWER_STATE, 0x00000433 },
145a497a436SSimon Ho 	{ CX2072X_PORTA_PIN_CTRL, 0x000000c0 },
146a497a436SSimon Ho 	{ CX2072X_PORTA_UNSOLICITED_RESPONSE, 0x00000000 },
147a497a436SSimon Ho 	{ CX2072X_PORTA_PIN_SENSE, 0x00000000 },
148a497a436SSimon Ho 	{ CX2072X_PORTA_EAPD_BTL, 0x00000002 },
149a497a436SSimon Ho 	{ CX2072X_PORTB_POWER_STATE, 0x00000433 },
150a497a436SSimon Ho 	{ CX2072X_PORTB_PIN_CTRL, 0x00000000 },
151a497a436SSimon Ho 	{ CX2072X_PORTB_UNSOLICITED_RESPONSE, 0x00000000 },
152a497a436SSimon Ho 	{ CX2072X_PORTB_PIN_SENSE, 0x00000000 },
153a497a436SSimon Ho 	{ CX2072X_PORTB_EAPD_BTL, 0x00000002 },
154a497a436SSimon Ho 	{ CX2072X_PORTB_GAIN_RIGHT, 0x00000000 },
155a497a436SSimon Ho 	{ CX2072X_PORTB_GAIN_LEFT, 0x00000000 },
156a497a436SSimon Ho 	{ CX2072X_PORTC_POWER_STATE, 0x00000433 },
157a497a436SSimon Ho 	{ CX2072X_PORTC_PIN_CTRL, 0x00000000 },
158a497a436SSimon Ho 	{ CX2072X_PORTC_GAIN_RIGHT, 0x00000000 },
159a497a436SSimon Ho 	{ CX2072X_PORTC_GAIN_LEFT, 0x00000000 },
160a497a436SSimon Ho 	{ CX2072X_PORTD_POWER_STATE, 0x00000433 },
161a497a436SSimon Ho 	{ CX2072X_PORTD_PIN_CTRL, 0x00000020 },
162a497a436SSimon Ho 	{ CX2072X_PORTD_UNSOLICITED_RESPONSE, 0x00000000 },
163a497a436SSimon Ho 	{ CX2072X_PORTD_PIN_SENSE, 0x00000000 },
164a497a436SSimon Ho 	{ CX2072X_PORTD_GAIN_RIGHT, 0x00000000 },
165a497a436SSimon Ho 	{ CX2072X_PORTD_GAIN_LEFT, 0x00000000 },
166a497a436SSimon Ho 	{ CX2072X_PORTE_CONNECTION_SELECT_CTRL, 0x00000000 },
167a497a436SSimon Ho 	{ CX2072X_PORTE_POWER_STATE, 0x00000433 },
168a497a436SSimon Ho 	{ CX2072X_PORTE_PIN_CTRL, 0x00000040 },
169a497a436SSimon Ho 	{ CX2072X_PORTE_UNSOLICITED_RESPONSE, 0x00000000 },
170a497a436SSimon Ho 	{ CX2072X_PORTE_PIN_SENSE, 0x00000000 },
171a497a436SSimon Ho 	{ CX2072X_PORTE_EAPD_BTL, 0x00000002 },
172a497a436SSimon Ho 	{ CX2072X_PORTE_GAIN_RIGHT, 0x00000000 },
173a497a436SSimon Ho 	{ CX2072X_PORTE_GAIN_LEFT, 0x00000000 },
174a497a436SSimon Ho 	{ CX2072X_PORTF_POWER_STATE, 0x00000433 },
175a497a436SSimon Ho 	{ CX2072X_PORTF_PIN_CTRL, 0x00000000 },
176a497a436SSimon Ho 	{ CX2072X_PORTF_UNSOLICITED_RESPONSE, 0x00000000 },
177a497a436SSimon Ho 	{ CX2072X_PORTF_PIN_SENSE, 0x00000000 },
178a497a436SSimon Ho 	{ CX2072X_PORTF_GAIN_RIGHT, 0x00000000 },
179a497a436SSimon Ho 	{ CX2072X_PORTF_GAIN_LEFT, 0x00000000 },
180a497a436SSimon Ho 	{ CX2072X_PORTG_POWER_STATE, 0x00000433 },
181a497a436SSimon Ho 	{ CX2072X_PORTG_PIN_CTRL, 0x00000040 },
182a497a436SSimon Ho 	{ CX2072X_PORTG_CONNECTION_SELECT_CTRL, 0x00000000 },
183a497a436SSimon Ho 	{ CX2072X_PORTG_EAPD_BTL, 0x00000002 },
184a497a436SSimon Ho 	{ CX2072X_PORTM_POWER_STATE, 0x00000433 },
185a497a436SSimon Ho 	{ CX2072X_PORTM_PIN_CTRL, 0x00000000 },
186a497a436SSimon Ho 	{ CX2072X_PORTM_CONNECTION_SELECT_CTRL, 0x00000000 },
187a497a436SSimon Ho 	{ CX2072X_PORTM_EAPD_BTL, 0x00000002 },
188a497a436SSimon Ho 	{ CX2072X_MIXER_POWER_STATE, 0x00000433 },
189a497a436SSimon Ho 	{ CX2072X_MIXER_GAIN_RIGHT_0, 0x0000004a },
190a497a436SSimon Ho 	{ CX2072X_MIXER_GAIN_LEFT_0, 0x0000004a },
191a497a436SSimon Ho 	{ CX2072X_MIXER_GAIN_RIGHT_1, 0x0000004a },
192a497a436SSimon Ho 	{ CX2072X_MIXER_GAIN_LEFT_1, 0x0000004a },
193a497a436SSimon Ho 	{ CX2072X_SPKR_DRC_ENABLE_STEP, 0x040065a4 },
194a497a436SSimon Ho 	{ CX2072X_SPKR_DRC_CONTROL, 0x007b0024 },
195a497a436SSimon Ho 	{ CX2072X_SPKR_DRC_TEST, 0x00000000 },
196a497a436SSimon Ho 	{ CX2072X_DIGITAL_BIOS_TEST0, 0x001f008a },
197a497a436SSimon Ho 	{ CX2072X_DIGITAL_BIOS_TEST2, 0x00990026 },
198a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL1, 0x00010001 },
199a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL2, 0x00000000 },
200a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL3, 0x00000000 },
201a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL4, 0x00000000 },
202a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL5, 0x00000000 },
203a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL6, 0x00000000 },
204a497a436SSimon Ho 	{ CX2072X_UM_INTERRUPT_CRTL_E, 0x00000000 },
205a497a436SSimon Ho 	{ CX2072X_CODEC_TEST2, 0x00000000 },
206a497a436SSimon Ho 	{ CX2072X_CODEC_TEST9, 0x00000004 },
207a497a436SSimon Ho 	{ CX2072X_CODEC_TEST20, 0x00000600 },
208a497a436SSimon Ho 	{ CX2072X_CODEC_TEST26, 0x00000208 },
209a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST4, 0x00000000 },
210a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST5, 0x00000000 },
211a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST6, 0x0000059a },
212a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST7, 0x000000a7 },
213a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST8, 0x00000017 },
214a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST9, 0x00000000 },
215a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST10, 0x00000285 },
216a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST11, 0x00000000 },
217a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST12, 0x00000000 },
218a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST13, 0x00000000 },
219a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST1, 0x00000242 },
220a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST11, 0x00000000 },
221a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST12, 0x00000084 },
222a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST15, 0x00000077 },
223a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST16, 0x00000021 },
224a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST17, 0x00000018 },
225a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST18, 0x00000024 },
226a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST19, 0x00000001 },
227a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST20, 0x00000002 },
228a497a436SSimon Ho };
229a497a436SSimon Ho 
230a497a436SSimon Ho /*
231a497a436SSimon Ho  * register initialization
232a497a436SSimon Ho  */
233a497a436SSimon Ho static const struct reg_sequence cx2072x_reg_init[] = {
234a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST9,	0x080 },    /* DC offset Calibration */
235a497a436SSimon Ho 	{ CX2072X_CODEC_TEST26,	0x65f },    /* Disable the PA */
236a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST10, 0x289 },   /* Set the speaker output gain */
237a497a436SSimon Ho 	{ CX2072X_CODEC_TEST20,	0xf05 },
238a497a436SSimon Ho 	{ CX2072X_CODEC_TESTXX,	0x380 },
239a497a436SSimon Ho 	{ CX2072X_CODEC_TEST26,	0xb90 },
240a497a436SSimon Ho 	{ CX2072X_CODEC_TEST9,	0x001 },    /* Enable 30 Hz High pass filter */
241a497a436SSimon Ho 	{ CX2072X_ANALOG_TEST3,	0x300 },    /* Disable PCBEEP pad */
242a497a436SSimon Ho 	{ CX2072X_CODEC_TEST24,	0x100 },    /* Disable SnM mode */
243a497a436SSimon Ho 	{ CX2072X_PORTD_PIN_CTRL, 0x020 },  /* Enable PortD input */
244a497a436SSimon Ho 	{ CX2072X_GPIO_ENABLE,	0x040 },    /* Enable GPIO7 pin for button */
245a497a436SSimon Ho 	{ CX2072X_GPIO_UM_ENABLE, 0x040 },  /* Enable UM for GPIO7 */
246a497a436SSimon Ho 	{ CX2072X_UM_RESPONSE,	0x080 },    /* Enable button response */
247a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST12, 0x0c4 },  /* Enable headset button */
248a497a436SSimon Ho 	{ CX2072X_DIGITAL_TEST0, 0x415 },   /* Power down class-D during idle */
249a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL2, 0x00f }, /* Enable I2S TX */
250a497a436SSimon Ho 	{ CX2072X_I2SPCM_CONTROL3, 0x00f }, /* Enable I2S RX */
251a497a436SSimon Ho };
252a497a436SSimon Ho 
253a497a436SSimon Ho static unsigned int cx2072x_register_size(unsigned int reg)
254a497a436SSimon Ho {
255a497a436SSimon Ho 	switch (reg) {
256a497a436SSimon Ho 	case CX2072X_VENDOR_ID:
257a497a436SSimon Ho 	case CX2072X_REVISION_ID:
258a497a436SSimon Ho 	case CX2072X_PORTA_PIN_SENSE:
259a497a436SSimon Ho 	case CX2072X_PORTB_PIN_SENSE:
260a497a436SSimon Ho 	case CX2072X_PORTD_PIN_SENSE:
261a497a436SSimon Ho 	case CX2072X_PORTE_PIN_SENSE:
262a497a436SSimon Ho 	case CX2072X_PORTF_PIN_SENSE:
263a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL1:
264a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL2:
265a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL3:
266a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL4:
267a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL5:
268a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL6:
269a497a436SSimon Ho 	case CX2072X_UM_INTERRUPT_CRTL_E:
270a497a436SSimon Ho 	case CX2072X_EQ_G_COEFF:
271a497a436SSimon Ho 	case CX2072X_SPKR_DRC_CONTROL:
272a497a436SSimon Ho 	case CX2072X_SPKR_DRC_TEST:
273a497a436SSimon Ho 	case CX2072X_DIGITAL_BIOS_TEST0:
274a497a436SSimon Ho 	case CX2072X_DIGITAL_BIOS_TEST2:
275a497a436SSimon Ho 		return 4;
276a497a436SSimon Ho 	case CX2072X_EQ_ENABLE_BYPASS:
277a497a436SSimon Ho 	case CX2072X_EQ_B0_COEFF:
278a497a436SSimon Ho 	case CX2072X_EQ_B1_COEFF:
279a497a436SSimon Ho 	case CX2072X_EQ_B2_COEFF:
280a497a436SSimon Ho 	case CX2072X_EQ_A1_COEFF:
281a497a436SSimon Ho 	case CX2072X_EQ_A2_COEFF:
282a497a436SSimon Ho 	case CX2072X_DAC1_CONVERTER_FORMAT:
283a497a436SSimon Ho 	case CX2072X_DAC2_CONVERTER_FORMAT:
284a497a436SSimon Ho 	case CX2072X_ADC1_CONVERTER_FORMAT:
285a497a436SSimon Ho 	case CX2072X_ADC2_CONVERTER_FORMAT:
286a497a436SSimon Ho 	case CX2072X_CODEC_TEST2:
287a497a436SSimon Ho 	case CX2072X_CODEC_TEST9:
288a497a436SSimon Ho 	case CX2072X_CODEC_TEST20:
289a497a436SSimon Ho 	case CX2072X_CODEC_TEST26:
290a497a436SSimon Ho 	case CX2072X_ANALOG_TEST3:
291a497a436SSimon Ho 	case CX2072X_ANALOG_TEST4:
292a497a436SSimon Ho 	case CX2072X_ANALOG_TEST5:
293a497a436SSimon Ho 	case CX2072X_ANALOG_TEST6:
294a497a436SSimon Ho 	case CX2072X_ANALOG_TEST7:
295a497a436SSimon Ho 	case CX2072X_ANALOG_TEST8:
296a497a436SSimon Ho 	case CX2072X_ANALOG_TEST9:
297a497a436SSimon Ho 	case CX2072X_ANALOG_TEST10:
298a497a436SSimon Ho 	case CX2072X_ANALOG_TEST11:
299a497a436SSimon Ho 	case CX2072X_ANALOG_TEST12:
300a497a436SSimon Ho 	case CX2072X_ANALOG_TEST13:
301a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST0:
302a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST1:
303a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST11:
304a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST12:
305a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST15:
306a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST16:
307a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST17:
308a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST18:
309a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST19:
310a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST20:
311a497a436SSimon Ho 		return 2;
312a497a436SSimon Ho 	default:
313a497a436SSimon Ho 		return 1;
314a497a436SSimon Ho 	}
315a497a436SSimon Ho }
316a497a436SSimon Ho 
317a497a436SSimon Ho static bool cx2072x_readable_register(struct device *dev, unsigned int reg)
318a497a436SSimon Ho {
319a497a436SSimon Ho 	switch (reg) {
320a497a436SSimon Ho 	case CX2072X_VENDOR_ID:
321a497a436SSimon Ho 	case CX2072X_REVISION_ID:
322a497a436SSimon Ho 	case CX2072X_CURRENT_BCLK_FREQUENCY:
323a497a436SSimon Ho 	case CX2072X_AFG_POWER_STATE:
324a497a436SSimon Ho 	case CX2072X_UM_RESPONSE:
325a497a436SSimon Ho 	case CX2072X_GPIO_DATA:
326a497a436SSimon Ho 	case CX2072X_GPIO_ENABLE:
327a497a436SSimon Ho 	case CX2072X_GPIO_DIRECTION:
328a497a436SSimon Ho 	case CX2072X_GPIO_WAKE:
329a497a436SSimon Ho 	case CX2072X_GPIO_UM_ENABLE:
330a497a436SSimon Ho 	case CX2072X_GPIO_STICKY_MASK:
331a497a436SSimon Ho 	case CX2072X_DAC1_CONVERTER_FORMAT:
332a497a436SSimon Ho 	case CX2072X_DAC1_AMP_GAIN_RIGHT:
333a497a436SSimon Ho 	case CX2072X_DAC1_AMP_GAIN_LEFT:
334a497a436SSimon Ho 	case CX2072X_DAC1_POWER_STATE:
335a497a436SSimon Ho 	case CX2072X_DAC1_CONVERTER_STREAM_CHANNEL:
336a497a436SSimon Ho 	case CX2072X_DAC1_EAPD_ENABLE:
337a497a436SSimon Ho 	case CX2072X_DAC2_CONVERTER_FORMAT:
338a497a436SSimon Ho 	case CX2072X_DAC2_AMP_GAIN_RIGHT:
339a497a436SSimon Ho 	case CX2072X_DAC2_AMP_GAIN_LEFT:
340a497a436SSimon Ho 	case CX2072X_DAC2_POWER_STATE:
341a497a436SSimon Ho 	case CX2072X_DAC2_CONVERTER_STREAM_CHANNEL:
342a497a436SSimon Ho 	case CX2072X_ADC1_CONVERTER_FORMAT:
343a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_0:
344a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_0:
345a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_1:
346a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_1:
347a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_2:
348a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_2:
349a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_3:
350a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_3:
351a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_4:
352a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_4:
353a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_5:
354a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_5:
355a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_RIGHT_6:
356a497a436SSimon Ho 	case CX2072X_ADC1_AMP_GAIN_LEFT_6:
357a497a436SSimon Ho 	case CX2072X_ADC1_CONNECTION_SELECT_CONTROL:
358a497a436SSimon Ho 	case CX2072X_ADC1_POWER_STATE:
359a497a436SSimon Ho 	case CX2072X_ADC1_CONVERTER_STREAM_CHANNEL:
360a497a436SSimon Ho 	case CX2072X_ADC2_CONVERTER_FORMAT:
361a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_RIGHT_0:
362a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_LEFT_0:
363a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_RIGHT_1:
364a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_LEFT_1:
365a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_RIGHT_2:
366a497a436SSimon Ho 	case CX2072X_ADC2_AMP_GAIN_LEFT_2:
367a497a436SSimon Ho 	case CX2072X_ADC2_CONNECTION_SELECT_CONTROL:
368a497a436SSimon Ho 	case CX2072X_ADC2_POWER_STATE:
369a497a436SSimon Ho 	case CX2072X_ADC2_CONVERTER_STREAM_CHANNEL:
370a497a436SSimon Ho 	case CX2072X_PORTA_CONNECTION_SELECT_CTRL:
371a497a436SSimon Ho 	case CX2072X_PORTA_POWER_STATE:
372a497a436SSimon Ho 	case CX2072X_PORTA_PIN_CTRL:
373a497a436SSimon Ho 	case CX2072X_PORTA_UNSOLICITED_RESPONSE:
374a497a436SSimon Ho 	case CX2072X_PORTA_PIN_SENSE:
375a497a436SSimon Ho 	case CX2072X_PORTA_EAPD_BTL:
376a497a436SSimon Ho 	case CX2072X_PORTB_POWER_STATE:
377a497a436SSimon Ho 	case CX2072X_PORTB_PIN_CTRL:
378a497a436SSimon Ho 	case CX2072X_PORTB_UNSOLICITED_RESPONSE:
379a497a436SSimon Ho 	case CX2072X_PORTB_PIN_SENSE:
380a497a436SSimon Ho 	case CX2072X_PORTB_EAPD_BTL:
381a497a436SSimon Ho 	case CX2072X_PORTB_GAIN_RIGHT:
382a497a436SSimon Ho 	case CX2072X_PORTB_GAIN_LEFT:
383a497a436SSimon Ho 	case CX2072X_PORTC_POWER_STATE:
384a497a436SSimon Ho 	case CX2072X_PORTC_PIN_CTRL:
385a497a436SSimon Ho 	case CX2072X_PORTC_GAIN_RIGHT:
386a497a436SSimon Ho 	case CX2072X_PORTC_GAIN_LEFT:
387a497a436SSimon Ho 	case CX2072X_PORTD_POWER_STATE:
388a497a436SSimon Ho 	case CX2072X_PORTD_PIN_CTRL:
389a497a436SSimon Ho 	case CX2072X_PORTD_UNSOLICITED_RESPONSE:
390a497a436SSimon Ho 	case CX2072X_PORTD_PIN_SENSE:
391a497a436SSimon Ho 	case CX2072X_PORTD_GAIN_RIGHT:
392a497a436SSimon Ho 	case CX2072X_PORTD_GAIN_LEFT:
393a497a436SSimon Ho 	case CX2072X_PORTE_CONNECTION_SELECT_CTRL:
394a497a436SSimon Ho 	case CX2072X_PORTE_POWER_STATE:
395a497a436SSimon Ho 	case CX2072X_PORTE_PIN_CTRL:
396a497a436SSimon Ho 	case CX2072X_PORTE_UNSOLICITED_RESPONSE:
397a497a436SSimon Ho 	case CX2072X_PORTE_PIN_SENSE:
398a497a436SSimon Ho 	case CX2072X_PORTE_EAPD_BTL:
399a497a436SSimon Ho 	case CX2072X_PORTE_GAIN_RIGHT:
400a497a436SSimon Ho 	case CX2072X_PORTE_GAIN_LEFT:
401a497a436SSimon Ho 	case CX2072X_PORTF_POWER_STATE:
402a497a436SSimon Ho 	case CX2072X_PORTF_PIN_CTRL:
403a497a436SSimon Ho 	case CX2072X_PORTF_UNSOLICITED_RESPONSE:
404a497a436SSimon Ho 	case CX2072X_PORTF_PIN_SENSE:
405a497a436SSimon Ho 	case CX2072X_PORTF_GAIN_RIGHT:
406a497a436SSimon Ho 	case CX2072X_PORTF_GAIN_LEFT:
407a497a436SSimon Ho 	case CX2072X_PORTG_POWER_STATE:
408a497a436SSimon Ho 	case CX2072X_PORTG_PIN_CTRL:
409a497a436SSimon Ho 	case CX2072X_PORTG_CONNECTION_SELECT_CTRL:
410a497a436SSimon Ho 	case CX2072X_PORTG_EAPD_BTL:
411a497a436SSimon Ho 	case CX2072X_PORTM_POWER_STATE:
412a497a436SSimon Ho 	case CX2072X_PORTM_PIN_CTRL:
413a497a436SSimon Ho 	case CX2072X_PORTM_CONNECTION_SELECT_CTRL:
414a497a436SSimon Ho 	case CX2072X_PORTM_EAPD_BTL:
415a497a436SSimon Ho 	case CX2072X_MIXER_POWER_STATE:
416a497a436SSimon Ho 	case CX2072X_MIXER_GAIN_RIGHT_0:
417a497a436SSimon Ho 	case CX2072X_MIXER_GAIN_LEFT_0:
418a497a436SSimon Ho 	case CX2072X_MIXER_GAIN_RIGHT_1:
419a497a436SSimon Ho 	case CX2072X_MIXER_GAIN_LEFT_1:
420a497a436SSimon Ho 	case CX2072X_EQ_ENABLE_BYPASS:
421a497a436SSimon Ho 	case CX2072X_EQ_B0_COEFF:
422a497a436SSimon Ho 	case CX2072X_EQ_B1_COEFF:
423a497a436SSimon Ho 	case CX2072X_EQ_B2_COEFF:
424a497a436SSimon Ho 	case CX2072X_EQ_A1_COEFF:
425a497a436SSimon Ho 	case CX2072X_EQ_A2_COEFF:
426a497a436SSimon Ho 	case CX2072X_EQ_G_COEFF:
427a497a436SSimon Ho 	case CX2072X_SPKR_DRC_ENABLE_STEP:
428a497a436SSimon Ho 	case CX2072X_SPKR_DRC_CONTROL:
429a497a436SSimon Ho 	case CX2072X_SPKR_DRC_TEST:
430a497a436SSimon Ho 	case CX2072X_DIGITAL_BIOS_TEST0:
431a497a436SSimon Ho 	case CX2072X_DIGITAL_BIOS_TEST2:
432a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL1:
433a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL2:
434a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL3:
435a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL4:
436a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL5:
437a497a436SSimon Ho 	case CX2072X_I2SPCM_CONTROL6:
438a497a436SSimon Ho 	case CX2072X_UM_INTERRUPT_CRTL_E:
439a497a436SSimon Ho 	case CX2072X_CODEC_TEST2:
440a497a436SSimon Ho 	case CX2072X_CODEC_TEST9:
441a497a436SSimon Ho 	case CX2072X_CODEC_TEST20:
442a497a436SSimon Ho 	case CX2072X_CODEC_TEST26:
443a497a436SSimon Ho 	case CX2072X_ANALOG_TEST4:
444a497a436SSimon Ho 	case CX2072X_ANALOG_TEST5:
445a497a436SSimon Ho 	case CX2072X_ANALOG_TEST6:
446a497a436SSimon Ho 	case CX2072X_ANALOG_TEST7:
447a497a436SSimon Ho 	case CX2072X_ANALOG_TEST8:
448a497a436SSimon Ho 	case CX2072X_ANALOG_TEST9:
449a497a436SSimon Ho 	case CX2072X_ANALOG_TEST10:
450a497a436SSimon Ho 	case CX2072X_ANALOG_TEST11:
451a497a436SSimon Ho 	case CX2072X_ANALOG_TEST12:
452a497a436SSimon Ho 	case CX2072X_ANALOG_TEST13:
453a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST0:
454a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST1:
455a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST11:
456a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST12:
457a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST15:
458a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST16:
459a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST17:
460a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST18:
461a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST19:
462a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST20:
463a497a436SSimon Ho 		return true;
464a497a436SSimon Ho 	default:
465a497a436SSimon Ho 		return false;
466a497a436SSimon Ho 	}
467a497a436SSimon Ho }
468a497a436SSimon Ho 
469a497a436SSimon Ho static bool cx2072x_volatile_register(struct device *dev, unsigned int reg)
470a497a436SSimon Ho {
471a497a436SSimon Ho 	switch (reg) {
472a497a436SSimon Ho 	case CX2072X_VENDOR_ID:
473a497a436SSimon Ho 	case CX2072X_REVISION_ID:
474a497a436SSimon Ho 	case CX2072X_UM_INTERRUPT_CRTL_E:
475a497a436SSimon Ho 	case CX2072X_DIGITAL_TEST11:
476a497a436SSimon Ho 	case CX2072X_PORTA_PIN_SENSE:
477a497a436SSimon Ho 	case CX2072X_PORTB_PIN_SENSE:
478a497a436SSimon Ho 	case CX2072X_PORTD_PIN_SENSE:
479a497a436SSimon Ho 	case CX2072X_PORTE_PIN_SENSE:
480a497a436SSimon Ho 	case CX2072X_PORTF_PIN_SENSE:
481a497a436SSimon Ho 	case CX2072X_EQ_G_COEFF:
482a497a436SSimon Ho 	case CX2072X_EQ_BAND:
483a497a436SSimon Ho 		return true;
484a497a436SSimon Ho 	default:
485a497a436SSimon Ho 		return false;
486a497a436SSimon Ho 	}
487a497a436SSimon Ho }
488a497a436SSimon Ho 
489a497a436SSimon Ho static int cx2072x_reg_raw_write(struct i2c_client *client,
490a497a436SSimon Ho 				 unsigned int reg,
491a497a436SSimon Ho 				 const void *val, size_t val_count)
492a497a436SSimon Ho {
493a497a436SSimon Ho 	struct device *dev = &client->dev;
494a497a436SSimon Ho 	u8 buf[2 + CX2072X_MAX_EQ_COEFF];
495a497a436SSimon Ho 	int ret;
496a497a436SSimon Ho 
497a497a436SSimon Ho 	if (WARN_ON(val_count + 2 > sizeof(buf)))
498a497a436SSimon Ho 		return -EINVAL;
499a497a436SSimon Ho 
500a497a436SSimon Ho 	buf[0] = reg >> 8;
501a497a436SSimon Ho 	buf[1] = reg & 0xff;
502a497a436SSimon Ho 
503a497a436SSimon Ho 	memcpy(buf + 2, val, val_count);
504a497a436SSimon Ho 
505a497a436SSimon Ho 	ret = i2c_master_send(client, buf, val_count + 2);
506a497a436SSimon Ho 	if (ret != val_count + 2) {
507a497a436SSimon Ho 		dev_err(dev, "I2C write failed, ret = %d\n", ret);
508a497a436SSimon Ho 		return ret < 0 ? ret : -EIO;
509a497a436SSimon Ho 	}
510a497a436SSimon Ho 	return 0;
511a497a436SSimon Ho }
512a497a436SSimon Ho 
513a497a436SSimon Ho static int cx2072x_reg_write(void *context, unsigned int reg,
514a497a436SSimon Ho 			     unsigned int value)
515a497a436SSimon Ho {
516a497a436SSimon Ho 	__le32 raw_value;
517a497a436SSimon Ho 	unsigned int size;
518a497a436SSimon Ho 
519a497a436SSimon Ho 	size = cx2072x_register_size(reg);
520a497a436SSimon Ho 
521a497a436SSimon Ho 	if (reg == CX2072X_UM_INTERRUPT_CRTL_E) {
522a497a436SSimon Ho 		/* Update the MSB byte only */
523a497a436SSimon Ho 		reg += 3;
524a497a436SSimon Ho 		size = 1;
525a497a436SSimon Ho 		value >>= 24;
526a497a436SSimon Ho 	}
527a497a436SSimon Ho 
528a497a436SSimon Ho 	raw_value = cpu_to_le32(value);
529a497a436SSimon Ho 	return cx2072x_reg_raw_write(context, reg, &raw_value, size);
530a497a436SSimon Ho }
531a497a436SSimon Ho 
532a497a436SSimon Ho static int cx2072x_reg_read(void *context, unsigned int reg,
533a497a436SSimon Ho 			    unsigned int *value)
534a497a436SSimon Ho {
535a497a436SSimon Ho 	struct i2c_client *client = context;
536a497a436SSimon Ho 	struct device *dev = &client->dev;
537a497a436SSimon Ho 	__le32 recv_buf = 0;
538a497a436SSimon Ho 	struct i2c_msg msgs[2];
539a497a436SSimon Ho 	unsigned int size;
540a497a436SSimon Ho 	u8 send_buf[2];
541a497a436SSimon Ho 	int ret;
542a497a436SSimon Ho 
543a497a436SSimon Ho 	size = cx2072x_register_size(reg);
544a497a436SSimon Ho 
545a497a436SSimon Ho 	send_buf[0] = reg >> 8;
546a497a436SSimon Ho 	send_buf[1] = reg & 0xff;
547a497a436SSimon Ho 
548a497a436SSimon Ho 	msgs[0].addr = client->addr;
549a497a436SSimon Ho 	msgs[0].len = sizeof(send_buf);
550a497a436SSimon Ho 	msgs[0].buf = send_buf;
551a497a436SSimon Ho 	msgs[0].flags = 0;
552a497a436SSimon Ho 
553a497a436SSimon Ho 	msgs[1].addr = client->addr;
554a497a436SSimon Ho 	msgs[1].len = size;
555a497a436SSimon Ho 	msgs[1].buf = (u8 *)&recv_buf;
556a497a436SSimon Ho 	msgs[1].flags = I2C_M_RD;
557a497a436SSimon Ho 
558a497a436SSimon Ho 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
559a497a436SSimon Ho 	if (ret != ARRAY_SIZE(msgs)) {
560a497a436SSimon Ho 		dev_err(dev, "Failed to read register, ret = %d\n", ret);
561a497a436SSimon Ho 		return ret < 0 ? ret : -EIO;
562a497a436SSimon Ho 	}
563a497a436SSimon Ho 
564a497a436SSimon Ho 	*value = le32_to_cpu(recv_buf);
565a497a436SSimon Ho 	return 0;
566a497a436SSimon Ho }
567a497a436SSimon Ho 
568a497a436SSimon Ho /* get suggested pre_div valuce from mclk frequency */
569a497a436SSimon Ho static unsigned int get_div_from_mclk(unsigned int mclk)
570a497a436SSimon Ho {
571a497a436SSimon Ho 	unsigned int div = 8;
572a497a436SSimon Ho 	int i;
573a497a436SSimon Ho 
574a497a436SSimon Ho 	for (i = 0; i < ARRAY_SIZE(mclk_pre_div); i++) {
575a497a436SSimon Ho 		if (mclk <= mclk_pre_div[i].mclk) {
576a497a436SSimon Ho 			div = mclk_pre_div[i].div;
577a497a436SSimon Ho 			break;
578a497a436SSimon Ho 		}
579a497a436SSimon Ho 	}
580a497a436SSimon Ho 	return div;
581a497a436SSimon Ho }
582a497a436SSimon Ho 
583a497a436SSimon Ho static int cx2072x_config_pll(struct cx2072x_priv *cx2072x)
584a497a436SSimon Ho {
585a497a436SSimon Ho 	struct device *dev = cx2072x->dev;
586a497a436SSimon Ho 	unsigned int pre_div;
587a497a436SSimon Ho 	unsigned int pre_div_val;
588a497a436SSimon Ho 	unsigned int pll_input;
589a497a436SSimon Ho 	unsigned int pll_output;
590a497a436SSimon Ho 	unsigned int int_div;
591a497a436SSimon Ho 	unsigned int frac_div;
592a497a436SSimon Ho 	u64 frac_num;
593a497a436SSimon Ho 	unsigned int frac;
594a497a436SSimon Ho 	unsigned int sample_rate = cx2072x->sample_rate;
595a497a436SSimon Ho 	int pt_sample_per_sync = 2;
596a497a436SSimon Ho 	int pt_clock_per_sample = 96;
597a497a436SSimon Ho 
598a497a436SSimon Ho 	switch (sample_rate) {
599a497a436SSimon Ho 	case 48000:
600a497a436SSimon Ho 	case 32000:
601a497a436SSimon Ho 	case 24000:
602a497a436SSimon Ho 	case 16000:
603a497a436SSimon Ho 		break;
604a497a436SSimon Ho 
605a497a436SSimon Ho 	case 96000:
606a497a436SSimon Ho 		pt_sample_per_sync = 1;
607a497a436SSimon Ho 		pt_clock_per_sample = 48;
608a497a436SSimon Ho 		break;
609a497a436SSimon Ho 
610a497a436SSimon Ho 	case 192000:
611a497a436SSimon Ho 		pt_sample_per_sync = 0;
612a497a436SSimon Ho 		pt_clock_per_sample = 24;
613a497a436SSimon Ho 		break;
614a497a436SSimon Ho 
615a497a436SSimon Ho 	default:
616a497a436SSimon Ho 		dev_err(dev, "Unsupported sample rate %d\n", sample_rate);
617a497a436SSimon Ho 		return -EINVAL;
618a497a436SSimon Ho 	}
619a497a436SSimon Ho 
620a497a436SSimon Ho 	/* Configure PLL settings */
621a497a436SSimon Ho 	pre_div = get_div_from_mclk(cx2072x->mclk_rate);
622a497a436SSimon Ho 	pll_input = cx2072x->mclk_rate / pre_div;
623a497a436SSimon Ho 	pll_output = sample_rate * 3072;
624a497a436SSimon Ho 	int_div = pll_output / pll_input;
625a497a436SSimon Ho 	frac_div = pll_output - (int_div * pll_input);
626a497a436SSimon Ho 
627a497a436SSimon Ho 	if (frac_div) {
628a497a436SSimon Ho 		frac_div *= 1000;
629a497a436SSimon Ho 		frac_div /= pll_input;
630a497a436SSimon Ho 		frac_num = ((4000 + frac_div) * ((1 << 20) - 4));
631a497a436SSimon Ho 		do_div(frac_num, 7);
632a497a436SSimon Ho 		frac = ((u32)frac_num + 499) / 1000;
633a497a436SSimon Ho 	}
634a497a436SSimon Ho 	pre_div_val = (pre_div - 1) * 2;
635a497a436SSimon Ho 
636a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST4,
637a497a436SSimon Ho 		     0x40 | (pre_div_val << 8));
638a497a436SSimon Ho 	if (frac_div == 0) {
639a497a436SSimon Ho 		/* Int mode */
640a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST7, 0x100);
641a497a436SSimon Ho 	} else {
642a497a436SSimon Ho 		/* frac mode */
643a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST6,
644a497a436SSimon Ho 			     frac & 0xfff);
645a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST7,
646a497a436SSimon Ho 			     (u8)(frac >> 12));
647a497a436SSimon Ho 	}
648a497a436SSimon Ho 
649a497a436SSimon Ho 	int_div--;
650a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST8, int_div);
651a497a436SSimon Ho 
652a497a436SSimon Ho 	/* configure PLL tracking */
653a497a436SSimon Ho 	if (frac_div == 0) {
654a497a436SSimon Ho 		/* disable PLL tracking */
655a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST16, 0x00);
656a497a436SSimon Ho 	} else {
657a497a436SSimon Ho 		/* configure and enable PLL tracking */
658a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST16,
659a497a436SSimon Ho 			     (pt_sample_per_sync << 4) & 0xf0);
660a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST17,
661a497a436SSimon Ho 			     pt_clock_per_sample);
662a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST18,
663a497a436SSimon Ho 			     pt_clock_per_sample * 3 / 2);
664a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST19, 0x01);
665a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST20, 0x02);
666a497a436SSimon Ho 		regmap_update_bits(cx2072x->regmap, CX2072X_DIGITAL_TEST16,
667a497a436SSimon Ho 				   0x01, 0x01);
668a497a436SSimon Ho 	}
669a497a436SSimon Ho 
670a497a436SSimon Ho 	return 0;
671a497a436SSimon Ho }
672a497a436SSimon Ho 
673a497a436SSimon Ho static int cx2072x_config_i2spcm(struct cx2072x_priv *cx2072x)
674a497a436SSimon Ho {
675a497a436SSimon Ho 	struct device *dev = cx2072x->dev;
676a497a436SSimon Ho 	unsigned int bclk_rate = 0;
677a497a436SSimon Ho 	int is_i2s = 0;
678a497a436SSimon Ho 	int has_one_bit_delay = 0;
679a497a436SSimon Ho 	int is_right_j = 0;
680a497a436SSimon Ho 	int is_frame_inv = 0;
681a497a436SSimon Ho 	int is_bclk_inv = 0;
682a497a436SSimon Ho 	int pulse_len = 1;
683a497a436SSimon Ho 	int frame_len = cx2072x->frame_size;
684a497a436SSimon Ho 	int sample_size = cx2072x->sample_size;
685a497a436SSimon Ho 	int i2s_right_slot;
686a497a436SSimon Ho 	int i2s_right_pause_interval = 0;
687a497a436SSimon Ho 	int i2s_right_pause_pos;
688a497a436SSimon Ho 	int is_big_endian = 1;
689a497a436SSimon Ho 	u64 div;
690a497a436SSimon Ho 	unsigned int mod;
691a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg1 reg1;
692a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg2 reg2;
693a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg3 reg3;
694a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg4 reg4;
695a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg5 reg5;
696a497a436SSimon Ho 	union cx2072x_reg_i2spcm_ctrl_reg6 reg6;
697a497a436SSimon Ho 	union cx2072x_reg_digital_bios_test2 regdbt2;
698a497a436SSimon Ho 	const unsigned int fmt = cx2072x->dai_fmt;
699a497a436SSimon Ho 
700a497a436SSimon Ho 	if (frame_len <= 0) {
701a497a436SSimon Ho 		dev_err(dev, "Incorrect frame len %d\n", frame_len);
702a497a436SSimon Ho 		return -EINVAL;
703a497a436SSimon Ho 	}
704a497a436SSimon Ho 
705a497a436SSimon Ho 	if (sample_size <= 0) {
706a497a436SSimon Ho 		dev_err(dev, "Incorrect sample size %d\n", sample_size);
707a497a436SSimon Ho 		return -EINVAL;
708a497a436SSimon Ho 	}
709a497a436SSimon Ho 
710a497a436SSimon Ho 	dev_dbg(dev, "config_i2spcm set_dai_fmt- %08x\n", fmt);
711a497a436SSimon Ho 
712a497a436SSimon Ho 	regdbt2.ulval = 0xac;
713a497a436SSimon Ho 
714a497a436SSimon Ho 	/* set master/slave */
715a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
716a497a436SSimon Ho 	case SND_SOC_DAIFMT_CBM_CFM:
717a497a436SSimon Ho 		reg2.r.tx_master = 1;
718a497a436SSimon Ho 		reg3.r.rx_master = 1;
719a497a436SSimon Ho 		dev_dbg(dev, "Sets Master mode\n");
720a497a436SSimon Ho 		break;
721a497a436SSimon Ho 
722a497a436SSimon Ho 	case SND_SOC_DAIFMT_CBS_CFS:
723a497a436SSimon Ho 		reg2.r.tx_master = 0;
724a497a436SSimon Ho 		reg3.r.rx_master = 0;
725a497a436SSimon Ho 		dev_dbg(dev, "Sets Slave mode\n");
726a497a436SSimon Ho 		break;
727a497a436SSimon Ho 
728a497a436SSimon Ho 	default:
729a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI master mode\n");
730a497a436SSimon Ho 		return -EINVAL;
731a497a436SSimon Ho 	}
732a497a436SSimon Ho 
733a497a436SSimon Ho 	/* set format */
734a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
735a497a436SSimon Ho 	case SND_SOC_DAIFMT_I2S:
736a497a436SSimon Ho 		is_i2s = 1;
737a497a436SSimon Ho 		has_one_bit_delay = 1;
738a497a436SSimon Ho 		pulse_len = frame_len / 2;
739a497a436SSimon Ho 		break;
740a497a436SSimon Ho 
741a497a436SSimon Ho 	case SND_SOC_DAIFMT_RIGHT_J:
742a497a436SSimon Ho 		is_i2s = 1;
743a497a436SSimon Ho 		is_right_j = 1;
744a497a436SSimon Ho 		pulse_len = frame_len / 2;
745a497a436SSimon Ho 		break;
746a497a436SSimon Ho 
747a497a436SSimon Ho 	case SND_SOC_DAIFMT_LEFT_J:
748a497a436SSimon Ho 		is_i2s = 1;
749a497a436SSimon Ho 		pulse_len = frame_len / 2;
750a497a436SSimon Ho 		break;
751a497a436SSimon Ho 
752a497a436SSimon Ho 	default:
753a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI format\n");
754a497a436SSimon Ho 		return -EINVAL;
755a497a436SSimon Ho 	}
756a497a436SSimon Ho 
757a497a436SSimon Ho 	/* clock inversion */
758a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
759a497a436SSimon Ho 	case SND_SOC_DAIFMT_NB_NF:
760a497a436SSimon Ho 		is_frame_inv = is_i2s;
761a497a436SSimon Ho 		is_bclk_inv = is_i2s;
762a497a436SSimon Ho 		break;
763a497a436SSimon Ho 
764a497a436SSimon Ho 	case SND_SOC_DAIFMT_IB_IF:
765a497a436SSimon Ho 		is_frame_inv = !is_i2s;
766a497a436SSimon Ho 		is_bclk_inv = !is_i2s;
767a497a436SSimon Ho 		break;
768a497a436SSimon Ho 
769a497a436SSimon Ho 	case SND_SOC_DAIFMT_IB_NF:
770a497a436SSimon Ho 		is_frame_inv = is_i2s;
771a497a436SSimon Ho 		is_bclk_inv = !is_i2s;
772a497a436SSimon Ho 		break;
773a497a436SSimon Ho 
774a497a436SSimon Ho 	case SND_SOC_DAIFMT_NB_IF:
775a497a436SSimon Ho 		is_frame_inv = !is_i2s;
776a497a436SSimon Ho 		is_bclk_inv = is_i2s;
777a497a436SSimon Ho 		break;
778a497a436SSimon Ho 
779a497a436SSimon Ho 	default:
780a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI clock inversion\n");
781a497a436SSimon Ho 		return -EINVAL;
782a497a436SSimon Ho 	}
783a497a436SSimon Ho 
784a497a436SSimon Ho 	reg1.r.rx_data_one_line = 1;
785a497a436SSimon Ho 	reg1.r.tx_data_one_line = 1;
786a497a436SSimon Ho 
787a497a436SSimon Ho 	if (is_i2s) {
788a497a436SSimon Ho 		i2s_right_slot = (frame_len / 2) / BITS_PER_SLOT;
789a497a436SSimon Ho 		i2s_right_pause_interval = (frame_len / 2) % BITS_PER_SLOT;
790a497a436SSimon Ho 		i2s_right_pause_pos = i2s_right_slot * BITS_PER_SLOT;
791a497a436SSimon Ho 	}
792a497a436SSimon Ho 
793a497a436SSimon Ho 	reg1.r.rx_ws_pol = is_frame_inv;
794a497a436SSimon Ho 	reg1.r.rx_ws_wid = pulse_len - 1;
795a497a436SSimon Ho 
796a497a436SSimon Ho 	reg1.r.rx_frm_len = frame_len / BITS_PER_SLOT - 1;
797a497a436SSimon Ho 	reg1.r.rx_sa_size = (sample_size / BITS_PER_SLOT) - 1;
798a497a436SSimon Ho 
799a497a436SSimon Ho 	reg1.r.tx_ws_pol = reg1.r.rx_ws_pol;
800a497a436SSimon Ho 	reg1.r.tx_ws_wid = pulse_len - 1;
801a497a436SSimon Ho 	reg1.r.tx_frm_len = reg1.r.rx_frm_len;
802a497a436SSimon Ho 	reg1.r.tx_sa_size = reg1.r.rx_sa_size;
803a497a436SSimon Ho 
804a497a436SSimon Ho 	reg2.r.tx_endian_sel = !is_big_endian;
805a497a436SSimon Ho 	reg2.r.tx_dstart_dly = has_one_bit_delay;
806a497a436SSimon Ho 	if (cx2072x->en_aec_ref)
807a497a436SSimon Ho 		reg2.r.tx_dstart_dly = 0;
808a497a436SSimon Ho 
809a497a436SSimon Ho 	reg3.r.rx_endian_sel = !is_big_endian;
810a497a436SSimon Ho 	reg3.r.rx_dstart_dly = has_one_bit_delay;
811a497a436SSimon Ho 
812a497a436SSimon Ho 	reg4.ulval = 0;
813a497a436SSimon Ho 
814a497a436SSimon Ho 	if (is_i2s) {
815a497a436SSimon Ho 		reg2.r.tx_slot_1 = 0;
816a497a436SSimon Ho 		reg2.r.tx_slot_2 = i2s_right_slot;
817a497a436SSimon Ho 		reg3.r.rx_slot_1 = 0;
818a497a436SSimon Ho 		if (cx2072x->en_aec_ref)
819a497a436SSimon Ho 			reg3.r.rx_slot_2 = 0;
820a497a436SSimon Ho 		else
821a497a436SSimon Ho 			reg3.r.rx_slot_2 = i2s_right_slot;
822a497a436SSimon Ho 		reg6.r.rx_pause_start_pos = i2s_right_pause_pos;
823a497a436SSimon Ho 		reg6.r.rx_pause_cycles = i2s_right_pause_interval;
824a497a436SSimon Ho 		reg6.r.tx_pause_start_pos = i2s_right_pause_pos;
825a497a436SSimon Ho 		reg6.r.tx_pause_cycles = i2s_right_pause_interval;
826a497a436SSimon Ho 	} else {
827a497a436SSimon Ho 		dev_err(dev, "TDM mode is not implemented yet\n");
828a497a436SSimon Ho 		return -EINVAL;
829a497a436SSimon Ho 	}
830a497a436SSimon Ho 	regdbt2.r.i2s_bclk_invert = is_bclk_inv;
831a497a436SSimon Ho 
832a497a436SSimon Ho 	reg1.r.rx_data_one_line = 1;
833a497a436SSimon Ho 	reg1.r.tx_data_one_line = 1;
834a497a436SSimon Ho 
835a497a436SSimon Ho 	/* Configures the BCLK output */
836a497a436SSimon Ho 	bclk_rate = cx2072x->sample_rate * frame_len;
837a497a436SSimon Ho 	reg5.r.i2s_pcm_clk_div_chan_en = 0;
838a497a436SSimon Ho 
839a497a436SSimon Ho 	/* Disables bclk output before setting new value */
840a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_I2SPCM_CONTROL5, 0);
841a497a436SSimon Ho 
842a497a436SSimon Ho 	if (reg2.r.tx_master) {
843a497a436SSimon Ho 		/* Configures BCLK rate */
844a497a436SSimon Ho 		div = PLL_OUT_HZ_48;
845a497a436SSimon Ho 		mod = do_div(div, bclk_rate);
846a497a436SSimon Ho 		if (mod) {
847a497a436SSimon Ho 			dev_err(dev, "Unsupported BCLK %dHz\n", bclk_rate);
848a497a436SSimon Ho 			return -EINVAL;
849a497a436SSimon Ho 		}
850a497a436SSimon Ho 		dev_dbg(dev, "enables BCLK %dHz output\n", bclk_rate);
851a497a436SSimon Ho 		reg5.r.i2s_pcm_clk_div = (u32)div - 1;
852a497a436SSimon Ho 		reg5.r.i2s_pcm_clk_div_chan_en = 1;
853a497a436SSimon Ho 	}
854a497a436SSimon Ho 
855a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_I2SPCM_CONTROL1, reg1.ulval);
856a497a436SSimon Ho 	regmap_update_bits(cx2072x->regmap, CX2072X_I2SPCM_CONTROL2, 0xffffffc0,
857a497a436SSimon Ho 			   reg2.ulval);
858a497a436SSimon Ho 	regmap_update_bits(cx2072x->regmap, CX2072X_I2SPCM_CONTROL3, 0xffffffc0,
859a497a436SSimon Ho 			   reg3.ulval);
860a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_I2SPCM_CONTROL4, reg4.ulval);
861a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_I2SPCM_CONTROL6, reg6.ulval);
862a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_I2SPCM_CONTROL5, reg5.ulval);
863a497a436SSimon Ho 
864a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_DIGITAL_BIOS_TEST2,
865a497a436SSimon Ho 		     regdbt2.ulval);
866a497a436SSimon Ho 
867a497a436SSimon Ho 	return 0;
868a497a436SSimon Ho }
869a497a436SSimon Ho 
870a497a436SSimon Ho static int afg_power_ev(struct snd_soc_dapm_widget *w,
871a497a436SSimon Ho 			struct snd_kcontrol *kcontrol, int event)
872a497a436SSimon Ho {
873a497a436SSimon Ho 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
874a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
875a497a436SSimon Ho 
876a497a436SSimon Ho 	switch (event) {
877a497a436SSimon Ho 	case SND_SOC_DAPM_POST_PMU:
878a497a436SSimon Ho 		regmap_update_bits(cx2072x->regmap, CX2072X_DIGITAL_BIOS_TEST0,
879a497a436SSimon Ho 				   0x00, 0x10);
880a497a436SSimon Ho 		break;
881a497a436SSimon Ho 
882a497a436SSimon Ho 	case SND_SOC_DAPM_PRE_PMD:
883a497a436SSimon Ho 		regmap_update_bits(cx2072x->regmap, CX2072X_DIGITAL_BIOS_TEST0,
884a497a436SSimon Ho 				   0x10, 0x10);
885a497a436SSimon Ho 		break;
886a497a436SSimon Ho 	}
887a497a436SSimon Ho 
888a497a436SSimon Ho 	return 0;
889a497a436SSimon Ho }
890a497a436SSimon Ho 
891a497a436SSimon Ho static const struct snd_kcontrol_new cx2072x_snd_controls[] = {
892a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortD Boost Volume", CX2072X_PORTD_GAIN_LEFT,
893a497a436SSimon Ho 			 CX2072X_PORTD_GAIN_RIGHT, 0, 3, 0, boost_tlv),
894a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortC Boost Volume", CX2072X_PORTC_GAIN_LEFT,
895a497a436SSimon Ho 			 CX2072X_PORTC_GAIN_RIGHT, 0, 3, 0, boost_tlv),
896a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortB Boost Volume", CX2072X_PORTB_GAIN_LEFT,
897a497a436SSimon Ho 			 CX2072X_PORTB_GAIN_RIGHT, 0, 3, 0, boost_tlv),
898a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortD ADC1 Volume", CX2072X_ADC1_AMP_GAIN_LEFT_1,
899a497a436SSimon Ho 			 CX2072X_ADC1_AMP_GAIN_RIGHT_1, 0, 0x4a, 0, adc_tlv),
900a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortC ADC1 Volume", CX2072X_ADC1_AMP_GAIN_LEFT_2,
901a497a436SSimon Ho 			 CX2072X_ADC1_AMP_GAIN_RIGHT_2, 0, 0x4a, 0, adc_tlv),
902a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("PortB ADC1 Volume", CX2072X_ADC1_AMP_GAIN_LEFT_0,
903a497a436SSimon Ho 			 CX2072X_ADC1_AMP_GAIN_RIGHT_0, 0, 0x4a, 0, adc_tlv),
904a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("DAC1 Volume", CX2072X_DAC1_AMP_GAIN_LEFT,
905a497a436SSimon Ho 			 CX2072X_DAC1_AMP_GAIN_RIGHT, 0, 0x4a, 0, dac_tlv),
906a497a436SSimon Ho 	SOC_DOUBLE_R("DAC1 Switch", CX2072X_DAC1_AMP_GAIN_LEFT,
907a497a436SSimon Ho 		     CX2072X_DAC1_AMP_GAIN_RIGHT, 7,  1, 0),
908a497a436SSimon Ho 	SOC_DOUBLE_R_TLV("DAC2 Volume", CX2072X_DAC2_AMP_GAIN_LEFT,
909a497a436SSimon Ho 			 CX2072X_DAC2_AMP_GAIN_RIGHT, 0, 0x4a, 0, dac_tlv),
910a497a436SSimon Ho 	SOC_SINGLE_TLV("HPF Freq", CX2072X_CODEC_TEST9, 0, 0x3f, 0, hpf_tlv),
911a497a436SSimon Ho 	SOC_DOUBLE("HPF Switch", CX2072X_CODEC_TEST9, 8, 9, 1, 1),
912a497a436SSimon Ho 	SOC_SINGLE("PortA HP Amp Switch", CX2072X_PORTA_PIN_CTRL, 7, 1, 0),
913a497a436SSimon Ho };
914a497a436SSimon Ho 
915a497a436SSimon Ho static int cx2072x_hw_params(struct snd_pcm_substream *substream,
916a497a436SSimon Ho 			     struct snd_pcm_hw_params *params,
917a497a436SSimon Ho 			     struct snd_soc_dai *dai)
918a497a436SSimon Ho {
919a497a436SSimon Ho 	struct snd_soc_component *codec = dai->component;
920a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
921a497a436SSimon Ho 	struct device *dev = codec->dev;
922a497a436SSimon Ho 	const unsigned int sample_rate = params_rate(params);
923a497a436SSimon Ho 	int sample_size, frame_size;
924a497a436SSimon Ho 
925a497a436SSimon Ho 	/* Data sizes if not using TDM */
926a497a436SSimon Ho 	sample_size = params_width(params);
927a497a436SSimon Ho 
928a497a436SSimon Ho 	if (sample_size < 0)
929a497a436SSimon Ho 		return sample_size;
930a497a436SSimon Ho 
931a497a436SSimon Ho 	frame_size = snd_soc_params_to_frame_size(params);
932a497a436SSimon Ho 	if (frame_size < 0)
933a497a436SSimon Ho 		return frame_size;
934a497a436SSimon Ho 
935a497a436SSimon Ho 	if (cx2072x->mclk_rate == 0) {
936a497a436SSimon Ho 		dev_err(dev, "Master clock rate is not configued\n");
937a497a436SSimon Ho 		return -EINVAL;
938a497a436SSimon Ho 	}
939a497a436SSimon Ho 
940a497a436SSimon Ho 	if (cx2072x->bclk_ratio)
941a497a436SSimon Ho 		frame_size = cx2072x->bclk_ratio;
942a497a436SSimon Ho 
943a497a436SSimon Ho 	switch (sample_rate) {
944a497a436SSimon Ho 	case 48000:
945a497a436SSimon Ho 	case 32000:
946a497a436SSimon Ho 	case 24000:
947a497a436SSimon Ho 	case 16000:
948a497a436SSimon Ho 	case 96000:
949a497a436SSimon Ho 	case 192000:
950a497a436SSimon Ho 		break;
951a497a436SSimon Ho 
952a497a436SSimon Ho 	default:
953a497a436SSimon Ho 		dev_err(dev, "Unsupported sample rate %d\n", sample_rate);
954a497a436SSimon Ho 		return -EINVAL;
955a497a436SSimon Ho 	}
956a497a436SSimon Ho 
957a497a436SSimon Ho 	dev_dbg(dev, "Sample size %d bits, frame = %d bits, rate = %d Hz\n",
958a497a436SSimon Ho 		sample_size, frame_size, sample_rate);
959a497a436SSimon Ho 
960a497a436SSimon Ho 	cx2072x->frame_size = frame_size;
961a497a436SSimon Ho 	cx2072x->sample_size = sample_size;
962a497a436SSimon Ho 	cx2072x->sample_rate = sample_rate;
963a497a436SSimon Ho 
964a497a436SSimon Ho 	if (dai->id == CX2072X_DAI_DSP) {
965a497a436SSimon Ho 		cx2072x->en_aec_ref = true;
966a497a436SSimon Ho 		dev_dbg(cx2072x->dev, "enables aec reference\n");
967a497a436SSimon Ho 		regmap_write(cx2072x->regmap,
968a497a436SSimon Ho 			     CX2072X_ADC1_CONNECTION_SELECT_CONTROL, 3);
969a497a436SSimon Ho 	}
970a497a436SSimon Ho 
971a497a436SSimon Ho 	if (cx2072x->pll_changed) {
972a497a436SSimon Ho 		cx2072x_config_pll(cx2072x);
973a497a436SSimon Ho 		cx2072x->pll_changed = false;
974a497a436SSimon Ho 	}
975a497a436SSimon Ho 
976a497a436SSimon Ho 	if (cx2072x->i2spcm_changed) {
977a497a436SSimon Ho 		cx2072x_config_i2spcm(cx2072x);
978a497a436SSimon Ho 		cx2072x->i2spcm_changed = false;
979a497a436SSimon Ho 	}
980a497a436SSimon Ho 
981a497a436SSimon Ho 	return 0;
982a497a436SSimon Ho }
983a497a436SSimon Ho 
984a497a436SSimon Ho static int cx2072x_set_dai_bclk_ratio(struct snd_soc_dai *dai,
985a497a436SSimon Ho 				      unsigned int ratio)
986a497a436SSimon Ho {
987a497a436SSimon Ho 	struct snd_soc_component *codec = dai->component;
988a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
989a497a436SSimon Ho 
990a497a436SSimon Ho 	cx2072x->bclk_ratio = ratio;
991a497a436SSimon Ho 	return 0;
992a497a436SSimon Ho }
993a497a436SSimon Ho 
994a497a436SSimon Ho static int cx2072x_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
995a497a436SSimon Ho 				  unsigned int freq, int dir)
996a497a436SSimon Ho {
997a497a436SSimon Ho 	struct snd_soc_component *codec = dai->component;
998a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
999a497a436SSimon Ho 
1000a497a436SSimon Ho 	if (clk_set_rate(cx2072x->mclk, freq)) {
1001a497a436SSimon Ho 		dev_err(codec->dev, "set clk rate failed\n");
1002a497a436SSimon Ho 		return -EINVAL;
1003a497a436SSimon Ho 	}
1004a497a436SSimon Ho 
1005a497a436SSimon Ho 	cx2072x->mclk_rate = freq;
1006a497a436SSimon Ho 	return 0;
1007a497a436SSimon Ho }
1008a497a436SSimon Ho 
1009a497a436SSimon Ho static int cx2072x_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1010a497a436SSimon Ho {
1011a497a436SSimon Ho 	struct snd_soc_component *codec = dai->component;
1012a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1013a497a436SSimon Ho 	struct device *dev = codec->dev;
1014a497a436SSimon Ho 
1015a497a436SSimon Ho 	dev_dbg(dev, "set_dai_fmt- %08x\n", fmt);
1016a497a436SSimon Ho 	/* set master/slave */
1017a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1018a497a436SSimon Ho 	case SND_SOC_DAIFMT_CBM_CFM:
1019a497a436SSimon Ho 	case SND_SOC_DAIFMT_CBS_CFS:
1020a497a436SSimon Ho 		break;
1021a497a436SSimon Ho 
1022a497a436SSimon Ho 	default:
1023a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI master mode\n");
1024a497a436SSimon Ho 		return -EINVAL;
1025a497a436SSimon Ho 	}
1026a497a436SSimon Ho 
1027a497a436SSimon Ho 	/* set format */
1028a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1029a497a436SSimon Ho 	case SND_SOC_DAIFMT_I2S:
1030a497a436SSimon Ho 	case SND_SOC_DAIFMT_RIGHT_J:
1031a497a436SSimon Ho 	case SND_SOC_DAIFMT_LEFT_J:
1032a497a436SSimon Ho 		break;
1033a497a436SSimon Ho 
1034a497a436SSimon Ho 	default:
1035a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI format\n");
1036a497a436SSimon Ho 		return -EINVAL;
1037a497a436SSimon Ho 	}
1038a497a436SSimon Ho 
1039a497a436SSimon Ho 	/* clock inversion */
1040a497a436SSimon Ho 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1041a497a436SSimon Ho 	case SND_SOC_DAIFMT_NB_NF:
1042a497a436SSimon Ho 	case SND_SOC_DAIFMT_IB_IF:
1043a497a436SSimon Ho 	case SND_SOC_DAIFMT_IB_NF:
1044a497a436SSimon Ho 	case SND_SOC_DAIFMT_NB_IF:
1045a497a436SSimon Ho 		break;
1046a497a436SSimon Ho 
1047a497a436SSimon Ho 	default:
1048a497a436SSimon Ho 		dev_err(dev, "Unsupported DAI clock inversion\n");
1049a497a436SSimon Ho 		return -EINVAL;
1050a497a436SSimon Ho 	}
1051a497a436SSimon Ho 
1052a497a436SSimon Ho 	cx2072x->dai_fmt = fmt;
1053a497a436SSimon Ho 	return 0;
1054a497a436SSimon Ho }
1055a497a436SSimon Ho 
1056a497a436SSimon Ho static const struct snd_kcontrol_new portaouten_ctl =
1057a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTA_PIN_CTRL, 6, 1, 0);
1058a497a436SSimon Ho 
1059a497a436SSimon Ho static const struct snd_kcontrol_new porteouten_ctl =
1060a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTE_PIN_CTRL, 6, 1, 0);
1061a497a436SSimon Ho 
1062a497a436SSimon Ho static const struct snd_kcontrol_new portgouten_ctl =
1063a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTG_PIN_CTRL, 6, 1, 0);
1064a497a436SSimon Ho 
1065a497a436SSimon Ho static const struct snd_kcontrol_new portmouten_ctl =
1066a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTM_PIN_CTRL, 6, 1, 0);
1067a497a436SSimon Ho 
1068a497a436SSimon Ho static const struct snd_kcontrol_new portbinen_ctl =
1069a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTB_PIN_CTRL, 5, 1, 0);
1070a497a436SSimon Ho 
1071a497a436SSimon Ho static const struct snd_kcontrol_new portcinen_ctl =
1072a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTC_PIN_CTRL, 5, 1, 0);
1073a497a436SSimon Ho 
1074a497a436SSimon Ho static const struct snd_kcontrol_new portdinen_ctl =
1075a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTD_PIN_CTRL, 5, 1, 0);
1076a497a436SSimon Ho 
1077a497a436SSimon Ho static const struct snd_kcontrol_new porteinen_ctl =
1078a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_PORTE_PIN_CTRL, 5, 1, 0);
1079a497a436SSimon Ho 
1080a497a436SSimon Ho static const struct snd_kcontrol_new i2sadc1l_ctl =
1081a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL2, 0, 1, 0);
1082a497a436SSimon Ho 
1083a497a436SSimon Ho static const struct snd_kcontrol_new i2sadc1r_ctl =
1084a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL2, 1, 1, 0);
1085a497a436SSimon Ho 
1086a497a436SSimon Ho static const struct snd_kcontrol_new i2sadc2l_ctl =
1087a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL2, 2, 1, 0);
1088a497a436SSimon Ho 
1089a497a436SSimon Ho static const struct snd_kcontrol_new i2sadc2r_ctl =
1090a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL2, 3, 1, 0);
1091a497a436SSimon Ho 
1092a497a436SSimon Ho static const struct snd_kcontrol_new i2sdac1l_ctl =
1093a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL3, 0, 1, 0);
1094a497a436SSimon Ho 
1095a497a436SSimon Ho static const struct snd_kcontrol_new i2sdac1r_ctl =
1096a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL3, 1, 1, 0);
1097a497a436SSimon Ho 
1098a497a436SSimon Ho static const struct snd_kcontrol_new i2sdac2l_ctl =
1099a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL3, 2, 1, 0);
1100a497a436SSimon Ho 
1101a497a436SSimon Ho static const struct snd_kcontrol_new i2sdac2r_ctl =
1102a497a436SSimon Ho 	SOC_DAPM_SINGLE("Switch", CX2072X_I2SPCM_CONTROL3, 3, 1, 0);
1103a497a436SSimon Ho 
1104a497a436SSimon Ho static const char * const dac_enum_text[] = {
1105a497a436SSimon Ho 	"DAC1 Switch", "DAC2 Switch",
1106a497a436SSimon Ho };
1107a497a436SSimon Ho 
1108a497a436SSimon Ho static const struct soc_enum porta_dac_enum =
1109a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_PORTA_CONNECTION_SELECT_CTRL, 0, 2, dac_enum_text);
1110a497a436SSimon Ho 
1111a497a436SSimon Ho static const struct snd_kcontrol_new porta_mux =
1112a497a436SSimon Ho SOC_DAPM_ENUM("PortA Mux", porta_dac_enum);
1113a497a436SSimon Ho 
1114a497a436SSimon Ho static const struct soc_enum portg_dac_enum =
1115a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_PORTG_CONNECTION_SELECT_CTRL, 0, 2, dac_enum_text);
1116a497a436SSimon Ho 
1117a497a436SSimon Ho static const struct snd_kcontrol_new portg_mux =
1118a497a436SSimon Ho SOC_DAPM_ENUM("PortG Mux", portg_dac_enum);
1119a497a436SSimon Ho 
1120a497a436SSimon Ho static const struct soc_enum porte_dac_enum =
1121a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_PORTE_CONNECTION_SELECT_CTRL, 0, 2, dac_enum_text);
1122a497a436SSimon Ho 
1123a497a436SSimon Ho static const struct snd_kcontrol_new porte_mux =
1124a497a436SSimon Ho SOC_DAPM_ENUM("PortE Mux", porte_dac_enum);
1125a497a436SSimon Ho 
1126a497a436SSimon Ho static const struct soc_enum portm_dac_enum =
1127a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_PORTM_CONNECTION_SELECT_CTRL, 0, 2, dac_enum_text);
1128a497a436SSimon Ho 
1129a497a436SSimon Ho static const struct snd_kcontrol_new portm_mux =
1130a497a436SSimon Ho SOC_DAPM_ENUM("PortM Mux", portm_dac_enum);
1131a497a436SSimon Ho 
1132a497a436SSimon Ho static const char * const adc1in_sel_text[] = {
1133a497a436SSimon Ho 	"PortB Switch", "PortD Switch", "PortC Switch", "Widget15 Switch",
1134a497a436SSimon Ho 	"PortE Switch", "PortF Switch", "PortH Switch"
1135a497a436SSimon Ho };
1136a497a436SSimon Ho 
1137a497a436SSimon Ho static const struct soc_enum adc1in_sel_enum =
1138a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_ADC1_CONNECTION_SELECT_CONTROL, 0, 7, adc1in_sel_text);
1139a497a436SSimon Ho 
1140a497a436SSimon Ho static const struct snd_kcontrol_new adc1_mux =
1141a497a436SSimon Ho SOC_DAPM_ENUM("ADC1 Mux", adc1in_sel_enum);
1142a497a436SSimon Ho 
1143a497a436SSimon Ho static const char * const adc2in_sel_text[] = {
1144a497a436SSimon Ho 	"PortC Switch", "Widget15 Switch", "PortH Switch"
1145a497a436SSimon Ho };
1146a497a436SSimon Ho 
1147a497a436SSimon Ho static const struct soc_enum adc2in_sel_enum =
1148a497a436SSimon Ho SOC_ENUM_SINGLE(CX2072X_ADC2_CONNECTION_SELECT_CONTROL, 0, 3, adc2in_sel_text);
1149a497a436SSimon Ho 
1150a497a436SSimon Ho static const struct snd_kcontrol_new adc2_mux =
1151a497a436SSimon Ho SOC_DAPM_ENUM("ADC2 Mux", adc2in_sel_enum);
1152a497a436SSimon Ho 
1153a497a436SSimon Ho static const struct snd_kcontrol_new wid15_mix[] = {
1154a497a436SSimon Ho 	SOC_DAPM_SINGLE("DAC1L Switch", CX2072X_MIXER_GAIN_LEFT_0, 7, 1, 1),
1155a497a436SSimon Ho 	SOC_DAPM_SINGLE("DAC1R Switch", CX2072X_MIXER_GAIN_RIGHT_0, 7, 1, 1),
1156a497a436SSimon Ho 	SOC_DAPM_SINGLE("DAC2L Switch", CX2072X_MIXER_GAIN_LEFT_1, 7, 1, 1),
1157a497a436SSimon Ho 	SOC_DAPM_SINGLE("DAC2R Switch", CX2072X_MIXER_GAIN_RIGHT_1, 7, 1, 1),
1158a497a436SSimon Ho };
1159a497a436SSimon Ho 
1160a497a436SSimon Ho #define CX2072X_DAPM_SUPPLY_S(wname, wsubseq, wreg, wshift, wmask,  won_val, \
1161a497a436SSimon Ho 	woff_val, wevent, wflags) \
1162a497a436SSimon Ho 	{.id = snd_soc_dapm_supply, .name = wname, .kcontrol_news = NULL, \
1163a497a436SSimon Ho 	.num_kcontrols = 0, .reg = wreg, .shift = wshift, .mask = wmask, \
1164a497a436SSimon Ho 	.on_val = won_val, .off_val = woff_val, \
1165a497a436SSimon Ho 	.subseq = wsubseq, .event = wevent, .event_flags = wflags}
1166a497a436SSimon Ho 
1167a497a436SSimon Ho #define CX2072X_DAPM_SWITCH(wname,  wreg, wshift, wmask,  won_val, woff_val, \
1168a497a436SSimon Ho 	wevent, wflags) \
1169a497a436SSimon Ho 	{.id = snd_soc_dapm_switch, .name = wname, .kcontrol_news = NULL, \
1170a497a436SSimon Ho 	.num_kcontrols = 0, .reg = wreg, .shift = wshift, .mask = wmask, \
1171a497a436SSimon Ho 	.on_val = won_val, .off_val = woff_val, \
1172a497a436SSimon Ho 	.event = wevent, .event_flags = wflags}
1173a497a436SSimon Ho 
1174a497a436SSimon Ho #define CX2072X_DAPM_SWITCH(wname,  wreg, wshift, wmask,  won_val, woff_val, \
1175a497a436SSimon Ho 	wevent, wflags) \
1176a497a436SSimon Ho 	{.id = snd_soc_dapm_switch, .name = wname, .kcontrol_news = NULL, \
1177a497a436SSimon Ho 	.num_kcontrols = 0, .reg = wreg, .shift = wshift, .mask = wmask, \
1178a497a436SSimon Ho 	.on_val = won_val, .off_val = woff_val, \
1179a497a436SSimon Ho 	.event = wevent, .event_flags = wflags}
1180a497a436SSimon Ho 
1181a497a436SSimon Ho #define CX2072X_DAPM_REG_E(wid, wname, wreg, wshift, wmask, won_val, woff_val, \
1182a497a436SSimon Ho 				wevent, wflags) \
1183a497a436SSimon Ho 	{.id = wid, .name = wname, .kcontrol_news = NULL, .num_kcontrols = 0, \
1184a497a436SSimon Ho 	.reg = wreg, .shift = wshift, .mask = wmask, \
1185a497a436SSimon Ho 	.on_val = won_val, .off_val = woff_val, \
1186a497a436SSimon Ho 	.event = wevent, .event_flags = wflags}
1187a497a436SSimon Ho 
1188a497a436SSimon Ho static const struct snd_soc_dapm_widget cx2072x_dapm_widgets[] = {
1189a497a436SSimon Ho 	/*Playback*/
1190a497a436SSimon Ho 	SND_SOC_DAPM_AIF_IN("In AIF", "Playback", 0, SND_SOC_NOPM, 0, 0),
1191a497a436SSimon Ho 
1192a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S DAC1L", SND_SOC_NOPM, 0, 0, &i2sdac1l_ctl),
1193a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S DAC1R", SND_SOC_NOPM, 0, 0, &i2sdac1r_ctl),
1194a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S DAC2L", SND_SOC_NOPM, 0, 0, &i2sdac2l_ctl),
1195a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S DAC2R", SND_SOC_NOPM, 0, 0, &i2sdac2r_ctl),
1196a497a436SSimon Ho 
1197a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_dac, "DAC1", CX2072X_DAC1_POWER_STATE,
1198a497a436SSimon Ho 			 0, 0xfff, 0x00, 0x03),
1199a497a436SSimon Ho 
1200a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_dac, "DAC2", CX2072X_DAC2_POWER_STATE,
1201a497a436SSimon Ho 			 0, 0xfff, 0x00, 0x03),
1202a497a436SSimon Ho 
1203a497a436SSimon Ho 	SND_SOC_DAPM_MUX("PortA Mux", SND_SOC_NOPM, 0, 0, &porta_mux),
1204a497a436SSimon Ho 	SND_SOC_DAPM_MUX("PortG Mux", SND_SOC_NOPM, 0, 0, &portg_mux),
1205a497a436SSimon Ho 	SND_SOC_DAPM_MUX("PortE Mux", SND_SOC_NOPM, 0, 0, &porte_mux),
1206a497a436SSimon Ho 	SND_SOC_DAPM_MUX("PortM Mux", SND_SOC_NOPM, 0, 0, &portm_mux),
1207a497a436SSimon Ho 
1208a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortA Power",
1209a497a436SSimon Ho 			 CX2072X_PORTA_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1210a497a436SSimon Ho 
1211a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortM Power",
1212a497a436SSimon Ho 			 CX2072X_PORTM_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1213a497a436SSimon Ho 
1214a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortG Power",
1215a497a436SSimon Ho 			 CX2072X_PORTG_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1216a497a436SSimon Ho 
1217a497a436SSimon Ho 	CX2072X_DAPM_SUPPLY_S("AFG Power", 0, CX2072X_AFG_POWER_STATE,
1218a497a436SSimon Ho 			      0, 0xfff, 0x00, 0x03, afg_power_ev,
1219a497a436SSimon Ho 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1220a497a436SSimon Ho 
1221a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortA Out En", SND_SOC_NOPM, 0, 0,
1222a497a436SSimon Ho 			    &portaouten_ctl),
1223a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortE Out En", SND_SOC_NOPM, 0, 0,
1224a497a436SSimon Ho 			    &porteouten_ctl),
1225a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortG Out En", SND_SOC_NOPM, 0, 0,
1226a497a436SSimon Ho 			    &portgouten_ctl),
1227a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortM Out En", SND_SOC_NOPM, 0, 0,
1228a497a436SSimon Ho 			    &portmouten_ctl),
1229a497a436SSimon Ho 
1230a497a436SSimon Ho 	SND_SOC_DAPM_OUTPUT("PORTA"),
1231a497a436SSimon Ho 	SND_SOC_DAPM_OUTPUT("PORTG"),
1232a497a436SSimon Ho 	SND_SOC_DAPM_OUTPUT("PORTE"),
1233a497a436SSimon Ho 	SND_SOC_DAPM_OUTPUT("PORTM"),
1234a497a436SSimon Ho 	SND_SOC_DAPM_OUTPUT("AEC REF"),
1235a497a436SSimon Ho 
1236a497a436SSimon Ho 	/*Capture*/
1237a497a436SSimon Ho 	SND_SOC_DAPM_AIF_OUT("Out AIF", "Capture", 0, SND_SOC_NOPM, 0, 0),
1238a497a436SSimon Ho 
1239a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S ADC1L", SND_SOC_NOPM, 0, 0, &i2sadc1l_ctl),
1240a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S ADC1R", SND_SOC_NOPM, 0, 0, &i2sadc1r_ctl),
1241a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S ADC2L", SND_SOC_NOPM, 0, 0, &i2sadc2l_ctl),
1242a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("I2S ADC2R", SND_SOC_NOPM, 0, 0, &i2sadc2r_ctl),
1243a497a436SSimon Ho 
1244a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_adc, "ADC1", CX2072X_ADC1_POWER_STATE,
1245a497a436SSimon Ho 			 0, 0xff, 0x00, 0x03),
1246a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_adc, "ADC2", CX2072X_ADC2_POWER_STATE,
1247a497a436SSimon Ho 			 0, 0xff, 0x00, 0x03),
1248a497a436SSimon Ho 
1249a497a436SSimon Ho 	SND_SOC_DAPM_MUX("ADC1 Mux", SND_SOC_NOPM, 0, 0, &adc1_mux),
1250a497a436SSimon Ho 	SND_SOC_DAPM_MUX("ADC2 Mux", SND_SOC_NOPM, 0, 0, &adc2_mux),
1251a497a436SSimon Ho 
1252a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortB Power",
1253a497a436SSimon Ho 			 CX2072X_PORTB_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1254a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortC Power",
1255a497a436SSimon Ho 			 CX2072X_PORTC_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1256a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortD Power",
1257a497a436SSimon Ho 			 CX2072X_PORTD_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1258a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "PortE Power",
1259a497a436SSimon Ho 			 CX2072X_PORTE_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1260a497a436SSimon Ho 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Widget15 Power",
1261a497a436SSimon Ho 			 CX2072X_MIXER_POWER_STATE, 0, 0xfff, 0x00, 0x03),
1262a497a436SSimon Ho 
1263a497a436SSimon Ho 	SND_SOC_DAPM_MIXER("Widget15 Mixer", SND_SOC_NOPM, 0, 0,
1264a497a436SSimon Ho 			   wid15_mix, ARRAY_SIZE(wid15_mix)),
1265a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortB In En", SND_SOC_NOPM, 0, 0, &portbinen_ctl),
1266a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortC In En", SND_SOC_NOPM, 0, 0, &portcinen_ctl),
1267a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortD In En", SND_SOC_NOPM, 0, 0, &portdinen_ctl),
1268a497a436SSimon Ho 	SND_SOC_DAPM_SWITCH("PortE In En", SND_SOC_NOPM, 0, 0, &porteinen_ctl),
1269a497a436SSimon Ho 
1270a497a436SSimon Ho 	SND_SOC_DAPM_MICBIAS("Headset Bias", CX2072X_ANALOG_TEST11, 1, 0),
1271a497a436SSimon Ho 	SND_SOC_DAPM_MICBIAS("PortB Mic Bias", CX2072X_PORTB_PIN_CTRL, 2, 0),
1272a497a436SSimon Ho 	SND_SOC_DAPM_MICBIAS("PortD Mic Bias", CX2072X_PORTD_PIN_CTRL, 2, 0),
1273a497a436SSimon Ho 	SND_SOC_DAPM_MICBIAS("PortE Mic Bias", CX2072X_PORTE_PIN_CTRL, 2, 0),
1274a497a436SSimon Ho 	SND_SOC_DAPM_INPUT("PORTB"),
1275a497a436SSimon Ho 	SND_SOC_DAPM_INPUT("PORTC"),
1276a497a436SSimon Ho 	SND_SOC_DAPM_INPUT("PORTD"),
1277a497a436SSimon Ho 	SND_SOC_DAPM_INPUT("PORTEIN"),
1278a497a436SSimon Ho 
1279a497a436SSimon Ho };
1280a497a436SSimon Ho 
1281a497a436SSimon Ho static const struct snd_soc_dapm_route cx2072x_intercon[] = {
1282a497a436SSimon Ho 	/* Playback */
1283a497a436SSimon Ho 	{"In AIF", NULL, "AFG Power"},
1284a497a436SSimon Ho 	{"I2S DAC1L", "Switch", "In AIF"},
1285a497a436SSimon Ho 	{"I2S DAC1R", "Switch", "In AIF"},
1286a497a436SSimon Ho 	{"I2S DAC2L", "Switch", "In AIF"},
1287a497a436SSimon Ho 	{"I2S DAC2R", "Switch", "In AIF"},
1288a497a436SSimon Ho 	{"DAC1", NULL, "I2S DAC1L"},
1289a497a436SSimon Ho 	{"DAC1", NULL, "I2S DAC1R"},
1290a497a436SSimon Ho 	{"DAC2", NULL, "I2S DAC2L"},
1291a497a436SSimon Ho 	{"DAC2", NULL, "I2S DAC2R"},
1292a497a436SSimon Ho 	{"PortA Mux", "DAC1 Switch", "DAC1"},
1293a497a436SSimon Ho 	{"PortA Mux", "DAC2 Switch", "DAC2"},
1294a497a436SSimon Ho 	{"PortG Mux", "DAC1 Switch", "DAC1"},
1295a497a436SSimon Ho 	{"PortG Mux", "DAC2 Switch", "DAC2"},
1296a497a436SSimon Ho 	{"PortE Mux", "DAC1 Switch", "DAC1"},
1297a497a436SSimon Ho 	{"PortE Mux", "DAC2 Switch", "DAC2"},
1298a497a436SSimon Ho 	{"PortM Mux", "DAC1 Switch", "DAC1"},
1299a497a436SSimon Ho 	{"PortM Mux", "DAC2 Switch", "DAC2"},
1300a497a436SSimon Ho 	{"Widget15 Mixer", "DAC1L Switch", "DAC1"},
1301a497a436SSimon Ho 	{"Widget15 Mixer", "DAC1R Switch", "DAC2"},
1302a497a436SSimon Ho 	{"Widget15 Mixer", "DAC2L Switch", "DAC1"},
1303a497a436SSimon Ho 	{"Widget15 Mixer", "DAC2R Switch", "DAC2"},
1304a497a436SSimon Ho 	{"Widget15 Mixer", NULL, "Widget15 Power"},
1305a497a436SSimon Ho 	{"PortA Out En", "Switch", "PortA Mux"},
1306a497a436SSimon Ho 	{"PortG Out En", "Switch", "PortG Mux"},
1307a497a436SSimon Ho 	{"PortE Out En", "Switch", "PortE Mux"},
1308a497a436SSimon Ho 	{"PortM Out En", "Switch", "PortM Mux"},
1309a497a436SSimon Ho 	{"PortA Mux", NULL, "PortA Power"},
1310a497a436SSimon Ho 	{"PortG Mux", NULL, "PortG Power"},
1311a497a436SSimon Ho 	{"PortE Mux", NULL, "PortE Power"},
1312a497a436SSimon Ho 	{"PortM Mux", NULL, "PortM Power"},
1313a497a436SSimon Ho 	{"PortA Out En", NULL, "PortA Power"},
1314a497a436SSimon Ho 	{"PortG Out En", NULL, "PortG Power"},
1315a497a436SSimon Ho 	{"PortE Out En", NULL, "PortE Power"},
1316a497a436SSimon Ho 	{"PortM Out En", NULL, "PortM Power"},
1317a497a436SSimon Ho 	{"PORTA", NULL, "PortA Out En"},
1318a497a436SSimon Ho 	{"PORTG", NULL, "PortG Out En"},
1319a497a436SSimon Ho 	{"PORTE", NULL, "PortE Out En"},
1320a497a436SSimon Ho 	{"PORTM", NULL, "PortM Out En"},
1321a497a436SSimon Ho 
1322a497a436SSimon Ho 	/* Capture */
1323a497a436SSimon Ho 	{"PORTD", NULL, "Headset Bias"},
1324a497a436SSimon Ho 	{"PortB In En", "Switch", "PORTB"},
1325a497a436SSimon Ho 	{"PortC In En", "Switch", "PORTC"},
1326a497a436SSimon Ho 	{"PortD In En", "Switch", "PORTD"},
1327a497a436SSimon Ho 	{"PortE In En", "Switch", "PORTEIN"},
1328a497a436SSimon Ho 	{"ADC1 Mux", "PortB Switch", "PortB In En"},
1329a497a436SSimon Ho 	{"ADC1 Mux", "PortC Switch", "PortC In En"},
1330a497a436SSimon Ho 	{"ADC1 Mux", "PortD Switch", "PortD In En"},
1331a497a436SSimon Ho 	{"ADC1 Mux", "PortE Switch", "PortE In En"},
1332a497a436SSimon Ho 	{"ADC1 Mux", "Widget15 Switch", "Widget15 Mixer"},
1333a497a436SSimon Ho 	{"ADC2 Mux", "PortC Switch", "PortC In En"},
1334a497a436SSimon Ho 	{"ADC2 Mux", "Widget15 Switch", "Widget15 Mixer"},
1335a497a436SSimon Ho 	{"ADC1", NULL, "ADC1 Mux"},
1336a497a436SSimon Ho 	{"ADC2", NULL, "ADC2 Mux"},
1337a497a436SSimon Ho 	{"I2S ADC1L", "Switch", "ADC1"},
1338a497a436SSimon Ho 	{"I2S ADC1R", "Switch", "ADC1"},
1339a497a436SSimon Ho 	{"I2S ADC2L", "Switch", "ADC2"},
1340a497a436SSimon Ho 	{"I2S ADC2R", "Switch", "ADC2"},
1341a497a436SSimon Ho 	{"Out AIF", NULL, "I2S ADC1L"},
1342a497a436SSimon Ho 	{"Out AIF", NULL, "I2S ADC1R"},
1343a497a436SSimon Ho 	{"Out AIF", NULL, "I2S ADC2L"},
1344a497a436SSimon Ho 	{"Out AIF", NULL, "I2S ADC2R"},
1345a497a436SSimon Ho 	{"Out AIF", NULL, "AFG Power"},
1346a497a436SSimon Ho 	{"AEC REF", NULL, "Out AIF"},
1347a497a436SSimon Ho 	{"PortB In En", NULL, "PortB Power"},
1348a497a436SSimon Ho 	{"PortC In En", NULL, "PortC Power"},
1349a497a436SSimon Ho 	{"PortD In En", NULL, "PortD Power"},
1350a497a436SSimon Ho 	{"PortE In En", NULL, "PortE Power"},
1351a497a436SSimon Ho };
1352a497a436SSimon Ho 
1353a497a436SSimon Ho static int cx2072x_set_bias_level(struct snd_soc_component *codec,
1354a497a436SSimon Ho 				  enum snd_soc_bias_level level)
1355a497a436SSimon Ho {
1356a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1357a497a436SSimon Ho 	const enum snd_soc_bias_level old_level =
1358a497a436SSimon Ho 		snd_soc_component_get_bias_level(codec);
1359a497a436SSimon Ho 
1360a497a436SSimon Ho 	if (level == SND_SOC_BIAS_STANDBY && old_level == SND_SOC_BIAS_OFF)
1361a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_AFG_POWER_STATE, 0);
1362a497a436SSimon Ho 	else if (level == SND_SOC_BIAS_OFF && old_level != SND_SOC_BIAS_OFF)
1363a497a436SSimon Ho 		regmap_write(cx2072x->regmap, CX2072X_AFG_POWER_STATE, 3);
1364a497a436SSimon Ho 
1365a497a436SSimon Ho 	return 0;
1366a497a436SSimon Ho }
1367a497a436SSimon Ho 
1368a497a436SSimon Ho /*
1369a497a436SSimon Ho  * FIXME: the whole jack detection code below is pretty platform-specific;
1370a497a436SSimon Ho  * it has lots of implicit assumptions about the pins, etc.
1371a497a436SSimon Ho  * However, since we have no other code and reference, take this hard-coded
1372a497a436SSimon Ho  * setup for now.  Once when we have different platform implementations,
1373a497a436SSimon Ho  * this needs to be rewritten in a more generic form, or moving into the
1374a497a436SSimon Ho  * platform data.
1375a497a436SSimon Ho  */
1376a497a436SSimon Ho static void cx2072x_enable_jack_detect(struct snd_soc_component *codec)
1377a497a436SSimon Ho {
1378a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1379a497a436SSimon Ho 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(codec);
1380a497a436SSimon Ho 
1381a497a436SSimon Ho 	/* No-sticky input type */
1382a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_GPIO_STICKY_MASK, 0x1f);
1383a497a436SSimon Ho 
1384a497a436SSimon Ho 	/* Use GPOI0 as interrupt pin */
1385a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_UM_INTERRUPT_CRTL_E, 0x12 << 24);
1386a497a436SSimon Ho 
1387a497a436SSimon Ho 	/* Enables unsolitited message on PortA */
1388a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_PORTA_UNSOLICITED_RESPONSE, 0x80);
1389a497a436SSimon Ho 
1390a497a436SSimon Ho 	/* support both nokia and apple headset set. Monitor time = 275 ms */
1391a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST15, 0x73);
1392a497a436SSimon Ho 
1393a497a436SSimon Ho 	/* Disable TIP detection */
1394a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_ANALOG_TEST12, 0x300);
1395a497a436SSimon Ho 
1396a497a436SSimon Ho 	/* Switch MusicD3Live pin to GPIO */
1397a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_DIGITAL_TEST1, 0);
1398a497a436SSimon Ho 
1399a497a436SSimon Ho 	snd_soc_dapm_mutex_lock(dapm);
1400a497a436SSimon Ho 
1401a497a436SSimon Ho 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "PORTD");
1402a497a436SSimon Ho 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Headset Bias");
1403a497a436SSimon Ho 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "PortD Mic Bias");
1404a497a436SSimon Ho 
1405a497a436SSimon Ho 	snd_soc_dapm_mutex_unlock(dapm);
1406a497a436SSimon Ho }
1407a497a436SSimon Ho 
1408a497a436SSimon Ho static void cx2072x_disable_jack_detect(struct snd_soc_component *codec)
1409a497a436SSimon Ho {
1410a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1411a497a436SSimon Ho 
1412a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_UM_INTERRUPT_CRTL_E, 0);
1413a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_PORTA_UNSOLICITED_RESPONSE, 0);
1414a497a436SSimon Ho }
1415a497a436SSimon Ho 
1416a497a436SSimon Ho static int cx2072x_jack_status_check(void *data)
1417a497a436SSimon Ho {
1418a497a436SSimon Ho 	struct snd_soc_component *codec = data;
1419a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1420a497a436SSimon Ho 	unsigned int jack;
1421a497a436SSimon Ho 	unsigned int type = 0;
1422a497a436SSimon Ho 	int state = 0;
1423a497a436SSimon Ho 
1424a497a436SSimon Ho 	mutex_lock(&cx2072x->lock);
1425a497a436SSimon Ho 
1426a497a436SSimon Ho 	regmap_read(cx2072x->regmap, CX2072X_PORTA_PIN_SENSE, &jack);
1427a497a436SSimon Ho 	jack = jack >> 24;
1428a497a436SSimon Ho 	regmap_read(cx2072x->regmap, CX2072X_DIGITAL_TEST11, &type);
1429a497a436SSimon Ho 
1430a497a436SSimon Ho 	if (jack == 0x80) {
1431a497a436SSimon Ho 		type = type >> 8;
1432a497a436SSimon Ho 
1433a497a436SSimon Ho 		if (type & 0x8) {
1434a497a436SSimon Ho 			/* Apple headset */
1435a497a436SSimon Ho 			state |= SND_JACK_HEADSET;
1436a497a436SSimon Ho 			if (type & 0x2)
1437a497a436SSimon Ho 				state |= SND_JACK_BTN_0;
1438a497a436SSimon Ho 		} else if (type & 0x4) {
1439a497a436SSimon Ho 			/* Nokia headset */
1440a497a436SSimon Ho 			state |= SND_JACK_HEADPHONE;
1441a497a436SSimon Ho 		} else {
1442a497a436SSimon Ho 			/* Headphone */
1443a497a436SSimon Ho 			state |= SND_JACK_HEADPHONE;
1444a497a436SSimon Ho 		}
1445a497a436SSimon Ho 	}
1446a497a436SSimon Ho 
1447a497a436SSimon Ho 	/* clear interrupt */
1448a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_UM_INTERRUPT_CRTL_E, 0x12 << 24);
1449a497a436SSimon Ho 
1450a497a436SSimon Ho 	mutex_unlock(&cx2072x->lock);
1451a497a436SSimon Ho 
1452a497a436SSimon Ho 	dev_dbg(codec->dev, "CX2072X_HSDETECT type=0x%X,Jack state = %x\n",
1453a497a436SSimon Ho 		type, state);
1454a497a436SSimon Ho 	return state;
1455a497a436SSimon Ho }
1456a497a436SSimon Ho 
1457a497a436SSimon Ho static const struct snd_soc_jack_gpio cx2072x_jack_gpio = {
1458a497a436SSimon Ho 	.name = "headset",
1459a497a436SSimon Ho 	.report = SND_JACK_HEADSET | SND_JACK_BTN_0,
1460a497a436SSimon Ho 	.debounce_time = 150,
1461a497a436SSimon Ho 	.wake = true,
1462a497a436SSimon Ho 	.jack_status_check = cx2072x_jack_status_check,
1463a497a436SSimon Ho };
1464a497a436SSimon Ho 
1465a497a436SSimon Ho static int cx2072x_set_jack(struct snd_soc_component *codec,
1466a497a436SSimon Ho 			    struct snd_soc_jack *jack, void *data)
1467a497a436SSimon Ho {
1468a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1469a497a436SSimon Ho 	int err;
1470a497a436SSimon Ho 
1471a497a436SSimon Ho 	if (!jack) {
1472a497a436SSimon Ho 		cx2072x_disable_jack_detect(codec);
1473a497a436SSimon Ho 		return 0;
1474a497a436SSimon Ho 	}
1475a497a436SSimon Ho 
1476a497a436SSimon Ho 	if (!cx2072x->jack_gpio.gpiod_dev) {
1477a497a436SSimon Ho 		cx2072x->jack_gpio = cx2072x_jack_gpio;
1478a497a436SSimon Ho 		cx2072x->jack_gpio.gpiod_dev = codec->dev;
1479a497a436SSimon Ho 		cx2072x->jack_gpio.data = codec;
1480a497a436SSimon Ho 		err = snd_soc_jack_add_gpios(jack, 1, &cx2072x->jack_gpio);
1481a497a436SSimon Ho 		if (err) {
1482a497a436SSimon Ho 			cx2072x->jack_gpio.gpiod_dev = NULL;
1483a497a436SSimon Ho 			return err;
1484a497a436SSimon Ho 		}
1485a497a436SSimon Ho 	}
1486a497a436SSimon Ho 
1487a497a436SSimon Ho 	cx2072x_enable_jack_detect(codec);
1488a497a436SSimon Ho 	return 0;
1489a497a436SSimon Ho }
1490a497a436SSimon Ho 
1491a497a436SSimon Ho static int cx2072x_probe(struct snd_soc_component *codec)
1492a497a436SSimon Ho {
1493a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = snd_soc_component_get_drvdata(codec);
1494a497a436SSimon Ho 
1495a497a436SSimon Ho 	cx2072x->codec = codec;
1496a497a436SSimon Ho 
1497a497a436SSimon Ho 	/*
1498a497a436SSimon Ho 	 * FIXME: below is, again, a very platform-specific init sequence,
1499a497a436SSimon Ho 	 * but we keep the code here just for simplicity.  It seems that all
1500a497a436SSimon Ho 	 * existing hardware implementations require this, so there is no very
1501a497a436SSimon Ho 	 * much reason to move this out of the codec driver to the platform
1502a497a436SSimon Ho 	 * data.
1503a497a436SSimon Ho 	 * But of course it's no "right" thing; if you are a good boy, don't
1504a497a436SSimon Ho 	 * read and follow the code like this!
1505a497a436SSimon Ho 	 */
1506a497a436SSimon Ho 	pm_runtime_get_sync(codec->dev);
1507a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_AFG_POWER_STATE, 0);
1508a497a436SSimon Ho 
1509a497a436SSimon Ho 	regmap_multi_reg_write(cx2072x->regmap, cx2072x_reg_init,
1510a497a436SSimon Ho 			       ARRAY_SIZE(cx2072x_reg_init));
1511a497a436SSimon Ho 
1512a497a436SSimon Ho 	/* configre PortC as input device */
1513a497a436SSimon Ho 	regmap_update_bits(cx2072x->regmap, CX2072X_PORTC_PIN_CTRL,
1514a497a436SSimon Ho 			   0x20, 0x20);
1515a497a436SSimon Ho 
1516a497a436SSimon Ho 	regmap_update_bits(cx2072x->regmap, CX2072X_DIGITAL_BIOS_TEST2,
1517a497a436SSimon Ho 			   0x84, 0xff);
1518a497a436SSimon Ho 
1519a497a436SSimon Ho 	regmap_write(cx2072x->regmap, CX2072X_AFG_POWER_STATE, 3);
1520a497a436SSimon Ho 	pm_runtime_put(codec->dev);
1521a497a436SSimon Ho 
1522a497a436SSimon Ho 	return 0;
1523a497a436SSimon Ho }
1524a497a436SSimon Ho 
1525a497a436SSimon Ho static const struct snd_soc_component_driver soc_codec_driver_cx2072x = {
1526a497a436SSimon Ho 	.probe = cx2072x_probe,
1527a497a436SSimon Ho 	.set_bias_level = cx2072x_set_bias_level,
1528a497a436SSimon Ho 	.set_jack = cx2072x_set_jack,
1529a497a436SSimon Ho 	.controls = cx2072x_snd_controls,
1530a497a436SSimon Ho 	.num_controls = ARRAY_SIZE(cx2072x_snd_controls),
1531a497a436SSimon Ho 	.dapm_widgets = cx2072x_dapm_widgets,
1532a497a436SSimon Ho 	.num_dapm_widgets = ARRAY_SIZE(cx2072x_dapm_widgets),
1533a497a436SSimon Ho 	.dapm_routes = cx2072x_intercon,
1534a497a436SSimon Ho 	.num_dapm_routes = ARRAY_SIZE(cx2072x_intercon),
1535a497a436SSimon Ho };
1536a497a436SSimon Ho 
1537a497a436SSimon Ho /*
1538a497a436SSimon Ho  * DAI ops
1539a497a436SSimon Ho  */
1540a497a436SSimon Ho static struct snd_soc_dai_ops cx2072x_dai_ops = {
1541a497a436SSimon Ho 	.set_sysclk = cx2072x_set_dai_sysclk,
1542a497a436SSimon Ho 	.set_fmt = cx2072x_set_dai_fmt,
1543a497a436SSimon Ho 	.hw_params = cx2072x_hw_params,
1544a497a436SSimon Ho 	.set_bclk_ratio = cx2072x_set_dai_bclk_ratio,
1545a497a436SSimon Ho };
1546a497a436SSimon Ho 
1547a497a436SSimon Ho static int cx2072x_dsp_dai_probe(struct snd_soc_dai *dai)
1548a497a436SSimon Ho {
1549a497a436SSimon Ho 	struct cx2072x_priv *cx2072x =
1550a497a436SSimon Ho 		snd_soc_component_get_drvdata(dai->component);
1551a497a436SSimon Ho 
1552a497a436SSimon Ho 	cx2072x->en_aec_ref = true;
1553a497a436SSimon Ho 	return 0;
1554a497a436SSimon Ho }
1555a497a436SSimon Ho 
1556a497a436SSimon Ho #define CX2072X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
1557a497a436SSimon Ho 
1558a497a436SSimon Ho static struct snd_soc_dai_driver soc_codec_cx2072x_dai[] = {
1559a497a436SSimon Ho 	{ /* playback and capture */
1560a497a436SSimon Ho 		.name = "cx2072x-hifi",
1561a497a436SSimon Ho 		.id	= CX2072X_DAI_HIFI,
1562a497a436SSimon Ho 		.playback = {
1563a497a436SSimon Ho 			.stream_name = "Playback",
1564a497a436SSimon Ho 			.channels_min = 1,
1565a497a436SSimon Ho 			.channels_max = 2,
1566a497a436SSimon Ho 			.rates = CX2072X_RATES_DSP,
1567a497a436SSimon Ho 			.formats = CX2072X_FORMATS,
1568a497a436SSimon Ho 		},
1569a497a436SSimon Ho 		.capture = {
1570a497a436SSimon Ho 			.stream_name = "Capture",
1571a497a436SSimon Ho 			.channels_min = 1,
1572a497a436SSimon Ho 			.channels_max = 2,
1573a497a436SSimon Ho 			.rates = CX2072X_RATES_DSP,
1574a497a436SSimon Ho 			.formats = CX2072X_FORMATS,
1575a497a436SSimon Ho 		},
1576a497a436SSimon Ho 		.ops = &cx2072x_dai_ops,
1577a497a436SSimon Ho 		.symmetric_rates = 1,
1578a497a436SSimon Ho 	},
1579a497a436SSimon Ho 	{ /* plabayck only, return echo reference to Conexant DSP chip */
1580a497a436SSimon Ho 		.name = "cx2072x-dsp",
1581a497a436SSimon Ho 		.id	= CX2072X_DAI_DSP,
1582a497a436SSimon Ho 		.probe = cx2072x_dsp_dai_probe,
1583a497a436SSimon Ho 		.playback = {
1584a497a436SSimon Ho 			.stream_name = "Playback",
1585a497a436SSimon Ho 			.channels_min = 2,
1586a497a436SSimon Ho 			.channels_max = 2,
1587a497a436SSimon Ho 			.rates = CX2072X_RATES_DSP,
1588a497a436SSimon Ho 			.formats = CX2072X_FORMATS,
1589a497a436SSimon Ho 		},
1590a497a436SSimon Ho 		.ops = &cx2072x_dai_ops,
1591a497a436SSimon Ho 	},
1592a497a436SSimon Ho 	{ /* plabayck only, return echo reference through I2S TX */
1593a497a436SSimon Ho 		.name = "cx2072x-aec",
1594a497a436SSimon Ho 		.id	= 3,
1595a497a436SSimon Ho 		.capture = {
1596a497a436SSimon Ho 			.stream_name = "Capture",
1597a497a436SSimon Ho 			.channels_min = 2,
1598a497a436SSimon Ho 			.channels_max = 2,
1599a497a436SSimon Ho 			.rates = CX2072X_RATES_DSP,
1600a497a436SSimon Ho 			.formats = CX2072X_FORMATS,
1601a497a436SSimon Ho 		},
1602a497a436SSimon Ho 	},
1603a497a436SSimon Ho };
1604a497a436SSimon Ho 
1605a497a436SSimon Ho static const struct regmap_config cx2072x_regmap = {
1606a497a436SSimon Ho 	.reg_bits = 16,
1607a497a436SSimon Ho 	.val_bits = 32,
1608a497a436SSimon Ho 	.max_register = CX2072X_REG_MAX,
1609a497a436SSimon Ho 	.reg_defaults = cx2072x_reg_defaults,
1610a497a436SSimon Ho 	.num_reg_defaults = ARRAY_SIZE(cx2072x_reg_defaults),
1611a497a436SSimon Ho 	.cache_type = REGCACHE_RBTREE,
1612a497a436SSimon Ho 	.readable_reg = cx2072x_readable_register,
1613a497a436SSimon Ho 	.volatile_reg = cx2072x_volatile_register,
1614a497a436SSimon Ho 	/* Needs custom read/write functions for various register lengths */
1615a497a436SSimon Ho 	.reg_read = cx2072x_reg_read,
1616a497a436SSimon Ho 	.reg_write = cx2072x_reg_write,
1617a497a436SSimon Ho };
1618a497a436SSimon Ho 
1619a497a436SSimon Ho static int __maybe_unused cx2072x_runtime_suspend(struct device *dev)
1620a497a436SSimon Ho {
1621a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = dev_get_drvdata(dev);
1622a497a436SSimon Ho 
1623a497a436SSimon Ho 	clk_disable_unprepare(cx2072x->mclk);
1624a497a436SSimon Ho 	return 0;
1625a497a436SSimon Ho }
1626a497a436SSimon Ho 
1627a497a436SSimon Ho static int cx2072x_runtime_resume(struct device *dev)
1628a497a436SSimon Ho {
1629a497a436SSimon Ho 	struct cx2072x_priv *cx2072x = dev_get_drvdata(dev);
1630a497a436SSimon Ho 
1631a497a436SSimon Ho 	return clk_prepare_enable(cx2072x->mclk);
1632a497a436SSimon Ho }
1633a497a436SSimon Ho 
1634a497a436SSimon Ho static int cx2072x_i2c_probe(struct i2c_client *i2c,
1635a497a436SSimon Ho 			     const struct i2c_device_id *id)
1636a497a436SSimon Ho {
1637a497a436SSimon Ho 	struct cx2072x_priv *cx2072x;
1638a497a436SSimon Ho 	unsigned int ven_id, rev_id;
1639a497a436SSimon Ho 	int ret;
1640a497a436SSimon Ho 
1641a497a436SSimon Ho 	cx2072x = devm_kzalloc(&i2c->dev, sizeof(struct cx2072x_priv),
1642a497a436SSimon Ho 			       GFP_KERNEL);
1643a497a436SSimon Ho 	if (!cx2072x)
1644a497a436SSimon Ho 		return -ENOMEM;
1645a497a436SSimon Ho 
1646a497a436SSimon Ho 	cx2072x->regmap = devm_regmap_init(&i2c->dev, NULL, i2c,
1647a497a436SSimon Ho 					   &cx2072x_regmap);
1648a497a436SSimon Ho 	if (IS_ERR(cx2072x->regmap))
1649a497a436SSimon Ho 		return PTR_ERR(cx2072x->regmap);
1650a497a436SSimon Ho 
1651a497a436SSimon Ho 	mutex_init(&cx2072x->lock);
1652a497a436SSimon Ho 
1653a497a436SSimon Ho 	i2c_set_clientdata(i2c, cx2072x);
1654a497a436SSimon Ho 
1655a497a436SSimon Ho 	cx2072x->dev = &i2c->dev;
1656a497a436SSimon Ho 	cx2072x->pll_changed = true;
1657a497a436SSimon Ho 	cx2072x->i2spcm_changed = true;
1658a497a436SSimon Ho 	cx2072x->bclk_ratio = 0;
1659a497a436SSimon Ho 
1660a497a436SSimon Ho 	cx2072x->mclk = devm_clk_get(cx2072x->dev, "mclk");
1661a497a436SSimon Ho 	if (IS_ERR(cx2072x->mclk)) {
1662a497a436SSimon Ho 		dev_err(cx2072x->dev, "Failed to get MCLK\n");
1663a497a436SSimon Ho 		return PTR_ERR(cx2072x->mclk);
1664a497a436SSimon Ho 	}
1665a497a436SSimon Ho 
1666a497a436SSimon Ho 	regmap_read(cx2072x->regmap, CX2072X_VENDOR_ID, &ven_id);
1667a497a436SSimon Ho 	regmap_read(cx2072x->regmap, CX2072X_REVISION_ID, &rev_id);
1668a497a436SSimon Ho 
1669a497a436SSimon Ho 	dev_info(cx2072x->dev, "codec version: %08x,%08x\n", ven_id, rev_id);
1670a497a436SSimon Ho 
1671a497a436SSimon Ho 	ret = devm_snd_soc_register_component(cx2072x->dev,
1672a497a436SSimon Ho 					      &soc_codec_driver_cx2072x,
1673a497a436SSimon Ho 					      soc_codec_cx2072x_dai,
1674a497a436SSimon Ho 					      ARRAY_SIZE(soc_codec_cx2072x_dai));
1675a497a436SSimon Ho 	if (ret < 0)
1676a497a436SSimon Ho 		return ret;
1677a497a436SSimon Ho 
1678a497a436SSimon Ho 	pm_runtime_use_autosuspend(cx2072x->dev);
1679a497a436SSimon Ho 	pm_runtime_enable(cx2072x->dev);
1680a497a436SSimon Ho 
1681a497a436SSimon Ho 	return 0;
1682a497a436SSimon Ho }
1683a497a436SSimon Ho 
1684a497a436SSimon Ho static int cx2072x_i2c_remove(struct i2c_client *i2c)
1685a497a436SSimon Ho {
1686a497a436SSimon Ho 	pm_runtime_disable(&i2c->dev);
1687a497a436SSimon Ho 	return 0;
1688a497a436SSimon Ho }
1689a497a436SSimon Ho 
1690a497a436SSimon Ho static const struct i2c_device_id cx2072x_i2c_id[] = {
1691a497a436SSimon Ho 	{ "cx20721", 0 },
1692a497a436SSimon Ho 	{ "cx20723", 0 },
1693a497a436SSimon Ho 	{}
1694a497a436SSimon Ho };
1695a497a436SSimon Ho MODULE_DEVICE_TABLE(i2c, cx2072x_i2c_id);
1696a497a436SSimon Ho 
1697a497a436SSimon Ho #ifdef CONFIG_ACPI
1698a497a436SSimon Ho static struct acpi_device_id cx2072x_acpi_match[] = {
1699a497a436SSimon Ho 	{ "14F10720", 0 },
1700a497a436SSimon Ho 	{},
1701a497a436SSimon Ho };
1702a497a436SSimon Ho MODULE_DEVICE_TABLE(acpi, cx2072x_acpi_match);
1703a497a436SSimon Ho #endif
1704a497a436SSimon Ho 
1705a497a436SSimon Ho static const struct dev_pm_ops cx2072x_runtime_pm = {
1706a497a436SSimon Ho 	SET_RUNTIME_PM_OPS(cx2072x_runtime_suspend, cx2072x_runtime_resume,
1707a497a436SSimon Ho 			   NULL)
1708a497a436SSimon Ho 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1709a497a436SSimon Ho 				pm_runtime_force_resume)
1710a497a436SSimon Ho };
1711a497a436SSimon Ho 
1712a497a436SSimon Ho static struct i2c_driver cx2072x_i2c_driver = {
1713a497a436SSimon Ho 	.driver = {
1714a497a436SSimon Ho 		.name = "cx2072x",
1715a497a436SSimon Ho 		.acpi_match_table = ACPI_PTR(cx2072x_acpi_match),
1716a497a436SSimon Ho 		.pm = &cx2072x_runtime_pm,
1717a497a436SSimon Ho 	},
1718a497a436SSimon Ho 	.probe = cx2072x_i2c_probe,
1719a497a436SSimon Ho 	.remove = cx2072x_i2c_remove,
1720a497a436SSimon Ho 	.id_table = cx2072x_i2c_id,
1721a497a436SSimon Ho };
1722a497a436SSimon Ho 
1723a497a436SSimon Ho module_i2c_driver(cx2072x_i2c_driver);
1724a497a436SSimon Ho 
1725a497a436SSimon Ho MODULE_DESCRIPTION("ASoC cx2072x Codec Driver");
1726a497a436SSimon Ho MODULE_AUTHOR("Simon Ho <simon.ho@conexant.com>");
1727a497a436SSimon Ho MODULE_LICENSE("GPL");
1728