1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2022, Analog Devices Inc. 3 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> 14 15 #include "max98363.h" 16 17 static const 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}, 26 }; 27 28 static bool max98363_readable_register(struct device *dev, unsigned int reg) 29 { 30 switch (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: 43 return true; 44 default: 45 return false; 46 } 47 }; 48 49 static bool max98363_volatile_reg(struct device *dev, unsigned int reg) 50 { 51 switch (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: 58 return true; 59 default: 60 return false; 61 } 62 } 63 64 static const struct regmap_config max98363_sdw_regmap = { 65 .reg_bits = 32, 66 .val_bits = 8, 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, 75 }; 76 77 static int max98363_suspend(struct device *dev) 78 { 79 struct max98363_priv *max98363 = dev_get_drvdata(dev); 80 81 regcache_cache_only(max98363->regmap, true); 82 regcache_mark_dirty(max98363->regmap); 83 84 return 0; 85 } 86 87 #define MAX98363_PROBE_TIMEOUT 5000 88 89 static int max98363_resume(struct device *dev) 90 { 91 struct sdw_slave *slave = dev_to_sdw_dev(dev); 92 struct max98363_priv *max98363 = dev_get_drvdata(dev); 93 int ret; 94 95 if (!max98363->first_hw_init) 96 return 0; 97 98 ret = sdw_slave_wait_for_init(slave, MAX98363_PROBE_TIMEOUT); 99 if (ret) 100 return ret; 101 102 regcache_cache_only(max98363->regmap, false); 103 regcache_sync(max98363->regmap); 104 105 return 0; 106 } 107 108 static DEFINE_RUNTIME_DEV_PM_OPS(max98363_pm, max98363_suspend, max98363_resume, NULL); 109 110 static int max98363_read_prop(struct sdw_slave *slave) 111 { 112 struct sdw_slave_prop *prop = &slave->prop; 113 int nval, i; 114 u32 bit; 115 unsigned long addr; 116 struct sdw_dpn_prop *dpn; 117 118 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY; 119 120 /* BITMAP: 00000010 Dataport 1 is active */ 121 prop->sink_ports = BIT(1); 122 prop->paging_support = true; 123 prop->clk_stop_timeout = 20; 124 prop->simple_clk_stop_capable = true; 125 prop->clock_reg_supported = true; 126 127 nval = hweight32(prop->sink_ports); 128 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 129 sizeof(*prop->sink_dpn_prop), 130 GFP_KERNEL); 131 if (!prop->sink_dpn_prop) 132 return -ENOMEM; 133 134 i = 0; 135 dpn = prop->sink_dpn_prop; 136 addr = prop->sink_ports; 137 for_each_set_bit(bit, &addr, 32) { 138 dpn[i].num = bit; 139 dpn[i].type = SDW_DPN_FULL; 140 dpn[i].simple_ch_prep_sm = true; 141 dpn[i].ch_prep_timeout = 10; 142 i++; 143 } 144 145 return 0; 146 } 147 148 static int max98363_io_init(struct sdw_slave *slave) 149 { 150 struct device *dev = &slave->dev; 151 struct max98363_priv *max98363 = dev_get_drvdata(dev); 152 int ret, reg; 153 154 regcache_cache_only(max98363->regmap, false); 155 if (max98363->first_hw_init) 156 regcache_cache_bypass(max98363->regmap, true); 157 158 /* 159 * PM runtime status is marked as 'active' only when a Slave reports as Attached 160 */ 161 if (!max98363->first_hw_init) 162 /* update count of parent 'active' children */ 163 pm_runtime_set_active(dev); 164 165 pm_runtime_get_noresume(dev); 166 167 ret = regmap_read(max98363->regmap, MAX98363_R21FF_REV_ID, ®); 168 if (!ret) 169 dev_info(dev, "Revision ID: %X\n", reg); 170 else 171 goto out; 172 173 if (max98363->first_hw_init) { 174 regcache_cache_bypass(max98363->regmap, false); 175 regcache_mark_dirty(max98363->regmap); 176 } 177 178 max98363->first_hw_init = true; 179 max98363->hw_init = true; 180 181 out: 182 pm_runtime_put_autosuspend(dev); 183 184 return ret; 185 } 186 187 #define MAX98363_RATES SNDRV_PCM_RATE_8000_192000 188 #define MAX98363_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE) 189 190 static int max98363_sdw_dai_hw_params(struct snd_pcm_substream *substream, 191 struct snd_pcm_hw_params *params, 192 struct snd_soc_dai *dai) 193 { 194 struct snd_soc_component *component = dai->component; 195 struct max98363_priv *max98363 = 196 snd_soc_component_get_drvdata(component); 197 198 struct sdw_stream_config stream_config; 199 struct sdw_port_config port_config; 200 enum sdw_data_direction direction; 201 struct sdw_stream_runtime *stream; 202 struct snd_pcm_runtime *runtime = substream->runtime; 203 204 int ret; 205 206 stream = snd_soc_dai_get_dma_data(dai, substream); 207 208 if (!stream) 209 return -EINVAL; 210 211 if (!max98363->slave) 212 return -EINVAL; 213 214 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) 215 return -EINVAL; 216 217 direction = SDW_DATA_DIR_RX; 218 port_config.num = 1; 219 220 stream_config.frame_rate = params_rate(params); 221 stream_config.bps = snd_pcm_format_width(params_format(params)); 222 stream_config.direction = direction; 223 stream_config.ch_count = 1; 224 225 if (stream_config.ch_count > runtime->hw.channels_max) { 226 stream_config.ch_count = runtime->hw.channels_max; 227 dev_info(dai->dev, "Number of channels: %d (requested: %d)\n", 228 stream_config.ch_count, params_channels(params)); 229 } 230 port_config.ch_mask = GENMASK((int)stream_config.ch_count - 1, 0); 231 232 ret = sdw_stream_add_slave(max98363->slave, &stream_config, 233 &port_config, 1, stream); 234 if (ret) { 235 dev_err(dai->dev, "Unable to configure port\n"); 236 return ret; 237 } 238 239 dev_dbg(component->dev, "Format supported %d", params_format(params)); 240 241 return 0; 242 } 243 244 static int max98363_pcm_hw_free(struct snd_pcm_substream *substream, 245 struct snd_soc_dai *dai) 246 { 247 struct snd_soc_component *component = dai->component; 248 struct max98363_priv *max98363 = 249 snd_soc_component_get_drvdata(component); 250 struct sdw_stream_runtime *stream = 251 snd_soc_dai_get_dma_data(dai, substream); 252 253 if (!max98363->slave) 254 return -EINVAL; 255 256 sdw_stream_remove_slave(max98363->slave, stream); 257 258 return 0; 259 } 260 261 static int max98363_set_sdw_stream(struct snd_soc_dai *dai, 262 void *sdw_stream, int direction) 263 { 264 snd_soc_dai_dma_data_set(dai, direction, sdw_stream); 265 266 return 0; 267 } 268 269 static const struct snd_soc_dai_ops max98363_dai_sdw_ops = { 270 .hw_params = max98363_sdw_dai_hw_params, 271 .hw_free = max98363_pcm_hw_free, 272 .set_stream = max98363_set_sdw_stream, 273 }; 274 275 static struct snd_soc_dai_driver max98363_dai[] = { 276 { 277 .name = "max98363-aif1", 278 .playback = { 279 .stream_name = "HiFi Playback", 280 .channels_min = 1, 281 .channels_max = 1, 282 .rates = MAX98363_RATES, 283 .formats = MAX98363_FORMATS, 284 }, 285 .ops = &max98363_dai_sdw_ops, 286 } 287 }; 288 289 static int max98363_update_status(struct sdw_slave *slave, 290 enum sdw_slave_status status) 291 { 292 struct max98363_priv *max98363 = dev_get_drvdata(&slave->dev); 293 294 if (status == SDW_SLAVE_UNATTACHED) 295 max98363->hw_init = false; 296 297 /* 298 * Perform initialization only if slave status is SDW_SLAVE_ATTACHED 299 */ 300 if (max98363->hw_init || status != SDW_SLAVE_ATTACHED) 301 return 0; 302 303 /* perform I/O transfers required for Slave initialization */ 304 return max98363_io_init(slave); 305 } 306 307 static const struct sdw_slave_ops max98363_slave_ops = { 308 .read_prop = max98363_read_prop, 309 .update_status = max98363_update_status, 310 }; 311 312 static DECLARE_TLV_DB_SCALE(max98363_digital_tlv, -6350, 50, 1); 313 static const DECLARE_TLV_DB_RANGE(max98363_spk_tlv, 314 0, 5, TLV_DB_SCALE_ITEM(-300, 300, 0), 315 ); 316 317 static const char * const max98363_tone_cfg_text[] = { 318 "Reserved", "0", "+FS/2", "-FS/2", "1KHz", 319 "12KHz", "8KHz", "6KHz", "4KHz", "3KHz", 320 "2KHz", "1.5KHz", "Reserved", "500Hz", "250Hz" 321 }; 322 323 static SOC_ENUM_SINGLE_DECL(max98363_tone_cfg_enum, 324 MAX98363_R2030_TONE_GEN_CFG, 0, 325 max98363_tone_cfg_text); 326 327 static const char * const max98363_spkmon_duration_text[] = { 328 "8ms", "20ms", "40ms", "60ms", 329 "80ms", "160ms", "240ms", "320ms", 330 "400ms", "480ms", "560ms", "640ms", 331 "720ms", "800ms", "880ms", "960ms" 332 }; 333 334 static SOC_ENUM_SINGLE_DECL(max98363_spkmon_duration_enum, 335 MAX98363_R2023_SPK_MON_DURATION, 0, 336 max98363_spkmon_duration_text); 337 338 static const struct snd_kcontrol_new max98363_snd_controls[] = { 339 SOC_SINGLE_TLV("Digital Volume", MAX98363_R2040_AMP_VOL, 340 0, 0x7F, 1, max98363_digital_tlv), 341 SOC_SINGLE_TLV("Speaker Volume", MAX98363_R2041_AMP_GAIN, 342 0, 10, 0, max98363_spk_tlv), 343 SOC_SINGLE("Tone Generator Switch", MAX98363_R203F_TONE_GEN_EN, 344 0, 1, 0), 345 SOC_ENUM("Tone Config", max98363_tone_cfg_enum), 346 SOC_SINGLE("Ramp Switch", MAX98363_R2042_DSP_CFG, 347 MAX98363_AMP_DSP_CFG_RMP_SHIFT, 1, 0), 348 SOC_SINGLE("CLK Monitor Switch", MAX98363_R2021_ERR_MON_CTRL, 349 MAX98363_CLOCK_MON_SHIFT, 1, 0), 350 SOC_SINGLE("SPKMON Monitor Switch", MAX98363_R2021_ERR_MON_CTRL, 351 MAX98363_SPKMON_SHIFT, 1, 0), 352 SOC_SINGLE("SPKMON Thresh", MAX98363_R2022_SPK_MON_THRESH, 0, 0xFF, 0), 353 SOC_ENUM("SPKMON Duration", max98363_spkmon_duration_enum), 354 }; 355 356 static const struct snd_soc_dapm_widget max98363_dapm_widgets[] = { 357 SND_SOC_DAPM_AIF_IN("AIFIN", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0), 358 SND_SOC_DAPM_OUTPUT("BE_OUT"), 359 }; 360 361 static const struct snd_soc_dapm_route max98363_audio_map[] = { 362 /* Plabyack */ 363 {"BE_OUT", NULL, "AIFIN"}, 364 }; 365 366 static const struct snd_soc_component_driver soc_codec_dev_max98363 = { 367 .controls = max98363_snd_controls, 368 .num_controls = ARRAY_SIZE(max98363_snd_controls), 369 .dapm_widgets = max98363_dapm_widgets, 370 .num_dapm_widgets = ARRAY_SIZE(max98363_dapm_widgets), 371 .dapm_routes = max98363_audio_map, 372 .num_dapm_routes = ARRAY_SIZE(max98363_audio_map), 373 .use_pmdown_time = 1, 374 .endianness = 1, 375 }; 376 377 static int max98363_init(struct sdw_slave *slave, struct regmap *regmap) 378 { 379 struct max98363_priv *max98363; 380 int ret; 381 struct device *dev = &slave->dev; 382 383 /* Allocate and assign private driver data structure */ 384 max98363 = devm_kzalloc(dev, sizeof(*max98363), GFP_KERNEL); 385 if (!max98363) 386 return -ENOMEM; 387 388 dev_set_drvdata(dev, max98363); 389 max98363->regmap = regmap; 390 max98363->slave = slave; 391 392 regcache_cache_only(max98363->regmap, true); 393 394 max98363->hw_init = false; 395 max98363->first_hw_init = false; 396 397 /* codec registration */ 398 ret = devm_snd_soc_register_component(dev, &soc_codec_dev_max98363, 399 max98363_dai, 400 ARRAY_SIZE(max98363_dai)); 401 if (ret < 0) { 402 dev_err(dev, "Failed to register codec: %d\n", ret); 403 return ret; 404 } 405 406 /* set autosuspend parameters */ 407 pm_runtime_set_autosuspend_delay(dev, 3000); 408 pm_runtime_use_autosuspend(dev); 409 410 /* make sure the device does not suspend immediately */ 411 pm_runtime_mark_last_busy(dev); 412 413 pm_runtime_enable(dev); 414 415 /* important note: the device is NOT tagged as 'active' and will remain 416 * 'suspended' until the hardware is enumerated/initialized. This is required 417 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently 418 * fail with -EACCESS because of race conditions between card creation and enumeration 419 */ 420 return 0; 421 } 422 423 static int max98363_sdw_probe(struct sdw_slave *slave, 424 const struct sdw_device_id *id) 425 { 426 struct regmap *regmap; 427 428 /* Regmap Initialization */ 429 regmap = devm_regmap_init_sdw(slave, &max98363_sdw_regmap); 430 if (IS_ERR(regmap)) 431 return PTR_ERR(regmap); 432 433 return max98363_init(slave, regmap); 434 } 435 436 static const struct sdw_device_id max98363_id[] = { 437 SDW_SLAVE_ENTRY(0x019F, 0x8363, 0), 438 {}, 439 }; 440 MODULE_DEVICE_TABLE(sdw, max98363_id); 441 442 static struct sdw_driver max98363_sdw_driver = { 443 .driver = { 444 .name = "max98363", 445 .pm = pm_ptr(&max98363_pm), 446 }, 447 .probe = max98363_sdw_probe, 448 .ops = &max98363_slave_ops, 449 .id_table = max98363_id, 450 }; 451 452 module_sdw_driver(max98363_sdw_driver); 453 454 MODULE_DESCRIPTION("ASoC MAX98363 driver SDW"); 455 MODULE_AUTHOR("Ryan Lee <ryans.lee@analog.com>"); 456 MODULE_LICENSE("GPL"); 457