GNU Linux-libre 4.14.290-gnu1
[releases.git] / sound / soc / fsl / imx-es8328.c
1 /*
2  * Copyright 2012 Freescale Semiconductor, Inc.
3  * Copyright 2012 Linaro Ltd.
4  *
5  * The code contained herein is licensed under the GNU General Public
6  * License. You may obtain a copy of the GNU General Public License
7  * Version 2 or later at the following locations:
8  *
9  * http://www.opensource.org/licenses/gpl-license.html
10  * http://www.gnu.org/copyleft/gpl.html
11  */
12
13 #include <linux/gpio.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/i2c.h>
18 #include <linux/of_gpio.h>
19 #include <sound/soc.h>
20 #include <sound/jack.h>
21
22 #include "imx-audmux.h"
23
24 #define DAI_NAME_SIZE   32
25 #define MUX_PORT_MAX    7
26
27 struct imx_es8328_data {
28         struct device *dev;
29         struct snd_soc_dai_link dai;
30         struct snd_soc_card card;
31         char codec_dai_name[DAI_NAME_SIZE];
32         char platform_name[DAI_NAME_SIZE];
33         int jack_gpio;
34 };
35
36 static struct snd_soc_jack_gpio headset_jack_gpios[] = {
37         {
38                 .gpio = -1,
39                 .name = "headset-gpio",
40                 .report = SND_JACK_HEADSET,
41                 .invert = 0,
42                 .debounce_time = 200,
43         },
44 };
45
46 static struct snd_soc_jack headset_jack;
47
48 static int imx_es8328_dai_init(struct snd_soc_pcm_runtime *rtd)
49 {
50         struct imx_es8328_data *data = container_of(rtd->card,
51                                         struct imx_es8328_data, card);
52         int ret = 0;
53
54         /* Headphone jack detection */
55         if (gpio_is_valid(data->jack_gpio)) {
56                 ret = snd_soc_card_jack_new(rtd->card, "Headphone",
57                                             SND_JACK_HEADPHONE | SND_JACK_BTN_0,
58                                             &headset_jack, NULL, 0);
59                 if (ret)
60                         return ret;
61
62                 headset_jack_gpios[0].gpio = data->jack_gpio;
63                 ret = snd_soc_jack_add_gpios(&headset_jack,
64                                              ARRAY_SIZE(headset_jack_gpios),
65                                              headset_jack_gpios);
66         }
67
68         return ret;
69 }
70
71 static const struct snd_soc_dapm_widget imx_es8328_dapm_widgets[] = {
72         SND_SOC_DAPM_MIC("Mic Jack", NULL),
73         SND_SOC_DAPM_HP("Headphone", NULL),
74         SND_SOC_DAPM_SPK("Speaker", NULL),
75         SND_SOC_DAPM_REGULATOR_SUPPLY("audio-amp", 1, 0),
76 };
77
78 static int imx_es8328_probe(struct platform_device *pdev)
79 {
80         struct device_node *np = pdev->dev.of_node;
81         struct device_node *ssi_np = NULL, *codec_np = NULL;
82         struct platform_device *ssi_pdev;
83         struct imx_es8328_data *data;
84         u32 int_port, ext_port;
85         int ret;
86         struct device *dev = &pdev->dev;
87
88         ret = of_property_read_u32(np, "mux-int-port", &int_port);
89         if (ret) {
90                 dev_err(dev, "mux-int-port missing or invalid\n");
91                 goto fail;
92         }
93         if (int_port > MUX_PORT_MAX || int_port == 0) {
94                 dev_err(dev, "mux-int-port: hardware only has %d mux ports\n",
95                         MUX_PORT_MAX);
96                 ret = -EINVAL;
97                 goto fail;
98         }
99
100         ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
101         if (ret) {
102                 dev_err(dev, "mux-ext-port missing or invalid\n");
103                 goto fail;
104         }
105         if (ext_port > MUX_PORT_MAX || ext_port == 0) {
106                 dev_err(dev, "mux-ext-port: hardware only has %d mux ports\n",
107                         MUX_PORT_MAX);
108                 ret = -EINVAL;
109                 goto fail;
110         }
111
112         /*
113          * The port numbering in the hardware manual starts at 1, while
114          * the audmux API expects it starts at 0.
115          */
116         int_port--;
117         ext_port--;
118         ret = imx_audmux_v2_configure_port(int_port,
119                         IMX_AUDMUX_V2_PTCR_SYN |
120                         IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
121                         IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
122                         IMX_AUDMUX_V2_PTCR_TFSDIR |
123                         IMX_AUDMUX_V2_PTCR_TCLKDIR,
124                         IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
125         if (ret) {
126                 dev_err(dev, "audmux internal port setup failed\n");
127                 return ret;
128         }
129         ret = imx_audmux_v2_configure_port(ext_port,
130                         IMX_AUDMUX_V2_PTCR_SYN,
131                         IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
132         if (ret) {
133                 dev_err(dev, "audmux external port setup failed\n");
134                 return ret;
135         }
136
137         ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
138         codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
139         if (!ssi_np || !codec_np) {
140                 dev_err(dev, "phandle missing or invalid\n");
141                 ret = -EINVAL;
142                 goto fail;
143         }
144
145         ssi_pdev = of_find_device_by_node(ssi_np);
146         if (!ssi_pdev) {
147                 dev_err(dev, "failed to find SSI platform device\n");
148                 ret = -EINVAL;
149                 goto fail;
150         }
151
152         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
153         if (!data) {
154                 ret = -ENOMEM;
155                 goto fail;
156         }
157
158         data->dev = dev;
159
160         data->jack_gpio = of_get_named_gpio(pdev->dev.of_node, "jack-gpio", 0);
161
162         data->dai.name = "hifi";
163         data->dai.stream_name = "hifi";
164         data->dai.codec_dai_name = "es8328-hifi-analog";
165         data->dai.codec_of_node = codec_np;
166         data->dai.cpu_of_node = ssi_np;
167         data->dai.platform_of_node = ssi_np;
168         data->dai.init = &imx_es8328_dai_init;
169         data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
170                             SND_SOC_DAIFMT_CBM_CFM;
171
172         data->card.dev = dev;
173         data->card.dapm_widgets = imx_es8328_dapm_widgets;
174         data->card.num_dapm_widgets = ARRAY_SIZE(imx_es8328_dapm_widgets);
175         ret = snd_soc_of_parse_card_name(&data->card, "model");
176         if (ret) {
177                 dev_err(dev, "Unable to parse card name\n");
178                 goto fail;
179         }
180         ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
181         if (ret) {
182                 dev_err(dev, "Unable to parse routing: %d\n", ret);
183                 goto fail;
184         }
185         data->card.num_links = 1;
186         data->card.owner = THIS_MODULE;
187         data->card.dai_link = &data->dai;
188
189         ret = snd_soc_register_card(&data->card);
190         if (ret) {
191                 dev_err(dev, "Unable to register: %d\n", ret);
192                 goto fail;
193         }
194
195         platform_set_drvdata(pdev, data);
196 fail:
197         of_node_put(ssi_np);
198         of_node_put(codec_np);
199
200         return ret;
201 }
202
203 static int imx_es8328_remove(struct platform_device *pdev)
204 {
205         struct imx_es8328_data *data = platform_get_drvdata(pdev);
206
207         snd_soc_unregister_card(&data->card);
208
209         return 0;
210 }
211
212 static const struct of_device_id imx_es8328_dt_ids[] = {
213         { .compatible = "fsl,imx-audio-es8328", },
214         { /* sentinel */ }
215 };
216 MODULE_DEVICE_TABLE(of, imx_es8328_dt_ids);
217
218 static struct platform_driver imx_es8328_driver = {
219         .driver = {
220                 .name = "imx-es8328",
221                 .of_match_table = imx_es8328_dt_ids,
222         },
223         .probe = imx_es8328_probe,
224         .remove = imx_es8328_remove,
225 };
226 module_platform_driver(imx_es8328_driver);
227
228 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
229 MODULE_DESCRIPTION("Kosagi i.MX6 ES8328 ASoC machine driver");
230 MODULE_LICENSE("GPL v2");
231 MODULE_ALIAS("platform:imx-audio-es8328");