Merge tag 'lsk-v3.10-android-14.12'
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_spdif_card.c
1 /*
2  * rk_spdif_card.c  --  spdif card for rockchip
3  *
4  * Copyright 2014 rockchip Co. Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  */
12
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/clk.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22
23 #include "card_info.h"
24 #include "rk_pcm.h"
25
26 static int rk_hw_params(struct snd_pcm_substream *substream,
27                         struct snd_pcm_hw_params *params)
28 {
29         struct snd_soc_pcm_runtime *rtd = substream->private_data;
30         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
31         unsigned long pll_out, rclk_rate;
32         int ret, ratio;
33
34         switch (params_rate(params)) {
35         case 44100:
36                 pll_out = 11289600;
37                 break;
38         case 32000:
39                 pll_out = 8192000;
40                 break;
41         case 48000:
42                 pll_out = 12288000;
43                 break;
44         case 96000:
45                 pll_out = 24576000;
46                 break;
47         default:
48                 pr_err("rk_spdif: params not support\n");
49                 return -EINVAL;
50         }
51
52         ratio = 256;
53         rclk_rate = params_rate(params) * ratio;
54
55         /* Set audio source clock rates */
56         ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
57                                      rclk_rate, SND_SOC_CLOCK_IN);
58         if (ret < 0)
59                 return ret;
60
61         return ret;
62 }
63
64 static struct snd_soc_ops rk_spdif_ops = {
65         .hw_params = rk_hw_params,
66 };
67
68 static struct snd_soc_dai_link rk_dai = {
69         .name = "SPDIF",
70         .stream_name = "SPDIF PCM Playback",
71         .codec_dai_name = "rk-hdmi-spdif-hifi",
72         .ops = &rk_spdif_ops,
73 };
74
75 static struct snd_soc_card rockchip_spdif_card = {
76         .name = "RK-SPDIF-CARD",
77         .dai_link = &rk_dai,
78         .num_links = 1,
79 };
80
81 static int rockchip_spdif_card_probe(struct platform_device *pdev)
82 {
83         int ret;
84         struct snd_soc_card *card = &rockchip_spdif_card;
85
86         card->dev = &pdev->dev;
87
88         ret = rockchip_of_get_sound_card_info_(card, false);
89         if (ret) {
90                 pr_err("%s() get sound card info failed:%d\n",
91                        __func__, ret);
92                 return ret;
93         }
94
95         ret = snd_soc_register_card(card);
96
97         if (ret)
98                 pr_err("%s() register card failed:%d\n",
99                        __func__, ret);
100
101         return ret;
102 }
103
104 static int rockchip_spdif_card_remove(struct platform_device *pdev)
105 {
106         struct snd_soc_card *card = platform_get_drvdata(pdev);
107
108         snd_soc_unregister_card(card);
109
110         return 0;
111 }
112
113 #ifdef CONFIG_OF
114 static const struct of_device_id rockchip_spdif_card_of_match[] = {
115         { .compatible = "rockchip-spdif-card"},
116         {},
117 };
118 MODULE_DEVICE_TABLE(of, rockchip_spdif_card_of_match);
119 #endif /* CONFIG_OF */
120
121 static struct platform_driver rockchip_spdif_card_driver = {
122         .driver         = {
123                 .name   = "rockchip-spdif-card",
124                 .owner  = THIS_MODULE,
125                 .pm = &snd_soc_pm_ops,
126                 .of_match_table = of_match_ptr(rockchip_spdif_card_of_match),
127         },
128         .probe          = rockchip_spdif_card_probe,
129         .remove         = rockchip_spdif_card_remove,
130 };
131
132 module_platform_driver(rockchip_spdif_card_driver);
133
134 MODULE_AUTHOR("hzb, <hzb@rock-chips.com>");
135 MODULE_DESCRIPTION("ALSA SoC RK S/PDIF");
136 MODULE_LICENSE("GPL");