btfm_slim_codec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/slimbus/slimbus.h>
  20. #include <linux/ratelimit.h>
  21. #include <linux/slab.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. #include "btfm_slim.h"
  28. static int bt_soc_enable_status;
  29. int btfm_feedback_ch_setting;
  30. static int btfm_slim_codec_write(struct snd_soc_codec *codec, unsigned int reg,
  31. unsigned int value)
  32. {
  33. return 0;
  34. }
  35. static unsigned int btfm_slim_codec_read(struct snd_soc_codec *codec,
  36. unsigned int reg)
  37. {
  38. return 0;
  39. }
  40. static int bt_soc_status_get(struct snd_kcontrol *kcontrol,
  41. struct snd_ctl_elem_value *ucontrol)
  42. {
  43. ucontrol->value.integer.value[0] = bt_soc_enable_status;
  44. return 1;
  45. }
  46. static int bt_soc_status_put(struct snd_kcontrol *kcontrol,
  47. struct snd_ctl_elem_value *ucontrol)
  48. {
  49. return 1;
  50. }
  51. static int btfm_get_feedback_ch_setting(struct snd_kcontrol *kcontrol,
  52. struct snd_ctl_elem_value *ucontrol)
  53. {
  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. btfm_feedback_ch_setting = ucontrol->value.integer.value[0];
  61. return 1;
  62. }
  63. static const struct snd_kcontrol_new status_controls[] = {
  64. SOC_SINGLE_EXT("BT SOC status", 0, 0, 1, 0,
  65. bt_soc_status_get,
  66. bt_soc_status_put),
  67. SOC_SINGLE_EXT("BT set feedback channel", 0, 0, 1, 0,
  68. btfm_get_feedback_ch_setting,
  69. btfm_put_feedback_ch_setting)
  70. };
  71. static int btfm_slim_codec_probe(struct snd_soc_codec *codec)
  72. {
  73. snd_soc_add_codec_controls(codec, status_controls,
  74. ARRAY_SIZE(status_controls));
  75. return 0;
  76. }
  77. static int btfm_slim_codec_remove(struct snd_soc_codec *codec)
  78. {
  79. return 0;
  80. }
  81. static int btfm_slim_dai_startup(struct snd_pcm_substream *substream,
  82. struct snd_soc_dai *dai)
  83. {
  84. int ret;
  85. struct btfmslim *btfmslim = dai->dev->platform_data;
  86. BTFMSLIM_DBG("substream = %s stream = %d dai->name = %s",
  87. substream->name, substream->stream, dai->name);
  88. ret = btfm_slim_hw_init(btfmslim);
  89. return ret;
  90. }
  91. static void btfm_slim_dai_shutdown(struct snd_pcm_substream *substream,
  92. struct snd_soc_dai *dai)
  93. {
  94. int i;
  95. struct btfmslim *btfmslim = dai->dev->platform_data;
  96. struct btfmslim_ch *ch;
  97. uint8_t rxport, grp = false, nchan = 1;
  98. BTFMSLIM_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
  99. dai->id, dai->rate);
  100. switch (dai->id) {
  101. case BTFM_FM_SLIM_TX:
  102. grp = true; nchan = 2;
  103. ch = btfmslim->tx_chs;
  104. rxport = 0;
  105. break;
  106. case BTFM_BT_SCO_SLIM_TX:
  107. ch = btfmslim->tx_chs;
  108. rxport = 0;
  109. break;
  110. case BTFM_BT_SCO_A2DP_SLIM_RX:
  111. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  112. ch = btfmslim->rx_chs;
  113. rxport = 1;
  114. break;
  115. case BTFM_SLIM_NUM_CODEC_DAIS:
  116. default:
  117. BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
  118. return;
  119. }
  120. /* Search for dai->id matched port handler */
  121. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  122. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  123. (ch->id != dai->id); ch++, i++)
  124. ;
  125. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  126. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  127. BTFMSLIM_ERR("ch is invalid!!");
  128. return;
  129. }
  130. btfm_slim_disable_ch(btfmslim, ch, rxport, grp, nchan);
  131. btfm_slim_hw_deinit(btfmslim);
  132. }
  133. static int btfm_slim_dai_hw_params(struct snd_pcm_substream *substream,
  134. struct snd_pcm_hw_params *params,
  135. struct snd_soc_dai *dai)
  136. {
  137. BTFMSLIM_DBG("dai->name = %s DAI-ID %x rate %d num_ch %d",
  138. dai->name, dai->id, params_rate(params),
  139. params_channels(params));
  140. return 0;
  141. }
  142. static int btfm_slim_dai_prepare(struct snd_pcm_substream *substream,
  143. struct snd_soc_dai *dai)
  144. {
  145. int i, ret = -EINVAL;
  146. struct btfmslim *btfmslim = dai->dev->platform_data;
  147. struct btfmslim_ch *ch;
  148. uint8_t rxport, grp = false, nchan = 1;
  149. bt_soc_enable_status = 0;
  150. BTFMSLIM_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
  151. dai->id, dai->rate);
  152. /* save sample rate */
  153. btfmslim->sample_rate = dai->rate;
  154. switch (dai->id) {
  155. case BTFM_FM_SLIM_TX:
  156. grp = true; nchan = 2;
  157. ch = btfmslim->tx_chs;
  158. rxport = 0;
  159. break;
  160. case BTFM_BT_SCO_SLIM_TX:
  161. ch = btfmslim->tx_chs;
  162. rxport = 0;
  163. break;
  164. case BTFM_BT_SCO_A2DP_SLIM_RX:
  165. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  166. ch = btfmslim->rx_chs;
  167. rxport = 1;
  168. break;
  169. case BTFM_SLIM_NUM_CODEC_DAIS:
  170. default:
  171. BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
  172. return ret;
  173. }
  174. /* Search for dai->id matched port handler */
  175. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
  176. (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
  177. (ch->id != dai->id); ch++, i++)
  178. ;
  179. if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
  180. (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
  181. BTFMSLIM_ERR("ch is invalid!!");
  182. return ret;
  183. }
  184. ret = btfm_slim_enable_ch(btfmslim, ch, rxport, dai->rate, grp, nchan);
  185. /* save the enable channel status */
  186. if (ret == 0)
  187. bt_soc_enable_status = 1;
  188. return ret;
  189. }
  190. /* This function will be called once during boot up */
  191. static int btfm_slim_dai_set_channel_map(struct snd_soc_dai *dai,
  192. unsigned int tx_num, unsigned int *tx_slot,
  193. unsigned int rx_num, unsigned int *rx_slot)
  194. {
  195. int ret = -EINVAL, i;
  196. struct btfmslim *btfmslim = dai->dev->platform_data;
  197. struct btfmslim_ch *rx_chs;
  198. struct btfmslim_ch *tx_chs;
  199. BTFMSLIM_DBG("");
  200. if (!btfmslim)
  201. return ret;
  202. rx_chs = btfmslim->rx_chs;
  203. tx_chs = btfmslim->tx_chs;
  204. if (!rx_chs || !tx_chs)
  205. return ret;
  206. BTFMSLIM_DBG("Rx: id\tname\tport\thdl\tch\tch_hdl");
  207. for (i = 0; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < rx_num);
  208. i++, rx_chs++) {
  209. /* Set Rx Channel number from machine driver and
  210. * get channel handler from slimbus driver
  211. */
  212. rx_chs->ch = *(uint8_t *)(rx_slot + i);
  213. ret = slim_query_ch(btfmslim->slim_pgd, rx_chs->ch,
  214. &rx_chs->ch_hdl);
  215. if (ret < 0) {
  216. BTFMSLIM_ERR("slim_query_ch failure ch#%d - ret[%d]",
  217. rx_chs->ch, ret);
  218. goto error;
  219. }
  220. BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", rx_chs->id,
  221. rx_chs->name, rx_chs->port, rx_chs->port_hdl,
  222. rx_chs->ch, rx_chs->ch_hdl);
  223. }
  224. BTFMSLIM_DBG("Tx: id\tname\tport\thdl\tch\tch_hdl");
  225. for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < tx_num);
  226. i++, tx_chs++) {
  227. /* Set Tx Channel number from machine driver and
  228. * get channel handler from slimbus driver
  229. */
  230. tx_chs->ch = *(uint8_t *)(tx_slot + i);
  231. ret = slim_query_ch(btfmslim->slim_pgd, tx_chs->ch,
  232. &tx_chs->ch_hdl);
  233. if (ret < 0) {
  234. BTFMSLIM_ERR("slim_query_ch failure ch#%d - ret[%d]",
  235. tx_chs->ch, ret);
  236. goto error;
  237. }
  238. BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", tx_chs->id,
  239. tx_chs->name, tx_chs->port, tx_chs->port_hdl,
  240. tx_chs->ch, tx_chs->ch_hdl);
  241. }
  242. error:
  243. return ret;
  244. }
  245. static int btfm_slim_dai_get_channel_map(struct snd_soc_dai *dai,
  246. unsigned int *tx_num, unsigned int *tx_slot,
  247. unsigned int *rx_num, unsigned int *rx_slot)
  248. {
  249. int i, ret = -EINVAL, *slot = NULL, j = 0, num = 1;
  250. struct btfmslim *btfmslim = dai->dev->platform_data;
  251. struct btfmslim_ch *ch = NULL;
  252. if (!btfmslim)
  253. return ret;
  254. switch (dai->id) {
  255. case BTFM_FM_SLIM_TX:
  256. num = 2;
  257. case BTFM_BT_SCO_SLIM_TX:
  258. if (!tx_slot || !tx_num) {
  259. BTFMSLIM_ERR("Invalid tx_slot %p or tx_num %p",
  260. tx_slot, tx_num);
  261. return -EINVAL;
  262. }
  263. ch = btfmslim->tx_chs;
  264. if (!ch)
  265. return -EINVAL;
  266. slot = tx_slot;
  267. *rx_slot = 0;
  268. *tx_num = num;
  269. *rx_num = 0;
  270. break;
  271. case BTFM_BT_SCO_A2DP_SLIM_RX:
  272. case BTFM_BT_SPLIT_A2DP_SLIM_RX:
  273. if (!rx_slot || !rx_num) {
  274. BTFMSLIM_ERR("Invalid rx_slot %p or rx_num %p",
  275. rx_slot, rx_num);
  276. return -EINVAL;
  277. }
  278. ch = btfmslim->rx_chs;
  279. if (!ch)
  280. return -EINVAL;
  281. slot = rx_slot;
  282. *tx_slot = 0;
  283. *tx_num = 0;
  284. *rx_num = num;
  285. break;
  286. default:
  287. BTFMSLIM_ERR("Unsupported DAI %d", dai->id);
  288. return -EINVAL;
  289. }
  290. do {
  291. if (!ch)
  292. return -EINVAL;
  293. for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id !=
  294. BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id != dai->id);
  295. ch++, i++)
  296. ;
  297. if (ch->id == BTFM_SLIM_NUM_CODEC_DAIS ||
  298. i == BTFM_SLIM_NUM_CODEC_DAIS) {
  299. BTFMSLIM_ERR(
  300. "No channel has been allocated for dai (%d)",
  301. dai->id);
  302. return -EINVAL;
  303. }
  304. if (!slot)
  305. return -EINVAL;
  306. *(slot + j) = ch->ch;
  307. BTFMSLIM_DBG("id:%d, port:%d, ch:%d, slot: %d", ch->id,
  308. ch->port, ch->ch, *(slot + j));
  309. /* In case it has mulitiple channels */
  310. if (++j < num)
  311. ch++;
  312. } while (j < num);
  313. return 0;
  314. }
  315. static struct snd_soc_dai_ops btfmslim_dai_ops = {
  316. .startup = btfm_slim_dai_startup,
  317. .shutdown = btfm_slim_dai_shutdown,
  318. .hw_params = btfm_slim_dai_hw_params,
  319. .prepare = btfm_slim_dai_prepare,
  320. .set_channel_map = btfm_slim_dai_set_channel_map,
  321. .get_channel_map = btfm_slim_dai_get_channel_map,
  322. };
  323. static struct snd_soc_dai_driver btfmslim_dai[] = {
  324. { /* FM Audio data multiple channel : FM -> qdsp */
  325. .name = "btfm_fm_slim_tx",
  326. .id = BTFM_FM_SLIM_TX,
  327. .capture = {
  328. .stream_name = "FM TX Capture",
  329. .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
  330. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  331. .rate_max = 48000,
  332. .rate_min = 48000,
  333. .channels_min = 1,
  334. .channels_max = 2,
  335. },
  336. .ops = &btfmslim_dai_ops,
  337. },
  338. { /* Bluetooth SCO voice uplink: bt -> modem */
  339. .name = "btfm_bt_sco_slim_tx",
  340. .id = BTFM_BT_SCO_SLIM_TX,
  341. .capture = {
  342. .stream_name = "SCO TX Capture",
  343. /* 8 KHz or 16 KHz */
  344. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  345. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  346. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
  347. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  348. .rate_max = 96000,
  349. .rate_min = 8000,
  350. .channels_min = 1,
  351. .channels_max = 1,
  352. },
  353. .ops = &btfmslim_dai_ops,
  354. },
  355. { /* Bluetooth SCO voice downlink: modem -> bt or A2DP Playback */
  356. .name = "btfm_bt_sco_a2dp_slim_rx",
  357. .id = BTFM_BT_SCO_A2DP_SLIM_RX,
  358. .playback = {
  359. .stream_name = "SCO A2DP RX Playback",
  360. /* 8/16/44.1/48/88.2/96 Khz */
  361. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
  362. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
  363. | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
  364. .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
  365. .rate_max = 96000,
  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 struct snd_soc_codec_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 device *dev)
  394. {
  395. int ret = 0;
  396. BTFMSLIM_DBG("");
  397. /* Register Codec driver */
  398. ret = snd_soc_register_codec(dev, &btfmslim_codec,
  399. btfmslim_dai, ARRAY_SIZE(btfmslim_dai));
  400. if (ret)
  401. BTFMSLIM_ERR("failed to register codec (%d)", ret);
  402. return ret;
  403. }
  404. void btfm_slim_unregister_codec(struct device *dev)
  405. {
  406. BTFMSLIM_DBG("");
  407. /* Unregister Codec driver */
  408. snd_soc_unregister_codec(dev);
  409. }
  410. MODULE_DESCRIPTION("BTFM Slimbus Codec driver");
  411. MODULE_LICENSE("GPL v2");