1 // SPDX-License-Identifier: GPL-2.0
3 // rt700-sdw.c -- rt700 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 "rt700-sdw.h"
20 static bool rt700_readable_register(struct device
*dev
, unsigned int reg
)
25 case 0x2000 ... 0x200e:
26 case 0x2012 ... 0x2016:
27 case 0x201a ... 0x2027:
28 case 0x2029 ... 0x202a:
29 case 0x202d ... 0x2034:
30 case 0x2200 ... 0x2204:
31 case 0x2206 ... 0x2212:
32 case 0x2220 ... 0x2223:
33 case 0x2230 ... 0x2231:
34 case 0x3000 ... 0x3fff:
35 case 0x7000 ... 0x7fff:
36 case 0x8300 ... 0x83ff:
37 case 0x9c00 ... 0x9cff:
38 case 0xb900 ... 0xb9ff:
53 static bool rt700_volatile_register(struct device
*dev
, unsigned int reg
)
65 case 0x200b ... 0x200e: /* i2c read */
66 case 0x2012 ... 0x2015: /* HD-A read */
67 case 0x202d ... 0x202f: /* BRA */
68 case 0x2201 ... 0x2212: /* i2c debug */
69 case 0x2220 ... 0x2223: /* decoded HD-A */
70 case 0x9c00 ... 0x9cff:
71 case 0xb900 ... 0xb9ff:
83 static int rt700_sdw_read(void *context
, unsigned int reg
, unsigned int *val
)
85 struct device
*dev
= context
;
86 struct rt700_priv
*rt700
= dev_get_drvdata(dev
);
87 unsigned int sdw_data_3
, sdw_data_2
, sdw_data_1
, sdw_data_0
;
88 unsigned int reg2
= 0, reg3
= 0, reg4
= 0, mask
, nid
, val2
;
89 unsigned int is_hda_reg
= 1, is_index_reg
= 0;
97 if (is_index_reg
) { /* index registers */
101 ret
= regmap_write(rt700
->sdw_regmap
, reg
, 0);
106 ret
= regmap_write(rt700
->sdw_regmap
, reg2
, val2
);
110 reg3
= RT700_PRIV_DATA_R_H
| nid
;
111 ret
= regmap_write(rt700
->sdw_regmap
,
112 reg3
, ((*val
>> 8) & 0xff));
115 reg4
= reg3
+ 0x1000;
117 ret
= regmap_write(rt700
->sdw_regmap
, reg4
, (*val
& 0xff));
120 } else if (mask
== 0x3000) {
122 ret
= regmap_write(rt700
->sdw_regmap
, reg
, *val
);
125 } else if (mask
== 0x7000) {
128 ret
= regmap_write(rt700
->sdw_regmap
,
129 reg
, ((*val
>> 8) & 0xff));
134 ret
= regmap_write(rt700
->sdw_regmap
, reg2
, (*val
& 0xff));
137 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
140 ret
= regmap_write(rt700
->sdw_regmap
,
141 reg2
, ((*val
>> 8) & 0xff));
144 ret
= regmap_write(rt700
->sdw_regmap
, reg
, (*val
& 0xff));
147 } else if (mask
== 0x9000) {
148 ret
= regmap_write(rt700
->sdw_regmap
,
149 reg
, ((*val
>> 8) & 0xff));
154 ret
= regmap_write(rt700
->sdw_regmap
, reg2
, (*val
& 0xff));
157 } else if (mask
== 0xb000) {
158 ret
= regmap_write(rt700
->sdw_regmap
, reg
, *val
);
162 ret
= regmap_read(rt700
->sdw_regmap
, reg
, val
);
168 if (is_hda_reg
|| is_index_reg
) {
173 ret
= regmap_read(rt700
->sdw_regmap
,
174 RT700_READ_HDA_3
, &sdw_data_3
);
177 ret
= regmap_read(rt700
->sdw_regmap
,
178 RT700_READ_HDA_2
, &sdw_data_2
);
181 ret
= regmap_read(rt700
->sdw_regmap
,
182 RT700_READ_HDA_1
, &sdw_data_1
);
185 ret
= regmap_read(rt700
->sdw_regmap
,
186 RT700_READ_HDA_0
, &sdw_data_0
);
189 *val
= ((sdw_data_3
& 0xff) << 24) |
190 ((sdw_data_2
& 0xff) << 16) |
191 ((sdw_data_1
& 0xff) << 8) | (sdw_data_0
& 0xff);
195 dev_dbg(dev
, "[%s] %04x => %08x\n", __func__
, reg
, *val
);
196 else if (is_index_reg
)
197 dev_dbg(dev
, "[%s] %04x %04x %04x %04x => %08x\n",
198 __func__
, reg
, reg2
, reg3
, reg4
, *val
);
200 dev_dbg(dev
, "[%s] %04x %04x => %08x\n",
201 __func__
, reg
, reg2
, *val
);
206 static int rt700_sdw_write(void *context
, unsigned int reg
, unsigned int val
)
208 struct device
*dev
= context
;
209 struct rt700_priv
*rt700
= dev_get_drvdata(dev
);
210 unsigned int reg2
= 0, reg3
, reg4
, nid
, mask
, val2
;
211 unsigned int is_index_reg
= 0;
219 if (is_index_reg
) { /* index registers */
223 ret
= regmap_write(rt700
->sdw_regmap
, reg
, 0);
228 ret
= regmap_write(rt700
->sdw_regmap
, reg2
, val2
);
232 reg3
= RT700_PRIV_DATA_W_H
| nid
;
233 ret
= regmap_write(rt700
->sdw_regmap
,
234 reg3
, ((val
>> 8) & 0xff));
237 reg4
= reg3
+ 0x1000;
239 ret
= regmap_write(rt700
->sdw_regmap
, reg4
, (val
& 0xff));
243 } else if (reg
< 0x4fff) {
244 ret
= regmap_write(rt700
->sdw_regmap
, reg
, val
);
247 } else if (reg
== 0xff01) {
248 ret
= regmap_write(rt700
->sdw_regmap
, reg
, val
);
251 } else if (mask
== 0x7000) {
252 ret
= regmap_write(rt700
->sdw_regmap
,
253 reg
, ((val
>> 8) & 0xff));
258 ret
= regmap_write(rt700
->sdw_regmap
, reg2
, (val
& 0xff));
261 } else if ((reg
& 0xff00) == 0x8300) { /* for R channel */
264 ret
= regmap_write(rt700
->sdw_regmap
,
265 reg2
, ((val
>> 8) & 0xff));
268 ret
= regmap_write(rt700
->sdw_regmap
, reg
, (val
& 0xff));
274 dev_dbg(dev
, "[%s] %04x <= %04x\n", __func__
, reg
, val
);
275 else if (is_index_reg
)
276 dev_dbg(dev
, "[%s] %04x %04x %04x %04x <= %04x %04x\n",
277 __func__
, reg
, reg2
, reg3
, reg4
, val2
, val
);
279 dev_dbg(dev
, "[%s] %04x %04x <= %04x\n",
280 __func__
, reg
, reg2
, val
);
285 static const struct regmap_config rt700_regmap
= {
288 .readable_reg
= rt700_readable_register
,
289 .volatile_reg
= rt700_volatile_register
,
290 .max_register
= 0x755800,
291 .reg_defaults
= rt700_reg_defaults
,
292 .num_reg_defaults
= ARRAY_SIZE(rt700_reg_defaults
),
293 .cache_type
= REGCACHE_RBTREE
,
294 .use_single_read
= true,
295 .use_single_write
= true,
296 .reg_read
= rt700_sdw_read
,
297 .reg_write
= rt700_sdw_write
,
300 static const struct regmap_config rt700_sdw_regmap
= {
304 .readable_reg
= rt700_readable_register
,
305 .max_register
= 0xff01,
306 .cache_type
= REGCACHE_NONE
,
307 .use_single_read
= true,
308 .use_single_write
= true,
311 static int rt700_update_status(struct sdw_slave
*slave
,
312 enum sdw_slave_status status
)
314 struct rt700_priv
*rt700
= dev_get_drvdata(&slave
->dev
);
316 /* Update the status */
317 rt700
->status
= status
;
319 if (status
== SDW_SLAVE_UNATTACHED
)
320 rt700
->hw_init
= false;
323 * Perform initialization only if slave status is present and
324 * hw_init flag is false
326 if (rt700
->hw_init
|| rt700
->status
!= SDW_SLAVE_ATTACHED
)
329 /* perform I/O transfers required for Slave initialization */
330 return rt700_io_init(&slave
->dev
, slave
);
333 static int rt700_read_prop(struct sdw_slave
*slave
)
335 struct sdw_slave_prop
*prop
= &slave
->prop
;
336 int nval
, i
, num_of_ports
= 1;
339 struct sdw_dpn_prop
*dpn
;
341 prop
->paging_support
= false;
343 /* first we need to allocate memory for set bits in port lists */
344 prop
->source_ports
= 0x14; /* BITMAP: 00010100 */
345 prop
->sink_ports
= 0xA; /* BITMAP: 00001010 */
347 nval
= hweight32(prop
->source_ports
);
348 num_of_ports
+= nval
;
349 prop
->src_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
350 sizeof(*prop
->src_dpn_prop
),
352 if (!prop
->src_dpn_prop
)
356 dpn
= prop
->src_dpn_prop
;
357 addr
= prop
->source_ports
;
358 for_each_set_bit(bit
, &addr
, 32) {
360 dpn
[i
].type
= SDW_DPN_FULL
;
361 dpn
[i
].simple_ch_prep_sm
= true;
362 dpn
[i
].ch_prep_timeout
= 10;
366 /* do this again for sink now */
367 nval
= hweight32(prop
->sink_ports
);
368 num_of_ports
+= nval
;
369 prop
->sink_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
370 sizeof(*prop
->sink_dpn_prop
),
372 if (!prop
->sink_dpn_prop
)
376 dpn
= prop
->sink_dpn_prop
;
377 addr
= prop
->sink_ports
;
378 for_each_set_bit(bit
, &addr
, 32) {
380 dpn
[i
].type
= SDW_DPN_FULL
;
381 dpn
[i
].simple_ch_prep_sm
= true;
382 dpn
[i
].ch_prep_timeout
= 10;
386 /* Allocate port_ready based on num_of_ports */
387 slave
->port_ready
= devm_kcalloc(&slave
->dev
, num_of_ports
,
388 sizeof(*slave
->port_ready
),
390 if (!slave
->port_ready
)
393 /* Initialize completion */
394 for (i
= 0; i
< num_of_ports
; i
++)
395 init_completion(&slave
->port_ready
[i
]);
397 /* set the timeout values */
398 prop
->clk_stop_timeout
= 20;
401 prop
->wake_capable
= 1;
406 static int rt700_bus_config(struct sdw_slave
*slave
,
407 struct sdw_bus_params
*params
)
409 struct rt700_priv
*rt700
= dev_get_drvdata(&slave
->dev
);
412 memcpy(&rt700
->params
, params
, sizeof(*params
));
414 ret
= rt700_clock_config(&slave
->dev
);
416 dev_err(&slave
->dev
, "Invalid clk config");
421 static int rt700_interrupt_callback(struct sdw_slave
*slave
,
422 struct sdw_slave_intr_status
*status
)
424 struct rt700_priv
*rt700
= dev_get_drvdata(&slave
->dev
);
427 "%s control_port_stat=%x", __func__
, status
->control_port
);
429 if (status
->control_port
& 0x4) {
430 mod_delayed_work(system_power_efficient_wq
,
431 &rt700
->jack_detect_work
, msecs_to_jiffies(250));
438 * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
439 * port_prep are not defined for now
441 static struct sdw_slave_ops rt700_slave_ops
= {
442 .read_prop
= rt700_read_prop
,
443 .interrupt_callback
= rt700_interrupt_callback
,
444 .update_status
= rt700_update_status
,
445 .bus_config
= rt700_bus_config
,
448 static int rt700_sdw_probe(struct sdw_slave
*slave
,
449 const struct sdw_device_id
*id
)
451 struct regmap
*sdw_regmap
, *regmap
;
454 slave
->ops
= &rt700_slave_ops
;
456 /* Regmap Initialization */
457 sdw_regmap
= devm_regmap_init_sdw(slave
, &rt700_sdw_regmap
);
461 regmap
= devm_regmap_init(&slave
->dev
, NULL
,
462 &slave
->dev
, &rt700_regmap
);
464 return PTR_ERR(regmap
);
466 rt700_init(&slave
->dev
, sdw_regmap
, regmap
, slave
);
471 static int rt700_sdw_remove(struct sdw_slave
*slave
)
473 struct rt700_priv
*rt700
= dev_get_drvdata(&slave
->dev
);
475 if (rt700
&& rt700
->hw_init
) {
476 cancel_delayed_work(&rt700
->jack_detect_work
);
477 cancel_delayed_work(&rt700
->jack_btn_check_work
);
483 static const struct sdw_device_id rt700_id
[] = {
484 SDW_SLAVE_ENTRY(0x025d, 0x700, 0),
487 MODULE_DEVICE_TABLE(sdw
, rt700_id
);
489 static int rt700_dev_suspend(struct device
*dev
)
491 struct rt700_priv
*rt700
= dev_get_drvdata(dev
);
496 regcache_cache_only(rt700
->regmap
, true);
501 #define RT700_PROBE_TIMEOUT 2000
503 static int rt700_dev_resume(struct device
*dev
)
505 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
506 struct rt700_priv
*rt700
= dev_get_drvdata(dev
);
512 if (!slave
->unattach_request
)
515 time
= wait_for_completion_timeout(&slave
->initialization_complete
,
516 msecs_to_jiffies(RT700_PROBE_TIMEOUT
));
518 dev_err(&slave
->dev
, "Initialization not complete, timed out\n");
523 slave
->unattach_request
= 0;
524 regcache_cache_only(rt700
->regmap
, false);
525 regcache_sync_region(rt700
->regmap
, 0x3000, 0x8fff);
526 regcache_sync_region(rt700
->regmap
, 0x752010, 0x75206b);
531 static const struct dev_pm_ops rt700_pm
= {
532 SET_SYSTEM_SLEEP_PM_OPS(rt700_dev_suspend
, rt700_dev_resume
)
533 SET_RUNTIME_PM_OPS(rt700_dev_suspend
, rt700_dev_resume
, NULL
)
536 static struct sdw_driver rt700_sdw_driver
= {
539 .owner
= THIS_MODULE
,
542 .probe
= rt700_sdw_probe
,
543 .remove
= rt700_sdw_remove
,
544 .ops
= &rt700_slave_ops
,
545 .id_table
= rt700_id
,
547 module_sdw_driver(rt700_sdw_driver
);
549 MODULE_DESCRIPTION("ASoC RT700 driver SDW");
550 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
551 MODULE_LICENSE("GPL v2");