1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2022, Analog Devices Inc.
4 #include <linux/module.h>
5 #include <linux/pm_runtime.h>
6 #include <linux/regmap.h>
7 #include <linux/soundwire/sdw.h>
8 #include <linux/soundwire/sdw_registers.h>
9 #include <linux/soundwire/sdw_type.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/tlv.h>
17 static struct reg_default max98363_reg
[] = {
18 {MAX98363_R2021_ERR_MON_CTRL
, 0x0},
19 {MAX98363_R2022_SPK_MON_THRESH
, 0x0},
20 {MAX98363_R2023_SPK_MON_DURATION
, 0x0},
21 {MAX98363_R2030_TONE_GEN_CFG
, 0x0},
22 {MAX98363_R203F_TONE_GEN_EN
, 0x0},
23 {MAX98363_R2040_AMP_VOL
, 0x0},
24 {MAX98363_R2041_AMP_GAIN
, 0x5},
25 {MAX98363_R2042_DSP_CFG
, 0x0},
28 static bool max98363_readable_register(struct device
*dev
, unsigned int reg
)
31 case MAX98363_R2001_INTR_RAW
:
32 case MAX98363_R2003_INTR_STATE
:
33 case MAX98363_R2005_INTR_FALG
:
34 case MAX98363_R2007_INTR_EN
:
35 case MAX98363_R2009_INTR_CLR
:
36 case MAX98363_R2021_ERR_MON_CTRL
... MAX98363_R2023_SPK_MON_DURATION
:
37 case MAX98363_R2030_TONE_GEN_CFG
:
38 case MAX98363_R203F_TONE_GEN_EN
:
39 case MAX98363_R2040_AMP_VOL
:
40 case MAX98363_R2041_AMP_GAIN
:
41 case MAX98363_R2042_DSP_CFG
:
42 case MAX98363_R21FF_REV_ID
:
49 static bool max98363_volatile_reg(struct device
*dev
, unsigned int reg
)
52 case MAX98363_R2001_INTR_RAW
:
53 case MAX98363_R2003_INTR_STATE
:
54 case MAX98363_R2005_INTR_FALG
:
55 case MAX98363_R2007_INTR_EN
:
56 case MAX98363_R2009_INTR_CLR
:
57 case MAX98363_R21FF_REV_ID
:
64 static const struct regmap_config max98363_sdw_regmap
= {
67 .max_register
= MAX98363_R21FF_REV_ID
,
68 .reg_defaults
= max98363_reg
,
69 .num_reg_defaults
= ARRAY_SIZE(max98363_reg
),
70 .readable_reg
= max98363_readable_register
,
71 .volatile_reg
= max98363_volatile_reg
,
72 .cache_type
= REGCACHE_RBTREE
,
73 .use_single_read
= true,
74 .use_single_write
= true,
77 static int max98363_suspend(struct device
*dev
)
79 struct max98363_priv
*max98363
= dev_get_drvdata(dev
);
81 regcache_cache_only(max98363
->regmap
, true);
82 regcache_mark_dirty(max98363
->regmap
);
87 #define MAX98363_PROBE_TIMEOUT 5000
89 static int max98363_resume(struct device
*dev
)
91 struct sdw_slave
*slave
= dev_to_sdw_dev(dev
);
92 struct max98363_priv
*max98363
= dev_get_drvdata(dev
);
95 if (!max98363
->first_hw_init
)
98 if (!slave
->unattach_request
)
101 time
= wait_for_completion_timeout(&slave
->initialization_complete
,
102 msecs_to_jiffies(MAX98363_PROBE_TIMEOUT
));
104 dev_err(dev
, "Initialization not complete, timed out\n");
110 slave
->unattach_request
= 0;
111 regcache_cache_only(max98363
->regmap
, false);
112 regcache_sync(max98363
->regmap
);
117 static DEFINE_RUNTIME_DEV_PM_OPS(max98363_pm
, max98363_suspend
, max98363_resume
, NULL
);
119 static int max98363_read_prop(struct sdw_slave
*slave
)
121 struct sdw_slave_prop
*prop
= &slave
->prop
;
125 struct sdw_dpn_prop
*dpn
;
127 prop
->scp_int1_mask
= SDW_SCP_INT1_BUS_CLASH
| SDW_SCP_INT1_PARITY
;
129 /* BITMAP: 00000010 Dataport 1 is active */
130 prop
->sink_ports
= BIT(1);
131 prop
->paging_support
= true;
132 prop
->clk_stop_timeout
= 20;
133 prop
->simple_clk_stop_capable
= true;
134 prop
->clock_reg_supported
= true;
136 nval
= hweight32(prop
->sink_ports
);
137 prop
->sink_dpn_prop
= devm_kcalloc(&slave
->dev
, nval
,
138 sizeof(*prop
->sink_dpn_prop
),
140 if (!prop
->sink_dpn_prop
)
144 dpn
= prop
->sink_dpn_prop
;
145 addr
= prop
->sink_ports
;
146 for_each_set_bit(bit
, &addr
, 32) {
148 dpn
[i
].type
= SDW_DPN_FULL
;
149 dpn
[i
].simple_ch_prep_sm
= true;
150 dpn
[i
].ch_prep_timeout
= 10;
157 static int max98363_io_init(struct sdw_slave
*slave
)
159 struct device
*dev
= &slave
->dev
;
160 struct max98363_priv
*max98363
= dev_get_drvdata(dev
);
163 regcache_cache_only(max98363
->regmap
, false);
164 if (max98363
->first_hw_init
)
165 regcache_cache_bypass(max98363
->regmap
, true);
168 * PM runtime status is marked as 'active' only when a Slave reports as Attached
170 if (!max98363
->first_hw_init
)
171 /* update count of parent 'active' children */
172 pm_runtime_set_active(dev
);
174 pm_runtime_get_noresume(dev
);
176 ret
= regmap_read(max98363
->regmap
, MAX98363_R21FF_REV_ID
, ®
);
178 dev_info(dev
, "Revision ID: %X\n", reg
);
182 if (max98363
->first_hw_init
) {
183 regcache_cache_bypass(max98363
->regmap
, false);
184 regcache_mark_dirty(max98363
->regmap
);
187 max98363
->first_hw_init
= true;
188 max98363
->hw_init
= true;
191 pm_runtime_mark_last_busy(dev
);
192 pm_runtime_put_autosuspend(dev
);
197 #define MAX98363_RATES SNDRV_PCM_RATE_8000_192000
198 #define MAX98363_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
200 static int max98363_sdw_dai_hw_params(struct snd_pcm_substream
*substream
,
201 struct snd_pcm_hw_params
*params
,
202 struct snd_soc_dai
*dai
)
204 struct snd_soc_component
*component
= dai
->component
;
205 struct max98363_priv
*max98363
=
206 snd_soc_component_get_drvdata(component
);
208 struct sdw_stream_config stream_config
;
209 struct sdw_port_config port_config
;
210 enum sdw_data_direction direction
;
211 struct sdw_stream_runtime
*stream
;
212 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
216 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
221 if (!max98363
->slave
)
224 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
227 direction
= SDW_DATA_DIR_RX
;
230 stream_config
.frame_rate
= params_rate(params
);
231 stream_config
.bps
= snd_pcm_format_width(params_format(params
));
232 stream_config
.direction
= direction
;
233 stream_config
.ch_count
= 1;
235 if (stream_config
.ch_count
> runtime
->hw
.channels_max
) {
236 stream_config
.ch_count
= runtime
->hw
.channels_max
;
237 dev_info(dai
->dev
, "Number of channels: %d (requested: %d)\n",
238 stream_config
.ch_count
, params_channels(params
));
240 port_config
.ch_mask
= GENMASK((int)stream_config
.ch_count
- 1, 0);
242 ret
= sdw_stream_add_slave(max98363
->slave
, &stream_config
,
243 &port_config
, 1, stream
);
245 dev_err(dai
->dev
, "Unable to configure port\n");
249 dev_dbg(component
->dev
, "Format supported %d", params_format(params
));
254 static int max98363_pcm_hw_free(struct snd_pcm_substream
*substream
,
255 struct snd_soc_dai
*dai
)
257 struct snd_soc_component
*component
= dai
->component
;
258 struct max98363_priv
*max98363
=
259 snd_soc_component_get_drvdata(component
);
260 struct sdw_stream_runtime
*stream
=
261 snd_soc_dai_get_dma_data(dai
, substream
);
263 if (!max98363
->slave
)
266 sdw_stream_remove_slave(max98363
->slave
, stream
);
271 static int max98363_set_sdw_stream(struct snd_soc_dai
*dai
,
272 void *sdw_stream
, int direction
)
274 snd_soc_dai_dma_data_set(dai
, direction
, sdw_stream
);
279 static const struct snd_soc_dai_ops max98363_dai_sdw_ops
= {
280 .hw_params
= max98363_sdw_dai_hw_params
,
281 .hw_free
= max98363_pcm_hw_free
,
282 .set_stream
= max98363_set_sdw_stream
,
285 static struct snd_soc_dai_driver max98363_dai
[] = {
287 .name
= "max98363-aif1",
289 .stream_name
= "HiFi Playback",
292 .rates
= MAX98363_RATES
,
293 .formats
= MAX98363_FORMATS
,
295 .ops
= &max98363_dai_sdw_ops
,
299 static int max98363_update_status(struct sdw_slave
*slave
,
300 enum sdw_slave_status status
)
302 struct max98363_priv
*max98363
= dev_get_drvdata(&slave
->dev
);
304 if (status
== SDW_SLAVE_UNATTACHED
)
305 max98363
->hw_init
= false;
308 * Perform initialization only if slave status is SDW_SLAVE_ATTACHED
310 if (max98363
->hw_init
|| status
!= SDW_SLAVE_ATTACHED
)
313 /* perform I/O transfers required for Slave initialization */
314 return max98363_io_init(slave
);
317 static const struct sdw_slave_ops max98363_slave_ops
= {
318 .read_prop
= max98363_read_prop
,
319 .update_status
= max98363_update_status
,
322 static DECLARE_TLV_DB_SCALE(max98363_digital_tlv
, -6350, 50, 1);
323 static const DECLARE_TLV_DB_RANGE(max98363_spk_tlv
,
324 0, 5, TLV_DB_SCALE_ITEM(-300, 300, 0),
327 static const char * const max98363_tone_cfg_text
[] = {
328 "Reserved", "0", "+FS/2", "-FS/2", "1KHz",
329 "12KHz", "8KHz", "6KHz", "4KHz", "3KHz",
330 "2KHz", "1.5KHz", "Reserved", "500Hz", "250Hz"
333 static SOC_ENUM_SINGLE_DECL(max98363_tone_cfg_enum
,
334 MAX98363_R2030_TONE_GEN_CFG
, 0,
335 max98363_tone_cfg_text
);
337 static const char * const max98363_spkmon_duration_text
[] = {
338 "8ms", "20ms", "40ms", "60ms",
339 "80ms", "160ms", "240ms", "320ms",
340 "400ms", "480ms", "560ms", "640ms",
341 "720ms", "800ms", "880ms", "960ms"
344 static SOC_ENUM_SINGLE_DECL(max98363_spkmon_duration_enum
,
345 MAX98363_R2023_SPK_MON_DURATION
, 0,
346 max98363_spkmon_duration_text
);
348 static const struct snd_kcontrol_new max98363_snd_controls
[] = {
349 SOC_SINGLE_TLV("Digital Volume", MAX98363_R2040_AMP_VOL
,
350 0, 0x7F, 1, max98363_digital_tlv
),
351 SOC_SINGLE_TLV("Speaker Volume", MAX98363_R2041_AMP_GAIN
,
352 0, 10, 0, max98363_spk_tlv
),
353 SOC_SINGLE("Tone Generator Switch", MAX98363_R203F_TONE_GEN_EN
,
355 SOC_ENUM("Tone Config", max98363_tone_cfg_enum
),
356 SOC_SINGLE("Ramp Switch", MAX98363_R2042_DSP_CFG
,
357 MAX98363_AMP_DSP_CFG_RMP_SHIFT
, 1, 0),
358 SOC_SINGLE("CLK Monitor Switch", MAX98363_R2021_ERR_MON_CTRL
,
359 MAX98363_CLOCK_MON_SHIFT
, 1, 0),
360 SOC_SINGLE("SPKMON Monitor Switch", MAX98363_R2021_ERR_MON_CTRL
,
361 MAX98363_SPKMON_SHIFT
, 1, 0),
362 SOC_SINGLE("SPKMON Thresh", MAX98363_R2022_SPK_MON_THRESH
, 0, 0xFF, 0),
363 SOC_ENUM("SPKMON Duration", max98363_spkmon_duration_enum
),
366 static const struct snd_soc_dapm_widget max98363_dapm_widgets
[] = {
367 SND_SOC_DAPM_AIF_IN("AIFIN", "HiFi Playback", 0, SND_SOC_NOPM
, 0, 0),
368 SND_SOC_DAPM_OUTPUT("BE_OUT"),
371 static const struct snd_soc_dapm_route max98363_audio_map
[] = {
373 {"BE_OUT", NULL
, "AIFIN"},
376 static const struct snd_soc_component_driver soc_codec_dev_max98363
= {
377 .controls
= max98363_snd_controls
,
378 .num_controls
= ARRAY_SIZE(max98363_snd_controls
),
379 .dapm_widgets
= max98363_dapm_widgets
,
380 .num_dapm_widgets
= ARRAY_SIZE(max98363_dapm_widgets
),
381 .dapm_routes
= max98363_audio_map
,
382 .num_dapm_routes
= ARRAY_SIZE(max98363_audio_map
),
383 .use_pmdown_time
= 1,
387 static int max98363_init(struct sdw_slave
*slave
, struct regmap
*regmap
)
389 struct max98363_priv
*max98363
;
391 struct device
*dev
= &slave
->dev
;
393 /* Allocate and assign private driver data structure */
394 max98363
= devm_kzalloc(dev
, sizeof(*max98363
), GFP_KERNEL
);
398 dev_set_drvdata(dev
, max98363
);
399 max98363
->regmap
= regmap
;
400 max98363
->slave
= slave
;
402 regcache_cache_only(max98363
->regmap
, true);
404 max98363
->hw_init
= false;
405 max98363
->first_hw_init
= false;
407 /* codec registration */
408 ret
= devm_snd_soc_register_component(dev
, &soc_codec_dev_max98363
,
410 ARRAY_SIZE(max98363_dai
));
412 dev_err(dev
, "Failed to register codec: %d\n", ret
);
416 /* set autosuspend parameters */
417 pm_runtime_set_autosuspend_delay(dev
, 3000);
418 pm_runtime_use_autosuspend(dev
);
420 /* make sure the device does not suspend immediately */
421 pm_runtime_mark_last_busy(dev
);
423 pm_runtime_enable(dev
);
425 /* important note: the device is NOT tagged as 'active' and will remain
426 * 'suspended' until the hardware is enumerated/initialized. This is required
427 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
428 * fail with -EACCESS because of race conditions between card creation and enumeration
433 static int max98363_sdw_probe(struct sdw_slave
*slave
,
434 const struct sdw_device_id
*id
)
436 struct regmap
*regmap
;
438 /* Regmap Initialization */
439 regmap
= devm_regmap_init_sdw(slave
, &max98363_sdw_regmap
);
441 return PTR_ERR(regmap
);
443 return max98363_init(slave
, regmap
);
446 static const struct sdw_device_id max98363_id
[] = {
447 SDW_SLAVE_ENTRY(0x019F, 0x8363, 0),
450 MODULE_DEVICE_TABLE(sdw
, max98363_id
);
452 static struct sdw_driver max98363_sdw_driver
= {
455 .pm
= pm_ptr(&max98363_pm
),
457 .probe
= max98363_sdw_probe
,
458 .ops
= &max98363_slave_ops
,
459 .id_table
= max98363_id
,
462 module_sdw_driver(max98363_sdw_driver
);
464 MODULE_DESCRIPTION("ASoC MAX98363 driver SDW");
465 MODULE_AUTHOR("Ryan Lee <ryans.lee@analog.com>");
466 MODULE_LICENSE("GPL");