msm-pcm-q6-v2.c 77 KB

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