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