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