xref: /openbmc/linux/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c (revision 4da722ca19f30f7db250db808d1ab1703607a932)
1 /*
2  * dw-hdmi-i2s-audio.c
3  *
4  * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <drm/bridge/dw_hdmi.h>
11 
12 #include <sound/hdmi-codec.h>
13 
14 #include "dw-hdmi.h"
15 #include "dw-hdmi-audio.h"
16 
17 #define DRIVER_NAME "dw-hdmi-i2s-audio"
18 
19 static inline void hdmi_write(struct dw_hdmi_i2s_audio_data *audio,
20 			      u8 val, int offset)
21 {
22 	struct dw_hdmi *hdmi = audio->hdmi;
23 
24 	audio->write(hdmi, val, offset);
25 }
26 
27 static inline u8 hdmi_read(struct dw_hdmi_i2s_audio_data *audio, int offset)
28 {
29 	struct dw_hdmi *hdmi = audio->hdmi;
30 
31 	return audio->read(hdmi, offset);
32 }
33 
34 static int dw_hdmi_i2s_hw_params(struct device *dev, void *data,
35 				 struct hdmi_codec_daifmt *fmt,
36 				 struct hdmi_codec_params *hparms)
37 {
38 	struct dw_hdmi_i2s_audio_data *audio = data;
39 	struct dw_hdmi *hdmi = audio->hdmi;
40 	u8 conf0 = 0;
41 	u8 conf1 = 0;
42 	u8 inputclkfs = 0;
43 
44 	/* it cares I2S only */
45 	if ((fmt->fmt != HDMI_I2S) ||
46 	    (fmt->bit_clk_master | fmt->frame_clk_master)) {
47 		dev_err(dev, "unsupported format/settings\n");
48 		return -EINVAL;
49 	}
50 
51 	inputclkfs	= HDMI_AUD_INPUTCLKFS_64FS;
52 	conf0		= HDMI_AUD_CONF0_I2S_ALL_ENABLE;
53 
54 	switch (hparms->sample_width) {
55 	case 16:
56 		conf1 = HDMI_AUD_CONF1_WIDTH_16;
57 		break;
58 	case 24:
59 	case 32:
60 		conf1 = HDMI_AUD_CONF1_WIDTH_24;
61 		break;
62 	}
63 
64 	dw_hdmi_set_sample_rate(hdmi, hparms->sample_rate);
65 
66 	hdmi_write(audio, inputclkfs, HDMI_AUD_INPUTCLKFS);
67 	hdmi_write(audio, conf0, HDMI_AUD_CONF0);
68 	hdmi_write(audio, conf1, HDMI_AUD_CONF1);
69 
70 	dw_hdmi_audio_enable(hdmi);
71 
72 	return 0;
73 }
74 
75 static void dw_hdmi_i2s_audio_shutdown(struct device *dev, void *data)
76 {
77 	struct dw_hdmi_i2s_audio_data *audio = data;
78 	struct dw_hdmi *hdmi = audio->hdmi;
79 
80 	dw_hdmi_audio_disable(hdmi);
81 
82 	hdmi_write(audio, HDMI_AUD_CONF0_SW_RESET, HDMI_AUD_CONF0);
83 }
84 
85 static int dw_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
86 				  struct device_node *endpoint)
87 {
88 	struct of_endpoint of_ep;
89 	int ret;
90 
91 	ret = of_graph_parse_endpoint(endpoint, &of_ep);
92 	if (ret < 0)
93 		return ret;
94 
95 	/*
96 	 * HDMI sound should be located as reg = <2>
97 	 * Then, it is sound port 0
98 	 */
99 	if (of_ep.port == 2)
100 		return 0;
101 
102 	return -EINVAL;
103 }
104 
105 static struct hdmi_codec_ops dw_hdmi_i2s_ops = {
106 	.hw_params	= dw_hdmi_i2s_hw_params,
107 	.audio_shutdown	= dw_hdmi_i2s_audio_shutdown,
108 	.get_dai_id	= dw_hdmi_i2s_get_dai_id,
109 };
110 
111 static int snd_dw_hdmi_probe(struct platform_device *pdev)
112 {
113 	struct dw_hdmi_i2s_audio_data *audio = pdev->dev.platform_data;
114 	struct platform_device_info pdevinfo;
115 	struct hdmi_codec_pdata pdata;
116 	struct platform_device *platform;
117 
118 	pdata.ops		= &dw_hdmi_i2s_ops;
119 	pdata.i2s		= 1;
120 	pdata.max_i2s_channels	= 6;
121 	pdata.data		= audio;
122 
123 	memset(&pdevinfo, 0, sizeof(pdevinfo));
124 	pdevinfo.parent		= pdev->dev.parent;
125 	pdevinfo.id		= PLATFORM_DEVID_AUTO;
126 	pdevinfo.name		= HDMI_CODEC_DRV_NAME;
127 	pdevinfo.data		= &pdata;
128 	pdevinfo.size_data	= sizeof(pdata);
129 	pdevinfo.dma_mask	= DMA_BIT_MASK(32);
130 
131 	platform = platform_device_register_full(&pdevinfo);
132 	if (IS_ERR(platform))
133 		return PTR_ERR(platform);
134 
135 	dev_set_drvdata(&pdev->dev, platform);
136 
137 	return 0;
138 }
139 
140 static int snd_dw_hdmi_remove(struct platform_device *pdev)
141 {
142 	struct platform_device *platform = dev_get_drvdata(&pdev->dev);
143 
144 	platform_device_unregister(platform);
145 
146 	return 0;
147 }
148 
149 static struct platform_driver snd_dw_hdmi_driver = {
150 	.probe	= snd_dw_hdmi_probe,
151 	.remove	= snd_dw_hdmi_remove,
152 	.driver	= {
153 		.name = DRIVER_NAME,
154 		.owner = THIS_MODULE,
155 	},
156 };
157 module_platform_driver(snd_dw_hdmi_driver);
158 
159 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
160 MODULE_DESCRIPTION("Synopsis Designware HDMI I2S ALSA SoC interface");
161 MODULE_LICENSE("GPL v2");
162 MODULE_ALIAS("platform:" DRIVER_NAME);
163