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