msm-pcm-q6-v2.c 77 KB

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