soc-component.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // soc-component.c
  4. //
  5. // Copyright 2009-2011 Wolfson Microelectronics PLC.
  6. // Copyright (C) 2019 Renesas Electronics Corp.
  7. //
  8. // Mark Brown <broonie@opensource.wolfsonmicro.com>
  9. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  10. //
  11. #include <linux/module.h>
  12. #include <linux/pm_runtime.h>
  13. #include <sound/soc.h>
  14. #include <linux/bitops.h>
  15. #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret, -1)
  16. #define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret(dai, __func__, ret, reg)
  17. static inline int _soc_component_ret(struct snd_soc_component *component,
  18. const char *func, int ret, int reg)
  19. {
  20. /* Positive/Zero values are not errors */
  21. if (ret >= 0)
  22. return ret;
  23. /* Negative values might be errors */
  24. switch (ret) {
  25. case -EPROBE_DEFER:
  26. case -ENOTSUPP:
  27. break;
  28. default:
  29. if (reg == -1)
  30. dev_err(component->dev,
  31. "ASoC: error at %s on %s: %d\n",
  32. func, component->name, ret);
  33. else
  34. dev_err(component->dev,
  35. "ASoC: error at %s on %s for register: [0x%08x] %d\n",
  36. func, component->name, reg, ret);
  37. }
  38. return ret;
  39. }
  40. static inline int soc_component_field_shift(struct snd_soc_component *component,
  41. unsigned int mask)
  42. {
  43. if (!mask) {
  44. dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
  45. component->name);
  46. return 0;
  47. }
  48. return (ffs(mask) - 1);
  49. }
  50. /*
  51. * We might want to check substream by using list.
  52. * In such case, we can update these macros.
  53. */
  54. #define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
  55. #define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
  56. #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
  57. void snd_soc_component_set_aux(struct snd_soc_component *component,
  58. struct snd_soc_aux_dev *aux)
  59. {
  60. component->init = (aux) ? aux->init : NULL;
  61. }
  62. int snd_soc_component_init(struct snd_soc_component *component)
  63. {
  64. int ret = 0;
  65. if (component->init)
  66. ret = component->init(component);
  67. return soc_component_ret(component, ret);
  68. }
  69. /**
  70. * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
  71. * @component: COMPONENT
  72. * @clk_id: DAI specific clock ID
  73. * @source: Source for the clock
  74. * @freq: new clock frequency in Hz
  75. * @dir: new clock direction - input/output.
  76. *
  77. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  78. */
  79. int snd_soc_component_set_sysclk(struct snd_soc_component *component,
  80. int clk_id, int source, unsigned int freq,
  81. int dir)
  82. {
  83. int ret = -ENOTSUPP;
  84. if (component->driver->set_sysclk)
  85. ret = component->driver->set_sysclk(component, clk_id, source,
  86. freq, dir);
  87. return soc_component_ret(component, ret);
  88. }
  89. EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
  90. /*
  91. * snd_soc_component_set_pll - configure component PLL.
  92. * @component: COMPONENT
  93. * @pll_id: DAI specific PLL ID
  94. * @source: DAI specific source for the PLL
  95. * @freq_in: PLL input clock frequency in Hz
  96. * @freq_out: requested PLL output clock frequency in Hz
  97. *
  98. * Configures and enables PLL to generate output clock based on input clock.
  99. */
  100. int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
  101. int source, unsigned int freq_in,
  102. unsigned int freq_out)
  103. {
  104. int ret = -EINVAL;
  105. if (component->driver->set_pll)
  106. ret = component->driver->set_pll(component, pll_id, source,
  107. freq_in, freq_out);
  108. return soc_component_ret(component, ret);
  109. }
  110. EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
  111. void snd_soc_component_seq_notifier(struct snd_soc_component *component,
  112. enum snd_soc_dapm_type type, int subseq)
  113. {
  114. if (component->driver->seq_notifier)
  115. component->driver->seq_notifier(component, type, subseq);
  116. }
  117. int snd_soc_component_stream_event(struct snd_soc_component *component,
  118. int event)
  119. {
  120. int ret = 0;
  121. if (component->driver->stream_event)
  122. ret = component->driver->stream_event(component, event);
  123. return soc_component_ret(component, ret);
  124. }
  125. int snd_soc_component_set_bias_level(struct snd_soc_component *component,
  126. enum snd_soc_bias_level level)
  127. {
  128. int ret = 0;
  129. if (component->driver->set_bias_level)
  130. ret = component->driver->set_bias_level(component, level);
  131. return soc_component_ret(component, ret);
  132. }
  133. int snd_soc_component_enable_pin(struct snd_soc_component *component,
  134. const char *pin)
  135. {
  136. struct snd_soc_dapm_context *dapm =
  137. snd_soc_component_get_dapm(component);
  138. return snd_soc_dapm_enable_pin(dapm, pin);
  139. }
  140. EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
  141. int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
  142. const char *pin)
  143. {
  144. struct snd_soc_dapm_context *dapm =
  145. snd_soc_component_get_dapm(component);
  146. return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
  147. }
  148. EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
  149. int snd_soc_component_disable_pin(struct snd_soc_component *component,
  150. const char *pin)
  151. {
  152. struct snd_soc_dapm_context *dapm =
  153. snd_soc_component_get_dapm(component);
  154. return snd_soc_dapm_disable_pin(dapm, pin);
  155. }
  156. EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
  157. int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
  158. const char *pin)
  159. {
  160. struct snd_soc_dapm_context *dapm =
  161. snd_soc_component_get_dapm(component);
  162. return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
  163. }
  164. EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
  165. int snd_soc_component_nc_pin(struct snd_soc_component *component,
  166. const char *pin)
  167. {
  168. struct snd_soc_dapm_context *dapm =
  169. snd_soc_component_get_dapm(component);
  170. return snd_soc_dapm_nc_pin(dapm, pin);
  171. }
  172. EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
  173. int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
  174. const char *pin)
  175. {
  176. struct snd_soc_dapm_context *dapm =
  177. snd_soc_component_get_dapm(component);
  178. return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
  179. }
  180. EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
  181. int snd_soc_component_get_pin_status(struct snd_soc_component *component,
  182. const char *pin)
  183. {
  184. struct snd_soc_dapm_context *dapm =
  185. snd_soc_component_get_dapm(component);
  186. return snd_soc_dapm_get_pin_status(dapm, pin);
  187. }
  188. EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
  189. int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
  190. const char *pin)
  191. {
  192. struct snd_soc_dapm_context *dapm =
  193. snd_soc_component_get_dapm(component);
  194. return snd_soc_dapm_force_enable_pin(dapm, pin);
  195. }
  196. EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
  197. int snd_soc_component_force_enable_pin_unlocked(
  198. struct snd_soc_component *component,
  199. const char *pin)
  200. {
  201. struct snd_soc_dapm_context *dapm =
  202. snd_soc_component_get_dapm(component);
  203. return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
  204. }
  205. EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
  206. static void soc_get_kcontrol_name(struct snd_soc_component *component,
  207. char *buf, int size, const char * const ctl)
  208. {
  209. /* When updating, change also snd_soc_dapm_widget_name_cmp() */
  210. if (component->name_prefix)
  211. snprintf(buf, size, "%s %s", component->name_prefix, ctl);
  212. else
  213. snprintf(buf, size, "%s", ctl);
  214. }
  215. struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component,
  216. const char * const ctl)
  217. {
  218. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  219. soc_get_kcontrol_name(component, name, ARRAY_SIZE(name), ctl);
  220. return snd_soc_card_get_kcontrol(component->card, name);
  221. }
  222. EXPORT_SYMBOL_GPL(snd_soc_component_get_kcontrol);
  223. int snd_soc_component_notify_control(struct snd_soc_component *component,
  224. const char * const ctl)
  225. {
  226. struct snd_kcontrol *kctl;
  227. kctl = snd_soc_component_get_kcontrol(component, ctl);
  228. if (!kctl)
  229. return soc_component_ret(component, -EINVAL);
  230. snd_ctl_notify(component->card->snd_card,
  231. SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(snd_soc_component_notify_control);
  235. /**
  236. * snd_soc_component_set_jack - configure component jack.
  237. * @component: COMPONENTs
  238. * @jack: structure to use for the jack
  239. * @data: can be used if codec driver need extra data for configuring jack
  240. *
  241. * Configures and enables jack detection function.
  242. */
  243. int snd_soc_component_set_jack(struct snd_soc_component *component,
  244. struct snd_soc_jack *jack, void *data)
  245. {
  246. int ret = -ENOTSUPP;
  247. if (component->driver->set_jack)
  248. ret = component->driver->set_jack(component, jack, data);
  249. return soc_component_ret(component, ret);
  250. }
  251. EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
  252. /**
  253. * snd_soc_component_get_jack_type
  254. * @component: COMPONENTs
  255. *
  256. * Returns the jack type of the component
  257. * This can either be the supported type or one read from
  258. * devicetree with the property: jack-type.
  259. */
  260. int snd_soc_component_get_jack_type(
  261. struct snd_soc_component *component)
  262. {
  263. int ret = -ENOTSUPP;
  264. if (component->driver->get_jack_type)
  265. ret = component->driver->get_jack_type(component);
  266. return soc_component_ret(component, ret);
  267. }
  268. EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type);
  269. int snd_soc_component_module_get(struct snd_soc_component *component,
  270. void *mark, int upon_open)
  271. {
  272. int ret = 0;
  273. if (component->driver->module_get_upon_open == !!upon_open &&
  274. !try_module_get(component->dev->driver->owner))
  275. ret = -ENODEV;
  276. /* mark module if succeeded */
  277. if (ret == 0)
  278. soc_component_mark_push(component, mark, module);
  279. return soc_component_ret(component, ret);
  280. }
  281. void snd_soc_component_module_put(struct snd_soc_component *component,
  282. void *mark, int upon_open, int rollback)
  283. {
  284. if (rollback && !soc_component_mark_match(component, mark, module))
  285. return;
  286. if (component->driver->module_get_upon_open == !!upon_open)
  287. module_put(component->dev->driver->owner);
  288. /* remove the mark from module */
  289. soc_component_mark_pop(component, mark, module);
  290. }
  291. int snd_soc_component_open(struct snd_soc_component *component,
  292. struct snd_pcm_substream *substream)
  293. {
  294. int ret = 0;
  295. if (component->driver->open)
  296. ret = component->driver->open(component, substream);
  297. /* mark substream if succeeded */
  298. if (ret == 0)
  299. soc_component_mark_push(component, substream, open);
  300. return soc_component_ret(component, ret);
  301. }
  302. int snd_soc_component_close(struct snd_soc_component *component,
  303. struct snd_pcm_substream *substream,
  304. int rollback)
  305. {
  306. int ret = 0;
  307. if (rollback && !soc_component_mark_match(component, substream, open))
  308. return 0;
  309. if (component->driver->close)
  310. ret = component->driver->close(component, substream);
  311. /* remove marked substream */
  312. soc_component_mark_pop(component, substream, open);
  313. return soc_component_ret(component, ret);
  314. }
  315. void snd_soc_component_suspend(struct snd_soc_component *component)
  316. {
  317. if (component->driver->suspend)
  318. component->driver->suspend(component);
  319. component->suspended = 1;
  320. }
  321. void snd_soc_component_resume(struct snd_soc_component *component)
  322. {
  323. if (component->driver->resume)
  324. component->driver->resume(component);
  325. component->suspended = 0;
  326. }
  327. int snd_soc_component_is_suspended(struct snd_soc_component *component)
  328. {
  329. return component->suspended;
  330. }
  331. int snd_soc_component_probe(struct snd_soc_component *component)
  332. {
  333. int ret = 0;
  334. if (component->driver->probe)
  335. ret = component->driver->probe(component);
  336. return soc_component_ret(component, ret);
  337. }
  338. void snd_soc_component_remove(struct snd_soc_component *component)
  339. {
  340. if (component->driver->remove)
  341. component->driver->remove(component);
  342. }
  343. int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
  344. struct device_node *ep)
  345. {
  346. int ret = -ENOTSUPP;
  347. if (component->driver->of_xlate_dai_id)
  348. ret = component->driver->of_xlate_dai_id(component, ep);
  349. return soc_component_ret(component, ret);
  350. }
  351. int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
  352. const struct of_phandle_args *args,
  353. const char **dai_name)
  354. {
  355. if (component->driver->of_xlate_dai_name)
  356. return component->driver->of_xlate_dai_name(component,
  357. args, dai_name);
  358. /*
  359. * Don't use soc_component_ret here because we may not want to report
  360. * the error just yet. If a device has more than one component, the
  361. * first may not match and we don't want spam the log with this.
  362. */
  363. return -ENOTSUPP;
  364. }
  365. void snd_soc_component_setup_regmap(struct snd_soc_component *component)
  366. {
  367. int val_bytes = regmap_get_val_bytes(component->regmap);
  368. /* Errors are legitimate for non-integer byte multiples */
  369. if (val_bytes > 0)
  370. component->val_bytes = val_bytes;
  371. }
  372. #ifdef CONFIG_REGMAP
  373. /**
  374. * snd_soc_component_init_regmap() - Initialize regmap instance for the
  375. * component
  376. * @component: The component for which to initialize the regmap instance
  377. * @regmap: The regmap instance that should be used by the component
  378. *
  379. * This function allows deferred assignment of the regmap instance that is
  380. * associated with the component. Only use this if the regmap instance is not
  381. * yet ready when the component is registered. The function must also be called
  382. * before the first IO attempt of the component.
  383. */
  384. void snd_soc_component_init_regmap(struct snd_soc_component *component,
  385. struct regmap *regmap)
  386. {
  387. component->regmap = regmap;
  388. snd_soc_component_setup_regmap(component);
  389. }
  390. EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
  391. /**
  392. * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
  393. * component
  394. * @component: The component for which to de-initialize the regmap instance
  395. *
  396. * Calls regmap_exit() on the regmap instance associated to the component and
  397. * removes the regmap instance from the component.
  398. *
  399. * This function should only be used if snd_soc_component_init_regmap() was used
  400. * to initialize the regmap instance.
  401. */
  402. void snd_soc_component_exit_regmap(struct snd_soc_component *component)
  403. {
  404. regmap_exit(component->regmap);
  405. component->regmap = NULL;
  406. }
  407. EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
  408. #endif
  409. int snd_soc_component_compr_open(struct snd_soc_component *component,
  410. struct snd_compr_stream *cstream)
  411. {
  412. int ret = 0;
  413. if (component->driver->compress_ops &&
  414. component->driver->compress_ops->open)
  415. ret = component->driver->compress_ops->open(component, cstream);
  416. /* mark substream if succeeded */
  417. if (ret == 0)
  418. soc_component_mark_push(component, cstream, compr_open);
  419. return soc_component_ret(component, ret);
  420. }
  421. EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
  422. void snd_soc_component_compr_free(struct snd_soc_component *component,
  423. struct snd_compr_stream *cstream,
  424. int rollback)
  425. {
  426. if (rollback && !soc_component_mark_match(component, cstream, compr_open))
  427. return;
  428. if (component->driver->compress_ops &&
  429. component->driver->compress_ops->free)
  430. component->driver->compress_ops->free(component, cstream);
  431. /* remove marked substream */
  432. soc_component_mark_pop(component, cstream, compr_open);
  433. }
  434. EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
  435. int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  436. {
  437. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  438. struct snd_soc_component *component;
  439. int i, ret;
  440. for_each_rtd_components(rtd, i, component) {
  441. if (component->driver->compress_ops &&
  442. component->driver->compress_ops->trigger) {
  443. ret = component->driver->compress_ops->trigger(
  444. component, cstream, cmd);
  445. if (ret < 0)
  446. return soc_component_ret(component, ret);
  447. }
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
  452. int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
  453. struct snd_compr_params *params)
  454. {
  455. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  456. struct snd_soc_component *component;
  457. int i, ret;
  458. for_each_rtd_components(rtd, i, component) {
  459. if (component->driver->compress_ops &&
  460. component->driver->compress_ops->set_params) {
  461. ret = component->driver->compress_ops->set_params(
  462. component, cstream, params);
  463. if (ret < 0)
  464. return soc_component_ret(component, ret);
  465. }
  466. }
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
  470. int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
  471. struct snd_codec *params)
  472. {
  473. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  474. struct snd_soc_component *component;
  475. int i, ret;
  476. for_each_rtd_components(rtd, i, component) {
  477. if (component->driver->compress_ops &&
  478. component->driver->compress_ops->get_params) {
  479. ret = component->driver->compress_ops->get_params(
  480. component, cstream, params);
  481. return soc_component_ret(component, ret);
  482. }
  483. }
  484. return 0;
  485. }
  486. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
  487. int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
  488. struct snd_compr_caps *caps)
  489. {
  490. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  491. struct snd_soc_component *component;
  492. int i, ret = 0;
  493. snd_soc_dpcm_mutex_lock(rtd);
  494. for_each_rtd_components(rtd, i, component) {
  495. if (component->driver->compress_ops &&
  496. component->driver->compress_ops->get_caps) {
  497. ret = component->driver->compress_ops->get_caps(
  498. component, cstream, caps);
  499. break;
  500. }
  501. }
  502. snd_soc_dpcm_mutex_unlock(rtd);
  503. return soc_component_ret(component, ret);
  504. }
  505. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
  506. int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
  507. struct snd_compr_codec_caps *codec)
  508. {
  509. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  510. struct snd_soc_component *component;
  511. int i, ret = 0;
  512. snd_soc_dpcm_mutex_lock(rtd);
  513. for_each_rtd_components(rtd, i, component) {
  514. if (component->driver->compress_ops &&
  515. component->driver->compress_ops->get_codec_caps) {
  516. ret = component->driver->compress_ops->get_codec_caps(
  517. component, cstream, codec);
  518. break;
  519. }
  520. }
  521. snd_soc_dpcm_mutex_unlock(rtd);
  522. return soc_component_ret(component, ret);
  523. }
  524. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
  525. int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  526. {
  527. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  528. struct snd_soc_component *component;
  529. int i, ret;
  530. for_each_rtd_components(rtd, i, component) {
  531. if (component->driver->compress_ops &&
  532. component->driver->compress_ops->ack) {
  533. ret = component->driver->compress_ops->ack(
  534. component, cstream, bytes);
  535. if (ret < 0)
  536. return soc_component_ret(component, ret);
  537. }
  538. }
  539. return 0;
  540. }
  541. EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
  542. int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
  543. struct snd_compr_tstamp *tstamp)
  544. {
  545. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  546. struct snd_soc_component *component;
  547. int i, ret;
  548. for_each_rtd_components(rtd, i, component) {
  549. if (component->driver->compress_ops &&
  550. component->driver->compress_ops->pointer) {
  551. ret = component->driver->compress_ops->pointer(
  552. component, cstream, tstamp);
  553. return soc_component_ret(component, ret);
  554. }
  555. }
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
  559. int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
  560. char __user *buf, size_t count)
  561. {
  562. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  563. struct snd_soc_component *component;
  564. int i, ret = 0;
  565. snd_soc_dpcm_mutex_lock(rtd);
  566. for_each_rtd_components(rtd, i, component) {
  567. if (component->driver->compress_ops &&
  568. component->driver->compress_ops->copy) {
  569. ret = component->driver->compress_ops->copy(
  570. component, cstream, buf, count);
  571. break;
  572. }
  573. }
  574. snd_soc_dpcm_mutex_unlock(rtd);
  575. return soc_component_ret(component, ret);
  576. }
  577. EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
  578. int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
  579. struct snd_compr_metadata *metadata)
  580. {
  581. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  582. struct snd_soc_component *component;
  583. int i, ret;
  584. for_each_rtd_components(rtd, i, component) {
  585. if (component->driver->compress_ops &&
  586. component->driver->compress_ops->set_metadata) {
  587. ret = component->driver->compress_ops->set_metadata(
  588. component, cstream, metadata);
  589. if (ret < 0)
  590. return soc_component_ret(component, ret);
  591. }
  592. }
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
  596. int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
  597. struct snd_compr_metadata *metadata)
  598. {
  599. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  600. struct snd_soc_component *component;
  601. int i, ret;
  602. for_each_rtd_components(rtd, i, component) {
  603. if (component->driver->compress_ops &&
  604. component->driver->compress_ops->get_metadata) {
  605. ret = component->driver->compress_ops->get_metadata(
  606. component, cstream, metadata);
  607. return soc_component_ret(component, ret);
  608. }
  609. }
  610. return 0;
  611. }
  612. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
  613. static unsigned int soc_component_read_no_lock(
  614. struct snd_soc_component *component,
  615. unsigned int reg)
  616. {
  617. int ret;
  618. unsigned int val = 0;
  619. if (component->regmap)
  620. ret = regmap_read(component->regmap, reg, &val);
  621. else if (component->driver->read) {
  622. ret = 0;
  623. val = component->driver->read(component, reg);
  624. }
  625. else
  626. ret = -EIO;
  627. if (ret < 0)
  628. return soc_component_ret_reg_rw(component, ret, reg);
  629. return val;
  630. }
  631. /**
  632. * snd_soc_component_read() - Read register value
  633. * @component: Component to read from
  634. * @reg: Register to read
  635. *
  636. * Return: read value
  637. */
  638. unsigned int snd_soc_component_read(struct snd_soc_component *component,
  639. unsigned int reg)
  640. {
  641. unsigned int val;
  642. mutex_lock(&component->io_mutex);
  643. val = soc_component_read_no_lock(component, reg);
  644. mutex_unlock(&component->io_mutex);
  645. return val;
  646. }
  647. EXPORT_SYMBOL_GPL(snd_soc_component_read);
  648. static int soc_component_write_no_lock(
  649. struct snd_soc_component *component,
  650. unsigned int reg, unsigned int val)
  651. {
  652. int ret = -EIO;
  653. if (component->regmap)
  654. ret = regmap_write(component->regmap, reg, val);
  655. else if (component->driver->write)
  656. ret = component->driver->write(component, reg, val);
  657. return soc_component_ret_reg_rw(component, ret, reg);
  658. }
  659. /**
  660. * snd_soc_component_write() - Write register value
  661. * @component: Component to write to
  662. * @reg: Register to write
  663. * @val: Value to write to the register
  664. *
  665. * Return: 0 on success, a negative error code otherwise.
  666. */
  667. int snd_soc_component_write(struct snd_soc_component *component,
  668. unsigned int reg, unsigned int val)
  669. {
  670. int ret;
  671. mutex_lock(&component->io_mutex);
  672. ret = soc_component_write_no_lock(component, reg, val);
  673. mutex_unlock(&component->io_mutex);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL_GPL(snd_soc_component_write);
  677. static int snd_soc_component_update_bits_legacy(
  678. struct snd_soc_component *component, unsigned int reg,
  679. unsigned int mask, unsigned int val, bool *change)
  680. {
  681. unsigned int old, new;
  682. int ret = 0;
  683. mutex_lock(&component->io_mutex);
  684. old = soc_component_read_no_lock(component, reg);
  685. new = (old & ~mask) | (val & mask);
  686. *change = old != new;
  687. if (*change)
  688. ret = soc_component_write_no_lock(component, reg, new);
  689. mutex_unlock(&component->io_mutex);
  690. return soc_component_ret_reg_rw(component, ret, reg);
  691. }
  692. /**
  693. * snd_soc_component_update_bits() - Perform read/modify/write cycle
  694. * @component: Component to update
  695. * @reg: Register to update
  696. * @mask: Mask that specifies which bits to update
  697. * @val: New value for the bits specified by mask
  698. *
  699. * Return: 1 if the operation was successful and the value of the register
  700. * changed, 0 if the operation was successful, but the value did not change.
  701. * Returns a negative error code otherwise.
  702. */
  703. int snd_soc_component_update_bits(struct snd_soc_component *component,
  704. unsigned int reg, unsigned int mask, unsigned int val)
  705. {
  706. bool change;
  707. int ret;
  708. if (component->regmap)
  709. ret = regmap_update_bits_check(component->regmap, reg, mask,
  710. val, &change);
  711. else
  712. ret = snd_soc_component_update_bits_legacy(component, reg,
  713. mask, val, &change);
  714. if (ret < 0)
  715. return soc_component_ret_reg_rw(component, ret, reg);
  716. return change;
  717. }
  718. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
  719. /**
  720. * snd_soc_component_update_bits_async() - Perform asynchronous
  721. * read/modify/write cycle
  722. * @component: Component to update
  723. * @reg: Register to update
  724. * @mask: Mask that specifies which bits to update
  725. * @val: New value for the bits specified by mask
  726. *
  727. * This function is similar to snd_soc_component_update_bits(), but the update
  728. * operation is scheduled asynchronously. This means it may not be completed
  729. * when the function returns. To make sure that all scheduled updates have been
  730. * completed snd_soc_component_async_complete() must be called.
  731. *
  732. * Return: 1 if the operation was successful and the value of the register
  733. * changed, 0 if the operation was successful, but the value did not change.
  734. * Returns a negative error code otherwise.
  735. */
  736. int snd_soc_component_update_bits_async(struct snd_soc_component *component,
  737. unsigned int reg, unsigned int mask, unsigned int val)
  738. {
  739. bool change;
  740. int ret;
  741. if (component->regmap)
  742. ret = regmap_update_bits_check_async(component->regmap, reg,
  743. mask, val, &change);
  744. else
  745. ret = snd_soc_component_update_bits_legacy(component, reg,
  746. mask, val, &change);
  747. if (ret < 0)
  748. return soc_component_ret_reg_rw(component, ret, reg);
  749. return change;
  750. }
  751. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
  752. /**
  753. * snd_soc_component_read_field() - Read register field value
  754. * @component: Component to read from
  755. * @reg: Register to read
  756. * @mask: mask of the register field
  757. *
  758. * Return: read value of register field.
  759. */
  760. unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
  761. unsigned int reg, unsigned int mask)
  762. {
  763. unsigned int val;
  764. val = snd_soc_component_read(component, reg);
  765. val = (val & mask) >> soc_component_field_shift(component, mask);
  766. return val;
  767. }
  768. EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
  769. /**
  770. * snd_soc_component_write_field() - write to register field
  771. * @component: Component to write to
  772. * @reg: Register to write
  773. * @mask: mask of the register field to update
  774. * @val: value of the field to write
  775. *
  776. * Return: 1 for change, otherwise 0.
  777. */
  778. int snd_soc_component_write_field(struct snd_soc_component *component,
  779. unsigned int reg, unsigned int mask,
  780. unsigned int val)
  781. {
  782. val = (val << soc_component_field_shift(component, mask)) & mask;
  783. return snd_soc_component_update_bits(component, reg, mask, val);
  784. }
  785. EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
  786. /**
  787. * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
  788. * @component: Component for which to wait
  789. *
  790. * This function blocks until all asynchronous I/O which has previously been
  791. * scheduled using snd_soc_component_update_bits_async() has completed.
  792. */
  793. void snd_soc_component_async_complete(struct snd_soc_component *component)
  794. {
  795. if (component->regmap)
  796. regmap_async_complete(component->regmap);
  797. }
  798. EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
  799. /**
  800. * snd_soc_component_test_bits - Test register for change
  801. * @component: component
  802. * @reg: Register to test
  803. * @mask: Mask that specifies which bits to test
  804. * @value: Value to test against
  805. *
  806. * Tests a register with a new value and checks if the new value is
  807. * different from the old value.
  808. *
  809. * Return: 1 for change, otherwise 0.
  810. */
  811. int snd_soc_component_test_bits(struct snd_soc_component *component,
  812. unsigned int reg, unsigned int mask, unsigned int value)
  813. {
  814. unsigned int old, new;
  815. old = snd_soc_component_read(component, reg);
  816. new = (old & ~mask) | value;
  817. return old != new;
  818. }
  819. EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
  820. int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
  821. {
  822. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  823. struct snd_soc_component *component;
  824. int i;
  825. /* FIXME: use 1st pointer */
  826. for_each_rtd_components(rtd, i, component)
  827. if (component->driver->pointer)
  828. return component->driver->pointer(component, substream);
  829. return 0;
  830. }
  831. static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
  832. struct snd_soc_component *component)
  833. {
  834. struct snd_soc_dai *dai;
  835. int i;
  836. for_each_rtd_codec_dais(rtd, i, dai) {
  837. if (dai->component == component)
  838. return true;
  839. }
  840. return false;
  841. }
  842. void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
  843. snd_pcm_sframes_t *cpu_delay,
  844. snd_pcm_sframes_t *codec_delay)
  845. {
  846. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  847. struct snd_soc_component *component;
  848. snd_pcm_sframes_t delay;
  849. int i;
  850. /*
  851. * We're looking for the delay through the full audio path so it needs to
  852. * be the maximum of the Components doing transmit and the maximum of the
  853. * Components doing receive (ie, all CPUs and all CODECs) rather than
  854. * just the maximum of all Components.
  855. */
  856. for_each_rtd_components(rtd, i, component) {
  857. if (!component->driver->delay)
  858. continue;
  859. delay = component->driver->delay(component, substream);
  860. if (snd_soc_component_is_codec_on_rtd(rtd, component))
  861. *codec_delay = max(*codec_delay, delay);
  862. else
  863. *cpu_delay = max(*cpu_delay, delay);
  864. }
  865. }
  866. int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
  867. unsigned int cmd, void *arg)
  868. {
  869. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  870. struct snd_soc_component *component;
  871. int i;
  872. /* FIXME: use 1st ioctl */
  873. for_each_rtd_components(rtd, i, component)
  874. if (component->driver->ioctl)
  875. return soc_component_ret(
  876. component,
  877. component->driver->ioctl(component,
  878. substream, cmd, arg));
  879. return snd_pcm_lib_ioctl(substream, cmd, arg);
  880. }
  881. int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
  882. {
  883. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  884. struct snd_soc_component *component;
  885. int i, ret;
  886. for_each_rtd_components(rtd, i, component) {
  887. if (component->driver->sync_stop) {
  888. ret = component->driver->sync_stop(component,
  889. substream);
  890. if (ret < 0)
  891. return soc_component_ret(component, ret);
  892. }
  893. }
  894. return 0;
  895. }
  896. int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream,
  897. int channel, unsigned long pos,
  898. struct iov_iter *iter, unsigned long bytes)
  899. {
  900. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  901. struct snd_soc_component *component;
  902. int i;
  903. /* FIXME. it returns 1st copy now */
  904. for_each_rtd_components(rtd, i, component)
  905. if (component->driver->copy)
  906. return soc_component_ret(component,
  907. component->driver->copy(component, substream,
  908. channel, pos, iter, bytes));
  909. return -EINVAL;
  910. }
  911. struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
  912. unsigned long offset)
  913. {
  914. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  915. struct snd_soc_component *component;
  916. struct page *page;
  917. int i;
  918. /* FIXME. it returns 1st page now */
  919. for_each_rtd_components(rtd, i, component) {
  920. if (component->driver->page) {
  921. page = component->driver->page(component,
  922. substream, offset);
  923. if (page)
  924. return page;
  925. }
  926. }
  927. return NULL;
  928. }
  929. int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
  930. struct vm_area_struct *vma)
  931. {
  932. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  933. struct snd_soc_component *component;
  934. int i;
  935. /* FIXME. it returns 1st mmap now */
  936. for_each_rtd_components(rtd, i, component)
  937. if (component->driver->mmap)
  938. return soc_component_ret(
  939. component,
  940. component->driver->mmap(component,
  941. substream, vma));
  942. return -EINVAL;
  943. }
  944. int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
  945. {
  946. struct snd_soc_component *component;
  947. int ret;
  948. int i;
  949. for_each_rtd_components(rtd, i, component) {
  950. if (component->driver->pcm_construct) {
  951. ret = component->driver->pcm_construct(component, rtd);
  952. if (ret < 0)
  953. return soc_component_ret(component, ret);
  954. }
  955. }
  956. return 0;
  957. }
  958. void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
  959. {
  960. struct snd_soc_component *component;
  961. int i;
  962. if (!rtd->pcm)
  963. return;
  964. for_each_rtd_components(rtd, i, component)
  965. if (component->driver->pcm_destruct)
  966. component->driver->pcm_destruct(component, rtd->pcm);
  967. }
  968. int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
  969. {
  970. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  971. struct snd_soc_component *component;
  972. int i, ret;
  973. for_each_rtd_components(rtd, i, component) {
  974. if (component->driver->prepare) {
  975. ret = component->driver->prepare(component, substream);
  976. if (ret < 0)
  977. return soc_component_ret(component, ret);
  978. }
  979. }
  980. return 0;
  981. }
  982. int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
  983. struct snd_pcm_hw_params *params)
  984. {
  985. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  986. struct snd_soc_component *component;
  987. int i, ret;
  988. for_each_rtd_components(rtd, i, component) {
  989. if (component->driver->hw_params) {
  990. ret = component->driver->hw_params(component,
  991. substream, params);
  992. if (ret < 0)
  993. return soc_component_ret(component, ret);
  994. }
  995. /* mark substream if succeeded */
  996. soc_component_mark_push(component, substream, hw_params);
  997. }
  998. return 0;
  999. }
  1000. void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
  1001. int rollback)
  1002. {
  1003. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  1004. struct snd_soc_component *component;
  1005. int i, ret;
  1006. for_each_rtd_components(rtd, i, component) {
  1007. if (rollback && !soc_component_mark_match(component, substream, hw_params))
  1008. continue;
  1009. if (component->driver->hw_free) {
  1010. ret = component->driver->hw_free(component, substream);
  1011. if (ret < 0)
  1012. soc_component_ret(component, ret);
  1013. }
  1014. /* remove marked substream */
  1015. soc_component_mark_pop(component, substream, hw_params);
  1016. }
  1017. }
  1018. static int soc_component_trigger(struct snd_soc_component *component,
  1019. struct snd_pcm_substream *substream,
  1020. int cmd)
  1021. {
  1022. int ret = 0;
  1023. if (component->driver->trigger)
  1024. ret = component->driver->trigger(component, substream, cmd);
  1025. return soc_component_ret(component, ret);
  1026. }
  1027. int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
  1028. int cmd, int rollback)
  1029. {
  1030. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  1031. struct snd_soc_component *component;
  1032. int i, r, ret = 0;
  1033. switch (cmd) {
  1034. case SNDRV_PCM_TRIGGER_START:
  1035. case SNDRV_PCM_TRIGGER_RESUME:
  1036. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1037. for_each_rtd_components(rtd, i, component) {
  1038. ret = soc_component_trigger(component, substream, cmd);
  1039. if (ret < 0)
  1040. break;
  1041. soc_component_mark_push(component, substream, trigger);
  1042. }
  1043. break;
  1044. case SNDRV_PCM_TRIGGER_STOP:
  1045. case SNDRV_PCM_TRIGGER_SUSPEND:
  1046. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1047. for_each_rtd_components(rtd, i, component) {
  1048. if (rollback && !soc_component_mark_match(component, substream, trigger))
  1049. continue;
  1050. r = soc_component_trigger(component, substream, cmd);
  1051. if (r < 0)
  1052. ret = r; /* use last ret */
  1053. soc_component_mark_pop(component, substream, trigger);
  1054. }
  1055. }
  1056. return ret;
  1057. }
  1058. int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
  1059. void *stream)
  1060. {
  1061. struct snd_soc_component *component;
  1062. int i;
  1063. for_each_rtd_components(rtd, i, component) {
  1064. int ret = pm_runtime_get_sync(component->dev);
  1065. if (ret < 0 && ret != -EACCES) {
  1066. pm_runtime_put_noidle(component->dev);
  1067. return soc_component_ret(component, ret);
  1068. }
  1069. /* mark stream if succeeded */
  1070. soc_component_mark_push(component, stream, pm);
  1071. }
  1072. return 0;
  1073. }
  1074. void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
  1075. void *stream, int rollback)
  1076. {
  1077. struct snd_soc_component *component;
  1078. int i;
  1079. for_each_rtd_components(rtd, i, component) {
  1080. if (rollback && !soc_component_mark_match(component, stream, pm))
  1081. continue;
  1082. pm_runtime_mark_last_busy(component->dev);
  1083. pm_runtime_put_autosuspend(component->dev);
  1084. /* remove marked stream */
  1085. soc_component_mark_pop(component, stream, pm);
  1086. }
  1087. }
  1088. int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
  1089. {
  1090. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  1091. struct snd_soc_component *component;
  1092. int i;
  1093. /* FIXME: use 1st pointer */
  1094. for_each_rtd_components(rtd, i, component)
  1095. if (component->driver->ack)
  1096. return component->driver->ack(component, substream);
  1097. return 0;
  1098. }