18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * rt711.h -- RT711 ALSA SoC audio driver header
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright(c) 2019 Realtek Semiconductor Corp.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef __RT711_H__
98c2ecf20Sopenharmony_ci#define __RT711_H__
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciextern const struct dev_pm_ops rt711_runtime_pm;
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct  rt711_priv {
148c2ecf20Sopenharmony_ci	struct regmap *regmap;
158c2ecf20Sopenharmony_ci	struct regmap *sdw_regmap;
168c2ecf20Sopenharmony_ci	struct snd_soc_component *component;
178c2ecf20Sopenharmony_ci	struct sdw_slave *slave;
188c2ecf20Sopenharmony_ci	enum sdw_slave_status status;
198c2ecf20Sopenharmony_ci	struct sdw_bus_params params;
208c2ecf20Sopenharmony_ci	bool hw_init;
218c2ecf20Sopenharmony_ci	bool first_hw_init;
228c2ecf20Sopenharmony_ci	struct snd_soc_jack *hs_jack;
238c2ecf20Sopenharmony_ci	struct delayed_work jack_detect_work;
248c2ecf20Sopenharmony_ci	struct delayed_work jack_btn_check_work;
258c2ecf20Sopenharmony_ci	struct work_struct calibration_work;
268c2ecf20Sopenharmony_ci	struct mutex calibrate_mutex; /* for headset calibration */
278c2ecf20Sopenharmony_ci	int jack_type, jd_src;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct sdw_stream_data {
318c2ecf20Sopenharmony_ci	struct sdw_stream_runtime *sdw_stream;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* NID */
358c2ecf20Sopenharmony_ci#define RT711_AUDIO_FUNCTION_GROUP			0x01
368c2ecf20Sopenharmony_ci#define RT711_DAC_OUT2					0x03
378c2ecf20Sopenharmony_ci#define RT711_ADC_IN1					0x09
388c2ecf20Sopenharmony_ci#define RT711_ADC_IN2					0x08
398c2ecf20Sopenharmony_ci#define RT711_DMIC1					0x12
408c2ecf20Sopenharmony_ci#define RT711_DMIC2					0x13
418c2ecf20Sopenharmony_ci#define RT711_MIC2					0x19
428c2ecf20Sopenharmony_ci#define RT711_LINE1					0x1a
438c2ecf20Sopenharmony_ci#define RT711_LINE2					0x1b
448c2ecf20Sopenharmony_ci#define RT711_BEEP					0x1d
458c2ecf20Sopenharmony_ci#define RT711_VENDOR_REG				0x20
468c2ecf20Sopenharmony_ci#define RT711_HP_OUT					0x21
478c2ecf20Sopenharmony_ci#define RT711_MIXER_IN1					0x22
488c2ecf20Sopenharmony_ci#define RT711_MIXER_IN2					0x23
498c2ecf20Sopenharmony_ci#define RT711_INLINE_CMD				0x55
508c2ecf20Sopenharmony_ci#define RT711_VENDOR_CALI				0x58
518c2ecf20Sopenharmony_ci#define RT711_VENDOR_IMS_DRE			0x5b
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* Index (NID:20h) */
548c2ecf20Sopenharmony_ci#define RT711_DAC_DC_CALI_CTL1				0x00
558c2ecf20Sopenharmony_ci#define RT711_JD_CTL1				0x08
568c2ecf20Sopenharmony_ci#define RT711_JD_CTL2				0x09
578c2ecf20Sopenharmony_ci#define RT711_JD_CTL4				0x0b
588c2ecf20Sopenharmony_ci#define RT711_CC_DET1				0x11
598c2ecf20Sopenharmony_ci#define RT711_PARA_VERB_CTL				0x1a
608c2ecf20Sopenharmony_ci#define RT711_COMBO_JACK_AUTO_CTL1				0x45
618c2ecf20Sopenharmony_ci#define RT711_COMBO_JACK_AUTO_CTL2				0x46
628c2ecf20Sopenharmony_ci#define RT711_INLINE_CMD_CTL				0x48
638c2ecf20Sopenharmony_ci#define RT711_DIGITAL_MISC_CTRL4			0x4a
648c2ecf20Sopenharmony_ci#define RT711_VREFOUT_CTL				0x6b
658c2ecf20Sopenharmony_ci#define RT711_FSM_CTL				0x6f
668c2ecf20Sopenharmony_ci#define RT711_IRQ_FLAG_TABLE1				0x80
678c2ecf20Sopenharmony_ci#define RT711_IRQ_FLAG_TABLE2				0x81
688c2ecf20Sopenharmony_ci#define RT711_IRQ_FLAG_TABLE3				0x82
698c2ecf20Sopenharmony_ci#define RT711_TX_RX_MUX_CTL				0x91
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* Index (NID:5bh) */
728c2ecf20Sopenharmony_ci#define RT711_IMS_DIGITAL_CTL1				0x00
738c2ecf20Sopenharmony_ci#define RT711_HP_IMS_RESULT_L				0x20
748c2ecf20Sopenharmony_ci#define RT711_HP_IMS_RESULT_R				0x21
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* Verb */
778c2ecf20Sopenharmony_ci#define RT711_VERB_SET_CONNECT_SEL			0x3100
788c2ecf20Sopenharmony_ci#define RT711_VERB_SET_EAPD_BTLENABLE			0x3c00
798c2ecf20Sopenharmony_ci#define RT711_VERB_GET_CONNECT_SEL			0xb100
808c2ecf20Sopenharmony_ci#define RT711_VERB_SET_POWER_STATE			0x3500
818c2ecf20Sopenharmony_ci#define RT711_VERB_SET_CHANNEL_STREAMID			0x3600
828c2ecf20Sopenharmony_ci#define RT711_VERB_SET_PIN_WIDGET_CONTROL		0x3700
838c2ecf20Sopenharmony_ci#define RT711_VERB_SET_UNSOLICITED_ENABLE		0x3800
848c2ecf20Sopenharmony_ci#define RT711_SET_AMP_GAIN_MUTE_H			0x7300
858c2ecf20Sopenharmony_ci#define RT711_SET_AMP_GAIN_MUTE_L			0x8380
868c2ecf20Sopenharmony_ci#define RT711_VERB_GET_POWER_STATE			0xb500
878c2ecf20Sopenharmony_ci#define RT711_VERB_GET_CHANNEL_STREAMID			0xb600
888c2ecf20Sopenharmony_ci#define RT711_VERB_GET_PIN_SENSE			0xb900
898c2ecf20Sopenharmony_ci#define RT711_FUNC_RESET			0xff01
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define RT711_READ_HDA_3				0x2012
928c2ecf20Sopenharmony_ci#define RT711_READ_HDA_2				0x2013
938c2ecf20Sopenharmony_ci#define RT711_READ_HDA_1				0x2014
948c2ecf20Sopenharmony_ci#define RT711_READ_HDA_0				0x2015
958c2ecf20Sopenharmony_ci#define RT711_PRIV_INDEX_W_H				0x7500
968c2ecf20Sopenharmony_ci#define RT711_PRIV_INDEX_W_L				0x8580
978c2ecf20Sopenharmony_ci#define RT711_PRIV_DATA_W_H				0x7400
988c2ecf20Sopenharmony_ci#define RT711_PRIV_DATA_W_L				0x8480
998c2ecf20Sopenharmony_ci#define RT711_PRIV_INDEX_R_H				0x9d00
1008c2ecf20Sopenharmony_ci#define RT711_PRIV_INDEX_R_L				0xad80
1018c2ecf20Sopenharmony_ci#define RT711_PRIV_DATA_R_H				0x9c00
1028c2ecf20Sopenharmony_ci#define RT711_PRIV_DATA_R_L				0xac80
1038c2ecf20Sopenharmony_ci#define RT711_DAC_FORMAT_H				0x7203
1048c2ecf20Sopenharmony_ci#define RT711_DAC_FORMAT_L				0x8283
1058c2ecf20Sopenharmony_ci#define RT711_ADC1_FORMAT_H				0x7209
1068c2ecf20Sopenharmony_ci#define RT711_ADC1_FORMAT_L				0x8289
1078c2ecf20Sopenharmony_ci#define RT711_ADC2_FORMAT_H				0x7208
1088c2ecf20Sopenharmony_ci#define RT711_ADC2_FORMAT_L				0x8288
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define RT711_SET_AUDIO_POWER_STATE\
1118c2ecf20Sopenharmony_ci	(RT711_VERB_SET_POWER_STATE | RT711_AUDIO_FUNCTION_GROUP)
1128c2ecf20Sopenharmony_ci#define RT711_GET_AUDIO_POWER_STATE\
1138c2ecf20Sopenharmony_ci		(RT711_VERB_GET_POWER_STATE | RT711_AUDIO_FUNCTION_GROUP)
1148c2ecf20Sopenharmony_ci#define RT711_SET_PIN_DMIC1\
1158c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_DMIC1)
1168c2ecf20Sopenharmony_ci#define RT711_SET_PIN_DMIC2\
1178c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_DMIC2)
1188c2ecf20Sopenharmony_ci#define RT711_SET_PIN_HP\
1198c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_HP_OUT)
1208c2ecf20Sopenharmony_ci#define RT711_SET_PIN_MIC2\
1218c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_MIC2)
1228c2ecf20Sopenharmony_ci#define RT711_SET_PIN_LINE1\
1238c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_LINE1)
1248c2ecf20Sopenharmony_ci#define RT711_SET_PIN_LINE2\
1258c2ecf20Sopenharmony_ci	(RT711_VERB_SET_PIN_WIDGET_CONTROL | RT711_LINE2)
1268c2ecf20Sopenharmony_ci#define RT711_SET_MIC2_UNSOLICITED_ENABLE\
1278c2ecf20Sopenharmony_ci	(RT711_VERB_SET_UNSOLICITED_ENABLE | RT711_MIC2)
1288c2ecf20Sopenharmony_ci#define RT711_SET_HP_UNSOLICITED_ENABLE\
1298c2ecf20Sopenharmony_ci	(RT711_VERB_SET_UNSOLICITED_ENABLE | RT711_HP_OUT)
1308c2ecf20Sopenharmony_ci#define RT711_SET_INLINE_UNSOLICITED_ENABLE\
1318c2ecf20Sopenharmony_ci	(RT711_VERB_SET_UNSOLICITED_ENABLE | RT711_INLINE_CMD)
1328c2ecf20Sopenharmony_ci#define RT711_SET_STREAMID_DAC2\
1338c2ecf20Sopenharmony_ci	(RT711_VERB_SET_CHANNEL_STREAMID | RT711_DAC_OUT2)
1348c2ecf20Sopenharmony_ci#define RT711_SET_STREAMID_ADC1\
1358c2ecf20Sopenharmony_ci	(RT711_VERB_SET_CHANNEL_STREAMID | RT711_ADC_IN1)
1368c2ecf20Sopenharmony_ci#define RT711_SET_STREAMID_ADC2\
1378c2ecf20Sopenharmony_ci	(RT711_VERB_SET_CHANNEL_STREAMID | RT711_ADC_IN2)
1388c2ecf20Sopenharmony_ci#define RT711_GET_STREAMID_DAC2\
1398c2ecf20Sopenharmony_ci	(RT711_VERB_GET_CHANNEL_STREAMID | RT711_DAC_OUT2)
1408c2ecf20Sopenharmony_ci#define RT711_GET_STREAMID_ADC1\
1418c2ecf20Sopenharmony_ci	(RT711_VERB_GET_CHANNEL_STREAMID | RT711_ADC_IN1)
1428c2ecf20Sopenharmony_ci#define RT711_GET_STREAMID_ADC2\
1438c2ecf20Sopenharmony_ci	(RT711_VERB_GET_CHANNEL_STREAMID | RT711_ADC_IN2)
1448c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DAC2_L\
1458c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_DAC_OUT2)
1468c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DAC2_H\
1478c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_DAC_OUT2)
1488c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_ADC1_L\
1498c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_ADC_IN1)
1508c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_ADC1_H\
1518c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_ADC_IN1)
1528c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_ADC2_L\
1538c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_ADC_IN2)
1548c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_ADC2_H\
1558c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_ADC_IN2)
1568c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_AMIC_L\
1578c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_MIC2)
1588c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_AMIC_H\
1598c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_MIC2)
1608c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DMIC1_L\
1618c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_DMIC1)
1628c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DMIC1_H\
1638c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_DMIC1)
1648c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DMIC2_L\
1658c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_DMIC2)
1668c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_DMIC2_H\
1678c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_DMIC2)
1688c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_HP_L\
1698c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_L | RT711_HP_OUT)
1708c2ecf20Sopenharmony_ci#define RT711_SET_GAIN_HP_H\
1718c2ecf20Sopenharmony_ci	(RT711_SET_AMP_GAIN_MUTE_H | RT711_HP_OUT)
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* DAC DC offset calibration control-1 (0x00)(NID:20h) */
1748c2ecf20Sopenharmony_ci#define RT711_DAC_DC_CALI_TRIGGER (0x1 << 15)
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/* jack detect control 1 (0x08)(NID:20h) */
1778c2ecf20Sopenharmony_ci#define RT711_JD2_DIGITAL_JD_MODE_SEL (0x1 << 1)
1788c2ecf20Sopenharmony_ci#define RT711_JD2_1_JD_MODE (0x0 << 1)
1798c2ecf20Sopenharmony_ci#define RT711_JD2_2_JD_MODE (0x1 << 1)
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* jack detect control 2 (0x09)(NID:20h) */
1828c2ecf20Sopenharmony_ci#define RT711_JD2_2PORT_200K_DECODE_HP (0x1 << 13)
1838c2ecf20Sopenharmony_ci#define RT711_JD2_2PORT_100K_DECODE (0x1 << 12)
1848c2ecf20Sopenharmony_ci#define RT711_JD2_2PORT_100K_DECODE_HP (0x0 << 12)
1858c2ecf20Sopenharmony_ci#define RT711_HP_JD_SEL_JD1 (0x0 << 1)
1868c2ecf20Sopenharmony_ci#define RT711_HP_JD_SEL_JD2 (0x1 << 1)
1878c2ecf20Sopenharmony_ci#define RT711_JD2_1PORT_TYPE_DECODE (0x3 << 10)
1888c2ecf20Sopenharmony_ci#define RT711_JD2_1PORT_JD_LINE2 (0x0 << 10)
1898c2ecf20Sopenharmony_ci#define RT711_JD2_1PORT_JD_HP (0x1 << 10)
1908c2ecf20Sopenharmony_ci#define RT711_JD2_1PORT_JD_LINE1 (0x2 << 10)
1918c2ecf20Sopenharmony_ci#define RT711_JD1_2PORT_TYPE_100K_DECODE (0x1 << 0)
1928c2ecf20Sopenharmony_ci#define RT711_JD1_2PORT_JD_RESERVED (0x0 << 0)
1938c2ecf20Sopenharmony_ci#define RT711_JD1_2PORT_JD_LINE1 (0x1 << 0)
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/* jack detect control 4 (0x0b)(NID:20h) */
1968c2ecf20Sopenharmony_ci#define RT711_JD2_PAD_PULL_UP_MASK (0x1 << 3)
1978c2ecf20Sopenharmony_ci#define RT711_JD2_PAD_NOT_PULL_UP (0x0 << 3)
1988c2ecf20Sopenharmony_ci#define RT711_JD2_PAD_PULL_UP (0x1 << 3)
1998c2ecf20Sopenharmony_ci#define RT711_JD2_MODE_SEL_MASK (0x3 << 0)
2008c2ecf20Sopenharmony_ci#define RT711_JD2_MODE0_2PORT (0x0 << 0)
2018c2ecf20Sopenharmony_ci#define RT711_JD2_MODE1_3P3V_1PORT (0x1 << 0)
2028c2ecf20Sopenharmony_ci#define RT711_JD2_MODE2_1P8V_1PORT (0x2 << 0)
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* CC DET1 (0x11)(NID:20h) */
2058c2ecf20Sopenharmony_ci#define RT711_HP_JD_FINAL_RESULT_CTL_JD12 (0x1 << 10)
2068c2ecf20Sopenharmony_ci#define RT711_HP_JD_FINAL_RESULT_CTL_CCDET (0x0 << 10)
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/* Parameter & Verb control (0x1a)(NID:20h) */
2098c2ecf20Sopenharmony_ci#define RT711_HIDDEN_REG_SW_RESET (0x1 << 14)
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/* combo jack auto switch control 2 (0x46)(NID:20h) */
2128c2ecf20Sopenharmony_ci#define RT711_COMBOJACK_AUTO_DET_STATUS			(0x1 << 11)
2138c2ecf20Sopenharmony_ci#define RT711_COMBOJACK_AUTO_DET_TRS			(0x1 << 10)
2148c2ecf20Sopenharmony_ci#define RT711_COMBOJACK_AUTO_DET_CTIA			(0x1 << 9)
2158c2ecf20Sopenharmony_ci#define RT711_COMBOJACK_AUTO_DET_OMTP			(0x1 << 8)
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* FSM control (0x6f)(NID:20h) */
2188c2ecf20Sopenharmony_ci#define RT711_CALI_CTL			(0x0 << 0)
2198c2ecf20Sopenharmony_ci#define RT711_COMBOJACK_CTL			(0x1 << 0)
2208c2ecf20Sopenharmony_ci#define RT711_IMS_CTL			(0x2 << 0)
2218c2ecf20Sopenharmony_ci#define RT711_DEPOP_CTL			(0x3 << 0)
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/* Impedance Sense Digital Control 1 (0x00)(NID:5bh) */
2248c2ecf20Sopenharmony_ci#define RT711_TRIGGER_IMS			(0x1 << 15)
2258c2ecf20Sopenharmony_ci#define RT711_IMS_EN			(0x1 << 6)
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define RT711_EAPD_HIGH					0x2
2288c2ecf20Sopenharmony_ci#define RT711_EAPD_LOW					0x0
2298c2ecf20Sopenharmony_ci#define RT711_MUTE_SFT					7
2308c2ecf20Sopenharmony_ci/* set input/output mapping to payload[14][15] separately */
2318c2ecf20Sopenharmony_ci#define RT711_DIR_IN_SFT				6
2328c2ecf20Sopenharmony_ci#define RT711_DIR_OUT_SFT				7
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cienum {
2358c2ecf20Sopenharmony_ci	RT711_AIF1,
2368c2ecf20Sopenharmony_ci	RT711_AIF2,
2378c2ecf20Sopenharmony_ci	RT711_AIFS,
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cienum rt711_jd_src {
2418c2ecf20Sopenharmony_ci	RT711_JD_NULL,
2428c2ecf20Sopenharmony_ci	RT711_JD1,
2438c2ecf20Sopenharmony_ci	RT711_JD2,
2448c2ecf20Sopenharmony_ci	RT711_JD2_100K,
2458c2ecf20Sopenharmony_ci	RT711_JD2_1P8V_1PORT
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciint rt711_io_init(struct device *dev, struct sdw_slave *slave);
2498c2ecf20Sopenharmony_ciint rt711_init(struct device *dev, struct regmap *sdw_regmap,
2508c2ecf20Sopenharmony_ci	       struct regmap *regmap, struct sdw_slave *slave);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciint rt711_jack_detect(struct rt711_priv *rt711, bool *hp, bool *mic);
2538c2ecf20Sopenharmony_ciint rt711_clock_config(struct device *dev);
2548c2ecf20Sopenharmony_ci#endif /* __RT711_H__ */
255