blob: 5c47159e8df749470b282d3a738f4e04c9375114 [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) {
427 pr_err("%s: AFE is not opened\n", __func__);
428 ret = -EPERM;
429 goto fail_cmd;
430 }
431
432 if (afe_validate_port(port_id) < 0) {
433
434 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
435 port_id);
436 ret = -EINVAL;
437 goto fail_cmd;
438 }
439
440 /* RX ports numbers are even .TX ports numbers are odd. */
441 if (port_id % 2 == 0) {
442 pr_err("%s: Failed : afe loopback gain only for TX ports."
443 " port_id %d\n", __func__, port_id);
444 ret = -EINVAL;
445 goto fail_cmd;
446 }
447
448 pr_debug("%s: %d %hX\n", __func__, port_id, volume);
449
450 set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
451 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
452 set_param.hdr.pkt_size = sizeof(set_param);
453 set_param.hdr.src_port = 0;
454 set_param.hdr.dest_port = 0;
455 set_param.hdr.token = 0;
456 set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
457
458 set_param.port_id = port_id;
459 set_param.payload_size = sizeof(struct afe_param_payload);
460 set_param.payload_address = 0;
461
462 set_param.payload.module_id = AFE_MODULE_ID_PORT_INFO;
463 set_param.payload.param_id = AFE_PARAM_ID_LOOPBACK_GAIN;
464 set_param.payload.param_size = sizeof(struct afe_param_loopback_gain);
465 set_param.payload.reserved = 0;
466
467 set_param.payload.param.loopback_gain.gain = volume;
468 set_param.payload.param.loopback_gain.reserved = 0;
469
470 atomic_set(&this_afe.state, 1);
471 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_param);
472 if (ret < 0) {
473 pr_err("%s: AFE param set failed for port %d\n",
474 __func__, port_id);
475 ret = -EINVAL;
476 goto fail_cmd;
477 }
478
479 ret = wait_event_timeout(this_afe.wait,
480 (atomic_read(&this_afe.state) == 0),
481 msecs_to_jiffies(TIMEOUT_MS));
482 if (ret < 0) {
483 pr_err("%s: wait_event timeout\n", __func__);
484 ret = -EINVAL;
485 goto fail_cmd;
486 }
487 return 0;
488fail_cmd:
489 return ret;
490}
491
Laxminath Kasam885f5102011-07-14 10:20:21 +0530492int afe_apply_gain(u16 port_id, u16 gain)
493{
494 struct afe_port_gain_command set_gain;
495 int ret = 0;
496
497 if (this_afe.apr == NULL) {
498 pr_err("%s: AFE is not opened\n", __func__);
499 ret = -EPERM;
500 goto fail_cmd;
501 }
502
503 if (afe_validate_port(port_id) < 0) {
504 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
505 port_id);
506 ret = -EINVAL;
507 goto fail_cmd;
508 }
509
510 /* RX ports numbers are even .TX ports numbers are odd. */
511 if (port_id % 2 == 0) {
512 pr_err("%s: Failed : afe apply gain only for TX ports."
513 " port_id %d\n", __func__, port_id);
514 ret = -EINVAL;
515 goto fail_cmd;
516 }
517
518 pr_debug("%s: %d %hX\n", __func__, port_id, gain);
519
520 set_gain.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
521 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
522 set_gain.hdr.pkt_size = sizeof(set_gain);
523 set_gain.hdr.src_port = 0;
524 set_gain.hdr.dest_port = 0;
525 set_gain.hdr.token = 0;
526 set_gain.hdr.opcode = AFE_PORT_CMD_APPLY_GAIN;
527
528 set_gain.port_id = port_id;
529 set_gain.gain = gain;
530
531 atomic_set(&this_afe.state, 1);
532 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_gain);
533 if (ret < 0) {
534 pr_err("%s: AFE Gain set failed for port %d\n",
535 __func__, port_id);
536 ret = -EINVAL;
537 goto fail_cmd;
538 }
539
540 ret = wait_event_timeout(this_afe.wait,
541 (atomic_read(&this_afe.state) == 0),
542 msecs_to_jiffies(TIMEOUT_MS));
543 if (ret < 0) {
544 pr_err("%s: wait_event timeout\n", __func__);
545 ret = -EINVAL;
546 goto fail_cmd;
547 }
548 return 0;
549fail_cmd:
550 return ret;
551}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552int afe_start_pseudo_port(u16 port_id)
553{
554 int ret = 0;
555 struct afe_pseudoport_start_command start;
556
557 pr_info("%s: port_id=%d\n", __func__, port_id);
558
559 if (this_afe.apr == NULL) {
560 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
561 0xFFFFFFFF, &this_afe);
562 pr_info("%s: Register AFE\n", __func__);
563 if (this_afe.apr == NULL) {
564 pr_err("%s: Unable to register AFE\n", __func__);
565 ret = -ENODEV;
566 return ret;
567 }
568 }
569
570 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
571 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
572 start.hdr.pkt_size = sizeof(start);
573 start.hdr.src_port = 0;
574 start.hdr.dest_port = 0;
575 start.hdr.token = 0;
576 start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
577 start.port_id = port_id;
578 start.timing = 1;
579
580 atomic_set(&this_afe.state, 1);
581 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
582 if (ret < 0) {
583 pr_err("%s: AFE enable for port %d failed %d\n",
584 __func__, port_id, ret);
585 ret = -EINVAL;
586 return ret;
587 }
588
589 ret = wait_event_timeout(this_afe.wait,
590 (atomic_read(&this_afe.state) == 0),
591 msecs_to_jiffies(TIMEOUT_MS));
592 if (!ret) {
593 pr_err("%s: wait_event timeout\n", __func__);
594 ret = -EINVAL;
595 return ret;
596 }
597
598 return 0;
599}
600
601int afe_stop_pseudo_port(u16 port_id)
602{
603 int ret = 0;
604 struct afe_pseudoport_stop_command stop;
605
606 pr_info("%s: port_id=%d\n", __func__, port_id);
607
608 if (this_afe.apr == NULL) {
609 pr_err("%s: AFE is already closed\n", __func__);
610 ret = -EINVAL;
611 return ret;
612 }
613
614 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
615 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
616 stop.hdr.pkt_size = sizeof(stop);
617 stop.hdr.src_port = 0;
618 stop.hdr.dest_port = 0;
619 stop.hdr.token = 0;
620 stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
621 stop.port_id = port_id;
622 stop.reserved = 0;
623
624 atomic_set(&this_afe.state, 1);
625 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
626 if (ret < 0) {
627 pr_err("%s: AFE close failed %d\n", __func__, ret);
628 ret = -EINVAL;
629 return ret;
630 }
631
632 ret = wait_event_timeout(this_afe.wait,
633 (atomic_read(&this_afe.state) == 0),
634 msecs_to_jiffies(TIMEOUT_MS));
635 if (!ret) {
636 pr_err("%s: wait_event timeout\n", __func__);
637 ret = -EINVAL;
638 return ret;
639 }
640
641 return 0;
642}
643
644#ifdef CONFIG_DEBUG_FS
645static struct dentry *debugfs_afelb;
646static struct dentry *debugfs_afelb_gain;
647
648static int afe_debug_open(struct inode *inode, struct file *file)
649{
650 file->private_data = inode->i_private;
651 pr_info("debug intf %s\n", (char *) file->private_data);
652 return 0;
653}
654
655static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
656{
657 char *token;
658 int base, cnt;
659
660 token = strsep(&buf, " ");
661
662 for (cnt = 0; cnt < num_of_par; cnt++) {
663 if (token != NULL) {
664 if ((token[1] == 'x') || (token[1] == 'X'))
665 base = 16;
666 else
667 base = 10;
668
669 if (strict_strtoul(token, base, &param1[cnt]) != 0)
670 return -EINVAL;
671
672 token = strsep(&buf, " ");
673 } else
674 return -EINVAL;
675 }
676 return 0;
677}
678#define AFE_LOOPBACK_ON (1)
679#define AFE_LOOPBACK_OFF (0)
680static ssize_t afe_debug_write(struct file *filp,
681 const char __user *ubuf, size_t cnt, loff_t *ppos)
682{
683 char *lb_str = filp->private_data;
684 char lbuf[32];
685 int rc;
686 unsigned long param[5];
687
688 if (cnt > sizeof(lbuf) - 1)
689 return -EINVAL;
690
691 rc = copy_from_user(lbuf, ubuf, cnt);
692 if (rc)
693 return -EFAULT;
694
695 lbuf[cnt] = '\0';
696
697 if (!strcmp(lb_str, "afe_loopback")) {
698 rc = afe_get_parameters(lbuf, param, 3);
699 if (!rc) {
700 pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
701 param[2]);
702
703 if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
704 AFE_LOOPBACK_OFF)) {
705 pr_err("%s: Error, parameter 0 incorrect\n",
706 __func__);
707 rc = -EINVAL;
708 goto afe_error;
709 }
710 if ((afe_validate_port(param[1]) < 0) ||
711 (afe_validate_port(param[2])) < 0) {
712 pr_err("%s: Error, invalid afe port\n",
713 __func__);
714 }
715 if (this_afe.apr == NULL) {
716 pr_err("%s: Error, AFE not opened\n", __func__);
717 rc = -EINVAL;
718 } else {
719 rc = afe_loopback(param[0], param[1], param[2]);
720 }
721 } else {
722 pr_err("%s: Error, invalid parameters\n", __func__);
723 rc = -EINVAL;
724 }
725
726 } else if (!strcmp(lb_str, "afe_loopback_gain")) {
727 rc = afe_get_parameters(lbuf, param, 2);
728 if (!rc) {
729 pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
730
731 if (afe_validate_port(param[0]) < 0) {
732 pr_err("%s: Error, invalid afe port\n",
733 __func__);
734 rc = -EINVAL;
735 goto afe_error;
736 }
737
738 if (param[1] < 0 || param[1] > 100) {
739 pr_err("%s: Error, volume shoud be 0 to 100"
740 " percentage param = %lu\n",
741 __func__, param[1]);
742 rc = -EINVAL;
743 goto afe_error;
744 }
745
746 param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
747
748 if (this_afe.apr == NULL) {
749 pr_err("%s: Error, AFE not opened\n", __func__);
750 rc = -EINVAL;
751 } else {
752 rc = afe_loopback_gain(param[0], param[1]);
753 }
754 } else {
755 pr_err("%s: Error, invalid parameters\n", __func__);
756 rc = -EINVAL;
757 }
758 }
759
760afe_error:
761 if (rc == 0)
762 rc = cnt;
763 else
764 pr_err("%s: rc = %d\n", __func__, rc);
765
766 return rc;
767}
768
769static const struct file_operations afe_debug_fops = {
770 .open = afe_debug_open,
771 .write = afe_debug_write
772};
773#endif
774int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
775{
776 struct afe_port_sidetone_command cmd_sidetone;
777 int ret = 0;
778
779 pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
780 tx_port_id, rx_port_id, enable, gain);
781 cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
782 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
783 cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
784 cmd_sidetone.hdr.src_port = 0;
785 cmd_sidetone.hdr.dest_port = 0;
786 cmd_sidetone.hdr.token = 0;
787 cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL;
788 cmd_sidetone.tx_port_id = tx_port_id;
789 cmd_sidetone.rx_port_id = rx_port_id;
790 cmd_sidetone.gain = gain;
791 cmd_sidetone.enable = enable;
792
793 atomic_set(&this_afe.state, 1);
794 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone);
795 if (ret < 0) {
796 pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
797 __func__, tx_port_id, rx_port_id);
798 ret = -EINVAL;
799 goto fail_cmd;
800 }
801
802 ret = wait_event_timeout(this_afe.wait,
803 (atomic_read(&this_afe.state) == 0),
804 msecs_to_jiffies(TIMEOUT_MS));
805 if (ret < 0) {
806 pr_err("%s: wait_event timeout\n", __func__);
807 ret = -EINVAL;
808 goto fail_cmd;
809 }
810 return 0;
811fail_cmd:
812 return ret;
813}
814
815int afe_port_stop_nowait(int port_id)
816{
817 struct afe_port_stop_command stop;
818 int ret = 0;
819
820 if (this_afe.apr == NULL) {
821 pr_err("AFE is already closed\n");
822 ret = -EINVAL;
823 goto fail_cmd;
824 }
825 pr_info("%s: port_id=%d\n", __func__, port_id);
826 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
827 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
828 stop.hdr.pkt_size = sizeof(stop);
829 stop.hdr.src_port = 0;
830 stop.hdr.dest_port = 0;
831 stop.hdr.token = 0;
832 stop.hdr.opcode = AFE_PORT_CMD_STOP;
833 stop.port_id = port_id;
834 stop.reserved = 0;
835
836 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
837
838 if (ret == -ENETRESET) {
839 pr_info("%s: Need to reset, calling APR deregister", __func__);
840 return apr_deregister(this_afe.apr);
841 } else if (IS_ERR_VALUE(ret)) {
842 pr_err("%s: AFE close failed\n", __func__);
843 ret = -EINVAL;
844 }
845
846fail_cmd:
847 return ret;
848
849}
850
851int afe_close(int port_id)
852{
853 struct afe_port_stop_command stop;
854 int ret = 0;
855
856 if (this_afe.apr == NULL) {
857 pr_err("AFE is already closed\n");
858 ret = -EINVAL;
859 goto fail_cmd;
860 }
861 pr_info("%s: port_id=%d\n", __func__, port_id);
862 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
863 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
864 stop.hdr.pkt_size = sizeof(stop);
865 stop.hdr.src_port = 0;
866 stop.hdr.dest_port = 0;
867 stop.hdr.token = 0;
868 stop.hdr.opcode = AFE_PORT_CMD_STOP;
869 stop.port_id = port_id;
870 stop.reserved = 0;
871
872 atomic_set(&this_afe.state, 1);
873 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
874
875 if (ret == -ENETRESET) {
876 pr_info("%s: Need to reset, calling APR deregister", __func__);
877 return apr_deregister(this_afe.apr);
878 }
879
880 if (ret < 0) {
881 pr_err("%s: AFE close failed\n", __func__);
882 ret = -EINVAL;
883 goto fail_cmd;
884 }
885
886 ret = wait_event_timeout(this_afe.wait,
887 (atomic_read(&this_afe.state) == 0),
888 msecs_to_jiffies(TIMEOUT_MS));
889 if (!ret) {
890 pr_err("%s: wait_event timeout\n", __func__);
891 ret = -EINVAL;
892 goto fail_cmd;
893 }
894fail_cmd:
895 return ret;
896}
897
898static int __init afe_init(void)
899{
900 init_waitqueue_head(&this_afe.wait);
901 atomic_set(&this_afe.state, 0);
902 atomic_set(&this_afe.status, 0);
903 this_afe.apr = NULL;
904#ifdef CONFIG_DEBUG_FS
905 debugfs_afelb = debugfs_create_file("afe_loopback",
906 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback",
907 &afe_debug_fops);
908
909 debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
910 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain",
911 &afe_debug_fops);
912
913
914#endif
915 return 0;
916}
917
918static void __exit afe_exit(void)
919{
920#ifdef CONFIG_DEBUG_FS
921 if (debugfs_afelb)
922 debugfs_remove(debugfs_afelb);
923 if (debugfs_afelb_gain)
924 debugfs_remove(debugfs_afelb_gain);
925#endif
926}
927
928device_initcall(afe_init);
929__exitcall(afe_exit);