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