xref: /linux/sound/core/pcm_drm_eld.c (revision ef815d2cba782e96b9aad9483523d474ed41c62a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  PCM DRM helpers
4  */
5 #include <linux/bitfield.h>
6 #include <linux/export.h>
7 #include <linux/hdmi.h>
8 #include <drm/drm_edid.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_drm_eld.h>
11 
12 #define SAD0_CHANNELS_MASK	GENMASK(2, 0) /* max number of channels - 1 */
13 #define SAD0_FORMAT_MASK	GENMASK(6, 3) /* audio format */
14 
15 #define SAD1_RATE_MASK		GENMASK(6, 0) /* bitfield of supported rates */
16 #define SAD1_RATE_32000_MASK	BIT(0)
17 #define SAD1_RATE_44100_MASK	BIT(1)
18 #define SAD1_RATE_48000_MASK	BIT(2)
19 #define SAD1_RATE_88200_MASK	BIT(3)
20 #define SAD1_RATE_96000_MASK	BIT(4)
21 #define SAD1_RATE_176400_MASK	BIT(5)
22 #define SAD1_RATE_192000_MASK	BIT(6)
23 
24 static const unsigned int eld_rates[] = {
25 	32000,
26 	44100,
27 	48000,
28 	88200,
29 	96000,
30 	176400,
31 	192000,
32 };
33 
34 static unsigned int map_rate_families(const u8 *sad,
35 				      unsigned int mask_32000,
36 				      unsigned int mask_44100,
37 				      unsigned int mask_48000)
38 {
39 	unsigned int rate_mask = 0;
40 
41 	if (sad[1] & SAD1_RATE_32000_MASK)
42 		rate_mask |= mask_32000;
43 	if (sad[1] & (SAD1_RATE_44100_MASK | SAD1_RATE_88200_MASK | SAD1_RATE_176400_MASK))
44 		rate_mask |= mask_44100;
45 	if (sad[1] & (SAD1_RATE_48000_MASK | SAD1_RATE_96000_MASK | SAD1_RATE_192000_MASK))
46 		rate_mask |= mask_48000;
47 	return rate_mask;
48 }
49 
50 static unsigned int sad_rate_mask(const u8 *sad)
51 {
52 	switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
53 	case HDMI_AUDIO_CODING_TYPE_PCM:
54 		return sad[1] & SAD1_RATE_MASK;
55 	case HDMI_AUDIO_CODING_TYPE_AC3:
56 	case HDMI_AUDIO_CODING_TYPE_DTS:
57 		return map_rate_families(sad,
58 					 SAD1_RATE_32000_MASK,
59 					 SAD1_RATE_44100_MASK,
60 					 SAD1_RATE_48000_MASK);
61 	case HDMI_AUDIO_CODING_TYPE_EAC3:
62 	case HDMI_AUDIO_CODING_TYPE_DTS_HD:
63 	case HDMI_AUDIO_CODING_TYPE_MLP:
64 		return map_rate_families(sad,
65 					 0,
66 					 SAD1_RATE_176400_MASK,
67 					 SAD1_RATE_192000_MASK);
68 	default:
69 		/* TODO adjust for other compressed formats as well */
70 		return sad[1] & SAD1_RATE_MASK;
71 	}
72 }
73 
74 static unsigned int sad_max_channels(const u8 *sad)
75 {
76 	switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
77 	case HDMI_AUDIO_CODING_TYPE_PCM:
78 		return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
79 	case HDMI_AUDIO_CODING_TYPE_AC3:
80 	case HDMI_AUDIO_CODING_TYPE_DTS:
81 	case HDMI_AUDIO_CODING_TYPE_EAC3:
82 		return 2;
83 	case HDMI_AUDIO_CODING_TYPE_DTS_HD:
84 	case HDMI_AUDIO_CODING_TYPE_MLP:
85 		return 8;
86 	default:
87 		/* TODO adjust for other compressed formats as well */
88 		return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
89 	}
90 }
91 
92 static int eld_limit_rates(struct snd_pcm_hw_params *params,
93 			   struct snd_pcm_hw_rule *rule)
94 {
95 	struct snd_interval *r = hw_param_interval(params, rule->var);
96 	const struct snd_interval *c;
97 	unsigned int rate_mask = 7, i;
98 	const u8 *sad, *eld = rule->private;
99 
100 	sad = drm_eld_sad(eld);
101 	if (sad) {
102 		c = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
103 
104 		for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3) {
105 			unsigned max_channels = sad_max_channels(sad);
106 
107 			/*
108 			 * Exclude SADs which do not include the
109 			 * requested number of channels.
110 			 */
111 			if (c->min <= max_channels)
112 				rate_mask |= sad_rate_mask(sad);
113 		}
114 	}
115 
116 	return snd_interval_list(r, ARRAY_SIZE(eld_rates), eld_rates,
117 				 rate_mask);
118 }
119 
120 static int eld_limit_channels(struct snd_pcm_hw_params *params,
121 			      struct snd_pcm_hw_rule *rule)
122 {
123 	struct snd_interval *c = hw_param_interval(params, rule->var);
124 	const struct snd_interval *r;
125 	struct snd_interval t = { .min = 1, .max = 2, .integer = 1, };
126 	unsigned int i;
127 	const u8 *sad, *eld = rule->private;
128 
129 	sad = drm_eld_sad(eld);
130 	if (sad) {
131 		unsigned int rate_mask = 0;
132 
133 		/* Convert the rate interval to a mask */
134 		r = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
135 		for (i = 0; i < ARRAY_SIZE(eld_rates); i++)
136 			if (r->min <= eld_rates[i] && r->max >= eld_rates[i])
137 				rate_mask |= BIT(i);
138 
139 		for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3)
140 			if (rate_mask & sad_rate_mask(sad))
141 				t.max = max(t.max, sad_max_channels(sad));
142 	}
143 
144 	return snd_interval_refine(c, &t);
145 }
146 
147 int snd_pcm_hw_constraint_eld(struct snd_pcm_runtime *runtime, void *eld)
148 {
149 	int ret;
150 
151 	ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
152 				  eld_limit_rates, eld,
153 				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
154 	if (ret < 0)
155 		return ret;
156 
157 	ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
158 				  eld_limit_channels, eld,
159 				  SNDRV_PCM_HW_PARAM_RATE, -1);
160 
161 	return ret;
162 }
163 EXPORT_SYMBOL_GPL(snd_pcm_hw_constraint_eld);
164