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