btfm_slim_hw_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021, 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/delay.h>
  11. #include <linux/gpio.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/slimbus.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/slab.h>
  16. #include <linux/errno.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-dapm.h>
  21. #include <sound/tlv.h>
  22. #include "btfm_slim.h"
  23. #include "btfm_slim_hw_interface.h"
  24. #include "btfm_codec_hw_interface.h"
  25. static int bt_soc_enable_status;
  26. int btfm_feedback_ch_setting;
  27. static uint8_t usecase_codec;
  28. static int btfm_slim_hwep_write(struct snd_soc_component *codec,
  29. unsigned int reg, unsigned int value)
  30. {
  31. BTFMSLIM_DBG("");
  32. return 0;
  33. }
  34. static unsigned int btfm_slim_hwep_read(struct snd_soc_component *codec,
  35. unsigned int reg)
  36. {
  37. BTFMSLIM_DBG("");
  38. return 0;
  39. }
  40. static int btfm_soc_status_get(struct snd_kcontrol *kcontrol,
  41. struct snd_ctl_elem_value *ucontrol)
  42. {
  43. BTFMSLIM_DBG("");
  44. ucontrol->value.integer.value[0] = bt_soc_enable_status;
  45. return 1;
  46. }
  47. static int btfm_soc_status_put(struct snd_kcontrol *kcontrol,
  48. struct snd_ctl_elem_value *ucontrol)
  49. {
  50. BTFMSLIM_DBG("");
  51. return 1;
  52. }
  53. static int btfm_get_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  54. struct snd_ctl_elem_value *ucontrol)
  55. {
  56. BTFMSLIM_DBG("");
  57. ucontrol->value.integer.value[0] = btfm_feedback_ch_setting;
  58. return 1;
  59. }
  60. static int btfm_put_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  61. struct snd_ctl_elem_value *ucontrol)
  62. {
  63. BTFMSLIM_DBG("");
  64. btfm_feedback_ch_setting = ucontrol->value.integer.value[0];
  65. return 1;
  66. }
  67. static int btfm_get_codec_type(struct snd_kcontrol *kcontrol,
  68. struct snd_ctl_elem_value *ucontrol)
  69. {
  70. BTFMSLIM_DBG("current codec type:%s", codec_text[usecase_codec]);
  71. ucontrol->value.integer.value[0] = usecase_codec;
  72. return 1;
  73. }
  74. static int btfm_put_codec_type(struct snd_kcontrol *kcontrol,
  75. struct snd_ctl_elem_value *ucontrol)
  76. {
  77. usecase_codec = ucontrol->value.integer.value[0];
  78. BTFMSLIM_DBG("codec type set to:%s", codec_text[usecase_codec]);
  79. return 1;
  80. }
  81. static struct snd_kcontrol_new status_controls[] = {
  82. SOC_SINGLE_EXT("BT SOC status", 0, 0, 1, 0,
  83. btfm_soc_status_get, btfm_soc_status_put),
  84. SOC_SINGLE_EXT("BT set feedback channel", 0, 0, 1, 0,
  85. btfm_get_feedback_ch_setting,
  86. btfm_put_feedback_ch_setting),
  87. SOC_ENUM_EXT("BT codec type", codec_display,
  88. btfm_get_codec_type, btfm_put_codec_type),
  89. };
  90. static int btfm_slim_hwep_probe(struct snd_soc_component *codec)
  91. {
  92. BTFMSLIM_DBG("");
  93. return 0;
  94. }
  95. static void btfm_slim_hwep_remove(struct snd_soc_component *codec)
  96. {
  97. BTFMSLIM_DBG("");
  98. }
  99. static int btfm_slim_dai_startup(void *dai)
  100. {
  101. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  102. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  103. int ret = -1;
  104. BTFMSLIM_DBG("");
  105. ret = btfm_slim_hw_init(btfmslim);
  106. return ret;
  107. }
  108. static void btfm_slim_dai_shutdown(void *dai, int id)
  109. {
  110. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  111. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  112. struct btfmslim_ch *ch;
  113. int i;
  114. uint8_t rxport, nchan = 1;
  115. BTFMSLIM_DBG("");
  116. switch (id) {
  117. case BTFM_FM_SLIM_TX:
  118. nchan = 2;
  119. ch = btfmslim->tx_chs;
  120. rxport = 0;
  121. break;
  122. case BTFM_BT_SCO_SLIM_TX:
  123. ch = btfmslim->tx_chs;
  124. rxport = 0;
  125. break;
  126. case BTFM_BT_SCO_A2DP_SLIM_RX:
  127. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  128. ch = btfmslim->rx_chs;
  129. rxport = 1;
  130. break;
  131. case BTFM_SLIM_NUM_CODEC_DAIS:
  132. default:
  133. BTFMSLIM_ERR("id is invalid:%d", id);
  134. return;
  135. }
  136. /* Search for dai->id matched port handler */
  137. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  138. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  139. (ch->id != id); ch++, i++)
  140. ;
  141. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  142. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  143. BTFMSLIM_ERR("ch is invalid!!");
  144. return;
  145. }
  146. btfm_slim_disable_ch(btfmslim, ch, rxport, nchan);
  147. btfm_slim_hw_deinit(btfmslim);
  148. }
  149. static int btfm_slim_dai_hw_params(void *dai, uint32_t bps,
  150. uint32_t direction,
  151. uint8_t num_channels) {
  152. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  153. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  154. BTFMSLIM_DBG("");
  155. btfmslim->bps = bps;
  156. btfmslim->direction = direction;
  157. return 0;
  158. }
  159. void btfm_get_sampling_rate(uint32_t *sampling_rate)
  160. {
  161. uint8_t codec_types_avb = ARRAY_SIZE(codec_text);
  162. if (usecase_codec > (codec_types_avb - 1)) {
  163. BTFMSLIM_ERR("falling back to use default sampling_rate: %u",
  164. *sampling_rate);
  165. return;
  166. }
  167. if (*sampling_rate == 44100 || *sampling_rate == 48000) {
  168. if (usecase_codec == LDAC ||
  169. usecase_codec == APTX_AD)
  170. *sampling_rate = (*sampling_rate) *2;
  171. }
  172. if (usecase_codec == LC3_VOICE ||
  173. usecase_codec == APTX_AD_SPEECH ||
  174. usecase_codec == LC3 || usecase_codec == APTX_AD_R4) {
  175. *sampling_rate = 96000;
  176. }
  177. if (usecase_codec == APTX_AD_QLEA)
  178. *sampling_rate = 192000;
  179. BTFMSLIM_INFO("current usecase codec type %s and sampling rate:%u khz",
  180. codec_text[usecase_codec], *sampling_rate);
  181. }
  182. static int btfm_slim_dai_prepare(void *dai, uint32_t sampling_rate, uint32_t direction, int id)
  183. {
  184. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  185. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  186. struct btfmslim_ch *ch;
  187. int ret = -EINVAL;
  188. int i = 0;
  189. uint8_t rxport, nchan = 1;
  190. btfmslim->direction = direction;
  191. bt_soc_enable_status = 0;
  192. btfm_get_sampling_rate(&sampling_rate);
  193. /* save sample rate */
  194. btfmslim->sample_rate = sampling_rate;
  195. switch (id) {
  196. case BTFM_FM_SLIM_TX:
  197. nchan = 2;
  198. ch = btfmslim->tx_chs;
  199. rxport = 0;
  200. break;
  201. case BTFM_BT_SCO_SLIM_TX:
  202. ch = btfmslim->tx_chs;
  203. rxport = 0;
  204. break;
  205. case BTFM_BT_SCO_A2DP_SLIM_RX:
  206. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  207. ch = btfmslim->rx_chs;
  208. rxport = 1;
  209. break;
  210. case BTFM_SLIM_NUM_CODEC_DAIS:
  211. default:
  212. BTFMSLIM_ERR("id is invalid:%d", id);
  213. return ret;
  214. }
  215. /* Search for dai->id matched port handler */
  216. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  217. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  218. (ch->id != id); ch++, i++)
  219. ;
  220. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  221. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  222. BTFMSLIM_ERR("ch is invalid!!");
  223. return ret;
  224. }
  225. ret = btfm_slim_enable_ch(btfmslim, ch, rxport, sampling_rate, nchan);
  226. /* save the enable channel status */
  227. if (ret == 0)
  228. bt_soc_enable_status = 1;
  229. if (ret == -EISCONN) {
  230. BTFMSLIM_ERR("channel opened without closing, returning success");
  231. ret = 0;
  232. }
  233. return ret;
  234. }
  235. /* This function will be called once during boot up */
  236. static int btfm_slim_dai_set_channel_map(void *dai,
  237. unsigned int tx_num, unsigned int *tx_slot,
  238. unsigned int rx_num, unsigned int *rx_slot)
  239. {
  240. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  241. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  242. struct btfmslim_ch *rx_chs;
  243. struct btfmslim_ch *tx_chs;
  244. int ret = 0, i;
  245. BTFMSLIM_DBG("");
  246. if (!btfmslim)
  247. return -EINVAL;
  248. rx_chs = btfmslim->rx_chs;
  249. tx_chs = btfmslim->tx_chs;
  250. if (!rx_chs || !tx_chs)
  251. return ret;
  252. BTFMSLIM_DBG("Rx: id\tname\tport\tch");
  253. for (i = 0; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < rx_num);
  254. i++, rx_chs++) {
  255. /* Set Rx Channel number from machine driver and
  256. * get channel handler from slimbus driver
  257. */
  258. rx_chs->ch = *(uint8_t *)(rx_slot + i);
  259. BTFMSLIM_DBG(" %d\t%s\t%d\t%x", rx_chs->id,
  260. rx_chs->name, rx_chs->port, rx_chs->ch);
  261. }
  262. BTFMSLIM_DBG("Tx: id\tname\tport\tch");
  263. for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < tx_num);
  264. i++, tx_chs++) {
  265. /* Set Tx Channel number from machine driver and
  266. * get channel handler from slimbus driver
  267. */
  268. tx_chs->ch = *(uint8_t *)(tx_slot + i);
  269. BTFMSLIM_DBG(" %d\t%s\t%d\t%x", tx_chs->id,
  270. tx_chs->name, tx_chs->port, tx_chs->ch);
  271. }
  272. return ret;
  273. }
  274. static int btfm_slim_dai_get_channel_map(void *dai,
  275. unsigned int *tx_num, unsigned int *tx_slot,
  276. unsigned int *rx_num, unsigned int *rx_slot, int id)
  277. {
  278. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  279. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  280. int i, ret = -EINVAL, *slot = NULL, j = 0, num = 1;
  281. struct btfmslim_ch *ch = NULL;
  282. BTFMSLIM_DBG("");
  283. if (!btfmslim)
  284. return ret;
  285. switch (id) {
  286. case BTFM_FM_SLIM_TX:
  287. num = 2;
  288. fallthrough;
  289. case BTFM_BT_SCO_SLIM_TX:
  290. if (!tx_slot || !tx_num) {
  291. BTFMSLIM_ERR("Invalid tx_slot %p or tx_num %p",
  292. tx_slot, tx_num);
  293. return -EINVAL;
  294. }
  295. ch = btfmslim->tx_chs;
  296. if (!ch)
  297. return -EINVAL;
  298. slot = tx_slot;
  299. *rx_slot = 0;
  300. *tx_num = num;
  301. *rx_num = 0;
  302. break;
  303. case BTFM_BT_SCO_A2DP_SLIM_RX:
  304. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  305. if (!rx_slot || !rx_num) {
  306. BTFMSLIM_ERR("Invalid rx_slot %p or rx_num %p",
  307. rx_slot, rx_num);
  308. return -EINVAL;
  309. }
  310. ch = btfmslim->rx_chs;
  311. if (!ch)
  312. return -EINVAL;
  313. slot = rx_slot;
  314. *tx_slot = 0;
  315. *tx_num = 0;
  316. *rx_num = num;
  317. break;
  318. default:
  319. BTFMSLIM_ERR("Unsupported DAI %d", id);
  320. return -EINVAL;
  321. }
  322. do {
  323. if (!ch)
  324. return -EINVAL;
  325. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id !=
  326. BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id != id);
  327. ch++, i++)
  328. ;
  329. if (ch->id == BTFM_SLIM_NUM_CODEC_DAIS ||
  330. i == BTFM_SLIM_NUM_CODEC_DAIS) {
  331. BTFMSLIM_ERR(
  332. "No channel has been allocated for dai (%d)",
  333. id);
  334. return -EINVAL;
  335. }
  336. if (!slot)
  337. return -EINVAL;
  338. *(slot + j) = ch->ch;
  339. BTFMSLIM_DBG("id:%d, port:%d, ch:%d, slot: %d", ch->id,
  340. ch->port, ch->ch, *(slot + j));
  341. /* In case it has mulitiple channels */
  342. if (++j < num)
  343. ch++;
  344. } while (j < num);
  345. return 0;
  346. }
  347. int btfm_slim_dai_get_configs(void *dai, void *config, uint8_t id)
  348. {
  349. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  350. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  351. struct master_hwep_configurations *hwep_config;
  352. struct btfmslim_ch *ch = NULL;
  353. int i = 0;
  354. BTFMSLIM_DBG("");
  355. hwep_config = (struct master_hwep_configurations *) config;
  356. hwep_config->stream_id = id;
  357. hwep_config->device_id = btfmslim->device_id;
  358. hwep_config->sample_rate = btfmslim->sample_rate;
  359. hwep_config->bit_width = (uint8_t)btfmslim->bps;
  360. hwep_config->codectype = usecase_codec;
  361. hwep_config->direction = btfmslim->direction;
  362. switch (id) {
  363. case BTFM_FM_SLIM_TX:
  364. case BTFM_BT_SCO_SLIM_TX:
  365. ch = btfmslim->tx_chs;
  366. break;
  367. case BTFM_BT_SCO_A2DP_SLIM_RX:
  368. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  369. ch = btfmslim->rx_chs;
  370. break;
  371. }
  372. for (; i < id ; i++) {
  373. if (ch[i].id == id) {
  374. BTFMSLIM_DBG("id matched");
  375. hwep_config->num_channels = 1;
  376. hwep_config->chan_num = ch[i].ch;
  377. break;
  378. }
  379. }
  380. return 1;
  381. }
  382. static struct hwep_dai_ops btfmslim_hw_dai_ops = {
  383. .hwep_startup = btfm_slim_dai_startup,
  384. .hwep_shutdown = btfm_slim_dai_shutdown,
  385. .hwep_hw_params = btfm_slim_dai_hw_params,
  386. .hwep_prepare = btfm_slim_dai_prepare,
  387. .hwep_set_channel_map = btfm_slim_dai_set_channel_map,
  388. .hwep_get_channel_map = btfm_slim_dai_get_channel_map,
  389. .hwep_get_configs = btfm_slim_dai_get_configs,
  390. .hwep_codectype = &usecase_codec,
  391. };
  392. static struct hwep_dai_driver btfmslim_dai_driver[] = {
  393. { /* Bluetooth SCO voice uplink: bt -> lpass */
  394. .dai_name = "btaudio_tx",
  395. .id = BTAUDIO_TX,
  396. .capture = {
  397. .stream_name = "BT Audio Slim Tx Capture",
  398. /* 8 KHz or 16 KHz */
  399. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  400. | SNDRV_PCM_RATE_8000_192000
  401. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  402. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  403. | SNDRV_PCM_RATE_192000,
  404. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  405. .rate_max = 192000,
  406. .rate_min = 8000,
  407. .channels_min = 1,
  408. .channels_max = 1,
  409. },
  410. .dai_ops = &btfmslim_hw_dai_ops,
  411. },
  412. { /* Bluetooth SCO voice downlink: lpass -> bt or A2DP Playback */
  413. .dai_name = "btaudio_rx",
  414. .id = BTAUDIO_RX,
  415. .playback = {
  416. .stream_name = "BT Audio Slim Rx Playback",
  417. /* 8/16/44.1/48/88.2/96 Khz */
  418. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  419. | SNDRV_PCM_RATE_8000_192000
  420. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  421. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  422. | SNDRV_PCM_RATE_192000,
  423. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  424. .rate_max = 192000,
  425. .rate_min = 8000,
  426. .channels_min = 1,
  427. .channels_max = 1,
  428. },
  429. .dai_ops = &btfmslim_hw_dai_ops,
  430. },
  431. };
  432. static struct hwep_comp_drv btfmslim_hw_driver = {
  433. .hwep_probe = btfm_slim_hwep_probe,
  434. .hwep_remove = btfm_slim_hwep_remove,
  435. .hwep_read = btfm_slim_hwep_read,
  436. .hwep_write = btfm_slim_hwep_write,
  437. };
  438. int btfm_slim_register_hw_ep(struct btfmslim *btfm_slim)
  439. {
  440. struct device *dev = btfm_slim->dev;
  441. struct hwep_data *hwep_info;
  442. int ret = 0;
  443. BTFMSLIM_INFO("Registering with BTFMCODEC HWEP interface\n");
  444. hwep_info = kzalloc(sizeof(struct hwep_data), GFP_KERNEL);
  445. if (!hwep_info) {
  446. BTFMSLIM_ERR("%s: failed to allocate memory\n", __func__);
  447. ret = -ENOMEM;
  448. goto end;
  449. }
  450. /* Copy EP device parameters as intercations will be on the same device */
  451. hwep_info->dev = dev;
  452. strlcpy(hwep_info->driver_name, BTFMSLIM_DEV_NAME, DEVICE_NAME_MAX_LEN);
  453. hwep_info->drv = &btfmslim_hw_driver;
  454. hwep_info->dai_drv = btfmslim_dai_driver;
  455. hwep_info->num_dai = ARRAY_SIZE(btfmslim_dai_driver);
  456. hwep_info->num_dai = 2;
  457. hwep_info->num_mixer_ctrl = ARRAY_SIZE(status_controls);
  458. hwep_info->mixer_ctrl = status_controls;
  459. /* Register to hardware endpoint */
  460. ret = btfmcodec_register_hw_ep(hwep_info);
  461. if (ret) {
  462. BTFMSLIM_ERR("failed to register with btfmcodec driver hw interface (%d)", ret);
  463. goto end;
  464. }
  465. BTFMSLIM_INFO("Registered succesfull with BTFMCODEC HWEP interface\n");
  466. return ret;
  467. end:
  468. return ret;
  469. }
  470. void btfm_slim_unregister_hwep(void)
  471. {
  472. BTFMSLIM_INFO("Unregistered with BTFMCODEC HWEP interface");
  473. /* Unregister with BTFMCODEC HWEP driver */
  474. btfmcodec_unregister_hw_ep(BTFMSLIM_DEV_NAME);
  475. }
  476. MODULE_DESCRIPTION("BTFM Slimbus driver");
  477. MODULE_LICENSE("GPL v2");