q6adm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/device.h>
  5. #include <linux/jiffies.h>
  6. #include <linux/kernel.h>
  7. #include <linux/kref.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/soc/qcom/apr.h>
  15. #include <linux/wait.h>
  16. #include <sound/asound.h>
  17. #include "q6adm.h"
  18. #include "q6afe.h"
  19. #include "q6core.h"
  20. #include "q6dsp-common.h"
  21. #include "q6dsp-errno.h"
  22. #define ADM_CMD_DEVICE_OPEN_V5 0x00010326
  23. #define ADM_CMDRSP_DEVICE_OPEN_V5 0x00010329
  24. #define ADM_CMD_DEVICE_CLOSE_V5 0x00010327
  25. #define ADM_CMD_MATRIX_MAP_ROUTINGS_V5 0x00010325
  26. #define TIMEOUT_MS 1000
  27. #define RESET_COPP_ID 99
  28. #define INVALID_COPP_ID 0xFF
  29. /* Definition for a legacy device session. */
  30. #define ADM_LEGACY_DEVICE_SESSION 0
  31. #define ADM_MATRIX_ID_AUDIO_RX 0
  32. #define ADM_MATRIX_ID_AUDIO_TX 1
  33. struct q6copp {
  34. int afe_port;
  35. int copp_idx;
  36. int id;
  37. int topology;
  38. int mode;
  39. int rate;
  40. int bit_width;
  41. int channels;
  42. int app_type;
  43. int acdb_id;
  44. struct aprv2_ibasic_rsp_result_t result;
  45. struct kref refcount;
  46. wait_queue_head_t wait;
  47. struct list_head node;
  48. struct q6adm *adm;
  49. };
  50. struct q6adm {
  51. struct apr_device *apr;
  52. struct device *dev;
  53. struct q6core_svc_api_info ainfo;
  54. unsigned long copp_bitmap[AFE_MAX_PORTS];
  55. struct list_head copps_list;
  56. spinlock_t copps_list_lock;
  57. struct aprv2_ibasic_rsp_result_t result;
  58. struct mutex lock;
  59. wait_queue_head_t matrix_map_wait;
  60. };
  61. struct q6adm_cmd_device_open_v5 {
  62. u16 flags;
  63. u16 mode_of_operation;
  64. u16 endpoint_id_1;
  65. u16 endpoint_id_2;
  66. u32 topology_id;
  67. u16 dev_num_channel;
  68. u16 bit_width;
  69. u32 sample_rate;
  70. u8 dev_channel_mapping[8];
  71. } __packed;
  72. struct q6adm_cmd_matrix_map_routings_v5 {
  73. u32 matrix_id;
  74. u32 num_sessions;
  75. } __packed;
  76. struct q6adm_session_map_node_v5 {
  77. u16 session_id;
  78. u16 num_copps;
  79. } __packed;
  80. static struct q6copp *q6adm_find_copp(struct q6adm *adm, int port_idx,
  81. int copp_idx)
  82. {
  83. struct q6copp *c;
  84. struct q6copp *ret = NULL;
  85. unsigned long flags;
  86. spin_lock_irqsave(&adm->copps_list_lock, flags);
  87. list_for_each_entry(c, &adm->copps_list, node) {
  88. if ((port_idx == c->afe_port) && (copp_idx == c->copp_idx)) {
  89. ret = c;
  90. kref_get(&c->refcount);
  91. break;
  92. }
  93. }
  94. spin_unlock_irqrestore(&adm->copps_list_lock, flags);
  95. return ret;
  96. }
  97. static void q6adm_free_copp(struct kref *ref)
  98. {
  99. struct q6copp *c = container_of(ref, struct q6copp, refcount);
  100. struct q6adm *adm = c->adm;
  101. unsigned long flags;
  102. spin_lock_irqsave(&adm->copps_list_lock, flags);
  103. clear_bit(c->copp_idx, &adm->copp_bitmap[c->afe_port]);
  104. list_del(&c->node);
  105. spin_unlock_irqrestore(&adm->copps_list_lock, flags);
  106. kfree(c);
  107. }
  108. static int q6adm_callback(struct apr_device *adev, struct apr_resp_pkt *data)
  109. {
  110. struct aprv2_ibasic_rsp_result_t *result = data->payload;
  111. int port_idx, copp_idx;
  112. struct apr_hdr *hdr = &data->hdr;
  113. struct q6copp *copp;
  114. struct q6adm *adm = dev_get_drvdata(&adev->dev);
  115. if (!data->payload_size)
  116. return 0;
  117. copp_idx = (hdr->token) & 0XFF;
  118. port_idx = ((hdr->token) >> 16) & 0xFF;
  119. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  120. dev_err(&adev->dev, "Invalid port idx %d token %d\n",
  121. port_idx, hdr->token);
  122. return 0;
  123. }
  124. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  125. dev_err(&adev->dev, "Invalid copp idx %d token %d\n",
  126. copp_idx, hdr->token);
  127. return 0;
  128. }
  129. switch (hdr->opcode) {
  130. case APR_BASIC_RSP_RESULT: {
  131. if (result->status != 0) {
  132. dev_err(&adev->dev, "cmd = 0x%x return error = 0x%x\n",
  133. result->opcode, result->status);
  134. }
  135. switch (result->opcode) {
  136. case ADM_CMD_DEVICE_OPEN_V5:
  137. case ADM_CMD_DEVICE_CLOSE_V5:
  138. copp = q6adm_find_copp(adm, port_idx, copp_idx);
  139. if (!copp)
  140. return 0;
  141. copp->result = *result;
  142. wake_up(&copp->wait);
  143. kref_put(&copp->refcount, q6adm_free_copp);
  144. break;
  145. case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
  146. adm->result = *result;
  147. wake_up(&adm->matrix_map_wait);
  148. break;
  149. default:
  150. dev_err(&adev->dev, "Unknown Cmd: 0x%x\n",
  151. result->opcode);
  152. break;
  153. }
  154. return 0;
  155. }
  156. case ADM_CMDRSP_DEVICE_OPEN_V5: {
  157. struct adm_cmd_rsp_device_open_v5 {
  158. u32 status;
  159. u16 copp_id;
  160. u16 reserved;
  161. } __packed *open = data->payload;
  162. copp = q6adm_find_copp(adm, port_idx, copp_idx);
  163. if (!copp)
  164. return 0;
  165. if (open->copp_id == INVALID_COPP_ID) {
  166. dev_err(&adev->dev, "Invalid coppid rxed %d\n",
  167. open->copp_id);
  168. copp->result.status = ADSP_EBADPARAM;
  169. wake_up(&copp->wait);
  170. kref_put(&copp->refcount, q6adm_free_copp);
  171. break;
  172. }
  173. copp->result.opcode = hdr->opcode;
  174. copp->id = open->copp_id;
  175. wake_up(&copp->wait);
  176. kref_put(&copp->refcount, q6adm_free_copp);
  177. }
  178. break;
  179. default:
  180. dev_err(&adev->dev, "Unknown cmd:0x%x\n",
  181. hdr->opcode);
  182. break;
  183. }
  184. return 0;
  185. }
  186. static struct q6copp *q6adm_alloc_copp(struct q6adm *adm, int port_idx)
  187. {
  188. struct q6copp *c;
  189. int idx;
  190. idx = find_first_zero_bit(&adm->copp_bitmap[port_idx],
  191. MAX_COPPS_PER_PORT);
  192. if (idx >= MAX_COPPS_PER_PORT)
  193. return ERR_PTR(-EBUSY);
  194. c = kzalloc(sizeof(*c), GFP_ATOMIC);
  195. if (!c)
  196. return ERR_PTR(-ENOMEM);
  197. set_bit(idx, &adm->copp_bitmap[port_idx]);
  198. c->copp_idx = idx;
  199. c->afe_port = port_idx;
  200. c->adm = adm;
  201. init_waitqueue_head(&c->wait);
  202. return c;
  203. }
  204. static int q6adm_apr_send_copp_pkt(struct q6adm *adm, struct q6copp *copp,
  205. struct apr_pkt *pkt, uint32_t rsp_opcode)
  206. {
  207. struct device *dev = adm->dev;
  208. uint32_t opcode = pkt->hdr.opcode;
  209. int ret;
  210. mutex_lock(&adm->lock);
  211. copp->result.opcode = 0;
  212. copp->result.status = 0;
  213. ret = apr_send_pkt(adm->apr, pkt);
  214. if (ret < 0) {
  215. dev_err(dev, "Failed to send APR packet\n");
  216. ret = -EINVAL;
  217. goto err;
  218. }
  219. /* Wait for the callback with copp id */
  220. if (rsp_opcode)
  221. ret = wait_event_timeout(copp->wait,
  222. (copp->result.opcode == opcode) ||
  223. (copp->result.opcode == rsp_opcode),
  224. msecs_to_jiffies(TIMEOUT_MS));
  225. else
  226. ret = wait_event_timeout(copp->wait,
  227. (copp->result.opcode == opcode),
  228. msecs_to_jiffies(TIMEOUT_MS));
  229. if (!ret) {
  230. dev_err(dev, "ADM copp cmd timedout\n");
  231. ret = -ETIMEDOUT;
  232. } else if (copp->result.status > 0) {
  233. dev_err(dev, "DSP returned error[%d]\n",
  234. copp->result.status);
  235. ret = -EINVAL;
  236. }
  237. err:
  238. mutex_unlock(&adm->lock);
  239. return ret;
  240. }
  241. static int q6adm_device_close(struct q6adm *adm, struct q6copp *copp,
  242. int port_id, int copp_idx)
  243. {
  244. struct apr_pkt close;
  245. close.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  246. APR_HDR_LEN(APR_HDR_SIZE),
  247. APR_PKT_VER);
  248. close.hdr.pkt_size = sizeof(close);
  249. close.hdr.src_port = port_id;
  250. close.hdr.dest_port = copp->id;
  251. close.hdr.token = port_id << 16 | copp_idx;
  252. close.hdr.opcode = ADM_CMD_DEVICE_CLOSE_V5;
  253. return q6adm_apr_send_copp_pkt(adm, copp, &close, 0);
  254. }
  255. static struct q6copp *q6adm_find_matching_copp(struct q6adm *adm,
  256. int port_id, int topology,
  257. int mode, int rate,
  258. int channel_mode, int bit_width,
  259. int app_type)
  260. {
  261. struct q6copp *c;
  262. struct q6copp *ret = NULL;
  263. unsigned long flags;
  264. spin_lock_irqsave(&adm->copps_list_lock, flags);
  265. list_for_each_entry(c, &adm->copps_list, node) {
  266. if ((port_id == c->afe_port) && (topology == c->topology) &&
  267. (mode == c->mode) && (rate == c->rate) &&
  268. (bit_width == c->bit_width) && (app_type == c->app_type)) {
  269. ret = c;
  270. kref_get(&c->refcount);
  271. }
  272. }
  273. spin_unlock_irqrestore(&adm->copps_list_lock, flags);
  274. return ret;
  275. }
  276. static int q6adm_device_open(struct q6adm *adm, struct q6copp *copp,
  277. int port_id, int path, int topology,
  278. int channel_mode, int bit_width, int rate)
  279. {
  280. struct q6adm_cmd_device_open_v5 *open;
  281. int afe_port = q6afe_get_port_id(port_id);
  282. struct apr_pkt *pkt;
  283. void *p;
  284. int ret, pkt_size;
  285. pkt_size = APR_HDR_SIZE + sizeof(*open);
  286. p = kzalloc(pkt_size, GFP_KERNEL);
  287. if (!p)
  288. return -ENOMEM;
  289. pkt = p;
  290. open = p + APR_HDR_SIZE;
  291. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  292. APR_HDR_LEN(APR_HDR_SIZE),
  293. APR_PKT_VER);
  294. pkt->hdr.pkt_size = pkt_size;
  295. pkt->hdr.src_port = afe_port;
  296. pkt->hdr.dest_port = afe_port;
  297. pkt->hdr.token = port_id << 16 | copp->copp_idx;
  298. pkt->hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
  299. open->flags = ADM_LEGACY_DEVICE_SESSION;
  300. open->mode_of_operation = path;
  301. open->endpoint_id_1 = afe_port;
  302. open->topology_id = topology;
  303. open->dev_num_channel = channel_mode & 0x00FF;
  304. open->bit_width = bit_width;
  305. open->sample_rate = rate;
  306. ret = q6dsp_map_channels(&open->dev_channel_mapping[0],
  307. channel_mode);
  308. if (ret)
  309. goto err;
  310. ret = q6adm_apr_send_copp_pkt(adm, copp, pkt,
  311. ADM_CMDRSP_DEVICE_OPEN_V5);
  312. err:
  313. kfree(pkt);
  314. return ret;
  315. }
  316. /**
  317. * q6adm_open() - open adm and grab a free copp
  318. *
  319. * @dev: Pointer to adm child device.
  320. * @port_id: port id
  321. * @path: playback or capture path.
  322. * @rate: rate at which copp is required.
  323. * @channel_mode: channel mode
  324. * @topology: adm topology id
  325. * @perf_mode: performace mode.
  326. * @bit_width: audio sample bit width
  327. * @app_type: Application type.
  328. * @acdb_id: ACDB id
  329. *
  330. * Return: Will be an negative on error or a valid copp pointer on success.
  331. */
  332. struct q6copp *q6adm_open(struct device *dev, int port_id, int path, int rate,
  333. int channel_mode, int topology, int perf_mode,
  334. uint16_t bit_width, int app_type, int acdb_id)
  335. {
  336. struct q6adm *adm = dev_get_drvdata(dev->parent);
  337. struct q6copp *copp;
  338. unsigned long flags;
  339. int ret = 0;
  340. if (port_id < 0) {
  341. dev_err(dev, "Invalid port_id %d\n", port_id);
  342. return ERR_PTR(-EINVAL);
  343. }
  344. copp = q6adm_find_matching_copp(adm, port_id, topology, perf_mode,
  345. rate, channel_mode, bit_width, app_type);
  346. if (copp) {
  347. dev_err(dev, "Found Matching Copp 0x%x\n", copp->copp_idx);
  348. return copp;
  349. }
  350. spin_lock_irqsave(&adm->copps_list_lock, flags);
  351. copp = q6adm_alloc_copp(adm, port_id);
  352. if (IS_ERR(copp)) {
  353. spin_unlock_irqrestore(&adm->copps_list_lock, flags);
  354. return ERR_CAST(copp);
  355. }
  356. list_add_tail(&copp->node, &adm->copps_list);
  357. spin_unlock_irqrestore(&adm->copps_list_lock, flags);
  358. kref_init(&copp->refcount);
  359. copp->topology = topology;
  360. copp->mode = perf_mode;
  361. copp->rate = rate;
  362. copp->channels = channel_mode;
  363. copp->bit_width = bit_width;
  364. copp->app_type = app_type;
  365. ret = q6adm_device_open(adm, copp, port_id, path, topology,
  366. channel_mode, bit_width, rate);
  367. if (ret < 0) {
  368. kref_put(&copp->refcount, q6adm_free_copp);
  369. return ERR_PTR(ret);
  370. }
  371. return copp;
  372. }
  373. EXPORT_SYMBOL_GPL(q6adm_open);
  374. /**
  375. * q6adm_get_copp_id() - get copp index
  376. *
  377. * @copp: Pointer to valid copp
  378. *
  379. * Return: Will be an negative on error or a valid copp index on success.
  380. **/
  381. int q6adm_get_copp_id(struct q6copp *copp)
  382. {
  383. if (!copp)
  384. return -EINVAL;
  385. return copp->copp_idx;
  386. }
  387. EXPORT_SYMBOL_GPL(q6adm_get_copp_id);
  388. /**
  389. * q6adm_matrix_map() - Map asm streams and afe ports using payload
  390. *
  391. * @dev: Pointer to adm child device.
  392. * @path: playback or capture path.
  393. * @payload_map: map between session id and afe ports.
  394. * @perf_mode: Performace mode.
  395. *
  396. * Return: Will be an negative on error or a zero on success.
  397. */
  398. int q6adm_matrix_map(struct device *dev, int path,
  399. struct route_payload payload_map, int perf_mode)
  400. {
  401. struct q6adm *adm = dev_get_drvdata(dev->parent);
  402. struct q6adm_cmd_matrix_map_routings_v5 *route;
  403. struct q6adm_session_map_node_v5 *node;
  404. struct apr_pkt *pkt;
  405. uint16_t *copps_list;
  406. int pkt_size, ret, i, copp_idx;
  407. void *matrix_map;
  408. struct q6copp *copp;
  409. /* Assumes port_ids have already been validated during adm_open */
  410. pkt_size = (APR_HDR_SIZE + sizeof(*route) + sizeof(*node) +
  411. (sizeof(uint32_t) * payload_map.num_copps));
  412. matrix_map = kzalloc(pkt_size, GFP_KERNEL);
  413. if (!matrix_map)
  414. return -ENOMEM;
  415. pkt = matrix_map;
  416. route = matrix_map + APR_HDR_SIZE;
  417. node = matrix_map + APR_HDR_SIZE + sizeof(*route);
  418. copps_list = matrix_map + APR_HDR_SIZE + sizeof(*route) + sizeof(*node);
  419. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  420. APR_HDR_LEN(APR_HDR_SIZE),
  421. APR_PKT_VER);
  422. pkt->hdr.pkt_size = pkt_size;
  423. pkt->hdr.token = 0;
  424. pkt->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
  425. route->num_sessions = 1;
  426. switch (path) {
  427. case ADM_PATH_PLAYBACK:
  428. route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
  429. break;
  430. case ADM_PATH_LIVE_REC:
  431. route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
  432. break;
  433. default:
  434. dev_err(dev, "Wrong path set[%d]\n", path);
  435. break;
  436. }
  437. node->session_id = payload_map.session_id;
  438. node->num_copps = payload_map.num_copps;
  439. for (i = 0; i < payload_map.num_copps; i++) {
  440. int port_idx = payload_map.port_id[i];
  441. if (port_idx < 0) {
  442. dev_err(dev, "Invalid port_id %d\n",
  443. payload_map.port_id[i]);
  444. kfree(pkt);
  445. return -EINVAL;
  446. }
  447. copp_idx = payload_map.copp_idx[i];
  448. copp = q6adm_find_copp(adm, port_idx, copp_idx);
  449. if (!copp) {
  450. kfree(pkt);
  451. return -EINVAL;
  452. }
  453. copps_list[i] = copp->id;
  454. kref_put(&copp->refcount, q6adm_free_copp);
  455. }
  456. mutex_lock(&adm->lock);
  457. adm->result.status = 0;
  458. adm->result.opcode = 0;
  459. ret = apr_send_pkt(adm->apr, pkt);
  460. if (ret < 0) {
  461. dev_err(dev, "routing for stream %d failed ret %d\n",
  462. payload_map.session_id, ret);
  463. goto fail_cmd;
  464. }
  465. ret = wait_event_timeout(adm->matrix_map_wait,
  466. adm->result.opcode == pkt->hdr.opcode,
  467. msecs_to_jiffies(TIMEOUT_MS));
  468. if (!ret) {
  469. dev_err(dev, "routing for stream %d failed\n",
  470. payload_map.session_id);
  471. ret = -ETIMEDOUT;
  472. goto fail_cmd;
  473. } else if (adm->result.status > 0) {
  474. dev_err(dev, "DSP returned error[%d]\n",
  475. adm->result.status);
  476. ret = -EINVAL;
  477. goto fail_cmd;
  478. }
  479. fail_cmd:
  480. mutex_unlock(&adm->lock);
  481. kfree(pkt);
  482. return ret;
  483. }
  484. EXPORT_SYMBOL_GPL(q6adm_matrix_map);
  485. /**
  486. * q6adm_close() - Close adm copp
  487. *
  488. * @dev: Pointer to adm child device.
  489. * @copp: pointer to previously opened copp
  490. *
  491. * Return: Will be an negative on error or a zero on success.
  492. */
  493. int q6adm_close(struct device *dev, struct q6copp *copp)
  494. {
  495. struct q6adm *adm = dev_get_drvdata(dev->parent);
  496. int ret = 0;
  497. ret = q6adm_device_close(adm, copp, copp->afe_port, copp->copp_idx);
  498. if (ret < 0) {
  499. dev_err(adm->dev, "Failed to close copp %d\n", ret);
  500. return ret;
  501. }
  502. kref_put(&copp->refcount, q6adm_free_copp);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(q6adm_close);
  506. static int q6adm_probe(struct apr_device *adev)
  507. {
  508. struct device *dev = &adev->dev;
  509. struct q6adm *adm;
  510. adm = devm_kzalloc(dev, sizeof(*adm), GFP_KERNEL);
  511. if (!adm)
  512. return -ENOMEM;
  513. adm->apr = adev;
  514. dev_set_drvdata(dev, adm);
  515. adm->dev = dev;
  516. q6core_get_svc_api_info(adev->svc_id, &adm->ainfo);
  517. mutex_init(&adm->lock);
  518. init_waitqueue_head(&adm->matrix_map_wait);
  519. INIT_LIST_HEAD(&adm->copps_list);
  520. spin_lock_init(&adm->copps_list_lock);
  521. return devm_of_platform_populate(dev);
  522. }
  523. #ifdef CONFIG_OF
  524. static const struct of_device_id q6adm_device_id[] = {
  525. { .compatible = "qcom,q6adm" },
  526. {},
  527. };
  528. MODULE_DEVICE_TABLE(of, q6adm_device_id);
  529. #endif
  530. static struct apr_driver qcom_q6adm_driver = {
  531. .probe = q6adm_probe,
  532. .callback = q6adm_callback,
  533. .driver = {
  534. .name = "qcom-q6adm",
  535. .of_match_table = of_match_ptr(q6adm_device_id),
  536. },
  537. };
  538. module_apr_driver(qcom_q6adm_driver);
  539. MODULE_DESCRIPTION("Q6 Audio Device Manager");
  540. MODULE_LICENSE("GPL v2");