4dee75c8c581d305c2ab4cd12833aaac57162b81
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rockchip_hdmi_dp.c
1 /*
2  * Rockchip machine ASoC driver for Rockchip built-in HDMI and DP audio output
3  *
4  * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * Authors: Sugar Zhang <sugar.zhang@rock-chips.com>,
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30
31 #include "rockchip_i2s.h"
32
33 #define DRV_NAME "rk-hdmi-dp-sound"
34 #define MAX_CODECS      2
35
36 static int rk_hdmi_dp_hw_params(struct snd_pcm_substream *substream,
37                                 struct snd_pcm_hw_params *params)
38 {
39         int ret = 0;
40         struct snd_soc_pcm_runtime *rtd = substream->private_data;
41         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
42         int mclk;
43
44         switch (params_rate(params)) {
45         case 8000:
46         case 16000:
47         case 24000:
48         case 32000:
49         case 48000:
50         case 64000:
51         case 96000:
52                 mclk = 12288000;
53                 break;
54         case 11025:
55         case 22050:
56         case 44100:
57         case 88200:
58                 mclk = 11289600;
59                 break;
60         default:
61                 return -EINVAL;
62         }
63
64         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
65                                      SND_SOC_CLOCK_OUT);
66
67         if (ret && ret != -ENOTSUPP) {
68                 dev_err(cpu_dai->dev, "Can't set cpu clock %d\n", ret);
69                 return ret;
70         }
71
72         return 0;
73 }
74
75 static struct snd_soc_ops rk_ops = {
76         .hw_params = rk_hdmi_dp_hw_params,
77 };
78
79 static struct snd_soc_dai_link rk_dailink = {
80         .name = "HDMI-DP",
81         .stream_name = "HDMI-DP",
82         .ops = &rk_ops,
83         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
84                 SND_SOC_DAIFMT_CBS_CFS,
85 };
86
87 static struct snd_soc_card snd_soc_card_rk = {
88         .name = "rk-hdmi-dp-sound",
89         .dai_link = &rk_dailink,
90         .num_links = 1,
91         .num_aux_devs = 0,
92 };
93
94 static int rk_hdmi_dp_probe(struct platform_device *pdev)
95 {
96         struct snd_soc_card *card = &snd_soc_card_rk;
97         struct device_node *np = pdev->dev.of_node;
98         struct snd_soc_dai_link *link = card->dai_link;
99         struct snd_soc_dai_link_component *codecs;
100         struct of_phandle_args args;
101         struct device_node *node;
102         int count;
103         int ret = 0, i = 0, idx = 0;
104
105         card->dev = &pdev->dev;
106
107         count = of_count_phandle_with_args(np, "rockchip,codec", NULL);
108         if (count < 0 || count > MAX_CODECS)
109                 return -EINVAL;
110
111         /* refine codecs, remove unavailable node */
112         for (i = 0; i < count; i++) {
113                 node = of_parse_phandle(np, "rockchip,codec", i);
114                 if (!node)
115                         return -ENODEV;
116                 if (of_device_is_available(node))
117                         idx++;
118         }
119
120         if (!idx)
121                 return -ENODEV;
122
123         codecs = devm_kcalloc(&pdev->dev, idx,
124                               sizeof(*codecs), GFP_KERNEL);
125         link->codecs = codecs;
126         link->num_codecs = idx;
127         idx = 0;
128         for (i = 0; i < count; i++) {
129                 node = of_parse_phandle(np, "rockchip,codec", i);
130                 if (!node)
131                         return -ENODEV;
132                 if (!of_device_is_available(node))
133                         continue;
134
135                 ret = of_parse_phandle_with_fixed_args(np, "rockchip,codec",
136                                                        0, i, &args);
137                 if (ret)
138                         return ret;
139
140                 codecs[idx].of_node = node;
141                 ret = snd_soc_get_dai_name(&args, &codecs[idx].dai_name);
142                 if (ret)
143                         return ret;
144                 idx++;
145         }
146
147         link->cpu_of_node = of_parse_phandle(np, "rockchip,cpu", 0);
148         if (!link->cpu_of_node)
149                 return -ENODEV;
150
151         link->platform_of_node = link->cpu_of_node;
152
153         ret = devm_snd_soc_register_card(&pdev->dev, card);
154         if (ret == -EPROBE_DEFER)
155                 return -EPROBE_DEFER;
156         if (ret) {
157                 dev_err(&pdev->dev, "card register failed %d\n", ret);
158                 return ret;
159         }
160
161         platform_set_drvdata(pdev, card);
162
163         return ret;
164 }
165
166 static const struct of_device_id rockchip_sound_of_match[] = {
167         { .compatible = "rockchip,rk3399-hdmi-dp", },
168         {},
169 };
170
171 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
172
173 static struct platform_driver rockchip_sound_driver = {
174         .probe = rk_hdmi_dp_probe,
175         .driver = {
176                 .name = DRV_NAME,
177                 .pm = &snd_soc_pm_ops,
178                 .of_match_table = rockchip_sound_of_match,
179         },
180 };
181
182 module_platform_driver(rockchip_sound_driver);
183
184 MODULE_AUTHOR("Sugar Zhang <sugar.zhang@rock-chips.com>");
185 MODULE_DESCRIPTION("Rockchip Built-in HDMI and DP machine ASoC driver");
186 MODULE_LICENSE("GPL v2");
187 MODULE_ALIAS("platform:" DRV_NAME);