Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / i-sabre-q2m.c
1 /*
2  * ASoC Driver for I-Sabre Q2M
3  *
4  * Author: Satoru Kawase
5  * Modified by: Xiao Qingyong
6  * Update kernel v4.18+ by : Audiophonics
7  *              Copyright 2018 Audiophonics
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
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/fs.h>
24 #include <asm/uaccess.h>
25 #include <sound/core.h>
26 #include <sound/soc.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29
30 #include "../codecs/i-sabre-codec.h"
31
32
33 static int snd_rpi_i_sabre_q2m_init(struct snd_soc_pcm_runtime *rtd)
34 {
35         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
36         unsigned int value;
37
38         /* Device ID */
39         value = snd_soc_component_read(component, ISABRECODEC_REG_01);
40         dev_info(component->card->dev, "Audiophonics Device ID : %02X\n", value);
41
42         /* API revision */
43         value = snd_soc_component_read(component, ISABRECODEC_REG_02);
44         dev_info(component->card->dev, "Audiophonics API revision : %02X\n", value);
45
46         return 0;
47 }
48
49 static int snd_rpi_i_sabre_q2m_hw_params(
50         struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
51 {
52         struct snd_soc_pcm_runtime *rtd     = substream->private_data;
53         struct snd_soc_dai         *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
54         int bclk_ratio;
55
56         bclk_ratio = snd_pcm_format_physical_width(
57                         params_format(params)) * params_channels(params);
58         return snd_soc_dai_set_bclk_ratio(cpu_dai, bclk_ratio);
59 }
60
61 /* machine stream operations */
62 static struct snd_soc_ops snd_rpi_i_sabre_q2m_ops = {
63         .hw_params = snd_rpi_i_sabre_q2m_hw_params,
64 };
65
66 SND_SOC_DAILINK_DEFS(rpi_i_sabre_q2m,
67         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
68         DAILINK_COMP_ARRAY(COMP_CODEC("i-sabre-codec-i2c.1-0048", "i-sabre-codec-dai")),
69         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
70
71 static struct snd_soc_dai_link snd_rpi_i_sabre_q2m_dai[] = {
72         {
73                 .name           = "I-Sabre Q2M",
74                 .stream_name    = "I-Sabre Q2M DAC",
75                 .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
76                                                 | SND_SOC_DAIFMT_CBS_CFS,
77                 .init           = snd_rpi_i_sabre_q2m_init,
78                 .ops            = &snd_rpi_i_sabre_q2m_ops,
79                 SND_SOC_DAILINK_REG(rpi_i_sabre_q2m),
80         }
81 };
82
83 /* audio machine driver */
84 static struct snd_soc_card snd_rpi_i_sabre_q2m = {
85         .name      = "I-Sabre Q2M DAC",
86         .owner     = THIS_MODULE,
87         .dai_link  = snd_rpi_i_sabre_q2m_dai,
88         .num_links = ARRAY_SIZE(snd_rpi_i_sabre_q2m_dai)
89 };
90
91
92 static int snd_rpi_i_sabre_q2m_probe(struct platform_device *pdev)
93 {
94         int ret = 0;
95
96         snd_rpi_i_sabre_q2m.dev = &pdev->dev;
97         if (pdev->dev.of_node) {
98                 struct device_node *i2s_node;
99                 struct snd_soc_dai_link *dai;
100
101                 dai = &snd_rpi_i_sabre_q2m_dai[0];
102                 i2s_node = of_parse_phandle(pdev->dev.of_node,
103                                                         "i2s-controller", 0);
104                 if (i2s_node) {
105                         dai->cpus->dai_name     = NULL;
106                         dai->cpus->of_node      = i2s_node;
107                         dai->platforms->name    = NULL;
108                         dai->platforms->of_node = i2s_node;
109                 } else {
110                         dev_err(&pdev->dev,
111                             "Property 'i2s-controller' missing or invalid\n");
112                         return (-EINVAL);
113                 }
114
115                 dai->name        = "I-Sabre Q2M";
116                 dai->stream_name = "I-Sabre Q2M DAC";
117                 dai->dai_fmt     = SND_SOC_DAIFMT_I2S
118                                         | SND_SOC_DAIFMT_NB_NF
119                                         | SND_SOC_DAIFMT_CBS_CFS;
120         }
121
122         /* Wait for registering codec driver */
123         mdelay(50);
124
125         ret = snd_soc_register_card(&snd_rpi_i_sabre_q2m);
126         if (ret) {
127                 dev_err(&pdev->dev,
128                         "snd_soc_register_card() failed: %d\n", ret);
129         }
130
131         return ret;
132 }
133
134 static int snd_rpi_i_sabre_q2m_remove(struct platform_device *pdev)
135 {
136         return snd_soc_unregister_card(&snd_rpi_i_sabre_q2m);
137 }
138
139 static const struct of_device_id snd_rpi_i_sabre_q2m_of_match[] = {
140         { .compatible = "audiophonics,i-sabre-q2m", },
141         {}
142 };
143 MODULE_DEVICE_TABLE(of, snd_rpi_i_sabre_q2m_of_match);
144
145 static struct platform_driver snd_rpi_i_sabre_q2m_driver = {
146         .driver = {
147                 .name           = "snd-rpi-i-sabre-q2m",
148                 .owner          = THIS_MODULE,
149                 .of_match_table = snd_rpi_i_sabre_q2m_of_match,
150         },
151         .probe  = snd_rpi_i_sabre_q2m_probe,
152         .remove = snd_rpi_i_sabre_q2m_remove,
153 };
154 module_platform_driver(snd_rpi_i_sabre_q2m_driver);
155
156 MODULE_DESCRIPTION("ASoC Driver for I-Sabre Q2M");
157 MODULE_AUTHOR("Audiophonics <http://www.audiophonics.fr>");
158 MODULE_LICENSE("GPL");