blob: 7157a8b9e52bbaea75c45992dc87f97995740b56 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. 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/debugfs.h>
14#include <linux/kernel.h>
15#include <linux/kthread.h>
16#include <linux/uaccess.h>
17#include <linux/wait.h>
18#include <linux/jiffies.h>
19#include <linux/sched.h>
20#include <sound/apr_audio.h>
21#include <sound/q6afe.h>
22
23struct afe_ctl {
24 void *apr;
25 atomic_t state;
26 atomic_t status;
27 wait_queue_head_t wait;
28 struct task_struct *task;
29};
30
31static struct afe_ctl this_afe;
32
33#define TIMEOUT_MS 1000
34#define Q6AFE_MAX_VOLUME 0x3FFF
35
36#define SIZEOF_CFG_CMD(y) \
37 (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
38
39static int32_t afe_callback(struct apr_client_data *data, void *priv)
40{
41 if (data->opcode == RESET_EVENTS) {
42 pr_debug("q6afe: reset event = %d %d apr[%p]\n",
43 data->reset_event, data->reset_proc, this_afe.apr);
44 if (this_afe.apr) {
45 apr_reset(this_afe.apr);
46 atomic_set(&this_afe.state, 0);
47 this_afe.apr = NULL;
48 }
49 /* send info to user */
50 pr_debug("task_name = %s pid = %d\n",
51 this_afe.task->comm, this_afe.task->pid);
52 send_sig(SIGUSR1, this_afe.task, 0);
53 }
54 if (data->payload_size) {
55 uint32_t *payload;
56 payload = data->payload;
57 pr_debug("%s: cmd = 0x%x status = 0x%x\n", __func__,
58 payload[0], payload[1]);
59 /* payload[1] contains the error status for response */
60 if (payload[1] != 0) {
61 atomic_set(&this_afe.status, -1);
62 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
63 __func__, payload[0], payload[1]);
64 }
65 if (data->opcode == APR_BASIC_RSP_RESULT) {
66 switch (payload[0]) {
67 case AFE_PORT_AUDIO_IF_CONFIG:
68 case AFE_PORT_CMD_STOP:
69 case AFE_PORT_CMD_START:
70 case AFE_PORT_CMD_LOOPBACK:
71 case AFE_PORT_CMD_SIDETONE_CTL:
72 case AFE_PORT_CMD_SET_PARAM:
73 case AFE_PSEUDOPORT_CMD_START:
74 case AFE_PSEUDOPORT_CMD_STOP:
Laxminath Kasam885f5102011-07-14 10:20:21 +053075 case AFE_PORT_CMD_APPLY_GAIN:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076 atomic_set(&this_afe.state, 0);
77 wake_up(&this_afe.wait);
78 break;
79 default:
80 pr_err("Unknown cmd 0x%x\n",
81 payload[0]);
82 break;
83 }
84 }
85 }
86 return 0;
87}
88
89int afe_validate_port(u16 port_id)
90{
91 int ret;
92
93 switch (port_id) {
94 case PRIMARY_I2S_RX:
95 case PRIMARY_I2S_TX:
96 case PCM_RX:
97 case PCM_TX:
98 case SECONDARY_I2S_RX:
99 case SECONDARY_I2S_TX:
100 case MI2S_RX:
101 case MI2S_TX:
102 case HDMI_RX:
103 case RSVD_2:
104 case RSVD_3:
105 case DIGI_MIC_TX:
106 case VOICE_RECORD_RX:
107 case VOICE_RECORD_TX:
108 case VOICE_PLAYBACK_TX:
109 case SLIMBUS_0_RX:
110 case SLIMBUS_0_TX:
111 case INT_BT_SCO_RX:
112 case INT_BT_SCO_TX:
113 case INT_BT_A2DP_RX:
114 case INT_FM_RX:
115 case INT_FM_TX:
116 {
117 ret = 0;
118 break;
119 }
120
121 default:
122 ret = -EINVAL;
123 }
124
125 return ret;
126}
127
128int afe_get_port_index(u16 port_id)
129{
130 switch (port_id) {
131 case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
132 case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
133 case PCM_RX: return IDX_PCM_RX;
134 case PCM_TX: return IDX_PCM_TX;
135 case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
136 case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
137 case MI2S_RX: return IDX_MI2S_RX;
138 case MI2S_TX: return IDX_MI2S_TX;
139 case HDMI_RX: return IDX_HDMI_RX;
140 case RSVD_2: return IDX_RSVD_2;
141 case RSVD_3: return IDX_RSVD_3;
142 case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
143 case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
144 case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
145 case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
146 case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
147 case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
148 case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
149 case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
150 case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
151 case INT_FM_RX: return IDX_INT_FM_RX;
152 case INT_FM_TX: return IDX_INT_FM_TX;
153
154 default: return -EINVAL;
155 }
156}
157
158int afe_sizeof_cfg_cmd(u16 port_id)
159{
160 int ret_size;
161 switch (port_id) {
162 case PRIMARY_I2S_RX:
163 case PRIMARY_I2S_TX:
164 case SECONDARY_I2S_RX:
165 case SECONDARY_I2S_TX:
166 case MI2S_RX:
167 case MI2S_TX:
168 ret_size = SIZEOF_CFG_CMD(afe_port_mi2s_cfg);
169 break;
170 case HDMI_RX:
171 ret_size = SIZEOF_CFG_CMD(afe_port_hdmi_cfg);
172 break;
173 case SLIMBUS_0_RX:
174 case SLIMBUS_0_TX:
175 ret_size = SIZEOF_CFG_CMD(afe_port_slimbus_cfg);
176 break;
177 case PCM_RX:
178 case PCM_TX:
179 default:
180 ret_size = SIZEOF_CFG_CMD(afe_port_pcm_cfg);
181 break;
182 }
183 return ret_size;
184}
185
186int afe_port_start_nowait(u16 port_id, union afe_port_config *afe_config,
187 u32 rate) /* This function is no blocking */
188{
189 struct afe_port_start_command start;
190 struct afe_audioif_config_command config;
191 int ret;
192
193 if (!afe_config) {
194 pr_err("%s: Error, no configuration data\n", __func__);
195 ret = -EINVAL;
196 return ret;
197 }
198
199 pr_info("%s: %d %d\n", __func__, port_id, rate);
200
201 if (this_afe.apr == NULL) {
202 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
203 0xFFFFFFFF, &this_afe);
204 pr_info("%s: Register AFE\n", __func__);
205 if (this_afe.apr == NULL) {
206 pr_err("%s: Unable to register AFE\n", __func__);
207 ret = -ENODEV;
208 return ret;
209 }
210 }
211
212 config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
213 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
214 config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
215 config.hdr.src_port = 0;
216 config.hdr.dest_port = 0;
217 config.hdr.token = 0;
218 config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
219
220 if (afe_validate_port(port_id) < 0) {
221
222 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
223 port_id);
224 ret = -EINVAL;
225 goto fail_cmd;
226 }
227
228 config.port_id = port_id;
229 config.port = *afe_config;
230
231 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
232 if (ret < 0) {
233 pr_err("%s: AFE enable for port %d failed\n", __func__,
234 port_id);
235 ret = -EINVAL;
236 goto fail_cmd;
237 }
238
239 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
240 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
241 start.hdr.pkt_size = sizeof(start);
242 start.hdr.src_port = 0;
243 start.hdr.dest_port = 0;
244 start.hdr.token = 0;
245 start.hdr.opcode = AFE_PORT_CMD_START;
246 start.port_id = port_id;
247 start.gain = 0x2000;
248 start.sample_rate = rate;
249
250 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
251
252 if (IS_ERR_VALUE(ret)) {
253 pr_err("%s: AFE enable for port %d failed\n", __func__,
254 port_id);
255 ret = -EINVAL;
256 goto fail_cmd;
257 }
258
259 if (this_afe.task != current)
260 this_afe.task = current;
261
262 pr_debug("task_name = %s pid = %d\n",
263 this_afe.task->comm, this_afe.task->pid);
264 return 0;
265
266fail_cmd:
267 return ret;
268}
269
270int afe_open(u16 port_id, union afe_port_config *afe_config, int rate)
271{
272 struct afe_port_start_command start;
273 struct afe_audioif_config_command config;
274 int ret = 0;
275
276 if (!afe_config) {
277 pr_err("%s: Error, no configuration data\n", __func__);
278 ret = -EINVAL;
279 return ret;
280 }
281
282 pr_info("%s: %d %d\n", __func__, port_id, rate);
283
284 if (this_afe.apr == NULL) {
285 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
286 0xFFFFFFFF, &this_afe);
287 pr_info("%s: Register AFE\n", __func__);
288 if (this_afe.apr == NULL) {
289 pr_err("%s: Unable to register AFE\n", __func__);
290 ret = -ENODEV;
291 return ret;
292 }
293 }
294
295 config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
296 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
297 config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
298 config.hdr.src_port = 0;
299 config.hdr.dest_port = 0;
300 config.hdr.token = 0;
301 config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
302
303 if (afe_validate_port(port_id) < 0) {
304
305 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
306 port_id);
307 ret = -EINVAL;
308 goto fail_cmd;
309 }
310
311 config.port_id = port_id;
312 config.port = *afe_config;
313
314 atomic_set(&this_afe.state, 1);
315 atomic_set(&this_afe.status, 0);
316 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
317 if (ret < 0) {
318 pr_err("%s: AFE enable for port %d failed\n", __func__,
319 port_id);
320 ret = -EINVAL;
321 goto fail_cmd;
322 }
323
324 ret = wait_event_timeout(this_afe.wait,
325 (atomic_read(&this_afe.state) == 0),
326 msecs_to_jiffies(TIMEOUT_MS));
327 if (!ret) {
328 pr_err("%s: wait_event timeout\n", __func__);
329 ret = -EINVAL;
330 goto fail_cmd;
331 }
332 if (atomic_read(&this_afe.status) != 0) {
333 pr_err("%s: config cmd failed\n", __func__);
334 ret = -EINVAL;
335 goto fail_cmd;
336 }
337 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
338 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
339 start.hdr.pkt_size = sizeof(start);
340 start.hdr.src_port = 0;
341 start.hdr.dest_port = 0;
342 start.hdr.token = 0;
343 start.hdr.opcode = AFE_PORT_CMD_START;
344 start.port_id = port_id;
345 start.gain = 0x2000;
346 start.sample_rate = rate;
347
348 atomic_set(&this_afe.state, 1);
349 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
350 if (ret < 0) {
351 pr_err("%s: AFE enable for port %d failed\n", __func__,
352 port_id);
353 ret = -EINVAL;
354 goto fail_cmd;
355 }
356 ret = wait_event_timeout(this_afe.wait,
357 (atomic_read(&this_afe.state) == 0),
358 msecs_to_jiffies(TIMEOUT_MS));
359 if (!ret) {
360 pr_err("%s: wait_event timeout\n", __func__);
361 ret = -EINVAL;
362 goto fail_cmd;
363 }
364
365 if (this_afe.task != current)
366 this_afe.task = current;
367
368 pr_debug("task_name = %s pid = %d\n",
369 this_afe.task->comm, this_afe.task->pid);
370 return 0;
371fail_cmd:
372 return ret;
373}
374
375int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
376{
377 struct afe_loopback_command lb_cmd;
378 int ret = 0;
379 if (this_afe.apr == NULL) {
380 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
381 0xFFFFFFFF, &this_afe);
382 pr_info("%s: Register AFE\n", __func__);
383 if (this_afe.apr == NULL) {
384 pr_err("%s: Unable to register AFE\n", __func__);
385 ret = -ENODEV;
386 return ret;
387 }
388 }
389 lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
390 APR_HDR_LEN(20), APR_PKT_VER);
391 lb_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
392 sizeof(lb_cmd) - APR_HDR_SIZE);
393 lb_cmd.hdr.src_port = 0;
394 lb_cmd.hdr.dest_port = 0;
395 lb_cmd.hdr.token = 0;
396 lb_cmd.hdr.opcode = AFE_PORT_CMD_LOOPBACK;
397 lb_cmd.tx_port_id = tx_port;
398 lb_cmd.rx_port_id = rx_port;
399 lb_cmd.mode = 0xFFFF;
400 lb_cmd.enable = (enable ? 1 : 0);
401 atomic_set(&this_afe.state, 1);
402
403 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &lb_cmd);
404 if (ret < 0) {
405 pr_err("%s: AFE loopback failed\n", __func__);
406 ret = -EINVAL;
407 goto done;
408 }
409 ret = wait_event_timeout(this_afe.wait,
410 (atomic_read(&this_afe.state) == 0),
411 msecs_to_jiffies(TIMEOUT_MS));
412 if (!ret) {
413 pr_err("%s: wait_event timeout\n", __func__);
414 ret = -EINVAL;
415 }
416done:
417 return ret;
418}
419
420
421int afe_loopback_gain(u16 port_id, u16 volume)
422{
423 struct afe_port_cmd_set_param set_param;
424 int ret = 0;
425
426 if (this_afe.apr == NULL) {
Jayasena Sangaraboina82435032011-07-26 15:23:00 -0700427 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
428 0xFFFFFFFF, &this_afe);
429 pr_debug("%s: Register AFE\n", __func__);
430 if (this_afe.apr == NULL) {
431 pr_err("%s: Unable to register AFE\n", __func__);
432 ret = -ENODEV;
433 return ret;
434 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 }
436
437 if (afe_validate_port(port_id) < 0) {
438
439 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
440 port_id);
441 ret = -EINVAL;
442 goto fail_cmd;
443 }
444
445 /* RX ports numbers are even .TX ports numbers are odd. */
446 if (port_id % 2 == 0) {
447 pr_err("%s: Failed : afe loopback gain only for TX ports."
448 " port_id %d\n", __func__, port_id);
449 ret = -EINVAL;
450 goto fail_cmd;
451 }
452
453 pr_debug("%s: %d %hX\n", __func__, port_id, volume);
454
455 set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
456 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
457 set_param.hdr.pkt_size = sizeof(set_param);
458 set_param.hdr.src_port = 0;
459 set_param.hdr.dest_port = 0;
460 set_param.hdr.token = 0;
461 set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
462
463 set_param.port_id = port_id;
464 set_param.payload_size = sizeof(struct afe_param_payload);
465 set_param.payload_address = 0;
466
467 set_param.payload.module_id = AFE_MODULE_ID_PORT_INFO;
468 set_param.payload.param_id = AFE_PARAM_ID_LOOPBACK_GAIN;
469 set_param.payload.param_size = sizeof(struct afe_param_loopback_gain);
470 set_param.payload.reserved = 0;
471
472 set_param.payload.param.loopback_gain.gain = volume;
473 set_param.payload.param.loopback_gain.reserved = 0;
474
475 atomic_set(&this_afe.state, 1);
476 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_param);
477 if (ret < 0) {
478 pr_err("%s: AFE param set failed for port %d\n",
479 __func__, port_id);
480 ret = -EINVAL;
481 goto fail_cmd;
482 }
483
484 ret = wait_event_timeout(this_afe.wait,
485 (atomic_read(&this_afe.state) == 0),
486 msecs_to_jiffies(TIMEOUT_MS));
487 if (ret < 0) {
488 pr_err("%s: wait_event timeout\n", __func__);
489 ret = -EINVAL;
490 goto fail_cmd;
491 }
492 return 0;
493fail_cmd:
494 return ret;
495}
496
Laxminath Kasam885f5102011-07-14 10:20:21 +0530497int afe_apply_gain(u16 port_id, u16 gain)
498{
499 struct afe_port_gain_command set_gain;
500 int ret = 0;
501
502 if (this_afe.apr == NULL) {
503 pr_err("%s: AFE is not opened\n", __func__);
504 ret = -EPERM;
505 goto fail_cmd;
506 }
507
508 if (afe_validate_port(port_id) < 0) {
509 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
510 port_id);
511 ret = -EINVAL;
512 goto fail_cmd;
513 }
514
515 /* RX ports numbers are even .TX ports numbers are odd. */
516 if (port_id % 2 == 0) {
517 pr_err("%s: Failed : afe apply gain only for TX ports."
518 " port_id %d\n", __func__, port_id);
519 ret = -EINVAL;
520 goto fail_cmd;
521 }
522
523 pr_debug("%s: %d %hX\n", __func__, port_id, gain);
524
525 set_gain.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
526 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
527 set_gain.hdr.pkt_size = sizeof(set_gain);
528 set_gain.hdr.src_port = 0;
529 set_gain.hdr.dest_port = 0;
530 set_gain.hdr.token = 0;
531 set_gain.hdr.opcode = AFE_PORT_CMD_APPLY_GAIN;
532
533 set_gain.port_id = port_id;
534 set_gain.gain = gain;
535
536 atomic_set(&this_afe.state, 1);
537 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_gain);
538 if (ret < 0) {
539 pr_err("%s: AFE Gain set failed for port %d\n",
540 __func__, port_id);
541 ret = -EINVAL;
542 goto fail_cmd;
543 }
544
545 ret = wait_event_timeout(this_afe.wait,
546 (atomic_read(&this_afe.state) == 0),
547 msecs_to_jiffies(TIMEOUT_MS));
548 if (ret < 0) {
549 pr_err("%s: wait_event timeout\n", __func__);
550 ret = -EINVAL;
551 goto fail_cmd;
552 }
553 return 0;
554fail_cmd:
555 return ret;
556}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557int afe_start_pseudo_port(u16 port_id)
558{
559 int ret = 0;
560 struct afe_pseudoport_start_command start;
561
562 pr_info("%s: port_id=%d\n", __func__, port_id);
563
564 if (this_afe.apr == NULL) {
565 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
566 0xFFFFFFFF, &this_afe);
567 pr_info("%s: Register AFE\n", __func__);
568 if (this_afe.apr == NULL) {
569 pr_err("%s: Unable to register AFE\n", __func__);
570 ret = -ENODEV;
571 return ret;
572 }
573 }
574
575 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
576 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
577 start.hdr.pkt_size = sizeof(start);
578 start.hdr.src_port = 0;
579 start.hdr.dest_port = 0;
580 start.hdr.token = 0;
581 start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
582 start.port_id = port_id;
583 start.timing = 1;
584
585 atomic_set(&this_afe.state, 1);
586 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
587 if (ret < 0) {
588 pr_err("%s: AFE enable for port %d failed %d\n",
589 __func__, port_id, ret);
590 ret = -EINVAL;
591 return ret;
592 }
593
594 ret = wait_event_timeout(this_afe.wait,
595 (atomic_read(&this_afe.state) == 0),
596 msecs_to_jiffies(TIMEOUT_MS));
597 if (!ret) {
598 pr_err("%s: wait_event timeout\n", __func__);
599 ret = -EINVAL;
600 return ret;
601 }
602
603 return 0;
604}
605
606int afe_stop_pseudo_port(u16 port_id)
607{
608 int ret = 0;
609 struct afe_pseudoport_stop_command stop;
610
611 pr_info("%s: port_id=%d\n", __func__, port_id);
612
613 if (this_afe.apr == NULL) {
614 pr_err("%s: AFE is already closed\n", __func__);
615 ret = -EINVAL;
616 return ret;
617 }
618
619 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
620 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
621 stop.hdr.pkt_size = sizeof(stop);
622 stop.hdr.src_port = 0;
623 stop.hdr.dest_port = 0;
624 stop.hdr.token = 0;
625 stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
626 stop.port_id = port_id;
627 stop.reserved = 0;
628
629 atomic_set(&this_afe.state, 1);
630 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
631 if (ret < 0) {
632 pr_err("%s: AFE close failed %d\n", __func__, ret);
633 ret = -EINVAL;
634 return ret;
635 }
636
637 ret = wait_event_timeout(this_afe.wait,
638 (atomic_read(&this_afe.state) == 0),
639 msecs_to_jiffies(TIMEOUT_MS));
640 if (!ret) {
641 pr_err("%s: wait_event timeout\n", __func__);
642 ret = -EINVAL;
643 return ret;
644 }
645
646 return 0;
647}
648
649#ifdef CONFIG_DEBUG_FS
650static struct dentry *debugfs_afelb;
651static struct dentry *debugfs_afelb_gain;
652
653static int afe_debug_open(struct inode *inode, struct file *file)
654{
655 file->private_data = inode->i_private;
656 pr_info("debug intf %s\n", (char *) file->private_data);
657 return 0;
658}
659
660static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
661{
662 char *token;
663 int base, cnt;
664
665 token = strsep(&buf, " ");
666
667 for (cnt = 0; cnt < num_of_par; cnt++) {
668 if (token != NULL) {
669 if ((token[1] == 'x') || (token[1] == 'X'))
670 base = 16;
671 else
672 base = 10;
673
674 if (strict_strtoul(token, base, &param1[cnt]) != 0)
675 return -EINVAL;
676
677 token = strsep(&buf, " ");
678 } else
679 return -EINVAL;
680 }
681 return 0;
682}
683#define AFE_LOOPBACK_ON (1)
684#define AFE_LOOPBACK_OFF (0)
685static ssize_t afe_debug_write(struct file *filp,
686 const char __user *ubuf, size_t cnt, loff_t *ppos)
687{
688 char *lb_str = filp->private_data;
689 char lbuf[32];
690 int rc;
691 unsigned long param[5];
692
693 if (cnt > sizeof(lbuf) - 1)
694 return -EINVAL;
695
696 rc = copy_from_user(lbuf, ubuf, cnt);
697 if (rc)
698 return -EFAULT;
699
700 lbuf[cnt] = '\0';
701
702 if (!strcmp(lb_str, "afe_loopback")) {
703 rc = afe_get_parameters(lbuf, param, 3);
704 if (!rc) {
705 pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
706 param[2]);
707
708 if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
709 AFE_LOOPBACK_OFF)) {
710 pr_err("%s: Error, parameter 0 incorrect\n",
711 __func__);
712 rc = -EINVAL;
713 goto afe_error;
714 }
715 if ((afe_validate_port(param[1]) < 0) ||
716 (afe_validate_port(param[2])) < 0) {
717 pr_err("%s: Error, invalid afe port\n",
718 __func__);
719 }
720 if (this_afe.apr == NULL) {
721 pr_err("%s: Error, AFE not opened\n", __func__);
722 rc = -EINVAL;
723 } else {
724 rc = afe_loopback(param[0], param[1], param[2]);
725 }
726 } else {
727 pr_err("%s: Error, invalid parameters\n", __func__);
728 rc = -EINVAL;
729 }
730
731 } else if (!strcmp(lb_str, "afe_loopback_gain")) {
732 rc = afe_get_parameters(lbuf, param, 2);
733 if (!rc) {
734 pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
735
736 if (afe_validate_port(param[0]) < 0) {
737 pr_err("%s: Error, invalid afe port\n",
738 __func__);
739 rc = -EINVAL;
740 goto afe_error;
741 }
742
743 if (param[1] < 0 || param[1] > 100) {
744 pr_err("%s: Error, volume shoud be 0 to 100"
745 " percentage param = %lu\n",
746 __func__, param[1]);
747 rc = -EINVAL;
748 goto afe_error;
749 }
750
751 param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
752
753 if (this_afe.apr == NULL) {
754 pr_err("%s: Error, AFE not opened\n", __func__);
755 rc = -EINVAL;
756 } else {
757 rc = afe_loopback_gain(param[0], param[1]);
758 }
759 } else {
760 pr_err("%s: Error, invalid parameters\n", __func__);
761 rc = -EINVAL;
762 }
763 }
764
765afe_error:
766 if (rc == 0)
767 rc = cnt;
768 else
769 pr_err("%s: rc = %d\n", __func__, rc);
770
771 return rc;
772}
773
774static const struct file_operations afe_debug_fops = {
775 .open = afe_debug_open,
776 .write = afe_debug_write
777};
778#endif
779int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
780{
781 struct afe_port_sidetone_command cmd_sidetone;
782 int ret = 0;
783
784 pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
785 tx_port_id, rx_port_id, enable, gain);
786 cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
787 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
788 cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
789 cmd_sidetone.hdr.src_port = 0;
790 cmd_sidetone.hdr.dest_port = 0;
791 cmd_sidetone.hdr.token = 0;
792 cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL;
793 cmd_sidetone.tx_port_id = tx_port_id;
794 cmd_sidetone.rx_port_id = rx_port_id;
795 cmd_sidetone.gain = gain;
796 cmd_sidetone.enable = enable;
797
798 atomic_set(&this_afe.state, 1);
799 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone);
800 if (ret < 0) {
801 pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
802 __func__, tx_port_id, rx_port_id);
803 ret = -EINVAL;
804 goto fail_cmd;
805 }
806
807 ret = wait_event_timeout(this_afe.wait,
808 (atomic_read(&this_afe.state) == 0),
809 msecs_to_jiffies(TIMEOUT_MS));
810 if (ret < 0) {
811 pr_err("%s: wait_event timeout\n", __func__);
812 ret = -EINVAL;
813 goto fail_cmd;
814 }
815 return 0;
816fail_cmd:
817 return ret;
818}
819
820int afe_port_stop_nowait(int port_id)
821{
822 struct afe_port_stop_command stop;
823 int ret = 0;
824
825 if (this_afe.apr == NULL) {
826 pr_err("AFE is already closed\n");
827 ret = -EINVAL;
828 goto fail_cmd;
829 }
830 pr_info("%s: port_id=%d\n", __func__, port_id);
831 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
832 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
833 stop.hdr.pkt_size = sizeof(stop);
834 stop.hdr.src_port = 0;
835 stop.hdr.dest_port = 0;
836 stop.hdr.token = 0;
837 stop.hdr.opcode = AFE_PORT_CMD_STOP;
838 stop.port_id = port_id;
839 stop.reserved = 0;
840
841 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
842
843 if (ret == -ENETRESET) {
844 pr_info("%s: Need to reset, calling APR deregister", __func__);
845 return apr_deregister(this_afe.apr);
846 } else if (IS_ERR_VALUE(ret)) {
847 pr_err("%s: AFE close failed\n", __func__);
848 ret = -EINVAL;
849 }
850
851fail_cmd:
852 return ret;
853
854}
855
856int afe_close(int port_id)
857{
858 struct afe_port_stop_command stop;
859 int ret = 0;
860
861 if (this_afe.apr == NULL) {
862 pr_err("AFE is already closed\n");
863 ret = -EINVAL;
864 goto fail_cmd;
865 }
866 pr_info("%s: port_id=%d\n", __func__, port_id);
867 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
868 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
869 stop.hdr.pkt_size = sizeof(stop);
870 stop.hdr.src_port = 0;
871 stop.hdr.dest_port = 0;
872 stop.hdr.token = 0;
873 stop.hdr.opcode = AFE_PORT_CMD_STOP;
874 stop.port_id = port_id;
875 stop.reserved = 0;
876
877 atomic_set(&this_afe.state, 1);
878 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
879
880 if (ret == -ENETRESET) {
881 pr_info("%s: Need to reset, calling APR deregister", __func__);
882 return apr_deregister(this_afe.apr);
883 }
884
885 if (ret < 0) {
886 pr_err("%s: AFE close failed\n", __func__);
887 ret = -EINVAL;
888 goto fail_cmd;
889 }
890
891 ret = wait_event_timeout(this_afe.wait,
892 (atomic_read(&this_afe.state) == 0),
893 msecs_to_jiffies(TIMEOUT_MS));
894 if (!ret) {
895 pr_err("%s: wait_event timeout\n", __func__);
896 ret = -EINVAL;
897 goto fail_cmd;
898 }
899fail_cmd:
900 return ret;
901}
902
903static int __init afe_init(void)
904{
905 init_waitqueue_head(&this_afe.wait);
906 atomic_set(&this_afe.state, 0);
907 atomic_set(&this_afe.status, 0);
908 this_afe.apr = NULL;
909#ifdef CONFIG_DEBUG_FS
910 debugfs_afelb = debugfs_create_file("afe_loopback",
911 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback",
912 &afe_debug_fops);
913
914 debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
915 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain",
916 &afe_debug_fops);
917
918
919#endif
920 return 0;
921}
922
923static void __exit afe_exit(void)
924{
925#ifdef CONFIG_DEBUG_FS
926 if (debugfs_afelb)
927 debugfs_remove(debugfs_afelb);
928 if (debugfs_afelb_gain)
929 debugfs_remove(debugfs_afelb_gain);
930#endif
931}
932
933device_initcall(afe_init);
934__exitcall(afe_exit);