msm-pcm-q6-v2.c 76 KB

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