btfm_swr_hw_interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/errno.h>
  8. #include <sound/pcm.h>
  9. #include <sound/pcm_params.h>
  10. #include <sound/soc.h>
  11. #include "btfm_swr.h"
  12. #include "btfm_swr_hw_interface.h"
  13. #include "btfm_codec_hw_interface.h"
  14. #define LPAIF_AUD 0x05
  15. static int bt_soc_enable_status;
  16. int btfm_feedback_ch_setting;
  17. static uint8_t usecase_codec;
  18. static int btfm_swr_hwep_write(struct snd_soc_component *codec,
  19. unsigned int reg, unsigned int value)
  20. {
  21. BTFMSWR_DBG("");
  22. return 0;
  23. }
  24. static unsigned int btfm_swr_hwep_read(struct snd_soc_component *codec,
  25. unsigned int reg)
  26. {
  27. BTFMSWR_DBG("");
  28. return 0;
  29. }
  30. static int btfm_soc_status_get(struct snd_kcontrol *kcontrol,
  31. struct snd_ctl_elem_value *ucontrol)
  32. {
  33. BTFMSWR_DBG("");
  34. ucontrol->value.integer.value[0] = bt_soc_enable_status;
  35. return 1;
  36. }
  37. static int btfm_soc_status_put(struct snd_kcontrol *kcontrol,
  38. struct snd_ctl_elem_value *ucontrol)
  39. {
  40. BTFMSWR_DBG("");
  41. return 1;
  42. }
  43. static int btfm_get_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  44. struct snd_ctl_elem_value *ucontrol)
  45. {
  46. BTFMSWR_DBG("");
  47. ucontrol->value.integer.value[0] = btfm_feedback_ch_setting;
  48. return 1;
  49. }
  50. static int btfm_put_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  51. struct snd_ctl_elem_value *ucontrol)
  52. {
  53. BTFMSWR_DBG("");
  54. btfm_feedback_ch_setting = ucontrol->value.integer.value[0];
  55. return 1;
  56. }
  57. static int btfm_get_codec_type(struct snd_kcontrol *kcontrol,
  58. struct snd_ctl_elem_value *ucontrol)
  59. {
  60. BTFMSWR_DBG("current codec type:%s", codec_text[usecase_codec]);
  61. ucontrol->value.integer.value[0] = usecase_codec;
  62. return 1;
  63. }
  64. static int btfm_put_codec_type(struct snd_kcontrol *kcontrol,
  65. struct snd_ctl_elem_value *ucontrol)
  66. {
  67. usecase_codec = ucontrol->value.integer.value[0];
  68. BTFMSWR_DBG("codec type set to:%s", codec_text[usecase_codec]);
  69. return 1;
  70. }
  71. static struct snd_kcontrol_new status_controls[] = {
  72. SOC_SINGLE_EXT("BT SOC status", 0, 0, 1, 0,
  73. btfm_soc_status_get, btfm_soc_status_put),
  74. SOC_SINGLE_EXT("BT set feedback channel", 0, 0, 1, 0,
  75. btfm_get_feedback_ch_setting,
  76. btfm_put_feedback_ch_setting),
  77. SOC_ENUM_EXT("BT codec type", codec_display,
  78. btfm_get_codec_type, btfm_put_codec_type),
  79. };
  80. static int btfm_swr_hwep_probe(struct snd_soc_component *codec)
  81. {
  82. BTFMSWR_DBG("");
  83. return 0;
  84. }
  85. static void btfm_swr_hwep_remove(struct snd_soc_component *codec)
  86. {
  87. BTFMSWR_DBG("");
  88. }
  89. static int btfm_swr_dai_startup(void *dai)
  90. {
  91. //struct hwep_data *hwep_info = (struct hwep_data *)dai;
  92. int ret = -1;
  93. BTFMSWR_DBG("");
  94. ret = btfm_swr_hw_init();
  95. return ret;
  96. }
  97. static void btfm_swr_dai_shutdown(void *dai, int id)
  98. {
  99. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  100. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  101. int ret = 0;
  102. u8 port_type;
  103. BTFMSWR_INFO("");
  104. if (btfmswr == NULL || btfmswr->p_dai_port == NULL) {
  105. BTFMSWR_INFO("port shutdown might have called with out open\n");
  106. return;
  107. }
  108. switch (id) {
  109. case FMAUDIO_TX:
  110. port_type = FM_AUDIO_TX1;
  111. break;
  112. case BTAUDIO_TX:
  113. port_type = BT_AUDIO_TX1;
  114. break;
  115. case BTAUDIO_RX:
  116. port_type = BT_AUDIO_RX1;
  117. break;
  118. case BTAUDIO_A2DP_SINK_TX:
  119. port_type = BT_AUDIO_TX2;
  120. break;
  121. case BTFM_NUM_CODEC_DAIS:
  122. default:
  123. BTFMSWR_ERR("dai->id is invalid:%d", id);
  124. return;
  125. }
  126. ret = btfm_swr_disable_port(btfmswr->p_dai_port->port_info[id].port,
  127. btfmswr->num_channels, port_type);
  128. }
  129. static int btfm_swr_dai_hw_params(void *dai, uint32_t bps,
  130. uint32_t direction, uint8_t num_channels)
  131. {
  132. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  133. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  134. BTFMSWR_DBG("");
  135. btfmswr->bps = bps;
  136. btfmswr->direction = direction;
  137. btfmswr->num_channels = num_channels;
  138. return 0;
  139. }
  140. void btfm_get_sampling_rate(uint32_t *sampling_rate)
  141. {
  142. uint8_t codec_types_avb = ARRAY_SIZE(codec_text);
  143. if (usecase_codec > (codec_types_avb - 1)) {
  144. BTFMSWR_ERR("falling back to use default sampling_rate: %u",
  145. *sampling_rate);
  146. return;
  147. }
  148. if (*sampling_rate == 44100 || *sampling_rate == 48000) {
  149. if (usecase_codec == LDAC ||
  150. usecase_codec == APTX_AD)
  151. *sampling_rate = (*sampling_rate) * 2;
  152. }
  153. if (usecase_codec == LC3_VOICE ||
  154. usecase_codec == APTX_AD_SPEECH ||
  155. usecase_codec == LC3 || usecase_codec == APTX_AD_QLEA ||
  156. usecase_codec == APTX_AD_R4) {
  157. *sampling_rate = 96000;
  158. }
  159. BTFMSWR_INFO("current usecase codec type %s and sampling rate:%u khz",
  160. codec_text[usecase_codec], *sampling_rate);
  161. }
  162. static int btfm_swr_dai_prepare(void *dai, uint32_t sampling_rate, uint32_t direction, int id)
  163. {
  164. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  165. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  166. int ret = -EINVAL;
  167. u8 port_type;
  168. bt_soc_enable_status = 0;
  169. BTFMSWR_INFO("dai->id: %d, dai->rate: %d direction: %d", id, sampling_rate, direction);
  170. btfm_get_sampling_rate(&sampling_rate);
  171. btfmswr->sample_rate = sampling_rate;
  172. switch (id) {
  173. case FMAUDIO_TX:
  174. port_type = FM_AUDIO_TX1;
  175. break;
  176. case BTAUDIO_TX:
  177. port_type = BT_AUDIO_TX1;
  178. break;
  179. case BTAUDIO_RX:
  180. port_type = BT_AUDIO_RX1;
  181. break;
  182. case BTAUDIO_A2DP_SINK_TX:
  183. port_type = BT_AUDIO_TX2;
  184. break;
  185. case BTFM_NUM_CODEC_DAIS:
  186. default:
  187. BTFMSWR_ERR("dai->id is invalid:%d", id);
  188. return -EINVAL;
  189. }
  190. ret = btfm_swr_enable_port(btfmswr->p_dai_port->port_info[id].port,
  191. btfmswr->num_channels, sampling_rate, port_type);
  192. /* save the enable channel status */
  193. if (ret == 0)
  194. bt_soc_enable_status = 1;
  195. if (ret == -EISCONN) {
  196. BTFMSWR_ERR("channel opened without closing, returning success");
  197. ret = 0;
  198. }
  199. return ret;
  200. }
  201. /* This function will be called once during boot up */
  202. static int btfm_swr_dai_set_channel_map(void *dai,
  203. unsigned int tx_num, unsigned int *tx_slot,
  204. unsigned int rx_num, unsigned int *rx_slot)
  205. {
  206. BTFMSWR_DBG("");
  207. return 0;
  208. }
  209. static int btfm_swr_dai_get_channel_map(void *dai,
  210. unsigned int *tx_num, unsigned int *tx_slot,
  211. unsigned int *rx_num, unsigned int *rx_slot, int id)
  212. {
  213. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  214. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  215. *rx_slot = 0;
  216. *tx_slot = 0;
  217. *rx_num = 0;
  218. *tx_num = 0;
  219. switch (id) {
  220. case FMAUDIO_TX:
  221. case BTAUDIO_TX:
  222. case BTAUDIO_A2DP_SINK_TX:
  223. *tx_num = btfmswr->num_channels;
  224. *tx_slot = btfmswr->num_channels == 2 ? TWO_CHANNEL_MASK : ONE_CHANNEL_MASK;
  225. break;
  226. case BTAUDIO_RX:
  227. *rx_num = btfmswr->num_channels;
  228. *rx_slot = btfmswr->num_channels == 2 ? TWO_CHANNEL_MASK : ONE_CHANNEL_MASK;
  229. break;
  230. default:
  231. BTFMSWR_ERR("Unsupported DAI %d", id);
  232. return -EINVAL;
  233. }
  234. return 0;
  235. }
  236. int btfm_swr_dai_get_configs(void *dai, void *config, uint8_t id)
  237. {
  238. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  239. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  240. struct hwep_dma_configurations *hwep_config;
  241. BTFMSWR_DBG("");
  242. hwep_config = (struct hwep_dma_configurations *)config;
  243. hwep_config->stream_id = id;
  244. hwep_config->sample_rate = btfmswr->sample_rate;
  245. hwep_config->bit_width = (uint8_t)btfmswr->bps;
  246. hwep_config->codectype = usecase_codec;
  247. hwep_config->num_channels = btfmswr->num_channels;
  248. hwep_config->active_channel_mask = (btfmswr->num_channels == 2 ?
  249. TWO_CHANNEL_MASK : ONE_CHANNEL_MASK);
  250. hwep_config->lpaif = LPAIF_AUD;
  251. hwep_config->inf_index = 1;
  252. return 1;
  253. }
  254. static struct hwep_dai_ops btfmswr_hw_dai_ops = {
  255. .hwep_startup = btfm_swr_dai_startup,
  256. .hwep_shutdown = btfm_swr_dai_shutdown,
  257. .hwep_hw_params = btfm_swr_dai_hw_params,
  258. .hwep_prepare = btfm_swr_dai_prepare,
  259. .hwep_set_channel_map = btfm_swr_dai_set_channel_map,
  260. .hwep_get_channel_map = btfm_swr_dai_get_channel_map,
  261. .hwep_get_configs = btfm_swr_dai_get_configs,
  262. .hwep_codectype = &usecase_codec,
  263. };
  264. static struct hwep_dai_driver btfmswr_dai_driver[] = {
  265. { /* FM Audio data multiple channel : FM -> lpass */
  266. .dai_name = "btaudio_fm_tx",
  267. .id = FMAUDIO_TX,
  268. .capture = {
  269. .stream_name = "FM SWR TX Capture",
  270. .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
  271. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  272. .rate_max = 48000,
  273. .rate_min = 48000,
  274. .channels_min = 1,
  275. .channels_max = 2,
  276. },
  277. .dai_ops = &btfmswr_hw_dai_ops,
  278. },
  279. { /* Bluetooth SCO voice uplink: bt -> lpass */
  280. .dai_name = "btaudio_tx",
  281. .id = BTAUDIO_TX,
  282. .capture = {
  283. .stream_name = "BT Audio SWR Tx Capture",
  284. /* 8 KHz or 16 KHz */
  285. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  286. | SNDRV_PCM_RATE_8000_192000
  287. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  288. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  289. | SNDRV_PCM_RATE_192000,
  290. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  291. .rate_max = 192000,
  292. .rate_min = 8000,
  293. .channels_min = 1,
  294. .channels_max = 1,
  295. },
  296. .dai_ops = &btfmswr_hw_dai_ops,
  297. },
  298. { /* Bluetooth SCO voice downlink: lpass -> bt or A2DP Playback */
  299. .dai_name = "btaudio_rx",
  300. .id = BTAUDIO_RX,
  301. .playback = {
  302. .stream_name = "BT Audio SWR Rx Playback",
  303. /* 8/16/44.1/48/88.2/96 Khz */
  304. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  305. | SNDRV_PCM_RATE_8000_192000
  306. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  307. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  308. | SNDRV_PCM_RATE_192000,
  309. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  310. .rate_max = 192000,
  311. .rate_min = 8000,
  312. .channels_min = 1,
  313. .channels_max = 1,
  314. },
  315. .dai_ops = &btfmswr_hw_dai_ops,
  316. },
  317. { /* Bluetooth A2DP sink: bt -> lpass */
  318. .dai_name = "btfm_a2dp_sink_swr_tx",
  319. .id = BTAUDIO_A2DP_SINK_TX,
  320. .capture = {
  321. .stream_name = "A2DP sink TX Capture",
  322. /* 8/16/44.1/48/88.2/96/192 Khz */
  323. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  324. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  325. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  326. | SNDRV_PCM_RATE_192000,
  327. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  328. .rate_max = 192000,
  329. .rate_min = 8000,
  330. .channels_min = 1,
  331. .channels_max = 1,
  332. },
  333. .dai_ops = &btfmswr_hw_dai_ops,
  334. }
  335. };
  336. static struct hwep_comp_drv btfmswr_hw_driver = {
  337. .hwep_probe = btfm_swr_hwep_probe,
  338. .hwep_remove = btfm_swr_hwep_remove,
  339. .hwep_read = btfm_swr_hwep_read,
  340. .hwep_write = btfm_swr_hwep_write,
  341. };
  342. int btfm_swr_register_hw_ep(struct btfmswr *btfm_swr)
  343. {
  344. struct device *dev = btfm_swr->dev;
  345. struct hwep_data *hwep_info;
  346. int ret = 0;
  347. BTFMSWR_INFO("Registering with BTFMCODEC HWEP interface\n");
  348. hwep_info = kzalloc(sizeof(struct hwep_data), GFP_KERNEL);
  349. if (!hwep_info) {
  350. BTFMSWR_ERR("%s: failed to allocate memory\n", __func__);
  351. ret = -ENOMEM;
  352. goto end;
  353. }
  354. /* Copy EP device parameters as intercations will be on the same device */
  355. hwep_info->dev = dev;
  356. strscpy(hwep_info->driver_name, SWR_SLAVE_COMPATIBLE_STR, DEVICE_NAME_MAX_LEN);
  357. hwep_info->drv = &btfmswr_hw_driver;
  358. hwep_info->dai_drv = btfmswr_dai_driver;
  359. hwep_info->num_dai = ARRAY_SIZE(btfmswr_dai_driver);
  360. hwep_info->num_dai = 4;
  361. hwep_info->num_mixer_ctrl = ARRAY_SIZE(status_controls);
  362. hwep_info->mixer_ctrl = status_controls;
  363. /* Register to hardware endpoint */
  364. ret = btfmcodec_register_hw_ep(hwep_info);
  365. if (ret) {
  366. BTFMSWR_ERR("failed to register with btfmcodec driver hw interface (%d)", ret);
  367. goto end;
  368. }
  369. BTFMSWR_INFO("Registered succesfull with BTFMCODEC HWEP interface\n");
  370. return ret;
  371. end:
  372. return ret;
  373. }
  374. void btfm_swr_unregister_hwep(void)
  375. {
  376. BTFMSWR_INFO("Unregistered with BTFMCODEC HWEP interface");
  377. /* Unregister with BTFMCODEC HWEP driver */
  378. btfmcodec_unregister_hw_ep(SWR_SLAVE_COMPATIBLE_STR);
  379. }
  380. MODULE_DESCRIPTION("BTFM SoundWire Codec driver");
  381. MODULE_LICENSE("GPL v2");