blob: 017c1b618c7a219c12f75220ddda8adb91686b6a [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;
Laxminath Kasam32657ec2011-08-01 19:26:57 +053029 void (*tx_cb) (uint32_t opcode,
30 uint32_t token, uint32_t *payload, void *priv);
31 void (*rx_cb) (uint32_t opcode,
32 uint32_t token, uint32_t *payload, void *priv);
33 void *tx_private_data;
34 void *rx_private_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035};
36
37static struct afe_ctl this_afe;
38
39#define TIMEOUT_MS 1000
40#define Q6AFE_MAX_VOLUME 0x3FFF
41
42#define SIZEOF_CFG_CMD(y) \
43 (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
44
45static int32_t afe_callback(struct apr_client_data *data, void *priv)
46{
47 if (data->opcode == RESET_EVENTS) {
48 pr_debug("q6afe: reset event = %d %d apr[%p]\n",
49 data->reset_event, data->reset_proc, this_afe.apr);
50 if (this_afe.apr) {
51 apr_reset(this_afe.apr);
52 atomic_set(&this_afe.state, 0);
53 this_afe.apr = NULL;
54 }
55 /* send info to user */
56 pr_debug("task_name = %s pid = %d\n",
57 this_afe.task->comm, this_afe.task->pid);
58 send_sig(SIGUSR1, this_afe.task, 0);
59 }
60 if (data->payload_size) {
61 uint32_t *payload;
Laxminath Kasam32657ec2011-08-01 19:26:57 +053062 uint16_t port_id = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063 payload = data->payload;
Laxminath Kasam32657ec2011-08-01 19:26:57 +053064 pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x\n",
65 __func__, data->opcode,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 payload[0], payload[1]);
67 /* payload[1] contains the error status for response */
68 if (payload[1] != 0) {
69 atomic_set(&this_afe.status, -1);
70 pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
71 __func__, payload[0], payload[1]);
72 }
73 if (data->opcode == APR_BASIC_RSP_RESULT) {
74 switch (payload[0]) {
75 case AFE_PORT_AUDIO_IF_CONFIG:
76 case AFE_PORT_CMD_STOP:
77 case AFE_PORT_CMD_START:
78 case AFE_PORT_CMD_LOOPBACK:
79 case AFE_PORT_CMD_SIDETONE_CTL:
80 case AFE_PORT_CMD_SET_PARAM:
81 case AFE_PSEUDOPORT_CMD_START:
82 case AFE_PSEUDOPORT_CMD_STOP:
Laxminath Kasam885f5102011-07-14 10:20:21 +053083 case AFE_PORT_CMD_APPLY_GAIN:
Laxminath Kasam32657ec2011-08-01 19:26:57 +053084 case AFE_SERVICE_CMD_MEMORY_MAP:
85 case AFE_SERVICE_CMD_MEMORY_UNMAP:
86 case AFE_SERVICE_CMD_UNREG_RTPORT:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 atomic_set(&this_afe.state, 0);
88 wake_up(&this_afe.wait);
89 break;
Laxminath Kasam32657ec2011-08-01 19:26:57 +053090 case AFE_SERVICE_CMD_REG_RTPORT:
91 break;
92 case AFE_SERVICE_CMD_RTPORT_WR:
93 port_id = RT_PROXY_PORT_001_TX;
94 break;
95 case AFE_SERVICE_CMD_RTPORT_RD:
96 port_id = RT_PROXY_PORT_001_RX;
97 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 default:
99 pr_err("Unknown cmd 0x%x\n",
100 payload[0]);
101 break;
102 }
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530103 } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
104 port_id = (uint16_t)(0x0000FFFF & payload[0]);
105 }
106 pr_debug("%s:port_id = %x\n", __func__, port_id);
107 switch (port_id) {
108 case RT_PROXY_PORT_001_TX: {
109 if (this_afe.tx_cb) {
110 this_afe.tx_cb(data->opcode, data->token,
111 data->payload,
112 this_afe.tx_private_data);
113 }
114 break;
115 }
116 case RT_PROXY_PORT_001_RX: {
117 if (this_afe.rx_cb) {
118 this_afe.rx_cb(data->opcode, data->token,
119 data->payload,
120 this_afe.rx_private_data);
121 }
122 break;
123 }
124 default:
125 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 }
127 }
128 return 0;
129}
130
Neema Shettyfeea7742011-09-11 12:30:36 -0700131int afe_get_port_type(u16 port_id)
132{
133 int ret;
134
135 switch (port_id) {
136 case PRIMARY_I2S_RX:
137 case PCM_RX:
138 case SECONDARY_I2S_RX:
139 case MI2S_RX:
140 case HDMI_RX:
141 case SLIMBUS_0_RX:
142 case INT_BT_SCO_RX:
143 case INT_BT_A2DP_RX:
144 case INT_FM_RX:
145 case VOICE_PLAYBACK_TX:
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530146 case RT_PROXY_PORT_001_RX:
Neema Shettyfeea7742011-09-11 12:30:36 -0700147 ret = MSM_AFE_PORT_TYPE_RX;
148 break;
149
150 case PRIMARY_I2S_TX:
151 case PCM_TX:
152 case SECONDARY_I2S_TX:
153 case MI2S_TX:
154 case DIGI_MIC_TX:
155 case VOICE_RECORD_TX:
156 case SLIMBUS_0_TX:
157 case INT_FM_TX:
158 case VOICE_RECORD_RX:
159 case INT_BT_SCO_TX:
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530160 case RT_PROXY_PORT_001_TX:
Neema Shettyfeea7742011-09-11 12:30:36 -0700161 ret = MSM_AFE_PORT_TYPE_TX;
162 break;
163
164 default:
165 pr_err("%s: invalid port id\n", __func__);
166 ret = -EINVAL;
167 }
168
169 return ret;
170}
171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172int afe_validate_port(u16 port_id)
173{
174 int ret;
175
176 switch (port_id) {
177 case PRIMARY_I2S_RX:
178 case PRIMARY_I2S_TX:
179 case PCM_RX:
180 case PCM_TX:
181 case SECONDARY_I2S_RX:
182 case SECONDARY_I2S_TX:
183 case MI2S_RX:
184 case MI2S_TX:
185 case HDMI_RX:
186 case RSVD_2:
187 case RSVD_3:
188 case DIGI_MIC_TX:
189 case VOICE_RECORD_RX:
190 case VOICE_RECORD_TX:
191 case VOICE_PLAYBACK_TX:
192 case SLIMBUS_0_RX:
193 case SLIMBUS_0_TX:
194 case INT_BT_SCO_RX:
195 case INT_BT_SCO_TX:
196 case INT_BT_A2DP_RX:
197 case INT_FM_RX:
198 case INT_FM_TX:
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530199 case RT_PROXY_PORT_001_RX:
200 case RT_PROXY_PORT_001_TX:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 {
202 ret = 0;
203 break;
204 }
205
206 default:
207 ret = -EINVAL;
208 }
209
210 return ret;
211}
212
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530213int afe_convert_virtual_to_portid(u16 port_id)
214{
215 int ret;
216
217 /* if port_id is virtual, convert to physical..
218 * if port_id is already physical, return physical
219 */
220 if (afe_validate_port(port_id) < 0) {
221 if (port_id == RT_PROXY_DAI_001_RX ||
222 port_id == RT_PROXY_DAI_001_TX ||
223 port_id == RT_PROXY_DAI_002_RX ||
224 port_id == RT_PROXY_DAI_002_TX)
225 ret = VIRTUAL_ID_TO_PORTID(port_id);
226 else
227 ret = -EINVAL;
228 } else
229 ret = port_id;
230
231 return ret;
232}
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234int afe_get_port_index(u16 port_id)
235{
236 switch (port_id) {
237 case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
238 case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
239 case PCM_RX: return IDX_PCM_RX;
240 case PCM_TX: return IDX_PCM_TX;
241 case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
242 case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
243 case MI2S_RX: return IDX_MI2S_RX;
244 case MI2S_TX: return IDX_MI2S_TX;
245 case HDMI_RX: return IDX_HDMI_RX;
246 case RSVD_2: return IDX_RSVD_2;
247 case RSVD_3: return IDX_RSVD_3;
248 case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
249 case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
250 case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
251 case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
252 case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
253 case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
254 case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
255 case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
256 case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
257 case INT_FM_RX: return IDX_INT_FM_RX;
258 case INT_FM_TX: return IDX_INT_FM_TX;
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530259 case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
260 case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261
262 default: return -EINVAL;
263 }
264}
265
266int afe_sizeof_cfg_cmd(u16 port_id)
267{
268 int ret_size;
269 switch (port_id) {
270 case PRIMARY_I2S_RX:
271 case PRIMARY_I2S_TX:
272 case SECONDARY_I2S_RX:
273 case SECONDARY_I2S_TX:
274 case MI2S_RX:
275 case MI2S_TX:
276 ret_size = SIZEOF_CFG_CMD(afe_port_mi2s_cfg);
277 break;
278 case HDMI_RX:
279 ret_size = SIZEOF_CFG_CMD(afe_port_hdmi_cfg);
280 break;
281 case SLIMBUS_0_RX:
282 case SLIMBUS_0_TX:
283 ret_size = SIZEOF_CFG_CMD(afe_port_slimbus_cfg);
284 break;
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530285 case RT_PROXY_PORT_001_RX:
286 case RT_PROXY_PORT_001_TX:
287 ret_size = SIZEOF_CFG_CMD(afe_port_rtproxy_cfg);
288 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 case PCM_RX:
290 case PCM_TX:
291 default:
292 ret_size = SIZEOF_CFG_CMD(afe_port_pcm_cfg);
293 break;
294 }
295 return ret_size;
296}
297
Jay Wang6a305432011-08-05 16:01:54 -0700298int afe_q6_interface_prepare(void)
299{
300 int ret = 0;
301
302 pr_debug("%s:", __func__);
303
304 if (this_afe.apr == NULL) {
305 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
306 0xFFFFFFFF, &this_afe);
307 pr_debug("%s: Register AFE\n", __func__);
308 if (this_afe.apr == NULL) {
309 pr_err("%s: Unable to register AFE\n", __func__);
310 ret = -ENODEV;
311 }
312 }
313 return ret;
314}
315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316int afe_port_start_nowait(u16 port_id, union afe_port_config *afe_config,
317 u32 rate) /* This function is no blocking */
318{
319 struct afe_port_start_command start;
320 struct afe_audioif_config_command config;
321 int ret;
322
323 if (!afe_config) {
324 pr_err("%s: Error, no configuration data\n", __func__);
325 ret = -EINVAL;
326 return ret;
327 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 pr_info("%s: %d %d\n", __func__, port_id, rate);
329
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530330 if ((port_id == RT_PROXY_DAI_001_RX) ||
331 (port_id == RT_PROXY_DAI_002_TX))
332 return -EINVAL;
333 if ((port_id == RT_PROXY_DAI_002_RX) ||
334 (port_id == RT_PROXY_DAI_001_TX))
335 port_id = VIRTUAL_ID_TO_PORTID(port_id);
336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 if (this_afe.apr == NULL) {
Jay Wang6a305432011-08-05 16:01:54 -0700338 pr_err("%s: AFE APR is not registered\n", __func__);
339 ret = -ENODEV;
340 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
343 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
344 config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
345 config.hdr.src_port = 0;
346 config.hdr.dest_port = 0;
347 config.hdr.token = 0;
348 config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
349
350 if (afe_validate_port(port_id) < 0) {
351
352 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
353 port_id);
354 ret = -EINVAL;
355 goto fail_cmd;
356 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 config.port_id = port_id;
358 config.port = *afe_config;
359
360 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
361 if (ret < 0) {
362 pr_err("%s: AFE enable for port %d failed\n", __func__,
363 port_id);
364 ret = -EINVAL;
365 goto fail_cmd;
366 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
368 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
369 start.hdr.pkt_size = sizeof(start);
370 start.hdr.src_port = 0;
371 start.hdr.dest_port = 0;
372 start.hdr.token = 0;
373 start.hdr.opcode = AFE_PORT_CMD_START;
374 start.port_id = port_id;
375 start.gain = 0x2000;
376 start.sample_rate = rate;
377
378 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
379
380 if (IS_ERR_VALUE(ret)) {
381 pr_err("%s: AFE enable for port %d failed\n", __func__,
382 port_id);
383 ret = -EINVAL;
384 goto fail_cmd;
385 }
386
387 if (this_afe.task != current)
388 this_afe.task = current;
389
390 pr_debug("task_name = %s pid = %d\n",
391 this_afe.task->comm, this_afe.task->pid);
392 return 0;
393
394fail_cmd:
395 return ret;
396}
397
398int afe_open(u16 port_id, union afe_port_config *afe_config, int rate)
399{
400 struct afe_port_start_command start;
401 struct afe_audioif_config_command config;
402 int ret = 0;
403
404 if (!afe_config) {
405 pr_err("%s: Error, no configuration data\n", __func__);
406 ret = -EINVAL;
407 return ret;
408 }
409
410 pr_info("%s: %d %d\n", __func__, port_id, rate);
411
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530412 if ((port_id == RT_PROXY_DAI_001_RX) ||
413 (port_id == RT_PROXY_DAI_002_TX))
414 return -EINVAL;
415 if ((port_id == RT_PROXY_DAI_002_RX) ||
416 (port_id == RT_PROXY_DAI_001_TX))
417 port_id = VIRTUAL_ID_TO_PORTID(port_id);
418
Jay Wang6a305432011-08-05 16:01:54 -0700419 ret = afe_q6_interface_prepare();
420 if (ret != 0)
421 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
424 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
425 config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
426 config.hdr.src_port = 0;
427 config.hdr.dest_port = 0;
428 config.hdr.token = 0;
429 config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
430
431 if (afe_validate_port(port_id) < 0) {
432
433 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
434 port_id);
435 ret = -EINVAL;
436 goto fail_cmd;
437 }
438
439 config.port_id = port_id;
440 config.port = *afe_config;
441
442 atomic_set(&this_afe.state, 1);
443 atomic_set(&this_afe.status, 0);
444 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
445 if (ret < 0) {
446 pr_err("%s: AFE enable for port %d failed\n", __func__,
447 port_id);
448 ret = -EINVAL;
449 goto fail_cmd;
450 }
451
452 ret = wait_event_timeout(this_afe.wait,
453 (atomic_read(&this_afe.state) == 0),
454 msecs_to_jiffies(TIMEOUT_MS));
455 if (!ret) {
456 pr_err("%s: wait_event timeout\n", __func__);
457 ret = -EINVAL;
458 goto fail_cmd;
459 }
460 if (atomic_read(&this_afe.status) != 0) {
461 pr_err("%s: config cmd failed\n", __func__);
462 ret = -EINVAL;
463 goto fail_cmd;
464 }
465 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
466 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
467 start.hdr.pkt_size = sizeof(start);
468 start.hdr.src_port = 0;
469 start.hdr.dest_port = 0;
470 start.hdr.token = 0;
471 start.hdr.opcode = AFE_PORT_CMD_START;
472 start.port_id = port_id;
473 start.gain = 0x2000;
474 start.sample_rate = rate;
475
476 atomic_set(&this_afe.state, 1);
477 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
478 if (ret < 0) {
479 pr_err("%s: AFE enable for port %d failed\n", __func__,
480 port_id);
481 ret = -EINVAL;
482 goto fail_cmd;
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) {
488 pr_err("%s: wait_event timeout\n", __func__);
489 ret = -EINVAL;
490 goto fail_cmd;
491 }
492
493 if (this_afe.task != current)
494 this_afe.task = current;
495
496 pr_debug("task_name = %s pid = %d\n",
497 this_afe.task->comm, this_afe.task->pid);
498 return 0;
499fail_cmd:
500 return ret;
501}
502
503int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
504{
505 struct afe_loopback_command lb_cmd;
506 int ret = 0;
Jay Wang6a305432011-08-05 16:01:54 -0700507
508 ret = afe_q6_interface_prepare();
509 if (ret != 0)
510 return ret;
511
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
513 APR_HDR_LEN(20), APR_PKT_VER);
514 lb_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
515 sizeof(lb_cmd) - APR_HDR_SIZE);
516 lb_cmd.hdr.src_port = 0;
517 lb_cmd.hdr.dest_port = 0;
518 lb_cmd.hdr.token = 0;
519 lb_cmd.hdr.opcode = AFE_PORT_CMD_LOOPBACK;
520 lb_cmd.tx_port_id = tx_port;
521 lb_cmd.rx_port_id = rx_port;
522 lb_cmd.mode = 0xFFFF;
523 lb_cmd.enable = (enable ? 1 : 0);
524 atomic_set(&this_afe.state, 1);
525
526 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &lb_cmd);
527 if (ret < 0) {
528 pr_err("%s: AFE loopback failed\n", __func__);
529 ret = -EINVAL;
530 goto done;
531 }
532 ret = wait_event_timeout(this_afe.wait,
533 (atomic_read(&this_afe.state) == 0),
534 msecs_to_jiffies(TIMEOUT_MS));
535 if (!ret) {
536 pr_err("%s: wait_event timeout\n", __func__);
537 ret = -EINVAL;
538 }
539done:
540 return ret;
541}
542
543
544int afe_loopback_gain(u16 port_id, u16 volume)
545{
546 struct afe_port_cmd_set_param set_param;
547 int ret = 0;
548
549 if (this_afe.apr == NULL) {
Jayasena Sangaraboina82435032011-07-26 15:23:00 -0700550 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
551 0xFFFFFFFF, &this_afe);
552 pr_debug("%s: Register AFE\n", __func__);
553 if (this_afe.apr == NULL) {
554 pr_err("%s: Unable to register AFE\n", __func__);
555 ret = -ENODEV;
556 return ret;
557 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 }
559
560 if (afe_validate_port(port_id) < 0) {
561
562 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
563 port_id);
564 ret = -EINVAL;
565 goto fail_cmd;
566 }
567
568 /* RX ports numbers are even .TX ports numbers are odd. */
569 if (port_id % 2 == 0) {
570 pr_err("%s: Failed : afe loopback gain only for TX ports."
571 " port_id %d\n", __func__, port_id);
572 ret = -EINVAL;
573 goto fail_cmd;
574 }
575
576 pr_debug("%s: %d %hX\n", __func__, port_id, volume);
577
578 set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
579 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
580 set_param.hdr.pkt_size = sizeof(set_param);
581 set_param.hdr.src_port = 0;
582 set_param.hdr.dest_port = 0;
583 set_param.hdr.token = 0;
584 set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
585
586 set_param.port_id = port_id;
587 set_param.payload_size = sizeof(struct afe_param_payload);
588 set_param.payload_address = 0;
589
590 set_param.payload.module_id = AFE_MODULE_ID_PORT_INFO;
591 set_param.payload.param_id = AFE_PARAM_ID_LOOPBACK_GAIN;
592 set_param.payload.param_size = sizeof(struct afe_param_loopback_gain);
593 set_param.payload.reserved = 0;
594
595 set_param.payload.param.loopback_gain.gain = volume;
596 set_param.payload.param.loopback_gain.reserved = 0;
597
598 atomic_set(&this_afe.state, 1);
599 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_param);
600 if (ret < 0) {
601 pr_err("%s: AFE param set failed for port %d\n",
602 __func__, port_id);
603 ret = -EINVAL;
604 goto fail_cmd;
605 }
606
607 ret = wait_event_timeout(this_afe.wait,
608 (atomic_read(&this_afe.state) == 0),
609 msecs_to_jiffies(TIMEOUT_MS));
610 if (ret < 0) {
611 pr_err("%s: wait_event timeout\n", __func__);
612 ret = -EINVAL;
613 goto fail_cmd;
614 }
615 return 0;
616fail_cmd:
617 return ret;
618}
619
Laxminath Kasam885f5102011-07-14 10:20:21 +0530620int afe_apply_gain(u16 port_id, u16 gain)
621{
622 struct afe_port_gain_command set_gain;
623 int ret = 0;
624
625 if (this_afe.apr == NULL) {
626 pr_err("%s: AFE is not opened\n", __func__);
627 ret = -EPERM;
628 goto fail_cmd;
629 }
630
631 if (afe_validate_port(port_id) < 0) {
632 pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
633 port_id);
634 ret = -EINVAL;
635 goto fail_cmd;
636 }
637
638 /* RX ports numbers are even .TX ports numbers are odd. */
639 if (port_id % 2 == 0) {
640 pr_err("%s: Failed : afe apply gain only for TX ports."
641 " port_id %d\n", __func__, port_id);
642 ret = -EINVAL;
643 goto fail_cmd;
644 }
645
646 pr_debug("%s: %d %hX\n", __func__, port_id, gain);
647
648 set_gain.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
649 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
650 set_gain.hdr.pkt_size = sizeof(set_gain);
651 set_gain.hdr.src_port = 0;
652 set_gain.hdr.dest_port = 0;
653 set_gain.hdr.token = 0;
654 set_gain.hdr.opcode = AFE_PORT_CMD_APPLY_GAIN;
655
656 set_gain.port_id = port_id;
657 set_gain.gain = gain;
658
659 atomic_set(&this_afe.state, 1);
660 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_gain);
661 if (ret < 0) {
662 pr_err("%s: AFE Gain set failed for port %d\n",
663 __func__, port_id);
664 ret = -EINVAL;
665 goto fail_cmd;
666 }
667
668 ret = wait_event_timeout(this_afe.wait,
669 (atomic_read(&this_afe.state) == 0),
670 msecs_to_jiffies(TIMEOUT_MS));
671 if (ret < 0) {
672 pr_err("%s: wait_event timeout\n", __func__);
673 ret = -EINVAL;
674 goto fail_cmd;
675 }
676 return 0;
677fail_cmd:
678 return ret;
679}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680int afe_start_pseudo_port(u16 port_id)
681{
682 int ret = 0;
683 struct afe_pseudoport_start_command start;
684
685 pr_info("%s: port_id=%d\n", __func__, port_id);
686
Jay Wang6a305432011-08-05 16:01:54 -0700687 ret = afe_q6_interface_prepare();
688 if (ret != 0)
689 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690
691 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
692 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
693 start.hdr.pkt_size = sizeof(start);
694 start.hdr.src_port = 0;
695 start.hdr.dest_port = 0;
696 start.hdr.token = 0;
697 start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
698 start.port_id = port_id;
699 start.timing = 1;
700
701 atomic_set(&this_afe.state, 1);
702 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
703 if (ret < 0) {
704 pr_err("%s: AFE enable for port %d failed %d\n",
705 __func__, port_id, ret);
706 ret = -EINVAL;
707 return ret;
708 }
709
710 ret = wait_event_timeout(this_afe.wait,
711 (atomic_read(&this_afe.state) == 0),
712 msecs_to_jiffies(TIMEOUT_MS));
713 if (!ret) {
714 pr_err("%s: wait_event timeout\n", __func__);
715 ret = -EINVAL;
716 return ret;
717 }
718
719 return 0;
720}
721
722int afe_stop_pseudo_port(u16 port_id)
723{
724 int ret = 0;
725 struct afe_pseudoport_stop_command stop;
726
727 pr_info("%s: port_id=%d\n", __func__, port_id);
728
729 if (this_afe.apr == NULL) {
730 pr_err("%s: AFE is already closed\n", __func__);
731 ret = -EINVAL;
732 return ret;
733 }
734
735 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
736 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
737 stop.hdr.pkt_size = sizeof(stop);
738 stop.hdr.src_port = 0;
739 stop.hdr.dest_port = 0;
740 stop.hdr.token = 0;
741 stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
742 stop.port_id = port_id;
743 stop.reserved = 0;
744
745 atomic_set(&this_afe.state, 1);
746 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
747 if (ret < 0) {
748 pr_err("%s: AFE close failed %d\n", __func__, ret);
749 ret = -EINVAL;
750 return ret;
751 }
752
753 ret = wait_event_timeout(this_afe.wait,
754 (atomic_read(&this_afe.state) == 0),
755 msecs_to_jiffies(TIMEOUT_MS));
756 if (!ret) {
757 pr_err("%s: wait_event timeout\n", __func__);
758 ret = -EINVAL;
759 return ret;
760 }
761
762 return 0;
763}
764
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530765int afe_cmd_memory_map(u32 dma_addr_p, u32 dma_buf_sz)
766{
767 int ret = 0;
768 struct afe_cmd_memory_map mregion;
769
770 pr_debug("%s:\n", __func__);
771
772 if (this_afe.apr == NULL) {
773 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
774 0xFFFFFFFF, &this_afe);
775 pr_debug("%s: Register AFE\n", __func__);
776 if (this_afe.apr == NULL) {
777 pr_err("%s: Unable to register AFE\n", __func__);
778 ret = -ENODEV;
779 return ret;
780 }
781 }
782
783 mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
784 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
785 mregion.hdr.pkt_size = sizeof(mregion);
786 mregion.hdr.src_port = 0;
787 mregion.hdr.dest_port = 0;
788 mregion.hdr.token = 0;
789 mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_MAP;
790 mregion.phy_addr = dma_addr_p;
791 mregion.mem_sz = dma_buf_sz;
792 mregion.mem_id = 0;
793 mregion.rsvd = 0;
794
795 atomic_set(&this_afe.state, 1);
796 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
797 if (ret < 0) {
798 pr_err("%s: AFE memory map cmd failed %d\n",
799 __func__, ret);
800 ret = -EINVAL;
801 return ret;
802 }
803
804 ret = wait_event_timeout(this_afe.wait,
805 (atomic_read(&this_afe.state) == 0),
806 msecs_to_jiffies(TIMEOUT_MS));
807 if (!ret) {
808 pr_err("%s: wait_event timeout\n", __func__);
809 ret = -EINVAL;
810 return ret;
811 }
812
813 return 0;
814}
815
816int afe_cmd_memory_unmap(u32 dma_addr_p)
817{
818 int ret = 0;
819 struct afe_cmd_memory_unmap mregion;
820
821 pr_debug("%s:\n", __func__);
822
823 if (this_afe.apr == NULL) {
824 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
825 0xFFFFFFFF, &this_afe);
826 pr_debug("%s: Register AFE\n", __func__);
827 if (this_afe.apr == NULL) {
828 pr_err("%s: Unable to register AFE\n", __func__);
829 ret = -ENODEV;
830 return ret;
831 }
832 }
833
834 mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
835 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
836 mregion.hdr.pkt_size = sizeof(mregion);
837 mregion.hdr.src_port = 0;
838 mregion.hdr.dest_port = 0;
839 mregion.hdr.token = 0;
840 mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_UNMAP;
841 mregion.phy_addr = dma_addr_p;
842
843 atomic_set(&this_afe.state, 1);
844 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
845 if (ret < 0) {
846 pr_err("%s: AFE memory map cmd failed %d\n",
847 __func__, ret);
848 ret = -EINVAL;
849 return ret;
850 }
851
852 ret = wait_event_timeout(this_afe.wait,
853 (atomic_read(&this_afe.state) == 0),
854 msecs_to_jiffies(TIMEOUT_MS));
855 if (!ret) {
856 pr_err("%s: wait_event timeout\n", __func__);
857 ret = -EINVAL;
858 return ret;
859 }
860
861 return 0;
862}
863
864int afe_register_get_events(u16 port_id,
865 void (*cb) (uint32_t opcode,
866 uint32_t token, uint32_t *payload, void *priv),
867 void *private_data)
868{
869 int ret = 0;
870 struct afe_cmd_reg_rtport rtproxy;
871
872 pr_debug("%s:\n", __func__);
873
874 if (this_afe.apr == NULL) {
875 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
876 0xFFFFFFFF, &this_afe);
877 pr_debug("%s: Register AFE\n", __func__);
878 if (this_afe.apr == NULL) {
879 pr_err("%s: Unable to register AFE\n", __func__);
880 ret = -ENODEV;
881 return ret;
882 }
883 }
884 if ((port_id == RT_PROXY_DAI_002_RX) ||
885 (port_id == RT_PROXY_DAI_001_TX))
886 port_id = VIRTUAL_ID_TO_PORTID(port_id);
887 else
888 return -EINVAL;
889
890 if (port_id == RT_PROXY_PORT_001_TX) {
891 this_afe.tx_cb = cb;
892 this_afe.tx_private_data = private_data;
893 } else if (port_id == RT_PROXY_PORT_001_RX) {
894 this_afe.rx_cb = cb;
895 this_afe.rx_private_data = private_data;
896 }
897
898 rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
899 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
900 rtproxy.hdr.pkt_size = sizeof(rtproxy);
901 rtproxy.hdr.src_port = 1;
902 rtproxy.hdr.dest_port = 1;
903 rtproxy.hdr.token = 0;
904 rtproxy.hdr.opcode = AFE_SERVICE_CMD_REG_RTPORT;
905 rtproxy.port_id = port_id;
906 rtproxy.rsvd = 0;
907
908 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
909 if (ret < 0) {
910 pr_err("%s: AFE reg. rtproxy_event failed %d\n",
911 __func__, ret);
912 ret = -EINVAL;
913 return ret;
914 }
915 return 0;
916}
917
918int afe_unregister_get_events(u16 port_id)
919{
920 int ret = 0;
921 struct afe_cmd_unreg_rtport rtproxy;
922
923 pr_debug("%s:\n", __func__);
924
925 if (this_afe.apr == NULL) {
926 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
927 0xFFFFFFFF, &this_afe);
928 pr_debug("%s: Register AFE\n", __func__);
929 if (this_afe.apr == NULL) {
930 pr_err("%s: Unable to register AFE\n", __func__);
931 ret = -ENODEV;
932 return ret;
933 }
934 }
935 if ((port_id == RT_PROXY_DAI_002_RX) ||
936 (port_id == RT_PROXY_DAI_001_TX))
937 port_id = VIRTUAL_ID_TO_PORTID(port_id);
938 else
939 return -EINVAL;
940
941 rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
942 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
943 rtproxy.hdr.pkt_size = sizeof(rtproxy);
944 rtproxy.hdr.src_port = 0;
945 rtproxy.hdr.dest_port = 0;
946 rtproxy.hdr.token = 0;
947 rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREG_RTPORT;
948 rtproxy.port_id = port_id;
949 rtproxy.rsvd = 0;
950
951 if (port_id == RT_PROXY_PORT_001_TX) {
952 this_afe.tx_cb = NULL;
953 this_afe.tx_private_data = NULL;
954 } else if (port_id == RT_PROXY_PORT_001_RX) {
955 this_afe.rx_cb = NULL;
956 this_afe.rx_private_data = NULL;
957 }
958
959 atomic_set(&this_afe.state, 1);
960 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
961 if (ret < 0) {
962 pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
963 __func__, ret);
964 ret = -EINVAL;
965 return ret;
966 }
967
968 ret = wait_event_timeout(this_afe.wait,
969 (atomic_read(&this_afe.state) == 0),
970 msecs_to_jiffies(TIMEOUT_MS));
971 if (!ret) {
972 pr_err("%s: wait_event timeout\n", __func__);
973 ret = -EINVAL;
974 return ret;
975 }
976 return 0;
977}
978
979int afe_rt_proxy_port_write(u32 buf_addr_p, int bytes)
980{
981 int ret = 0;
982 struct afe_cmd_rtport_wr afecmd_wr;
983
984 if (this_afe.apr == NULL) {
985 pr_err("%s:register to AFE is not done\n", __func__);
986 ret = -ENODEV;
987 return ret;
988 }
989 pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
990 buf_addr_p, bytes);
991
992 afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
993 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
994 afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
995 afecmd_wr.hdr.src_port = 0;
996 afecmd_wr.hdr.dest_port = 0;
997 afecmd_wr.hdr.token = 0;
998 afecmd_wr.hdr.opcode = AFE_SERVICE_CMD_RTPORT_WR;
999 afecmd_wr.buf_addr = (uint32_t)buf_addr_p;
1000 afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
1001 afecmd_wr.bytes_avail = bytes;
1002 afecmd_wr.rsvd = 0;
1003
1004 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_wr);
1005 if (ret < 0) {
1006 pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
1007 __func__, afecmd_wr.port_id, ret);
1008 ret = -EINVAL;
1009 return ret;
1010 }
1011 return 0;
1012
1013}
1014
1015int afe_rt_proxy_port_read(u32 buf_addr_p, int bytes)
1016{
1017 int ret = 0;
1018 struct afe_cmd_rtport_rd afecmd_rd;
1019
1020 if (this_afe.apr == NULL) {
1021 pr_err("%s: register to AFE is not done\n", __func__);
1022 ret = -ENODEV;
1023 return ret;
1024 }
1025 pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
1026 buf_addr_p, bytes);
1027
1028 afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1029 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1030 afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
1031 afecmd_rd.hdr.src_port = 0;
1032 afecmd_rd.hdr.dest_port = 0;
1033 afecmd_rd.hdr.token = 0;
1034 afecmd_rd.hdr.opcode = AFE_SERVICE_CMD_RTPORT_RD;
1035 afecmd_rd.buf_addr = (uint32_t)buf_addr_p;
1036 afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
1037 afecmd_rd.bytes_avail = bytes;
1038 afecmd_rd.rsvd = 0;
1039
1040 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_rd);
1041 if (ret < 0) {
1042 pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
1043 __func__, afecmd_rd.port_id, ret);
1044 ret = -EINVAL;
1045 return ret;
1046 }
1047 return 0;
1048}
1049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050#ifdef CONFIG_DEBUG_FS
1051static struct dentry *debugfs_afelb;
1052static struct dentry *debugfs_afelb_gain;
1053
1054static int afe_debug_open(struct inode *inode, struct file *file)
1055{
1056 file->private_data = inode->i_private;
1057 pr_info("debug intf %s\n", (char *) file->private_data);
1058 return 0;
1059}
1060
1061static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
1062{
1063 char *token;
1064 int base, cnt;
1065
1066 token = strsep(&buf, " ");
1067
1068 for (cnt = 0; cnt < num_of_par; cnt++) {
1069 if (token != NULL) {
1070 if ((token[1] == 'x') || (token[1] == 'X'))
1071 base = 16;
1072 else
1073 base = 10;
1074
1075 if (strict_strtoul(token, base, &param1[cnt]) != 0)
1076 return -EINVAL;
1077
1078 token = strsep(&buf, " ");
1079 } else
1080 return -EINVAL;
1081 }
1082 return 0;
1083}
1084#define AFE_LOOPBACK_ON (1)
1085#define AFE_LOOPBACK_OFF (0)
1086static ssize_t afe_debug_write(struct file *filp,
1087 const char __user *ubuf, size_t cnt, loff_t *ppos)
1088{
1089 char *lb_str = filp->private_data;
1090 char lbuf[32];
1091 int rc;
1092 unsigned long param[5];
1093
1094 if (cnt > sizeof(lbuf) - 1)
1095 return -EINVAL;
1096
1097 rc = copy_from_user(lbuf, ubuf, cnt);
1098 if (rc)
1099 return -EFAULT;
1100
1101 lbuf[cnt] = '\0';
1102
1103 if (!strcmp(lb_str, "afe_loopback")) {
1104 rc = afe_get_parameters(lbuf, param, 3);
1105 if (!rc) {
1106 pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
1107 param[2]);
1108
1109 if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
1110 AFE_LOOPBACK_OFF)) {
1111 pr_err("%s: Error, parameter 0 incorrect\n",
1112 __func__);
1113 rc = -EINVAL;
1114 goto afe_error;
1115 }
1116 if ((afe_validate_port(param[1]) < 0) ||
1117 (afe_validate_port(param[2])) < 0) {
1118 pr_err("%s: Error, invalid afe port\n",
1119 __func__);
1120 }
1121 if (this_afe.apr == NULL) {
1122 pr_err("%s: Error, AFE not opened\n", __func__);
1123 rc = -EINVAL;
1124 } else {
1125 rc = afe_loopback(param[0], param[1], param[2]);
1126 }
1127 } else {
1128 pr_err("%s: Error, invalid parameters\n", __func__);
1129 rc = -EINVAL;
1130 }
1131
1132 } else if (!strcmp(lb_str, "afe_loopback_gain")) {
1133 rc = afe_get_parameters(lbuf, param, 2);
1134 if (!rc) {
1135 pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
1136
1137 if (afe_validate_port(param[0]) < 0) {
1138 pr_err("%s: Error, invalid afe port\n",
1139 __func__);
1140 rc = -EINVAL;
1141 goto afe_error;
1142 }
1143
1144 if (param[1] < 0 || param[1] > 100) {
1145 pr_err("%s: Error, volume shoud be 0 to 100"
1146 " percentage param = %lu\n",
1147 __func__, param[1]);
1148 rc = -EINVAL;
1149 goto afe_error;
1150 }
1151
1152 param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
1153
1154 if (this_afe.apr == NULL) {
1155 pr_err("%s: Error, AFE not opened\n", __func__);
1156 rc = -EINVAL;
1157 } else {
1158 rc = afe_loopback_gain(param[0], param[1]);
1159 }
1160 } else {
1161 pr_err("%s: Error, invalid parameters\n", __func__);
1162 rc = -EINVAL;
1163 }
1164 }
1165
1166afe_error:
1167 if (rc == 0)
1168 rc = cnt;
1169 else
1170 pr_err("%s: rc = %d\n", __func__, rc);
1171
1172 return rc;
1173}
1174
1175static const struct file_operations afe_debug_fops = {
1176 .open = afe_debug_open,
1177 .write = afe_debug_write
1178};
1179#endif
1180int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
1181{
1182 struct afe_port_sidetone_command cmd_sidetone;
1183 int ret = 0;
1184
1185 pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
1186 tx_port_id, rx_port_id, enable, gain);
1187 cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1188 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1189 cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
1190 cmd_sidetone.hdr.src_port = 0;
1191 cmd_sidetone.hdr.dest_port = 0;
1192 cmd_sidetone.hdr.token = 0;
1193 cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL;
1194 cmd_sidetone.tx_port_id = tx_port_id;
1195 cmd_sidetone.rx_port_id = rx_port_id;
1196 cmd_sidetone.gain = gain;
1197 cmd_sidetone.enable = enable;
1198
1199 atomic_set(&this_afe.state, 1);
1200 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone);
1201 if (ret < 0) {
1202 pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
1203 __func__, tx_port_id, rx_port_id);
1204 ret = -EINVAL;
1205 goto fail_cmd;
1206 }
1207
1208 ret = wait_event_timeout(this_afe.wait,
1209 (atomic_read(&this_afe.state) == 0),
1210 msecs_to_jiffies(TIMEOUT_MS));
1211 if (ret < 0) {
1212 pr_err("%s: wait_event timeout\n", __func__);
1213 ret = -EINVAL;
1214 goto fail_cmd;
1215 }
1216 return 0;
1217fail_cmd:
1218 return ret;
1219}
1220
1221int afe_port_stop_nowait(int port_id)
1222{
1223 struct afe_port_stop_command stop;
1224 int ret = 0;
1225
1226 if (this_afe.apr == NULL) {
1227 pr_err("AFE is already closed\n");
1228 ret = -EINVAL;
1229 goto fail_cmd;
1230 }
1231 pr_info("%s: port_id=%d\n", __func__, port_id);
Laxminath Kasam32657ec2011-08-01 19:26:57 +05301232 port_id = afe_convert_virtual_to_portid(port_id);
1233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1235 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1236 stop.hdr.pkt_size = sizeof(stop);
1237 stop.hdr.src_port = 0;
1238 stop.hdr.dest_port = 0;
1239 stop.hdr.token = 0;
1240 stop.hdr.opcode = AFE_PORT_CMD_STOP;
1241 stop.port_id = port_id;
1242 stop.reserved = 0;
1243
1244 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
1245
1246 if (ret == -ENETRESET) {
1247 pr_info("%s: Need to reset, calling APR deregister", __func__);
1248 return apr_deregister(this_afe.apr);
1249 } else if (IS_ERR_VALUE(ret)) {
1250 pr_err("%s: AFE close failed\n", __func__);
1251 ret = -EINVAL;
1252 }
1253
1254fail_cmd:
1255 return ret;
1256
1257}
1258
1259int afe_close(int port_id)
1260{
1261 struct afe_port_stop_command stop;
1262 int ret = 0;
1263
1264 if (this_afe.apr == NULL) {
1265 pr_err("AFE is already closed\n");
1266 ret = -EINVAL;
1267 goto fail_cmd;
1268 }
1269 pr_info("%s: port_id=%d\n", __func__, port_id);
Laxminath Kasam32657ec2011-08-01 19:26:57 +05301270 port_id = afe_convert_virtual_to_portid(port_id);
1271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1273 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1274 stop.hdr.pkt_size = sizeof(stop);
1275 stop.hdr.src_port = 0;
1276 stop.hdr.dest_port = 0;
1277 stop.hdr.token = 0;
1278 stop.hdr.opcode = AFE_PORT_CMD_STOP;
1279 stop.port_id = port_id;
1280 stop.reserved = 0;
1281
1282 atomic_set(&this_afe.state, 1);
1283 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
1284
1285 if (ret == -ENETRESET) {
1286 pr_info("%s: Need to reset, calling APR deregister", __func__);
1287 return apr_deregister(this_afe.apr);
1288 }
1289
1290 if (ret < 0) {
1291 pr_err("%s: AFE close failed\n", __func__);
1292 ret = -EINVAL;
1293 goto fail_cmd;
1294 }
1295
1296 ret = wait_event_timeout(this_afe.wait,
1297 (atomic_read(&this_afe.state) == 0),
1298 msecs_to_jiffies(TIMEOUT_MS));
1299 if (!ret) {
1300 pr_err("%s: wait_event timeout\n", __func__);
1301 ret = -EINVAL;
1302 goto fail_cmd;
1303 }
1304fail_cmd:
1305 return ret;
1306}
1307
1308static int __init afe_init(void)
1309{
1310 init_waitqueue_head(&this_afe.wait);
1311 atomic_set(&this_afe.state, 0);
1312 atomic_set(&this_afe.status, 0);
1313 this_afe.apr = NULL;
1314#ifdef CONFIG_DEBUG_FS
1315 debugfs_afelb = debugfs_create_file("afe_loopback",
1316 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback",
1317 &afe_debug_fops);
1318
1319 debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
1320 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain",
1321 &afe_debug_fops);
1322
1323
1324#endif
1325 return 0;
1326}
1327
1328static void __exit afe_exit(void)
1329{
1330#ifdef CONFIG_DEBUG_FS
1331 if (debugfs_afelb)
1332 debugfs_remove(debugfs_afelb);
1333 if (debugfs_afelb_gain)
1334 debugfs_remove(debugfs_afelb_gain);
1335#endif
1336}
1337
1338device_initcall(afe_init);
1339__exitcall(afe_exit);