Skip to content

Commit 7e56762

Browse files
Jyri SarhaRussell King
authored andcommitted
drm/i2c: tda998x: Register ASoC hdmi-codec and add audio DT binding
Register ASoC HDMI codec for audio functionality and adds device tree binding for audio configuration. With the registered HDMI codec the tda998x node can be used like a regular codec node in ASoC card configurations. HDMI audio info-frame and audio stream header is generated by the ASoC HDMI codec. The codec also applies constraints for available sample-rates based on Edid Like Data from the display. The device tree binding document has been updated [1]. Part of this patch has been inspired by Jean Francoise's "drm/i2c: tda998x: Add support of a DT graph of ports"-patch [2]. There may still be some identical lines left from the original patch and some of the ideas have come from there. [1] Documentation/devicetree/bindings/display/bridge/tda998x.txt [2] http://mailman.alsa-project.org/pipermail/alsa-devel/2015-July/095255.html Signed-off-by: Jyri Sarha <jsarha@ti.com> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
1 parent 95db3b2 commit 7e56762

5 files changed

Lines changed: 239 additions & 5 deletions

File tree

‎Documentation/devicetree/bindings/display/bridge/tda998x.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,31 @@ Optional properties:
2121
- video-ports: 24 bits value which defines how the video controller
2222
output is wired to the TDA998x input - default: <0x230145>
2323

24+
- audio-ports: array of 8-bit values, 2 values per one DAI[1].
25+
The first value defines the DAI type: TDA998x_SPDIF or TDA998x_I2S[2].
26+
The second value defines the tda998x AP_ENA reg content when the DAI
27+
in question is used. The implementation allows one or two DAIs. If two
28+
DAIs are defined, they must be of different type.
29+
30+
[1] Documentation/sound/alsa/soc/DAI.txt
31+
[2] include/dt-bindings/display/tda998x.h
32+
2433
Example:
2534

35+
#include <dt-bindings/display/tda998x.h>
36+
2637
tda998x: hdmi-encoder {
2738
compatible = "nxp,tda998x";
2839
reg = <0x70>;
2940
interrupt-parent = <&gpio0>;
3041
interrupts = <27 2>; /* falling edge */
3142
pinctrl-0 = <&pmx_camera>;
3243
pinctrl-names = "default";
44+
video-ports = <0x230145>;
45+
46+
#sound-dai-cells = <2>;
47+
/* DAI-format AP_ENA reg value */
48+
audio-ports = < TDA998x_SPDIF 0x04
49+
TDA998x_I2S 0x03>;
50+
3351
};

‎drivers/gpu/drm/i2c/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ config DRM_I2C_SIL164
2828
config DRM_I2C_NXP_TDA998X
2929
tristate "NXP Semiconductors TDA998X HDMI encoder"
3030
default m if DRM_TILCDC
31+
select SND_SOC_HDMI_CODEC if SND_SOC
3132
help
3233
Support for NXP Semiconductors TDA998X HDMI encoders.
3334

‎drivers/gpu/drm/i2c/tda998x_drv.c‎

Lines changed: 210 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/module.h>
2121
#include <linux/irq.h>
2222
#include <sound/asoundef.h>
23+
#include <sound/hdmi-codec.h>
2324

2425
#include <drm/drmP.h>
2526
#include <drm/drm_atomic_helper.h>
@@ -30,6 +31,11 @@
3031

3132
#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
3233

34+
struct tda998x_audio_port {
35+
u8 format; /* AFMT_xxx */
36+
u8 config; /* AP value */
37+
};
38+
3339
struct tda998x_priv {
3440
struct i2c_client *cec;
3541
struct i2c_client *hdmi;
@@ -43,6 +49,9 @@ struct tda998x_priv {
4349
u8 vip_cntrl_2;
4450
struct tda998x_audio_params audio_params;
4551

52+
struct platform_device *audio_pdev;
53+
struct mutex audio_mutex;
54+
4655
wait_queue_head_t wq_edid;
4756
volatile int wq_edid_wait;
4857

@@ -53,6 +62,8 @@ struct tda998x_priv {
5362

5463
struct drm_encoder encoder;
5564
struct drm_connector connector;
65+
66+
struct tda998x_audio_port audio_port[2];
5667
};
5768

5869
#define conn_to_tda998x_priv(x) \
@@ -743,7 +754,7 @@ tda998x_configure_audio(struct tda998x_priv *priv,
743754
break;
744755

745756
default:
746-
BUG();
757+
dev_err(&priv->hdmi->dev, "Unsupported I2S format\n");
747758
return -EINVAL;
748759
}
749760

@@ -1064,9 +1075,11 @@ tda998x_encoder_mode_set(struct drm_encoder *encoder,
10641075
tda998x_write_avi(priv, adjusted_mode);
10651076

10661077
if (priv->audio_params.format != AFMT_UNUSED) {
1078+
mutex_lock(&priv->audio_mutex);
10671079
tda998x_configure_audio(priv,
10681080
&priv->audio_params,
10691081
adjusted_mode->clock);
1082+
mutex_unlock(&priv->audio_mutex);
10701083
}
10711084
}
10721085
}
@@ -1167,6 +1180,8 @@ static int tda998x_connector_get_modes(struct drm_connector *connector)
11671180
drm_mode_connector_update_edid_property(connector, edid);
11681181
n = drm_add_edid_modes(connector, edid);
11691182
priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
1183+
drm_edid_to_eld(connector, edid);
1184+
11701185
kfree(edid);
11711186

11721187
return n;
@@ -1188,6 +1203,9 @@ static void tda998x_destroy(struct tda998x_priv *priv)
11881203
cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
11891204
reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
11901205

1206+
if (priv->audio_pdev)
1207+
platform_device_unregister(priv->audio_pdev);
1208+
11911209
if (priv->hdmi->irq)
11921210
free_irq(priv->hdmi->irq, priv);
11931211

@@ -1197,8 +1215,189 @@ static void tda998x_destroy(struct tda998x_priv *priv)
11971215
i2c_unregister_device(priv->cec);
11981216
}
11991217

