1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog Devices ADV7511 HDMI transmitter driver
5 * Copyright 2012 Analog Devices Inc.
6 * Copyright (c) 2016, Linaro Limited
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>
17 static void adv7511_calc_cts_n(unsigned int f_tmds
, unsigned int fs
,
18 unsigned int *cts
, unsigned int *n
)
34 *cts
= ((f_tmds
* *n
) / (128 * fs
)) * 1000;
37 static int adv7511_update_cts_n(struct adv7511
*adv7511
)
42 adv7511_calc_cts_n(adv7511
->f_tmds
, adv7511
->f_audio
, &cts
, &n
);
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);
48 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL0
,
50 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL1
,
52 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL2
,
58 static int adv7511_hdmi_hw_params(struct device
*dev
, void *data
,
59 struct hdmi_codec_daifmt
*fmt
,
60 struct hdmi_codec_params
*hparms
)
62 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
63 unsigned int audio_source
, i2s_format
= 0;
64 unsigned int invert_clock
;
68 switch (hparms
->sample_rate
) {
70 rate
= ADV7511_SAMPLE_FREQ_32000
;
73 rate
= ADV7511_SAMPLE_FREQ_44100
;
76 rate
= ADV7511_SAMPLE_FREQ_48000
;
79 rate
= ADV7511_SAMPLE_FREQ_88200
;
82 rate
= ADV7511_SAMPLE_FREQ_96000
;
85 rate
= ADV7511_SAMPLE_FREQ_176400
;
88 rate
= ADV7511_SAMPLE_FREQ_192000
;
94 switch (hparms
->sample_width
) {
96 len
= ADV7511_I2S_SAMPLE_LEN_16
;
99 len
= ADV7511_I2S_SAMPLE_LEN_18
;
102 len
= ADV7511_I2S_SAMPLE_LEN_20
;
105 len
= ADV7511_I2S_SAMPLE_LEN_24
;
113 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
114 i2s_format
= ADV7511_I2S_FORMAT_I2S
;
117 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
118 i2s_format
= ADV7511_I2S_FORMAT_RIGHT_J
;
121 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
122 i2s_format
= ADV7511_I2S_FORMAT_LEFT_J
;
125 audio_source
= ADV7511_AUDIO_SOURCE_SPDIF
;
131 invert_clock
= fmt
->bit_clk_inv
;
133 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_SOURCE
, 0x70,
135 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
, BIT(6),
137 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2S_CONFIG
, 0x03,
140 adv7511
->audio_source
= audio_source
;
142 adv7511
->f_audio
= hparms
->sample_rate
;
144 adv7511_update_cts_n(adv7511
);
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);
155 static int audio_startup(struct device
*dev
, void *data
)
157 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
159 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
,
162 /* hide Audio infoframe updates */
163 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_INFOFRAME_UPDATE
,
165 /* enable N/CTS, enable Audio sample packets */
166 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
169 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
171 /* not copyrighted */
172 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CFG1
,
174 /* enable audio infoframes */
175 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
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),
183 /* enable SPDIF receiver */
184 if (adv7511
->audio_source
== ADV7511_AUDIO_SOURCE_SPDIF
)
185 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
,
191 static void audio_shutdown(struct device
*dev
, void *data
)
193 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
195 if (adv7511
->audio_source
== ADV7511_AUDIO_SOURCE_SPDIF
)
196 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
,
200 static int adv7511_hdmi_i2s_get_dai_id(struct snd_soc_component
*component
,
201 struct device_node
*endpoint
)
203 struct of_endpoint of_ep
;
206 ret
= of_graph_parse_endpoint(endpoint
, &of_ep
);
211 * HDMI sound should be located as reg = <2>
212 * Then, it is sound port 0
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
,
227 static const struct hdmi_codec_pdata codec_data
= {
228 .ops
= &adv7511_codec_ops
,
229 .max_i2s_channels
= 2,
234 int adv7511_audio_init(struct device
*dev
, struct adv7511
*adv7511
)
236 adv7511
->audio_pdev
= platform_device_register_data(dev
,
241 return PTR_ERR_OR_ZERO(adv7511
->audio_pdev
);
244 void adv7511_audio_exit(struct adv7511
*adv7511
)
246 if (adv7511
->audio_pdev
) {
247 platform_device_unregister(adv7511
->audio_pdev
);
248 adv7511
->audio_pdev
= NULL
;