blob: 21bbcf2dab1cfeebd903bb9f7871863964f0c9de [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}
Helen Zeng0705a5f2011-10-14 15:29:52 -0700680
681int afe_pseudo_port_start_nowait(u16 port_id)
682{
683 int ret = 0;
684 struct afe_pseudoport_start_command start;
685
686 pr_debug("%s: port_id=%d\n", __func__, port_id);
687 if (this_afe.apr == NULL) {
688 pr_err("%s: AFE APR is not registered\n", __func__);
689 return -ENODEV;
690 }
691
692
693 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
694 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
695 start.hdr.pkt_size = sizeof(start);
696 start.hdr.src_port = 0;
697 start.hdr.dest_port = 0;
698 start.hdr.token = 0;
699 start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
700 start.port_id = port_id;
701 start.timing = 1;
702
703 atomic_set(&this_afe.state, 1);
704 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
705 if (ret < 0) {
706 pr_err("%s: AFE enable for port %d failed %d\n",
707 __func__, port_id, ret);
708 return -EINVAL;
709 }
710 return 0;
711}
712
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713int afe_start_pseudo_port(u16 port_id)
714{
715 int ret = 0;
716 struct afe_pseudoport_start_command start;
717
718 pr_info("%s: port_id=%d\n", __func__, port_id);
719
Jay Wang6a305432011-08-05 16:01:54 -0700720 ret = afe_q6_interface_prepare();
721 if (ret != 0)
722 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
724 start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
725 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
726 start.hdr.pkt_size = sizeof(start);
727 start.hdr.src_port = 0;
728 start.hdr.dest_port = 0;
729 start.hdr.token = 0;
730 start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
731 start.port_id = port_id;
732 start.timing = 1;
733
734 atomic_set(&this_afe.state, 1);
735 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
736 if (ret < 0) {
737 pr_err("%s: AFE enable for port %d failed %d\n",
738 __func__, port_id, ret);
Helen Zeng0705a5f2011-10-14 15:29:52 -0700739 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 }
741
742 ret = wait_event_timeout(this_afe.wait,
743 (atomic_read(&this_afe.state) == 0),
744 msecs_to_jiffies(TIMEOUT_MS));
745 if (!ret) {
746 pr_err("%s: wait_event timeout\n", __func__);
Helen Zeng0705a5f2011-10-14 15:29:52 -0700747 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 }
749
750 return 0;
751}
752
Helen Zeng0705a5f2011-10-14 15:29:52 -0700753int afe_pseudo_port_stop_nowait(u16 port_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754{
755 int ret = 0;
756 struct afe_pseudoport_stop_command stop;
757
Helen Zeng0705a5f2011-10-14 15:29:52 -0700758 pr_debug("%s: port_id=%d\n", __func__, port_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759
760 if (this_afe.apr == NULL) {
761 pr_err("%s: AFE is already closed\n", __func__);
Helen Zeng0705a5f2011-10-14 15:29:52 -0700762 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 }
764
765 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
766 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
767 stop.hdr.pkt_size = sizeof(stop);
768 stop.hdr.src_port = 0;
769 stop.hdr.dest_port = 0;
770 stop.hdr.token = 0;
771 stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
772 stop.port_id = port_id;
773 stop.reserved = 0;
774
775 atomic_set(&this_afe.state, 1);
776 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
777 if (ret < 0) {
778 pr_err("%s: AFE close failed %d\n", __func__, ret);
Helen Zeng0705a5f2011-10-14 15:29:52 -0700779 return -EINVAL;
780 }
781
782 return 0;
783
784}
785
786int afe_stop_pseudo_port(u16 port_id)
787{
788 int ret = 0;
789 struct afe_pseudoport_stop_command stop;
790
791 pr_info("%s: port_id=%d\n", __func__, port_id);
792
793 if (this_afe.apr == NULL) {
794 pr_err("%s: AFE is already closed\n", __func__);
795 return -EINVAL;
796 }
797
798 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
799 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
800 stop.hdr.pkt_size = sizeof(stop);
801 stop.hdr.src_port = 0;
802 stop.hdr.dest_port = 0;
803 stop.hdr.token = 0;
804 stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
805 stop.port_id = port_id;
806 stop.reserved = 0;
807
808 atomic_set(&this_afe.state, 1);
809 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
810 if (ret < 0) {
811 pr_err("%s: AFE close failed %d\n", __func__, ret);
812 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 }
814
815 ret = wait_event_timeout(this_afe.wait,
816 (atomic_read(&this_afe.state) == 0),
817 msecs_to_jiffies(TIMEOUT_MS));
818 if (!ret) {
819 pr_err("%s: wait_event timeout\n", __func__);
Helen Zeng0705a5f2011-10-14 15:29:52 -0700820 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 }
822
823 return 0;
824}
825
Laxminath Kasam32657ec2011-08-01 19:26:57 +0530826int afe_cmd_memory_map(u32 dma_addr_p, u32 dma_buf_sz)
827{
828 int ret = 0;
829 struct afe_cmd_memory_map mregion;
830
831 pr_debug("%s:\n", __func__);
832
833 if (this_afe.apr == NULL) {
834 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
835 0xFFFFFFFF, &this_afe);
836 pr_debug("%s: Register AFE\n", __func__);
837 if (this_afe.apr == NULL) {
838 pr_err("%s: Unable to register AFE\n", __func__);
839 ret = -ENODEV;
840 return ret;
841 }
842 }
843
844 mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
845 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
846 mregion.hdr.pkt_size = sizeof(mregion);
847 mregion.hdr.src_port = 0;
848 mregion.hdr.dest_port = 0;
849 mregion.hdr.token = 0;
850 mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_MAP;
851 mregion.phy_addr = dma_addr_p;
852 mregion.mem_sz = dma_buf_sz;
853 mregion.mem_id = 0;
854 mregion.rsvd = 0;
855
856 atomic_set(&this_afe.state, 1);
857 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
858 if (ret < 0) {
859 pr_err("%s: AFE memory map cmd failed %d\n",
860 __func__, ret);
861 ret = -EINVAL;
862 return ret;
863 }
864
865 ret = wait_event_timeout(this_afe.wait,
866 (atomic_read(&this_afe.state) == 0),
867 msecs_to_jiffies(TIMEOUT_MS));
868 if (!ret) {
869 pr_err("%s: wait_event timeout\n", __func__);
870 ret = -EINVAL;
871 return ret;
872 }
873
874 return 0;
875}
876
877int afe_cmd_memory_unmap(u32 dma_addr_p)
878{
879 int ret = 0;
880 struct afe_cmd_memory_unmap mregion;
881
882 pr_debug("%s:\n", __func__);
883
884 if (this_afe.apr == NULL) {
885 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
886 0xFFFFFFFF, &this_afe);
887 pr_debug("%s: Register AFE\n", __func__);
888 if (this_afe.apr == NULL) {
889 pr_err("%s: Unable to register AFE\n", __func__);
890 ret = -ENODEV;
891 return ret;
892 }
893 }
894
895 mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
896 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
897 mregion.hdr.pkt_size = sizeof(mregion);
898 mregion.hdr.src_port = 0;
899 mregion.hdr.dest_port = 0;
900 mregion.hdr.token = 0;
901 mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_UNMAP;
902 mregion.phy_addr = dma_addr_p;
903
904 atomic_set(&this_afe.state, 1);
905 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
906 if (ret < 0) {
907 pr_err("%s: AFE memory map cmd failed %d\n",
908 __func__, ret);
909 ret = -EINVAL;
910 return ret;
911 }
912
913 ret = wait_event_timeout(this_afe.wait,
914 (atomic_read(&this_afe.state) == 0),
915 msecs_to_jiffies(TIMEOUT_MS));
916 if (!ret) {
917 pr_err("%s: wait_event timeout\n", __func__);
918 ret = -EINVAL;
919 return ret;
920 }
921
922 return 0;
923}
924
925int afe_register_get_events(u16 port_id,
926 void (*cb) (uint32_t opcode,
927 uint32_t token, uint32_t *payload, void *priv),
928 void *private_data)
929{
930 int ret = 0;
931 struct afe_cmd_reg_rtport rtproxy;
932
933 pr_debug("%s:\n", __func__);
934
935 if (this_afe.apr == NULL) {
936 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
937 0xFFFFFFFF, &this_afe);
938 pr_debug("%s: Register AFE\n", __func__);
939 if (this_afe.apr == NULL) {
940 pr_err("%s: Unable to register AFE\n", __func__);
941 ret = -ENODEV;
942 return ret;
943 }
944 }
945 if ((port_id == RT_PROXY_DAI_002_RX) ||
946 (port_id == RT_PROXY_DAI_001_TX))
947 port_id = VIRTUAL_ID_TO_PORTID(port_id);
948 else
949 return -EINVAL;
950
951 if (port_id == RT_PROXY_PORT_001_TX) {
952 this_afe.tx_cb = cb;
953 this_afe.tx_private_data = private_data;
954 } else if (port_id == RT_PROXY_PORT_001_RX) {
955 this_afe.rx_cb = cb;
956 this_afe.rx_private_data = private_data;
957 }
958
959 rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
960 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
961 rtproxy.hdr.pkt_size = sizeof(rtproxy);
962 rtproxy.hdr.src_port = 1;
963 rtproxy.hdr.dest_port = 1;
964 rtproxy.hdr.token = 0;
965 rtproxy.hdr.opcode = AFE_SERVICE_CMD_REG_RTPORT;
966 rtproxy.port_id = port_id;
967 rtproxy.rsvd = 0;
968
969 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
970 if (ret < 0) {
971 pr_err("%s: AFE reg. rtproxy_event failed %d\n",
972 __func__, ret);
973 ret = -EINVAL;
974 return ret;
975 }
976 return 0;
977}
978
979int afe_unregister_get_events(u16 port_id)
980{
981 int ret = 0;
982 struct afe_cmd_unreg_rtport rtproxy;
983
984 pr_debug("%s:\n", __func__);
985
986 if (this_afe.apr == NULL) {
987 this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
988 0xFFFFFFFF, &this_afe);
989 pr_debug("%s: Register AFE\n", __func__);
990 if (this_afe.apr == NULL) {
991 pr_err("%s: Unable to register AFE\n", __func__);
992 ret = -ENODEV;
993 return ret;
994 }
995 }
996 if ((port_id == RT_PROXY_DAI_002_RX) ||
997 (port_id == RT_PROXY_DAI_001_TX))
998 port_id = VIRTUAL_ID_TO_PORTID(port_id);
999 else
1000 return -EINVAL;
1001
1002 rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1003 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1004 rtproxy.hdr.pkt_size = sizeof(rtproxy);
1005 rtproxy.hdr.src_port = 0;
1006 rtproxy.hdr.dest_port = 0;
1007 rtproxy.hdr.token = 0;
1008 rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREG_RTPORT;
1009 rtproxy.port_id = port_id;
1010 rtproxy.rsvd = 0;
1011
1012 if (port_id == RT_PROXY_PORT_001_TX) {
1013 this_afe.tx_cb = NULL;
1014 this_afe.tx_private_data = NULL;
1015 } else if (port_id == RT_PROXY_PORT_001_RX) {
1016 this_afe.rx_cb = NULL;
1017 this_afe.rx_private_data = NULL;
1018 }
1019
1020 atomic_set(&this_afe.state, 1);
1021 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
1022 if (ret < 0) {
1023 pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
1024 __func__, ret);
1025 ret = -EINVAL;
1026 return ret;
1027 }
1028
1029 ret = wait_event_timeout(this_afe.wait,
1030 (atomic_read(&this_afe.state) == 0),
1031 msecs_to_jiffies(TIMEOUT_MS));
1032 if (!ret) {
1033 pr_err("%s: wait_event timeout\n", __func__);
1034 ret = -EINVAL;
1035 return ret;
1036 }
1037 return 0;
1038}
1039
1040int afe_rt_proxy_port_write(u32 buf_addr_p, int bytes)
1041{
1042 int ret = 0;
1043 struct afe_cmd_rtport_wr afecmd_wr;
1044
1045 if (this_afe.apr == NULL) {
1046 pr_err("%s:register to AFE is not done\n", __func__);
1047 ret = -ENODEV;
1048 return ret;
1049 }
1050 pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
1051 buf_addr_p, bytes);
1052
1053 afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1054 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1055 afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
1056 afecmd_wr.hdr.src_port = 0;
1057 afecmd_wr.hdr.dest_port = 0;
1058 afecmd_wr.hdr.token = 0;
1059 afecmd_wr.hdr.opcode = AFE_SERVICE_CMD_RTPORT_WR;
1060 afecmd_wr.buf_addr = (uint32_t)buf_addr_p;
1061 afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
1062 afecmd_wr.bytes_avail = bytes;
1063 afecmd_wr.rsvd = 0;
1064
1065 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_wr);
1066 if (ret < 0) {
1067 pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
1068 __func__, afecmd_wr.port_id, ret);
1069 ret = -EINVAL;
1070 return ret;
1071 }
1072 return 0;
1073
1074}
1075
1076int afe_rt_proxy_port_read(u32 buf_addr_p, int bytes)
1077{
1078 int ret = 0;
1079 struct afe_cmd_rtport_rd afecmd_rd;
1080
1081 if (this_afe.apr == NULL) {
1082 pr_err("%s: register to AFE is not done\n", __func__);
1083 ret = -ENODEV;
1084 return ret;
1085 }
1086 pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
1087 buf_addr_p, bytes);
1088
1089 afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1090 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1091 afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
1092 afecmd_rd.hdr.src_port = 0;
1093 afecmd_rd.hdr.dest_port = 0;
1094 afecmd_rd.hdr.token = 0;
1095 afecmd_rd.hdr.opcode = AFE_SERVICE_CMD_RTPORT_RD;
1096 afecmd_rd.buf_addr = (uint32_t)buf_addr_p;
1097 afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
1098 afecmd_rd.bytes_avail = bytes;
1099 afecmd_rd.rsvd = 0;
1100
1101 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_rd);
1102 if (ret < 0) {
1103 pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
1104 __func__, afecmd_rd.port_id, ret);
1105 ret = -EINVAL;
1106 return ret;
1107 }
1108 return 0;
1109}
1110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111#ifdef CONFIG_DEBUG_FS
1112static struct dentry *debugfs_afelb;
1113static struct dentry *debugfs_afelb_gain;
1114
1115static int afe_debug_open(struct inode *inode, struct file *file)
1116{
1117 file->private_data = inode->i_private;
1118 pr_info("debug intf %s\n", (char *) file->private_data);
1119 return 0;
1120}
1121
1122static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
1123{
1124 char *token;
1125 int base, cnt;
1126
1127 token = strsep(&buf, " ");
1128
1129 for (cnt = 0; cnt < num_of_par; cnt++) {
1130 if (token != NULL) {
1131 if ((token[1] == 'x') || (token[1] == 'X'))
1132 base = 16;
1133 else
1134 base = 10;
1135
1136 if (strict_strtoul(token, base, &param1[cnt]) != 0)
1137 return -EINVAL;
1138
1139 token = strsep(&buf, " ");
1140 } else
1141 return -EINVAL;
1142 }
1143 return 0;
1144}
1145#define AFE_LOOPBACK_ON (1)
1146#define AFE_LOOPBACK_OFF (0)
1147static ssize_t afe_debug_write(struct file *filp,
1148 const char __user *ubuf, size_t cnt, loff_t *ppos)
1149{
1150 char *lb_str = filp->private_data;
1151 char lbuf[32];
1152 int rc;
1153 unsigned long param[5];
1154
1155 if (cnt > sizeof(lbuf) - 1)
1156 return -EINVAL;
1157
1158 rc = copy_from_user(lbuf, ubuf, cnt);
1159 if (rc)
1160 return -EFAULT;
1161
1162 lbuf[cnt] = '\0';
1163
1164 if (!strcmp(lb_str, "afe_loopback")) {
1165 rc = afe_get_parameters(lbuf, param, 3);
1166 if (!rc) {
1167 pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
1168 param[2]);
1169
1170 if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
1171 AFE_LOOPBACK_OFF)) {
1172 pr_err("%s: Error, parameter 0 incorrect\n",
1173 __func__);
1174 rc = -EINVAL;
1175 goto afe_error;
1176 }
1177 if ((afe_validate_port(param[1]) < 0) ||
1178 (afe_validate_port(param[2])) < 0) {
1179 pr_err("%s: Error, invalid afe port\n",
1180 __func__);
1181 }
1182 if (this_afe.apr == NULL) {
1183 pr_err("%s: Error, AFE not opened\n", __func__);
1184 rc = -EINVAL;
1185 } else {
1186 rc = afe_loopback(param[0], param[1], param[2]);
1187 }
1188 } else {
1189 pr_err("%s: Error, invalid parameters\n", __func__);
1190 rc = -EINVAL;
1191 }
1192
1193 } else if (!strcmp(lb_str, "afe_loopback_gain")) {
1194 rc = afe_get_parameters(lbuf, param, 2);
1195 if (!rc) {
1196 pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
1197
1198 if (afe_validate_port(param[0]) < 0) {
1199 pr_err("%s: Error, invalid afe port\n",
1200 __func__);
1201 rc = -EINVAL;
1202 goto afe_error;
1203 }
1204
1205 if (param[1] < 0 || param[1] > 100) {
1206 pr_err("%s: Error, volume shoud be 0 to 100"
1207 " percentage param = %lu\n",
1208 __func__, param[1]);
1209 rc = -EINVAL;
1210 goto afe_error;
1211 }
1212
1213 param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
1214
1215 if (this_afe.apr == NULL) {
1216 pr_err("%s: Error, AFE not opened\n", __func__);
1217 rc = -EINVAL;
1218 } else {
1219 rc = afe_loopback_gain(param[0], param[1]);
1220 }
1221 } else {
1222 pr_err("%s: Error, invalid parameters\n", __func__);
1223 rc = -EINVAL;
1224 }
1225 }
1226
1227afe_error:
1228 if (rc == 0)
1229 rc = cnt;
1230 else
1231 pr_err("%s: rc = %d\n", __func__, rc);
1232
1233 return rc;
1234}
1235
1236static const struct file_operations afe_debug_fops = {
1237 .open = afe_debug_open,
1238 .write = afe_debug_write
1239};
1240#endif
1241int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
1242{
1243 struct afe_port_sidetone_command cmd_sidetone;
1244 int ret = 0;
1245
1246 pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
1247 tx_port_id, rx_port_id, enable, gain);
1248 cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1249 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1250 cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
1251 cmd_sidetone.hdr.src_port = 0;
1252 cmd_sidetone.hdr.dest_port = 0;
1253 cmd_sidetone.hdr.token = 0;
1254 cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL;
1255 cmd_sidetone.tx_port_id = tx_port_id;
1256 cmd_sidetone.rx_port_id = rx_port_id;
1257 cmd_sidetone.gain = gain;
1258 cmd_sidetone.enable = enable;
1259
1260 atomic_set(&this_afe.state, 1);
1261 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone);
1262 if (ret < 0) {
1263 pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
1264 __func__, tx_port_id, rx_port_id);
1265 ret = -EINVAL;
1266 goto fail_cmd;
1267 }
1268
1269 ret = wait_event_timeout(this_afe.wait,
1270 (atomic_read(&this_afe.state) == 0),
1271 msecs_to_jiffies(TIMEOUT_MS));
1272 if (ret < 0) {
1273 pr_err("%s: wait_event timeout\n", __func__);
1274 ret = -EINVAL;
1275 goto fail_cmd;
1276 }
1277 return 0;
1278fail_cmd:
1279 return ret;
1280}
1281
1282int afe_port_stop_nowait(int port_id)
1283{
1284 struct afe_port_stop_command stop;
1285 int ret = 0;
1286
1287 if (this_afe.apr == NULL) {
1288 pr_err("AFE is already closed\n");
1289 ret = -EINVAL;
1290 goto fail_cmd;
1291 }
1292 pr_info("%s: port_id=%d\n", __func__, port_id);
Laxminath Kasam32657ec2011-08-01 19:26:57 +05301293 port_id = afe_convert_virtual_to_portid(port_id);
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1296 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1297 stop.hdr.pkt_size = sizeof(stop);
1298 stop.hdr.src_port = 0;
1299 stop.hdr.dest_port = 0;
1300 stop.hdr.token = 0;
1301 stop.hdr.opcode = AFE_PORT_CMD_STOP;
1302 stop.port_id = port_id;
1303 stop.reserved = 0;
1304
1305 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
1306
1307 if (ret == -ENETRESET) {
1308 pr_info("%s: Need to reset, calling APR deregister", __func__);
1309 return apr_deregister(this_afe.apr);
1310 } else if (IS_ERR_VALUE(ret)) {
1311 pr_err("%s: AFE close failed\n", __func__);
1312 ret = -EINVAL;
1313 }
1314
1315fail_cmd:
1316 return ret;
1317
1318}
1319
1320int afe_close(int port_id)
1321{
1322 struct afe_port_stop_command stop;
1323 int ret = 0;
1324
1325 if (this_afe.apr == NULL) {
1326 pr_err("AFE is already closed\n");
1327 ret = -EINVAL;
1328 goto fail_cmd;
1329 }
1330 pr_info("%s: port_id=%d\n", __func__, port_id);
Laxminath Kasam32657ec2011-08-01 19:26:57 +05301331 port_id = afe_convert_virtual_to_portid(port_id);
1332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
1334 APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
1335 stop.hdr.pkt_size = sizeof(stop);
1336 stop.hdr.src_port = 0;
1337 stop.hdr.dest_port = 0;
1338 stop.hdr.token = 0;
1339 stop.hdr.opcode = AFE_PORT_CMD_STOP;
1340 stop.port_id = port_id;
1341 stop.reserved = 0;
1342
1343 atomic_set(&this_afe.state, 1);
1344 ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
1345
1346 if (ret == -ENETRESET) {
1347 pr_info("%s: Need to reset, calling APR deregister", __func__);
1348 return apr_deregister(this_afe.apr);
1349 }
1350
1351 if (ret < 0) {
1352 pr_err("%s: AFE close failed\n", __func__);
1353 ret = -EINVAL;
1354 goto fail_cmd;
1355 }
1356
1357 ret = wait_event_timeout(this_afe.wait,
1358 (atomic_read(&this_afe.state) == 0),
1359 msecs_to_jiffies(TIMEOUT_MS));
1360 if (!ret) {
1361 pr_err("%s: wait_event timeout\n", __func__);
1362 ret = -EINVAL;
1363 goto fail_cmd;
1364 }
1365fail_cmd:
1366 return ret;
1367}
1368
1369static int __init afe_init(void)
1370{
1371 init_waitqueue_head(&this_afe.wait);
1372 atomic_set(&this_afe.state, 0);
1373 atomic_set(&this_afe.status, 0);
1374 this_afe.apr = NULL;
1375#ifdef CONFIG_DEBUG_FS
1376 debugfs_afelb = debugfs_create_file("afe_loopback",
1377 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback",
1378 &afe_debug_fops);
1379
1380 debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
1381 S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain",
1382 &afe_debug_fops);
1383
1384
1385#endif
1386 return 0;
1387}
1388
1389static void __exit afe_exit(void)
1390{
1391#ifdef CONFIG_DEBUG_FS
1392 if (debugfs_afelb)
1393 debugfs_remove(debugfs_afelb);
1394 if (debugfs_afelb_gain)
1395 debugfs_remove(debugfs_afelb_gain);
1396#endif
1397}
1398
1399device_initcall(afe_init);
1400__exitcall(afe_exit);