msm-pcm-q6-v2.c 34 KB

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