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/module.h>
15 #include <linux/regmap.h>
16 #include <sound/soc.h>
18 #include "rt711-sdw.h"
20 static bool rt711_readable_register(struct device
*dev
, unsigned int reg
)
25 case 0x2012 ... 0x2016:
26 case 0x201a ... 0x2027:
27 case 0x2029 ... 0x202a:
28 case 0x202d ... 0x2034:
29 case 0x2201 ... 0x2204:
30 case 0x2206 ... 0x2212:
31 case 0x2220 ... 0x2223:
32 case 0x2230 ... 0x2239:
33 case 0x2f01 ... 0x2f0f:
34 case 0x3000 ... 0x3fff:
35 case 0x7000 ... 0x7fff:
36 case 0x8300 ... 0x83ff:
37 case 0x9c00 ... 0x9cff:
38 case 0xb900 ... 0xb9ff:
58 static bool rt711_volatile_register(struct device
*dev
, unsigned int reg
)
69 case 0x2012 ... 0x2015: /* HD-A read */
70 case 0x202d ... 0x202f: /* BRA */
71 case 0x2201 ... 0x2212: /* i2c debug */
72 case 0x2220 ... 0x2223: /* decoded HD-A */
73 case 0x9c00 ... 0x9cff:
74 case 0xb900 ... 0xb9ff:
87 static int rt711_sdw_read(void *context
, unsigned int reg
, unsigned int *val
)
89 struct device
*dev
= context
;
90 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
91 unsigned int sdw_data_3
, sdw_data_2
, sdw_data_1
, sdw_data_0
;
92 unsigned int reg2
= 0, reg3
= 0, reg4
= 0, mask
, nid
, val2
;
93 unsigned int is_hda_reg
= 1, is_index_reg
= 0;
101 if (is_index_reg
) { /* index registers */
105 ret
= regmap_write(rt711
->sdw_regmap
, reg
, 0);
110 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, val2
);
114 reg3
= RT711_PRIV_DATA_R_H
| nid
;
115 ret
= regmap_write(rt711
->sdw_regmap
,
116 reg3
, ((*val
>> 8) & 0xff));
119 reg4
= reg3
+ 0x1000;
121 ret
= regmap_write(rt711
->sdw_regmap
, reg4
, (*val
& 0xff));
124 } else if (mask
== 0x3000) {
126 ret
= regmap_write(rt711
->sdw_regmap
, reg
, *val
);
129 } else if (mask
== 0x7000) {
132 ret
= regmap_write(rt711
->sdw_regmap
,
133 reg
, ((*val
>> 8) & 0xff));
138 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (*val
& 0xff));
141 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
144 ret
= regmap_write(rt711
->sdw_regmap
,
145 reg2
, ((*val
>> 8) & 0xff));
148 ret
= regmap_write(rt711
->sdw_regmap
, reg
, (*val
& 0xff));
151 } else if (mask
== 0x9000) {
152 ret
= regmap_write(rt711
->sdw_regmap
,
153 reg
, ((*val
>> 8) & 0xff));
158 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (*val
& 0xff));
161 } else if (mask
== 0xb000) {
162 ret
= regmap_write(rt711
->sdw_regmap
, reg
, *val
);
166 ret
= regmap_read(rt711
->sdw_regmap
, reg
, val
);
172 if (is_hda_reg
|| is_index_reg
) {
177 ret
= regmap_read(rt711
->sdw_regmap
,
178 RT711_READ_HDA_3
, &sdw_data_3
);
181 ret
= regmap_read(rt711
->sdw_regmap
,
182 RT711_READ_HDA_2
, &sdw_data_2
);
185 ret
= regmap_read(rt711
->sdw_regmap
,
186 RT711_READ_HDA_1
, &sdw_data_1
);
189 ret
= regmap_read(rt711
->sdw_regmap
,
190 RT711_READ_HDA_0
, &sdw_data_0
);
193 *val
= ((sdw_data_3
& 0xff) << 24) |
194 ((sdw_data_2
& 0xff) << 16) |
195 ((sdw_data_1
& 0xff) << 8) | (sdw_data_0
& 0xff);
199 dev_dbg(dev
, "[%s] %04x => %08x\n", __func__
, reg
, *val
);
200 else if (is_index_reg
)
201 dev_dbg(dev
, "[%s] %04x %04x %04x %04x => %08x\n",
202 __func__
, reg
, reg2
, reg3
, reg4
, *val
);
204 dev_dbg(dev
, "[%s] %04x %04x => %08x\n",
205 __func__
, reg
, reg2
, *val
);
210 static int rt711_sdw_write(void *context
, unsigned int reg
, unsigned int val
)
212 struct device
*dev
= context
;
213 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
214 unsigned int reg2
= 0, reg3
, reg4
, nid
, mask
, val2
;
215 unsigned int is_index_reg
= 0;
223 if (is_index_reg
) { /* index registers */
227 ret
= regmap_write(rt711
->sdw_regmap
, reg
, 0);
232 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, val2
);
236 reg3
= RT711_PRIV_DATA_W_H
| nid
;
237 ret
= regmap_write(rt711
->sdw_regmap
,
238 reg3
, ((val
>> 8) & 0xff));
241 reg4
= reg3
+ 0x1000;
243 ret
= regmap_write(rt711
->sdw_regmap
, reg4
, (val
& 0xff));
247 } else if (reg
< 0x4fff) {
248 ret
= regmap_write(rt711
->sdw_regmap
, reg
, val
);
251 } else if (reg
== RT711_FUNC_RESET
) {
252 ret
= regmap_write(rt711
->sdw_regmap
, reg
, val
);
255 } else if (mask
== 0x7000) {
256 ret
= regmap_write(rt711
->sdw_regmap
,
257 reg
, ((val
>> 8) & 0xff));
262 ret
= regmap_write(rt711
->sdw_regmap
, reg2
, (val
& 0xff));
265 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
268 ret
= regmap_write(rt711
->sdw_regmap
,
269 reg2
, ((val
>> 8) & 0xff));
272 ret
= regmap_write(rt711
->sdw_regmap
, reg
, (val
& 0xff));
278 dev_dbg(dev
, "[%s] %04x <= %04x\n", __func__
, reg
, val
);
279 else if (is_index_reg
)
280 dev_dbg(dev
, "[%s] %04x %04x %04x %04x <= %04x %04x\n",
281 __func__
, reg
, reg2
, reg3
, reg4
, val2
, val
);
283 dev_dbg(dev
, "[%s] %04x %04x <= %04x\n",
284 __func__
, reg
, reg2
, val
);
289 static const struct regmap_config rt711_regmap
= {
292 .readable_reg
= rt711_readable_register
,
293 .volatile_reg
= rt711_volatile_register
,
294 .max_register
= 0x755800,
295 .reg_defaults
= rt711_reg_defaults
,
296 .num_reg_defaults
= ARRAY_SIZE(rt711_reg_defaults
),
297 .cache_type
= REGCACHE_RBTREE
,
298 .use_single_read
= true,
299 .use_single_write
= true,
300 .reg_read
= rt711_sdw_read
,
301 .reg_write
= rt711_sdw_write
,
304 static const struct regmap_config rt711_sdw_regmap
= {
308 .readable_reg
= rt711_readable_register
,
309 .max_register
= 0xff01,
310 .cache_type
= REGCACHE_NONE
,
311 .use_single_read
= true,
312 .use_single_write
= true,
315 static int rt711_update_status(struct sdw_slave
*slave
,
316 enum sdw_slave_status status
)
318 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
320 /* Update the status */
321 rt711
->status
= status
;
323 if (status
== SDW_SLAVE_UNATTACHED
)
324 rt711
->hw_init
= false;
327 * Perform initialization only if slave status is present and
328 * hw_init flag is false
330 if (rt711
->hw_init
|| rt711
->status
!= SDW_SLAVE_ATTACHED
)
333 /* perform I/O transfers required for Slave initialization */
334 return rt711_io_init(&slave
->dev
, slave
);
337 static int rt711_read_prop(struct sdw_slave
*slave
)
339 struct sdw_slave_prop
*prop
= &slave
->prop
;
340 int nval
, i
, num_of_ports
= 1;
343 struct sdw_dpn_prop
*dpn
;
345 prop
->paging_support
= false;
347 /* first we need to allocate memory for set bits in port lists */
348 prop
->source_ports
= 0x14; /* BITMAP: 00010100 */
349 prop
->sink_ports
= 0x8; /* BITMAP: 00001000 */
351 nval
= hweight32(prop
->source_ports
);
352 num_of_ports
+= nval
;
353 prop
->src_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
354 sizeof(*prop
->src_dpn_prop
),
356 if (!prop
->src_dpn_prop
)
360 dpn
= prop
->src_dpn_prop
;
361 addr
= prop
->source_ports
;
362 for_each_set_bit(bit
, &addr
, 32) {
364 dpn
[i
].type
= SDW_DPN_FULL
;
365 dpn
[i
].simple_ch_prep_sm
= true;
366 dpn
[i
].ch_prep_timeout
= 10;
370 /* do this again for sink now */
371 nval
= hweight32(prop
->sink_ports
);
372 num_of_ports
+= nval
;
373 prop
->sink_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
374 sizeof(*prop
->sink_dpn_prop
),
376 if (!prop
->sink_dpn_prop
)
380 dpn
= prop
->sink_dpn_prop
;
381 addr
= prop
->sink_ports
;
382 for_each_set_bit(bit
, &addr
, 32) {
384 dpn
[i
].type
= SDW_DPN_FULL
;
385 dpn
[i
].simple_ch_prep_sm
= true;
386 dpn
[i
].ch_prep_timeout
= 10;
390 /* Allocate port_ready based on num_of_ports */
391 slave
->port_ready
= devm_kcalloc(&slave
->dev
, num_of_ports
,
392 sizeof(*slave
->port_ready
),
394 if (!slave
->port_ready
)
397 /* Initialize completion */
398 for (i
= 0; i
< num_of_ports
; i
++)
399 init_completion(&slave
->port_ready
[i
]);
401 /* set the timeout values */
402 prop
->clk_stop_timeout
= 20;
405 prop
->wake_capable
= 1;
410 static int rt711_bus_config(struct sdw_slave
*slave
,
411 struct sdw_bus_params
*params
)
413 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
416 memcpy(&rt711
->params
, params
, sizeof(*params
));
418 ret
= rt711_clock_config(&slave
->dev
);
420 dev_err(&slave
->dev
, "Invalid clk config");
425 static int rt711_interrupt_callback(struct sdw_slave
*slave
,
426 struct sdw_slave_intr_status
*status
)
428 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
431 "%s control_port_stat=%x", __func__
, status
->control_port
);
433 if (status
->control_port
& 0x4) {
434 mod_delayed_work(system_power_efficient_wq
,
435 &rt711
->jack_detect_work
, msecs_to_jiffies(250));
441 static struct sdw_slave_ops rt711_slave_ops
= {
442 .read_prop
= rt711_read_prop
,
443 .interrupt_callback
= rt711_interrupt_callback
,
444 .update_status
= rt711_update_status
,
445 .bus_config
= rt711_bus_config
,
448 static int rt711_sdw_probe(struct sdw_slave
*slave
,
449 const struct sdw_device_id
*id
)
451 struct regmap
*sdw_regmap
, *regmap
;
454 slave
->ops
= &rt711_slave_ops
;
456 /* Regmap Initialization */
457 sdw_regmap
= devm_regmap_init_sdw(slave
, &rt711_sdw_regmap
);
461 regmap
= devm_regmap_init(&slave
->dev
, NULL
,
462 &slave
->dev
, &rt711_regmap
);
464 return PTR_ERR(regmap
);
466 rt711_init(&slave
->dev
, sdw_regmap
, regmap
, slave
);
471 static int rt711_sdw_remove(struct sdw_slave
*slave
)
473 struct rt711_priv
*rt711
= dev_get_drvdata(&slave
->dev
);
475 if (rt711
&& rt711
->hw_init
) {
476 cancel_delayed_work(&rt711
->jack_detect_work
);
477 cancel_delayed_work(&rt711
->jack_btn_check_work
);
478 cancel_work_sync(&rt711
->calibration_work
);
484 static const struct sdw_device_id rt711_id
[] = {
485 SDW_SLAVE_ENTRY(0x025d, 0x711, 0),
488 MODULE_DEVICE_TABLE(sdw
, rt711_id
);
490 static int rt711_dev_suspend(struct device
*dev
)
492 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
497 regcache_cache_only(rt711
->regmap
, true);
502 #define RT711_PROBE_TIMEOUT 2000
504 static int rt711_dev_resume(struct device
*dev
)
506 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
507 struct rt711_priv
*rt711
= dev_get_drvdata(dev
);
513 if (!slave
->unattach_request
)
516 time
= wait_for_completion_timeout(&slave
->initialization_complete
,
517 msecs_to_jiffies(RT711_PROBE_TIMEOUT
));
519 dev_err(&slave
->dev
, "Initialization not complete, timed out\n");
524 slave
->unattach_request
= 0;
525 regcache_cache_only(rt711
->regmap
, false);
526 regcache_sync_region(rt711
->regmap
, 0x3000, 0x8fff);
527 regcache_sync_region(rt711
->regmap
, 0x752009, 0x752091);
532 static const struct dev_pm_ops rt711_pm
= {
533 SET_SYSTEM_SLEEP_PM_OPS(rt711_dev_suspend
, rt711_dev_resume
)
534 SET_RUNTIME_PM_OPS(rt711_dev_suspend
, rt711_dev_resume
, NULL
)
537 static struct sdw_driver rt711_sdw_driver
= {
540 .owner
= THIS_MODULE
,
543 .probe
= rt711_sdw_probe
,
544 .remove
= rt711_sdw_remove
,
545 .ops
= &rt711_slave_ops
,
546 .id_table
= rt711_id
,
548 module_sdw_driver(rt711_sdw_driver
);
550 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
551 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
552 MODULE_LICENSE("GPL");