18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// Mediatek ALSA BT SCO CVSD/MSBC Driver 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (c) 2019 MediaTek Inc. 68c2ecf20Sopenharmony_ci// Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/of_address.h> 118c2ecf20Sopenharmony_ci#include <linux/sched/clock.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <sound/soc.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define BTCVSD_SND_NAME "mtk-btcvsd-snd" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define BT_CVSD_TX_NREADY BIT(21) 188c2ecf20Sopenharmony_ci#define BT_CVSD_RX_READY BIT(22) 198c2ecf20Sopenharmony_ci#define BT_CVSD_TX_UNDERFLOW BIT(23) 208c2ecf20Sopenharmony_ci#define BT_CVSD_RX_OVERFLOW BIT(24) 218c2ecf20Sopenharmony_ci#define BT_CVSD_INTERRUPT BIT(31) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define BT_CVSD_CLEAR \ 248c2ecf20Sopenharmony_ci (BT_CVSD_TX_NREADY | BT_CVSD_RX_READY | BT_CVSD_TX_UNDERFLOW |\ 258c2ecf20Sopenharmony_ci BT_CVSD_RX_OVERFLOW | BT_CVSD_INTERRUPT) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* TX */ 288c2ecf20Sopenharmony_ci#define SCO_TX_ENCODE_SIZE (60) 298c2ecf20Sopenharmony_ci/* 18 = 6 * 180 / SCO_TX_ENCODE_SIZE */ 308c2ecf20Sopenharmony_ci#define SCO_TX_PACKER_BUF_NUM (18) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* RX */ 338c2ecf20Sopenharmony_ci#define SCO_RX_PLC_SIZE (30) 348c2ecf20Sopenharmony_ci#define SCO_RX_PACKER_BUF_NUM (64) 358c2ecf20Sopenharmony_ci#define SCO_RX_PACKET_MASK (0x3F) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define SCO_CVSD_PACKET_VALID_SIZE 2 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define SCO_PACKET_120 120 408c2ecf20Sopenharmony_ci#define SCO_PACKET_180 180 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define BTCVSD_RX_PACKET_SIZE (SCO_RX_PLC_SIZE + SCO_CVSD_PACKET_VALID_SIZE) 438c2ecf20Sopenharmony_ci#define BTCVSD_TX_PACKET_SIZE (SCO_TX_ENCODE_SIZE) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define BTCVSD_RX_BUF_SIZE (BTCVSD_RX_PACKET_SIZE * SCO_RX_PACKER_BUF_NUM) 468c2ecf20Sopenharmony_ci#define BTCVSD_TX_BUF_SIZE (BTCVSD_TX_PACKET_SIZE * SCO_TX_PACKER_BUF_NUM) 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cienum bt_sco_state { 498c2ecf20Sopenharmony_ci BT_SCO_STATE_IDLE, 508c2ecf20Sopenharmony_ci BT_SCO_STATE_RUNNING, 518c2ecf20Sopenharmony_ci BT_SCO_STATE_ENDING, 528c2ecf20Sopenharmony_ci BT_SCO_STATE_LOOPBACK, 538c2ecf20Sopenharmony_ci}; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cienum bt_sco_direct { 568c2ecf20Sopenharmony_ci BT_SCO_DIRECT_BT2ARM, 578c2ecf20Sopenharmony_ci BT_SCO_DIRECT_ARM2BT, 588c2ecf20Sopenharmony_ci}; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cienum bt_sco_packet_len { 618c2ecf20Sopenharmony_ci BT_SCO_CVSD_30 = 0, 628c2ecf20Sopenharmony_ci BT_SCO_CVSD_60, 638c2ecf20Sopenharmony_ci BT_SCO_CVSD_90, 648c2ecf20Sopenharmony_ci BT_SCO_CVSD_120, 658c2ecf20Sopenharmony_ci BT_SCO_CVSD_10, 668c2ecf20Sopenharmony_ci BT_SCO_CVSD_20, 678c2ecf20Sopenharmony_ci BT_SCO_CVSD_MAX, 688c2ecf20Sopenharmony_ci}; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cienum BT_SCO_BAND { 718c2ecf20Sopenharmony_ci BT_SCO_NB, 728c2ecf20Sopenharmony_ci BT_SCO_WB, 738c2ecf20Sopenharmony_ci}; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistruct mtk_btcvsd_snd_hw_info { 768c2ecf20Sopenharmony_ci unsigned int num_valid_addr; 778c2ecf20Sopenharmony_ci unsigned long bt_sram_addr[20]; 788c2ecf20Sopenharmony_ci unsigned int packet_length; 798c2ecf20Sopenharmony_ci unsigned int packet_num; 808c2ecf20Sopenharmony_ci}; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistruct mtk_btcvsd_snd_stream { 838c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream; 848c2ecf20Sopenharmony_ci int stream; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci enum bt_sco_state state; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci unsigned int packet_size; 898c2ecf20Sopenharmony_ci unsigned int buf_size; 908c2ecf20Sopenharmony_ci u8 temp_packet_buf[SCO_PACKET_180]; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci int packet_w; 938c2ecf20Sopenharmony_ci int packet_r; 948c2ecf20Sopenharmony_ci snd_pcm_uframes_t prev_frame; 958c2ecf20Sopenharmony_ci int prev_packet_idx; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci unsigned int xrun:1; 988c2ecf20Sopenharmony_ci unsigned int timeout:1; 998c2ecf20Sopenharmony_ci unsigned int mute:1; 1008c2ecf20Sopenharmony_ci unsigned int trigger_start:1; 1018c2ecf20Sopenharmony_ci unsigned int wait_flag:1; 1028c2ecf20Sopenharmony_ci unsigned int rw_cnt; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci unsigned long long time_stamp; 1058c2ecf20Sopenharmony_ci unsigned long long buf_data_equivalent_time; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_hw_info buffer_info; 1088c2ecf20Sopenharmony_ci}; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistruct mtk_btcvsd_snd { 1118c2ecf20Sopenharmony_ci struct device *dev; 1128c2ecf20Sopenharmony_ci int irq_id; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci struct regmap *infra; 1158c2ecf20Sopenharmony_ci void __iomem *bt_pkv_base; 1168c2ecf20Sopenharmony_ci void __iomem *bt_sram_bank2_base; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci unsigned int infra_misc_offset; 1198c2ecf20Sopenharmony_ci unsigned int conn_bt_cvsd_mask; 1208c2ecf20Sopenharmony_ci unsigned int cvsd_mcu_read_offset; 1218c2ecf20Sopenharmony_ci unsigned int cvsd_mcu_write_offset; 1228c2ecf20Sopenharmony_ci unsigned int cvsd_packet_indicator; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci u32 *bt_reg_pkt_r; 1258c2ecf20Sopenharmony_ci u32 *bt_reg_pkt_w; 1268c2ecf20Sopenharmony_ci u32 *bt_reg_ctl; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci unsigned int irq_disabled:1; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci spinlock_t tx_lock; /* spinlock for bt tx stream control */ 1318c2ecf20Sopenharmony_ci spinlock_t rx_lock; /* spinlock for bt rx stream control */ 1328c2ecf20Sopenharmony_ci wait_queue_head_t tx_wait; 1338c2ecf20Sopenharmony_ci wait_queue_head_t rx_wait; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *tx; 1368c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *rx; 1378c2ecf20Sopenharmony_ci u8 tx_packet_buf[BTCVSD_TX_BUF_SIZE]; 1388c2ecf20Sopenharmony_ci u8 rx_packet_buf[BTCVSD_RX_BUF_SIZE]; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci enum BT_SCO_BAND band; 1418c2ecf20Sopenharmony_ci}; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistruct mtk_btcvsd_snd_time_buffer_info { 1448c2ecf20Sopenharmony_ci unsigned long long data_count_equi_time; 1458c2ecf20Sopenharmony_ci unsigned long long time_stamp_us; 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic const unsigned int btsco_packet_valid_mask[BT_SCO_CVSD_MAX][6] = { 1498c2ecf20Sopenharmony_ci {0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3, 0x1 << 4, 0x1 << 5}, 1508c2ecf20Sopenharmony_ci {0x1, 0x1, 0x2, 0x2, 0x4, 0x4}, 1518c2ecf20Sopenharmony_ci {0x1, 0x1, 0x1, 0x2, 0x2, 0x2}, 1528c2ecf20Sopenharmony_ci {0x1, 0x1, 0x1, 0x1, 0x0, 0x0}, 1538c2ecf20Sopenharmony_ci {0x7, 0x7 << 3, 0x7 << 6, 0x7 << 9, 0x7 << 12, 0x7 << 15}, 1548c2ecf20Sopenharmony_ci {0x3, 0x3 << 1, 0x3 << 3, 0x3 << 4, 0x3 << 6, 0x3 << 7}, 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic const unsigned int btsco_packet_info[BT_SCO_CVSD_MAX][4] = { 1588c2ecf20Sopenharmony_ci {30, 6, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE, 1598c2ecf20Sopenharmony_ci SCO_PACKET_180 / SCO_RX_PLC_SIZE}, 1608c2ecf20Sopenharmony_ci {60, 3, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE, 1618c2ecf20Sopenharmony_ci SCO_PACKET_180 / SCO_RX_PLC_SIZE}, 1628c2ecf20Sopenharmony_ci {90, 2, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE, 1638c2ecf20Sopenharmony_ci SCO_PACKET_180 / SCO_RX_PLC_SIZE}, 1648c2ecf20Sopenharmony_ci {120, 1, SCO_PACKET_120 / SCO_TX_ENCODE_SIZE, 1658c2ecf20Sopenharmony_ci SCO_PACKET_120 / SCO_RX_PLC_SIZE}, 1668c2ecf20Sopenharmony_ci {10, 18, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE, 1678c2ecf20Sopenharmony_ci SCO_PACKET_180 / SCO_RX_PLC_SIZE}, 1688c2ecf20Sopenharmony_ci {20, 9, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE, 1698c2ecf20Sopenharmony_ci SCO_PACKET_180 / SCO_RX_PLC_SIZE}, 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic const u8 table_msbc_silence[SCO_PACKET_180] = { 1738c2ecf20Sopenharmony_ci 0x01, 0x38, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 1748c2ecf20Sopenharmony_ci 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 1758c2ecf20Sopenharmony_ci 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 1768c2ecf20Sopenharmony_ci 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 1778c2ecf20Sopenharmony_ci 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 1788c2ecf20Sopenharmony_ci 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00, 1798c2ecf20Sopenharmony_ci 0x01, 0xc8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 1808c2ecf20Sopenharmony_ci 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 1818c2ecf20Sopenharmony_ci 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 1828c2ecf20Sopenharmony_ci 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 1838c2ecf20Sopenharmony_ci 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 1848c2ecf20Sopenharmony_ci 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00, 1858c2ecf20Sopenharmony_ci 0x01, 0xf8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 1868c2ecf20Sopenharmony_ci 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 1878c2ecf20Sopenharmony_ci 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 1888c2ecf20Sopenharmony_ci 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 1898c2ecf20Sopenharmony_ci 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 1908c2ecf20Sopenharmony_ci 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00 1918c2ecf20Sopenharmony_ci}; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic void mtk_btcvsd_snd_irq_enable(struct mtk_btcvsd_snd *bt) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci regmap_update_bits(bt->infra, bt->infra_misc_offset, 1968c2ecf20Sopenharmony_ci bt->conn_bt_cvsd_mask, 0); 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic void mtk_btcvsd_snd_irq_disable(struct mtk_btcvsd_snd *bt) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci regmap_update_bits(bt->infra, bt->infra_misc_offset, 2028c2ecf20Sopenharmony_ci bt->conn_bt_cvsd_mask, bt->conn_bt_cvsd_mask); 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic void mtk_btcvsd_snd_set_state(struct mtk_btcvsd_snd *bt, 2068c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream, 2078c2ecf20Sopenharmony_ci int state) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), stream %d, state %d, tx->state %d, rx->state %d, irq_disabled %d\n", 2108c2ecf20Sopenharmony_ci __func__, 2118c2ecf20Sopenharmony_ci bt_stream->stream, state, 2128c2ecf20Sopenharmony_ci bt->tx->state, bt->rx->state, bt->irq_disabled); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci bt_stream->state = state; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci if (bt->tx->state == BT_SCO_STATE_IDLE && 2178c2ecf20Sopenharmony_ci bt->rx->state == BT_SCO_STATE_IDLE) { 2188c2ecf20Sopenharmony_ci if (!bt->irq_disabled) { 2198c2ecf20Sopenharmony_ci disable_irq(bt->irq_id); 2208c2ecf20Sopenharmony_ci mtk_btcvsd_snd_irq_disable(bt); 2218c2ecf20Sopenharmony_ci bt->irq_disabled = 1; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci } else { 2248c2ecf20Sopenharmony_ci if (bt->irq_disabled) { 2258c2ecf20Sopenharmony_ci enable_irq(bt->irq_id); 2268c2ecf20Sopenharmony_ci mtk_btcvsd_snd_irq_enable(bt); 2278c2ecf20Sopenharmony_ci bt->irq_disabled = 0; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic int mtk_btcvsd_snd_tx_init(struct mtk_btcvsd_snd *bt) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci memset(bt->tx, 0, sizeof(*bt->tx)); 2358c2ecf20Sopenharmony_ci memset(bt->tx_packet_buf, 0, sizeof(bt->tx_packet_buf)); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci bt->tx->packet_size = BTCVSD_TX_PACKET_SIZE; 2388c2ecf20Sopenharmony_ci bt->tx->buf_size = BTCVSD_TX_BUF_SIZE; 2398c2ecf20Sopenharmony_ci bt->tx->timeout = 0; 2408c2ecf20Sopenharmony_ci bt->tx->rw_cnt = 0; 2418c2ecf20Sopenharmony_ci bt->tx->stream = SNDRV_PCM_STREAM_PLAYBACK; 2428c2ecf20Sopenharmony_ci return 0; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic int mtk_btcvsd_snd_rx_init(struct mtk_btcvsd_snd *bt) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci memset(bt->rx, 0, sizeof(*bt->rx)); 2488c2ecf20Sopenharmony_ci memset(bt->rx_packet_buf, 0, sizeof(bt->rx_packet_buf)); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci bt->rx->packet_size = BTCVSD_RX_PACKET_SIZE; 2518c2ecf20Sopenharmony_ci bt->rx->buf_size = BTCVSD_RX_BUF_SIZE; 2528c2ecf20Sopenharmony_ci bt->rx->timeout = 0; 2538c2ecf20Sopenharmony_ci bt->rx->rw_cnt = 0; 2548c2ecf20Sopenharmony_ci bt->rx->stream = SNDRV_PCM_STREAM_CAPTURE; 2558c2ecf20Sopenharmony_ci return 0; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cistatic void get_tx_time_stamp(struct mtk_btcvsd_snd *bt, 2598c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_time_buffer_info *ts) 2608c2ecf20Sopenharmony_ci{ 2618c2ecf20Sopenharmony_ci ts->time_stamp_us = bt->tx->time_stamp; 2628c2ecf20Sopenharmony_ci ts->data_count_equi_time = bt->tx->buf_data_equivalent_time; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistatic void get_rx_time_stamp(struct mtk_btcvsd_snd *bt, 2668c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_time_buffer_info *ts) 2678c2ecf20Sopenharmony_ci{ 2688c2ecf20Sopenharmony_ci ts->time_stamp_us = bt->rx->time_stamp; 2698c2ecf20Sopenharmony_ci ts->data_count_equi_time = bt->rx->buf_data_equivalent_time; 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic int btcvsd_bytes_to_frame(struct snd_pcm_substream *substream, 2738c2ecf20Sopenharmony_ci int bytes) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci int count = bytes; 2768c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime = substream->runtime; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci if (runtime->format == SNDRV_PCM_FORMAT_S32_LE || 2798c2ecf20Sopenharmony_ci runtime->format == SNDRV_PCM_FORMAT_U32_LE) 2808c2ecf20Sopenharmony_ci count = count >> 2; 2818c2ecf20Sopenharmony_ci else 2828c2ecf20Sopenharmony_ci count = count >> 1; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci count = count / runtime->channels; 2858c2ecf20Sopenharmony_ci return count; 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic void mtk_btcvsd_snd_data_transfer(enum bt_sco_direct dir, 2898c2ecf20Sopenharmony_ci u8 *src, u8 *dst, 2908c2ecf20Sopenharmony_ci unsigned int blk_size, 2918c2ecf20Sopenharmony_ci unsigned int blk_num) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci unsigned int i, j; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci if (blk_size == 60 || blk_size == 120 || blk_size == 20) { 2968c2ecf20Sopenharmony_ci u32 *src_32 = (u32 *)src; 2978c2ecf20Sopenharmony_ci u32 *dst_32 = (u32 *)dst; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci for (i = 0; i < (blk_size * blk_num / 4); i++) 3008c2ecf20Sopenharmony_ci *dst_32++ = *src_32++; 3018c2ecf20Sopenharmony_ci } else { 3028c2ecf20Sopenharmony_ci u16 *src_16 = (u16 *)src; 3038c2ecf20Sopenharmony_ci u16 *dst_16 = (u16 *)dst; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci for (j = 0; j < blk_num; j++) { 3068c2ecf20Sopenharmony_ci for (i = 0; i < (blk_size / 2); i++) 3078c2ecf20Sopenharmony_ci *dst_16++ = *src_16++; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci if (dir == BT_SCO_DIRECT_BT2ARM) 3108c2ecf20Sopenharmony_ci src_16++; 3118c2ecf20Sopenharmony_ci else 3128c2ecf20Sopenharmony_ci dst_16++; 3138c2ecf20Sopenharmony_ci } 3148c2ecf20Sopenharmony_ci } 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci/* write encoded mute data to bt sram */ 3188c2ecf20Sopenharmony_cistatic int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci unsigned int i; 3218c2ecf20Sopenharmony_ci unsigned int num_valid_addr; 3228c2ecf20Sopenharmony_ci unsigned long flags; 3238c2ecf20Sopenharmony_ci enum BT_SCO_BAND band = bt->band; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci /* prepare encoded mute data */ 3268c2ecf20Sopenharmony_ci if (band == BT_SCO_NB) 3278c2ecf20Sopenharmony_ci memset(bt->tx->temp_packet_buf, 170, SCO_PACKET_180); 3288c2ecf20Sopenharmony_ci else 3298c2ecf20Sopenharmony_ci memcpy(bt->tx->temp_packet_buf, 3308c2ecf20Sopenharmony_ci table_msbc_silence, SCO_PACKET_180); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci /* write mute data to bt tx sram buffer */ 3338c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->tx_lock, flags); 3348c2ecf20Sopenharmony_ci num_valid_addr = bt->tx->buffer_info.num_valid_addr; 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci dev_info(bt->dev, "%s(), band %d, num_valid_addr %u\n", 3378c2ecf20Sopenharmony_ci __func__, band, num_valid_addr); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci for (i = 0; i < num_valid_addr; i++) { 3408c2ecf20Sopenharmony_ci void *dst; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci dev_info(bt->dev, "%s(), clean addr 0x%lx\n", __func__, 3438c2ecf20Sopenharmony_ci bt->tx->buffer_info.bt_sram_addr[i]); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci dst = (void *)bt->tx->buffer_info.bt_sram_addr[i]; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT, 3488c2ecf20Sopenharmony_ci bt->tx->temp_packet_buf, dst, 3498c2ecf20Sopenharmony_ci bt->tx->buffer_info.packet_length, 3508c2ecf20Sopenharmony_ci bt->tx->buffer_info.packet_num); 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->tx_lock, flags); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci return 0; 3558c2ecf20Sopenharmony_ci} 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_cistatic int mtk_btcvsd_read_from_bt(struct mtk_btcvsd_snd *bt, 3588c2ecf20Sopenharmony_ci enum bt_sco_packet_len packet_type, 3598c2ecf20Sopenharmony_ci unsigned int packet_length, 3608c2ecf20Sopenharmony_ci unsigned int packet_num, 3618c2ecf20Sopenharmony_ci unsigned int blk_size, 3628c2ecf20Sopenharmony_ci unsigned int control) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci unsigned int i; 3658c2ecf20Sopenharmony_ci int pv; 3668c2ecf20Sopenharmony_ci u8 *src; 3678c2ecf20Sopenharmony_ci unsigned int packet_buf_ofs; 3688c2ecf20Sopenharmony_ci unsigned long flags; 3698c2ecf20Sopenharmony_ci unsigned long connsys_addr_rx, ap_addr_rx; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci connsys_addr_rx = *bt->bt_reg_pkt_r; 3728c2ecf20Sopenharmony_ci ap_addr_rx = (unsigned long)bt->bt_sram_bank2_base + 3738c2ecf20Sopenharmony_ci (connsys_addr_rx & 0xFFFF); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (connsys_addr_rx == 0xdeadfeed) { 3768c2ecf20Sopenharmony_ci /* bt return 0xdeadfeed if read register during bt sleep */ 3778c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), connsys_addr_rx == 0xdeadfeed", 3788c2ecf20Sopenharmony_ci __func__); 3798c2ecf20Sopenharmony_ci return -EIO; 3808c2ecf20Sopenharmony_ci } 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci src = (u8 *)ap_addr_rx; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_BT2ARM, src, 3858c2ecf20Sopenharmony_ci bt->rx->temp_packet_buf, packet_length, 3868c2ecf20Sopenharmony_ci packet_num); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->rx_lock, flags); 3898c2ecf20Sopenharmony_ci for (i = 0; i < blk_size; i++) { 3908c2ecf20Sopenharmony_ci packet_buf_ofs = (bt->rx->packet_w & SCO_RX_PACKET_MASK) * 3918c2ecf20Sopenharmony_ci bt->rx->packet_size; 3928c2ecf20Sopenharmony_ci memcpy(bt->rx_packet_buf + packet_buf_ofs, 3938c2ecf20Sopenharmony_ci bt->rx->temp_packet_buf + (SCO_RX_PLC_SIZE * i), 3948c2ecf20Sopenharmony_ci SCO_RX_PLC_SIZE); 3958c2ecf20Sopenharmony_ci if ((control & btsco_packet_valid_mask[packet_type][i]) == 3968c2ecf20Sopenharmony_ci btsco_packet_valid_mask[packet_type][i]) 3978c2ecf20Sopenharmony_ci pv = 1; 3988c2ecf20Sopenharmony_ci else 3998c2ecf20Sopenharmony_ci pv = 0; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci packet_buf_ofs += SCO_RX_PLC_SIZE; 4028c2ecf20Sopenharmony_ci memcpy(bt->rx_packet_buf + packet_buf_ofs, (void *)&pv, 4038c2ecf20Sopenharmony_ci SCO_CVSD_PACKET_VALID_SIZE); 4048c2ecf20Sopenharmony_ci bt->rx->packet_w++; 4058c2ecf20Sopenharmony_ci } 4068c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->rx_lock, flags); 4078c2ecf20Sopenharmony_ci return 0; 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic int mtk_btcvsd_write_to_bt(struct mtk_btcvsd_snd *bt, 4118c2ecf20Sopenharmony_ci enum bt_sco_packet_len packet_type, 4128c2ecf20Sopenharmony_ci unsigned int packet_length, 4138c2ecf20Sopenharmony_ci unsigned int packet_num, 4148c2ecf20Sopenharmony_ci unsigned int blk_size) 4158c2ecf20Sopenharmony_ci{ 4168c2ecf20Sopenharmony_ci unsigned int i; 4178c2ecf20Sopenharmony_ci unsigned long flags; 4188c2ecf20Sopenharmony_ci u8 *dst; 4198c2ecf20Sopenharmony_ci unsigned long connsys_addr_tx, ap_addr_tx; 4208c2ecf20Sopenharmony_ci bool new_ap_addr_tx = true; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci connsys_addr_tx = *bt->bt_reg_pkt_w; 4238c2ecf20Sopenharmony_ci ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base + 4248c2ecf20Sopenharmony_ci (connsys_addr_tx & 0xFFFF); 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci if (connsys_addr_tx == 0xdeadfeed) { 4278c2ecf20Sopenharmony_ci /* bt return 0xdeadfeed if read register during bt sleep */ 4288c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n", 4298c2ecf20Sopenharmony_ci __func__); 4308c2ecf20Sopenharmony_ci return -EIO; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->tx_lock, flags); 4348c2ecf20Sopenharmony_ci for (i = 0; i < blk_size; i++) { 4358c2ecf20Sopenharmony_ci memcpy(bt->tx->temp_packet_buf + (bt->tx->packet_size * i), 4368c2ecf20Sopenharmony_ci (bt->tx_packet_buf + 4378c2ecf20Sopenharmony_ci (bt->tx->packet_r % SCO_TX_PACKER_BUF_NUM) * 4388c2ecf20Sopenharmony_ci bt->tx->packet_size), 4398c2ecf20Sopenharmony_ci bt->tx->packet_size); 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci bt->tx->packet_r++; 4428c2ecf20Sopenharmony_ci } 4438c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->tx_lock, flags); 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci dst = (u8 *)ap_addr_tx; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci if (!bt->tx->mute) { 4488c2ecf20Sopenharmony_ci mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT, 4498c2ecf20Sopenharmony_ci bt->tx->temp_packet_buf, dst, 4508c2ecf20Sopenharmony_ci packet_length, packet_num); 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci /* store bt tx buffer sram info */ 4548c2ecf20Sopenharmony_ci bt->tx->buffer_info.packet_length = packet_length; 4558c2ecf20Sopenharmony_ci bt->tx->buffer_info.packet_num = packet_num; 4568c2ecf20Sopenharmony_ci for (i = 0; i < bt->tx->buffer_info.num_valid_addr; i++) { 4578c2ecf20Sopenharmony_ci if (bt->tx->buffer_info.bt_sram_addr[i] == ap_addr_tx) { 4588c2ecf20Sopenharmony_ci new_ap_addr_tx = false; 4598c2ecf20Sopenharmony_ci break; 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci } 4628c2ecf20Sopenharmony_ci if (new_ap_addr_tx) { 4638c2ecf20Sopenharmony_ci unsigned int next_idx; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->tx_lock, flags); 4668c2ecf20Sopenharmony_ci bt->tx->buffer_info.num_valid_addr++; 4678c2ecf20Sopenharmony_ci next_idx = bt->tx->buffer_info.num_valid_addr - 1; 4688c2ecf20Sopenharmony_ci bt->tx->buffer_info.bt_sram_addr[next_idx] = ap_addr_tx; 4698c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->tx_lock, flags); 4708c2ecf20Sopenharmony_ci dev_info(bt->dev, "%s(), new ap_addr_tx = 0x%lx, num_valid_addr %d\n", 4718c2ecf20Sopenharmony_ci __func__, ap_addr_tx, 4728c2ecf20Sopenharmony_ci bt->tx->buffer_info.num_valid_addr); 4738c2ecf20Sopenharmony_ci } 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci if (bt->tx->mute) 4768c2ecf20Sopenharmony_ci btcvsd_tx_clean_buffer(bt); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci return 0; 4798c2ecf20Sopenharmony_ci} 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_cistatic irqreturn_t mtk_btcvsd_snd_irq_handler(int irq_id, void *dev) 4828c2ecf20Sopenharmony_ci{ 4838c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = dev; 4848c2ecf20Sopenharmony_ci unsigned int packet_type, packet_num, packet_length; 4858c2ecf20Sopenharmony_ci unsigned int buf_cnt_tx, buf_cnt_rx, control; 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci if (bt->rx->state != BT_SCO_STATE_RUNNING && 4888c2ecf20Sopenharmony_ci bt->rx->state != BT_SCO_STATE_ENDING && 4898c2ecf20Sopenharmony_ci bt->tx->state != BT_SCO_STATE_RUNNING && 4908c2ecf20Sopenharmony_ci bt->tx->state != BT_SCO_STATE_ENDING && 4918c2ecf20Sopenharmony_ci bt->tx->state != BT_SCO_STATE_LOOPBACK) { 4928c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), in idle state: rx->state: %d, tx->state: %d\n", 4938c2ecf20Sopenharmony_ci __func__, bt->rx->state, bt->tx->state); 4948c2ecf20Sopenharmony_ci goto irq_handler_exit; 4958c2ecf20Sopenharmony_ci } 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci control = *bt->bt_reg_ctl; 4988c2ecf20Sopenharmony_ci packet_type = (control >> 18) & 0x7; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci if (((control >> 31) & 1) == 0) { 5018c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), ((control >> 31) & 1) == 0, control 0x%x\n", 5028c2ecf20Sopenharmony_ci __func__, control); 5038c2ecf20Sopenharmony_ci goto irq_handler_exit; 5048c2ecf20Sopenharmony_ci } 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci if (packet_type >= BT_SCO_CVSD_MAX) { 5078c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), invalid packet_type %u, exit\n", 5088c2ecf20Sopenharmony_ci __func__, packet_type); 5098c2ecf20Sopenharmony_ci goto irq_handler_exit; 5108c2ecf20Sopenharmony_ci } 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci packet_length = btsco_packet_info[packet_type][0]; 5138c2ecf20Sopenharmony_ci packet_num = btsco_packet_info[packet_type][1]; 5148c2ecf20Sopenharmony_ci buf_cnt_tx = btsco_packet_info[packet_type][2]; 5158c2ecf20Sopenharmony_ci buf_cnt_rx = btsco_packet_info[packet_type][3]; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci if (bt->tx->state == BT_SCO_STATE_LOOPBACK) { 5188c2ecf20Sopenharmony_ci u8 *src, *dst; 5198c2ecf20Sopenharmony_ci unsigned long connsys_addr_rx, ap_addr_rx; 5208c2ecf20Sopenharmony_ci unsigned long connsys_addr_tx, ap_addr_tx; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci connsys_addr_rx = *bt->bt_reg_pkt_r; 5238c2ecf20Sopenharmony_ci ap_addr_rx = (unsigned long)bt->bt_sram_bank2_base + 5248c2ecf20Sopenharmony_ci (connsys_addr_rx & 0xFFFF); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci connsys_addr_tx = *bt->bt_reg_pkt_w; 5278c2ecf20Sopenharmony_ci ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base + 5288c2ecf20Sopenharmony_ci (connsys_addr_tx & 0xFFFF); 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci if (connsys_addr_tx == 0xdeadfeed || 5318c2ecf20Sopenharmony_ci connsys_addr_rx == 0xdeadfeed) { 5328c2ecf20Sopenharmony_ci /* bt return 0xdeadfeed if read reg during bt sleep */ 5338c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n", 5348c2ecf20Sopenharmony_ci __func__); 5358c2ecf20Sopenharmony_ci goto irq_handler_exit; 5368c2ecf20Sopenharmony_ci } 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci src = (u8 *)ap_addr_rx; 5398c2ecf20Sopenharmony_ci dst = (u8 *)ap_addr_tx; 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_BT2ARM, src, 5428c2ecf20Sopenharmony_ci bt->tx->temp_packet_buf, 5438c2ecf20Sopenharmony_ci packet_length, 5448c2ecf20Sopenharmony_ci packet_num); 5458c2ecf20Sopenharmony_ci mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT, 5468c2ecf20Sopenharmony_ci bt->tx->temp_packet_buf, dst, 5478c2ecf20Sopenharmony_ci packet_length, 5488c2ecf20Sopenharmony_ci packet_num); 5498c2ecf20Sopenharmony_ci bt->rx->rw_cnt++; 5508c2ecf20Sopenharmony_ci bt->tx->rw_cnt++; 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci if (bt->rx->state == BT_SCO_STATE_RUNNING || 5548c2ecf20Sopenharmony_ci bt->rx->state == BT_SCO_STATE_ENDING) { 5558c2ecf20Sopenharmony_ci if (bt->rx->xrun) { 5568c2ecf20Sopenharmony_ci if (bt->rx->packet_w - bt->rx->packet_r <= 5578c2ecf20Sopenharmony_ci SCO_RX_PACKER_BUF_NUM - 2 * buf_cnt_rx) { 5588c2ecf20Sopenharmony_ci /* 5598c2ecf20Sopenharmony_ci * free space is larger then 5608c2ecf20Sopenharmony_ci * twice interrupt rx data size 5618c2ecf20Sopenharmony_ci */ 5628c2ecf20Sopenharmony_ci bt->rx->xrun = 0; 5638c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), rx->xrun 0!\n", 5648c2ecf20Sopenharmony_ci __func__); 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci } 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci if (!bt->rx->xrun && 5698c2ecf20Sopenharmony_ci (bt->rx->packet_w - bt->rx->packet_r <= 5708c2ecf20Sopenharmony_ci SCO_RX_PACKER_BUF_NUM - buf_cnt_rx)) { 5718c2ecf20Sopenharmony_ci mtk_btcvsd_read_from_bt(bt, 5728c2ecf20Sopenharmony_ci packet_type, 5738c2ecf20Sopenharmony_ci packet_length, 5748c2ecf20Sopenharmony_ci packet_num, 5758c2ecf20Sopenharmony_ci buf_cnt_rx, 5768c2ecf20Sopenharmony_ci control); 5778c2ecf20Sopenharmony_ci bt->rx->rw_cnt++; 5788c2ecf20Sopenharmony_ci } else { 5798c2ecf20Sopenharmony_ci bt->rx->xrun = 1; 5808c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), rx->xrun 1\n", __func__); 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci } 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci /* tx */ 5858c2ecf20Sopenharmony_ci bt->tx->timeout = 0; 5868c2ecf20Sopenharmony_ci if ((bt->tx->state == BT_SCO_STATE_RUNNING || 5878c2ecf20Sopenharmony_ci bt->tx->state == BT_SCO_STATE_ENDING) && 5888c2ecf20Sopenharmony_ci bt->tx->trigger_start) { 5898c2ecf20Sopenharmony_ci if (bt->tx->xrun) { 5908c2ecf20Sopenharmony_ci /* prepared data is larger then twice 5918c2ecf20Sopenharmony_ci * interrupt tx data size 5928c2ecf20Sopenharmony_ci */ 5938c2ecf20Sopenharmony_ci if (bt->tx->packet_w - bt->tx->packet_r >= 5948c2ecf20Sopenharmony_ci 2 * buf_cnt_tx) { 5958c2ecf20Sopenharmony_ci bt->tx->xrun = 0; 5968c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), tx->xrun 0\n", 5978c2ecf20Sopenharmony_ci __func__); 5988c2ecf20Sopenharmony_ci } 5998c2ecf20Sopenharmony_ci } 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci if ((!bt->tx->xrun && 6028c2ecf20Sopenharmony_ci (bt->tx->packet_w - bt->tx->packet_r >= buf_cnt_tx)) || 6038c2ecf20Sopenharmony_ci bt->tx->state == BT_SCO_STATE_ENDING) { 6048c2ecf20Sopenharmony_ci mtk_btcvsd_write_to_bt(bt, 6058c2ecf20Sopenharmony_ci packet_type, 6068c2ecf20Sopenharmony_ci packet_length, 6078c2ecf20Sopenharmony_ci packet_num, 6088c2ecf20Sopenharmony_ci buf_cnt_tx); 6098c2ecf20Sopenharmony_ci bt->tx->rw_cnt++; 6108c2ecf20Sopenharmony_ci } else { 6118c2ecf20Sopenharmony_ci bt->tx->xrun = 1; 6128c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), tx->xrun 1\n", __func__); 6138c2ecf20Sopenharmony_ci } 6148c2ecf20Sopenharmony_ci } 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci *bt->bt_reg_ctl &= ~BT_CVSD_CLEAR; 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci if (bt->rx->state == BT_SCO_STATE_RUNNING || 6198c2ecf20Sopenharmony_ci bt->rx->state == BT_SCO_STATE_ENDING) { 6208c2ecf20Sopenharmony_ci bt->rx->wait_flag = 1; 6218c2ecf20Sopenharmony_ci wake_up_interruptible(&bt->rx_wait); 6228c2ecf20Sopenharmony_ci snd_pcm_period_elapsed(bt->rx->substream); 6238c2ecf20Sopenharmony_ci } 6248c2ecf20Sopenharmony_ci if (bt->tx->state == BT_SCO_STATE_RUNNING || 6258c2ecf20Sopenharmony_ci bt->tx->state == BT_SCO_STATE_ENDING) { 6268c2ecf20Sopenharmony_ci bt->tx->wait_flag = 1; 6278c2ecf20Sopenharmony_ci wake_up_interruptible(&bt->tx_wait); 6288c2ecf20Sopenharmony_ci snd_pcm_period_elapsed(bt->tx->substream); 6298c2ecf20Sopenharmony_ci } 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci return IRQ_HANDLED; 6328c2ecf20Sopenharmony_ciirq_handler_exit: 6338c2ecf20Sopenharmony_ci *bt->bt_reg_ctl &= ~BT_CVSD_CLEAR; 6348c2ecf20Sopenharmony_ci return IRQ_HANDLED; 6358c2ecf20Sopenharmony_ci} 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_cistatic int wait_for_bt_irq(struct mtk_btcvsd_snd *bt, 6388c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream) 6398c2ecf20Sopenharmony_ci{ 6408c2ecf20Sopenharmony_ci unsigned long long t1, t2; 6418c2ecf20Sopenharmony_ci /* one interrupt period = 22.5ms */ 6428c2ecf20Sopenharmony_ci unsigned long long timeout_limit = 22500000; 6438c2ecf20Sopenharmony_ci int max_timeout_trial = 2; 6448c2ecf20Sopenharmony_ci int ret; 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci bt_stream->wait_flag = 0; 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci while (max_timeout_trial && !bt_stream->wait_flag) { 6498c2ecf20Sopenharmony_ci t1 = sched_clock(); 6508c2ecf20Sopenharmony_ci if (bt_stream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 6518c2ecf20Sopenharmony_ci ret = wait_event_interruptible_timeout(bt->tx_wait, 6528c2ecf20Sopenharmony_ci bt_stream->wait_flag, 6538c2ecf20Sopenharmony_ci nsecs_to_jiffies(timeout_limit)); 6548c2ecf20Sopenharmony_ci } else { 6558c2ecf20Sopenharmony_ci ret = wait_event_interruptible_timeout(bt->rx_wait, 6568c2ecf20Sopenharmony_ci bt_stream->wait_flag, 6578c2ecf20Sopenharmony_ci nsecs_to_jiffies(timeout_limit)); 6588c2ecf20Sopenharmony_ci } 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci t2 = sched_clock(); 6618c2ecf20Sopenharmony_ci t2 = t2 - t1; /* in ns (10^9) */ 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci if (t2 > timeout_limit) { 6648c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), stream %d, timeout %llu, limit %llu, ret %d, flag %d\n", 6658c2ecf20Sopenharmony_ci __func__, bt_stream->stream, 6668c2ecf20Sopenharmony_ci t2, timeout_limit, ret, 6678c2ecf20Sopenharmony_ci bt_stream->wait_flag); 6688c2ecf20Sopenharmony_ci } 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci if (ret < 0) { 6718c2ecf20Sopenharmony_ci /* 6728c2ecf20Sopenharmony_ci * error, -ERESTARTSYS if it was interrupted by 6738c2ecf20Sopenharmony_ci * a signal 6748c2ecf20Sopenharmony_ci */ 6758c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), stream %d, error, trial left %d\n", 6768c2ecf20Sopenharmony_ci __func__, 6778c2ecf20Sopenharmony_ci bt_stream->stream, max_timeout_trial); 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci bt_stream->timeout = 1; 6808c2ecf20Sopenharmony_ci return ret; 6818c2ecf20Sopenharmony_ci } else if (ret == 0) { 6828c2ecf20Sopenharmony_ci /* conidtion is false after timeout */ 6838c2ecf20Sopenharmony_ci max_timeout_trial--; 6848c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), stream %d, error, timeout, condition is false, trial left %d\n", 6858c2ecf20Sopenharmony_ci __func__, 6868c2ecf20Sopenharmony_ci bt_stream->stream, max_timeout_trial); 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci if (max_timeout_trial <= 0) { 6898c2ecf20Sopenharmony_ci bt_stream->timeout = 1; 6908c2ecf20Sopenharmony_ci return -ETIME; 6918c2ecf20Sopenharmony_ci } 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci } 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci return 0; 6968c2ecf20Sopenharmony_ci} 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_cistatic ssize_t mtk_btcvsd_snd_read(struct mtk_btcvsd_snd *bt, 6998c2ecf20Sopenharmony_ci char __user *buf, 7008c2ecf20Sopenharmony_ci size_t count) 7018c2ecf20Sopenharmony_ci{ 7028c2ecf20Sopenharmony_ci ssize_t read_size = 0, read_count = 0, cur_read_idx, cont; 7038c2ecf20Sopenharmony_ci unsigned int cur_buf_ofs = 0; 7048c2ecf20Sopenharmony_ci unsigned long avail; 7058c2ecf20Sopenharmony_ci unsigned long flags; 7068c2ecf20Sopenharmony_ci unsigned int packet_size = bt->rx->packet_size; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci while (count) { 7098c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->rx_lock, flags); 7108c2ecf20Sopenharmony_ci /* available data in RX packet buffer */ 7118c2ecf20Sopenharmony_ci avail = (bt->rx->packet_w - bt->rx->packet_r) * packet_size; 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci cur_read_idx = (bt->rx->packet_r & SCO_RX_PACKET_MASK) * 7148c2ecf20Sopenharmony_ci packet_size; 7158c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->rx_lock, flags); 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci if (!avail) { 7188c2ecf20Sopenharmony_ci int ret = wait_for_bt_irq(bt, bt->rx); 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci if (ret) 7218c2ecf20Sopenharmony_ci return read_count; 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci continue; 7248c2ecf20Sopenharmony_ci } 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci /* count must be multiple of packet_size */ 7278c2ecf20Sopenharmony_ci if (count % packet_size != 0 || 7288c2ecf20Sopenharmony_ci avail % packet_size != 0) { 7298c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), count %zu or d %lu is not multiple of packet_size %dd\n", 7308c2ecf20Sopenharmony_ci __func__, count, avail, packet_size); 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci count -= count % packet_size; 7338c2ecf20Sopenharmony_ci avail -= avail % packet_size; 7348c2ecf20Sopenharmony_ci } 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci if (count > avail) 7378c2ecf20Sopenharmony_ci read_size = avail; 7388c2ecf20Sopenharmony_ci else 7398c2ecf20Sopenharmony_ci read_size = count; 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci /* calculate continue space */ 7428c2ecf20Sopenharmony_ci cont = bt->rx->buf_size - cur_read_idx; 7438c2ecf20Sopenharmony_ci if (read_size > cont) 7448c2ecf20Sopenharmony_ci read_size = cont; 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci if (copy_to_user(buf + cur_buf_ofs, 7478c2ecf20Sopenharmony_ci bt->rx_packet_buf + cur_read_idx, 7488c2ecf20Sopenharmony_ci read_size)) { 7498c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), copy_to_user fail\n", 7508c2ecf20Sopenharmony_ci __func__); 7518c2ecf20Sopenharmony_ci return -EFAULT; 7528c2ecf20Sopenharmony_ci } 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->rx_lock, flags); 7558c2ecf20Sopenharmony_ci bt->rx->packet_r += read_size / packet_size; 7568c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->rx_lock, flags); 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci read_count += read_size; 7598c2ecf20Sopenharmony_ci cur_buf_ofs += read_size; 7608c2ecf20Sopenharmony_ci count -= read_size; 7618c2ecf20Sopenharmony_ci } 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci /* 7648c2ecf20Sopenharmony_ci * save current timestamp & buffer time in times_tamp and 7658c2ecf20Sopenharmony_ci * buf_data_equivalent_time 7668c2ecf20Sopenharmony_ci */ 7678c2ecf20Sopenharmony_ci bt->rx->time_stamp = sched_clock(); 7688c2ecf20Sopenharmony_ci bt->rx->buf_data_equivalent_time = 7698c2ecf20Sopenharmony_ci (unsigned long long)(bt->rx->packet_w - bt->rx->packet_r) * 7708c2ecf20Sopenharmony_ci SCO_RX_PLC_SIZE * 16 * 1000 / 2 / 64; 7718c2ecf20Sopenharmony_ci bt->rx->buf_data_equivalent_time += read_count * SCO_RX_PLC_SIZE * 7728c2ecf20Sopenharmony_ci 16 * 1000 / packet_size / 2 / 64; 7738c2ecf20Sopenharmony_ci /* return equivalent time(us) to data count */ 7748c2ecf20Sopenharmony_ci bt->rx->buf_data_equivalent_time *= 1000; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci return read_count; 7778c2ecf20Sopenharmony_ci} 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_cistatic ssize_t mtk_btcvsd_snd_write(struct mtk_btcvsd_snd *bt, 7808c2ecf20Sopenharmony_ci char __user *buf, 7818c2ecf20Sopenharmony_ci size_t count) 7828c2ecf20Sopenharmony_ci{ 7838c2ecf20Sopenharmony_ci int written_size = count, avail = 0, cur_write_idx, write_size, cont; 7848c2ecf20Sopenharmony_ci unsigned int cur_buf_ofs = 0; 7858c2ecf20Sopenharmony_ci unsigned long flags; 7868c2ecf20Sopenharmony_ci unsigned int packet_size = bt->tx->packet_size; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci /* 7898c2ecf20Sopenharmony_ci * save current timestamp & buffer time in time_stamp and 7908c2ecf20Sopenharmony_ci * buf_data_equivalent_time 7918c2ecf20Sopenharmony_ci */ 7928c2ecf20Sopenharmony_ci bt->tx->time_stamp = sched_clock(); 7938c2ecf20Sopenharmony_ci bt->tx->buf_data_equivalent_time = 7948c2ecf20Sopenharmony_ci (unsigned long long)(bt->tx->packet_w - bt->tx->packet_r) * 7958c2ecf20Sopenharmony_ci packet_size * 16 * 1000 / 2 / 64; 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci /* return equivalent time(us) to data count */ 7988c2ecf20Sopenharmony_ci bt->tx->buf_data_equivalent_time *= 1000; 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci while (count) { 8018c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->tx_lock, flags); 8028c2ecf20Sopenharmony_ci /* free space of TX packet buffer */ 8038c2ecf20Sopenharmony_ci avail = bt->tx->buf_size - 8048c2ecf20Sopenharmony_ci (bt->tx->packet_w - bt->tx->packet_r) * packet_size; 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci cur_write_idx = (bt->tx->packet_w % SCO_TX_PACKER_BUF_NUM) * 8078c2ecf20Sopenharmony_ci packet_size; 8088c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->tx_lock, flags); 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci if (!avail) { 8118c2ecf20Sopenharmony_ci int ret = wait_for_bt_irq(bt, bt->rx); 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci if (ret) 8148c2ecf20Sopenharmony_ci return written_size; 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci continue; 8178c2ecf20Sopenharmony_ci } 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci /* count must be multiple of bt->tx->packet_size */ 8208c2ecf20Sopenharmony_ci if (count % packet_size != 0 || 8218c2ecf20Sopenharmony_ci avail % packet_size != 0) { 8228c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), count %zu or avail %d is not multiple of packet_size %d\n", 8238c2ecf20Sopenharmony_ci __func__, count, avail, packet_size); 8248c2ecf20Sopenharmony_ci count -= count % packet_size; 8258c2ecf20Sopenharmony_ci avail -= avail % packet_size; 8268c2ecf20Sopenharmony_ci } 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci if (count > avail) 8298c2ecf20Sopenharmony_ci write_size = avail; 8308c2ecf20Sopenharmony_ci else 8318c2ecf20Sopenharmony_ci write_size = count; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci /* calculate continue space */ 8348c2ecf20Sopenharmony_ci cont = bt->tx->buf_size - cur_write_idx; 8358c2ecf20Sopenharmony_ci if (write_size > cont) 8368c2ecf20Sopenharmony_ci write_size = cont; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci if (copy_from_user(bt->tx_packet_buf + 8398c2ecf20Sopenharmony_ci cur_write_idx, 8408c2ecf20Sopenharmony_ci buf + cur_buf_ofs, 8418c2ecf20Sopenharmony_ci write_size)) { 8428c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), copy_from_user fail\n", 8438c2ecf20Sopenharmony_ci __func__); 8448c2ecf20Sopenharmony_ci return -EFAULT; 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci spin_lock_irqsave(&bt->tx_lock, flags); 8488c2ecf20Sopenharmony_ci bt->tx->packet_w += write_size / packet_size; 8498c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&bt->tx_lock, flags); 8508c2ecf20Sopenharmony_ci cur_buf_ofs += write_size; 8518c2ecf20Sopenharmony_ci count -= write_size; 8528c2ecf20Sopenharmony_ci } 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci return written_size; 8558c2ecf20Sopenharmony_ci} 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_cistatic struct mtk_btcvsd_snd_stream *get_bt_stream 8588c2ecf20Sopenharmony_ci (struct mtk_btcvsd_snd *bt, struct snd_pcm_substream *substream) 8598c2ecf20Sopenharmony_ci{ 8608c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 8618c2ecf20Sopenharmony_ci return bt->tx; 8628c2ecf20Sopenharmony_ci else 8638c2ecf20Sopenharmony_ci return bt->rx; 8648c2ecf20Sopenharmony_ci} 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci/* pcm ops */ 8678c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware mtk_btcvsd_hardware = { 8688c2ecf20Sopenharmony_ci .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 8698c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_RESUME), 8708c2ecf20Sopenharmony_ci .formats = SNDRV_PCM_FMTBIT_S16_LE, 8718c2ecf20Sopenharmony_ci .buffer_bytes_max = 24 * 1024, 8728c2ecf20Sopenharmony_ci .period_bytes_max = 24 * 1024, 8738c2ecf20Sopenharmony_ci .periods_min = 2, 8748c2ecf20Sopenharmony_ci .periods_max = 16, 8758c2ecf20Sopenharmony_ci .fifo_size = 0, 8768c2ecf20Sopenharmony_ci}; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_open(struct snd_soc_component *component, 8798c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 8808c2ecf20Sopenharmony_ci{ 8818c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 8828c2ecf20Sopenharmony_ci int ret; 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), stream %d, substream %p\n", 8858c2ecf20Sopenharmony_ci __func__, substream->stream, substream); 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci snd_soc_set_runtime_hwparams(substream, &mtk_btcvsd_hardware); 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 8908c2ecf20Sopenharmony_ci ret = mtk_btcvsd_snd_tx_init(bt); 8918c2ecf20Sopenharmony_ci bt->tx->substream = substream; 8928c2ecf20Sopenharmony_ci } else { 8938c2ecf20Sopenharmony_ci ret = mtk_btcvsd_snd_rx_init(bt); 8948c2ecf20Sopenharmony_ci bt->rx->substream = substream; 8958c2ecf20Sopenharmony_ci } 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci return ret; 8988c2ecf20Sopenharmony_ci} 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_close(struct snd_soc_component *component, 9018c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 9028c2ecf20Sopenharmony_ci{ 9038c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9048c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream); 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_IDLE); 9098c2ecf20Sopenharmony_ci bt_stream->substream = NULL; 9108c2ecf20Sopenharmony_ci return 0; 9118c2ecf20Sopenharmony_ci} 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_hw_params(struct snd_soc_component *component, 9148c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream, 9158c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *hw_params) 9168c2ecf20Sopenharmony_ci{ 9178c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 9208c2ecf20Sopenharmony_ci params_buffer_bytes(hw_params) % bt->tx->packet_size != 0) { 9218c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), error, buffer size %d not valid\n", 9228c2ecf20Sopenharmony_ci __func__, 9238c2ecf20Sopenharmony_ci params_buffer_bytes(hw_params)); 9248c2ecf20Sopenharmony_ci return -EINVAL; 9258c2ecf20Sopenharmony_ci } 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci substream->runtime->dma_bytes = params_buffer_bytes(hw_params); 9288c2ecf20Sopenharmony_ci return 0; 9298c2ecf20Sopenharmony_ci} 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_hw_free(struct snd_soc_component *component, 9328c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 9338c2ecf20Sopenharmony_ci{ 9348c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 9378c2ecf20Sopenharmony_ci btcvsd_tx_clean_buffer(bt); 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci return 0; 9408c2ecf20Sopenharmony_ci} 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_prepare(struct snd_soc_component *component, 9438c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 9448c2ecf20Sopenharmony_ci{ 9458c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9468c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream); 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream); 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_RUNNING); 9518c2ecf20Sopenharmony_ci return 0; 9528c2ecf20Sopenharmony_ci} 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_trigger(struct snd_soc_component *component, 9558c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream, int cmd) 9568c2ecf20Sopenharmony_ci{ 9578c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9588c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream); 9598c2ecf20Sopenharmony_ci int stream = substream->stream; 9608c2ecf20Sopenharmony_ci int hw_packet_ptr; 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), stream %d, cmd %d\n", 9638c2ecf20Sopenharmony_ci __func__, substream->stream, cmd); 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci switch (cmd) { 9668c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 9678c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_RESUME: 9688c2ecf20Sopenharmony_ci hw_packet_ptr = stream == SNDRV_PCM_STREAM_PLAYBACK ? 9698c2ecf20Sopenharmony_ci bt_stream->packet_r : bt_stream->packet_w; 9708c2ecf20Sopenharmony_ci bt_stream->prev_packet_idx = hw_packet_ptr; 9718c2ecf20Sopenharmony_ci bt_stream->prev_frame = 0; 9728c2ecf20Sopenharmony_ci bt_stream->trigger_start = 1; 9738c2ecf20Sopenharmony_ci return 0; 9748c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 9758c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_SUSPEND: 9768c2ecf20Sopenharmony_ci bt_stream->trigger_start = 0; 9778c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_ENDING); 9788c2ecf20Sopenharmony_ci return 0; 9798c2ecf20Sopenharmony_ci default: 9808c2ecf20Sopenharmony_ci return -EINVAL; 9818c2ecf20Sopenharmony_ci } 9828c2ecf20Sopenharmony_ci} 9838c2ecf20Sopenharmony_ci 9848c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t mtk_pcm_btcvsd_pointer( 9858c2ecf20Sopenharmony_ci struct snd_soc_component *component, 9868c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream) 9878c2ecf20Sopenharmony_ci{ 9888c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 9898c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_stream *bt_stream; 9908c2ecf20Sopenharmony_ci snd_pcm_uframes_t frame = 0; 9918c2ecf20Sopenharmony_ci int byte = 0; 9928c2ecf20Sopenharmony_ci int hw_packet_ptr; 9938c2ecf20Sopenharmony_ci int packet_diff; 9948c2ecf20Sopenharmony_ci spinlock_t *lock; /* spinlock for bt stream control */ 9958c2ecf20Sopenharmony_ci unsigned long flags; 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 9988c2ecf20Sopenharmony_ci lock = &bt->tx_lock; 9998c2ecf20Sopenharmony_ci bt_stream = bt->tx; 10008c2ecf20Sopenharmony_ci } else { 10018c2ecf20Sopenharmony_ci lock = &bt->rx_lock; 10028c2ecf20Sopenharmony_ci bt_stream = bt->rx; 10038c2ecf20Sopenharmony_ci } 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci spin_lock_irqsave(lock, flags); 10068c2ecf20Sopenharmony_ci hw_packet_ptr = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 10078c2ecf20Sopenharmony_ci bt->tx->packet_r : bt->rx->packet_w; 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci /* get packet diff from last time */ 10108c2ecf20Sopenharmony_ci if (hw_packet_ptr >= bt_stream->prev_packet_idx) { 10118c2ecf20Sopenharmony_ci packet_diff = hw_packet_ptr - bt_stream->prev_packet_idx; 10128c2ecf20Sopenharmony_ci } else { 10138c2ecf20Sopenharmony_ci /* integer overflow */ 10148c2ecf20Sopenharmony_ci packet_diff = (INT_MAX - bt_stream->prev_packet_idx) + 10158c2ecf20Sopenharmony_ci (hw_packet_ptr - INT_MIN) + 1; 10168c2ecf20Sopenharmony_ci } 10178c2ecf20Sopenharmony_ci bt_stream->prev_packet_idx = hw_packet_ptr; 10188c2ecf20Sopenharmony_ci 10198c2ecf20Sopenharmony_ci /* increased bytes */ 10208c2ecf20Sopenharmony_ci byte = packet_diff * bt_stream->packet_size; 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci frame = btcvsd_bytes_to_frame(substream, byte); 10238c2ecf20Sopenharmony_ci frame += bt_stream->prev_frame; 10248c2ecf20Sopenharmony_ci frame %= substream->runtime->buffer_size; 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci bt_stream->prev_frame = frame; 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci spin_unlock_irqrestore(lock, flags); 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_ci return frame; 10318c2ecf20Sopenharmony_ci} 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_cistatic int mtk_pcm_btcvsd_copy(struct snd_soc_component *component, 10348c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream, 10358c2ecf20Sopenharmony_ci int channel, unsigned long pos, 10368c2ecf20Sopenharmony_ci void __user *buf, unsigned long count) 10378c2ecf20Sopenharmony_ci{ 10388c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component); 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 10418c2ecf20Sopenharmony_ci return mtk_btcvsd_snd_write(bt, buf, count); 10428c2ecf20Sopenharmony_ci else 10438c2ecf20Sopenharmony_ci return mtk_btcvsd_snd_read(bt, buf, count); 10448c2ecf20Sopenharmony_ci} 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_ci/* kcontrol */ 10478c2ecf20Sopenharmony_cistatic const char *const btsco_band_str[] = {"NB", "WB"}; 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_cistatic const struct soc_enum btcvsd_enum[] = { 10508c2ecf20Sopenharmony_ci SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(btsco_band_str), btsco_band_str), 10518c2ecf20Sopenharmony_ci}; 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_cistatic int btcvsd_band_get(struct snd_kcontrol *kcontrol, 10548c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 10558c2ecf20Sopenharmony_ci{ 10568c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 10578c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->band; 10608c2ecf20Sopenharmony_ci return 0; 10618c2ecf20Sopenharmony_ci} 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_cistatic int btcvsd_band_set(struct snd_kcontrol *kcontrol, 10648c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 10658c2ecf20Sopenharmony_ci{ 10668c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 10678c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 10688c2ecf20Sopenharmony_ci struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci if (ucontrol->value.enumerated.item[0] >= e->items) 10718c2ecf20Sopenharmony_ci return -EINVAL; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci bt->band = ucontrol->value.integer.value[0]; 10748c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), band %d\n", __func__, bt->band); 10758c2ecf20Sopenharmony_ci return 0; 10768c2ecf20Sopenharmony_ci} 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_cistatic int btcvsd_loopback_get(struct snd_kcontrol *kcontrol, 10798c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 10808c2ecf20Sopenharmony_ci{ 10818c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 10828c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 10838c2ecf20Sopenharmony_ci bool lpbk_en = bt->tx->state == BT_SCO_STATE_LOOPBACK; 10848c2ecf20Sopenharmony_ci 10858c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = lpbk_en; 10868c2ecf20Sopenharmony_ci return 0; 10878c2ecf20Sopenharmony_ci} 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_cistatic int btcvsd_loopback_set(struct snd_kcontrol *kcontrol, 10908c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 10918c2ecf20Sopenharmony_ci{ 10928c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 10938c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci if (ucontrol->value.integer.value[0]) { 10968c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_LOOPBACK); 10978c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_LOOPBACK); 10988c2ecf20Sopenharmony_ci } else { 10998c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_RUNNING); 11008c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_RUNNING); 11018c2ecf20Sopenharmony_ci } 11028c2ecf20Sopenharmony_ci return 0; 11038c2ecf20Sopenharmony_ci} 11048c2ecf20Sopenharmony_ci 11058c2ecf20Sopenharmony_cistatic int btcvsd_tx_mute_get(struct snd_kcontrol *kcontrol, 11068c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 11078c2ecf20Sopenharmony_ci{ 11088c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11098c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_ci if (!bt->tx) { 11128c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = 0; 11138c2ecf20Sopenharmony_ci return 0; 11148c2ecf20Sopenharmony_ci } 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->tx->mute; 11178c2ecf20Sopenharmony_ci return 0; 11188c2ecf20Sopenharmony_ci} 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_cistatic int btcvsd_tx_mute_set(struct snd_kcontrol *kcontrol, 11218c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 11228c2ecf20Sopenharmony_ci{ 11238c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11248c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci if (!bt->tx) 11278c2ecf20Sopenharmony_ci return 0; 11288c2ecf20Sopenharmony_ci 11298c2ecf20Sopenharmony_ci bt->tx->mute = ucontrol->value.integer.value[0]; 11308c2ecf20Sopenharmony_ci return 0; 11318c2ecf20Sopenharmony_ci} 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_cistatic int btcvsd_rx_irq_received_get(struct snd_kcontrol *kcontrol, 11348c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 11358c2ecf20Sopenharmony_ci{ 11368c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11378c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci if (!bt->rx) 11408c2ecf20Sopenharmony_ci return 0; 11418c2ecf20Sopenharmony_ci 11428c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->rx->rw_cnt ? 1 : 0; 11438c2ecf20Sopenharmony_ci return 0; 11448c2ecf20Sopenharmony_ci} 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_cistatic int btcvsd_rx_timeout_get(struct snd_kcontrol *kcontrol, 11478c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 11488c2ecf20Sopenharmony_ci{ 11498c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11508c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_ci if (!bt->rx) 11538c2ecf20Sopenharmony_ci return 0; 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->rx->timeout; 11568c2ecf20Sopenharmony_ci bt->rx->timeout = 0; 11578c2ecf20Sopenharmony_ci return 0; 11588c2ecf20Sopenharmony_ci} 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_cistatic int btcvsd_rx_timestamp_get(struct snd_kcontrol *kcontrol, 11618c2ecf20Sopenharmony_ci unsigned int __user *data, unsigned int size) 11628c2ecf20Sopenharmony_ci{ 11638c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11648c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11658c2ecf20Sopenharmony_ci int ret = 0; 11668c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_rx; 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info)) 11698c2ecf20Sopenharmony_ci return -EINVAL; 11708c2ecf20Sopenharmony_ci 11718c2ecf20Sopenharmony_ci get_rx_time_stamp(bt, &time_buffer_info_rx); 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu", 11748c2ecf20Sopenharmony_ci __func__, 11758c2ecf20Sopenharmony_ci time_buffer_info_rx.time_stamp_us, 11768c2ecf20Sopenharmony_ci time_buffer_info_rx.data_count_equi_time); 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci if (copy_to_user(data, &time_buffer_info_rx, 11798c2ecf20Sopenharmony_ci sizeof(struct mtk_btcvsd_snd_time_buffer_info))) { 11808c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), copy_to_user fail", __func__); 11818c2ecf20Sopenharmony_ci ret = -EFAULT; 11828c2ecf20Sopenharmony_ci } 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_ci return ret; 11858c2ecf20Sopenharmony_ci} 11868c2ecf20Sopenharmony_ci 11878c2ecf20Sopenharmony_cistatic int btcvsd_tx_irq_received_get(struct snd_kcontrol *kcontrol, 11888c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 11898c2ecf20Sopenharmony_ci{ 11908c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 11918c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 11928c2ecf20Sopenharmony_ci 11938c2ecf20Sopenharmony_ci if (!bt->tx) 11948c2ecf20Sopenharmony_ci return 0; 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->tx->rw_cnt ? 1 : 0; 11978c2ecf20Sopenharmony_ci return 0; 11988c2ecf20Sopenharmony_ci} 11998c2ecf20Sopenharmony_ci 12008c2ecf20Sopenharmony_cistatic int btcvsd_tx_timeout_get(struct snd_kcontrol *kcontrol, 12018c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 12028c2ecf20Sopenharmony_ci{ 12038c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 12048c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = bt->tx->timeout; 12078c2ecf20Sopenharmony_ci return 0; 12088c2ecf20Sopenharmony_ci} 12098c2ecf20Sopenharmony_ci 12108c2ecf20Sopenharmony_cistatic int btcvsd_tx_timestamp_get(struct snd_kcontrol *kcontrol, 12118c2ecf20Sopenharmony_ci unsigned int __user *data, unsigned int size) 12128c2ecf20Sopenharmony_ci{ 12138c2ecf20Sopenharmony_ci struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol); 12148c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt); 12158c2ecf20Sopenharmony_ci int ret = 0; 12168c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_tx; 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_ci if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info)) 12198c2ecf20Sopenharmony_ci return -EINVAL; 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ci get_tx_time_stamp(bt, &time_buffer_info_tx); 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ci dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu", 12248c2ecf20Sopenharmony_ci __func__, 12258c2ecf20Sopenharmony_ci time_buffer_info_tx.time_stamp_us, 12268c2ecf20Sopenharmony_ci time_buffer_info_tx.data_count_equi_time); 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_ci if (copy_to_user(data, &time_buffer_info_tx, 12298c2ecf20Sopenharmony_ci sizeof(struct mtk_btcvsd_snd_time_buffer_info))) { 12308c2ecf20Sopenharmony_ci dev_warn(bt->dev, "%s(), copy_to_user fail", __func__); 12318c2ecf20Sopenharmony_ci ret = -EFAULT; 12328c2ecf20Sopenharmony_ci } 12338c2ecf20Sopenharmony_ci 12348c2ecf20Sopenharmony_ci return ret; 12358c2ecf20Sopenharmony_ci} 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new mtk_btcvsd_snd_controls[] = { 12388c2ecf20Sopenharmony_ci SOC_ENUM_EXT("BTCVSD Band", btcvsd_enum[0], 12398c2ecf20Sopenharmony_ci btcvsd_band_get, btcvsd_band_set), 12408c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Loopback Switch", 0, 12418c2ecf20Sopenharmony_ci btcvsd_loopback_get, btcvsd_loopback_set), 12428c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Tx Mute Switch", 0, 12438c2ecf20Sopenharmony_ci btcvsd_tx_mute_get, btcvsd_tx_mute_set), 12448c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Tx Irq Received Switch", 0, 12458c2ecf20Sopenharmony_ci btcvsd_tx_irq_received_get, NULL), 12468c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Tx Timeout Switch", 0, 12478c2ecf20Sopenharmony_ci btcvsd_tx_timeout_get, NULL), 12488c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Rx Irq Received Switch", 0, 12498c2ecf20Sopenharmony_ci btcvsd_rx_irq_received_get, NULL), 12508c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("BTCVSD Rx Timeout Switch", 0, 12518c2ecf20Sopenharmony_ci btcvsd_rx_timeout_get, NULL), 12528c2ecf20Sopenharmony_ci SND_SOC_BYTES_TLV("BTCVSD Rx Timestamp", 12538c2ecf20Sopenharmony_ci sizeof(struct mtk_btcvsd_snd_time_buffer_info), 12548c2ecf20Sopenharmony_ci btcvsd_rx_timestamp_get, NULL), 12558c2ecf20Sopenharmony_ci SND_SOC_BYTES_TLV("BTCVSD Tx Timestamp", 12568c2ecf20Sopenharmony_ci sizeof(struct mtk_btcvsd_snd_time_buffer_info), 12578c2ecf20Sopenharmony_ci btcvsd_tx_timestamp_get, NULL), 12588c2ecf20Sopenharmony_ci}; 12598c2ecf20Sopenharmony_ci 12608c2ecf20Sopenharmony_cistatic int mtk_btcvsd_snd_component_probe(struct snd_soc_component *component) 12618c2ecf20Sopenharmony_ci{ 12628c2ecf20Sopenharmony_ci return snd_soc_add_component_controls(component, 12638c2ecf20Sopenharmony_ci mtk_btcvsd_snd_controls, 12648c2ecf20Sopenharmony_ci ARRAY_SIZE(mtk_btcvsd_snd_controls)); 12658c2ecf20Sopenharmony_ci} 12668c2ecf20Sopenharmony_ci 12678c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver mtk_btcvsd_snd_platform = { 12688c2ecf20Sopenharmony_ci .name = BTCVSD_SND_NAME, 12698c2ecf20Sopenharmony_ci .probe = mtk_btcvsd_snd_component_probe, 12708c2ecf20Sopenharmony_ci .open = mtk_pcm_btcvsd_open, 12718c2ecf20Sopenharmony_ci .close = mtk_pcm_btcvsd_close, 12728c2ecf20Sopenharmony_ci .hw_params = mtk_pcm_btcvsd_hw_params, 12738c2ecf20Sopenharmony_ci .hw_free = mtk_pcm_btcvsd_hw_free, 12748c2ecf20Sopenharmony_ci .prepare = mtk_pcm_btcvsd_prepare, 12758c2ecf20Sopenharmony_ci .trigger = mtk_pcm_btcvsd_trigger, 12768c2ecf20Sopenharmony_ci .pointer = mtk_pcm_btcvsd_pointer, 12778c2ecf20Sopenharmony_ci .copy_user = mtk_pcm_btcvsd_copy, 12788c2ecf20Sopenharmony_ci}; 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_cistatic int mtk_btcvsd_snd_probe(struct platform_device *pdev) 12818c2ecf20Sopenharmony_ci{ 12828c2ecf20Sopenharmony_ci int ret; 12838c2ecf20Sopenharmony_ci int irq_id; 12848c2ecf20Sopenharmony_ci u32 offset[5] = {0, 0, 0, 0, 0}; 12858c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *btcvsd; 12868c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_ci /* init btcvsd private data */ 12898c2ecf20Sopenharmony_ci btcvsd = devm_kzalloc(dev, sizeof(*btcvsd), GFP_KERNEL); 12908c2ecf20Sopenharmony_ci if (!btcvsd) 12918c2ecf20Sopenharmony_ci return -ENOMEM; 12928c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, btcvsd); 12938c2ecf20Sopenharmony_ci btcvsd->dev = dev; 12948c2ecf20Sopenharmony_ci 12958c2ecf20Sopenharmony_ci /* init tx/rx */ 12968c2ecf20Sopenharmony_ci btcvsd->rx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->rx), GFP_KERNEL); 12978c2ecf20Sopenharmony_ci if (!btcvsd->rx) 12988c2ecf20Sopenharmony_ci return -ENOMEM; 12998c2ecf20Sopenharmony_ci 13008c2ecf20Sopenharmony_ci btcvsd->tx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->tx), GFP_KERNEL); 13018c2ecf20Sopenharmony_ci if (!btcvsd->tx) 13028c2ecf20Sopenharmony_ci return -ENOMEM; 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_ci spin_lock_init(&btcvsd->tx_lock); 13058c2ecf20Sopenharmony_ci spin_lock_init(&btcvsd->rx_lock); 13068c2ecf20Sopenharmony_ci 13078c2ecf20Sopenharmony_ci init_waitqueue_head(&btcvsd->tx_wait); 13088c2ecf20Sopenharmony_ci init_waitqueue_head(&btcvsd->rx_wait); 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci mtk_btcvsd_snd_tx_init(btcvsd); 13118c2ecf20Sopenharmony_ci mtk_btcvsd_snd_rx_init(btcvsd); 13128c2ecf20Sopenharmony_ci 13138c2ecf20Sopenharmony_ci /* irq */ 13148c2ecf20Sopenharmony_ci irq_id = platform_get_irq(pdev, 0); 13158c2ecf20Sopenharmony_ci if (irq_id <= 0) 13168c2ecf20Sopenharmony_ci return irq_id < 0 ? irq_id : -ENXIO; 13178c2ecf20Sopenharmony_ci 13188c2ecf20Sopenharmony_ci ret = devm_request_irq(dev, irq_id, mtk_btcvsd_snd_irq_handler, 13198c2ecf20Sopenharmony_ci IRQF_TRIGGER_LOW, "BTCVSD_ISR_Handle", 13208c2ecf20Sopenharmony_ci (void *)btcvsd); 13218c2ecf20Sopenharmony_ci if (ret) { 13228c2ecf20Sopenharmony_ci dev_err(dev, "could not request_irq for BTCVSD_ISR_Handle\n"); 13238c2ecf20Sopenharmony_ci return ret; 13248c2ecf20Sopenharmony_ci } 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_ci btcvsd->irq_id = irq_id; 13278c2ecf20Sopenharmony_ci 13288c2ecf20Sopenharmony_ci /* iomap */ 13298c2ecf20Sopenharmony_ci btcvsd->bt_pkv_base = of_iomap(dev->of_node, 0); 13308c2ecf20Sopenharmony_ci if (!btcvsd->bt_pkv_base) { 13318c2ecf20Sopenharmony_ci dev_err(dev, "iomap bt_pkv_base fail\n"); 13328c2ecf20Sopenharmony_ci return -EIO; 13338c2ecf20Sopenharmony_ci } 13348c2ecf20Sopenharmony_ci 13358c2ecf20Sopenharmony_ci btcvsd->bt_sram_bank2_base = of_iomap(dev->of_node, 1); 13368c2ecf20Sopenharmony_ci if (!btcvsd->bt_sram_bank2_base) { 13378c2ecf20Sopenharmony_ci dev_err(dev, "iomap bt_sram_bank2_base fail\n"); 13388c2ecf20Sopenharmony_ci ret = -EIO; 13398c2ecf20Sopenharmony_ci goto unmap_pkv_err; 13408c2ecf20Sopenharmony_ci } 13418c2ecf20Sopenharmony_ci 13428c2ecf20Sopenharmony_ci btcvsd->infra = syscon_regmap_lookup_by_phandle(dev->of_node, 13438c2ecf20Sopenharmony_ci "mediatek,infracfg"); 13448c2ecf20Sopenharmony_ci if (IS_ERR(btcvsd->infra)) { 13458c2ecf20Sopenharmony_ci dev_err(dev, "cannot find infra controller: %ld\n", 13468c2ecf20Sopenharmony_ci PTR_ERR(btcvsd->infra)); 13478c2ecf20Sopenharmony_ci ret = PTR_ERR(btcvsd->infra); 13488c2ecf20Sopenharmony_ci goto unmap_bank2_err; 13498c2ecf20Sopenharmony_ci } 13508c2ecf20Sopenharmony_ci 13518c2ecf20Sopenharmony_ci /* get offset */ 13528c2ecf20Sopenharmony_ci ret = of_property_read_u32_array(dev->of_node, "mediatek,offset", 13538c2ecf20Sopenharmony_ci offset, 13548c2ecf20Sopenharmony_ci ARRAY_SIZE(offset)); 13558c2ecf20Sopenharmony_ci if (ret) { 13568c2ecf20Sopenharmony_ci dev_warn(dev, "%s(), get offset fail, ret %d\n", __func__, ret); 13578c2ecf20Sopenharmony_ci goto unmap_bank2_err; 13588c2ecf20Sopenharmony_ci } 13598c2ecf20Sopenharmony_ci btcvsd->infra_misc_offset = offset[0]; 13608c2ecf20Sopenharmony_ci btcvsd->conn_bt_cvsd_mask = offset[1]; 13618c2ecf20Sopenharmony_ci btcvsd->cvsd_mcu_read_offset = offset[2]; 13628c2ecf20Sopenharmony_ci btcvsd->cvsd_mcu_write_offset = offset[3]; 13638c2ecf20Sopenharmony_ci btcvsd->cvsd_packet_indicator = offset[4]; 13648c2ecf20Sopenharmony_ci 13658c2ecf20Sopenharmony_ci btcvsd->bt_reg_pkt_r = btcvsd->bt_pkv_base + 13668c2ecf20Sopenharmony_ci btcvsd->cvsd_mcu_read_offset; 13678c2ecf20Sopenharmony_ci btcvsd->bt_reg_pkt_w = btcvsd->bt_pkv_base + 13688c2ecf20Sopenharmony_ci btcvsd->cvsd_mcu_write_offset; 13698c2ecf20Sopenharmony_ci btcvsd->bt_reg_ctl = btcvsd->bt_pkv_base + 13708c2ecf20Sopenharmony_ci btcvsd->cvsd_packet_indicator; 13718c2ecf20Sopenharmony_ci 13728c2ecf20Sopenharmony_ci /* init state */ 13738c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(btcvsd, btcvsd->tx, BT_SCO_STATE_IDLE); 13748c2ecf20Sopenharmony_ci mtk_btcvsd_snd_set_state(btcvsd, btcvsd->rx, BT_SCO_STATE_IDLE); 13758c2ecf20Sopenharmony_ci 13768c2ecf20Sopenharmony_ci ret = devm_snd_soc_register_component(dev, &mtk_btcvsd_snd_platform, 13778c2ecf20Sopenharmony_ci NULL, 0); 13788c2ecf20Sopenharmony_ci if (ret) 13798c2ecf20Sopenharmony_ci goto unmap_bank2_err; 13808c2ecf20Sopenharmony_ci 13818c2ecf20Sopenharmony_ci return 0; 13828c2ecf20Sopenharmony_ci 13838c2ecf20Sopenharmony_ciunmap_bank2_err: 13848c2ecf20Sopenharmony_ci iounmap(btcvsd->bt_sram_bank2_base); 13858c2ecf20Sopenharmony_ciunmap_pkv_err: 13868c2ecf20Sopenharmony_ci iounmap(btcvsd->bt_pkv_base); 13878c2ecf20Sopenharmony_ci return ret; 13888c2ecf20Sopenharmony_ci} 13898c2ecf20Sopenharmony_ci 13908c2ecf20Sopenharmony_cistatic int mtk_btcvsd_snd_remove(struct platform_device *pdev) 13918c2ecf20Sopenharmony_ci{ 13928c2ecf20Sopenharmony_ci struct mtk_btcvsd_snd *btcvsd = dev_get_drvdata(&pdev->dev); 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_ci iounmap(btcvsd->bt_pkv_base); 13958c2ecf20Sopenharmony_ci iounmap(btcvsd->bt_sram_bank2_base); 13968c2ecf20Sopenharmony_ci return 0; 13978c2ecf20Sopenharmony_ci} 13988c2ecf20Sopenharmony_ci 13998c2ecf20Sopenharmony_cistatic const struct of_device_id mtk_btcvsd_snd_dt_match[] = { 14008c2ecf20Sopenharmony_ci { .compatible = "mediatek,mtk-btcvsd-snd", }, 14018c2ecf20Sopenharmony_ci {}, 14028c2ecf20Sopenharmony_ci}; 14038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mtk_btcvsd_snd_dt_match); 14048c2ecf20Sopenharmony_ci 14058c2ecf20Sopenharmony_cistatic struct platform_driver mtk_btcvsd_snd_driver = { 14068c2ecf20Sopenharmony_ci .driver = { 14078c2ecf20Sopenharmony_ci .name = "mtk-btcvsd-snd", 14088c2ecf20Sopenharmony_ci .of_match_table = mtk_btcvsd_snd_dt_match, 14098c2ecf20Sopenharmony_ci }, 14108c2ecf20Sopenharmony_ci .probe = mtk_btcvsd_snd_probe, 14118c2ecf20Sopenharmony_ci .remove = mtk_btcvsd_snd_remove, 14128c2ecf20Sopenharmony_ci}; 14138c2ecf20Sopenharmony_ci 14148c2ecf20Sopenharmony_cimodule_platform_driver(mtk_btcvsd_snd_driver); 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Mediatek ALSA BT SCO CVSD/MSBC Driver"); 14178c2ecf20Sopenharmony_ciMODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>"); 14188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1419