blob: b406febb78242ea1c404e22080e6301611d4ccb3 [file] [log] [blame]
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Kiran Kandi52650322012-12-11 15:49:39 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/platform_device.h>
13#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/of_device.h>
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080016#include <linux/err.h>
Kiran Kandi52650322012-12-11 15:49:39 -080017#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/soc.h>
20#include <mach/msm_hdmi_audio_codec.h>
21
22#define MSM_HDMI_PCM_RATES SNDRV_PCM_RATE_48000
23
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -070024static int msm_hdmi_audio_codec_return_value;
25
Kiran Kandi52650322012-12-11 15:49:39 -080026struct msm_hdmi_audio_codec_rx_data {
27 struct platform_device *hdmi_core_pdev;
28 struct msm_hdmi_audio_codec_ops hdmi_ops;
29};
30
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080031static int msm_hdmi_edid_ctl_info(struct snd_kcontrol *kcontrol,
32 struct snd_ctl_elem_info *uinfo)
33{
34 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
35 struct msm_hdmi_audio_codec_rx_data *codec_data;
36 struct msm_hdmi_audio_edid_blk edid_blk;
37 int rc;
38
39 codec_data = snd_soc_codec_get_drvdata(codec);
40 rc = codec_data->hdmi_ops.get_audio_edid_blk(codec_data->hdmi_core_pdev,
41 &edid_blk);
42 if (!IS_ERR_VALUE(rc)) {
43 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
44 uinfo->count = edid_blk.audio_data_blk_size +
45 edid_blk.spk_alloc_data_blk_size;
46 }
47
Phani Kumar Uppalapati944e1a72013-01-29 22:04:52 -080048 return 0;
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080049}
50
51static int msm_hdmi_edid_get(struct snd_kcontrol *kcontrol,
52 struct snd_ctl_elem_value *ucontrol) {
53 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
54 struct msm_hdmi_audio_codec_rx_data *codec_data;
55 struct msm_hdmi_audio_edid_blk edid_blk;
56 int rc;
57
58 codec_data = snd_soc_codec_get_drvdata(codec);
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -070059 rc = codec_data->hdmi_ops.get_audio_edid_blk(
60 codec_data->hdmi_core_pdev, &edid_blk);
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080061
62 if (!IS_ERR_VALUE(rc)) {
63 memcpy(ucontrol->value.bytes.data, edid_blk.audio_data_blk,
64 edid_blk.audio_data_blk_size);
65 memcpy((ucontrol->value.bytes.data +
66 edid_blk.audio_data_blk_size),
67 edid_blk.spk_alloc_data_blk,
68 edid_blk.spk_alloc_data_blk_size);
69 }
70
71 return rc;
72}
73
74static const struct snd_kcontrol_new msm_hdmi_codec_rx_controls[] = {
75 {
76 .access = SNDRV_CTL_ELEM_ACCESS_READ |
77 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
78 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
79 .name = "HDMI EDID",
80 .info = msm_hdmi_edid_ctl_info,
81 .get = msm_hdmi_edid_get,
82 },
83};
84
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -070085static int msm_hdmi_audio_codec_rx_dai_startup(
86 struct snd_pcm_substream *substream,
87 struct snd_soc_dai *dai)
88{
89 struct msm_hdmi_audio_codec_rx_data *codec_data =
90 dev_get_drvdata(dai->codec->dev);
91
92 msm_hdmi_audio_codec_return_value =
93 codec_data->hdmi_ops.hdmi_cable_status(
94 codec_data->hdmi_core_pdev, 1);
95 if (IS_ERR_VALUE(msm_hdmi_audio_codec_return_value)) {
96 dev_err(dai->dev,
97 "%s() HDMI core is not ready\n", __func__);
98 }
99
100 return msm_hdmi_audio_codec_return_value;
101}
102
Kiran Kandi52650322012-12-11 15:49:39 -0800103static int msm_hdmi_audio_codec_rx_dai_hw_params(
104 struct snd_pcm_substream *substream,
105 struct snd_pcm_hw_params *params,
106 struct snd_soc_dai *dai)
107{
108 u32 channel_allocation = 0;
109 u32 level_shift = 0; /* 0dB */
110 bool down_mix = 0;
111 u32 num_channels = params_channels(params);
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700112 int rc = 0;
Kiran Kandi52650322012-12-11 15:49:39 -0800113
114 struct msm_hdmi_audio_codec_rx_data *codec_data =
115 dev_get_drvdata(dai->codec->dev);
116
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700117 if (IS_ERR_VALUE(msm_hdmi_audio_codec_return_value)) {
118 dev_err(dai->dev,
119 "%s() HDMI core is not ready\n", __func__);
120 return msm_hdmi_audio_codec_return_value;
121 }
122
Kiran Kandi52650322012-12-11 15:49:39 -0800123 switch (num_channels) {
124 case 2:
125 channel_allocation = 0;
126 break;
127 case 6:
128 channel_allocation = 0x0B;
129 break;
130 case 8:
131 channel_allocation = 0x13;
132 break;
133 default:
134 dev_err(dai->dev, "invalid Channels = %u\n", num_channels);
135 return -EINVAL;
136 }
137
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700138 dev_dbg(dai->dev,
139 "%s() num_ch %u samplerate %u channel_allocation = %u\n",
Kiran Kandi52650322012-12-11 15:49:39 -0800140 __func__, num_channels, params_rate(params),
141 channel_allocation);
142
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700143 rc = codec_data->hdmi_ops.audio_info_setup(
144 codec_data->hdmi_core_pdev,
145 params_rate(params), num_channels,
146 channel_allocation, level_shift, down_mix);
147 if (IS_ERR_VALUE(rc)) {
148 dev_err(dai->dev,
149 "%s() HDMI core is not ready\n", __func__);
150 }
Kiran Kandi52650322012-12-11 15:49:39 -0800151
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700152 return rc;
153}
154
155static void msm_hdmi_audio_codec_rx_dai_shutdown(
156 struct snd_pcm_substream *substream,
157 struct snd_soc_dai *dai)
158{
159 int rc;
160
161 struct msm_hdmi_audio_codec_rx_data *codec_data =
162 dev_get_drvdata(dai->codec->dev);
163
164 rc = codec_data->hdmi_ops.hdmi_cable_status(
165 codec_data->hdmi_core_pdev, 0);
166 if (IS_ERR_VALUE(rc)) {
167 dev_err(dai->dev,
168 "%s() HDMI core had problems releasing HDMI audio flag\n",
169 __func__);
170 }
171
172 return;
Kiran Kandi52650322012-12-11 15:49:39 -0800173}
174
175static struct snd_soc_dai_ops msm_hdmi_audio_codec_rx_dai_ops = {
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700176 .startup = msm_hdmi_audio_codec_rx_dai_startup,
Kiran Kandi52650322012-12-11 15:49:39 -0800177 .hw_params = msm_hdmi_audio_codec_rx_dai_hw_params,
Kenneth Westfieldbbcd3b52013-10-14 15:09:05 -0700178 .shutdown = msm_hdmi_audio_codec_rx_dai_shutdown
Kiran Kandi52650322012-12-11 15:49:39 -0800179};
180
181static int msm_hdmi_audio_codec_rx_probe(struct snd_soc_codec *codec)
182{
183 struct msm_hdmi_audio_codec_rx_data *codec_data;
184 struct device_node *of_node_parent = NULL;
185
186 codec_data = kzalloc(sizeof(struct msm_hdmi_audio_codec_rx_data),
187 GFP_KERNEL);
188
189 if (!codec_data) {
190 dev_err(codec->dev, "%s(): fail to allocate dai data\n",
191 __func__);
192 return -ENOMEM;
193 }
194
195 of_node_parent = of_get_parent(codec->dev->of_node);
196 if (!of_node_parent) {
197 dev_err(codec->dev, "%s(): Parent device tree node not found\n",
198 __func__);
199 kfree(codec_data);
200 return -ENODEV;
201 }
202
203 codec_data->hdmi_core_pdev = of_find_device_by_node(of_node_parent);
204 if (!codec_data->hdmi_core_pdev) {
205 dev_err(codec->dev, "%s(): can't get parent pdev\n", __func__);
206 kfree(codec_data);
207 return -ENODEV;
208 }
209
210 if (msm_hdmi_register_audio_codec(codec_data->hdmi_core_pdev,
211 &codec_data->hdmi_ops)) {
212 dev_err(codec->dev, "%s(): can't register with hdmi core",
213 __func__);
214 kfree(codec_data);
215 return -ENODEV;
216 }
217
218 dev_set_drvdata(codec->dev, codec_data);
219
220 dev_dbg(codec->dev, "%s(): registerd %s with HDMI core\n",
221 __func__, codec->name);
222
223 return 0;
224}
225
226static int msm_hdmi_audio_codec_rx_remove(struct snd_soc_codec *codec)
227{
228 struct msm_hdmi_audio_codec_rx_data *codec_data;
229
230 codec_data = dev_get_drvdata(codec->dev);
231 kfree(codec_data);
232
233 return 0;
234}
235
236static struct snd_soc_dai_driver msm_hdmi_audio_codec_rx_dais[] = {
237 {
238 .name = "msm_hdmi_audio_codec_rx_dai",
239 .playback = {
240 .stream_name = "HDMI Playback",
241 .channels_min = 1,
242 .channels_max = 8,
243 .rate_min = 48000,
244 .rate_max = 48000,
245 .rates = MSM_HDMI_PCM_RATES,
246 .formats = SNDRV_PCM_FMTBIT_S16_LE,
247 },
248 .ops = &msm_hdmi_audio_codec_rx_dai_ops,
249 },
250};
251
252static struct snd_soc_codec_driver msm_hdmi_audio_codec_rx_soc_driver = {
253 .probe = msm_hdmi_audio_codec_rx_probe,
254 .remove = msm_hdmi_audio_codec_rx_remove,
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -0800255 .controls = msm_hdmi_codec_rx_controls,
256 .num_controls = ARRAY_SIZE(msm_hdmi_codec_rx_controls),
Kiran Kandi52650322012-12-11 15:49:39 -0800257};
258
259static int __devinit msm_hdmi_audio_codec_rx_plat_probe(
260 struct platform_device *pdev)
261{
262 dev_dbg(&pdev->dev, "%s(): orginal dev name = %s, id = %d\n",
263 __func__, dev_name(&pdev->dev), pdev->id);
264
265 if (pdev->dev.of_node) {
266 dev_dbg(&pdev->dev, "%s(): node full name = %s, name = %s\n",
267 __func__, pdev->dev.of_node->full_name,
268 pdev->dev.of_node->name);
269 dev_set_name(&pdev->dev, "%s", "msm-hdmi-audio-codec-rx");
270 } else
271 dev_err(&pdev->dev, "%s(): platfrom data not from device tree\n",
272 __func__);
273
274 dev_dbg(&pdev->dev, "%s(): new dev name %s\n", __func__,
275 dev_name(&pdev->dev));
276
277 return snd_soc_register_codec(&pdev->dev,
278 &msm_hdmi_audio_codec_rx_soc_driver,
279 msm_hdmi_audio_codec_rx_dais,
280 ARRAY_SIZE(msm_hdmi_audio_codec_rx_dais));
281}
282
283static int __devexit msm_hdmi_audio_codec_rx_plat_remove(
284 struct platform_device *pdev)
285{
286 snd_soc_unregister_codec(&pdev->dev);
287 return 0;
288}
289static const struct of_device_id msm_hdmi_audio_codec_rx_dt_match[] = {
290 { .compatible = "qcom,msm-hdmi-audio-codec-rx", },
291 {}
292};
293MODULE_DEVICE_TABLE(of, msm_hdmi_codec_dt_match);
294
295static struct platform_driver msm_hdmi_audio_codec_rx_driver = {
296 .driver = {
297 .name = "msm-hdmi-audio-codec-rx",
298 .owner = THIS_MODULE,
299 .of_match_table = msm_hdmi_audio_codec_rx_dt_match,
300 },
301 .probe = msm_hdmi_audio_codec_rx_plat_probe,
302 .remove = __devexit_p(msm_hdmi_audio_codec_rx_plat_remove),
303};
304
305static int __init msm_hdmi_audio_codec_rx_init(void)
306{
307 return platform_driver_register(&msm_hdmi_audio_codec_rx_driver);
308}
309module_init(msm_hdmi_audio_codec_rx_init);
310
311static void __exit msm_hdmi_audio_codec_rx_exit(void)
312{
313 platform_driver_unregister(&msm_hdmi_audio_codec_rx_driver);
314}
315module_exit(msm_hdmi_audio_codec_rx_exit);
316
317MODULE_DESCRIPTION("MSM HDMI CODEC driver");
318MODULE_VERSION("1.0");
319MODULE_LICENSE("GPL v2");