msm-pcm-q6-v2.c 83 KB

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