1 // SPDX-License-Identifier: GPL-2.0
3 // rt711-sdw.c -- rt711 ALSA SoC audio driver
5 // Copyright(c) 2019 Realtek Semiconductor Corp.
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/soundwire/sdw.h>
13 #include <linux/soundwire/sdw_type.h>
14 #include <linux/soundwire/sdw_registers.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <sound/soc.h>
20 #include "rt711-sdw.h"
22 static bool rt711_readable_register(struct device
*dev
, unsigned int reg
)
27 case 0x2012 ... 0x2016:
28 case 0x201a ... 0x2027:
29 case 0x2029 ... 0x202a:
30 case 0x202d ... 0x2034:
31 case 0x2201 ... 0x2204:
32 case 0x2206 ... 0x2212:
33 case 0x2220 ... 0x2223:
34 case 0x2230 ... 0x2239:
35 case 0x2f01 ... 0x2f0f:
36 case 0x3000 ... 0x3fff:
37 case 0x7000 ... 0x7fff:
38 case 0x8300 ... 0x83ff:
39 case 0x9c00 ... 0x9cff:
40 case 0xb900 ... 0xb9ff:
62 static bool rt711_volatile_register(struct device
*dev
, unsigned int reg
)
73 case 0x2012 ... 0x2015: /* HD-A read */
74 case 0x202d ... 0x202f: /* BRA */
75 case 0x2201 ... 0x2212: /* i2c debug */
76 case 0x2220 ... 0x2223: /* decoded HD-A */
77 case 0x9c00 ... 0x9cff:
78 case 0xb900 ... 0xb9ff:
91 static int rt711_sdw_read(void *context
, unsigned int reg
, unsigned int *val
)
93 struct device
*dev
= context
;
94 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
95 unsigned int sdw_data_3
, sdw_data_2
, sdw_data_1
, sdw_data_0
;
96 unsigned int reg2
= 0, reg3
= 0, reg4
= 0, mask
, nid
, val2
;
97 unsigned int is_hda_reg
= 1, is_index_reg
= 0;
105 if (is_index_reg
) { /* index registers */
109 ret
= regmap_write(rt711
->sdw_regmap
, reg
, 0);
114 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, val2
);
118 reg3
= RT711_PRIV_DATA_R_H
| nid
;
119 ret
= regmap_write(rt711
->sdw_regmap
,
120 reg3
, ((*val
>> 8) & 0xff));
123 reg4
= reg3
+ 0x1000;
125 ret
= regmap_write(rt711
->sdw_regmap
, reg4
, (*val
& 0xff));
128 } else if (mask
== 0x3000) {
130 ret
= regmap_write(rt711
->sdw_regmap
, reg
, *val
);
133 } else if (mask
== 0x7000) {
136 ret
= regmap_write(rt711
->sdw_regmap
,
137 reg
, ((*val
>> 8) & 0xff));
142 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (*val
& 0xff));
145 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
148 ret
= regmap_write(rt711
->sdw_regmap
,
149 reg2
, ((*val
>> 8) & 0xff));
152 ret
= regmap_write(rt711
->sdw_regmap
, reg
, (*val
& 0xff));
155 } else if (mask
== 0x9000) {
156 ret
= regmap_write(rt711
->sdw_regmap
,
157 reg
, ((*val
>> 8) & 0xff));
162 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (*val
& 0xff));
165 } else if (mask
== 0xb000) {
166 ret
= regmap_write(rt711
->sdw_regmap
, reg
, *val
);
170 ret
= regmap_read(rt711
->sdw_regmap
, reg
, val
);
176 if (is_hda_reg
|| is_index_reg
) {
181 ret
= regmap_read(rt711
->sdw_regmap
,
182 RT711_READ_HDA_3
, &sdw_data_3
);
185 ret
= regmap_read(rt711
->sdw_regmap
,
186 RT711_READ_HDA_2
, &sdw_data_2
);
189 ret
= regmap_read(rt711
->sdw_regmap
,
190 RT711_READ_HDA_1
, &sdw_data_1
);
193 ret
= regmap_read(rt711
->sdw_regmap
,
194 RT711_READ_HDA_0
, &sdw_data_0
);
197 *val
= ((sdw_data_3
& 0xff) << 24) |
198 ((sdw_data_2
& 0xff) << 16) |
199 ((sdw_data_1
& 0xff) << 8) | (sdw_data_0
& 0xff);
203 dev_dbg(dev
, "[%s] %04x => %08x\n", __func__
, reg
, *val
);
204 else if (is_index_reg
)
205 dev_dbg(dev
, "[%s] %04x %04x %04x %04x => %08x\n",
206 __func__
, reg
, reg2
, reg3
, reg4
, *val
);
208 dev_dbg(dev
, "[%s] %04x %04x => %08x\n",
209 __func__
, reg
, reg2
, *val
);
214 static int rt711_sdw_write(void *context
, unsigned int reg
, unsigned int val
)
216 struct device
*dev
= context
;
217 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
218 unsigned int reg2
= 0, reg3
, reg4
, nid
, mask
, val2
;
219 unsigned int is_index_reg
= 0;
227 if (is_index_reg
) { /* index registers */
231 ret
= regmap_write(rt711
->sdw_regmap
, reg
, 0);
236 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, val2
);
240 reg3
= RT711_PRIV_DATA_W_H
| nid
;
241 ret
= regmap_write(rt711
->sdw_regmap
,
242 reg3
, ((val
>> 8) & 0xff));
245 reg4
= reg3
+ 0x1000;
247 ret
= regmap_write(rt711
->sdw_regmap
, reg4
, (val
& 0xff));
251 } else if (reg
< 0x4fff) {
252 ret
= regmap_write(rt711
->sdw_regmap
, reg
, val
);
255 } else if (reg
== RT711_FUNC_RESET
) {
256 ret
= regmap_write(rt711
->sdw_regmap
, reg
, val
);
259 } else if (mask
== 0x7000) {
260 ret
= regmap_write(rt711
->sdw_regmap
,
261 reg
, ((val
>> 8) & 0xff));
266 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (val
& 0xff));
269 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
272 ret
= regmap_write(rt711
->sdw_regmap
,
273 reg2
, ((val
>> 8) & 0xff));
276 ret
= regmap_write(rt711
->sdw_regmap
, reg
, (val
& 0xff));
282 dev_dbg(dev
, "[%s] %04x <= %04x\n", __func__
, reg
, val
);
283 else if (is_index_reg
)
284 dev_dbg(dev
, "[%s] %04x %04x %04x %04x <= %04x %04x\n",
285 __func__
, reg
, reg2
, reg3
, reg4
, val2
, val
);
287 dev_dbg(dev
, "[%s] %04x %04x <= %04x\n",
288 __func__
, reg
, reg2
, val
);
293 static const struct regmap_config rt711_regmap
= {
296 .readable_reg
= rt711_readable_register
,
297 .volatile_reg
= rt711_volatile_register
,
298 .max_register
= 0x755800,
299 .reg_defaults
= rt711_reg_defaults
,
300 .num_reg_defaults
= ARRAY_SIZE(rt711_reg_defaults
),
301 .cache_type
= REGCACHE_MAPLE
,
302 .use_single_read
= true,
303 .use_single_write
= true,
304 .reg_read
= rt711_sdw_read
,
305 .reg_write
= rt711_sdw_write
,
308 static const struct regmap_config rt711_sdw_regmap
= {
312 .readable_reg
= rt711_readable_register
,
313 .max_register
= 0xff01,
314 .cache_type
= REGCACHE_NONE
,
315 .use_single_read
= true,
316 .use_single_write
= true,
319 static int rt711_update_status(struct sdw_slave
*slave
,
320 enum sdw_slave_status status
)
322 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
324 if (status
== SDW_SLAVE_UNATTACHED
)
325 rt711
->hw_init
= false;
328 * Perform initialization only if slave status is present and
329 * hw_init flag is false
331 if (rt711
->hw_init
|| status
!= SDW_SLAVE_ATTACHED
)
334 /* perform I/O transfers required for Slave initialization */
335 return rt711_io_init(&slave
->dev
, slave
);
338 static int rt711_read_prop(struct sdw_slave
*slave
)
340 struct sdw_slave_prop
*prop
= &slave
->prop
;
345 struct sdw_dpn_prop
*dpn
;
347 prop
->scp_int1_mask
= SDW_SCP_INT1_IMPL_DEF
| SDW_SCP_INT1_BUS_CLASH
|
349 prop
->quirks
= SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY
;
351 prop
->paging_support
= false;
353 /* first we need to allocate memory for set bits in port lists */
354 prop
->source_ports
= 0x14; /* BITMAP: 00010100 */
355 prop
->sink_ports
= 0x8; /* BITMAP: 00001000 */
357 nval
= hweight32(prop
->source_ports
);
358 prop
->src_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
359 sizeof(*prop
->src_dpn_prop
),
361 if (!prop
->src_dpn_prop
)
365 dpn
= prop
->src_dpn_prop
;
366 addr
= prop
->source_ports
;
367 for_each_set_bit(bit
, &addr
, 32) {
369 dpn
[i
].type
= SDW_DPN_FULL
;
370 dpn
[i
].simple_ch_prep_sm
= true;
371 dpn
[i
].ch_prep_timeout
= 10;
375 /* do this again for sink now */
376 nval
= hweight32(prop
->sink_ports
);
377 prop
->sink_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
378 sizeof(*prop
->sink_dpn_prop
),
380 if (!prop
->sink_dpn_prop
)
384 dpn
= prop
->sink_dpn_prop
;
385 addr
= prop
->sink_ports
;
386 for_each_set_bit(bit
, &addr
, 32) {
388 dpn
[j
].type
= SDW_DPN_FULL
;
389 dpn
[j
].simple_ch_prep_sm
= true;
390 dpn
[j
].ch_prep_timeout
= 10;
394 /* set the timeout values */
395 prop
->clk_stop_timeout
= 20;
398 prop
->wake_capable
= 1;
403 static int rt711_bus_config(struct sdw_slave
*slave
,
404 struct sdw_bus_params
*params
)
406 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
409 memcpy(&rt711
->params
, params
, sizeof(*params
));
411 ret
= rt711_clock_config(&slave
->dev
);
413 dev_err(&slave
->dev
, "%s: Invalid clk config", __func__
);
418 static int rt711_interrupt_callback(struct sdw_slave
*slave
,
419 struct sdw_slave_intr_status
*status
)
421 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
424 "%s control_port_stat=%x", __func__
, status
->control_port
);
426 mutex_lock(&rt711
->disable_irq_lock
);
427 if (status
->control_port
& 0x4 && !rt711
->disable_irq
) {
428 mod_delayed_work(system_power_efficient_wq
,
429 &rt711
->jack_detect_work
, msecs_to_jiffies(250));
431 mutex_unlock(&rt711
->disable_irq_lock
);
436 static const struct sdw_slave_ops rt711_slave_ops
= {
437 .read_prop
= rt711_read_prop
,
438 .interrupt_callback
= rt711_interrupt_callback
,
439 .update_status
= rt711_update_status
,
440 .bus_config
= rt711_bus_config
,
443 static int rt711_sdw_probe(struct sdw_slave
*slave
,
444 const struct sdw_device_id
*id
)
446 struct regmap
*sdw_regmap
, *regmap
;
448 /* Regmap Initialization */
449 sdw_regmap
= devm_regmap_init_sdw(slave
, &rt711_sdw_regmap
);
450 if (IS_ERR(sdw_regmap
))
451 return PTR_ERR(sdw_regmap
);
453 regmap
= devm_regmap_init(&slave
->dev
, NULL
,
454 &slave
->dev
, &rt711_regmap
);
456 return PTR_ERR(regmap
);
458 return rt711_init(&slave
->dev
, sdw_regmap
, regmap
, slave
);
461 static int rt711_sdw_remove(struct sdw_slave
*slave
)
463 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
465 if (rt711
->hw_init
) {
466 cancel_delayed_work_sync(&rt711
->jack_detect_work
);
467 cancel_delayed_work_sync(&rt711
->jack_btn_check_work
);
468 cancel_work_sync(&rt711
->calibration_work
);
471 pm_runtime_disable(&slave
->dev
);
473 mutex_destroy(&rt711
->calibrate_mutex
);
474 mutex_destroy(&rt711
->disable_irq_lock
);
479 static const struct sdw_device_id rt711_id
[] = {
480 SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x2, 0, 0),
483 MODULE_DEVICE_TABLE(sdw
, rt711_id
);
485 static int __maybe_unused
rt711_dev_suspend(struct device
*dev
)
487 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
492 cancel_delayed_work_sync(&rt711
->jack_detect_work
);
493 cancel_delayed_work_sync(&rt711
->jack_btn_check_work
);
494 cancel_work_sync(&rt711
->calibration_work
);
496 regcache_cache_only(rt711
->regmap
, true);
501 static int __maybe_unused
rt711_dev_system_suspend(struct device
*dev
)
503 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
504 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
511 * prevent new interrupts from being handled after the
512 * deferred work completes and before the parent disables
513 * interrupts on the link
515 mutex_lock(&rt711
->disable_irq_lock
);
516 rt711
->disable_irq
= true;
517 ret
= sdw_update_no_pm(slave
, SDW_SCP_INTMASK1
,
518 SDW_SCP_INT1_IMPL_DEF
, 0);
519 mutex_unlock(&rt711
->disable_irq_lock
);
522 /* log but don't prevent suspend from happening */
523 dev_dbg(&slave
->dev
, "%s: could not disable imp-def interrupts\n:", __func__
);
526 return rt711_dev_suspend(dev
);
529 #define RT711_PROBE_TIMEOUT 5000
531 static int __maybe_unused
rt711_dev_resume(struct device
*dev
)
533 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
534 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
537 if (!rt711
->first_hw_init
)
540 if (!slave
->unattach_request
) {
541 mutex_lock(&rt711
->disable_irq_lock
);
542 if (rt711
->disable_irq
== true) {
543 sdw_write_no_pm(slave
, SDW_SCP_INTMASK1
, SDW_SCP_INT1_IMPL_DEF
);
544 rt711
->disable_irq
= false;
546 mutex_unlock(&rt711
->disable_irq_lock
);
550 time
= wait_for_completion_timeout(&slave
->initialization_complete
,
551 msecs_to_jiffies(RT711_PROBE_TIMEOUT
));
553 dev_err(&slave
->dev
, "%s: Initialization not complete, timed out\n", __func__
);
558 slave
->unattach_request
= 0;
559 regcache_cache_only(rt711
->regmap
, false);
560 regcache_sync_region(rt711
->regmap
, 0x3000, 0x8fff);
561 regcache_sync_region(rt711
->regmap
, 0x752009, 0x752091);
566 static const struct dev_pm_ops rt711_pm
= {
567 SET_SYSTEM_SLEEP_PM_OPS(rt711_dev_system_suspend
, rt711_dev_resume
)
568 SET_RUNTIME_PM_OPS(rt711_dev_suspend
, rt711_dev_resume
, NULL
)
571 static struct sdw_driver rt711_sdw_driver
= {
576 .probe
= rt711_sdw_probe
,
577 .remove
= rt711_sdw_remove
,
578 .ops
= &rt711_slave_ops
,
579 .id_table
= rt711_id
,
581 module_sdw_driver(rt711_sdw_driver
);
583 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
584 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
585 MODULE_LICENSE("GPL");