pcm.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2020 Intel Corporation
  4. //
  5. // Author: Cezary Rojewski <cezary.rojewski@intel.com>
  6. //
  7. #include <linux/pm_runtime.h>
  8. #include <sound/soc.h>
  9. #include <sound/pcm_params.h>
  10. #include <uapi/sound/tlv.h>
  11. #include "core.h"
  12. #include "messages.h"
  13. struct catpt_stream_template {
  14. enum catpt_path_id path_id;
  15. enum catpt_stream_type type;
  16. u32 persistent_size;
  17. u8 num_entries;
  18. struct catpt_module_entry entries[];
  19. };
  20. static struct catpt_stream_template system_pb = {
  21. .path_id = CATPT_PATH_SSP0_OUT,
  22. .type = CATPT_STRM_TYPE_SYSTEM,
  23. .num_entries = 1,
  24. .entries = {{ CATPT_MODID_PCM_SYSTEM, 0 }},
  25. };
  26. static struct catpt_stream_template system_cp = {
  27. .path_id = CATPT_PATH_SSP0_IN,
  28. .type = CATPT_STRM_TYPE_CAPTURE,
  29. .num_entries = 1,
  30. .entries = {{ CATPT_MODID_PCM_CAPTURE, 0 }},
  31. };
  32. static struct catpt_stream_template offload_pb = {
  33. .path_id = CATPT_PATH_SSP0_OUT,
  34. .type = CATPT_STRM_TYPE_RENDER,
  35. .num_entries = 1,
  36. .entries = {{ CATPT_MODID_PCM, 0 }},
  37. };
  38. static struct catpt_stream_template loopback_cp = {
  39. .path_id = CATPT_PATH_SSP0_OUT,
  40. .type = CATPT_STRM_TYPE_LOOPBACK,
  41. .num_entries = 1,
  42. .entries = {{ CATPT_MODID_PCM_REFERENCE, 0 }},
  43. };
  44. static struct catpt_stream_template bluetooth_pb = {
  45. .path_id = CATPT_PATH_SSP1_OUT,
  46. .type = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
  47. .num_entries = 1,
  48. .entries = {{ CATPT_MODID_BLUETOOTH_RENDER, 0 }},
  49. };
  50. static struct catpt_stream_template bluetooth_cp = {
  51. .path_id = CATPT_PATH_SSP1_IN,
  52. .type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE,
  53. .num_entries = 1,
  54. .entries = {{ CATPT_MODID_BLUETOOTH_CAPTURE, 0 }},
  55. };
  56. static struct catpt_stream_template *catpt_topology[] = {
  57. [CATPT_STRM_TYPE_RENDER] = &offload_pb,
  58. [CATPT_STRM_TYPE_SYSTEM] = &system_pb,
  59. [CATPT_STRM_TYPE_CAPTURE] = &system_cp,
  60. [CATPT_STRM_TYPE_LOOPBACK] = &loopback_cp,
  61. [CATPT_STRM_TYPE_BLUETOOTH_RENDER] = &bluetooth_pb,
  62. [CATPT_STRM_TYPE_BLUETOOTH_CAPTURE] = &bluetooth_cp,
  63. };
  64. static struct catpt_stream_template *
  65. catpt_get_stream_template(struct snd_pcm_substream *substream)
  66. {
  67. struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
  68. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0);
  69. enum catpt_stream_type type;
  70. type = cpu_dai->driver->id;
  71. /* account for capture in bidirectional dais */
  72. switch (type) {
  73. case CATPT_STRM_TYPE_SYSTEM:
  74. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  75. type = CATPT_STRM_TYPE_CAPTURE;
  76. break;
  77. case CATPT_STRM_TYPE_BLUETOOTH_RENDER:
  78. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  79. type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE;
  80. break;
  81. default:
  82. break;
  83. }
  84. return catpt_topology[type];
  85. }
  86. struct catpt_stream_runtime *
  87. catpt_stream_find(struct catpt_dev *cdev, u8 stream_hw_id)
  88. {
  89. struct catpt_stream_runtime *pos, *result = NULL;
  90. spin_lock(&cdev->list_lock);
  91. list_for_each_entry(pos, &cdev->stream_list, node) {
  92. if (pos->info.stream_hw_id == stream_hw_id) {
  93. result = pos;
  94. break;
  95. }
  96. }
  97. spin_unlock(&cdev->list_lock);
  98. return result;
  99. }
  100. static u32 catpt_stream_read_position(struct catpt_dev *cdev,
  101. struct catpt_stream_runtime *stream)
  102. {
  103. u32 pos;
  104. memcpy_fromio(&pos, cdev->lpe_ba + stream->info.read_pos_regaddr,
  105. sizeof(pos));
  106. return pos;
  107. }
  108. static u32 catpt_stream_volume(struct catpt_dev *cdev,
  109. struct catpt_stream_runtime *stream, u32 channel)
  110. {
  111. u32 volume, offset;
  112. if (channel >= CATPT_CHANNELS_MAX)
  113. channel = 0;
  114. offset = stream->info.volume_regaddr[channel];
  115. memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
  116. return volume;
  117. }
  118. static u32 catpt_mixer_volume(struct catpt_dev *cdev,
  119. struct catpt_mixer_stream_info *info, u32 channel)
  120. {
  121. u32 volume, offset;
  122. if (channel >= CATPT_CHANNELS_MAX)
  123. channel = 0;
  124. offset = info->volume_regaddr[channel];
  125. memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
  126. return volume;
  127. }
  128. static void catpt_arrange_page_table(struct snd_pcm_substream *substream,
  129. struct snd_dma_buffer *pgtbl)
  130. {
  131. struct snd_pcm_runtime *rtm = substream->runtime;
  132. struct snd_dma_buffer *databuf = snd_pcm_get_dma_buf(substream);
  133. int i, pages;
  134. pages = snd_sgbuf_aligned_pages(rtm->dma_bytes);
  135. for (i = 0; i < pages; i++) {
  136. u32 pfn, offset;
  137. u32 *page_table;
  138. pfn = PFN_DOWN(snd_sgbuf_get_addr(databuf, i * PAGE_SIZE));
  139. /* incrementing by 2 on even and 3 on odd */
  140. offset = ((i << 2) + i) >> 1;
  141. page_table = (u32 *)(pgtbl->area + offset);
  142. if (i & 1)
  143. *page_table |= (pfn << 4);
  144. else
  145. *page_table |= pfn;
  146. }
  147. }
  148. static u32 catpt_get_channel_map(enum catpt_channel_config config)
  149. {
  150. switch (config) {
  151. case CATPT_CHANNEL_CONFIG_MONO:
  152. return GENMASK(31, 4) | CATPT_CHANNEL_CENTER;
  153. case CATPT_CHANNEL_CONFIG_STEREO:
  154. return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
  155. | (CATPT_CHANNEL_RIGHT << 4);
  156. case CATPT_CHANNEL_CONFIG_2_POINT_1:
  157. return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
  158. | (CATPT_CHANNEL_RIGHT << 4)
  159. | (CATPT_CHANNEL_LFE << 8);
  160. case CATPT_CHANNEL_CONFIG_3_POINT_0:
  161. return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
  162. | (CATPT_CHANNEL_CENTER << 4)
  163. | (CATPT_CHANNEL_RIGHT << 8);
  164. case CATPT_CHANNEL_CONFIG_3_POINT_1:
  165. return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
  166. | (CATPT_CHANNEL_CENTER << 4)
  167. | (CATPT_CHANNEL_RIGHT << 8)
  168. | (CATPT_CHANNEL_LFE << 12);
  169. case CATPT_CHANNEL_CONFIG_QUATRO:
  170. return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
  171. | (CATPT_CHANNEL_RIGHT << 4)
  172. | (CATPT_CHANNEL_LEFT_SURROUND << 8)
  173. | (CATPT_CHANNEL_RIGHT_SURROUND << 12);
  174. case CATPT_CHANNEL_CONFIG_4_POINT_0:
  175. return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
  176. | (CATPT_CHANNEL_CENTER << 4)
  177. | (CATPT_CHANNEL_RIGHT << 8)
  178. | (CATPT_CHANNEL_CENTER_SURROUND << 12);
  179. case CATPT_CHANNEL_CONFIG_5_POINT_0:
  180. return GENMASK(31, 20) | CATPT_CHANNEL_LEFT
  181. | (CATPT_CHANNEL_CENTER << 4)
  182. | (CATPT_CHANNEL_RIGHT << 8)
  183. | (CATPT_CHANNEL_LEFT_SURROUND << 12)
  184. | (CATPT_CHANNEL_RIGHT_SURROUND << 16);
  185. case CATPT_CHANNEL_CONFIG_5_POINT_1:
  186. return GENMASK(31, 24) | CATPT_CHANNEL_CENTER
  187. | (CATPT_CHANNEL_LEFT << 4)
  188. | (CATPT_CHANNEL_RIGHT << 8)
  189. | (CATPT_CHANNEL_LEFT_SURROUND << 12)
  190. | (CATPT_CHANNEL_RIGHT_SURROUND << 16)
  191. | (CATPT_CHANNEL_LFE << 20);
  192. case CATPT_CHANNEL_CONFIG_DUAL_MONO:
  193. return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
  194. | (CATPT_CHANNEL_LEFT << 4);
  195. default:
  196. return U32_MAX;
  197. }
  198. }
  199. static enum catpt_channel_config catpt_get_channel_config(u32 num_channels)
  200. {
  201. switch (num_channels) {
  202. case 6:
  203. return CATPT_CHANNEL_CONFIG_5_POINT_1;
  204. case 5:
  205. return CATPT_CHANNEL_CONFIG_5_POINT_0;
  206. case 4:
  207. return CATPT_CHANNEL_CONFIG_QUATRO;
  208. case 3:
  209. return CATPT_CHANNEL_CONFIG_2_POINT_1;
  210. case 1:
  211. return CATPT_CHANNEL_CONFIG_MONO;
  212. case 2:
  213. default:
  214. return CATPT_CHANNEL_CONFIG_STEREO;
  215. }
  216. }
  217. static int catpt_dai_startup(struct snd_pcm_substream *substream,
  218. struct snd_soc_dai *dai)
  219. {
  220. struct catpt_stream_template *template;
  221. struct catpt_stream_runtime *stream;
  222. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  223. struct resource *res;
  224. int ret;
  225. template = catpt_get_stream_template(substream);
  226. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  227. if (!stream)
  228. return -ENOMEM;
  229. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, cdev->dev, PAGE_SIZE,
  230. &stream->pgtbl);
  231. if (ret)
  232. goto err_pgtbl;
  233. res = catpt_request_region(&cdev->dram, template->persistent_size);
  234. if (!res) {
  235. ret = -EBUSY;
  236. goto err_request;
  237. }
  238. catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
  239. stream->template = template;
  240. stream->persistent = res;
  241. stream->substream = substream;
  242. INIT_LIST_HEAD(&stream->node);
  243. snd_soc_dai_set_dma_data(dai, substream, stream);
  244. spin_lock(&cdev->list_lock);
  245. list_add_tail(&stream->node, &cdev->stream_list);
  246. spin_unlock(&cdev->list_lock);
  247. return 0;
  248. err_request:
  249. snd_dma_free_pages(&stream->pgtbl);
  250. err_pgtbl:
  251. kfree(stream);
  252. return ret;
  253. }
  254. static void catpt_dai_shutdown(struct snd_pcm_substream *substream,
  255. struct snd_soc_dai *dai)
  256. {
  257. struct catpt_stream_runtime *stream;
  258. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  259. stream = snd_soc_dai_get_dma_data(dai, substream);
  260. spin_lock(&cdev->list_lock);
  261. list_del(&stream->node);
  262. spin_unlock(&cdev->list_lock);
  263. release_resource(stream->persistent);
  264. kfree(stream->persistent);
  265. catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
  266. snd_dma_free_pages(&stream->pgtbl);
  267. kfree(stream);
  268. snd_soc_dai_set_dma_data(dai, substream, NULL);
  269. }
  270. static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol);
  271. static int catpt_dai_apply_usettings(struct snd_soc_dai *dai,
  272. struct catpt_stream_runtime *stream)
  273. {
  274. struct snd_soc_component *component = dai->component;
  275. struct snd_kcontrol *pos;
  276. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  277. const char *name;
  278. int ret;
  279. u32 id = stream->info.stream_hw_id;
  280. /* only selected streams have individual controls */
  281. switch (id) {
  282. case CATPT_PIN_ID_OFFLOAD1:
  283. name = "Media0 Playback Volume";
  284. break;
  285. case CATPT_PIN_ID_OFFLOAD2:
  286. name = "Media1 Playback Volume";
  287. break;
  288. case CATPT_PIN_ID_CAPTURE1:
  289. name = "Mic Capture Volume";
  290. break;
  291. case CATPT_PIN_ID_REFERENCE:
  292. name = "Loopback Mute";
  293. break;
  294. default:
  295. return 0;
  296. }
  297. list_for_each_entry(pos, &component->card->snd_card->controls, list) {
  298. if (pos->private_data == component &&
  299. !strncmp(name, pos->id.name, sizeof(pos->id.name)))
  300. break;
  301. }
  302. if (list_entry_is_head(pos, &component->card->snd_card->controls, list))
  303. return -ENOENT;
  304. if (stream->template->type != CATPT_STRM_TYPE_LOOPBACK)
  305. return catpt_set_dspvol(cdev, id, (long *)pos->private_value);
  306. ret = catpt_ipc_mute_loopback(cdev, id, *(bool *)pos->private_value);
  307. if (ret)
  308. return CATPT_IPC_ERROR(ret);
  309. return 0;
  310. }
  311. static int catpt_dai_hw_params(struct snd_pcm_substream *substream,
  312. struct snd_pcm_hw_params *params,
  313. struct snd_soc_dai *dai)
  314. {
  315. struct snd_pcm_runtime *rtm = substream->runtime;
  316. struct snd_dma_buffer *dmab;
  317. struct catpt_stream_runtime *stream;
  318. struct catpt_audio_format afmt;
  319. struct catpt_ring_info rinfo;
  320. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  321. int ret;
  322. stream = snd_soc_dai_get_dma_data(dai, substream);
  323. if (stream->allocated)
  324. return 0;
  325. memset(&afmt, 0, sizeof(afmt));
  326. afmt.sample_rate = params_rate(params);
  327. afmt.bit_depth = params_physical_width(params);
  328. afmt.valid_bit_depth = params_width(params);
  329. afmt.num_channels = params_channels(params);
  330. afmt.channel_config = catpt_get_channel_config(afmt.num_channels);
  331. afmt.channel_map = catpt_get_channel_map(afmt.channel_config);
  332. afmt.interleaving = CATPT_INTERLEAVING_PER_CHANNEL;
  333. dmab = snd_pcm_get_dma_buf(substream);
  334. catpt_arrange_page_table(substream, &stream->pgtbl);
  335. memset(&rinfo, 0, sizeof(rinfo));
  336. rinfo.page_table_addr = stream->pgtbl.addr;
  337. rinfo.num_pages = DIV_ROUND_UP(rtm->dma_bytes, PAGE_SIZE);
  338. rinfo.size = rtm->dma_bytes;
  339. rinfo.offset = 0;
  340. rinfo.ring_first_page_pfn = PFN_DOWN(snd_sgbuf_get_addr(dmab, 0));
  341. ret = catpt_ipc_alloc_stream(cdev, stream->template->path_id,
  342. stream->template->type,
  343. &afmt, &rinfo,
  344. stream->template->num_entries,
  345. stream->template->entries,
  346. stream->persistent,
  347. cdev->scratch,
  348. &stream->info);
  349. if (ret)
  350. return CATPT_IPC_ERROR(ret);
  351. ret = catpt_dai_apply_usettings(dai, stream);
  352. if (ret)
  353. return ret;
  354. stream->allocated = true;
  355. return 0;
  356. }
  357. static int catpt_dai_hw_free(struct snd_pcm_substream *substream,
  358. struct snd_soc_dai *dai)
  359. {
  360. struct catpt_stream_runtime *stream;
  361. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  362. stream = snd_soc_dai_get_dma_data(dai, substream);
  363. if (!stream->allocated)
  364. return 0;
  365. catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
  366. catpt_ipc_free_stream(cdev, stream->info.stream_hw_id);
  367. stream->allocated = false;
  368. return 0;
  369. }
  370. static int catpt_dai_prepare(struct snd_pcm_substream *substream,
  371. struct snd_soc_dai *dai)
  372. {
  373. struct catpt_stream_runtime *stream;
  374. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  375. int ret;
  376. stream = snd_soc_dai_get_dma_data(dai, substream);
  377. if (stream->prepared)
  378. return 0;
  379. ret = catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
  380. if (ret)
  381. return CATPT_IPC_ERROR(ret);
  382. ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
  383. if (ret)
  384. return CATPT_IPC_ERROR(ret);
  385. stream->prepared = true;
  386. return 0;
  387. }
  388. static int catpt_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  389. struct snd_soc_dai *dai)
  390. {
  391. struct snd_pcm_runtime *runtime = substream->runtime;
  392. struct catpt_stream_runtime *stream;
  393. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  394. snd_pcm_uframes_t pos;
  395. int ret;
  396. stream = snd_soc_dai_get_dma_data(dai, substream);
  397. switch (cmd) {
  398. case SNDRV_PCM_TRIGGER_START:
  399. /* only offload is set_write_pos driven */
  400. if (stream->template->type != CATPT_STRM_TYPE_RENDER)
  401. goto resume_stream;
  402. pos = frames_to_bytes(runtime, runtime->start_threshold);
  403. /*
  404. * Dsp operates on buffer halves, thus max 2x set_write_pos
  405. * (entire buffer filled) prior to stream start.
  406. */
  407. ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
  408. pos, false, false);
  409. if (ret)
  410. return CATPT_IPC_ERROR(ret);
  411. fallthrough;
  412. case SNDRV_PCM_TRIGGER_RESUME:
  413. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  414. resume_stream:
  415. catpt_dsp_update_lpclock(cdev);
  416. ret = catpt_ipc_resume_stream(cdev, stream->info.stream_hw_id);
  417. if (ret)
  418. return CATPT_IPC_ERROR(ret);
  419. break;
  420. case SNDRV_PCM_TRIGGER_STOP:
  421. stream->prepared = false;
  422. fallthrough;
  423. case SNDRV_PCM_TRIGGER_SUSPEND:
  424. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  425. ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
  426. catpt_dsp_update_lpclock(cdev);
  427. if (ret)
  428. return CATPT_IPC_ERROR(ret);
  429. break;
  430. default:
  431. break;
  432. }
  433. return 0;
  434. }
  435. void catpt_stream_update_position(struct catpt_dev *cdev,
  436. struct catpt_stream_runtime *stream,
  437. struct catpt_notify_position *pos)
  438. {
  439. struct snd_pcm_substream *substream = stream->substream;
  440. struct snd_pcm_runtime *r = substream->runtime;
  441. snd_pcm_uframes_t dsppos, newpos;
  442. int ret;
  443. dsppos = bytes_to_frames(r, pos->stream_position);
  444. if (!stream->prepared)
  445. goto exit;
  446. /* only offload is set_write_pos driven */
  447. if (stream->template->type != CATPT_STRM_TYPE_RENDER)
  448. goto exit;
  449. if (dsppos >= r->buffer_size / 2)
  450. newpos = r->buffer_size / 2;
  451. else
  452. newpos = 0;
  453. /*
  454. * Dsp operates on buffer halves, thus on every notify position
  455. * (buffer half consumed) update wp to allow stream progression.
  456. */
  457. ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
  458. frames_to_bytes(r, newpos),
  459. false, false);
  460. if (ret) {
  461. dev_err(cdev->dev, "update position for stream %d failed: %d\n",
  462. stream->info.stream_hw_id, ret);
  463. return;
  464. }
  465. exit:
  466. snd_pcm_period_elapsed(substream);
  467. }
  468. /* 200 ms for 2 32-bit channels at 48kHz (native format) */
  469. #define CATPT_BUFFER_MAX_SIZE 76800
  470. #define CATPT_PCM_PERIODS_MAX 4
  471. #define CATPT_PCM_PERIODS_MIN 2
  472. static const struct snd_pcm_hardware catpt_pcm_hardware = {
  473. .info = SNDRV_PCM_INFO_MMAP |
  474. SNDRV_PCM_INFO_MMAP_VALID |
  475. SNDRV_PCM_INFO_INTERLEAVED |
  476. SNDRV_PCM_INFO_PAUSE |
  477. SNDRV_PCM_INFO_RESUME |
  478. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  479. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  480. SNDRV_PCM_FMTBIT_S24_LE |
  481. SNDRV_PCM_FMTBIT_S32_LE,
  482. .period_bytes_min = PAGE_SIZE,
  483. .period_bytes_max = CATPT_BUFFER_MAX_SIZE / CATPT_PCM_PERIODS_MIN,
  484. .periods_min = CATPT_PCM_PERIODS_MIN,
  485. .periods_max = CATPT_PCM_PERIODS_MAX,
  486. .buffer_bytes_max = CATPT_BUFFER_MAX_SIZE,
  487. };
  488. static int catpt_component_pcm_construct(struct snd_soc_component *component,
  489. struct snd_soc_pcm_runtime *rtm)
  490. {
  491. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  492. snd_pcm_set_managed_buffer_all(rtm->pcm, SNDRV_DMA_TYPE_DEV_SG,
  493. cdev->dev,
  494. catpt_pcm_hardware.buffer_bytes_max,
  495. catpt_pcm_hardware.buffer_bytes_max);
  496. return 0;
  497. }
  498. static int catpt_component_open(struct snd_soc_component *component,
  499. struct snd_pcm_substream *substream)
  500. {
  501. struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
  502. if (!rtm->dai_link->no_pcm)
  503. snd_soc_set_runtime_hwparams(substream, &catpt_pcm_hardware);
  504. return 0;
  505. }
  506. static snd_pcm_uframes_t
  507. catpt_component_pointer(struct snd_soc_component *component,
  508. struct snd_pcm_substream *substream)
  509. {
  510. struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
  511. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0);
  512. struct catpt_stream_runtime *stream;
  513. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  514. u32 pos;
  515. if (rtm->dai_link->no_pcm)
  516. return 0;
  517. stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
  518. pos = catpt_stream_read_position(cdev, stream);
  519. return bytes_to_frames(substream->runtime, pos);
  520. }
  521. static const struct snd_soc_dai_ops catpt_fe_dai_ops = {
  522. .startup = catpt_dai_startup,
  523. .shutdown = catpt_dai_shutdown,
  524. .hw_params = catpt_dai_hw_params,
  525. .hw_free = catpt_dai_hw_free,
  526. .prepare = catpt_dai_prepare,
  527. .trigger = catpt_dai_trigger,
  528. };
  529. static int catpt_dai_pcm_new(struct snd_soc_pcm_runtime *rtm,
  530. struct snd_soc_dai *dai)
  531. {
  532. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtm, 0);
  533. struct catpt_ssp_device_format devfmt;
  534. struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
  535. int ret;
  536. devfmt.iface = dai->driver->id;
  537. devfmt.channels = codec_dai->driver->capture.channels_max;
  538. switch (devfmt.iface) {
  539. case CATPT_SSP_IFACE_0:
  540. devfmt.mclk = CATPT_MCLK_FREQ_24_MHZ;
  541. switch (devfmt.channels) {
  542. case 4:
  543. devfmt.mode = CATPT_SSP_MODE_TDM_PROVIDER;
  544. devfmt.clock_divider = 4;
  545. break;
  546. case 2:
  547. default:
  548. devfmt.mode = CATPT_SSP_MODE_I2S_PROVIDER;
  549. devfmt.clock_divider = 9;
  550. break;
  551. }
  552. break;
  553. case CATPT_SSP_IFACE_1:
  554. devfmt.mclk = CATPT_MCLK_OFF;
  555. devfmt.mode = CATPT_SSP_MODE_I2S_CONSUMER;
  556. devfmt.clock_divider = 0;
  557. break;
  558. }
  559. /* see if this is a new configuration */
  560. if (!memcmp(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt)))
  561. return 0;
  562. ret = pm_runtime_resume_and_get(cdev->dev);
  563. if (ret < 0 && ret != -EACCES)
  564. return ret;
  565. ret = catpt_ipc_set_device_format(cdev, &devfmt);
  566. pm_runtime_mark_last_busy(cdev->dev);
  567. pm_runtime_put_autosuspend(cdev->dev);
  568. if (ret)
  569. return CATPT_IPC_ERROR(ret);
  570. /* store device format set for given SSP */
  571. memcpy(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt));
  572. return 0;
  573. }
  574. static const struct snd_soc_dai_ops catpt_dai_ops = {
  575. .pcm_new = catpt_dai_pcm_new,
  576. };
  577. static struct snd_soc_dai_driver dai_drivers[] = {
  578. /* FE DAIs */
  579. {
  580. .name = "System Pin",
  581. .id = CATPT_STRM_TYPE_SYSTEM,
  582. .ops = &catpt_fe_dai_ops,
  583. .playback = {
  584. .stream_name = "System Playback",
  585. .channels_min = 2,
  586. .channels_max = 2,
  587. .rates = SNDRV_PCM_RATE_48000,
  588. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  589. },
  590. .capture = {
  591. .stream_name = "Analog Capture",
  592. .channels_min = 2,
  593. .channels_max = 4,
  594. .rates = SNDRV_PCM_RATE_48000,
  595. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  596. },
  597. },
  598. {
  599. .name = "Offload0 Pin",
  600. .id = CATPT_STRM_TYPE_RENDER,
  601. .ops = &catpt_fe_dai_ops,
  602. .playback = {
  603. .stream_name = "Offload0 Playback",
  604. .channels_min = 2,
  605. .channels_max = 2,
  606. .rates = SNDRV_PCM_RATE_8000_192000,
  607. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  608. },
  609. },
  610. {
  611. .name = "Offload1 Pin",
  612. .id = CATPT_STRM_TYPE_RENDER,
  613. .ops = &catpt_fe_dai_ops,
  614. .playback = {
  615. .stream_name = "Offload1 Playback",
  616. .channels_min = 2,
  617. .channels_max = 2,
  618. .rates = SNDRV_PCM_RATE_8000_192000,
  619. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  620. },
  621. },
  622. {
  623. .name = "Loopback Pin",
  624. .id = CATPT_STRM_TYPE_LOOPBACK,
  625. .ops = &catpt_fe_dai_ops,
  626. .capture = {
  627. .stream_name = "Loopback Capture",
  628. .channels_min = 2,
  629. .channels_max = 2,
  630. .rates = SNDRV_PCM_RATE_48000,
  631. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  632. },
  633. },
  634. {
  635. .name = "Bluetooth Pin",
  636. .id = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
  637. .ops = &catpt_fe_dai_ops,
  638. .playback = {
  639. .stream_name = "Bluetooth Playback",
  640. .channels_min = 1,
  641. .channels_max = 1,
  642. .rates = SNDRV_PCM_RATE_8000,
  643. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  644. },
  645. .capture = {
  646. .stream_name = "Bluetooth Capture",
  647. .channels_min = 1,
  648. .channels_max = 1,
  649. .rates = SNDRV_PCM_RATE_8000,
  650. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  651. },
  652. },
  653. /* BE DAIs */
  654. {
  655. .name = "ssp0-port",
  656. .id = CATPT_SSP_IFACE_0,
  657. .playback = {
  658. .channels_min = 1,
  659. .channels_max = 8,
  660. },
  661. .capture = {
  662. .channels_min = 1,
  663. .channels_max = 8,
  664. },
  665. .ops = &catpt_dai_ops,
  666. },
  667. {
  668. .name = "ssp1-port",
  669. .id = CATPT_SSP_IFACE_1,
  670. .playback = {
  671. .channels_min = 1,
  672. .channels_max = 8,
  673. },
  674. .capture = {
  675. .channels_min = 1,
  676. .channels_max = 8,
  677. },
  678. .ops = &catpt_dai_ops,
  679. },
  680. };
  681. #define DSP_VOLUME_MAX S32_MAX /* 0db */
  682. #define DSP_VOLUME_STEP_MAX 30
  683. static u32 ctlvol_to_dspvol(u32 value)
  684. {
  685. if (value > DSP_VOLUME_STEP_MAX)
  686. value = 0;
  687. return DSP_VOLUME_MAX >> (DSP_VOLUME_STEP_MAX - value);
  688. }
  689. static u32 dspvol_to_ctlvol(u32 volume)
  690. {
  691. if (volume > DSP_VOLUME_MAX)
  692. return DSP_VOLUME_STEP_MAX;
  693. return volume ? __fls(volume) : 0;
  694. }
  695. static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol)
  696. {
  697. u32 dspvol;
  698. int ret, i;
  699. for (i = 1; i < CATPT_CHANNELS_MAX; i++)
  700. if (ctlvol[i] != ctlvol[0])
  701. break;
  702. if (i == CATPT_CHANNELS_MAX) {
  703. dspvol = ctlvol_to_dspvol(ctlvol[0]);
  704. ret = catpt_ipc_set_volume(cdev, stream_id,
  705. CATPT_ALL_CHANNELS_MASK, dspvol,
  706. 0, CATPT_AUDIO_CURVE_NONE);
  707. } else {
  708. for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
  709. dspvol = ctlvol_to_dspvol(ctlvol[i]);
  710. ret = catpt_ipc_set_volume(cdev, stream_id,
  711. i, dspvol,
  712. 0, CATPT_AUDIO_CURVE_NONE);
  713. if (ret)
  714. break;
  715. }
  716. }
  717. if (ret)
  718. return CATPT_IPC_ERROR(ret);
  719. return 0;
  720. }
  721. static int catpt_volume_info(struct snd_kcontrol *kcontrol,
  722. struct snd_ctl_elem_info *uinfo)
  723. {
  724. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  725. uinfo->count = CATPT_CHANNELS_MAX;
  726. uinfo->value.integer.min = 0;
  727. uinfo->value.integer.max = DSP_VOLUME_STEP_MAX;
  728. return 0;
  729. }
  730. static int catpt_mixer_volume_get(struct snd_kcontrol *kcontrol,
  731. struct snd_ctl_elem_value *ucontrol)
  732. {
  733. struct snd_soc_component *component =
  734. snd_soc_kcontrol_component(kcontrol);
  735. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  736. u32 dspvol;
  737. int ret;
  738. int i;
  739. ret = pm_runtime_resume_and_get(cdev->dev);
  740. if (ret < 0 && ret != -EACCES)
  741. return ret;
  742. for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
  743. dspvol = catpt_mixer_volume(cdev, &cdev->mixer, i);
  744. ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
  745. }
  746. pm_runtime_mark_last_busy(cdev->dev);
  747. pm_runtime_put_autosuspend(cdev->dev);
  748. return 0;
  749. }
  750. static int catpt_mixer_volume_put(struct snd_kcontrol *kcontrol,
  751. struct snd_ctl_elem_value *ucontrol)
  752. {
  753. struct snd_soc_component *component =
  754. snd_soc_kcontrol_component(kcontrol);
  755. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  756. int ret;
  757. ret = pm_runtime_resume_and_get(cdev->dev);
  758. if (ret < 0 && ret != -EACCES)
  759. return ret;
  760. ret = catpt_set_dspvol(cdev, cdev->mixer.mixer_hw_id,
  761. ucontrol->value.integer.value);
  762. pm_runtime_mark_last_busy(cdev->dev);
  763. pm_runtime_put_autosuspend(cdev->dev);
  764. return ret;
  765. }
  766. static int catpt_stream_volume_get(struct snd_kcontrol *kcontrol,
  767. struct snd_ctl_elem_value *ucontrol,
  768. enum catpt_pin_id pin_id)
  769. {
  770. struct snd_soc_component *component =
  771. snd_soc_kcontrol_component(kcontrol);
  772. struct catpt_stream_runtime *stream;
  773. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  774. long *ctlvol = (long *)kcontrol->private_value;
  775. u32 dspvol;
  776. int ret;
  777. int i;
  778. stream = catpt_stream_find(cdev, pin_id);
  779. if (!stream) {
  780. for (i = 0; i < CATPT_CHANNELS_MAX; i++)
  781. ucontrol->value.integer.value[i] = ctlvol[i];
  782. return 0;
  783. }
  784. ret = pm_runtime_resume_and_get(cdev->dev);
  785. if (ret < 0 && ret != -EACCES)
  786. return ret;
  787. for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
  788. dspvol = catpt_stream_volume(cdev, stream, i);
  789. ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
  790. }
  791. pm_runtime_mark_last_busy(cdev->dev);
  792. pm_runtime_put_autosuspend(cdev->dev);
  793. return 0;
  794. }
  795. static int catpt_stream_volume_put(struct snd_kcontrol *kcontrol,
  796. struct snd_ctl_elem_value *ucontrol,
  797. enum catpt_pin_id pin_id)
  798. {
  799. struct snd_soc_component *component =
  800. snd_soc_kcontrol_component(kcontrol);
  801. struct catpt_stream_runtime *stream;
  802. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  803. long *ctlvol = (long *)kcontrol->private_value;
  804. int ret, i;
  805. stream = catpt_stream_find(cdev, pin_id);
  806. if (!stream) {
  807. for (i = 0; i < CATPT_CHANNELS_MAX; i++)
  808. ctlvol[i] = ucontrol->value.integer.value[i];
  809. return 0;
  810. }
  811. ret = pm_runtime_resume_and_get(cdev->dev);
  812. if (ret < 0 && ret != -EACCES)
  813. return ret;
  814. ret = catpt_set_dspvol(cdev, stream->info.stream_hw_id,
  815. ucontrol->value.integer.value);
  816. pm_runtime_mark_last_busy(cdev->dev);
  817. pm_runtime_put_autosuspend(cdev->dev);
  818. if (ret)
  819. return ret;
  820. for (i = 0; i < CATPT_CHANNELS_MAX; i++)
  821. ctlvol[i] = ucontrol->value.integer.value[i];
  822. return 0;
  823. }
  824. static int catpt_offload1_volume_get(struct snd_kcontrol *kctl,
  825. struct snd_ctl_elem_value *uctl)
  826. {
  827. return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
  828. }
  829. static int catpt_offload1_volume_put(struct snd_kcontrol *kctl,
  830. struct snd_ctl_elem_value *uctl)
  831. {
  832. return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
  833. }
  834. static int catpt_offload2_volume_get(struct snd_kcontrol *kctl,
  835. struct snd_ctl_elem_value *uctl)
  836. {
  837. return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
  838. }
  839. static int catpt_offload2_volume_put(struct snd_kcontrol *kctl,
  840. struct snd_ctl_elem_value *uctl)
  841. {
  842. return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
  843. }
  844. static int catpt_capture_volume_get(struct snd_kcontrol *kctl,
  845. struct snd_ctl_elem_value *uctl)
  846. {
  847. return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
  848. }
  849. static int catpt_capture_volume_put(struct snd_kcontrol *kctl,
  850. struct snd_ctl_elem_value *uctl)
  851. {
  852. return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
  853. }
  854. static int catpt_loopback_switch_get(struct snd_kcontrol *kcontrol,
  855. struct snd_ctl_elem_value *ucontrol)
  856. {
  857. ucontrol->value.integer.value[0] = *(bool *)kcontrol->private_value;
  858. return 0;
  859. }
  860. static int catpt_loopback_switch_put(struct snd_kcontrol *kcontrol,
  861. struct snd_ctl_elem_value *ucontrol)
  862. {
  863. struct snd_soc_component *component =
  864. snd_soc_kcontrol_component(kcontrol);
  865. struct catpt_stream_runtime *stream;
  866. struct catpt_dev *cdev = dev_get_drvdata(component->dev);
  867. bool mute;
  868. int ret;
  869. mute = (bool)ucontrol->value.integer.value[0];
  870. stream = catpt_stream_find(cdev, CATPT_PIN_ID_REFERENCE);
  871. if (!stream) {
  872. *(bool *)kcontrol->private_value = mute;
  873. return 0;
  874. }
  875. ret = pm_runtime_resume_and_get(cdev->dev);
  876. if (ret < 0 && ret != -EACCES)
  877. return ret;
  878. ret = catpt_ipc_mute_loopback(cdev, stream->info.stream_hw_id, mute);
  879. pm_runtime_mark_last_busy(cdev->dev);
  880. pm_runtime_put_autosuspend(cdev->dev);
  881. if (ret)
  882. return CATPT_IPC_ERROR(ret);
  883. *(bool *)kcontrol->private_value = mute;
  884. return 0;
  885. }
  886. static int catpt_waves_switch_get(struct snd_kcontrol *kcontrol,
  887. struct snd_ctl_elem_value *ucontrol)
  888. {
  889. return 0;
  890. }
  891. static int catpt_waves_switch_put(struct snd_kcontrol *kcontrol,
  892. struct snd_ctl_elem_value *ucontrol)
  893. {
  894. return 0;
  895. }
  896. static int catpt_waves_param_get(struct snd_kcontrol *kcontrol,
  897. unsigned int __user *bytes,
  898. unsigned int size)
  899. {
  900. return 0;
  901. }
  902. static int catpt_waves_param_put(struct snd_kcontrol *kcontrol,
  903. const unsigned int __user *bytes,
  904. unsigned int size)
  905. {
  906. return 0;
  907. }
  908. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(catpt_volume_tlv, -9000, 300, 1);
  909. #define CATPT_VOLUME_CTL(kname, sname) \
  910. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  911. .name = (kname), \
  912. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  913. SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  914. .info = catpt_volume_info, \
  915. .get = catpt_##sname##_volume_get, \
  916. .put = catpt_##sname##_volume_put, \
  917. .tlv.p = catpt_volume_tlv, \
  918. .private_value = (unsigned long) \
  919. &(long[CATPT_CHANNELS_MAX]) {0} }
  920. static const struct snd_kcontrol_new component_kcontrols[] = {
  921. /* Master volume (mixer stream) */
  922. CATPT_VOLUME_CTL("Master Playback Volume", mixer),
  923. /* Individual volume controls for offload and capture */
  924. CATPT_VOLUME_CTL("Media0 Playback Volume", offload1),
  925. CATPT_VOLUME_CTL("Media1 Playback Volume", offload2),
  926. CATPT_VOLUME_CTL("Mic Capture Volume", capture),
  927. SOC_SINGLE_BOOL_EXT("Loopback Mute", (unsigned long)&(bool[1]) {0},
  928. catpt_loopback_switch_get, catpt_loopback_switch_put),
  929. /* Enable or disable WAVES module */
  930. SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
  931. catpt_waves_switch_get, catpt_waves_switch_put),
  932. /* WAVES module parameter control */
  933. SND_SOC_BYTES_TLV("Waves Set Param", 128,
  934. catpt_waves_param_get, catpt_waves_param_put),
  935. };
  936. static const struct snd_soc_dapm_widget component_widgets[] = {
  937. SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  938. SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  939. SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
  940. SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
  941. SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  942. };
  943. static const struct snd_soc_dapm_route component_routes[] = {
  944. {"Playback VMixer", NULL, "System Playback"},
  945. {"Playback VMixer", NULL, "Offload0 Playback"},
  946. {"Playback VMixer", NULL, "Offload1 Playback"},
  947. {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
  948. {"Analog Capture", NULL, "SSP0 CODEC IN"},
  949. {"Loopback Capture", NULL, "SSP0 CODEC IN"},
  950. {"SSP1 BT OUT", NULL, "Bluetooth Playback"},
  951. {"Bluetooth Capture", NULL, "SSP1 BT IN"},
  952. };
  953. static const struct snd_soc_component_driver catpt_comp_driver = {
  954. .name = "catpt-platform",
  955. .pcm_construct = catpt_component_pcm_construct,
  956. .open = catpt_component_open,
  957. .pointer = catpt_component_pointer,
  958. .controls = component_kcontrols,
  959. .num_controls = ARRAY_SIZE(component_kcontrols),
  960. .dapm_widgets = component_widgets,
  961. .num_dapm_widgets = ARRAY_SIZE(component_widgets),
  962. .dapm_routes = component_routes,
  963. .num_dapm_routes = ARRAY_SIZE(component_routes),
  964. };
  965. int catpt_arm_stream_templates(struct catpt_dev *cdev)
  966. {
  967. struct resource *res;
  968. u32 scratch_size = 0;
  969. int i, j;
  970. for (i = 0; i < ARRAY_SIZE(catpt_topology); i++) {
  971. struct catpt_stream_template *template;
  972. struct catpt_module_entry *entry;
  973. struct catpt_module_type *type;
  974. template = catpt_topology[i];
  975. template->persistent_size = 0;
  976. for (j = 0; j < template->num_entries; j++) {
  977. entry = &template->entries[j];
  978. type = &cdev->modules[entry->module_id];
  979. if (!type->loaded)
  980. return -ENOENT;
  981. entry->entry_point = type->entry_point;
  982. template->persistent_size += type->persistent_size;
  983. if (type->scratch_size > scratch_size)
  984. scratch_size = type->scratch_size;
  985. }
  986. }
  987. if (scratch_size) {
  988. /* allocate single scratch area for all modules */
  989. res = catpt_request_region(&cdev->dram, scratch_size);
  990. if (!res)
  991. return -EBUSY;
  992. cdev->scratch = res;
  993. }
  994. return 0;
  995. }
  996. int catpt_register_plat_component(struct catpt_dev *cdev)
  997. {
  998. struct snd_soc_component *component;
  999. int ret;
  1000. component = devm_kzalloc(cdev->dev, sizeof(*component), GFP_KERNEL);
  1001. if (!component)
  1002. return -ENOMEM;
  1003. ret = snd_soc_component_initialize(component, &catpt_comp_driver,
  1004. cdev->dev);
  1005. if (ret)
  1006. return ret;
  1007. component->name = catpt_comp_driver.name;
  1008. return snd_soc_add_component(component, dai_drivers,
  1009. ARRAY_SIZE(dai_drivers));
  1010. }