WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / codecs / rt5682-sdw.c
blob4d707e854875bd148e2eb5cc57148506ee594817
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt5682-sdw.c -- RT5682 ALSA SoC audio component driver
4 //
5 // Copyright 2019 Realtek Semiconductor Corp.
6 // Author: Oder Chiou <oder_chiou@realtek.com>
7 //
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/pm.h>
14 #include <linux/acpi.h>
15 #include <linux/gpio.h>
16 #include <linux/of_gpio.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/mutex.h>
20 #include <linux/soundwire/sdw.h>
21 #include <linux/soundwire/sdw_type.h>
22 #include <linux/soundwire/sdw_registers.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/jack.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
32 #include "rt5682.h"
34 #define RT5682_SDW_ADDR_L 0x3000
35 #define RT5682_SDW_ADDR_H 0x3001
36 #define RT5682_SDW_DATA_L 0x3004
37 #define RT5682_SDW_DATA_H 0x3005
38 #define RT5682_SDW_CMD 0x3008
40 static int rt5682_sdw_read(void *context, unsigned int reg, unsigned int *val)
42 struct device *dev = context;
43 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
44 unsigned int data_l, data_h;
46 regmap_write(rt5682->sdw_regmap, RT5682_SDW_CMD, 0);
47 regmap_write(rt5682->sdw_regmap, RT5682_SDW_ADDR_H, (reg >> 8) & 0xff);
48 regmap_write(rt5682->sdw_regmap, RT5682_SDW_ADDR_L, (reg & 0xff));
49 regmap_read(rt5682->sdw_regmap, RT5682_SDW_DATA_H, &data_h);
50 regmap_read(rt5682->sdw_regmap, RT5682_SDW_DATA_L, &data_l);
52 *val = (data_h << 8) | data_l;
54 dev_vdbg(dev, "[%s] %04x => %04x\n", __func__, reg, *val);
56 return 0;
59 static int rt5682_sdw_write(void *context, unsigned int reg, unsigned int val)
61 struct device *dev = context;
62 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
64 regmap_write(rt5682->sdw_regmap, RT5682_SDW_CMD, 1);
65 regmap_write(rt5682->sdw_regmap, RT5682_SDW_ADDR_H, (reg >> 8) & 0xff);
66 regmap_write(rt5682->sdw_regmap, RT5682_SDW_ADDR_L, (reg & 0xff));
67 regmap_write(rt5682->sdw_regmap, RT5682_SDW_DATA_H, (val >> 8) & 0xff);
68 regmap_write(rt5682->sdw_regmap, RT5682_SDW_DATA_L, (val & 0xff));
70 dev_vdbg(dev, "[%s] %04x <= %04x\n", __func__, reg, val);
72 return 0;
75 static const struct regmap_config rt5682_sdw_indirect_regmap = {
76 .reg_bits = 16,
77 .val_bits = 16,
78 .max_register = RT5682_I2C_MODE,
79 .volatile_reg = rt5682_volatile_register,
80 .readable_reg = rt5682_readable_register,
81 .cache_type = REGCACHE_RBTREE,
82 .reg_defaults = rt5682_reg,
83 .num_reg_defaults = RT5682_REG_NUM,
84 .use_single_read = true,
85 .use_single_write = true,
86 .reg_read = rt5682_sdw_read,
87 .reg_write = rt5682_sdw_write,
90 struct sdw_stream_data {
91 struct sdw_stream_runtime *sdw_stream;
94 static int rt5682_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
95 int direction)
97 struct sdw_stream_data *stream;
99 if (!sdw_stream)
100 return 0;
102 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
103 if (!stream)
104 return -ENOMEM;
106 stream->sdw_stream = sdw_stream;
108 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
109 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
110 dai->playback_dma_data = stream;
111 else
112 dai->capture_dma_data = stream;
114 return 0;
117 static void rt5682_sdw_shutdown(struct snd_pcm_substream *substream,
118 struct snd_soc_dai *dai)
120 struct sdw_stream_data *stream;
122 stream = snd_soc_dai_get_dma_data(dai, substream);
123 snd_soc_dai_set_dma_data(dai, substream, NULL);
124 kfree(stream);
127 static int rt5682_sdw_hw_params(struct snd_pcm_substream *substream,
128 struct snd_pcm_hw_params *params,
129 struct snd_soc_dai *dai)
131 struct snd_soc_component *component = dai->component;
132 struct rt5682_priv *rt5682 = snd_soc_component_get_drvdata(component);
133 struct sdw_stream_config stream_config;
134 struct sdw_port_config port_config;
135 enum sdw_data_direction direction;
136 struct sdw_stream_data *stream;
137 int retval, port, num_channels;
138 unsigned int val_p = 0, val_c = 0, osr_p = 0, osr_c = 0;
140 dev_dbg(dai->dev, "%s %s", __func__, dai->name);
142 stream = snd_soc_dai_get_dma_data(dai, substream);
143 if (!stream)
144 return -ENOMEM;
146 if (!rt5682->slave)
147 return -EINVAL;
149 /* SoundWire specific configuration */
150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
151 direction = SDW_DATA_DIR_RX;
152 port = 1;
153 } else {
154 direction = SDW_DATA_DIR_TX;
155 port = 2;
158 stream_config.frame_rate = params_rate(params);
159 stream_config.ch_count = params_channels(params);
160 stream_config.bps = snd_pcm_format_width(params_format(params));
161 stream_config.direction = direction;
163 num_channels = params_channels(params);
164 port_config.ch_mask = (1 << (num_channels)) - 1;
165 port_config.num = port;
167 retval = sdw_stream_add_slave(rt5682->slave, &stream_config,
168 &port_config, 1, stream->sdw_stream);
169 if (retval) {
170 dev_err(dai->dev, "Unable to configure port\n");
171 return retval;
174 switch (params_rate(params)) {
175 case 48000:
176 val_p = RT5682_SDW_REF_1_48K;
177 val_c = RT5682_SDW_REF_2_48K;
178 break;
179 case 96000:
180 val_p = RT5682_SDW_REF_1_96K;
181 val_c = RT5682_SDW_REF_2_96K;
182 break;
183 case 192000:
184 val_p = RT5682_SDW_REF_1_192K;
185 val_c = RT5682_SDW_REF_2_192K;
186 break;
187 case 32000:
188 val_p = RT5682_SDW_REF_1_32K;
189 val_c = RT5682_SDW_REF_2_32K;
190 break;
191 case 24000:
192 val_p = RT5682_SDW_REF_1_24K;
193 val_c = RT5682_SDW_REF_2_24K;
194 break;
195 case 16000:
196 val_p = RT5682_SDW_REF_1_16K;
197 val_c = RT5682_SDW_REF_2_16K;
198 break;
199 case 12000:
200 val_p = RT5682_SDW_REF_1_12K;
201 val_c = RT5682_SDW_REF_2_12K;
202 break;
203 case 8000:
204 val_p = RT5682_SDW_REF_1_8K;
205 val_c = RT5682_SDW_REF_2_8K;
206 break;
207 case 44100:
208 val_p = RT5682_SDW_REF_1_44K;
209 val_c = RT5682_SDW_REF_2_44K;
210 break;
211 case 88200:
212 val_p = RT5682_SDW_REF_1_88K;
213 val_c = RT5682_SDW_REF_2_88K;
214 break;
215 case 176400:
216 val_p = RT5682_SDW_REF_1_176K;
217 val_c = RT5682_SDW_REF_2_176K;
218 break;
219 case 22050:
220 val_p = RT5682_SDW_REF_1_22K;
221 val_c = RT5682_SDW_REF_2_22K;
222 break;
223 case 11025:
224 val_p = RT5682_SDW_REF_1_11K;
225 val_c = RT5682_SDW_REF_2_11K;
226 break;
227 default:
228 return -EINVAL;
231 if (params_rate(params) <= 48000) {
232 osr_p = RT5682_DAC_OSR_D_8;
233 osr_c = RT5682_ADC_OSR_D_8;
234 } else if (params_rate(params) <= 96000) {
235 osr_p = RT5682_DAC_OSR_D_4;
236 osr_c = RT5682_ADC_OSR_D_4;
237 } else {
238 osr_p = RT5682_DAC_OSR_D_2;
239 osr_c = RT5682_ADC_OSR_D_2;
242 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
243 regmap_update_bits(rt5682->regmap, RT5682_SDW_REF_CLK,
244 RT5682_SDW_REF_1_MASK, val_p);
245 regmap_update_bits(rt5682->regmap, RT5682_ADDA_CLK_1,
246 RT5682_DAC_OSR_MASK, osr_p);
247 } else {
248 regmap_update_bits(rt5682->regmap, RT5682_SDW_REF_CLK,
249 RT5682_SDW_REF_2_MASK, val_c);
250 regmap_update_bits(rt5682->regmap, RT5682_ADDA_CLK_1,
251 RT5682_ADC_OSR_MASK, osr_c);
254 return retval;
257 static int rt5682_sdw_hw_free(struct snd_pcm_substream *substream,
258 struct snd_soc_dai *dai)
260 struct snd_soc_component *component = dai->component;
261 struct rt5682_priv *rt5682 = snd_soc_component_get_drvdata(component);
262 struct sdw_stream_data *stream =
263 snd_soc_dai_get_dma_data(dai, substream);
265 if (!rt5682->slave)
266 return -EINVAL;
268 sdw_stream_remove_slave(rt5682->slave, stream->sdw_stream);
269 return 0;
272 static struct snd_soc_dai_ops rt5682_sdw_ops = {
273 .hw_params = rt5682_sdw_hw_params,
274 .hw_free = rt5682_sdw_hw_free,
275 .set_sdw_stream = rt5682_set_sdw_stream,
276 .shutdown = rt5682_sdw_shutdown,
279 static struct snd_soc_dai_driver rt5682_dai[] = {
281 .name = "rt5682-aif1",
282 .id = RT5682_AIF1,
283 .playback = {
284 .stream_name = "AIF1 Playback",
285 .channels_min = 1,
286 .channels_max = 2,
287 .rates = RT5682_STEREO_RATES,
288 .formats = RT5682_FORMATS,
290 .capture = {
291 .stream_name = "AIF1 Capture",
292 .channels_min = 1,
293 .channels_max = 2,
294 .rates = RT5682_STEREO_RATES,
295 .formats = RT5682_FORMATS,
297 .ops = &rt5682_aif1_dai_ops,
300 .name = "rt5682-aif2",
301 .id = RT5682_AIF2,
302 .capture = {
303 .stream_name = "AIF2 Capture",
304 .channels_min = 1,
305 .channels_max = 2,
306 .rates = RT5682_STEREO_RATES,
307 .formats = RT5682_FORMATS,
309 .ops = &rt5682_aif2_dai_ops,
312 .name = "rt5682-sdw",
313 .id = RT5682_SDW,
314 .playback = {
315 .stream_name = "SDW Playback",
316 .channels_min = 1,
317 .channels_max = 2,
318 .rates = RT5682_STEREO_RATES,
319 .formats = RT5682_FORMATS,
321 .capture = {
322 .stream_name = "SDW Capture",
323 .channels_min = 1,
324 .channels_max = 2,
325 .rates = RT5682_STEREO_RATES,
326 .formats = RT5682_FORMATS,
328 .ops = &rt5682_sdw_ops,
332 static int rt5682_sdw_init(struct device *dev, struct regmap *regmap,
333 struct sdw_slave *slave)
335 struct rt5682_priv *rt5682;
336 int ret;
338 rt5682 = devm_kzalloc(dev, sizeof(*rt5682), GFP_KERNEL);
339 if (!rt5682)
340 return -ENOMEM;
342 dev_set_drvdata(dev, rt5682);
343 rt5682->slave = slave;
344 rt5682->sdw_regmap = regmap;
345 rt5682->is_sdw = true;
347 rt5682->regmap = devm_regmap_init(dev, NULL, dev,
348 &rt5682_sdw_indirect_regmap);
349 if (IS_ERR(rt5682->regmap)) {
350 ret = PTR_ERR(rt5682->regmap);
351 dev_err(dev, "Failed to allocate register map: %d\n",
352 ret);
353 return ret;
357 * Mark hw_init to false
358 * HW init will be performed when device reports present
360 rt5682->hw_init = false;
361 rt5682->first_hw_init = false;
363 mutex_init(&rt5682->calibrate_mutex);
364 INIT_DELAYED_WORK(&rt5682->jack_detect_work,
365 rt5682_jack_detect_handler);
367 ret = devm_snd_soc_register_component(dev,
368 &rt5682_soc_component_dev,
369 rt5682_dai, ARRAY_SIZE(rt5682_dai));
370 dev_dbg(&slave->dev, "%s\n", __func__);
372 return ret;
375 static int rt5682_io_init(struct device *dev, struct sdw_slave *slave)
377 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
378 int ret = 0;
379 unsigned int val;
381 if (rt5682->hw_init)
382 return 0;
384 regmap_read(rt5682->regmap, RT5682_DEVICE_ID, &val);
385 if (val != DEVICE_ID) {
386 dev_err(dev, "Device with ID register %x is not rt5682\n", val);
387 return -ENODEV;
391 * PM runtime is only enabled when a Slave reports as Attached
393 if (!rt5682->first_hw_init) {
394 /* set autosuspend parameters */
395 pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
396 pm_runtime_use_autosuspend(&slave->dev);
398 /* update count of parent 'active' children */
399 pm_runtime_set_active(&slave->dev);
401 /* make sure the device does not suspend immediately */
402 pm_runtime_mark_last_busy(&slave->dev);
404 pm_runtime_enable(&slave->dev);
407 pm_runtime_get_noresume(&slave->dev);
409 if (rt5682->first_hw_init) {
410 regcache_cache_only(rt5682->regmap, false);
411 regcache_cache_bypass(rt5682->regmap, true);
414 rt5682_calibrate(rt5682);
416 if (rt5682->first_hw_init) {
417 regcache_cache_bypass(rt5682->regmap, false);
418 regcache_mark_dirty(rt5682->regmap);
419 regcache_sync(rt5682->regmap);
421 /* volatile registers */
422 regmap_update_bits(rt5682->regmap, RT5682_CBJ_CTRL_2,
423 RT5682_EXT_JD_SRC, RT5682_EXT_JD_SRC_MANUAL);
425 goto reinit;
428 rt5682_apply_patch_list(rt5682, dev);
430 regmap_write(rt5682->regmap, RT5682_DEPOP_1, 0x0000);
432 regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_1,
433 RT5682_LDO1_DVO_MASK | RT5682_HP_DRIVER_MASK,
434 RT5682_LDO1_DVO_12 | RT5682_HP_DRIVER_5X);
435 regmap_write(rt5682->regmap, RT5682_MICBIAS_2, 0x0080);
436 regmap_write(rt5682->regmap, RT5682_TEST_MODE_CTRL_1, 0x0000);
437 regmap_update_bits(rt5682->regmap, RT5682_BIAS_CUR_CTRL_8,
438 RT5682_HPA_CP_BIAS_CTRL_MASK, RT5682_HPA_CP_BIAS_3UA);
439 regmap_update_bits(rt5682->regmap, RT5682_CHARGE_PUMP_1,
440 RT5682_CP_CLK_HP_MASK, RT5682_CP_CLK_HP_300KHZ);
441 regmap_update_bits(rt5682->regmap, RT5682_HP_CHARGE_PUMP_1,
442 RT5682_PM_HP_MASK, RT5682_PM_HP_HV);
444 /* Soundwire */
445 regmap_write(rt5682->regmap, RT5682_PLL2_INTERNAL, 0xa266);
446 regmap_write(rt5682->regmap, RT5682_PLL2_CTRL_1, 0x1700);
447 regmap_write(rt5682->regmap, RT5682_PLL2_CTRL_2, 0x0006);
448 regmap_write(rt5682->regmap, RT5682_PLL2_CTRL_3, 0x2600);
449 regmap_write(rt5682->regmap, RT5682_PLL2_CTRL_4, 0x0c8f);
450 regmap_write(rt5682->regmap, RT5682_PLL_TRACK_2, 0x3000);
451 regmap_write(rt5682->regmap, RT5682_PLL_TRACK_3, 0x4000);
452 regmap_update_bits(rt5682->regmap, RT5682_GLB_CLK,
453 RT5682_SCLK_SRC_MASK | RT5682_PLL2_SRC_MASK,
454 RT5682_SCLK_SRC_PLL2 | RT5682_PLL2_SRC_SDW);
456 regmap_update_bits(rt5682->regmap, RT5682_CBJ_CTRL_2,
457 RT5682_EXT_JD_SRC, RT5682_EXT_JD_SRC_MANUAL);
458 regmap_write(rt5682->regmap, RT5682_CBJ_CTRL_1, 0xd042);
459 regmap_update_bits(rt5682->regmap, RT5682_CBJ_CTRL_3,
460 RT5682_CBJ_IN_BUF_EN, RT5682_CBJ_IN_BUF_EN);
461 regmap_update_bits(rt5682->regmap, RT5682_SAR_IL_CMD_1,
462 RT5682_SAR_POW_MASK, RT5682_SAR_POW_EN);
463 regmap_update_bits(rt5682->regmap, RT5682_RC_CLK_CTRL,
464 RT5682_POW_IRQ | RT5682_POW_JDH |
465 RT5682_POW_ANA, RT5682_POW_IRQ |
466 RT5682_POW_JDH | RT5682_POW_ANA);
467 regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_2,
468 RT5682_PWR_JDH, RT5682_PWR_JDH);
469 regmap_update_bits(rt5682->regmap, RT5682_IRQ_CTRL_2,
470 RT5682_JD1_EN_MASK | RT5682_JD1_IRQ_MASK,
471 RT5682_JD1_EN | RT5682_JD1_IRQ_PUL);
473 reinit:
474 mod_delayed_work(system_power_efficient_wq,
475 &rt5682->jack_detect_work, msecs_to_jiffies(250));
477 /* Mark Slave initialization complete */
478 rt5682->hw_init = true;
479 rt5682->first_hw_init = true;
481 pm_runtime_mark_last_busy(&slave->dev);
482 pm_runtime_put_autosuspend(&slave->dev);
484 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
486 return ret;
489 static bool rt5682_sdw_readable_register(struct device *dev, unsigned int reg)
491 switch (reg) {
492 case 0x00e0:
493 case 0x00f0:
494 case 0x3000:
495 case 0x3001:
496 case 0x3004:
497 case 0x3005:
498 case 0x3008:
499 return true;
500 default:
501 return false;
505 static const struct regmap_config rt5682_sdw_regmap = {
506 .name = "sdw",
507 .reg_bits = 32,
508 .val_bits = 8,
509 .max_register = RT5682_I2C_MODE,
510 .readable_reg = rt5682_sdw_readable_register,
511 .cache_type = REGCACHE_NONE,
512 .use_single_read = true,
513 .use_single_write = true,
516 static int rt5682_update_status(struct sdw_slave *slave,
517 enum sdw_slave_status status)
519 struct rt5682_priv *rt5682 = dev_get_drvdata(&slave->dev);
521 /* Update the status */
522 rt5682->status = status;
524 if (status == SDW_SLAVE_UNATTACHED)
525 rt5682->hw_init = false;
528 * Perform initialization only if slave status is present and
529 * hw_init flag is false
531 if (rt5682->hw_init || rt5682->status != SDW_SLAVE_ATTACHED)
532 return 0;
534 /* perform I/O transfers required for Slave initialization */
535 return rt5682_io_init(&slave->dev, slave);
538 static int rt5682_read_prop(struct sdw_slave *slave)
540 struct sdw_slave_prop *prop = &slave->prop;
541 int nval, i;
542 u32 bit;
543 unsigned long addr;
544 struct sdw_dpn_prop *dpn;
546 prop->scp_int1_mask = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
547 SDW_SCP_INT1_PARITY;
548 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
550 prop->paging_support = false;
552 /* first we need to allocate memory for set bits in port lists */
553 prop->source_ports = 0x4; /* BITMAP: 00000100 */
554 prop->sink_ports = 0x2; /* BITMAP: 00000010 */
556 nval = hweight32(prop->source_ports);
557 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
558 sizeof(*prop->src_dpn_prop),
559 GFP_KERNEL);
560 if (!prop->src_dpn_prop)
561 return -ENOMEM;
563 i = 0;
564 dpn = prop->src_dpn_prop;
565 addr = prop->source_ports;
566 for_each_set_bit(bit, &addr, 32) {
567 dpn[i].num = bit;
568 dpn[i].type = SDW_DPN_FULL;
569 dpn[i].simple_ch_prep_sm = true;
570 dpn[i].ch_prep_timeout = 10;
571 i++;
574 /* do this again for sink now */
575 nval = hweight32(prop->sink_ports);
576 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
577 sizeof(*prop->sink_dpn_prop),
578 GFP_KERNEL);
579 if (!prop->sink_dpn_prop)
580 return -ENOMEM;
582 i = 0;
583 dpn = prop->sink_dpn_prop;
584 addr = prop->sink_ports;
585 for_each_set_bit(bit, &addr, 32) {
586 dpn[i].num = bit;
587 dpn[i].type = SDW_DPN_FULL;
588 dpn[i].simple_ch_prep_sm = true;
589 dpn[i].ch_prep_timeout = 10;
590 i++;
593 /* set the timeout values */
594 prop->clk_stop_timeout = 20;
596 /* wake-up event */
597 prop->wake_capable = 1;
599 return 0;
602 /* Bus clock frequency */
603 #define RT5682_CLK_FREQ_9600000HZ 9600000
604 #define RT5682_CLK_FREQ_12000000HZ 12000000
605 #define RT5682_CLK_FREQ_6000000HZ 6000000
606 #define RT5682_CLK_FREQ_4800000HZ 4800000
607 #define RT5682_CLK_FREQ_2400000HZ 2400000
608 #define RT5682_CLK_FREQ_12288000HZ 12288000
610 static int rt5682_clock_config(struct device *dev)
612 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
613 unsigned int clk_freq, value;
615 clk_freq = (rt5682->params.curr_dr_freq >> 1);
617 switch (clk_freq) {
618 case RT5682_CLK_FREQ_12000000HZ:
619 value = 0x0;
620 break;
621 case RT5682_CLK_FREQ_6000000HZ:
622 value = 0x1;
623 break;
624 case RT5682_CLK_FREQ_9600000HZ:
625 value = 0x2;
626 break;
627 case RT5682_CLK_FREQ_4800000HZ:
628 value = 0x3;
629 break;
630 case RT5682_CLK_FREQ_2400000HZ:
631 value = 0x4;
632 break;
633 case RT5682_CLK_FREQ_12288000HZ:
634 value = 0x5;
635 break;
636 default:
637 return -EINVAL;
640 regmap_write(rt5682->sdw_regmap, 0xe0, value);
641 regmap_write(rt5682->sdw_regmap, 0xf0, value);
643 dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
645 return 0;
648 static int rt5682_bus_config(struct sdw_slave *slave,
649 struct sdw_bus_params *params)
651 struct rt5682_priv *rt5682 = dev_get_drvdata(&slave->dev);
652 int ret;
654 memcpy(&rt5682->params, params, sizeof(*params));
656 ret = rt5682_clock_config(&slave->dev);
657 if (ret < 0)
658 dev_err(&slave->dev, "Invalid clk config");
660 return ret;
663 static int rt5682_interrupt_callback(struct sdw_slave *slave,
664 struct sdw_slave_intr_status *status)
666 struct rt5682_priv *rt5682 = dev_get_drvdata(&slave->dev);
668 dev_dbg(&slave->dev,
669 "%s control_port_stat=%x", __func__, status->control_port);
671 if (status->control_port & 0x4) {
672 mod_delayed_work(system_power_efficient_wq,
673 &rt5682->jack_detect_work, msecs_to_jiffies(250));
676 return 0;
679 static struct sdw_slave_ops rt5682_slave_ops = {
680 .read_prop = rt5682_read_prop,
681 .interrupt_callback = rt5682_interrupt_callback,
682 .update_status = rt5682_update_status,
683 .bus_config = rt5682_bus_config,
686 static int rt5682_sdw_probe(struct sdw_slave *slave,
687 const struct sdw_device_id *id)
689 struct regmap *regmap;
691 /* Regmap Initialization */
692 regmap = devm_regmap_init_sdw(slave, &rt5682_sdw_regmap);
693 if (IS_ERR(regmap))
694 return -EINVAL;
696 rt5682_sdw_init(&slave->dev, regmap, slave);
698 return 0;
701 static int rt5682_sdw_remove(struct sdw_slave *slave)
703 struct rt5682_priv *rt5682 = dev_get_drvdata(&slave->dev);
705 if (rt5682 && rt5682->hw_init)
706 cancel_delayed_work(&rt5682->jack_detect_work);
708 return 0;
711 static const struct sdw_device_id rt5682_id[] = {
712 SDW_SLAVE_ENTRY_EXT(0x025d, 0x5682, 0x2, 0, 0),
715 MODULE_DEVICE_TABLE(sdw, rt5682_id);
717 static int __maybe_unused rt5682_dev_suspend(struct device *dev)
719 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
721 if (!rt5682->hw_init)
722 return 0;
724 regcache_cache_only(rt5682->regmap, true);
725 regcache_mark_dirty(rt5682->regmap);
727 return 0;
730 static int __maybe_unused rt5682_dev_resume(struct device *dev)
732 struct sdw_slave *slave = dev_to_sdw_dev(dev);
733 struct rt5682_priv *rt5682 = dev_get_drvdata(dev);
734 unsigned long time;
736 if (!rt5682->hw_init)
737 return 0;
739 if (!slave->unattach_request)
740 goto regmap_sync;
742 time = wait_for_completion_timeout(&slave->initialization_complete,
743 msecs_to_jiffies(RT5682_PROBE_TIMEOUT));
744 if (!time) {
745 dev_err(&slave->dev, "Initialization not complete, timed out\n");
746 return -ETIMEDOUT;
749 regmap_sync:
750 slave->unattach_request = 0;
751 regcache_cache_only(rt5682->regmap, false);
752 regcache_sync(rt5682->regmap);
754 return 0;
757 static const struct dev_pm_ops rt5682_pm = {
758 SET_SYSTEM_SLEEP_PM_OPS(rt5682_dev_suspend, rt5682_dev_resume)
759 SET_RUNTIME_PM_OPS(rt5682_dev_suspend, rt5682_dev_resume, NULL)
762 static struct sdw_driver rt5682_sdw_driver = {
763 .driver = {
764 .name = "rt5682",
765 .owner = THIS_MODULE,
766 .pm = &rt5682_pm,
768 .probe = rt5682_sdw_probe,
769 .remove = rt5682_sdw_remove,
770 .ops = &rt5682_slave_ops,
771 .id_table = rt5682_id,
773 module_sdw_driver(rt5682_sdw_driver);
775 MODULE_DESCRIPTION("ASoC RT5682 driver SDW");
776 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
777 MODULE_LICENSE("GPL v2");