msm-pcm-host-voice.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /* Copyright (c) 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/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/time.h>
  16. #include <linux/wait.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/dma-mapping.h>
  20. #include <sound/core.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/control.h>
  26. #include <asm/dma.h>
  27. #include "qdsp6/q6voice.h"
  28. #define HPCM_MAX_Q_LEN 10
  29. #define HPCM_MIN_VOC_PKT_SIZE 320
  30. #define HPCM_MAX_VOC_PKT_SIZE 640
  31. #define VOICE_TX_CAPTURE_DAI_ID "CS-VOICE HOST TX CAPTURE"
  32. #define VOICE_TX_PLAYBACK_DAI_ID "CS-VOICE HOST TX PLAYBACK"
  33. #define VOICE_RX_CAPTURE_DAI_ID "CS-VOICE HOST RX CAPTURE"
  34. #define VOICE_RX_PLAYBACK_DAI_ID "CS-VOICE HOST RX PLAYBACK"
  35. #define VOLTE_TX_CAPTURE_DAI_ID "VOLTE HOST TX CAPTURE"
  36. #define VOLTE_TX_PLAYBACK_DAI_ID "VOLTE HOST TX PLAYBACK"
  37. #define VOLTE_RX_CAPTURE_DAI_ID "VOLTE HOST RX CAPTURE"
  38. #define VOLTE_RX_PLAYBACK_DAI_ID "VOLTE HOST RX PLAYBACK"
  39. enum {
  40. RX = 1,
  41. TX,
  42. };
  43. enum {
  44. VOICE_INDEX = 0,
  45. VOLTE_INDEX,
  46. MAX_SESSION
  47. };
  48. enum hpcm_state {
  49. HPCM_STOPPED = 1,
  50. HPCM_CLOSED,
  51. HPCM_PREPARED,
  52. HPCM_STARTED,
  53. };
  54. struct hpcm_frame {
  55. uint32_t len;
  56. uint8_t voc_pkt[HPCM_MAX_VOC_PKT_SIZE];
  57. };
  58. struct hpcm_buf_node {
  59. struct list_head list;
  60. struct hpcm_frame frame;
  61. };
  62. struct vocpcm_ion_buffer {
  63. /* Physical address */
  64. uint32_t paddr;
  65. /* Kernel virtual address */
  66. uint32_t kvaddr;
  67. };
  68. struct dai_data {
  69. enum hpcm_state state;
  70. struct snd_pcm_substream *substream;
  71. struct list_head filled_queue;
  72. struct list_head free_queue;
  73. wait_queue_head_t queue_wait;
  74. spinlock_t dsp_lock;
  75. uint32_t pcm_size;
  76. uint32_t pcm_count;
  77. /* IRQ position */
  78. uint32_t pcm_irq_pos;
  79. /* Position in buffer */
  80. uint32_t pcm_buf_pos;
  81. struct vocpcm_ion_buffer vocpcm_ion_buffer;
  82. };
  83. struct tap_point {
  84. struct dai_data playback_dai_data;
  85. struct dai_data capture_dai_data;
  86. struct ion_handle *ion_handle;
  87. int ion_mem_len;
  88. };
  89. struct session {
  90. struct tap_point tx_tap_point;
  91. struct tap_point rx_tap_point;
  92. };
  93. struct tappnt_mxr_data {
  94. bool enable;
  95. uint16_t direction;
  96. uint16_t sample_rate;
  97. };
  98. /* Values from mixer ctl are cached in this structure */
  99. struct mixer_conf {
  100. uint8_t sess_indx;
  101. struct tappnt_mxr_data rx;
  102. struct tappnt_mxr_data tx;
  103. };
  104. struct start_cmd {
  105. struct vss_ivpcm_tap_point tap_pnt[2];
  106. uint32_t no_of_tapoints;
  107. };
  108. struct hpcm_drv {
  109. struct mutex lock;
  110. struct session session[MAX_SESSION];
  111. struct mixer_conf mixer_conf;
  112. struct ion_client *ion_client;
  113. struct start_cmd start_cmd;
  114. };
  115. static struct hpcm_drv hpcm_drv;
  116. static struct snd_pcm_hardware msm_pcm_hardware = {
  117. .info = (SNDRV_PCM_INFO_MMAP |
  118. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  119. SNDRV_PCM_INFO_MMAP_VALID |
  120. SNDRV_PCM_INFO_INTERLEAVED),
  121. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  122. SNDRV_PCM_FMTBIT_SPECIAL,
  123. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
  124. .rate_min = 8000,
  125. .rate_max = 16000,
  126. .channels_min = 1,
  127. .channels_max = 1,
  128. .buffer_bytes_max = sizeof(struct hpcm_buf_node) * HPCM_MAX_Q_LEN,
  129. .period_bytes_min = HPCM_MIN_VOC_PKT_SIZE,
  130. .period_bytes_max = HPCM_MAX_VOC_PKT_SIZE,
  131. .periods_min = HPCM_MAX_Q_LEN,
  132. .periods_max = HPCM_MAX_Q_LEN,
  133. .fifo_size = 0,
  134. };
  135. static char *hpcm_get_sess_name(int sess_indx)
  136. {
  137. char *sess_name = NULL;
  138. if (sess_indx == VOICE_INDEX)
  139. sess_name = VOICE_SESSION_NAME;
  140. else if (sess_indx == VOLTE_INDEX)
  141. sess_name = VOLTE_SESSION_NAME;
  142. else
  143. pr_err("%s:, Invalid sess_index\n", __func__);
  144. return sess_name;
  145. }
  146. static void hpcm_reset_mixer_config(struct hpcm_drv *prtd)
  147. {
  148. prtd->mixer_conf.sess_indx = -1;
  149. prtd->mixer_conf.rx.enable = false;
  150. prtd->mixer_conf.rx.direction = -1;
  151. prtd->mixer_conf.rx.sample_rate = 0;
  152. prtd->mixer_conf.tx.enable = false;
  153. prtd->mixer_conf.tx.direction = -1;
  154. prtd->mixer_conf.tx.sample_rate = 0;
  155. }
  156. /* Check for valid mixer control values */
  157. static bool hpcm_is_valid_config(int sess_indx, int tap_point,
  158. uint16_t direction, uint16_t samplerate)
  159. {
  160. if (sess_indx < VOICE_INDEX || sess_indx > VOLTE_INDEX) {
  161. pr_err("%s: invalid sess_indx :%d\n", __func__, sess_indx);
  162. goto error;
  163. }
  164. if (samplerate != VSS_IVPCM_SAMPLING_RATE_8K &&
  165. samplerate != VSS_IVPCM_SAMPLING_RATE_16K) {
  166. pr_err("%s: invalid sample rate :%d\n", __func__, samplerate);
  167. goto error;
  168. }
  169. if ((tap_point != RX) && (tap_point != TX)) {
  170. pr_err("%s: invalid tappoint :%d\n", __func__, tap_point);
  171. goto error;
  172. }
  173. if ((direction != VSS_IVPCM_TAP_POINT_DIR_IN) &&
  174. (direction != VSS_IVPCM_TAP_POINT_DIR_OUT) &&
  175. (direction != VSS_IVPCM_TAP_POINT_DIR_OUT_IN)) {
  176. pr_err("%s: invalid direction :%d\n", __func__, direction);
  177. goto error;
  178. }
  179. return true;
  180. error:
  181. return false;
  182. }
  183. static struct dai_data *hpcm_get_dai_data(char *pcm_id, struct hpcm_drv *prtd)
  184. {
  185. struct dai_data *dai_data = NULL;
  186. size_t size = 0;
  187. if (pcm_id) {
  188. size = strlen(pcm_id);
  189. /* Check for Voice DAI */
  190. if (strnstr(pcm_id, VOICE_TX_CAPTURE_DAI_ID, size)) {
  191. dai_data =
  192. &prtd->session[VOICE_INDEX].tx_tap_point.capture_dai_data;
  193. } else if (strnstr(pcm_id, VOICE_TX_PLAYBACK_DAI_ID, size)) {
  194. dai_data =
  195. &prtd->session[VOICE_INDEX].tx_tap_point.playback_dai_data;
  196. } else if (strnstr(pcm_id, VOICE_RX_CAPTURE_DAI_ID, size)) {
  197. dai_data =
  198. &prtd->session[VOICE_INDEX].rx_tap_point.capture_dai_data;
  199. } else if (strnstr(pcm_id, VOICE_RX_PLAYBACK_DAI_ID, size)) {
  200. dai_data =
  201. &prtd->session[VOICE_INDEX].rx_tap_point.playback_dai_data;
  202. /* Check for VoLTE DAI */
  203. } else if (strnstr(pcm_id, VOLTE_TX_CAPTURE_DAI_ID, size)) {
  204. dai_data =
  205. &prtd->session[VOLTE_INDEX].tx_tap_point.capture_dai_data;
  206. } else if (strnstr(pcm_id, VOLTE_TX_PLAYBACK_DAI_ID, size)) {
  207. dai_data =
  208. &prtd->session[VOLTE_INDEX].tx_tap_point.playback_dai_data;
  209. } else if (strnstr(pcm_id, VOLTE_RX_CAPTURE_DAI_ID, size)) {
  210. dai_data =
  211. &prtd->session[VOLTE_INDEX].rx_tap_point.capture_dai_data;
  212. } else if (strnstr(pcm_id, VOLTE_RX_PLAYBACK_DAI_ID, size)) {
  213. dai_data =
  214. &prtd->session[VOLTE_INDEX].rx_tap_point.playback_dai_data;
  215. } else {
  216. pr_err("%s: Wrong dai id\n", __func__);
  217. }
  218. }
  219. return dai_data;
  220. }
  221. static struct tap_point *hpcm_get_tappoint_data(char *pcm_id,
  222. struct hpcm_drv *prtd)
  223. {
  224. struct tap_point *tp = NULL;
  225. size_t size = strlen(pcm_id);
  226. if (pcm_id) {
  227. /* Check for Voice DAI */
  228. if (strnstr(pcm_id, VOICE_TX_CAPTURE_DAI_ID, size)) {
  229. tp = &prtd->session[VOICE_INDEX].tx_tap_point;
  230. } else if (strnstr(pcm_id, VOICE_TX_PLAYBACK_DAI_ID, size)) {
  231. tp = &prtd->session[VOICE_INDEX].tx_tap_point;
  232. } else if (strnstr(pcm_id, VOICE_RX_CAPTURE_DAI_ID, size)) {
  233. tp = &prtd->session[VOICE_INDEX].rx_tap_point;
  234. } else if (strnstr(pcm_id, VOICE_RX_PLAYBACK_DAI_ID, size)) {
  235. tp = &prtd->session[VOICE_INDEX].rx_tap_point;
  236. /* Check for VoLTE DAI */
  237. } else if (strnstr(pcm_id, VOLTE_TX_CAPTURE_DAI_ID, size)) {
  238. tp = &prtd->session[VOLTE_INDEX].tx_tap_point;
  239. } else if (strnstr(pcm_id, VOLTE_TX_PLAYBACK_DAI_ID, size)) {
  240. tp = &prtd->session[VOLTE_INDEX].tx_tap_point;
  241. } else if (strnstr(pcm_id, VOLTE_RX_CAPTURE_DAI_ID, size)) {
  242. tp = &prtd->session[VOLTE_INDEX].rx_tap_point;
  243. } else if (strnstr(pcm_id, VOLTE_RX_PLAYBACK_DAI_ID, size)) {
  244. tp = &prtd->session[VOLTE_INDEX].rx_tap_point;
  245. } else {
  246. pr_err("%s: wrong dai id\n", __func__);
  247. }
  248. }
  249. return tp;
  250. }
  251. static struct tappnt_mxr_data *hpcm_get_tappnt_mixer_data(char *pcm_id,
  252. struct hpcm_drv *prtd)
  253. {
  254. if (strnstr(pcm_id, VOICE_TX_CAPTURE_DAI_ID, strlen(pcm_id)) ||
  255. strnstr(pcm_id, VOICE_TX_PLAYBACK_DAI_ID, strlen(pcm_id)) ||
  256. strnstr(pcm_id, VOLTE_TX_CAPTURE_DAI_ID, strlen(pcm_id)) ||
  257. strnstr(pcm_id, VOLTE_TX_PLAYBACK_DAI_ID, strlen(pcm_id))) {
  258. return &prtd->mixer_conf.tx;
  259. } else {
  260. return &prtd->mixer_conf.rx;
  261. }
  262. }
  263. static int get_tappnt_value(char *pcm_id)
  264. {
  265. if (strnstr(pcm_id, VOICE_TX_CAPTURE_DAI_ID, strlen(pcm_id)) ||
  266. strnstr(pcm_id, VOICE_TX_PLAYBACK_DAI_ID, strlen(pcm_id)) ||
  267. strnstr(pcm_id, VOLTE_TX_CAPTURE_DAI_ID, strlen(pcm_id)) ||
  268. strnstr(pcm_id, VOLTE_TX_PLAYBACK_DAI_ID, strlen(pcm_id))) {
  269. return TX;
  270. } else {
  271. return RX;
  272. }
  273. }
  274. static bool hpcm_all_dais_are_ready(uint16_t direction, struct tap_point *tp,
  275. enum hpcm_state state)
  276. {
  277. bool dais_started = false;
  278. /*
  279. * Based on the direction set per tap point in the mixer control,
  280. * all the dais per tap point should meet the required state for the
  281. * commands such as vpcm_map_memory/vpcm_start to be executed.
  282. */
  283. switch (direction) {
  284. case VSS_IVPCM_TAP_POINT_DIR_OUT_IN:
  285. if ((tp->playback_dai_data.state >= state) &&
  286. (tp->capture_dai_data.state >= state)) {
  287. dais_started = true;
  288. }
  289. break;
  290. case VSS_IVPCM_TAP_POINT_DIR_IN:
  291. if (tp->playback_dai_data.state >= state)
  292. dais_started = true;
  293. break;
  294. case VSS_IVPCM_TAP_POINT_DIR_OUT:
  295. if (tp->capture_dai_data.state >= state)
  296. dais_started = true;
  297. break;
  298. default:
  299. pr_err("invalid direction\n");
  300. }
  301. return dais_started;
  302. }
  303. static void hpcm_create_free_queue(struct snd_dma_buffer *dma_buf,
  304. struct dai_data *dai_data)
  305. {
  306. struct hpcm_buf_node *buf_node = NULL;
  307. int i = 0, offset = 0;
  308. for (i = 0; i < HPCM_MAX_Q_LEN; i++) {
  309. buf_node = (void *)dma_buf->area + offset;
  310. list_add_tail(&buf_node->list,
  311. &dai_data->free_queue);
  312. offset = offset + sizeof(struct hpcm_buf_node);
  313. }
  314. }
  315. static void hpcm_unmap_and_free_shared_memory(struct hpcm_drv *prtd,
  316. struct tap_point *tp,
  317. uint16_t direction)
  318. {
  319. unsigned long paddr = 0;
  320. char *sess = hpcm_get_sess_name(prtd->mixer_conf.sess_indx);
  321. switch (direction) {
  322. case VSS_IVPCM_TAP_POINT_DIR_OUT_IN:
  323. case VSS_IVPCM_TAP_POINT_DIR_IN:
  324. paddr = tp->playback_dai_data.vocpcm_ion_buffer.paddr;
  325. break;
  326. case VSS_IVPCM_TAP_POINT_DIR_OUT:
  327. paddr = tp->capture_dai_data.vocpcm_ion_buffer.paddr;
  328. break;
  329. default:
  330. pr_err("Invalid direction\n");
  331. }
  332. if (paddr) {
  333. voc_send_cvp_unmap_vocpcm_memory(voc_get_session_id(sess),
  334. paddr);
  335. ion_unmap_kernel(prtd->ion_client, tp->ion_handle);
  336. ion_free(prtd->ion_client, tp->ion_handle);
  337. tp->ion_mem_len = 0;
  338. tp->playback_dai_data.vocpcm_ion_buffer.paddr = 0;
  339. tp->capture_dai_data.vocpcm_ion_buffer.paddr = 0;
  340. tp->playback_dai_data.vocpcm_ion_buffer.kvaddr = 0;
  341. tp->capture_dai_data.vocpcm_ion_buffer.kvaddr = 0;
  342. }
  343. }
  344. static int hpcm_map_vocpcm_memory(struct hpcm_drv *prtd,
  345. struct tap_point *tp,
  346. struct tappnt_mxr_data *tmd)
  347. {
  348. unsigned long paddr = 0;
  349. bool send_cmd = false;
  350. int ret = 0;
  351. char *sess = hpcm_get_sess_name(prtd->mixer_conf.sess_indx);
  352. /*
  353. * only one memory map command is sent per tap point, ensure all dais
  354. * for a tap point are in HPCM_PREPARED state.
  355. */
  356. send_cmd = hpcm_all_dais_are_ready(tmd->direction, tp, HPCM_PREPARED);
  357. if (send_cmd == true) {
  358. switch (tmd->direction) {
  359. case VSS_IVPCM_TAP_POINT_DIR_OUT_IN:
  360. case VSS_IVPCM_TAP_POINT_DIR_IN:
  361. paddr = tp->playback_dai_data.vocpcm_ion_buffer.paddr;
  362. break;
  363. case VSS_IVPCM_TAP_POINT_DIR_OUT:
  364. paddr = tp->capture_dai_data.vocpcm_ion_buffer.paddr;
  365. break;
  366. }
  367. ret = voc_send_cvp_map_vocpcm_memory(voc_get_session_id(sess),
  368. paddr, tp->ion_mem_len);
  369. }
  370. return ret;
  371. }
  372. static int hpcm_allocate_shared_memory(struct hpcm_drv *prtd,
  373. struct tap_point *tp,
  374. struct tappnt_mxr_data *tmd)
  375. {
  376. int result;
  377. int mem_len;
  378. unsigned long paddr;
  379. void *kvptr;
  380. int ion_mem_reqd = 0;
  381. bool create_mem = false;
  382. create_mem = hpcm_all_dais_are_ready(tmd->direction, tp,
  383. HPCM_PREPARED);
  384. if (create_mem) {
  385. if (tmd->direction == VSS_IVPCM_TAP_POINT_DIR_OUT_IN)
  386. ion_mem_reqd = HPCM_MAX_VOC_PKT_SIZE * 2;
  387. else
  388. ion_mem_reqd = HPCM_MAX_VOC_PKT_SIZE;
  389. tp->ion_handle = ion_alloc(prtd->ion_client,
  390. ion_mem_reqd,
  391. SZ_4K, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
  392. if (IS_ERR_OR_NULL((void *) tp->ion_handle)) {
  393. pr_err("%s: ION memory allocation failed\n",
  394. __func__);
  395. goto error;
  396. }
  397. result = ion_phys(prtd->ion_client, tp->ion_handle,
  398. &paddr, (size_t *)&mem_len);
  399. if (result) {
  400. pr_err("%s: ION Get Physical failed, rc = %d\n",
  401. __func__, result);
  402. goto error;
  403. }
  404. kvptr = ion_map_kernel(prtd->ion_client, tp->ion_handle);
  405. if (IS_ERR_OR_NULL(kvptr)) {
  406. pr_err("%s: ION memory mapping failed\n", __func__);
  407. goto error;
  408. }
  409. switch (tmd->direction) {
  410. case VSS_IVPCM_TAP_POINT_DIR_OUT_IN:
  411. tp->playback_dai_data.vocpcm_ion_buffer.paddr =
  412. (uint32_t)paddr;
  413. tp->playback_dai_data.vocpcm_ion_buffer.kvaddr =
  414. (uint32_t)(uint8_t *)kvptr;
  415. tp->capture_dai_data.vocpcm_ion_buffer.paddr =
  416. (uint32_t)paddr + 1 * HPCM_MAX_VOC_PKT_SIZE;
  417. tp->capture_dai_data.vocpcm_ion_buffer.kvaddr =
  418. (uint32_t)(uint8_t *)kvptr + 1 * HPCM_MAX_VOC_PKT_SIZE;
  419. break;
  420. case VSS_IVPCM_TAP_POINT_DIR_IN:
  421. tp->playback_dai_data.vocpcm_ion_buffer.paddr =
  422. (uint32_t)paddr;
  423. tp->playback_dai_data.vocpcm_ion_buffer.kvaddr =
  424. (uint32_t)(uint8_t *)kvptr;
  425. break;
  426. case VSS_IVPCM_TAP_POINT_DIR_OUT:
  427. tp->capture_dai_data.vocpcm_ion_buffer.paddr =
  428. (uint32_t)paddr;
  429. tp->capture_dai_data.vocpcm_ion_buffer.kvaddr =
  430. (uint32_t)(uint8_t *)kvptr;
  431. break;
  432. }
  433. tp->ion_mem_len = ion_mem_reqd;
  434. }
  435. return 0;
  436. error:
  437. if (tp->ion_handle)
  438. ion_free(prtd->ion_client, tp->ion_handle);
  439. return -ENOMEM;
  440. }
  441. static int hpcm_start_vocpcm(char *pcm_id, struct hpcm_drv *prtd,
  442. struct tap_point *tp)
  443. {
  444. int indx = prtd->mixer_conf.sess_indx;
  445. uint32_t *no_of_tp = &prtd->start_cmd.no_of_tapoints;
  446. struct vss_ivpcm_tap_point *tap_pnt = &prtd->start_cmd.tap_pnt[0];
  447. uint32_t no_of_tp_req = 0;
  448. char *sess = hpcm_get_sess_name(indx);
  449. if (prtd->mixer_conf.rx.enable)
  450. no_of_tp_req++;
  451. if (prtd->mixer_conf.tx.enable)
  452. no_of_tp_req++;
  453. if (prtd->mixer_conf.rx.enable && (get_tappnt_value(pcm_id) == RX)) {
  454. if (hpcm_all_dais_are_ready(prtd->mixer_conf.rx.direction,
  455. tp, HPCM_PREPARED)) {
  456. pr_debug("%s: RX conditions met\n", __func__);
  457. tap_pnt[*no_of_tp].tap_point =
  458. VSS_IVPCM_TAP_POINT_RX_DEFAULT;
  459. tap_pnt[*no_of_tp].direction =
  460. prtd->mixer_conf.rx.direction;
  461. tap_pnt[*no_of_tp].sampling_rate =
  462. prtd->mixer_conf.rx.sample_rate;
  463. (*no_of_tp)++;
  464. }
  465. }
  466. if (prtd->mixer_conf.tx.enable && (get_tappnt_value(pcm_id) == TX)) {
  467. if (hpcm_all_dais_are_ready(prtd->mixer_conf.tx.direction,
  468. tp, HPCM_PREPARED)) {
  469. pr_debug("%s: TX conditions met\n", __func__);
  470. tap_pnt[*no_of_tp].tap_point =
  471. VSS_IVPCM_TAP_POINT_TX_DEFAULT;
  472. tap_pnt[*no_of_tp].direction =
  473. prtd->mixer_conf.tx.direction;
  474. tap_pnt[*no_of_tp].sampling_rate =
  475. prtd->mixer_conf.tx.sample_rate;
  476. (*no_of_tp)++;
  477. }
  478. }
  479. pr_debug("%s: *no_of_tp = %d no_of_tp_req = %d\n",
  480. __func__, *no_of_tp, no_of_tp_req);
  481. if ((prtd->mixer_conf.tx.enable && prtd->mixer_conf.rx.enable) &&
  482. *no_of_tp == no_of_tp_req) {
  483. voc_send_cvp_start_vocpcm(voc_get_session_id(sess),
  484. tap_pnt, *no_of_tp);
  485. memset(&prtd->start_cmd, 0, sizeof(struct start_cmd));
  486. } else if ((prtd->mixer_conf.tx.enable ||
  487. prtd->mixer_conf.rx.enable) && *no_of_tp == no_of_tp_req) {
  488. voc_send_cvp_start_vocpcm(voc_get_session_id(sess),
  489. tap_pnt, *no_of_tp);
  490. memset(&prtd->start_cmd, 0, sizeof(struct start_cmd));
  491. } else {
  492. pr_debug("%s: required pcm handles not opened yet\n", __func__);
  493. }
  494. return 0;
  495. }
  496. /* Playback path*/
  497. static void hpcm_copy_playback_data_from_queue(struct dai_data *dai_data,
  498. uint32_t *len)
  499. {
  500. struct hpcm_buf_node *buf_node = NULL;
  501. unsigned long dsp_flags;
  502. if (dai_data->substream == NULL)
  503. return;
  504. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  505. if (!list_empty(&dai_data->filled_queue)) {
  506. buf_node = list_first_entry(&dai_data->filled_queue,
  507. struct hpcm_buf_node, list);
  508. list_del(&buf_node->list);
  509. *len = buf_node->frame.len;
  510. memcpy((uint8_t *)dai_data->vocpcm_ion_buffer.kvaddr,
  511. &buf_node->frame.voc_pkt[0],
  512. buf_node->frame.len);
  513. list_add_tail(&buf_node->list, &dai_data->free_queue);
  514. dai_data->pcm_irq_pos += dai_data->pcm_count;
  515. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  516. snd_pcm_period_elapsed(dai_data->substream);
  517. } else {
  518. *len = 0;
  519. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  520. pr_err("IN data not available\n");
  521. }
  522. wake_up(&dai_data->queue_wait);
  523. }
  524. /* Capture path*/
  525. static void hpcm_copy_capture_data_to_queue(struct dai_data *dai_data,
  526. uint32_t len)
  527. {
  528. struct hpcm_buf_node *buf_node = NULL;
  529. unsigned long dsp_flags;
  530. if (dai_data->substream == NULL)
  531. return;
  532. /* Copy out buffer packet into free_queue */
  533. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  534. if (!list_empty(&dai_data->free_queue)) {
  535. buf_node = list_first_entry(&dai_data->free_queue,
  536. struct hpcm_buf_node, list);
  537. list_del(&buf_node->list);
  538. buf_node->frame.len = len;
  539. memcpy(&buf_node->frame.voc_pkt[0],
  540. (uint8_t *)dai_data->vocpcm_ion_buffer.kvaddr,
  541. buf_node->frame.len);
  542. list_add_tail(&buf_node->list, &dai_data->filled_queue);
  543. dai_data->pcm_irq_pos += dai_data->pcm_count;
  544. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  545. snd_pcm_period_elapsed(dai_data->substream);
  546. } else {
  547. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  548. pr_err("OUTPUT data dropped\n");
  549. }
  550. wake_up(&dai_data->queue_wait);
  551. }
  552. void hpcm_notify_evt_processing(uint8_t *data, char *session,
  553. void *private_data)
  554. {
  555. struct hpcm_drv *prtd = (struct hpcm_drv *)private_data;
  556. struct vss_ivpcm_evt_notify *notify_evt =
  557. (struct vss_ivpcm_evt_notify *)data;
  558. struct vss_ivpcm_evt_push_buffer push_buff_event;
  559. struct tap_point *tp = NULL;
  560. int in_buf_len = 0;
  561. struct tappnt_mxr_data *tmd = NULL;
  562. char *sess = hpcm_get_sess_name(prtd->mixer_conf.sess_indx);
  563. /* If it's not a timetick, it's a error notification, drop the event */
  564. if ((notify_evt->notify_mask & VSS_IVPCM_NOTIFY_MASK_TIMETICK) == 0) {
  565. pr_err("%s: Error notification. mask=%d\n", __func__,
  566. notify_evt->notify_mask);
  567. return;
  568. }
  569. if (notify_evt->tap_point == VSS_IVPCM_TAP_POINT_TX_DEFAULT) {
  570. tp = &prtd->session[prtd->mixer_conf.sess_indx].tx_tap_point;
  571. tmd = &prtd->mixer_conf.tx;
  572. } else if (notify_evt->tap_point == VSS_IVPCM_TAP_POINT_RX_DEFAULT) {
  573. tp = &prtd->session[prtd->mixer_conf.sess_indx].rx_tap_point;
  574. tmd = &prtd->mixer_conf.rx;
  575. }
  576. if (notify_evt->notify_mask & VSS_IVPCM_NOTIFY_MASK_OUTPUT_BUFFER) {
  577. hpcm_copy_capture_data_to_queue(&tp->capture_dai_data,
  578. notify_evt->filled_out_size);
  579. }
  580. if (notify_evt->notify_mask & VSS_IVPCM_NOTIFY_MASK_INPUT_BUFFER) {
  581. hpcm_copy_playback_data_from_queue(&tp->playback_dai_data,
  582. &in_buf_len);
  583. }
  584. switch (tmd->direction) {
  585. /*
  586. * When the dir is OUT_IN, for the first notify mask, pushbuf mask
  587. * should be set to VSS_IVPCM_PUSH_BUFFER_MASK_OUTPUT_BUFFER since we
  588. * atleast need one buffer's worth data before we can send IN buffer.
  589. * For the consecutive notify evts, the push buf mask will set for both
  590. * VSS_IVPCM_PUSH_BUFFER_MASK_OUTPUT_BUFFER and
  591. * VSS_IVPCM_PUSH_BUFFER_MASK_IN_BUFFER.
  592. */
  593. case VSS_IVPCM_TAP_POINT_DIR_OUT_IN:
  594. if (notify_evt->notify_mask ==
  595. VSS_IVPCM_NOTIFY_MASK_TIMETICK) {
  596. push_buff_event.push_buf_mask =
  597. VSS_IVPCM_PUSH_BUFFER_MASK_OUTPUT_BUFFER;
  598. } else {
  599. push_buff_event.push_buf_mask =
  600. VSS_IVPCM_PUSH_BUFFER_MASK_OUTPUT_BUFFER |
  601. VSS_IVPCM_PUSH_BUFFER_MASK_INPUT_BUFFER;
  602. }
  603. break;
  604. case VSS_IVPCM_TAP_POINT_DIR_IN:
  605. push_buff_event.push_buf_mask =
  606. VSS_IVPCM_PUSH_BUFFER_MASK_INPUT_BUFFER;
  607. break;
  608. case VSS_IVPCM_TAP_POINT_DIR_OUT:
  609. push_buff_event.push_buf_mask =
  610. VSS_IVPCM_PUSH_BUFFER_MASK_OUTPUT_BUFFER;
  611. break;
  612. }
  613. push_buff_event.tap_point = notify_evt->tap_point;
  614. push_buff_event.out_buf_addr =
  615. tp->capture_dai_data.vocpcm_ion_buffer.paddr;
  616. push_buff_event.in_buf_addr =
  617. tp->playback_dai_data.vocpcm_ion_buffer.paddr;
  618. push_buff_event.out_buf_size = notify_evt->request_buff_size;
  619. push_buff_event.in_buf_size = in_buf_len;
  620. push_buff_event.sampling_rate = notify_evt->sampling_rate;
  621. push_buff_event.num_in_channels = 1;
  622. voc_send_cvp_vocpcm_push_buf_evt(voc_get_session_id(sess),
  623. &push_buff_event);
  624. }
  625. static int msm_hpcm_configure_voice_put(struct snd_kcontrol *kcontrol,
  626. struct snd_ctl_elem_value *ucontrol)
  627. {
  628. int tap_point = ucontrol->value.integer.value[0];
  629. uint16_t direction = ucontrol->value.integer.value[1];
  630. uint16_t sample_rate = ucontrol->value.integer.value[2];
  631. struct tappnt_mxr_data *tmd = NULL;
  632. int ret = 0;
  633. mutex_lock(&hpcm_drv.lock);
  634. pr_debug("%s: tap_point = %d direction = %d sample_rate = %d\n",
  635. __func__, tap_point, direction, sample_rate);
  636. if (!hpcm_is_valid_config(VOICE_INDEX, tap_point, direction,
  637. sample_rate)) {
  638. pr_err("Invalid vpcm mixer control voice values\n");
  639. ret = -EINVAL;
  640. goto done;
  641. }
  642. if (tap_point == RX)
  643. tmd = &hpcm_drv.mixer_conf.rx;
  644. else
  645. tmd = &hpcm_drv.mixer_conf.tx;
  646. tmd->enable = true;
  647. tmd->direction = direction;
  648. tmd->sample_rate = sample_rate;
  649. hpcm_drv.mixer_conf.sess_indx = VOICE_INDEX;
  650. done:
  651. mutex_unlock(&hpcm_drv.lock);
  652. return ret;
  653. }
  654. static int msm_hpcm_configure_voice_get(struct snd_kcontrol *kcontrol,
  655. struct snd_ctl_elem_value *ucontrol)
  656. {
  657. pr_debug("%s:\n", __func__);
  658. return -EINVAL;
  659. }
  660. static int msm_hpcm_configure_volte_put(struct snd_kcontrol *kcontrol,
  661. struct snd_ctl_elem_value *ucontrol)
  662. {
  663. int tap_point = ucontrol->value.integer.value[0];
  664. uint16_t direction = ucontrol->value.integer.value[1];
  665. uint16_t sample_rate = ucontrol->value.integer.value[2];
  666. struct tappnt_mxr_data *tmd = NULL;
  667. int ret = 0;
  668. mutex_lock(&hpcm_drv.lock);
  669. pr_debug("%s: tap_point=%d direction=%d sample_rate=%d\n",
  670. __func__, tap_point, direction, sample_rate);
  671. if (!hpcm_is_valid_config(VOLTE_INDEX, tap_point, direction,
  672. sample_rate)) {
  673. pr_err("Invalid vpcm mixer control volte values\n");
  674. ret = -EINVAL;
  675. goto done;
  676. }
  677. if (tap_point == RX)
  678. tmd = &hpcm_drv.mixer_conf.rx;
  679. else
  680. tmd = &hpcm_drv.mixer_conf.tx;
  681. tmd->enable = true;
  682. tmd->direction = direction;
  683. tmd->sample_rate = sample_rate;
  684. hpcm_drv.mixer_conf.sess_indx = VOLTE_INDEX;
  685. done:
  686. mutex_unlock(&hpcm_drv.lock);
  687. return ret;
  688. }
  689. static int msm_hpcm_configure_volte_get(struct snd_kcontrol *kcontrol,
  690. struct snd_ctl_elem_value *ucontrol)
  691. {
  692. pr_debug("%s:\n", __func__);
  693. return -EINVAL;
  694. }
  695. static struct snd_kcontrol_new msm_hpcm_controls[] = {
  696. SOC_SINGLE_MULTI_EXT("HPCM_Voice tappoint direction samplerate",
  697. SND_SOC_NOPM, 0, 16000 , 0, 3,
  698. msm_hpcm_configure_voice_get,
  699. msm_hpcm_configure_voice_put),
  700. SOC_SINGLE_MULTI_EXT("HPCM_VoLTE tappoint direction samplerate",
  701. SND_SOC_NOPM, 0, 16000 , 0, 3,
  702. msm_hpcm_configure_volte_get,
  703. msm_hpcm_configure_volte_put),
  704. };
  705. /* Sample rates supported */
  706. static unsigned int supported_sample_rates[] = {8000, 16000};
  707. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  708. .count = ARRAY_SIZE(supported_sample_rates),
  709. .list = supported_sample_rates,
  710. .mask = 0,
  711. };
  712. static int msm_pcm_close(struct snd_pcm_substream *substream)
  713. {
  714. int ret = 0;
  715. struct list_head *ptr = NULL;
  716. struct list_head *next = NULL;
  717. struct hpcm_buf_node *buf_node = NULL;
  718. struct snd_dma_buffer *dma_buf;
  719. struct snd_pcm_runtime *runtime;
  720. struct hpcm_drv *prtd;
  721. unsigned long dsp_flags;
  722. struct dai_data *dai_data = NULL;
  723. struct tap_point *tp = NULL;
  724. struct tappnt_mxr_data *tmd = NULL;
  725. char *sess = NULL;
  726. if (substream == NULL) {
  727. pr_err("substream is NULL\n");
  728. return -EINVAL;
  729. }
  730. pr_debug("%s, %s\n", __func__, substream->pcm->id);
  731. runtime = substream->runtime;
  732. prtd = runtime->private_data;
  733. sess = hpcm_get_sess_name(prtd->mixer_conf.sess_indx);
  734. dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  735. wake_up(&dai_data->queue_wait);
  736. mutex_lock(&prtd->lock);
  737. tmd = hpcm_get_tappnt_mixer_data(substream->pcm->id, prtd);
  738. tp = hpcm_get_tappoint_data(substream->pcm->id, prtd);
  739. /* Send stop command */
  740. voc_send_cvp_stop_vocpcm(voc_get_session_id(sess));
  741. /* Unmap will be called twice once for RX and TX each */
  742. hpcm_unmap_and_free_shared_memory(prtd, tp, tmd->direction);
  743. /* Reset the cached start cmd */
  744. memset(&prtd->start_cmd, 0, sizeof(struct start_cmd));
  745. /* Release all buffer */
  746. pr_debug("%s: Release all buffer\n", __func__);
  747. substream = dai_data->substream;
  748. if (substream == NULL) {
  749. pr_debug("%s: substream is NULL\n", __func__);
  750. goto done;
  751. }
  752. dma_buf = &substream->dma_buffer;
  753. if (dma_buf == NULL) {
  754. pr_debug("%s: dma_buf is NULL\n", __func__);
  755. goto done;
  756. }
  757. if (dma_buf->area != NULL) {
  758. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  759. list_for_each_safe(ptr, next, &dai_data->filled_queue) {
  760. buf_node = list_entry(ptr,
  761. struct hpcm_buf_node, list);
  762. list_del(&buf_node->list);
  763. }
  764. list_for_each_safe(ptr, next, &dai_data->free_queue) {
  765. buf_node = list_entry(ptr,
  766. struct hpcm_buf_node, list);
  767. list_del(&buf_node->list);
  768. }
  769. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  770. dma_free_coherent(substream->pcm->card->dev,
  771. runtime->hw.buffer_bytes_max, dma_buf->area,
  772. dma_buf->addr);
  773. dma_buf->area = NULL;
  774. }
  775. dai_data->substream = NULL;
  776. dai_data->pcm_buf_pos = 0;
  777. dai_data->pcm_count = 0;
  778. dai_data->pcm_irq_pos = 0;
  779. dai_data->pcm_size = 0;
  780. dai_data->state = HPCM_CLOSED;
  781. hpcm_reset_mixer_config(prtd);
  782. done:
  783. mutex_unlock(&prtd->lock);
  784. return ret;
  785. }
  786. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  787. snd_pcm_uframes_t hwoff, void __user *buf,
  788. snd_pcm_uframes_t frames)
  789. {
  790. int ret = 0;
  791. struct hpcm_buf_node *buf_node = NULL;
  792. struct snd_pcm_runtime *runtime = substream->runtime;
  793. struct hpcm_drv *prtd = runtime->private_data;
  794. struct dai_data *dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  795. unsigned long dsp_flags;
  796. int count = frames_to_bytes(runtime, frames);
  797. ret = wait_event_interruptible_timeout(dai_data->queue_wait,
  798. (!list_empty(&dai_data->free_queue) ||
  799. dai_data->state == HPCM_STOPPED),
  800. 1 * HZ);
  801. if (ret > 0) {
  802. if (count <= HPCM_MAX_VOC_PKT_SIZE) {
  803. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  804. buf_node =
  805. list_first_entry(&dai_data->free_queue,
  806. struct hpcm_buf_node, list);
  807. list_del(&buf_node->list);
  808. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  809. ret = copy_from_user(&buf_node->frame.voc_pkt,
  810. buf, count);
  811. buf_node->frame.len = count;
  812. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  813. list_add_tail(&buf_node->list, &dai_data->filled_queue);
  814. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  815. } else {
  816. pr_err("%s: Write cnt %d is > HPCM_MAX_VOC_PKT_SIZE\n",
  817. __func__, count);
  818. ret = -ENOMEM;
  819. }
  820. } else if (ret == 0) {
  821. pr_err("%s: No free Playback buffer\n", __func__);
  822. ret = -ETIMEDOUT;
  823. } else {
  824. pr_err("%s: playback copy was interrupted\n", __func__);
  825. }
  826. return ret;
  827. }
  828. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  829. int channel, snd_pcm_uframes_t hwoff,
  830. void __user *buf, snd_pcm_uframes_t frames)
  831. {
  832. int ret = 0;
  833. int count = 0;
  834. struct hpcm_buf_node *buf_node = NULL;
  835. struct snd_pcm_runtime *runtime = substream->runtime;
  836. struct hpcm_drv *prtd = runtime->private_data;
  837. struct dai_data *dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  838. unsigned long dsp_flags;
  839. count = frames_to_bytes(runtime, frames);
  840. ret = wait_event_interruptible_timeout(dai_data->queue_wait,
  841. (!list_empty(&dai_data->filled_queue) ||
  842. dai_data->state == HPCM_STOPPED),
  843. 1 * HZ);
  844. if (ret > 0) {
  845. if (count <= HPCM_MAX_VOC_PKT_SIZE) {
  846. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  847. buf_node = list_first_entry(&dai_data->filled_queue,
  848. struct hpcm_buf_node, list);
  849. list_del(&buf_node->list);
  850. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  851. ret = copy_to_user(buf,
  852. &buf_node->frame.voc_pkt,
  853. buf_node->frame.len);
  854. if (ret) {
  855. pr_err("%s: Copy to user retuned %d\n",
  856. __func__, ret);
  857. ret = -EFAULT;
  858. }
  859. spin_lock_irqsave(&dai_data->dsp_lock, dsp_flags);
  860. list_add_tail(&buf_node->list, &dai_data->free_queue);
  861. spin_unlock_irqrestore(&dai_data->dsp_lock, dsp_flags);
  862. } else {
  863. pr_err("%s: Read count %d > HPCM_MAX_VOC_PKT_SIZE\n",
  864. __func__, count);
  865. ret = -ENOMEM;
  866. }
  867. } else if (ret == 0) {
  868. pr_err("%s: No Caputre data available\n", __func__);
  869. ret = -ETIMEDOUT;
  870. } else {
  871. pr_err("%s: Read was interrupted\n", __func__);
  872. ret = -ERESTARTSYS;
  873. }
  874. return ret;
  875. }
  876. static int msm_pcm_copy(struct snd_pcm_substream *substream, int channel,
  877. snd_pcm_uframes_t hwoff, void __user *buf,
  878. snd_pcm_uframes_t frames)
  879. {
  880. int ret = 0;
  881. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  882. ret = msm_pcm_playback_copy(substream, channel,
  883. hwoff, buf, frames);
  884. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  885. ret = msm_pcm_capture_copy(substream, channel,
  886. hwoff, buf, frames);
  887. return ret;
  888. }
  889. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  890. {
  891. struct dai_data *dai_data = NULL;
  892. struct snd_pcm_runtime *runtime = substream->runtime;
  893. struct hpcm_drv *prtd = runtime->private_data;
  894. dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  895. if (dai_data->pcm_irq_pos >= dai_data->pcm_size)
  896. dai_data->pcm_irq_pos = 0;
  897. return bytes_to_frames(runtime, (dai_data->pcm_irq_pos));
  898. }
  899. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  900. {
  901. int ret = 0;
  902. struct snd_pcm_runtime *runtime = substream->runtime;
  903. struct hpcm_drv *prtd = runtime->private_data;
  904. struct dai_data *dai_data =
  905. hpcm_get_dai_data(substream->pcm->id, prtd);
  906. pr_debug("%s, %s\n", __func__, substream->pcm->id);
  907. switch (cmd) {
  908. case SNDRV_PCM_TRIGGER_START:
  909. pr_debug("SNDRV_PCM_TRIGGER_START\n");
  910. dai_data->state = HPCM_STARTED;
  911. break;
  912. case SNDRV_PCM_TRIGGER_STOP:
  913. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  914. dai_data->state = HPCM_STOPPED;
  915. break;
  916. default:
  917. ret = -EINVAL;
  918. break;
  919. }
  920. return ret;
  921. }
  922. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  923. {
  924. int ret = 0;
  925. struct snd_pcm_runtime *runtime = substream->runtime;
  926. struct hpcm_drv *prtd = runtime->private_data;
  927. struct dai_data *dai_data = NULL;
  928. struct tap_point *tp = NULL;
  929. struct tappnt_mxr_data *tmd = NULL;
  930. pr_debug("%s, %s\n", __func__, substream->pcm->id);
  931. mutex_lock(&prtd->lock);
  932. dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  933. dai_data->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  934. dai_data->pcm_count = snd_pcm_lib_period_bytes(substream);
  935. dai_data->pcm_irq_pos = 0;
  936. dai_data->pcm_buf_pos = 0;
  937. dai_data->state = HPCM_PREPARED;
  938. tp = hpcm_get_tappoint_data(substream->pcm->id, prtd);
  939. tmd = hpcm_get_tappnt_mixer_data(substream->pcm->id, prtd);
  940. ret = hpcm_allocate_shared_memory(prtd, tp, tmd);
  941. if (ret) {
  942. pr_err("error creating shared memory err=%d\n", ret);
  943. goto done;
  944. }
  945. ret = hpcm_map_vocpcm_memory(prtd, tp, tmd);
  946. if (ret) {
  947. pr_err("error mapping shared memory err=%d\n", ret);
  948. goto done;
  949. }
  950. ret = hpcm_start_vocpcm(substream->pcm->id, prtd,
  951. hpcm_get_tappoint_data(substream->pcm->id,
  952. prtd));
  953. if (ret) {
  954. pr_err("error sending start cmd err=%d\n", ret);
  955. goto done;
  956. }
  957. done:
  958. mutex_unlock(&prtd->lock);
  959. return ret;
  960. }
  961. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  962. struct snd_pcm_hw_params *params)
  963. {
  964. struct snd_pcm_runtime *runtime = substream->runtime;
  965. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  966. struct hpcm_drv *prtd = (struct hpcm_drv *)runtime->private_data;
  967. int ret = 0;
  968. pr_debug("%s: %s\n", __func__, substream->pcm->id);
  969. mutex_lock(&prtd->lock);
  970. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  971. dma_buf->dev.dev = substream->pcm->card->dev;
  972. dma_buf->private_data = NULL;
  973. dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  974. runtime->hw.buffer_bytes_max,
  975. &dma_buf->addr, GFP_KERNEL);
  976. if (!dma_buf->area) {
  977. pr_err("%s:MSM dma_alloc failed\n", __func__);
  978. ret = -ENOMEM;
  979. goto done;
  980. }
  981. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  982. memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
  983. hpcm_create_free_queue(dma_buf,
  984. hpcm_get_dai_data(substream->pcm->id, prtd));
  985. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  986. done:
  987. mutex_unlock(&prtd->lock);
  988. return ret;
  989. }
  990. static int msm_pcm_open(struct snd_pcm_substream *substream)
  991. {
  992. struct snd_pcm_runtime *runtime = substream->runtime;
  993. struct hpcm_drv *prtd = &hpcm_drv;
  994. struct tappnt_mxr_data *tmd = NULL;
  995. struct dai_data *dai_data = NULL;
  996. int ret = 0;
  997. int tp_val = 0;
  998. pr_debug("%s, %s\n", __func__, substream->pcm->id);
  999. mutex_lock(&prtd->lock);
  1000. runtime->hw = msm_pcm_hardware;
  1001. ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1002. &constraints_sample_rates);
  1003. if (ret < 0)
  1004. pr_debug("snd_pcm_hw_constraint_list failed\n");
  1005. ret = snd_pcm_hw_constraint_integer(runtime,
  1006. SNDRV_PCM_HW_PARAM_PERIODS);
  1007. if (ret < 0) {
  1008. pr_debug("snd_pcm_hw_constraint_integer failed\n");
  1009. goto done;
  1010. }
  1011. tp_val = get_tappnt_value(substream->pcm->id);
  1012. tmd = hpcm_get_tappnt_mixer_data(substream->pcm->id, prtd);
  1013. /*Check whehter the kcontrol values set are valid*/
  1014. if (!tmd ||
  1015. !(tmd->enable) ||
  1016. !hpcm_is_valid_config(prtd->mixer_conf.sess_indx,
  1017. tp_val, tmd->direction,
  1018. tmd->sample_rate)) {
  1019. ret = -EINVAL;
  1020. goto done;
  1021. }
  1022. dai_data = hpcm_get_dai_data(substream->pcm->id, prtd);
  1023. dai_data->substream = substream;
  1024. runtime->private_data = prtd;
  1025. done:
  1026. mutex_unlock(&prtd->lock);
  1027. return ret;
  1028. }
  1029. static struct snd_pcm_ops msm_pcm_ops = {
  1030. .open = msm_pcm_open,
  1031. .hw_params = msm_pcm_hw_params,
  1032. .prepare = msm_pcm_prepare,
  1033. .trigger = msm_pcm_trigger,
  1034. .pointer = msm_pcm_pointer,
  1035. .copy = msm_pcm_copy,
  1036. .close = msm_pcm_close,
  1037. };
  1038. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1039. {
  1040. struct snd_card *card = rtd->card->snd_card;
  1041. pr_debug("%s:\n", __func__);
  1042. if (!card->dev->coherent_dma_mask)
  1043. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1044. return 0;
  1045. }
  1046. static int msm_pcm_hpcm_probe(struct snd_soc_platform *platform)
  1047. {
  1048. snd_soc_add_platform_controls(platform, msm_hpcm_controls,
  1049. ARRAY_SIZE(msm_hpcm_controls));
  1050. return 0;
  1051. }
  1052. static struct snd_soc_platform_driver msm_soc_platform = {
  1053. .ops = &msm_pcm_ops,
  1054. .pcm_new = msm_asoc_pcm_new,
  1055. .probe = msm_pcm_hpcm_probe,
  1056. };
  1057. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  1058. {
  1059. pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1060. return snd_soc_register_platform(&pdev->dev, &msm_soc_platform);
  1061. }
  1062. static int msm_pcm_remove(struct platform_device *pdev)
  1063. {
  1064. snd_soc_unregister_platform(&pdev->dev);
  1065. return 0;
  1066. }
  1067. static struct platform_driver msm_pcm_driver = {
  1068. .driver = {
  1069. .name = "msm-host-pcm-voice",
  1070. .owner = THIS_MODULE,
  1071. },
  1072. .probe = msm_pcm_probe,
  1073. .remove = __devexit_p(msm_pcm_remove),
  1074. };
  1075. static int __init msm_soc_platform_init(void)
  1076. {
  1077. int i = 0;
  1078. struct session *s = NULL;
  1079. memset(&hpcm_drv, 0, sizeof(hpcm_drv));
  1080. mutex_init(&hpcm_drv.lock);
  1081. hpcm_drv.ion_client = msm_ion_client_create(UINT_MAX, "host_voice_pcm");
  1082. if (IS_ERR_OR_NULL((void *)hpcm_drv.ion_client)) {
  1083. pr_err("%s: ION create client failed\n", __func__);
  1084. return -ENOMEM;
  1085. }
  1086. for (i = 0; i < MAX_SESSION; i++) {
  1087. s = &hpcm_drv.session[i];
  1088. spin_lock_init(&s->rx_tap_point.capture_dai_data.dsp_lock);
  1089. spin_lock_init(&s->rx_tap_point.playback_dai_data.dsp_lock);
  1090. spin_lock_init(&s->tx_tap_point.capture_dai_data.dsp_lock);
  1091. spin_lock_init(&s->tx_tap_point.playback_dai_data.dsp_lock);
  1092. init_waitqueue_head(
  1093. &s->rx_tap_point.capture_dai_data.queue_wait);
  1094. init_waitqueue_head(
  1095. &s->rx_tap_point.playback_dai_data.queue_wait);
  1096. init_waitqueue_head(
  1097. &s->tx_tap_point.capture_dai_data.queue_wait);
  1098. init_waitqueue_head(
  1099. &s->tx_tap_point.playback_dai_data.queue_wait);
  1100. INIT_LIST_HEAD(&s->rx_tap_point.capture_dai_data.filled_queue);
  1101. INIT_LIST_HEAD(&s->rx_tap_point.capture_dai_data.free_queue);
  1102. INIT_LIST_HEAD(&s->rx_tap_point.playback_dai_data.filled_queue);
  1103. INIT_LIST_HEAD(&s->rx_tap_point.playback_dai_data.free_queue);
  1104. INIT_LIST_HEAD(&s->tx_tap_point.capture_dai_data.filled_queue);
  1105. INIT_LIST_HEAD(&s->tx_tap_point.capture_dai_data.free_queue);
  1106. INIT_LIST_HEAD(&s->tx_tap_point.playback_dai_data.filled_queue);
  1107. INIT_LIST_HEAD(&s->tx_tap_point.playback_dai_data.free_queue);
  1108. }
  1109. voc_register_hpcm_evt_cb(hpcm_notify_evt_processing, &hpcm_drv);
  1110. return platform_driver_register(&msm_pcm_driver);
  1111. }
  1112. module_init(msm_soc_platform_init);
  1113. static void __exit msm_soc_platform_exit(void)
  1114. {
  1115. platform_driver_unregister(&msm_pcm_driver);
  1116. }
  1117. module_exit(msm_soc_platform_exit);
  1118. MODULE_DESCRIPTION("PCM module platform driver");
  1119. MODULE_LICENSE("GPL v2");