q6afe.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kthread.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/wait.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/sched.h>
  19. #include <mach/qdsp6v2/audio_acdb.h>
  20. #include <sound/apr_audio.h>
  21. #include <sound/q6afe.h>
  22. struct afe_ctl {
  23. void *apr;
  24. atomic_t state;
  25. atomic_t status;
  26. wait_queue_head_t wait;
  27. struct task_struct *task;
  28. void (*tx_cb) (uint32_t opcode,
  29. uint32_t token, uint32_t *payload, void *priv);
  30. void (*rx_cb) (uint32_t opcode,
  31. uint32_t token, uint32_t *payload, void *priv);
  32. void *tx_private_data;
  33. void *rx_private_data;
  34. u16 dtmf_gen_rx_portid;
  35. };
  36. static struct afe_ctl this_afe;
  37. static struct acdb_cal_block afe_cal_addr[MAX_AUDPROC_TYPES];
  38. #define TIMEOUT_MS 1000
  39. #define Q6AFE_MAX_VOLUME 0x3FFF
  40. #define SIZEOF_CFG_CMD(y) \
  41. (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
  42. static int32_t afe_callback(struct apr_client_data *data, void *priv)
  43. {
  44. if (data->opcode == RESET_EVENTS) {
  45. pr_debug("q6afe: reset event = %d %d apr[%p]\n",
  46. data->reset_event, data->reset_proc, this_afe.apr);
  47. if (this_afe.apr) {
  48. apr_reset(this_afe.apr);
  49. atomic_set(&this_afe.state, 0);
  50. this_afe.apr = NULL;
  51. }
  52. /* send info to user */
  53. pr_debug("task_name = %s pid = %d\n",
  54. this_afe.task->comm, this_afe.task->pid);
  55. send_sig(SIGUSR1, this_afe.task, 0);
  56. return 0;
  57. }
  58. if (data->payload_size) {
  59. uint32_t *payload;
  60. uint16_t port_id = 0;
  61. payload = data->payload;
  62. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x\n",
  63. __func__, data->opcode,
  64. payload[0], payload[1]);
  65. /* payload[1] contains the error status for response */
  66. if (payload[1] != 0) {
  67. atomic_set(&this_afe.status, -1);
  68. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  69. __func__, payload[0], payload[1]);
  70. }
  71. if (data->opcode == APR_BASIC_RSP_RESULT) {
  72. switch (payload[0]) {
  73. case AFE_PORT_AUDIO_IF_CONFIG:
  74. case AFE_PORT_CMD_I2S_CONFIG:
  75. case AFE_PORT_MULTI_CHAN_HDMI_AUDIO_IF_CONFIG:
  76. case AFE_PORT_AUDIO_SLIM_SCH_CONFIG:
  77. case AFE_PORT_CMD_STOP:
  78. case AFE_PORT_CMD_START:
  79. case AFE_PORT_CMD_LOOPBACK:
  80. case AFE_PORT_CMD_SIDETONE_CTL:
  81. case AFE_PORT_CMD_SET_PARAM:
  82. case AFE_PSEUDOPORT_CMD_START:
  83. case AFE_PSEUDOPORT_CMD_STOP:
  84. case AFE_PORT_CMD_APPLY_GAIN:
  85. case AFE_SERVICE_CMD_MEMORY_MAP:
  86. case AFE_SERVICE_CMD_MEMORY_UNMAP:
  87. case AFE_SERVICE_CMD_UNREG_RTPORT:
  88. case AFE_PORTS_CMD_DTMF_CTL:
  89. atomic_set(&this_afe.state, 0);
  90. wake_up(&this_afe.wait);
  91. break;
  92. case AFE_SERVICE_CMD_REG_RTPORT:
  93. break;
  94. case AFE_SERVICE_CMD_RTPORT_WR:
  95. port_id = RT_PROXY_PORT_001_TX;
  96. break;
  97. case AFE_SERVICE_CMD_RTPORT_RD:
  98. port_id = RT_PROXY_PORT_001_RX;
  99. break;
  100. default:
  101. pr_err("Unknown cmd 0x%x\n",
  102. payload[0]);
  103. break;
  104. }
  105. } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
  106. port_id = (uint16_t)(0x0000FFFF & payload[0]);
  107. }
  108. pr_debug("%s:port_id = %x\n", __func__, port_id);
  109. switch (port_id) {
  110. case RT_PROXY_PORT_001_TX: {
  111. if (this_afe.tx_cb) {
  112. this_afe.tx_cb(data->opcode, data->token,
  113. data->payload,
  114. this_afe.tx_private_data);
  115. }
  116. break;
  117. }
  118. case RT_PROXY_PORT_001_RX: {
  119. if (this_afe.rx_cb) {
  120. this_afe.rx_cb(data->opcode, data->token,
  121. data->payload,
  122. this_afe.rx_private_data);
  123. }
  124. break;
  125. }
  126. default:
  127. break;
  128. }
  129. }
  130. return 0;
  131. }
  132. int afe_get_port_type(u16 port_id)
  133. {
  134. int ret;
  135. switch (port_id) {
  136. case PRIMARY_I2S_RX:
  137. case PCM_RX:
  138. case SECONDARY_PCM_RX:
  139. case SECONDARY_I2S_RX:
  140. case MI2S_RX:
  141. case HDMI_RX:
  142. case SLIMBUS_0_RX:
  143. case SLIMBUS_1_RX:
  144. case SLIMBUS_2_RX:
  145. case SLIMBUS_3_RX:
  146. case INT_BT_SCO_RX:
  147. case INT_BT_A2DP_RX:
  148. case INT_FM_RX:
  149. case VOICE_PLAYBACK_TX:
  150. case RT_PROXY_PORT_001_RX:
  151. case SLIMBUS_4_RX:
  152. case PSEUDOPORT_01:
  153. ret = MSM_AFE_PORT_TYPE_RX;
  154. break;
  155. case PRIMARY_I2S_TX:
  156. case PCM_TX:
  157. case SECONDARY_PCM_TX:
  158. case SECONDARY_I2S_TX:
  159. case MI2S_TX:
  160. case DIGI_MIC_TX:
  161. case VOICE_RECORD_TX:
  162. case SLIMBUS_0_TX:
  163. case SLIMBUS_1_TX:
  164. case SLIMBUS_2_TX:
  165. case SLIMBUS_3_TX:
  166. case INT_FM_TX:
  167. case VOICE_RECORD_RX:
  168. case INT_BT_SCO_TX:
  169. case RT_PROXY_PORT_001_TX:
  170. case SLIMBUS_4_TX:
  171. ret = MSM_AFE_PORT_TYPE_TX;
  172. break;
  173. default:
  174. pr_err("%s: invalid port id %d\n", __func__, port_id);
  175. ret = -EINVAL;
  176. }
  177. return ret;
  178. }
  179. int afe_validate_port(u16 port_id)
  180. {
  181. int ret;
  182. switch (port_id) {
  183. case PRIMARY_I2S_RX:
  184. case PRIMARY_I2S_TX:
  185. case PCM_RX:
  186. case PCM_TX:
  187. case SECONDARY_PCM_RX:
  188. case SECONDARY_PCM_TX:
  189. case SECONDARY_I2S_RX:
  190. case SECONDARY_I2S_TX:
  191. case MI2S_RX:
  192. case MI2S_TX:
  193. case HDMI_RX:
  194. case RSVD_2:
  195. case RSVD_3:
  196. case DIGI_MIC_TX:
  197. case VOICE_RECORD_RX:
  198. case VOICE_RECORD_TX:
  199. case VOICE_PLAYBACK_TX:
  200. case SLIMBUS_0_RX:
  201. case SLIMBUS_0_TX:
  202. case SLIMBUS_1_RX:
  203. case SLIMBUS_1_TX:
  204. case SLIMBUS_2_RX:
  205. case SLIMBUS_2_TX:
  206. case SLIMBUS_3_RX:
  207. case SLIMBUS_3_TX:
  208. case INT_BT_SCO_RX:
  209. case INT_BT_SCO_TX:
  210. case INT_BT_A2DP_RX:
  211. case INT_FM_RX:
  212. case INT_FM_TX:
  213. case RT_PROXY_PORT_001_RX:
  214. case RT_PROXY_PORT_001_TX:
  215. case SLIMBUS_4_RX:
  216. case SLIMBUS_4_TX:
  217. case PSEUDOPORT_01:
  218. {
  219. ret = 0;
  220. break;
  221. }
  222. default:
  223. ret = -EINVAL;
  224. }
  225. return ret;
  226. }
  227. int afe_convert_virtual_to_portid(u16 port_id)
  228. {
  229. int ret;
  230. /* if port_id is virtual, convert to physical..
  231. * if port_id is already physical, return physical
  232. */
  233. if (afe_validate_port(port_id) < 0) {
  234. if (port_id == RT_PROXY_DAI_001_RX ||
  235. port_id == RT_PROXY_DAI_001_TX ||
  236. port_id == RT_PROXY_DAI_002_RX ||
  237. port_id == RT_PROXY_DAI_002_TX)
  238. ret = VIRTUAL_ID_TO_PORTID(port_id);
  239. else
  240. ret = -EINVAL;
  241. } else
  242. ret = port_id;
  243. return ret;
  244. }
  245. int afe_get_port_index(u16 port_id)
  246. {
  247. switch (port_id) {
  248. case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
  249. case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
  250. case PCM_RX: return IDX_PCM_RX;
  251. case PCM_TX: return IDX_PCM_TX;
  252. case SECONDARY_PCM_RX: return IDX_SECONDARY_PCM_RX;
  253. case SECONDARY_PCM_TX: return IDX_SECONDARY_PCM_TX;
  254. case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
  255. case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
  256. case MI2S_RX: return IDX_MI2S_RX;
  257. case MI2S_TX: return IDX_MI2S_TX;
  258. case HDMI_RX: return IDX_HDMI_RX;
  259. case RSVD_2: return IDX_RSVD_2;
  260. case RSVD_3: return IDX_RSVD_3;
  261. case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
  262. case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
  263. case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
  264. case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
  265. case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
  266. case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
  267. case SLIMBUS_1_RX: return IDX_SLIMBUS_1_RX;
  268. case SLIMBUS_1_TX: return IDX_SLIMBUS_1_TX;
  269. case SLIMBUS_2_RX: return IDX_SLIMBUS_2_RX;
  270. case SLIMBUS_2_TX: return IDX_SLIMBUS_2_TX;
  271. case SLIMBUS_3_RX: return IDX_SLIMBUS_3_RX;
  272. case SLIMBUS_3_TX: return IDX_SLIMBUS_3_TX;
  273. case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
  274. case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
  275. case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
  276. case INT_FM_RX: return IDX_INT_FM_RX;
  277. case INT_FM_TX: return IDX_INT_FM_TX;
  278. case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
  279. case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
  280. case SLIMBUS_4_RX: return IDX_SLIMBUS_4_RX;
  281. case SLIMBUS_4_TX: return IDX_SLIMBUS_4_TX;
  282. case PSEUDOPORT_01: return IDX_PSEUDOPORT_01;
  283. default: return -EINVAL;
  284. }
  285. }
  286. int afe_sizeof_cfg_cmd(u16 port_id)
  287. {
  288. int ret_size;
  289. switch (port_id) {
  290. case PRIMARY_I2S_RX:
  291. case PRIMARY_I2S_TX:
  292. case SECONDARY_I2S_RX:
  293. case SECONDARY_I2S_TX:
  294. case MI2S_RX:
  295. case MI2S_TX:
  296. ret_size = SIZEOF_CFG_CMD(afe_port_mi2s_cfg);
  297. break;
  298. case HDMI_RX:
  299. ret_size = SIZEOF_CFG_CMD(afe_port_hdmi_multi_ch_cfg);
  300. break;
  301. case SLIMBUS_0_RX:
  302. case SLIMBUS_0_TX:
  303. case SLIMBUS_1_RX:
  304. case SLIMBUS_1_TX:
  305. case SLIMBUS_2_RX:
  306. case SLIMBUS_2_TX:
  307. case SLIMBUS_3_RX:
  308. case SLIMBUS_3_TX:
  309. case SLIMBUS_4_RX:
  310. case SLIMBUS_4_TX:
  311. ret_size = SIZEOF_CFG_CMD(afe_port_slimbus_sch_cfg);
  312. break;
  313. case RT_PROXY_PORT_001_RX:
  314. case RT_PROXY_PORT_001_TX:
  315. ret_size = SIZEOF_CFG_CMD(afe_port_rtproxy_cfg);
  316. break;
  317. case PSEUDOPORT_01:
  318. ret_size = SIZEOF_CFG_CMD(afe_port_pseudo_cfg);
  319. break;
  320. case PCM_RX:
  321. case PCM_TX:
  322. case SECONDARY_PCM_RX:
  323. case SECONDARY_PCM_TX:
  324. default:
  325. ret_size = SIZEOF_CFG_CMD(afe_port_pcm_cfg);
  326. break;
  327. }
  328. return ret_size;
  329. }
  330. int afe_q6_interface_prepare(void)
  331. {
  332. int ret = 0;
  333. pr_debug("%s:", __func__);
  334. if (this_afe.apr == NULL) {
  335. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  336. 0xFFFFFFFF, &this_afe);
  337. pr_debug("%s: Register AFE\n", __func__);
  338. if (this_afe.apr == NULL) {
  339. pr_err("%s: Unable to register AFE\n", __func__);
  340. ret = -ENODEV;
  341. }
  342. }
  343. return ret;
  344. }
  345. static void afe_send_cal_block(int32_t path, u16 port_id)
  346. {
  347. int result = 0;
  348. struct acdb_cal_block cal_block;
  349. struct afe_port_cmd_set_param_no_payload afe_cal;
  350. pr_debug("%s: path %d\n", __func__, path);
  351. get_afe_cal(path, &cal_block);
  352. if (cal_block.cal_size <= 0) {
  353. pr_debug("%s: No AFE cal to send!\n", __func__);
  354. goto done;
  355. }
  356. if ((afe_cal_addr[path].cal_paddr != cal_block.cal_paddr) ||
  357. (cal_block.cal_size > afe_cal_addr[path].cal_size)) {
  358. if (afe_cal_addr[path].cal_paddr != 0)
  359. afe_cmd_memory_unmap(
  360. afe_cal_addr[path].cal_paddr);
  361. afe_cmd_memory_map(cal_block.cal_paddr, cal_block.cal_size);
  362. afe_cal_addr[path].cal_paddr = cal_block.cal_paddr;
  363. afe_cal_addr[path].cal_size = cal_block.cal_size;
  364. }
  365. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  366. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  367. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  368. afe_cal.hdr.src_port = 0;
  369. afe_cal.hdr.dest_port = 0;
  370. afe_cal.hdr.token = 0;
  371. afe_cal.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
  372. afe_cal.port_id = port_id;
  373. afe_cal.payload_size = cal_block.cal_size;
  374. afe_cal.payload_address = cal_block.cal_paddr;
  375. pr_debug("%s: AFE cal sent for device port = %d, path = %d, "
  376. "cal size = %d, cal addr = 0x%x\n", __func__,
  377. port_id, path, cal_block.cal_size, cal_block.cal_paddr);
  378. atomic_set(&this_afe.state, 1);
  379. result = apr_send_pkt(this_afe.apr, (uint32_t *) &afe_cal);
  380. if (result < 0) {
  381. pr_err("%s: AFE cal for port %d failed\n",
  382. __func__, port_id);
  383. }
  384. result = wait_event_timeout(this_afe.wait,
  385. (atomic_read(&this_afe.state) == 0),
  386. msecs_to_jiffies(TIMEOUT_MS));
  387. if (!result) {
  388. pr_err("%s: wait_event timeout SET AFE CAL\n", __func__);
  389. goto done;
  390. }
  391. pr_debug("%s: AFE cal sent for path %d device!\n", __func__, path);
  392. done:
  393. return;
  394. }
  395. void afe_send_cal(u16 port_id)
  396. {
  397. pr_debug("%s\n", __func__);
  398. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX)
  399. afe_send_cal_block(TX_CAL, port_id);
  400. else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX)
  401. afe_send_cal_block(RX_CAL, port_id);
  402. }
  403. /* This function sends multi-channel HDMI configuration command and AFE
  404. * calibration which is only supported by QDSP6 on 8960 and onward.
  405. */
  406. int afe_port_start(u16 port_id, union afe_port_config *afe_config,
  407. u32 rate)
  408. {
  409. struct afe_port_start_command start;
  410. struct afe_audioif_config_command config;
  411. int ret;
  412. if (!afe_config) {
  413. pr_err("%s: Error, no configuration data\n", __func__);
  414. ret = -EINVAL;
  415. return ret;
  416. }
  417. pr_debug("%s: %d %d\n", __func__, port_id, rate);
  418. if ((port_id == RT_PROXY_DAI_001_RX) ||
  419. (port_id == RT_PROXY_DAI_002_TX))
  420. return 0;
  421. if ((port_id == RT_PROXY_DAI_002_RX) ||
  422. (port_id == RT_PROXY_DAI_001_TX))
  423. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  424. ret = afe_q6_interface_prepare();
  425. if (IS_ERR_VALUE(ret))
  426. return ret;
  427. if (port_id == HDMI_RX) {
  428. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  429. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  430. config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
  431. config.hdr.src_port = 0;
  432. config.hdr.dest_port = 0;
  433. config.hdr.token = 0;
  434. config.hdr.opcode = AFE_PORT_MULTI_CHAN_HDMI_AUDIO_IF_CONFIG;
  435. } else {
  436. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  437. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  438. config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
  439. config.hdr.src_port = 0;
  440. config.hdr.dest_port = 0;
  441. config.hdr.token = 0;
  442. switch (port_id) {
  443. case SLIMBUS_0_RX:
  444. case SLIMBUS_0_TX:
  445. case SLIMBUS_1_RX:
  446. case SLIMBUS_1_TX:
  447. case SLIMBUS_2_RX:
  448. case SLIMBUS_2_TX:
  449. case SLIMBUS_3_RX:
  450. case SLIMBUS_3_TX:
  451. case SLIMBUS_4_RX:
  452. case SLIMBUS_4_TX:
  453. config.hdr.opcode = AFE_PORT_AUDIO_SLIM_SCH_CONFIG;
  454. break;
  455. case MI2S_TX:
  456. case MI2S_RX:
  457. case SECONDARY_I2S_RX:
  458. case SECONDARY_I2S_TX:
  459. case PRIMARY_I2S_RX:
  460. case PRIMARY_I2S_TX:
  461. /* AFE_PORT_CMD_I2S_CONFIG command is not supported
  462. * in the LPASS EL 1.0. So we have to distiguish
  463. * which AFE command, AFE_PORT_CMD_I2S_CONFIG or
  464. * AFE_PORT_AUDIO_IF_CONFIG to use. If the format
  465. * is L-PCM, the AFE_PORT_AUDIO_IF_CONFIG is used
  466. * to make the backward compatible.
  467. */
  468. pr_debug("%s: afe_config->mi2s.format = %d\n", __func__,
  469. afe_config->mi2s.format);
  470. if (afe_config->mi2s.format == MSM_AFE_I2S_FORMAT_LPCM)
  471. config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
  472. else
  473. config.hdr.opcode = AFE_PORT_CMD_I2S_CONFIG;
  474. break;
  475. case PSEUDOPORT_01:
  476. config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
  477. pr_debug("%s, config, opcode=%x\n", __func__,
  478. config.hdr.opcode);
  479. break;
  480. default:
  481. config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
  482. break;
  483. }
  484. }
  485. if (afe_validate_port(port_id) < 0) {
  486. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  487. port_id);
  488. ret = -EINVAL;
  489. goto fail_cmd;
  490. }
  491. config.port_id = port_id;
  492. config.port = *afe_config;
  493. atomic_set(&this_afe.state, 1);
  494. atomic_set(&this_afe.status, 0);
  495. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  496. if (ret < 0) {
  497. pr_err("%s: AFE enable for port %d failed\n", __func__,
  498. port_id);
  499. ret = -EINVAL;
  500. goto fail_cmd;
  501. }
  502. ret = wait_event_timeout(this_afe.wait,
  503. (atomic_read(&this_afe.state) == 0),
  504. msecs_to_jiffies(TIMEOUT_MS));
  505. if (!ret) {
  506. pr_err("%s: wait_event timeout IF CONFIG\n", __func__);
  507. ret = -EINVAL;
  508. goto fail_cmd;
  509. }
  510. if (atomic_read(&this_afe.status) != 0) {
  511. pr_err("%s: config cmd failed\n", __func__);
  512. ret = -EINVAL;
  513. goto fail_cmd;
  514. }
  515. /* send AFE cal */
  516. afe_send_cal(port_id);
  517. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  518. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  519. start.hdr.pkt_size = sizeof(start);
  520. start.hdr.src_port = 0;
  521. start.hdr.dest_port = 0;
  522. start.hdr.token = 0;
  523. start.hdr.opcode = AFE_PORT_CMD_START;
  524. start.port_id = port_id;
  525. start.gain = 0x2000;
  526. start.sample_rate = rate;
  527. atomic_set(&this_afe.state, 1);
  528. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
  529. if (IS_ERR_VALUE(ret)) {
  530. pr_err("%s: AFE enable for port %d failed\n", __func__,
  531. port_id);
  532. ret = -EINVAL;
  533. goto fail_cmd;
  534. }
  535. ret = wait_event_timeout(this_afe.wait,
  536. (atomic_read(&this_afe.state) == 0),
  537. msecs_to_jiffies(TIMEOUT_MS));
  538. if (!ret) {
  539. pr_err("%s: wait_event timeout PORT START\n", __func__);
  540. ret = -EINVAL;
  541. goto fail_cmd;
  542. }
  543. if (this_afe.task != current)
  544. this_afe.task = current;
  545. pr_debug("task_name = %s pid = %d\n",
  546. this_afe.task->comm, this_afe.task->pid);
  547. return 0;
  548. fail_cmd:
  549. return ret;
  550. }
  551. /* This function should be used by 8660 exclusively */
  552. int afe_open(u16 port_id, union afe_port_config *afe_config, int rate)
  553. {
  554. struct afe_port_start_command start;
  555. struct afe_audioif_config_command config;
  556. int ret = 0;
  557. if (!afe_config) {
  558. pr_err("%s: Error, no configuration data\n", __func__);
  559. ret = -EINVAL;
  560. return ret;
  561. }
  562. pr_debug("%s: %d %d\n", __func__, port_id, rate);
  563. if ((port_id == RT_PROXY_DAI_001_RX) ||
  564. (port_id == RT_PROXY_DAI_002_TX))
  565. return 0;
  566. if ((port_id == RT_PROXY_DAI_002_RX) ||
  567. (port_id == RT_PROXY_DAI_001_TX))
  568. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  569. ret = afe_q6_interface_prepare();
  570. if (ret != 0)
  571. return ret;
  572. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  573. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  574. config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id);
  575. config.hdr.src_port = 0;
  576. config.hdr.dest_port = 0;
  577. config.hdr.token = 0;
  578. switch (port_id) {
  579. case SLIMBUS_0_RX:
  580. case SLIMBUS_0_TX:
  581. case SLIMBUS_1_RX:
  582. case SLIMBUS_1_TX:
  583. case SLIMBUS_2_RX:
  584. case SLIMBUS_2_TX:
  585. case SLIMBUS_3_RX:
  586. case SLIMBUS_3_TX:
  587. case SLIMBUS_4_RX:
  588. case SLIMBUS_4_TX:
  589. config.hdr.opcode = AFE_PORT_AUDIO_SLIM_SCH_CONFIG;
  590. break;
  591. case MI2S_TX:
  592. case MI2S_RX:
  593. case SECONDARY_I2S_RX:
  594. case SECONDARY_I2S_TX:
  595. case PRIMARY_I2S_RX:
  596. case PRIMARY_I2S_TX:
  597. /* AFE_PORT_CMD_I2S_CONFIG command is not supported
  598. * in the LPASS EL 1.0. So we have to distiguish
  599. * which AFE command, AFE_PORT_CMD_I2S_CONFIG or
  600. * AFE_PORT_AUDIO_IF_CONFIG to use. If the format
  601. * is L-PCM, the AFE_PORT_AUDIO_IF_CONFIG is used
  602. * to make the backward compatible.
  603. */
  604. pr_debug("%s: afe_config->mi2s.format = %d\n", __func__,
  605. afe_config->mi2s.format);
  606. if (afe_config->mi2s.format == MSM_AFE_I2S_FORMAT_LPCM)
  607. config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
  608. else
  609. config.hdr.opcode = AFE_PORT_CMD_I2S_CONFIG;
  610. break;
  611. default:
  612. config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG;
  613. break;
  614. }
  615. if (afe_validate_port(port_id) < 0) {
  616. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  617. port_id);
  618. ret = -EINVAL;
  619. goto fail_cmd;
  620. }
  621. config.port_id = port_id;
  622. config.port = *afe_config;
  623. atomic_set(&this_afe.state, 1);
  624. atomic_set(&this_afe.status, 0);
  625. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  626. if (ret < 0) {
  627. pr_err("%s: AFE enable for port %d failed\n", __func__,
  628. port_id);
  629. ret = -EINVAL;
  630. goto fail_cmd;
  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. goto fail_cmd;
  639. }
  640. if (atomic_read(&this_afe.status) != 0) {
  641. pr_err("%s: config cmd failed\n", __func__);
  642. ret = -EINVAL;
  643. goto fail_cmd;
  644. }
  645. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  646. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  647. start.hdr.pkt_size = sizeof(start);
  648. start.hdr.src_port = 0;
  649. start.hdr.dest_port = 0;
  650. start.hdr.token = 0;
  651. start.hdr.opcode = AFE_PORT_CMD_START;
  652. start.port_id = port_id;
  653. start.gain = 0x2000;
  654. start.sample_rate = rate;
  655. atomic_set(&this_afe.state, 1);
  656. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
  657. if (ret < 0) {
  658. pr_err("%s: AFE enable for port %d failed\n", __func__,
  659. port_id);
  660. ret = -EINVAL;
  661. goto fail_cmd;
  662. }
  663. ret = wait_event_timeout(this_afe.wait,
  664. (atomic_read(&this_afe.state) == 0),
  665. msecs_to_jiffies(TIMEOUT_MS));
  666. if (!ret) {
  667. pr_err("%s: wait_event timeout\n", __func__);
  668. ret = -EINVAL;
  669. goto fail_cmd;
  670. }
  671. if (this_afe.task != current)
  672. this_afe.task = current;
  673. pr_debug("task_name = %s pid = %d\n",
  674. this_afe.task->comm, this_afe.task->pid);
  675. return 0;
  676. fail_cmd:
  677. return ret;
  678. }
  679. int afe_loopback(u16 enable, u16 dst_port, u16 src_port)
  680. {
  681. struct afe_loopback_command lb_cmd;
  682. int ret = 0;
  683. ret = afe_q6_interface_prepare();
  684. if (ret != 0)
  685. return ret;
  686. if ((afe_get_port_type(dst_port) == MSM_AFE_PORT_TYPE_RX) &&
  687. (afe_get_port_type(src_port) == MSM_AFE_PORT_TYPE_RX))
  688. return afe_loopback_cfg(enable, dst_port, src_port,
  689. LB_MODE_EC_REF_VOICE_AUDIO);
  690. lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  691. APR_HDR_LEN(20), APR_PKT_VER);
  692. lb_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  693. sizeof(lb_cmd) - APR_HDR_SIZE);
  694. lb_cmd.hdr.src_port = 0;
  695. lb_cmd.hdr.dest_port = 0;
  696. lb_cmd.hdr.token = 0;
  697. lb_cmd.hdr.opcode = AFE_PORT_CMD_LOOPBACK;
  698. lb_cmd.tx_port_id = src_port;
  699. lb_cmd.rx_port_id = dst_port;
  700. lb_cmd.mode = 0xFFFF;
  701. lb_cmd.enable = (enable ? 1 : 0);
  702. atomic_set(&this_afe.state, 1);
  703. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &lb_cmd);
  704. if (ret < 0) {
  705. pr_err("%s: AFE loopback failed\n", __func__);
  706. ret = -EINVAL;
  707. goto done;
  708. }
  709. ret = wait_event_timeout(this_afe.wait,
  710. (atomic_read(&this_afe.state) == 0),
  711. msecs_to_jiffies(TIMEOUT_MS));
  712. if (!ret) {
  713. pr_err("%s: wait_event timeout\n", __func__);
  714. ret = -EINVAL;
  715. }
  716. done:
  717. return ret;
  718. }
  719. int afe_loopback_cfg(u16 enable, u16 dst_port, u16 src_port, u16 mode)
  720. {
  721. struct afe_port_cmd_set_param lp_cfg;
  722. int ret = 0;
  723. ret = afe_q6_interface_prepare();
  724. if (ret != 0)
  725. return ret;
  726. pr_debug("%s: src_port %d, dst_port %d\n",
  727. __func__, src_port, dst_port);
  728. lp_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  729. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  730. lp_cfg.hdr.pkt_size = sizeof(lp_cfg);
  731. lp_cfg.hdr.src_port = 0;
  732. lp_cfg.hdr.dest_port = 0;
  733. lp_cfg.hdr.token = 0;
  734. lp_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
  735. lp_cfg.port_id = src_port;
  736. lp_cfg.payload_size = sizeof(struct afe_param_payload_base) +
  737. sizeof(struct afe_param_loopback_cfg);
  738. lp_cfg.payload_address = 0;
  739. lp_cfg.payload.base.module_id = AFE_MODULE_LOOPBACK;
  740. lp_cfg.payload.base.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  741. lp_cfg.payload.base.param_size = sizeof(struct afe_param_loopback_cfg);
  742. lp_cfg.payload.base.reserved = 0;
  743. lp_cfg.payload.param.loopback_cfg.loopback_cfg_minor_version =
  744. AFE_API_VERSION_LOOPBACK_CONFIG;
  745. lp_cfg.payload.param.loopback_cfg.dst_port_id = dst_port;
  746. lp_cfg.payload.param.loopback_cfg.routing_mode = mode;
  747. lp_cfg.payload.param.loopback_cfg.enable = enable;
  748. lp_cfg.payload.param.loopback_cfg.reserved = 0;
  749. atomic_set(&this_afe.state, 1);
  750. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &lp_cfg);
  751. if (ret < 0) {
  752. pr_err("%s: AFE loopback config failed for src_port %d, dst_port %d\n",
  753. __func__, src_port, dst_port);
  754. ret = -EINVAL;
  755. goto fail_cmd;
  756. }
  757. ret = wait_event_timeout(this_afe.wait,
  758. (atomic_read(&this_afe.state) == 0),
  759. msecs_to_jiffies(TIMEOUT_MS));
  760. if (!ret) {
  761. pr_err("%s: wait_event timeout\n", __func__);
  762. ret = -EINVAL;
  763. goto fail_cmd;
  764. }
  765. return 0;
  766. fail_cmd:
  767. return ret;
  768. }
  769. int afe_loopback_gain(u16 port_id, u16 volume)
  770. {
  771. struct afe_port_cmd_set_param set_param;
  772. int ret = 0;
  773. if (this_afe.apr == NULL) {
  774. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  775. 0xFFFFFFFF, &this_afe);
  776. pr_debug("%s: Register AFE\n", __func__);
  777. if (this_afe.apr == NULL) {
  778. pr_err("%s: Unable to register AFE\n", __func__);
  779. ret = -ENODEV;
  780. return ret;
  781. }
  782. }
  783. if (afe_validate_port(port_id) < 0) {
  784. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  785. port_id);
  786. ret = -EINVAL;
  787. goto fail_cmd;
  788. }
  789. /* RX ports numbers are even .TX ports numbers are odd. */
  790. if (port_id % 2 == 0) {
  791. pr_err("%s: Failed : afe loopback gain only for TX ports."
  792. " port_id %d\n", __func__, port_id);
  793. ret = -EINVAL;
  794. goto fail_cmd;
  795. }
  796. pr_debug("%s: %d %hX\n", __func__, port_id, volume);
  797. set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  798. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  799. set_param.hdr.pkt_size = sizeof(set_param);
  800. set_param.hdr.src_port = 0;
  801. set_param.hdr.dest_port = 0;
  802. set_param.hdr.token = 0;
  803. set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM;
  804. set_param.port_id = port_id;
  805. set_param.payload_size = sizeof(struct afe_param_payload_base) +
  806. sizeof(struct afe_param_loopback_gain);
  807. set_param.payload_address = 0;
  808. set_param.payload.base.module_id = AFE_MODULE_ID_PORT_INFO;
  809. set_param.payload.base.param_id = AFE_PARAM_ID_LOOPBACK_GAIN;
  810. set_param.payload.base.param_size =
  811. sizeof(struct afe_param_loopback_gain);
  812. set_param.payload.base.reserved = 0;
  813. set_param.payload.param.loopback_gain.gain = volume;
  814. set_param.payload.param.loopback_gain.reserved = 0;
  815. atomic_set(&this_afe.state, 1);
  816. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_param);
  817. if (ret < 0) {
  818. pr_err("%s: AFE param set failed for port %d\n",
  819. __func__, port_id);
  820. ret = -EINVAL;
  821. goto fail_cmd;
  822. }
  823. ret = wait_event_timeout(this_afe.wait,
  824. (atomic_read(&this_afe.state) == 0),
  825. msecs_to_jiffies(TIMEOUT_MS));
  826. if (!ret) {
  827. pr_err("%s: wait_event timeout\n", __func__);
  828. ret = -EINVAL;
  829. goto fail_cmd;
  830. }
  831. return 0;
  832. fail_cmd:
  833. return ret;
  834. }
  835. int afe_apply_gain(u16 port_id, u16 gain)
  836. {
  837. struct afe_port_gain_command set_gain;
  838. int ret = 0;
  839. if (this_afe.apr == NULL) {
  840. pr_err("%s: AFE is not opened\n", __func__);
  841. ret = -EPERM;
  842. goto fail_cmd;
  843. }
  844. if (afe_validate_port(port_id) < 0) {
  845. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  846. port_id);
  847. ret = -EINVAL;
  848. goto fail_cmd;
  849. }
  850. /* RX ports numbers are even .TX ports numbers are odd. */
  851. if (port_id % 2 == 0) {
  852. pr_err("%s: Failed : afe apply gain only for TX ports."
  853. " port_id %d\n", __func__, port_id);
  854. ret = -EINVAL;
  855. goto fail_cmd;
  856. }
  857. pr_debug("%s: %d %hX\n", __func__, port_id, gain);
  858. set_gain.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  859. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  860. set_gain.hdr.pkt_size = sizeof(set_gain);
  861. set_gain.hdr.src_port = 0;
  862. set_gain.hdr.dest_port = 0;
  863. set_gain.hdr.token = 0;
  864. set_gain.hdr.opcode = AFE_PORT_CMD_APPLY_GAIN;
  865. set_gain.port_id = port_id;
  866. set_gain.gain = gain;
  867. atomic_set(&this_afe.state, 1);
  868. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_gain);
  869. if (ret < 0) {
  870. pr_err("%s: AFE Gain set failed for port %d\n",
  871. __func__, port_id);
  872. ret = -EINVAL;
  873. goto fail_cmd;
  874. }
  875. ret = wait_event_timeout(this_afe.wait,
  876. (atomic_read(&this_afe.state) == 0),
  877. msecs_to_jiffies(TIMEOUT_MS));
  878. if (!ret) {
  879. pr_err("%s: wait_event timeout\n", __func__);
  880. ret = -EINVAL;
  881. goto fail_cmd;
  882. }
  883. return 0;
  884. fail_cmd:
  885. return ret;
  886. }
  887. int afe_pseudo_port_start_nowait(u16 port_id)
  888. {
  889. int ret = 0;
  890. struct afe_pseudoport_start_command start;
  891. pr_debug("%s: port_id=%d\n", __func__, port_id);
  892. if (this_afe.apr == NULL) {
  893. pr_err("%s: AFE APR is not registered\n", __func__);
  894. return -ENODEV;
  895. }
  896. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  897. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  898. start.hdr.pkt_size = sizeof(start);
  899. start.hdr.src_port = 0;
  900. start.hdr.dest_port = 0;
  901. start.hdr.token = 0;
  902. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  903. start.port_id = port_id;
  904. start.timing = 1;
  905. atomic_set(&this_afe.state, 1);
  906. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
  907. if (ret < 0) {
  908. pr_err("%s: AFE enable for port %d failed %d\n",
  909. __func__, port_id, ret);
  910. return -EINVAL;
  911. }
  912. return 0;
  913. }
  914. int afe_start_pseudo_port(u16 port_id)
  915. {
  916. int ret = 0;
  917. struct afe_pseudoport_start_command start;
  918. pr_debug("%s: port_id=%d\n", __func__, port_id);
  919. ret = afe_q6_interface_prepare();
  920. if (ret != 0)
  921. return ret;
  922. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  923. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  924. start.hdr.pkt_size = sizeof(start);
  925. start.hdr.src_port = 0;
  926. start.hdr.dest_port = 0;
  927. start.hdr.token = 0;
  928. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  929. start.port_id = port_id;
  930. start.timing = 1;
  931. atomic_set(&this_afe.state, 1);
  932. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start);
  933. if (ret < 0) {
  934. pr_err("%s: AFE enable for port %d failed %d\n",
  935. __func__, port_id, ret);
  936. return -EINVAL;
  937. }
  938. ret = wait_event_timeout(this_afe.wait,
  939. (atomic_read(&this_afe.state) == 0),
  940. msecs_to_jiffies(TIMEOUT_MS));
  941. if (!ret) {
  942. pr_err("%s: wait_event timeout\n", __func__);
  943. return -EINVAL;
  944. }
  945. return 0;
  946. }
  947. int afe_pseudo_port_stop_nowait(u16 port_id)
  948. {
  949. int ret = 0;
  950. struct afe_pseudoport_stop_command stop;
  951. pr_debug("%s: port_id=%d\n", __func__, port_id);
  952. if (this_afe.apr == NULL) {
  953. pr_err("%s: AFE is already closed\n", __func__);
  954. return -EINVAL;
  955. }
  956. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  957. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  958. stop.hdr.pkt_size = sizeof(stop);
  959. stop.hdr.src_port = 0;
  960. stop.hdr.dest_port = 0;
  961. stop.hdr.token = 0;
  962. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  963. stop.port_id = port_id;
  964. stop.reserved = 0;
  965. atomic_set(&this_afe.state, 1);
  966. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
  967. if (ret < 0) {
  968. pr_err("%s: AFE close failed %d\n", __func__, ret);
  969. return -EINVAL;
  970. }
  971. return 0;
  972. }
  973. int afe_stop_pseudo_port(u16 port_id)
  974. {
  975. int ret = 0;
  976. struct afe_pseudoport_stop_command stop;
  977. pr_debug("%s: port_id=%d\n", __func__, port_id);
  978. if (this_afe.apr == NULL) {
  979. pr_err("%s: AFE is already closed\n", __func__);
  980. return -EINVAL;
  981. }
  982. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  983. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  984. stop.hdr.pkt_size = sizeof(stop);
  985. stop.hdr.src_port = 0;
  986. stop.hdr.dest_port = 0;
  987. stop.hdr.token = 0;
  988. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  989. stop.port_id = port_id;
  990. stop.reserved = 0;
  991. atomic_set(&this_afe.state, 1);
  992. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
  993. if (ret < 0) {
  994. pr_err("%s: AFE close failed %d\n", __func__, ret);
  995. return -EINVAL;
  996. }
  997. ret = wait_event_timeout(this_afe.wait,
  998. (atomic_read(&this_afe.state) == 0),
  999. msecs_to_jiffies(TIMEOUT_MS));
  1000. if (!ret) {
  1001. pr_err("%s: wait_event timeout\n", __func__);
  1002. return -EINVAL;
  1003. }
  1004. return 0;
  1005. }
  1006. int afe_cmd_memory_map(u32 dma_addr_p, u32 dma_buf_sz)
  1007. {
  1008. int ret = 0;
  1009. struct afe_cmd_memory_map mregion;
  1010. pr_debug("%s:\n", __func__);
  1011. if (this_afe.apr == NULL) {
  1012. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1013. 0xFFFFFFFF, &this_afe);
  1014. pr_debug("%s: Register AFE\n", __func__);
  1015. if (this_afe.apr == NULL) {
  1016. pr_err("%s: Unable to register AFE\n", __func__);
  1017. ret = -ENODEV;
  1018. return ret;
  1019. }
  1020. }
  1021. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1022. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1023. mregion.hdr.pkt_size = sizeof(mregion);
  1024. mregion.hdr.src_port = 0;
  1025. mregion.hdr.dest_port = 0;
  1026. mregion.hdr.token = 0;
  1027. mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_MAP;
  1028. mregion.phy_addr = dma_addr_p;
  1029. mregion.mem_sz = dma_buf_sz;
  1030. mregion.mem_id = 0;
  1031. mregion.rsvd = 0;
  1032. atomic_set(&this_afe.state, 1);
  1033. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
  1034. if (ret < 0) {
  1035. pr_err("%s: AFE memory map cmd failed %d\n",
  1036. __func__, ret);
  1037. ret = -EINVAL;
  1038. return ret;
  1039. }
  1040. ret = wait_event_timeout(this_afe.wait,
  1041. (atomic_read(&this_afe.state) == 0),
  1042. msecs_to_jiffies(TIMEOUT_MS));
  1043. if (!ret) {
  1044. pr_err("%s: wait_event timeout\n", __func__);
  1045. ret = -EINVAL;
  1046. return ret;
  1047. }
  1048. return 0;
  1049. }
  1050. int afe_cmd_memory_map_nowait(u32 dma_addr_p, u32 dma_buf_sz)
  1051. {
  1052. int ret = 0;
  1053. struct afe_cmd_memory_map mregion;
  1054. pr_debug("%s:\n", __func__);
  1055. if (this_afe.apr == NULL) {
  1056. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1057. 0xFFFFFFFF, &this_afe);
  1058. pr_debug("%s: Register AFE\n", __func__);
  1059. if (this_afe.apr == NULL) {
  1060. pr_err("%s: Unable to register AFE\n", __func__);
  1061. ret = -ENODEV;
  1062. return ret;
  1063. }
  1064. }
  1065. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1066. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1067. mregion.hdr.pkt_size = sizeof(mregion);
  1068. mregion.hdr.src_port = 0;
  1069. mregion.hdr.dest_port = 0;
  1070. mregion.hdr.token = 0;
  1071. mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_MAP;
  1072. mregion.phy_addr = dma_addr_p;
  1073. mregion.mem_sz = dma_buf_sz;
  1074. mregion.mem_id = 0;
  1075. mregion.rsvd = 0;
  1076. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
  1077. if (ret < 0) {
  1078. pr_err("%s: AFE memory map cmd failed %d\n",
  1079. __func__, ret);
  1080. ret = -EINVAL;
  1081. }
  1082. return 0;
  1083. }
  1084. int afe_cmd_memory_unmap(u32 dma_addr_p)
  1085. {
  1086. int ret = 0;
  1087. struct afe_cmd_memory_unmap mregion;
  1088. pr_debug("%s:\n", __func__);
  1089. if (this_afe.apr == NULL) {
  1090. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1091. 0xFFFFFFFF, &this_afe);
  1092. pr_debug("%s: Register AFE\n", __func__);
  1093. if (this_afe.apr == NULL) {
  1094. pr_err("%s: Unable to register AFE\n", __func__);
  1095. ret = -ENODEV;
  1096. return ret;
  1097. }
  1098. }
  1099. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1100. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1101. mregion.hdr.pkt_size = sizeof(mregion);
  1102. mregion.hdr.src_port = 0;
  1103. mregion.hdr.dest_port = 0;
  1104. mregion.hdr.token = 0;
  1105. mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_UNMAP;
  1106. mregion.phy_addr = dma_addr_p;
  1107. atomic_set(&this_afe.state, 1);
  1108. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
  1109. if (ret < 0) {
  1110. pr_err("%s: AFE memory unmap cmd failed %d\n",
  1111. __func__, ret);
  1112. ret = -EINVAL;
  1113. return ret;
  1114. }
  1115. ret = wait_event_timeout(this_afe.wait,
  1116. (atomic_read(&this_afe.state) == 0),
  1117. msecs_to_jiffies(TIMEOUT_MS));
  1118. if (!ret) {
  1119. pr_err("%s: wait_event timeout\n", __func__);
  1120. ret = -EINVAL;
  1121. return ret;
  1122. }
  1123. return 0;
  1124. }
  1125. int afe_cmd_memory_unmap_nowait(u32 dma_addr_p)
  1126. {
  1127. int ret = 0;
  1128. struct afe_cmd_memory_unmap mregion;
  1129. pr_debug("%s:\n", __func__);
  1130. if (this_afe.apr == NULL) {
  1131. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1132. 0xFFFFFFFF, &this_afe);
  1133. pr_debug("%s: Register AFE\n", __func__);
  1134. if (this_afe.apr == NULL) {
  1135. pr_err("%s: Unable to register AFE\n", __func__);
  1136. ret = -ENODEV;
  1137. return ret;
  1138. }
  1139. }
  1140. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1141. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1142. mregion.hdr.pkt_size = sizeof(mregion);
  1143. mregion.hdr.src_port = 0;
  1144. mregion.hdr.dest_port = 0;
  1145. mregion.hdr.token = 0;
  1146. mregion.hdr.opcode = AFE_SERVICE_CMD_MEMORY_UNMAP;
  1147. mregion.phy_addr = dma_addr_p;
  1148. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &mregion);
  1149. if (ret < 0) {
  1150. pr_err("%s: AFE memory unmap cmd failed %d\n",
  1151. __func__, ret);
  1152. ret = -EINVAL;
  1153. }
  1154. return 0;
  1155. }
  1156. int afe_register_get_events(u16 port_id,
  1157. void (*cb) (uint32_t opcode,
  1158. uint32_t token, uint32_t *payload, void *priv),
  1159. void *private_data)
  1160. {
  1161. int ret = 0;
  1162. struct afe_cmd_reg_rtport rtproxy;
  1163. pr_debug("%s:\n", __func__);
  1164. if (this_afe.apr == NULL) {
  1165. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1166. 0xFFFFFFFF, &this_afe);
  1167. pr_debug("%s: Register AFE\n", __func__);
  1168. if (this_afe.apr == NULL) {
  1169. pr_err("%s: Unable to register AFE\n", __func__);
  1170. ret = -ENODEV;
  1171. return ret;
  1172. }
  1173. }
  1174. if ((port_id == RT_PROXY_DAI_002_RX) ||
  1175. (port_id == RT_PROXY_DAI_001_TX))
  1176. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  1177. else
  1178. return -EINVAL;
  1179. if (port_id == RT_PROXY_PORT_001_TX) {
  1180. this_afe.tx_cb = cb;
  1181. this_afe.tx_private_data = private_data;
  1182. } else if (port_id == RT_PROXY_PORT_001_RX) {
  1183. this_afe.rx_cb = cb;
  1184. this_afe.rx_private_data = private_data;
  1185. }
  1186. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1187. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1188. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  1189. rtproxy.hdr.src_port = 1;
  1190. rtproxy.hdr.dest_port = 1;
  1191. rtproxy.hdr.token = 0;
  1192. rtproxy.hdr.opcode = AFE_SERVICE_CMD_REG_RTPORT;
  1193. rtproxy.port_id = port_id;
  1194. rtproxy.rsvd = 0;
  1195. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
  1196. if (ret < 0) {
  1197. pr_err("%s: AFE reg. rtproxy_event failed %d\n",
  1198. __func__, ret);
  1199. ret = -EINVAL;
  1200. return ret;
  1201. }
  1202. return 0;
  1203. }
  1204. int afe_unregister_get_events(u16 port_id)
  1205. {
  1206. int ret = 0;
  1207. struct afe_cmd_unreg_rtport rtproxy;
  1208. pr_debug("%s:\n", __func__);
  1209. if (this_afe.apr == NULL) {
  1210. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1211. 0xFFFFFFFF, &this_afe);
  1212. pr_debug("%s: Register AFE\n", __func__);
  1213. if (this_afe.apr == NULL) {
  1214. pr_err("%s: Unable to register AFE\n", __func__);
  1215. ret = -ENODEV;
  1216. return ret;
  1217. }
  1218. }
  1219. if ((port_id == RT_PROXY_DAI_002_RX) ||
  1220. (port_id == RT_PROXY_DAI_001_TX))
  1221. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  1222. else
  1223. return -EINVAL;
  1224. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1225. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1226. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  1227. rtproxy.hdr.src_port = 0;
  1228. rtproxy.hdr.dest_port = 0;
  1229. rtproxy.hdr.token = 0;
  1230. rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREG_RTPORT;
  1231. rtproxy.port_id = port_id;
  1232. rtproxy.rsvd = 0;
  1233. if (port_id == RT_PROXY_PORT_001_TX) {
  1234. this_afe.tx_cb = NULL;
  1235. this_afe.tx_private_data = NULL;
  1236. } else if (port_id == RT_PROXY_PORT_001_RX) {
  1237. this_afe.rx_cb = NULL;
  1238. this_afe.rx_private_data = NULL;
  1239. }
  1240. atomic_set(&this_afe.state, 1);
  1241. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &rtproxy);
  1242. if (ret < 0) {
  1243. pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
  1244. __func__, ret);
  1245. ret = -EINVAL;
  1246. return ret;
  1247. }
  1248. ret = wait_event_timeout(this_afe.wait,
  1249. (atomic_read(&this_afe.state) == 0),
  1250. msecs_to_jiffies(TIMEOUT_MS));
  1251. if (!ret) {
  1252. pr_err("%s: wait_event timeout\n", __func__);
  1253. ret = -EINVAL;
  1254. return ret;
  1255. }
  1256. return 0;
  1257. }
  1258. int afe_rt_proxy_port_write(u32 buf_addr_p, int bytes)
  1259. {
  1260. int ret = 0;
  1261. struct afe_cmd_rtport_wr afecmd_wr;
  1262. if (this_afe.apr == NULL) {
  1263. pr_err("%s:register to AFE is not done\n", __func__);
  1264. ret = -ENODEV;
  1265. return ret;
  1266. }
  1267. pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
  1268. buf_addr_p, bytes);
  1269. afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1270. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1271. afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
  1272. afecmd_wr.hdr.src_port = 0;
  1273. afecmd_wr.hdr.dest_port = 0;
  1274. afecmd_wr.hdr.token = 0;
  1275. afecmd_wr.hdr.opcode = AFE_SERVICE_CMD_RTPORT_WR;
  1276. afecmd_wr.buf_addr = (uint32_t)buf_addr_p;
  1277. afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
  1278. afecmd_wr.bytes_avail = bytes;
  1279. afecmd_wr.rsvd = 0;
  1280. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_wr);
  1281. if (ret < 0) {
  1282. pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
  1283. __func__, afecmd_wr.port_id, ret);
  1284. ret = -EINVAL;
  1285. return ret;
  1286. }
  1287. return 0;
  1288. }
  1289. int afe_rt_proxy_port_read(u32 buf_addr_p, int bytes)
  1290. {
  1291. int ret = 0;
  1292. struct afe_cmd_rtport_rd afecmd_rd;
  1293. if (this_afe.apr == NULL) {
  1294. pr_err("%s: register to AFE is not done\n", __func__);
  1295. ret = -ENODEV;
  1296. return ret;
  1297. }
  1298. pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
  1299. buf_addr_p, bytes);
  1300. afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1301. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1302. afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
  1303. afecmd_rd.hdr.src_port = 0;
  1304. afecmd_rd.hdr.dest_port = 0;
  1305. afecmd_rd.hdr.token = 0;
  1306. afecmd_rd.hdr.opcode = AFE_SERVICE_CMD_RTPORT_RD;
  1307. afecmd_rd.buf_addr = (uint32_t)buf_addr_p;
  1308. afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
  1309. afecmd_rd.bytes_avail = bytes;
  1310. afecmd_rd.rsvd = 0;
  1311. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &afecmd_rd);
  1312. if (ret < 0) {
  1313. pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
  1314. __func__, afecmd_rd.port_id, ret);
  1315. ret = -EINVAL;
  1316. return ret;
  1317. }
  1318. return 0;
  1319. }
  1320. #ifdef CONFIG_DEBUG_FS
  1321. static struct dentry *debugfs_afelb;
  1322. static struct dentry *debugfs_afelb_gain;
  1323. static int afe_debug_open(struct inode *inode, struct file *file)
  1324. {
  1325. file->private_data = inode->i_private;
  1326. pr_info("debug intf %s\n", (char *) file->private_data);
  1327. return 0;
  1328. }
  1329. static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
  1330. {
  1331. char *token;
  1332. int base, cnt;
  1333. token = strsep(&buf, " ");
  1334. for (cnt = 0; cnt < num_of_par; cnt++) {
  1335. if (token != NULL) {
  1336. if ((token[1] == 'x') || (token[1] == 'X'))
  1337. base = 16;
  1338. else
  1339. base = 10;
  1340. if (strict_strtoul(token, base, &param1[cnt]) != 0)
  1341. return -EINVAL;
  1342. token = strsep(&buf, " ");
  1343. } else
  1344. return -EINVAL;
  1345. }
  1346. return 0;
  1347. }
  1348. #define AFE_LOOPBACK_ON (1)
  1349. #define AFE_LOOPBACK_OFF (0)
  1350. static ssize_t afe_debug_write(struct file *filp,
  1351. const char __user *ubuf, size_t cnt, loff_t *ppos)
  1352. {
  1353. char *lb_str = filp->private_data;
  1354. char lbuf[32];
  1355. int rc;
  1356. unsigned long param[5];
  1357. if (cnt > sizeof(lbuf) - 1)
  1358. return -EINVAL;
  1359. rc = copy_from_user(lbuf, ubuf, cnt);
  1360. if (rc)
  1361. return -EFAULT;
  1362. lbuf[cnt] = '\0';
  1363. if (!strcmp(lb_str, "afe_loopback")) {
  1364. rc = afe_get_parameters(lbuf, param, 3);
  1365. if (!rc) {
  1366. pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
  1367. param[2]);
  1368. if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
  1369. AFE_LOOPBACK_OFF)) {
  1370. pr_err("%s: Error, parameter 0 incorrect\n",
  1371. __func__);
  1372. rc = -EINVAL;
  1373. goto afe_error;
  1374. }
  1375. if ((afe_validate_port(param[1]) < 0) ||
  1376. (afe_validate_port(param[2])) < 0) {
  1377. pr_err("%s: Error, invalid afe port\n",
  1378. __func__);
  1379. }
  1380. if (this_afe.apr == NULL) {
  1381. pr_err("%s: Error, AFE not opened\n", __func__);
  1382. rc = -EINVAL;
  1383. } else {
  1384. rc = afe_loopback(param[0], param[1], param[2]);
  1385. }
  1386. } else {
  1387. pr_err("%s: Error, invalid parameters\n", __func__);
  1388. rc = -EINVAL;
  1389. }
  1390. } else if (!strcmp(lb_str, "afe_loopback_gain")) {
  1391. rc = afe_get_parameters(lbuf, param, 2);
  1392. if (!rc) {
  1393. pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
  1394. if (afe_validate_port(param[0]) < 0) {
  1395. pr_err("%s: Error, invalid afe port\n",
  1396. __func__);
  1397. rc = -EINVAL;
  1398. goto afe_error;
  1399. }
  1400. if (param[1] > 100) {
  1401. pr_err("%s: Error, volume shoud be 0 to 100"
  1402. " percentage param = %lu\n",
  1403. __func__, param[1]);
  1404. rc = -EINVAL;
  1405. goto afe_error;
  1406. }
  1407. param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
  1408. if (this_afe.apr == NULL) {
  1409. pr_err("%s: Error, AFE not opened\n", __func__);
  1410. rc = -EINVAL;
  1411. } else {
  1412. rc = afe_loopback_gain(param[0], param[1]);
  1413. }
  1414. } else {
  1415. pr_err("%s: Error, invalid parameters\n", __func__);
  1416. rc = -EINVAL;
  1417. }
  1418. }
  1419. afe_error:
  1420. if (rc == 0)
  1421. rc = cnt;
  1422. else
  1423. pr_err("%s: rc = %d\n", __func__, rc);
  1424. return rc;
  1425. }
  1426. static const struct file_operations afe_debug_fops = {
  1427. .open = afe_debug_open,
  1428. .write = afe_debug_write
  1429. };
  1430. #endif
  1431. void afe_set_dtmf_gen_rx_portid(u16 port_id, int set)
  1432. {
  1433. if (set)
  1434. this_afe.dtmf_gen_rx_portid = port_id;
  1435. else if (this_afe.dtmf_gen_rx_portid == port_id)
  1436. this_afe.dtmf_gen_rx_portid = -1;
  1437. }
  1438. int afe_dtmf_generate_rx(int64_t duration_in_ms,
  1439. uint16_t high_freq,
  1440. uint16_t low_freq, uint16_t gain)
  1441. {
  1442. int ret = 0;
  1443. struct afe_dtmf_generation_command cmd_dtmf;
  1444. pr_debug("%s: DTMF AFE Gen\n", __func__);
  1445. if (afe_validate_port(this_afe.dtmf_gen_rx_portid) < 0) {
  1446. pr_err("%s: Failed : Invalid Port id = %d\n",
  1447. __func__, this_afe.dtmf_gen_rx_portid);
  1448. ret = -EINVAL;
  1449. goto fail_cmd;
  1450. }
  1451. if (this_afe.apr == NULL) {
  1452. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1453. 0xFFFFFFFF, &this_afe);
  1454. pr_debug("%s: Register AFE\n", __func__);
  1455. if (this_afe.apr == NULL) {
  1456. pr_err("%s: Unable to register AFE\n", __func__);
  1457. ret = -ENODEV;
  1458. return ret;
  1459. }
  1460. }
  1461. pr_debug("dur=%lld: hfreq=%d lfreq=%d gain=%d portid=%x\n",
  1462. duration_in_ms, high_freq, low_freq, gain,
  1463. this_afe.dtmf_gen_rx_portid);
  1464. cmd_dtmf.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1465. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1466. cmd_dtmf.hdr.pkt_size = sizeof(cmd_dtmf);
  1467. cmd_dtmf.hdr.src_port = 0;
  1468. cmd_dtmf.hdr.dest_port = 0;
  1469. cmd_dtmf.hdr.token = 0;
  1470. cmd_dtmf.hdr.opcode = AFE_PORTS_CMD_DTMF_CTL;
  1471. cmd_dtmf.duration_in_ms = duration_in_ms;
  1472. cmd_dtmf.high_freq = high_freq;
  1473. cmd_dtmf.low_freq = low_freq;
  1474. cmd_dtmf.gain = gain;
  1475. cmd_dtmf.num_ports = 1;
  1476. cmd_dtmf.port_ids = this_afe.dtmf_gen_rx_portid;
  1477. atomic_set(&this_afe.state, 1);
  1478. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_dtmf);
  1479. if (ret < 0) {
  1480. pr_err("%s: AFE DTMF failed for num_ports:%d ids:%x\n",
  1481. __func__, cmd_dtmf.num_ports, cmd_dtmf.port_ids);
  1482. ret = -EINVAL;
  1483. goto fail_cmd;
  1484. }
  1485. ret = wait_event_timeout(this_afe.wait,
  1486. (atomic_read(&this_afe.state) == 0),
  1487. msecs_to_jiffies(TIMEOUT_MS));
  1488. if (ret < 0) {
  1489. pr_err("%s: wait_event timeout\n", __func__);
  1490. ret = -EINVAL;
  1491. goto fail_cmd;
  1492. }
  1493. return 0;
  1494. fail_cmd:
  1495. return ret;
  1496. }
  1497. int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
  1498. {
  1499. struct afe_port_sidetone_command cmd_sidetone;
  1500. int ret = 0;
  1501. pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
  1502. tx_port_id, rx_port_id, enable, gain);
  1503. cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1504. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1505. cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
  1506. cmd_sidetone.hdr.src_port = 0;
  1507. cmd_sidetone.hdr.dest_port = 0;
  1508. cmd_sidetone.hdr.token = 0;
  1509. cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL;
  1510. cmd_sidetone.tx_port_id = tx_port_id;
  1511. cmd_sidetone.rx_port_id = rx_port_id;
  1512. cmd_sidetone.gain = gain;
  1513. cmd_sidetone.enable = enable;
  1514. atomic_set(&this_afe.state, 1);
  1515. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone);
  1516. if (ret < 0) {
  1517. pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
  1518. __func__, tx_port_id, rx_port_id);
  1519. ret = -EINVAL;
  1520. goto fail_cmd;
  1521. }
  1522. ret = wait_event_timeout(this_afe.wait,
  1523. (atomic_read(&this_afe.state) == 0),
  1524. msecs_to_jiffies(TIMEOUT_MS));
  1525. if (!ret) {
  1526. pr_err("%s: wait_event timeout\n", __func__);
  1527. ret = -EINVAL;
  1528. goto fail_cmd;
  1529. }
  1530. return 0;
  1531. fail_cmd:
  1532. return ret;
  1533. }
  1534. int afe_port_stop_nowait(int port_id)
  1535. {
  1536. struct afe_port_stop_command stop;
  1537. int ret = 0;
  1538. if (this_afe.apr == NULL) {
  1539. pr_err("AFE is already closed\n");
  1540. ret = -EINVAL;
  1541. goto fail_cmd;
  1542. }
  1543. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1544. port_id = afe_convert_virtual_to_portid(port_id);
  1545. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1546. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1547. stop.hdr.pkt_size = sizeof(stop);
  1548. stop.hdr.src_port = 0;
  1549. stop.hdr.dest_port = 0;
  1550. stop.hdr.token = 0;
  1551. stop.hdr.opcode = AFE_PORT_CMD_STOP;
  1552. stop.port_id = port_id;
  1553. stop.reserved = 0;
  1554. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
  1555. if (ret == -ENETRESET) {
  1556. pr_info("%s: Need to reset, calling APR deregister", __func__);
  1557. return apr_deregister(this_afe.apr);
  1558. } else if (IS_ERR_VALUE(ret)) {
  1559. pr_err("%s: AFE close failed\n", __func__);
  1560. ret = -EINVAL;
  1561. }
  1562. fail_cmd:
  1563. return ret;
  1564. }
  1565. int afe_close(int port_id)
  1566. {
  1567. struct afe_port_stop_command stop;
  1568. int ret = 0;
  1569. if (this_afe.apr == NULL) {
  1570. pr_err("AFE is already closed\n");
  1571. ret = -EINVAL;
  1572. goto fail_cmd;
  1573. }
  1574. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1575. if ((port_id == RT_PROXY_DAI_001_RX) ||
  1576. (port_id == RT_PROXY_DAI_002_TX))
  1577. return 0;
  1578. port_id = afe_convert_virtual_to_portid(port_id);
  1579. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1580. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1581. stop.hdr.pkt_size = sizeof(stop);
  1582. stop.hdr.src_port = 0;
  1583. stop.hdr.dest_port = 0;
  1584. stop.hdr.token = 0;
  1585. stop.hdr.opcode = AFE_PORT_CMD_STOP;
  1586. stop.port_id = port_id;
  1587. stop.reserved = 0;
  1588. atomic_set(&this_afe.state, 1);
  1589. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop);
  1590. if (ret == -ENETRESET) {
  1591. pr_info("%s: Need to reset, calling APR deregister", __func__);
  1592. return apr_deregister(this_afe.apr);
  1593. }
  1594. if (ret < 0) {
  1595. pr_err("%s: AFE close failed\n", __func__);
  1596. ret = -EINVAL;
  1597. goto fail_cmd;
  1598. }
  1599. ret = wait_event_timeout(this_afe.wait,
  1600. (atomic_read(&this_afe.state) == 0),
  1601. msecs_to_jiffies(TIMEOUT_MS));
  1602. if (!ret) {
  1603. pr_err("%s: wait_event timeout\n", __func__);
  1604. ret = -EINVAL;
  1605. goto fail_cmd;
  1606. }
  1607. fail_cmd:
  1608. return ret;
  1609. }
  1610. static int __init afe_init(void)
  1611. {
  1612. init_waitqueue_head(&this_afe.wait);
  1613. atomic_set(&this_afe.state, 0);
  1614. atomic_set(&this_afe.status, 0);
  1615. this_afe.apr = NULL;
  1616. this_afe.dtmf_gen_rx_portid = -1;
  1617. #ifdef CONFIG_DEBUG_FS
  1618. debugfs_afelb = debugfs_create_file("afe_loopback",
  1619. S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback",
  1620. &afe_debug_fops);
  1621. debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
  1622. S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain",
  1623. &afe_debug_fops);
  1624. #endif
  1625. return 0;
  1626. }
  1627. static void __exit afe_exit(void)
  1628. {
  1629. int i;
  1630. #ifdef CONFIG_DEBUG_FS
  1631. if (debugfs_afelb)
  1632. debugfs_remove(debugfs_afelb);
  1633. if (debugfs_afelb_gain)
  1634. debugfs_remove(debugfs_afelb_gain);
  1635. #endif
  1636. for (i = 0; i < MAX_AUDPROC_TYPES; i++) {
  1637. if (afe_cal_addr[i].cal_paddr != 0)
  1638. afe_cmd_memory_unmap_nowait(
  1639. afe_cal_addr[i].cal_paddr);
  1640. }
  1641. }
  1642. device_initcall(afe_init);
  1643. __exitcall(afe_exit);