soc-component.c 33 KB

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