blob: 46bce9e3713e1f4d52793aa5e1c1e77e4c593095 [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
24struct msm_hdmi_audio_codec_rx_data {
25 struct platform_device *hdmi_core_pdev;
26 struct msm_hdmi_audio_codec_ops hdmi_ops;
27};
28
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080029static int msm_hdmi_edid_ctl_info(struct snd_kcontrol *kcontrol,
30 struct snd_ctl_elem_info *uinfo)
31{
32 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
33 struct msm_hdmi_audio_codec_rx_data *codec_data;
34 struct msm_hdmi_audio_edid_blk edid_blk;
35 int rc;
36
37 codec_data = snd_soc_codec_get_drvdata(codec);
38 rc = codec_data->hdmi_ops.get_audio_edid_blk(codec_data->hdmi_core_pdev,
39 &edid_blk);
40 if (!IS_ERR_VALUE(rc)) {
41 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
42 uinfo->count = edid_blk.audio_data_blk_size +
43 edid_blk.spk_alloc_data_blk_size;
44 }
45
Phani Kumar Uppalapati944e1a72013-01-29 22:04:52 -080046 return 0;
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -080047}
48
49static int msm_hdmi_edid_get(struct snd_kcontrol *kcontrol,
50 struct snd_ctl_elem_value *ucontrol) {
51 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
52 struct msm_hdmi_audio_codec_rx_data *codec_data;
53 struct msm_hdmi_audio_edid_blk edid_blk;
54 int rc;
55
56 codec_data = snd_soc_codec_get_drvdata(codec);
57 rc = codec_data->hdmi_ops.get_audio_edid_blk(codec_data->hdmi_core_pdev,
58 &edid_blk);
59
60 if (!IS_ERR_VALUE(rc)) {
61 memcpy(ucontrol->value.bytes.data, edid_blk.audio_data_blk,
62 edid_blk.audio_data_blk_size);
63 memcpy((ucontrol->value.bytes.data +
64 edid_blk.audio_data_blk_size),
65 edid_blk.spk_alloc_data_blk,
66 edid_blk.spk_alloc_data_blk_size);
67 }
68
69 return rc;
70}
71
72static const struct snd_kcontrol_new msm_hdmi_codec_rx_controls[] = {
73 {
74 .access = SNDRV_CTL_ELEM_ACCESS_READ |
75 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
76 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
77 .name = "HDMI EDID",
78 .info = msm_hdmi_edid_ctl_info,
79 .get = msm_hdmi_edid_get,
80 },
81};
82
Kiran Kandi52650322012-12-11 15:49:39 -080083static int msm_hdmi_audio_codec_rx_dai_hw_params(
84 struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *params,
86 struct snd_soc_dai *dai)
87{
88 u32 channel_allocation = 0;
89 u32 level_shift = 0; /* 0dB */
90 bool down_mix = 0;
91 u32 num_channels = params_channels(params);
92
93 struct msm_hdmi_audio_codec_rx_data *codec_data =
94 dev_get_drvdata(dai->codec->dev);
95
96 switch (num_channels) {
97 case 2:
98 channel_allocation = 0;
99 break;
100 case 6:
101 channel_allocation = 0x0B;
102 break;
103 case 8:
104 channel_allocation = 0x13;
105 break;
106 default:
107 dev_err(dai->dev, "invalid Channels = %u\n", num_channels);
108 return -EINVAL;
109 }
110
111 dev_dbg(dai->dev, "%s() num_ch %u samplerate %u channel_allocation = %u\n",
112 __func__, num_channels, params_rate(params),
113 channel_allocation);
114
115 codec_data->hdmi_ops.audio_info_setup(codec_data->hdmi_core_pdev,
116 num_channels, channel_allocation,
117 level_shift, down_mix);
118
119 return 0;
120}
121
122static struct snd_soc_dai_ops msm_hdmi_audio_codec_rx_dai_ops = {
123 .hw_params = msm_hdmi_audio_codec_rx_dai_hw_params,
124};
125
126static int msm_hdmi_audio_codec_rx_probe(struct snd_soc_codec *codec)
127{
128 struct msm_hdmi_audio_codec_rx_data *codec_data;
129 struct device_node *of_node_parent = NULL;
130
131 codec_data = kzalloc(sizeof(struct msm_hdmi_audio_codec_rx_data),
132 GFP_KERNEL);
133
134 if (!codec_data) {
135 dev_err(codec->dev, "%s(): fail to allocate dai data\n",
136 __func__);
137 return -ENOMEM;
138 }
139
140 of_node_parent = of_get_parent(codec->dev->of_node);
141 if (!of_node_parent) {
142 dev_err(codec->dev, "%s(): Parent device tree node not found\n",
143 __func__);
144 kfree(codec_data);
145 return -ENODEV;
146 }
147
148 codec_data->hdmi_core_pdev = of_find_device_by_node(of_node_parent);
149 if (!codec_data->hdmi_core_pdev) {
150 dev_err(codec->dev, "%s(): can't get parent pdev\n", __func__);
151 kfree(codec_data);
152 return -ENODEV;
153 }
154
155 if (msm_hdmi_register_audio_codec(codec_data->hdmi_core_pdev,
156 &codec_data->hdmi_ops)) {
157 dev_err(codec->dev, "%s(): can't register with hdmi core",
158 __func__);
159 kfree(codec_data);
160 return -ENODEV;
161 }
162
163 dev_set_drvdata(codec->dev, codec_data);
164
165 dev_dbg(codec->dev, "%s(): registerd %s with HDMI core\n",
166 __func__, codec->name);
167
168 return 0;
169}
170
171static int msm_hdmi_audio_codec_rx_remove(struct snd_soc_codec *codec)
172{
173 struct msm_hdmi_audio_codec_rx_data *codec_data;
174
175 codec_data = dev_get_drvdata(codec->dev);
176 kfree(codec_data);
177
178 return 0;
179}
180
181static struct snd_soc_dai_driver msm_hdmi_audio_codec_rx_dais[] = {
182 {
183 .name = "msm_hdmi_audio_codec_rx_dai",
184 .playback = {
185 .stream_name = "HDMI Playback",
186 .channels_min = 1,
187 .channels_max = 8,
188 .rate_min = 48000,
189 .rate_max = 48000,
190 .rates = MSM_HDMI_PCM_RATES,
191 .formats = SNDRV_PCM_FMTBIT_S16_LE,
192 },
193 .ops = &msm_hdmi_audio_codec_rx_dai_ops,
194 },
195};
196
197static struct snd_soc_codec_driver msm_hdmi_audio_codec_rx_soc_driver = {
198 .probe = msm_hdmi_audio_codec_rx_probe,
199 .remove = msm_hdmi_audio_codec_rx_remove,
Subhash Chandra Bose Naripeddy17f7a512012-12-14 21:42:53 -0800200 .controls = msm_hdmi_codec_rx_controls,
201 .num_controls = ARRAY_SIZE(msm_hdmi_codec_rx_controls),
Kiran Kandi52650322012-12-11 15:49:39 -0800202};
203
204static int __devinit msm_hdmi_audio_codec_rx_plat_probe(
205 struct platform_device *pdev)
206{
207 dev_dbg(&pdev->dev, "%s(): orginal dev name = %s, id = %d\n",
208 __func__, dev_name(&pdev->dev), pdev->id);
209
210 if (pdev->dev.of_node) {
211 dev_dbg(&pdev->dev, "%s(): node full name = %s, name = %s\n",
212 __func__, pdev->dev.of_node->full_name,
213 pdev->dev.of_node->name);
214 dev_set_name(&pdev->dev, "%s", "msm-hdmi-audio-codec-rx");
215 } else
216 dev_err(&pdev->dev, "%s(): platfrom data not from device tree\n",
217 __func__);
218
219 dev_dbg(&pdev->dev, "%s(): new dev name %s\n", __func__,
220 dev_name(&pdev->dev));
221
222 return snd_soc_register_codec(&pdev->dev,
223 &msm_hdmi_audio_codec_rx_soc_driver,
224 msm_hdmi_audio_codec_rx_dais,
225 ARRAY_SIZE(msm_hdmi_audio_codec_rx_dais));
226}
227
228static int __devexit msm_hdmi_audio_codec_rx_plat_remove(
229 struct platform_device *pdev)
230{
231 snd_soc_unregister_codec(&pdev->dev);
232 return 0;
233}
234static const struct of_device_id msm_hdmi_audio_codec_rx_dt_match[] = {
235 { .compatible = "qcom,msm-hdmi-audio-codec-rx", },
236 {}
237};
238MODULE_DEVICE_TABLE(of, msm_hdmi_codec_dt_match);
239
240static struct platform_driver msm_hdmi_audio_codec_rx_driver = {
241 .driver = {
242 .name = "msm-hdmi-audio-codec-rx",
243 .owner = THIS_MODULE,
244 .of_match_table = msm_hdmi_audio_codec_rx_dt_match,
245 },
246 .probe = msm_hdmi_audio_codec_rx_plat_probe,
247 .remove = __devexit_p(msm_hdmi_audio_codec_rx_plat_remove),
248};
249
250static int __init msm_hdmi_audio_codec_rx_init(void)
251{
252 return platform_driver_register(&msm_hdmi_audio_codec_rx_driver);
253}
254module_init(msm_hdmi_audio_codec_rx_init);
255
256static void __exit msm_hdmi_audio_codec_rx_exit(void)
257{
258 platform_driver_unregister(&msm_hdmi_audio_codec_rx_driver);
259}
260module_exit(msm_hdmi_audio_codec_rx_exit);
261
262MODULE_DESCRIPTION("MSM HDMI CODEC driver");
263MODULE_VERSION("1.0");
264MODULE_LICENSE("GPL v2");