GNU Linux-libre 4.19.211-gnu1
[releases.git] / sound / soc / fsl / eukrea-tlv320.c
1 /*
2  * eukrea-tlv320.c  --  SoC audio for eukrea_cpuimxXX in I2S mode
3  *
4  * Copyright 2010 Eric Bénard, Eukréa Electromatique <eric@eukrea.com>
5  *
6  * based on sound/soc/s3c24xx/s3c24xx_simtec_tlv320aic23.c
7  * which is Copyright 2009 Simtec Electronics
8  * and on sound/soc/imx/phycore-ac97.c which is
9  * Copyright 2009 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  */
17
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/device.h>
24 #include <linux/i2c.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/soc.h>
28 #include <asm/mach-types.h>
29
30 #include "../codecs/tlv320aic23.h"
31 #include "imx-ssi.h"
32 #include "imx-audmux.h"
33
34 #define CODEC_CLOCK 12000000
35
36 static int eukrea_tlv320_hw_params(struct snd_pcm_substream *substream,
37                             struct snd_pcm_hw_params *params)
38 {
39         struct snd_soc_pcm_runtime *rtd = substream->private_data;
40         struct snd_soc_dai *codec_dai = rtd->codec_dai;
41         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
42         int ret;
43
44         ret = snd_soc_dai_set_sysclk(codec_dai, 0,
45                                      CODEC_CLOCK, SND_SOC_CLOCK_OUT);
46         if (ret) {
47                 dev_err(cpu_dai->dev,
48                         "Failed to set the codec sysclk.\n");
49                 return ret;
50         }
51
52         snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
53
54         ret = snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
55                                 SND_SOC_CLOCK_IN);
56         /* fsl_ssi lacks the set_sysclk ops */
57         if (ret && ret != -EINVAL) {
58                 dev_err(cpu_dai->dev,
59                         "Can't set the IMX_SSP_SYS_CLK CPU system clock.\n");
60                 return ret;
61         }
62
63         return 0;
64 }
65
66 static const struct snd_soc_ops eukrea_tlv320_snd_ops = {
67         .hw_params      = eukrea_tlv320_hw_params,
68 };
69
70 static struct snd_soc_dai_link eukrea_tlv320_dai = {
71         .name           = "tlv320aic23",
72         .stream_name    = "TLV320AIC23",
73         .codec_dai_name = "tlv320aic23-hifi",
74         .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
75                           SND_SOC_DAIFMT_CBM_CFM,
76         .ops            = &eukrea_tlv320_snd_ops,
77 };
78
79 static struct snd_soc_card eukrea_tlv320 = {
80         .owner          = THIS_MODULE,
81         .dai_link       = &eukrea_tlv320_dai,
82         .num_links      = 1,
83 };
84
85 static int eukrea_tlv320_probe(struct platform_device *pdev)
86 {
87         int ret;
88         int int_port = 0, ext_port;
89         struct device_node *np = pdev->dev.of_node;
90         struct device_node *ssi_np = NULL, *codec_np = NULL;
91
92         eukrea_tlv320.dev = &pdev->dev;
93         if (np) {
94                 ret = snd_soc_of_parse_card_name(&eukrea_tlv320,
95                                                  "eukrea,model");
96                 if (ret) {
97                         dev_err(&pdev->dev,
98                                 "eukrea,model node missing or invalid.\n");
99                         goto err;
100                 }
101
102                 ssi_np = of_parse_phandle(pdev->dev.of_node,
103                                           "ssi-controller", 0);
104                 if (!ssi_np) {
105                         dev_err(&pdev->dev,
106                                 "ssi-controller missing or invalid.\n");
107                         ret = -ENODEV;
108                         goto err;
109                 }
110
111                 codec_np = of_parse_phandle(ssi_np, "codec-handle", 0);
112                 if (codec_np)
113                         eukrea_tlv320_dai.codec_of_node = codec_np;
114                 else
115                         dev_err(&pdev->dev, "codec-handle node missing or invalid.\n");
116
117                 ret = of_property_read_u32(np, "fsl,mux-int-port", &int_port);
118                 if (ret) {
119                         dev_err(&pdev->dev,
120                                 "fsl,mux-int-port node missing or invalid.\n");
121                         goto err;
122                 }
123                 ret = of_property_read_u32(np, "fsl,mux-ext-port", &ext_port);
124                 if (ret) {
125                         dev_err(&pdev->dev,
126                                 "fsl,mux-ext-port node missing or invalid.\n");
127                         goto err;
128                 }
129
130                 /*
131                  * The port numbering in the hardware manual starts at 1, while
132                  * the audmux API expects it starts at 0.
133                  */
134                 int_port--;
135                 ext_port--;
136
137                 eukrea_tlv320_dai.cpu_of_node = ssi_np;
138                 eukrea_tlv320_dai.platform_of_node = ssi_np;
139         } else {
140                 eukrea_tlv320_dai.cpu_dai_name = "imx-ssi.0";
141                 eukrea_tlv320_dai.platform_name = "imx-ssi.0";
142                 eukrea_tlv320_dai.codec_name = "tlv320aic23-codec.0-001a";
143                 eukrea_tlv320.name = "cpuimx-audio";
144         }
145
146         if (machine_is_eukrea_cpuimx27() ||
147             of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux")) {
148                 imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
149                         IMX_AUDMUX_V1_PCR_SYN |
150                         IMX_AUDMUX_V1_PCR_TFSDIR |
151                         IMX_AUDMUX_V1_PCR_TCLKDIR |
152                         IMX_AUDMUX_V1_PCR_RFSDIR |
153                         IMX_AUDMUX_V1_PCR_RCLKDIR |
154                         IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
155                         IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
156                         IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
157                 );
158                 imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
159                         IMX_AUDMUX_V1_PCR_SYN |
160                         IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
161                 );
162         } else if (machine_is_eukrea_cpuimx25sd() ||
163                    machine_is_eukrea_cpuimx35sd() ||
164                    machine_is_eukrea_cpuimx51sd() ||
165                    of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux")) {
166                 if (!np)
167                         ext_port = machine_is_eukrea_cpuimx25sd() ?
168                                 4 : 3;
169
170                 imx_audmux_v2_configure_port(int_port,
171                         IMX_AUDMUX_V2_PTCR_SYN |
172                         IMX_AUDMUX_V2_PTCR_TFSDIR |
173                         IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
174                         IMX_AUDMUX_V2_PTCR_TCLKDIR |
175                         IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
176                         IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
177                 );
178                 imx_audmux_v2_configure_port(ext_port,
179                         IMX_AUDMUX_V2_PTCR_SYN,
180                         IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
181                 );
182         } else {
183                 if (np) {
184                         /* The eukrea,asoc-tlv320 driver was explicitly
185                          * requested (through the device tree).
186                          */
187                         dev_err(&pdev->dev,
188                                 "Missing or invalid audmux DT node.\n");
189                         return -ENODEV;
190                 } else {
191                         /* Return happy.
192                          * We might run on a totally different machine.
193                          */
194                         return 0;
195                 }
196         }
197
198         ret = snd_soc_register_card(&eukrea_tlv320);
199 err:
200         if (ret)
201                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
202         of_node_put(ssi_np);
203
204         return ret;
205 }
206
207 static int eukrea_tlv320_remove(struct platform_device *pdev)
208 {
209         snd_soc_unregister_card(&eukrea_tlv320);
210
211         return 0;
212 }
213
214 static const struct of_device_id imx_tlv320_dt_ids[] = {
215         { .compatible = "eukrea,asoc-tlv320"},
216         { /* sentinel */ }
217 };
218 MODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
219
220 static struct platform_driver eukrea_tlv320_driver = {
221         .driver = {
222                 .name = "eukrea_tlv320",
223                 .of_match_table = imx_tlv320_dt_ids,
224         },
225         .probe = eukrea_tlv320_probe,
226         .remove = eukrea_tlv320_remove,
227 };
228
229 module_platform_driver(eukrea_tlv320_driver);
230
231 MODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
232 MODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
233 MODULE_LICENSE("GPL");
234 MODULE_ALIAS("platform:eukrea_tlv320");