cfeb7f6cb1c1228f47a7973e9a627909829719b5
[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 static int rk_hw_params(struct snd_pcm_substream *substream,
31                         struct snd_pcm_hw_params *params)
32 {
33         struct snd_soc_pcm_runtime *rtd = substream->private_data;
34         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
35         unsigned long sclk;
36         int ret, ratio;
37
38         /* bmc: 2*32*fs*2 = 128fs */
39         ratio = 128;
40         switch (params_rate(params)) {
41         case 44100:
42         case 32000:
43         case 48000:
44         case 96000:
45         case 192000:
46                 sclk = params_rate(params) * ratio;
47                 break;
48         default:
49                 pr_err("rk_spdif: params not support\n");
50                 return -EINVAL;
51         }
52
53         ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
54                                      sclk, SND_SOC_CLOCK_IN);
55
56         return ret;
57 }
58
59 static struct snd_soc_ops rk_spdif_ops = {
60         .hw_params = rk_hw_params,
61 };
62
63 static struct snd_soc_dai_link rk_dai = {
64         .name = "SPDIF",
65         .stream_name = "SPDIF PCM Playback",
66         .codec_dai_name = "rk-hdmi-spdif-hifi",
67         .ops = &rk_spdif_ops,
68 };
69
70 static struct snd_soc_card rockchip_hdmi_spdif_snd_card = {
71         .name = "RK-HDMI-SPDIF",
72         .dai_link = &rk_dai,
73         .num_links = 1,
74 };
75
76 static int rockchip_hdmi_spdif_audio_probe(struct platform_device *pdev)
77 {
78         int ret;
79         struct snd_soc_card *card = &rockchip_hdmi_spdif_snd_card;
80
81         card->dev = &pdev->dev;
82
83         ret = rockchip_of_get_sound_card_info_(card, false);
84         if (ret) {
85                 pr_err("%s() get sound card info failed:%d\n",
86                        __func__, ret);
87                 return ret;
88         }
89
90         ret = snd_soc_register_card(card);
91
92         if (ret)
93                 pr_err("%s() register card failed:%d\n",
94                        __func__, ret);
95
96         return ret;
97 }
98
99 static int rockchip_hdmi_spdif_audio_remove(struct platform_device *pdev)
100 {
101         struct snd_soc_card *card = platform_get_drvdata(pdev);
102
103         snd_soc_unregister_card(card);
104
105         return 0;
106 }
107
108 #ifdef CONFIG_OF
109 static const struct of_device_id rockchip_hdmi_spdif_of_match[] = {
110         { .compatible = "rockchip-hdmi-spdif", },
111         {},
112 };
113 MODULE_DEVICE_TABLE(of, rockchip_hdmi_spdif_of_match);
114 #endif /* CONFIG_OF */
115
116 static struct platform_driver rockchip_hdmi_spdif_audio_driver = {
117         .driver = {
118                 .name = "rockchip-hdmi-spdif",
119                 .pm = &snd_soc_pm_ops,
120                 .of_match_table = of_match_ptr(rockchip_hdmi_spdif_of_match),
121         },
122         .probe = rockchip_hdmi_spdif_audio_probe,
123         .remove = rockchip_hdmi_spdif_audio_remove,
124 };
125
126 module_platform_driver(rockchip_hdmi_spdif_audio_driver);
127
128 MODULE_AUTHOR("hzb <hzb@rock-chips.com>");
129 MODULE_DESCRIPTION("Rockchip HDMI Spdif Card");
130 MODULE_LICENSE("GPL");