blob: deb179898b6ae37b1b26ce2afd7c399ceca18f63 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/bitops.h>
18#include <linux/slab.h>
19#include <linux/of_device.h>
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/soc.h>
23#include <sound/apr_audio-v2.h>
24#include <sound/q6afe-v2.h>
25#include <sound/msm-dai-q6-v2.h>
26#include <sound/pcm_params.h>
27
28#define HDMI_RX_CA_MAX 0x32
29
30enum {
31 STATUS_PORT_STARTED, /* track if AFE port has started */
32 STATUS_MAX
33};
34
35struct msm_ext_disp_ca {
36 bool set_ca;
37 u32 ca;
38};
39
40struct msm_dai_q6_hdmi_dai_data {
41 DECLARE_BITMAP(status_mask, STATUS_MAX);
42 u32 rate;
43 u32 channels;
44 struct msm_ext_disp_ca ca;
45 union afe_port_config port_config;
46};
47
48static int msm_dai_q6_ext_disp_format_put(struct snd_kcontrol *kcontrol,
49 struct snd_ctl_elem_value *ucontrol)
50{
51 struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
52 int value = ucontrol->value.integer.value[0];
53
54 if (!dai_data) {
55 pr_err("%s: dai_data is NULL\n", __func__);
56 return -EINVAL;
57 }
58
59 dai_data->port_config.hdmi_multi_ch.datatype = value;
60 pr_debug("%s: value = %d\n", __func__, value);
61
62 return 0;
63}
64
65static int msm_dai_q6_ext_disp_format_get(struct snd_kcontrol *kcontrol,
66 struct snd_ctl_elem_value *ucontrol)
67{
68 struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
69
70 if (!dai_data) {
71 pr_err("%s: dai_data is NULL\n", __func__);
72 return -EINVAL;
73 }
74
75 ucontrol->value.integer.value[0] =
76 dai_data->port_config.hdmi_multi_ch.datatype;
77 pr_debug("%s: value = %ld\n",
78 __func__, ucontrol->value.integer.value[0]);
79
80 return 0;
81}
82
83static int msm_dai_q6_ext_disp_ca_put(struct snd_kcontrol *kcontrol,
84 struct snd_ctl_elem_value *ucontrol)
85{
86 struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
87
88 if (!dai_data) {
89 pr_err("%s: dai_data is NULL\n", __func__);
90 return -EINVAL;
91 }
92
93 dai_data->ca.ca = ucontrol->value.integer.value[0];
94 dai_data->ca.set_ca = true;
95 pr_debug("%s: ca = %d\n", __func__, dai_data->ca.ca);
96 return 0;
97}
98
99static int msm_dai_q6_ext_disp_ca_get(struct snd_kcontrol *kcontrol,
100 struct snd_ctl_elem_value *ucontrol)
101{
102 struct msm_dai_q6_hdmi_dai_data *dai_data = kcontrol->private_data;
103
104 if (!dai_data) {
105 pr_err("%s: dai_data is NULL\n", __func__);
106 return -EINVAL;
107 }
108
109 ucontrol->value.integer.value[0] = dai_data->ca.ca;
110 pr_debug("%s: ca = %d\n", __func__, dai_data->ca.ca);
111 return 0;
112}
113
114/* HDMI format field for AFE_PORT_MULTI_CHAN_HDMI_AUDIO_IF_CONFIG command
115 * 0: linear PCM
116 * 1: non-linear PCM
117 */
118static const char * const hdmi_format[] = {
119 "LPCM",
120 "Compr"
121};
122
123static const struct soc_enum hdmi_config_enum[] = {
124 SOC_ENUM_SINGLE_EXT(2, hdmi_format),
125};
126
127static int msm_dai_q6_ext_disp_drift_info(struct snd_kcontrol *kcontrol,
128 struct snd_ctl_elem_info *uinfo)
129{
130 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
131 uinfo->count = sizeof(struct afe_param_id_dev_timing_stats);
132
133 return 0;
134}
135
136static int msm_dai_q6_ext_disp_drift_get(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_value *ucontrol)
138{
139 int ret = -EINVAL;
140 struct afe_param_id_dev_timing_stats timing_stats;
141 struct snd_soc_dai *dai = kcontrol->private_data;
142 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
143
144 if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
145 pr_err("%s: afe port not started. status_mask = %ld\n",
146 __func__, *dai_data->status_mask);
147 goto done;
148 }
149
150 memset(&timing_stats, 0, sizeof(struct afe_param_id_dev_timing_stats));
151 ret = afe_get_av_dev_drift(&timing_stats, dai->id);
152 if (ret) {
153 pr_err("%s: Error getting AFE Drift for port %d, err=%d\n",
154 __func__, dai->id, ret);
155
156 ret = -EINVAL;
157 goto done;
158 }
159
160 memcpy(ucontrol->value.bytes.data, (void *)&timing_stats,
161 sizeof(struct afe_param_id_dev_timing_stats));
162done:
163 return ret;
164}
165
166static const struct snd_kcontrol_new hdmi_config_controls[] = {
167 SOC_ENUM_EXT("HDMI RX Format", hdmi_config_enum[0],
168 msm_dai_q6_ext_disp_format_get,
169 msm_dai_q6_ext_disp_format_put),
170 SOC_SINGLE_MULTI_EXT("HDMI RX CA", SND_SOC_NOPM, 0,
171 HDMI_RX_CA_MAX, 0, 1,
172 msm_dai_q6_ext_disp_ca_get,
173 msm_dai_q6_ext_disp_ca_put),
174 {
175 .access = SNDRV_CTL_ELEM_ACCESS_READ,
176 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
177 .name = "HDMI DRIFT",
178 .info = msm_dai_q6_ext_disp_drift_info,
179 .get = msm_dai_q6_ext_disp_drift_get,
180 },
181};
182
183static const struct snd_kcontrol_new display_port_config_controls[] = {
184 SOC_ENUM_EXT("Display Port RX Format", hdmi_config_enum[0],
185 msm_dai_q6_ext_disp_format_get,
186 msm_dai_q6_ext_disp_format_put),
187 SOC_SINGLE_MULTI_EXT("Display Port RX CA", SND_SOC_NOPM, 0,
188 HDMI_RX_CA_MAX, 0, 1,
189 msm_dai_q6_ext_disp_ca_get,
190 msm_dai_q6_ext_disp_ca_put),
191 {
192 .access = SNDRV_CTL_ELEM_ACCESS_READ,
193 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
194 .name = "DISPLAY_PORT DRIFT",
195 .info = msm_dai_q6_ext_disp_drift_info,
196 .get = msm_dai_q6_ext_disp_drift_get,
197 },
198};
199
200/* Current implementation assumes hw_param is called once
201 * This may not be the case but what to do when ADM and AFE
202 * port are already opened and parameter changes
203 */
204static int msm_dai_q6_hdmi_hw_params(struct snd_pcm_substream *substream,
205 struct snd_pcm_hw_params *params,
206 struct snd_soc_dai *dai)
207{
208 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
209
210 dai_data->channels = params_channels(params);
211 dai_data->rate = params_rate(params);
212 dai_data->port_config.hdmi_multi_ch.reserved = 0;
213 dai_data->port_config.hdmi_multi_ch.hdmi_cfg_minor_version = 1;
214 dai_data->port_config.hdmi_multi_ch.sample_rate = dai_data->rate;
215 switch (params_format(params)) {
216 case SNDRV_PCM_FORMAT_S16_LE:
217 dai_data->port_config.hdmi_multi_ch.bit_width = 16;
218 break;
219 case SNDRV_PCM_FORMAT_S24_LE:
220 dai_data->port_config.hdmi_multi_ch.bit_width = 24;
221 break;
222 }
223
224 /*refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4*/
225 switch (dai_data->channels) {
226 case 2:
227 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0;
228 break;
229 case 3:
230 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x02;
231 break;
232 case 4:
233 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x06;
234 break;
235 case 5:
236 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0A;
237 break;
238 case 6:
239 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0B;
240 break;
241 case 7:
242 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x12;
243 break;
244 case 8:
245 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x13;
246 break;
247 default:
248 dev_err(dai->dev, "invalid Channels = %u\n",
249 dai_data->channels);
250 return -EINVAL;
251 }
252 dev_dbg(dai->dev, "%s() minor version: %u samplerate: %u bitwidth: %u\n"
253 "num_ch = %u channel_allocation = %u datatype = %d\n", __func__,
254 dai_data->port_config.hdmi_multi_ch.hdmi_cfg_minor_version,
255 dai_data->port_config.hdmi_multi_ch.sample_rate,
256 dai_data->port_config.hdmi_multi_ch.bit_width,
257 dai_data->channels,
258 dai_data->port_config.hdmi_multi_ch.channel_allocation,
259 dai_data->port_config.hdmi_multi_ch.datatype);
260
261 return 0;
262}
263
264
265static void msm_dai_q6_hdmi_shutdown(struct snd_pcm_substream *substream,
266 struct snd_soc_dai *dai)
267{
268 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
269 int rc = 0;
270
271 if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
272 pr_info("%s: afe port not started. dai_data->status_mask = %ld\n",
273 __func__, *dai_data->status_mask);
274 return;
275 }
276
277 rc = afe_close(dai->id); /* can block */
278 if (rc < 0)
279 dev_err(dai->dev, "fail to close AFE port\n");
280
281 pr_debug("%s: dai_data->status_mask = %ld\n", __func__,
282 *dai_data->status_mask);
283
284 clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
285}
286
287
288static int msm_dai_q6_hdmi_prepare(struct snd_pcm_substream *substream,
289 struct snd_soc_dai *dai)
290{
291 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
292 int rc = 0;
293
294 if (dai_data->ca.set_ca)
295 dai_data->port_config.hdmi_multi_ch.channel_allocation =
296 dai_data->ca.ca;
297
298 if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
299 rc = afe_port_start(dai->id, &dai_data->port_config,
300 dai_data->rate);
301 if (rc < 0)
302 dev_err(dai->dev, "fail to open AFE port %x\n",
303 dai->id);
304 else
305 set_bit(STATUS_PORT_STARTED,
306 dai_data->status_mask);
307 }
308
309 return rc;
310}
311
312static inline void msm_dai_q6_hdmi_set_dai_id(struct snd_soc_dai *dai)
313{
314 if (!dai->driver->id) {
315 dev_warn(dai->dev, "DAI driver id is not set\n");
316 return;
317 }
318 dai->id = dai->driver->id;
319}
320
321static int msm_dai_q6_hdmi_dai_probe(struct snd_soc_dai *dai)
322{
323 struct msm_dai_q6_hdmi_dai_data *dai_data;
324 const struct snd_kcontrol_new *kcontrol;
325 int rc = 0;
326 struct snd_soc_dapm_route intercon;
327 struct snd_soc_dapm_context *dapm;
328
329 if (!dai || !dai->driver) {
330 pr_err("%s: dai or dai->driver is NULL\n", __func__);
331 return -EINVAL;
332 }
333 dai_data = kzalloc(sizeof(struct msm_dai_q6_hdmi_dai_data),
334 GFP_KERNEL);
335
336 if (!dai_data) {
337 dev_err(dai->dev, "DAI-%d: fail to allocate dai data\n",
338 dai->id);
339 rc = -ENOMEM;
340 } else
341 dev_set_drvdata(dai->dev, dai_data);
342
343 msm_dai_q6_hdmi_set_dai_id(dai);
344
345 if (dai->driver->id == HDMI_RX) {
346 kcontrol = &hdmi_config_controls[0];
347 rc = snd_ctl_add(dai->component->card->snd_card,
348 snd_ctl_new1(kcontrol, dai_data));
349
350 kcontrol = &hdmi_config_controls[1];
351 rc = snd_ctl_add(dai->component->card->snd_card,
352 snd_ctl_new1(kcontrol, dai_data));
353
354 kcontrol = &hdmi_config_controls[2];
355 rc = snd_ctl_add(dai->component->card->snd_card,
356 snd_ctl_new1(kcontrol, dai));
357 } else if (dai->driver->id == DISPLAY_PORT_RX) {
358 kcontrol = &display_port_config_controls[0];
359 rc = snd_ctl_add(dai->component->card->snd_card,
360 snd_ctl_new1(kcontrol, dai_data));
361
362 kcontrol = &display_port_config_controls[1];
363 rc = snd_ctl_add(dai->component->card->snd_card,
364 snd_ctl_new1(kcontrol, dai_data));
365
366 kcontrol = &display_port_config_controls[2];
367 rc = snd_ctl_add(dai->component->card->snd_card,
368 snd_ctl_new1(kcontrol, dai));
369 } else {
370 dev_err(dai->dev, "%s: Invalid id:%d\n",
371 __func__, dai->driver->id);
372 kfree(dai_data);
373 dev_set_drvdata(dai->dev, NULL);
374 return -EINVAL;
375 }
376
377 dapm = snd_soc_component_get_dapm(dai->component);
378 memset(&intercon, 0, sizeof(intercon));
379 if (!rc) {
380 if (dai->driver->playback.stream_name &&
381 dai->driver->playback.aif_name) {
382 dev_dbg(dai->dev, "%s add route for widget %s",
383 __func__, dai->driver->playback.stream_name);
384 intercon.source = dai->driver->playback.aif_name;
385 intercon.sink = dai->driver->playback.stream_name;
386 dev_dbg(dai->dev, "%s src %s sink %s\n",
387 __func__, intercon.source, intercon.sink);
388 snd_soc_dapm_add_routes(dapm, &intercon, 1);
389 }
390 if (dai->driver->capture.stream_name &&
391 dai->driver->capture.aif_name) {
392 dev_dbg(dai->dev, "%s add route for widget %s",
393 __func__, dai->driver->capture.stream_name);
394 intercon.sink = dai->driver->capture.aif_name;
395 intercon.source = dai->driver->capture.stream_name;
396 dev_dbg(dai->dev, "%s src %s sink %s\n",
397 __func__, intercon.source, intercon.sink);
398 snd_soc_dapm_add_routes(dapm, &intercon, 1);
399 }
400 }
401 return rc;
402}
403
404static int msm_dai_q6_hdmi_dai_remove(struct snd_soc_dai *dai)
405{
406 struct msm_dai_q6_hdmi_dai_data *dai_data;
407 int rc;
408
409 dai_data = dev_get_drvdata(dai->dev);
410
411 /* If AFE port is still up, close it */
412 if (test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
413 rc = afe_close(dai->id); /* can block */
414 if (rc < 0)
415 dev_err(dai->dev, "fail to close AFE port\n");
416
417 clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
418 }
419 kfree(dai_data);
420
421 return 0;
422}
423
424static struct snd_soc_dai_ops msm_dai_q6_hdmi_ops = {
425 .prepare = msm_dai_q6_hdmi_prepare,
426 .hw_params = msm_dai_q6_hdmi_hw_params,
427 .shutdown = msm_dai_q6_hdmi_shutdown,
428};
429
430static struct snd_soc_dai_driver msm_dai_q6_hdmi_hdmi_rx_dai = {
431 .playback = {
432 .stream_name = "HDMI Playback",
433 .aif_name = "HDMI",
434 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
435 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
436 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
437 SNDRV_PCM_RATE_192000,
438 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
439 .channels_min = 2,
440 .channels_max = 8,
441 .rate_max = 192000,
442 .rate_min = 48000,
443 },
444 .ops = &msm_dai_q6_hdmi_ops,
445 .id = HDMI_RX,
446 .probe = msm_dai_q6_hdmi_dai_probe,
447 .remove = msm_dai_q6_hdmi_dai_remove,
448};
449
450static struct snd_soc_dai_driver msm_dai_q6_display_port_rx_dai[] = {
451 {
452 .playback = {
453 .stream_name = "Display Port Playback",
454 .aif_name = "DISPLAY_PORT",
455 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
456 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
457 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
458 SNDRV_PCM_RATE_192000,
459 .formats = SNDRV_PCM_FMTBIT_S16_LE |
460 SNDRV_PCM_FMTBIT_S24_LE,
461 .channels_min = 2,
462 .channels_max = 8,
463 .rate_max = 192000,
464 .rate_min = 48000,
465 },
466 .ops = &msm_dai_q6_hdmi_ops,
467 .id = DISPLAY_PORT_RX,
468 .probe = msm_dai_q6_hdmi_dai_probe,
469 .remove = msm_dai_q6_hdmi_dai_remove,
470 },
471};
472
473static const struct snd_soc_component_driver msm_dai_hdmi_q6_component = {
474 .name = "msm-dai-q6-hdmi",
475};
476
477/* To do: change to register DAIs as batch */
478static int msm_dai_q6_hdmi_dev_probe(struct platform_device *pdev)
479{
480 int rc, id;
481 const char *q6_dev_id = "qcom,msm-dai-q6-dev-id";
482
483 rc = of_property_read_u32(pdev->dev.of_node, q6_dev_id, &id);
484 if (rc) {
485 dev_err(&pdev->dev,
486 "%s: missing %s in dt node\n", __func__, q6_dev_id);
487 return rc;
488 }
489
490 pdev->id = id;
491
492 pr_debug("%s: dev name %s, id:%d\n", __func__,
493 dev_name(&pdev->dev), pdev->id);
494
495 switch (pdev->id) {
496 case HDMI_RX:
497 rc = snd_soc_register_component(&pdev->dev,
498 &msm_dai_hdmi_q6_component,
499 &msm_dai_q6_hdmi_hdmi_rx_dai, 1);
500 break;
501 case DISPLAY_PORT_RX:
502 rc = snd_soc_register_component(&pdev->dev,
503 &msm_dai_hdmi_q6_component,
504 &msm_dai_q6_display_port_rx_dai[0],
505 ARRAY_SIZE(msm_dai_q6_display_port_rx_dai));
506 break;
507 default:
508 dev_err(&pdev->dev, "invalid device ID %d\n", pdev->id);
509 rc = -ENODEV;
510 break;
511 }
512 return rc;
513}
514
515static int msm_dai_q6_hdmi_dev_remove(struct platform_device *pdev)
516{
517 snd_soc_unregister_component(&pdev->dev);
518 return 0;
519}
520
521static const struct of_device_id msm_dai_q6_hdmi_dt_match[] = {
522 {.compatible = "qcom,msm-dai-q6-hdmi"},
523 {}
524};
525MODULE_DEVICE_TABLE(of, msm_dai_q6_hdmi_dt_match);
526
527static struct platform_driver msm_dai_q6_hdmi_driver = {
528 .probe = msm_dai_q6_hdmi_dev_probe,
529 .remove = msm_dai_q6_hdmi_dev_remove,
530 .driver = {
531 .name = "msm-dai-q6-hdmi",
532 .owner = THIS_MODULE,
533 .of_match_table = msm_dai_q6_hdmi_dt_match,
534 },
535};
536
537static int __init msm_dai_q6_hdmi_init(void)
538{
539 return platform_driver_register(&msm_dai_q6_hdmi_driver);
540}
541module_init(msm_dai_q6_hdmi_init);
542
543static void __exit msm_dai_q6_hdmi_exit(void)
544{
545 platform_driver_unregister(&msm_dai_q6_hdmi_driver);
546}
547module_exit(msm_dai_q6_hdmi_exit);
548
549/* Module information */
550MODULE_DESCRIPTION("MSM DSP HDMI DAI driver");
551MODULE_LICENSE("GPL v2");