soc-component.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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 <[email protected]>
  9. // Kuninori Morimoto <[email protected]>
  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. /**
  207. * snd_soc_component_set_jack - configure component jack.
  208. * @component: COMPONENTs
  209. * @jack: structure to use for the jack
  210. * @data: can be used if codec driver need extra data for configuring jack
  211. *
  212. * Configures and enables jack detection function.
  213. */
  214. int snd_soc_component_set_jack(struct snd_soc_component *component,
  215. struct snd_soc_jack *jack, void *data)
  216. {
  217. int ret = -ENOTSUPP;
  218. if (component->driver->set_jack)
  219. ret = component->driver->set_jack(component, jack, data);
  220. return soc_component_ret(component, ret);
  221. }
  222. EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
  223. int snd_soc_component_module_get(struct snd_soc_component *component,
  224. void *mark, int upon_open)
  225. {
  226. int ret = 0;
  227. if (component->driver->module_get_upon_open == !!upon_open &&
  228. !try_module_get(component->dev->driver->owner))
  229. ret = -ENODEV;
  230. /* mark module if succeeded */
  231. if (ret == 0)
  232. soc_component_mark_push(component, mark, module);
  233. return soc_component_ret(component, ret);
  234. }
  235. void snd_soc_component_module_put(struct snd_soc_component *component,
  236. void *mark, int upon_open, int rollback)
  237. {
  238. if (rollback && !soc_component_mark_match(component, mark, module))
  239. return;
  240. if (component->driver->module_get_upon_open == !!upon_open)
  241. module_put(component->dev->driver->owner);
  242. /* remove the mark from module */
  243. soc_component_mark_pop(component, mark, module);
  244. }
  245. int snd_soc_component_open(struct snd_soc_component *component,
  246. struct snd_pcm_substream *substream)
  247. {
  248. int ret = 0;
  249. if (component->driver->open)
  250. ret = component->driver->open(component, substream);
  251. /* mark substream if succeeded */
  252. if (ret == 0)
  253. soc_component_mark_push(component, substream, open);
  254. return soc_component_ret(component, ret);
  255. }
  256. int snd_soc_component_close(struct snd_soc_component *component,
  257. struct snd_pcm_substream *substream,
  258. int rollback)
  259. {
  260. int ret = 0;
  261. if (rollback && !soc_component_mark_match(component, substream, open))
  262. return 0;
  263. if (component->driver->close)
  264. ret = component->driver->close(component, substream);
  265. /* remove marked substream */
  266. soc_component_mark_pop(component, substream, open);
  267. return soc_component_ret(component, ret);
  268. }
  269. void snd_soc_component_suspend(struct snd_soc_component *component)
  270. {
  271. if (component->driver->suspend)
  272. component->driver->suspend(component);
  273. component->suspended = 1;
  274. }
  275. void snd_soc_component_resume(struct snd_soc_component *component)
  276. {
  277. if (component->driver->resume)
  278. component->driver->resume(component);
  279. component->suspended = 0;
  280. }
  281. int snd_soc_component_is_suspended(struct snd_soc_component *component)
  282. {
  283. return component->suspended;
  284. }
  285. int snd_soc_component_probe(struct snd_soc_component *component)
  286. {
  287. int ret = 0;
  288. if (component->driver->probe)
  289. ret = component->driver->probe(component);
  290. return soc_component_ret(component, ret);
  291. }
  292. void snd_soc_component_remove(struct snd_soc_component *component)
  293. {
  294. if (component->driver->remove)
  295. component->driver->remove(component);
  296. }
  297. int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
  298. struct device_node *ep)
  299. {
  300. int ret = -ENOTSUPP;
  301. if (component->driver->of_xlate_dai_id)
  302. ret = component->driver->of_xlate_dai_id(component, ep);
  303. return soc_component_ret(component, ret);
  304. }
  305. int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
  306. const struct of_phandle_args *args,
  307. const char **dai_name)
  308. {
  309. if (component->driver->of_xlate_dai_name)
  310. return component->driver->of_xlate_dai_name(component,
  311. args, dai_name);
  312. /*
  313. * Don't use soc_component_ret here because we may not want to report
  314. * the error just yet. If a device has more than one component, the
  315. * first may not match and we don't want spam the log with this.
  316. */
  317. return -ENOTSUPP;
  318. }
  319. void snd_soc_component_setup_regmap(struct snd_soc_component *component)
  320. {
  321. int val_bytes = regmap_get_val_bytes(component->regmap);
  322. /* Errors are legitimate for non-integer byte multiples */
  323. if (val_bytes > 0)
  324. component->val_bytes = val_bytes;
  325. }
  326. #ifdef CONFIG_REGMAP
  327. /**
  328. * snd_soc_component_init_regmap() - Initialize regmap instance for the
  329. * component
  330. * @component: The component for which to initialize the regmap instance
  331. * @regmap: The regmap instance that should be used by the component
  332. *
  333. * This function allows deferred assignment of the regmap instance that is
  334. * associated with the component. Only use this if the regmap instance is not
  335. * yet ready when the component is registered. The function must also be called
  336. * before the first IO attempt of the component.
  337. */
  338. void snd_soc_component_init_regmap(struct snd_soc_component *component,
  339. struct regmap *regmap)
  340. {
  341. component->regmap = regmap;
  342. snd_soc_component_setup_regmap(component);
  343. }
  344. EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
  345. /**
  346. * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
  347. * component
  348. * @component: The component for which to de-initialize the regmap instance
  349. *
  350. * Calls regmap_exit() on the regmap instance associated to the component and
  351. * removes the regmap instance from the component.
  352. *
  353. * This function should only be used if snd_soc_component_init_regmap() was used
  354. * to initialize the regmap instance.
  355. */
  356. void snd_soc_component_exit_regmap(struct snd_soc_component *component)
  357. {
  358. regmap_exit(component->regmap);
  359. component->regmap = NULL;
  360. }
  361. EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
  362. #endif
  363. int snd_soc_component_compr_open(struct snd_soc_component *component,
  364. struct snd_compr_stream *cstream)
  365. {
  366. int ret = 0;
  367. if (component->driver->compress_ops &&
  368. component->driver->compress_ops->open)
  369. ret = component->driver->compress_ops->open(component, cstream);
  370. /* mark substream if succeeded */
  371. if (ret == 0)
  372. soc_component_mark_push(component, cstream, compr_open);
  373. return soc_component_ret(component, ret);
  374. }
  375. EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
  376. void snd_soc_component_compr_free(struct snd_soc_component *component,
  377. struct snd_compr_stream *cstream,
  378. int rollback)
  379. {
  380. if (rollback && !soc_component_mark_match(component, cstream, compr_open))
  381. return;
  382. if (component->driver->compress_ops &&
  383. component->driver->compress_ops->free)
  384. component->driver->compress_ops->free(component, cstream);
  385. /* remove marked substream */
  386. soc_component_mark_pop(component, cstream, compr_open);
  387. }
  388. EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
  389. int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  390. {
  391. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  392. struct snd_soc_component *component;
  393. int i, ret;
  394. for_each_rtd_components(rtd, i, component) {
  395. if (component->driver->compress_ops &&
  396. component->driver->compress_ops->trigger) {
  397. ret = component->driver->compress_ops->trigger(
  398. component, cstream, cmd);
  399. if (ret < 0)
  400. return soc_component_ret(component, ret);
  401. }
  402. }
  403. return 0;
  404. }
  405. EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
  406. int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
  407. struct snd_compr_params *params)
  408. {
  409. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  410. struct snd_soc_component *component;
  411. int i, ret;
  412. for_each_rtd_components(rtd, i, component) {
  413. if (component->driver->compress_ops &&
  414. component->driver->compress_ops->set_params) {
  415. ret = component->driver->compress_ops->set_params(
  416. component, cstream, params);
  417. if (ret < 0)
  418. return soc_component_ret(component, ret);
  419. }
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
  424. int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
  425. struct snd_codec *params)
  426. {
  427. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  428. struct snd_soc_component *component;
  429. int i, ret;
  430. for_each_rtd_components(rtd, i, component) {
  431. if (component->driver->compress_ops &&
  432. component->driver->compress_ops->get_params) {
  433. ret = component->driver->compress_ops->get_params(
  434. component, cstream, params);
  435. return soc_component_ret(component, ret);
  436. }
  437. }
  438. return 0;
  439. }
  440. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
  441. int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
  442. struct snd_compr_caps *caps)
  443. {
  444. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  445. struct snd_soc_component *component;
  446. int i, ret = 0;
  447. snd_soc_dpcm_mutex_lock(rtd);
  448. for_each_rtd_components(rtd, i, component) {
  449. if (component->driver->compress_ops &&
  450. component->driver->compress_ops->get_caps) {
  451. ret = component->driver->compress_ops->get_caps(
  452. component, cstream, caps);
  453. break;
  454. }
  455. }
  456. snd_soc_dpcm_mutex_unlock(rtd);
  457. return soc_component_ret(component, ret);
  458. }
  459. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
  460. int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
  461. struct snd_compr_codec_caps *codec)
  462. {
  463. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  464. struct snd_soc_component *component;
  465. int i, ret = 0;
  466. snd_soc_dpcm_mutex_lock(rtd);
  467. for_each_rtd_components(rtd, i, component) {
  468. if (component->driver->compress_ops &&
  469. component->driver->compress_ops->get_codec_caps) {
  470. ret = component->driver->compress_ops->get_codec_caps(
  471. component, cstream, codec);
  472. break;
  473. }
  474. }
  475. snd_soc_dpcm_mutex_unlock(rtd);
  476. return soc_component_ret(component, ret);
  477. }
  478. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
  479. int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  480. {
  481. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  482. struct snd_soc_component *component;
  483. int i, ret;
  484. for_each_rtd_components(rtd, i, component) {
  485. if (component->driver->compress_ops &&
  486. component->driver->compress_ops->ack) {
  487. ret = component->driver->compress_ops->ack(
  488. component, cstream, bytes);
  489. if (ret < 0)
  490. return soc_component_ret(component, ret);
  491. }
  492. }
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
  496. int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
  497. struct snd_compr_tstamp *tstamp)
  498. {
  499. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  500. struct snd_soc_component *component;
  501. int i, ret;
  502. for_each_rtd_components(rtd, i, component) {
  503. if (component->driver->compress_ops &&
  504. component->driver->compress_ops->pointer) {
  505. ret = component->driver->compress_ops->pointer(
  506. component, cstream, tstamp);
  507. return soc_component_ret(component, ret);
  508. }
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
  513. int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
  514. char __user *buf, size_t count)
  515. {
  516. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  517. struct snd_soc_component *component;
  518. int i, ret = 0;
  519. snd_soc_dpcm_mutex_lock(rtd);
  520. for_each_rtd_components(rtd, i, component) {
  521. if (component->driver->compress_ops &&
  522. component->driver->compress_ops->copy) {
  523. ret = component->driver->compress_ops->copy(
  524. component, cstream, buf, count);
  525. break;
  526. }
  527. }
  528. snd_soc_dpcm_mutex_unlock(rtd);
  529. return soc_component_ret(component, ret);
  530. }
  531. EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
  532. int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
  533. struct snd_compr_metadata *metadata)
  534. {
  535. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  536. struct snd_soc_component *component;
  537. int i, ret;
  538. for_each_rtd_components(rtd, i, component) {
  539. if (component->driver->compress_ops &&
  540. component->driver->compress_ops->set_metadata) {
  541. ret = component->driver->compress_ops->set_metadata(
  542. component, cstream, metadata);
  543. if (ret < 0)
  544. return soc_component_ret(component, ret);
  545. }
  546. }
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
  550. int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
  551. struct snd_compr_metadata *metadata)
  552. {
  553. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  554. struct snd_soc_component *component;
  555. int i, ret;
  556. for_each_rtd_components(rtd, i, component) {
  557. if (component->driver->compress_ops &&
  558. component->driver->compress_ops->get_metadata) {
  559. ret = component->driver->compress_ops->get_metadata(
  560. component, cstream, metadata);
  561. return soc_component_ret(component, ret);
  562. }
  563. }
  564. return 0;
  565. }
  566. EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
  567. static unsigned int soc_component_read_no_lock(
  568. struct snd_soc_component *component,
  569. unsigned int reg)
  570. {
  571. int ret;
  572. unsigned int val = 0;
  573. if (component->regmap)
  574. ret = regmap_read(component->regmap, reg, &val);
  575. else if (component->driver->read) {
  576. ret = 0;
  577. val = component->driver->read(component, reg);
  578. }
  579. else
  580. ret = -EIO;
  581. if (ret < 0)
  582. return soc_component_ret_reg_rw(component, ret, reg);
  583. return val;
  584. }
  585. /**
  586. * snd_soc_component_read() - Read register value
  587. * @component: Component to read from
  588. * @reg: Register to read
  589. *
  590. * Return: read value
  591. */
  592. unsigned int snd_soc_component_read(struct snd_soc_component *component,
  593. unsigned int reg)
  594. {
  595. unsigned int val;
  596. mutex_lock(&component->io_mutex);
  597. val = soc_component_read_no_lock(component, reg);
  598. mutex_unlock(&component->io_mutex);
  599. return val;
  600. }
  601. EXPORT_SYMBOL_GPL(snd_soc_component_read);
  602. static int soc_component_write_no_lock(
  603. struct snd_soc_component *component,
  604. unsigned int reg, unsigned int val)
  605. {
  606. int ret = -EIO;
  607. if (component->regmap)
  608. ret = regmap_write(component->regmap, reg, val);
  609. else if (component->driver->write)
  610. ret = component->driver->write(component, reg, val);
  611. return soc_component_ret_reg_rw(component, ret, reg);
  612. }
  613. /**
  614. * snd_soc_component_write() - Write register value
  615. * @component: Component to write to
  616. * @reg: Register to write
  617. * @val: Value to write to the register
  618. *
  619. * Return: 0 on success, a negative error code otherwise.
  620. */
  621. int snd_soc_component_write(struct snd_soc_component *component,
  622. unsigned int reg, unsigned int val)
  623. {
  624. int ret;
  625. mutex_lock(&component->io_mutex);
  626. ret = soc_component_write_no_lock(component, reg, val);
  627. mutex_unlock(&component->io_mutex);
  628. return ret;
  629. }
  630. EXPORT_SYMBOL_GPL(snd_soc_component_write);
  631. static int snd_soc_component_update_bits_legacy(
  632. struct snd_soc_component *component, unsigned int reg,
  633. unsigned int mask, unsigned int val, bool *change)
  634. {
  635. unsigned int old, new;
  636. int ret = 0;
  637. mutex_lock(&component->io_mutex);
  638. old = soc_component_read_no_lock(component, reg);
  639. new = (old & ~mask) | (val & mask);
  640. *change = old != new;
  641. if (*change)
  642. ret = soc_component_write_no_lock(component, reg, new);
  643. mutex_unlock(&component->io_mutex);
  644. return soc_component_ret_reg_rw(component, ret, reg);
  645. }
  646. /**
  647. * snd_soc_component_update_bits() - Perform read/modify/write cycle
  648. * @component: Component to update
  649. * @reg: Register to update
  650. * @mask: Mask that specifies which bits to update
  651. * @val: New value for the bits specified by mask
  652. *
  653. * Return: 1 if the operation was successful and the value of the register
  654. * changed, 0 if the operation was successful, but the value did not change.
  655. * Returns a negative error code otherwise.
  656. */
  657. int snd_soc_component_update_bits(struct snd_soc_component *component,
  658. unsigned int reg, unsigned int mask, unsigned int val)
  659. {
  660. bool change;
  661. int ret;
  662. if (component->regmap)
  663. ret = regmap_update_bits_check(component->regmap, reg, mask,
  664. val, &change);
  665. else
  666. ret = snd_soc_component_update_bits_legacy(component, reg,
  667. mask, val, &change);
  668. if (ret < 0)
  669. return soc_component_ret_reg_rw(component, ret, reg);
  670. return change;
  671. }
  672. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
  673. /**
  674. * snd_soc_component_update_bits_async() - Perform asynchronous
  675. * read/modify/write cycle
  676. * @component: Component to update
  677. * @reg: Register to update
  678. * @mask: Mask that specifies which bits to update
  679. * @val: New value for the bits specified by mask
  680. *
  681. * This function is similar to snd_soc_component_update_bits(), but the update
  682. * operation is scheduled asynchronously. This means it may not be completed
  683. * when the function returns. To make sure that all scheduled updates have been
  684. * completed snd_soc_component_async_complete() must be called.
  685. *
  686. * Return: 1 if the operation was successful and the value of the register
  687. * changed, 0 if the operation was successful, but the value did not change.
  688. * Returns a negative error code otherwise.
  689. */
  690. int snd_soc_component_update_bits_async(struct snd_soc_component *component,
  691. unsigned int reg, unsigned int mask, unsigned int val)
  692. {
  693. bool change;
  694. int ret;
  695. if (component->regmap)
  696. ret = regmap_update_bits_check_async(component->regmap, reg,
  697. mask, val, &change);
  698. else
  699. ret = snd_soc_component_update_bits_legacy(component, reg,
  700. mask, val, &change);
  701. if (ret < 0)
  702. return soc_component_ret_reg_rw(component, ret, reg);
  703. return change;
  704. }
  705. EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
  706. /**
  707. * snd_soc_component_read_field() - Read register field value
  708. * @component: Component to read from
  709. * @reg: Register to read
  710. * @mask: mask of the register field
  711. *
  712. * Return: read value of register field.
  713. */
  714. unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
  715. unsigned int reg, unsigned int mask)
  716. {
  717. unsigned int val;
  718. val = snd_soc_component_read(component, reg);
  719. val = (val & mask) >> soc_component_field_shift(component, mask);
  720. return val;
  721. }
  722. EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
  723. /**
  724. * snd_soc_component_write_field() - write to register field
  725. * @component: Component to write to
  726. * @reg: Register to write
  727. * @mask: mask of the register field to update
  728. * @val: value of the field to write
  729. *
  730. * Return: 1 for change, otherwise 0.
  731. */
  732. int snd_soc_component_write_field(struct snd_soc_component *component,
  733. unsigned int reg, unsigned int mask,
  734. unsigned int val)
  735. {
  736. val = (val << soc_component_field_shift(component, mask)) & mask;
  737. return snd_soc_component_update_bits(component, reg, mask, val);
  738. }
  739. EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
  740. /**
  741. * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
  742. * @component: Component for which to wait
  743. *
  744. * This function blocks until all asynchronous I/O which has previously been
  745. * scheduled using snd_soc_component_update_bits_async() has completed.
  746. */
  747. void snd_soc_component_async_complete(struct snd_soc_component *component)
  748. {
  749. if (component->regmap)
  750. regmap_async_complete(component->regmap);
  751. }
  752. EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
  753. /**
  754. * snd_soc_component_test_bits - Test register for change
  755. * @component: component
  756. * @reg: Register to test
  757. * @mask: Mask that specifies which bits to test
  758. * @value: Value to test against
  759. *
  760. * Tests a register with a new value and checks if the new value is
  761. * different from the old value.
  762. *
  763. * Return: 1 for change, otherwise 0.
  764. */
  765. int snd_soc_component_test_bits(struct snd_soc_component *component,
  766. unsigned int reg, unsigned int mask, unsigned int value)
  767. {
  768. unsigned int old, new;
  769. old = snd_soc_component_read(component, reg);
  770. new = (old & ~mask) | value;
  771. return old != new;
  772. }
  773. EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
  774. int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
  775. {
  776. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  777. struct snd_soc_component *component;
  778. int i;
  779. /* FIXME: use 1st pointer */
  780. for_each_rtd_components(rtd, i, component)
  781. if (component->driver->pointer)
  782. return component->driver->pointer(component, substream);
  783. return 0;
  784. }
  785. static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
  786. struct snd_soc_component *component)
  787. {
  788. struct snd_soc_dai *dai;
  789. int i;
  790. for_each_rtd_codec_dais(rtd, i, dai) {
  791. if (dai->component == component)
  792. return true;
  793. }
  794. return false;
  795. }
  796. void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
  797. snd_pcm_sframes_t *cpu_delay,
  798. snd_pcm_sframes_t *codec_delay)
  799. {
  800. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  801. struct snd_soc_component *component;
  802. snd_pcm_sframes_t delay;
  803. int i;
  804. /*
  805. * We're looking for the delay through the full audio path so it needs to
  806. * be the maximum of the Components doing transmit and the maximum of the
  807. * Components doing receive (ie, all CPUs and all CODECs) rather than
  808. * just the maximum of all Components.
  809. */
  810. for_each_rtd_components(rtd, i, component) {
  811. if (!component->driver->delay)
  812. continue;
  813. delay = component->driver->delay(component, substream);
  814. if (snd_soc_component_is_codec_on_rtd(rtd, component))
  815. *codec_delay = max(*codec_delay, delay);
  816. else
  817. *cpu_delay = max(*cpu_delay, delay);
  818. }
  819. }
  820. int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
  821. unsigned int cmd, void *arg)
  822. {
  823. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  824. struct snd_soc_component *component;
  825. int i;
  826. /* FIXME: use 1st ioctl */
  827. for_each_rtd_components(rtd, i, component)
  828. if (component->driver->ioctl)
  829. return soc_component_ret(
  830. component,
  831. component->driver->ioctl(component,
  832. substream, cmd, arg));
  833. return snd_pcm_lib_ioctl(substream, cmd, arg);
  834. }
  835. int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
  836. {
  837. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  838. struct snd_soc_component *component;
  839. int i, ret;
  840. for_each_rtd_components(rtd, i, component) {
  841. if (component->driver->sync_stop) {
  842. ret = component->driver->sync_stop(component,
  843. substream);
  844. if (ret < 0)
  845. return soc_component_ret(component, ret);
  846. }
  847. }
  848. return 0;
  849. }
  850. int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
  851. int channel, unsigned long pos,
  852. void __user *buf, unsigned long bytes)
  853. {
  854. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  855. struct snd_soc_component *component;
  856. int i;
  857. /* FIXME. it returns 1st copy now */
  858. for_each_rtd_components(rtd, i, component)
  859. if (component->driver->copy_user)
  860. return soc_component_ret(
  861. component,
  862. component->driver->copy_user(
  863. component, substream, channel,
  864. pos, buf, bytes));
  865. return -EINVAL;
  866. }
  867. struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
  868. unsigned long offset)
  869. {
  870. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  871. struct snd_soc_component *component;
  872. struct page *page;
  873. int i;
  874. /* FIXME. it returns 1st page now */
  875. for_each_rtd_components(rtd, i, component) {
  876. if (component->driver->page) {
  877. page = component->driver->page(component,
  878. substream, offset);
  879. if (page)
  880. return page;
  881. }
  882. }
  883. return NULL;
  884. }
  885. int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
  886. struct vm_area_struct *vma)
  887. {
  888. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  889. struct snd_soc_component *component;
  890. int i;
  891. /* FIXME. it returns 1st mmap now */
  892. for_each_rtd_components(rtd, i, component)
  893. if (component->driver->mmap)
  894. return soc_component_ret(
  895. component,
  896. component->driver->mmap(component,
  897. substream, vma));
  898. return -EINVAL;
  899. }
  900. int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
  901. {
  902. struct snd_soc_component *component;
  903. int ret;
  904. int i;
  905. for_each_rtd_components(rtd, i, component) {
  906. if (component->driver->pcm_construct) {
  907. ret = component->driver->pcm_construct(component, rtd);
  908. if (ret < 0)
  909. return soc_component_ret(component, ret);
  910. }
  911. }
  912. return 0;
  913. }
  914. void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
  915. {
  916. struct snd_soc_component *component;
  917. int i;
  918. if (!rtd->pcm)
  919. return;
  920. for_each_rtd_components(rtd, i, component)
  921. if (component->driver->pcm_destruct)
  922. component->driver->pcm_destruct(component, rtd->pcm);
  923. }
  924. int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
  925. {
  926. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  927. struct snd_soc_component *component;
  928. int i, ret;
  929. for_each_rtd_components(rtd, i, component) {
  930. if (component->driver->prepare) {
  931. ret = component->driver->prepare(component, substream);
  932. if (ret < 0)
  933. return soc_component_ret(component, ret);
  934. }
  935. }
  936. return 0;
  937. }
  938. int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
  939. struct snd_pcm_hw_params *params)
  940. {
  941. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  942. struct snd_soc_component *component;
  943. int i, ret;
  944. for_each_rtd_components(rtd, i, component) {
  945. if (component->driver->hw_params) {
  946. ret = component->driver->hw_params(component,
  947. substream, params);
  948. if (ret < 0)
  949. return soc_component_ret(component, ret);
  950. }
  951. /* mark substream if succeeded */
  952. soc_component_mark_push(component, substream, hw_params);
  953. }
  954. return 0;
  955. }
  956. void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
  957. int rollback)
  958. {
  959. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  960. struct snd_soc_component *component;
  961. int i, ret;
  962. for_each_rtd_components(rtd, i, component) {
  963. if (rollback && !soc_component_mark_match(component, substream, hw_params))
  964. continue;
  965. if (component->driver->hw_free) {
  966. ret = component->driver->hw_free(component, substream);
  967. if (ret < 0)
  968. soc_component_ret(component, ret);
  969. }
  970. /* remove marked substream */
  971. soc_component_mark_pop(component, substream, hw_params);
  972. }
  973. }
  974. static int soc_component_trigger(struct snd_soc_component *component,
  975. struct snd_pcm_substream *substream,
  976. int cmd)
  977. {
  978. int ret = 0;
  979. if (component->driver->trigger)
  980. ret = component->driver->trigger(component, substream, cmd);
  981. return soc_component_ret(component, ret);
  982. }
  983. int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
  984. int cmd, int rollback)
  985. {
  986. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  987. struct snd_soc_component *component;
  988. int i, r, ret = 0;
  989. switch (cmd) {
  990. case SNDRV_PCM_TRIGGER_START:
  991. case SNDRV_PCM_TRIGGER_RESUME:
  992. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  993. for_each_rtd_components(rtd, i, component) {
  994. ret = soc_component_trigger(component, substream, cmd);
  995. if (ret < 0)
  996. break;
  997. soc_component_mark_push(component, substream, trigger);
  998. }
  999. break;
  1000. case SNDRV_PCM_TRIGGER_STOP:
  1001. case SNDRV_PCM_TRIGGER_SUSPEND:
  1002. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1003. for_each_rtd_components(rtd, i, component) {
  1004. if (rollback && !soc_component_mark_match(component, substream, trigger))
  1005. continue;
  1006. r = soc_component_trigger(component, substream, cmd);
  1007. if (r < 0)
  1008. ret = r; /* use last ret */
  1009. soc_component_mark_pop(component, substream, trigger);
  1010. }
  1011. }
  1012. return ret;
  1013. }
  1014. int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
  1015. void *stream)
  1016. {
  1017. struct snd_soc_component *component;
  1018. int i;
  1019. for_each_rtd_components(rtd, i, component) {
  1020. int ret = pm_runtime_get_sync(component->dev);
  1021. if (ret < 0 && ret != -EACCES) {
  1022. pm_runtime_put_noidle(component->dev);
  1023. return soc_component_ret(component, ret);
  1024. }
  1025. /* mark stream if succeeded */
  1026. soc_component_mark_push(component, stream, pm);
  1027. }
  1028. return 0;
  1029. }
  1030. void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
  1031. void *stream, int rollback)
  1032. {
  1033. struct snd_soc_component *component;
  1034. int i;
  1035. for_each_rtd_components(rtd, i, component) {
  1036. if (rollback && !soc_component_mark_match(component, stream, pm))
  1037. continue;
  1038. pm_runtime_mark_last_busy(component->dev);
  1039. pm_runtime_put_autosuspend(component->dev);
  1040. /* remove marked stream */
  1041. soc_component_mark_pop(component, stream, pm);
  1042. }
  1043. }
  1044. int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
  1045. {
  1046. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  1047. struct snd_soc_component *component;
  1048. int i;
  1049. /* FIXME: use 1st pointer */
  1050. for_each_rtd_components(rtd, i, component)
  1051. if (component->driver->ack)
  1052. return component->driver->ack(component, substream);
  1053. return 0;
  1054. }