btfm_codec_interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. int ret;
  143. BTFMCODEC_INFO("dai->name: %s, dai->id: %d, dai->rate: %d direction: %d", dai->name,
  144. id, sampling_rate, direction);
  145. // ToDo: check whether hw_params has to allowed when state if different
  146. if (states.current_state != IDLE) {
  147. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  148. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_prepare) {
  149. ret = dai_drv->dai_ops->hwep_prepare((void *)btfmcodec->hwep_info, sampling_rate,
  150. direction, id);
  151. if (ret == 0 && test_bit(BTADV_AUDIO_MASTER_CONFIG, &btfmcodec->hwep_info->flags)) {
  152. BTFMCODEC_DBG("configuring master now");
  153. }
  154. } else {
  155. return -1;
  156. }
  157. return 0;
  158. return 0;
  159. }
  160. static int btfmcodec_dai_set_channel_map(struct snd_soc_dai *dai,
  161. unsigned int tx_num, unsigned int *tx_slot,
  162. unsigned int rx_num, unsigned int *rx_slot)
  163. {
  164. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  165. struct btfmcodec_state_machine states = btfmcodec->states;
  166. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  167. BTFMCODEC_DBG("");
  168. // ToDo: check whether hw_params has to allowed when state if different
  169. if (states.current_state != IDLE) {
  170. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  171. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_set_channel_map) {
  172. return dai_drv->dai_ops->hwep_set_channel_map((void *)btfmcodec->hwep_info, tx_num,
  173. tx_slot, rx_num, rx_slot);
  174. } else {
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. static int btfmcodec_dai_get_channel_map(struct snd_soc_dai *dai,
  180. unsigned int *tx_num, unsigned int *tx_slot,
  181. unsigned int *rx_num, unsigned int *rx_slot)
  182. {
  183. struct btfmcodec_data *btfmcodec = snd_soc_component_get_drvdata(dai->component);
  184. struct btfmcodec_state_machine states = btfmcodec->states;
  185. struct hwep_dai_driver *dai_drv = (struct hwep_dai_driver *)btfmcodec_get_dai_drvdata(btfmcodec->hwep_info);
  186. BTFMCODEC_DBG("");
  187. // ToDo: check whether hw_params has to allowed when state if different
  188. if (states.current_state != IDLE) {
  189. BTFMCODEC_WARN("Received probe when state is :%s", coverttostring(states.current_state));
  190. } else if (dai_drv && dai_drv->dai_ops && dai_drv->dai_ops->hwep_get_channel_map) {
  191. return dai_drv->dai_ops->hwep_get_channel_map((void *)btfmcodec->hwep_info, tx_num,
  192. tx_slot, rx_num, rx_slot, dai->id);
  193. } else {
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. static struct snd_soc_dai_ops btfmcodec_dai_ops = {
  199. .startup = btfmcodec_dai_startup,
  200. .shutdown = btfmcodec_dai_shutdown,
  201. .hw_params = btfmcodec_dai_hw_params,
  202. .prepare = btfmcodec_dai_prepare,
  203. .set_channel_map = btfmcodec_dai_set_channel_map,
  204. .get_channel_map = btfmcodec_dai_get_channel_map,
  205. };
  206. int btfm_register_codec(struct hwep_data *hwep_info)
  207. {
  208. struct btfmcodec_data *btfmcodec;
  209. struct device *dev;
  210. struct hwep_dai_driver *dai_drv;
  211. int i, ret;
  212. btfmcodec = btfm_get_btfmcodec();
  213. dev = &btfmcodec->dev;
  214. btfmcodec_dai_info = kzalloc((sizeof(struct snd_soc_dai_driver) * hwep_info->num_dai), GFP_KERNEL);
  215. if (!btfmcodec_dai_info) {
  216. BTFMCODEC_ERR("failed to allocate memory");
  217. return -ENOMEM;
  218. }
  219. for (i = 0; i < hwep_info->num_dai; i++) {
  220. dai_drv = &hwep_info->dai_drv[i];
  221. btfmcodec_dai_info[i].name = dai_drv->dai_name;
  222. btfmcodec_dai_info[i].id = dai_drv->id;
  223. btfmcodec_dai_info[i].capture = dai_drv->capture;
  224. btfmcodec_dai_info[i].playback = dai_drv->playback;
  225. btfmcodec_dai_info[i].ops = &btfmcodec_dai_ops;
  226. }
  227. BTFMCODEC_INFO("Adding %d dai support to codec", hwep_info->num_dai);
  228. BTFMCODEC_INFO("slim bus driver name:%s", dev->driver->name);
  229. ret = snd_soc_register_component(dev, &btfmcodec_codec_component,
  230. btfmcodec_dai_info, hwep_info->num_dai);
  231. return ret;
  232. }
  233. void btfm_unregister_codec(void)
  234. {
  235. struct btfmcodec_data *btfmcodec;
  236. btfmcodec = btfm_get_btfmcodec();
  237. snd_soc_unregister_component(&btfmcodec->dev);
  238. }