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 | static struct snd_soc_jack_pin headset_jack_pins[] = { |
41 | { |
42 | .pin = "Headphone" , |
43 | .mask = SND_JACK_HEADPHONE, |
44 | }, |
45 | { |
46 | .pin = "Mic Jack" , |
47 | .mask = SND_JACK_MICROPHONE, |
48 | }, |
49 | }; |
50 | |
51 | static int imx_es8328_dai_init(struct snd_soc_pcm_runtime *rtd) |
52 | { |
53 | struct imx_es8328_data *data = container_of(rtd->card, |
54 | struct imx_es8328_data, card); |
55 | int ret = 0; |
56 | |
57 | /* Headphone jack detection */ |
58 | if (gpio_is_valid(number: data->jack_gpio)) { |
59 | ret = snd_soc_card_jack_new_pins(card: rtd->card, id: "Headphone" , |
60 | type: SND_JACK_HEADSET | SND_JACK_BTN_0, |
61 | jack: &headset_jack, |
62 | pins: headset_jack_pins, |
63 | ARRAY_SIZE(headset_jack_pins)); |
64 | if (ret) |
65 | return ret; |
66 | |
67 | headset_jack_gpios[0].gpio = data->jack_gpio; |
68 | ret = snd_soc_jack_add_gpios(jack: &headset_jack, |
69 | ARRAY_SIZE(headset_jack_gpios), |
70 | gpios: headset_jack_gpios); |
71 | } |
72 | |
73 | return ret; |
74 | } |
75 | |
76 | static const struct snd_soc_dapm_widget imx_es8328_dapm_widgets[] = { |
77 | SND_SOC_DAPM_MIC("Mic Jack" , NULL), |
78 | SND_SOC_DAPM_HP("Headphone" , NULL), |
79 | SND_SOC_DAPM_SPK("Speaker" , NULL), |
80 | SND_SOC_DAPM_REGULATOR_SUPPLY("audio-amp" , 1, 0), |
81 | }; |
82 | |
83 | static const struct snd_kcontrol_new imx_es8328_controls[] = { |
84 | SOC_DAPM_PIN_SWITCH("Headphone" ), |
85 | SOC_DAPM_PIN_SWITCH("Mic Jack" ), |
86 | }; |
87 | |
88 | static int imx_es8328_probe(struct platform_device *pdev) |
89 | { |
90 | struct device_node *np = pdev->dev.of_node; |
91 | struct device_node *ssi_np = NULL, *codec_np = NULL; |
92 | struct platform_device *ssi_pdev; |
93 | struct imx_es8328_data *data; |
94 | struct snd_soc_dai_link_component *comp; |
95 | u32 int_port, ext_port; |
96 | int ret; |
97 | struct device *dev = &pdev->dev; |
98 | |
99 | ret = of_property_read_u32(np, propname: "mux-int-port" , out_value: &int_port); |
100 | if (ret) { |
101 | dev_err(dev, "mux-int-port missing or invalid\n" ); |
102 | goto fail; |
103 | } |
104 | if (int_port > MUX_PORT_MAX || int_port == 0) { |
105 | dev_err(dev, "mux-int-port: hardware only has %d mux ports\n" , |
106 | MUX_PORT_MAX); |
107 | ret = -EINVAL; |
108 | goto fail; |
109 | } |
110 | |
111 | ret = of_property_read_u32(np, propname: "mux-ext-port" , out_value: &ext_port); |
112 | if (ret) { |
113 | dev_err(dev, "mux-ext-port missing or invalid\n" ); |
114 | goto fail; |
115 | } |
116 | if (ext_port > MUX_PORT_MAX || ext_port == 0) { |
117 | dev_err(dev, "mux-ext-port: hardware only has %d mux ports\n" , |
118 | MUX_PORT_MAX); |
119 | ret = -EINVAL; |
120 | goto fail; |
121 | } |
122 | |
123 | /* |
124 | * The port numbering in the hardware manual starts at 1, while |
125 | * the audmux API expects it starts at 0. |
126 | */ |
127 | int_port--; |
128 | ext_port--; |
129 | ret = imx_audmux_v2_configure_port(port: int_port, |
130 | IMX_AUDMUX_V2_PTCR_SYN | |
131 | IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | |
132 | IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | |
133 | IMX_AUDMUX_V2_PTCR_TFSDIR | |
134 | IMX_AUDMUX_V2_PTCR_TCLKDIR, |
135 | IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)); |
136 | if (ret) { |
137 | dev_err(dev, "audmux internal port setup failed\n" ); |
138 | return ret; |
139 | } |
140 | ret = imx_audmux_v2_configure_port(port: ext_port, |
141 | IMX_AUDMUX_V2_PTCR_SYN, |
142 | IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)); |
143 | if (ret) { |
144 | dev_err(dev, "audmux external port setup failed\n" ); |
145 | return ret; |
146 | } |
147 | |
148 | ssi_np = of_parse_phandle(np: pdev->dev.of_node, phandle_name: "ssi-controller" , index: 0); |
149 | codec_np = of_parse_phandle(np: pdev->dev.of_node, phandle_name: "audio-codec" , index: 0); |
150 | if (!ssi_np || !codec_np) { |
151 | dev_err(dev, "phandle missing or invalid\n" ); |
152 | ret = -EINVAL; |
153 | goto fail; |
154 | } |
155 | |
156 | ssi_pdev = of_find_device_by_node(np: ssi_np); |
157 | if (!ssi_pdev) { |
158 | dev_err(dev, "failed to find SSI platform device\n" ); |
159 | ret = -EINVAL; |
160 | goto fail; |
161 | } |
162 | |
163 | data = devm_kzalloc(dev, size: sizeof(*data), GFP_KERNEL); |
164 | if (!data) { |
165 | ret = -ENOMEM; |
166 | goto put_device; |
167 | } |
168 | |
169 | comp = devm_kzalloc(dev, size: 2 * sizeof(*comp), GFP_KERNEL); |
170 | if (!comp) { |
171 | ret = -ENOMEM; |
172 | goto put_device; |
173 | } |
174 | |
175 | data->dev = dev; |
176 | |
177 | data->jack_gpio = of_get_named_gpio(np: pdev->dev.of_node, list_name: "jack-gpio" , index: 0); |
178 | |
179 | /* |
180 | * CPU == Platform |
181 | * platform is using soc-generic-dmaengine-pcm |
182 | */ |
183 | data->dai.cpus = |
184 | data->dai.platforms = &comp[0]; |
185 | data->dai.codecs = &comp[1]; |
186 | |
187 | data->dai.num_cpus = 1; |
188 | data->dai.num_codecs = 1; |
189 | data->dai.num_platforms = 1; |
190 | |
191 | data->dai.name = "hifi" ; |
192 | data->dai.stream_name = "hifi" ; |
193 | data->dai.codecs->dai_name = "es8328-hifi-analog" ; |
194 | data->dai.codecs->of_node = codec_np; |
195 | data->dai.cpus->of_node = ssi_np; |
196 | data->dai.init = &imx_es8328_dai_init; |
197 | data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | |
198 | SND_SOC_DAIFMT_CBP_CFP; |
199 | |
200 | data->card.dev = dev; |
201 | data->card.dapm_widgets = imx_es8328_dapm_widgets; |
202 | data->card.num_dapm_widgets = ARRAY_SIZE(imx_es8328_dapm_widgets); |
203 | data->card.controls = imx_es8328_controls; |
204 | data->card.num_controls = ARRAY_SIZE(imx_es8328_controls); |
205 | ret = snd_soc_of_parse_card_name(card: &data->card, propname: "model" ); |
206 | if (ret) { |
207 | dev_err(dev, "Unable to parse card name\n" ); |
208 | goto put_device; |
209 | } |
210 | ret = snd_soc_of_parse_audio_routing(card: &data->card, propname: "audio-routing" ); |
211 | if (ret) { |
212 | dev_err(dev, "Unable to parse routing: %d\n" , ret); |
213 | goto put_device; |
214 | } |
215 | data->card.num_links = 1; |
216 | data->card.owner = THIS_MODULE; |
217 | data->card.dai_link = &data->dai; |
218 | |
219 | ret = devm_snd_soc_register_card(dev: &pdev->dev, card: &data->card); |
220 | if (ret) { |
221 | dev_err(dev, "Unable to register: %d\n" , ret); |
222 | goto put_device; |
223 | } |
224 | |
225 | platform_set_drvdata(pdev, data); |
226 | put_device: |
227 | put_device(dev: &ssi_pdev->dev); |
228 | fail: |
229 | of_node_put(node: ssi_np); |
230 | of_node_put(node: codec_np); |
231 | |
232 | return ret; |
233 | } |
234 | |
235 | static const struct of_device_id imx_es8328_dt_ids[] = { |
236 | { .compatible = "fsl,imx-audio-es8328" , }, |
237 | { /* sentinel */ } |
238 | }; |
239 | MODULE_DEVICE_TABLE(of, imx_es8328_dt_ids); |
240 | |
241 | static struct platform_driver imx_es8328_driver = { |
242 | .driver = { |
243 | .name = "imx-es8328" , |
244 | .of_match_table = imx_es8328_dt_ids, |
245 | }, |
246 | .probe = imx_es8328_probe, |
247 | }; |
248 | module_platform_driver(imx_es8328_driver); |
249 | |
250 | MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>" ); |
251 | MODULE_DESCRIPTION("Kosagi i.MX6 ES8328 ASoC machine driver" ); |
252 | MODULE_LICENSE("GPL v2" ); |
253 | MODULE_ALIAS("platform:imx-audio-es8328" ); |
254 | |