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
)
32 *cts
= ((f_tmds
* *n
) / (128 * fs
)) * 1000;
35 static int adv7511_update_cts_n(struct adv7511
*adv7511
)
40 adv7511_calc_cts_n(adv7511
->f_tmds
, adv7511
->f_audio
, &cts
, &n
);
42 regmap_write(adv7511
->regmap
, ADV7511_REG_N0
, (n
>> 16) & 0xf);
43 regmap_write(adv7511
->regmap
, ADV7511_REG_N1
, (n
>> 8) & 0xff);
44 regmap_write(adv7511
->regmap
, ADV7511_REG_N2
, n
& 0xff);
46 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL0
,
48 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL1
,
50 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL2
,
56 int adv7511_hdmi_hw_params(struct device
*dev
, void *data
,
57 struct hdmi_codec_daifmt
*fmt
,
58 struct hdmi_codec_params
*hparms
)
60 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
61 unsigned int audio_source
, i2s_format
= 0;
62 unsigned int invert_clock
;
66 switch (hparms
->sample_rate
) {
68 rate
= ADV7511_SAMPLE_FREQ_32000
;
71 rate
= ADV7511_SAMPLE_FREQ_44100
;
74 rate
= ADV7511_SAMPLE_FREQ_48000
;
77 rate
= ADV7511_SAMPLE_FREQ_88200
;
80 rate
= ADV7511_SAMPLE_FREQ_96000
;
83 rate
= ADV7511_SAMPLE_FREQ_176400
;
86 rate
= ADV7511_SAMPLE_FREQ_192000
;
92 switch (hparms
->sample_width
) {
94 len
= ADV7511_I2S_SAMPLE_LEN_16
;
97 len
= ADV7511_I2S_SAMPLE_LEN_18
;
100 len
= ADV7511_I2S_SAMPLE_LEN_20
;
103 len
= ADV7511_I2S_SAMPLE_LEN_24
;
111 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
112 i2s_format
= ADV7511_I2S_FORMAT_I2S
;
115 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
116 i2s_format
= ADV7511_I2S_FORMAT_RIGHT_J
;
119 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
120 i2s_format
= ADV7511_I2S_FORMAT_LEFT_J
;
126 invert_clock
= fmt
->bit_clk_inv
;
128 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_SOURCE
, 0x70,
130 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
, BIT(6),
132 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2S_CONFIG
, 0x03,
135 adv7511
->audio_source
= audio_source
;
137 adv7511
->f_audio
= hparms
->sample_rate
;
139 adv7511_update_cts_n(adv7511
);
141 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CFG3
,
142 ADV7511_AUDIO_CFG3_LEN_MASK
, len
);
143 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2C_FREQ_ID_CFG
,
144 ADV7511_I2C_FREQ_ID_CFG_RATE_MASK
, rate
<< 4);
145 regmap_write(adv7511
->regmap
, 0x73, 0x1);
150 static int audio_startup(struct device
*dev
, void *data
)
152 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
154 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
,
157 /* hide Audio infoframe updates */
158 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_INFOFRAME_UPDATE
,
160 /* enable N/CTS, enable Audio sample packets */
161 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
164 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
166 /* not copyrighted */
167 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CFG1
,
169 /* enable audio infoframes */
170 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
172 /* AV mute disable */
173 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_GC(0),
174 BIT(7) | BIT(6), BIT(7));
175 /* use Audio infoframe updated info */
176 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_GC(1),
181 static void audio_shutdown(struct device
*dev
, void *data
)
185 static int adv7511_hdmi_i2s_get_dai_id(struct snd_soc_component
*component
,
186 struct device_node
*endpoint
)
188 struct of_endpoint of_ep
;
191 ret
= of_graph_parse_endpoint(endpoint
, &of_ep
);
196 * HDMI sound should be located as reg = <2>
197 * Then, it is sound port 0
205 static const struct hdmi_codec_ops adv7511_codec_ops
= {
206 .hw_params
= adv7511_hdmi_hw_params
,
207 .audio_shutdown
= audio_shutdown
,
208 .audio_startup
= audio_startup
,
209 .get_dai_id
= adv7511_hdmi_i2s_get_dai_id
,
212 static const struct hdmi_codec_pdata codec_data
= {
213 .ops
= &adv7511_codec_ops
,
214 .max_i2s_channels
= 2,
218 int adv7511_audio_init(struct device
*dev
, struct adv7511
*adv7511
)
220 adv7511
->audio_pdev
= platform_device_register_data(dev
,
225 return PTR_ERR_OR_ZERO(adv7511
->audio_pdev
);
228 void adv7511_audio_exit(struct adv7511
*adv7511
)
230 if (adv7511
->audio_pdev
) {
231 platform_device_unregister(adv7511
->audio_pdev
);
232 adv7511
->audio_pdev
= NULL
;