btfm_slim_hw_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  152. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  153. BTFMSLIM_DBG("");
  154. btfmslim->bps = bps;
  155. btfmslim->direction = direction;
  156. return 0;
  157. }
  158. void btfm_get_sampling_rate(uint32_t *sampling_rate)
  159. {
  160. uint8_t codec_types_avb = ARRAY_SIZE(codec_text);
  161. if (usecase_codec > (codec_types_avb - 1)) {
  162. BTFMSLIM_ERR("falling back to use default sampling_rate: %u",
  163. *sampling_rate);
  164. return;
  165. }
  166. if (*sampling_rate == 44100 || *sampling_rate == 48000) {
  167. if (usecase_codec == LDAC ||
  168. usecase_codec == APTX_AD)
  169. *sampling_rate = (*sampling_rate) *2;
  170. }
  171. if (usecase_codec == LC3_VOICE ||
  172. usecase_codec == APTX_AD_SPEECH ||
  173. usecase_codec == LC3 || usecase_codec == APTX_AD_QLEA) {
  174. *sampling_rate = 96000;
  175. }
  176. BTFMSLIM_INFO("current usecase codec type %s and sampling rate:%u khz",
  177. codec_text[usecase_codec], *sampling_rate);
  178. }
  179. static int btfm_slim_dai_prepare(void *dai, uint32_t sampling_rate, uint32_t direction, int id)
  180. {
  181. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  182. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  183. struct btfmslim_ch *ch;
  184. int ret = -EINVAL;
  185. int i = 0;
  186. uint8_t rxport, nchan = 1;
  187. btfmslim->direction = direction;
  188. bt_soc_enable_status = 0;
  189. btfm_get_sampling_rate(&sampling_rate);
  190. /* save sample rate */
  191. btfmslim->sample_rate = sampling_rate;
  192. switch (id) {
  193. case BTFM_FM_SLIM_TX:
  194. nchan = 2;
  195. ch = btfmslim->tx_chs;
  196. rxport = 0;
  197. break;
  198. case BTFM_BT_SCO_SLIM_TX:
  199. ch = btfmslim->tx_chs;
  200. rxport = 0;
  201. break;
  202. case BTFM_BT_SCO_A2DP_SLIM_RX:
  203. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  204. ch = btfmslim->rx_chs;
  205. rxport = 1;
  206. break;
  207. case BTFM_SLIM_NUM_CODEC_DAIS:
  208. default:
  209. BTFMSLIM_ERR("id is invalid:%d", id);
  210. return ret;
  211. }
  212. /* Search for dai->id matched port handler */
  213. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  214. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  215. (ch->id != id); ch++, i++)
  216. ;
  217. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  218. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  219. BTFMSLIM_ERR("ch is invalid!!");
  220. return ret;
  221. }
  222. ret = btfm_slim_enable_ch(btfmslim, ch, rxport, sampling_rate, nchan);
  223. /* save the enable channel status */
  224. if (ret == 0)
  225. bt_soc_enable_status = 1;
  226. if (ret == -EISCONN) {
  227. BTFMSLIM_ERR("channel opened without closing, returning success");
  228. ret = 0;
  229. }
  230. return ret;
  231. }
  232. /* This function will be called once during boot up */
  233. static int btfm_slim_dai_set_channel_map(void *dai,
  234. unsigned int tx_num, unsigned int *tx_slot,
  235. unsigned int rx_num, unsigned int *rx_slot)
  236. {
  237. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  238. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  239. struct btfmslim_ch *rx_chs;
  240. struct btfmslim_ch *tx_chs;
  241. int ret = 0, i;
  242. BTFMSLIM_DBG("");
  243. if (!btfmslim)
  244. return -EINVAL;
  245. rx_chs = btfmslim->rx_chs;
  246. tx_chs = btfmslim->tx_chs;
  247. if (!rx_chs || !tx_chs)
  248. return ret;
  249. BTFMSLIM_DBG("Rx: id\tname\tport\tch");
  250. for (i = 0; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < rx_num);
  251. i++, rx_chs++) {
  252. /* Set Rx Channel number from machine driver and
  253. * get channel handler from slimbus driver
  254. */
  255. rx_chs->ch = *(uint8_t *)(rx_slot + i);
  256. BTFMSLIM_DBG(" %d\t%s\t%d\t%x", rx_chs->id,
  257. rx_chs->name, rx_chs->port, rx_chs->ch);
  258. }
  259. BTFMSLIM_DBG("Tx: id\tname\tport\tch");
  260. for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < tx_num);
  261. i++, tx_chs++) {
  262. /* Set Tx Channel number from machine driver and
  263. * get channel handler from slimbus driver
  264. */
  265. tx_chs->ch = *(uint8_t *)(tx_slot + i);
  266. BTFMSLIM_DBG(" %d\t%s\t%d\t%x", tx_chs->id,
  267. tx_chs->name, tx_chs->port, tx_chs->ch);
  268. }
  269. return ret;
  270. }
  271. static int btfm_slim_dai_get_channel_map(void *dai,
  272. unsigned int *tx_num, unsigned int *tx_slot,
  273. unsigned int *rx_num, unsigned int *rx_slot, int id)
  274. {
  275. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  276. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  277. int i, ret = -EINVAL, *slot = NULL, j = 0, num = 1;
  278. struct btfmslim_ch *ch = NULL;
  279. BTFMSLIM_DBG("");
  280. if (!btfmslim)
  281. return ret;
  282. switch (id) {
  283. case BTFM_FM_SLIM_TX:
  284. num = 2;
  285. fallthrough;
  286. case BTFM_BT_SCO_SLIM_TX:
  287. if (!tx_slot || !tx_num) {
  288. BTFMSLIM_ERR("Invalid tx_slot %p or tx_num %p",
  289. tx_slot, tx_num);
  290. return -EINVAL;
  291. }
  292. ch = btfmslim->tx_chs;
  293. if (!ch)
  294. return -EINVAL;
  295. slot = tx_slot;
  296. *rx_slot = 0;
  297. *tx_num = num;
  298. *rx_num = 0;
  299. break;
  300. case BTFM_BT_SCO_A2DP_SLIM_RX:
  301. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  302. if (!rx_slot || !rx_num) {
  303. BTFMSLIM_ERR("Invalid rx_slot %p or rx_num %p",
  304. rx_slot, rx_num);
  305. return -EINVAL;
  306. }
  307. ch = btfmslim->rx_chs;
  308. if (!ch)
  309. return -EINVAL;
  310. slot = rx_slot;
  311. *tx_slot = 0;
  312. *tx_num = 0;
  313. *rx_num = num;
  314. break;
  315. default:
  316. BTFMSLIM_ERR("Unsupported DAI %d", id);
  317. return -EINVAL;
  318. }
  319. do {
  320. if (!ch)
  321. return -EINVAL;
  322. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id !=
  323. BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id != id);
  324. ch++, i++)
  325. ;
  326. if (ch->id == BTFM_SLIM_NUM_CODEC_DAIS ||
  327. i == BTFM_SLIM_NUM_CODEC_DAIS) {
  328. BTFMSLIM_ERR(
  329. "No channel has been allocated for dai (%d)",
  330. id);
  331. return -EINVAL;
  332. }
  333. if (!slot)
  334. return -EINVAL;
  335. *(slot + j) = ch->ch;
  336. BTFMSLIM_DBG("id:%d, port:%d, ch:%d, slot: %d", ch->id,
  337. ch->port, ch->ch, *(slot + j));
  338. /* In case it has mulitiple channels */
  339. if (++j < num)
  340. ch++;
  341. } while (j < num);
  342. return 0;
  343. }
  344. int btfm_slim_dai_get_configs (void * dai,
  345. struct master_hwep_configurations *hwep_config,
  346. uint8_t id)
  347. {
  348. struct hwep_data *hwep_info = (struct hwep_data *)dai;
  349. struct btfmslim *btfmslim = dev_get_drvdata(hwep_info->dev);
  350. struct btfmslim_ch *ch = NULL;
  351. int i = 0;
  352. BTFMSLIM_DBG("");
  353. hwep_config->stream_id = id;
  354. hwep_config->device_id = btfmslim->device_id;
  355. hwep_config->sample_rate = btfmslim->sample_rate;
  356. hwep_config->bit_width = (uint8_t)btfmslim->bps;
  357. hwep_config->codectype = usecase_codec;
  358. hwep_config->direction = btfmslim->direction;
  359. switch (id) {
  360. case BTFM_FM_SLIM_TX:
  361. case BTFM_BT_SCO_SLIM_TX:
  362. ch = btfmslim->tx_chs;
  363. break;
  364. case BTFM_BT_SCO_A2DP_SLIM_RX:
  365. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  366. ch = btfmslim->rx_chs;
  367. break;
  368. }
  369. for (; i < id ; i++) {
  370. if (ch[i].id == id) {
  371. BTFMSLIM_DBG("id matched");
  372. hwep_config->num_channels = 1;
  373. hwep_config->chan_num = ch[i].ch;
  374. break;
  375. }
  376. }
  377. return 1;
  378. }
  379. static struct hwep_dai_ops btfmslim_hw_dai_ops = {
  380. .hwep_startup = btfm_slim_dai_startup,
  381. .hwep_shutdown = btfm_slim_dai_shutdown,
  382. .hwep_hw_params = btfm_slim_dai_hw_params,
  383. .hwep_prepare = btfm_slim_dai_prepare,
  384. .hwep_set_channel_map = btfm_slim_dai_set_channel_map,
  385. .hwep_get_channel_map = btfm_slim_dai_get_channel_map,
  386. .hwep_get_configs = btfm_slim_dai_get_configs,
  387. .hwep_codectype = &usecase_codec,
  388. };
  389. static struct hwep_dai_driver btfmslim_dai_driver[] = {
  390. { /* Bluetooth SCO voice uplink: bt -> lpass */
  391. .dai_name = "btfm_bt_sco_slim_tx",
  392. .id = BTFM_BT_SCO_SLIM_TX,
  393. .capture = {
  394. .stream_name = "SCO TX Capture",
  395. /* 8 KHz or 16 KHz */
  396. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  397. | SNDRV_PCM_RATE_8000_192000
  398. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  399. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  400. | SNDRV_PCM_RATE_192000,
  401. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  402. .rate_max = 192000,
  403. .rate_min = 8000,
  404. .channels_min = 1,
  405. .channels_max = 1,
  406. },
  407. .dai_ops = &btfmslim_hw_dai_ops,
  408. },
  409. { /* Bluetooth SCO voice downlink: lpass -> bt or A2DP Playback */
  410. .dai_name = "btfm_bt_sco_a2dp_slim_rx",
  411. .id = BTFM_BT_SCO_A2DP_SLIM_RX,
  412. .playback = {
  413. .stream_name = "SCO A2DP RX Playback",
  414. /* 8/16/44.1/48/88.2/96 Khz */
  415. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  416. | SNDRV_PCM_RATE_8000_192000
  417. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  418. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  419. | SNDRV_PCM_RATE_192000,
  420. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  421. .rate_max = 192000,
  422. .rate_min = 8000,
  423. .channels_min = 1,
  424. .channels_max = 1,
  425. },
  426. .dai_ops = &btfmslim_hw_dai_ops,
  427. },
  428. };
  429. static struct hwep_comp_drv btfmslim_hw_driver = {
  430. .hwep_probe = btfm_slim_hwep_probe,
  431. .hwep_remove = btfm_slim_hwep_remove,
  432. .hwep_read = btfm_slim_hwep_read,
  433. .hwep_write = btfm_slim_hwep_write,
  434. };
  435. int btfm_slim_register_hw_ep(struct btfmslim *btfm_slim)
  436. {
  437. struct device *dev = btfm_slim->dev;
  438. struct hwep_data *hwep_info;
  439. int ret = 0;
  440. BTFMSLIM_INFO("Registering with BTFMCODEC HWEP interface\n");
  441. hwep_info = kzalloc(sizeof(struct hwep_data), GFP_KERNEL);
  442. if (!hwep_info) {
  443. BTFMSLIM_ERR("%s: failed to allocate memory\n", __func__);
  444. ret = -ENOMEM;
  445. goto end;
  446. }
  447. /* Copy EP device parameters as intercations will be on the same device */
  448. hwep_info->dev = dev;
  449. strlcpy(hwep_info->driver_name, BTFMSLIM_DEV_NAME, DEVICE_NAME_MAX_LEN);
  450. hwep_info->drv = &btfmslim_hw_driver;
  451. hwep_info->dai_drv = btfmslim_dai_driver;
  452. hwep_info->num_dai = ARRAY_SIZE(btfmslim_dai_driver);
  453. hwep_info->num_dai = 2;
  454. hwep_info->num_mixer_ctrl = ARRAY_SIZE(status_controls);
  455. hwep_info->mixer_ctrl = status_controls;
  456. set_bit(BTADV_AUDIO_MASTER_CONFIG, &hwep_info->flags);
  457. /* Register to hardware endpoint */
  458. ret = btfmcodec_register_hw_ep(hwep_info);
  459. if (ret) {
  460. BTFMSLIM_ERR("failed to register with btfmcodec driver hw interface (%d)", ret);
  461. goto end;
  462. }
  463. BTFMSLIM_INFO("Registered succesfull with BTFMCODEC HWEP interface\n");
  464. return ret;
  465. end:
  466. return ret;
  467. }
  468. void btfm_slim_unregister_hwep(void)
  469. {
  470. BTFMSLIM_INFO("Unregistered with BTFMCODEC HWEP interface");
  471. /* Unregister with BTFMCODEC HWEP driver */
  472. btfmcodec_unregister_hw_ep(BTFMSLIM_DEV_NAME);
  473. }
  474. MODULE_DESCRIPTION("BTFM Slimbus driver");
  475. MODULE_LICENSE("GPL v2");