msm-pcm-q6-v2.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012-2019, 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/moduleparam.h>
  8. #include <linux/time.h>
  9. #include <linux/wait.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.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 <sound/timer.h>
  19. #include <asm/dma.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/msm_audio.h>
  22. #include <linux/of_device.h>
  23. #include <sound/tlv.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/devdep_params.h>
  26. #include <dsp/msm_audio_ion.h>
  27. #include <dsp/q6audio-v2.h>
  28. #include <dsp/q6core.h>
  29. #include <dsp/q6asm-v2.h>
  30. #include "msm-pcm-q6-v2.h"
  31. #include "msm-pcm-routing-v2.h"
  32. #include "msm-qti-pp-config.h"
  33. #define DRV_NAME "msm-pcm-q6-v2"
  34. #define TIMEOUT_MS 1000
  35. enum stream_state {
  36. IDLE = 0,
  37. STOPPED,
  38. RUNNING,
  39. };
  40. static struct audio_locks the_locks;
  41. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  42. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  43. PCM_MASTER_VOL_MAX_STEPS);
  44. struct snd_msm {
  45. struct snd_card *card;
  46. struct snd_pcm *pcm;
  47. };
  48. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  49. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  50. #define MAX_PB_COPY_RETRIES 3
  51. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  52. .info = (SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  54. SNDRV_PCM_INFO_MMAP_VALID |
  55. SNDRV_PCM_INFO_INTERLEAVED |
  56. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  57. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  58. SNDRV_PCM_FMTBIT_S24_LE |
  59. SNDRV_PCM_FMTBIT_S24_3LE |
  60. SNDRV_PCM_FMTBIT_S32_LE),
  61. .rates = SNDRV_PCM_RATE_8000_384000,
  62. .rate_min = 8000,
  63. .rate_max = 384000,
  64. .channels_min = 1,
  65. .channels_max = 4,
  66. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  67. CAPTURE_MAX_PERIOD_SIZE,
  68. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  69. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  70. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  71. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  72. .fifo_size = 0,
  73. };
  74. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  75. .info = (SNDRV_PCM_INFO_MMAP |
  76. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  77. SNDRV_PCM_INFO_MMAP_VALID |
  78. SNDRV_PCM_INFO_INTERLEAVED |
  79. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  80. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  81. SNDRV_PCM_FMTBIT_S24_LE |
  82. SNDRV_PCM_FMTBIT_S24_3LE |
  83. SNDRV_PCM_FMTBIT_S32_LE),
  84. .rates = SNDRV_PCM_RATE_8000_384000,
  85. .rate_min = 8000,
  86. .rate_max = 384000,
  87. .channels_min = 1,
  88. .channels_max = 8,
  89. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  90. PLAYBACK_MAX_PERIOD_SIZE,
  91. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  92. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  93. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  94. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  95. .fifo_size = 0,
  96. };
  97. /* Conventional and unconventional sample rate supported */
  98. static unsigned int supported_sample_rates[] = {
  99. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  100. 88200, 96000, 176400, 192000, 352800, 384000
  101. };
  102. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  103. .count = ARRAY_SIZE(supported_sample_rates),
  104. .list = supported_sample_rates,
  105. .mask = 0,
  106. };
  107. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  108. void *priv_data)
  109. {
  110. struct msm_audio *prtd = priv_data;
  111. WARN_ON(!prtd);
  112. pr_debug("%s: event %x\n", __func__, event);
  113. switch (event) {
  114. case MSM_PCM_RT_EVT_BUF_RECFG:
  115. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  116. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  117. q6asm_run(prtd->audio_client, 0, 0, 0);
  118. /* fallthrough */
  119. default:
  120. break;
  121. }
  122. }
  123. static void event_handler(uint32_t opcode,
  124. uint32_t token, uint32_t *payload, void *priv)
  125. {
  126. struct msm_audio *prtd = priv;
  127. struct snd_pcm_substream *substream = prtd->substream;
  128. uint32_t *ptrmem = (uint32_t *)payload;
  129. uint32_t idx = 0;
  130. uint32_t size = 0;
  131. uint8_t buf_index;
  132. struct snd_soc_pcm_runtime *rtd;
  133. int ret = 0;
  134. switch (opcode) {
  135. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  136. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  137. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  138. prtd->pcm_irq_pos += prtd->pcm_count;
  139. if (atomic_read(&prtd->start))
  140. snd_pcm_period_elapsed(substream);
  141. atomic_inc(&prtd->out_count);
  142. wake_up(&the_locks.write_wait);
  143. if (!atomic_read(&prtd->start))
  144. break;
  145. if (!prtd->mmap_flag || prtd->reset_event)
  146. break;
  147. if (q6asm_is_cpu_buf_avail_nolock(IN,
  148. prtd->audio_client,
  149. &size, &idx)) {
  150. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  151. __func__, prtd->pcm_count);
  152. q6asm_write_nolock(prtd->audio_client,
  153. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  154. }
  155. break;
  156. }
  157. case ASM_DATA_EVENT_RENDERED_EOS:
  158. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  159. clear_bit(CMD_EOS, &prtd->cmd_pending);
  160. wake_up(&the_locks.eos_wait);
  161. break;
  162. case ASM_DATA_EVENT_READ_DONE_V2: {
  163. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  164. buf_index = q6asm_get_buf_index_from_token(token);
  165. if (buf_index >= CAPTURE_MAX_NUM_PERIODS) {
  166. pr_err("%s: buffer index %u is out of range.\n",
  167. __func__, buf_index);
  168. return;
  169. }
  170. pr_debug("%s: token=0x%08x buf_index=0x%08x\n",
  171. __func__, token, buf_index);
  172. prtd->in_frame_info[buf_index].size = payload[4];
  173. prtd->in_frame_info[buf_index].offset = payload[5];
  174. /* assume data size = 0 during flushing */
  175. if (prtd->in_frame_info[buf_index].size) {
  176. prtd->pcm_irq_pos +=
  177. prtd->in_frame_info[buf_index].size;
  178. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  179. if (atomic_read(&prtd->start))
  180. snd_pcm_period_elapsed(substream);
  181. if (atomic_read(&prtd->in_count) <= prtd->periods)
  182. atomic_inc(&prtd->in_count);
  183. wake_up(&the_locks.read_wait);
  184. if (prtd->mmap_flag &&
  185. q6asm_is_cpu_buf_avail_nolock(OUT,
  186. prtd->audio_client,
  187. &size, &idx) &&
  188. (substream->runtime->status->state ==
  189. SNDRV_PCM_STATE_RUNNING))
  190. q6asm_read_nolock(prtd->audio_client);
  191. } else {
  192. pr_debug("%s: reclaim flushed buf in_count %x\n",
  193. __func__, atomic_read(&prtd->in_count));
  194. prtd->pcm_irq_pos += prtd->pcm_count;
  195. if (prtd->mmap_flag) {
  196. if (q6asm_is_cpu_buf_avail_nolock(OUT,
  197. prtd->audio_client,
  198. &size, &idx) &&
  199. (substream->runtime->status->state ==
  200. SNDRV_PCM_STATE_RUNNING))
  201. q6asm_read_nolock(prtd->audio_client);
  202. } else {
  203. atomic_inc(&prtd->in_count);
  204. }
  205. if (atomic_read(&prtd->in_count) == prtd->periods) {
  206. pr_info("%s: reclaimed all bufs\n", __func__);
  207. if (atomic_read(&prtd->start))
  208. snd_pcm_period_elapsed(substream);
  209. wake_up(&the_locks.read_wait);
  210. }
  211. }
  212. break;
  213. }
  214. case ASM_STREAM_PP_EVENT:
  215. case ASM_STREAM_CMD_ENCDEC_EVENTS: {
  216. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  217. if (!substream) {
  218. pr_err("%s: substream is NULL.\n", __func__);
  219. return;
  220. }
  221. rtd = substream->private_data;
  222. if (!rtd) {
  223. pr_err("%s: rtd is NULL\n", __func__);
  224. return;
  225. }
  226. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  227. if (ret) {
  228. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  229. __func__, ret);
  230. return;
  231. }
  232. break;
  233. }
  234. case APR_BASIC_RSP_RESULT: {
  235. switch (payload[0]) {
  236. case ASM_SESSION_CMD_RUN_V2:
  237. if (substream->stream
  238. != SNDRV_PCM_STREAM_PLAYBACK) {
  239. atomic_set(&prtd->start, 1);
  240. break;
  241. }
  242. if (prtd->mmap_flag) {
  243. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  244. __func__,
  245. prtd->pcm_count);
  246. q6asm_write_nolock(prtd->audio_client,
  247. prtd->pcm_count,
  248. 0, 0, NO_TIMESTAMP);
  249. } else {
  250. while (atomic_read(&prtd->out_needed)) {
  251. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  252. __func__,
  253. prtd->pcm_count);
  254. q6asm_write_nolock(prtd->audio_client,
  255. prtd->pcm_count,
  256. 0, 0, NO_TIMESTAMP);
  257. atomic_dec(&prtd->out_needed);
  258. wake_up(&the_locks.write_wait);
  259. };
  260. }
  261. atomic_set(&prtd->start, 1);
  262. break;
  263. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  264. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:",
  265. __func__);
  266. break;
  267. default:
  268. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  269. __func__, payload[0], payload[1]);
  270. break;
  271. }
  272. }
  273. break;
  274. case RESET_EVENTS:
  275. pr_debug("%s RESET_EVENTS\n", __func__);
  276. prtd->pcm_irq_pos += prtd->pcm_count;
  277. atomic_inc(&prtd->out_count);
  278. atomic_inc(&prtd->in_count);
  279. prtd->reset_event = true;
  280. if (atomic_read(&prtd->start))
  281. snd_pcm_period_elapsed(substream);
  282. wake_up(&the_locks.eos_wait);
  283. wake_up(&the_locks.write_wait);
  284. wake_up(&the_locks.read_wait);
  285. break;
  286. default:
  287. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  288. break;
  289. }
  290. }
  291. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  292. {
  293. struct snd_pcm_runtime *runtime = substream->runtime;
  294. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  295. struct snd_soc_component *component =
  296. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  297. struct msm_audio *prtd = runtime->private_data;
  298. struct msm_plat_data *pdata;
  299. struct snd_pcm_hw_params *params;
  300. int ret;
  301. uint32_t fmt_type = FORMAT_LINEAR_PCM;
  302. uint16_t bits_per_sample;
  303. uint16_t sample_word_size;
  304. if (!component) {
  305. pr_err("%s: component is NULL\n", __func__);
  306. return -EINVAL;
  307. }
  308. pdata = (struct msm_plat_data *)
  309. dev_get_drvdata(component->dev);
  310. if (!pdata) {
  311. pr_err("%s: platform data not populated\n", __func__);
  312. return -EINVAL;
  313. }
  314. if (!prtd || !prtd->audio_client) {
  315. pr_err("%s: private data null or audio client freed\n",
  316. __func__);
  317. return -EINVAL;
  318. }
  319. params = &soc_prtd->dpcm[substream->stream].hw_params;
  320. pr_debug("%s\n", __func__);
  321. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  322. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  323. prtd->pcm_irq_pos = 0;
  324. /* rate and channels are sent to audio driver */
  325. prtd->samp_rate = runtime->rate;
  326. prtd->channel_mode = runtime->channels;
  327. if (prtd->enabled)
  328. return 0;
  329. prtd->audio_client->perf_mode = pdata->perf_mode;
  330. pr_debug("%s: perf: %x\n", __func__, pdata->perf_mode);
  331. switch (params_format(params)) {
  332. case SNDRV_PCM_FORMAT_S32_LE:
  333. bits_per_sample = 32;
  334. sample_word_size = 32;
  335. break;
  336. case SNDRV_PCM_FORMAT_S24_LE:
  337. bits_per_sample = 24;
  338. sample_word_size = 32;
  339. break;
  340. case SNDRV_PCM_FORMAT_S24_3LE:
  341. bits_per_sample = 24;
  342. sample_word_size = 24;
  343. break;
  344. case SNDRV_PCM_FORMAT_S16_LE:
  345. default:
  346. bits_per_sample = 16;
  347. sample_word_size = 16;
  348. break;
  349. }
  350. if (prtd->compress_enable) {
  351. fmt_type = FORMAT_GEN_COMPR;
  352. pr_debug("%s: Compressed enabled!\n", __func__);
  353. ret = q6asm_open_write_compressed(prtd->audio_client, fmt_type,
  354. COMPRESSED_PASSTHROUGH_GEN);
  355. if (ret < 0) {
  356. pr_err("%s: q6asm_open_write_compressed failed (%d)\n",
  357. __func__, ret);
  358. q6asm_audio_client_free(prtd->audio_client);
  359. prtd->audio_client = NULL;
  360. return -ENOMEM;
  361. }
  362. } else {
  363. if ((q6core_get_avcs_api_version_per_service(
  364. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  365. ADSP_ASM_API_VERSION_V2))
  366. ret = q6asm_open_write_v5(prtd->audio_client,
  367. fmt_type, bits_per_sample);
  368. else
  369. ret = q6asm_open_write_v4(prtd->audio_client,
  370. fmt_type, bits_per_sample);
  371. if (ret < 0) {
  372. pr_err("%s: q6asm_open_write failed (%d)\n",
  373. __func__, ret);
  374. q6asm_audio_client_free(prtd->audio_client);
  375. prtd->audio_client = NULL;
  376. return -ENOMEM;
  377. }
  378. ret = q6asm_send_cal(prtd->audio_client);
  379. if (ret < 0)
  380. pr_debug("%s : Send cal failed : %d", __func__, ret);
  381. }
  382. pr_debug("%s: session ID %d\n", __func__,
  383. prtd->audio_client->session);
  384. prtd->session_id = prtd->audio_client->session;
  385. if (prtd->compress_enable) {
  386. ret = msm_pcm_routing_reg_phy_compr_stream(
  387. soc_prtd->dai_link->id,
  388. prtd->audio_client->perf_mode,
  389. prtd->session_id,
  390. SNDRV_PCM_STREAM_PLAYBACK,
  391. COMPRESSED_PASSTHROUGH_GEN);
  392. } else {
  393. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  394. prtd->audio_client->perf_mode,
  395. prtd->session_id, substream->stream);
  396. }
  397. if (ret) {
  398. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  399. return ret;
  400. }
  401. if (prtd->compress_enable) {
  402. ret = q6asm_media_format_block_gen_compr(
  403. prtd->audio_client, runtime->rate,
  404. runtime->channels, !prtd->set_channel_map,
  405. prtd->channel_map, bits_per_sample);
  406. } else {
  407. if ((q6core_get_avcs_api_version_per_service(
  408. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  409. ADSP_ASM_API_VERSION_V2)) {
  410. ret = q6asm_media_format_block_multi_ch_pcm_v5(
  411. prtd->audio_client, runtime->rate,
  412. runtime->channels, !prtd->set_channel_map,
  413. prtd->channel_map, bits_per_sample,
  414. sample_word_size, ASM_LITTLE_ENDIAN,
  415. DEFAULT_QF);
  416. } else {
  417. ret = q6asm_media_format_block_multi_ch_pcm_v4(
  418. prtd->audio_client, runtime->rate,
  419. runtime->channels, !prtd->set_channel_map,
  420. prtd->channel_map, bits_per_sample,
  421. sample_word_size, ASM_LITTLE_ENDIAN,
  422. DEFAULT_QF);
  423. }
  424. }
  425. if (ret < 0)
  426. pr_info("%s: CMD Format block failed\n", __func__);
  427. atomic_set(&prtd->out_count, runtime->periods);
  428. prtd->enabled = 1;
  429. prtd->cmd_pending = 0;
  430. prtd->cmd_interrupt = 0;
  431. return 0;
  432. }
  433. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  434. {
  435. struct snd_pcm_runtime *runtime = substream->runtime;
  436. struct msm_audio *prtd = runtime->private_data;
  437. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  438. struct snd_soc_component *component =
  439. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  440. struct msm_plat_data *pdata;
  441. struct snd_pcm_hw_params *params;
  442. struct msm_pcm_routing_evt event;
  443. int ret = 0;
  444. int i = 0;
  445. uint16_t bits_per_sample = 16;
  446. uint16_t sample_word_size;
  447. if (!component) {
  448. pr_err("%s: component is NULL\n", __func__);
  449. return -EINVAL;
  450. }
  451. pdata = (struct msm_plat_data *)
  452. dev_get_drvdata(component->dev);
  453. if (!pdata) {
  454. pr_err("%s: platform data not populated\n", __func__);
  455. return -EINVAL;
  456. }
  457. if (!prtd || !prtd->audio_client) {
  458. pr_err("%s: private data null or audio client freed\n",
  459. __func__);
  460. return -EINVAL;
  461. }
  462. if (prtd->enabled == IDLE) {
  463. pr_debug("%s:perf_mode=%d periods=%d\n", __func__,
  464. pdata->perf_mode, runtime->periods);
  465. params = &soc_prtd->dpcm[substream->stream].hw_params;
  466. if ((params_format(params) == SNDRV_PCM_FORMAT_S24_LE) ||
  467. (params_format(params) == SNDRV_PCM_FORMAT_S24_3LE))
  468. bits_per_sample = 24;
  469. else if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
  470. bits_per_sample = 32;
  471. /* ULL mode is not supported in capture path */
  472. if (pdata->perf_mode == LEGACY_PCM_MODE)
  473. prtd->audio_client->perf_mode = LEGACY_PCM_MODE;
  474. else
  475. prtd->audio_client->perf_mode = LOW_LATENCY_PCM_MODE;
  476. pr_debug("%s Opening %d-ch PCM read stream, perf_mode %d\n",
  477. __func__, params_channels(params),
  478. prtd->audio_client->perf_mode);
  479. if ((q6core_get_avcs_api_version_per_service(
  480. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  481. ADSP_ASM_API_VERSION_V2))
  482. ret = q6asm_open_read_v5(prtd->audio_client,
  483. FORMAT_LINEAR_PCM,
  484. bits_per_sample, false, ENC_CFG_ID_NONE);
  485. else
  486. ret = q6asm_open_read_v4(prtd->audio_client,
  487. FORMAT_LINEAR_PCM,
  488. bits_per_sample, false, ENC_CFG_ID_NONE);
  489. if (ret < 0) {
  490. pr_err("%s: q6asm_open_read failed\n", __func__);
  491. q6asm_audio_client_free(prtd->audio_client);
  492. prtd->audio_client = NULL;
  493. return -ENOMEM;
  494. }
  495. ret = q6asm_send_cal(prtd->audio_client);
  496. if (ret < 0)
  497. pr_debug("%s : Send cal failed : %d", __func__, ret);
  498. pr_debug("%s: session ID %d\n",
  499. __func__, prtd->audio_client->session);
  500. prtd->session_id = prtd->audio_client->session;
  501. event.event_func = msm_pcm_route_event_handler;
  502. event.priv_data = (void *) prtd;
  503. ret = msm_pcm_routing_reg_phy_stream_v2(
  504. soc_prtd->dai_link->id,
  505. prtd->audio_client->perf_mode,
  506. prtd->session_id, substream->stream,
  507. event);
  508. if (ret) {
  509. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  510. return ret;
  511. }
  512. }
  513. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  514. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  515. prtd->pcm_irq_pos = 0;
  516. /* rate and channels are sent to audio driver */
  517. prtd->samp_rate = runtime->rate;
  518. prtd->channel_mode = runtime->channels;
  519. if (prtd->enabled == IDLE || prtd->enabled == STOPPED) {
  520. for (i = 0; i < runtime->periods; i++)
  521. q6asm_read(prtd->audio_client);
  522. prtd->periods = runtime->periods;
  523. }
  524. if (prtd->enabled != IDLE)
  525. return 0;
  526. switch (runtime->format) {
  527. case SNDRV_PCM_FORMAT_S32_LE:
  528. bits_per_sample = 32;
  529. sample_word_size = 32;
  530. break;
  531. case SNDRV_PCM_FORMAT_S24_LE:
  532. bits_per_sample = 24;
  533. sample_word_size = 32;
  534. break;
  535. case SNDRV_PCM_FORMAT_S24_3LE:
  536. bits_per_sample = 24;
  537. sample_word_size = 24;
  538. break;
  539. case SNDRV_PCM_FORMAT_S16_LE:
  540. default:
  541. bits_per_sample = 16;
  542. sample_word_size = 16;
  543. break;
  544. }
  545. pr_debug("%s: Samp_rate = %d Channel = %d bit width = %d, word size = %d\n",
  546. __func__, prtd->samp_rate, prtd->channel_mode,
  547. bits_per_sample, sample_word_size);
  548. if ((q6core_get_avcs_api_version_per_service(
  549. APRV2_IDS_SERVICE_ID_ADSP_ASM_V) >=
  550. ADSP_ASM_API_VERSION_V2))
  551. ret = q6asm_enc_cfg_blk_pcm_format_support_v5(
  552. prtd->audio_client,
  553. prtd->samp_rate,
  554. prtd->channel_mode,
  555. bits_per_sample,
  556. sample_word_size,
  557. ASM_LITTLE_ENDIAN,
  558. DEFAULT_QF);
  559. else
  560. ret = q6asm_enc_cfg_blk_pcm_format_support_v4(
  561. prtd->audio_client,
  562. prtd->samp_rate,
  563. prtd->channel_mode,
  564. bits_per_sample,
  565. sample_word_size,
  566. ASM_LITTLE_ENDIAN,
  567. DEFAULT_QF);
  568. if (ret < 0)
  569. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  570. prtd->enabled = RUNNING;
  571. return ret;
  572. }
  573. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  574. {
  575. int ret = 0;
  576. struct snd_pcm_runtime *runtime = substream->runtime;
  577. struct msm_audio *prtd = runtime->private_data;
  578. switch (cmd) {
  579. case SNDRV_PCM_TRIGGER_START:
  580. case SNDRV_PCM_TRIGGER_RESUME:
  581. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  582. pr_debug("%s: Trigger start\n", __func__);
  583. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  584. break;
  585. case SNDRV_PCM_TRIGGER_STOP:
  586. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  587. atomic_set(&prtd->start, 0);
  588. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
  589. prtd->enabled = STOPPED;
  590. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  591. break;
  592. }
  593. /* pending CMD_EOS isn't expected */
  594. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  595. set_bit(CMD_EOS, &prtd->cmd_pending);
  596. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  597. if (ret)
  598. clear_bit(CMD_EOS, &prtd->cmd_pending);
  599. break;
  600. case SNDRV_PCM_TRIGGER_SUSPEND:
  601. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  602. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  603. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  604. atomic_set(&prtd->start, 0);
  605. break;
  606. default:
  607. ret = -EINVAL;
  608. break;
  609. }
  610. return ret;
  611. }
  612. static int msm_pcm_open(struct snd_pcm_substream *substream)
  613. {
  614. struct snd_pcm_runtime *runtime = substream->runtime;
  615. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  616. struct snd_soc_component *component =
  617. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  618. struct msm_audio *prtd;
  619. struct msm_plat_data *pdata;
  620. enum apr_subsys_state subsys_state;
  621. int ret = 0;
  622. if (!component) {
  623. pr_err("%s: component is NULL\n", __func__);
  624. return -EINVAL;
  625. }
  626. pdata = (struct msm_plat_data *)
  627. dev_get_drvdata(component->dev);
  628. if (!pdata) {
  629. pr_err("%s: platform data not populated\n", __func__);
  630. return -EINVAL;
  631. }
  632. subsys_state = apr_get_subsys_state();
  633. if (subsys_state == APR_SUBSYS_DOWN) {
  634. pr_debug("%s: adsp is down\n", __func__);
  635. return -ENETRESET;
  636. }
  637. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  638. if (prtd == NULL)
  639. return -ENOMEM;
  640. prtd->substream = substream;
  641. prtd->audio_client = q6asm_audio_client_alloc(
  642. (app_cb)event_handler, prtd);
  643. if (!prtd->audio_client) {
  644. pr_info("%s: Could not allocate memory\n", __func__);
  645. kfree(prtd);
  646. prtd = NULL;
  647. return -ENOMEM;
  648. }
  649. prtd->audio_client->dev = component->dev;
  650. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  651. runtime->hw = msm_pcm_hardware_playback;
  652. /* Capture path */
  653. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  654. runtime->hw = msm_pcm_hardware_capture;
  655. else {
  656. pr_err("Invalid Stream type %d\n", substream->stream);
  657. return -EINVAL;
  658. }
  659. ret = snd_pcm_hw_constraint_list(runtime, 0,
  660. SNDRV_PCM_HW_PARAM_RATE,
  661. &constraints_sample_rates);
  662. if (ret < 0)
  663. pr_info("snd_pcm_hw_constraint_list failed\n");
  664. /* Ensure that buffer size is a multiple of period size */
  665. ret = snd_pcm_hw_constraint_integer(runtime,
  666. SNDRV_PCM_HW_PARAM_PERIODS);
  667. if (ret < 0)
  668. pr_info("snd_pcm_hw_constraint_integer failed\n");
  669. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  670. ret = snd_pcm_hw_constraint_minmax(runtime,
  671. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  672. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  673. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  674. if (ret < 0) {
  675. pr_err("constraint for buffer bytes min max ret = %d\n",
  676. ret);
  677. }
  678. }
  679. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  680. ret = snd_pcm_hw_constraint_minmax(runtime,
  681. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  682. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  683. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  684. if (ret < 0) {
  685. pr_err("constraint for buffer bytes min max ret = %d\n",
  686. ret);
  687. }
  688. }
  689. ret = snd_pcm_hw_constraint_step(runtime, 0,
  690. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  691. if (ret < 0) {
  692. pr_err("constraint for period bytes step ret = %d\n",
  693. ret);
  694. }
  695. ret = snd_pcm_hw_constraint_step(runtime, 0,
  696. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  697. if (ret < 0) {
  698. pr_err("constraint for buffer bytes step ret = %d\n",
  699. ret);
  700. }
  701. prtd->enabled = IDLE;
  702. prtd->dsp_cnt = 0;
  703. prtd->set_channel_map = false;
  704. prtd->reset_event = false;
  705. runtime->private_data = prtd;
  706. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  707. msm_adsp_init_mixer_ctl_pp_event_queue(soc_prtd);
  708. /* Vote to update the Rx thread priority to RT Thread for playback */
  709. if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
  710. (pdata->perf_mode == LOW_LATENCY_PCM_MODE))
  711. apr_start_rx_rt(prtd->audio_client->apr);
  712. return 0;
  713. }
  714. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  715. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  716. {
  717. int ret = 0;
  718. int xfer = 0;
  719. char *bufptr = NULL;
  720. void *data = NULL;
  721. uint32_t idx = 0;
  722. uint32_t size = 0;
  723. uint32_t retries = 0;
  724. struct snd_pcm_runtime *runtime = substream->runtime;
  725. struct msm_audio *prtd = runtime->private_data;
  726. pr_debug("%s: prtd->out_count = %d\n",
  727. __func__, atomic_read(&prtd->out_count));
  728. while ((fbytes > 0) && (retries < MAX_PB_COPY_RETRIES)) {
  729. if (prtd->reset_event) {
  730. pr_err("%s: In SSR return ENETRESET before wait\n",
  731. __func__);
  732. return -ENETRESET;
  733. }
  734. ret = wait_event_timeout(the_locks.write_wait,
  735. (atomic_read(&prtd->out_count)),
  736. msecs_to_jiffies(TIMEOUT_MS));
  737. if (!ret) {
  738. pr_err("%s: wait_event_timeout failed\n", __func__);
  739. ret = -ETIMEDOUT;
  740. goto fail;
  741. }
  742. ret = 0;
  743. if (prtd->reset_event) {
  744. pr_err("%s: In SSR return ENETRESET after wait\n",
  745. __func__);
  746. return -ENETRESET;
  747. }
  748. if (!atomic_read(&prtd->out_count)) {
  749. pr_err("%s: pcm stopped out_count 0\n", __func__);
  750. return 0;
  751. }
  752. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size,
  753. &idx);
  754. if (data == NULL) {
  755. retries++;
  756. continue;
  757. } else {
  758. retries = 0;
  759. }
  760. if (fbytes > size)
  761. xfer = size;
  762. else
  763. xfer = fbytes;
  764. bufptr = data;
  765. if (bufptr) {
  766. pr_debug("%s:fbytes =%lu: xfer=%d size=%d\n",
  767. __func__, fbytes, xfer, size);
  768. if (copy_from_user(bufptr, buf, xfer)) {
  769. ret = -EFAULT;
  770. pr_err("%s: copy_from_user failed\n",
  771. __func__);
  772. q6asm_cpu_buf_release(IN, prtd->audio_client);
  773. goto fail;
  774. }
  775. buf += xfer;
  776. fbytes -= xfer;
  777. pr_debug("%s:fbytes = %lu: xfer=%d\n", __func__,
  778. fbytes, xfer);
  779. if (atomic_read(&prtd->start)) {
  780. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  781. __func__, xfer);
  782. ret = q6asm_write(prtd->audio_client, xfer,
  783. 0, 0, NO_TIMESTAMP);
  784. if (ret < 0) {
  785. ret = -EFAULT;
  786. q6asm_cpu_buf_release(IN,
  787. prtd->audio_client);
  788. goto fail;
  789. }
  790. } else
  791. atomic_inc(&prtd->out_needed);
  792. atomic_dec(&prtd->out_count);
  793. }
  794. }
  795. fail:
  796. if (retries >= MAX_PB_COPY_RETRIES)
  797. ret = -ENOMEM;
  798. return ret;
  799. }
  800. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  801. {
  802. struct snd_pcm_runtime *runtime = substream->runtime;
  803. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  804. struct msm_audio *prtd = runtime->private_data;
  805. struct snd_soc_component *component =
  806. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  807. struct msm_plat_data *pdata;
  808. uint32_t timeout;
  809. int dir = 0;
  810. int ret = 0;
  811. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  812. if (prtd->audio_client) {
  813. dir = IN;
  814. if (!component) {
  815. pr_err("%s: component is NULL\n", __func__);
  816. return -EINVAL;
  817. }
  818. /*
  819. * Unvote to downgrade the Rx thread priority from
  820. * RT Thread for Low-Latency use case.
  821. */
  822. pdata = (struct msm_plat_data *)
  823. dev_get_drvdata(component->dev);
  824. if (pdata) {
  825. if (pdata->perf_mode == LOW_LATENCY_PCM_MODE)
  826. apr_end_rx_rt(prtd->audio_client->apr);
  827. }
  828. /* determine timeout length */
  829. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  830. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  831. } else {
  832. timeout = (runtime->period_size *
  833. CMD_EOS_TIMEOUT_MULTIPLIER) /
  834. ((runtime->frame_bits / 8) *
  835. runtime->rate);
  836. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  837. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  838. }
  839. pr_debug("%s: CMD_EOS timeout is %d\n", __func__, timeout);
  840. ret = wait_event_timeout(the_locks.eos_wait,
  841. !test_bit(CMD_EOS, &prtd->cmd_pending),
  842. timeout);
  843. if (!ret)
  844. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  845. __func__, prtd->cmd_pending);
  846. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  847. q6asm_audio_client_buf_free_contiguous(dir,
  848. prtd->audio_client);
  849. q6asm_audio_client_free(prtd->audio_client);
  850. }
  851. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  852. SNDRV_PCM_STREAM_PLAYBACK);
  853. msm_adsp_clean_mixer_ctl_pp_event_queue(soc_prtd);
  854. kfree(prtd);
  855. runtime->private_data = NULL;
  856. return 0;
  857. }
  858. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  859. int channel, unsigned long hwoff, void __user *buf,
  860. unsigned long fbytes)
  861. {
  862. int ret = 0;
  863. int xfer;
  864. char *bufptr;
  865. void *data = NULL;
  866. uint32_t idx = 0;
  867. uint32_t size = 0;
  868. uint32_t offset = 0;
  869. struct snd_pcm_runtime *runtime = substream->runtime;
  870. struct msm_audio *prtd = substream->runtime->private_data;
  871. pr_debug("%s\n", __func__);
  872. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  873. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  874. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  875. if (prtd->reset_event) {
  876. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  877. return -ENETRESET;
  878. }
  879. ret = wait_event_timeout(the_locks.read_wait,
  880. (atomic_read(&prtd->in_count)),
  881. msecs_to_jiffies(TIMEOUT_MS));
  882. if (!ret) {
  883. pr_debug("%s: wait_event_timeout failed\n", __func__);
  884. goto fail;
  885. }
  886. if (prtd->reset_event) {
  887. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  888. return -ENETRESET;
  889. }
  890. if (!atomic_read(&prtd->in_count)) {
  891. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  892. return 0;
  893. }
  894. pr_debug("Checking if valid buffer is available...%pK\n",
  895. data);
  896. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  897. bufptr = data;
  898. pr_debug("Size = %d\n", size);
  899. pr_debug("fbytes = %lu\n", fbytes);
  900. pr_debug("idx = %d\n", idx);
  901. if (bufptr) {
  902. xfer = fbytes;
  903. if (xfer > size)
  904. xfer = size;
  905. offset = prtd->in_frame_info[idx].offset;
  906. pr_debug("Offset value = %d\n", offset);
  907. if (copy_to_user(buf, bufptr+offset, xfer)) {
  908. pr_err("Failed to copy buf to user\n");
  909. ret = -EFAULT;
  910. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  911. goto fail;
  912. }
  913. fbytes -= xfer;
  914. size -= xfer;
  915. prtd->in_frame_info[idx].offset += xfer;
  916. pr_debug("%s:fbytes = %lu: size=%d: xfer=%d\n",
  917. __func__, fbytes, size, xfer);
  918. pr_debug(" Sending next buffer to dsp\n");
  919. memset(&prtd->in_frame_info[idx], 0,
  920. sizeof(struct msm_audio_in_frame_info));
  921. atomic_dec(&prtd->in_count);
  922. ret = q6asm_read(prtd->audio_client);
  923. if (ret < 0) {
  924. pr_err("q6asm read failed\n");
  925. ret = -EFAULT;
  926. q6asm_cpu_buf_release(OUT, prtd->audio_client);
  927. goto fail;
  928. }
  929. } else
  930. pr_err("No valid buffer\n");
  931. pr_debug("Returning from capture_copy... %d\n", ret);
  932. fail:
  933. return ret;
  934. }
  935. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  936. {
  937. struct snd_pcm_runtime *runtime = substream->runtime;
  938. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  939. struct msm_audio *prtd = runtime->private_data;
  940. int dir = OUT;
  941. pr_debug("%s\n", __func__);
  942. if (prtd->audio_client) {
  943. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  944. q6asm_audio_client_buf_free_contiguous(dir,
  945. prtd->audio_client);
  946. q6asm_audio_client_free(prtd->audio_client);
  947. }
  948. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  949. SNDRV_PCM_STREAM_CAPTURE);
  950. kfree(prtd);
  951. runtime->private_data = NULL;
  952. return 0;
  953. }
  954. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  955. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  956. {
  957. int ret = 0;
  958. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  959. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
  960. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  961. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
  962. return ret;
  963. }
  964. static int msm_pcm_close(struct snd_pcm_substream *substream)
  965. {
  966. int ret = 0;
  967. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  968. ret = msm_pcm_playback_close(substream);
  969. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  970. ret = msm_pcm_capture_close(substream);
  971. return ret;
  972. }
  973. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  974. {
  975. int ret = 0;
  976. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  977. ret = msm_pcm_playback_prepare(substream);
  978. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  979. ret = msm_pcm_capture_prepare(substream);
  980. return ret;
  981. }
  982. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  983. {
  984. struct snd_pcm_runtime *runtime = substream->runtime;
  985. struct msm_audio *prtd = runtime->private_data;
  986. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  987. prtd->pcm_irq_pos = 0;
  988. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  989. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  990. }
  991. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  992. struct vm_area_struct *vma)
  993. {
  994. struct snd_pcm_runtime *runtime = substream->runtime;
  995. struct msm_audio *prtd = runtime->private_data;
  996. struct audio_client *ac = prtd->audio_client;
  997. struct audio_port_data *apd = ac->port;
  998. struct audio_buffer *ab;
  999. int dir = -1;
  1000. prtd->mmap_flag = 1;
  1001. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1002. dir = IN;
  1003. else
  1004. dir = OUT;
  1005. ab = &(apd[dir].buf[0]);
  1006. return msm_audio_ion_mmap(ab, vma);
  1007. }
  1008. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1009. struct snd_pcm_hw_params *params)
  1010. {
  1011. struct snd_pcm_runtime *runtime = substream->runtime;
  1012. struct msm_audio *prtd = runtime->private_data;
  1013. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1014. struct audio_buffer *buf;
  1015. int dir, ret;
  1016. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1017. dir = IN;
  1018. else
  1019. dir = OUT;
  1020. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  1021. prtd->audio_client,
  1022. (params_buffer_bytes(params) / params_periods(params)),
  1023. params_periods(params));
  1024. if (ret < 0) {
  1025. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  1026. ret);
  1027. return -ENOMEM;
  1028. }
  1029. buf = prtd->audio_client->port[dir].buf;
  1030. if (buf == NULL || buf[0].data == NULL)
  1031. return -ENOMEM;
  1032. pr_debug("%s:buf = %pK\n", __func__, buf);
  1033. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1034. dma_buf->dev.dev = substream->pcm->card->dev;
  1035. dma_buf->private_data = NULL;
  1036. dma_buf->area = buf[0].data;
  1037. dma_buf->addr = buf[0].phys;
  1038. dma_buf->bytes = params_buffer_bytes(params);
  1039. if (!dma_buf->area)
  1040. return -ENOMEM;
  1041. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1042. return 0;
  1043. }
  1044. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  1045. unsigned int cmd, void __user *arg)
  1046. {
  1047. struct msm_audio *prtd = NULL;
  1048. struct snd_soc_pcm_runtime *rtd = NULL;
  1049. uint64_t ses_time = 0, abs_time = 0;
  1050. int64_t av_offset = 0;
  1051. int32_t clock_id = -EINVAL;
  1052. int rc = 0;
  1053. struct snd_pcm_prsnt_position userarg;
  1054. if (!substream || !substream->private_data) {
  1055. pr_err("%s: Invalid %s\n", __func__,
  1056. (!substream) ? "substream" : "private_data");
  1057. return -EINVAL;
  1058. }
  1059. if (!substream->runtime) {
  1060. pr_err("%s substream runtime not found\n", __func__);
  1061. return -EINVAL;
  1062. }
  1063. prtd = substream->runtime->private_data;
  1064. if (!prtd) {
  1065. pr_err("%s prtd is null.\n", __func__);
  1066. return -EINVAL;
  1067. }
  1068. rtd = substream->private_data;
  1069. switch (cmd) {
  1070. case SNDRV_PCM_IOCTL_DSP_POSITION:
  1071. dev_dbg(rtd->dev, "%s: SNDRV_PCM_DSP_POSITION", __func__);
  1072. if (!arg) {
  1073. dev_err(rtd->dev, "%s: Invalid params DSP_POSITION\n",
  1074. __func__);
  1075. rc = -EINVAL;
  1076. goto done;
  1077. }
  1078. memset(&userarg, 0, sizeof(userarg));
  1079. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  1080. dev_err(rtd->dev, "%s: err copyuser DSP_POSITION\n",
  1081. __func__);
  1082. rc = -EFAULT;
  1083. goto done;
  1084. }
  1085. clock_id = userarg.clock_id;
  1086. rc = q6asm_get_session_time_v2(prtd->audio_client, &ses_time,
  1087. &abs_time);
  1088. if (rc) {
  1089. pr_err("%s: q6asm_get_session_time_v2 failed, rc=%d\n",
  1090. __func__, rc);
  1091. goto done;
  1092. }
  1093. userarg.frames = div64_u64((ses_time * prtd->samp_rate),
  1094. 1000000);
  1095. rc = avcs_core_query_timer_offset(&av_offset, clock_id);
  1096. if (rc) {
  1097. pr_err("%s: avcs offset query failed, rc=%d\n",
  1098. __func__, rc);
  1099. goto done;
  1100. }
  1101. userarg.timestamp = abs_time + av_offset;
  1102. if (copy_to_user(arg, &userarg, sizeof(userarg))) {
  1103. dev_err(rtd->dev, "%s: err copy to user DSP_POSITION\n",
  1104. __func__);
  1105. rc = -EFAULT;
  1106. goto done;
  1107. }
  1108. pr_debug("%s, vals f %lld, t %lld, avoff %lld, abst %lld, sess_time %llu sr %d\n",
  1109. __func__, userarg.frames, userarg.timestamp,
  1110. av_offset, abs_time, ses_time, prtd->samp_rate);
  1111. break;
  1112. default:
  1113. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1114. break;
  1115. }
  1116. done:
  1117. return rc;
  1118. }
  1119. #ifdef CONFIG_COMPAT
  1120. static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
  1121. unsigned int cmd, void __user *arg)
  1122. {
  1123. return msm_pcm_ioctl(substream, cmd, arg);
  1124. }
  1125. #else
  1126. #define msm_pcm_compat_ioctl NULL
  1127. #endif
  1128. static const struct snd_pcm_ops msm_pcm_ops = {
  1129. .open = msm_pcm_open,
  1130. .copy_user = msm_pcm_copy,
  1131. .hw_params = msm_pcm_hw_params,
  1132. .close = msm_pcm_close,
  1133. .ioctl = msm_pcm_ioctl,
  1134. .compat_ioctl = msm_pcm_compat_ioctl,
  1135. .prepare = msm_pcm_prepare,
  1136. .trigger = msm_pcm_trigger,
  1137. .pointer = msm_pcm_pointer,
  1138. .mmap = msm_pcm_mmap,
  1139. };
  1140. static int msm_pcm_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  1141. struct snd_ctl_elem_value *ucontrol)
  1142. {
  1143. struct snd_soc_component *component =
  1144. snd_soc_kcontrol_component(kcontrol);
  1145. struct msm_plat_data *pdata = NULL;
  1146. struct snd_pcm_substream *substream;
  1147. struct msm_audio *prtd;
  1148. int ret = 0;
  1149. struct msm_adsp_event_data *event_data = NULL;
  1150. if (!component) {
  1151. pr_err("%s: component is NULL\n", __func__);
  1152. return -EINVAL;
  1153. }
  1154. pdata = dev_get_drvdata(component->dev);
  1155. if (!pdata) {
  1156. pr_err("%s pdata is NULL\n", __func__);
  1157. ret = -ENODEV;
  1158. goto done;
  1159. }
  1160. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1161. if (!substream) {
  1162. pr_err("%s substream not found\n", __func__);
  1163. ret = -EINVAL;
  1164. goto done;
  1165. }
  1166. if (!substream->runtime) {
  1167. pr_err("%s substream runtime not found\n", __func__);
  1168. ret = -EINVAL;
  1169. goto done;
  1170. }
  1171. prtd = substream->runtime->private_data;
  1172. if (prtd == NULL) {
  1173. pr_err("%s prtd is null.\n", __func__);
  1174. ret = -EINVAL;
  1175. goto done;
  1176. }
  1177. if (prtd->audio_client == NULL) {
  1178. pr_err("%s prtd is null.\n", __func__);
  1179. ret = -EINVAL;
  1180. goto done;
  1181. }
  1182. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  1183. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  1184. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  1185. pr_err("%s: invalid event_type=%d",
  1186. __func__, event_data->event_type);
  1187. ret = -EINVAL;
  1188. goto done;
  1189. }
  1190. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  1191. - sizeof(struct msm_adsp_event_data)) {
  1192. pr_err("%s param length=%d exceeds limit",
  1193. __func__, event_data->payload_len);
  1194. ret = -EINVAL;
  1195. goto done;
  1196. }
  1197. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  1198. if (ret < 0)
  1199. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1200. __func__, ret);
  1201. done:
  1202. return ret;
  1203. }
  1204. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1205. struct snd_soc_pcm_runtime *rtd)
  1206. {
  1207. struct snd_soc_component *component = NULL;
  1208. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1209. const char *deviceNo = "NN";
  1210. char *mixer_str = NULL;
  1211. int ctl_len = 0, ret = 0;
  1212. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1213. {
  1214. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1215. .name = "?",
  1216. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1217. .info = msm_adsp_stream_cmd_info,
  1218. .put = msm_pcm_adsp_stream_cmd_put,
  1219. .private_value = 0,
  1220. }
  1221. };
  1222. if (!rtd) {
  1223. pr_err("%s rtd is NULL\n", __func__);
  1224. ret = -EINVAL;
  1225. goto done;
  1226. }
  1227. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1228. if (!component) {
  1229. pr_err("%s: component is NULL\n", __func__);
  1230. return -EINVAL;
  1231. }
  1232. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1233. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1234. if (!mixer_str) {
  1235. ret = -ENOMEM;
  1236. goto done;
  1237. }
  1238. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1239. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1240. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1241. rtd->dai_link->id;
  1242. pr_debug("Registering new mixer ctl %s\n", mixer_str);
  1243. ret = snd_soc_add_component_controls(component,
  1244. fe_audio_adsp_stream_cmd_config_control,
  1245. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1246. if (ret < 0)
  1247. pr_err("%s: failed add ctl %s. err = %d\n",
  1248. __func__, mixer_str, ret);
  1249. kfree(mixer_str);
  1250. done:
  1251. return ret;
  1252. }
  1253. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1254. struct snd_soc_pcm_runtime *rtd)
  1255. {
  1256. struct snd_soc_component *component = NULL;
  1257. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1258. const char *deviceNo = "NN";
  1259. char *mixer_str = NULL;
  1260. int ctl_len = 0, ret = 0;
  1261. struct snd_kcontrol *kctl;
  1262. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1263. {
  1264. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1265. .name = "?",
  1266. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1267. .info = msm_adsp_stream_callback_info,
  1268. .get = msm_adsp_stream_callback_get,
  1269. .private_value = 0,
  1270. }
  1271. };
  1272. if (!rtd) {
  1273. pr_err("%s NULL rtd\n", __func__);
  1274. ret = -EINVAL;
  1275. goto done;
  1276. }
  1277. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1278. if (!component) {
  1279. pr_err("%s: component is NULL\n", __func__);
  1280. return -EINVAL;
  1281. }
  1282. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  1283. __func__, rtd->dai_link->name, rtd->dai_link->id,
  1284. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  1285. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1286. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1287. if (!mixer_str) {
  1288. ret = -ENOMEM;
  1289. goto done;
  1290. }
  1291. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1292. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1293. fe_audio_adsp_callback_config_control[0].private_value =
  1294. rtd->dai_link->id;
  1295. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1296. ret = snd_soc_add_component_controls(component,
  1297. fe_audio_adsp_callback_config_control,
  1298. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1299. if (ret < 0) {
  1300. pr_err("%s: failed to add ctl %s. err = %d\n",
  1301. __func__, mixer_str, ret);
  1302. ret = -EINVAL;
  1303. goto free_mixer_str;
  1304. }
  1305. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1306. if (!kctl) {
  1307. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1308. ret = -EINVAL;
  1309. goto free_mixer_str;
  1310. }
  1311. kctl->private_data = NULL;
  1312. free_mixer_str:
  1313. kfree(mixer_str);
  1314. done:
  1315. return ret;
  1316. }
  1317. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  1318. {
  1319. int rc = 0;
  1320. if (prtd && prtd->audio_client) {
  1321. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  1322. prtd->channel_mode, volume);
  1323. rc = q6asm_set_volume(prtd->audio_client, volume);
  1324. if (rc < 0) {
  1325. pr_err("%s: Send Volume command failed rc=%d\n",
  1326. __func__, rc);
  1327. }
  1328. }
  1329. return rc;
  1330. }
  1331. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  1332. struct snd_ctl_elem_value *ucontrol)
  1333. {
  1334. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1335. struct snd_pcm_substream *substream =
  1336. vol->pcm->streams[vol->stream].substream;
  1337. struct msm_audio *prtd;
  1338. pr_debug("%s\n", __func__);
  1339. if (!substream) {
  1340. pr_err("%s substream not found\n", __func__);
  1341. return -ENODEV;
  1342. }
  1343. if (!substream->runtime) {
  1344. pr_debug("%s substream runtime not found\n", __func__);
  1345. return 0;
  1346. }
  1347. prtd = substream->runtime->private_data;
  1348. if (prtd)
  1349. ucontrol->value.integer.value[0] = prtd->volume;
  1350. return 0;
  1351. }
  1352. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  1353. struct snd_ctl_elem_value *ucontrol)
  1354. {
  1355. int rc = 0;
  1356. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  1357. struct snd_pcm_substream *substream =
  1358. vol->pcm->streams[vol->stream].substream;
  1359. struct msm_audio *prtd;
  1360. int volume = ucontrol->value.integer.value[0];
  1361. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  1362. if (!substream) {
  1363. pr_err("%s substream not found\n", __func__);
  1364. return -ENODEV;
  1365. }
  1366. if (!substream->runtime) {
  1367. pr_err("%s substream runtime not found\n", __func__);
  1368. return 0;
  1369. }
  1370. prtd = substream->runtime->private_data;
  1371. if (prtd) {
  1372. rc = msm_pcm_set_volume(prtd, volume);
  1373. prtd->volume = volume;
  1374. }
  1375. return rc;
  1376. }
  1377. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  1378. int stream)
  1379. {
  1380. int ret = 0;
  1381. struct snd_pcm *pcm = rtd->pcm;
  1382. struct snd_pcm_volume *volume_info;
  1383. struct snd_kcontrol *kctl;
  1384. dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
  1385. ret = snd_pcm_add_volume_ctls(pcm, stream,
  1386. NULL, 1, rtd->dai_link->id,
  1387. &volume_info);
  1388. if (ret < 0) {
  1389. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1390. return ret;
  1391. }
  1392. kctl = volume_info->kctl;
  1393. kctl->put = msm_pcm_volume_ctl_put;
  1394. kctl->get = msm_pcm_volume_ctl_get;
  1395. kctl->tlv.p = msm_pcm_vol_gain;
  1396. return 0;
  1397. }
  1398. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1399. struct snd_ctl_elem_info *uinfo)
  1400. {
  1401. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1402. uinfo->count = 1;
  1403. uinfo->value.integer.min = 0;
  1404. uinfo->value.integer.max = 0x2000;
  1405. return 0;
  1406. }
  1407. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1408. struct snd_ctl_elem_value *ucontrol)
  1409. {
  1410. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1411. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1412. struct snd_pcm_substream *substream;
  1413. struct msm_audio *prtd;
  1414. if (!pdata) {
  1415. pr_err("%s pdata is NULL\n", __func__);
  1416. return -ENODEV;
  1417. }
  1418. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1419. if (!substream) {
  1420. pr_err("%s substream not found\n", __func__);
  1421. return -EINVAL;
  1422. }
  1423. if (!substream->runtime) {
  1424. pr_debug("%s substream runtime not found\n", __func__);
  1425. return 0;
  1426. }
  1427. prtd = substream->runtime->private_data;
  1428. if (prtd)
  1429. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1430. return 0;
  1431. }
  1432. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1433. struct snd_ctl_elem_value *ucontrol)
  1434. {
  1435. int rc = 0;
  1436. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1437. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1438. struct snd_pcm_substream *substream;
  1439. struct msm_audio *prtd;
  1440. int compress = ucontrol->value.integer.value[0];
  1441. if (!pdata) {
  1442. pr_err("%s pdata is NULL\n", __func__);
  1443. return -ENODEV;
  1444. }
  1445. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1446. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1447. if (!substream) {
  1448. pr_err("%s substream not found\n", __func__);
  1449. return -EINVAL;
  1450. }
  1451. if (!substream->runtime) {
  1452. pr_err("%s substream runtime not found\n", __func__);
  1453. return 0;
  1454. }
  1455. prtd = substream->runtime->private_data;
  1456. if (prtd) {
  1457. pr_debug("%s: setting compress flag to 0x%x\n",
  1458. __func__, compress);
  1459. prtd->compress_enable = compress;
  1460. }
  1461. return rc;
  1462. }
  1463. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1464. {
  1465. struct snd_soc_component *component = NULL;
  1466. const char *mixer_ctl_name = "Playback ";
  1467. const char *mixer_ctl_end_name = " Compress";
  1468. const char *deviceNo = "NN";
  1469. char *mixer_str = NULL;
  1470. int ctl_len;
  1471. int ret = 0;
  1472. struct msm_plat_data *pdata;
  1473. struct snd_kcontrol_new pcm_compress_control[1] = {
  1474. {
  1475. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1476. .name = "?",
  1477. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1478. .info = msm_pcm_compress_ctl_info,
  1479. .get = msm_pcm_compress_ctl_get,
  1480. .put = msm_pcm_compress_ctl_put,
  1481. .private_value = 0,
  1482. }
  1483. };
  1484. if (!rtd) {
  1485. pr_err("%s: NULL rtd\n", __func__);
  1486. return -EINVAL;
  1487. }
  1488. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1489. if (!component) {
  1490. pr_err("%s: component is NULL\n", __func__);
  1491. return -EINVAL;
  1492. }
  1493. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1494. strlen(mixer_ctl_end_name) + 1;
  1495. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1496. if (!mixer_str)
  1497. return -ENOMEM;
  1498. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1499. rtd->pcm->device, mixer_ctl_end_name);
  1500. pcm_compress_control[0].name = mixer_str;
  1501. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1502. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1503. pdata = dev_get_drvdata(component->dev);
  1504. if (pdata) {
  1505. if (!pdata->pcm) {
  1506. pdata->pcm = rtd->pcm;
  1507. snd_soc_add_component_controls(component,
  1508. pcm_compress_control,
  1509. ARRAY_SIZE
  1510. (pcm_compress_control));
  1511. pr_debug("%s: add control success plt = %pK\n",
  1512. __func__, component);
  1513. }
  1514. } else {
  1515. pr_err("%s: NULL pdata\n", __func__);
  1516. ret = -EINVAL;
  1517. }
  1518. kfree(mixer_str);
  1519. return ret;
  1520. }
  1521. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1522. struct snd_ctl_elem_value *ucontrol)
  1523. {
  1524. int i;
  1525. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1526. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1527. struct snd_pcm_substream *substream;
  1528. struct msm_audio *prtd;
  1529. struct snd_soc_pcm_runtime *rtd = NULL;
  1530. struct msm_plat_data *pdata = NULL;
  1531. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1532. struct snd_soc_component *component = NULL;
  1533. u64 fe_id = 0;
  1534. pr_debug("%s", __func__);
  1535. substream = snd_pcm_chmap_substream(info, idx);
  1536. if (!substream)
  1537. return -ENODEV;
  1538. if (!substream->runtime)
  1539. return 0;
  1540. prtd = substream->runtime->private_data;
  1541. if (prtd) {
  1542. prtd->set_channel_map = true;
  1543. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1544. prtd->channel_map[i] =
  1545. (char)(ucontrol->value.integer.value[i]);
  1546. /* update chmixer_pspd chmap cached with routing driver as well */
  1547. rtd = substream->private_data;
  1548. if (rtd) {
  1549. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1550. if (component) {
  1551. fe_id = rtd->dai_link->id;
  1552. pdata = (struct msm_plat_data *)
  1553. dev_get_drvdata(component->dev);
  1554. chmixer_pspd = pdata ?
  1555. pdata->chmixer_pspd[fe_id][SESSION_TYPE_RX] : NULL;
  1556. if (chmixer_pspd && chmixer_pspd->enable) {
  1557. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1558. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1559. chmixer_pspd->override_in_ch_map = true;
  1560. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1561. SESSION_TYPE_RX, chmixer_pspd);
  1562. }
  1563. }
  1564. }
  1565. }
  1566. return 0;
  1567. }
  1568. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1569. struct snd_ctl_elem_value *ucontrol)
  1570. {
  1571. int i;
  1572. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1573. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1574. struct snd_pcm_substream *substream;
  1575. struct msm_audio *prtd;
  1576. pr_debug("%s", __func__);
  1577. substream = snd_pcm_chmap_substream(info, idx);
  1578. if (!substream)
  1579. return -ENODEV;
  1580. memset(ucontrol->value.integer.value, 0,
  1581. sizeof(ucontrol->value.integer.value));
  1582. if (!substream->runtime)
  1583. return 0; /* no channels set */
  1584. prtd = substream->runtime->private_data;
  1585. if (prtd && prtd->set_channel_map == true) {
  1586. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1587. ucontrol->value.integer.value[i] =
  1588. (int)prtd->channel_map[i];
  1589. } else {
  1590. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1591. ucontrol->value.integer.value[i] = 0;
  1592. }
  1593. return 0;
  1594. }
  1595. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1596. {
  1597. struct snd_pcm *pcm = rtd->pcm;
  1598. struct snd_pcm_chmap *chmap_info;
  1599. struct snd_kcontrol *kctl;
  1600. char device_num[12];
  1601. int i, ret = 0;
  1602. pr_debug("%s, Channel map cntrl add\n", __func__);
  1603. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1604. snd_pcm_std_chmaps,
  1605. PCM_FORMAT_MAX_NUM_CHANNEL_V8, 0,
  1606. &chmap_info);
  1607. if (ret < 0) {
  1608. pr_err("%s, channel map cntrl add failed\n", __func__);
  1609. return ret;
  1610. }
  1611. kctl = chmap_info->kctl;
  1612. for (i = 0; i < kctl->count; i++)
  1613. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1614. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1615. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1616. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1617. __func__, kctl->id.name);
  1618. kctl->put = msm_pcm_chmap_ctl_put;
  1619. kctl->get = msm_pcm_chmap_ctl_get;
  1620. return 0;
  1621. }
  1622. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1623. struct snd_ctl_elem_value *ucontrol)
  1624. {
  1625. u64 fe_id = kcontrol->private_value;
  1626. int session_type = SESSION_TYPE_RX;
  1627. int be_id = ucontrol->value.integer.value[3];
  1628. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1629. int ret = 0;
  1630. cfg_data.app_type = ucontrol->value.integer.value[0];
  1631. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1632. if (ucontrol->value.integer.value[2] != 0)
  1633. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1634. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1635. __func__, fe_id, session_type, be_id,
  1636. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1637. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1638. be_id, &cfg_data);
  1639. if (ret < 0)
  1640. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1641. __func__, ret);
  1642. return ret;
  1643. }
  1644. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1645. struct snd_ctl_elem_value *ucontrol)
  1646. {
  1647. u64 fe_id = kcontrol->private_value;
  1648. int session_type = SESSION_TYPE_RX;
  1649. int be_id = 0;
  1650. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1651. int ret = 0;
  1652. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1653. &be_id, &cfg_data);
  1654. if (ret < 0) {
  1655. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1656. __func__, ret);
  1657. goto done;
  1658. }
  1659. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1660. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1661. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1662. ucontrol->value.integer.value[3] = be_id;
  1663. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1664. __func__, fe_id, session_type, be_id,
  1665. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1666. done:
  1667. return ret;
  1668. }
  1669. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1670. struct snd_ctl_elem_value *ucontrol)
  1671. {
  1672. u64 fe_id = kcontrol->private_value;
  1673. int session_type = SESSION_TYPE_TX;
  1674. int be_id = ucontrol->value.integer.value[3];
  1675. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1676. int ret = 0;
  1677. cfg_data.app_type = ucontrol->value.integer.value[0];
  1678. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1679. if (ucontrol->value.integer.value[2] != 0)
  1680. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1681. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1682. __func__, fe_id, session_type, be_id,
  1683. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1684. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1685. be_id, &cfg_data);
  1686. if (ret < 0)
  1687. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1688. __func__, ret);
  1689. return ret;
  1690. }
  1691. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1692. struct snd_ctl_elem_value *ucontrol)
  1693. {
  1694. u64 fe_id = kcontrol->private_value;
  1695. int session_type = SESSION_TYPE_TX;
  1696. int be_id = 0;
  1697. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1698. int ret = 0;
  1699. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1700. &be_id, &cfg_data);
  1701. if (ret < 0) {
  1702. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1703. __func__, ret);
  1704. goto done;
  1705. }
  1706. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1707. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1708. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1709. ucontrol->value.integer.value[3] = be_id;
  1710. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1711. __func__, fe_id, session_type, be_id,
  1712. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1713. done:
  1714. return ret;
  1715. }
  1716. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1717. {
  1718. struct snd_pcm *pcm = rtd->pcm;
  1719. struct snd_pcm_usr *app_type_info;
  1720. struct snd_kcontrol *kctl;
  1721. const char *playback_mixer_ctl_name = "Audio Stream";
  1722. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1723. const char *deviceNo = "NN";
  1724. const char *suffix = "App Type Cfg";
  1725. int ctl_len, ret = 0;
  1726. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1727. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1728. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1729. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1730. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1731. NULL, 1, ctl_len, rtd->dai_link->id,
  1732. &app_type_info);
  1733. if (ret < 0) {
  1734. pr_err("%s: playback app type cntrl add failed: %d\n",
  1735. __func__, ret);
  1736. return ret;
  1737. }
  1738. kctl = app_type_info->kctl;
  1739. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1740. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1741. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1742. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1743. }
  1744. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1745. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1746. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1747. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1748. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1749. NULL, 1, ctl_len, rtd->dai_link->id,
  1750. &app_type_info);
  1751. if (ret < 0) {
  1752. pr_err("%s: capture app type cntrl add failed: %d\n",
  1753. __func__, ret);
  1754. return ret;
  1755. }
  1756. kctl = app_type_info->kctl;
  1757. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1758. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1759. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1760. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1761. }
  1762. return 0;
  1763. }
  1764. static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
  1765. struct msm_plat_data *pdata,
  1766. u64 fe_id, int session_type)
  1767. {
  1768. if (!pdata) {
  1769. pr_err("%s: missing pdata\n", __func__);
  1770. return NULL;
  1771. }
  1772. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  1773. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  1774. return NULL;
  1775. }
  1776. if ((session_type != SESSION_TYPE_TX) &&
  1777. (session_type != SESSION_TYPE_RX)) {
  1778. pr_err("%s: invalid session type %d\n", __func__, session_type);
  1779. return NULL;
  1780. }
  1781. return pdata->chmixer_pspd[fe_id][session_type];
  1782. }
  1783. static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1784. struct snd_ctl_elem_value *ucontrol)
  1785. {
  1786. u64 fe_id = kcontrol->private_value & 0xFF;
  1787. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1788. int ret = 0;
  1789. int stream_id = 0;
  1790. int be_id = 0, i = 0;
  1791. struct msm_audio *prtd = NULL;
  1792. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1793. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1794. struct snd_pcm *pcm = NULL;
  1795. struct snd_pcm_substream *substream = NULL;
  1796. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1797. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  1798. bool reset_override_out_ch_map = false;
  1799. bool reset_override_in_ch_map = false;
  1800. pcm = pdata->pcm_device[fe_id];
  1801. if (!pcm) {
  1802. pr_err("%s invalid pcm handle for fe_id %llu\n",
  1803. __func__, fe_id);
  1804. return -EINVAL;
  1805. }
  1806. if (session_type == SESSION_TYPE_RX)
  1807. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1808. else
  1809. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1810. if (!substream) {
  1811. pr_err("%s substream not found\n", __func__);
  1812. return -EINVAL;
  1813. }
  1814. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1815. if (!chmixer_pspd) {
  1816. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1817. return -EINVAL;
  1818. }
  1819. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  1820. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  1821. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  1822. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  1823. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  1824. if (chmixer_pspd->input_channel < 0 ||
  1825. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  1826. chmixer_pspd->output_channel < 0 ||
  1827. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1828. pr_err("%s: Invalid channels, in %d, out %d\n",
  1829. __func__, chmixer_pspd->input_channel,
  1830. chmixer_pspd->output_channel);
  1831. return -EINVAL;
  1832. }
  1833. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1834. if (chmixer_pspd->enable && prtd) {
  1835. if (session_type == SESSION_TYPE_RX &&
  1836. !chmixer_pspd->override_in_ch_map) {
  1837. if (prtd->set_channel_map) {
  1838. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1839. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1840. } else {
  1841. q6asm_map_channels(asm_ch_map,
  1842. chmixer_pspd->input_channel, false);
  1843. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1844. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  1845. }
  1846. chmixer_pspd->override_in_ch_map = true;
  1847. reset_override_in_ch_map = true;
  1848. } else if (session_type == SESSION_TYPE_TX &&
  1849. !chmixer_pspd->override_out_ch_map) {
  1850. /*
  1851. * Channel map set in prtd is for plyback only,
  1852. * hence always use default for capture path.
  1853. */
  1854. q6asm_map_channels(asm_ch_map,
  1855. chmixer_pspd->output_channel, false);
  1856. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1857. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  1858. chmixer_pspd->override_out_ch_map = true;
  1859. reset_override_out_ch_map = true;
  1860. }
  1861. } else {
  1862. chmixer_pspd->override_out_ch_map = false;
  1863. chmixer_pspd->override_in_ch_map = false;
  1864. }
  1865. /* cache value and take effect during adm_open stage */
  1866. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1867. session_type,
  1868. chmixer_pspd);
  1869. if (chmixer_pspd->enable && prtd && prtd->audio_client) {
  1870. stream_id = prtd->audio_client->session;
  1871. be_id = chmixer_pspd->port_idx;
  1872. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  1873. stream_id,
  1874. session_type,
  1875. chmixer_pspd);
  1876. }
  1877. if (reset_override_out_ch_map)
  1878. chmixer_pspd->override_out_ch_map = false;
  1879. if (reset_override_in_ch_map)
  1880. chmixer_pspd->override_in_ch_map = false;
  1881. return ret;
  1882. }
  1883. static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1884. struct snd_ctl_elem_value *ucontrol)
  1885. {
  1886. u64 fe_id = kcontrol->private_value & 0xFF;
  1887. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1888. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1889. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1890. struct msm_pcm_channel_mixer *chmixer_pspd;
  1891. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1892. if (!chmixer_pspd) {
  1893. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1894. return -EINVAL;
  1895. }
  1896. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  1897. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  1898. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  1899. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  1900. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  1901. return 0;
  1902. }
  1903. static int msm_pcm_channel_mixer_output_map_ctl_put(
  1904. struct snd_kcontrol *kcontrol,
  1905. struct snd_ctl_elem_value *ucontrol)
  1906. {
  1907. u64 fe_id = kcontrol->private_value & 0xFF;
  1908. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1909. int i = 0;
  1910. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1911. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1912. struct msm_pcm_channel_mixer *chmixer_pspd;
  1913. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1914. if (!chmixer_pspd) {
  1915. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1916. return -EINVAL;
  1917. }
  1918. chmixer_pspd->override_out_ch_map = true;
  1919. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1920. chmixer_pspd->out_ch_map[i] =
  1921. ucontrol->value.integer.value[i];
  1922. return 0;
  1923. }
  1924. static int msm_pcm_channel_mixer_output_map_ctl_get(
  1925. struct snd_kcontrol *kcontrol,
  1926. struct snd_ctl_elem_value *ucontrol)
  1927. {
  1928. u64 fe_id = kcontrol->private_value & 0xFF;
  1929. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1930. int i = 0;
  1931. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1932. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1933. struct msm_pcm_channel_mixer *chmixer_pspd;
  1934. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1935. if (!chmixer_pspd) {
  1936. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1937. return -EINVAL;
  1938. }
  1939. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1940. ucontrol->value.integer.value[i] =
  1941. chmixer_pspd->out_ch_map[i];
  1942. return 0;
  1943. }
  1944. static int msm_pcm_channel_mixer_input_map_ctl_put(
  1945. struct snd_kcontrol *kcontrol,
  1946. struct snd_ctl_elem_value *ucontrol)
  1947. {
  1948. u64 fe_id = kcontrol->private_value & 0xFF;
  1949. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1950. int i = 0;
  1951. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1952. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1953. struct msm_pcm_channel_mixer *chmixer_pspd;
  1954. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1955. if (!chmixer_pspd) {
  1956. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1957. return -EINVAL;
  1958. }
  1959. chmixer_pspd->override_in_ch_map = true;
  1960. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1961. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  1962. return 0;
  1963. }
  1964. static int msm_pcm_channel_mixer_input_map_ctl_get(
  1965. struct snd_kcontrol *kcontrol,
  1966. struct snd_ctl_elem_value *ucontrol)
  1967. {
  1968. u64 fe_id = kcontrol->private_value & 0xFF;
  1969. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1970. int i = 0;
  1971. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1972. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1973. struct msm_pcm_channel_mixer *chmixer_pspd;
  1974. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1975. if (!chmixer_pspd) {
  1976. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1977. return -EINVAL;
  1978. }
  1979. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1980. ucontrol->value.integer.value[i] =
  1981. chmixer_pspd->in_ch_map[i];
  1982. return 0;
  1983. }
  1984. static int msm_pcm_channel_mixer_weight_ctl_put(
  1985. struct snd_kcontrol *kcontrol,
  1986. struct snd_ctl_elem_value *ucontrol)
  1987. {
  1988. u64 fe_id = kcontrol->private_value & 0xFF;
  1989. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1990. int channel = (kcontrol->private_value >> 16) & 0xFF;
  1991. int i = 0;
  1992. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1993. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1994. struct msm_pcm_channel_mixer *chmixer_pspd;
  1995. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1996. if (!chmixer_pspd) {
  1997. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1998. return -EINVAL;
  1999. }
  2000. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2001. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2002. return -EINVAL;
  2003. }
  2004. channel--;
  2005. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2006. chmixer_pspd->channel_weight[channel][i] =
  2007. ucontrol->value.integer.value[i];
  2008. return 0;
  2009. }
  2010. static int msm_pcm_channel_mixer_weight_ctl_get(
  2011. struct snd_kcontrol *kcontrol,
  2012. struct snd_ctl_elem_value *ucontrol)
  2013. {
  2014. u64 fe_id = kcontrol->private_value & 0xFF;
  2015. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  2016. int channel = (kcontrol->private_value >> 16) & 0xFF;
  2017. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  2018. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  2019. int i = 0;
  2020. struct msm_pcm_channel_mixer *chmixer_pspd;
  2021. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  2022. pr_err("%s: invalid channel number %d\n", __func__, channel);
  2023. return -EINVAL;
  2024. }
  2025. channel--;
  2026. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  2027. if (!chmixer_pspd) {
  2028. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  2029. return -EINVAL;
  2030. }
  2031. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  2032. ucontrol->value.integer.value[i] =
  2033. chmixer_pspd->channel_weight[channel][i];
  2034. return 0;
  2035. }
  2036. static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
  2037. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  2038. const char *name_suffix, int session_type, int channels)
  2039. {
  2040. int ret = -EINVAL;
  2041. char *mixer_name = NULL;
  2042. struct snd_pcm *pcm = rtd->pcm;
  2043. const char *deviceNo = "NN";
  2044. const char *channelNo = "NN";
  2045. int ctl_len = 0;
  2046. struct snd_soc_component *component = NULL;
  2047. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2048. if (!component) {
  2049. pr_err("%s: component is NULL\n", __func__);
  2050. return -EINVAL;
  2051. }
  2052. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  2053. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  2054. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  2055. if (mixer_name == NULL)
  2056. return -ENOMEM;
  2057. if (channels >= 0) {
  2058. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  2059. name_prefix, pcm->device, name_suffix, channels);
  2060. kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
  2061. (channels << 16);
  2062. } else {
  2063. snprintf(mixer_name, ctl_len, "%s %d %s",
  2064. name_prefix, pcm->device, name_suffix);
  2065. kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
  2066. }
  2067. kctl->name = mixer_name;
  2068. ret = snd_soc_add_component_controls(component, kctl, 1);
  2069. kfree(mixer_name);
  2070. return ret;
  2071. }
  2072. static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  2073. struct snd_ctl_elem_info *uinfo)
  2074. {
  2075. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2076. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2077. /* Valid channel map value ranges from 1 to 64 */
  2078. uinfo->value.integer.min = 1;
  2079. uinfo->value.integer.max = 64;
  2080. return 0;
  2081. }
  2082. static int msm_pcm_add_channel_mixer_output_map_controls(
  2083. struct snd_soc_pcm_runtime *rtd)
  2084. {
  2085. struct snd_pcm *pcm = rtd->pcm;
  2086. const char *playback_mixer_ctl_name = "AudStr";
  2087. const char *capture_mixer_ctl_name = "AudStr Capture";
  2088. const char *suffix = "ChMixer Output Map";
  2089. int session_type = 0, ret = 0, channel = -1;
  2090. struct snd_kcontrol_new channel_mixer_output_map_control = {
  2091. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2092. .name = "?",
  2093. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2094. .info = msm_pcm_channel_mixer_output_map_info,
  2095. .put = msm_pcm_channel_mixer_output_map_ctl_put,
  2096. .get = msm_pcm_channel_mixer_output_map_ctl_get,
  2097. .private_value = 0,
  2098. };
  2099. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2100. session_type = SESSION_TYPE_RX;
  2101. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2102. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2103. if (ret < 0)
  2104. goto fail;
  2105. }
  2106. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2107. session_type = SESSION_TYPE_TX;
  2108. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2109. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2110. if (ret < 0)
  2111. goto fail;
  2112. }
  2113. return 0;
  2114. fail:
  2115. pr_err("%s: failed add platform ctl, err = %d\n",
  2116. __func__, ret);
  2117. return ret;
  2118. }
  2119. static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  2120. struct snd_ctl_elem_info *uinfo)
  2121. {
  2122. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2123. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2124. /* Valid channel map value ranges from 1 to 64 */
  2125. uinfo->value.integer.min = 1;
  2126. uinfo->value.integer.max = 64;
  2127. return 0;
  2128. }
  2129. static int msm_pcm_add_channel_mixer_input_map_controls(
  2130. struct snd_soc_pcm_runtime *rtd)
  2131. {
  2132. struct snd_pcm *pcm = rtd->pcm;
  2133. const char *playback_mixer_ctl_name = "AudStr";
  2134. const char *capture_mixer_ctl_name = "AudStr Capture";
  2135. const char *suffix = "ChMixer Input Map";
  2136. int session_type = 0, ret = 0, channel = -1;
  2137. struct snd_kcontrol_new channel_mixer_input_map_control = {
  2138. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2139. .name = "?",
  2140. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2141. .info = msm_pcm_channel_mixer_input_map_info,
  2142. .put = msm_pcm_channel_mixer_input_map_ctl_put,
  2143. .get = msm_pcm_channel_mixer_input_map_ctl_get,
  2144. .private_value = 0,
  2145. };
  2146. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2147. session_type = SESSION_TYPE_RX;
  2148. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2149. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2150. if (ret < 0)
  2151. goto fail;
  2152. }
  2153. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2154. session_type = SESSION_TYPE_TX;
  2155. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2156. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2157. if (ret < 0)
  2158. goto fail;
  2159. }
  2160. return 0;
  2161. fail:
  2162. pr_err("%s: failed add platform ctl, err = %d\n",
  2163. __func__, ret);
  2164. return ret;
  2165. }
  2166. static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  2167. struct snd_ctl_elem_info *uinfo)
  2168. {
  2169. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2170. /* five int values: enable, rule, in_channels, out_channels and port_id */
  2171. uinfo->count = 5;
  2172. /* Valid range is all positive values to support above controls */
  2173. uinfo->value.integer.min = 0;
  2174. uinfo->value.integer.max = INT_MAX;
  2175. return 0;
  2176. }
  2177. static int msm_pcm_add_channel_mixer_cfg_controls(
  2178. struct snd_soc_pcm_runtime *rtd)
  2179. {
  2180. struct snd_pcm *pcm = rtd->pcm;
  2181. const char *playback_mixer_ctl_name = "AudStr";
  2182. const char *capture_mixer_ctl_name = "AudStr Capture";
  2183. const char *suffix = "ChMixer Cfg";
  2184. int session_type = 0, ret = 0, channel = -1;
  2185. struct msm_plat_data *pdata = NULL;
  2186. struct snd_soc_component *component = NULL;
  2187. struct snd_kcontrol_new channel_mixer_cfg_control = {
  2188. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2189. .name = "?",
  2190. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2191. .info = msm_pcm_channel_mixer_cfg_info,
  2192. .put = msm_pcm_channel_mixer_cfg_ctl_put,
  2193. .get = msm_pcm_channel_mixer_cfg_ctl_get,
  2194. .private_value = 0,
  2195. };
  2196. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2197. if (!component) {
  2198. pr_err("%s: component is NULL\n", __func__);
  2199. return -EINVAL;
  2200. }
  2201. pdata = (struct msm_plat_data *)
  2202. dev_get_drvdata(component->dev);
  2203. pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
  2204. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2205. session_type = SESSION_TYPE_RX;
  2206. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2207. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2208. if (ret < 0)
  2209. goto fail;
  2210. }
  2211. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2212. session_type = SESSION_TYPE_TX;
  2213. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2214. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2215. if (ret < 0)
  2216. goto fail;
  2217. }
  2218. return 0;
  2219. fail:
  2220. pr_err("%s: failed add platform ctl, err = %d\n",
  2221. __func__, ret);
  2222. return ret;
  2223. }
  2224. static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  2225. struct snd_ctl_elem_info *uinfo)
  2226. {
  2227. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2228. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2229. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  2230. uinfo->value.integer.min = 0;
  2231. uinfo->value.integer.max = 0x4000;
  2232. return 0;
  2233. }
  2234. static int msm_pcm_add_channel_mixer_weight_controls(
  2235. struct snd_soc_pcm_runtime *rtd,
  2236. int channel)
  2237. {
  2238. struct snd_pcm *pcm = rtd->pcm;
  2239. const char *playback_mixer_ctl_name = "AudStr";
  2240. const char *capture_mixer_ctl_name = "AudStr Capture";
  2241. const char *suffix = "ChMixer Weight Ch";
  2242. int session_type = 0, ret = 0;
  2243. struct snd_kcontrol_new channel_mixer_weight_control = {
  2244. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2245. .name = "?",
  2246. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2247. .info = msm_pcm_channel_mixer_weight_info,
  2248. .put = msm_pcm_channel_mixer_weight_ctl_put,
  2249. .get = msm_pcm_channel_mixer_weight_ctl_get,
  2250. .private_value = 0,
  2251. };
  2252. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2253. session_type = SESSION_TYPE_RX;
  2254. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2255. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2256. if (ret < 0)
  2257. goto fail;
  2258. }
  2259. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2260. session_type = SESSION_TYPE_TX;
  2261. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2262. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2263. if (ret < 0)
  2264. goto fail;
  2265. }
  2266. return 0;
  2267. fail:
  2268. pr_err("%s: failed add platform ctl, err = %d\n",
  2269. __func__, ret);
  2270. return ret;
  2271. }
  2272. static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  2273. {
  2274. int i, ret = 0;
  2275. struct snd_pcm *pcm = NULL;
  2276. struct msm_plat_data *pdata = NULL;
  2277. struct snd_soc_component *component = NULL;
  2278. if (!rtd || !rtd->pcm) {
  2279. pr_err("%s invalid rtd or pcm\n", __func__);
  2280. return -EINVAL;
  2281. }
  2282. pcm = rtd->pcm;
  2283. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2284. if (!component) {
  2285. pr_err("%s: component is NULL\n", __func__);
  2286. return -EINVAL;
  2287. }
  2288. pdata = (struct msm_plat_data *)
  2289. dev_get_drvdata(component->dev);
  2290. if (!pdata) {
  2291. pr_err("%s: platform data not populated\n", __func__);
  2292. return -EINVAL;
  2293. }
  2294. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
  2295. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2296. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
  2297. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2298. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2299. ret = -ENOMEM;
  2300. goto fail;
  2301. }
  2302. }
  2303. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
  2304. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2305. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
  2306. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2307. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2308. ret = -ENOMEM;
  2309. goto fail;
  2310. }
  2311. }
  2312. ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
  2313. if (ret) {
  2314. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  2315. __func__, ret);
  2316. goto fail;
  2317. }
  2318. ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
  2319. if (ret) {
  2320. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  2321. __func__, ret);
  2322. goto fail;
  2323. }
  2324. ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
  2325. if (ret) {
  2326. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  2327. __func__, ret);
  2328. goto fail;
  2329. }
  2330. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  2331. ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
  2332. if (ret) {
  2333. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  2334. __func__, ret);
  2335. goto fail;
  2336. }
  2337. }
  2338. return 0;
  2339. fail:
  2340. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
  2341. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
  2342. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
  2343. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
  2344. return ret;
  2345. }
  2346. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  2347. {
  2348. int ret = 0;
  2349. pr_debug("%s\n", __func__);
  2350. ret = msm_pcm_add_chmap_controls(rtd);
  2351. if (ret)
  2352. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  2353. ret = msm_pcm_add_app_type_controls(rtd);
  2354. if (ret)
  2355. pr_err("%s: pcm add app type controls failed:%d\n",
  2356. __func__, ret);
  2357. ret = msm_pcm_add_channel_mixer_controls(rtd);
  2358. if (ret)
  2359. pr_err("%s: pcm add channel mixer controls failed:%d\n",
  2360. __func__, ret);
  2361. return ret;
  2362. }
  2363. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  2364. {
  2365. struct snd_card *card = rtd->card->snd_card;
  2366. int ret = 0;
  2367. if (!card->dev->coherent_dma_mask)
  2368. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  2369. ret = msm_pcm_add_controls(rtd);
  2370. if (ret) {
  2371. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  2372. return ret;
  2373. }
  2374. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
  2375. if (ret)
  2376. pr_err("%s: Could not add pcm Volume Control %d\n",
  2377. __func__, ret);
  2378. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
  2379. if (ret)
  2380. pr_err("%s: Could not add pcm Volume Control %d\n",
  2381. __func__, ret);
  2382. ret = msm_pcm_add_compress_control(rtd);
  2383. if (ret)
  2384. pr_err("%s: Could not add pcm Compress Control %d\n",
  2385. __func__, ret);
  2386. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  2387. if (ret)
  2388. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  2389. __func__);
  2390. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  2391. if (ret)
  2392. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  2393. __func__);
  2394. return ret;
  2395. }
  2396. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  2397. struct snd_soc_dai *dai)
  2398. {
  2399. struct snd_pcm_runtime *runtime = substream->runtime;
  2400. struct msm_audio *prtd = runtime->private_data;
  2401. struct audio_client *ac = prtd->audio_client;
  2402. snd_pcm_sframes_t frames;
  2403. int ret;
  2404. ret = q6asm_get_path_delay(prtd->audio_client);
  2405. if (ret) {
  2406. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  2407. return 0;
  2408. }
  2409. /* convert microseconds to frames */
  2410. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  2411. /* also convert the remainder from the initial division */
  2412. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  2413. /* overcompensate for the loss of precision (empirical) */
  2414. frames += 2;
  2415. return frames;
  2416. }
  2417. static struct snd_soc_component_driver msm_soc_component = {
  2418. .name = DRV_NAME,
  2419. .ops = &msm_pcm_ops,
  2420. .pcm_new = msm_asoc_pcm_new,
  2421. .delay_blk = msm_pcm_delay_blk,
  2422. };
  2423. static int msm_pcm_probe(struct platform_device *pdev)
  2424. {
  2425. int rc;
  2426. int id;
  2427. struct msm_plat_data *pdata;
  2428. const char *latency_level;
  2429. rc = of_property_read_u32(pdev->dev.of_node,
  2430. "qcom,msm-pcm-dsp-id", &id);
  2431. if (rc) {
  2432. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  2433. __func__);
  2434. return rc;
  2435. }
  2436. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  2437. if (!pdata)
  2438. return -ENOMEM;
  2439. if (of_property_read_bool(pdev->dev.of_node,
  2440. "qcom,msm-pcm-low-latency")) {
  2441. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  2442. rc = of_property_read_string(pdev->dev.of_node,
  2443. "qcom,latency-level", &latency_level);
  2444. if (!rc) {
  2445. if (!strcmp(latency_level, "ultra"))
  2446. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  2447. else if (!strcmp(latency_level, "ull-pp"))
  2448. pdata->perf_mode =
  2449. ULL_POST_PROCESSING_PCM_MODE;
  2450. }
  2451. } else {
  2452. pdata->perf_mode = LEGACY_PCM_MODE;
  2453. }
  2454. dev_set_drvdata(&pdev->dev, pdata);
  2455. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  2456. __func__, dev_name(&pdev->dev));
  2457. return snd_soc_register_component(&pdev->dev,
  2458. &msm_soc_component,
  2459. NULL, 0);
  2460. }
  2461. static int msm_pcm_remove(struct platform_device *pdev)
  2462. {
  2463. struct msm_plat_data *pdata;
  2464. int i = 0;
  2465. pdata = dev_get_drvdata(&pdev->dev);
  2466. if (pdata) {
  2467. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
  2468. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
  2469. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
  2470. }
  2471. }
  2472. kfree(pdata);
  2473. snd_soc_unregister_component(&pdev->dev);
  2474. return 0;
  2475. }
  2476. static const struct of_device_id msm_pcm_dt_match[] = {
  2477. {.compatible = "qcom,msm-pcm-dsp"},
  2478. {}
  2479. };
  2480. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  2481. static struct platform_driver msm_pcm_driver = {
  2482. .driver = {
  2483. .name = "msm-pcm-dsp",
  2484. .owner = THIS_MODULE,
  2485. .of_match_table = msm_pcm_dt_match,
  2486. .suppress_bind_attrs = true,
  2487. },
  2488. .probe = msm_pcm_probe,
  2489. .remove = msm_pcm_remove,
  2490. };
  2491. int __init msm_pcm_dsp_init(void)
  2492. {
  2493. init_waitqueue_head(&the_locks.enable_wait);
  2494. init_waitqueue_head(&the_locks.eos_wait);
  2495. init_waitqueue_head(&the_locks.write_wait);
  2496. init_waitqueue_head(&the_locks.read_wait);
  2497. return platform_driver_register(&msm_pcm_driver);
  2498. }
  2499. void msm_pcm_dsp_exit(void)
  2500. {
  2501. platform_driver_unregister(&msm_pcm_driver);
  2502. }
  2503. MODULE_DESCRIPTION("PCM module platform driver");
  2504. MODULE_LICENSE("GPL v2");