msm-pcm-q6-v2.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  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[vol->stream].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[vol->stream].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. int stream)
  1291. {
  1292. int ret = 0;
  1293. struct snd_pcm *pcm = rtd->pcm;
  1294. struct snd_pcm_volume *volume_info;
  1295. struct snd_kcontrol *kctl;
  1296. dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
  1297. ret = snd_pcm_add_volume_ctls(pcm, stream,
  1298. NULL, 1, rtd->dai_link->id,
  1299. &volume_info);
  1300. if (ret < 0) {
  1301. pr_err("%s volume control failed ret %d\n", __func__, ret);
  1302. return ret;
  1303. }
  1304. kctl = volume_info->kctl;
  1305. kctl->put = msm_pcm_volume_ctl_put;
  1306. kctl->get = msm_pcm_volume_ctl_get;
  1307. kctl->tlv.p = msm_pcm_vol_gain;
  1308. return 0;
  1309. }
  1310. static int msm_pcm_compress_ctl_info(struct snd_kcontrol *kcontrol,
  1311. struct snd_ctl_elem_info *uinfo)
  1312. {
  1313. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1314. uinfo->count = 1;
  1315. uinfo->value.integer.min = 0;
  1316. uinfo->value.integer.max = 0x2000;
  1317. return 0;
  1318. }
  1319. static int msm_pcm_compress_ctl_get(struct snd_kcontrol *kcontrol,
  1320. struct snd_ctl_elem_value *ucontrol)
  1321. {
  1322. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1323. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1324. struct snd_pcm_substream *substream;
  1325. struct msm_audio *prtd;
  1326. if (!pdata) {
  1327. pr_err("%s pdata is NULL\n", __func__);
  1328. return -ENODEV;
  1329. }
  1330. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1331. if (!substream) {
  1332. pr_err("%s substream not found\n", __func__);
  1333. return -EINVAL;
  1334. }
  1335. if (!substream->runtime) {
  1336. pr_debug("%s substream runtime not found\n", __func__);
  1337. return 0;
  1338. }
  1339. prtd = substream->runtime->private_data;
  1340. if (prtd)
  1341. ucontrol->value.integer.value[0] = prtd->compress_enable;
  1342. return 0;
  1343. }
  1344. static int msm_pcm_compress_ctl_put(struct snd_kcontrol *kcontrol,
  1345. struct snd_ctl_elem_value *ucontrol)
  1346. {
  1347. int rc = 0;
  1348. struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
  1349. struct msm_plat_data *pdata = dev_get_drvdata(comp->dev);
  1350. struct snd_pcm_substream *substream;
  1351. struct msm_audio *prtd;
  1352. int compress = ucontrol->value.integer.value[0];
  1353. if (!pdata) {
  1354. pr_err("%s pdata is NULL\n", __func__);
  1355. return -ENODEV;
  1356. }
  1357. substream = pdata->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1358. pr_debug("%s: compress : 0x%x\n", __func__, compress);
  1359. if (!substream) {
  1360. pr_err("%s substream not found\n", __func__);
  1361. return -EINVAL;
  1362. }
  1363. if (!substream->runtime) {
  1364. pr_err("%s substream runtime not found\n", __func__);
  1365. return 0;
  1366. }
  1367. prtd = substream->runtime->private_data;
  1368. if (prtd) {
  1369. pr_debug("%s: setting compress flag to 0x%x\n",
  1370. __func__, compress);
  1371. prtd->compress_enable = compress;
  1372. }
  1373. return rc;
  1374. }
  1375. static int msm_pcm_add_compress_control(struct snd_soc_pcm_runtime *rtd)
  1376. {
  1377. struct snd_soc_component *component = NULL;
  1378. const char *mixer_ctl_name = "Playback ";
  1379. const char *mixer_ctl_end_name = " Compress";
  1380. const char *deviceNo = "NN";
  1381. char *mixer_str = NULL;
  1382. int ctl_len;
  1383. int ret = 0;
  1384. struct msm_plat_data *pdata;
  1385. struct snd_kcontrol_new pcm_compress_control[1] = {
  1386. {
  1387. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1388. .name = "?",
  1389. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1390. .info = msm_pcm_compress_ctl_info,
  1391. .get = msm_pcm_compress_ctl_get,
  1392. .put = msm_pcm_compress_ctl_put,
  1393. .private_value = 0,
  1394. }
  1395. };
  1396. if (!rtd) {
  1397. pr_err("%s: NULL rtd\n", __func__);
  1398. return -EINVAL;
  1399. }
  1400. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1401. if (!component) {
  1402. pr_err("%s: component is NULL\n", __func__);
  1403. return -EINVAL;
  1404. }
  1405. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) +
  1406. strlen(mixer_ctl_end_name) + 1;
  1407. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1408. if (!mixer_str)
  1409. return -ENOMEM;
  1410. snprintf(mixer_str, ctl_len, "%s%d%s", mixer_ctl_name,
  1411. rtd->pcm->device, mixer_ctl_end_name);
  1412. pcm_compress_control[0].name = mixer_str;
  1413. pcm_compress_control[0].private_value = rtd->dai_link->id;
  1414. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1415. pdata = dev_get_drvdata(component->dev);
  1416. if (pdata) {
  1417. if (!pdata->pcm) {
  1418. pdata->pcm = rtd->pcm;
  1419. snd_soc_add_component_controls(component,
  1420. pcm_compress_control,
  1421. ARRAY_SIZE
  1422. (pcm_compress_control));
  1423. pr_debug("%s: add control success plt = %pK\n",
  1424. __func__, component);
  1425. }
  1426. } else {
  1427. pr_err("%s: NULL pdata\n", __func__);
  1428. ret = -EINVAL;
  1429. }
  1430. kfree(mixer_str);
  1431. return ret;
  1432. }
  1433. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1434. struct snd_ctl_elem_value *ucontrol)
  1435. {
  1436. int i;
  1437. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1438. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1439. struct snd_pcm_substream *substream;
  1440. struct msm_audio *prtd;
  1441. struct snd_soc_pcm_runtime *rtd = NULL;
  1442. struct msm_plat_data *pdata = NULL;
  1443. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1444. struct snd_soc_component *component = NULL;
  1445. u64 fe_id = 0;
  1446. pr_debug("%s", __func__);
  1447. substream = snd_pcm_chmap_substream(info, idx);
  1448. if (!substream)
  1449. return -ENODEV;
  1450. if (!substream->runtime)
  1451. return 0;
  1452. prtd = substream->runtime->private_data;
  1453. if (prtd) {
  1454. prtd->set_channel_map = true;
  1455. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1456. prtd->channel_map[i] =
  1457. (char)(ucontrol->value.integer.value[i]);
  1458. /* update chmixer_pspd chmap cached with routing driver as well */
  1459. rtd = substream->private_data;
  1460. if (rtd) {
  1461. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1462. if (component) {
  1463. fe_id = rtd->dai_link->id;
  1464. pdata = (struct msm_plat_data *)
  1465. dev_get_drvdata(component->dev);
  1466. chmixer_pspd = pdata ?
  1467. pdata->chmixer_pspd[fe_id][SESSION_TYPE_RX] : NULL;
  1468. if (chmixer_pspd && chmixer_pspd->enable) {
  1469. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1470. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1471. chmixer_pspd->override_in_ch_map = true;
  1472. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1473. SESSION_TYPE_RX, chmixer_pspd);
  1474. }
  1475. }
  1476. }
  1477. }
  1478. return 0;
  1479. }
  1480. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1481. struct snd_ctl_elem_value *ucontrol)
  1482. {
  1483. int i;
  1484. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1485. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1486. struct snd_pcm_substream *substream;
  1487. struct msm_audio *prtd;
  1488. pr_debug("%s", __func__);
  1489. substream = snd_pcm_chmap_substream(info, idx);
  1490. if (!substream)
  1491. return -ENODEV;
  1492. memset(ucontrol->value.integer.value, 0,
  1493. sizeof(ucontrol->value.integer.value));
  1494. if (!substream->runtime)
  1495. return 0; /* no channels set */
  1496. prtd = substream->runtime->private_data;
  1497. if (prtd && prtd->set_channel_map == true) {
  1498. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1499. ucontrol->value.integer.value[i] =
  1500. (int)prtd->channel_map[i];
  1501. } else {
  1502. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1503. ucontrol->value.integer.value[i] = 0;
  1504. }
  1505. return 0;
  1506. }
  1507. static int msm_pcm_add_chmap_controls(struct snd_soc_pcm_runtime *rtd)
  1508. {
  1509. struct snd_pcm *pcm = rtd->pcm;
  1510. struct snd_pcm_chmap *chmap_info;
  1511. struct snd_kcontrol *kctl;
  1512. char device_num[12];
  1513. int i, ret = 0;
  1514. pr_debug("%s, Channel map cntrl add\n", __func__);
  1515. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1516. snd_pcm_std_chmaps,
  1517. PCM_FORMAT_MAX_NUM_CHANNEL_V8, 0,
  1518. &chmap_info);
  1519. if (ret < 0) {
  1520. pr_err("%s, channel map cntrl add failed\n", __func__);
  1521. return ret;
  1522. }
  1523. kctl = chmap_info->kctl;
  1524. for (i = 0; i < kctl->count; i++)
  1525. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1526. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1527. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1528. pr_debug("%s, Overwriting channel map control name to: %s\n",
  1529. __func__, kctl->id.name);
  1530. kctl->put = msm_pcm_chmap_ctl_put;
  1531. kctl->get = msm_pcm_chmap_ctl_get;
  1532. return 0;
  1533. }
  1534. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1535. struct snd_ctl_elem_value *ucontrol)
  1536. {
  1537. u64 fe_id = kcontrol->private_value;
  1538. int session_type = SESSION_TYPE_RX;
  1539. int be_id = ucontrol->value.integer.value[3];
  1540. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1541. int ret = 0;
  1542. cfg_data.app_type = ucontrol->value.integer.value[0];
  1543. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1544. if (ucontrol->value.integer.value[2] != 0)
  1545. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1546. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1547. __func__, fe_id, session_type, be_id,
  1548. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1549. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1550. be_id, &cfg_data);
  1551. if (ret < 0)
  1552. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1553. __func__, ret);
  1554. return ret;
  1555. }
  1556. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1557. struct snd_ctl_elem_value *ucontrol)
  1558. {
  1559. u64 fe_id = kcontrol->private_value;
  1560. int session_type = SESSION_TYPE_RX;
  1561. int be_id = 0;
  1562. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1563. int ret = 0;
  1564. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1565. &be_id, &cfg_data);
  1566. if (ret < 0) {
  1567. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1568. __func__, ret);
  1569. goto done;
  1570. }
  1571. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1572. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1573. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1574. ucontrol->value.integer.value[3] = be_id;
  1575. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1576. __func__, fe_id, session_type, be_id,
  1577. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1578. done:
  1579. return ret;
  1580. }
  1581. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1582. struct snd_ctl_elem_value *ucontrol)
  1583. {
  1584. u64 fe_id = kcontrol->private_value;
  1585. int session_type = SESSION_TYPE_TX;
  1586. int be_id = ucontrol->value.integer.value[3];
  1587. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  1588. int ret = 0;
  1589. cfg_data.app_type = ucontrol->value.integer.value[0];
  1590. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  1591. if (ucontrol->value.integer.value[2] != 0)
  1592. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  1593. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  1594. __func__, fe_id, session_type, be_id,
  1595. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1596. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  1597. be_id, &cfg_data);
  1598. if (ret < 0)
  1599. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  1600. __func__, ret);
  1601. return ret;
  1602. }
  1603. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1604. struct snd_ctl_elem_value *ucontrol)
  1605. {
  1606. u64 fe_id = kcontrol->private_value;
  1607. int session_type = SESSION_TYPE_TX;
  1608. int be_id = 0;
  1609. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  1610. int ret = 0;
  1611. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  1612. &be_id, &cfg_data);
  1613. if (ret < 0) {
  1614. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  1615. __func__, ret);
  1616. goto done;
  1617. }
  1618. ucontrol->value.integer.value[0] = cfg_data.app_type;
  1619. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  1620. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  1621. ucontrol->value.integer.value[3] = be_id;
  1622. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  1623. __func__, fe_id, session_type, be_id,
  1624. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  1625. done:
  1626. return ret;
  1627. }
  1628. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  1629. {
  1630. struct snd_pcm *pcm = rtd->pcm;
  1631. struct snd_pcm_usr *app_type_info;
  1632. struct snd_kcontrol *kctl;
  1633. const char *playback_mixer_ctl_name = "Audio Stream";
  1634. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  1635. const char *deviceNo = "NN";
  1636. const char *suffix = "App Type Cfg";
  1637. int ctl_len, ret = 0;
  1638. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  1639. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  1640. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1641. pr_debug("%s: Playback app type cntrl add\n", __func__);
  1642. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1643. NULL, 1, ctl_len, rtd->dai_link->id,
  1644. &app_type_info);
  1645. if (ret < 0) {
  1646. pr_err("%s: playback app type cntrl add failed: %d\n",
  1647. __func__, ret);
  1648. return ret;
  1649. }
  1650. kctl = app_type_info->kctl;
  1651. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1652. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  1653. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  1654. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  1655. }
  1656. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  1657. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  1658. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  1659. pr_debug("%s: Capture app type cntrl add\n", __func__);
  1660. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1661. NULL, 1, ctl_len, rtd->dai_link->id,
  1662. &app_type_info);
  1663. if (ret < 0) {
  1664. pr_err("%s: capture app type cntrl add failed: %d\n",
  1665. __func__, ret);
  1666. return ret;
  1667. }
  1668. kctl = app_type_info->kctl;
  1669. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  1670. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  1671. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  1672. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  1673. }
  1674. return 0;
  1675. }
  1676. static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
  1677. struct msm_plat_data *pdata,
  1678. u64 fe_id, int session_type)
  1679. {
  1680. if (!pdata) {
  1681. pr_err("%s: missing pdata\n", __func__);
  1682. return NULL;
  1683. }
  1684. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  1685. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  1686. return NULL;
  1687. }
  1688. if ((session_type != SESSION_TYPE_TX) &&
  1689. (session_type != SESSION_TYPE_RX)) {
  1690. pr_err("%s: invalid session type %d\n", __func__, session_type);
  1691. return NULL;
  1692. }
  1693. return pdata->chmixer_pspd[fe_id][session_type];
  1694. }
  1695. static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  1696. struct snd_ctl_elem_value *ucontrol)
  1697. {
  1698. u64 fe_id = kcontrol->private_value & 0xFF;
  1699. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1700. int ret = 0;
  1701. int stream_id = 0;
  1702. int be_id = 0, i = 0;
  1703. struct msm_audio *prtd = NULL;
  1704. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1705. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1706. struct snd_pcm *pcm = NULL;
  1707. struct snd_pcm_substream *substream = NULL;
  1708. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  1709. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  1710. bool reset_override_out_ch_map = false;
  1711. bool reset_override_in_ch_map = false;
  1712. pcm = pdata->pcm_device[fe_id];
  1713. if (!pcm) {
  1714. pr_err("%s invalid pcm handle for fe_id %llu\n",
  1715. __func__, fe_id);
  1716. return -EINVAL;
  1717. }
  1718. if (session_type == SESSION_TYPE_RX)
  1719. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1720. else
  1721. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1722. if (!substream) {
  1723. pr_err("%s substream not found\n", __func__);
  1724. return -EINVAL;
  1725. }
  1726. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1727. if (!chmixer_pspd) {
  1728. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1729. return -EINVAL;
  1730. }
  1731. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  1732. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  1733. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  1734. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  1735. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  1736. if (chmixer_pspd->input_channel <= 0 ||
  1737. chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8 ||
  1738. chmixer_pspd->output_channel <= 0 ||
  1739. chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1740. pr_err("%s: Invalid channels, in %d, out %d\n",
  1741. __func__, chmixer_pspd->input_channel,
  1742. chmixer_pspd->output_channel);
  1743. return -EINVAL;
  1744. }
  1745. prtd = substream->runtime ? substream->runtime->private_data : NULL;
  1746. if (chmixer_pspd->enable && prtd) {
  1747. if (session_type == SESSION_TYPE_RX &&
  1748. !chmixer_pspd->override_in_ch_map) {
  1749. if (prtd->set_channel_map) {
  1750. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1751. chmixer_pspd->in_ch_map[i] = prtd->channel_map[i];
  1752. } else {
  1753. q6asm_map_channels(asm_ch_map,
  1754. chmixer_pspd->input_channel, false);
  1755. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1756. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  1757. }
  1758. chmixer_pspd->override_in_ch_map = true;
  1759. reset_override_in_ch_map = true;
  1760. } else if (session_type == SESSION_TYPE_TX &&
  1761. !chmixer_pspd->override_out_ch_map) {
  1762. /*
  1763. * Channel map set in prtd is for plyback only,
  1764. * hence always use default for capture path.
  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. chmixer_pspd->override_out_ch_map = true;
  1771. reset_override_out_ch_map = true;
  1772. }
  1773. } else {
  1774. chmixer_pspd->override_out_ch_map = false;
  1775. chmixer_pspd->override_in_ch_map = false;
  1776. }
  1777. /* cache value and take effect during adm_open stage */
  1778. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  1779. session_type,
  1780. chmixer_pspd);
  1781. if (chmixer_pspd->enable && prtd && prtd->audio_client) {
  1782. stream_id = prtd->audio_client->session;
  1783. be_id = chmixer_pspd->port_idx;
  1784. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  1785. stream_id,
  1786. session_type,
  1787. chmixer_pspd);
  1788. }
  1789. if (reset_override_out_ch_map)
  1790. chmixer_pspd->override_out_ch_map = false;
  1791. if (reset_override_in_ch_map)
  1792. chmixer_pspd->override_in_ch_map = false;
  1793. return ret;
  1794. }
  1795. static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  1796. struct snd_ctl_elem_value *ucontrol)
  1797. {
  1798. u64 fe_id = kcontrol->private_value & 0xFF;
  1799. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1800. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1801. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1802. struct msm_pcm_channel_mixer *chmixer_pspd;
  1803. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1804. if (!chmixer_pspd) {
  1805. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1806. return -EINVAL;
  1807. }
  1808. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  1809. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  1810. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  1811. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  1812. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  1813. return 0;
  1814. }
  1815. static int msm_pcm_channel_mixer_output_map_ctl_put(
  1816. struct snd_kcontrol *kcontrol,
  1817. struct snd_ctl_elem_value *ucontrol)
  1818. {
  1819. u64 fe_id = kcontrol->private_value & 0xFF;
  1820. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1821. int i = 0;
  1822. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1823. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1824. struct msm_pcm_channel_mixer *chmixer_pspd;
  1825. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1826. if (!chmixer_pspd) {
  1827. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1828. return -EINVAL;
  1829. }
  1830. chmixer_pspd->override_out_ch_map = true;
  1831. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1832. chmixer_pspd->out_ch_map[i] =
  1833. ucontrol->value.integer.value[i];
  1834. return 0;
  1835. }
  1836. static int msm_pcm_channel_mixer_output_map_ctl_get(
  1837. struct snd_kcontrol *kcontrol,
  1838. struct snd_ctl_elem_value *ucontrol)
  1839. {
  1840. u64 fe_id = kcontrol->private_value & 0xFF;
  1841. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1842. int i = 0;
  1843. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1844. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1845. struct msm_pcm_channel_mixer *chmixer_pspd;
  1846. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1847. if (!chmixer_pspd) {
  1848. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1849. return -EINVAL;
  1850. }
  1851. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1852. ucontrol->value.integer.value[i] =
  1853. chmixer_pspd->out_ch_map[i];
  1854. return 0;
  1855. }
  1856. static int msm_pcm_channel_mixer_input_map_ctl_put(
  1857. struct snd_kcontrol *kcontrol,
  1858. struct snd_ctl_elem_value *ucontrol)
  1859. {
  1860. u64 fe_id = kcontrol->private_value & 0xFF;
  1861. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1862. int i = 0;
  1863. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1864. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1865. struct msm_pcm_channel_mixer *chmixer_pspd;
  1866. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1867. if (!chmixer_pspd) {
  1868. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1869. return -EINVAL;
  1870. }
  1871. chmixer_pspd->override_in_ch_map = true;
  1872. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1873. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  1874. return 0;
  1875. }
  1876. static int msm_pcm_channel_mixer_input_map_ctl_get(
  1877. struct snd_kcontrol *kcontrol,
  1878. struct snd_ctl_elem_value *ucontrol)
  1879. {
  1880. u64 fe_id = kcontrol->private_value & 0xFF;
  1881. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1882. int i = 0;
  1883. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1884. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1885. struct msm_pcm_channel_mixer *chmixer_pspd;
  1886. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1887. if (!chmixer_pspd) {
  1888. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1889. return -EINVAL;
  1890. }
  1891. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1892. ucontrol->value.integer.value[i] =
  1893. chmixer_pspd->in_ch_map[i];
  1894. return 0;
  1895. }
  1896. static int msm_pcm_channel_mixer_weight_ctl_put(
  1897. struct snd_kcontrol *kcontrol,
  1898. struct snd_ctl_elem_value *ucontrol)
  1899. {
  1900. u64 fe_id = kcontrol->private_value & 0xFF;
  1901. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1902. int channel = (kcontrol->private_value >> 16) & 0xFF;
  1903. int i = 0;
  1904. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1905. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1906. struct msm_pcm_channel_mixer *chmixer_pspd;
  1907. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1908. if (!chmixer_pspd) {
  1909. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1910. return -EINVAL;
  1911. }
  1912. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1913. pr_err("%s: invalid channel number %d\n", __func__, channel);
  1914. return -EINVAL;
  1915. }
  1916. channel--;
  1917. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1918. chmixer_pspd->channel_weight[channel][i] =
  1919. ucontrol->value.integer.value[i];
  1920. return 0;
  1921. }
  1922. static int msm_pcm_channel_mixer_weight_ctl_get(
  1923. struct snd_kcontrol *kcontrol,
  1924. struct snd_ctl_elem_value *ucontrol)
  1925. {
  1926. u64 fe_id = kcontrol->private_value & 0xFF;
  1927. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1928. int channel = (kcontrol->private_value >> 16) & 0xFF;
  1929. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  1930. struct msm_plat_data *pdata = dev_get_drvdata(component->dev);
  1931. int i = 0;
  1932. struct msm_pcm_channel_mixer *chmixer_pspd;
  1933. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1934. pr_err("%s: invalid channel number %d\n", __func__, channel);
  1935. return -EINVAL;
  1936. }
  1937. channel--;
  1938. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1939. if (!chmixer_pspd) {
  1940. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1941. return -EINVAL;
  1942. }
  1943. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1944. ucontrol->value.integer.value[i] =
  1945. chmixer_pspd->channel_weight[channel][i];
  1946. return 0;
  1947. }
  1948. static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
  1949. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  1950. const char *name_suffix, int session_type, int channels)
  1951. {
  1952. int ret = -EINVAL;
  1953. char *mixer_name = NULL;
  1954. struct snd_pcm *pcm = rtd->pcm;
  1955. const char *deviceNo = "NN";
  1956. const char *channelNo = "NN";
  1957. int ctl_len = 0;
  1958. struct snd_soc_component *component = NULL;
  1959. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1960. if (!component) {
  1961. pr_err("%s: component is NULL\n", __func__);
  1962. return -EINVAL;
  1963. }
  1964. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  1965. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  1966. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  1967. if (mixer_name == NULL)
  1968. return -ENOMEM;
  1969. if (channels >= 0) {
  1970. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  1971. name_prefix, pcm->device, name_suffix, channels);
  1972. kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
  1973. (channels << 16);
  1974. } else {
  1975. snprintf(mixer_name, ctl_len, "%s %d %s",
  1976. name_prefix, pcm->device, name_suffix);
  1977. kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
  1978. }
  1979. kctl->name = mixer_name;
  1980. ret = snd_soc_add_component_controls(component, kctl, 1);
  1981. kfree(mixer_name);
  1982. return ret;
  1983. }
  1984. static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  1985. struct snd_ctl_elem_info *uinfo)
  1986. {
  1987. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1988. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  1989. /* Valid channel map value ranges from 1 to 64 */
  1990. uinfo->value.integer.min = 1;
  1991. uinfo->value.integer.max = 64;
  1992. return 0;
  1993. }
  1994. static int msm_pcm_add_channel_mixer_output_map_controls(
  1995. struct snd_soc_pcm_runtime *rtd)
  1996. {
  1997. struct snd_pcm *pcm = rtd->pcm;
  1998. const char *playback_mixer_ctl_name = "AudStr";
  1999. const char *capture_mixer_ctl_name = "AudStr Capture";
  2000. const char *suffix = "ChMixer Output Map";
  2001. int session_type = 0, ret = 0, channel = -1;
  2002. struct snd_kcontrol_new channel_mixer_output_map_control = {
  2003. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2004. .name = "?",
  2005. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2006. .info = msm_pcm_channel_mixer_output_map_info,
  2007. .put = msm_pcm_channel_mixer_output_map_ctl_put,
  2008. .get = msm_pcm_channel_mixer_output_map_ctl_get,
  2009. .private_value = 0,
  2010. };
  2011. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2012. session_type = SESSION_TYPE_RX;
  2013. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2014. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2015. if (ret < 0)
  2016. goto fail;
  2017. }
  2018. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2019. session_type = SESSION_TYPE_TX;
  2020. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  2021. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2022. if (ret < 0)
  2023. goto fail;
  2024. }
  2025. return 0;
  2026. fail:
  2027. pr_err("%s: failed add platform ctl, err = %d\n",
  2028. __func__, ret);
  2029. return ret;
  2030. }
  2031. static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  2032. struct snd_ctl_elem_info *uinfo)
  2033. {
  2034. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2035. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2036. /* Valid channel map value ranges from 1 to 64 */
  2037. uinfo->value.integer.min = 1;
  2038. uinfo->value.integer.max = 64;
  2039. return 0;
  2040. }
  2041. static int msm_pcm_add_channel_mixer_input_map_controls(
  2042. struct snd_soc_pcm_runtime *rtd)
  2043. {
  2044. struct snd_pcm *pcm = rtd->pcm;
  2045. const char *playback_mixer_ctl_name = "AudStr";
  2046. const char *capture_mixer_ctl_name = "AudStr Capture";
  2047. const char *suffix = "ChMixer Input Map";
  2048. int session_type = 0, ret = 0, channel = -1;
  2049. struct snd_kcontrol_new channel_mixer_input_map_control = {
  2050. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2051. .name = "?",
  2052. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2053. .info = msm_pcm_channel_mixer_input_map_info,
  2054. .put = msm_pcm_channel_mixer_input_map_ctl_put,
  2055. .get = msm_pcm_channel_mixer_input_map_ctl_get,
  2056. .private_value = 0,
  2057. };
  2058. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2059. session_type = SESSION_TYPE_RX;
  2060. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2061. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2062. if (ret < 0)
  2063. goto fail;
  2064. }
  2065. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2066. session_type = SESSION_TYPE_TX;
  2067. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  2068. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2069. if (ret < 0)
  2070. goto fail;
  2071. }
  2072. return 0;
  2073. fail:
  2074. pr_err("%s: failed add platform ctl, err = %d\n",
  2075. __func__, ret);
  2076. return ret;
  2077. }
  2078. static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  2079. struct snd_ctl_elem_info *uinfo)
  2080. {
  2081. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2082. /* five int values: enable, rule, in_channels, out_channels and port_id */
  2083. uinfo->count = 5;
  2084. /* Valid range is all positive values to support above controls */
  2085. uinfo->value.integer.min = 0;
  2086. uinfo->value.integer.max = INT_MAX;
  2087. return 0;
  2088. }
  2089. static int msm_pcm_add_channel_mixer_cfg_controls(
  2090. struct snd_soc_pcm_runtime *rtd)
  2091. {
  2092. struct snd_pcm *pcm = rtd->pcm;
  2093. const char *playback_mixer_ctl_name = "AudStr";
  2094. const char *capture_mixer_ctl_name = "AudStr Capture";
  2095. const char *suffix = "ChMixer Cfg";
  2096. int session_type = 0, ret = 0, channel = -1;
  2097. struct msm_plat_data *pdata = NULL;
  2098. struct snd_soc_component *component = NULL;
  2099. struct snd_kcontrol_new channel_mixer_cfg_control = {
  2100. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2101. .name = "?",
  2102. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2103. .info = msm_pcm_channel_mixer_cfg_info,
  2104. .put = msm_pcm_channel_mixer_cfg_ctl_put,
  2105. .get = msm_pcm_channel_mixer_cfg_ctl_get,
  2106. .private_value = 0,
  2107. };
  2108. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2109. if (!component) {
  2110. pr_err("%s: component is NULL\n", __func__);
  2111. return -EINVAL;
  2112. }
  2113. pdata = (struct msm_plat_data *)
  2114. dev_get_drvdata(component->dev);
  2115. pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
  2116. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2117. session_type = SESSION_TYPE_RX;
  2118. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2119. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2120. if (ret < 0)
  2121. goto fail;
  2122. }
  2123. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2124. session_type = SESSION_TYPE_TX;
  2125. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  2126. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2127. if (ret < 0)
  2128. goto fail;
  2129. }
  2130. return 0;
  2131. fail:
  2132. pr_err("%s: failed add platform ctl, err = %d\n",
  2133. __func__, ret);
  2134. return ret;
  2135. }
  2136. static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  2137. struct snd_ctl_elem_info *uinfo)
  2138. {
  2139. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2140. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  2141. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  2142. uinfo->value.integer.min = 0;
  2143. uinfo->value.integer.max = 0x4000;
  2144. return 0;
  2145. }
  2146. static int msm_pcm_add_channel_mixer_weight_controls(
  2147. struct snd_soc_pcm_runtime *rtd,
  2148. int channel)
  2149. {
  2150. struct snd_pcm *pcm = rtd->pcm;
  2151. const char *playback_mixer_ctl_name = "AudStr";
  2152. const char *capture_mixer_ctl_name = "AudStr Capture";
  2153. const char *suffix = "ChMixer Weight Ch";
  2154. int session_type = 0, ret = 0;
  2155. struct snd_kcontrol_new channel_mixer_weight_control = {
  2156. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2157. .name = "?",
  2158. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2159. .info = msm_pcm_channel_mixer_weight_info,
  2160. .put = msm_pcm_channel_mixer_weight_ctl_put,
  2161. .get = msm_pcm_channel_mixer_weight_ctl_get,
  2162. .private_value = 0,
  2163. };
  2164. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  2165. session_type = SESSION_TYPE_RX;
  2166. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2167. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  2168. if (ret < 0)
  2169. goto fail;
  2170. }
  2171. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  2172. session_type = SESSION_TYPE_TX;
  2173. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  2174. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  2175. if (ret < 0)
  2176. goto fail;
  2177. }
  2178. return 0;
  2179. fail:
  2180. pr_err("%s: failed add platform ctl, err = %d\n",
  2181. __func__, ret);
  2182. return ret;
  2183. }
  2184. static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  2185. {
  2186. int i, ret = 0;
  2187. struct snd_pcm *pcm = NULL;
  2188. struct msm_plat_data *pdata = NULL;
  2189. struct snd_soc_component *component = NULL;
  2190. if (!rtd || !rtd->pcm) {
  2191. pr_err("%s invalid rtd or pcm\n", __func__);
  2192. return -EINVAL;
  2193. }
  2194. pcm = rtd->pcm;
  2195. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  2196. if (!component) {
  2197. pr_err("%s: component is NULL\n", __func__);
  2198. return -EINVAL;
  2199. }
  2200. pdata = (struct msm_plat_data *)
  2201. dev_get_drvdata(component->dev);
  2202. if (!pdata) {
  2203. pr_err("%s: platform data not populated\n", __func__);
  2204. return -EINVAL;
  2205. }
  2206. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
  2207. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2208. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
  2209. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2210. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  2211. ret = -ENOMEM;
  2212. goto fail;
  2213. }
  2214. }
  2215. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
  2216. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2217. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
  2218. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  2219. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  2220. ret = -ENOMEM;
  2221. goto fail;
  2222. }
  2223. }
  2224. ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
  2225. if (ret) {
  2226. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  2227. __func__, ret);
  2228. goto fail;
  2229. }
  2230. ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
  2231. if (ret) {
  2232. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  2233. __func__, ret);
  2234. goto fail;
  2235. }
  2236. ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
  2237. if (ret) {
  2238. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  2239. __func__, ret);
  2240. goto fail;
  2241. }
  2242. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  2243. ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
  2244. if (ret) {
  2245. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  2246. __func__, ret);
  2247. goto fail;
  2248. }
  2249. }
  2250. return 0;
  2251. fail:
  2252. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
  2253. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
  2254. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
  2255. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
  2256. return ret;
  2257. }
  2258. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  2259. {
  2260. int ret = 0;
  2261. pr_debug("%s\n", __func__);
  2262. ret = msm_pcm_add_chmap_controls(rtd);
  2263. if (ret)
  2264. pr_err("%s: pcm add controls failed:%d\n", __func__, ret);
  2265. ret = msm_pcm_add_app_type_controls(rtd);
  2266. if (ret)
  2267. pr_err("%s: pcm add app type controls failed:%d\n",
  2268. __func__, ret);
  2269. ret = msm_pcm_add_channel_mixer_controls(rtd);
  2270. if (ret)
  2271. pr_err("%s: pcm add channel mixer controls failed:%d\n",
  2272. __func__, ret);
  2273. return ret;
  2274. }
  2275. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  2276. {
  2277. struct snd_card *card = rtd->card->snd_card;
  2278. int ret = 0;
  2279. if (!card->dev->coherent_dma_mask)
  2280. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  2281. ret = msm_pcm_add_controls(rtd);
  2282. if (ret) {
  2283. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  2284. return ret;
  2285. }
  2286. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
  2287. if (ret)
  2288. pr_err("%s: Could not add pcm Volume Control %d\n",
  2289. __func__, ret);
  2290. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
  2291. if (ret)
  2292. pr_err("%s: Could not add pcm Volume Control %d\n",
  2293. __func__, ret);
  2294. ret = msm_pcm_add_compress_control(rtd);
  2295. if (ret)
  2296. pr_err("%s: Could not add pcm Compress Control %d\n",
  2297. __func__, ret);
  2298. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  2299. if (ret)
  2300. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  2301. __func__);
  2302. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  2303. if (ret)
  2304. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  2305. __func__);
  2306. return ret;
  2307. }
  2308. static snd_pcm_sframes_t msm_pcm_delay_blk(struct snd_pcm_substream *substream,
  2309. struct snd_soc_dai *dai)
  2310. {
  2311. struct snd_pcm_runtime *runtime = substream->runtime;
  2312. struct msm_audio *prtd = runtime->private_data;
  2313. struct audio_client *ac = prtd->audio_client;
  2314. snd_pcm_sframes_t frames;
  2315. int ret;
  2316. ret = q6asm_get_path_delay(prtd->audio_client);
  2317. if (ret) {
  2318. pr_err("%s: get_path_delay failed, ret=%d\n", __func__, ret);
  2319. return 0;
  2320. }
  2321. /* convert microseconds to frames */
  2322. frames = ac->path_delay / 1000 * runtime->rate / 1000;
  2323. /* also convert the remainder from the initial division */
  2324. frames += ac->path_delay % 1000 * runtime->rate / 1000000;
  2325. /* overcompensate for the loss of precision (empirical) */
  2326. frames += 2;
  2327. return frames;
  2328. }
  2329. static struct snd_soc_component_driver msm_soc_component = {
  2330. .name = DRV_NAME,
  2331. .ops = &msm_pcm_ops,
  2332. .pcm_new = msm_asoc_pcm_new,
  2333. .delay_blk = msm_pcm_delay_blk,
  2334. };
  2335. static int msm_pcm_probe(struct platform_device *pdev)
  2336. {
  2337. int rc;
  2338. int id;
  2339. struct msm_plat_data *pdata;
  2340. const char *latency_level;
  2341. rc = of_property_read_u32(pdev->dev.of_node,
  2342. "qcom,msm-pcm-dsp-id", &id);
  2343. if (rc) {
  2344. dev_err(&pdev->dev, "%s: qcom,msm-pcm-dsp-id missing in DT node\n",
  2345. __func__);
  2346. return rc;
  2347. }
  2348. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  2349. if (!pdata)
  2350. return -ENOMEM;
  2351. if (of_property_read_bool(pdev->dev.of_node,
  2352. "qcom,msm-pcm-low-latency")) {
  2353. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  2354. rc = of_property_read_string(pdev->dev.of_node,
  2355. "qcom,latency-level", &latency_level);
  2356. if (!rc) {
  2357. if (!strcmp(latency_level, "ultra"))
  2358. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  2359. else if (!strcmp(latency_level, "ull-pp"))
  2360. pdata->perf_mode =
  2361. ULL_POST_PROCESSING_PCM_MODE;
  2362. }
  2363. } else {
  2364. pdata->perf_mode = LEGACY_PCM_MODE;
  2365. }
  2366. dev_set_drvdata(&pdev->dev, pdata);
  2367. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  2368. __func__, dev_name(&pdev->dev));
  2369. return snd_soc_register_component(&pdev->dev,
  2370. &msm_soc_component,
  2371. NULL, 0);
  2372. }
  2373. static int msm_pcm_remove(struct platform_device *pdev)
  2374. {
  2375. struct msm_plat_data *pdata;
  2376. int i = 0;
  2377. pdata = dev_get_drvdata(&pdev->dev);
  2378. if (pdata) {
  2379. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
  2380. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
  2381. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
  2382. }
  2383. }
  2384. kfree(pdata);
  2385. snd_soc_unregister_component(&pdev->dev);
  2386. return 0;
  2387. }
  2388. static const struct of_device_id msm_pcm_dt_match[] = {
  2389. {.compatible = "qcom,msm-pcm-dsp"},
  2390. {}
  2391. };
  2392. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  2393. static struct platform_driver msm_pcm_driver = {
  2394. .driver = {
  2395. .name = "msm-pcm-dsp",
  2396. .owner = THIS_MODULE,
  2397. .of_match_table = msm_pcm_dt_match,
  2398. .suppress_bind_attrs = true,
  2399. },
  2400. .probe = msm_pcm_probe,
  2401. .remove = msm_pcm_remove,
  2402. };
  2403. int __init msm_pcm_dsp_init(void)
  2404. {
  2405. init_waitqueue_head(&the_locks.enable_wait);
  2406. init_waitqueue_head(&the_locks.eos_wait);
  2407. init_waitqueue_head(&the_locks.write_wait);
  2408. init_waitqueue_head(&the_locks.read_wait);
  2409. return platform_driver_register(&msm_pcm_driver);
  2410. }
  2411. void msm_pcm_dsp_exit(void)
  2412. {
  2413. platform_driver_unregister(&msm_pcm_driver);
  2414. }
  2415. MODULE_DESCRIPTION("PCM module platform driver");
  2416. MODULE_LICENSE("GPL v2");