sst-haswell-pcm.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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%zx 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. pcm_data->substream = substream;
  709. snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
  710. pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
  711. hsw_notify_pointer, pcm_data);
  712. if (pcm_data->stream == NULL) {
  713. dev_err(rtd->dev, "error: failed to create stream\n");
  714. pm_runtime_mark_last_busy(pdata->dev);
  715. pm_runtime_put_autosuspend(pdata->dev);
  716. mutex_unlock(&pcm_data->mutex);
  717. return -EINVAL;
  718. }
  719. mutex_unlock(&pcm_data->mutex);
  720. return 0;
  721. }
  722. static int hsw_pcm_close(struct snd_pcm_substream *substream)
  723. {
  724. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  725. struct hsw_priv_data *pdata =
  726. snd_soc_platform_get_drvdata(rtd->platform);
  727. struct hsw_pcm_data *pcm_data;
  728. struct sst_hsw *hsw = pdata->hsw;
  729. int ret, dai;
  730. dai = mod_map[rtd->cpu_dai->id].dai_id;
  731. pcm_data = &pdata->pcm[dai][substream->stream];
  732. mutex_lock(&pcm_data->mutex);
  733. ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
  734. if (ret < 0) {
  735. dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
  736. goto out;
  737. }
  738. ret = sst_hsw_stream_free(hsw, pcm_data->stream);
  739. if (ret < 0) {
  740. dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
  741. goto out;
  742. }
  743. pcm_data->allocated = 0;
  744. pcm_data->stream = NULL;
  745. out:
  746. pm_runtime_mark_last_busy(pdata->dev);
  747. pm_runtime_put_autosuspend(pdata->dev);
  748. mutex_unlock(&pcm_data->mutex);
  749. return ret;
  750. }
  751. static const struct snd_pcm_ops hsw_pcm_ops = {
  752. .open = hsw_pcm_open,
  753. .close = hsw_pcm_close,
  754. .ioctl = snd_pcm_lib_ioctl,
  755. .hw_params = hsw_pcm_hw_params,
  756. .hw_free = hsw_pcm_hw_free,
  757. .trigger = hsw_pcm_trigger,
  758. .pointer = hsw_pcm_pointer,
  759. .page = snd_pcm_sgbuf_ops_page,
  760. };
  761. static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
  762. {
  763. struct sst_hsw *hsw = pdata->hsw;
  764. struct hsw_pcm_data *pcm_data;
  765. int i;
  766. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  767. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  768. /* create new runtime module, use same offset if recreated */
  769. pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
  770. mod_map[i].mod_id, pcm_data->persistent_offset);
  771. if (pcm_data->runtime == NULL)
  772. goto err;
  773. pcm_data->persistent_offset =
  774. pcm_data->runtime->persistent_offset;
  775. }
  776. /* create runtime blocks for module waves */
  777. if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
  778. pdata->runtime_waves = sst_hsw_runtime_module_create(hsw,
  779. SST_HSW_MODULE_WAVES, 0);
  780. if (pdata->runtime_waves == NULL)
  781. goto err;
  782. }
  783. return 0;
  784. err:
  785. for (--i; i >= 0; i--) {
  786. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  787. sst_hsw_runtime_module_free(pcm_data->runtime);
  788. }
  789. return -ENODEV;
  790. }
  791. static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
  792. {
  793. struct sst_hsw *hsw = pdata->hsw;
  794. struct hsw_pcm_data *pcm_data;
  795. int i;
  796. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  797. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  798. if (pcm_data->runtime){
  799. sst_hsw_runtime_module_free(pcm_data->runtime);
  800. pcm_data->runtime = NULL;
  801. }
  802. }
  803. if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES) &&
  804. pdata->runtime_waves) {
  805. sst_hsw_runtime_module_free(pdata->runtime_waves);
  806. pdata->runtime_waves = NULL;
  807. }
  808. }
  809. static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
  810. {
  811. struct snd_pcm *pcm = rtd->pcm;
  812. struct snd_soc_platform *platform = rtd->platform;
  813. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  814. struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
  815. struct device *dev = pdata->dma_dev;
  816. int ret = 0;
  817. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
  818. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  819. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  820. SNDRV_DMA_TYPE_DEV_SG,
  821. dev,
  822. hsw_pcm_hardware.buffer_bytes_max,
  823. hsw_pcm_hardware.buffer_bytes_max);
  824. if (ret) {
  825. dev_err(rtd->dev, "dma buffer allocation failed %d\n",
  826. ret);
  827. return ret;
  828. }
  829. }
  830. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
  831. priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
  832. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
  833. priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
  834. return ret;
  835. }
  836. #define HSW_FORMATS \
  837. (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  838. static struct snd_soc_dai_driver hsw_dais[] = {
  839. {
  840. .name = "System Pin",
  841. .id = HSW_PCM_DAI_ID_SYSTEM,
  842. .playback = {
  843. .stream_name = "System Playback",
  844. .channels_min = 2,
  845. .channels_max = 2,
  846. .rates = SNDRV_PCM_RATE_48000,
  847. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  848. },
  849. .capture = {
  850. .stream_name = "Analog Capture",
  851. .channels_min = 2,
  852. .channels_max = 4,
  853. .rates = SNDRV_PCM_RATE_48000,
  854. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  855. },
  856. },
  857. {
  858. /* PCM */
  859. .name = "Offload0 Pin",
  860. .id = HSW_PCM_DAI_ID_OFFLOAD0,
  861. .playback = {
  862. .stream_name = "Offload0 Playback",
  863. .channels_min = 2,
  864. .channels_max = 2,
  865. .rates = SNDRV_PCM_RATE_8000_192000,
  866. .formats = HSW_FORMATS,
  867. },
  868. },
  869. {
  870. /* PCM */
  871. .name = "Offload1 Pin",
  872. .id = HSW_PCM_DAI_ID_OFFLOAD1,
  873. .playback = {
  874. .stream_name = "Offload1 Playback",
  875. .channels_min = 2,
  876. .channels_max = 2,
  877. .rates = SNDRV_PCM_RATE_8000_192000,
  878. .formats = HSW_FORMATS,
  879. },
  880. },
  881. {
  882. .name = "Loopback Pin",
  883. .id = HSW_PCM_DAI_ID_LOOPBACK,
  884. .capture = {
  885. .stream_name = "Loopback Capture",
  886. .channels_min = 2,
  887. .channels_max = 2,
  888. .rates = SNDRV_PCM_RATE_48000,
  889. .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
  890. },
  891. },
  892. };
  893. static const struct snd_soc_dapm_widget widgets[] = {
  894. /* Backend DAIs */
  895. SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  896. SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  897. SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  898. SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  899. /* Global Playback Mixer */
  900. SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  901. };
  902. static const struct snd_soc_dapm_route graph[] = {
  903. /* Playback Mixer */
  904. {"Playback VMixer", NULL, "System Playback"},
  905. {"Playback VMixer", NULL, "Offload0 Playback"},
  906. {"Playback VMixer", NULL, "Offload1 Playback"},
  907. {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
  908. {"Analog Capture", NULL, "SSP0 CODEC IN"},
  909. };
  910. static int hsw_pcm_probe(struct snd_soc_platform *platform)
  911. {
  912. struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
  913. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  914. struct device *dma_dev, *dev;
  915. int i, ret = 0;
  916. if (!pdata)
  917. return -ENODEV;
  918. dev = platform->dev;
  919. dma_dev = pdata->dma_dev;
  920. priv_data->hsw = pdata->dsp;
  921. priv_data->dev = platform->dev;
  922. priv_data->pm_state = HSW_PM_STATE_D0;
  923. priv_data->soc_card = platform->component.card;
  924. /* allocate DSP buffer page tables */
  925. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  926. /* playback */
  927. if (hsw_dais[i].playback.channels_min) {
  928. mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
  929. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  930. PAGE_SIZE, &priv_data->dmab[i][0]);
  931. if (ret < 0)
  932. goto err;
  933. }
  934. /* capture */
  935. if (hsw_dais[i].capture.channels_min) {
  936. mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
  937. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
  938. PAGE_SIZE, &priv_data->dmab[i][1]);
  939. if (ret < 0)
  940. goto err;
  941. }
  942. }
  943. /* allocate runtime modules */
  944. ret = hsw_pcm_create_modules(priv_data);
  945. if (ret < 0)
  946. goto err;
  947. /* enable runtime PM with auto suspend */
  948. pm_runtime_set_autosuspend_delay(platform->dev,
  949. SST_RUNTIME_SUSPEND_DELAY);
  950. pm_runtime_use_autosuspend(platform->dev);
  951. pm_runtime_enable(platform->dev);
  952. pm_runtime_idle(platform->dev);
  953. return 0;
  954. err:
  955. for (--i; i >= 0; i--) {
  956. if (hsw_dais[i].playback.channels_min)
  957. snd_dma_free_pages(&priv_data->dmab[i][0]);
  958. if (hsw_dais[i].capture.channels_min)
  959. snd_dma_free_pages(&priv_data->dmab[i][1]);
  960. }
  961. return ret;
  962. }
  963. static int hsw_pcm_remove(struct snd_soc_platform *platform)
  964. {
  965. struct hsw_priv_data *priv_data =
  966. snd_soc_platform_get_drvdata(platform);
  967. int i;
  968. pm_runtime_disable(platform->dev);
  969. hsw_pcm_free_modules(priv_data);
  970. for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
  971. if (hsw_dais[i].playback.channels_min)
  972. snd_dma_free_pages(&priv_data->dmab[i][0]);
  973. if (hsw_dais[i].capture.channels_min)
  974. snd_dma_free_pages(&priv_data->dmab[i][1]);
  975. }
  976. return 0;
  977. }
  978. static struct snd_soc_platform_driver hsw_soc_platform = {
  979. .probe = hsw_pcm_probe,
  980. .remove = hsw_pcm_remove,
  981. .ops = &hsw_pcm_ops,
  982. .pcm_new = hsw_pcm_new,
  983. };
  984. static const struct snd_soc_component_driver hsw_dai_component = {
  985. .name = "haswell-dai",
  986. .controls = hsw_volume_controls,
  987. .num_controls = ARRAY_SIZE(hsw_volume_controls),
  988. .dapm_widgets = widgets,
  989. .num_dapm_widgets = ARRAY_SIZE(widgets),
  990. .dapm_routes = graph,
  991. .num_dapm_routes = ARRAY_SIZE(graph),
  992. };
  993. static int hsw_pcm_dev_probe(struct platform_device *pdev)
  994. {
  995. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  996. struct hsw_priv_data *priv_data;
  997. int ret;
  998. if (!sst_pdata)
  999. return -EINVAL;
  1000. priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
  1001. if (!priv_data)
  1002. return -ENOMEM;
  1003. ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
  1004. if (ret < 0)
  1005. return -ENODEV;
  1006. priv_data->hsw = sst_pdata->dsp;
  1007. platform_set_drvdata(pdev, priv_data);
  1008. ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
  1009. if (ret < 0)
  1010. goto err_plat;
  1011. ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
  1012. hsw_dais, ARRAY_SIZE(hsw_dais));
  1013. if (ret < 0)
  1014. goto err_comp;
  1015. return 0;
  1016. err_comp:
  1017. snd_soc_unregister_platform(&pdev->dev);
  1018. err_plat:
  1019. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  1020. return 0;
  1021. }
  1022. static int hsw_pcm_dev_remove(struct platform_device *pdev)
  1023. {
  1024. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  1025. snd_soc_unregister_platform(&pdev->dev);
  1026. snd_soc_unregister_component(&pdev->dev);
  1027. sst_hsw_dsp_free(&pdev->dev, sst_pdata);
  1028. return 0;
  1029. }
  1030. #ifdef CONFIG_PM
  1031. static int hsw_pcm_runtime_idle(struct device *dev)
  1032. {
  1033. return 0;
  1034. }
  1035. static int hsw_pcm_suspend(struct device *dev)
  1036. {
  1037. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1038. struct sst_hsw *hsw = pdata->hsw;
  1039. /* enter D3 state and stall */
  1040. sst_hsw_dsp_runtime_suspend(hsw);
  1041. /* free all runtime modules */
  1042. hsw_pcm_free_modules(pdata);
  1043. /* put the DSP to sleep, fw unloaded after runtime modules freed */
  1044. sst_hsw_dsp_runtime_sleep(hsw);
  1045. return 0;
  1046. }
  1047. static int hsw_pcm_runtime_suspend(struct device *dev)
  1048. {
  1049. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1050. struct sst_hsw *hsw = pdata->hsw;
  1051. int ret;
  1052. if (pdata->pm_state >= HSW_PM_STATE_RTD3)
  1053. return 0;
  1054. /* fw modules will be unloaded on RTD3, set flag to track */
  1055. if (sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
  1056. ret = sst_hsw_module_disable(hsw, SST_HSW_MODULE_WAVES, 0);
  1057. if (ret < 0)
  1058. return ret;
  1059. sst_hsw_set_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
  1060. }
  1061. hsw_pcm_suspend(dev);
  1062. pdata->pm_state = HSW_PM_STATE_RTD3;
  1063. return 0;
  1064. }
  1065. static int hsw_pcm_runtime_resume(struct device *dev)
  1066. {
  1067. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1068. struct sst_hsw *hsw = pdata->hsw;
  1069. int ret;
  1070. if (pdata->pm_state != HSW_PM_STATE_RTD3)
  1071. return 0;
  1072. ret = sst_hsw_dsp_load(hsw);
  1073. if (ret < 0) {
  1074. dev_err(dev, "failed to reload %d\n", ret);
  1075. return ret;
  1076. }
  1077. ret = hsw_pcm_create_modules(pdata);
  1078. if (ret < 0) {
  1079. dev_err(dev, "failed to create modules %d\n", ret);
  1080. return ret;
  1081. }
  1082. ret = sst_hsw_dsp_runtime_resume(hsw);
  1083. if (ret < 0)
  1084. return ret;
  1085. else if (ret == 1) /* no action required */
  1086. return 0;
  1087. /* check flag when resume */
  1088. if (sst_hsw_is_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES)) {
  1089. ret = sst_hsw_module_enable(hsw, SST_HSW_MODULE_WAVES, 0);
  1090. if (ret < 0)
  1091. return ret;
  1092. /* put parameters from buffer to dsp */
  1093. ret = sst_hsw_launch_param_buf(hsw);
  1094. if (ret < 0)
  1095. return ret;
  1096. /* unset flag */
  1097. sst_hsw_set_module_disabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
  1098. }
  1099. pdata->pm_state = HSW_PM_STATE_D0;
  1100. return ret;
  1101. }
  1102. #else
  1103. #define hsw_pcm_runtime_idle NULL
  1104. #define hsw_pcm_runtime_suspend NULL
  1105. #define hsw_pcm_runtime_resume NULL
  1106. #endif
  1107. #ifdef CONFIG_PM
  1108. static void hsw_pcm_complete(struct device *dev)
  1109. {
  1110. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1111. struct sst_hsw *hsw = pdata->hsw;
  1112. struct hsw_pcm_data *pcm_data;
  1113. int i, err;
  1114. if (pdata->pm_state != HSW_PM_STATE_D3)
  1115. return;
  1116. err = sst_hsw_dsp_load(hsw);
  1117. if (err < 0) {
  1118. dev_err(dev, "failed to reload %d\n", err);
  1119. return;
  1120. }
  1121. err = hsw_pcm_create_modules(pdata);
  1122. if (err < 0) {
  1123. dev_err(dev, "failed to create modules %d\n", err);
  1124. return;
  1125. }
  1126. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1127. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1128. if (!pcm_data->substream)
  1129. continue;
  1130. err = sst_module_runtime_restore(pcm_data->runtime,
  1131. &pcm_data->context);
  1132. if (err < 0)
  1133. dev_err(dev, "failed to restore context for PCM %d\n", i);
  1134. }
  1135. snd_soc_resume(pdata->soc_card->dev);
  1136. err = sst_hsw_dsp_runtime_resume(hsw);
  1137. if (err < 0)
  1138. return;
  1139. else if (err == 1) /* no action required */
  1140. return;
  1141. pdata->pm_state = HSW_PM_STATE_D0;
  1142. return;
  1143. }
  1144. static int hsw_pcm_prepare(struct device *dev)
  1145. {
  1146. struct hsw_priv_data *pdata = dev_get_drvdata(dev);
  1147. struct hsw_pcm_data *pcm_data;
  1148. int i, err;
  1149. if (pdata->pm_state == HSW_PM_STATE_D3)
  1150. return 0;
  1151. else if (pdata->pm_state == HSW_PM_STATE_D0) {
  1152. /* suspend all active streams */
  1153. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1154. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1155. if (!pcm_data->substream)
  1156. continue;
  1157. dev_dbg(dev, "suspending pcm %d\n", i);
  1158. snd_pcm_suspend_all(pcm_data->hsw_pcm);
  1159. /* We need to wait until the DSP FW stops the streams */
  1160. msleep(2);
  1161. }
  1162. /* preserve persistent memory */
  1163. for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
  1164. pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
  1165. if (!pcm_data->substream)
  1166. continue;
  1167. dev_dbg(dev, "saving context pcm %d\n", i);
  1168. err = sst_module_runtime_save(pcm_data->runtime,
  1169. &pcm_data->context);
  1170. if (err < 0)
  1171. dev_err(dev, "failed to save context for PCM %d\n", i);
  1172. }
  1173. hsw_pcm_suspend(dev);
  1174. }
  1175. snd_soc_suspend(pdata->soc_card->dev);
  1176. snd_soc_poweroff(pdata->soc_card->dev);
  1177. pdata->pm_state = HSW_PM_STATE_D3;
  1178. return 0;
  1179. }
  1180. #else
  1181. #define hsw_pcm_prepare NULL
  1182. #define hsw_pcm_complete NULL
  1183. #endif
  1184. static const struct dev_pm_ops hsw_pcm_pm = {
  1185. .runtime_idle = hsw_pcm_runtime_idle,
  1186. .runtime_suspend = hsw_pcm_runtime_suspend,
  1187. .runtime_resume = hsw_pcm_runtime_resume,
  1188. .prepare = hsw_pcm_prepare,
  1189. .complete = hsw_pcm_complete,
  1190. };
  1191. static struct platform_driver hsw_pcm_driver = {
  1192. .driver = {
  1193. .name = "haswell-pcm-audio",
  1194. .pm = &hsw_pcm_pm,
  1195. },
  1196. .probe = hsw_pcm_dev_probe,
  1197. .remove = hsw_pcm_dev_remove,
  1198. };
  1199. module_platform_driver(hsw_pcm_driver);
  1200. MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
  1201. MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
  1202. MODULE_LICENSE("GPL v2");
  1203. MODULE_ALIAS("platform:haswell-pcm-audio");