ipc4-control.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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) 2022 Intel Corporation. All rights reserved.
  7. //
  8. //
  9. #include "sof-priv.h"
  10. #include "sof-audio.h"
  11. #include "ipc4-priv.h"
  12. #include "ipc4-topology.h"
  13. static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol, bool set)
  14. {
  15. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  16. struct snd_soc_component *scomp = scontrol->scomp;
  17. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  18. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  19. struct sof_ipc4_msg *msg = &cdata->msg;
  20. struct snd_sof_widget *swidget;
  21. bool widget_found = false;
  22. /* find widget associated with the control */
  23. list_for_each_entry(swidget, &sdev->widget_list, list) {
  24. if (swidget->comp_id == scontrol->comp_id) {
  25. widget_found = true;
  26. break;
  27. }
  28. }
  29. if (!widget_found) {
  30. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  31. return -ENOENT;
  32. }
  33. /*
  34. * Volatile controls should always be part of static pipelines and the widget use_count
  35. * would always be > 0 in this case. For the others, just return the cached value if the
  36. * widget is not set up.
  37. */
  38. if (!swidget->use_count)
  39. return 0;
  40. msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
  41. msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
  42. return iops->set_get_data(sdev, msg, msg->data_size, set);
  43. }
  44. static int
  45. sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
  46. struct snd_sof_control *scontrol)
  47. {
  48. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  49. struct sof_ipc4_gain *gain = swidget->private;
  50. struct sof_ipc4_msg *msg = &cdata->msg;
  51. struct sof_ipc4_gain_data data;
  52. bool all_channels_equal = true;
  53. u32 value;
  54. int ret, i;
  55. /* check if all channel values are equal */
  56. value = cdata->chanv[0].value;
  57. for (i = 1; i < scontrol->num_channels; i++) {
  58. if (cdata->chanv[i].value != value) {
  59. all_channels_equal = false;
  60. break;
  61. }
  62. }
  63. /*
  64. * notify DSP with a single IPC message if all channel values are equal. Otherwise send
  65. * a separate IPC for each channel.
  66. */
  67. for (i = 0; i < scontrol->num_channels; i++) {
  68. if (all_channels_equal) {
  69. data.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;
  70. data.init_val = cdata->chanv[0].value;
  71. } else {
  72. data.channels = cdata->chanv[i].channel;
  73. data.init_val = cdata->chanv[i].value;
  74. }
  75. /* set curve type and duration from topology */
  76. data.curve_duration_l = gain->data.curve_duration_l;
  77. data.curve_duration_h = gain->data.curve_duration_h;
  78. data.curve_type = gain->data.curve_type;
  79. msg->data_ptr = &data;
  80. msg->data_size = sizeof(data);
  81. ret = sof_ipc4_set_get_kcontrol_data(scontrol, true);
  82. msg->data_ptr = NULL;
  83. msg->data_size = 0;
  84. if (ret < 0) {
  85. dev_err(sdev->dev, "Failed to set volume update for %s\n",
  86. scontrol->name);
  87. return ret;
  88. }
  89. if (all_channels_equal)
  90. break;
  91. }
  92. return 0;
  93. }
  94. static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,
  95. struct snd_ctl_elem_value *ucontrol)
  96. {
  97. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  98. struct snd_soc_component *scomp = scontrol->scomp;
  99. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  100. unsigned int channels = scontrol->num_channels;
  101. struct snd_sof_widget *swidget;
  102. bool widget_found = false;
  103. bool change = false;
  104. unsigned int i;
  105. int ret;
  106. /* update each channel */
  107. for (i = 0; i < channels; i++) {
  108. u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
  109. scontrol->volume_table, scontrol->max + 1);
  110. change = change || (value != cdata->chanv[i].value);
  111. cdata->chanv[i].channel = i;
  112. cdata->chanv[i].value = value;
  113. }
  114. if (!pm_runtime_active(scomp->dev))
  115. return change;
  116. /* find widget associated with the control */
  117. list_for_each_entry(swidget, &sdev->widget_list, list) {
  118. if (swidget->comp_id == scontrol->comp_id) {
  119. widget_found = true;
  120. break;
  121. }
  122. }
  123. if (!widget_found) {
  124. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  125. return false;
  126. }
  127. ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
  128. if (ret < 0)
  129. return false;
  130. return change;
  131. }
  132. static int sof_ipc4_volume_get(struct snd_sof_control *scontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  136. unsigned int channels = scontrol->num_channels;
  137. unsigned int i;
  138. for (i = 0; i < channels; i++)
  139. ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
  140. scontrol->volume_table,
  141. scontrol->max + 1);
  142. return 0;
  143. }
  144. /* set up all controls for the widget */
  145. static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  146. {
  147. struct snd_sof_control *scontrol;
  148. int ret;
  149. list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
  150. if (scontrol->comp_id == swidget->comp_id) {
  151. ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
  152. if (ret < 0) {
  153. dev_err(sdev->dev, "%s: kcontrol %d set up failed for widget %s\n",
  154. __func__, scontrol->comp_id, swidget->widget->name);
  155. return ret;
  156. }
  157. }
  158. return 0;
  159. }
  160. static int
  161. sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
  162. {
  163. int i;
  164. /* init the volume table */
  165. scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
  166. if (!scontrol->volume_table)
  167. return -ENOMEM;
  168. /* populate the volume table */
  169. for (i = 0; i < size ; i++) {
  170. u32 val = vol_compute_gain(i, tlv);
  171. u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
  172. scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
  173. SOF_IPC4_VOL_ZERO_DB : q31val;
  174. }
  175. return 0;
  176. }
  177. const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
  178. .volume_put = sof_ipc4_volume_put,
  179. .volume_get = sof_ipc4_volume_get,
  180. .widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
  181. .set_up_volume_table = sof_ipc4_set_up_volume_table,
  182. };