1218+
static int tda998x_audio_hw_params(struct device *dev, void *data,
1219+
struct hdmi_codec_daifmt *daifmt,
1220+
struct hdmi_codec_params *params)
1221+
{
1222+
struct tda998x_priv *priv = dev_get_drvdata(dev);
1223+
int i, ret;
1224+
struct tda998x_audio_params audio = {
1225+
.sample_width = params->sample_width,
1226+
.sample_rate = params->sample_rate,
1227+
.cea = params->cea,
1228+
};
1229+
1230+
if (!priv->encoder.crtc)
1231+
return -ENODEV;
1232+
1233+
memcpy(audio.status, params->iec.status,
1234+
min(sizeof(audio.status), sizeof(params->iec.status)));
1235+
1236+
switch (daifmt->fmt) {
1237+
case HDMI_I2S:
1238+
if (daifmt->bit_clk_inv || daifmt->frame_clk_inv ||
1239+
daifmt->bit_clk_master || daifmt->frame_clk_master) {
1240+
dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
1241+
daifmt->bit_clk_inv, daifmt->frame_clk_inv,
1242+
daifmt->bit_clk_master,
1243+
daifmt->frame_clk_master);
1244+
return -EINVAL;
1245+
}
1246+
for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
1247+
if (priv->audio_port[i].format == AFMT_I2S)
1248+
audio.config = priv->audio_port[i].config;
1249+
audio.format = AFMT_I2S;
1250+
break;
1251+
case HDMI_SPDIF:
1252+
for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
1253+
if (priv->audio_port[i].format == AFMT_SPDIF)
1254+
audio.config = priv->audio_port[i].config;
1255+
audio.format = AFMT_SPDIF;
1256+
break;
1257+
default:
1258+
dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
1259+
return -EINVAL;
1260+
}
1261+
1262+
if (audio.config == 0) {
1263+
dev_err(dev, "%s: No audio configutation found\n", __func__);
1264+
return -EINVAL;
1265+
}
1266+
1267+
mutex_lock(&priv->audio_mutex);
1268+
ret = tda998x_configure_audio(priv,
1269+
&audio,
1270+
priv->encoder.crtc->hwmode.clock);
1271+
1272+
if (ret == 0)
1273+
priv->audio_params = audio;
1274+
mutex_unlock(&priv->audio_mutex);
1275+
1276+
return ret;
1277+
}
1278+
1279+
static void tda998x_audio_shutdown(struct device *dev, void *data)
1280+
{
1281+
struct tda998x_priv *priv = dev_get_drvdata(dev);
1282+
1283+
mutex_lock(&priv->audio_mutex);
1284+
1285+
reg_write(priv, REG_ENA_AP, 0);
1286+
1287+
priv->audio_params.format = AFMT_UNUSED;
1288+
1289+
mutex_unlock(&priv->audio_mutex);
1290+
}
1291+
1292+
int tda998x_audio_digital_mute(struct device *dev, void *data, bool enable)
1293+
{
1294+
struct tda998x_priv *priv = dev_get_drvdata(dev);
1295+
1296+
mutex_lock(&priv->audio_mutex);
1297+
1298+
tda998x_audio_mute(priv, enable);
1299+
1300+
mutex_unlock(&priv->audio_mutex);
1301+
return 0;
1302+
}
1303+
1304+
static int tda998x_audio_get_eld(struct device *dev, void *data,
1305+
uint8_t *buf, size_t len)
1306+
{
1307+
struct tda998x_priv *priv = dev_get_drvdata(dev);
1308+
struct drm_mode_config *config = &priv->encoder.dev->mode_config;
1309+
struct drm_connector *connector;
1310+
int ret = -ENODEV;
1311+
1312+
mutex_lock(&config->mutex);
1313+
list_for_each_entry(connector, &config->connector_list, head) {
1314+
if (&priv->encoder == connector->encoder) {
1315+
memcpy(buf, connector->eld,
1316+
min(sizeof(connector->eld), len));
1317+
ret = 0;
1318+
}
1319+
}
1320+
mutex_unlock(&config->mutex);
1321+
1322+
return ret;
1323+
}
1324+
1325+
static const struct hdmi_codec_ops audio_codec_ops = {
1326+
.hw_params = tda998x_audio_hw_params,
1327+
.audio_shutdown = tda998x_audio_shutdown,
1328+
.digital_mute = tda998x_audio_digital_mute,
1329+
.get_eld = tda998x_audio_get_eld,
1330+
};
1331+
1332+
static int tda998x_audio_codec_init(struct tda998x_priv *priv,
1333+
struct device *dev)
1334+
{
1335+
struct hdmi_codec_pdata codec_data = {
1336+
.ops = &audio_codec_ops,
1337+
.max_i2s_channels = 2,
1338+
};
1339+
int i;
1340+
1341+
for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++) {
1342+
if (priv->audio_port[i].format == AFMT_I2S &&
1343+
priv->audio_port[i].config != 0)
1344+
codec_data.i2s = 1;
1345+
if (priv->audio_port[i].format == AFMT_SPDIF &&
1346+
priv->audio_port[i].config != 0)
1347+
codec_data.spdif = 1;
1348+
}
1349+
1350+
priv->audio_pdev = platform_device_register_data(
1351+
dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
1352+
&codec_data, sizeof(codec_data));
1353+
1354+
return PTR_ERR_OR_ZERO(priv->audio_pdev);
1355+
}
1356+
12001357
/* I2C driver functions */
12011358

