control.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <[email protected]>
  9. //
  10. /* Mixer Controls */
  11. #include <linux/pm_runtime.h>
  12. #include <linux/leds.h>
  13. #include "sof-priv.h"
  14. #include "sof-audio.h"
  15. int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
  16. struct snd_ctl_elem_value *ucontrol)
  17. {
  18. struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
  19. struct snd_sof_control *scontrol = sm->dobj.private;
  20. struct snd_soc_component *scomp = scontrol->scomp;
  21. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  22. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  23. if (tplg_ops->control->volume_get)
  24. return tplg_ops->control->volume_get(scontrol, ucontrol);
  25. return 0;
  26. }
  27. int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
  28. struct snd_ctl_elem_value *ucontrol)
  29. {
  30. struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
  31. struct snd_sof_control *scontrol = sm->dobj.private;
  32. struct snd_soc_component *scomp = scontrol->scomp;
  33. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  34. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  35. if (tplg_ops->control->volume_put)
  36. return tplg_ops->control->volume_put(scontrol, ucontrol);
  37. return false;
  38. }
  39. int snd_sof_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  40. {
  41. struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
  42. struct snd_sof_control *scontrol = sm->dobj.private;
  43. unsigned int channels = scontrol->num_channels;
  44. int platform_max;
  45. if (!sm->platform_max)
  46. sm->platform_max = sm->max;
  47. platform_max = sm->platform_max;
  48. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  49. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  50. else
  51. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  52. uinfo->count = channels;
  53. uinfo->value.integer.min = 0;
  54. uinfo->value.integer.max = platform_max - sm->min;
  55. return 0;
  56. }
  57. int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
  58. struct snd_ctl_elem_value *ucontrol)
  59. {
  60. struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
  61. struct snd_sof_control *scontrol = sm->dobj.private;
  62. struct snd_soc_component *scomp = scontrol->scomp;
  63. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  64. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  65. if (tplg_ops->control->switch_get)
  66. return tplg_ops->control->switch_get(scontrol, ucontrol);
  67. return 0;
  68. }
  69. int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
  70. struct snd_ctl_elem_value *ucontrol)
  71. {
  72. struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
  73. struct snd_sof_control *scontrol = sm->dobj.private;
  74. struct snd_soc_component *scomp = scontrol->scomp;
  75. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  76. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  77. if (tplg_ops->control->switch_put)
  78. return tplg_ops->control->switch_put(scontrol, ucontrol);
  79. return false;
  80. }
  81. int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
  82. struct snd_ctl_elem_value *ucontrol)
  83. {
  84. struct soc_enum *se = (struct soc_enum *)kcontrol->private_value;
  85. struct snd_sof_control *scontrol = se->dobj.private;
  86. struct snd_soc_component *scomp = scontrol->scomp;
  87. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  88. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  89. if (tplg_ops->control->enum_get)
  90. return tplg_ops->control->enum_get(scontrol, ucontrol);
  91. return 0;
  92. }
  93. int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
  94. struct snd_ctl_elem_value *ucontrol)
  95. {
  96. struct soc_enum *se = (struct soc_enum *)kcontrol->private_value;
  97. struct snd_sof_control *scontrol = se->dobj.private;
  98. struct snd_soc_component *scomp = scontrol->scomp;
  99. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  100. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  101. if (tplg_ops->control->enum_put)
  102. return tplg_ops->control->enum_put(scontrol, ucontrol);
  103. return false;
  104. }
  105. int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
  106. struct snd_ctl_elem_value *ucontrol)
  107. {
  108. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  109. struct snd_sof_control *scontrol = be->dobj.private;
  110. struct snd_soc_component *scomp = scontrol->scomp;
  111. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  112. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  113. if (tplg_ops->control->bytes_get)
  114. return tplg_ops->control->bytes_get(scontrol, ucontrol);
  115. return 0;
  116. }
  117. int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
  118. struct snd_ctl_elem_value *ucontrol)
  119. {
  120. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  121. struct snd_sof_control *scontrol = be->dobj.private;
  122. struct snd_soc_component *scomp = scontrol->scomp;
  123. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  124. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  125. if (tplg_ops->control->bytes_put)
  126. return tplg_ops->control->bytes_put(scontrol, ucontrol);
  127. return 0;
  128. }
  129. int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
  130. const unsigned int __user *binary_data,
  131. unsigned int size)
  132. {
  133. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  134. struct snd_sof_control *scontrol = be->dobj.private;
  135. struct snd_soc_component *scomp = scontrol->scomp;
  136. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  137. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  138. /* make sure we have at least a header */
  139. if (size < sizeof(struct snd_ctl_tlv))
  140. return -EINVAL;
  141. if (tplg_ops->control->bytes_ext_put)
  142. return tplg_ops->control->bytes_ext_put(scontrol, binary_data, size);
  143. return 0;
  144. }
  145. int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
  146. unsigned int size)
  147. {
  148. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  149. struct snd_sof_control *scontrol = be->dobj.private;
  150. struct snd_soc_component *scomp = scontrol->scomp;
  151. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  152. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  153. int ret, err;
  154. ret = pm_runtime_resume_and_get(scomp->dev);
  155. if (ret < 0 && ret != -EACCES) {
  156. dev_err_ratelimited(scomp->dev, "%s: failed to resume %d\n", __func__, ret);
  157. return ret;
  158. }
  159. if (tplg_ops->control->bytes_ext_volatile_get)
  160. ret = tplg_ops->control->bytes_ext_volatile_get(scontrol, binary_data, size);
  161. pm_runtime_mark_last_busy(scomp->dev);
  162. err = pm_runtime_put_autosuspend(scomp->dev);
  163. if (err < 0)
  164. dev_err_ratelimited(scomp->dev, "%s: failed to idle %d\n", __func__, err);
  165. return ret;
  166. }
  167. int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
  168. unsigned int __user *binary_data,
  169. unsigned int size)
  170. {
  171. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  172. struct snd_sof_control *scontrol = be->dobj.private;
  173. struct snd_soc_component *scomp = scontrol->scomp;
  174. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  175. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  176. if (tplg_ops->control->bytes_ext_get)
  177. return tplg_ops->control->bytes_ext_get(scontrol, binary_data, size);
  178. return 0;
  179. }