xref: /linux/sound/soc/sdca/sdca_class_function.c (revision da7afdc79cba00f952df12cd579e44832d829c0a)
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 	pm_runtime_set_autosuspend_delay(dev, 200);
381 	pm_runtime_use_autosuspend(dev);
382 	pm_runtime_set_active(dev);
383 	pm_runtime_get_noresume(dev);
384 
385 	ret = devm_pm_runtime_enable(dev);
386 	if (ret)
387 		return ret;
388 
389 	ret = class_function_boot(drv);
390 	if (ret)
391 		return ret;
392 
393 	ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais);
394 	if (ret)
395 		return dev_err_probe(dev, ret, "failed to register component\n");
396 
397 	pm_runtime_mark_last_busy(dev);
398 	pm_runtime_put_autosuspend(dev);
399 
400 	return 0;
401 }
402 
403 static int class_function_runtime_suspend(struct device *dev)
404 {
405 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
406 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
407 
408 	/*
409 	 * Whilst the driver doesn't power the chip down here, going into
410 	 * runtime suspend means the driver can't be sure the bus won't
411 	 * power down which would prevent communication with the device.
412 	 */
413 	regcache_cache_only(drv->regmap, true);
414 
415 	return 0;
416 }
417 
418 static int class_function_runtime_resume(struct device *dev)
419 {
420 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
421 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
422 	int ret;
423 
424 	guard(mutex)(&drv->core->init_lock);
425 
426 	regcache_mark_dirty(drv->regmap);
427 	regcache_cache_only(drv->regmap, false);
428 
429 	if (drv->suspended) {
430 		unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
431 						SDCA_ENTITY_TYPE_ENTITY_0,
432 						SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
433 		unsigned int val;
434 
435 		ret = regmap_read(drv->regmap, reg, &val);
436 		if (ret < 0) {
437 			dev_err(drv->dev, "failed to read function status: %d\n", ret);
438 			goto err;
439 		}
440 
441 		ret = class_function_init_device(drv, val);
442 		if (ret)
443 			goto err;
444 
445 		sdca_irq_enable_early(drv->function, drv->core->irq_info);
446 
447 		ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
448 		if (ret)
449 			goto err;
450 
451 		sdca_irq_enable(drv->function, drv->core->irq_info);
452 
453 		ret = regmap_write(drv->regmap, reg, 0xFF);
454 		if (ret < 0) {
455 			dev_err(drv->dev, "failed to clear function status: %d\n", ret);
456 			goto err;
457 		}
458 
459 		drv->suspended = false;
460 	}
461 
462 	ret = regcache_sync(drv->regmap);
463 	if (ret) {
464 		dev_err(drv->dev, "failed to restore register cache: %d\n", ret);
465 		goto err;
466 	}
467 
468 	return 0;
469 
470 err:
471 	regcache_cache_only(drv->regmap, true);
472 
473 	return ret;
474 }
475 
476 static int class_function_suspend(struct device *dev)
477 {
478 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
479 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
480 	int ret;
481 
482 	drv->suspended = true;
483 
484 	/* Ensure runtime resume runs on resume */
485 	ret = pm_runtime_resume_and_get(dev);
486 	if (ret) {
487 		dev_err(dev, "failed to resume for suspend: %d\n", ret);
488 		return ret;
489 	}
490 
491 	sdca_irq_disable(drv->function, drv->core->irq_info);
492 
493 	ret = pm_runtime_force_suspend(dev);
494 	if (ret) {
495 		dev_err(dev, "failed to force suspend: %d\n", ret);
496 		return ret;
497 	}
498 
499 	pm_runtime_put_noidle(dev);
500 
501 	return 0;
502 }
503 
504 static int class_function_resume(struct device *dev)
505 {
506 	int ret;
507 
508 	ret = pm_runtime_force_resume(dev);
509 	if (ret) {
510 		dev_err(dev, "failed to force resume: %d\n", ret);
511 		return ret;
512 	}
513 
514 	return 0;
515 }
516 
517 static const struct dev_pm_ops class_function_pm_ops = {
518 	SYSTEM_SLEEP_PM_OPS(class_function_suspend, class_function_resume)
519 	RUNTIME_PM_OPS(class_function_runtime_suspend,
520 		       class_function_runtime_resume, NULL)
521 };
522 
523 static const struct auxiliary_device_id class_function_id_table[] = {
524 	{
525 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME,
526 		.driver_data = SDCA_FUNCTION_TYPE_SMART_AMP,
527 	},
528 	{
529 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME,
530 		.driver_data = SDCA_FUNCTION_TYPE_SMART_MIC,
531 	},
532 	{
533 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME,
534 		.driver_data = SDCA_FUNCTION_TYPE_UAJ,
535 	},
536 	{
537 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME,
538 		.driver_data = SDCA_FUNCTION_TYPE_HID,
539 	},
540 	{},
541 };
542 MODULE_DEVICE_TABLE(auxiliary, class_function_id_table);
543 
544 static struct auxiliary_driver class_function_drv = {
545 	.driver = {
546 		.name		= "sdca_function",
547 		.pm		= pm_ptr(&class_function_pm_ops),
548 	},
549 
550 	.probe		= class_function_probe,
551 	.id_table	= class_function_id_table
552 };
553 module_auxiliary_driver(class_function_drv);
554 
555 MODULE_LICENSE("GPL");
556 MODULE_DESCRIPTION("SDCA Class Function Driver");
557 MODULE_IMPORT_NS("SND_SOC_SDCA");
558