GNU Linux-libre 5.4.200-gnu1
[releases.git] / sound / soc / samsung / arndale_rt5631.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2014, Insignal Co., Ltd.
4 //
5 //  Author: Claude <claude@insginal.co.kr>
6
7 #include <linux/module.h>
8 #include <linux/of_device.h>
9 #include <linux/platform_device.h>
10 #include <linux/clk.h>
11
12 #include <sound/soc.h>
13 #include <sound/soc-dapm.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16
17 #include "i2s.h"
18
19 static int arndale_hw_params(struct snd_pcm_substream *substream,
20         struct snd_pcm_hw_params *params)
21 {
22         struct snd_soc_pcm_runtime *rtd = substream->private_data;
23         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
24         struct snd_soc_dai *codec_dai = rtd->codec_dai;
25         int rfs, ret;
26         unsigned long rclk;
27
28         rfs = 256;
29
30         rclk = params_rate(params) * rfs;
31
32         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
33                                         0, SND_SOC_CLOCK_OUT);
34         if (ret < 0)
35                 return ret;
36
37         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
38                                         0, SND_SOC_CLOCK_OUT);
39
40         if (ret < 0)
41                 return ret;
42
43         ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk, SND_SOC_CLOCK_OUT);
44         if (ret < 0)
45                 return ret;
46
47         return 0;
48 }
49
50 static struct snd_soc_ops arndale_ops = {
51         .hw_params = arndale_hw_params,
52 };
53
54 SND_SOC_DAILINK_DEFS(rt5631_hifi,
55         DAILINK_COMP_ARRAY(COMP_EMPTY()),
56         DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5631-hifi")),
57         DAILINK_COMP_ARRAY(COMP_EMPTY()));
58
59 static struct snd_soc_dai_link arndale_rt5631_dai[] = {
60         {
61                 .name = "RT5631 HiFi",
62                 .stream_name = "Primary",
63                 .dai_fmt = SND_SOC_DAIFMT_I2S
64                         | SND_SOC_DAIFMT_NB_NF
65                         | SND_SOC_DAIFMT_CBS_CFS,
66                 .ops = &arndale_ops,
67                 SND_SOC_DAILINK_REG(rt5631_hifi),
68         },
69 };
70
71 static struct snd_soc_card arndale_rt5631 = {
72         .name = "Arndale RT5631",
73         .owner = THIS_MODULE,
74         .dai_link = arndale_rt5631_dai,
75         .num_links = ARRAY_SIZE(arndale_rt5631_dai),
76 };
77
78 static void arndale_put_of_nodes(struct snd_soc_card *card)
79 {
80         struct snd_soc_dai_link *dai_link;
81         int i;
82
83         for_each_card_prelinks(card, i, dai_link) {
84                 of_node_put(dai_link->cpus->of_node);
85                 of_node_put(dai_link->codecs->of_node);
86         }
87 }
88
89 static int arndale_audio_probe(struct platform_device *pdev)
90 {
91         int n, ret;
92         struct device_node *np = pdev->dev.of_node;
93         struct snd_soc_card *card = &arndale_rt5631;
94
95         card->dev = &pdev->dev;
96
97         for (n = 0; np && n < ARRAY_SIZE(arndale_rt5631_dai); n++) {
98                 if (!arndale_rt5631_dai[n].cpus->dai_name) {
99                         arndale_rt5631_dai[n].cpus->of_node = of_parse_phandle(np,
100                                         "samsung,audio-cpu", n);
101
102                         if (!arndale_rt5631_dai[n].cpus->of_node) {
103                                 dev_err(&pdev->dev,
104                                 "Property 'samsung,audio-cpu' missing or invalid\n");
105                                 return -EINVAL;
106                         }
107                 }
108                 if (!arndale_rt5631_dai[n].platforms->name)
109                         arndale_rt5631_dai[n].platforms->of_node =
110                                         arndale_rt5631_dai[n].cpus->of_node;
111
112                 arndale_rt5631_dai[n].codecs->name = NULL;
113                 arndale_rt5631_dai[n].codecs->of_node = of_parse_phandle(np,
114                                         "samsung,audio-codec", n);
115                 if (!arndale_rt5631_dai[0].codecs->of_node) {
116                         dev_err(&pdev->dev,
117                         "Property 'samsung,audio-codec' missing or invalid\n");
118                         ret = -EINVAL;
119                         goto err_put_of_nodes;
120                 }
121         }
122
123         ret = devm_snd_soc_register_card(card->dev, card);
124         if (ret) {
125                 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
126                 goto err_put_of_nodes;
127         }
128         return 0;
129
130 err_put_of_nodes:
131         arndale_put_of_nodes(card);
132         return ret;
133 }
134
135 static int arndale_audio_remove(struct platform_device *pdev)
136 {
137         struct snd_soc_card *card = platform_get_drvdata(pdev);
138
139         arndale_put_of_nodes(card);
140         return 0;
141 }
142
143 static const struct of_device_id samsung_arndale_rt5631_of_match[] __maybe_unused = {
144         { .compatible = "samsung,arndale-rt5631", },
145         { .compatible = "samsung,arndale-alc5631", },
146         {},
147 };
148 MODULE_DEVICE_TABLE(of, samsung_arndale_rt5631_of_match);
149
150 static struct platform_driver arndale_audio_driver = {
151         .driver = {
152                 .name   = "arndale-audio",
153                 .pm = &snd_soc_pm_ops,
154                 .of_match_table = of_match_ptr(samsung_arndale_rt5631_of_match),
155         },
156         .probe = arndale_audio_probe,
157         .remove = arndale_audio_remove,
158 };
159
160 module_platform_driver(arndale_audio_driver);
161
162 MODULE_AUTHOR("Claude <claude@insignal.co.kr>");
163 MODULE_DESCRIPTION("ALSA SoC Driver for Arndale Board");
164 MODULE_LICENSE("GPL");