msm-pcm-q6-v2.c 79 KB

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