Subhash Chandra Bose Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 2 | * |
| 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 Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 17 | #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 | |
| 24 | struct 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 Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 29 | static 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 Uppalapati | 944e1a7 | 2013-01-29 22:04:52 -0800 | [diff] [blame] | 46 | return 0; |
Subhash Chandra Bose Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static 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); |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 57 | rc = codec_data->hdmi_ops.get_audio_edid_blk( |
| 58 | codec_data->hdmi_core_pdev, &edid_blk); |
Subhash Chandra Bose Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 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 | |
| 72 | static 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 | |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 83 | static int msm_hdmi_audio_codec_rx_dai_startup( |
| 84 | struct snd_pcm_substream *substream, |
| 85 | struct snd_soc_dai *dai) |
| 86 | { |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 87 | int rv; |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 88 | struct msm_hdmi_audio_codec_rx_data *codec_data = |
| 89 | dev_get_drvdata(dai->codec->dev); |
| 90 | |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 91 | rv = codec_data->hdmi_ops.hdmi_cable_status( |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 92 | codec_data->hdmi_core_pdev, 1); |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 93 | if (IS_ERR_VALUE(rv)) { |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 94 | dev_err(dai->dev, |
| 95 | "%s() HDMI core is not ready\n", __func__); |
| 96 | } |
| 97 | |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 98 | return rv; |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 101 | static int msm_hdmi_audio_codec_rx_dai_hw_params( |
| 102 | struct snd_pcm_substream *substream, |
| 103 | struct snd_pcm_hw_params *params, |
| 104 | struct snd_soc_dai *dai) |
| 105 | { |
| 106 | u32 channel_allocation = 0; |
| 107 | u32 level_shift = 0; /* 0dB */ |
| 108 | bool down_mix = 0; |
| 109 | u32 num_channels = params_channels(params); |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 110 | int rv = 0; |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 111 | |
| 112 | struct msm_hdmi_audio_codec_rx_data *codec_data = |
| 113 | dev_get_drvdata(dai->codec->dev); |
| 114 | |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 115 | rv = codec_data->hdmi_ops.hdmi_cable_status( |
| 116 | codec_data->hdmi_core_pdev, 1); |
| 117 | if (IS_ERR_VALUE(rv)) { |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 118 | dev_err(dai->dev, |
| 119 | "%s() HDMI core is not ready\n", __func__); |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 120 | return rv; |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 123 | switch (num_channels) { |
| 124 | case 2: |
| 125 | channel_allocation = 0; |
| 126 | break; |
Mingming Yin | 7c7195e | 2013-12-07 14:26:31 -0800 | [diff] [blame] | 127 | case 3: |
| 128 | channel_allocation = 0x02;//default to FL/FR/FC |
| 129 | break; |
| 130 | case 4: |
| 131 | channel_allocation = 0x06;//default to FL/FR/FC/RC |
| 132 | break; |
| 133 | case 5: |
| 134 | channel_allocation = 0x0A;//default to FL/FR/FC/RR/RL |
| 135 | break; |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 136 | case 6: |
| 137 | channel_allocation = 0x0B; |
| 138 | break; |
Mingming Yin | 7c7195e | 2013-12-07 14:26:31 -0800 | [diff] [blame] | 139 | case 7: |
| 140 | channel_allocation = 0x12;//default to FL/FR/FC/RL/RR/RRC/RLC |
| 141 | break; |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 142 | case 8: |
| 143 | channel_allocation = 0x13; |
| 144 | break; |
| 145 | default: |
| 146 | dev_err(dai->dev, "invalid Channels = %u\n", num_channels); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 150 | dev_dbg(dai->dev, |
| 151 | "%s() num_ch %u samplerate %u channel_allocation = %u\n", |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 152 | __func__, num_channels, params_rate(params), |
| 153 | channel_allocation); |
| 154 | |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 155 | rv = codec_data->hdmi_ops.audio_info_setup( |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 156 | codec_data->hdmi_core_pdev, |
| 157 | params_rate(params), num_channels, |
| 158 | channel_allocation, level_shift, down_mix); |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 159 | if (IS_ERR_VALUE(rv)) { |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 160 | dev_err(dai->dev, |
| 161 | "%s() HDMI core is not ready\n", __func__); |
| 162 | } |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 163 | |
Kenneth Westfield | c182866 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 164 | return rv; |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void msm_hdmi_audio_codec_rx_dai_shutdown( |
| 168 | struct snd_pcm_substream *substream, |
| 169 | struct snd_soc_dai *dai) |
| 170 | { |
| 171 | int rc; |
| 172 | |
| 173 | struct msm_hdmi_audio_codec_rx_data *codec_data = |
| 174 | dev_get_drvdata(dai->codec->dev); |
| 175 | |
| 176 | rc = codec_data->hdmi_ops.hdmi_cable_status( |
| 177 | codec_data->hdmi_core_pdev, 0); |
| 178 | if (IS_ERR_VALUE(rc)) { |
| 179 | dev_err(dai->dev, |
| 180 | "%s() HDMI core had problems releasing HDMI audio flag\n", |
| 181 | __func__); |
| 182 | } |
| 183 | |
| 184 | return; |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static struct snd_soc_dai_ops msm_hdmi_audio_codec_rx_dai_ops = { |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 188 | .startup = msm_hdmi_audio_codec_rx_dai_startup, |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 189 | .hw_params = msm_hdmi_audio_codec_rx_dai_hw_params, |
Kenneth Westfield | bbcd3b5 | 2013-10-14 15:09:05 -0700 | [diff] [blame] | 190 | .shutdown = msm_hdmi_audio_codec_rx_dai_shutdown |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static int msm_hdmi_audio_codec_rx_probe(struct snd_soc_codec *codec) |
| 194 | { |
| 195 | struct msm_hdmi_audio_codec_rx_data *codec_data; |
| 196 | struct device_node *of_node_parent = NULL; |
| 197 | |
| 198 | codec_data = kzalloc(sizeof(struct msm_hdmi_audio_codec_rx_data), |
| 199 | GFP_KERNEL); |
| 200 | |
| 201 | if (!codec_data) { |
| 202 | dev_err(codec->dev, "%s(): fail to allocate dai data\n", |
| 203 | __func__); |
| 204 | return -ENOMEM; |
| 205 | } |
| 206 | |
| 207 | of_node_parent = of_get_parent(codec->dev->of_node); |
| 208 | if (!of_node_parent) { |
| 209 | dev_err(codec->dev, "%s(): Parent device tree node not found\n", |
| 210 | __func__); |
| 211 | kfree(codec_data); |
| 212 | return -ENODEV; |
| 213 | } |
| 214 | |
| 215 | codec_data->hdmi_core_pdev = of_find_device_by_node(of_node_parent); |
| 216 | if (!codec_data->hdmi_core_pdev) { |
| 217 | dev_err(codec->dev, "%s(): can't get parent pdev\n", __func__); |
| 218 | kfree(codec_data); |
| 219 | return -ENODEV; |
| 220 | } |
| 221 | |
| 222 | if (msm_hdmi_register_audio_codec(codec_data->hdmi_core_pdev, |
| 223 | &codec_data->hdmi_ops)) { |
| 224 | dev_err(codec->dev, "%s(): can't register with hdmi core", |
| 225 | __func__); |
| 226 | kfree(codec_data); |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | dev_set_drvdata(codec->dev, codec_data); |
| 231 | |
| 232 | dev_dbg(codec->dev, "%s(): registerd %s with HDMI core\n", |
| 233 | __func__, codec->name); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int msm_hdmi_audio_codec_rx_remove(struct snd_soc_codec *codec) |
| 239 | { |
| 240 | struct msm_hdmi_audio_codec_rx_data *codec_data; |
| 241 | |
| 242 | codec_data = dev_get_drvdata(codec->dev); |
| 243 | kfree(codec_data); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static struct snd_soc_dai_driver msm_hdmi_audio_codec_rx_dais[] = { |
| 249 | { |
| 250 | .name = "msm_hdmi_audio_codec_rx_dai", |
| 251 | .playback = { |
| 252 | .stream_name = "HDMI Playback", |
| 253 | .channels_min = 1, |
| 254 | .channels_max = 8, |
| 255 | .rate_min = 48000, |
| 256 | .rate_max = 48000, |
| 257 | .rates = MSM_HDMI_PCM_RATES, |
| 258 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 259 | }, |
| 260 | .ops = &msm_hdmi_audio_codec_rx_dai_ops, |
| 261 | }, |
| 262 | }; |
| 263 | |
| 264 | static struct snd_soc_codec_driver msm_hdmi_audio_codec_rx_soc_driver = { |
| 265 | .probe = msm_hdmi_audio_codec_rx_probe, |
| 266 | .remove = msm_hdmi_audio_codec_rx_remove, |
Subhash Chandra Bose Naripeddy | 17f7a51 | 2012-12-14 21:42:53 -0800 | [diff] [blame] | 267 | .controls = msm_hdmi_codec_rx_controls, |
| 268 | .num_controls = ARRAY_SIZE(msm_hdmi_codec_rx_controls), |
Kiran Kandi | 5265032 | 2012-12-11 15:49:39 -0800 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | static int __devinit msm_hdmi_audio_codec_rx_plat_probe( |
| 272 | struct platform_device *pdev) |
| 273 | { |
| 274 | dev_dbg(&pdev->dev, "%s(): orginal dev name = %s, id = %d\n", |
| 275 | __func__, dev_name(&pdev->dev), pdev->id); |
| 276 | |
| 277 | if (pdev->dev.of_node) { |
| 278 | dev_dbg(&pdev->dev, "%s(): node full name = %s, name = %s\n", |
| 279 | __func__, pdev->dev.of_node->full_name, |
| 280 | pdev->dev.of_node->name); |
| 281 | dev_set_name(&pdev->dev, "%s", "msm-hdmi-audio-codec-rx"); |
| 282 | } else |
| 283 | dev_err(&pdev->dev, "%s(): platfrom data not from device tree\n", |
| 284 | __func__); |
| 285 | |
| 286 | dev_dbg(&pdev->dev, "%s(): new dev name %s\n", __func__, |
| 287 | dev_name(&pdev->dev)); |
| 288 | |
| 289 | return snd_soc_register_codec(&pdev->dev, |
| 290 | &msm_hdmi_audio_codec_rx_soc_driver, |
| 291 | msm_hdmi_audio_codec_rx_dais, |
| 292 | ARRAY_SIZE(msm_hdmi_audio_codec_rx_dais)); |
| 293 | } |
| 294 | |
| 295 | static int __devexit msm_hdmi_audio_codec_rx_plat_remove( |
| 296 | struct platform_device *pdev) |
| 297 | { |
| 298 | snd_soc_unregister_codec(&pdev->dev); |
| 299 | return 0; |
| 300 | } |
| 301 | static const struct of_device_id msm_hdmi_audio_codec_rx_dt_match[] = { |
| 302 | { .compatible = "qcom,msm-hdmi-audio-codec-rx", }, |
| 303 | {} |
| 304 | }; |
| 305 | MODULE_DEVICE_TABLE(of, msm_hdmi_codec_dt_match); |
| 306 | |
| 307 | static struct platform_driver msm_hdmi_audio_codec_rx_driver = { |
| 308 | .driver = { |
| 309 | .name = "msm-hdmi-audio-codec-rx", |
| 310 | .owner = THIS_MODULE, |
| 311 | .of_match_table = msm_hdmi_audio_codec_rx_dt_match, |
| 312 | }, |
| 313 | .probe = msm_hdmi_audio_codec_rx_plat_probe, |
| 314 | .remove = __devexit_p(msm_hdmi_audio_codec_rx_plat_remove), |
| 315 | }; |
| 316 | |
| 317 | static int __init msm_hdmi_audio_codec_rx_init(void) |
| 318 | { |
| 319 | return platform_driver_register(&msm_hdmi_audio_codec_rx_driver); |
| 320 | } |
| 321 | module_init(msm_hdmi_audio_codec_rx_init); |
| 322 | |
| 323 | static void __exit msm_hdmi_audio_codec_rx_exit(void) |
| 324 | { |
| 325 | platform_driver_unregister(&msm_hdmi_audio_codec_rx_driver); |
| 326 | } |
| 327 | module_exit(msm_hdmi_audio_codec_rx_exit); |
| 328 | |
| 329 | MODULE_DESCRIPTION("MSM HDMI CODEC driver"); |
| 330 | MODULE_VERSION("1.0"); |
| 331 | MODULE_LICENSE("GPL v2"); |