btfm_slim_codec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021 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. static int bt_soc_enable_status;
  24. int btfm_feedback_ch_setting;
  25. static int btfm_slim_codec_write(struct snd_soc_component *codec,
  26. unsigned int reg, unsigned int value)
  27. {
  28. BTFMSLIM_DBG("");
  29. return 0;
  30. }
  31. static unsigned int btfm_slim_codec_read(struct snd_soc_component *codec,
  32. unsigned int reg)
  33. {
  34. BTFMSLIM_DBG("");
  35. return 0;
  36. }
  37. static int btfm_soc_status_get(struct snd_kcontrol *kcontrol,
  38. struct snd_ctl_elem_value *ucontrol)
  39. {
  40. BTFMSLIM_DBG("");
  41. ucontrol->value.integer.value[0] = bt_soc_enable_status;
  42. return 1;
  43. }
  44. static int btfm_soc_status_put(struct snd_kcontrol *kcontrol,
  45. struct snd_ctl_elem_value *ucontrol)
  46. {
  47. BTFMSLIM_DBG("");
  48. return 1;
  49. }
  50. static int btfm_get_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  51. struct snd_ctl_elem_value *ucontrol)
  52. {
  53. BTFMSLIM_DBG("");
  54. ucontrol->value.integer.value[0] = btfm_feedback_ch_setting;
  55. return 1;
  56. }
  57. static int btfm_put_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  58. struct snd_ctl_elem_value *ucontrol)
  59. {
  60. BTFMSLIM_DBG("");
  61. btfm_feedback_ch_setting = ucontrol->value.integer.value[0];
  62. return 1;
  63. }
  64. static const struct snd_kcontrol_new status_controls[] = {
  65. SOC_SINGLE_EXT("BT SOC status", 0, 0, 1, 0,
  66. btfm_soc_status_get,
  67. btfm_soc_status_put),
  68. SOC_SINGLE_EXT("BT set feedback channel", 0, 0, 1, 0,
  69. btfm_get_feedback_ch_setting,
  70. btfm_put_feedback_ch_setting)
  71. };
  72. static int btfm_slim_codec_probe(struct snd_soc_component *codec)
  73. {
  74. BTFMSLIM_DBG("");
  75. snd_soc_add_component_controls(codec, status_controls,
  76. ARRAY_SIZE(status_controls));
  77. return 0;
  78. }
  79. static void btfm_slim_codec_remove(struct snd_soc_component *codec)
  80. {
  81. BTFMSLIM_DBG("");
  82. }
  83. static int btfm_slim_dai_startup(struct snd_pcm_substream *substream,
  84. struct snd_soc_dai *dai)
  85. {
  86. int ret = -1;
  87. struct btfmslim *btfmslim = snd_soc_component_get_drvdata(dai->component);
  88. BTFMSLIM_DBG("substream = %s stream = %d dai->name = %s",
  89. substream->name, substream->stream, dai->name);
  90. ret = btfm_slim_hw_init(btfmslim);
  91. return ret;
  92. }
  93. static void btfm_slim_dai_shutdown(struct snd_pcm_substream *substream,
  94. struct snd_soc_dai *dai)
  95. {
  96. int i;
  97. struct btfmslim *btfmslim = snd_soc_component_get_drvdata(dai->component);
  98. struct btfmslim_ch *ch;
  99. uint8_t rxport, nchan = 1;
  100. BTFMSLIM_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
  101. dai->id, dai->rate);
  102. switch (dai->id) {
  103. case BTFM_FM_SLIM_TX:
  104. nchan = 2;
  105. ch = btfmslim->tx_chs;
  106. rxport = 0;
  107. break;
  108. case BTFM_BT_SCO_SLIM_TX:
  109. ch = btfmslim->tx_chs;
  110. rxport = 0;
  111. break;
  112. case BTFM_BT_SCO_A2DP_SLIM_RX:
  113. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  114. ch = btfmslim->rx_chs;
  115. rxport = 1;
  116. break;
  117. case BTFM_SLIM_NUM_CODEC_DAIS:
  118. default:
  119. BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
  120. return;
  121. }
  122. /* Search for dai->id matched port handler */
  123. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  124. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  125. (ch->id != dai->id); ch++, i++)
  126. ;
  127. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  128. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  129. BTFMSLIM_ERR("ch is invalid!!");
  130. return;
  131. }
  132. btfm_slim_disable_ch(btfmslim, ch, rxport, nchan);
  133. btfm_slim_hw_deinit(btfmslim);
  134. }
  135. static int btfm_slim_dai_hw_params(struct snd_pcm_substream *substream,
  136. struct snd_pcm_hw_params *params,
  137. struct snd_soc_dai *dai)
  138. {
  139. struct btfmslim *btfmslim;
  140. btfmslim = snd_soc_component_get_drvdata(dai->component);
  141. btfmslim->bps = params_width(params);
  142. btfmslim->direction = substream->stream;
  143. BTFMSLIM_DBG("dai->name = %s DAI-ID %x rate %d bps %d num_ch %d",
  144. dai->name, dai->id, params_rate(params), params_width(params),
  145. params_channels(params));
  146. return 0;
  147. }
  148. static int btfm_slim_dai_prepare(struct snd_pcm_substream *substream,
  149. struct snd_soc_dai *dai)
  150. {
  151. int ret = -EINVAL;
  152. int i = 0;
  153. struct btfmslim_ch *ch;
  154. uint8_t rxport, nchan = 1;
  155. struct btfmslim *btfmslim;
  156. btfmslim = snd_soc_component_get_drvdata(dai->component);
  157. btfmslim->direction = substream->stream;
  158. bt_soc_enable_status = 0;
  159. BTFMSLIM_INFO("dai->name: %s, dai->id: %d, dai->rate: %d direction: %d", dai->name,
  160. dai->id, dai->rate, btfmslim->direction);
  161. /* save sample rate */
  162. btfmslim->sample_rate = dai->rate;
  163. switch (dai->id) {
  164. case BTFM_FM_SLIM_TX:
  165. nchan = 2;
  166. ch = btfmslim->tx_chs;
  167. rxport = 0;
  168. break;
  169. case BTFM_BT_SCO_SLIM_TX:
  170. ch = btfmslim->tx_chs;
  171. rxport = 0;
  172. break;
  173. case BTFM_BT_SCO_A2DP_SLIM_RX:
  174. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  175. ch = btfmslim->rx_chs;
  176. rxport = 1;
  177. break;
  178. case BTFM_SLIM_NUM_CODEC_DAIS:
  179. default:
  180. BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
  181. return ret;
  182. }
  183. /* Search for dai->id matched port handler */
  184. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  185. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  186. (ch->id != dai->id); ch++, i++)
  187. ;
  188. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  189. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  190. BTFMSLIM_ERR("ch is invalid!!");
  191. return ret;
  192. }
  193. ret = btfm_slim_enable_ch(btfmslim, ch, rxport, dai->rate, nchan);
  194. /* save the enable channel status */
  195. if (ret == 0)
  196. bt_soc_enable_status = 1;
  197. if (ret == -EISCONN) {
  198. BTFMSLIM_ERR("channel opened without closing, returning success");
  199. ret = 0;
  200. }
  201. return ret;
  202. }
  203. /* This function will be called once during boot up */
  204. static int btfm_slim_dai_set_channel_map(struct snd_soc_dai *dai,
  205. unsigned int tx_num, unsigned int *tx_slot,
  206. unsigned int rx_num, unsigned int *rx_slot)
  207. {
  208. int ret = 0, i;
  209. struct btfmslim *btfmslim = snd_soc_component_get_drvdata(dai->component);
  210. struct btfmslim_ch *rx_chs;
  211. struct btfmslim_ch *tx_chs;
  212. BTFMSLIM_DBG("");
  213. if (!btfmslim)
  214. return -EINVAL;
  215. rx_chs = btfmslim->rx_chs;
  216. tx_chs = btfmslim->tx_chs;
  217. if (!rx_chs || !tx_chs)
  218. return ret;
  219. BTFMSLIM_DBG("Rx: id\tname\tport\tch");
  220. for (i = 0; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < rx_num);
  221. i++, rx_chs++) {
  222. /* Set Rx Channel number from machine driver and
  223. * get channel handler from slimbus driver
  224. */
  225. rx_chs->ch = *(uint8_t *)(rx_slot + i);
  226. BTFMSLIM_DBG(" %d\t%s\t%d\t%d\t", rx_chs->id,
  227. rx_chs->name, rx_chs->port, rx_chs->ch);
  228. }
  229. BTFMSLIM_DBG("Tx: id\tname\tport\tch");
  230. for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < tx_num);
  231. i++, tx_chs++) {
  232. /* Set Tx Channel number from machine driver and
  233. * get channel handler from slimbus driver
  234. */
  235. tx_chs->ch = *(uint8_t *)(tx_slot + i);
  236. BTFMSLIM_DBG(" %d\t%s\t%d\t%d\t", tx_chs->id,
  237. tx_chs->name, tx_chs->port, tx_chs->ch);
  238. }
  239. return ret;
  240. }
  241. static int btfm_slim_dai_get_channel_map(struct snd_soc_dai *dai,
  242. unsigned int *tx_num, unsigned int *tx_slot,
  243. unsigned int *rx_num, unsigned int *rx_slot)
  244. {
  245. int i, ret = -EINVAL, *slot = NULL, j = 0, num = 1;
  246. struct btfmslim *btfmslim = snd_soc_component_get_drvdata(dai->component);
  247. struct btfmslim_ch *ch = NULL;
  248. if (!btfmslim)
  249. return ret;
  250. switch (dai->id) {
  251. case BTFM_FM_SLIM_TX:
  252. num = 2;
  253. /* fall through */
  254. fallthrough;
  255. case BTFM_BT_SCO_SLIM_TX:
  256. if (!tx_slot || !tx_num) {
  257. BTFMSLIM_ERR("Invalid tx_slot %p or tx_num %p",
  258. tx_slot, tx_num);
  259. return -EINVAL;
  260. }
  261. ch = btfmslim->tx_chs;
  262. if (!ch)
  263. return -EINVAL;
  264. slot = tx_slot;
  265. *rx_slot = 0;
  266. *tx_num = num;
  267. *rx_num = 0;
  268. break;
  269. case BTFM_BT_SCO_A2DP_SLIM_RX:
  270. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  271. if (!rx_slot || !rx_num) {
  272. BTFMSLIM_ERR("Invalid rx_slot %p or rx_num %p",
  273. rx_slot, rx_num);
  274. return -EINVAL;
  275. }
  276. ch = btfmslim->rx_chs;
  277. if (!ch)
  278. return -EINVAL;
  279. slot = rx_slot;
  280. *tx_slot = 0;
  281. *tx_num = 0;
  282. *rx_num = num;
  283. break;
  284. default:
  285. BTFMSLIM_ERR("Unsupported DAI %d", dai->id);
  286. return -EINVAL;
  287. }
  288. do {
  289. if (!ch)
  290. return -EINVAL;
  291. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id !=
  292. BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id != dai->id);
  293. ch++, i++)
  294. ;
  295. if (ch->id == BTFM_SLIM_NUM_CODEC_DAIS ||
  296. i == BTFM_SLIM_NUM_CODEC_DAIS) {
  297. BTFMSLIM_ERR(
  298. "No channel has been allocated for dai (%d)",
  299. dai->id);
  300. return -EINVAL;
  301. }
  302. if (!slot)
  303. return -EINVAL;
  304. *(slot + j) = ch->ch;
  305. BTFMSLIM_DBG("id:%d, port:%d, ch:%d, slot: %d", ch->id,
  306. ch->port, ch->ch, *(slot + j));
  307. /* In case it has mulitiple channels */
  308. if (++j < num)
  309. ch++;
  310. } while (j < num);
  311. return 0;
  312. }
  313. static struct snd_soc_dai_ops btfmslim_dai_ops = {
  314. .startup = btfm_slim_dai_startup,
  315. .shutdown = btfm_slim_dai_shutdown,
  316. .hw_params = btfm_slim_dai_hw_params,
  317. .prepare = btfm_slim_dai_prepare,
  318. .set_channel_map = btfm_slim_dai_set_channel_map,
  319. .get_channel_map = btfm_slim_dai_get_channel_map,
  320. };
  321. static struct snd_soc_dai_driver btfmslim_dai[] = {
  322. { /* FM Audio data multiple channel : FM -> qdsp */
  323. .name = "btfm_fm_slim_tx",
  324. .id = BTFM_FM_SLIM_TX,
  325. .capture = {
  326. .stream_name = "FM TX Capture",
  327. .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
  328. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  329. .rate_max = 48000,
  330. .rate_min = 48000,
  331. .channels_min = 1,
  332. .channels_max = 2,
  333. },
  334. .ops = &btfmslim_dai_ops,
  335. },
  336. { /* Bluetooth SCO voice uplink: bt -> lpass */
  337. .name = "btfm_bt_sco_slim_tx",
  338. .id = BTFM_BT_SCO_SLIM_TX,
  339. .capture = {
  340. .stream_name = "SCO TX Capture",
  341. /* 8 KHz or 16 KHz */
  342. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  343. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  344. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  345. | SNDRV_PCM_RATE_192000,
  346. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  347. .rate_max = 192000,
  348. .rate_min = 8000,
  349. .channels_min = 1,
  350. .channels_max = 1,
  351. },
  352. .ops = &btfmslim_dai_ops,
  353. },
  354. { /* Bluetooth SCO voice downlink: lpass -> bt or A2DP Playback */
  355. .name = "btfm_bt_sco_a2dp_slim_rx",
  356. .id = BTFM_BT_SCO_A2DP_SLIM_RX,
  357. .playback = {
  358. .stream_name = "SCO A2DP RX Playback",
  359. /* 8/16/44.1/48/88.2/96 Khz */
  360. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  361. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  362. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000
  363. | SNDRV_PCM_RATE_192000,
  364. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  365. .rate_max = 192000,
  366. .rate_min = 8000,
  367. .channels_min = 1,
  368. .channels_max = 1,
  369. },
  370. .ops = &btfmslim_dai_ops,
  371. },
  372. { /* Bluetooth Split A2DP data: qdsp -> bt */
  373. .name = "btfm_bt_split_a2dp_slim_rx",
  374. .id = BTFM_BT_SPLIT_A2DP_SLIM_RX,
  375. .playback = {
  376. .stream_name = "SPLIT A2DP Playback",
  377. .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
  378. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  379. .rate_max = 48000,
  380. .rate_min = 48000,
  381. .channels_min = 1,
  382. .channels_max = 1,
  383. },
  384. .ops = &btfmslim_dai_ops,
  385. },
  386. };
  387. static const struct snd_soc_component_driver btfmslim_codec = {
  388. .probe = btfm_slim_codec_probe,
  389. .remove = btfm_slim_codec_remove,
  390. .read = btfm_slim_codec_read,
  391. .write = btfm_slim_codec_write,
  392. };
  393. int btfm_slim_register_codec(struct btfmslim *btfm_slim)
  394. {
  395. int ret = 0;
  396. struct device *dev = btfm_slim->dev;
  397. BTFMSLIM_DBG("");
  398. dev_err(dev, "\n");
  399. /* Register Codec driver */
  400. ret = snd_soc_register_component(dev, &btfmslim_codec,
  401. btfmslim_dai, ARRAY_SIZE(btfmslim_dai));
  402. if (ret)
  403. BTFMSLIM_ERR("failed to register codec (%d)", ret);
  404. return ret;
  405. }
  406. void btfm_slim_unregister_codec(struct device *dev)
  407. {
  408. BTFMSLIM_DBG("");
  409. /* Unregister Codec driver */
  410. snd_soc_unregister_component(dev);
  411. }
  412. MODULE_DESCRIPTION("BTFM Slimbus Codec driver");
  413. MODULE_LICENSE("GPL v2");