2 * Analog Devices ADV7511 HDMI transmitter driver
4 * Copyright 2012 Analog Devices Inc.
5 * Copyright (c) 2016, Linaro Limited
7 * Licensed under the GPL-2.
10 #include <sound/core.h>
11 #include <sound/hdmi-codec.h>
12 #include <sound/pcm.h>
13 #include <sound/soc.h>
14 #include <linux/of_graph.h>
18 static void adv7511_calc_cts_n(unsigned int f_tmds
, unsigned int fs
,
19 unsigned int *cts
, unsigned int *n
)
33 *cts
= ((f_tmds
* *n
) / (128 * fs
)) * 1000;
36 static int adv7511_update_cts_n(struct adv7511
*adv7511
)
41 adv7511_calc_cts_n(adv7511
->f_tmds
, adv7511
->f_audio
, &cts
, &n
);
43 regmap_write(adv7511
->regmap
, ADV7511_REG_N0
, (n
>> 16) & 0xf);
44 regmap_write(adv7511
->regmap
, ADV7511_REG_N1
, (n
>> 8) & 0xff);
45 regmap_write(adv7511
->regmap
, ADV7511_REG_N2
, n
& 0xff);
47 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL0
,
49 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL1
,
51 regmap_write(adv7511
->regmap
, ADV7511_REG_CTS_MANUAL2
,
57 int adv7511_hdmi_hw_params(struct device
*dev
, void *data
,
58 struct hdmi_codec_daifmt
*fmt
,
59 struct hdmi_codec_params
*hparms
)
61 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
62 unsigned int audio_source
, i2s_format
= 0;
63 unsigned int invert_clock
;
67 switch (hparms
->sample_rate
) {
69 rate
= ADV7511_SAMPLE_FREQ_32000
;
72 rate
= ADV7511_SAMPLE_FREQ_44100
;
75 rate
= ADV7511_SAMPLE_FREQ_48000
;
78 rate
= ADV7511_SAMPLE_FREQ_88200
;
81 rate
= ADV7511_SAMPLE_FREQ_96000
;
84 rate
= ADV7511_SAMPLE_FREQ_176400
;
87 rate
= ADV7511_SAMPLE_FREQ_192000
;
93 switch (hparms
->sample_width
) {
95 len
= ADV7511_I2S_SAMPLE_LEN_16
;
98 len
= ADV7511_I2S_SAMPLE_LEN_18
;
101 len
= ADV7511_I2S_SAMPLE_LEN_20
;
104 len
= ADV7511_I2S_SAMPLE_LEN_24
;
112 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
113 i2s_format
= ADV7511_I2S_FORMAT_I2S
;
116 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
117 i2s_format
= ADV7511_I2S_FORMAT_RIGHT_J
;
120 audio_source
= ADV7511_AUDIO_SOURCE_I2S
;
121 i2s_format
= ADV7511_I2S_FORMAT_LEFT_J
;
127 invert_clock
= fmt
->bit_clk_inv
;
129 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_SOURCE
, 0x70,
131 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
, BIT(6),
133 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2S_CONFIG
, 0x03,
136 adv7511
->audio_source
= audio_source
;
138 adv7511
->f_audio
= hparms
->sample_rate
;
140 adv7511_update_cts_n(adv7511
);
142 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CFG3
,
143 ADV7511_AUDIO_CFG3_LEN_MASK
, len
);
144 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_I2C_FREQ_ID_CFG
,
145 ADV7511_I2C_FREQ_ID_CFG_RATE_MASK
, rate
<< 4);
146 regmap_write(adv7511
->regmap
, 0x73, 0x1);
151 static int audio_startup(struct device
*dev
, void *data
)
153 struct adv7511
*adv7511
= dev_get_drvdata(dev
);
155 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CONFIG
,
158 /* hide Audio infoframe updates */
159 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_INFOFRAME_UPDATE
,
161 /* enable N/CTS, enable Audio sample packets */
162 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
165 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
167 /* not copyrighted */
168 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_AUDIO_CFG1
,
170 /* enable audio infoframes */
171 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_PACKET_ENABLE1
,
173 /* AV mute disable */
174 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_GC(0),
175 BIT(7) | BIT(6), BIT(7));
176 /* use Audio infoframe updated info */
177 regmap_update_bits(adv7511
->regmap
, ADV7511_REG_GC(1),
182 static void audio_shutdown(struct device
*dev
, void *data
)
186 static int adv7511_hdmi_i2s_get_dai_id(struct snd_soc_component
*component
,
187 struct device_node
*endpoint
)
189 struct of_endpoint of_ep
;
192 ret
= of_graph_parse_endpoint(endpoint
, &of_ep
);
197 * HDMI sound should be located as reg = <2>
198 * Then, it is sound port 0
206 static const struct hdmi_codec_ops adv7511_codec_ops
= {
207 .hw_params
= adv7511_hdmi_hw_params
,
208 .audio_shutdown
= audio_shutdown
,
209 .audio_startup
= audio_startup
,
210 .get_dai_id
= adv7511_hdmi_i2s_get_dai_id
,
213 static struct hdmi_codec_pdata codec_data
= {
214 .ops
= &adv7511_codec_ops
,
215 .max_i2s_channels
= 2,
219 int adv7511_audio_init(struct device
*dev
, struct adv7511
*adv7511
)
221 adv7511
->audio_pdev
= platform_device_register_data(dev
,
226 return PTR_ERR_OR_ZERO(adv7511
->audio_pdev
);
229 void adv7511_audio_exit(struct adv7511
*adv7511
)
231 if (adv7511
->audio_pdev
) {
232 platform_device_unregister(adv7511
->audio_pdev
);
233 adv7511
->audio_pdev
= NULL
;