1359+
static int tda998x_get_audio_ports(struct tda998x_priv *priv,
1360+
struct device_node *np)
1361+
{
1362+
const u32 *port_data;
1363+
u32 size;
1364+
int i;
1365+
1366+
port_data = of_get_property(np, "audio-ports", &size);
1367+
if (!port_data)
1368+
return 0;
1369+
1370+
size /= sizeof(u32);
1371+
if (size > 2 * ARRAY_SIZE(priv->audio_port) || size % 2 != 0) {
1372+
dev_err(&priv->hdmi->dev,
1373+
"Bad number of elements in audio-ports dt-property\n");
1374+
return -EINVAL;
1375+
}
1376+
1377+
size /= 2;
1378+
1379+
for (i = 0; i < size; i++) {
1380+
u8 afmt = be32_to_cpup(&port_data[2*i]);
1381+
u8 ena_ap = be32_to_cpup(&port_data[2*i+1]);
1382+
1383+
if (afmt != AFMT_SPDIF && afmt != AFMT_I2S) {
1384+
dev_err(&priv->hdmi->dev,
1385+
"Bad audio format %u\n", afmt);
1386+
return -EINVAL;
1387+
}
1388+
1389+
priv->audio_port[i].format = afmt;
1390+
priv->audio_port[i].config = ena_ap;
1391+
}
1392+
1393+
if (priv->audio_port[0].format == priv->audio_port[1].format) {
1394+
dev_err(&priv->hdmi->dev,
1395+
"There can only be on I2S port and one SPDIF port\n");
1396+
return -EINVAL;
1397+
}
1398+
return 0;
1399+
}
1400+
12021401
static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
12031402
{
12041403
struct device_node *np = client->dev.of_node;
@@ -1312,16 +1511,24 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
13121511
if (!np)
13131512
return 0; /* non-DT */
13141513

1315-
/* get the optional video properties */
1514+
/* get the device tree parameters */
13161515
ret = of_property_read_u32(np, "video-ports", &video);
13171516
if (ret == 0) {
13181517
priv->vip_cntrl_0 = video >> 16;
13191518
priv->vip_cntrl_1 = video >> 8;
13201519
priv->vip_cntrl_2 = video;
13211520
}
13221521

1323-
return 0;
1522+
mutex_init(&priv->audio_mutex); /* Protect access from audio thread */
13241523

1524+
ret = tda998x_get_audio_ports(priv, np);
1525+
if (ret)
1526+
goto fail;
1527+
1528+
if (priv->audio_port[0].format != AFMT_UNUSED)
1529+
tda998x_audio_codec_init(priv, &client->dev);
1530+
1531+
return 0;
13251532
fail:
13261533
/* if encoder_init fails, the encoder slave is never registered,
13271534
* so cleanup here:

‎include/drm/i2c/tda998x.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#define __DRM_I2C_TDA998X_H__
33

44
#include <linux/hdmi.h>
5+
#include <dt-bindings/display/tda998x.h>
56

67
enum {
78
AFMT_UNUSED = 0,
8-
AFMT_SPDIF = 1,
9-
AFMT_I2S = 2,
9+
AFMT_SPDIF = TDA998x_SPDIF,
10+
AFMT_I2S = TDA998x_I2S,
1011
};
1112

1213
struct tda998x_audio_params {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _DT_BINDINGS_TDA998X_H
2+
#define _DT_BINDINGS_TDA998X_H
3+
4+
#define TDA998x_SPDIF 1
5+
#define TDA998x_I2S 2
6+
7+
#endif /*_DT_BINDINGS_TDA998X_H */

0 commit comments

Comments
 (0)