msm-pcm-q6-v2.c 81 KB

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