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