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