1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Analog Devices ADV7511 HDMI transmitter driver
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Copyright (c) 2016, Linaro Limited
7 */
8
9 #include <sound/core.h>
10 #include <sound/hdmi-codec.h>
11 #include <sound/pcm.h>
12 #include <sound/soc.h>
13 #include <linux/of_graph.h>
14
15 #include "adv7511.h"
16
adv7511_calc_cts_n(unsigned int f_tmds,unsigned int fs,unsigned int * cts,unsigned int * n)17 static void adv7511_calc_cts_n(unsigned int f_tmds, unsigned int fs,
18 unsigned int *cts, unsigned int *n)
19 {
20 switch (fs) {
21 case 32000:
22 case 48000:
23 case 96000:
24 case 192000:
25 *n = fs * 128 / 1000;
26 break;
27 case 44100:
28 case 88200:
29 case 176400:
30 *n = fs * 128 / 900;
31 break;
32 }
33
34 *cts = ((f_tmds * *n) / (128 * fs)) * 1000;
35 }
36
adv7511_update_cts_n(struct adv7511 * adv7511)37 static int adv7511_update_cts_n(struct adv7511 *adv7511)
38 {
39 unsigned int cts = 0;
40 unsigned int n = 0;
41
42 adv7511_calc_cts_n(adv7511->f_tmds, adv7511->f_audio, &cts, &n);
43
44 regmap_write(adv7511->regmap, ADV7511_REG_N0, (n >> 16) & 0xf);
45 regmap_write(adv7511->regmap, ADV7511_REG_N1, (n >> 8) & 0xff);
46 regmap_write(adv7511->regmap, ADV7511_REG_N2, n & 0xff);
47
48 regmap_write(adv7511->regmap, ADV7511_REG_CTS_MANUAL0,
49 (cts >> 16) & 0xf);
50 regmap_write(adv7511->regmap, ADV7511_REG_CTS_MANUAL1,
51 (cts >> 8) & 0xff);
52 regmap_write(adv7511->regmap, ADV7511_REG_CTS_MANUAL2,
53 cts & 0xff);
54
55 return 0;
56 }
57
adv7511_hdmi_hw_params(struct device * dev,void * data,struct hdmi_codec_daifmt * fmt,struct hdmi_codec_params * hparms)58 int adv7511_hdmi_hw_params(struct device *dev, void *data,
59 struct hdmi_codec_daifmt *fmt,
60 struct hdmi_codec_params *hparms)
61 {
62 struct adv7511 *adv7511 = dev_get_drvdata(dev);
63 unsigned int audio_source, i2s_format = 0;
64 unsigned int invert_clock;
65 unsigned int rate;
66 unsigned int len;
67
68 switch (hparms->sample_rate) {
69 case 32000:
70 rate = ADV7511_SAMPLE_FREQ_32000;
71 break;
72 case 44100:
73 rate = ADV7511_SAMPLE_FREQ_44100;
74 break;
75 case 48000:
76 rate = ADV7511_SAMPLE_FREQ_48000;
77 break;
78 case 88200:
79 rate = ADV7511_SAMPLE_FREQ_88200;
80 break;
81 case 96000:
82 rate = ADV7511_SAMPLE_FREQ_96000;
83 break;
84 case 176400:
85 rate = ADV7511_SAMPLE_FREQ_176400;
86 break;
87 case 192000:
88 rate = ADV7511_SAMPLE_FREQ_192000;
89 break;
90 default:
91 return -EINVAL;
92 }
93
94 switch (hparms->sample_width) {
95 case 16:
96 len = ADV7511_I2S_SAMPLE_LEN_16;
97 break;
98 case 18:
99 len = ADV7511_I2S_SAMPLE_LEN_18;
100 break;
101 case 20:
102 len = ADV7511_I2S_SAMPLE_LEN_20;
103 break;
104 case 24:
105 len = ADV7511_I2S_SAMPLE_LEN_24;
106 break;
107 default:
108 return -EINVAL;
109 }
110
111 switch (fmt->fmt) {
112 case HDMI_I2S:
113 audio_source = ADV7511_AUDIO_SOURCE_I2S;
114 i2s_format = ADV7511_I2S_FORMAT_I2S;
115 break;
116 case HDMI_RIGHT_J:
117 audio_source = ADV7511_AUDIO_SOURCE_I2S;
118 i2s_format = ADV7511_I2S_FORMAT_RIGHT_J;
119 break;
120 case HDMI_LEFT_J:
121 audio_source = ADV7511_AUDIO_SOURCE_I2S;
122 i2s_format = ADV7511_I2S_FORMAT_LEFT_J;
123 break;
124 case HDMI_SPDIF:
125 audio_source = ADV7511_AUDIO_SOURCE_SPDIF;
126 break;
127 default:
128 return -EINVAL;
129 }
130
131 invert_clock = fmt->bit_clk_inv;
132
133 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_SOURCE, 0x70,
134 audio_source << 4);
135 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CONFIG, BIT(6),
136 invert_clock << 6);
137 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2S_CONFIG, 0x03,
138 i2s_format);
139
140 adv7511->audio_source = audio_source;
141
142 adv7511->f_audio = hparms->sample_rate;
143
144 adv7511_update_cts_n(adv7511);
145
146 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CFG3,
147 ADV7511_AUDIO_CFG3_LEN_MASK, len);
148 regmap_update_bits(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG,
149 ADV7511_I2C_FREQ_ID_CFG_RATE_MASK, rate << 4);
150 regmap_write(adv7511->regmap, 0x73, 0x1);
151
152 return 0;
153 }
154
audio_startup(struct device * dev,void * data)155 static int audio_startup(struct device *dev, void *data)
156 {
157 struct adv7511 *adv7511 = dev_get_drvdata(dev);
158
159 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CONFIG,
160 BIT(7), 0);
161
162 /* hide Audio infoframe updates */
163 regmap_update_bits(adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
164 BIT(5), BIT(5));
165 /* enable N/CTS, enable Audio sample packets */
166 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
167 BIT(5), BIT(5));
168 /* enable N/CTS */
169 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
170 BIT(6), BIT(6));
171 /* not copyrighted */
172 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CFG1,
173 BIT(5), BIT(5));
174 /* enable audio infoframes */
175 regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
176 BIT(3), BIT(3));
177 /* AV mute disable */
178 regmap_update_bits(adv7511->regmap, ADV7511_REG_GC(0),
179 BIT(7) | BIT(6), BIT(7));
180 /* use Audio infoframe updated info */
181 regmap_update_bits(adv7511->regmap, ADV7511_REG_GC(1),
182 BIT(5), 0);
183 /* enable SPDIF receiver */
184 if (adv7511->audio_source == ADV7511_AUDIO_SOURCE_SPDIF)
185 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CONFIG,
186 BIT(7), BIT(7));
187
188 return 0;
189 }
190
audio_shutdown(struct device * dev,void * data)191 static void audio_shutdown(struct device *dev, void *data)
192 {
193 struct adv7511 *adv7511 = dev_get_drvdata(dev);
194
195 if (adv7511->audio_source == ADV7511_AUDIO_SOURCE_SPDIF)
196 regmap_update_bits(adv7511->regmap, ADV7511_REG_AUDIO_CONFIG,
197 BIT(7), 0);
198 }
199
adv7511_hdmi_i2s_get_dai_id(struct snd_soc_component * component,struct device_node * endpoint)200 static int adv7511_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
201 struct device_node *endpoint)
202 {
203 struct of_endpoint of_ep;
204 int ret;
205
206 ret = of_graph_parse_endpoint(endpoint, &of_ep);
207 if (ret < 0)
208 return ret;
209
210 /*
211 * HDMI sound should be located as reg = <2>
212 * Then, it is sound port 0
213 */
214 if (of_ep.port == 2)
215 return 0;
216
217 return -EINVAL;
218 }
219
220 static const struct hdmi_codec_ops adv7511_codec_ops = {
221 .hw_params = adv7511_hdmi_hw_params,
222 .audio_shutdown = audio_shutdown,
223 .audio_startup = audio_startup,
224 .get_dai_id = adv7511_hdmi_i2s_get_dai_id,
225 };
226
227 static const struct hdmi_codec_pdata codec_data = {
228 .ops = &adv7511_codec_ops,
229 .max_i2s_channels = 2,
230 .i2s = 1,
231 .spdif = 1,
232 };
233
adv7511_audio_init(struct device * dev,struct adv7511 * adv7511)234 int adv7511_audio_init(struct device *dev, struct adv7511 *adv7511)
235 {
236 adv7511->audio_pdev = platform_device_register_data(dev,
237 HDMI_CODEC_DRV_NAME,
238 PLATFORM_DEVID_AUTO,
239 &codec_data,
240 sizeof(codec_data));
241 return PTR_ERR_OR_ZERO(adv7511->audio_pdev);
242 }
243
adv7511_audio_exit(struct adv7511 * adv7511)244 void adv7511_audio_exit(struct adv7511 *adv7511)
245 {
246 if (adv7511->audio_pdev) {
247 platform_device_unregister(adv7511->audio_pdev);
248 adv7511->audio_pdev = NULL;
249 }
250 }
251