msm-pcm-q6-v2.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /* Copyright (c) 2012-2017, 2019 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/time.h>
  17. #include <linux/mutex.h>
  18. #include <linux/wait.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <sound/pcm.h>
  25. #include <sound/initval.h>
  26. #include <sound/control.h>
  27. #include <sound/q6audio-v2.h>
  28. #include <sound/timer.h>
  29. #include <asm/dma.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/msm_audio_ion.h>
  32. #include <linux/of_device.h>
  33. #include <sound/tlv.h>
  34. #include <sound/pcm_params.h>
  35. #include "msm-pcm-q6-v2.h"
  36. #include "msm-pcm-routing-v2.h"
  37. #ifdef DUALWAVE_ENABLE
  38. #include <linux/syscalls.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/vmalloc.h>
  42. #include <linux/fs.h>
  43. #define SND_PLAYBACK_UNAVAILABLE 0
  44. #define SND_PLAYBACK_AVAILABLE 1
  45. #define SND_CAPTURE_UNAVAILABLE 2
  46. #define SND_CAPTURE_AVAILABLE 3
  47. #define DUALWAVE_INACTIVE 0
  48. #define DUALWAVE_PLAYBACK 1
  49. #define DUALWAVE_CAPTURE 2
  50. #define GET_CUR_TIME_ON(tCurTimespec) \
  51. do { \
  52. long int llErrTime = 0; \
  53. struct timespec tMyTime; \
  54. mm_segment_t tOldfs; \
  55. tOldfs = get_fs(); \
  56. set_fs(KERNEL_DS); \
  57. \
  58. llErrTime = sys_clock_gettime(CLOCK_REALTIME, &tMyTime); \
  59. set_fs(tOldfs); \
  60. \
  61. tCurTimespec = tMyTime; \
  62. }while(0)
  63. static struct timespec res;
  64. extern int send_uevent_wh_timeinfo(const char *szName, struct timespec *ptTime);
  65. extern int send_uevent_snd_avail(int state);
  66. extern int checkDualWaveStatus(void);
  67. static int dw_status = 0;
  68. #endif
  69. static struct audio_locks the_locks;
  70. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  71. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  72. PCM_MASTER_VOL_MAX_STEPS);
  73. struct snd_msm {
  74. struct snd_card *card;
  75. struct snd_pcm *pcm;
  76. };
  77. #define PLAYBACK_MIN_NUM_PERIODS 2
  78. #define PLAYBACK_MAX_NUM_PERIODS 8
  79. #define PLAYBACK_MAX_PERIOD_SIZE 30720
  80. #define PLAYBACK_MIN_PERIOD_SIZE 128
  81. #define CAPTURE_MIN_NUM_PERIODS 2
  82. #define CAPTURE_MAX_NUM_PERIODS 8
  83. #define CAPTURE_MAX_PERIOD_SIZE 4096
  84. #define CAPTURE_MIN_PERIOD_SIZE 320
  85. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  86. .info = (SNDRV_PCM_INFO_MMAP |
  87. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  88. SNDRV_PCM_INFO_MMAP_VALID |
  89. SNDRV_PCM_INFO_INTERLEAVED |
  90. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  91. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  92. SNDRV_PCM_FMTBIT_S24_LE),
  93. .rates = SNDRV_PCM_RATE_8000_48000,
  94. .rate_min = 8000,
  95. .rate_max = 48000,
  96. .channels_min = 1,
  97. .channels_max = 4,
  98. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  99. CAPTURE_MAX_PERIOD_SIZE,
  100. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  101. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  102. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  103. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  104. .fifo_size = 0,
  105. };
  106. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  107. .info = (SNDRV_PCM_INFO_MMAP |
  108. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  109. SNDRV_PCM_INFO_MMAP_VALID |
  110. SNDRV_PCM_INFO_INTERLEAVED |
  111. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  112. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  113. SNDRV_PCM_FMTBIT_S24_LE),
  114. .rates = SNDRV_PCM_RATE_8000_192000,
  115. .rate_min = 8000,
  116. .rate_max = 192000,
  117. .channels_min = 1,
  118. .channels_max = 8,
  119. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS *
  120. PLAYBACK_MAX_PERIOD_SIZE,
  121. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  122. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  123. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  124. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  125. .fifo_size = 0,
  126. };
  127. /* Conventional and unconventional sample rate supported */
  128. static unsigned int supported_sample_rates[] = {
  129. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  130. 88200, 96000, 176400, 192000
  131. };
  132. static uint32_t in_frame_info[CAPTURE_MAX_NUM_PERIODS][2];
  133. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  134. .count = ARRAY_SIZE(supported_sample_rates),
  135. .list = supported_sample_rates,
  136. .mask = 0,
  137. };
  138. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  139. void *priv_data)
  140. {
  141. struct msm_audio *prtd = priv_data;
  142. BUG_ON(!prtd);
  143. pr_debug("%s: event %x\n", __func__, event);
  144. switch (event) {
  145. case MSM_PCM_RT_EVT_BUF_RECFG:
  146. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  147. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  148. q6asm_run(prtd->audio_client, 0, 0, 0);
  149. default:
  150. break;
  151. }
  152. }
  153. static void event_handler(uint32_t opcode,
  154. uint32_t token, uint32_t *payload, void *priv)
  155. {
  156. struct msm_audio *prtd = priv;
  157. struct snd_pcm_substream *substream = prtd->substream;
  158. uint32_t *ptrmem = (uint32_t *)payload;
  159. uint32_t idx = 0;
  160. uint32_t size = 0;
  161. switch (opcode) {
  162. case ASM_DATA_EVENT_WRITE_DONE_V2: {
  163. pr_debug("ASM_DATA_EVENT_WRITE_DONE_V2\n");
  164. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  165. prtd->pcm_irq_pos += prtd->pcm_count;
  166. if (atomic_read(&prtd->start))
  167. snd_pcm_period_elapsed(substream);
  168. atomic_inc(&prtd->out_count);
  169. wake_up(&the_locks.write_wait);
  170. if (!atomic_read(&prtd->start))
  171. break;
  172. if (!prtd->mmap_flag)
  173. break;
  174. if (q6asm_is_cpu_buf_avail_nolock(IN,
  175. prtd->audio_client,
  176. &size, &idx)) {
  177. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  178. __func__, prtd->pcm_count);
  179. q6asm_write_nolock(prtd->audio_client,
  180. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  181. }
  182. break;
  183. }
  184. case ASM_DATA_EVENT_RENDERED_EOS:
  185. pr_debug("ASM_DATA_EVENT_RENDERED_EOS\n");
  186. clear_bit(CMD_EOS, &prtd->cmd_pending);
  187. wake_up(&the_locks.eos_wait);
  188. break;
  189. case ASM_DATA_EVENT_READ_DONE_V2: {
  190. pr_debug("ASM_DATA_EVENT_READ_DONE_V2\n");
  191. pr_debug("token = 0x%08x\n", token);
  192. in_frame_info[token][0] = payload[4];
  193. in_frame_info[token][1] = payload[5];
  194. /* assume data size = 0 during flushing */
  195. if (in_frame_info[token][0]) {
  196. prtd->pcm_irq_pos += in_frame_info[token][0];
  197. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  198. if (atomic_read(&prtd->start))
  199. snd_pcm_period_elapsed(substream);
  200. if (atomic_read(&prtd->in_count) <= prtd->periods)
  201. atomic_inc(&prtd->in_count);
  202. wake_up(&the_locks.read_wait);
  203. if (prtd->mmap_flag &&
  204. q6asm_is_cpu_buf_avail_nolock(OUT,
  205. prtd->audio_client,
  206. &size, &idx))
  207. q6asm_read_nolock(prtd->audio_client);
  208. } else {
  209. pr_debug("%s: reclaim flushed buf in_count %x\n",
  210. __func__, atomic_read(&prtd->in_count));
  211. prtd->pcm_irq_pos += prtd->pcm_count;
  212. atomic_inc(&prtd->in_count);
  213. if (atomic_read(&prtd->in_count) == prtd->periods) {
  214. pr_info("%s: reclaimed all bufs\n", __func__);
  215. if (atomic_read(&prtd->start))
  216. snd_pcm_period_elapsed(substream);
  217. wake_up(&the_locks.read_wait);
  218. }
  219. }
  220. break;
  221. }
  222. case APR_BASIC_RSP_RESULT: {
  223. switch (payload[0]) {
  224. case ASM_SESSION_CMD_RUN_V2:
  225. if (substream->stream
  226. != SNDRV_PCM_STREAM_PLAYBACK) {
  227. atomic_set(&prtd->start, 1);
  228. break;
  229. }
  230. if (prtd->mmap_flag) {
  231. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  232. __func__,
  233. prtd->pcm_count);
  234. q6asm_write_nolock(prtd->audio_client,
  235. prtd->pcm_count,
  236. 0, 0, NO_TIMESTAMP);
  237. } else {
  238. while (atomic_read(&prtd->out_needed)) {
  239. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  240. __func__,
  241. prtd->pcm_count);
  242. q6asm_write_nolock(prtd->audio_client,
  243. prtd->pcm_count,
  244. 0, 0, NO_TIMESTAMP);
  245. atomic_dec(&prtd->out_needed);
  246. wake_up(&the_locks.write_wait);
  247. };
  248. }
  249. atomic_set(&prtd->start, 1);
  250. break;
  251. default:
  252. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  253. __func__, payload[0], payload[1]);
  254. break;
  255. }
  256. }
  257. break;
  258. case RESET_EVENTS:
  259. pr_err("%s RESET_EVENTS\n", __func__);
  260. prtd->pcm_irq_pos += prtd->pcm_count;
  261. atomic_inc(&prtd->out_count);
  262. atomic_inc(&prtd->in_count);
  263. prtd->reset_event = true;
  264. if (atomic_read(&prtd->start))
  265. snd_pcm_period_elapsed(substream);
  266. wake_up(&the_locks.eos_wait);
  267. wake_up(&the_locks.write_wait);
  268. wake_up(&the_locks.read_wait);
  269. break;
  270. default:
  271. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  272. break;
  273. }
  274. }
  275. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  276. {
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  279. struct msm_audio *prtd = runtime->private_data;
  280. struct msm_plat_data *pdata;
  281. struct snd_pcm_hw_params *params;
  282. int ret;
  283. uint16_t bits_per_sample = 16;
  284. pdata = (struct msm_plat_data *)
  285. dev_get_drvdata(soc_prtd->platform->dev);
  286. if (!pdata) {
  287. pr_err("%s: platform data not populated\n", __func__);
  288. return -EINVAL;
  289. }
  290. params = &soc_prtd->dpcm[substream->stream].hw_params;
  291. pr_debug("%s\n", __func__);
  292. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  293. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  294. prtd->pcm_irq_pos = 0;
  295. /* rate and channels are sent to audio driver */
  296. prtd->samp_rate = runtime->rate;
  297. prtd->channel_mode = runtime->channels;
  298. if (prtd->enabled)
  299. return 0;
  300. prtd->audio_client->perf_mode = pdata->perf_mode;
  301. pr_info("%s: perf: %x\n", __func__, pdata->perf_mode);
  302. if (params_format(params) == SNDRV_PCM_FORMAT_S24_LE)
  303. bits_per_sample = 24;
  304. ret = q6asm_open_write_v2(prtd->audio_client,
  305. FORMAT_LINEAR_PCM, bits_per_sample);
  306. if (ret < 0) {
  307. pr_err("%s: q6asm_open_write_v2 failed\n", __func__);
  308. q6asm_audio_client_free(prtd->audio_client);
  309. prtd->audio_client = NULL;
  310. return -ENOMEM;
  311. }
  312. pr_info("%s: session ID %d\n", __func__,
  313. prtd->audio_client->session);
  314. prtd->session_id = prtd->audio_client->session;
  315. msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id,
  316. prtd->audio_client->perf_mode,
  317. prtd->session_id, substream->stream);
  318. switch (runtime->format) {
  319. case SNDRV_PCM_FORMAT_S16_LE:
  320. bits_per_sample = 16;
  321. break;
  322. case SNDRV_PCM_FORMAT_S24_LE:
  323. bits_per_sample = 24;
  324. break;
  325. }
  326. ret = q6asm_media_format_block_multi_ch_pcm_v2(
  327. prtd->audio_client, runtime->rate,
  328. runtime->channels, !prtd->set_channel_map,
  329. prtd->channel_map, bits_per_sample);
  330. if (ret < 0)
  331. pr_info("%s: CMD Format block failed\n", __func__);
  332. atomic_set(&prtd->out_count, runtime->periods);
  333. prtd->enabled = 1;
  334. prtd->cmd_pending = 0;
  335. prtd->cmd_interrupt = 0;
  336. return 0;
  337. }
  338. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  339. {
  340. struct snd_pcm_runtime *runtime = substream->runtime;
  341. struct msm_audio *prtd = runtime->private_data;
  342. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  343. struct msm_plat_data *pdata;
  344. struct snd_pcm_hw_params *params;
  345. struct msm_pcm_routing_evt event;
  346. int ret = 0;
  347. int i = 0;
  348. uint16_t bits_per_sample = 16;
  349. pdata = (struct msm_plat_data *)
  350. dev_get_drvdata(soc_prtd->platform->dev);
  351. if (!pdata) {
  352. pr_err("%s: platform data not populated\n", __func__);
  353. return -EINVAL;
  354. }
  355. pr_debug("%s\n", __func__);
  356. params = &soc_prtd->dpcm[substream->stream].hw_params;
  357. if (params_format(params) == SNDRV_PCM_FORMAT_S24_LE)
  358. bits_per_sample = 24;
  359. prtd->audio_client->perf_mode = pdata->perf_mode;
  360. pr_info("%s: perf_mode: 0x%x\n", __func__, pdata->perf_mode);
  361. pr_info("%s Opening %d-ch PCM read stream\n",
  362. __func__, params_channels(params));
  363. ret = q6asm_open_read_v2(prtd->audio_client, FORMAT_LINEAR_PCM,
  364. bits_per_sample);
  365. if (ret < 0) {
  366. pr_err("%s: q6asm_open_read failed\n", __func__);
  367. q6asm_audio_client_free(prtd->audio_client);
  368. prtd->audio_client = NULL;
  369. return -ENOMEM;
  370. }
  371. pr_info("%s: session ID %d\n",
  372. __func__, prtd->audio_client->session);
  373. prtd->session_id = prtd->audio_client->session;
  374. event.event_func = msm_pcm_route_event_handler;
  375. event.priv_data = (void *) prtd;
  376. msm_pcm_routing_reg_phy_stream_v2(
  377. soc_prtd->dai_link->be_id,
  378. prtd->audio_client->perf_mode,
  379. prtd->session_id, substream->stream,
  380. event);
  381. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  382. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  383. prtd->pcm_irq_pos = 0;
  384. /* rate and channels are sent to audio driver */
  385. prtd->samp_rate = runtime->rate;
  386. prtd->channel_mode = runtime->channels;
  387. if (prtd->enabled)
  388. return 0;
  389. switch (runtime->format) {
  390. case SNDRV_PCM_FORMAT_S16_LE:
  391. bits_per_sample = 16;
  392. break;
  393. case SNDRV_PCM_FORMAT_S24_LE:
  394. bits_per_sample = 24;
  395. break;
  396. }
  397. pr_debug("Samp_rate = %d\n", prtd->samp_rate);
  398. pr_debug("Channel = %d\n", prtd->channel_mode);
  399. ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client,
  400. prtd->samp_rate, prtd->channel_mode,
  401. bits_per_sample);
  402. if (ret < 0)
  403. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  404. for (i = 0; i < runtime->periods; i++)
  405. q6asm_read(prtd->audio_client);
  406. prtd->periods = runtime->periods;
  407. prtd->enabled = 1;
  408. return ret;
  409. }
  410. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  411. {
  412. int ret = 0;
  413. struct snd_pcm_runtime *runtime = substream->runtime;
  414. struct msm_audio *prtd = runtime->private_data;
  415. switch (cmd) {
  416. case SNDRV_PCM_TRIGGER_START:
  417. case SNDRV_PCM_TRIGGER_RESUME:
  418. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  419. pr_debug("%s: Trigger start\n", __func__);
  420. #ifdef DUALWAVE_ENABLE
  421. if(dw_status != DUALWAVE_INACTIVE){
  422. GET_CUR_TIME_ON(res);
  423. }
  424. #endif
  425. ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  426. #ifdef DUALWAVE_ENABLE
  427. switch(dw_status) {
  428. case DUALWAVE_PLAYBACK:
  429. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  430. send_uevent_wh_timeinfo("PLAY_TIME",&res);
  431. }
  432. break;
  433. case DUALWAVE_CAPTURE:
  434. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE){
  435. send_uevent_wh_timeinfo("CAPTURE_TIME",&res);
  436. }
  437. break;
  438. case DUALWAVE_INACTIVE:
  439. default:
  440. break;
  441. }
  442. #endif
  443. break;
  444. case SNDRV_PCM_TRIGGER_STOP:
  445. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  446. atomic_set(&prtd->start, 0);
  447. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  448. break;
  449. /* pending CMD_EOS isn't expected */
  450. WARN_ON_ONCE(test_bit(CMD_EOS, &prtd->cmd_pending));
  451. set_bit(CMD_EOS, &prtd->cmd_pending);
  452. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  453. if (ret)
  454. clear_bit(CMD_EOS, &prtd->cmd_pending);
  455. break;
  456. case SNDRV_PCM_TRIGGER_SUSPEND:
  457. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  458. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  459. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  460. atomic_set(&prtd->start, 0);
  461. break;
  462. default:
  463. ret = -EINVAL;
  464. break;
  465. }
  466. return ret;
  467. }
  468. static int msm_pcm_open(struct snd_pcm_substream *substream)
  469. {
  470. struct snd_pcm_runtime *runtime = substream->runtime;
  471. struct msm_audio *prtd;
  472. int ret = 0;
  473. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  474. if (prtd == NULL) {
  475. pr_err("Failed to allocate memory for msm_audio\n");
  476. return -ENOMEM;
  477. }
  478. prtd->substream = substream;
  479. prtd->audio_client = q6asm_audio_client_alloc(
  480. (app_cb)event_handler, prtd);
  481. if (!prtd->audio_client) {
  482. pr_info("%s: Could not allocate memory\n", __func__);
  483. kfree(prtd);
  484. return -ENOMEM;
  485. }
  486. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  487. runtime->hw = msm_pcm_hardware_playback;
  488. /* Capture path */
  489. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  490. runtime->hw = msm_pcm_hardware_capture;
  491. else {
  492. pr_err("Invalid Stream type %d\n", substream->stream);
  493. return -EINVAL;
  494. }
  495. ret = snd_pcm_hw_constraint_list(runtime, 0,
  496. SNDRV_PCM_HW_PARAM_RATE,
  497. &constraints_sample_rates);
  498. if (ret < 0)
  499. pr_info("snd_pcm_hw_constraint_list failed\n");
  500. /* Ensure that buffer size is a multiple of period size */
  501. ret = snd_pcm_hw_constraint_integer(runtime,
  502. SNDRV_PCM_HW_PARAM_PERIODS);
  503. if (ret < 0)
  504. pr_info("snd_pcm_hw_constraint_integer failed\n");
  505. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  506. ret = snd_pcm_hw_constraint_minmax(runtime,
  507. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  508. PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
  509. PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
  510. if (ret < 0) {
  511. pr_err("constraint for buffer bytes min max ret = %d\n",
  512. ret);
  513. }
  514. }
  515. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  516. ret = snd_pcm_hw_constraint_minmax(runtime,
  517. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  518. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  519. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  520. if (ret < 0) {
  521. pr_err("constraint for buffer bytes min max ret = %d\n",
  522. ret);
  523. }
  524. }
  525. ret = snd_pcm_hw_constraint_step(runtime, 0,
  526. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  527. if (ret < 0) {
  528. pr_err("constraint for period bytes step ret = %d\n",
  529. ret);
  530. }
  531. ret = snd_pcm_hw_constraint_step(runtime, 0,
  532. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  533. if (ret < 0) {
  534. pr_err("constraint for buffer bytes step ret = %d\n",
  535. ret);
  536. }
  537. prtd->dsp_cnt = 0;
  538. prtd->set_channel_map = false;
  539. prtd->reset_event = false;
  540. runtime->private_data = prtd;
  541. return 0;
  542. }
  543. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  544. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  545. {
  546. int ret = 0;
  547. int fbytes = 0;
  548. int xfer = 0;
  549. char *bufptr = NULL;
  550. void *data = NULL;
  551. uint32_t idx = 0;
  552. uint32_t size = 0;
  553. struct snd_pcm_runtime *runtime = substream->runtime;
  554. struct msm_audio *prtd = runtime->private_data;
  555. fbytes = frames_to_bytes(runtime, frames);
  556. pr_debug("%s: prtd->out_count = %d\n",
  557. __func__, atomic_read(&prtd->out_count));
  558. if (prtd->reset_event) {
  559. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  560. return -ENETRESET;
  561. }
  562. ret = wait_event_timeout(the_locks.write_wait,
  563. (atomic_read(&prtd->out_count)), 5 * HZ);
  564. if (!ret) {
  565. pr_err("%s: wait_event_timeout failed\n", __func__);
  566. goto fail;
  567. }
  568. if (prtd->reset_event) {
  569. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  570. return -ENETRESET;
  571. }
  572. if (!atomic_read(&prtd->out_count)) {
  573. pr_err("%s: pcm stopped out_count 0\n", __func__);
  574. return 0;
  575. }
  576. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size, &idx);
  577. if (size < fbytes) {
  578. fbytes = size;
  579. }
  580. bufptr = data;
  581. if (bufptr) {
  582. pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
  583. __func__, fbytes, xfer, size);
  584. xfer = fbytes;
  585. if (copy_from_user(bufptr, buf, xfer)) {
  586. ret = -EFAULT;
  587. goto fail;
  588. }
  589. buf += xfer;
  590. fbytes -= xfer;
  591. pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes, xfer);
  592. if (atomic_read(&prtd->start)) {
  593. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  594. __func__, xfer);
  595. ret = q6asm_write(prtd->audio_client, xfer,
  596. 0, 0, NO_TIMESTAMP);
  597. if (ret < 0) {
  598. ret = -EFAULT;
  599. goto fail;
  600. }
  601. } else
  602. atomic_inc(&prtd->out_needed);
  603. atomic_dec(&prtd->out_count);
  604. }
  605. fail:
  606. return ret;
  607. }
  608. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  609. {
  610. struct snd_pcm_runtime *runtime = substream->runtime;
  611. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  612. struct msm_audio *prtd = runtime->private_data;
  613. int dir = 0;
  614. int ret = 0;
  615. struct msm_plat_data *pdata;
  616. pr_debug("%s: cmd_pending 0x%lx\n", __func__, prtd->cmd_pending);
  617. pdata = (struct msm_plat_data *)
  618. dev_get_drvdata(soc_prtd->platform->dev);
  619. if (!pdata) {
  620. pr_err("%s: platform data is NULL\n", __func__);
  621. return -EINVAL;
  622. }
  623. mutex_lock(&pdata->lock);
  624. if (prtd->audio_client) {
  625. dir = IN;
  626. ret = wait_event_timeout(the_locks.eos_wait,
  627. !test_bit(CMD_EOS, &prtd->cmd_pending),
  628. 5 * HZ);
  629. if (!ret)
  630. pr_err("%s: CMD_EOS failed, cmd_pending 0x%lx\n",
  631. __func__, prtd->cmd_pending);
  632. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  633. q6asm_audio_client_buf_free_contiguous(dir,
  634. prtd->audio_client);
  635. q6asm_audio_client_free(prtd->audio_client);
  636. }
  637. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  638. SNDRV_PCM_STREAM_PLAYBACK);
  639. kfree(prtd);
  640. runtime->private_data = NULL;
  641. mutex_unlock(&pdata->lock);
  642. return 0;
  643. }
  644. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  645. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  646. snd_pcm_uframes_t frames)
  647. {
  648. int ret = 0;
  649. int fbytes = 0;
  650. int xfer;
  651. char *bufptr;
  652. void *data = NULL;
  653. static uint32_t idx;
  654. static uint32_t size;
  655. uint32_t offset = 0;
  656. struct snd_pcm_runtime *runtime = substream->runtime;
  657. struct msm_audio *prtd = substream->runtime->private_data;
  658. pr_debug("%s\n", __func__);
  659. fbytes = frames_to_bytes(runtime, frames);
  660. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  661. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  662. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  663. if (prtd->reset_event) {
  664. pr_err("%s: In SSR return ENETRESET before wait\n", __func__);
  665. return -ENETRESET;
  666. }
  667. ret = wait_event_timeout(the_locks.read_wait,
  668. (atomic_read(&prtd->in_count)), 5 * HZ);
  669. if (!ret) {
  670. pr_debug("%s: wait_event_timeout failed\n", __func__);
  671. goto fail;
  672. }
  673. if (prtd->reset_event) {
  674. pr_err("%s: In SSR return ENETRESET after wait\n", __func__);
  675. return -ENETRESET;
  676. }
  677. if (!atomic_read(&prtd->in_count)) {
  678. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  679. return 0;
  680. }
  681. pr_debug("Checking if valid buffer is available...%08x\n",
  682. (unsigned int) data);
  683. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  684. bufptr = data;
  685. pr_debug("Size = %d\n", size);
  686. pr_debug("fbytes = %d\n", fbytes);
  687. pr_debug("idx = %d\n", idx);
  688. if (bufptr) {
  689. xfer = fbytes;
  690. if (xfer > size)
  691. xfer = size;
  692. offset = in_frame_info[idx][1];
  693. pr_debug("Offset value = %d\n", offset);
  694. if (copy_to_user(buf, bufptr+offset, xfer)) {
  695. pr_err("Failed to copy buf to user\n");
  696. ret = -EFAULT;
  697. goto fail;
  698. }
  699. fbytes -= xfer;
  700. size -= xfer;
  701. in_frame_info[idx][1] += xfer;
  702. pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
  703. __func__, fbytes, size, xfer);
  704. pr_debug(" Sending next buffer to dsp\n");
  705. memset(&in_frame_info[idx], 0,
  706. sizeof(uint32_t) * 2);
  707. atomic_dec(&prtd->in_count);
  708. ret = q6asm_read(prtd->audio_client);
  709. if (ret < 0) {
  710. pr_err("q6asm read failed\n");
  711. ret = -EFAULT;
  712. goto fail;
  713. }
  714. } else
  715. pr_err("No valid buffer\n");
  716. pr_debug("Returning from capture_copy... %d\n", ret);
  717. fail:
  718. return ret;
  719. }
  720. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  721. {
  722. struct snd_pcm_runtime *runtime = substream->runtime;
  723. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  724. struct msm_audio *prtd = runtime->private_data;
  725. int dir = OUT;
  726. struct msm_plat_data *pdata;
  727. pr_debug("%s\n", __func__);
  728. pdata = (struct msm_plat_data *)
  729. dev_get_drvdata(soc_prtd->platform->dev);
  730. if (!pdata) {
  731. pr_err("%s: platform data is NULL\n", __func__);
  732. return -EINVAL;
  733. }
  734. mutex_lock(&pdata->lock);
  735. if (prtd->audio_client) {
  736. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  737. q6asm_audio_client_buf_free_contiguous(dir,
  738. prtd->audio_client);
  739. q6asm_audio_client_free(prtd->audio_client);
  740. }
  741. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  742. SNDRV_PCM_STREAM_CAPTURE);
  743. kfree(prtd);
  744. runtime->private_data = NULL;
  745. mutex_unlock(&pdata->lock);
  746. return 0;
  747. }
  748. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  749. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  750. {
  751. int ret = 0;
  752. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  753. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  754. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  755. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  756. return ret;
  757. }
  758. static int msm_pcm_close(struct snd_pcm_substream *substream)
  759. {
  760. int ret = 0;
  761. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  762. ret = msm_pcm_playback_close(substream);
  763. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  764. ret = msm_pcm_capture_close(substream);
  765. #ifdef DUALWAVE_ENABLE
  766. switch(dw_status) {
  767. case DUALWAVE_PLAYBACK:
  768. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  769. send_uevent_snd_avail(SND_PLAYBACK_AVAILABLE);
  770. }
  771. break;
  772. case DUALWAVE_CAPTURE:
  773. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE){
  774. send_uevent_snd_avail(SND_CAPTURE_AVAILABLE);
  775. }
  776. break;
  777. case DUALWAVE_INACTIVE:
  778. default:
  779. break;
  780. }
  781. #endif
  782. return ret;
  783. }
  784. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  785. {
  786. int ret = 0;
  787. #ifdef DUALWAVE_ENABLE
  788. dw_status = checkDualWaveStatus();
  789. switch(dw_status) {
  790. case DUALWAVE_PLAYBACK:
  791. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  792. printk("khhan pcm prepare # SEND_UEVENT : PLAYBACK UNAVAILABLE\n");
  793. send_uevent_snd_avail(SND_PLAYBACK_UNAVAILABLE);
  794. }
  795. break;
  796. case DUALWAVE_CAPTURE:
  797. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE){
  798. printk("khhan pcm prepare # SEND_UEVENT : CAPTURE UNAVAILABLE\n");
  799. send_uevent_snd_avail(SND_CAPTURE_UNAVAILABLE);
  800. }
  801. break;
  802. case DUALWAVE_INACTIVE:
  803. default:
  804. break;
  805. }
  806. #endif
  807. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  808. ret = msm_pcm_playback_prepare(substream);
  809. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  810. ret = msm_pcm_capture_prepare(substream);
  811. return ret;
  812. }
  813. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  814. {
  815. struct snd_pcm_runtime *runtime = substream->runtime;
  816. struct msm_audio *prtd = runtime->private_data;
  817. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  818. prtd->pcm_irq_pos = 0;
  819. pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos);
  820. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  821. }
  822. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  823. struct vm_area_struct *vma)
  824. {
  825. struct snd_pcm_runtime *runtime = substream->runtime;
  826. struct msm_audio *prtd = runtime->private_data;
  827. struct audio_client *ac = prtd->audio_client;
  828. struct audio_port_data *apd = ac->port;
  829. struct audio_buffer *ab;
  830. int dir = -1;
  831. prtd->mmap_flag = 1;
  832. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  833. dir = IN;
  834. else
  835. dir = OUT;
  836. ab = &(apd[dir].buf[0]);
  837. return msm_audio_ion_mmap(ab, vma);
  838. }
  839. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  840. struct snd_pcm_hw_params *params)
  841. {
  842. struct snd_pcm_runtime *runtime = substream->runtime;
  843. struct msm_audio *prtd = runtime->private_data;
  844. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  845. struct audio_buffer *buf;
  846. int dir, ret;
  847. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  848. dir = IN;
  849. else
  850. dir = OUT;
  851. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  852. prtd->audio_client,
  853. (params_buffer_bytes(params) / params_periods(params)),
  854. params_periods(params));
  855. if (ret < 0) {
  856. pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
  857. ret);
  858. return -ENOMEM;
  859. }
  860. buf = prtd->audio_client->port[dir].buf;
  861. if (buf == NULL || buf[0].data == NULL)
  862. return -ENOMEM;
  863. pr_debug("%s:buf = %p\n", __func__, buf);
  864. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  865. dma_buf->dev.dev = substream->pcm->card->dev;
  866. dma_buf->private_data = NULL;
  867. dma_buf->area = buf[0].data;
  868. dma_buf->addr = buf[0].phys;
  869. dma_buf->bytes = params_buffer_bytes(params);
  870. if (!dma_buf->area)
  871. return -ENOMEM;
  872. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  873. return 0;
  874. }
  875. static struct snd_pcm_ops msm_pcm_ops = {
  876. .open = msm_pcm_open,
  877. .copy = msm_pcm_copy,
  878. .hw_params = msm_pcm_hw_params,
  879. .close = msm_pcm_close,
  880. .ioctl = snd_pcm_lib_ioctl,
  881. .prepare = msm_pcm_prepare,
  882. .trigger = msm_pcm_trigger,
  883. .pointer = msm_pcm_pointer,
  884. .mmap = msm_pcm_mmap,
  885. };
  886. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  887. {
  888. int rc = 0;
  889. if (prtd && prtd->audio_client) {
  890. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  891. prtd->channel_mode, volume);
  892. rc = q6asm_set_volume(prtd->audio_client, volume);
  893. if (rc < 0) {
  894. pr_err("%s: Send Volume command failed rc=%d\n",
  895. __func__, rc);
  896. }
  897. }
  898. return rc;
  899. }
  900. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  901. struct snd_ctl_elem_value *ucontrol)
  902. {
  903. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  904. struct snd_pcm_substream *substream =
  905. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  906. struct msm_audio *prtd;
  907. struct msm_plat_data *pdata = NULL;
  908. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  909. pr_debug("%s\n", __func__);
  910. if (!substream) {
  911. pr_err("%s substream not found\n", __func__);
  912. return -ENODEV;
  913. }
  914. soc_prtd = substream->private_data;
  915. if (!substream->runtime || !soc_prtd) {
  916. pr_err("%s substream runtime not found\n", __func__);
  917. return 0;
  918. }
  919. pdata = (struct msm_plat_data *)
  920. dev_get_drvdata(soc_prtd->platform->dev);
  921. if (!pdata) {
  922. pr_err("%s: pdata not found\n", __func__);
  923. return -ENODEV;
  924. }
  925. mutex_lock(&pdata->lock);
  926. prtd = substream->runtime->private_data;
  927. if (prtd)
  928. ucontrol->value.integer.value[0] = prtd->volume;
  929. mutex_unlock(&pdata->lock);
  930. return 0;
  931. }
  932. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  933. struct snd_ctl_elem_value *ucontrol)
  934. {
  935. int rc = 0;
  936. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  937. struct snd_pcm_substream *substream =
  938. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  939. struct msm_audio *prtd;
  940. int volume = ucontrol->value.integer.value[0];
  941. struct snd_soc_pcm_runtime *soc_prtd = NULL;
  942. struct msm_plat_data *pdata = NULL;
  943. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  944. if (!substream) {
  945. pr_err("%s: substream not found\n", __func__);
  946. return -ENODEV;
  947. }
  948. soc_prtd = substream->private_data;
  949. if (!substream->runtime || !soc_prtd) {
  950. pr_err("%s: substream runtime or private_data not found\n",
  951. __func__);
  952. return 0;
  953. }
  954. pdata = (struct msm_plat_data *)
  955. dev_get_drvdata(soc_prtd->platform->dev);
  956. if (!pdata) {
  957. pr_err("%s: pdata not found\n", __func__);
  958. return -ENODEV;
  959. }
  960. mutex_lock(&pdata->lock);
  961. prtd = substream->runtime->private_data;
  962. if (prtd) {
  963. rc = msm_pcm_set_volume(prtd, volume);
  964. prtd->volume = volume;
  965. }
  966. mutex_unlock(&pdata->lock);
  967. return rc;
  968. }
  969. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  970. {
  971. int ret = 0;
  972. struct snd_pcm *pcm = rtd->pcm;
  973. struct snd_pcm_volume *volume_info;
  974. struct snd_kcontrol *kctl;
  975. dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
  976. ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  977. NULL, 1, rtd->dai_link->be_id,
  978. &volume_info);
  979. if (ret < 0) {
  980. pr_err("%s volume control failed ret %d\n", __func__, ret);
  981. return ret;
  982. }
  983. kctl = volume_info->kctl;
  984. kctl->put = msm_pcm_volume_ctl_put;
  985. kctl->get = msm_pcm_volume_ctl_get;
  986. kctl->tlv.p = msm_pcm_vol_gain;
  987. return 0;
  988. }
  989. static int msm_pcm_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  990. struct snd_ctl_elem_value *ucontrol)
  991. {
  992. int i;
  993. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  994. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  995. struct snd_pcm_substream *substream;
  996. struct msm_audio *prtd;
  997. struct snd_soc_pcm_runtime *rtd = NULL;
  998. struct msm_plat_data *pdata = NULL;
  999. pr_debug("%s", __func__);
  1000. substream = snd_pcm_chmap_substream(info, idx);
  1001. if (!substream)
  1002. return -ENODEV;
  1003. rtd = substream->private_data;
  1004. if (rtd) {
  1005. pdata = (struct msm_plat_data *)
  1006. dev_get_drvdata(rtd->platform->dev);
  1007. if (!pdata) {
  1008. pr_err("%s: pdata not found\n", __func__);
  1009. return -ENODEV;
  1010. }
  1011. }
  1012. if (!substream->runtime || !rtd)
  1013. return 0;
  1014. mutex_lock(&pdata->lock);
  1015. prtd = substream->runtime->private_data;
  1016. if (prtd) {
  1017. prtd->set_channel_map = true;
  1018. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1019. prtd->channel_map[i] =
  1020. (char)(ucontrol->value.integer.value[i]);
  1021. }
  1022. mutex_unlock(&pdata->lock);
  1023. return 0;
  1024. }
  1025. static int msm_pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1026. struct snd_ctl_elem_value *ucontrol)
  1027. {
  1028. int i;
  1029. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1030. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1031. struct snd_pcm_substream *substream;
  1032. struct msm_audio *prtd;
  1033. struct snd_soc_pcm_runtime *rtd = NULL;
  1034. struct msm_plat_data *pdata = NULL;
  1035. pr_debug("%s", __func__);
  1036. substream = snd_pcm_chmap_substream(info, idx);
  1037. if (!substream)
  1038. return -ENODEV;
  1039. rtd = substream->private_data;
  1040. if (rtd) {
  1041. pdata = (struct msm_plat_data *)
  1042. dev_get_drvdata(rtd->platform->dev);
  1043. if (!pdata) {
  1044. pr_err("%s: pdata not found\n", __func__);
  1045. return -ENODEV;
  1046. }
  1047. }
  1048. memset(ucontrol->value.integer.value, 0,
  1049. sizeof(ucontrol->value.integer.value));
  1050. if (!substream->runtime || !rtd)
  1051. return 0; /* no channels set */
  1052. mutex_lock(&pdata->lock);
  1053. prtd = substream->runtime->private_data;
  1054. if (prtd && prtd->set_channel_map == true) {
  1055. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1056. ucontrol->value.integer.value[i] =
  1057. (int)prtd->channel_map[i];
  1058. } else {
  1059. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  1060. ucontrol->value.integer.value[i] = 0;
  1061. }
  1062. mutex_unlock(&pdata->lock);
  1063. return 0;
  1064. }
  1065. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1066. {
  1067. struct snd_card *card = rtd->card->snd_card;
  1068. struct snd_pcm *pcm = rtd->pcm;
  1069. struct snd_pcm_chmap *chmap_info;
  1070. struct snd_kcontrol *kctl;
  1071. char device_num[12];
  1072. int i, ret = 0;
  1073. if (!card->dev->coherent_dma_mask)
  1074. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1075. pr_debug("%s, Channel map cntrl add\n", __func__);
  1076. ret = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1077. snd_pcm_std_chmaps,
  1078. PCM_FORMAT_MAX_NUM_CHANNEL, 0,
  1079. &chmap_info);
  1080. if (ret < 0)
  1081. return ret;
  1082. kctl = chmap_info->kctl;
  1083. for (i = 0; i < kctl->count; i++)
  1084. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1085. snprintf(device_num, sizeof(device_num), "%d", pcm->device);
  1086. strlcat(kctl->id.name, device_num, sizeof(kctl->id.name));
  1087. pr_debug("%s, Overwriting channel map control name to: %s",
  1088. __func__, kctl->id.name);
  1089. kctl->put = msm_pcm_chmap_ctl_put;
  1090. kctl->get = msm_pcm_chmap_ctl_get;
  1091. ret = msm_pcm_add_volume_control(rtd);
  1092. if (ret)
  1093. pr_err("%s: Could not add pcm Volume Control %d\n",
  1094. __func__, ret);
  1095. return ret;
  1096. }
  1097. static struct snd_soc_platform_driver msm_soc_platform = {
  1098. .ops = &msm_pcm_ops,
  1099. .pcm_new = msm_asoc_pcm_new,
  1100. };
  1101. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  1102. {
  1103. int rc;
  1104. int id;
  1105. struct msm_plat_data *pdata;
  1106. const char *latency_level;
  1107. rc = of_property_read_u32(pdev->dev.of_node,
  1108. "qti,msm-pcm-dsp-id", &id);
  1109. if (rc) {
  1110. dev_err(&pdev->dev, "%s: qti,msm-pcm-dsp-id missing in DT node\n",
  1111. __func__);
  1112. return rc;
  1113. }
  1114. pdata = kzalloc(sizeof(struct msm_plat_data), GFP_KERNEL);
  1115. if (!pdata) {
  1116. dev_err(&pdev->dev, "Failed to allocate memory for platform data\n");
  1117. return -ENOMEM;
  1118. }
  1119. if (of_property_read_bool(pdev->dev.of_node,
  1120. "qti,msm-pcm-low-latency")) {
  1121. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  1122. rc = of_property_read_string(pdev->dev.of_node,
  1123. "qti,latency-level", &latency_level);
  1124. if (!rc) {
  1125. if (!strcmp(latency_level, "ultra"))
  1126. pdata->perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  1127. }
  1128. } else
  1129. pdata->perf_mode = LEGACY_PCM_MODE;
  1130. mutex_init(&pdata->lock);
  1131. dev_set_drvdata(&pdev->dev, pdata);
  1132. dev_set_name(&pdev->dev, "%s.%d", "msm-pcm-dsp", id);
  1133. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1134. __func__, dev_name(&pdev->dev));
  1135. return snd_soc_register_platform(&pdev->dev,
  1136. &msm_soc_platform);
  1137. }
  1138. static int msm_pcm_remove(struct platform_device *pdev)
  1139. {
  1140. struct msm_plat_data *pdata;
  1141. pdata = dev_get_drvdata(&pdev->dev);
  1142. mutex_destroy(&pdata->lock);
  1143. kfree(pdata);
  1144. snd_soc_unregister_platform(&pdev->dev);
  1145. return 0;
  1146. }
  1147. static const struct of_device_id msm_pcm_dt_match[] = {
  1148. {.compatible = "qti,msm-pcm-dsp"},
  1149. {}
  1150. };
  1151. MODULE_DEVICE_TABLE(of, msm_pcm_dt_match);
  1152. static struct platform_driver msm_pcm_driver = {
  1153. .driver = {
  1154. .name = "msm-pcm-dsp",
  1155. .owner = THIS_MODULE,
  1156. .of_match_table = msm_pcm_dt_match,
  1157. },
  1158. .probe = msm_pcm_probe,
  1159. .remove = __devexit_p(msm_pcm_remove),
  1160. };
  1161. static int __init msm_soc_platform_init(void)
  1162. {
  1163. init_waitqueue_head(&the_locks.enable_wait);
  1164. init_waitqueue_head(&the_locks.eos_wait);
  1165. init_waitqueue_head(&the_locks.write_wait);
  1166. init_waitqueue_head(&the_locks.read_wait);
  1167. return platform_driver_register(&msm_pcm_driver);
  1168. }
  1169. module_init(msm_soc_platform_init);
  1170. static void __exit msm_soc_platform_exit(void)
  1171. {
  1172. platform_driver_unregister(&msm_pcm_driver);
  1173. }
  1174. module_exit(msm_soc_platform_exit);
  1175. MODULE_DESCRIPTION("PCM module platform driver");
  1176. MODULE_LICENSE("GPL v2");