xref: /linux/sound/soc/sdca/sdca_class_function.c (revision af0bc3ac9a9e830cb52b718ecb237c4e76a466be)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2025 Cirrus Logic, Inc. and
3 //                    Cirrus Logic International Semiconductor Ltd.
4 
5 /*
6  * The MIPI SDCA specification is available for public downloads at
7  * https://www.mipi.org/mipi-sdca-v1-0-download
8  */
9 
10 #include <linux/auxiliary_bus.h>
11 #include <linux/cleanup.h>
12 #include <linux/minmax.h>
13 #include <linux/module.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/soundwire/sdw_registers.h>
18 #include <sound/pcm.h>
19 #include <sound/sdca_asoc.h>
20 #include <sound/sdca_fdl.h>
21 #include <sound/sdca_function.h>
22 #include <sound/sdca_interrupts.h>
23 #include <sound/sdca_jack.h>
24 #include <sound/sdca_regmap.h>
25 #include <sound/sdw.h>
26 #include <sound/soc-component.h>
27 #include <sound/soc-dai.h>
28 #include <sound/soc.h>
29 #include "sdca_class.h"
30 
31 struct class_function_drv {
32 	struct device *dev;
33 	struct regmap *regmap;
34 	struct sdca_class_drv *core;
35 
36 	struct sdca_function_data *function;
37 	bool suspended;
38 };
39 
40 static void class_function_regmap_lock(void *data)
41 {
42 	struct mutex *lock = data;
43 
44 	mutex_lock(lock);
45 }
46 
47 static void class_function_regmap_unlock(void *data)
48 {
49 	struct mutex *lock = data;
50 
51 	mutex_unlock(lock);
52 }
53 
54 static bool class_function_regmap_writeable(struct device *dev, unsigned int reg)
55 {
56 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
57 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
58 
59 	return sdca_regmap_writeable(drv->function, reg);
60 }
61 
62 static bool class_function_regmap_readable(struct device *dev, unsigned int reg)
63 {
64 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
65 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
66 
67 	return sdca_regmap_readable(drv->function, reg);
68 }
69 
70 static bool class_function_regmap_volatile(struct device *dev, unsigned int reg)
71 {
72 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
73 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
74 
75 	return sdca_regmap_volatile(drv->function, reg);
76 }
77 
78 static const struct regmap_config class_function_regmap_config = {
79 	.name			= "sdca",
80 	.reg_bits		= 32,
81 	.val_bits		= 32,
82 	.reg_format_endian	= REGMAP_ENDIAN_LITTLE,
83 	.val_format_endian	= REGMAP_ENDIAN_LITTLE,
84 
85 	.max_register		= SDW_SDCA_MAX_REGISTER,
86 	.readable_reg		= class_function_regmap_readable,
87 	.writeable_reg		= class_function_regmap_writeable,
88 	.volatile_reg		= class_function_regmap_volatile,
89 
90 	.cache_type		= REGCACHE_MAPLE,
91 
92 	.lock			= class_function_regmap_lock,
93 	.unlock			= class_function_regmap_unlock,
94 };
95 
96 static int class_function_regmap_mbq_size(struct device *dev, unsigned int reg)
97 {
98 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
99 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
100 
101 	return sdca_regmap_mbq_size(drv->function, reg);
102 }
103 
104 static bool class_function_regmap_deferrable(struct device *dev, unsigned int reg)
105 {
106 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
107 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
108 
109 	return sdca_regmap_deferrable(drv->function, reg);
110 }
111 
112 static const struct regmap_sdw_mbq_cfg class_function_mbq_config = {
113 	.mbq_size		= class_function_regmap_mbq_size,
114 	.deferrable		= class_function_regmap_deferrable,
115 	.retry_us		= 1000,
116 	.timeout_us		= 10000,
117 };
118 
119 static int class_function_startup(struct snd_pcm_substream *substream,
120 				  struct snd_soc_dai *dai)
121 {
122 	struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
123 
124 	return sdca_asoc_set_constraints(drv->dev, drv->regmap, drv->function,
125 					 substream, dai);
126 }
127 
128 static int class_function_sdw_add_peripheral(struct snd_pcm_substream *substream,
129 					     struct snd_pcm_hw_params *params,
130 					     struct snd_soc_dai *dai)
131 {
132 	struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
133 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
134 	struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent);
135 	struct sdw_stream_config sconfig = {0};
136 	struct sdw_port_config pconfig = {0};
137 	int ret;
138 
139 	if (!sdw_stream)
140 		return -EINVAL;
141 
142 	snd_sdw_params_to_config(substream, params, &sconfig, &pconfig);
143 
144 	/*
145 	 * FIXME: As also noted in sdca_asoc_get_port(), currently only
146 	 * a single unshared port is supported for each DAI.
147 	 */
148 	ret = sdca_asoc_get_port(drv->dev, drv->regmap, drv->function, dai);
149 	if (ret < 0)
150 		return ret;
151 
152 	pconfig.num = ret;
153 
154 	ret = sdw_stream_add_slave(sdw, &sconfig, &pconfig, 1, sdw_stream);
155 	if (ret) {
156 		dev_err(drv->dev, "failed to add sdw stream: %d\n", ret);
157 		return ret;
158 	}
159 
160 	return sdca_asoc_hw_params(drv->dev, drv->regmap, drv->function,
161 				   substream, params, dai);
162 }
163 
164 static int class_function_sdw_remove_peripheral(struct snd_pcm_substream *substream,
165 						struct snd_soc_dai *dai)
166 {
167 	struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
168 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
169 	struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent);
170 
171 	if (!sdw_stream)
172 		return -EINVAL;
173 
174 	return sdw_stream_remove_slave(sdw, sdw_stream);
175 }
176 
177 static int class_function_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stream,
178 					 int direction)
179 {
180 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
181 
182 	return 0;
183 }
184 
185 static const struct snd_soc_dai_ops class_function_sdw_ops = {
186 	.startup	= class_function_startup,
187 	.shutdown	= sdca_asoc_free_constraints,
188 	.set_stream	= class_function_sdw_set_stream,
189 	.hw_params	= class_function_sdw_add_peripheral,
190 	.hw_free	= class_function_sdw_remove_peripheral,
191 };
192 
193 static int class_function_component_probe(struct snd_soc_component *component)
194 {
195 	struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
196 	struct sdca_class_drv *core = drv->core;
197 
198 	return sdca_irq_populate(drv->function, component, core->irq_info);
199 }
200 
201 static int class_function_set_jack(struct snd_soc_component *component,
202 				   struct snd_soc_jack *jack, void *d)
203 {
204 	struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
205 	struct sdca_class_drv *core = drv->core;
206 
207 	return sdca_jack_set_jack(core->irq_info, jack);
208 }
209 
210 static const struct snd_soc_component_driver class_function_component_drv = {
211 	.probe			= class_function_component_probe,
212 	.endianness		= 1,
213 };
214 
215 static int class_function_init_device(struct class_function_drv *drv,
216 				      unsigned int status)
217 {
218 	int ret;
219 
220 	if (!(status & SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET)) {
221 		dev_dbg(drv->dev, "reset function device\n");
222 
223 		ret = sdca_reset_function(drv->dev, drv->function, drv->regmap);
224 		if (ret)
225 			return ret;
226 	}
227 
228 	if (status & SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION) {
229 		dev_dbg(drv->dev, "write initialisation\n");
230 
231 		ret = sdca_regmap_write_init(drv->dev, drv->core->dev_regmap,
232 					     drv->function);
233 		if (ret)
234 			return ret;
235 	}
236 
237 	return 0;
238 }
239 
240 static int class_function_boot(struct class_function_drv *drv)
241 {
242 	unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
243 					SDCA_ENTITY_TYPE_ENTITY_0,
244 					SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
245 	unsigned int val;
246 	int ret;
247 
248 	guard(mutex)(&drv->core->init_lock);
249 
250 	ret = regmap_read(drv->regmap, reg, &val);
251 	if (ret < 0) {
252 		dev_err(drv->dev, "failed to read function status: %d\n", ret);
253 		return ret;
254 	}
255 
256 	ret = class_function_init_device(drv, val);
257 	if (ret)
258 		return ret;
259 
260 	/* Start FDL process */
261 	ret = sdca_irq_populate_early(drv->dev, drv->regmap, drv->function,
262 				      drv->core->irq_info);
263 	if (ret)
264 		return ret;
265 
266 	ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
267 	if (ret)
268 		return ret;
269 
270 	ret = sdca_regmap_write_defaults(drv->dev, drv->regmap, drv->function);
271 	if (ret)
272 		return ret;
273 
274 	ret = regmap_write(drv->regmap, reg, 0xFF);
275 	if (ret < 0) {
276 		dev_err(drv->dev, "failed to clear function status: %d\n", ret);
277 		return ret;
278 	}
279 
280 	return 0;
281 }
282 
283 static int class_function_probe(struct auxiliary_device *auxdev,
284 				const struct auxiliary_device_id *aux_dev_id)
285 {
286 	struct device *dev = &auxdev->dev;
287 	struct sdca_class_drv *core = dev_get_drvdata(dev->parent);
288 	struct sdca_device_data *data = &core->sdw->sdca_data;
289 	struct sdca_function_desc *desc;
290 	struct snd_soc_component_driver *cmp_drv;
291 	struct snd_soc_dai_driver *dais;
292 	struct class_function_drv *drv;
293 	struct regmap_sdw_mbq_cfg *mbq_config;
294 	struct regmap_config *config;
295 	struct reg_default *defaults;
296 	int ndefaults;
297 	int num_dais;
298 	int ret;
299 	int i;
300 
301 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
302 	if (!drv)
303 		return -ENOMEM;
304 
305 	cmp_drv = devm_kmemdup(dev, &class_function_component_drv, sizeof(*cmp_drv),
306 			       GFP_KERNEL);
307 	if (!cmp_drv)
308 		return -ENOMEM;
309 
310 	config = devm_kmemdup(dev, &class_function_regmap_config, sizeof(*config),
311 			      GFP_KERNEL);
312 	if (!config)
313 		return -ENOMEM;
314 
315 	mbq_config = devm_kmemdup(dev, &class_function_mbq_config, sizeof(*mbq_config),
316 				  GFP_KERNEL);
317 	if (!mbq_config)
318 		return -ENOMEM;
319 
320 	drv->dev = dev;
321 	drv->core = core;
322 
323 	for (i = 0; i < data->num_functions; i++) {
324 		desc = &data->function[i];
325 
326 		if (desc->type == aux_dev_id->driver_data)
327 			break;
328 	}
329 	if (i == core->sdw->sdca_data.num_functions) {
330 		dev_err(dev, "failed to locate function\n");
331 		return -EINVAL;
332 	}
333 
334 	drv->function = &core->functions[i];
335 
336 	ret = sdca_parse_function(dev, core->sdw, desc, drv->function);
337 	if (ret)
338 		return ret;
339 
340 	ndefaults = sdca_regmap_count_constants(dev, drv->function);
341 	if (ndefaults < 0)
342 		return ndefaults;
343 
344 	defaults = devm_kcalloc(dev, ndefaults, sizeof(*defaults), GFP_KERNEL);
345 	if (!defaults)
346 		return -ENOMEM;
347 
348 	ret = sdca_regmap_populate_constants(dev, drv->function, defaults);
349 	if (ret < 0)
350 		return ret;
351 
352 	regcache_sort_defaults(defaults, ndefaults);
353 
354 	auxiliary_set_drvdata(auxdev, drv);
355 
356 	config->reg_defaults = defaults;
357 	config->num_reg_defaults = ndefaults;
358 	config->lock_arg = &core->regmap_lock;
359 
360 	if (drv->function->busy_max_delay) {
361 		mbq_config->timeout_us = drv->function->busy_max_delay;
362 		mbq_config->retry_us = umax(drv->function->busy_max_delay / 10,
363 					    mbq_config->retry_us);
364 	}
365 
366 	drv->regmap = devm_regmap_init_sdw_mbq_cfg(dev, core->sdw, config, mbq_config);
367 	if (IS_ERR(drv->regmap))
368 		return dev_err_probe(dev, PTR_ERR(drv->regmap),
369 				     "failed to create regmap");
370 
371 	if (desc->type == SDCA_FUNCTION_TYPE_UAJ)
372 		cmp_drv->set_jack = class_function_set_jack;
373 
374 	ret = sdca_asoc_populate_component(dev, drv->function, cmp_drv,
375 					   &dais, &num_dais,
376 					   &class_function_sdw_ops);
377 	if (ret)
378 		return ret;
379 
380 	dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
381 
382 	pm_runtime_set_autosuspend_delay(dev, 200);
383 	pm_runtime_use_autosuspend(dev);
384 	pm_runtime_set_active(dev);
385 	pm_runtime_get_noresume(dev);
386 
387 	ret = devm_pm_runtime_enable(dev);
388 	if (ret)
389 		return ret;
390 
391 	ret = class_function_boot(drv);
392 	if (ret)
393 		return ret;
394 
395 	ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais);
396 	if (ret)
397 		return dev_err_probe(dev, ret, "failed to register component\n");
398 
399 	pm_runtime_mark_last_busy(dev);
400 	pm_runtime_put_autosuspend(dev);
401 
402 	return 0;
403 }
404 
405 static int class_function_runtime_suspend(struct device *dev)
406 {
407 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
408 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
409 
410 	/*
411 	 * Whilst the driver doesn't power the chip down here, going into
412 	 * runtime suspend means the driver can't be sure the bus won't
413 	 * power down which would prevent communication with the device.
414 	 */
415 	regcache_cache_only(drv->regmap, true);
416 
417 	return 0;
418 }
419 
420 static int class_function_runtime_resume(struct device *dev)
421 {
422 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
423 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
424 	int ret;
425 
426 	guard(mutex)(&drv->core->init_lock);
427 
428 	regcache_mark_dirty(drv->regmap);
429 	regcache_cache_only(drv->regmap, false);
430 
431 	if (drv->suspended) {
432 		unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
433 						SDCA_ENTITY_TYPE_ENTITY_0,
434 						SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
435 		unsigned int val;
436 
437 		ret = regmap_read(drv->regmap, reg, &val);
438 		if (ret < 0) {
439 			dev_err(drv->dev, "failed to read function status: %d\n", ret);
440 			goto err;
441 		}
442 
443 		ret = class_function_init_device(drv, val);
444 		if (ret)
445 			goto err;
446 
447 		sdca_irq_enable_early(drv->function, drv->core->irq_info);
448 
449 		ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
450 		if (ret)
451 			goto err;
452 
453 		sdca_irq_enable(drv->function, drv->core->irq_info);
454 
455 		ret = regmap_write(drv->regmap, reg, 0xFF);
456 		if (ret < 0) {
457 			dev_err(drv->dev, "failed to clear function status: %d\n", ret);
458 			goto err;
459 		}
460 
461 		drv->suspended = false;
462 	}
463 
464 	ret = regcache_sync(drv->regmap);
465 	if (ret) {
466 		dev_err(drv->dev, "failed to restore register cache: %d\n", ret);
467 		goto err;
468 	}
469 
470 	return 0;
471 
472 err:
473 	regcache_cache_only(drv->regmap, true);
474 
475 	return ret;
476 }
477 
478 static int class_function_suspend(struct device *dev)
479 {
480 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
481 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
482 	int ret;
483 
484 	drv->suspended = true;
485 
486 	/* Ensure runtime resume runs on resume */
487 	ret = pm_runtime_resume_and_get(dev);
488 	if (ret) {
489 		dev_err(dev, "failed to resume for suspend: %d\n", ret);
490 		return ret;
491 	}
492 
493 	sdca_irq_disable(drv->function, drv->core->irq_info);
494 
495 	ret = pm_runtime_force_suspend(dev);
496 	if (ret) {
497 		dev_err(dev, "failed to force suspend: %d\n", ret);
498 		return ret;
499 	}
500 
501 	pm_runtime_put_noidle(dev);
502 
503 	return 0;
504 }
505 
506 static int class_function_resume(struct device *dev)
507 {
508 	int ret;
509 
510 	ret = pm_runtime_force_resume(dev);
511 	if (ret) {
512 		dev_err(dev, "failed to force resume: %d\n", ret);
513 		return ret;
514 	}
515 
516 	return 0;
517 }
518 
519 static const struct dev_pm_ops class_function_pm_ops = {
520 	SYSTEM_SLEEP_PM_OPS(class_function_suspend, class_function_resume)
521 	RUNTIME_PM_OPS(class_function_runtime_suspend,
522 		       class_function_runtime_resume, NULL)
523 };
524 
525 static const struct auxiliary_device_id class_function_id_table[] = {
526 	{
527 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME,
528 		.driver_data = SDCA_FUNCTION_TYPE_SMART_AMP,
529 	},
530 	{
531 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME,
532 		.driver_data = SDCA_FUNCTION_TYPE_SMART_MIC,
533 	},
534 	{
535 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME,
536 		.driver_data = SDCA_FUNCTION_TYPE_UAJ,
537 	},
538 	{
539 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME,
540 		.driver_data = SDCA_FUNCTION_TYPE_HID,
541 	},
542 	{},
543 };
544 MODULE_DEVICE_TABLE(auxiliary, class_function_id_table);
545 
546 static struct auxiliary_driver class_function_drv = {
547 	.driver = {
548 		.name		= "sdca_function",
549 		.pm		= pm_ptr(&class_function_pm_ops),
550 	},
551 
552 	.probe		= class_function_probe,
553 	.id_table	= class_function_id_table
554 };
555 module_auxiliary_driver(class_function_drv);
556 
557 MODULE_LICENSE("GPL");
558 MODULE_DESCRIPTION("SDCA Class Function Driver");
559 MODULE_IMPORT_NS("SND_SOC_SDCA");
560