xref: /linux/sound/soc/codecs/tac5xx2-sdw.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAC5XX2 Audio Smart Amplifier
4 //
5 // Copyright (C) 2025 Texas Instruments Incorporated
6 // https://www.ti.com
7 //
8 // Author: Niranjan H Y <niranjan.hy@ti.com>
9 
10 #include <linux/err.h>
11 #include <linux/firmware.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/pm.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <linux/soundwire/sdw.h>
19 #include <linux/soundwire/sdw_registers.h>
20 #include <linux/soundwire/sdw_type.h>
21 #include <linux/time.h>
22 #include <linux/unaligned.h>
23 #include <sound/pcm_params.h>
24 #include <sound/sdw.h>
25 #include <sound/soc.h>
26 #include <sound/tlv.h>
27 #include <sound/sdca_asoc.h>
28 #include <sound/sdca_function.h>
29 #include <sound/sdca_regmap.h>
30 #include <sound/jack.h>
31 
32 #include "tac5xx2.h"
33 
34 #define TAC5XX2_PROBE_TIMEOUT_MS 3000
35 #define TAC5XX2_FW_CACHE_TIMEOUT_MS 300
36 
37 #define TAC5XX2_DEVICE_RATES (SNDRV_PCM_RATE_44100 | \
38 			      SNDRV_PCM_RATE_48000 | \
39 			      SNDRV_PCM_RATE_96000 | \
40 			      SNDRV_PCM_RATE_88200)
41 #define TAC5XX2_DEVICE_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
42 				SNDRV_PCM_FMTBIT_S24_LE | \
43 				SNDRV_PCM_FMTBIT_S32_LE)
44 /* Define channel constants */
45 #define TAC_CHANNEL_LEFT	1
46 #define TAC_CHANNEL_RIGHT	2
47 #define TAC_JACK_MONO_CS	2
48 
49 #define TAC_MUTE_REG(func, fu, ch) \
50 	SDW_SDCA_CTL(TAC_FUNCTION_ID_##func, TAC_SDCA_ENT_##fu, \
51 		     TAC_SDCA_CHANNEL_MUTE, TAC_CHANNEL_##ch)
52 #define TAC_USAGE_REG(func, ent) \
53 	SDW_SDCA_CTL(TAC_FUNCTION_ID_##func, TAC_SDCA_ENT_##ent, \
54 		     TAC_SDCA_CTL_USAGE, 0)
55 #define TAC_XU_BYPASS_REG(func, xu)                        \
56 	SDW_SDCA_CTL(TAC_FUNCTION_ID_##func, TAC_SDCA_ENT_##xu, \
57 			TAC_SDCA_CTL_XU_BYPASS, 0)
58 #define TAC_VOLUME_REG(func, fu, ch) \
59 	SDW_SDCA_CTL(TAC_FUNCTION_ID_##func, TAC_SDCA_ENT_##fu, \
60 		     TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_##ch)
61 #define TAC_GAIN_REG(func, fu, ch) \
62 	SDW_SDCA_CTL(TAC_FUNCTION_ID_##func, TAC_SDCA_ENT_##fu, \
63 		     TAC_SDCA_CHANNEL_GAIN, TAC_CHANNEL_##ch)
64 
65 /* mute registers */
66 #define FU21_L_MUTE_REG  TAC_MUTE_REG(SA, FU21, LEFT)
67 #define FU21_R_MUTE_REG  TAC_MUTE_REG(SA, FU21, RIGHT)
68 #define FU23_L_MUTE_REG  TAC_MUTE_REG(SA, FU23, LEFT)
69 #define FU23_R_MUTE_REG  TAC_MUTE_REG(SA, FU23, RIGHT)
70 #define FU26_MUTE_REG    TAC_MUTE_REG(SA, FU26, LEFT)
71 #define FU11_L_MUTE_REG  TAC_MUTE_REG(SM, FU11, LEFT)
72 #define FU11_R_MUTE_REG  TAC_MUTE_REG(SM, FU11, RIGHT)
73 #define FU113_L_MUTE_REG TAC_MUTE_REG(SM, FU113, LEFT)
74 #define FU113_R_MUTE_REG TAC_MUTE_REG(SM, FU113, RIGHT)
75 #define FU41_L_MUTE_REG  TAC_MUTE_REG(UAJ, FU41, LEFT)
76 #define FU41_R_MUTE_REG  TAC_MUTE_REG(UAJ, FU41, RIGHT)
77 #define FU36_MUTE_REG    TAC_MUTE_REG(UAJ, FU36, RIGHT)
78 
79 /* it/ot usage */
80 #define IT11_USAGE_REG  TAC_USAGE_REG(SM, IT11)
81 #define IT41_USAGE_REG  TAC_USAGE_REG(UAJ, IT41)
82 #define IT33_USAGE_REG  TAC_USAGE_REG(UAJ, IT33)
83 #define OT113_USAGE_REG TAC_USAGE_REG(SM, OT113)
84 #define OT45_USAGE_REG TAC_USAGE_REG(UAJ, OT45)
85 #define OT36_USAGE_REG TAC_USAGE_REG(UAJ, OT36)
86 
87 /* xu bypass */
88 #define XU12_BYPASS_REG TAC_XU_BYPASS_REG(SM, XU12)
89 #define XU42_BYPASS_REG TAC_XU_BYPASS_REG(UAJ, XU42)
90 
91 #define TAC_DSP_ALGO_STATUS		TAC_REG_SDW(0, 3, 12)
92 #define TAC_DSP_ALGO_STATUS_RUNNING	0x20
93 #define TAC_FW_HDR_SIZE		88
94 #define TAC_FW_FILE_HDR	 20
95 #define TAC_MAX_FW_CHUNKS 512
96 
97 #define TAC_UAJ_PREP_CONNECTED		0xff
98 #define TAC_UAJ_PREP_DISCONNECTED	0xdf
99 
100 struct tac_fw_hdr {
101 	u32 size;
102 	u32 version_offset;
103 	u32 plt_id;
104 	u32 ppc3_ver;
105 	u64 timestamp;
106 	u8 ddc_name[64];
107 };
108 
109 /* Firmware file/chunk structure */
110 struct tac_fw_file {
111 	u32 vendor_id;
112 	u32 file_id;
113 	u32 version;
114 	u32 length;
115 	u32 dest_addr;
116 	u8 *fw_data;
117 };
118 
119 /* TLV for volume control */
120 static const DECLARE_TLV_DB_SCALE(tac5xx2_amp_tlv, 0, 50, 0);
121 static const DECLARE_TLV_DB_SCALE(tac5xx2_dvc_tlv, -7200, 50, 0);
122 
123 /* Q7.8 volume control parameters: range -72dB to +6dB, step 0.5dB */
124 #define TAC_DVC_STEP		128	/* 0.5 dB in Q7.8 format */
125 #define TAC_DVC_MIN		(-144)	/* -72 dB / 0.5 dB step */
126 #define TAC_DVC_MAX		12	/* +6 dB / 0.5 dB step */
127 
128 /* TAC-specific stereo volume control macro using SDW_SDCA_CTL (single control for L/R) */
129 #define TAC_DOUBLE_Q78_TLV(name, func_id, ent_id) \
130 	SDCA_DOUBLE_Q78_TLV(name, \
131 			    SDW_SDCA_CTL(TAC_FUNCTION_ID_##func_id, TAC_SDCA_ENT_##ent_id, \
132 					 TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT), \
133 			    SDW_SDCA_CTL(TAC_FUNCTION_ID_##func_id, TAC_SDCA_ENT_##ent_id, \
134 					 TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT), \
135 			    TAC_DVC_MIN, TAC_DVC_MAX, TAC_DVC_STEP, tac5xx2_dvc_tlv)
136 
137 struct tac5xx2_prv {
138 	struct snd_soc_component *component;
139 	struct sdw_slave *sdw_peripheral;
140 	struct sdca_function_data *sa_func_data;
141 	struct sdca_function_data *sm_func_data;
142 	struct sdca_function_data *uaj_func_data;
143 	struct sdca_function_data *hid_func_data;
144 	enum sdw_slave_status status;
145 	struct regmap *regmap;
146 	struct device *dev;
147 	bool hw_init;
148 	bool first_hw_init_done;
149 	u32 part_id;
150 	u32 rev_id;
151 	struct snd_soc_jack *hs_jack;
152 	int jack_type;
153 	 /* Custom fw binary. UMP File Download is not used. */
154 	unsigned int fw_file_cnt;
155 	struct tac_fw_file *fw_files;
156 	struct completion fw_caching_complete;
157 	bool fw_dl_success;
158 	u8 fw_binaryname[64];
159 };
160 
161 static const struct reg_default tac_reg_default[] = {
162 	{TAC_SW_RESET, 0x0},
163 	{TAC_SLEEP_MODEZ, 0x0},
164 	{TAC_FEATURE_PDZ, 0x0},
165 	{TAC_TX_CH_EN, 0xf0},
166 	{TAC_REG_SDW(0, 0, 0x5), 0xcf},
167 	{TAC_REG_SDW(0, 0, 0x6), 0xa},
168 	{TAC_REG_SDW(0, 0, 0x7), 0x0},
169 	{TAC_REG_SDW(0, 0, 0x8), 0xfe},
170 	{TAC_REG_SDW(0, 0, 0x9), 0x9},
171 	{TAC_REG_SDW(0, 0, 0xa), 0x28},
172 	{TAC_REG_SDW(0, 0, 0xb), 0x1},
173 	{TAC_REG_SDW(0, 0, 0xc), 0x11},
174 	{TAC_REG_SDW(0, 0, 0xd), 0x11},
175 	{TAC_REG_SDW(0, 0, 0xe), 0x61},
176 	{TAC_REG_SDW(0, 0, 0xf), 0x0},
177 	{TAC_REG_SDW(0, 0, 0x10), 0x50},
178 	{TAC_REG_SDW(0, 0, 0x11), 0x70},
179 	{TAC_REG_SDW(0, 0, 0x12), 0x60},
180 	{TAC_REG_SDW(0, 0, 0x13), 0x28},
181 	{TAC_REG_SDW(0, 0, 0x14), 0x0},
182 	{TAC_REG_SDW(0, 0, 0x15), 0x18},
183 	{TAC_REG_SDW(0, 0, 0x16), 0x20},
184 	{TAC_REG_SDW(0, 0, 0x17), 0x0},
185 	{TAC_REG_SDW(0, 0, 0x18), 0x18},
186 	{TAC_REG_SDW(0, 0, 0x19), 0x54},
187 	{TAC_REG_SDW(0, 0, 0x1a), 0x8},
188 	{TAC_REG_SDW(0, 0, 0x1b), 0x0},
189 	{TAC_REG_SDW(0, 0, 0x1c), 0x30},
190 	{TAC_REG_SDW(0, 0, 0x1d), 0x0},
191 	{TAC_REG_SDW(0, 0, 0x1e), 0x0},
192 	{TAC_REG_SDW(0, 0, 0x1f), 0x0},
193 	{TAC_REG_SDW(0, 0, 0x20), 0x0},
194 	{TAC_REG_SDW(0, 0, 0x21), 0x20},
195 	{TAC_REG_SDW(0, 0, 0x22), 0x21},
196 	{TAC_REG_SDW(0, 0, 0x23), 0x22},
197 	{TAC_REG_SDW(0, 0, 0x24), 0x23},
198 	{TAC_REG_SDW(0, 0, 0x25), 0x4},
199 	{TAC_REG_SDW(0, 0, 0x26), 0x5},
200 	{TAC_REG_SDW(0, 0, 0x27), 0x6},
201 	{TAC_REG_SDW(0, 0, 0x28), 0x7},
202 	{TAC_REG_SDW(0, 0, 0x29), 0x0},
203 	{TAC_REG_SDW(0, 0, 0x2a), 0x0},
204 	{TAC_REG_SDW(0, 0, 0x2b), 0x0},
205 	{TAC_REG_SDW(0, 0, 0x2c), 0x20},
206 	{TAC_REG_SDW(0, 0, 0x2d), 0x21},
207 	{TAC_REG_SDW(0, 0, 0x2e), 0x2},
208 	{TAC_REG_SDW(0, 0, 0x2f), 0x3},
209 	{TAC_REG_SDW(0, 0, 0x30), 0x4},
210 	{TAC_REG_SDW(0, 0, 0x31), 0x5},
211 	{TAC_REG_SDW(0, 0, 0x32), 0x6},
212 	{TAC_REG_SDW(0, 0, 0x33), 0x7},
213 	{TAC_REG_SDW(0, 0, 0x34), 0x0},
214 	{TAC_REG_SDW(0, 0, 0x35), 0x90},
215 	{TAC_REG_SDW(0, 0, 0x36), 0x80},
216 	{TAC_REG_SDW(0, 0, 0x37), 0x0},
217 	{TAC_REG_SDW(0, 0, 0x39), 0x0},
218 	{TAC_REG_SDW(0, 0, 0x3a), 0x90},
219 	{TAC_REG_SDW(0, 0, 0x3b), 0x80},
220 	{TAC_REG_SDW(0, 0, 0x3c), 0x0},
221 	{TAC_REG_SDW(0, 0, 0x3e), 0x0},
222 	{TAC_REG_SDW(0, 0, 0x3f), 0x90},
223 	{TAC_REG_SDW(0, 0, 0x40), 0x80},
224 	{TAC_REG_SDW(0, 0, 0x41), 0x0},
225 	{TAC_REG_SDW(0, 0, 0x43), 0x90},
226 	{TAC_REG_SDW(0, 0, 0x44), 0x80},
227 	{TAC_REG_SDW(0, 0, 0x45), 0x0},
228 	{TAC_REG_SDW(0, 0, 0x47), 0x90},
229 	{TAC_REG_SDW(0, 0, 0x48), 0x80},
230 	{TAC_REG_SDW(0, 0, 0x49), 0x0},
231 	{TAC_REG_SDW(0, 0, 0x4b), 0x90},
232 	{TAC_REG_SDW(0, 0, 0x4c), 0x80},
233 	{TAC_REG_SDW(0, 0, 0x4d), 0x0},
234 	{TAC_REG_SDW(0, 0, 0x4f), 0x31},
235 	{TAC_REG_SDW(0, 0, 0x50), 0x0},
236 	{TAC_REG_SDW(0, 0, 0x51), 0x0},
237 	{TAC_REG_SDW(0, 0, 0x52), 0x90},
238 	{TAC_REG_SDW(0, 0, 0x53), 0x80},
239 	{TAC_REG_SDW(0, 0, 0x55), 0x90},
240 	{TAC_REG_SDW(0, 0, 0x56), 0x80},
241 	{TAC_REG_SDW(0, 0, 0x58), 0x90},
242 	{TAC_REG_SDW(0, 0, 0x59), 0x80},
243 	{TAC_REG_SDW(0, 0, 0x5b), 0x90},
244 	{TAC_REG_SDW(0, 0, 0x5c), 0x80},
245 	{TAC_REG_SDW(0, 0, 0x5e), 0x8},
246 	{TAC_REG_SDW(0, 0, 0x5f), 0x8},
247 	{TAC_REG_SDW(0, 0, 0x60), 0x0},
248 	{TAC_REG_SDW(0, 0, 0x61), 0x0},
249 	{TAC_REG_SDW(0, 0, 0x62), 0xff},
250 	{TAC_REG_SDW(0, 0, 0x63), 0xc0},
251 	{TAC_REG_SDW(0, 0, 0x64), 0x5},
252 	{TAC_REG_SDW(0, 0, 0x65), 0x3},
253 	{TAC_REG_SDW(0, 0, 0x66), 0x0},
254 	{TAC_REG_SDW(0, 0, 0x67), 0x0},
255 	{TAC_REG_SDW(0, 0, 0x68), 0x0},
256 	{TAC_REG_SDW(0, 0, 0x69), 0x8},
257 	{TAC_REG_SDW(0, 0, 0x6a), 0x0},
258 	{TAC_REG_SDW(0, 0, 0x6b), 0xa0},
259 	{TAC_REG_SDW(0, 0, 0x6c), 0x18},
260 	{TAC_REG_SDW(0, 0, 0x6d), 0x18},
261 	{TAC_REG_SDW(0, 0, 0x6e), 0x18},
262 	{TAC_REG_SDW(0, 0, 0x6f), 0x18},
263 	{TAC_REG_SDW(0, 0, 0x70), 0x88},
264 	{TAC_REG_SDW(0, 0, 0x71), 0xff},
265 	{TAC_REG_SDW(0, 0, 0x72), 0x0},
266 	{TAC_REG_SDW(0, 0, 0x73), 0x31},
267 	{TAC_REG_SDW(0, 0, 0x74), 0xc0},
268 	{TAC_REG_SDW(0, 0, 0x75), 0x0},
269 	{TAC_REG_SDW(0, 0, 0x76), 0x0},
270 	{TAC_REG_SDW(0, 0, 0x77), 0x0},
271 	{TAC_REG_SDW(0, 0, 0x78), 0x0},
272 	{TAC_REG_SDW(0, 0, 0x7b), 0x0},
273 	{TAC_REG_SDW(0, 0, 0x7c), 0xd0},
274 	{TAC_REG_SDW(0, 0, 0x7e), 0x0},
275 	{TAC_REG_SDW(0, 1, 0x1), 0x0},
276 	{TAC_REG_SDW(0, 1, 0x2), 0x0},
277 	{TAC_REG_SDW(0, 1, 0x3), 0x0},
278 	{TAC_REG_SDW(0, 1, 0x4), 0x4},
279 	{TAC_REG_SDW(0, 1, 0x5), 0x0},
280 	{TAC_REG_SDW(0, 1, 0x6), 0x0},
281 	{TAC_REG_SDW(0, 1, 0x7), 0x0},
282 	{TAC_REG_SDW(0, 1, 0x8), 0x0},
283 	{TAC_REG_SDW(0, 1, 0x9), 0x0},
284 	{TAC_REG_SDW(0, 1, 0xa), 0x0},
285 	{TAC_REG_SDW(0, 1, 0xb), 0x1},
286 	{TAC_REG_SDW(0, 1, 0xc), 0x0},
287 	{TAC_REG_SDW(0, 1, 0xd), 0x0},
288 	{TAC_REG_SDW(0, 1, 0xe), 0x0},
289 	{TAC_REG_SDW(0, 1, 0xf), 0x8},
290 	{TAC_REG_SDW(0, 1, 0x10), 0x0},
291 	{TAC_REG_SDW(0, 1, 0x11), 0x0},
292 	{TAC_REG_SDW(0, 1, 0x12), 0x1},
293 	{TAC_REG_SDW(0, 1, 0x13), 0x0},
294 	{TAC_REG_SDW(0, 1, 0x14), 0x0},
295 	{TAC_REG_SDW(0, 1, 0x15), 0x0},
296 	{TAC_REG_SDW(0, 1, 0x16), 0x0},
297 	{TAC_REG_SDW(0, 1, 0x17), 0x0},
298 	{TAC_REG_SDW(0, 1, 0x18), 0x0},
299 	{TAC_REG_SDW(0, 1, 0x19), 0x0},
300 	{TAC_REG_SDW(0, 1, 0x1a), 0x0},
301 	{TAC_REG_SDW(0, 1, 0x1b), 0x0},
302 	{TAC_REG_SDW(0, 1, 0x1c), 0x0},
303 	{TAC_REG_SDW(0, 1, 0x1d), 0x0},
304 	{TAC_REG_SDW(0, 1, 0x1e), 0x2},
305 	{TAC_REG_SDW(0, 1, 0x1f), 0x8},
306 	{TAC_REG_SDW(0, 1, 0x20), 0x9},
307 	{TAC_REG_SDW(0, 1, 0x21), 0xa},
308 	{TAC_REG_SDW(0, 1, 0x22), 0xb},
309 	{TAC_REG_SDW(0, 1, 0x23), 0xc},
310 	{TAC_REG_SDW(0, 1, 0x24), 0xd},
311 	{TAC_REG_SDW(0, 1, 0x25), 0xe},
312 	{TAC_REG_SDW(0, 1, 0x26), 0xf},
313 	{TAC_REG_SDW(0, 1, 0x27), 0x8},
314 	{TAC_REG_SDW(0, 1, 0x28), 0x9},
315 	{TAC_REG_SDW(0, 1, 0x29), 0xa},
316 	{TAC_REG_SDW(0, 1, 0x2a), 0xb},
317 	{TAC_REG_SDW(0, 1, 0x2b), 0xc},
318 	{TAC_REG_SDW(0, 1, 0x2c), 0xd},
319 	{TAC_REG_SDW(0, 1, 0x2d), 0xe},
320 	{TAC_REG_SDW(0, 1, 0x2e), 0xf},
321 	{TAC_REG_SDW(0, 1, 0x2f), 0x0},
322 	{TAC_REG_SDW(0, 1, 0x30), 0x0},
323 	{TAC_REG_SDW(0, 1, 0x31), 0x0},
324 	{TAC_REG_SDW(0, 1, 0x32), 0x0},
325 	{TAC_REG_SDW(0, 1, 0x33), 0x0},
326 	{TAC_REG_SDW(0, 1, 0x34), 0x0},
327 	{TAC_REG_SDW(0, 1, 0x35), 0x0},
328 	{TAC_REG_SDW(0, 1, 0x36), 0x0},
329 	{TAC_REG_SDW(0, 1, 0x37), 0x0},
330 	{TAC_REG_SDW(0, 1, 0x38), 0x98},
331 	{TAC_REG_SDW(0, 1, 0x39), 0x0},
332 	{TAC_REG_SDW(0, 1, 0x3a), 0x0},
333 	{TAC_REG_SDW(0, 1, 0x3b), 0x0},
334 	{TAC_REG_SDW(0, 1, 0x3c), 0x1},
335 	{TAC_REG_SDW(0, 1, 0x3d), 0x2},
336 	{TAC_REG_SDW(0, 1, 0x3e), 0x3},
337 	{TAC_REG_SDW(0, 1, 0x3f), 0x4},
338 	{TAC_REG_SDW(0, 1, 0x40), 0x5},
339 	{TAC_REG_SDW(0, 1, 0x41), 0x6},
340 	{TAC_REG_SDW(0, 1, 0x42), 0x7},
341 	{TAC_REG_SDW(0, 1, 0x43), 0x0},
342 	{TAC_REG_SDW(0, 1, 0x44), 0x0},
343 	{TAC_REG_SDW(0, 1, 0x45), 0x1},
344 	{TAC_REG_SDW(0, 1, 0x46), 0x2},
345 	{TAC_REG_SDW(0, 1, 0x47), 0x3},
346 	{TAC_REG_SDW(0, 1, 0x48), 0x4},
347 	{TAC_REG_SDW(0, 1, 0x49), 0x5},
348 	{TAC_REG_SDW(0, 1, 0x4a), 0x6},
349 	{TAC_REG_SDW(0, 1, 0x4b), 0x7},
350 	{TAC_REG_SDW(0, 1, 0x4c), 0x98},
351 	{TAC_REG_SDW(0, 1, 0x4d), 0x0},
352 	{TAC_REG_SDW(0, 1, 0x4e), 0x0},
353 	{TAC_REG_SDW(0, 1, 0x4f), 0x0},
354 	{TAC_REG_SDW(0, 1, 0x50), 0x1},
355 	{TAC_REG_SDW(0, 1, 0x51), 0x2},
356 	{TAC_REG_SDW(0, 1, 0x52), 0x3},
357 	{TAC_REG_SDW(0, 1, 0x53), 0x4},
358 	{TAC_REG_SDW(0, 1, 0x54), 0x5},
359 	{TAC_REG_SDW(0, 1, 0x55), 0x6},
360 	{TAC_REG_SDW(0, 1, 0x56), 0x7},
361 	{TAC_REG_SDW(0, 1, 0x57), 0x0},
362 	{TAC_REG_SDW(0, 1, 0x58), 0x0},
363 	{TAC_REG_SDW(0, 1, 0x59), 0x1},
364 	{TAC_REG_SDW(0, 1, 0x5a), 0x2},
365 	{TAC_REG_SDW(0, 1, 0x5b), 0x3},
366 	{TAC_REG_SDW(0, 1, 0x5c), 0x4},
367 	{TAC_REG_SDW(0, 1, 0x5d), 0x5},
368 	{TAC_REG_SDW(0, 1, 0x5e), 0x6},
369 	{TAC_REG_SDW(0, 1, 0x5f), 0x7},
370 	{TAC_REG_SDW(0, 1, 0x60), 0x98},
371 	{TAC_REG_SDW(0, 1, 0x61), 0x0},
372 	{TAC_REG_SDW(0, 1, 0x62), 0x0},
373 	{TAC_REG_SDW(0, 1, 0x63), 0x0},
374 	{TAC_REG_SDW(0, 1, 0x64), 0x1},
375 	{TAC_REG_SDW(0, 1, 0x65), 0x2},
376 	{TAC_REG_SDW(0, 1, 0x66), 0x3},
377 	{TAC_REG_SDW(0, 1, 0x67), 0x4},
378 	{TAC_REG_SDW(0, 1, 0x68), 0x5},
379 	{TAC_REG_SDW(0, 1, 0x69), 0x6},
380 	{TAC_REG_SDW(0, 1, 0x6a), 0x7},
381 	{TAC_REG_SDW(0, 1, 0x6b), 0x0},
382 	{TAC_REG_SDW(0, 1, 0x6c), 0x0},
383 	{TAC_REG_SDW(0, 1, 0x6d), 0x1},
384 	{TAC_REG_SDW(0, 1, 0x6e), 0x2},
385 	{TAC_REG_SDW(0, 1, 0x6f), 0x3},
386 	{TAC_REG_SDW(0, 1, 0x70), 0x4},
387 	{TAC_REG_SDW(0, 1, 0x71), 0x5},
388 	{TAC_REG_SDW(0, 1, 0x72), 0x6},
389 	{TAC_REG_SDW(0, 1, 0x73), 0x7},
390 	/* SA */
391 	{TAC_MUTE_REG(SA, FU21, LEFT), 0x1},
392 	{TAC_MUTE_REG(SA, FU21, RIGHT), 0x1},
393 	{TAC_VOLUME_REG(SA, FU21, LEFT), 0x9c00},
394 	{TAC_VOLUME_REG(SA, FU21, RIGHT), 0x9c00},
395 	{TAC_MUTE_REG(SA, FU23, LEFT), 0x1},
396 	{TAC_MUTE_REG(SA, FU23, RIGHT), 0x1},
397 	{TAC_GAIN_REG(SA, FU23, LEFT), 0x0},
398 	{TAC_GAIN_REG(SA, FU23, RIGHT), 0x0},
399 	/* SM */
400 	{TAC_USAGE_REG(SM, IT11), 0x0},
401 	{TAC_USAGE_REG(SM, OT113), 0x0},
402 	{TAC_MUTE_REG(SM, FU113, LEFT), 0x1},
403 	{TAC_MUTE_REG(SM, FU113, RIGHT), 0x1},
404 	{TAC_GAIN_REG(SM, FU113, LEFT), 0x0},
405 	{TAC_GAIN_REG(SM, FU113, RIGHT), 0x0},
406 	{TAC_MUTE_REG(SM, FU11,  LEFT), 0x1},
407 	{TAC_MUTE_REG(SM, FU11,  RIGHT), 0x1},
408 	{TAC_GAIN_REG(SM, FU11,  LEFT), 0x0},
409 	{TAC_GAIN_REG(SM, FU11,  RIGHT), 0x0},
410 	{TAC_XU_BYPASS_REG(SM, XU12), 0x1},
411 	{SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_CS113,
412 		      TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0), 0x0},
413 	/* UAJ */
414 	{TAC_USAGE_REG(UAJ, IT33), 0x0},
415 	{TAC_USAGE_REG(UAJ, IT41), 0x0},
416 	{TAC_USAGE_REG(UAJ, OT36), 0x0},
417 	{TAC_USAGE_REG(UAJ, OT45), 0x0},
418 	{TAC_MUTE_REG(UAJ, FU41, LEFT), 0x1},
419 	{TAC_MUTE_REG(UAJ, FU41, RIGHT), 0x1},
420 	{TAC_VOLUME_REG(UAJ, FU41, LEFT), 0x0},
421 	{TAC_VOLUME_REG(UAJ, FU41, RIGHT), 0x0},
422 	{TAC_MUTE_REG(UAJ, FU36, RIGHT), 0x1},
423 	{SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_FU36,
424 		      TAC_SDCA_CHANNEL_VOLUME, TAC_JACK_MONO_CS), 0x0},
425 	{TAC_XU_BYPASS_REG(UAJ, XU42), 0x0},
426 	{SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_CS36,
427 		      TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0), 0x0},
428 	{SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_CS41,
429 		      TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0), 0x0},
430 };
431 
432 static const struct reg_sequence tac_spk_seq[] = {
433 	REG_SEQ0(SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU21,
434 			      TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT), 0),
435 	REG_SEQ0(SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU21,
436 			      TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT), 0),
437 };
438 
439 static bool tac_volatile_reg(struct device *dev, unsigned int reg)
440 {
441 	switch (reg) {
442 	case TAC_REG_SDW(0, 0, 1) ... TAC_REG_SDW(0, 0, 5):
443 	case TAC_REG_SDW(0, 2, 1) ... TAC_REG_SDW(0, 2, 6):
444 	case TAC_REG_SDW(0, 2, 24) ... TAC_REG_SDW(0, 2, 55):
445 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_HID, TAC_SDCA_ENT_HID1,
446 			  TAC_SDCA_CTL_HIDTX_CURRENT_OWNER, 0):
447 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_HID, TAC_SDCA_ENT_HID1,
448 			  TAC_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0):
449 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_GE35,
450 			  TAC_SDCA_CTL_DET_MODE, 0):
451 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_PDE23,
452 			  TAC_SDCA_REQUESTED_PS, 0):
453 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_PDE11,
454 			  TAC_SDCA_REQUESTED_PS, 0):
455 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_PDE47,
456 			  TAC_SDCA_REQUESTED_PS, 0):
457 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_PDE34,
458 			  TAC_SDCA_REQUESTED_PS, 0):
459 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_PDE23,
460 			  TAC_SDCA_ACTUAL_PS, 0):
461 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_PDE11,
462 			  TAC_SDCA_ACTUAL_PS, 0):
463 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_PDE47,
464 			  TAC_SDCA_ACTUAL_PS, 0):
465 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_PDE34,
466 			  TAC_SDCA_ACTUAL_PS, 0):
467 	case SDW_SCP_SDCA_INT1:
468 	case SDW_SCP_SDCA_INT2:
469 	case SDW_SCP_SDCA_INT3:
470 	case SDW_SCP_SDCA_INT4:
471 	case SDW_SDCA_CTL(1, 0, 0x10, 0):
472 	case SDW_SDCA_CTL(2, 0, 0x10, 0):
473 	case SDW_SDCA_CTL(3, 0, 0x10, 0):
474 	case SDW_SDCA_CTL(4, 0, 0x1, 0):
475 	case 0x44007F80 ... 0x44007F87:
476 	case TAC_DSP_ALGO_STATUS:	/* DSP algo status - always read from HW */
477 		return true;
478 	default:
479 		break;
480 	}
481 
482 	return false;
483 }
484 
485 static int tac_sdca_mbq_size(struct device *dev, unsigned int reg)
486 {
487 	switch (reg) {
488 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU21,
489 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT):
490 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU21,
491 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT):
492 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU23,
493 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT):
494 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU23,
495 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT):
496 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SA, TAC_SDCA_ENT_FU23,
497 			  TAC_SDCA_CHANNEL_GAIN, 0):
498 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_FU113,
499 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT):
500 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_FU113,
501 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT):
502 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_FU11,
503 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT):
504 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_SM, TAC_SDCA_ENT_FU11,
505 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT):
506 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_FU41,
507 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_LEFT):
508 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_FU41,
509 			  TAC_SDCA_CHANNEL_VOLUME, TAC_CHANNEL_RIGHT):
510 	case SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_FU36,
511 			  TAC_SDCA_CHANNEL_VOLUME, TAC_JACK_MONO_CS):
512 		return 2;
513 
514 	default:
515 		return 1;
516 	}
517 }
518 
519 static const struct regmap_sdw_mbq_cfg tac_mbq_cfg = {
520 	.mbq_size = tac_sdca_mbq_size,
521 };
522 
523 static const struct regmap_config tac_regmap = {
524 	.reg_bits = 32,
525 	.val_bits = 16, /* mbq support */
526 	.reg_defaults = tac_reg_default,
527 	.num_reg_defaults = ARRAY_SIZE(tac_reg_default),
528 	.max_register = 0x47FFFFFF,
529 	.cache_type = REGCACHE_MAPLE,
530 	.volatile_reg = tac_volatile_reg,
531 	.use_single_read = true,
532 	.use_single_write = true,
533 };
534 
535 /* Check if device has UAJ (Universal Audio Jack) support */
536 static bool tac_has_uaj_support(struct tac5xx2_prv *tac_dev)
537 {
538 	return tac_dev->uaj_func_data;
539 }
540 
541 /* Forward declaration for headset detection */
542 static int tac5xx2_sdca_headset_detect(struct tac5xx2_prv *tac_dev);
543 
544 /* Volume controls for mic, hp and mic cap */
545 static const struct snd_kcontrol_new tac5xx2_snd_controls[] = {
546 	SOC_DOUBLE_R_RANGE_TLV("Amp Volume", TAC_AMP_LVL_CFG0, TAC_AMP_LVL_CFG1,
547 			       2, 0, 44, 1, tac5xx2_amp_tlv),
548 	TAC_DOUBLE_Q78_TLV("DMIC Capture Volume", SM, FU113),
549 	TAC_DOUBLE_Q78_TLV("Speaker Volume", SA, FU21),
550 };
551 
552 static const struct snd_kcontrol_new tac_uaj_controls[] = {
553 	TAC_DOUBLE_Q78_TLV("UAJ Playback Volume", UAJ, FU41),
554 	SDCA_SINGLE_Q78_TLV("UAJ Capture Volume",
555 			    SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_FU36,
556 					 TAC_SDCA_CHANNEL_VOLUME, TAC_JACK_MONO_CS),
557 			   TAC_DVC_MIN, TAC_DVC_MAX, TAC_DVC_STEP, tac5xx2_dvc_tlv),
558 };
559 
560 static const struct snd_soc_dapm_widget tac5xx2_common_widgets[] = {
561 	/* Port 1: Speaker Playback Path */
562 	SND_SOC_DAPM_AIF_IN("AIF1 Playback", "DP1 Speaker Playback", 0,
563 			    SND_SOC_NOPM, 0, 0),
564 	SND_SOC_DAPM_PGA("FU21_L", FU21_L_MUTE_REG, 0, 1, NULL, 0),
565 	SND_SOC_DAPM_PGA("FU21_R", FU21_R_MUTE_REG, 0, 1, NULL, 0),
566 	SND_SOC_DAPM_PGA("FU23_L", FU23_L_MUTE_REG, 0, 1, NULL, 0),
567 	SND_SOC_DAPM_PGA("FU23_R", FU23_R_MUTE_REG, 0, 1, NULL, 0),
568 	SND_SOC_DAPM_OUTPUT("SPK_L"),
569 	SND_SOC_DAPM_OUTPUT("SPK_R"),
570 
571 	/* Port 3: Smart Mic (DMIC) Capture Path */
572 	SND_SOC_DAPM_AIF_OUT("AIF3 Capture", "DP3 Mic Capture", 0, SND_SOC_NOPM, 0, 0),
573 	SND_SOC_DAPM_INPUT("DMIC_L"),
574 	SND_SOC_DAPM_INPUT("DMIC_R"),
575 	SND_SOC_DAPM_PGA("IT11", IT11_USAGE_REG, 0, 0, NULL, 0),
576 	SND_SOC_DAPM_SUPPLY("CS113", SND_SOC_NOPM, 0, 0, NULL, 0),
577 	SND_SOC_DAPM_PGA("FU11_L", FU11_L_MUTE_REG, 0, 1, NULL, 0),
578 	SND_SOC_DAPM_PGA("FU11_R", FU11_R_MUTE_REG, 0, 1, NULL, 0),
579 	SND_SOC_DAPM_PGA("PPU11", SND_SOC_NOPM, 0, 0, NULL, 0),
580 	SND_SOC_DAPM_PGA("XU12", XU12_BYPASS_REG, 0, 0, NULL, 0),
581 	SND_SOC_DAPM_PGA("FU113_L", FU113_L_MUTE_REG, 0, 1, NULL, 0),
582 	SND_SOC_DAPM_PGA("FU113_R", FU113_R_MUTE_REG, 0, 1, NULL, 0),
583 	SND_SOC_DAPM_PGA("OT113", OT113_USAGE_REG, 0, 0, NULL, 0),
584 };
585 
586 static const struct snd_soc_dapm_widget tac_uaj_widgets[] = {
587 	/* Port 4: UAJ (Headphone) Playback Path */
588 	SND_SOC_DAPM_AIF_IN("AIF4 Playback", "DP4 UAJ Speaker Playback", 0,
589 			    SND_SOC_NOPM, 0, 0),
590 	SND_SOC_DAPM_PGA("IT41", IT41_USAGE_REG, 0, 0, NULL, 0),
591 	SND_SOC_DAPM_PGA("FU41_L", FU41_L_MUTE_REG, 0, 1, NULL, 0),
592 	SND_SOC_DAPM_PGA("FU41_R", FU41_R_MUTE_REG, 0, 1, NULL, 0),
593 	SND_SOC_DAPM_PGA("XU42", XU42_BYPASS_REG, 0, 0, NULL, 0),
594 	SND_SOC_DAPM_SUPPLY("CS41", SND_SOC_NOPM, 0, 0, NULL, 0),
595 	SND_SOC_DAPM_DAC("OT45", "DP4 UAJ Speaker Playback", OT45_USAGE_REG, 0, 0),
596 	SND_SOC_DAPM_OUTPUT("HP_L"),
597 	SND_SOC_DAPM_OUTPUT("HP_R"),
598 
599 	/* Port 7: UAJ (Headset Mic) Capture Path */
600 	SND_SOC_DAPM_AIF_OUT("AIF7 Capture", "DP7 UAJ Mic Capture", 0,
601 			     SND_SOC_NOPM, 0, 0),
602 	SND_SOC_DAPM_INPUT("UAJ_MIC"),
603 	SND_SOC_DAPM_ADC("IT33", "DP7 UAJ Mic Capture", IT33_USAGE_REG, 0, 0),
604 	SND_SOC_DAPM_PGA("FU36", FU36_MUTE_REG, 0, 1, NULL, 0),
605 	SND_SOC_DAPM_SUPPLY("CS36", SND_SOC_NOPM, 0, 0, NULL, 0),
606 	SND_SOC_DAPM_PGA("OT36", OT36_USAGE_REG, 0, 0, NULL, 0),
607 };
608 
609 static const struct snd_soc_dapm_route tac5xx2_common_routes[] = {
610 	/* Speaker Playback Path */
611 	{"FU21_L", NULL, "AIF1 Playback"},
612 	{"FU21_R", NULL, "AIF1 Playback"},
613 
614 	{"FU23_L", NULL, "FU21_L"},
615 	{"FU23_R", NULL, "FU21_R"},
616 
617 	{"SPK_L", NULL, "FU23_L"},
618 	{"SPK_R", NULL, "FU23_R"},
619 
620 	/* Smart Mic DAPM Routes */
621 	{"IT11", NULL, "DMIC_L"},
622 	{"IT11", NULL, "DMIC_R"},
623 	{"FU11_L", NULL, "IT11"},
624 	{"FU11_R", NULL, "IT11"},
625 	{"PPU11", NULL, "FU11_L"},
626 	{"PPU11", NULL, "FU11_R"},
627 	{"XU12", NULL, "PPU11"},
628 	{"FU113_L", NULL, "XU12"},
629 	{"FU113_R", NULL, "XU12"},
630 	{"FU113_L", NULL, "CS113"},
631 	{"FU113_R", NULL, "CS113"},
632 	{"OT113", NULL, "FU113_L"},
633 	{"OT113", NULL, "FU113_R"},
634 	{"OT113", NULL, "CS113"},
635 	{"AIF3 Capture", NULL, "OT113"},
636 };
637 
638 static const struct snd_soc_dapm_route tac_uaj_routes[] = {
639 	/* UAJ Playback routes */
640 	{"IT41", NULL, "AIF4 Playback"},
641 	{"IT41", NULL, "CS41"},
642 	{"FU41_L", NULL, "IT41"},
643 	{"FU41_R", NULL, "IT41"},
644 	{"XU42", NULL, "FU41_L"},
645 	{"XU42", NULL, "FU41_R"},
646 	{"OT45", NULL, "XU42"},
647 	{"OT45", NULL, "CS41"},
648 	{"HP_L", NULL, "OT45"},
649 	{"HP_R", NULL, "OT45"},
650 
651 	/* UAJ Capture routes */
652 	{"IT33", NULL, "UAJ_MIC"},
653 	{"IT33", NULL, "CS36"},
654 	{"FU36", NULL, "IT33"},
655 	{"OT36", NULL, "FU36"},
656 	{"OT36", NULL, "CS36"},
657 	{"AIF7 Capture", NULL, "OT36"},
658 };
659 
660 static s32 tac_set_sdw_stream(struct snd_soc_dai *dai,
661 			      void *sdw_stream, s32 direction)
662 {
663 	if (sdw_stream)
664 		snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
665 
666 	return 0;
667 }
668 
669 static void tac_sdw_shutdown(struct snd_pcm_substream *substream,
670 			     struct snd_soc_dai *dai)
671 {
672 	snd_soc_dai_set_dma_data(dai, substream, NULL);
673 }
674 
675 static int tac_clear_latch(struct tac5xx2_prv *priv)
676 {
677 	/* CLR_REG is a self-clearing bit */
678 	return regmap_update_bits(priv->regmap, TAC_INT_CFG,
679 				  TAC_INT_CFG_CLR_REG, TAC_INT_CFG_CLR_REG);
680 }
681 
682 static int tac_sdw_hw_params(struct snd_pcm_substream *substream,
683 			     struct snd_pcm_hw_params *params,
684 			     struct snd_soc_dai *dai)
685 {
686 	struct snd_soc_component *component = dai->component;
687 	struct tac5xx2_prv *tac_dev = snd_soc_component_get_drvdata(component);
688 	struct sdw_slave *sdw_peripheral = tac_dev->sdw_peripheral;
689 	struct sdw_stream_runtime *sdw_stream;
690 	struct sdw_stream_config stream_config = {0};
691 	struct sdw_port_config port_config = {0};
692 	u8 sample_rate_idx = 0;
693 	int function_id;
694 	int pde_entity;
695 	int port_num;
696 	int ret;
697 
698 	if (!tac_dev->hw_init) {
699 		dev_err(tac_dev->dev,
700 			"error: operation without hw initialization");
701 		return -EINVAL;
702 	}
703 
704 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
705 	if (!sdw_stream) {
706 		dev_err(tac_dev->dev, "failed to get dma data");
707 		return -EINVAL;
708 	}
709 
710 	ret = tac_clear_latch(tac_dev);
711 	if (ret)
712 		dev_warn(tac_dev->dev, "clear latch failed, err=%d", ret);
713 
714 	switch (dai->id) {
715 	case TAC5XX2_DMIC:
716 		function_id = TAC_FUNCTION_ID_SM;
717 		pde_entity = TAC_SDCA_ENT_PDE11;
718 		port_num = TAC_SDW_PORT_NUM_DMIC;
719 		break;
720 	case TAC5XX2_UAJ:
721 		function_id = TAC_FUNCTION_ID_UAJ;
722 		pde_entity = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
723 				TAC_SDCA_ENT_PDE47 : TAC_SDCA_ENT_PDE34;
724 		port_num = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
725 				TAC_SDW_PORT_NUM_UAJ_PLAYBACK :
726 				TAC_SDW_PORT_NUM_UAJ_CAPTURE;
727 		break;
728 	case TAC5XX2_SPK:
729 		function_id = TAC_FUNCTION_ID_SA;
730 		pde_entity = TAC_SDCA_ENT_PDE23;
731 		port_num = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
732 				TAC_SDW_PORT_NUM_SPK_PLAYBACK :
733 				TAC_SDW_PORT_NUM_SPK_CAPTURE;
734 		break;
735 	default:
736 		dev_err(tac_dev->dev, "Invalid dai id: %d for power up\n", dai->id);
737 		return -EINVAL;
738 	}
739 
740 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
741 	port_config.num = port_num;
742 	ret = sdw_stream_add_slave(sdw_peripheral, &stream_config,
743 				   &port_config, 1, sdw_stream);
744 	if (ret) {
745 		dev_err(dai->dev,
746 			"Unable to configure port %d: %d\n", port_num, ret);
747 		return ret;
748 	}
749 
750 	switch (params_rate(params)) {
751 	case 48000:
752 		sample_rate_idx = 0x01;
753 		break;
754 	case 44100:
755 		sample_rate_idx = 0x02;
756 		break;
757 	case 96000:
758 		sample_rate_idx = 0x03;
759 		break;
760 	case 88200:
761 		sample_rate_idx = 0x04;
762 		break;
763 	default:
764 		dev_dbg(tac_dev->dev, "Unsupported sample rate: %d Hz",
765 			params_rate(params));
766 		return -EINVAL;
767 	}
768 
769 	switch (function_id) {
770 	case TAC_FUNCTION_ID_SM:
771 		ret = regmap_write(tac_dev->regmap,
772 				   SDW_SDCA_CTL(function_id, TAC_SDCA_ENT_CS113,
773 						TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0),
774 			sample_rate_idx);
775 		if (ret) {
776 			dev_err(tac_dev->dev, "Failed to set CS113 sample rate: %d", ret);
777 			return ret;
778 		}
779 
780 		break;
781 	case TAC_FUNCTION_ID_UAJ:
782 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
783 			ret = regmap_write(tac_dev->regmap,
784 					   SDW_SDCA_CTL(function_id, TAC_SDCA_ENT_CS41,
785 							TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0),
786 					sample_rate_idx);
787 			if (ret) {
788 				dev_err(tac_dev->dev, "Failed to set CS41 sample rate: %d", ret);
789 				return ret;
790 			}
791 		} else {
792 			ret = regmap_write(tac_dev->regmap,
793 					   SDW_SDCA_CTL(function_id, TAC_SDCA_ENT_CS36,
794 							TAC_SDCA_CTL_CS_SAMP_RATE_IDX, 0),
795 					sample_rate_idx);
796 			if (ret) {
797 				dev_err(tac_dev->dev, "Failed to set CS36 sample rate: %d", ret);
798 				return ret;
799 			}
800 		}
801 		break;
802 	case TAC_FUNCTION_ID_SA:
803 		/* SmartAmp: no additional sample rate configuration needed */
804 		break;
805 	}
806 
807 	ret = regmap_write(tac_dev->regmap, SDW_SDCA_CTL(function_id, pde_entity,
808 							 TAC_SDCA_REQUESTED_PS, 0), 0);
809 	if (ret) {
810 		dev_err(tac_dev->dev,
811 			"failed to set func %d, entity %d's requested PS to 0: %d\n",
812 			function_id, pde_entity, ret);
813 		return ret;
814 	}
815 
816 	ret = sdca_asoc_pde_poll_actual_ps(tac_dev->regmap, function_id, pde_entity,
817 					   SDCA_PDE_PS3, SDCA_PDE_PS0, NULL, 0);
818 	if (ret)
819 		dev_err(tac_dev->dev, "failed to transition func %d, pde %d from PS3 -> PS0, err=%d\n",
820 			function_id, pde_entity, ret);
821 	return ret;
822 }
823 
824 static int tac_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
825 			       struct snd_soc_dai *dai)
826 {
827 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
828 	struct tac5xx2_prv *tac_dev = snd_soc_component_get_drvdata(dai->component);
829 	int pde_entity, function_id;
830 	int ret;
831 
832 	sdw_stream_remove_slave(tac_dev->sdw_peripheral, sdw_stream);
833 
834 	switch (dai->id) {
835 	case TAC5XX2_DMIC:
836 		pde_entity = TAC_SDCA_ENT_PDE11;
837 		function_id = TAC_FUNCTION_ID_SM;
838 		break;
839 	case TAC5XX2_UAJ:
840 		function_id = TAC_FUNCTION_ID_UAJ;
841 		pde_entity = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
842 				TAC_SDCA_ENT_PDE47 : TAC_SDCA_ENT_PDE34;
843 		break;
844 	case TAC5XX2_SPK:
845 		function_id = TAC_FUNCTION_ID_SA;
846 		pde_entity = TAC_SDCA_ENT_PDE23;
847 		break;
848 	default:
849 		dev_err(tac_dev->dev, "unhandled dai %d for power down\n", dai->id);
850 		return -EINVAL;
851 	}
852 
853 	ret = regmap_write(tac_dev->regmap, SDW_SDCA_CTL(function_id, pde_entity,
854 							 TAC_SDCA_REQUESTED_PS, 0),
855 			   SDCA_PDE_PS3);
856 	if (ret) {
857 		dev_err(tac_dev->dev,
858 			"failed to set func %d, entity %d's requested PS to 3: %d\n",
859 			function_id, pde_entity, ret);
860 		return ret;
861 	}
862 
863 	ret = sdca_asoc_pde_poll_actual_ps(tac_dev->regmap, function_id,
864 					   pde_entity, SDCA_PDE_PS0, SDCA_PDE_PS3,
865 					   NULL, 0);
866 	if (ret)
867 		dev_err(tac_dev->dev,
868 			"failed to transition func %d, pde %d from PS0 -> PS3, err=%d\n",
869 			function_id, pde_entity, ret);
870 
871 	return ret;
872 }
873 
874 static const struct snd_soc_dai_ops tac_dai_ops = {
875 	.hw_params = tac_sdw_hw_params,
876 	.hw_free = tac_sdw_pcm_hw_free,
877 	.set_stream = tac_set_sdw_stream,
878 	.shutdown = tac_sdw_shutdown,
879 };
880 
881 static int tac5xx2_sdca_btn_type(unsigned char *buffer, struct tac5xx2_prv *tac_dev)
882 {
883 	switch (*buffer) {
884 	case 1: /* play pause */
885 		return SND_JACK_BTN_0;
886 	case 10: /* vol down */
887 		return SND_JACK_BTN_3;
888 	case 8: /* vol up */
889 		return SND_JACK_BTN_2;
890 	case 4: /* long press */
891 		return SND_JACK_BTN_1;
892 	case 2: /* next song */
893 	case 32: /* next song */
894 		return SND_JACK_BTN_4;
895 	default:
896 		return 0;
897 	}
898 }
899 
900 static int tac5xx2_sdca_button_detect(struct tac5xx2_prv *tac_dev)
901 {
902 	unsigned int btn_type, offset, idx;
903 	int ret, value, owner;
904 	u8 buf[2];
905 
906 	ret = regmap_read(tac_dev->regmap,
907 			  SDW_SDCA_CTL(TAC_FUNCTION_ID_HID, TAC_SDCA_ENT_HID1,
908 				       TAC_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), &owner);
909 	if (ret) {
910 		dev_err(tac_dev->dev,
911 			"Failed to read current UMP message owner 0x%x", ret);
912 		return ret;
913 	}
914 
915 	if (owner == SDCA_UMP_OWNER_DEVICE) {
916 		dev_dbg(tac_dev->dev, "skip button detect as current owner is not host\n");
917 		return 0;
918 	}
919 
920 	ret = regmap_read(tac_dev->regmap,
921 			  SDW_SDCA_CTL(TAC_FUNCTION_ID_HID, TAC_SDCA_ENT_HID1,
922 				       TAC_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), &offset);
923 	if (ret) {
924 		dev_err(tac_dev->dev,
925 			"Failed to read current UMP message offset: %d", ret);
926 		goto end_btn_det;
927 	}
928 
929 	dev_dbg(tac_dev->dev, "button detect: message offset = %x", offset);
930 
931 	for (idx = 0; idx < sizeof(buf); idx++) {
932 		ret = regmap_read(tac_dev->regmap,
933 				  TAC_BUF_ADDR_HID1 + offset + idx, &value);
934 		if (ret) {
935 			dev_err(tac_dev->dev,
936 				"Failed to read HID buffer: %d", ret);
937 			goto end_btn_det;
938 		}
939 		buf[idx] = value & 0xff;
940 	}
941 
942 	if (buf[0] == 0x1) {
943 		btn_type = tac5xx2_sdca_btn_type(&buf[1], tac_dev);
944 		ret = btn_type;
945 	}
946 
947 end_btn_det:
948 	regmap_write(tac_dev->regmap,
949 		     SDW_SDCA_CTL(TAC_FUNCTION_ID_HID, TAC_SDCA_ENT_HID1,
950 				  TAC_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
951 
952 	return ret;
953 }
954 
955 static int tac5xx2_sdca_headset_detect(struct tac5xx2_prv *tac_dev)
956 {
957 	int val, ret;
958 	u8 jack_prep;
959 
960 	ret = regmap_read(tac_dev->regmap,
961 			  SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_GE35,
962 				       TAC_SDCA_CTL_DET_MODE, 0), &val);
963 	if (ret) {
964 		dev_err(tac_dev->dev, "Failed to read the detect mode");
965 		return ret;
966 	}
967 
968 	jack_prep = TAC_UAJ_PREP_CONNECTED;
969 
970 	switch (val) {
971 	case 4:
972 		tac_dev->jack_type = SND_JACK_MICROPHONE;
973 		break;
974 	case 5:
975 		tac_dev->jack_type = SND_JACK_HEADPHONE;
976 		break;
977 	case 6:
978 		tac_dev->jack_type = SND_JACK_HEADSET;
979 		break;
980 	case 0:
981 	default:
982 		tac_dev->jack_type = 0;
983 		jack_prep = TAC_UAJ_PREP_DISCONNECTED;
984 		break;
985 	}
986 
987 	ret = regmap_write(tac_dev->regmap,
988 			   SDW_SDCA_CTL(TAC_FUNCTION_ID_UAJ, TAC_SDCA_ENT_GE35,
989 					TAC_SDCA_CTL_SEL_MODE, 0), val);
990 	if (ret)
991 		dev_err(tac_dev->dev, "Failed to update the jack type to device");
992 
993 	/*
994 	 * When uaj is uplugged and booted, we end up with the channel prepare
995 	 * timeout error for the uaj ports. This writes allows the channel prepare
996 	 * to succeed when the uaj is not plugged in.
997 	 */
998 	if (tac_dev->rev_id >= 0x30) {
999 		ret = regmap_write(tac_dev->regmap, TAC_REG_SDW(0, 3, 127), jack_prep);
1000 		if (ret)
1001 			dev_warn(tac_dev->dev, "Failed to write jack_prep register: %d\n", ret);
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 static int tac5xx2_jack_init(struct tac5xx2_prv *tac_dev)
1008 {
1009 	u32 jd_int_mask, hid_int_mask;
1010 	int ret = 0;
1011 
1012 	if (tac_dev->rev_id >= 0x30) {
1013 		jd_int_mask = SDW_SCP_SDCA_INTMASK_SDCA_12;
1014 		hid_int_mask = SDW_SCP_SDCA_INTMASK_SDCA_17;
1015 	} else {
1016 		jd_int_mask = SDW_SCP_SDCA_INTMASK_SDCA_11;
1017 		hid_int_mask = SDW_SCP_SDCA_INTMASK_SDCA_16;
1018 	}
1019 
1020 	if (!tac_dev->hs_jack)
1021 		goto disable_interrupts;
1022 
1023 	ret = regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INTMASK2, jd_int_mask);
1024 	if (ret) {
1025 		dev_err(tac_dev->dev,
1026 			"Failed to register jack detection interrupt: %d\n", ret);
1027 		goto disable_interrupts;
1028 	}
1029 
1030 	ret = regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INTMASK3, hid_int_mask);
1031 	if (ret) {
1032 		dev_err(tac_dev->dev,
1033 			"Failed to register for button detect interrupt: %d\n", ret);
1034 		goto disable_interrupts;
1035 	}
1036 
1037 	return 0;
1038 
1039 disable_interrupts:
1040 	/* ignore errors while disabling interrupts */
1041 	regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INTMASK2, 0);
1042 	regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INTMASK3, 0);
1043 
1044 	return ret;
1045 }
1046 
1047 static int tac5xx2_set_jack(struct snd_soc_component *component,
1048 			    struct snd_soc_jack *hs_jack, void *data)
1049 {
1050 	struct tac5xx2_prv *tac_dev = snd_soc_component_get_drvdata(component);
1051 	int ret;
1052 
1053 	tac_dev->hs_jack = hs_jack;
1054 
1055 	/* resume can happen only after first hw_init */
1056 	if (!tac_dev->first_hw_init_done)
1057 		return 0;
1058 
1059 	ret = pm_runtime_resume_and_get(component->dev);
1060 	if (ret < 0) {
1061 		if (ret != -EACCES) {
1062 			dev_err(component->dev,
1063 				"%s: failed to resume %d\n", __func__, ret);
1064 			return ret;
1065 		}
1066 
1067 		/* pm_runtime not enabled yet */
1068 		dev_dbg(component->dev,
1069 			"%s: skipping jack init for now\n", __func__);
1070 		return 0;
1071 	}
1072 
1073 	ret = tac5xx2_jack_init(tac_dev);
1074 	if (ret)
1075 		dev_err(tac_dev->dev, "jack init failed, err=%d\n", ret);
1076 
1077 	pm_runtime_mark_last_busy(component->dev);
1078 	pm_runtime_put_autosuspend(component->dev);
1079 
1080 	return ret;
1081 }
1082 
1083 static int tac_interrupt_callback(struct sdw_slave *slave,
1084 				  struct sdw_slave_intr_status *status)
1085 {
1086 	unsigned int sdca_int2, sdca_int3, jack_report_mask = 0;
1087 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(&slave->dev);
1088 	struct device *dev = &slave->dev;
1089 	u32 headset_detect_chk, hid_detect_chk;
1090 	int btn_type = 0;
1091 	int ret = 0;
1092 
1093 	if (status->control_port) {
1094 		if (status->control_port & SDW_SCP_INT1_PARITY)
1095 			dev_warn(dev, "SCP: Parity error interrupt");
1096 		if (status->control_port & SDW_SCP_INT1_BUS_CLASH)
1097 			dev_warn(dev, "SCP: Bus clash interrupt");
1098 	}
1099 
1100 	if (!tac_has_uaj_support(tac_dev))
1101 		return 0;
1102 
1103 	ret = regmap_read(tac_dev->regmap, SDW_SCP_SDCA_INT2, &sdca_int2);
1104 	if (ret) {
1105 		dev_err(dev, "Failed to read UAJ Interrupt, reg:%#x err=%d\n",
1106 			SDW_SCP_SDCA_INT2, ret);
1107 		return ret;
1108 	}
1109 
1110 	ret = regmap_read(tac_dev->regmap, SDW_SCP_SDCA_INT3, &sdca_int3);
1111 	if (ret) {
1112 		dev_err(dev, "Failed to read HID interrupt reg=%#x: err=%d",
1113 			SDW_SCP_SDCA_INT3, ret);
1114 		return ret;
1115 	}
1116 
1117 	dev_dbg(dev, "SDCA_INT2: 0x%02x, SDCA_INT3: 0x%02x\n",
1118 		sdca_int2, sdca_int3);
1119 
1120 	if (tac_dev->rev_id >= 0x30) {
1121 		headset_detect_chk = SDW_SCP_SDCA_INT_SDCA_12;
1122 		hid_detect_chk = SDW_SCP_SDCA_INT_SDCA_17;
1123 	} else {
1124 		headset_detect_chk = SDW_SCP_SDCA_INT_SDCA_11;
1125 		hid_detect_chk = SDW_SCP_SDCA_INT_SDCA_16;
1126 	}
1127 
1128 	if (sdca_int2 & headset_detect_chk) {
1129 		ret = tac5xx2_sdca_headset_detect(tac_dev);
1130 		if (ret < 0)
1131 			goto clear;
1132 		jack_report_mask |= SND_JACK_HEADSET;
1133 	}
1134 
1135 	if (sdca_int3 & hid_detect_chk) {
1136 		btn_type = tac5xx2_sdca_button_detect(tac_dev);
1137 		if (btn_type < 0)
1138 			btn_type = 0;
1139 		jack_report_mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
1140 			SND_JACK_BTN_2 | SND_JACK_BTN_3 | SND_JACK_BTN_4;
1141 	}
1142 
1143 	if (tac_dev->jack_type == 0)
1144 		btn_type = 0;
1145 
1146 	dev_dbg(tac_dev->dev, "in %s, jack_type=%d\n", __func__, tac_dev->jack_type);
1147 	dev_dbg(tac_dev->dev, "in %s, btn_type=0x%x\n", __func__, btn_type);
1148 
1149 	if (!tac_dev->hs_jack)
1150 		goto clear;
1151 
1152 	snd_soc_jack_report(tac_dev->hs_jack, tac_dev->jack_type | btn_type,
1153 			    jack_report_mask);
1154 
1155 clear:
1156 	if (sdca_int2) {
1157 		ret = regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INT2, sdca_int2);
1158 		if (ret)
1159 			dev_dbg(tac_dev->dev, "Failed to clear jack interrupt\n");
1160 	}
1161 
1162 	if (sdca_int3) {
1163 		ret = regmap_write(tac_dev->regmap, SDW_SCP_SDCA_INT3, sdca_int3);
1164 		if (ret)
1165 			dev_dbg(tac_dev->dev, "failed to clear hid interrupt\n");
1166 	}
1167 
1168 	return 0;
1169 }
1170 
1171 static struct snd_soc_dai_driver tac5572_dai_driver[] = {
1172 	{
1173 		.name = "tac5xx2-aif1",
1174 		.id = TAC5XX2_SPK,
1175 		.playback = {
1176 			.stream_name = "DP1 Speaker Playback",
1177 			.channels_min = 1,
1178 			.channels_max = 2,
1179 			.rates = TAC5XX2_DEVICE_RATES,
1180 			.formats = TAC5XX2_DEVICE_FORMATS,
1181 		},
1182 		.ops = &tac_dai_ops,
1183 	},
1184 	{
1185 		.name = "tac5xx2-aif2",
1186 		.id = TAC5XX2_DMIC,
1187 		.capture = {
1188 			.stream_name = "DP3 Mic Capture",
1189 			.channels_min = 1,
1190 			.channels_max = 4,
1191 			.rates = TAC5XX2_DEVICE_RATES,
1192 			.formats = TAC5XX2_DEVICE_FORMATS,
1193 		},
1194 		.ops = &tac_dai_ops,
1195 	},
1196 	{
1197 		.name = "tac5xx2-aif3",
1198 		.id = TAC5XX2_UAJ,
1199 		.playback = {
1200 			.stream_name = "DP4 UAJ Speaker Playback",
1201 			.channels_min = 1,
1202 			.channels_max = 2,
1203 			.rates = TAC5XX2_DEVICE_RATES,
1204 			.formats = TAC5XX2_DEVICE_FORMATS,
1205 		},
1206 		.capture = {
1207 			.stream_name = "DP7 UAJ Mic Capture",
1208 			.channels_min = 1,
1209 			.channels_max = 2,
1210 			.rates = TAC5XX2_DEVICE_RATES,
1211 			.formats = TAC5XX2_DEVICE_FORMATS,
1212 		},
1213 		.ops = &tac_dai_ops,
1214 	},
1215 };
1216 
1217 static struct snd_soc_dai_driver tac5672_dai_driver[] = {
1218 	{
1219 		.name = "tac5xx2-aif1",
1220 		.id = TAC5XX2_SPK,
1221 		.playback = {
1222 			.stream_name = "DP1 Speaker Playback",
1223 			.channels_min = 1,
1224 			.channels_max = 2,
1225 			.rates = TAC5XX2_DEVICE_RATES,
1226 			.formats = TAC5XX2_DEVICE_FORMATS,
1227 		},
1228 		.capture = {
1229 			.stream_name = "DP8 IV Sense Capture",
1230 			.channels_min = 1,
1231 			.channels_max = 4,
1232 			.rates = TAC5XX2_DEVICE_RATES,
1233 			.formats = TAC5XX2_DEVICE_FORMATS,
1234 		},
1235 		.ops = &tac_dai_ops,
1236 		.symmetric_rate = 1,
1237 	},
1238 	{
1239 		.name = "tac5xx2-aif2",
1240 		.id = TAC5XX2_DMIC,
1241 		.capture = {
1242 			.stream_name = "DP3 Mic Capture",
1243 			.channels_min = 1,
1244 			.channels_max = 4,
1245 			.rates = TAC5XX2_DEVICE_RATES,
1246 			.formats = TAC5XX2_DEVICE_FORMATS,
1247 		},
1248 		.ops = &tac_dai_ops,
1249 	},
1250 	{
1251 		.name = "tac5xx2-aif3",
1252 		.id = TAC5XX2_UAJ,
1253 		.playback = {
1254 			.stream_name = "DP4 UAJ Speaker Playback",
1255 			.channels_min = 1,
1256 			.channels_max = 2,
1257 			.rates = TAC5XX2_DEVICE_RATES,
1258 			.formats = TAC5XX2_DEVICE_FORMATS,
1259 		},
1260 		.capture = {
1261 			.stream_name = "DP7 UAJ Mic Capture",
1262 			.channels_min = 1,
1263 			.channels_max = 2,
1264 			.rates = TAC5XX2_DEVICE_RATES,
1265 			.formats = TAC5XX2_DEVICE_FORMATS,
1266 		},
1267 		.ops = &tac_dai_ops,
1268 	},
1269 };
1270 
1271 static struct snd_soc_dai_driver tac5682_dai_driver[] = {
1272 	{
1273 		.name = "tac5xx2-aif1",
1274 		.id = TAC5XX2_SPK,
1275 		.playback = {
1276 			.stream_name = "DP1 Speaker Playback",
1277 			.channels_min = 1,
1278 			.channels_max = 2,
1279 			.rates = TAC5XX2_DEVICE_RATES,
1280 			.formats = TAC5XX2_DEVICE_FORMATS,
1281 		},
1282 		.capture = {
1283 			.stream_name = "DP2 Echo Reference Capture",
1284 			.channels_min = 1,
1285 			.channels_max = 4,
1286 			.rates = TAC5XX2_DEVICE_RATES,
1287 			.formats = TAC5XX2_DEVICE_FORMATS,
1288 		},
1289 		.ops = &tac_dai_ops,
1290 		.symmetric_rate = 1,
1291 	},
1292 	{
1293 		.name = "tac5xx2-aif2",
1294 		.id = TAC5XX2_DMIC,
1295 		.capture = {
1296 			.stream_name = "DP3 Mic Capture",
1297 			.channels_min = 1,
1298 			.channels_max = 4,
1299 			.rates = TAC5XX2_DEVICE_RATES,
1300 			.formats = TAC5XX2_DEVICE_FORMATS,
1301 		},
1302 		.ops = &tac_dai_ops,
1303 	},
1304 	{
1305 		.name = "tac5xx2-aif3",
1306 		.id = TAC5XX2_UAJ,
1307 		.playback = {
1308 			.stream_name = "DP4 UAJ Speaker Playback",
1309 			.channels_min = 1,
1310 			.channels_max = 2,
1311 			.rates = TAC5XX2_DEVICE_RATES,
1312 			.formats = TAC5XX2_DEVICE_FORMATS,
1313 		},
1314 		.capture = {
1315 			.stream_name = "DP7 UAJ Mic Capture",
1316 			.channels_min = 1,
1317 			.channels_max = 2,
1318 			.rates = TAC5XX2_DEVICE_RATES,
1319 			.formats = TAC5XX2_DEVICE_FORMATS,
1320 		},
1321 		.ops = &tac_dai_ops,
1322 	},
1323 };
1324 
1325 static struct snd_soc_dai_driver tas2883_dai_driver[] = {
1326 	{
1327 		.name = "tac5xx2-aif1",
1328 		.id = TAC5XX2_SPK,
1329 		.playback = {
1330 			.stream_name = "DP1 Speaker Playback",
1331 			.channels_min = 1,
1332 			.channels_max = 2,
1333 			.rates = TAC5XX2_DEVICE_RATES,
1334 			.formats = TAC5XX2_DEVICE_FORMATS,
1335 		},
1336 		.ops = &tac_dai_ops,
1337 		.symmetric_rate = 1,
1338 	},
1339 	{
1340 		.name = "tac5xx2-aif2",
1341 		.id = TAC5XX2_DMIC,
1342 		.capture = {
1343 			.stream_name = "DP3 Mic Capture",
1344 			.channels_min = 1,
1345 			.channels_max = 4,
1346 			.rates = TAC5XX2_DEVICE_RATES,
1347 			.formats = TAC5XX2_DEVICE_FORMATS,
1348 		},
1349 		.ops = &tac_dai_ops,
1350 	},
1351 };
1352 
1353 static s32 tac_component_probe(struct snd_soc_component *component)
1354 {
1355 	struct tac5xx2_prv *tac_dev = snd_soc_component_get_drvdata(component);
1356 	int ret;
1357 
1358 	ret = pm_runtime_resume(component->dev);
1359 	if (ret < 0 && ret != -EACCES)
1360 		return ret;
1361 
1362 	if (!tac_has_uaj_support(tac_dev))
1363 		goto done_comp_probe;
1364 
1365 	ret = snd_soc_dapm_new_controls(snd_soc_component_to_dapm(component),
1366 					tac_uaj_widgets,
1367 					ARRAY_SIZE(tac_uaj_widgets));
1368 	if (ret) {
1369 		dev_err(component->dev, "Failed to add UAJ widgets: %d\n", ret);
1370 		return ret;
1371 	}
1372 
1373 	ret = snd_soc_dapm_add_routes(snd_soc_component_to_dapm(component),
1374 				      tac_uaj_routes, ARRAY_SIZE(tac_uaj_routes));
1375 	if (ret) {
1376 		dev_err(component->dev, "Failed to add UAJ routes: %d\n", ret);
1377 		return ret;
1378 	}
1379 
1380 	ret = snd_soc_add_component_controls(component, tac_uaj_controls,
1381 					     ARRAY_SIZE(tac_uaj_controls));
1382 	if (ret) {
1383 		dev_err(component->dev, "Failed to add UAJ controls: %d\n", ret);
1384 			return ret;
1385 	}
1386 
1387 done_comp_probe:
1388 	tac_dev->component = component;
1389 	return 0;
1390 }
1391 
1392 static void tac_component_remove(struct snd_soc_component *codec)
1393 {
1394 	struct tac5xx2_prv *tac_dev = snd_soc_component_get_drvdata(codec);
1395 
1396 	tac_dev->component = NULL;
1397 }
1398 
1399 static const struct snd_soc_component_driver soc_codec_driver_tacdevice = {
1400 	.probe = tac_component_probe,
1401 	.remove = tac_component_remove,
1402 	.controls = tac5xx2_snd_controls,
1403 	.num_controls = ARRAY_SIZE(tac5xx2_snd_controls),
1404 	.dapm_widgets = tac5xx2_common_widgets,
1405 	.num_dapm_widgets = ARRAY_SIZE(tac5xx2_common_widgets),
1406 	.dapm_routes = tac5xx2_common_routes,
1407 	.num_dapm_routes = ARRAY_SIZE(tac5xx2_common_routes),
1408 	.idle_bias_on = 0,
1409 	.endianness = 1,
1410 };
1411 
1412 static s32 tac_init(struct tac5xx2_prv *tac_dev)
1413 {
1414 	struct snd_soc_component_driver *component_driver;
1415 	struct snd_soc_dai_driver *dai_drv;
1416 	int num_dais;
1417 	s32 ret;
1418 
1419 	dev_set_drvdata(tac_dev->dev, tac_dev);
1420 
1421 	switch (tac_dev->part_id) {
1422 	case 0x5572:
1423 		dai_drv = tac5572_dai_driver;
1424 		num_dais = ARRAY_SIZE(tac5572_dai_driver);
1425 		break;
1426 	case 0x5672:
1427 		dai_drv = tac5672_dai_driver;
1428 		num_dais = ARRAY_SIZE(tac5672_dai_driver);
1429 		break;
1430 	case 0x5682:
1431 		dai_drv = tac5682_dai_driver;
1432 		num_dais = ARRAY_SIZE(tac5682_dai_driver);
1433 		break;
1434 	case 0x2883:
1435 		dai_drv = tas2883_dai_driver;
1436 		num_dais = ARRAY_SIZE(tas2883_dai_driver);
1437 		break;
1438 	default:
1439 		dev_err(tac_dev->dev, "Unsupported device: 0x%x\n",
1440 			tac_dev->part_id);
1441 		return -EINVAL;
1442 	}
1443 
1444 	component_driver = devm_kzalloc_obj(tac_dev->dev, *component_driver);
1445 	if (!component_driver)
1446 		return -ENOMEM;
1447 
1448 	memcpy(component_driver, &soc_codec_driver_tacdevice, sizeof(*component_driver));
1449 	if (tac_has_uaj_support(tac_dev))
1450 		component_driver->set_jack = tac5xx2_set_jack;
1451 
1452 	ret = devm_snd_soc_register_component(tac_dev->dev, component_driver,
1453 					      dai_drv, num_dais);
1454 	if (ret) {
1455 		dev_err(tac_dev->dev, "%s: codec register error:%d.\n",
1456 			__func__, ret);
1457 		return ret;
1458 	}
1459 
1460 	return 0;
1461 }
1462 
1463 static s32 tac5xx2_sdca_dev_suspend(struct device *dev)
1464 {
1465 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(dev);
1466 
1467 	if (!tac_dev->hw_init)
1468 		return 0;
1469 
1470 	regcache_cache_only(tac_dev->regmap, true);
1471 	return 0;
1472 }
1473 
1474 static s32 tac5xx2_sdca_dev_system_suspend(struct device *dev)
1475 {
1476 	return tac5xx2_sdca_dev_suspend(dev);
1477 }
1478 
1479 static s32 tac5xx2_sdca_dev_resume(struct device *dev)
1480 {
1481 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(dev);
1482 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
1483 	int ret;
1484 
1485 	if (!tac_dev->first_hw_init_done) {
1486 		dev_dbg(dev, "Device not initialized yet, skipping resume sync\n");
1487 		return 0;
1488 	}
1489 
1490 	ret = sdw_slave_wait_for_init(slave, TAC5XX2_PROBE_TIMEOUT_MS);
1491 	if (ret) {
1492 		sdw_show_ping_status(slave->bus, true);
1493 		return ret;
1494 	}
1495 
1496 	regcache_cache_only(tac_dev->regmap, false);
1497 	regcache_mark_dirty(tac_dev->regmap);
1498 	ret = regcache_sync(tac_dev->regmap);
1499 	if (ret < 0)
1500 		dev_warn(dev, "Failed to sync regcache: %d\n", ret);
1501 
1502 	/* Detect and set jack type for UAJ path before playback.
1503 	 * This is required as jack detection does not trigger interrupt
1504 	 * when device is in runtime_pm suspend with bus in clock stop mode.
1505 	 */
1506 	if (tac_has_uaj_support(tac_dev))
1507 		tac5xx2_sdca_headset_detect(tac_dev);
1508 
1509 	return 0;
1510 }
1511 
1512 static const struct dev_pm_ops tac5xx2_sdca_pm = {
1513 	SYSTEM_SLEEP_PM_OPS(tac5xx2_sdca_dev_system_suspend, tac5xx2_sdca_dev_resume)
1514 	RUNTIME_PM_OPS(tac5xx2_sdca_dev_suspend, tac5xx2_sdca_dev_resume, NULL)
1515 };
1516 
1517 static s32 tac_fw_read_hdr(const u8 *data, struct tac_fw_hdr *hdr)
1518 {
1519 	hdr->size = get_unaligned_le32(data);
1520 	hdr->version_offset = get_unaligned_le32(data + 4);
1521 	hdr->plt_id = get_unaligned_le32(data + 8);
1522 	hdr->ppc3_ver = get_unaligned_le32(data + 12);
1523 	memcpy(hdr->ddc_name, data + 16, 64);
1524 	hdr->ddc_name[63] = 0;
1525 	hdr->timestamp = get_unaligned_le64(data + 80);
1526 
1527 	return TAC_FW_HDR_SIZE;
1528 }
1529 
1530 static s32 tac_fw_get_next_file(const u8 *data, size_t data_size, struct tac_fw_file *file)
1531 {
1532 	u32 file_length;
1533 
1534 	/* Validate file header size */
1535 	if (data_size < TAC_FW_FILE_HDR)
1536 		return -EINVAL;
1537 
1538 	file->vendor_id = get_unaligned_le32(&data[0]);
1539 	file->file_id = get_unaligned_le32(&data[4]);
1540 	file->version = get_unaligned_le32(&data[8]);
1541 	file->length = get_unaligned_le32(&data[12]);
1542 	file->dest_addr = get_unaligned_le32(&data[16]);
1543 	file_length = file->length;
1544 
1545 	/* Validate file payload exists */
1546 	if (data_size < TAC_FW_FILE_HDR + file_length)
1547 		return -EINVAL;
1548 
1549 	file->fw_data = (u8 *)&data[20];
1550 
1551 	return file_length + sizeof(u32) * 5;
1552 }
1553 
1554 static void tac5xx2_fw_ready(const struct firmware *fmw, void *context)
1555 {
1556 	struct tac5xx2_prv *tac_dev = context;
1557 	struct tac_fw_file *files;
1558 	u32 fw_hdr_size;
1559 	u32 num_files = 0;
1560 	struct tac_fw_hdr hdr;
1561 	struct tm tm_time;
1562 	size_t img_sz;
1563 	u32 offset;
1564 	s32 ret = 0;
1565 	u8 *buf;
1566 
1567 	if (!fmw || !fmw->data || fmw->size == 0 || fmw->size < TAC_FW_HDR_SIZE + TAC_FW_FILE_HDR) {
1568 		dev_err(tac_dev->dev, "fw file: %s is empty or invalid\n",
1569 			tac_dev->fw_binaryname);
1570 		goto out;
1571 	}
1572 
1573 	/* Verify firmware size from header */
1574 	fw_hdr_size = get_unaligned_le32(fmw->data);
1575 	if (fw_hdr_size != fmw->size) {
1576 		dev_err(tac_dev->dev, "firmware size mismatch: hdr=%u, actual=%zu\n",
1577 			fw_hdr_size, fmw->size);
1578 		goto out;
1579 	}
1580 
1581 	files = devm_kzalloc(tac_dev->dev, sizeof(*files) * TAC_MAX_FW_CHUNKS, GFP_KERNEL);
1582 	buf = devm_kmemdup(tac_dev->dev, fmw->data, fmw->size, GFP_KERNEL);
1583 	if (!files || !buf)
1584 		goto out;
1585 
1586 	/* validate the cache the firmware */
1587 	img_sz = fmw->size;
1588 	offset = tac_fw_read_hdr(buf, &hdr);
1589 	while (offset < img_sz && num_files < TAC_MAX_FW_CHUNKS) {
1590 		u32 file_length;
1591 
1592 		if (offset + TAC_FW_FILE_HDR > img_sz) {
1593 			dev_warn(tac_dev->dev, "Incomplete block header at offset %d\n",
1594 				 offset);
1595 			goto out;
1596 		}
1597 		/* Validate that the file payload doesn't exceed buffer */
1598 		file_length = get_unaligned_le32(&buf[offset + 12]);
1599 		/* Check for integer overflow and buffer bounds */
1600 		if (file_length > img_sz || offset > img_sz - TAC_FW_FILE_HDR ||
1601 		    file_length > img_sz - offset - TAC_FW_FILE_HDR) {
1602 			dev_warn(tac_dev->dev, "File at offset %d exceeds buffer: length=%u, available=%zu\n",
1603 				 offset, file_length, img_sz - offset - TAC_FW_FILE_HDR);
1604 			goto out;
1605 		}
1606 		ret = tac_fw_get_next_file(&buf[offset], img_sz - offset, &files[num_files]);
1607 		if (ret < 0) {
1608 			dev_err(tac_dev->dev, "Failed to parse file at offset %d\n", offset);
1609 			goto out;
1610 		}
1611 		offset += ret;
1612 		num_files++;
1613 	}
1614 
1615 	if (num_files == 0) {
1616 		dev_err(tac_dev->dev, "firmware with no files\n");
1617 		goto out;
1618 	}
1619 
1620 	/* cache ready to use validated firmware */
1621 	tac_dev->fw_file_cnt = num_files;
1622 	tac_dev->fw_files = files;
1623 
1624 	time64_to_tm(hdr.timestamp, 0, &tm_time);
1625 	dev_dbg(tac_dev->dev, "fw file: %s, num_files=%u, ts:%04ld-%02d-%02d %02d:%02d\n",
1626 		tac_dev->fw_binaryname, tac_dev->fw_file_cnt,
1627 		tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
1628 		tm_time.tm_hour, tm_time.tm_min);
1629 	dev_dbg(tac_dev->dev, "fw file: DDC Name: %s\n", hdr.ddc_name);
1630 	dev_dbg(tac_dev->dev, "fw file: PPC3 Version: 3.%ld.%ld.%ld\n",
1631 		FIELD_GET(GENMASK(31, 24), hdr.ppc3_ver),
1632 		FIELD_GET(GENMASK(23, 16), hdr.ppc3_ver),
1633 		FIELD_GET(GENMASK(15, 8), hdr.ppc3_ver) & 0x3f);
1634 
1635 out:
1636 	complete_all(&tac_dev->fw_caching_complete);
1637 	if (fmw)
1638 		release_firmware(fmw);
1639 }
1640 
1641 static int tac_load_and_cache_firmware_async(struct tac5xx2_prv *tac_dev)
1642 {
1643 	tac_dev->fw_file_cnt = 0;
1644 	tac_dev->fw_files = NULL; /* ready to download files */
1645 
1646 	return request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
1647 				       tac_dev->fw_binaryname, tac_dev->dev,
1648 				       GFP_KERNEL, tac_dev, tac5xx2_fw_ready);
1649 }
1650 
1651 static int tac_download(struct tac5xx2_prv *tac_dev)
1652 {
1653 	struct tac_fw_file *files = tac_dev->fw_files;
1654 	u32 num_files = tac_dev->fw_file_cnt;
1655 	u32 i;
1656 	int ret = 0;
1657 
1658 	for (i = 0; i < num_files; i++) {
1659 		ret = sdw_nwrite_no_pm(tac_dev->sdw_peripheral, files[i].dest_addr,
1660 				       files[i].length, files[i].fw_data);
1661 		if (ret < 0) {
1662 			dev_dbg(tac_dev->dev,
1663 				"FW write failed at addr 0x%x: %d\n",
1664 				files[i].dest_addr, ret);
1665 			return ret;
1666 		}
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 /*
1673  * tac5xx2 uses custom firmware binary fw.
1674  * This is not using UMP File Download.
1675  */
1676 static s32 tac_download_fw_to_hw(struct tac5xx2_prv *tac_dev)
1677 {
1678 	int ret;
1679 
1680 	ret = tac_download(tac_dev);
1681 	if (ret < 0) {
1682 		dev_err(tac_dev->dev, "Firmware download failed: %d\n", ret);
1683 		return ret;
1684 	}
1685 
1686 	dev_dbg(tac_dev->dev, "Firmware download complete: %d chunks\n",
1687 		tac_dev->fw_file_cnt);
1688 	tac_dev->fw_dl_success = true;
1689 
1690 	return 0;
1691 }
1692 
1693 static struct pci_dev *tac_get_pci_dev(struct sdw_slave *peripheral)
1694 {
1695 	struct device *dev = &peripheral->dev;
1696 
1697 	for (; dev; dev = dev->parent) {
1698 		if (dev_is_pci(dev))
1699 			return to_pci_dev(dev);
1700 	}
1701 
1702 	return NULL;
1703 }
1704 
1705 static void tac_generate_fw_name(struct sdw_slave *slave, char *name, size_t size)
1706 {
1707 	struct sdw_bus *bus = slave->bus;
1708 	u16 part_id = slave->id.part_id;
1709 	u8 unique_id = slave->id.unique_id;
1710 	struct pci_dev *pci = tac_get_pci_dev(slave);
1711 
1712 	if (pci)
1713 		scnprintf(name, size, "%04X-%04X-%1X-%1X.bin", part_id,
1714 			  pci->subsystem_device, bus->link_id, unique_id);
1715 	else
1716 		/* Default firmware name based on part ID */
1717 		scnprintf(name, size, "%s%04x-%1X-%1X.bin",
1718 			  part_id == 0x2883 ? "tas" : "tac",
1719 			  part_id, bus->link_id, unique_id);
1720 }
1721 
1722 static int tac_io_init(struct device *dev, struct sdw_slave *slave, bool first)
1723 {
1724 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(dev);
1725 	u64 time;
1726 	int ret;
1727 
1728 	if (tac_dev->hw_init) {
1729 		dev_dbg(dev, "early return hw_init already done..");
1730 		return 0;
1731 	}
1732 
1733 	time = wait_for_completion_timeout(&tac_dev->fw_caching_complete,
1734 					   msecs_to_jiffies(TAC5XX2_FW_CACHE_TIMEOUT_MS));
1735 	if (!time) {
1736 		ret = -ETIMEDOUT;
1737 		dev_warn(tac_dev->dev, "%s: fw caching timeout\n", __func__);
1738 		goto io_init_err;
1739 	}
1740 
1741 	if (!tac_dev->rev_id) {
1742 		ret = regmap_read(tac_dev->regmap, TAC_REV_ID, &tac_dev->rev_id);
1743 		if (ret) {
1744 			dev_err(tac_dev->dev, "failed to rev id, err=%d\n", ret);
1745 			goto io_init_err;
1746 		}
1747 		dev_dbg(tac_dev->dev, "detected rev_id 0x%x", tac_dev->rev_id);
1748 	}
1749 
1750 	if (tac_dev->fw_files && tac_dev->fw_file_cnt > 0) {
1751 		ret = tac_download_fw_to_hw(tac_dev);
1752 		if (ret) {
1753 			dev_err(tac_dev->dev, "FW download failed, fw: %d\n", ret);
1754 			goto io_init_err;
1755 		}
1756 	}
1757 
1758 	if (tac_dev->sa_func_data) {
1759 		ret = sdca_regmap_write_init(dev, tac_dev->regmap,
1760 					     tac_dev->sa_func_data);
1761 		if (ret) {
1762 			dev_err(dev, "smartamp init table update failed\n");
1763 			goto io_init_err;
1764 		}
1765 		dev_dbg(dev, "smartamp init done\n");
1766 
1767 		if (first) {
1768 			ret = regmap_multi_reg_write(tac_dev->regmap, tac_spk_seq,
1769 						     ARRAY_SIZE(tac_spk_seq));
1770 			if (ret) {
1771 				dev_err(dev, "init writes failed, err=%d", ret);
1772 				goto io_init_err;
1773 			}
1774 		}
1775 	}
1776 
1777 	if (tac_dev->sm_func_data) {
1778 		ret = sdca_regmap_write_init(dev, tac_dev->regmap,
1779 					     tac_dev->sm_func_data);
1780 		if (ret) {
1781 			dev_err(dev, "smartmic init table update failed\n");
1782 			goto io_init_err;
1783 		}
1784 		dev_dbg(dev, "smartmic init done\n");
1785 	}
1786 
1787 	if (tac_dev->uaj_func_data) {
1788 		ret = sdca_regmap_write_init(dev, tac_dev->regmap,
1789 					     tac_dev->uaj_func_data);
1790 		if (ret) {
1791 			dev_err(dev, "uaj init table update failed\n");
1792 			goto io_init_err;
1793 		}
1794 		dev_dbg(dev, "uaj init done\n");
1795 
1796 		if (first) {
1797 			if (tac_dev->hs_jack) {
1798 				ret = tac5xx2_jack_init(tac_dev);
1799 				if (ret) {
1800 					dev_err(tac_dev->dev, "jack init failed");
1801 					goto io_init_err;
1802 				}
1803 			}
1804 		}
1805 	}
1806 
1807 	if (tac_dev->hid_func_data) {
1808 		ret = sdca_regmap_write_init(dev, tac_dev->regmap,
1809 					     tac_dev->hid_func_data);
1810 		if (ret) {
1811 			dev_err(dev, "hid init table update failed\n");
1812 			goto io_init_err;
1813 		}
1814 		dev_dbg(dev, "hid init done\n");
1815 	}
1816 
1817 	tac_dev->hw_init = true;
1818 
1819 	return 0;
1820 
1821 io_init_err:
1822 	dev_err(dev, "init writes failed, err=%d", ret);
1823 	return ret;
1824 }
1825 
1826 static int tac_update_status(struct sdw_slave *slave,
1827 			     enum sdw_slave_status status)
1828 {
1829 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(&slave->dev);
1830 	struct device *dev = &slave->dev;
1831 	bool first = false;
1832 	int ret;
1833 
1834 	tac_dev->status = status;
1835 	if (status == SDW_SLAVE_UNATTACHED) {
1836 		tac_dev->hw_init = false;
1837 		tac_dev->fw_dl_success = false;
1838 	}
1839 
1840 	if (tac_dev->hw_init || tac_dev->status != SDW_SLAVE_ATTACHED) {
1841 		dev_dbg(dev, "%s: early return, hw_init=%d, status=%d",
1842 			__func__, tac_dev->hw_init, tac_dev->status);
1843 		return 0;
1844 	}
1845 
1846 	if (!tac_dev->first_hw_init_done) {
1847 		pm_runtime_set_active(tac_dev->dev);
1848 		tac_dev->first_hw_init_done = true;
1849 		first = true;
1850 	}
1851 
1852 	pm_runtime_get_noresume(tac_dev->dev);
1853 
1854 	regcache_mark_dirty(tac_dev->regmap);
1855 	regcache_cache_only(tac_dev->regmap, false);
1856 	ret = tac_io_init(&slave->dev, slave, first);
1857 	if (ret) {
1858 		dev_err(dev, "Device initialization failed: %d\n", ret);
1859 		goto err_out;
1860 	}
1861 
1862 	ret = regcache_sync(tac_dev->regmap);
1863 	if (ret)
1864 		dev_warn(dev, "Failed to sync regcache after init: %d\n", ret);
1865 
1866 err_out:
1867 	pm_runtime_mark_last_busy(tac_dev->dev);
1868 	pm_runtime_put_autosuspend(tac_dev->dev);
1869 
1870 	return ret;
1871 }
1872 
1873 static int tac5xx2_sdw_read_prop(struct sdw_slave *peripheral)
1874 {
1875 	struct device *dev = &peripheral->dev;
1876 	int ret;
1877 
1878 	ret = sdw_slave_read_prop(peripheral);
1879 	if (ret) {
1880 		dev_err(dev, "sdw_slave_read_prop failed: %d", ret);
1881 		return ret;
1882 	}
1883 
1884 	return 0;
1885 }
1886 
1887 static int tac_port_prep(struct sdw_slave *slave, struct sdw_prepare_ch *prep_ch,
1888 			 enum sdw_port_prep_ops pre_ops)
1889 {
1890 	struct device *dev = &slave->dev;
1891 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(dev);
1892 	unsigned int val;
1893 	int ret;
1894 
1895 	if (pre_ops != SDW_OPS_PORT_POST_PREP)
1896 		return 0;
1897 
1898 	if (!tac_dev->fw_dl_success)
1899 		return 0;
1900 
1901 	ret = regmap_read(tac_dev->regmap, TAC_DSP_ALGO_STATUS, &val);
1902 	if (ret) {
1903 		dev_err(dev, "Failed to read algo status: %d\n", ret);
1904 		return ret;
1905 	}
1906 
1907 	if (val != TAC_DSP_ALGO_STATUS_RUNNING) {
1908 		dev_dbg(dev, "Algo not running (0x%02x), re-enabling\n", val);
1909 		ret = regmap_write(tac_dev->regmap, TAC_DSP_ALGO_STATUS,
1910 				   TAC_DSP_ALGO_STATUS_RUNNING);
1911 		if (ret) {
1912 			dev_err(dev, "Failed to re-enable algo: %d\n", ret);
1913 			return ret;
1914 		}
1915 	}
1916 
1917 	return 0;
1918 }
1919 
1920 static const struct sdw_slave_ops tac_sdw_ops = {
1921 	.read_prop = tac5xx2_sdw_read_prop,
1922 	.update_status = tac_update_status,
1923 	.interrupt_callback = tac_interrupt_callback,
1924 	.port_prep = tac_port_prep,
1925 };
1926 
1927 static s32 tac_sdw_probe(struct sdw_slave *peripheral,
1928 			 const struct sdw_device_id *id)
1929 {
1930 	struct sdca_function_data *function_data = NULL;
1931 	struct device *dev = &peripheral->dev;
1932 	struct tac5xx2_prv *tac_dev;
1933 	struct regmap *regmap;
1934 	int ret, i;
1935 
1936 	tac_dev = devm_kzalloc_obj(dev, *tac_dev);
1937 	if (!tac_dev)
1938 		return dev_err_probe(dev, -ENOMEM,
1939 				     "Failed devm_kzalloc");
1940 
1941 	if (peripheral->sdca_data.num_functions > 0) {
1942 		dev_dbg(dev, "SDCA functions found: %d",
1943 			peripheral->sdca_data.num_functions);
1944 
1945 		for (i = 0; i < peripheral->sdca_data.num_functions; i++) {
1946 			struct sdca_function_data **func_ptr;
1947 			const char *func_name;
1948 
1949 			switch (peripheral->sdca_data.function[i].type) {
1950 			case SDCA_FUNCTION_TYPE_SMART_AMP:
1951 				func_ptr = &tac_dev->sa_func_data;
1952 				func_name = "smartamp";
1953 				break;
1954 			case SDCA_FUNCTION_TYPE_SMART_MIC:
1955 				func_ptr = &tac_dev->sm_func_data;
1956 				func_name = "smartmic";
1957 				break;
1958 			case SDCA_FUNCTION_TYPE_UAJ:
1959 				func_ptr = &tac_dev->uaj_func_data;
1960 				func_name = "uaj";
1961 				break;
1962 			case SDCA_FUNCTION_TYPE_HID:
1963 				func_ptr = &tac_dev->hid_func_data;
1964 				func_name = "hid";
1965 				break;
1966 			default:
1967 				continue;
1968 			}
1969 
1970 			function_data = devm_kzalloc_obj(dev, *function_data);
1971 			if (!function_data)
1972 				return dev_err_probe(dev, -ENOMEM,
1973 						     "failed to allocate %s function data",
1974 						     func_name);
1975 			function_data->desc = &peripheral->sdca_data.function[i];
1976 			ret = sdca_parse_function(dev, function_data);
1977 			if (!ret)
1978 				*func_ptr = function_data;
1979 			else
1980 				devm_kfree(dev, function_data);
1981 		}
1982 	}
1983 
1984 	dev_dbg(dev, "SDCA functions enabled: SA=%s SM=%s UAJ=%s HID=%s",
1985 		tac_dev->sa_func_data ? "yes" : "no",
1986 		tac_dev->sm_func_data ? "yes" : "no",
1987 		tac_dev->uaj_func_data ? "yes" : "no",
1988 		tac_dev->hid_func_data ? "yes" : "no");
1989 
1990 	tac_dev->dev = dev;
1991 	tac_dev->sdw_peripheral = peripheral;
1992 	tac_dev->hw_init = false;
1993 	tac_dev->first_hw_init_done = false;
1994 	tac_dev->part_id = id->part_id;
1995 	tac_dev->rev_id = 0x0;
1996 	dev_set_drvdata(dev, tac_dev);
1997 
1998 	regmap = devm_regmap_init_sdw_mbq_cfg(&peripheral->dev, peripheral,
1999 					      &tac_regmap, &tac_mbq_cfg);
2000 	if (IS_ERR(regmap))
2001 		return dev_err_probe(dev, PTR_ERR(regmap),
2002 				     "Failed devm_regmap_init_sdw\n");
2003 
2004 	regcache_cache_only(regmap, true);
2005 	tac_dev->regmap = regmap;
2006 	tac_dev->jack_type = 0;
2007 	init_completion(&tac_dev->fw_caching_complete);
2008 
2009 	tac_generate_fw_name(peripheral, tac_dev->fw_binaryname,
2010 			     sizeof(tac_dev->fw_binaryname));
2011 
2012 	ret = tac_load_and_cache_firmware_async(tac_dev);
2013 	if (ret) {
2014 		complete_all(&tac_dev->fw_caching_complete);
2015 		dev_dbg(dev, "failed to load fw: %d, use rom mode\n", ret);
2016 	}
2017 
2018 	ret = tac_init(tac_dev);
2019 	if (ret)
2020 		return dev_err_probe(dev, ret,
2021 				     "failed to initialize tac device\n");
2022 
2023 	/* set autosuspend parameters */
2024 	pm_runtime_set_autosuspend_delay(dev, 3000);
2025 	pm_runtime_use_autosuspend(dev);
2026 
2027 	/* make sure the device does not suspend immediately */
2028 	pm_runtime_mark_last_busy(dev);
2029 
2030 	pm_runtime_enable(dev);
2031 	/* the device is still not in active */
2032 
2033 	return 0;
2034 }
2035 
2036 static void tac_sdw_remove(struct sdw_slave *peripheral)
2037 {
2038 	struct tac5xx2_prv *tac_dev = dev_get_drvdata(&peripheral->dev);
2039 
2040 	pm_runtime_disable(tac_dev->dev);
2041 
2042 	dev_set_drvdata(&peripheral->dev, NULL);
2043 }
2044 
2045 static const struct sdw_device_id tac_sdw_id[] = {
2046 	SDW_SLAVE_ENTRY(0x0102, 0x5572, 0),
2047 	SDW_SLAVE_ENTRY(0x0102, 0x5672, 0),
2048 	SDW_SLAVE_ENTRY(0x0102, 0x5682, 0),
2049 	SDW_SLAVE_ENTRY(0x0102, 0x2883, 0),
2050 	{},
2051 };
2052 MODULE_DEVICE_TABLE(sdw, tac_sdw_id);
2053 
2054 static struct sdw_driver tac_sdw_driver = {
2055 	.driver = {
2056 		.name = "slave-tac5xx2",
2057 		.pm = pm_ptr(&tac5xx2_sdca_pm),
2058 	},
2059 	.probe = tac_sdw_probe,
2060 	.remove = tac_sdw_remove,
2061 	.ops = &tac_sdw_ops,
2062 	.id_table = tac_sdw_id,
2063 };
2064 module_sdw_driver(tac_sdw_driver);
2065 
2066 MODULE_IMPORT_NS("SND_SOC_SDCA");
2067 MODULE_AUTHOR("Texas Instruments Inc.");
2068 MODULE_DESCRIPTION("ASoC TAC5XX2 SoundWire Driver");
2069 MODULE_LICENSE("GPL");
2070