1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
// SPDX-License-Identifier: GPL-2.0-only
//
// Common code for Cirrus Logic Smart Amplifiers
//
// Copyright (C) 2024 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
#include <asm/byteorder.h>
#include <kunit/static_stub.h>
#include <linux/cleanup.h>
#include <linux/debugfs.h>
#include <linux/dev_printk.h>
#include <linux/efi.h>
#include <linux/firmware/cirrus/cs_dsp.h>
#include <linux/math64.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/timekeeping.h>
#include <linux/types.h>
#include <sound/cs-amp-lib.h>
#define CIRRUS_LOGIC_CALIBRATION_EFI_NAME L"CirrusSmartAmpCalibrationData"
#define CIRRUS_LOGIC_CALIBRATION_EFI_GUID \
EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3)
#define LENOVO_SPEAKER_ID_EFI_NAME L"SdwSpeaker"
#define LENOVO_SPEAKER_ID_EFI_GUID \
EFI_GUID(0x48df970e, 0xe27f, 0x460a, 0xb5, 0x86, 0x77, 0x19, 0x80, 0x1d, 0x92, 0x82)
#define HP_SPEAKER_ID_EFI_NAME L"HPSpeakerID"
#define HP_SPEAKER_ID_EFI_GUID \
EFI_GUID(0xc49593a4, 0xd099, 0x419b, 0xa2, 0xc3, 0x67, 0xe9, 0x80, 0xe6, 0x1d, 0x1e)
#define HP_CALIBRATION_EFI_NAME L"SmartAmpCalibrationData"
#define HP_CALIBRATION_EFI_GUID \
EFI_GUID(0x53559579, 0x8753, 0x4f5c, 0x91, 0x30, 0xe8, 0x2a, 0xcf, 0xb8, 0xd8, 0x93)
static const struct cs_amp_lib_cal_efivar {
efi_char16_t *name;
efi_guid_t *guid;
} cs_amp_lib_cal_efivars[] = {
{
.name = HP_CALIBRATION_EFI_NAME,
.guid = &HP_CALIBRATION_EFI_GUID,
},
{
.name = CIRRUS_LOGIC_CALIBRATION_EFI_NAME,
.guid = &CIRRUS_LOGIC_CALIBRATION_EFI_GUID,
},
};
#define CS_AMP_CAL_DEFAULT_EFI_ATTR \
(EFI_VARIABLE_NON_VOLATILE | \
EFI_VARIABLE_BOOTSERVICE_ACCESS | \
EFI_VARIABLE_RUNTIME_ACCESS)
/* Offset from Unix time to Windows time (100ns since 1 Jan 1601) */
#define UNIX_TIME_TO_WINDOWS_TIME_OFFSET 116444736000000000ULL
static DEFINE_MUTEX(cs_amp_efi_cal_write_lock);
static u64 cs_amp_time_now_in_windows_time(void)
{
u64 time_in_100ns = div_u64(ktime_get_real_ns(), 100);
return time_in_100ns + UNIX_TIME_TO_WINDOWS_TIME_OFFSET;
}
static int cs_amp_write_cal_coeff(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
const char *ctl_name, u32 val)
{
struct cs_dsp_coeff_ctl *cs_ctl;
__be32 beval = cpu_to_be32(val);
int ret;
KUNIT_STATIC_STUB_REDIRECT(cs_amp_write_cal_coeff, dsp, controls, ctl_name, val);
if (IS_REACHABLE(CONFIG_FW_CS_DSP)) {
mutex_lock(&dsp->pwr_lock);
cs_ctl = cs_dsp_get_ctl(dsp, ctl_name, controls->mem_region, controls->alg_id);
ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, &beval, sizeof(beval));
mutex_unlock(&dsp->pwr_lock);
if (ret < 0) {
dev_err(dsp->dev, "Failed to write to '%s': %d\n", ctl_name, ret);
return ret;
}
return 0;
}
return -ENODEV;
}
static int cs_amp_read_cal_coeff(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
const char *ctl_name, u32 *val)
{
struct cs_dsp_coeff_ctl *cs_ctl;
__be32 beval;
int ret;
KUNIT_STATIC_STUB_REDIRECT(cs_amp_read_cal_coeff, dsp, controls, ctl_name, val);
if (!IS_REACHABLE(CONFIG_FW_CS_DSP))
return -ENODEV;
scoped_guard(mutex, &dsp->pwr_lock) {
cs_ctl = cs_dsp_get_ctl(dsp, ctl_name, controls->mem_region, controls->alg_id);
ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, &beval, sizeof(beval));
}
if (ret < 0) {
dev_err(dsp->dev, "Failed to write to '%s': %d\n", ctl_name, ret);
return ret;
}
*val = be32_to_cpu(beval);
return 0;
}
static int _cs_amp_write_cal_coeffs(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
const struct cirrus_amp_cal_data *data)
{
int ret;
dev_dbg(dsp->dev, "Calibration: Ambient=%#x, Status=%#x, CalR=%d\n",
data->calAmbient, data->calStatus, data->calR);
if (list_empty(&dsp->ctl_list)) {
dev_info(dsp->dev, "Calibration disabled due to missing firmware controls\n");
return -ENOENT;
}
ret = cs_amp_write_cal_coeff(dsp, controls, controls->ambient, data->calAmbient);
if (ret)
return ret;
ret = cs_amp_write_cal_coeff(dsp, controls, controls->calr, data->calR);
if (ret)
return ret;
ret = cs_amp_write_cal_coeff(dsp, controls, controls->status, data->calStatus);
if (ret)
return ret;
ret = cs_amp_write_cal_coeff(dsp, controls, controls->checksum, data->calR + 1);
if (ret)
return ret;
return 0;
}
static int _cs_amp_read_cal_coeffs(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
struct cirrus_amp_cal_data *data)
{
u64 time;
u32 val;
int ret;
if (list_empty(&dsp->ctl_list)) {
dev_info(dsp->dev, "Calibration disabled due to missing firmware controls\n");
return -ENOENT;
}
ret = cs_amp_read_cal_coeff(dsp, controls, controls->ambient, &val);
if (ret)
return ret;
data->calAmbient = (s8)val;
ret = cs_amp_read_cal_coeff(dsp, controls, controls->calr, &val);
if (ret)
return ret;
data->calR = (u16)val;
ret = cs_amp_read_cal_coeff(dsp, controls, controls->status, &val);
if (ret)
return ret;
data->calStatus = (u8)val;
/* Fill in timestamp */
time = cs_amp_time_now_in_windows_time();
data->calTime[0] = (u32)time;
data->calTime[1] = (u32)(time >> 32);
return 0;
}
/**
* cs_amp_write_cal_coeffs - Write calibration data to firmware controls.
* @dsp: Pointer to struct cs_dsp.
* @controls: Pointer to definition of firmware controls to be written.
* @data: Pointer to calibration data.
*
* Returns: 0 on success, else negative error value.
*/
int cs_amp_write_cal_coeffs(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
const struct cirrus_amp_cal_data *data)
{
if (IS_REACHABLE(CONFIG_FW_CS_DSP) || IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST))
return _cs_amp_write_cal_coeffs(dsp, controls, data);
else
return -ENODEV;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_write_cal_coeffs, "SND_SOC_CS_AMP_LIB");
/**
* cs_amp_read_cal_coeffs - Read calibration data from firmware controls.
* @dsp: Pointer to struct cs_dsp.
* @controls: Pointer to definition of firmware controls to be read.
* @data: Pointer to calibration data where results will be written.
*
* Returns: 0 on success, else negative error value.
*/
int cs_amp_read_cal_coeffs(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
struct cirrus_amp_cal_data *data)
{
if (IS_REACHABLE(CONFIG_FW_CS_DSP) || IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST))
return _cs_amp_read_cal_coeffs(dsp, controls, data);
else
return -ENODEV;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_read_cal_coeffs, "SND_SOC_CS_AMP_LIB");
/**
* cs_amp_write_ambient_temp - write value to calibration ambient temperature
* @dsp: Pointer to struct cs_dsp.
* @controls: Pointer to definition of firmware controls to be read.
* @temp: Temperature in degrees celcius.
*
* Returns: 0 on success, else negative error value.
*/
int cs_amp_write_ambient_temp(struct cs_dsp *dsp,
const struct cirrus_amp_cal_controls *controls,
u32 temp)
{
if (IS_REACHABLE(CONFIG_FW_CS_DSP) || IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST))
return cs_amp_write_cal_coeff(dsp, controls, controls->ambient, temp);
else
return -ENODEV;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_write_ambient_temp, "SND_SOC_CS_AMP_LIB");
static efi_status_t cs_amp_get_efi_variable(efi_char16_t *name,
efi_guid_t *guid,
u32 *returned_attr,
unsigned long *size,
void *buf)
{
u32 attr;
if (!returned_attr)
returned_attr = &attr;
KUNIT_STATIC_STUB_REDIRECT(cs_amp_get_efi_variable, name, guid,
returned_attr, size, buf);
if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
return efi.get_variable(name, guid, returned_attr, size, buf);
return EFI_NOT_FOUND;
}
static efi_status_t cs_amp_set_efi_variable(efi_char16_t *name,
efi_guid_t *guid,
u32 attr,
unsigned long size,
void *buf)
{
KUNIT_STATIC_STUB_REDIRECT(cs_amp_set_efi_variable, name, guid, attr, size, buf);
if (!efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
return EFI_NOT_FOUND;
return efi.set_variable(name, guid, attr, size, buf);
}
static int cs_amp_convert_efi_status(efi_status_t status)
{
switch (status) {
case EFI_SUCCESS:
return 0;
case EFI_NOT_FOUND:
return -ENOENT;
case EFI_BUFFER_TOO_SMALL:
return -EFBIG;
case EFI_WRITE_PROTECTED:
case EFI_UNSUPPORTED:
case EFI_ACCESS_DENIED:
case EFI_SECURITY_VIOLATION:
return -EACCES;
default:
return -EIO;
}
}
static struct cirrus_amp_efi_data *cs_amp_get_cal_efi_buffer(struct device *dev,
efi_char16_t **name,
efi_guid_t **guid,
u32 *attr)
{
struct cirrus_amp_efi_data *efi_data __free(kfree) = NULL;
unsigned long data_size = 0;
efi_status_t status;
int i, ret;
/* Find EFI variable and get size */
for (i = 0; i < ARRAY_SIZE(cs_amp_lib_cal_efivars); i++) {
status = cs_amp_get_efi_variable(cs_amp_lib_cal_efivars[i].name,
cs_amp_lib_cal_efivars[i].guid,
attr, &data_size, NULL);
if (status == EFI_BUFFER_TOO_SMALL)
break;
}
if (status != EFI_BUFFER_TOO_SMALL)
return ERR_PTR(-ENOENT);
if (name)
*name = cs_amp_lib_cal_efivars[i].name;
if (guid)
*guid = cs_amp_lib_cal_efivars[i].guid;
if (data_size < sizeof(*efi_data)) {
dev_err(dev, "EFI cal variable truncated\n");
return ERR_PTR(-EOVERFLOW);
}
/* Get variable contents into buffer */
efi_data = kmalloc(data_size, GFP_KERNEL);
if (!efi_data)
return ERR_PTR(-ENOMEM);
status = cs_amp_get_efi_variable(cs_amp_lib_cal_efivars[i].name,
cs_amp_lib_cal_efivars[i].guid,
attr, &data_size, efi_data);
if (status != EFI_SUCCESS) {
ret = -EINVAL;
goto err;
}
dev_dbg(dev, "Calibration: Size=%d, Amp Count=%d\n", efi_data->size, efi_data->count);
if ((efi_data->count > 128) ||
struct_size(efi_data, data, efi_data->count) > data_size) {
dev_err(dev, "EFI cal variable truncated\n");
ret = -EOVERFLOW;
goto err;
}
/* This could be zero-filled space pre-allocated by the BIOS */
if (efi_data->size == 0)
efi_data->size = data_size;
return_ptr(efi_data);
err:
dev_err(dev, "Failed to read calibration data from EFI: %d\n", ret);
return ERR_PTR(ret);
}
static int cs_amp_set_cal_efi_buffer(struct device *dev,
efi_char16_t *name,
efi_guid_t *guid,
u32 attr,
struct cirrus_amp_efi_data *data)
{
efi_status_t status;
status = cs_amp_set_efi_variable(name, guid, attr,
struct_size(data, data, data->count), data);
return cs_amp_convert_efi_status(status);
}
static int _cs_amp_get_efi_calibration_data(struct device *dev, u64 target_uid, int amp_index,
struct cirrus_amp_cal_data *out_data)
{
struct cirrus_amp_efi_data *efi_data __free(kfree) = NULL;
struct cirrus_amp_cal_data *cal = NULL;
int i;
efi_data = cs_amp_get_cal_efi_buffer(dev, NULL, NULL, NULL);
if (IS_ERR(efi_data))
return PTR_ERR(efi_data);
if (target_uid) {
for (i = 0; i < efi_data->count; ++i) {
u64 cal_target = cs_amp_cal_target_u64(&efi_data->data[i]);
/* Skip empty entries */
if (!efi_data->data[i].calTime[0] && !efi_data->data[i].calTime[1])
continue;
/* Skip entries with unpopulated silicon ID */
if (cal_target == 0)
continue;
if (cal_target == target_uid) {
cal = &efi_data->data[i];
break;
}
}
}
if (!cal && (amp_index >= 0) && (amp_index < efi_data->count) &&
(efi_data->data[amp_index].calTime[0] || efi_data->data[amp_index].calTime[1])) {
u64 cal_target = cs_amp_cal_target_u64(&efi_data->data[amp_index]);
/*
* Treat unpopulated cal_target as a wildcard.
* If target_uid != 0 we can only get here if cal_target == 0
* or it didn't match any cal_target value.
* If target_uid == 0 it is a wildcard.
*/
if ((cal_target == 0) || (target_uid == 0))
cal = &efi_data->data[amp_index];
else
dev_warn(dev, "Calibration entry %d does not match silicon ID", amp_index);
}
if (!cal) {
dev_warn(dev, "No calibration for silicon ID %#llx\n", target_uid);
return -ENOENT;
}
memcpy(out_data, cal, sizeof(*out_data));
return 0;
}
static int _cs_amp_set_efi_calibration_data(struct device *dev, int amp_index, int num_amps,
const struct cirrus_amp_cal_data *in_data)
{
u64 cal_target = cs_amp_cal_target_u64(in_data);
unsigned long num_entries;
struct cirrus_amp_efi_data *data __free(kfree) = NULL;
efi_char16_t *name = CIRRUS_LOGIC_CALIBRATION_EFI_NAME;
efi_guid_t *guid = &CIRRUS_LOGIC_CALIBRATION_EFI_GUID;
u32 attr = CS_AMP_CAL_DEFAULT_EFI_ATTR;
int i, ret;
if (cal_target == 0)
return -EINVAL;
data = cs_amp_get_cal_efi_buffer(dev, &name, &guid, &attr);
ret = PTR_ERR_OR_ZERO(data);
if (ret == -ENOENT) {
data = NULL;
goto alloc_new;
} else if (ret) {
return ret;
}
/*
* If the EFI variable is just zero-filled reserved space the count
* must be set.
*/
if (data->count == 0)
data->count = (data->size - sizeof(data)) / sizeof(data->data[0]);
if (amp_index < 0) {
/* Is there already a slot for this target? */
for (amp_index = 0; amp_index < data->count; amp_index++) {
if (cs_amp_cal_target_u64(&data->data[amp_index]) == cal_target)
break;
}
/* Else find an empty slot */
if (amp_index >= data->count) {
for (amp_index = 0; amp_index < data->count; amp_index++) {
if ((data->data[amp_index].calTime[0] == 0) &&
(data->data[amp_index].calTime[1] == 0))
break;
}
}
} else {
/*
* If the index is forced there could be another active
* slot with the same calTarget. So deduplicate.
*/
for (i = 0; i < data->count; i++) {
if (i == amp_index)
continue;
if ((data->data[i].calTime[0] == 0) && (data->data[i].calTime[1] == 0))
continue;
if (cs_amp_cal_target_u64(&data->data[i]) == cal_target)
memset(data->data[i].calTime, 0, sizeof(data->data[i].calTime));
}
}
alloc_new:
if (amp_index < 0)
amp_index = 0;
num_entries = max(num_amps, amp_index + 1);
if (!data || (data->count < num_entries)) {
struct cirrus_amp_efi_data *old_data __free(kfree) = no_free_ptr(data);
unsigned int new_data_size = struct_size(data, data, num_entries);
data = kzalloc(new_data_size, GFP_KERNEL);
if (!data)
return -ENOMEM;
if (old_data)
memcpy(data, old_data, struct_size(old_data, data, old_data->count));
data->count = num_entries;
data->size = new_data_size;
}
data->data[amp_index] = *in_data;
ret = cs_amp_set_cal_efi_buffer(dev, name, guid, attr, data);
if (ret) {
dev_err(dev, "Failed writing calibration to EFI: %d\n", ret);
return ret;
}
return 0;
}
/**
* cs_amp_get_efi_calibration_data - get an entry from calibration data in EFI.
* @dev: struct device of the caller.
* @target_uid: UID to match, or zero to ignore UID matching.
* @amp_index: Entry index to use, or -1 to prevent lookup by index.
* @out_data: struct cirrus_amp_cal_data where the entry will be copied.
*
* This function can perform 3 types of lookup:
*
* (target_uid > 0, amp_index >= 0)
* UID search with fallback to using the array index.
* Search the calibration data for a non-zero calTarget that matches
* target_uid, and if found return that entry. Else, if the entry at
* [amp_index] has calTarget == 0, return that entry. Else fail.
*
* (target_uid > 0, amp_index < 0)
* UID search only.
* Search the calibration data for a non-zero calTarget that matches
* target_uid, and if found return that entry. Else fail.
*
* (target_uid == 0, amp_index >= 0)
* Array index fetch only.
* Return the entry at [amp_index].
*
* An array lookup will be skipped if amp_index exceeds the number of
* entries in the calibration array, and in this case the return will
* be -ENOENT. An out-of-range amp_index does not prevent matching by
* target_uid - it has the same effect as passing amp_index < 0.
*
* If the EFI data is too short to be a valid entry, or the entry count
* in the EFI data overflows the actual length of the data, this function
* returns -EOVERFLOW.
*
* Return: 0 if the entry was found, -ENOENT if no entry was found,
* -EOVERFLOW if the EFI file is corrupt, else other error value.
*/
int cs_amp_get_efi_calibration_data(struct device *dev, u64 target_uid, int amp_index,
struct cirrus_amp_cal_data *out_data)
{
if (IS_ENABLED(CONFIG_EFI) || IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST))
return _cs_amp_get_efi_calibration_data(dev, target_uid, amp_index, out_data);
else
return -ENOENT;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_get_efi_calibration_data, "SND_SOC_CS_AMP_LIB");
/**
* cs_amp_set_efi_calibration_data - write a calibration data entry to EFI.
* @dev: struct device of the caller.
* @amp_index: Entry index to use, or -1 to use any available slot.
* @num_amps: Maximum number of amps to reserve slots for, or -1 to ignore.
* @in_data: struct cirrus_amp_cal_data entry to be written to EFI.
*
* If a Vendor-specific variable exists it will be updated,
* else if the Cirrus variable exists it will be updated
* else the Cirrus variable will be created.
*
* If amp_index >= 0 the data will be placed in this entry of the calibration
* data array, overwriting what was in that entry. Any other entries with the
* same calTarget will be marked empty.
*
* If amp_index < 0 and in_data->calTarget matches any existing entry, that
* entry will be overwritten. Else the first available free entry will be used,
* extending the size of the EFI variable if there are no free entries.
*
* If num_amps > 0 the EFI variable will be sized to contain at least this
* many calibration entries, with any new entries marked empty.
*
* Return: 0 if the write was successful, -EFBIG if space could not be made in
* the EFI file to add the entry, -EACCES if it was not possible to
* read or write the EFI variable.
*/
int cs_amp_set_efi_calibration_data(struct device *dev, int amp_index, int num_amps,
const struct cirrus_amp_cal_data *in_data)
{
if (IS_ENABLED(CONFIG_EFI) || IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST)) {
scoped_guard(mutex, &cs_amp_efi_cal_write_lock) {
return _cs_amp_set_efi_calibration_data(dev, amp_index,
num_amps, in_data);
}
}
return -ENOENT;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_set_efi_calibration_data, "SND_SOC_CS_AMP_LIB");
struct cs_amp_spkid_efi {
efi_char16_t *name;
efi_guid_t *guid;
u8 values[2];
};
static int cs_amp_get_efi_byte_spkid(struct device *dev, const struct cs_amp_spkid_efi *info)
{
efi_status_t status;
unsigned long size;
u8 spkid;
int i, ret;
size = sizeof(spkid);
status = cs_amp_get_efi_variable(info->name, info->guid, NULL, &size, &spkid);
ret = cs_amp_convert_efi_status(status);
if (ret < 0)
return ret;
if (size == 0)
return -ENOENT;
for (i = 0; i < ARRAY_SIZE(info->values); i++) {
if (info->values[i] == spkid)
return i;
}
dev_err(dev, "EFI speaker ID bad value %#x\n", spkid);
return -EINVAL;
}
static const struct cs_amp_spkid_efi cs_amp_spkid_byte_types[] = {
{
.name = LENOVO_SPEAKER_ID_EFI_NAME,
.guid = &LENOVO_SPEAKER_ID_EFI_GUID,
.values = { 0xd0, 0xd1 },
},
{
.name = HP_SPEAKER_ID_EFI_NAME,
.guid = &HP_SPEAKER_ID_EFI_GUID,
.values = { 0x30, 0x31 },
},
};
/**
* cs_amp_get_vendor_spkid - get a speaker ID from vendor-specific storage
* @dev: pointer to struct device
*
* Known vendor-specific methods of speaker ID are checked and if one is
* found its speaker ID value is returned.
*
* Return: >=0 is a valid speaker ID. -ENOENT if a vendor-specific method
* was not found. -EACCES if the vendor-specific storage could not
* be read. Other error values indicate that the data from the
* vendor-specific storage was found but could not be understood.
*/
int cs_amp_get_vendor_spkid(struct device *dev)
{
int i, ret;
if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE) &&
!IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST))
return -ENOENT;
for (i = 0; i < ARRAY_SIZE(cs_amp_spkid_byte_types); i++) {
ret = cs_amp_get_efi_byte_spkid(dev, &cs_amp_spkid_byte_types[i]);
if (ret != -ENOENT)
return ret;
}
return -ENOENT;
}
EXPORT_SYMBOL_NS_GPL(cs_amp_get_vendor_spkid, "SND_SOC_CS_AMP_LIB");
/**
* cs_amp_create_debugfs - create a debugfs directory for a device
*
* @dev: pointer to struct device
*
* Creates a node under "cirrus_logic" in the root of the debugfs filesystem.
* This is for Cirrus-specific debugfs functionality to be grouped in a
* defined way, independently of the debugfs provided by ALSA/ASoC.
* The general ALSA/ASoC debugfs may not be enabled, and does not necessarily
* have a stable layout or naming convention.
*
* Return: Pointer to the dentry for the created directory, or -ENODEV.
*/
struct dentry *cs_amp_create_debugfs(struct device *dev)
{
struct dentry *dir;
dir = debugfs_lookup("cirrus_logic", NULL);
if (!dir)
dir = debugfs_create_dir("cirrus_logic", NULL);
return debugfs_create_dir(dev_name(dev), dir);
}
EXPORT_SYMBOL_NS_GPL(cs_amp_create_debugfs, "SND_SOC_CS_AMP_LIB");
static const struct cs_amp_test_hooks cs_amp_test_hook_ptrs = {
.get_efi_variable = cs_amp_get_efi_variable,
.set_efi_variable = cs_amp_set_efi_variable,
.write_cal_coeff = cs_amp_write_cal_coeff,
.read_cal_coeff = cs_amp_read_cal_coeff,
};
const struct cs_amp_test_hooks * const cs_amp_test_hooks =
PTR_IF(IS_ENABLED(CONFIG_SND_SOC_CS_AMP_LIB_TEST), &cs_amp_test_hook_ptrs);
EXPORT_SYMBOL_NS_GPL(cs_amp_test_hooks, "SND_SOC_CS_AMP_LIB");
MODULE_DESCRIPTION("Cirrus Logic amplifier library");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("FW_CS_DSP");
|