1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt1320-sdw.c -- rt1320 SDCA ALSA SoC amplifier audio driver
5 // Copyright(c) 2024 Realtek Semiconductor Corp.
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/dmi.h>
15 #include <linux/firmware.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc-dapm.h>
20 #include <sound/initval.h>
21 #include <sound/tlv.h>
22 #include <sound/sdw.h>
23 #include "rt1320-sdw.h"
24 #include "rt-sdw-common.h"
27 * The 'blind writes' is an SDCA term to deal with platform-specific initialization.
28 * It might include vendor-specific or SDCA control registers.
30 static const struct reg_sequence rt1320_blind_write
[] = {
100 { 0x1000db02, 0x11 },
101 { 0x1000db03, 0x00 },
102 { 0x1000db04, 0x00 },
103 { 0x1000db05, 0x82 },
104 { 0x1000db06, 0x04 },
105 { 0x1000db07, 0xf1 },
106 { 0x1000db08, 0x00 },
107 { 0x1000db09, 0x00 },
108 { 0x1000db0a, 0x40 },
109 { 0x0000d540, 0x01 },
114 static const struct reg_sequence rt1320_vc_blind_write
[] = {
193 { 0x3fc2bf83, 0x00 },
194 { 0x3fc2bf82, 0x00 },
195 { 0x3fc2bf81, 0x00 },
196 { 0x3fc2bf80, 0x00 },
197 { 0x3fc2bfc7, 0x00 },
198 { 0x3fc2bfc6, 0x00 },
199 { 0x3fc2bfc5, 0x00 },
200 { 0x3fc2bfc4, 0x00 },
201 { 0x3fc2bfc3, 0x00 },
202 { 0x3fc2bfc2, 0x00 },
203 { 0x3fc2bfc1, 0x00 },
204 { 0x3fc2bfc0, 0x03 },
205 { 0x0000d486, 0x43 },
206 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0x00 },
207 { 0x1000db00, 0x04 },
208 { 0x1000db01, 0x00 },
209 { 0x1000db02, 0x11 },
210 { 0x1000db03, 0x00 },
211 { 0x1000db04, 0x00 },
212 { 0x1000db05, 0x82 },
213 { 0x1000db06, 0x04 },
214 { 0x1000db07, 0xf1 },
215 { 0x1000db08, 0x00 },
216 { 0x1000db09, 0x00 },
217 { 0x1000db0a, 0x40 },
218 { 0x1000db0b, 0x02 },
219 { 0x1000db0c, 0xf2 },
220 { 0x1000db0d, 0x00 },
221 { 0x1000db0e, 0x00 },
222 { 0x1000db0f, 0xe0 },
223 { 0x1000db10, 0x00 },
224 { 0x1000db11, 0x10 },
225 { 0x1000db12, 0x00 },
226 { 0x1000db13, 0x00 },
227 { 0x1000db14, 0x45 },
228 { 0x0000d540, 0x01 },
229 { 0x0000c081, 0xfc },
230 { 0x0000f01e, 0x80 },
240 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0x03 },
243 static const struct reg_default rt1320_reg_defaults
[] = {
244 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0x03 },
245 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
), 0x01 },
246 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
), 0x01 },
247 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
), 0x01 },
248 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
), 0x01 },
249 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS113
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0), 0x09 },
250 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS14
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0), 0x0b },
251 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0), 0x03 },
252 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
), 0x01 },
253 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
), 0x01 },
254 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE27
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0x03 },
255 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0x03 },
256 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PPU21
, RT1320_SDCA_CTL_POSTURE_NUMBER
, 0), 0x00 },
257 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_CS21
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0), 0x09 },
258 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0), 0x03 },
261 static const struct reg_default rt1320_mbq_defaults
[] = {
262 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
), 0x0000 },
263 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
), 0x0000 },
264 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
), 0x0000 },
265 { SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
), 0x0000 },
266 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
), 0x0000 },
267 { SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
), 0x0000 },
270 static bool rt1320_readable_register(struct device
*dev
, unsigned int reg
)
273 case 0xc000 ... 0xc086:
274 case 0xc400 ... 0xc409:
275 case 0xc480 ... 0xc48f:
276 case 0xc4c0 ... 0xc4c4:
277 case 0xc4e0 ... 0xc4e7:
279 case 0xc560 ... 0xc56b:
281 case 0xc580 ... 0xc59a:
282 case 0xc5b0 ... 0xc60f:
283 case 0xc640 ... 0xc64f:
285 case 0xc680 ... 0xc683:
286 case 0xc700 ... 0xc76f:
287 case 0xc800 ... 0xc801:
289 case 0xc900 ... 0xc901:
290 case 0xc920 ... 0xc921:
291 case 0xca00 ... 0xca07:
292 case 0xca20 ... 0xca27:
293 case 0xca40 ... 0xca4b:
294 case 0xca60 ... 0xca68:
295 case 0xca80 ... 0xca88:
296 case 0xcb00 ... 0xcb0c:
297 case 0xcc00 ... 0xcc12:
298 case 0xcc80 ... 0xcc81:
300 case 0xcd80 ... 0xcd82:
301 case 0xce00 ... 0xce4d:
302 case 0xcf00 ... 0xcf25:
303 case 0xd000 ... 0xd0ff:
304 case 0xd100 ... 0xd1ff:
305 case 0xd200 ... 0xd2ff:
306 case 0xd300 ... 0xd3ff:
307 case 0xd400 ... 0xd403:
308 case 0xd410 ... 0xd417:
309 case 0xd470 ... 0xd497:
310 case 0xd4dc ... 0xd50f:
311 case 0xd520 ... 0xd543:
312 case 0xd560 ... 0xd5ef:
313 case 0xd600 ... 0xd663:
314 case 0xda00 ... 0xda6e:
315 case 0xda80 ... 0xda9e:
316 case 0xdb00 ... 0xdb7f:
318 case 0xdc20 ... 0xdc21:
319 case 0xdd00 ... 0xdd17:
320 case 0xde00 ... 0xde09:
321 case 0xdf00 ... 0xdf1b:
322 case 0xe000 ... 0xe847:
324 case 0xf717 ... 0xf719:
325 case 0xf720 ... 0xf723:
326 case 0x1000cd91 ... 0x1000cd96:
329 case 0x3fe2e000 ... 0x3fe2e003:
330 case 0x3fc2ab80 ... 0x3fc2abd4:
331 /* 0x40801508/0x40801809/0x4080180a/0x40801909/0x4080190a */
332 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0):
333 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
):
334 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
):
335 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
):
336 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
):
337 /* 0x40880900/0x40880980 */
338 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS113
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0):
339 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS14
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0):
341 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0):
342 /* 0x41000189/0x4100018a */
343 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
):
344 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
):
346 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE27
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0):
348 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0):
350 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT0
, RT1320_SDCA_CTL_FUNC_STATUS
, 0):
352 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PPU21
, RT1320_SDCA_CTL_POSTURE_NUMBER
, 0):
354 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_CS113
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0):
356 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_CS14
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0):
358 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_CS21
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0):
359 /* 0x41081480/0x41081488 */
360 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_SAPU
, RT1320_SDCA_CTL_SAPU_PROTECTION_MODE
, 0):
361 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_SAPU
, RT1320_SDCA_CTL_SAPU_PROTECTION_STATUS
, 0):
363 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0):
370 static bool rt1320_volatile_register(struct device
*dev
, unsigned int reg
)
376 case 0xc402 ... 0xc406:
377 case 0xc48c ... 0xc48f:
379 case 0xc5b5 ... 0xc5b7:
380 case 0xc5fc ... 0xc5ff:
390 case 0xce14 ... 0xce17:
391 case 0xce44 ... 0xce49:
392 case 0xce4c ... 0xce4d:
394 case 0xcf10 ... 0xcf25:
395 case 0xd486 ... 0xd487:
396 case 0xd4e5 ... 0xd4e6:
397 case 0xd4e8 ... 0xd4ff:
401 case 0xdb58 ... 0xdb5f:
402 case 0xdb60 ... 0xdb63:
403 case 0xdb68 ... 0xdb69:
405 case 0xdb70 ... 0xdb71:
408 case 0xdb7c ... 0xdb7f:
409 case 0xdd0c ... 0xdd13:
411 case 0xdf14 ... 0xdf1b:
412 case 0xe83c ... 0xe847:
414 case 0xf717 ... 0xf719:
415 case 0xf720 ... 0xf723:
416 case 0x10000000 ... 0x10007fff:
417 case 0x1000c000 ... 0x1000dfff:
420 case 0x3fc2ab80 ... 0x3fc2abd4:
421 case 0x3fc2bf80 ... 0x3fc2bf83:
422 case 0x3fc2bfc0 ... 0x3fc2bfc7:
423 case 0x3fe2e000 ... 0x3fe2e003:
424 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0):
425 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT0
, RT1320_SDCA_CTL_FUNC_STATUS
, 0):
426 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_SAPU
, RT1320_SDCA_CTL_SAPU_PROTECTION_MODE
, 0):
427 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_SAPU
, RT1320_SDCA_CTL_SAPU_PROTECTION_STATUS
, 0):
428 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0):
435 static bool rt1320_mbq_readable_register(struct device
*dev
, unsigned int reg
)
438 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
):
439 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
):
440 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
):
441 case SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
):
442 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
):
443 case SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
):
450 static const struct regmap_config rt1320_sdw_regmap
= {
453 .readable_reg
= rt1320_readable_register
,
454 .volatile_reg
= rt1320_volatile_register
,
455 .max_register
= 0x41081980,
456 .reg_defaults
= rt1320_reg_defaults
,
457 .num_reg_defaults
= ARRAY_SIZE(rt1320_reg_defaults
),
458 .cache_type
= REGCACHE_MAPLE
,
459 .use_single_read
= true,
460 .use_single_write
= true,
463 static const struct regmap_config rt1320_mbq_regmap
= {
467 .readable_reg
= rt1320_mbq_readable_register
,
468 .max_register
= 0x41000192,
469 .reg_defaults
= rt1320_mbq_defaults
,
470 .num_reg_defaults
= ARRAY_SIZE(rt1320_mbq_defaults
),
471 .cache_type
= REGCACHE_MAPLE
,
472 .use_single_read
= true,
473 .use_single_write
= true,
476 static int rt1320_read_prop(struct sdw_slave
*slave
)
478 struct sdw_slave_prop
*prop
= &slave
->prop
;
483 struct sdw_dpn_prop
*dpn
;
486 * Due to support the multi-lane, we call 'sdw_slave_read_prop' to get the lane mapping
488 sdw_slave_read_prop(slave
);
490 prop
->scp_int1_mask
= SDW_SCP_INT1_BUS_CLASH
| SDW_SCP_INT1_PARITY
;
491 prop
->quirks
= SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY
;
493 prop
->paging_support
= true;
494 prop
->lane_control_support
= true;
496 /* first we need to allocate memory for set bits in port lists */
497 prop
->source_ports
= BIT(4) | BIT(8) | BIT(10);
498 prop
->sink_ports
= BIT(1);
500 nval
= hweight32(prop
->source_ports
);
501 prop
->src_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
502 sizeof(*prop
->src_dpn_prop
), GFP_KERNEL
);
503 if (!prop
->src_dpn_prop
)
507 dpn
= prop
->src_dpn_prop
;
508 addr
= prop
->source_ports
;
509 for_each_set_bit(bit
, &addr
, 32) {
511 dpn
[i
].type
= SDW_DPN_FULL
;
512 dpn
[i
].simple_ch_prep_sm
= true;
513 dpn
[i
].ch_prep_timeout
= 10;
517 /* do this again for sink now */
518 nval
= hweight32(prop
->sink_ports
);
519 prop
->sink_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
520 sizeof(*prop
->sink_dpn_prop
), GFP_KERNEL
);
521 if (!prop
->sink_dpn_prop
)
525 dpn
= prop
->sink_dpn_prop
;
526 addr
= prop
->sink_ports
;
527 for_each_set_bit(bit
, &addr
, 32) {
529 dpn
[j
].type
= SDW_DPN_FULL
;
530 dpn
[j
].simple_ch_prep_sm
= true;
531 dpn
[j
].ch_prep_timeout
= 10;
535 /* set the timeout values */
536 prop
->clk_stop_timeout
= 64;
541 static int rt1320_pde_transition_delay(struct rt1320_sdw_priv
*rt1320
, unsigned char func
,
542 unsigned char entity
, unsigned char ps
)
544 unsigned int delay
= 1000, val
;
546 pm_runtime_mark_last_busy(&rt1320
->sdw_slave
->dev
);
548 /* waiting for Actual PDE becomes to PS0/PS3 */
550 regmap_read(rt1320
->regmap
,
551 SDW_SDCA_CTL(func
, entity
, RT1320_SDCA_CTL_ACTUAL_POWER_STATE
, 0), &val
);
555 usleep_range(1000, 1500);
559 dev_warn(&rt1320
->sdw_slave
->dev
, "%s PDE to %s is NOT ready", __func__
, ps
?"PS3":"PS0");
567 * The 'patch code' is written to the patch code area.
568 * The patch code area is used for SDCA register expansion flexibility.
570 static void rt1320_load_mcu_patch(struct rt1320_sdw_priv
*rt1320
)
572 struct sdw_slave
*slave
= rt1320
->sdw_slave
;
573 const struct firmware
*patch
;
574 const char *filename
;
575 unsigned int addr
, val
;
576 const unsigned char *ptr
;
579 if (rt1320
->version_id
<= RT1320_VB
)
580 filename
= RT1320_VAB_MCU_PATCH
;
582 filename
= RT1320_VC_MCU_PATCH
;
584 /* load the patch code here */
585 ret
= request_firmware(&patch
, filename
, &slave
->dev
);
587 dev_err(&slave
->dev
, "%s: Failed to load %s firmware", __func__
, filename
);
588 regmap_write(rt1320
->regmap
, 0xc598, 0x00);
589 regmap_write(rt1320
->regmap
, 0x10007000, 0x67);
590 regmap_write(rt1320
->regmap
, 0x10007001, 0x80);
591 regmap_write(rt1320
->regmap
, 0x10007002, 0x00);
592 regmap_write(rt1320
->regmap
, 0x10007003, 0x00);
594 ptr
= (const unsigned char *)patch
->data
;
595 if ((patch
->size
% 8) == 0) {
596 for (i
= 0; i
< patch
->size
; i
+= 8) {
597 addr
= (ptr
[i
] & 0xff) | (ptr
[i
+ 1] & 0xff) << 8 |
598 (ptr
[i
+ 2] & 0xff) << 16 | (ptr
[i
+ 3] & 0xff) << 24;
599 val
= (ptr
[i
+ 4] & 0xff) | (ptr
[i
+ 5] & 0xff) << 8 |
600 (ptr
[i
+ 6] & 0xff) << 16 | (ptr
[i
+ 7] & 0xff) << 24;
602 if (addr
> 0x10007fff || addr
< 0x10007000) {
603 dev_err(&slave
->dev
, "%s: the address 0x%x is wrong", __func__
, addr
);
607 dev_err(&slave
->dev
, "%s: the value 0x%x is wrong", __func__
, val
);
610 regmap_write(rt1320
->regmap
, addr
, val
);
614 release_firmware(patch
);
618 static void rt1320_vab_preset(struct rt1320_sdw_priv
*rt1320
)
620 unsigned int i
, reg
, val
, delay
;
622 for (i
= 0; i
< ARRAY_SIZE(rt1320_blind_write
); i
++) {
623 reg
= rt1320_blind_write
[i
].reg
;
624 val
= rt1320_blind_write
[i
].def
;
625 delay
= rt1320_blind_write
[i
].delay_us
;
627 if (reg
== 0x3fc2bfc7)
628 rt1320_load_mcu_patch(rt1320
);
630 regmap_write(rt1320
->regmap
, reg
, val
);
632 usleep_range(delay
, delay
+ 1000);
636 static void rt1320_vc_preset(struct rt1320_sdw_priv
*rt1320
)
638 struct sdw_slave
*slave
= rt1320
->sdw_slave
;
639 unsigned int i
, reg
, val
, delay
, retry
, tmp
;
641 for (i
= 0; i
< ARRAY_SIZE(rt1320_vc_blind_write
); i
++) {
642 reg
= rt1320_vc_blind_write
[i
].reg
;
643 val
= rt1320_vc_blind_write
[i
].def
;
644 delay
= rt1320_vc_blind_write
[i
].delay_us
;
646 if (reg
== 0x3fc2bf83)
647 rt1320_load_mcu_patch(rt1320
);
649 if ((reg
== SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0)) &&
653 regmap_read(rt1320
->regmap
, RT1320_KR0_INT_READY
, &tmp
);
654 dev_dbg(&slave
->dev
, "%s, RT1320_KR0_INT_READY=0x%x\n", __func__
, tmp
);
657 usleep_range(1000, 1500);
661 dev_warn(&slave
->dev
, "%s MCU is NOT ready!", __func__
);
663 regmap_write(rt1320
->regmap
, reg
, val
);
665 usleep_range(delay
, delay
+ 1000);
667 if (reg
== SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, RT1320_SDCA_CTL_REQ_POWER_STATE
, 0))
668 rt1320_pde_transition_delay(rt1320
, FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, val
);
672 static int rt1320_io_init(struct device
*dev
, struct sdw_slave
*slave
)
674 struct rt1320_sdw_priv
*rt1320
= dev_get_drvdata(dev
);
675 unsigned int amp_func_status
, val
, tmp
;
680 regcache_cache_only(rt1320
->regmap
, false);
681 regcache_cache_only(rt1320
->mbq_regmap
, false);
682 if (rt1320
->first_hw_init
) {
683 regcache_cache_bypass(rt1320
->regmap
, true);
684 regcache_cache_bypass(rt1320
->mbq_regmap
, true);
687 * PM runtime status is marked as 'active' only when a Slave reports as Attached
689 /* update count of parent 'active' children */
690 pm_runtime_set_active(&slave
->dev
);
693 pm_runtime_get_noresume(&slave
->dev
);
695 if (rt1320
->version_id
< 0) {
696 regmap_read(rt1320
->regmap
, RT1320_DEV_VERSION_ID_1
, &val
);
697 rt1320
->version_id
= val
;
700 regmap_read(rt1320
->regmap
,
701 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT0
, RT1320_SDCA_CTL_FUNC_STATUS
, 0), &_func_status
);
702 dev_dbg(dev
, "%s amp func_status=0x%x\n", __func__
, amp_func_status
);
704 /* initialization write */
705 if ((amp_func_status
& FUNCTION_NEEDS_INITIALIZATION
)) {
706 if (rt1320
->version_id
< RT1320_VC
)
707 rt1320_vab_preset(rt1320
);
709 rt1320_vc_preset(rt1320
);
711 regmap_write(rt1320
->regmap
,
712 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT0
, RT1320_SDCA_CTL_FUNC_STATUS
, 0),
713 FUNCTION_NEEDS_INITIALIZATION
);
715 if (!rt1320
->first_hw_init
&& rt1320
->version_id
== RT1320_VA
) {
716 regmap_write(rt1320
->regmap
, SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
,
717 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 0);
718 regmap_read(rt1320
->regmap
, RT1320_HIFI_VER_0
, &val
);
719 regmap_read(rt1320
->regmap
, RT1320_HIFI_VER_1
, &tmp
);
720 val
= (tmp
<< 8) | val
;
721 regmap_read(rt1320
->regmap
, RT1320_HIFI_VER_2
, &tmp
);
722 val
= (tmp
<< 16) | val
;
723 regmap_read(rt1320
->regmap
, RT1320_HIFI_VER_3
, &tmp
);
724 val
= (tmp
<< 24) | val
;
725 dev_dbg(dev
, "%s ROM version=0x%x\n", __func__
, val
);
727 * We call the version b which has the new DSP ROM code against version a.
728 * Therefore, we read the DSP address to check the ID.
730 if (val
== RT1320_VER_B_ID
)
731 rt1320
->version_id
= RT1320_VB
;
732 regmap_write(rt1320
->regmap
, SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
,
733 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), 3);
735 dev_dbg(dev
, "%s version_id=%d\n", __func__
, rt1320
->version_id
);
737 if (rt1320
->first_hw_init
) {
738 regcache_cache_bypass(rt1320
->regmap
, false);
739 regcache_cache_bypass(rt1320
->mbq_regmap
, false);
740 regcache_mark_dirty(rt1320
->regmap
);
741 regcache_mark_dirty(rt1320
->mbq_regmap
);
744 /* Mark Slave initialization complete */
745 rt1320
->first_hw_init
= true;
746 rt1320
->hw_init
= true;
748 pm_runtime_mark_last_busy(&slave
->dev
);
749 pm_runtime_put_autosuspend(&slave
->dev
);
751 dev_dbg(&slave
->dev
, "%s hw_init complete\n", __func__
);
755 static int rt1320_update_status(struct sdw_slave
*slave
,
756 enum sdw_slave_status status
)
758 struct rt1320_sdw_priv
*rt1320
= dev_get_drvdata(&slave
->dev
);
760 if (status
== SDW_SLAVE_UNATTACHED
)
761 rt1320
->hw_init
= false;
764 * Perform initialization only if slave status is present and
765 * hw_init flag is false
767 if (rt1320
->hw_init
|| status
!= SDW_SLAVE_ATTACHED
)
770 /* perform I/O transfers required for Slave initialization */
771 return rt1320_io_init(&slave
->dev
, slave
);
774 static int rt1320_pde11_event(struct snd_soc_dapm_widget
*w
,
775 struct snd_kcontrol
*kcontrol
, int event
)
777 struct snd_soc_component
*component
=
778 snd_soc_dapm_to_component(w
->dapm
);
779 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
780 unsigned char ps0
= 0x0, ps3
= 0x3;
783 case SND_SOC_DAPM_POST_PMU
:
784 regmap_write(rt1320
->regmap
,
785 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
,
786 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), ps0
);
787 rt1320_pde_transition_delay(rt1320
, FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, ps0
);
789 case SND_SOC_DAPM_PRE_PMD
:
790 regmap_write(rt1320
->regmap
,
791 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
,
792 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), ps3
);
793 rt1320_pde_transition_delay(rt1320
, FUNC_NUM_MIC
, RT1320_SDCA_ENT_PDE11
, ps3
);
802 static int rt1320_pde23_event(struct snd_soc_dapm_widget
*w
,
803 struct snd_kcontrol
*kcontrol
, int event
)
805 struct snd_soc_component
*component
=
806 snd_soc_dapm_to_component(w
->dapm
);
807 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
808 unsigned char ps0
= 0x0, ps3
= 0x3;
811 case SND_SOC_DAPM_POST_PMU
:
812 regmap_write(rt1320
->regmap
,
813 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
,
814 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), ps0
);
815 rt1320_pde_transition_delay(rt1320
, FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, ps0
);
817 case SND_SOC_DAPM_PRE_PMD
:
818 regmap_write(rt1320
->regmap
,
819 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
,
820 RT1320_SDCA_CTL_REQ_POWER_STATE
, 0), ps3
);
821 rt1320_pde_transition_delay(rt1320
, FUNC_NUM_AMP
, RT1320_SDCA_ENT_PDE23
, ps3
);
830 static int rt1320_set_gain_put(struct snd_kcontrol
*kcontrol
,
831 struct snd_ctl_elem_value
*ucontrol
)
833 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
834 struct soc_mixer_control
*mc
=
835 (struct soc_mixer_control
*)kcontrol
->private_value
;
836 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
837 unsigned int gain_l_val
, gain_r_val
;
838 unsigned int lvalue
, rvalue
;
839 const unsigned int interval_offset
= 0xc0;
840 unsigned int changed
= 0, reg_base
;
841 struct rt_sdca_dmic_kctrl_priv
*p
;
842 unsigned int regvalue
[4], gain_val
[4], i
;
845 if (strstr(ucontrol
->id
.name
, "FU Capture Volume"))
848 regmap_read(rt1320
->mbq_regmap
, mc
->reg
, &lvalue
);
849 regmap_read(rt1320
->mbq_regmap
, mc
->rreg
, &rvalue
);
852 gain_l_val
= ucontrol
->value
.integer
.value
[0];
853 if (gain_l_val
> mc
->max
)
854 gain_l_val
= mc
->max
;
855 gain_l_val
= 0 - ((mc
->max
- gain_l_val
) * interval_offset
);
856 gain_l_val
&= 0xffff;
859 gain_r_val
= ucontrol
->value
.integer
.value
[1];
860 if (gain_r_val
> mc
->max
)
861 gain_r_val
= mc
->max
;
862 gain_r_val
= 0 - ((mc
->max
- gain_r_val
) * interval_offset
);
863 gain_r_val
&= 0xffff;
865 if (lvalue
== gain_l_val
&& rvalue
== gain_r_val
)
869 regmap_write(rt1320
->mbq_regmap
, mc
->reg
, gain_l_val
);
871 regmap_write(rt1320
->mbq_regmap
, mc
->rreg
, gain_r_val
);
875 p
= (struct rt_sdca_dmic_kctrl_priv
*)kcontrol
->private_value
;
877 /* check all channels */
878 for (i
= 0; i
< p
->count
; i
++) {
880 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
881 regmap_read(rt1320
->mbq_regmap
, reg_base
+ i
, ®value
[i
]);
883 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
884 regmap_read(rt1320
->mbq_regmap
, reg_base
+ i
- 2, ®value
[i
]);
887 gain_val
[i
] = ucontrol
->value
.integer
.value
[i
];
888 if (gain_val
[i
] > p
->max
)
889 gain_val
[i
] = p
->max
;
891 gain_val
[i
] = 0x1e00 - ((p
->max
- gain_val
[i
]) * interval_offset
);
892 gain_val
[i
] &= 0xffff;
893 if (regvalue
[i
] != gain_val
[i
])
900 for (i
= 0; i
< p
->count
; i
++) {
902 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
903 err
= regmap_write(rt1320
->mbq_regmap
, reg_base
+ i
, gain_val
[i
]);
905 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
906 err
= regmap_write(rt1320
->mbq_regmap
, reg_base
+ i
- 2, gain_val
[i
]);
910 dev_err(&rt1320
->sdw_slave
->dev
, "0x%08x can't be set\n", reg_base
+ i
);
917 static int rt1320_set_gain_get(struct snd_kcontrol
*kcontrol
,
918 struct snd_ctl_elem_value
*ucontrol
)
920 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
921 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
922 struct soc_mixer_control
*mc
=
923 (struct soc_mixer_control
*)kcontrol
->private_value
;
924 unsigned int read_l
, read_r
, ctl_l
= 0, ctl_r
= 0;
925 const unsigned int interval_offset
= 0xc0;
926 unsigned int reg_base
, regvalue
, ctl
, i
;
927 struct rt_sdca_dmic_kctrl_priv
*p
;
929 if (strstr(ucontrol
->id
.name
, "FU Capture Volume"))
932 regmap_read(rt1320
->mbq_regmap
, mc
->reg
, &read_l
);
933 regmap_read(rt1320
->mbq_regmap
, mc
->rreg
, &read_r
);
935 ctl_l
= mc
->max
- (((0 - read_l
) & 0xffff) / interval_offset
);
937 if (read_l
!= read_r
)
938 ctl_r
= mc
->max
- (((0 - read_r
) & 0xffff) / interval_offset
);
942 ucontrol
->value
.integer
.value
[0] = ctl_l
;
943 ucontrol
->value
.integer
.value
[1] = ctl_r
;
947 p
= (struct rt_sdca_dmic_kctrl_priv
*)kcontrol
->private_value
;
949 /* check all channels */
950 for (i
= 0; i
< p
->count
; i
++) {
952 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
953 regmap_read(rt1320
->mbq_regmap
, reg_base
+ i
, ®value
);
955 reg_base
= SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
);
956 regmap_read(rt1320
->mbq_regmap
, reg_base
+ i
- 2, ®value
);
959 ctl
= p
->max
- (((0x1e00 - regvalue
) & 0xffff) / interval_offset
);
960 ucontrol
->value
.integer
.value
[i
] = ctl
;
966 static int rt1320_set_fu_capture_ctl(struct rt1320_sdw_priv
*rt1320
)
969 unsigned int ch_mute
;
971 for (i
= 0; i
< ARRAY_SIZE(rt1320
->fu_mixer_mute
); i
++) {
972 ch_mute
= (rt1320
->fu_dapm_mute
|| rt1320
->fu_mixer_mute
[i
]) ? 0x01 : 0x00;
975 err
= regmap_write(rt1320
->regmap
,
976 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
,
977 RT1320_SDCA_CTL_FU_MUTE
, CH_01
) + i
, ch_mute
);
979 err
= regmap_write(rt1320
->regmap
,
980 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU14
,
981 RT1320_SDCA_CTL_FU_MUTE
, CH_01
) + i
- 2, ch_mute
);
989 static int rt1320_dmic_fu_capture_get(struct snd_kcontrol
*kcontrol
,
990 struct snd_ctl_elem_value
*ucontrol
)
992 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
993 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
994 struct rt_sdca_dmic_kctrl_priv
*p
=
995 (struct rt_sdca_dmic_kctrl_priv
*)kcontrol
->private_value
;
998 for (i
= 0; i
< p
->count
; i
++)
999 ucontrol
->value
.integer
.value
[i
] = !rt1320
->fu_mixer_mute
[i
];
1004 static int rt1320_dmic_fu_capture_put(struct snd_kcontrol
*kcontrol
,
1005 struct snd_ctl_elem_value
*ucontrol
)
1007 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
1008 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
1009 struct rt_sdca_dmic_kctrl_priv
*p
=
1010 (struct rt_sdca_dmic_kctrl_priv
*)kcontrol
->private_value
;
1011 int err
, changed
= 0, i
;
1013 for (i
= 0; i
< p
->count
; i
++) {
1014 if (rt1320
->fu_mixer_mute
[i
] != !ucontrol
->value
.integer
.value
[i
])
1016 rt1320
->fu_mixer_mute
[i
] = !ucontrol
->value
.integer
.value
[i
];
1019 err
= rt1320_set_fu_capture_ctl(rt1320
);
1026 static int rt1320_dmic_fu_info(struct snd_kcontrol
*kcontrol
,
1027 struct snd_ctl_elem_info
*uinfo
)
1029 struct rt_sdca_dmic_kctrl_priv
*p
=
1030 (struct rt_sdca_dmic_kctrl_priv
*)kcontrol
->private_value
;
1033 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1035 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1036 uinfo
->count
= p
->count
;
1037 uinfo
->value
.integer
.min
= 0;
1038 uinfo
->value
.integer
.max
= p
->max
;
1042 static int rt1320_dmic_fu_event(struct snd_soc_dapm_widget
*w
,
1043 struct snd_kcontrol
*kcontrol
, int event
)
1045 struct snd_soc_component
*component
=
1046 snd_soc_dapm_to_component(w
->dapm
);
1047 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
1050 case SND_SOC_DAPM_POST_PMU
:
1051 rt1320
->fu_dapm_mute
= false;
1052 rt1320_set_fu_capture_ctl(rt1320
);
1054 case SND_SOC_DAPM_PRE_PMD
:
1055 rt1320
->fu_dapm_mute
= true;
1056 rt1320_set_fu_capture_ctl(rt1320
);
1062 static const char * const rt1320_rx_data_ch_select
[] = {
1074 static SOC_ENUM_SINGLE_DECL(rt1320_rx_data_ch_enum
,
1075 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_PPU21
, RT1320_SDCA_CTL_POSTURE_NUMBER
, 0), 0,
1076 rt1320_rx_data_ch_select
);
1078 static const DECLARE_TLV_DB_SCALE(out_vol_tlv
, -6525, 75, 0);
1079 static const DECLARE_TLV_DB_SCALE(in_vol_tlv
, -1725, 75, 0);
1081 static const struct snd_kcontrol_new rt1320_snd_controls
[] = {
1082 SOC_DOUBLE_R_EXT_TLV("FU21 Playback Volume",
1083 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
),
1084 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_VOLUME
, CH_02
),
1085 0, 0x57, 0, rt1320_set_gain_get
, rt1320_set_gain_put
, out_vol_tlv
),
1086 SOC_ENUM("RX Channel Select", rt1320_rx_data_ch_enum
),
1088 RT_SDCA_FU_CTRL("FU Capture Switch",
1089 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
),
1090 1, 1, 4, rt1320_dmic_fu_info
, rt1320_dmic_fu_capture_get
, rt1320_dmic_fu_capture_put
),
1091 RT_SDCA_EXT_TLV("FU Capture Volume",
1092 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_FU113
, RT1320_SDCA_CTL_FU_VOLUME
, CH_01
),
1093 rt1320_set_gain_get
, rt1320_set_gain_put
, 4, 0x3f, in_vol_tlv
, rt1320_dmic_fu_info
),
1096 static const struct snd_kcontrol_new rt1320_spk_l_dac
=
1097 SOC_DAPM_SINGLE_AUTODISABLE("Switch",
1098 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_01
),
1100 static const struct snd_kcontrol_new rt1320_spk_r_dac
=
1101 SOC_DAPM_SINGLE_AUTODISABLE("Switch",
1102 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_FU21
, RT1320_SDCA_CTL_FU_MUTE
, CH_02
),
1105 static const struct snd_soc_dapm_widget rt1320_dapm_widgets
[] = {
1106 /* Audio Interface */
1107 SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM
, 0, 0),
1108 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM
, 0, 0),
1109 SND_SOC_DAPM_AIF_OUT("DP8-10TX", "DP8-10 Capture", 0, SND_SOC_NOPM
, 0, 0),
1111 /* Digital Interface */
1112 SND_SOC_DAPM_PGA("FU21", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1113 SND_SOC_DAPM_SUPPLY("PDE 23", SND_SOC_NOPM
, 0, 0,
1115 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1116 SND_SOC_DAPM_SUPPLY("PDE 11", SND_SOC_NOPM
, 0, 0,
1117 rt1320_pde11_event
, SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1118 SND_SOC_DAPM_ADC("FU 113", NULL
, SND_SOC_NOPM
, 0, 0),
1119 SND_SOC_DAPM_ADC("FU 14", NULL
, SND_SOC_NOPM
, 0, 0),
1120 SND_SOC_DAPM_PGA_E("FU", SND_SOC_NOPM
, 0, 0, NULL
, 0,
1121 rt1320_dmic_fu_event
, SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1124 SND_SOC_DAPM_SWITCH("OT23 L", SND_SOC_NOPM
, 0, 0, &rt1320_spk_l_dac
),
1125 SND_SOC_DAPM_SWITCH("OT23 R", SND_SOC_NOPM
, 0, 0, &rt1320_spk_r_dac
),
1126 SND_SOC_DAPM_OUTPUT("SPOL"),
1127 SND_SOC_DAPM_OUTPUT("SPOR"),
1130 SND_SOC_DAPM_PGA("AEC Data", SND_SOC_NOPM
, 0, 0, NULL
, 0),
1131 SND_SOC_DAPM_SIGGEN("AEC Gen"),
1132 SND_SOC_DAPM_INPUT("DMIC1"),
1133 SND_SOC_DAPM_INPUT("DMIC2"),
1136 static const struct snd_soc_dapm_route rt1320_dapm_routes
[] = {
1137 { "FU21", NULL
, "DP1RX" },
1138 { "FU21", NULL
, "PDE 23" },
1139 { "OT23 L", "Switch", "FU21" },
1140 { "OT23 R", "Switch", "FU21" },
1141 { "SPOL", NULL
, "OT23 L" },
1142 { "SPOR", NULL
, "OT23 R" },
1144 { "AEC Data", NULL
, "AEC Gen" },
1145 { "DP4TX", NULL
, "AEC Data" },
1147 {"DP8-10TX", NULL
, "FU"},
1148 {"FU", NULL
, "PDE 11"},
1149 {"FU", NULL
, "FU 113"},
1150 {"FU", NULL
, "FU 14"},
1151 {"FU 113", NULL
, "DMIC1"},
1152 {"FU 14", NULL
, "DMIC2"},
1155 static int rt1320_set_sdw_stream(struct snd_soc_dai
*dai
, void *sdw_stream
,
1158 snd_soc_dai_dma_data_set(dai
, direction
, sdw_stream
);
1162 static void rt1320_sdw_shutdown(struct snd_pcm_substream
*substream
,
1163 struct snd_soc_dai
*dai
)
1165 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
1168 static int rt1320_sdw_hw_params(struct snd_pcm_substream
*substream
,
1169 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
1171 struct snd_soc_component
*component
= dai
->component
;
1172 struct rt1320_sdw_priv
*rt1320
=
1173 snd_soc_component_get_drvdata(component
);
1174 struct sdw_stream_config stream_config
;
1175 struct sdw_port_config port_config
;
1176 struct sdw_port_config dmic_port_config
[2];
1177 struct sdw_stream_runtime
*sdw_stream
;
1179 unsigned int sampling_rate
;
1181 dev_dbg(dai
->dev
, "%s %s", __func__
, dai
->name
);
1182 sdw_stream
= snd_soc_dai_get_dma_data(dai
, substream
);
1187 if (!rt1320
->sdw_slave
)
1190 /* SoundWire specific configuration */
1191 snd_sdw_params_to_config(substream
, params
, &stream_config
, &port_config
);
1193 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1194 if (dai
->id
== RT1320_AIF1
)
1195 port_config
.num
= 1;
1199 if (dai
->id
== RT1320_AIF1
)
1200 port_config
.num
= 4;
1201 else if (dai
->id
== RT1320_AIF2
) {
1202 dmic_port_config
[0].ch_mask
= BIT(0) | BIT(1);
1203 dmic_port_config
[0].num
= 8;
1204 dmic_port_config
[1].ch_mask
= BIT(0) | BIT(1);
1205 dmic_port_config
[1].num
= 10;
1210 if (dai
->id
== RT1320_AIF1
)
1211 retval
= sdw_stream_add_slave(rt1320
->sdw_slave
, &stream_config
,
1212 &port_config
, 1, sdw_stream
);
1213 else if (dai
->id
== RT1320_AIF2
)
1214 retval
= sdw_stream_add_slave(rt1320
->sdw_slave
, &stream_config
,
1215 dmic_port_config
, 2, sdw_stream
);
1219 dev_err(dai
->dev
, "%s: Unable to configure port\n", __func__
);
1223 /* sampling rate configuration */
1224 switch (params_rate(params
)) {
1226 sampling_rate
= RT1320_SDCA_RATE_16000HZ
;
1229 sampling_rate
= RT1320_SDCA_RATE_32000HZ
;
1232 sampling_rate
= RT1320_SDCA_RATE_44100HZ
;
1235 sampling_rate
= RT1320_SDCA_RATE_48000HZ
;
1238 sampling_rate
= RT1320_SDCA_RATE_96000HZ
;
1241 sampling_rate
= RT1320_SDCA_RATE_192000HZ
;
1244 dev_err(component
->dev
, "%s: Rate %d is not supported\n",
1245 __func__
, params_rate(params
));
1249 /* set sampling frequency */
1250 if (dai
->id
== RT1320_AIF1
)
1251 regmap_write(rt1320
->regmap
,
1252 SDW_SDCA_CTL(FUNC_NUM_AMP
, RT1320_SDCA_ENT_CS21
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1255 regmap_write(rt1320
->regmap
,
1256 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS113
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1258 regmap_write(rt1320
->regmap
,
1259 SDW_SDCA_CTL(FUNC_NUM_MIC
, RT1320_SDCA_ENT_CS14
, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX
, 0),
1266 static int rt1320_sdw_pcm_hw_free(struct snd_pcm_substream
*substream
,
1267 struct snd_soc_dai
*dai
)
1269 struct snd_soc_component
*component
= dai
->component
;
1270 struct rt1320_sdw_priv
*rt1320
=
1271 snd_soc_component_get_drvdata(component
);
1272 struct sdw_stream_runtime
*sdw_stream
=
1273 snd_soc_dai_get_dma_data(dai
, substream
);
1275 if (!rt1320
->sdw_slave
)
1278 sdw_stream_remove_slave(rt1320
->sdw_slave
, sdw_stream
);
1283 * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
1284 * port_prep are not defined for now
1286 static const struct sdw_slave_ops rt1320_slave_ops
= {
1287 .read_prop
= rt1320_read_prop
,
1288 .update_status
= rt1320_update_status
,
1291 static int rt1320_sdw_component_probe(struct snd_soc_component
*component
)
1294 struct rt1320_sdw_priv
*rt1320
= snd_soc_component_get_drvdata(component
);
1296 rt1320
->component
= component
;
1298 if (!rt1320
->first_hw_init
)
1301 ret
= pm_runtime_resume(component
->dev
);
1302 dev_dbg(&rt1320
->sdw_slave
->dev
, "%s pm_runtime_resume, ret=%d", __func__
, ret
);
1303 if (ret
< 0 && ret
!= -EACCES
)
1309 static const struct snd_soc_component_driver soc_component_sdw_rt1320
= {
1310 .probe
= rt1320_sdw_component_probe
,
1311 .controls
= rt1320_snd_controls
,
1312 .num_controls
= ARRAY_SIZE(rt1320_snd_controls
),
1313 .dapm_widgets
= rt1320_dapm_widgets
,
1314 .num_dapm_widgets
= ARRAY_SIZE(rt1320_dapm_widgets
),
1315 .dapm_routes
= rt1320_dapm_routes
,
1316 .num_dapm_routes
= ARRAY_SIZE(rt1320_dapm_routes
),
1320 static const struct snd_soc_dai_ops rt1320_aif_dai_ops
= {
1321 .hw_params
= rt1320_sdw_hw_params
,
1322 .hw_free
= rt1320_sdw_pcm_hw_free
,
1323 .set_stream
= rt1320_set_sdw_stream
,
1324 .shutdown
= rt1320_sdw_shutdown
,
1327 #define RT1320_STEREO_RATES (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
1328 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
1329 #define RT1320_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
1330 SNDRV_PCM_FMTBIT_S32_LE)
1332 static struct snd_soc_dai_driver rt1320_sdw_dai
[] = {
1334 .name
= "rt1320-aif1",
1337 .stream_name
= "DP1 Playback",
1340 .rates
= RT1320_STEREO_RATES
,
1341 .formats
= RT1320_FORMATS
,
1344 .stream_name
= "DP4 Capture",
1347 .rates
= RT1320_STEREO_RATES
,
1348 .formats
= RT1320_FORMATS
,
1350 .ops
= &rt1320_aif_dai_ops
,
1352 /* DMIC: DP8 2ch + DP10 2ch */
1354 .name
= "rt1320-aif2",
1357 .stream_name
= "DP8-10 Capture",
1360 .rates
= RT1320_STEREO_RATES
,
1361 .formats
= RT1320_FORMATS
,
1363 .ops
= &rt1320_aif_dai_ops
,
1367 static int rt1320_sdw_init(struct device
*dev
, struct regmap
*regmap
,
1368 struct regmap
*mbq_regmap
, struct sdw_slave
*slave
)
1370 struct rt1320_sdw_priv
*rt1320
;
1373 rt1320
= devm_kzalloc(dev
, sizeof(*rt1320
), GFP_KERNEL
);
1377 dev_set_drvdata(dev
, rt1320
);
1378 rt1320
->sdw_slave
= slave
;
1379 rt1320
->mbq_regmap
= mbq_regmap
;
1380 rt1320
->regmap
= regmap
;
1382 regcache_cache_only(rt1320
->regmap
, true);
1383 regcache_cache_only(rt1320
->mbq_regmap
, true);
1386 * Mark hw_init to false
1387 * HW init will be performed when device reports present
1389 rt1320
->hw_init
= false;
1390 rt1320
->first_hw_init
= false;
1391 rt1320
->version_id
= -1;
1392 rt1320
->fu_dapm_mute
= true;
1393 rt1320
->fu_mixer_mute
[0] = rt1320
->fu_mixer_mute
[1] =
1394 rt1320
->fu_mixer_mute
[2] = rt1320
->fu_mixer_mute
[3] = true;
1396 ret
= devm_snd_soc_register_component(dev
,
1397 &soc_component_sdw_rt1320
,
1399 ARRAY_SIZE(rt1320_sdw_dai
));
1403 /* set autosuspend parameters */
1404 pm_runtime_set_autosuspend_delay(dev
, 3000);
1405 pm_runtime_use_autosuspend(dev
);
1407 /* make sure the device does not suspend immediately */
1408 pm_runtime_mark_last_busy(dev
);
1410 pm_runtime_enable(dev
);
1412 /* important note: the device is NOT tagged as 'active' and will remain
1413 * 'suspended' until the hardware is enumerated/initialized. This is required
1414 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
1415 * fail with -EACCESS because of race conditions between card creation and enumeration
1418 dev_dbg(dev
, "%s\n", __func__
);
1423 static int rt1320_sdw_probe(struct sdw_slave
*slave
,
1424 const struct sdw_device_id
*id
)
1426 struct regmap
*regmap
, *mbq_regmap
;
1428 /* Regmap Initialization */
1429 mbq_regmap
= devm_regmap_init_sdw_mbq(slave
, &rt1320_mbq_regmap
);
1430 if (IS_ERR(mbq_regmap
))
1431 return PTR_ERR(mbq_regmap
);
1433 regmap
= devm_regmap_init_sdw(slave
, &rt1320_sdw_regmap
);
1435 return PTR_ERR(regmap
);
1437 return rt1320_sdw_init(&slave
->dev
, regmap
, mbq_regmap
, slave
);
1440 static int rt1320_sdw_remove(struct sdw_slave
*slave
)
1442 pm_runtime_disable(&slave
->dev
);
1448 * Version A/B will use the class id 0
1449 * The newer version than A/B will use the class id 1, so add it in advance
1451 static const struct sdw_device_id rt1320_id
[] = {
1452 SDW_SLAVE_ENTRY_EXT(0x025d, 0x1320, 0x3, 0x0, 0),
1453 SDW_SLAVE_ENTRY_EXT(0x025d, 0x1320, 0x3, 0x1, 0),
1456 MODULE_DEVICE_TABLE(sdw
, rt1320_id
);
1458 static int __maybe_unused
rt1320_dev_suspend(struct device
*dev
)
1460 struct rt1320_sdw_priv
*rt1320
= dev_get_drvdata(dev
);
1462 if (!rt1320
->hw_init
)
1465 regcache_cache_only(rt1320
->regmap
, true);
1466 regcache_cache_only(rt1320
->mbq_regmap
, true);
1470 #define RT1320_PROBE_TIMEOUT 5000
1472 static int __maybe_unused
rt1320_dev_resume(struct device
*dev
)
1474 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
1475 struct rt1320_sdw_priv
*rt1320
= dev_get_drvdata(dev
);
1478 if (!rt1320
->first_hw_init
)
1481 if (!slave
->unattach_request
)
1484 time
= wait_for_completion_timeout(&slave
->initialization_complete
,
1485 msecs_to_jiffies(RT1320_PROBE_TIMEOUT
));
1487 dev_err(&slave
->dev
, "%s: Initialization not complete, timed out\n", __func__
);
1492 slave
->unattach_request
= 0;
1493 regcache_cache_only(rt1320
->regmap
, false);
1494 regcache_sync(rt1320
->regmap
);
1495 regcache_cache_only(rt1320
->mbq_regmap
, false);
1496 regcache_sync(rt1320
->mbq_regmap
);
1500 static const struct dev_pm_ops rt1320_pm
= {
1501 SET_SYSTEM_SLEEP_PM_OPS(rt1320_dev_suspend
, rt1320_dev_resume
)
1502 SET_RUNTIME_PM_OPS(rt1320_dev_suspend
, rt1320_dev_resume
, NULL
)
1505 static struct sdw_driver rt1320_sdw_driver
= {
1507 .name
= "rt1320-sdca",
1510 .probe
= rt1320_sdw_probe
,
1511 .remove
= rt1320_sdw_remove
,
1512 .ops
= &rt1320_slave_ops
,
1513 .id_table
= rt1320_id
,
1515 module_sdw_driver(rt1320_sdw_driver
);
1517 MODULE_DESCRIPTION("ASoC RT1320 driver SDCA SDW");
1518 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1519 MODULE_LICENSE("GPL");