sst-haswell-pcm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * Intel SST Haswell/Broadwell PCM Support
  3. *
  4. * Copyright (C) 2013, Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/dmaengine_pcm.h>
  27. #include <sound/soc.h>
  28. #include <sound/tlv.h>
  29. #include <sound/compress_driver.h>
  30. #include "sst-haswell-ipc.h"
  31. #include "sst-dsp-priv.h"
  32. #include "sst-dsp.h"
  33. #define HSW_PCM_COUNT 6
  34. #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
  35. /* simple volume table */
  36. static const u32 volume_map[] = {
  37. HSW_VOLUME_MAX >> 30,
  38. HSW_VOLUME_MAX >> 29,
  39. HSW_VOLUME_MAX >> 28,
  40. HSW_VOLUME_MAX >> 27,
  41. HSW_VOLUME_MAX >> 26,
  42. HSW_VOLUME_MAX >> 25,
  43. HSW_VOLUME_MAX >> 24,
  44. HSW_VOLUME_MAX >> 23,
  45. HSW_VOLUME_MAX >> 22,
  46. HSW_VOLUME_MAX >> 21,
  47. HSW_VOLUME_MAX >> 20,
  48. HSW_VOLUME_MAX >> 19,
  49. HSW_VOLUME_MAX >> 18,
  50. HSW_VOLUME_MAX >> 17,
  51. HSW_VOLUME_MAX >> 16,
  52. HSW_VOLUME_MAX >> 15,
  53. HSW_VOLUME_MAX >> 14,
  54. HSW_VOLUME_MAX >> 13,
  55. HSW_VOLUME_MAX >> 12,
  56. HSW_VOLUME_MAX >> 11,
  57. HSW_VOLUME_MAX >> 10,
  58. HSW_VOLUME_MAX >> 9,
  59. HSW_VOLUME_MAX >> 8,
  60. HSW_VOLUME_MAX >> 7,
  61. HSW_VOLUME_MAX >> 6,
  62. HSW_VOLUME_MAX >> 5,
  63. HSW_VOLUME_MAX >> 4,
  64. HSW_VOLUME_MAX >> 3,
  65. HSW_VOLUME_MAX >> 2,
  66. HSW_VOLUME_MAX >> 1,
  67. HSW_VOLUME_MAX >> 0,
  68. };
  69. #define HSW_PCM_PERIODS_MAX 64
  70. #define HSW_PCM_PERIODS_MIN 2
  71. static const struct snd_pcm_hardware hsw_pcm_hardware = {
  72. .info = SNDRV_PCM_INFO_MMAP |
  73. SNDRV_PCM_INFO_MMAP_VALID |
  74. SNDRV_PCM_INFO_INTERLEAVED |
  75. SNDRV_PCM_INFO_PAUSE |
  76. SNDRV_PCM_INFO_RESUME |
  77. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  78. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE |
  79. SNDRV_PCM_FMTBIT_S32_LE,
  80. .period_bytes_min = PAGE_SIZE,
  81. .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
  82. .periods_min = HSW_PCM_PERIODS_MIN,
  83. .periods_max = HSW_PCM_PERIODS_MAX,
  84. .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
  85. };
  86. /* private data for each PCM DSP stream */
  87. struct hsw_pcm_data {
  88. int dai_id;
  89. struct sst_hsw_stream *stream;
  90. u32 volume[2];
  91. struct snd_pcm_substream *substream;
  92. struct snd_compr_stream *cstream;
  93. unsigned int wpos;
  94. struct mutex mutex;
  95. };
  96. /* private data for the driver */
  97. struct hsw_priv_data {
  98. /* runtime DSP */
  99. struct sst_hsw *hsw;
  100. /* page tables */
  101. struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
  102. /* DAI data */
  103. struct hsw_pcm_data pcm[HSW_PCM_COUNT];
  104. };
  105. static inline u32 hsw_mixer_to_ipc(unsigned int value)
  106. {
  107. if (value >= ARRAY_SIZE(volume_map))
  108. return volume_map[0];
  109. else
  110. return volume_map[value];
  111. }
  112. static inline unsigned int hsw_ipc_to_mixer(u32 value)
  113. {
  114. int i;
  115. for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
  116. if (volume_map[i] >= value)
  117. return i;
  118. }
  119. return i - 1;
  120. }
  121. static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
  122. struct snd_ctl_elem_value *ucontrol)
  123. {
  124. struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
  125. struct soc_mixer_control *mc =
  126. (struct soc_mixer_control *)kcontrol->private_value;
  127. struct hsw_priv_data *pdata =
  128. snd_soc_platform_get_drvdata(platform);
  129. struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
  130. struct sst_hsw *hsw = pdata->hsw;
  131. u32 volume;
  132. mutex_lock(&pcm_data->mutex);
  133. if (!pcm_data->stream) {
  134. pcm_data->volume[0] =
  135. hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  136. pcm_data->volume[1] =
  137. hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  138. mutex_unlock(&pcm_data->mutex);
  139. return 0;
  140. }
  141. if (ucontrol->value.integer.value[0] ==
  142. ucontrol->value.integer.value[1]) {
  143. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  144. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 2, volume);
  145. } else {
  146. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  147. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
  148. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  149. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
  150. }
  151. mutex_unlock(&pcm_data->mutex);
  152. return 0;
  153. }
  154. static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
  158. struct soc_mixer_control *mc =
  159. (struct soc_mixer_control *)kcontrol->private_value;
  160. struct hsw_priv_data *pdata =
  161. snd_soc_platform_get_drvdata(platform);
  162. struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
  163. struct sst_hsw *hsw = pdata->hsw;
  164. u32 volume;
  165. mutex_lock(&pcm_data->mutex);
  166. if (!pcm_data->stream) {
  167. ucontrol->value.integer.value[0] =
  168. hsw_ipc_to_mixer(pcm_data->volume[0]);
  169. ucontrol->value.integer.value[1] =
  170. hsw_ipc_to_mixer(pcm_data->volume[1]);
  171. mutex_unlock(&pcm_data->mutex);
  172. return 0;
  173. }
  174. sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
  175. ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
  176. sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
  177. ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
  178. mutex_unlock(&pcm_data->mutex);
  179. return 0;
  180. }
  181. static int hsw_volume_put(struct snd_kcontrol *kcontrol,
  182. struct snd_ctl_elem_value *ucontrol)
  183. {
  184. struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
  185. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  186. struct sst_hsw *hsw = pdata->hsw;
  187. u32 volume;
  188. if (ucontrol->value.integer.value[0] ==
  189. ucontrol->value.integer.value[1]) {
  190. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  191. sst_hsw_mixer_set_volume(hsw, 0, 2, volume);
  192. } else {
  193. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  194. sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
  195. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  196. sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
  197. }
  198. return 0;
  199. }
  200. static int hsw_volume_get(struct snd_kcontrol *kcontrol,
  201. struct snd_ctl_elem_value *ucontrol)
  202. {
  203. struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
  204. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  205. struct sst_hsw *hsw = pdata->hsw;
  206. unsigned int volume = 0;
  207. sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
  208. ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
  209. sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
  210. ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
  211. return 0;
  212. }
  213. /* TLV used by both global and stream volumes */
  214. static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
  215. /* System Pin has no volume control */
  216. static const struct snd_kcontrol_new hsw_volume_controls[] = {
  217. /* Global DSP volume */
  218. SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
  219. ARRAY_SIZE(volume_map) -1, 0,
  220. hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
  221. /* Offload 0 volume */
  222. SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
  223. ARRAY_SIZE(volume_map), 0,
  224. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  225. /* Offload 1 volume */
  226. SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
  227. ARRAY_SIZE(volume_map), 0,
  228. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  229. /* Loopback volume */
  230. SOC_DOUBLE_EXT_TLV("Loopback Capture Volume", 3, 0, 8,
  231. ARRAY_SIZE(volume_map), 0,
  232. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  233. /* Mic Capture volume */
  234. SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
  235. ARRAY_SIZE(volume_map), 0,
  236. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  237. };
  238. /* Create DMA buffer page table for DSP */
  239. static int create_adsp_page_table(struct snd_pcm_substream *substream,
  240. struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
  241. unsigned char *dma_area, size_t size, int pcm)
  242. {
  243. struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
  244. int i, pages, stream = substream->stream;
  245. pages = snd_sgbuf_aligned_pages(size);
  246. dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
  247. dma_area, size, pages);
  248. for (i = 0; i < pages; i++) {
  249. u32 idx = (((i << 2) + i)) >> 1;
  250. u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
  251. u32 *pg_table;
  252. dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
  253. pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
  254. if (i & 1)
  255. *pg_table |= (pfn << 4);
  256. else
  257. *pg_table |= pfn;
  258. }
  259. return 0;
  260. }
  261. /* this may get called several times by oss emulation */
  262. static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
  263. struct snd_pcm_hw_params *params)
  264. {
  265. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  266. struct snd_pcm_runtime *runtime = substream->runtime;
  267. struct hsw_priv_data *pdata =
  268. snd_soc_platform_get_drvdata(rtd->platform);
  269. struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
  270. struct sst_hsw *hsw = pdata->hsw;
  271. struct sst_module *module_data;
  272. struct sst_dsp *dsp;
  273. struct snd_dma_buffer *dmab;
  274. enum sst_hsw_stream_type stream_type;
  275. enum sst_hsw_stream_path_id path_id;
  276. u32 rate, bits, map, pages, module_id;
  277. u8 channels;
  278. int ret;
  279. /* stream direction */
  280. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  281. path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
  282. else
  283. path_id = SST_HSW_STREAM_PATH_SSP0_IN;
  284. /* DSP stream type depends on DAI ID */
  285. switch (rtd->cpu_dai->id) {
  286. case 0:
  287. stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
  288. module_id = SST_HSW_MODULE_PCM_SYSTEM;
  289. break;
  290. case 1:
  291. case 2:
  292. stream_type = SST_HSW_STREAM_TYPE_RENDER;
  293. module_id = SST_HSW_MODULE_PCM;
  294. break;
  295. case 3:
  296. /* path ID needs to be OUT for loopback */
  297. stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
  298. path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
  299. module_id = SST_HSW_MODULE_PCM_REFERENCE;
  300. break;
  301. case 4:
  302. stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
  303. module_id = SST_HSW_MODULE_PCM_CAPTURE;
  304. break;
  305. default:
  306. dev_err(rtd->dev, "error: invalid DAI ID %d\n",
  307. rtd->cpu_dai->id);
  308. return -EINVAL;
  309. };
  310. ret = sst_hsw_stream_format(hsw, pcm_data->stream,
  311. path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
  312. if (ret < 0) {
  313. dev_err(rtd->dev, "error: failed to set format %d\n", ret);
  314. return ret;
  315. }
  316. rate = params_rate(params);
  317. ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
  318. if (ret < 0) {
  319. dev_err(rtd->dev, "error: could not set rate %d\n", rate);
  320. return ret;
  321. }
  322. switch (params_format(params)) {
  323. case SNDRV_PCM_FORMAT_S16_LE:
  324. bits = SST_HSW_DEPTH_16BIT;
  325. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
  326. break;
  327. case SNDRV_PCM_FORMAT_S24_LE:
  328. bits = SST_HSW_DEPTH_24BIT;
  329. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
  330. break;
  331. default:
  332. dev_err(rtd->dev, "error: invalid format %d\n",
  333. params_format(params));
  334. return -EINVAL;
  335. }
  336. ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
  337. if (ret < 0) {
  338. dev_err(rtd->dev, "error: could not set bits %d\n", bits);
  339. return ret;
  340. }
  341. /* we only support stereo atm */
  342. channels = params_channels(params);
  343. if (channels != 2) {
  344. dev_err(rtd->dev, "error: invalid channels %d\n", channels);
  345. return -EINVAL;
  346. }
  347. map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
  348. sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
  349. map, SST_HSW_CHANNEL_CONFIG_STEREO);
  350. ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
  351. if (ret < 0) {
  352. dev_err(rtd->dev, "error: could not set channels %d\n",
  353. channels);
  354. return ret;
  355. }
  356. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  357. if (ret < 0) {
  358. dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
  359. params_buffer_bytes(params), ret);
  360. return ret;
  361. }
  362. dmab = snd_pcm_get_dma_buf(substream);
  363. ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
  364. runtime->dma_bytes, rtd->cpu_dai->id);
  365. if (ret < 0)
  366. return ret;
  367. sst_hsw_stream_set_style(hsw, pcm_data->stream,
  368. SST_HSW_INTERLEAVING_PER_CHANNEL);
  369. if (runtime->dma_bytes % PAGE_SIZE)
  370. pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
  371. else
  372. pages = runtime->dma_bytes / PAGE_SIZE;
  373. ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
  374. pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
  375. pages, runtime->dma_bytes, 0,
  376. snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
  377. if (ret < 0) {
  378. dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
  379. return ret;
  380. }
  381. dsp = sst_hsw_get_dsp(hsw);
  382. module_data = sst_module_get_from_id(dsp, module_id);
  383. if (module_data == NULL) {
  384. dev_err(rtd->dev, "error: failed to get module config\n");
  385. return -EINVAL;
  386. }
  387. /* we use hardcoded memory offsets atm, will be updated for new FW */
  388. if (stream_type == SST_HSW_STREAM_TYPE_CAPTURE) {
  389. sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
  390. SST_HSW_MODULE_PCM_CAPTURE, module_data->entry);
  391. sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
  392. 0x449400, 0x4000);
  393. sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
  394. 0x400000, 0);
  395. } else { /* stream_type == SST_HSW_STREAM_TYPE_SYSTEM */
  396. sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
  397. SST_HSW_MODULE_PCM_SYSTEM, module_data->entry);
  398. sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
  399. module_data->offset, module_data->size);
  400. sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
  401. 0x44d400, 0x3800);
  402. sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
  403. module_data->offset, module_data->size);
  404. sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
  405. 0x400000, 0);
  406. }
  407. ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
  408. if (ret < 0) {
  409. dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
  410. return ret;
  411. }
  412. ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
  413. if (ret < 0)
  414. dev_err(rtd->dev, "error: failed to pause %d\n", ret);
  415. return 0;
  416. }
  417. static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
  418. {
  419. snd_pcm_lib_free_pages(substream);
  420. return 0;
  421. }
  422. static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  423. {
  424. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  425. struct hsw_priv_data *pdata =
  426. snd_soc_platform_get_drvdata(rtd->platform);
  427. struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
  428. struct sst_hsw *hsw = pdata->hsw;
  429. switch (cmd) {
  430. case SNDRV_PCM_TRIGGER_START:
  431. case SNDRV_PCM_TRIGGER_RESUME:
  432. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  433. sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
  434. break;
  435. case SNDRV_PCM_TRIGGER_STOP:
  436. case SNDRV_PCM_TRIGGER_SUSPEND:
  437. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  438. sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
  439. break;
  440. default:
  441. break;
  442. }
  443. return 0;
  444. }
  445. static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
  446. {
  447. struct hsw_pcm_data *pcm_data = data;
  448. struct snd_pcm_substream *substream = pcm_data->substream;
  449. struct snd_pcm_runtime *runtime = substream->runtime;
  450. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  451. u32 pos;
  452. pos = frames_to_bytes(runtime,
  453. (runtime->control->appl_ptr % runtime->buffer_size));
  454. dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
  455. /* let alsa know we have play a period */
  456. snd_pcm_period_elapsed(substream);
  457. return pos;
  458. }
  459. static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
  460. {
  461. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  462. struct snd_pcm_runtime *runtime = substream->runtime;
  463. struct hsw_priv_data *pdata =
  464. snd_soc_platform_get_drvdata(rtd->platform);
  465. struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
  466. struct sst_hsw *hsw = pdata->hsw;
  467. snd_pcm_uframes_t offset;
  468. offset = bytes_to_frames(runtime,
  469. sst_hsw_get_dsp_position(hsw, pcm_data->stream));
  470. dev_dbg(rtd->dev, "PCM: DMA pointer %zu bytes\n",
  471. frames_to_bytes(runtime, (u32)offset));
  472. return offset;
  473. }
  474. static int hsw_pcm_open(struct snd_pcm_substream *substream)
  475. {
  476. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  477. struct hsw_priv_data *pdata =
  478. snd_soc_platform_get_drvdata(rtd->platform);
  479. struct hsw_pcm_data *pcm_data;
  480. struct sst_hsw *hsw = pdata->hsw;
  481. pcm_data = &pdata->pcm[rtd->cpu_dai->id];
  482. mutex_lock(&pcm_data->mutex);
  483. snd_soc_pcm_set_drvdata(rtd, pcm_data);
  484. pcm_data->substream = substream;
  485. snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
  486. pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
  487. hsw_notify_pointer, pcm_data);
  488. if (pcm_data->stream == NULL) {
  489. dev_err(rtd->dev, "error: failed to create stream\n");
  490. mutex_unlock(&pcm_data->mutex);
  491. return -EINVAL;
  492. }
  493. /* Set previous saved volume */
  494. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
  495. 0, pcm_data->volume[0]);
  496. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
  497. 1, pcm_data->volume[1]);
  498. mutex_unlock(&pcm_data->mutex);
  499. return 0;
  500. }
  501. static int hsw_pcm_close(struct snd_pcm_substream *substream)
  502. {
  503. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  504. struct hsw_priv_data *pdata =
  505. snd_soc_platform_get_drvdata(rtd->platform);
  506. struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
  507. struct sst_hsw *hsw = pdata->hsw;
  508. int ret;
  509. mutex_lock(&pcm_data->mutex);
  510. ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
  511. if (ret < 0) {
  512. dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
  513. goto out;
  514. }
  515. ret = sst_hsw_stream_free(hsw, pcm_data->stream);
  516. if (ret < 0) {
  517. dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
  518. goto out;
  519. }
  520. pcm_data->stream = NULL;
  521. out:
  522. mutex_unlock(&pcm_data->mutex);
  523. return ret;
  524. }
  525. static struct snd_pcm_ops hsw_pcm_ops = {
  526. .open = hsw_pcm_open,
  527. .close = hsw_pcm_close,
  528. .ioctl = snd_pcm_lib_ioctl,
  529. .hw_params = hsw_pcm_hw_params,
  530. .hw_free = hsw_pcm_hw_free,
  531. .trigger = hsw_pcm_trigger,
  532. .pointer = hsw_pcm_pointer,
  533. .page = snd_pcm_sgbuf_ops_page,
  534. };
  535. static void hsw_pcm_free(struct snd_pcm *pcm)
  536. {
  537. snd_pcm_lib_preallocate_free_for_all(pcm);
  538. }
  539. static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
  540. {
  541. struct snd_pcm *pcm = rtd->pcm;
  542. struct snd_soc_platform *platform = rtd->platform;
  543. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  544. struct device *dev = pdata->dma_dev;
  545. int ret = 0;
  546. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
  547. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  548. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  549. SNDRV_DMA_TYPE_DEV_SG,
  550. dev,
  551. hsw_pcm_hardware.buffer_bytes_max,
  552. hsw_pcm_hardware.buffer_bytes_max);
  553. if (ret) {
  554. dev_err(rtd->dev, "dma buffer allocation failed %d\n",
  555. ret);
  556. return ret;
  557. }
  558. }
  559. return ret;
  560. }
  561. #define HSW_FORMATS \
  562. (SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE |\
  563. SNDRV_PCM_FMTBIT_S32_LE)
  564. static struct snd_soc_dai_driver hsw_dais[] = {
  565. {
  566. .name = "System Pin",
  567. .playback = {
  568. .stream_name = "System Playback",
  569. .channels_min = 2,
  570. .channels_max = 2,
  571. .rates = SNDRV_PCM_RATE_48000,
  572. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  573. },
  574. },
  575. {
  576. /* PCM */
  577. .name = "Offload0 Pin",
  578. .playback = {
  579. .stream_name = "Offload0 Playback",
  580. .channels_min = 2,
  581. .channels_max = 2,
  582. .rates = SNDRV_PCM_RATE_8000_192000,
  583. .formats = HSW_FORMATS,
  584. },
  585. },
  586. {
  587. /* PCM */
  588. .name = "Offload1 Pin",
  589. .playback = {
  590. .stream_name = "Offload1 Playback",
  591. .channels_min = 2,
  592. .channels_max = 2,
  593. .rates = SNDRV_PCM_RATE_8000_192000,
  594. .formats = HSW_FORMATS,
  595. },
  596. },
  597. {
  598. .name = "Loopback Pin",
  599. .capture = {
  600. .stream_name = "Loopback Capture",
  601. .channels_min = 2,
  602. .channels_max = 2,
  603. .rates = SNDRV_PCM_RATE_8000_192000,
  604. .formats = HSW_FORMATS,
  605. },
  606. },
  607. {
  608. .name = "Capture Pin",
  609. .capture = {
  610. .stream_name = "Analog Capture",
  611. .channels_min = 2,
  612. .channels_max = 2,
  613. .rates = SNDRV_PCM_RATE_8000_192000,
  614. .formats = HSW_FORMATS,
  615. },
  616. },
  617. };
  618. static const struct snd_soc_dapm_widget widgets[] = {
  619. /* Backend DAIs */
  620. SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  621. SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  622. SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  623. SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  624. /* Global Playback Mixer */
  625. SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  626. };
  627. static const struct snd_soc_dapm_route graph[] = {
  628. /* Playback Mixer */
  629. {"Playback VMixer", NULL, "System Playback"},
  630. {"Playback VMixer", NULL, "Offload0 Playback"},
  631. {"Playback VMixer", NULL, "Offload1 Playback"},
  632. {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
  633. {"Analog Capture", NULL, "SSP0 CODEC IN"},
  634. };
  635. static int hsw_pcm_probe(struct snd_soc_platform *platform)
  636. {
  637. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  638. struct hsw_priv_data *priv_data;
  639. struct device *dma_dev = pdata->dma_dev;
  640. int i, ret = 0;
  641. if (!pdata)
  642. return -ENODEV;
  643. priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data), GFP_KERNEL);
  644. priv_data->hsw = pdata->dsp;
  645. snd_soc_platform_set_drvdata(platform, priv_data);
  646. /* allocate DSP buffer page tables */
  647. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  648. mutex_init(&priv_data->pcm[i].mutex);
  649. /* playback */
  650. if (hsw_dais[i].playback.channels_min) {
  651. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  652. PAGE_SIZE, &priv_data->dmab[i][0]);
  653. if (ret < 0)
  654. goto err;
  655. }
  656. /* capture */
  657. if (hsw_dais[i].capture.channels_min) {
  658. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  659. PAGE_SIZE, &priv_data->dmab[i][1]);
  660. if (ret < 0)
  661. goto err;
  662. }
  663. }
  664. return 0;
  665. err:
  666. for (;i >= 0; i--) {
  667. if (hsw_dais[i].playback.channels_min)
  668. snd_dma_free_pages(&priv_data->dmab[i][0]);
  669. if (hsw_dais[i].capture.channels_min)
  670. snd_dma_free_pages(&priv_data->dmab[i][1]);
  671. }
  672. return ret;
  673. }
  674. static int hsw_pcm_remove(struct snd_soc_platform *platform)
  675. {
  676. struct hsw_priv_data *priv_data =
  677. snd_soc_platform_get_drvdata(platform);
  678. int i;
  679. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  680. if (hsw_dais[i].playback.channels_min)
  681. snd_dma_free_pages(&priv_data->dmab[i][0]);
  682. if (hsw_dais[i].capture.channels_min)
  683. snd_dma_free_pages(&priv_data->dmab[i][1]);
  684. }
  685. return 0;
  686. }
  687. static struct snd_soc_platform_driver hsw_soc_platform = {
  688. .probe = hsw_pcm_probe,
  689. .remove = hsw_pcm_remove,
  690. .ops = &hsw_pcm_ops,
  691. .pcm_new = hsw_pcm_new,
  692. .pcm_free = hsw_pcm_free,
  693. .controls = hsw_volume_controls,
  694. .num_controls = ARRAY_SIZE(hsw_volume_controls),
  695. .dapm_widgets = widgets,
  696. .num_dapm_widgets = ARRAY_SIZE(widgets),
  697. .dapm_routes = graph,
  698. .num_dapm_routes = ARRAY_SIZE(graph),
  699. };
  700. static const struct snd_soc_component_driver hsw_dai_component = {
  701. .name = "haswell-dai",
  702. };
  703. static int hsw_pcm_dev_probe(struct platform_device *pdev)
  704. {
  705. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  706. int ret;
  707. ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
  708. if (ret < 0)
  709. return -ENODEV;
  710. ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
  711. if (ret < 0)
  712. goto err_plat;
  713. ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
  714. hsw_dais, ARRAY_SIZE(hsw_dais));
  715. if (ret < 0)
  716. goto err_comp;
  717. return 0;
  718. err_comp:
  719. snd_soc_unregister_platform(&pdev->dev);
  720. err_plat:
  721. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  722. return 0;
  723. }
  724. static int hsw_pcm_dev_remove(struct platform_device *pdev)
  725. {
  726. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  727. snd_soc_unregister_platform(&pdev->dev);
  728. snd_soc_unregister_component(&pdev->dev);
  729. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  730. return 0;
  731. }
  732. static struct platform_driver hsw_pcm_driver = {
  733. .driver = {
  734. .name = "haswell-pcm-audio",
  735. .owner = THIS_MODULE,
  736. },
  737. .probe = hsw_pcm_dev_probe,
  738. .remove = hsw_pcm_dev_remove,
  739. };
  740. module_platform_driver(hsw_pcm_driver);
  741. MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
  742. MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
  743. MODULE_LICENSE("GPL v2");
  744. MODULE_ALIAS("platform:haswell-pcm-audio");