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