blob: c061c08b2d0203114688396d7f08f1b502789a6a [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>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053023#include <sound/pcm_params.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053024#include <dsp/apr_audio-v2.h>
25#include <dsp/q6afe-v2.h>
26#include "msm-dai-q6-v2.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053027
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:
Ben Romberger8e4368d2017-08-18 15:54:18 -0700220 case SNDRV_PCM_FORMAT_S24_3LE:
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530221 dai_data->port_config.hdmi_multi_ch.bit_width = 24;
222 break;
223 }
224
225 /*refer to HDMI spec CEA-861-E: Table 28 Audio InfoFrame Data Byte 4*/
226 switch (dai_data->channels) {
227 case 2:
228 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0;
229 break;
230 case 3:
231 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x02;
232 break;
233 case 4:
234 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x06;
235 break;
236 case 5:
237 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0A;
238 break;
239 case 6:
240 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x0B;
241 break;
242 case 7:
243 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x12;
244 break;
245 case 8:
246 dai_data->port_config.hdmi_multi_ch.channel_allocation = 0x13;
247 break;
248 default:
249 dev_err(dai->dev, "invalid Channels = %u\n",
250 dai_data->channels);
251 return -EINVAL;
252 }
253 dev_dbg(dai->dev, "%s() minor version: %u samplerate: %u bitwidth: %u\n"
254 "num_ch = %u channel_allocation = %u datatype = %d\n", __func__,
255 dai_data->port_config.hdmi_multi_ch.hdmi_cfg_minor_version,
256 dai_data->port_config.hdmi_multi_ch.sample_rate,
257 dai_data->port_config.hdmi_multi_ch.bit_width,
258 dai_data->channels,
259 dai_data->port_config.hdmi_multi_ch.channel_allocation,
260 dai_data->port_config.hdmi_multi_ch.datatype);
261
262 return 0;
263}
264
265
266static void msm_dai_q6_hdmi_shutdown(struct snd_pcm_substream *substream,
267 struct snd_soc_dai *dai)
268{
269 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
270 int rc = 0;
271
272 if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
273 pr_info("%s: afe port not started. dai_data->status_mask = %ld\n",
274 __func__, *dai_data->status_mask);
275 return;
276 }
277
278 rc = afe_close(dai->id); /* can block */
279 if (rc < 0)
280 dev_err(dai->dev, "fail to close AFE port\n");
281
282 pr_debug("%s: dai_data->status_mask = %ld\n", __func__,
283 *dai_data->status_mask);
284
285 clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
286}
287
288
289static int msm_dai_q6_hdmi_prepare(struct snd_pcm_substream *substream,
290 struct snd_soc_dai *dai)
291{
292 struct msm_dai_q6_hdmi_dai_data *dai_data = dev_get_drvdata(dai->dev);
293 int rc = 0;
294
295 if (dai_data->ca.set_ca)
296 dai_data->port_config.hdmi_multi_ch.channel_allocation =
297 dai_data->ca.ca;
298
299 if (!test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
300 rc = afe_port_start(dai->id, &dai_data->port_config,
301 dai_data->rate);
302 if (rc < 0)
303 dev_err(dai->dev, "fail to open AFE port %x\n",
304 dai->id);
305 else
306 set_bit(STATUS_PORT_STARTED,
307 dai_data->status_mask);
308 }
309
310 return rc;
311}
312
313static inline void msm_dai_q6_hdmi_set_dai_id(struct snd_soc_dai *dai)
314{
315 if (!dai->driver->id) {
316 dev_warn(dai->dev, "DAI driver id is not set\n");
317 return;
318 }
319 dai->id = dai->driver->id;
320}
321
322static int msm_dai_q6_hdmi_dai_probe(struct snd_soc_dai *dai)
323{
324 struct msm_dai_q6_hdmi_dai_data *dai_data;
325 const struct snd_kcontrol_new *kcontrol;
326 int rc = 0;
327 struct snd_soc_dapm_route intercon;
328 struct snd_soc_dapm_context *dapm;
329
330 if (!dai || !dai->driver) {
331 pr_err("%s: dai or dai->driver is NULL\n", __func__);
332 return -EINVAL;
333 }
334 dai_data = kzalloc(sizeof(struct msm_dai_q6_hdmi_dai_data),
335 GFP_KERNEL);
336
337 if (!dai_data) {
338 dev_err(dai->dev, "DAI-%d: fail to allocate dai data\n",
339 dai->id);
340 rc = -ENOMEM;
341 } else
342 dev_set_drvdata(dai->dev, dai_data);
343
344 msm_dai_q6_hdmi_set_dai_id(dai);
345
346 if (dai->driver->id == HDMI_RX) {
347 kcontrol = &hdmi_config_controls[0];
348 rc = snd_ctl_add(dai->component->card->snd_card,
349 snd_ctl_new1(kcontrol, dai_data));
350
351 kcontrol = &hdmi_config_controls[1];
352 rc = snd_ctl_add(dai->component->card->snd_card,
353 snd_ctl_new1(kcontrol, dai_data));
354
355 kcontrol = &hdmi_config_controls[2];
356 rc = snd_ctl_add(dai->component->card->snd_card,
357 snd_ctl_new1(kcontrol, dai));
358 } else if (dai->driver->id == DISPLAY_PORT_RX) {
359 kcontrol = &display_port_config_controls[0];
360 rc = snd_ctl_add(dai->component->card->snd_card,
361 snd_ctl_new1(kcontrol, dai_data));
362
363 kcontrol = &display_port_config_controls[1];
364 rc = snd_ctl_add(dai->component->card->snd_card,
365 snd_ctl_new1(kcontrol, dai_data));
366
367 kcontrol = &display_port_config_controls[2];
368 rc = snd_ctl_add(dai->component->card->snd_card,
369 snd_ctl_new1(kcontrol, dai));
370 } else {
371 dev_err(dai->dev, "%s: Invalid id:%d\n",
372 __func__, dai->driver->id);
373 kfree(dai_data);
374 dev_set_drvdata(dai->dev, NULL);
375 return -EINVAL;
376 }
377
378 dapm = snd_soc_component_get_dapm(dai->component);
379 memset(&intercon, 0, sizeof(intercon));
380 if (!rc) {
381 if (dai->driver->playback.stream_name &&
382 dai->driver->playback.aif_name) {
383 dev_dbg(dai->dev, "%s add route for widget %s",
384 __func__, dai->driver->playback.stream_name);
385 intercon.source = dai->driver->playback.aif_name;
386 intercon.sink = dai->driver->playback.stream_name;
387 dev_dbg(dai->dev, "%s src %s sink %s\n",
388 __func__, intercon.source, intercon.sink);
389 snd_soc_dapm_add_routes(dapm, &intercon, 1);
390 }
391 if (dai->driver->capture.stream_name &&
392 dai->driver->capture.aif_name) {
393 dev_dbg(dai->dev, "%s add route for widget %s",
394 __func__, dai->driver->capture.stream_name);
395 intercon.sink = dai->driver->capture.aif_name;
396 intercon.source = dai->driver->capture.stream_name;
397 dev_dbg(dai->dev, "%s src %s sink %s\n",
398 __func__, intercon.source, intercon.sink);
399 snd_soc_dapm_add_routes(dapm, &intercon, 1);
400 }
401 }
402 return rc;
403}
404
405static int msm_dai_q6_hdmi_dai_remove(struct snd_soc_dai *dai)
406{
407 struct msm_dai_q6_hdmi_dai_data *dai_data;
408 int rc;
409
410 dai_data = dev_get_drvdata(dai->dev);
411
412 /* If AFE port is still up, close it */
413 if (test_bit(STATUS_PORT_STARTED, dai_data->status_mask)) {
414 rc = afe_close(dai->id); /* can block */
415 if (rc < 0)
416 dev_err(dai->dev, "fail to close AFE port\n");
417
418 clear_bit(STATUS_PORT_STARTED, dai_data->status_mask);
419 }
420 kfree(dai_data);
421
422 return 0;
423}
424
425static struct snd_soc_dai_ops msm_dai_q6_hdmi_ops = {
426 .prepare = msm_dai_q6_hdmi_prepare,
427 .hw_params = msm_dai_q6_hdmi_hw_params,
428 .shutdown = msm_dai_q6_hdmi_shutdown,
429};
430
431static struct snd_soc_dai_driver msm_dai_q6_hdmi_hdmi_rx_dai = {
432 .playback = {
433 .stream_name = "HDMI Playback",
434 .aif_name = "HDMI",
435 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
436 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
437 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
438 SNDRV_PCM_RATE_192000,
Ben Romberger8e4368d2017-08-18 15:54:18 -0700439 .formats = SNDRV_PCM_FMTBIT_S16_LE |
440 SNDRV_PCM_FMTBIT_S24_LE |
441 SNDRV_PCM_FMTBIT_S24_3LE,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530442 .channels_min = 2,
443 .channels_max = 8,
444 .rate_max = 192000,
445 .rate_min = 48000,
446 },
447 .ops = &msm_dai_q6_hdmi_ops,
448 .id = HDMI_RX,
449 .probe = msm_dai_q6_hdmi_dai_probe,
450 .remove = msm_dai_q6_hdmi_dai_remove,
451};
452
453static struct snd_soc_dai_driver msm_dai_q6_display_port_rx_dai[] = {
454 {
455 .playback = {
456 .stream_name = "Display Port Playback",
457 .aif_name = "DISPLAY_PORT",
458 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
459 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
460 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
461 SNDRV_PCM_RATE_192000,
462 .formats = SNDRV_PCM_FMTBIT_S16_LE |
Ben Romberger8e4368d2017-08-18 15:54:18 -0700463 SNDRV_PCM_FMTBIT_S24_LE |
464 SNDRV_PCM_FMTBIT_S24_3LE,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530465 .channels_min = 2,
466 .channels_max = 8,
467 .rate_max = 192000,
468 .rate_min = 48000,
469 },
470 .ops = &msm_dai_q6_hdmi_ops,
471 .id = DISPLAY_PORT_RX,
472 .probe = msm_dai_q6_hdmi_dai_probe,
473 .remove = msm_dai_q6_hdmi_dai_remove,
474 },
475};
476
477static const struct snd_soc_component_driver msm_dai_hdmi_q6_component = {
478 .name = "msm-dai-q6-hdmi",
479};
480
481/* To do: change to register DAIs as batch */
482static int msm_dai_q6_hdmi_dev_probe(struct platform_device *pdev)
483{
484 int rc, id;
485 const char *q6_dev_id = "qcom,msm-dai-q6-dev-id";
486
487 rc = of_property_read_u32(pdev->dev.of_node, q6_dev_id, &id);
488 if (rc) {
489 dev_err(&pdev->dev,
490 "%s: missing %s in dt node\n", __func__, q6_dev_id);
491 return rc;
492 }
493
494 pdev->id = id;
495
496 pr_debug("%s: dev name %s, id:%d\n", __func__,
497 dev_name(&pdev->dev), pdev->id);
498
499 switch (pdev->id) {
500 case HDMI_RX:
501 rc = snd_soc_register_component(&pdev->dev,
502 &msm_dai_hdmi_q6_component,
503 &msm_dai_q6_hdmi_hdmi_rx_dai, 1);
504 break;
505 case DISPLAY_PORT_RX:
506 rc = snd_soc_register_component(&pdev->dev,
507 &msm_dai_hdmi_q6_component,
508 &msm_dai_q6_display_port_rx_dai[0],
509 ARRAY_SIZE(msm_dai_q6_display_port_rx_dai));
510 break;
511 default:
512 dev_err(&pdev->dev, "invalid device ID %d\n", pdev->id);
513 rc = -ENODEV;
514 break;
515 }
516 return rc;
517}
518
519static int msm_dai_q6_hdmi_dev_remove(struct platform_device *pdev)
520{
521 snd_soc_unregister_component(&pdev->dev);
522 return 0;
523}
524
525static const struct of_device_id msm_dai_q6_hdmi_dt_match[] = {
526 {.compatible = "qcom,msm-dai-q6-hdmi"},
527 {}
528};
529MODULE_DEVICE_TABLE(of, msm_dai_q6_hdmi_dt_match);
530
531static struct platform_driver msm_dai_q6_hdmi_driver = {
532 .probe = msm_dai_q6_hdmi_dev_probe,
533 .remove = msm_dai_q6_hdmi_dev_remove,
534 .driver = {
535 .name = "msm-dai-q6-hdmi",
536 .owner = THIS_MODULE,
537 .of_match_table = msm_dai_q6_hdmi_dt_match,
538 },
539};
540
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530541int __init msm_dai_q6_hdmi_init(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530542{
543 return platform_driver_register(&msm_dai_q6_hdmi_driver);
544}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530545
Asish Bhattacharya5faacb32017-12-04 17:23:15 +0530546void msm_dai_q6_hdmi_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530547{
548 platform_driver_unregister(&msm_dai_q6_hdmi_driver);
549}
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530550
551/* Module information */
552MODULE_DESCRIPTION("MSM DSP HDMI DAI driver");
553MODULE_LICENSE("GPL v2");