btfm_codec_interface.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include "btfm_codec.h"
  7. #include "btfm_codec_interface.h"
  8. static struct snd_soc_dai_driver *btfmcodec_dai_info;
  9. static int btfmcodec_codec_probe(struct snd_soc_component *codec)
  10. {
  11. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  12. struct btfmcodec_state_machine states = btfmcodec->states;
  13. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  14. BTFMCODEC_DBG("");
  15. // ToDo: check weather probe has to allowed when state if different
  16. if (states.current_state != IDLE) {
  17. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  18. } else if (hwep_info->drv && hwep_info->drv->hwep_probe) {
  19. return hwep_info->drv->hwep_probe(codec);
  20. }
  21. // ToDo to add mixer control.
  22. return 0;
  23. }
  24. static void btfmcodec_codec_remove(struct snd_soc_component *codec)
  25. {
  26. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  27. struct btfmcodec_state_machine states = btfmcodec->states;
  28. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  29. BTFMCODEC_DBG("");
  30. // ToDo: check whether remove has to allowed when state if different
  31. if (states.current_state != IDLE) {
  32. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  33. } else if (hwep_info->drv && hwep_info->drv->hwep_remove) {
  34. hwep_info->drv->hwep_remove(codec);
  35. }
  36. }
  37. static int btfmcodec_codec_write(struct snd_soc_component *codec,
  38. unsigned int reg, unsigned int value)
  39. {
  40. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  41. struct btfmcodec_state_machine states = btfmcodec->states;
  42. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  43. BTFMCODEC_DBG("");
  44. // ToDo: check whether remove has to allowed when state if different
  45. if (states.current_state != IDLE) {
  46. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  47. } else if (hwep_info->drv && hwep_info->drv->hwep_remove) {
  48. return hwep_info->drv->hwep_write(codec, reg, value);
  49. }
  50. return 0;
  51. }
  52. static unsigned int btfmcodec_codec_read(struct snd_soc_component *codec,
  53. unsigned int reg)
  54. {
  55. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(codec);
  56. struct btfmcodec_state_machine states = btfmcodec->states;
  57. struct hwep_data *hwep_info = btfmcodec->hwep_info;
  58. BTFMCODEC_DBG("");
  59. // ToDo: check whether remove has to allowed when state if different
  60. if (states.current_state != IDLE) {
  61. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  62. } else if (hwep_info->drv && hwep_info->drv->hwep_read) {
  63. return hwep_info->drv->hwep_read(codec, reg);
  64. }
  65. return 0;
  66. }
  67. static const struct snd_soc_component_driver btfmcodec_codec_component = {
  68. .probe = btfmcodec_codec_probe,
  69. .remove = btfmcodec_codec_remove,
  70. .read = btfmcodec_codec_read,
  71. .write = btfmcodec_codec_write,
  72. };
  73. static inline void * btfmcodec_get_dai_drvdata(struct hwep_data *hwep_info)
  74. {
  75. if (!hwep_info || !hwep_info->dai_drv) return NULL;
  76. return hwep_info->dai_drv;
  77. }
  78. static int btfmcodec_dai_startup(struct snd_pcm_substream *substream,
  79. struct snd_soc_dai *dai)
  80. {
  81. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  82. struct btfmcodec_state_machine states = btfmcodec->states;
  83. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  84. BTFMCODEC_DBG("substream = %s stream = %d dai->name = %s",
  85. substream->name, substream->stream, dai->name);
  86. // ToDo: check whether statup has to allowed when state if different
  87. if (states.current_state != IDLE) {
  88. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  89. } else if (dai && dai_drv->dai_ops && dai_drv->dai_ops->hwep_startup) {
  90. return dai_drv->dai_ops->hwep_startup((void *)btfmcodec->hwep_info);
  91. } else {
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. static void btfmcodec_dai_shutdown(struct snd_pcm_substream *substream,
  97. struct snd_soc_dai *dai)
  98. {
  99. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  100. struct btfmcodec_state_machine states = btfmcodec->states;
  101. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  102. BTFMCODEC_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
  103. dai->id, dai->rate);
  104. // ToDo: check whether statup has to allowed when state if different
  105. if (states.current_state != IDLE) {
  106. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  107. } else if (dai && dai_drv->dai_ops && dai_drv->dai_ops->hwep_shutdown) {
  108. dai_drv->dai_ops->hwep_shutdown((void *)btfmcodec->hwep_info, dai->id);
  109. }
  110. }
  111. static int btfmcodec_dai_hw_params(struct snd_pcm_substream *substream,
  112. struct snd_pcm_hw_params *params,
  113. struct snd_soc_dai *dai)
  114. {
  115. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  116. struct btfmcodec_state_machine states = btfmcodec->states;
  117. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  118. uint32_t bps = params_width(params);
  119. uint32_t direction = substream->stream;
  120. BTFMCODEC_DBG("dai->name = %s DAI-ID %x rate %d bps %d num_ch %d",
  121. dai->name, dai->id, params_rate(params), params_width(params),
  122. params_channels(params));
  123. // ToDo: check whether hw_params has to allowed when state if different
  124. if (states.current_state != IDLE) {
  125. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  126. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_hw_params) {
  127. return dai_drv->dai_ops->hwep_hw_params((void *)btfmcodec->hwep_info, bps, direction);
  128. } else {
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. static int btfmcodec_dai_prepare(struct snd_pcm_substream *substream,
  134. struct snd_soc_dai *dai)
  135. {
  136. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  137. struct btfmcodec_state_machine states = btfmcodec->states;
  138. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  139. uint32_t sampling_rate = dai->rate;
  140. uint32_t direction = substream->stream;
  141. int id = dai->id;
  142. BTFMCODEC_INFO("dai->name: %s, dai->id: %d, dai->rate: %d direction: %d", dai->name,
  143. id, sampling_rate, direction);
  144. // ToDo: check whether hw_params has to allowed when state if different
  145. if (states.current_state != IDLE) {
  146. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  147. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_prepare) {
  148. return dai_drv->dai_ops->hwep_prepare((void *)btfmcodec->hwep_info, sampling_rate,
  149. direction, id);
  150. } else {
  151. return -1;
  152. }
  153. return 0;
  154. return 0;
  155. }
  156. static int btfmcodec_dai_set_channel_map(struct snd_soc_dai *dai,
  157. unsigned int tx_num, unsigned int *tx_slot,
  158. unsigned int rx_num, unsigned int *rx_slot)
  159. {
  160. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  161. struct btfmcodec_state_machine states = btfmcodec->states;
  162. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  163. BTFMCODEC_DBG("");
  164. // ToDo: check whether hw_params has to allowed when state if different
  165. if (states.current_state != IDLE) {
  166. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  167. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_set_channel_map) {
  168. return dai_drv->dai_ops->hwep_set_channel_map((void *)btfmcodec->hwep_info, tx_num,
  169. tx_slot, rx_num, rx_slot);
  170. } else {
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. static int btfmcodec_dai_get_channel_map(struct snd_soc_dai *dai,
  176. unsigned int *tx_num, unsigned int *tx_slot,
  177. unsigned int *rx_num, unsigned int *rx_slot)
  178. {
  179. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  180. struct btfmcodec_state_machine states = btfmcodec->states;
  181. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  182. BTFMCODEC_DBG("");
  183. // ToDo: check whether hw_params has to allowed when state if different
  184. if (states.current_state != IDLE) {
  185. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  186. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_get_channel_map) {
  187. return dai_drv->dai_ops->hwep_get_channel_map((void *)btfmcodec->hwep_info, tx_num,
  188. tx_slot, rx_num, rx_slot, dai->id);
  189. } else {
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. static struct snd_soc_dai_ops btfmcodec_dai_ops = {
  195. .startup = btfmcodec_dai_startup,
  196. .shutdown = btfmcodec_dai_shutdown,
  197. .hw_params = btfmcodec_dai_hw_params,
  198. .prepare = btfmcodec_dai_prepare,
  199. .set_channel_map = btfmcodec_dai_set_channel_map,
  200. .get_channel_map = btfmcodec_dai_get_channel_map,
  201. };
  202. int btfm_register_codec(struct hwep_data *hwep_info)
  203. {
  204. struct btfmcodec_data *btfmcodec;
  205. struct device *dev;
  206. struct hwep_dai_driver *dai_drv;
  207. int i, ret;
  208. btfmcodec = btfm_get_btfmcodec();
  209. dev = &btfmcodec->dev;
  210. btfmcodec_dai_info = kzalloc((sizeof(struct snd_soc_dai_driver) * hwep_info->num_dai), GFP_KERNEL);
  211. if (!btfmcodec_dai_info) {
  212. BTFMCODEC_ERR("failed to allocate memory");
  213. return -ENOMEM;
  214. }
  215. for (i = 0; i < hwep_info->num_dai; i++) {
  216. dai_drv = &hwep_info->dai_drv[i];
  217. btfmcodec_dai_info[i].name = dai_drv->dai_name;
  218. btfmcodec_dai_info[i].id = dai_drv->id;
  219. btfmcodec_dai_info[i].capture = dai_drv->capture;
  220. btfmcodec_dai_info[i].playback = dai_drv->playback;
  221. btfmcodec_dai_info[i].ops = &btfmcodec_dai_ops;
  222. }
  223. BTFMCODEC_INFO("Adding %d dai support to codec", hwep_info->num_dai);
  224. BTFMCODEC_INFO("slim bus driver name:%s", dev->driver->name);
  225. ret = snd_soc_register_component(dev, &btfmcodec_codec_component,
  226. btfmcodec_dai_info, hwep_info->num_dai);
  227. return ret;
  228. }
  229. void btfm_unregister_codec(void)
  230. {
  231. struct btfmcodec_data *btfmcodec;
  232. btfmcodec = btfm_get_btfmcodec();
  233. snd_soc_unregister_component(&btfmcodec->dev);
  234. }