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