Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * PCM DRM helpers
4 */
5#include <linux/export.h>
6#include <drm/drm_edid.h>
7#include <sound/pcm.h>
8#include <sound/pcm_drm_eld.h>
9
10static const unsigned int eld_rates[] = {
11 32000,
12 44100,
13 48000,
14 88200,
15 96000,
16 176400,
17 192000,
18};
19
20static unsigned int sad_max_channels(const u8 *sad)
21{
22 return 1 + (sad[0] & 7);
23}
24
25static int eld_limit_rates(struct snd_pcm_hw_params *params,
26 struct snd_pcm_hw_rule *rule)
27{
28 struct snd_interval *r = hw_param_interval(params, rule->var);
29 const struct snd_interval *c;
30 unsigned int rate_mask = 7, i;
31 const u8 *sad, *eld = rule->private;
32
33 sad = drm_eld_sad(eld);
34 if (sad) {
35 c = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
36
37 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3) {
38 unsigned max_channels = sad_max_channels(sad);
39
40 /*
41 * Exclude SADs which do not include the
42 * requested number of channels.
43 */
44 if (c->min <= max_channels)
45 rate_mask |= sad[1];
46 }
47 }
48
49 return snd_interval_list(r, ARRAY_SIZE(eld_rates), eld_rates,
50 rate_mask);
51}
52
53static int eld_limit_channels(struct snd_pcm_hw_params *params,
54 struct snd_pcm_hw_rule *rule)
55{
56 struct snd_interval *c = hw_param_interval(params, rule->var);
57 const struct snd_interval *r;
58 struct snd_interval t = { .min = 1, .max = 2, .integer = 1, };
59 unsigned int i;
60 const u8 *sad, *eld = rule->private;
61
62 sad = drm_eld_sad(eld);
63 if (sad) {
64 unsigned int rate_mask = 0;
65
66 /* Convert the rate interval to a mask */
67 r = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
68 for (i = 0; i < ARRAY_SIZE(eld_rates); i++)
69 if (r->min <= eld_rates[i] && r->max >= eld_rates[i])
70 rate_mask |= BIT(i);
71
72 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3)
73 if (rate_mask & sad[1])
74 t.max = max(t.max, sad_max_channels(sad));
75 }
76
77 return snd_interval_refine(c, &t);
78}
79
80int snd_pcm_hw_constraint_eld(struct snd_pcm_runtime *runtime, void *eld)
81{
82 int ret;
83
84 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
85 eld_limit_rates, eld,
86 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
87 if (ret < 0)
88 return ret;
89
90 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
91 eld_limit_channels, eld,
92 SNDRV_PCM_HW_PARAM_RATE, -1);
93
94 return ret;
95}
96EXPORT_SYMBOL_GPL(snd_pcm_hw_constraint_eld);
1/*
2 * PCM DRM helpers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/export.h>
9#include <drm/drm_edid.h>
10#include <sound/pcm.h>
11#include <sound/pcm_drm_eld.h>
12
13static const unsigned int eld_rates[] = {
14 32000,
15 44100,
16 48000,
17 88200,
18 96000,
19 176400,
20 192000,
21};
22
23static unsigned int sad_max_channels(const u8 *sad)
24{
25 return 1 + (sad[0] & 7);
26}
27
28static int eld_limit_rates(struct snd_pcm_hw_params *params,
29 struct snd_pcm_hw_rule *rule)
30{
31 struct snd_interval *r = hw_param_interval(params, rule->var);
32 struct snd_interval *c;
33 unsigned int rate_mask = 7, i;
34 const u8 *sad, *eld = rule->private;
35
36 sad = drm_eld_sad(eld);
37 if (sad) {
38 c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
39
40 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3) {
41 unsigned max_channels = sad_max_channels(sad);
42
43 /*
44 * Exclude SADs which do not include the
45 * requested number of channels.
46 */
47 if (c->min <= max_channels)
48 rate_mask |= sad[1];
49 }
50 }
51
52 return snd_interval_list(r, ARRAY_SIZE(eld_rates), eld_rates,
53 rate_mask);
54}
55
56static int eld_limit_channels(struct snd_pcm_hw_params *params,
57 struct snd_pcm_hw_rule *rule)
58{
59 struct snd_interval *c = hw_param_interval(params, rule->var);
60 struct snd_interval *r;
61 struct snd_interval t = { .min = 1, .max = 2, .integer = 1, };
62 unsigned int i;
63 const u8 *sad, *eld = rule->private;
64
65 sad = drm_eld_sad(eld);
66 if (sad) {
67 unsigned int rate_mask = 0;
68
69 /* Convert the rate interval to a mask */
70 r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
71 for (i = 0; i < ARRAY_SIZE(eld_rates); i++)
72 if (r->min <= eld_rates[i] && r->max >= eld_rates[i])
73 rate_mask |= BIT(i);
74
75 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3)
76 if (rate_mask & sad[1])
77 t.max = max(t.max, sad_max_channels(sad));
78 }
79
80 return snd_interval_refine(c, &t);
81}
82
83int snd_pcm_hw_constraint_eld(struct snd_pcm_runtime *runtime, void *eld)
84{
85 int ret;
86
87 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
88 eld_limit_rates, eld,
89 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
90 if (ret < 0)
91 return ret;
92
93 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
94 eld_limit_channels, eld,
95 SNDRV_PCM_HW_PARAM_RATE, -1);
96
97 return ret;
98}
99EXPORT_SYMBOL_GPL(snd_pcm_hw_constraint_eld);