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