rk3288 spdif: spdif sound support
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_hdmi_spdif.c
1 /*$_FOR_ROCKCHIP_RBOX_$*/
2 /*$_rbox_$_modify_$_huangzhibao for spdif output*/
3
4 /*
5  * smdk_spdif.c  --  S/PDIF audio for SMDK
6  *
7  * Copyright 2010 Samsung Electronics Co. Ltd.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/of.h>
19 #include <linux/of_gpio.h>
20 #include <linux/clk.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
25
26 #include "card_info.h"
27 #include "rk_pcm.h"
28
29
30 #if 0
31 #define RK_SPDIF_DBG(x...) printk(KERN_INFO "rk_hdmi_spdif:"x)
32 #else
33 #define RK_SPDIF_DBG(x...) do { } while (0)
34 #endif
35
36
37 static int set_audio_clock_rate(unsigned long pll_rate,
38                                 unsigned long audio_rate)
39 {
40         struct clk *hclk_spdif, *sclk_spdif;
41
42 #if defined (CONFIG_ARCH_RK30) || (CONFIG_ARCH_RK3188)  
43         hclk_spdif = clk_get(NULL, "hclk_spdif");
44         if (IS_ERR(hclk_spdif)) {
45                 printk(KERN_ERR "spdif:failed to get hclk_spdif\n");
46                 return -ENOENT;
47         }
48
49         clk_set_rate(hclk_spdif, pll_rate);
50         clk_put(hclk_spdif);
51 #endif
52
53         sclk_spdif = clk_get(NULL, "spdif");
54         if (IS_ERR(sclk_spdif)) {
55                 printk(KERN_ERR "spdif:failed to get sclk_spdif\n");
56                 return -ENOENT;
57         }
58
59         clk_set_rate(sclk_spdif, audio_rate);
60         clk_put(sclk_spdif);
61
62         return 0;
63 }
64
65 static int rk_hw_params(struct snd_pcm_substream *substream,
66                 struct snd_pcm_hw_params *params)
67 {
68         struct snd_soc_pcm_runtime *rtd = substream->private_data;
69         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70         struct snd_soc_dai *codec_dai = rtd->codec_dai;
71         unsigned long pll_out, rclk_rate, dai_fmt = rtd->dai_link->dai_fmt;
72         int ret, ratio;
73
74         RK_SPDIF_DBG("spdif:Entered %s\n", __func__);
75
76         return 0;
77
78         /* set codec DAI configuration */
79         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
80         if (ret < 0) {
81                 printk("%s():failed to set the format for codec side\n", __func__);
82                 return ret;
83         }
84
85         /* set cpu DAI configuration */
86         ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
87         if (ret < 0) {
88                 printk("%s():failed to set the format for cpu side\n", __func__);
89                 return ret;
90         }
91   
92         switch (params_rate(params)) {
93         case 44100:
94                 pll_out = 11289600;
95                 break;
96         case 32000:
97                 pll_out = 8192000;
98                 break;
99         case 48000:
100                 pll_out = 12288000;
101                 break;
102         case 96000:
103                 pll_out = 24576000;
104                 break;
105         default:
106                 printk("rk_spdif: params not support\n");
107                 return -EINVAL;
108         }
109
110         ratio = 256;
111         rclk_rate = params_rate(params) * ratio;
112
113         /* Set audio source clock rates */
114         ret = set_audio_clock_rate(pll_out, rclk_rate);
115         if (ret < 0)
116                 return ret;
117
118         /* Set S/PDIF uses internal source clock */
119         //ret = snd_soc_dai_set_sysclk(cpu_dai, SND_SOC_SPDIF_INT_MCLK,
120                                         //rclk_rate, SND_SOC_CLOCK_IN);
121         //if (ret < 0)
122                 //return ret;
123
124         return ret;
125 }
126
127 static struct snd_soc_ops rk_spdif_ops = {
128         .hw_params = rk_hw_params,
129 };
130
131 static struct snd_soc_dai_link rk_dai = {
132         .name = "SPDIF",
133         .stream_name = "SPDIF PCM Playback",
134         .cpu_dai_name = "rockchip-spdif",
135         .codec_dai_name = "rk-hdmi-spdif-hifi",
136         .codec_name = "hdmi-spdif",
137         .ops = &rk_spdif_ops,
138 };
139
140 static struct snd_soc_card rockchip_hdmi_spdif_snd_card = {
141         .name = "RK-HDMI-SPDIF",
142         .dai_link = &rk_dai,
143         .num_links = 1,
144 };
145
146 static int rockchip_hdmi_spdif_audio_probe(struct platform_device *pdev)
147 {
148         int ret;
149         struct snd_soc_card *card = &rockchip_hdmi_spdif_snd_card;
150
151         card->dev = &pdev->dev;
152
153         ret = rockchip_of_get_sound_card_info(card);
154         if (ret) {
155                 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
156                 return ret;
157         }
158
159         ret = snd_soc_register_card(card);
160
161         if (ret)
162                 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
163
164         return ret;
165 }
166
167 static int rockchip_hdmi_spdif_audio_remove(struct platform_device *pdev)
168 {
169         struct snd_soc_card *card = platform_get_drvdata(pdev);
170
171         snd_soc_unregister_card(card);
172
173         return 0;
174 }
175
176 #ifdef CONFIG_OF
177 static const struct of_device_id rockchip_hdmi_spdif_of_match[] = {
178         { .compatible = "rockchip-hdmi-spdif"},
179         {},
180 };
181 MODULE_DEVICE_TABLE(of, rockchip_hdmi_spdif_of_match);
182 #endif /* CONFIG_OF */
183
184 static struct platform_driver rockchip_hdmi_spdif_audio_driver = {
185         .driver         = {
186                 .name   = "rockchip-hdmi-spdif",
187                 .owner  = THIS_MODULE,
188                 .of_match_table = of_match_ptr(rockchip_hdmi_spdif_of_match),
189         },
190         .probe          = rockchip_hdmi_spdif_audio_probe,
191         .remove         = rockchip_hdmi_spdif_audio_remove,
192 };
193
194 module_platform_driver(rockchip_hdmi_spdif_audio_driver);
195
196 MODULE_AUTHOR("hzb, <hzb@rock-chips.com>");
197 MODULE_DESCRIPTION("ALSA SoC RK+S/PDIF");
198 MODULE_LICENSE("GPL");