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