ASoC: hdmi_spdif: config hdmi audio when hdmi using spdif source.
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_hdmi_spdif.c
1 /*
2  * rk_hdmi_spdif.c  -- hdmi spdif for rockchip
3  *
4  * Copyright (C) 2015 Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/of.h>
20 #include <linux/of_gpio.h>
21 #include <linux/clk.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26
27 #include "card_info.h"
28 #include "rk_pcm.h"
29
30 #if defined(CONFIG_RK_HDMI) && defined(CONFIG_SND_SOC_HDMI_SPDIF)
31 extern int snd_config_hdmi_audio(struct snd_pcm_hw_params *params);
32 #endif
33
34 static int rk_hw_params(struct snd_pcm_substream *substream,
35                         struct snd_pcm_hw_params *params)
36 {
37         struct snd_soc_pcm_runtime *rtd = substream->private_data;
38         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
39         unsigned long sclk;
40         int ret, ratio;
41
42         /* bmc: 2*32*fs*2 = 128fs */
43         ratio = 128;
44         switch (params_rate(params)) {
45         case 44100:
46         case 32000:
47         case 48000:
48         case 96000:
49         case 192000:
50                 sclk = params_rate(params) * ratio;
51                 break;
52         default:
53                 pr_err("rk_spdif: params not support\n");
54                 return -EINVAL;
55         }
56
57         ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
58                                      sclk, SND_SOC_CLOCK_IN);
59
60 #if defined(CONFIG_RK_HDMI) && defined(CONFIG_SND_SOC_HDMI_SPDIF)
61         snd_config_hdmi_audio(params);
62 #endif
63
64         return ret;
65 }
66
67 static struct snd_soc_ops rk_spdif_ops = {
68         .hw_params = rk_hw_params,
69 };
70
71 static struct snd_soc_dai_link rk_dai = {
72         .name = "SPDIF",
73         .stream_name = "SPDIF PCM Playback",
74         .codec_dai_name = "rk-hdmi-spdif-hifi",
75         .ops = &rk_spdif_ops,
76 };
77
78 static struct snd_soc_card rockchip_hdmi_spdif_snd_card = {
79         .name = "RK-HDMI-SPDIF",
80         .dai_link = &rk_dai,
81         .num_links = 1,
82 };
83
84 static int rockchip_hdmi_spdif_audio_probe(struct platform_device *pdev)
85 {
86         int ret;
87         struct snd_soc_card *card = &rockchip_hdmi_spdif_snd_card;
88
89         card->dev = &pdev->dev;
90
91         ret = rockchip_of_get_sound_card_info_(card, false);
92         if (ret) {
93                 pr_err("%s() get sound card info failed:%d\n",
94                        __func__, ret);
95                 return ret;
96         }
97
98         ret = snd_soc_register_card(card);
99
100         if (ret)
101                 pr_err("%s() register card failed:%d\n",
102                        __func__, ret);
103
104         return ret;
105 }
106
107 static int rockchip_hdmi_spdif_audio_remove(struct platform_device *pdev)
108 {
109         struct snd_soc_card *card = platform_get_drvdata(pdev);
110
111         snd_soc_unregister_card(card);
112
113         return 0;
114 }
115
116 #ifdef CONFIG_OF
117 static const struct of_device_id rockchip_hdmi_spdif_of_match[] = {
118         { .compatible = "rockchip-hdmi-spdif", },
119         {},
120 };
121 MODULE_DEVICE_TABLE(of, rockchip_hdmi_spdif_of_match);
122 #endif /* CONFIG_OF */
123
124 static struct platform_driver rockchip_hdmi_spdif_audio_driver = {
125         .driver = {
126                 .name = "rockchip-hdmi-spdif",
127                 .pm = &snd_soc_pm_ops,
128                 .of_match_table = of_match_ptr(rockchip_hdmi_spdif_of_match),
129         },
130         .probe = rockchip_hdmi_spdif_audio_probe,
131         .remove = rockchip_hdmi_spdif_audio_remove,
132 };
133
134 module_platform_driver(rockchip_hdmi_spdif_audio_driver);
135
136 MODULE_AUTHOR("hzb <hzb@rock-chips.com>");
137 MODULE_DESCRIPTION("Rockchip HDMI Spdif Card");
138 MODULE_LICENSE("GPL");