msm-pcm-host-voice-v2.c 43 KB

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