btfm_swr_hw_interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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)
  105. BTFMSWR_INFO("btfmswr is NULL\n");
  106. switch (id) {
  107. case FMAUDIO_TX:
  108. port_type = FM_AUDIO_TX1;
  109. break;
  110. case BTAUDIO_TX:
  111. port_type = BT_AUDIO_TX1;
  112. break;
  113. case BTAUDIO_RX:
  114. port_type = BT_AUDIO_RX1;
  115. break;
  116. case BTAUDIO_A2DP_SINK_TX:
  117. port_type = BT_AUDIO_TX2;
  118. break;
  119. case BTFM_NUM_CODEC_DAIS:
  120. default:
  121. BTFMSWR_ERR("dai->id is invalid:%d", id);
  122. return;
  123. }
  124. ret = btfm_swr_disable_port(btfmswr->p_dai_port->port_info[id].port,
  125. btfmswr->num_channels, port_type);
  126. }
  127. static int btfm_swr_dai_hw_params(void *dai, uint32_t bps,
  128. uint32_t direction, uint8_t num_channels)
  129. {
  130. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  131. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  132. BTFMSWR_DBG("");
  133. btfmswr->bps = bps;
  134. btfmswr->direction = direction;
  135. btfmswr->num_channels = num_channels;
  136. return 0;
  137. }
  138. void btfm_get_sampling_rate(uint32_t *sampling_rate)
  139. {
  140. uint8_t codec_types_avb = ARRAY_SIZE(codec_text);
  141. if (usecase_codec > (codec_types_avb - 1)) {
  142. BTFMSWR_ERR("falling back to use default sampling_rate: %u",
  143. *sampling_rate);
  144. return;
  145. }
  146. if (*sampling_rate == 44100 || *sampling_rate == 48000) {
  147. if (usecase_codec == LDAC ||
  148. usecase_codec == APTX_AD)
  149. *sampling_rate = (*sampling_rate) * 2;
  150. }
  151. if (usecase_codec == LC3_VOICE ||
  152. usecase_codec == APTX_AD_SPEECH ||
  153. usecase_codec == LC3 || usecase_codec == APTX_AD_QLEA ||
  154. usecase_codec == APTX_AD_R4) {
  155. *sampling_rate = 96000;
  156. }
  157. BTFMSWR_INFO("current usecase codec type %s and sampling rate:%u khz",
  158. codec_text[usecase_codec], *sampling_rate);
  159. }
  160. static int btfm_swr_dai_prepare(void *dai, uint32_t sampling_rate, uint32_t direction, int id)
  161. {
  162. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  163. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  164. int ret = -EINVAL;
  165. u8 port_type;
  166. bt_soc_enable_status = 0;
  167. BTFMSWR_INFO("dai->id: %d, dai->rate: %d direction: %d", id, sampling_rate, direction);
  168. btfm_get_sampling_rate(&sampling_rate);
  169. btfmswr->sample_rate = sampling_rate;
  170. switch (id) {
  171. case FMAUDIO_TX:
  172. port_type = FM_AUDIO_TX1;
  173. break;
  174. case BTAUDIO_TX:
  175. port_type = BT_AUDIO_TX1;
  176. break;
  177. case BTAUDIO_RX:
  178. port_type = BT_AUDIO_RX1;
  179. break;
  180. case BTAUDIO_A2DP_SINK_TX:
  181. port_type = BT_AUDIO_TX2;
  182. break;
  183. case BTFM_NUM_CODEC_DAIS:
  184. default:
  185. BTFMSWR_ERR("dai->id is invalid:%d", id);
  186. return -EINVAL;
  187. }
  188. ret = btfm_swr_enable_port(btfmswr->p_dai_port->port_info[id].port,
  189. btfmswr->num_channels, sampling_rate, port_type);
  190. /* save the enable channel status */
  191. if (ret == 0)
  192. bt_soc_enable_status = 1;
  193. if (ret == -EISCONN) {
  194. BTFMSWR_ERR("channel opened without closing, returning success");
  195. ret = 0;
  196. }
  197. return ret;
  198. }
  199. /* This function will be called once during boot up */
  200. static int btfm_swr_dai_set_channel_map(void *dai,
  201. unsigned int tx_num, unsigned int *tx_slot,
  202. unsigned int rx_num, unsigned int *rx_slot)
  203. {
  204. BTFMSWR_DBG("");
  205. return 0;
  206. }
  207. static int btfm_swr_dai_get_channel_map(void *dai,
  208. unsigned int *tx_num, unsigned int *tx_slot,
  209. unsigned int *rx_num, unsigned int *rx_slot, int id)
  210. {
  211. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  212. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  213. *rx_slot = 0;
  214. *tx_slot = 0;
  215. *rx_num = 0;
  216. *tx_num = 0;
  217. switch (id) {
  218. case FMAUDIO_TX:
  219. case BTAUDIO_TX:
  220. case BTAUDIO_A2DP_SINK_TX:
  221. *tx_num = btfmswr->num_channels;
  222. *tx_slot = btfmswr->num_channels == 2 ? TWO_CHANNEL_MASK : ONE_CHANNEL_MASK;
  223. break;
  224. case BTAUDIO_RX:
  225. *rx_num = btfmswr->num_channels;
  226. *rx_slot = btfmswr->num_channels == 2 ? TWO_CHANNEL_MASK : ONE_CHANNEL_MASK;
  227. break;
  228. default:
  229. BTFMSWR_ERR("Unsupported DAI %d", id);
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. int btfm_swr_dai_get_configs(void *dai, void *config, uint8_t id)
  235. {
  236. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  237. struct btfmswr *btfmswr = dev_get_drvdata(hwep_info->dev);
  238. struct hwep_dma_configurations *hwep_config;
  239. BTFMSWR_DBG("");
  240. hwep_config = (struct hwep_dma_configurations *)config;
  241. hwep_config->stream_id = id;
  242. hwep_config->sample_rate = btfmswr->sample_rate;
  243. hwep_config->bit_width = (uint8_t)btfmswr->bps;
  244. hwep_config->codectype = usecase_codec;
  245. hwep_config->num_channels = btfmswr->num_channels;
  246. hwep_config->active_channel_mask = (btfmswr->num_channels == 2 ?
  247. TWO_CHANNEL_MASK : ONE_CHANNEL_MASK);
  248. hwep_config->lpaif = LPAIF_AUD;
  249. hwep_config->inf_index = 1;
  250. return 1;
  251. }
  252. static struct hwep_dai_ops btfmswr_hw_dai_ops = {
  253. .hwep_startup = btfm_swr_dai_startup,
  254. .hwep_shutdown = btfm_swr_dai_shutdown,
  255. .hwep_hw_params = btfm_swr_dai_hw_params,
  256. .hwep_prepare = btfm_swr_dai_prepare,
  257. .hwep_set_channel_map = btfm_swr_dai_set_channel_map,
  258. .hwep_get_channel_map = btfm_swr_dai_get_channel_map,
  259. .hwep_get_configs = btfm_swr_dai_get_configs,
  260. .hwep_codectype = &usecase_codec,
  261. };
  262. static struct hwep_dai_driver btfmswr_dai_driver[] = {
  263. { /* FM Audio data multiple channel : FM -> lpass */
  264. .dai_name = "btaudio_fm_tx",
  265. .id = FMAUDIO_TX,
  266. .capture = {
  267. .stream_name = "FM SWR TX Capture",
  268. .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
  269. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  270. .rate_max = 48000,
  271. .rate_min = 48000,
  272. .channels_min = 1,
  273. .channels_max = 2,
  274. },
  275. .dai_ops = &btfmswr_hw_dai_ops,
  276. },
  277. { /* Bluetooth SCO voice uplink: bt -> lpass */
  278. .dai_name = "btaudio_tx",
  279. .id = BTAUDIO_TX,
  280. .capture = {
  281. .stream_name = "BT Audio SWR Tx Capture",
  282. /* 8 KHz or 16 KHz */
  283. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  284. | SNDRV_PCM_RATE_8000_192000
  285. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  286. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  287. | SNDRV_PCM_RATE_192000,
  288. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  289. .rate_max = 192000,
  290. .rate_min = 8000,
  291. .channels_min = 1,
  292. .channels_max = 1,
  293. },
  294. .dai_ops = &btfmswr_hw_dai_ops,
  295. },
  296. { /* Bluetooth SCO voice downlink: lpass -> bt or A2DP Playback */
  297. .dai_name = "btaudio_rx",
  298. .id = BTAUDIO_RX,
  299. .playback = {
  300. .stream_name = "BT Audio SWR Rx Playback",
  301. /* 8/16/44.1/48/88.2/96 Khz */
  302. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  303. | SNDRV_PCM_RATE_8000_192000
  304. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  305. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  306. | SNDRV_PCM_RATE_192000,
  307. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  308. .rate_max = 192000,
  309. .rate_min = 8000,
  310. .channels_min = 1,
  311. .channels_max = 1,
  312. },
  313. .dai_ops = &btfmswr_hw_dai_ops,
  314. },
  315. { /* Bluetooth A2DP sink: bt -> lpass */
  316. .dai_name = "btfm_a2dp_sink_swr_tx",
  317. .id = BTAUDIO_A2DP_SINK_TX,
  318. .capture = {
  319. .stream_name = "A2DP sink TX Capture",
  320. /* 8/16/44.1/48/88.2/96/192 Khz */
  321. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  322. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  323. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  324. | SNDRV_PCM_RATE_192000,
  325. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  326. .rate_max = 192000,
  327. .rate_min = 8000,
  328. .channels_min = 1,
  329. .channels_max = 1,
  330. },
  331. .dai_ops = &btfmswr_hw_dai_ops,
  332. }
  333. };
  334. static struct hwep_comp_drv btfmswr_hw_driver = {
  335. .hwep_probe = btfm_swr_hwep_probe,
  336. .hwep_remove = btfm_swr_hwep_remove,
  337. .hwep_read = btfm_swr_hwep_read,
  338. .hwep_write = btfm_swr_hwep_write,
  339. };
  340. int btfm_swr_register_hw_ep(struct btfmswr *btfm_swr)
  341. {
  342. struct device *dev = btfm_swr->dev;
  343. struct hwep_data *hwep_info;
  344. int ret = 0;
  345. BTFMSWR_INFO("Registering with BTFMCODEC HWEP interface\n");
  346. hwep_info = kzalloc(sizeof(struct hwep_data), GFP_KERNEL);
  347. if (!hwep_info) {
  348. BTFMSWR_ERR("%s: failed to allocate memory\n", __func__);
  349. ret = -ENOMEM;
  350. goto end;
  351. }
  352. /* Copy EP device parameters as intercations will be on the same device */
  353. hwep_info->dev = dev;
  354. strscpy(hwep_info->driver_name, SWR_SLAVE_COMPATIBLE_STR, DEVICE_NAME_MAX_LEN);
  355. hwep_info->drv = &btfmswr_hw_driver;
  356. hwep_info->dai_drv = btfmswr_dai_driver;
  357. hwep_info->num_dai = ARRAY_SIZE(btfmswr_dai_driver);
  358. hwep_info->num_dai = 4;
  359. hwep_info->num_mixer_ctrl = ARRAY_SIZE(status_controls);
  360. hwep_info->mixer_ctrl = status_controls;
  361. /* Register to hardware endpoint */
  362. ret = btfmcodec_register_hw_ep(hwep_info);
  363. if (ret) {
  364. BTFMSWR_ERR("failed to register with btfmcodec driver hw interface (%d)", ret);
  365. goto end;
  366. }
  367. BTFMSWR_INFO("Registered succesfull with BTFMCODEC HWEP interface\n");
  368. return ret;
  369. end:
  370. return ret;
  371. }
  372. void btfm_swr_unregister_hwep(void)
  373. {
  374. BTFMSWR_INFO("Unregistered with BTFMCODEC HWEP interface");
  375. /* Unregister with BTFMCODEC HWEP driver */
  376. btfmcodec_unregister_hw_ep(SWR_SLAVE_COMPATIBLE_STR);
  377. }
  378. MODULE_DESCRIPTION("BTFM SoundWire Codec driver");
  379. MODULE_LICENSE("GPL v2");