sst-haswell-pcm.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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/delay.h>
  20. #include <linux/pm_runtime.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 "../haswell/sst-haswell-ipc.h"
  31. #include "../common/sst-dsp-priv.h"
  32. #include "../common/sst-dsp.h"
  33. #define HSW_PCM_COUNT 6
  34. #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
  35. #define SST_OLD_POSITION(d, r, o) ((d) + \
  36. frames_to_bytes(r, o))
  37. #define SST_SAMPLES(r, x) (bytes_to_samples(r, \
  38. frames_to_bytes(r, (x))))
  39. /* simple volume table */
  40. static const u32 volume_map[] = {
  41. HSW_VOLUME_MAX >> 30,
  42. HSW_VOLUME_MAX >> 29,
  43. HSW_VOLUME_MAX >> 28,
  44. HSW_VOLUME_MAX >> 27,
  45. HSW_VOLUME_MAX >> 26,
  46. HSW_VOLUME_MAX >> 25,
  47. HSW_VOLUME_MAX >> 24,
  48. HSW_VOLUME_MAX >> 23,
  49. HSW_VOLUME_MAX >> 22,
  50. HSW_VOLUME_MAX >> 21,
  51. HSW_VOLUME_MAX >> 20,
  52. HSW_VOLUME_MAX >> 19,
  53. HSW_VOLUME_MAX >> 18,
  54. HSW_VOLUME_MAX >> 17,
  55. HSW_VOLUME_MAX >> 16,
  56. HSW_VOLUME_MAX >> 15,
  57. HSW_VOLUME_MAX >> 14,
  58. HSW_VOLUME_MAX >> 13,
  59. HSW_VOLUME_MAX >> 12,
  60. HSW_VOLUME_MAX >> 11,
  61. HSW_VOLUME_MAX >> 10,
  62. HSW_VOLUME_MAX >> 9,
  63. HSW_VOLUME_MAX >> 8,
  64. HSW_VOLUME_MAX >> 7,
  65. HSW_VOLUME_MAX >> 6,
  66. HSW_VOLUME_MAX >> 5,
  67. HSW_VOLUME_MAX >> 4,
  68. HSW_VOLUME_MAX >> 3,
  69. HSW_VOLUME_MAX >> 2,
  70. HSW_VOLUME_MAX >> 1,
  71. HSW_VOLUME_MAX >> 0,
  72. };
  73. #define HSW_PCM_PERIODS_MAX 64
  74. #define HSW_PCM_PERIODS_MIN 2
  75. #define HSW_PCM_DAI_ID_SYSTEM 0
  76. #define HSW_PCM_DAI_ID_OFFLOAD0 1
  77. #define HSW_PCM_DAI_ID_OFFLOAD1 2
  78. #define HSW_PCM_DAI_ID_LOOPBACK 3
  79. static const struct snd_pcm_hardware hsw_pcm_hardware = {
  80. .info = SNDRV_PCM_INFO_MMAP |
  81. SNDRV_PCM_INFO_MMAP_VALID |
  82. SNDRV_PCM_INFO_INTERLEAVED |
  83. SNDRV_PCM_INFO_PAUSE |
  84. SNDRV_PCM_INFO_RESUME |
  85. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
  86. SNDRV_PCM_INFO_DRAIN_TRIGGER,
  87. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  88. SNDRV_PCM_FMTBIT_S32_LE,
  89. .period_bytes_min = PAGE_SIZE,
  90. .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
  91. .periods_min = HSW_PCM_PERIODS_MIN,
  92. .periods_max = HSW_PCM_PERIODS_MAX,
  93. .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
  94. };
  95. struct hsw_pcm_module_map {
  96. int dai_id;
  97. int stream;
  98. enum sst_hsw_module_id mod_id;
  99. };
  100. /* private data for each PCM DSP stream */
  101. struct hsw_pcm_data {
  102. int dai_id;
  103. struct sst_hsw_stream *stream;
  104. struct sst_module_runtime *runtime;
  105. struct sst_module_runtime_context context;
  106. struct snd_pcm *hsw_pcm;
  107. u32 volume[2];
  108. struct snd_pcm_substream *substream;
  109. struct snd_compr_stream *cstream;
  110. unsigned int wpos;
  111. struct mutex mutex;
  112. bool allocated;
  113. int persistent_offset;
  114. };
  115. enum hsw_pm_state {
  116. HSW_PM_STATE_D0 = 0,
  117. HSW_PM_STATE_RTD3 = 1,
  118. HSW_PM_STATE_D3 = 2,
  119. };
  120. /* private data for the driver */
  121. struct hsw_priv_data {
  122. /* runtime DSP */
  123. struct sst_hsw *hsw;
  124. struct device *dev;
  125. enum hsw_pm_state pm_state;
  126. struct snd_soc_card *soc_card;
  127. struct sst_module_runtime *runtime_waves; /* sound effect module */
  128. /* page tables */
  129. struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
  130. /* DAI data */
  131. struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
  132. };
  133. /* static mappings between PCMs and modules - may be dynamic in future */
  134. static struct hsw_pcm_module_map mod_map[] = {
  135. {HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
  136. {HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
  137. {HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
  138. {HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
  139. {HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
  140. };
  141. static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
  142. static inline u32 hsw_mixer_to_ipc(unsigned int value)
  143. {
  144. if (value >= ARRAY_SIZE(volume_map))
  145. return volume_map[0];
  146. else
  147. return volume_map[value];
  148. }
  149. static inline unsigned int hsw_ipc_to_mixer(u32 value)
  150. {
  151. int i;
  152. for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
  153. if (volume_map[i] >= value)
  154. return i;
  155. }
  156. return i - 1;
  157. }
  158. static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol)
  160. {
  161. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  162. struct soc_mixer_control *mc =
  163. (struct soc_mixer_control *)kcontrol->private_value;
  164. struct hsw_priv_data *pdata =
  165. snd_soc_platform_get_drvdata(platform);
  166. struct hsw_pcm_data *pcm_data;
  167. struct sst_hsw *hsw = pdata->hsw;
  168. u32 volume;
  169. int dai, stream;
  170. dai = mod_map[mc->reg].dai_id;
  171. stream = mod_map[mc->reg].stream;
  172. pcm_data = &pdata->pcm[dai][stream];
  173. mutex_lock(&pcm_data->mutex);
  174. pm_runtime_get_sync(pdata->dev);
  175. if (!pcm_data->stream) {
  176. pcm_data->volume[0] =
  177. hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  178. pcm_data->volume[1] =
  179. hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  180. pm_runtime_mark_last_busy(pdata->dev);
  181. pm_runtime_put_autosuspend(pdata->dev);
  182. mutex_unlock(&pcm_data->mutex);
  183. return 0;
  184. }
  185. if (ucontrol->value.integer.value[0] ==
  186. ucontrol->value.integer.value[1]) {
  187. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  188. /* apply volume value to all channels */
  189. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
  190. } else {
  191. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  192. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
  193. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  194. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
  195. }
  196. pm_runtime_mark_last_busy(pdata->dev);
  197. pm_runtime_put_autosuspend(pdata->dev);
  198. mutex_unlock(&pcm_data->mutex);
  199. return 0;
  200. }
  201. static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
  202. struct snd_ctl_elem_value *ucontrol)
  203. {
  204. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  205. struct soc_mixer_control *mc =
  206. (struct soc_mixer_control *)kcontrol->private_value;
  207. struct hsw_priv_data *pdata =
  208. snd_soc_platform_get_drvdata(platform);
  209. struct hsw_pcm_data *pcm_data;
  210. struct sst_hsw *hsw = pdata->hsw;
  211. u32 volume;
  212. int dai, stream;
  213. dai = mod_map[mc->reg].dai_id;
  214. stream = mod_map[mc->reg].stream;
  215. pcm_data = &pdata->pcm[dai][stream];
  216. mutex_lock(&pcm_data->mutex);
  217. pm_runtime_get_sync(pdata->dev);
  218. if (!pcm_data->stream) {
  219. ucontrol->value.integer.value[0] =
  220. hsw_ipc_to_mixer(pcm_data->volume[0]);
  221. ucontrol->value.integer.value[1] =
  222. hsw_ipc_to_mixer(pcm_data->volume[1]);
  223. pm_runtime_mark_last_busy(pdata->dev);
  224. pm_runtime_put_autosuspend(pdata->dev);
  225. mutex_unlock(&pcm_data->mutex);
  226. return 0;
  227. }
  228. sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
  229. ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
  230. sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
  231. ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
  232. pm_runtime_mark_last_busy(pdata->dev);
  233. pm_runtime_put_autosuspend(pdata->dev);
  234. mutex_unlock(&pcm_data->mutex);
  235. return 0;
  236. }
  237. static int hsw_volume_put(struct snd_kcontrol *kcontrol,
  238. struct snd_ctl_elem_value *ucontrol)
  239. {
  240. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  241. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  242. struct sst_hsw *hsw = pdata->hsw;
  243. u32 volume;
  244. pm_runtime_get_sync(pdata->dev);
  245. if (ucontrol->value.integer.value[0] ==
  246. ucontrol->value.integer.value[1]) {
  247. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  248. sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
  249. } else {
  250. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
  251. sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
  252. volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
  253. sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
  254. }
  255. pm_runtime_mark_last_busy(pdata->dev);
  256. pm_runtime_put_autosuspend(pdata->dev);
  257. return 0;
  258. }
  259. static int hsw_volume_get(struct snd_kcontrol *kcontrol,
  260. struct snd_ctl_elem_value *ucontrol)
  261. {
  262. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  263. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  264. struct sst_hsw *hsw = pdata->hsw;
  265. unsigned int volume = 0;
  266. pm_runtime_get_sync(pdata->dev);
  267. sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
  268. ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
  269. sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
  270. ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
  271. pm_runtime_mark_last_busy(pdata->dev);
  272. pm_runtime_put_autosuspend(pdata->dev);
  273. return 0;
  274. }
  275. static int hsw_waves_switch_get(struct snd_kcontrol *kcontrol,
  276. struct snd_ctl_elem_value *ucontrol)
  277. {
  278. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  279. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  280. struct sst_hsw *hsw = pdata->hsw;
  281. enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
  282. ucontrol->value.integer.value[0] =
  283. (sst_hsw_is_module_active(hsw, id) ||
  284. sst_hsw_is_module_enabled_rtd3(hsw, id));
  285. return 0;
  286. }
  287. static int hsw_waves_switch_put(struct snd_kcontrol *kcontrol,
  288. struct snd_ctl_elem_value *ucontrol)
  289. {
  290. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  291. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  292. struct sst_hsw *hsw = pdata->hsw;
  293. int ret = 0;
  294. enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
  295. bool switch_on = (bool)ucontrol->value.integer.value[0];
  296. /* if module is in RAM on the DSP, apply user settings to module through
  297. * ipc. If module is not in RAM on the DSP, store user setting for
  298. * track */
  299. if (sst_hsw_is_module_loaded(hsw, id)) {
  300. if (switch_on == sst_hsw_is_module_active(hsw, id))
  301. return 0;
  302. if (switch_on)
  303. ret = sst_hsw_module_enable(hsw, id, 0);
  304. else
  305. ret = sst_hsw_module_disable(hsw, id, 0);
  306. } else {
  307. if (switch_on == sst_hsw_is_module_enabled_rtd3(hsw, id))
  308. return 0;
  309. if (switch_on)
  310. sst_hsw_set_module_enabled_rtd3(hsw, id);
  311. else
  312. sst_hsw_set_module_disabled_rtd3(hsw, id);
  313. }
  314. return ret;
  315. }
  316. static int hsw_waves_param_get(struct snd_kcontrol *kcontrol,
  317. struct snd_ctl_elem_value *ucontrol)
  318. {
  319. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  320. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  321. struct sst_hsw *hsw = pdata->hsw;
  322. /* return a matching line from param buffer */
  323. return sst_hsw_load_param_line(hsw, ucontrol->value.bytes.data);
  324. }
  325. static int hsw_waves_param_put(struct snd_kcontrol *kcontrol,
  326. struct snd_ctl_elem_value *ucontrol)
  327. {
  328. struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
  329. struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
  330. struct sst_hsw *hsw = pdata->hsw;
  331. int ret;
  332. enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
  333. int param_id = ucontrol->value.bytes.data[0];
  334. int param_size = WAVES_PARAM_COUNT;
  335. /* clear param buffer and reset buffer index */
  336. if (param_id == 0xFF) {
  337. sst_hsw_reset_param_buf(hsw);
  338. return 0;
  339. }
  340. /* store params into buffer */
  341. ret = sst_hsw_store_param_line(hsw, ucontrol->value.bytes.data);
  342. if (ret < 0)
  343. return ret;
  344. if (sst_hsw_is_module_active(hsw, id))
  345. ret = sst_hsw_module_set_param(hsw, id, 0, param_id,
  346. param_size, ucontrol->value.bytes.data);
  347. return ret;
  348. }
  349. /* TLV used by both global and stream volumes */
  350. static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
  351. /* System Pin has no volume control */
  352. static const struct snd_kcontrol_new hsw_volume_controls[] = {
  353. /* Global DSP volume */
  354. SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
  355. ARRAY_SIZE(volume_map) - 1, 0,
  356. hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
  357. /* Offload 0 volume */
  358. SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
  359. ARRAY_SIZE(volume_map) - 1, 0,
  360. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  361. /* Offload 1 volume */
  362. SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
  363. ARRAY_SIZE(volume_map) - 1, 0,
  364. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  365. /* Mic Capture volume */
  366. SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
  367. ARRAY_SIZE(volume_map) - 1, 0,
  368. hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
  369. /* enable/disable module waves */
  370. SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
  371. hsw_waves_switch_get, hsw_waves_switch_put),
  372. /* set parameters to module waves */
  373. SND_SOC_BYTES_EXT("Waves Set Param", WAVES_PARAM_COUNT,
  374. hsw_waves_param_get, hsw_waves_param_put),
  375. };
  376. /* Create DMA buffer page table for DSP */
  377. static int create_adsp_page_table(struct snd_pcm_substream *substream,
  378. struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
  379. unsigned char *dma_area, size_t size, int pcm)
  380. {
  381. struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
  382. int i, pages, stream = substream->stream;
  383. pages = snd_sgbuf_aligned_pages(size);
  384. dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
  385. dma_area, size, pages);
  386. for (i = 0; i < pages; i++) {
  387. u32 idx = (((i << 2) + i)) >> 1;
  388. u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
  389. u32 *pg_table;
  390. dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
  391. pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
  392. if (i & 1)
  393. *pg_table |= (pfn << 4);
  394. else
  395. *pg_table |= pfn;
  396. }
  397. return 0;
  398. }
  399. /* this may get called several times by oss emulation */
  400. static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
  401. struct snd_pcm_hw_params *params)
  402. {
  403. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  404. struct snd_pcm_runtime *runtime = substream->runtime;
  405. struct hsw_priv_data *pdata =
  406. snd_soc_platform_get_drvdata(rtd->platform);
  407. struct hsw_pcm_data *pcm_data;
  408. struct sst_hsw *hsw = pdata->hsw;
  409. struct sst_module *module_data;
  410. struct sst_dsp *dsp;
  411. struct snd_dma_buffer *dmab;
  412. enum sst_hsw_stream_type stream_type;
  413. enum sst_hsw_stream_path_id path_id;
  414. u32 rate, bits, map, pages, module_id;
  415. u8 channels;
  416. int ret, dai;
  417. dai = mod_map[rtd->cpu_dai->id].dai_id;
  418. pcm_data = &pdata->pcm[dai][substream->stream];
  419. /* check if we are being called a subsequent time */
  420. if (pcm_data->allocated) {
  421. ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
  422. if (ret < 0)
  423. dev_dbg(rtd->dev, "error: reset stream failed %d\n",
  424. ret);
  425. ret = sst_hsw_stream_free(hsw, pcm_data->stream);
  426. if (ret < 0) {
  427. dev_dbg(rtd->dev, "error: free stream failed %d\n",
  428. ret);
  429. return ret;
  430. }
  431. pcm_data->allocated = false;
  432. pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
  433. hsw_notify_pointer, pcm_data);
  434. if (pcm_data->stream == NULL) {
  435. dev_err(rtd->dev, "error: failed to create stream\n");
  436. return -EINVAL;
  437. }
  438. }
  439. /* stream direction */
  440. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  441. path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
  442. else
  443. path_id = SST_HSW_STREAM_PATH_SSP0_IN;
  444. /* DSP stream type depends on DAI ID */
  445. switch (rtd->cpu_dai->id) {
  446. case 0:
  447. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  448. stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
  449. module_id = SST_HSW_MODULE_PCM_SYSTEM;
  450. }
  451. else {
  452. stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
  453. module_id = SST_HSW_MODULE_PCM_CAPTURE;
  454. }
  455. break;
  456. case 1:
  457. case 2:
  458. stream_type = SST_HSW_STREAM_TYPE_RENDER;
  459. module_id = SST_HSW_MODULE_PCM;
  460. break;
  461. case 3:
  462. /* path ID needs to be OUT for loopback */
  463. stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
  464. path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
  465. module_id = SST_HSW_MODULE_PCM_REFERENCE;
  466. break;
  467. default:
  468. dev_err(rtd->dev, "error: invalid DAI ID %d\n",
  469. rtd->cpu_dai->id);
  470. return -EINVAL;
  471. };
  472. ret = sst_hsw_stream_format(hsw, pcm_data->stream,
  473. path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
  474. if (ret < 0) {
  475. dev_err(rtd->dev, "error: failed to set format %d\n", ret);
  476. return ret;
  477. }
  478. rate = params_rate(params);
  479. ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
  480. if (ret < 0) {
  481. dev_err(rtd->dev, "error: could not set rate %d\n", rate);
  482. return ret;
  483. }
  484. switch (params_format(params)) {
  485. case SNDRV_PCM_FORMAT_S16_LE:
  486. bits = SST_HSW_DEPTH_16BIT;
  487. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
  488. break;
  489. case SNDRV_PCM_FORMAT_S24_LE:
  490. bits = SST_HSW_DEPTH_32BIT;
  491. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
  492. break;
  493. case SNDRV_PCM_FORMAT_S8:
  494. bits = SST_HSW_DEPTH_8BIT;
  495. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
  496. break;
  497. case SNDRV_PCM_FORMAT_S32_LE:
  498. bits = SST_HSW_DEPTH_32BIT;
  499. sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
  500. break;
  501. default:
  502. dev_err(rtd->dev, "error: invalid format %d\n",
  503. params_format(params));
  504. return -EINVAL;
  505. }
  506. ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
  507. if (ret < 0) {
  508. dev_err(rtd->dev, "error: could not set bits %d\n", bits);
  509. return ret;
  510. }
  511. channels = params_channels(params);
  512. map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
  513. sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
  514. map, SST_HSW_CHANNEL_CONFIG_STEREO);
  515. ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
  516. if (ret < 0) {
  517. dev_err(rtd->dev, "error: could not set channels %d\n",
  518. channels);
  519. return ret;
  520. }
  521. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  522. if (ret < 0) {
  523. dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
  524. params_buffer_bytes(params), ret);
  525. return ret;
  526. }
  527. dmab = snd_pcm_get_dma_buf(substream);
  528. ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
  529. runtime->dma_bytes, rtd->cpu_dai->id);
  530. if (ret < 0)
  531. return ret;
  532. sst_hsw_stream_set_style(hsw, pcm_data->stream,
  533. SST_HSW_INTERLEAVING_PER_CHANNEL);
  534. if (runtime->dma_bytes % PAGE_SIZE)
  535. pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
  536. else
  537. pages = runtime->dma_bytes / PAGE_SIZE;
  538. ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
  539. pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
  540. pages, runtime->dma_bytes, 0,
  541. snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
  542. if (ret < 0) {
  543. dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
  544. return ret;
  545. }
  546. dsp = sst_hsw_get_dsp(hsw);
  547. module_data = sst_module_get_from_id(dsp, module_id);
  548. if (module_data == NULL) {
  549. dev_err(rtd->dev, "error: failed to get module config\n");
  550. return -EINVAL;
  551. }
  552. sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
  553. pcm_data->runtime);
  554. ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
  555. if (ret < 0) {
  556. dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
  557. return ret;
  558. }
  559. if (!pcm_data->allocated) {
  560. /* Set previous saved volume */
  561. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
  562. 0, pcm_data->volume[0]);
  563. sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
  564. 1, pcm_data->volume[1]);
  565. pcm_data->allocated = true;
  566. }
  567. ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
  568. if (ret < 0)
  569. dev_err(rtd->dev, "error: failed to pause %d\n", ret);
  570. return 0;
  571. }
  572. static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
  573. {
  574. snd_pcm_lib_free_pages(substream);
  575. return 0;
  576. }
  577. static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  578. {
  579. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  580. struct hsw_priv_data *pdata =
  581. snd_soc_platform_get_drvdata(rtd->platform);
  582. struct hsw_pcm_data *pcm_data;
  583. struct sst_hsw_stream *sst_stream;
  584. struct sst_hsw *hsw = pdata->hsw;
  585. struct snd_pcm_runtime *runtime = substream->runtime;
  586. snd_pcm_uframes_t pos;
  587. int dai;
  588. dai = mod_map[rtd->cpu_dai->id].dai_id;
  589. pcm_data = &pdata->pcm[dai][substream->stream];
  590. sst_stream = pcm_data->stream;
  591. switch (cmd) {
  592. case SNDRV_PCM_TRIGGER_START:
  593. case SNDRV_PCM_TRIGGER_RESUME:
  594. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  595. sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
  596. sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
  597. break;
  598. case SNDRV_PCM_TRIGGER_STOP:
  599. case SNDRV_PCM_TRIGGER_SUSPEND:
  600. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  601. sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
  602. sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
  603. break;
  604. case SNDRV_PCM_TRIGGER_DRAIN:
  605. pos = runtime->control->appl_ptr % runtime->buffer_size;
  606. sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
  607. sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
  608. break;
  609. default:
  610. break;
  611. }
  612. return 0;
  613. }
  614. static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
  615. {
  616. struct hsw_pcm_data *pcm_data = data;
  617. struct snd_pcm_substream *substream = pcm_data->substream;
  618. struct snd_pcm_runtime *runtime = substream->runtime;
  619. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  620. struct hsw_priv_data *pdata =
  621. snd_soc_platform_get_drvdata(rtd->platform);
  622. struct sst_hsw *hsw = pdata->hsw;
  623. u32 pos;
  624. snd_pcm_uframes_t position = bytes_to_frames(runtime,
  625. sst_hsw_get_dsp_position(hsw, pcm_data->stream));
  626. unsigned char *dma_area = runtime->dma_area;
  627. snd_pcm_uframes_t dma_frames =
  628. bytes_to_frames(runtime, runtime->dma_bytes);
  629. snd_pcm_uframes_t old_position;
  630. ssize_t samples;
  631. pos = frames_to_bytes(runtime,
  632. (runtime->control->appl_ptr % runtime->buffer_size));
  633. dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
  634. /* SST fw don't know where to stop dma
  635. * So, SST driver need to clean the data which has been consumed
  636. */
  637. if (dma_area == NULL || dma_frames <= 0
  638. || (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  639. || !sst_hsw_stream_get_silence_start(hsw, stream)) {
  640. snd_pcm_period_elapsed(substream);
  641. return pos;
  642. }
  643. old_position = sst_hsw_stream_get_old_position(hsw, stream);
  644. if (position > old_position) {
  645. if (position < dma_frames) {
  646. samples = SST_SAMPLES(runtime, position - old_position);
  647. snd_pcm_format_set_silence(runtime->format,
  648. SST_OLD_POSITION(dma_area,
  649. runtime, old_position),
  650. samples);
  651. } else
  652. dev_err(rtd->dev, "PCM: position is wrong\n");
  653. } else {
  654. if (old_position < dma_frames) {
  655. samples = SST_SAMPLES(runtime,
  656. dma_frames - old_position);
  657. snd_pcm_format_set_silence(runtime->format,
  658. SST_OLD_POSITION(dma_area,
  659. runtime, old_position),
  660. samples);
  661. } else
  662. dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
  663. if (position < dma_frames) {
  664. samples = SST_SAMPLES(runtime, position);
  665. snd_pcm_format_set_silence(runtime->format,
  666. dma_area, samples);
  667. } else
  668. dev_err(rtd->dev, "PCM: position is wrong\n");
  669. }
  670. sst_hsw_stream_set_old_position(hsw, stream, position);
  671. /* let alsa know we have play a period */
  672. snd_pcm_period_elapsed(substream);
  673. return pos;
  674. }
  675. static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
  676. {
  677. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  678. struct snd_pcm_runtime *runtime = substream->runtime;
  679. struct hsw_priv_data *pdata =
  680. snd_soc_platform_get_drvdata(rtd->platform);
  681. struct hsw_pcm_data *pcm_data;
  682. struct sst_hsw *hsw = pdata->hsw;
  683. snd_pcm_uframes_t offset;
  684. uint64_t ppos;
  685. u32 position;
  686. int dai;
  687. dai = mod_map[rtd->cpu_dai->id].dai_id;
  688. pcm_data = &pdata->pcm[dai][substream->stream];
  689. position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
  690. offset = bytes_to_frames(runtime, position);
  691. ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
  692. dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
  693. position, ppos);
  694. return offset;
  695. }
  696. static int hsw_pcm_open(struct snd_pcm_substream *substream)
  697. {
  698. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  699. struct hsw_priv_data *pdata =
  700. snd_soc_platform_get_drvdata(rtd->platform);
  701. struct hsw_pcm_data *pcm_data;
  702. struct sst_hsw *hsw = pdata->hsw;
  703. int dai;
  704. dai = mod_map[rtd->cpu_dai->id].dai_id;
  705. pcm_data = &pdata->pcm[dai][substream->stream];
  706. mutex_lock(&pcm_data->mutex);
  707. pm_runtime_get_sync(pdata->dev);
  708. snd_soc_pcm_set_drvdata(rtd, pcm_data);
  709. pcm_data->substream = substream;
  710. snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
  711. pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
  712. hsw_notify_pointer, pcm_data);
  713. if (pcm_data->stream == NULL) {
  714. dev_err(rtd->dev, "error: failed to create stream\n");
  715. pm_runtime_mark_last_busy(pdata->dev);
  716. pm_runtime_put_autosuspend(pdata->dev);
  717. mutex_unlock(&pcm_data->mutex);
  718. return -EINVAL;
  719. }
  720. mutex_unlock(&pcm_data->mutex);
  721. return 0;
  722. }
  723. static int hsw_pcm_close(struct snd_pcm_substream *substream)
  724. {
  725. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  726. struct hsw_priv_data *pdata =
  727. snd_soc_platform_get_drvdata(rtd->platform);
  728. struct hsw_pcm_data *pcm_data;
  729. struct sst_hsw *hsw = pdata->hsw;
  730. int ret, dai;
  731. dai = mod_map[rtd->cpu_dai->id].dai_id;
  732. pcm_data = &pdata->pcm[dai][substream->stream];
  733. mutex_lock(&pcm_data->mutex);
  734. ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
  735. if (ret < 0) {
  736. dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
  737. goto out;
  738. }
  739. ret = sst_hsw_stream_free(hsw, pcm_data->stream);
  740. if (ret < 0) {
  741. dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
  742. goto out;
  743. }
  744. pcm_data->allocated = 0;
  745. pcm_data->stream = NULL;
  746. out:
  747. pm_runtime_mark_last_busy(pdata->dev);
  748. pm_runtime_put_autosuspend(pdata->dev);
  749. mutex_unlock(&pcm_data->mutex);
  750. return ret;
  751. }
  752. static struct snd_pcm_ops hsw_pcm_ops = {
  753. .open = hsw_pcm_open,
  754. .close = hsw_pcm_close,
  755. .ioctl = snd_pcm_lib_ioctl,
  756. .hw_params = hsw_pcm_hw_params,
  757. .hw_free = hsw_pcm_hw_free,
  758. .trigger = hsw_pcm_trigger,
  759. .pointer = hsw_pcm_pointer,
  760. .page = snd_pcm_sgbuf_ops_page,
  761. };
  762. static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
  763. {
  764. struct sst_hsw *hsw = pdata->hsw;
  765. struct hsw_pcm_data *pcm_data;
  766. int i;
  767. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  768. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  769. /* create new runtime module, use same offset if recreated */
  770. pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
  771. mod_map[i].mod_id, pcm_data->persistent_offset);
  772. if (pcm_data->runtime == NULL)
  773. goto err;
  774. pcm_data->persistent_offset =
  775. pcm_data->runtime->persistent_offset;
  776. }
  777. /* create runtime blocks for module waves */
  778. if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
  779. pdata->runtime_waves = sst_hsw_runtime_module_create(hsw,
  780. SST_HSW_MODULE_WAVES, 0);
  781. if (pdata->runtime_waves == NULL)
  782. goto err;
  783. }
  784. return 0;
  785. err:
  786. for (--i; i >= 0; i--) {
  787. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  788. sst_hsw_runtime_module_free(pcm_data->runtime);
  789. }
  790. return -ENODEV;
  791. }
  792. static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
  793. {
  794. struct sst_hsw *hsw = pdata->hsw;
  795. struct hsw_pcm_data *pcm_data;
  796. int i;
  797. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  798. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  799. if (pcm_data->runtime){
  800. sst_hsw_runtime_module_free(pcm_data->runtime);
  801. pcm_data->runtime = NULL;
  802. }
  803. }
  804. if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES) &&
  805. pdata->runtime_waves) {
  806. sst_hsw_runtime_module_free(pdata->runtime_waves);
  807. pdata->runtime_waves = NULL;
  808. }
  809. }
  810. static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
  811. {
  812. struct snd_pcm *pcm = rtd->pcm;
  813. struct snd_soc_platform *platform = rtd->platform;
  814. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  815. struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
  816. struct device *dev = pdata->dma_dev;
  817. int ret = 0;
  818. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
  819. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  820. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  821. SNDRV_DMA_TYPE_DEV_SG,
  822. dev,
  823. hsw_pcm_hardware.buffer_bytes_max,
  824. hsw_pcm_hardware.buffer_bytes_max);
  825. if (ret) {
  826. dev_err(rtd->dev, "dma buffer allocation failed %d\n",
  827. ret);
  828. return ret;
  829. }
  830. }
  831. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  832. priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
  833. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  834. priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
  835. return ret;
  836. }
  837. #define HSW_FORMATS \
  838. (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  839. static struct snd_soc_dai_driver hsw_dais[] = {
  840. {
  841. .name = "System Pin",
  842. .id = HSW_PCM_DAI_ID_SYSTEM,
  843. .playback = {
  844. .stream_name = "System Playback",
  845. .channels_min = 2,
  846. .channels_max = 2,
  847. .rates = SNDRV_PCM_RATE_48000,
  848. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  849. },
  850. .capture = {
  851. .stream_name = "Analog Capture",
  852. .channels_min = 2,
  853. .channels_max = 4,
  854. .rates = SNDRV_PCM_RATE_48000,
  855. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  856. },
  857. },
  858. {
  859. /* PCM */
  860. .name = "Offload0 Pin",
  861. .id = HSW_PCM_DAI_ID_OFFLOAD0,
  862. .playback = {
  863. .stream_name = "Offload0 Playback",
  864. .channels_min = 2,
  865. .channels_max = 2,
  866. .rates = SNDRV_PCM_RATE_8000_192000,
  867. .formats = HSW_FORMATS,
  868. },
  869. },
  870. {
  871. /* PCM */
  872. .name = "Offload1 Pin",
  873. .id = HSW_PCM_DAI_ID_OFFLOAD1,
  874. .playback = {
  875. .stream_name = "Offload1 Playback",
  876. .channels_min = 2,
  877. .channels_max = 2,
  878. .rates = SNDRV_PCM_RATE_8000_192000,
  879. .formats = HSW_FORMATS,
  880. },
  881. },
  882. {
  883. .name = "Loopback Pin",
  884. .id = HSW_PCM_DAI_ID_LOOPBACK,
  885. .capture = {
  886. .stream_name = "Loopback Capture",
  887. .channels_min = 2,
  888. .channels_max = 2,
  889. .rates = SNDRV_PCM_RATE_48000,
  890. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  891. },
  892. },
  893. };
  894. static const struct snd_soc_dapm_widget widgets[] = {
  895. /* Backend DAIs */
  896. SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  897. SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  898. SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  899. SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  900. /* Global Playback Mixer */
  901. SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  902. };
  903. static const struct snd_soc_dapm_route graph[] = {
  904. /* Playback Mixer */
  905. {"Playback VMixer", NULL, "System Playback"},
  906. {"Playback VMixer", NULL, "Offload0 Playback"},
  907. {"Playback VMixer", NULL, "Offload1 Playback"},
  908. {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
  909. {"Analog Capture", NULL, "SSP0 CODEC IN"},
  910. };
  911. static int hsw_pcm_probe(struct snd_soc_platform *platform)
  912. {
  913. struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
  914. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  915. struct device *dma_dev, *dev;
  916. int i, ret = 0;
  917. if (!pdata)
  918. return -ENODEV;
  919. dev = platform->dev;
  920. dma_dev = pdata->dma_dev;
  921. priv_data->hsw = pdata->dsp;
  922. priv_data->dev = platform->dev;
  923. priv_data->pm_state = HSW_PM_STATE_D0;
  924. priv_data->soc_card = platform->component.card;
  925. /* allocate DSP buffer page tables */
  926. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  927. /* playback */
  928. if (hsw_dais[i].playback.channels_min) {
  929. mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
  930. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  931. PAGE_SIZE, &priv_data->dmab[i][0]);
  932. if (ret < 0)
  933. goto err;
  934. }
  935. /* capture */
  936. if (hsw_dais[i].capture.channels_min) {
  937. mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
  938. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  939. PAGE_SIZE, &priv_data->dmab[i][1]);
  940. if (ret < 0)
  941. goto err;
  942. }
  943. }
  944. /* allocate runtime modules */
  945. ret = hsw_pcm_create_modules(priv_data);
  946. if (ret < 0)
  947. goto err;
  948. /* enable runtime PM with auto suspend */
  949. pm_runtime_set_autosuspend_delay(platform->dev,
  950. SST_RUNTIME_SUSPEND_DELAY);
  951. pm_runtime_use_autosuspend(platform->dev);
  952. pm_runtime_enable(platform->dev);
  953. pm_runtime_idle(platform->dev);
  954. return 0;
  955. err:
  956. for (--i; i >= 0; i--) {
  957. if (hsw_dais[i].playback.channels_min)
  958. snd_dma_free_pages(&priv_data->dmab[i][0]);
  959. if (hsw_dais[i].capture.channels_min)
  960. snd_dma_free_pages(&priv_data->dmab[i][1]);
  961. }
  962. return ret;
  963. }
  964. static int hsw_pcm_remove(struct snd_soc_platform *platform)
  965. {
  966. struct hsw_priv_data *priv_data =
  967. snd_soc_platform_get_drvdata(platform);
  968. int i;
  969. pm_runtime_disable(platform->dev);
  970. hsw_pcm_free_modules(priv_data);
  971. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  972. if (hsw_dais[i].playback.channels_min)
  973. snd_dma_free_pages(&priv_data->dmab[i][0]);
  974. if (hsw_dais[i].capture.channels_min)
  975. snd_dma_free_pages(&priv_data->dmab[i][1]);
  976. }
  977. return 0;
  978. }
  979. static struct snd_soc_platform_driver hsw_soc_platform = {
  980. .probe = hsw_pcm_probe,
  981. .remove = hsw_pcm_remove,
  982. .ops = &hsw_pcm_ops,
  983. .pcm_new = hsw_pcm_new,
  984. };
  985. static const struct snd_soc_component_driver hsw_dai_component = {
  986. .name = "haswell-dai",
  987. .controls = hsw_volume_controls,
  988. .num_controls = ARRAY_SIZE(hsw_volume_controls),
  989. .dapm_widgets = widgets,
  990. .num_dapm_widgets = ARRAY_SIZE(widgets),
  991. .dapm_routes = graph,
  992. .num_dapm_routes = ARRAY_SIZE(graph),
  993. };
  994. static int hsw_pcm_dev_probe(struct platform_device *pdev)
  995. {
  996. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  997. struct hsw_priv_data *priv_data;
  998. int ret;
  999. if (!sst_pdata)
  1000. return -EINVAL;
  1001. priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
  1002. if (!priv_data)
  1003. return -ENOMEM;
  1004. ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
  1005. if (ret < 0)
  1006. return -ENODEV;
  1007. priv_data->hsw = sst_pdata->dsp;
  1008. platform_set_drvdata(pdev, priv_data);
  1009. ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
  1010. if (ret < 0)
  1011. goto err_plat;
  1012. ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
  1013. hsw_dais, ARRAY_SIZE(hsw_dais));
  1014. if (ret < 0)
  1015. goto err_comp;
  1016. return 0;
  1017. err_comp:
  1018. snd_soc_unregister_platform(&pdev->dev);
  1019. err_plat:
  1020. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  1021. return 0;
  1022. }
  1023. static int hsw_pcm_dev_remove(struct platform_device *pdev)
  1024. {
  1025. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  1026. snd_soc_unregister_platform(&pdev->dev);
  1027. snd_soc_unregister_component(&pdev->dev);
  1028. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  1029. return 0;
  1030. }
  1031. #ifdef CONFIG_PM
  1032. static int hsw_pcm_runtime_idle(struct device *dev)
  1033. {
  1034. return 0;
  1035. }
  1036. static int hsw_pcm_suspend(struct device *dev)
  1037. {
  1038. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1039. struct sst_hsw *hsw = pdata->hsw;
  1040. /* enter D3 state and stall */
  1041. sst_hsw_dsp_runtime_suspend(hsw);
  1042. /* free all runtime modules */
  1043. hsw_pcm_free_modules(pdata);
  1044. /* put the DSP to sleep, fw unloaded after runtime modules freed */
  1045. sst_hsw_dsp_runtime_sleep(hsw);
  1046. return 0;
  1047. }
  1048. static int hsw_pcm_runtime_suspend(struct device *dev)
  1049. {
  1050. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1051. struct sst_hsw *hsw = pdata->hsw;
  1052. int ret;
  1053. if (pdata->pm_state >= HSW_PM_STATE_RTD3)
  1054. return 0;
  1055. /* fw modules will be unloaded on RTD3, set flag to track */
  1056. if (sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
  1057. ret = sst_hsw_module_disable(hsw, SST_HSW_MODULE_WAVES, 0);
  1058. if (ret < 0)
  1059. return ret;
  1060. sst_hsw_set_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
  1061. }
  1062. hsw_pcm_suspend(dev);
  1063. pdata->pm_state = HSW_PM_STATE_RTD3;
  1064. return 0;
  1065. }
  1066. static int hsw_pcm_runtime_resume(struct device *dev)
  1067. {
  1068. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1069. struct sst_hsw *hsw = pdata->hsw;
  1070. int ret;
  1071. if (pdata->pm_state != HSW_PM_STATE_RTD3)
  1072. return 0;
  1073. ret = sst_hsw_dsp_load(hsw);
  1074. if (ret < 0) {
  1075. dev_err(dev, "failed to reload %d\n", ret);
  1076. return ret;
  1077. }
  1078. ret = hsw_pcm_create_modules(pdata);
  1079. if (ret < 0) {
  1080. dev_err(dev, "failed to create modules %d\n", ret);
  1081. return ret;
  1082. }
  1083. ret = sst_hsw_dsp_runtime_resume(hsw);
  1084. if (ret < 0)
  1085. return ret;
  1086. else if (ret == 1) /* no action required */
  1087. return 0;
  1088. /* check flag when resume */
  1089. if (sst_hsw_is_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES)) {
  1090. ret = sst_hsw_module_enable(hsw, SST_HSW_MODULE_WAVES, 0);
  1091. if (ret < 0)
  1092. return ret;
  1093. /* put parameters from buffer to dsp */
  1094. ret = sst_hsw_launch_param_buf(hsw);
  1095. if (ret < 0)
  1096. return ret;
  1097. /* unset flag */
  1098. sst_hsw_set_module_disabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
  1099. }
  1100. pdata->pm_state = HSW_PM_STATE_D0;
  1101. return ret;
  1102. }
  1103. #else
  1104. #define hsw_pcm_runtime_idle NULL
  1105. #define hsw_pcm_runtime_suspend NULL
  1106. #define hsw_pcm_runtime_resume NULL
  1107. #endif
  1108. #ifdef CONFIG_PM
  1109. static void hsw_pcm_complete(struct device *dev)
  1110. {
  1111. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1112. struct sst_hsw *hsw = pdata->hsw;
  1113. struct hsw_pcm_data *pcm_data;
  1114. int i, err;
  1115. if (pdata->pm_state != HSW_PM_STATE_D3)
  1116. return;
  1117. err = sst_hsw_dsp_load(hsw);
  1118. if (err < 0) {
  1119. dev_err(dev, "failed to reload %d\n", err);
  1120. return;
  1121. }
  1122. err = hsw_pcm_create_modules(pdata);
  1123. if (err < 0) {
  1124. dev_err(dev, "failed to create modules %d\n", err);
  1125. return;
  1126. }
  1127. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1128. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1129. if (!pcm_data->substream)
  1130. continue;
  1131. err = sst_module_runtime_restore(pcm_data->runtime,
  1132. &pcm_data->context);
  1133. if (err < 0)
  1134. dev_err(dev, "failed to restore context for PCM %d\n", i);
  1135. }
  1136. snd_soc_resume(pdata->soc_card->dev);
  1137. err = sst_hsw_dsp_runtime_resume(hsw);
  1138. if (err < 0)
  1139. return;
  1140. else if (err == 1) /* no action required */
  1141. return;
  1142. pdata->pm_state = HSW_PM_STATE_D0;
  1143. return;
  1144. }
  1145. static int hsw_pcm_prepare(struct device *dev)
  1146. {
  1147. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1148. struct hsw_pcm_data *pcm_data;
  1149. int i, err;
  1150. if (pdata->pm_state == HSW_PM_STATE_D3)
  1151. return 0;
  1152. else if (pdata->pm_state == HSW_PM_STATE_D0) {
  1153. /* suspend all active streams */
  1154. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1155. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1156. if (!pcm_data->substream)
  1157. continue;
  1158. dev_dbg(dev, "suspending pcm %d\n", i);
  1159. snd_pcm_suspend_all(pcm_data->hsw_pcm);
  1160. /* We need to wait until the DSP FW stops the streams */
  1161. msleep(2);
  1162. }
  1163. /* preserve persistent memory */
  1164. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1165. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1166. if (!pcm_data->substream)
  1167. continue;
  1168. dev_dbg(dev, "saving context pcm %d\n", i);
  1169. err = sst_module_runtime_save(pcm_data->runtime,
  1170. &pcm_data->context);
  1171. if (err < 0)
  1172. dev_err(dev, "failed to save context for PCM %d\n", i);
  1173. }
  1174. hsw_pcm_suspend(dev);
  1175. }
  1176. snd_soc_suspend(pdata->soc_card->dev);
  1177. snd_soc_poweroff(pdata->soc_card->dev);
  1178. pdata->pm_state = HSW_PM_STATE_D3;
  1179. return 0;
  1180. }
  1181. #else
  1182. #define hsw_pcm_prepare NULL
  1183. #define hsw_pcm_complete NULL
  1184. #endif
  1185. static const struct dev_pm_ops hsw_pcm_pm = {
  1186. .runtime_idle = hsw_pcm_runtime_idle,
  1187. .runtime_suspend = hsw_pcm_runtime_suspend,
  1188. .runtime_resume = hsw_pcm_runtime_resume,
  1189. .prepare = hsw_pcm_prepare,
  1190. .complete = hsw_pcm_complete,
  1191. };
  1192. static struct platform_driver hsw_pcm_driver = {
  1193. .driver = {
  1194. .name = "haswell-pcm-audio",
  1195. .pm = &hsw_pcm_pm,
  1196. },
  1197. .probe = hsw_pcm_dev_probe,
  1198. .remove = hsw_pcm_dev_remove,
  1199. };
  1200. module_platform_driver(hsw_pcm_driver);
  1201. MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
  1202. MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
  1203. MODULE_LICENSE("GPL v2");
  1204. MODULE_ALIAS("platform:haswell-pcm-audio");