xref: /linux/sound/soc/sdca/sdca_class_function.c (revision bb2ea74eeb6735eed29bd74695f90f0e5af09f5c)
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 
class_function_regmap_lock(void * data)40 static void class_function_regmap_lock(void *data)
41 {
42 	struct mutex *lock = data;
43 
44 	mutex_lock(lock);
45 }
46 
class_function_regmap_unlock(void * data)47 static void class_function_regmap_unlock(void *data)
48 {
49 	struct mutex *lock = data;
50 
51 	mutex_unlock(lock);
52 }
53 
class_function_regmap_writeable(struct device * dev,unsigned int reg)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 
class_function_regmap_readable(struct device * dev,unsigned int reg)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 
class_function_regmap_volatile(struct device * dev,unsigned int reg)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 
class_function_regmap_mbq_size(struct device * dev,unsigned int reg)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 
class_function_regmap_deferrable(struct device * dev,unsigned int reg)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 
class_function_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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 
class_function_sdw_add_peripheral(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
class_function_sdw_remove_peripheral(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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 
class_function_sdw_set_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)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 
class_function_component_probe(struct snd_soc_component * component)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 
class_function_component_remove(struct snd_soc_component * component)201 static void class_function_component_remove(struct snd_soc_component *component)
202 {
203 	struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
204 	struct sdca_class_drv *core = drv->core;
205 
206 	sdca_irq_cleanup(component->dev, drv->function, core->irq_info);
207 }
208 
class_function_set_jack(struct snd_soc_component * component,struct snd_soc_jack * jack,void * d)209 static int class_function_set_jack(struct snd_soc_component *component,
210 				   struct snd_soc_jack *jack, void *d)
211 {
212 	struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
213 	struct sdca_class_drv *core = drv->core;
214 
215 	return sdca_jack_set_jack(core->irq_info, jack);
216 }
217 
218 static const struct snd_soc_component_driver class_function_component_drv = {
219 	.probe			= class_function_component_probe,
220 	.remove			= class_function_component_remove,
221 	.endianness		= 1,
222 };
223 
class_function_init_device(struct class_function_drv * drv,unsigned int status)224 static int class_function_init_device(struct class_function_drv *drv,
225 				      unsigned int status)
226 {
227 	int ret;
228 
229 	if (!(status & SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET)) {
230 		dev_dbg(drv->dev, "reset function device\n");
231 
232 		ret = sdca_reset_function(drv->dev, drv->function, drv->regmap);
233 		if (ret)
234 			return ret;
235 	}
236 
237 	if (status & SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION) {
238 		dev_dbg(drv->dev, "write initialisation\n");
239 
240 		ret = sdca_regmap_write_init(drv->dev, drv->core->dev_regmap,
241 					     drv->function);
242 		if (ret)
243 			return ret;
244 	}
245 
246 	return 0;
247 }
248 
class_function_boot(struct class_function_drv * drv)249 static int class_function_boot(struct class_function_drv *drv)
250 {
251 	unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
252 					SDCA_ENTITY_TYPE_ENTITY_0,
253 					SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
254 	unsigned int val;
255 	int ret;
256 
257 	guard(mutex)(&drv->core->init_lock);
258 
259 	ret = regmap_read(drv->regmap, reg, &val);
260 	if (ret < 0) {
261 		dev_err(drv->dev, "failed to read function status: %d\n", ret);
262 		return ret;
263 	}
264 
265 	ret = class_function_init_device(drv, val);
266 	if (ret)
267 		return ret;
268 
269 	/* Start FDL process */
270 	ret = sdca_irq_populate_early(drv->dev, drv->regmap, drv->function,
271 				      drv->core->irq_info);
272 	if (ret)
273 		return ret;
274 
275 	ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
276 	if (ret)
277 		return ret;
278 
279 	ret = sdca_regmap_write_defaults(drv->dev, drv->regmap, drv->function);
280 	if (ret)
281 		return ret;
282 
283 	ret = regmap_write(drv->regmap, reg, 0xFF);
284 	if (ret < 0) {
285 		dev_err(drv->dev, "failed to clear function status: %d\n", ret);
286 		return ret;
287 	}
288 
289 	return 0;
290 }
291 
class_function_probe(struct auxiliary_device * auxdev,const struct auxiliary_device_id * aux_dev_id)292 static int class_function_probe(struct auxiliary_device *auxdev,
293 				const struct auxiliary_device_id *aux_dev_id)
294 {
295 	struct device *dev = &auxdev->dev;
296 	struct sdca_class_drv *core = dev_get_drvdata(dev->parent);
297 	struct sdca_device_data *data = &core->sdw->sdca_data;
298 	struct sdca_function_desc *desc;
299 	struct snd_soc_component_driver *cmp_drv;
300 	struct snd_soc_dai_driver *dais;
301 	struct class_function_drv *drv;
302 	struct regmap_sdw_mbq_cfg *mbq_config;
303 	struct regmap_config *config;
304 	struct reg_default *defaults;
305 	int ndefaults;
306 	int num_dais;
307 	int ret;
308 	int i;
309 
310 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
311 	if (!drv)
312 		return -ENOMEM;
313 
314 	cmp_drv = devm_kmemdup(dev, &class_function_component_drv, sizeof(*cmp_drv),
315 			       GFP_KERNEL);
316 	if (!cmp_drv)
317 		return -ENOMEM;
318 
319 	config = devm_kmemdup(dev, &class_function_regmap_config, sizeof(*config),
320 			      GFP_KERNEL);
321 	if (!config)
322 		return -ENOMEM;
323 
324 	mbq_config = devm_kmemdup(dev, &class_function_mbq_config, sizeof(*mbq_config),
325 				  GFP_KERNEL);
326 	if (!mbq_config)
327 		return -ENOMEM;
328 
329 	drv->dev = dev;
330 	drv->core = core;
331 
332 	for (i = 0; i < data->num_functions; i++) {
333 		desc = &data->function[i];
334 
335 		if (desc->type == aux_dev_id->driver_data)
336 			break;
337 	}
338 	if (i == core->sdw->sdca_data.num_functions) {
339 		dev_err(dev, "failed to locate function\n");
340 		return -EINVAL;
341 	}
342 
343 	drv->function = &core->functions[i];
344 
345 	ret = sdca_parse_function(dev, core->sdw, desc, drv->function);
346 	if (ret)
347 		return ret;
348 
349 	ndefaults = sdca_regmap_count_constants(dev, drv->function);
350 	if (ndefaults < 0)
351 		return ndefaults;
352 
353 	defaults = devm_kcalloc(dev, ndefaults, sizeof(*defaults), GFP_KERNEL);
354 	if (!defaults)
355 		return -ENOMEM;
356 
357 	ret = sdca_regmap_populate_constants(dev, drv->function, defaults);
358 	if (ret < 0)
359 		return ret;
360 
361 	regcache_sort_defaults(defaults, ndefaults);
362 
363 	auxiliary_set_drvdata(auxdev, drv);
364 
365 	config->reg_defaults = defaults;
366 	config->num_reg_defaults = ndefaults;
367 	config->lock_arg = &core->regmap_lock;
368 
369 	if (drv->function->busy_max_delay) {
370 		mbq_config->timeout_us = drv->function->busy_max_delay;
371 		mbq_config->retry_us = umax(drv->function->busy_max_delay / 10,
372 					    mbq_config->retry_us);
373 	}
374 
375 	drv->regmap = devm_regmap_init_sdw_mbq_cfg(dev, core->sdw, config, mbq_config);
376 	if (IS_ERR(drv->regmap))
377 		return dev_err_probe(dev, PTR_ERR(drv->regmap),
378 				     "failed to create regmap");
379 
380 	if (desc->type == SDCA_FUNCTION_TYPE_UAJ)
381 		cmp_drv->set_jack = class_function_set_jack;
382 
383 	ret = sdca_asoc_populate_component(dev, drv->function, cmp_drv,
384 					   &dais, &num_dais,
385 					   &class_function_sdw_ops);
386 	if (ret)
387 		return ret;
388 
389 	dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
390 
391 	pm_runtime_set_autosuspend_delay(dev, 200);
392 	pm_runtime_use_autosuspend(dev);
393 	pm_runtime_set_active(dev);
394 	pm_runtime_get_noresume(dev);
395 
396 	ret = devm_pm_runtime_enable(dev);
397 	if (ret)
398 		return ret;
399 
400 	ret = class_function_boot(drv);
401 	if (ret)
402 		return ret;
403 
404 	ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais);
405 	if (ret)
406 		return dev_err_probe(dev, ret, "failed to register component\n");
407 
408 	pm_runtime_mark_last_busy(dev);
409 	pm_runtime_put_autosuspend(dev);
410 
411 	return 0;
412 }
413 
class_function_remove(struct auxiliary_device * auxdev)414 static void class_function_remove(struct auxiliary_device *auxdev)
415 {
416 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
417 
418 	sdca_irq_cleanup(drv->dev, drv->function, drv->core->irq_info);
419 }
420 
class_function_runtime_suspend(struct device * dev)421 static int class_function_runtime_suspend(struct device *dev)
422 {
423 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
424 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
425 
426 	/*
427 	 * Whilst the driver doesn't power the chip down here, going into
428 	 * runtime suspend means the driver can't be sure the bus won't
429 	 * power down which would prevent communication with the device.
430 	 */
431 	regcache_cache_only(drv->regmap, true);
432 
433 	return 0;
434 }
435 
class_function_runtime_resume(struct device * dev)436 static int class_function_runtime_resume(struct device *dev)
437 {
438 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
439 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
440 	int ret;
441 
442 	guard(mutex)(&drv->core->init_lock);
443 
444 	regcache_mark_dirty(drv->regmap);
445 	regcache_cache_only(drv->regmap, false);
446 
447 	if (drv->suspended) {
448 		unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
449 						SDCA_ENTITY_TYPE_ENTITY_0,
450 						SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
451 		unsigned int val;
452 
453 		ret = regmap_read(drv->regmap, reg, &val);
454 		if (ret < 0) {
455 			dev_err(drv->dev, "failed to read function status: %d\n", ret);
456 			goto err;
457 		}
458 
459 		ret = class_function_init_device(drv, val);
460 		if (ret)
461 			goto err;
462 
463 		sdca_irq_enable_early(drv->function, drv->core->irq_info);
464 
465 		ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
466 		if (ret)
467 			goto err;
468 
469 		sdca_irq_enable(drv->function, drv->core->irq_info);
470 
471 		ret = regmap_write(drv->regmap, reg, 0xFF);
472 		if (ret < 0) {
473 			dev_err(drv->dev, "failed to clear function status: %d\n", ret);
474 			goto err;
475 		}
476 
477 		drv->suspended = false;
478 	}
479 
480 	ret = regcache_sync(drv->regmap);
481 	if (ret) {
482 		dev_err(drv->dev, "failed to restore register cache: %d\n", ret);
483 		goto err;
484 	}
485 
486 	return 0;
487 
488 err:
489 	regcache_cache_only(drv->regmap, true);
490 
491 	return ret;
492 }
493 
class_function_suspend(struct device * dev)494 static int class_function_suspend(struct device *dev)
495 {
496 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
497 	struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
498 	int ret;
499 
500 	drv->suspended = true;
501 
502 	/* Ensure runtime resume runs on resume */
503 	ret = pm_runtime_resume_and_get(dev);
504 	if (ret) {
505 		dev_err(dev, "failed to resume for suspend: %d\n", ret);
506 		return ret;
507 	}
508 
509 	sdca_irq_disable(drv->function, drv->core->irq_info);
510 
511 	ret = pm_runtime_force_suspend(dev);
512 	if (ret) {
513 		dev_err(dev, "failed to force suspend: %d\n", ret);
514 		return ret;
515 	}
516 
517 	pm_runtime_put_noidle(dev);
518 
519 	return 0;
520 }
521 
class_function_resume(struct device * dev)522 static int class_function_resume(struct device *dev)
523 {
524 	int ret;
525 
526 	ret = pm_runtime_force_resume(dev);
527 	if (ret) {
528 		dev_err(dev, "failed to force resume: %d\n", ret);
529 		return ret;
530 	}
531 
532 	return 0;
533 }
534 
535 static const struct dev_pm_ops class_function_pm_ops = {
536 	SYSTEM_SLEEP_PM_OPS(class_function_suspend, class_function_resume)
537 	RUNTIME_PM_OPS(class_function_runtime_suspend,
538 		       class_function_runtime_resume, NULL)
539 };
540 
541 static const struct auxiliary_device_id class_function_id_table[] = {
542 	{
543 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME,
544 		.driver_data = SDCA_FUNCTION_TYPE_SMART_AMP,
545 	},
546 	{
547 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME,
548 		.driver_data = SDCA_FUNCTION_TYPE_SMART_MIC,
549 	},
550 	{
551 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME,
552 		.driver_data = SDCA_FUNCTION_TYPE_UAJ,
553 	},
554 	{
555 		.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME,
556 		.driver_data = SDCA_FUNCTION_TYPE_HID,
557 	},
558 	{},
559 };
560 MODULE_DEVICE_TABLE(auxiliary, class_function_id_table);
561 
562 static struct auxiliary_driver class_function_drv = {
563 	.driver = {
564 		.name		= "sdca_function",
565 		.pm		= pm_ptr(&class_function_pm_ops),
566 	},
567 
568 	.probe		= class_function_probe,
569 	.remove		= class_function_remove,
570 	.id_table	= class_function_id_table
571 };
572 module_auxiliary_driver(class_function_drv);
573 
574 MODULE_LICENSE("GPL");
575 MODULE_DESCRIPTION("SDCA Class Function Driver");
576 MODULE_IMPORT_NS("SND_SOC_SDCA");
577