1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Mediatek ALSA BT SCO CVSD/MSBC Driver
4 //
5 // Copyright (c) 2019 MediaTek Inc.
6 // Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com>
7
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/sched/clock.h>
12
13 #include <sound/soc.h>
14
15 #define BTCVSD_SND_NAME "mtk-btcvsd-snd"
16
17 #define BT_CVSD_TX_NREADY BIT(21)
18 #define BT_CVSD_RX_READY BIT(22)
19 #define BT_CVSD_TX_UNDERFLOW BIT(23)
20 #define BT_CVSD_RX_OVERFLOW BIT(24)
21 #define BT_CVSD_INTERRUPT BIT(31)
22
23 #define BT_CVSD_CLEAR \
24 (BT_CVSD_TX_NREADY | BT_CVSD_RX_READY | BT_CVSD_TX_UNDERFLOW |\
25 BT_CVSD_RX_OVERFLOW | BT_CVSD_INTERRUPT)
26
27 /* TX */
28 #define SCO_TX_ENCODE_SIZE (60)
29 /* 18 = 6 * 180 / SCO_TX_ENCODE_SIZE */
30 #define SCO_TX_PACKER_BUF_NUM (18)
31
32 /* RX */
33 #define SCO_RX_PLC_SIZE (30)
34 #define SCO_RX_PACKER_BUF_NUM (64)
35 #define SCO_RX_PACKET_MASK (0x3F)
36
37 #define SCO_CVSD_PACKET_VALID_SIZE 2
38
39 #define SCO_PACKET_120 120
40 #define SCO_PACKET_180 180
41
42 #define BTCVSD_RX_PACKET_SIZE (SCO_RX_PLC_SIZE + SCO_CVSD_PACKET_VALID_SIZE)
43 #define BTCVSD_TX_PACKET_SIZE (SCO_TX_ENCODE_SIZE)
44
45 #define BTCVSD_RX_BUF_SIZE (BTCVSD_RX_PACKET_SIZE * SCO_RX_PACKER_BUF_NUM)
46 #define BTCVSD_TX_BUF_SIZE (BTCVSD_TX_PACKET_SIZE * SCO_TX_PACKER_BUF_NUM)
47
48 enum bt_sco_state {
49 BT_SCO_STATE_IDLE,
50 BT_SCO_STATE_RUNNING,
51 BT_SCO_STATE_ENDING,
52 BT_SCO_STATE_LOOPBACK,
53 };
54
55 enum bt_sco_direct {
56 BT_SCO_DIRECT_BT2ARM,
57 BT_SCO_DIRECT_ARM2BT,
58 };
59
60 enum bt_sco_packet_len {
61 BT_SCO_CVSD_30 = 0,
62 BT_SCO_CVSD_60,
63 BT_SCO_CVSD_90,
64 BT_SCO_CVSD_120,
65 BT_SCO_CVSD_10,
66 BT_SCO_CVSD_20,
67 BT_SCO_CVSD_MAX,
68 };
69
70 enum BT_SCO_BAND {
71 BT_SCO_NB,
72 BT_SCO_WB,
73 };
74
75 struct mtk_btcvsd_snd_hw_info {
76 unsigned int num_valid_addr;
77 unsigned long bt_sram_addr[20];
78 unsigned int packet_length;
79 unsigned int packet_num;
80 };
81
82 struct mtk_btcvsd_snd_stream {
83 struct snd_pcm_substream *substream;
84 int stream;
85
86 enum bt_sco_state state;
87
88 unsigned int packet_size;
89 unsigned int buf_size;
90 u8 temp_packet_buf[SCO_PACKET_180];
91
92 int packet_w;
93 int packet_r;
94 snd_pcm_uframes_t prev_frame;
95 int prev_packet_idx;
96
97 unsigned int xrun:1;
98 unsigned int timeout:1;
99 unsigned int mute:1;
100 unsigned int trigger_start:1;
101 unsigned int wait_flag:1;
102 unsigned int rw_cnt;
103
104 unsigned long long time_stamp;
105 unsigned long long buf_data_equivalent_time;
106
107 struct mtk_btcvsd_snd_hw_info buffer_info;
108 };
109
110 struct mtk_btcvsd_snd {
111 struct device *dev;
112 int irq_id;
113
114 struct regmap *infra;
115 void __iomem *bt_pkv_base;
116 void __iomem *bt_sram_bank2_base;
117
118 unsigned int infra_misc_offset;
119 unsigned int conn_bt_cvsd_mask;
120 unsigned int cvsd_mcu_read_offset;
121 unsigned int cvsd_mcu_write_offset;
122 unsigned int cvsd_packet_indicator;
123
124 u32 *bt_reg_pkt_r;
125 u32 *bt_reg_pkt_w;
126 u32 *bt_reg_ctl;
127
128 unsigned int irq_disabled:1;
129
130 spinlock_t tx_lock; /* spinlock for bt tx stream control */
131 spinlock_t rx_lock; /* spinlock for bt rx stream control */
132 wait_queue_head_t tx_wait;
133 wait_queue_head_t rx_wait;
134
135 struct mtk_btcvsd_snd_stream *tx;
136 struct mtk_btcvsd_snd_stream *rx;
137 u8 tx_packet_buf[BTCVSD_TX_BUF_SIZE];
138 u8 rx_packet_buf[BTCVSD_RX_BUF_SIZE];
139
140 enum BT_SCO_BAND band;
141 };
142
143 struct mtk_btcvsd_snd_time_buffer_info {
144 unsigned long long data_count_equi_time;
145 unsigned long long time_stamp_us;
146 };
147
148 static const unsigned int btsco_packet_valid_mask[BT_SCO_CVSD_MAX][6] = {
149 {0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3, 0x1 << 4, 0x1 << 5},
150 {0x1, 0x1, 0x2, 0x2, 0x4, 0x4},
151 {0x1, 0x1, 0x1, 0x2, 0x2, 0x2},
152 {0x1, 0x1, 0x1, 0x1, 0x0, 0x0},
153 {0x7, 0x7 << 3, 0x7 << 6, 0x7 << 9, 0x7 << 12, 0x7 << 15},
154 {0x3, 0x3 << 1, 0x3 << 3, 0x3 << 4, 0x3 << 6, 0x3 << 7},
155 };
156
157 static const unsigned int btsco_packet_info[BT_SCO_CVSD_MAX][4] = {
158 {30, 6, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
159 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
160 {60, 3, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
161 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
162 {90, 2, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
163 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
164 {120, 1, SCO_PACKET_120 / SCO_TX_ENCODE_SIZE,
165 SCO_PACKET_120 / SCO_RX_PLC_SIZE},
166 {10, 18, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
167 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
168 {20, 9, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
169 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
170 };
171
172 static const u8 table_msbc_silence[SCO_PACKET_180] = {
173 0x01, 0x38, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
174 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
175 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
176 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
177 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
178 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00,
179 0x01, 0xc8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
180 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
181 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
182 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
183 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
184 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00,
185 0x01, 0xf8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
186 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
187 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
188 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
189 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
190 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00
191 };
192
mtk_btcvsd_snd_irq_enable(struct mtk_btcvsd_snd * bt)193 static void mtk_btcvsd_snd_irq_enable(struct mtk_btcvsd_snd *bt)
194 {
195 regmap_update_bits(bt->infra, bt->infra_misc_offset,
196 bt->conn_bt_cvsd_mask, 0);
197 }
198
mtk_btcvsd_snd_irq_disable(struct mtk_btcvsd_snd * bt)199 static void mtk_btcvsd_snd_irq_disable(struct mtk_btcvsd_snd *bt)
200 {
201 regmap_update_bits(bt->infra, bt->infra_misc_offset,
202 bt->conn_bt_cvsd_mask, bt->conn_bt_cvsd_mask);
203 }
204
mtk_btcvsd_snd_set_state(struct mtk_btcvsd_snd * bt,struct mtk_btcvsd_snd_stream * bt_stream,int state)205 static void mtk_btcvsd_snd_set_state(struct mtk_btcvsd_snd *bt,
206 struct mtk_btcvsd_snd_stream *bt_stream,
207 int state)
208 {
209 dev_dbg(bt->dev, "%s(), stream %d, state %d, tx->state %d, rx->state %d, irq_disabled %d\n",
210 __func__,
211 bt_stream->stream, state,
212 bt->tx->state, bt->rx->state, bt->irq_disabled);
213
214 bt_stream->state = state;
215
216 if (bt->tx->state == BT_SCO_STATE_IDLE &&
217 bt->rx->state == BT_SCO_STATE_IDLE) {
218 if (!bt->irq_disabled) {
219 disable_irq(bt->irq_id);
220 mtk_btcvsd_snd_irq_disable(bt);
221 bt->irq_disabled = 1;
222 }
223 } else {
224 if (bt->irq_disabled) {
225 enable_irq(bt->irq_id);
226 mtk_btcvsd_snd_irq_enable(bt);
227 bt->irq_disabled = 0;
228 }
229 }
230 }
231
mtk_btcvsd_snd_tx_init(struct mtk_btcvsd_snd * bt)232 static int mtk_btcvsd_snd_tx_init(struct mtk_btcvsd_snd *bt)
233 {
234 memset(bt->tx, 0, sizeof(*bt->tx));
235 memset(bt->tx_packet_buf, 0, sizeof(bt->tx_packet_buf));
236
237 bt->tx->packet_size = BTCVSD_TX_PACKET_SIZE;
238 bt->tx->buf_size = BTCVSD_TX_BUF_SIZE;
239 bt->tx->timeout = 0;
240 bt->tx->rw_cnt = 0;
241 bt->tx->stream = SNDRV_PCM_STREAM_PLAYBACK;
242 return 0;
243 }
244
mtk_btcvsd_snd_rx_init(struct mtk_btcvsd_snd * bt)245 static int mtk_btcvsd_snd_rx_init(struct mtk_btcvsd_snd *bt)
246 {
247 memset(bt->rx, 0, sizeof(*bt->rx));
248 memset(bt->rx_packet_buf, 0, sizeof(bt->rx_packet_buf));
249
250 bt->rx->packet_size = BTCVSD_RX_PACKET_SIZE;
251 bt->rx->buf_size = BTCVSD_RX_BUF_SIZE;
252 bt->rx->timeout = 0;
253 bt->rx->rw_cnt = 0;
254 bt->rx->stream = SNDRV_PCM_STREAM_CAPTURE;
255 return 0;
256 }
257
get_tx_time_stamp(struct mtk_btcvsd_snd * bt,struct mtk_btcvsd_snd_time_buffer_info * ts)258 static void get_tx_time_stamp(struct mtk_btcvsd_snd *bt,
259 struct mtk_btcvsd_snd_time_buffer_info *ts)
260 {
261 ts->time_stamp_us = bt->tx->time_stamp;
262 ts->data_count_equi_time = bt->tx->buf_data_equivalent_time;
263 }
264
get_rx_time_stamp(struct mtk_btcvsd_snd * bt,struct mtk_btcvsd_snd_time_buffer_info * ts)265 static void get_rx_time_stamp(struct mtk_btcvsd_snd *bt,
266 struct mtk_btcvsd_snd_time_buffer_info *ts)
267 {
268 ts->time_stamp_us = bt->rx->time_stamp;
269 ts->data_count_equi_time = bt->rx->buf_data_equivalent_time;
270 }
271
btcvsd_bytes_to_frame(struct snd_pcm_substream * substream,int bytes)272 static int btcvsd_bytes_to_frame(struct snd_pcm_substream *substream,
273 int bytes)
274 {
275 int count = bytes;
276 struct snd_pcm_runtime *runtime = substream->runtime;
277
278 if (runtime->format == SNDRV_PCM_FORMAT_S32_LE ||
279 runtime->format == SNDRV_PCM_FORMAT_U32_LE)
280 count = count >> 2;
281 else
282 count = count >> 1;
283
284 count = count / runtime->channels;
285 return count;
286 }
287
mtk_btcvsd_snd_data_transfer(enum bt_sco_direct dir,u8 * src,u8 * dst,unsigned int blk_size,unsigned int blk_num)288 static void mtk_btcvsd_snd_data_transfer(enum bt_sco_direct dir,
289 u8 *src, u8 *dst,
290 unsigned int blk_size,
291 unsigned int blk_num)
292 {
293 unsigned int i, j;
294
295 if (blk_size == 60 || blk_size == 120 || blk_size == 20) {
296 u32 *src_32 = (u32 *)src;
297 u32 *dst_32 = (u32 *)dst;
298
299 for (i = 0; i < (blk_size * blk_num / 4); i++)
300 *dst_32++ = *src_32++;
301 } else {
302 u16 *src_16 = (u16 *)src;
303 u16 *dst_16 = (u16 *)dst;
304
305 for (j = 0; j < blk_num; j++) {
306 for (i = 0; i < (blk_size / 2); i++)
307 *dst_16++ = *src_16++;
308
309 if (dir == BT_SCO_DIRECT_BT2ARM)
310 src_16++;
311 else
312 dst_16++;
313 }
314 }
315 }
316
317 /* write encoded mute data to bt sram */
btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd * bt)318 static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt)
319 {
320 unsigned int i;
321 unsigned int num_valid_addr;
322 unsigned long flags;
323 enum BT_SCO_BAND band = bt->band;
324
325 /* prepare encoded mute data */
326 if (band == BT_SCO_NB)
327 memset(bt->tx->temp_packet_buf, 170, SCO_PACKET_180);
328 else
329 memcpy(bt->tx->temp_packet_buf,
330 table_msbc_silence, SCO_PACKET_180);
331
332 /* write mute data to bt tx sram buffer */
333 spin_lock_irqsave(&bt->tx_lock, flags);
334 num_valid_addr = bt->tx->buffer_info.num_valid_addr;
335
336 dev_info(bt->dev, "%s(), band %d, num_valid_addr %u\n",
337 __func__, band, num_valid_addr);
338
339 for (i = 0; i < num_valid_addr; i++) {
340 void *dst;
341
342 dev_info(bt->dev, "%s(), clean addr 0x%lx\n", __func__,
343 bt->tx->buffer_info.bt_sram_addr[i]);
344
345 dst = (void *)bt->tx->buffer_info.bt_sram_addr[i];
346
347 mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
348 bt->tx->temp_packet_buf, dst,
349 bt->tx->buffer_info.packet_length,
350 bt->tx->buffer_info.packet_num);
351 }
352 spin_unlock_irqrestore(&bt->tx_lock, flags);
353
354 return 0;
355 }
356
mtk_btcvsd_read_from_bt(struct mtk_btcvsd_snd * bt,enum bt_sco_packet_len packet_type,unsigned int packet_length,unsigned int packet_num,unsigned int blk_size,unsigned int control)357 static int mtk_btcvsd_read_from_bt(struct mtk_btcvsd_snd *bt,
358 enum bt_sco_packet_len packet_type,
359 unsigned int packet_length,
360 unsigned int packet_num,
361 unsigned int blk_size,
362 unsigned int control)
363 {
364 unsigned int i;
365 int pv;
366 u8 *src;
367 unsigned int packet_buf_ofs;
368 unsigned long flags;
369 unsigned long connsys_addr_rx, ap_addr_rx;
370
371 connsys_addr_rx = *bt->bt_reg_pkt_r;
372 ap_addr_rx = (unsigned long)bt->bt_sram_bank2_base +
373 (connsys_addr_rx & 0xFFFF);
374
375 if (connsys_addr_rx == 0xdeadfeed) {
376 /* bt return 0xdeadfeed if read register during bt sleep */
377 dev_warn(bt->dev, "%s(), connsys_addr_rx == 0xdeadfeed",
378 __func__);
379 return -EIO;
380 }
381
382 src = (u8 *)ap_addr_rx;
383
384 mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_BT2ARM, src,
385 bt->rx->temp_packet_buf, packet_length,
386 packet_num);
387
388 spin_lock_irqsave(&bt->rx_lock, flags);
389 for (i = 0; i < blk_size; i++) {
390 packet_buf_ofs = (bt->rx->packet_w & SCO_RX_PACKET_MASK) *
391 bt->rx->packet_size;
392 memcpy(bt->rx_packet_buf + packet_buf_ofs,
393 bt->rx->temp_packet_buf + (SCO_RX_PLC_SIZE * i),
394 SCO_RX_PLC_SIZE);
395 if ((control & btsco_packet_valid_mask[packet_type][i]) ==
396 btsco_packet_valid_mask[packet_type][i])
397 pv = 1;
398 else
399 pv = 0;
400
401 packet_buf_ofs += SCO_RX_PLC_SIZE;
402 memcpy(bt->rx_packet_buf + packet_buf_ofs, (void *)&pv,
403 SCO_CVSD_PACKET_VALID_SIZE);
404 bt->rx->packet_w++;
405 }
406 spin_unlock_irqrestore(&bt->rx_lock, flags);
407 return 0;
408 }
409
mtk_btcvsd_write_to_bt(struct mtk_btcvsd_snd * bt,enum bt_sco_packet_len packet_type,unsigned int packet_length,unsigned int packet_num,unsigned int blk_size)410 static int mtk_btcvsd_write_to_bt(struct mtk_btcvsd_snd *bt,
411 enum bt_sco_packet_len packet_type,
412 unsigned int packet_length,
413 unsigned int packet_num,
414 unsigned int blk_size)
415 {
416 unsigned int i;
417 unsigned long flags;
418 u8 *dst;
419 unsigned long connsys_addr_tx, ap_addr_tx;
420 bool new_ap_addr_tx = true;
421
422 connsys_addr_tx = *bt->bt_reg_pkt_w;
423 ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base +
424 (connsys_addr_tx & 0xFFFF);
425
426 if (connsys_addr_tx == 0xdeadfeed) {
427 /* bt return 0xdeadfeed if read register during bt sleep */
428 dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n",
429 __func__);
430 return -EIO;
431 }
432
433 spin_lock_irqsave(&bt->tx_lock, flags);
434 for (i = 0; i < blk_size; i++) {
435 memcpy(bt->tx->temp_packet_buf + (bt->tx->packet_size * i),
436 (bt->tx_packet_buf +
437 (bt->tx->packet_r % SCO_TX_PACKER_BUF_NUM) *
438 bt->tx->packet_size),
439 bt->tx->packet_size);
440
441 bt->tx->packet_r++;
442 }
443 spin_unlock_irqrestore(&bt->tx_lock, flags);
444
445 dst = (u8 *)ap_addr_tx;
446
447 if (!bt->tx->mute) {
448 mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
449 bt->tx->temp_packet_buf, dst,
450 packet_length, packet_num);
451 }
452
453 /* store bt tx buffer sram info */
454 bt->tx->buffer_info.packet_length = packet_length;
455 bt->tx->buffer_info.packet_num = packet_num;
456 for (i = 0; i < bt->tx->buffer_info.num_valid_addr; i++) {
457 if (bt->tx->buffer_info.bt_sram_addr[i] == ap_addr_tx) {
458 new_ap_addr_tx = false;
459 break;
460 }
461 }
462 if (new_ap_addr_tx) {
463 unsigned int next_idx;
464
465 spin_lock_irqsave(&bt->tx_lock, flags);
466 bt->tx->buffer_info.num_valid_addr++;
467 next_idx = bt->tx->buffer_info.num_valid_addr - 1;
468 bt->tx->buffer_info.bt_sram_addr[next_idx] = ap_addr_tx;
469 spin_unlock_irqrestore(&bt->tx_lock, flags);
470 dev_info(bt->dev, "%s(), new ap_addr_tx = 0x%lx, num_valid_addr %d\n",
471 __func__, ap_addr_tx,
472 bt->tx->buffer_info.num_valid_addr);
473 }
474
475 if (bt->tx->mute)
476 btcvsd_tx_clean_buffer(bt);
477
478 return 0;
479 }
480
mtk_btcvsd_snd_irq_handler(int irq_id,void * dev)481 static irqreturn_t mtk_btcvsd_snd_irq_handler(int irq_id, void *dev)
482 {
483 struct mtk_btcvsd_snd *bt = dev;
484 unsigned int packet_type, packet_num, packet_length;
485 unsigned int buf_cnt_tx, buf_cnt_rx, control;
486
487 if (bt->rx->state != BT_SCO_STATE_RUNNING &&
488 bt->rx->state != BT_SCO_STATE_ENDING &&
489 bt->tx->state != BT_SCO_STATE_RUNNING &&
490 bt->tx->state != BT_SCO_STATE_ENDING &&
491 bt->tx->state != BT_SCO_STATE_LOOPBACK) {
492 dev_warn(bt->dev, "%s(), in idle state: rx->state: %d, tx->state: %d\n",
493 __func__, bt->rx->state, bt->tx->state);
494 goto irq_handler_exit;
495 }
496
497 control = *bt->bt_reg_ctl;
498 packet_type = (control >> 18) & 0x7;
499
500 if (((control >> 31) & 1) == 0) {
501 dev_warn(bt->dev, "%s(), ((control >> 31) & 1) == 0, control 0x%x\n",
502 __func__, control);
503 goto irq_handler_exit;
504 }
505
506 if (packet_type >= BT_SCO_CVSD_MAX) {
507 dev_warn(bt->dev, "%s(), invalid packet_type %u, exit\n",
508 __func__, packet_type);
509 goto irq_handler_exit;
510 }
511
512 packet_length = btsco_packet_info[packet_type][0];
513 packet_num = btsco_packet_info[packet_type][1];
514 buf_cnt_tx = btsco_packet_info[packet_type][2];
515 buf_cnt_rx = btsco_packet_info[packet_type][3];
516
517 if (bt->tx->state == BT_SCO_STATE_LOOPBACK) {
518 u8 *src, *dst;
519 unsigned long connsys_addr_rx, ap_addr_rx;
520 unsigned long connsys_addr_tx, ap_addr_tx;
521
522 connsys_addr_rx = *bt->bt_reg_pkt_r;
523 ap_addr_rx = (unsigned long)bt->bt_sram_bank2_base +
524 (connsys_addr_rx & 0xFFFF);
525
526 connsys_addr_tx = *bt->bt_reg_pkt_w;
527 ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base +
528 (connsys_addr_tx & 0xFFFF);
529
530 if (connsys_addr_tx == 0xdeadfeed ||
531 connsys_addr_rx == 0xdeadfeed) {
532 /* bt return 0xdeadfeed if read reg during bt sleep */
533 dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n",
534 __func__);
535 goto irq_handler_exit;
536 }
537
538 src = (u8 *)ap_addr_rx;
539 dst = (u8 *)ap_addr_tx;
540
541 mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_BT2ARM, src,
542 bt->tx->temp_packet_buf,
543 packet_length,
544 packet_num);
545 mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
546 bt->tx->temp_packet_buf, dst,
547 packet_length,
548 packet_num);
549 bt->rx->rw_cnt++;
550 bt->tx->rw_cnt++;
551 }
552
553 if (bt->rx->state == BT_SCO_STATE_RUNNING ||
554 bt->rx->state == BT_SCO_STATE_ENDING) {
555 if (bt->rx->xrun) {
556 if (bt->rx->packet_w - bt->rx->packet_r <=
557 SCO_RX_PACKER_BUF_NUM - 2 * buf_cnt_rx) {
558 /*
559 * free space is larger then
560 * twice interrupt rx data size
561 */
562 bt->rx->xrun = 0;
563 dev_warn(bt->dev, "%s(), rx->xrun 0!\n",
564 __func__);
565 }
566 }
567
568 if (!bt->rx->xrun &&
569 (bt->rx->packet_w - bt->rx->packet_r <=
570 SCO_RX_PACKER_BUF_NUM - buf_cnt_rx)) {
571 mtk_btcvsd_read_from_bt(bt,
572 packet_type,
573 packet_length,
574 packet_num,
575 buf_cnt_rx,
576 control);
577 bt->rx->rw_cnt++;
578 } else {
579 bt->rx->xrun = 1;
580 dev_warn(bt->dev, "%s(), rx->xrun 1\n", __func__);
581 }
582 }
583
584 /* tx */
585 bt->tx->timeout = 0;
586 if ((bt->tx->state == BT_SCO_STATE_RUNNING ||
587 bt->tx->state == BT_SCO_STATE_ENDING) &&
588 bt->tx->trigger_start) {
589 if (bt->tx->xrun) {
590 /* prepared data is larger then twice
591 * interrupt tx data size
592 */
593 if (bt->tx->packet_w - bt->tx->packet_r >=
594 2 * buf_cnt_tx) {
595 bt->tx->xrun = 0;
596 dev_warn(bt->dev, "%s(), tx->xrun 0\n",
597 __func__);
598 }
599 }
600
601 if ((!bt->tx->xrun &&
602 (bt->tx->packet_w - bt->tx->packet_r >= buf_cnt_tx)) ||
603 bt->tx->state == BT_SCO_STATE_ENDING) {
604 mtk_btcvsd_write_to_bt(bt,
605 packet_type,
606 packet_length,
607 packet_num,
608 buf_cnt_tx);
609 bt->tx->rw_cnt++;
610 } else {
611 bt->tx->xrun = 1;
612 dev_warn(bt->dev, "%s(), tx->xrun 1\n", __func__);
613 }
614 }
615
616 *bt->bt_reg_ctl &= ~BT_CVSD_CLEAR;
617
618 if (bt->rx->state == BT_SCO_STATE_RUNNING ||
619 bt->rx->state == BT_SCO_STATE_ENDING) {
620 bt->rx->wait_flag = 1;
621 wake_up_interruptible(&bt->rx_wait);
622 snd_pcm_period_elapsed(bt->rx->substream);
623 }
624 if (bt->tx->state == BT_SCO_STATE_RUNNING ||
625 bt->tx->state == BT_SCO_STATE_ENDING) {
626 bt->tx->wait_flag = 1;
627 wake_up_interruptible(&bt->tx_wait);
628 snd_pcm_period_elapsed(bt->tx->substream);
629 }
630
631 return IRQ_HANDLED;
632 irq_handler_exit:
633 *bt->bt_reg_ctl &= ~BT_CVSD_CLEAR;
634 return IRQ_HANDLED;
635 }
636
wait_for_bt_irq(struct mtk_btcvsd_snd * bt,struct mtk_btcvsd_snd_stream * bt_stream)637 static int wait_for_bt_irq(struct mtk_btcvsd_snd *bt,
638 struct mtk_btcvsd_snd_stream *bt_stream)
639 {
640 unsigned long long t1, t2;
641 /* one interrupt period = 22.5ms */
642 unsigned long long timeout_limit = 22500000;
643 int max_timeout_trial = 2;
644 int ret;
645
646 bt_stream->wait_flag = 0;
647
648 while (max_timeout_trial && !bt_stream->wait_flag) {
649 t1 = sched_clock();
650 if (bt_stream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
651 ret = wait_event_interruptible_timeout(bt->tx_wait,
652 bt_stream->wait_flag,
653 nsecs_to_jiffies(timeout_limit));
654 } else {
655 ret = wait_event_interruptible_timeout(bt->rx_wait,
656 bt_stream->wait_flag,
657 nsecs_to_jiffies(timeout_limit));
658 }
659
660 t2 = sched_clock();
661 t2 = t2 - t1; /* in ns (10^9) */
662
663 if (t2 > timeout_limit) {
664 dev_warn(bt->dev, "%s(), stream %d, timeout %llu, limit %llu, ret %d, flag %d\n",
665 __func__, bt_stream->stream,
666 t2, timeout_limit, ret,
667 bt_stream->wait_flag);
668 }
669
670 if (ret < 0) {
671 /*
672 * error, -ERESTARTSYS if it was interrupted by
673 * a signal
674 */
675 dev_warn(bt->dev, "%s(), stream %d, error, trial left %d\n",
676 __func__,
677 bt_stream->stream, max_timeout_trial);
678
679 bt_stream->timeout = 1;
680 return ret;
681 } else if (ret == 0) {
682 /* conidtion is false after timeout */
683 max_timeout_trial--;
684 dev_warn(bt->dev, "%s(), stream %d, error, timeout, condition is false, trial left %d\n",
685 __func__,
686 bt_stream->stream, max_timeout_trial);
687
688 if (max_timeout_trial <= 0) {
689 bt_stream->timeout = 1;
690 return -ETIME;
691 }
692 }
693 }
694
695 return 0;
696 }
697
mtk_btcvsd_snd_read(struct mtk_btcvsd_snd * bt,char __user * buf,size_t count)698 static ssize_t mtk_btcvsd_snd_read(struct mtk_btcvsd_snd *bt,
699 char __user *buf,
700 size_t count)
701 {
702 ssize_t read_size = 0, read_count = 0, cur_read_idx, cont;
703 unsigned int cur_buf_ofs = 0;
704 unsigned long avail;
705 unsigned long flags;
706 unsigned int packet_size = bt->rx->packet_size;
707
708 while (count) {
709 spin_lock_irqsave(&bt->rx_lock, flags);
710 /* available data in RX packet buffer */
711 avail = (bt->rx->packet_w - bt->rx->packet_r) * packet_size;
712
713 cur_read_idx = (bt->rx->packet_r & SCO_RX_PACKET_MASK) *
714 packet_size;
715 spin_unlock_irqrestore(&bt->rx_lock, flags);
716
717 if (!avail) {
718 int ret = wait_for_bt_irq(bt, bt->rx);
719
720 if (ret)
721 return read_count;
722
723 continue;
724 }
725
726 /* count must be multiple of packet_size */
727 if (count % packet_size != 0 ||
728 avail % packet_size != 0) {
729 dev_warn(bt->dev, "%s(), count %zu or d %lu is not multiple of packet_size %dd\n",
730 __func__, count, avail, packet_size);
731
732 count -= count % packet_size;
733 avail -= avail % packet_size;
734 }
735
736 if (count > avail)
737 read_size = avail;
738 else
739 read_size = count;
740
741 /* calculate continue space */
742 cont = bt->rx->buf_size - cur_read_idx;
743 if (read_size > cont)
744 read_size = cont;
745
746 if (copy_to_user(buf + cur_buf_ofs,
747 bt->rx_packet_buf + cur_read_idx,
748 read_size)) {
749 dev_warn(bt->dev, "%s(), copy_to_user fail\n",
750 __func__);
751 return -EFAULT;
752 }
753
754 spin_lock_irqsave(&bt->rx_lock, flags);
755 bt->rx->packet_r += read_size / packet_size;
756 spin_unlock_irqrestore(&bt->rx_lock, flags);
757
758 read_count += read_size;
759 cur_buf_ofs += read_size;
760 count -= read_size;
761 }
762
763 /*
764 * save current timestamp & buffer time in times_tamp and
765 * buf_data_equivalent_time
766 */
767 bt->rx->time_stamp = sched_clock();
768 bt->rx->buf_data_equivalent_time =
769 (unsigned long long)(bt->rx->packet_w - bt->rx->packet_r) *
770 SCO_RX_PLC_SIZE * 16 * 1000 / 2 / 64;
771 bt->rx->buf_data_equivalent_time += read_count * SCO_RX_PLC_SIZE *
772 16 * 1000 / packet_size / 2 / 64;
773 /* return equivalent time(us) to data count */
774 bt->rx->buf_data_equivalent_time *= 1000;
775
776 return read_count;
777 }
778
mtk_btcvsd_snd_write(struct mtk_btcvsd_snd * bt,char __user * buf,size_t count)779 static ssize_t mtk_btcvsd_snd_write(struct mtk_btcvsd_snd *bt,
780 char __user *buf,
781 size_t count)
782 {
783 int written_size = count, avail = 0, cur_write_idx, write_size, cont;
784 unsigned int cur_buf_ofs = 0;
785 unsigned long flags;
786 unsigned int packet_size = bt->tx->packet_size;
787
788 /*
789 * save current timestamp & buffer time in time_stamp and
790 * buf_data_equivalent_time
791 */
792 bt->tx->time_stamp = sched_clock();
793 bt->tx->buf_data_equivalent_time =
794 (unsigned long long)(bt->tx->packet_w - bt->tx->packet_r) *
795 packet_size * 16 * 1000 / 2 / 64;
796
797 /* return equivalent time(us) to data count */
798 bt->tx->buf_data_equivalent_time *= 1000;
799
800 while (count) {
801 spin_lock_irqsave(&bt->tx_lock, flags);
802 /* free space of TX packet buffer */
803 avail = bt->tx->buf_size -
804 (bt->tx->packet_w - bt->tx->packet_r) * packet_size;
805
806 cur_write_idx = (bt->tx->packet_w % SCO_TX_PACKER_BUF_NUM) *
807 packet_size;
808 spin_unlock_irqrestore(&bt->tx_lock, flags);
809
810 if (!avail) {
811 int ret = wait_for_bt_irq(bt, bt->rx);
812
813 if (ret)
814 return written_size;
815
816 continue;
817 }
818
819 /* count must be multiple of bt->tx->packet_size */
820 if (count % packet_size != 0 ||
821 avail % packet_size != 0) {
822 dev_warn(bt->dev, "%s(), count %zu or avail %d is not multiple of packet_size %d\n",
823 __func__, count, avail, packet_size);
824 count -= count % packet_size;
825 avail -= avail % packet_size;
826 }
827
828 if (count > avail)
829 write_size = avail;
830 else
831 write_size = count;
832
833 /* calculate continue space */
834 cont = bt->tx->buf_size - cur_write_idx;
835 if (write_size > cont)
836 write_size = cont;
837
838 if (copy_from_user(bt->tx_packet_buf +
839 cur_write_idx,
840 buf + cur_buf_ofs,
841 write_size)) {
842 dev_warn(bt->dev, "%s(), copy_from_user fail\n",
843 __func__);
844 return -EFAULT;
845 }
846
847 spin_lock_irqsave(&bt->tx_lock, flags);
848 bt->tx->packet_w += write_size / packet_size;
849 spin_unlock_irqrestore(&bt->tx_lock, flags);
850 cur_buf_ofs += write_size;
851 count -= write_size;
852 }
853
854 return written_size;
855 }
856
get_bt_stream(struct mtk_btcvsd_snd * bt,struct snd_pcm_substream * substream)857 static struct mtk_btcvsd_snd_stream *get_bt_stream
858 (struct mtk_btcvsd_snd *bt, struct snd_pcm_substream *substream)
859 {
860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
861 return bt->tx;
862 else
863 return bt->rx;
864 }
865
866 /* pcm ops */
867 static const struct snd_pcm_hardware mtk_btcvsd_hardware = {
868 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
869 SNDRV_PCM_INFO_RESUME),
870 .formats = SNDRV_PCM_FMTBIT_S16_LE,
871 .buffer_bytes_max = 24 * 1024,
872 .period_bytes_max = 24 * 1024,
873 .periods_min = 2,
874 .periods_max = 16,
875 .fifo_size = 0,
876 };
877
mtk_pcm_btcvsd_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)878 static int mtk_pcm_btcvsd_open(struct snd_soc_component *component,
879 struct snd_pcm_substream *substream)
880 {
881 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
882 int ret;
883
884 dev_dbg(bt->dev, "%s(), stream %d, substream %p\n",
885 __func__, substream->stream, substream);
886
887 snd_soc_set_runtime_hwparams(substream, &mtk_btcvsd_hardware);
888
889 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
890 ret = mtk_btcvsd_snd_tx_init(bt);
891 bt->tx->substream = substream;
892 } else {
893 ret = mtk_btcvsd_snd_rx_init(bt);
894 bt->rx->substream = substream;
895 }
896
897 return ret;
898 }
899
mtk_pcm_btcvsd_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)900 static int mtk_pcm_btcvsd_close(struct snd_soc_component *component,
901 struct snd_pcm_substream *substream)
902 {
903 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
904 struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
905
906 dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);
907
908 mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_IDLE);
909 bt_stream->substream = NULL;
910 return 0;
911 }
912
mtk_pcm_btcvsd_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)913 static int mtk_pcm_btcvsd_hw_params(struct snd_soc_component *component,
914 struct snd_pcm_substream *substream,
915 struct snd_pcm_hw_params *hw_params)
916 {
917 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
918
919 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
920 params_buffer_bytes(hw_params) % bt->tx->packet_size != 0) {
921 dev_warn(bt->dev, "%s(), error, buffer size %d not valid\n",
922 __func__,
923 params_buffer_bytes(hw_params));
924 return -EINVAL;
925 }
926
927 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
928 return 0;
929 }
930
mtk_pcm_btcvsd_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)931 static int mtk_pcm_btcvsd_hw_free(struct snd_soc_component *component,
932 struct snd_pcm_substream *substream)
933 {
934 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
935
936 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
937 btcvsd_tx_clean_buffer(bt);
938
939 return 0;
940 }
941
mtk_pcm_btcvsd_prepare(struct snd_soc_component * component,struct snd_pcm_substream * substream)942 static int mtk_pcm_btcvsd_prepare(struct snd_soc_component *component,
943 struct snd_pcm_substream *substream)
944 {
945 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
946 struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
947
948 dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);
949
950 mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_RUNNING);
951 return 0;
952 }
953
mtk_pcm_btcvsd_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)954 static int mtk_pcm_btcvsd_trigger(struct snd_soc_component *component,
955 struct snd_pcm_substream *substream, int cmd)
956 {
957 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
958 struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
959 int stream = substream->stream;
960 int hw_packet_ptr;
961
962 dev_dbg(bt->dev, "%s(), stream %d, cmd %d\n",
963 __func__, substream->stream, cmd);
964
965 switch (cmd) {
966 case SNDRV_PCM_TRIGGER_START:
967 case SNDRV_PCM_TRIGGER_RESUME:
968 hw_packet_ptr = stream == SNDRV_PCM_STREAM_PLAYBACK ?
969 bt_stream->packet_r : bt_stream->packet_w;
970 bt_stream->prev_packet_idx = hw_packet_ptr;
971 bt_stream->prev_frame = 0;
972 bt_stream->trigger_start = 1;
973 return 0;
974 case SNDRV_PCM_TRIGGER_STOP:
975 case SNDRV_PCM_TRIGGER_SUSPEND:
976 bt_stream->trigger_start = 0;
977 mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_ENDING);
978 return 0;
979 default:
980 return -EINVAL;
981 }
982 }
983
mtk_pcm_btcvsd_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)984 static snd_pcm_uframes_t mtk_pcm_btcvsd_pointer(
985 struct snd_soc_component *component,
986 struct snd_pcm_substream *substream)
987 {
988 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
989 struct mtk_btcvsd_snd_stream *bt_stream;
990 snd_pcm_uframes_t frame = 0;
991 int byte = 0;
992 int hw_packet_ptr;
993 int packet_diff;
994 spinlock_t *lock; /* spinlock for bt stream control */
995 unsigned long flags;
996
997 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
998 lock = &bt->tx_lock;
999 bt_stream = bt->tx;
1000 } else {
1001 lock = &bt->rx_lock;
1002 bt_stream = bt->rx;
1003 }
1004
1005 spin_lock_irqsave(lock, flags);
1006 hw_packet_ptr = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1007 bt->tx->packet_r : bt->rx->packet_w;
1008
1009 /* get packet diff from last time */
1010 if (hw_packet_ptr >= bt_stream->prev_packet_idx) {
1011 packet_diff = hw_packet_ptr - bt_stream->prev_packet_idx;
1012 } else {
1013 /* integer overflow */
1014 packet_diff = (INT_MAX - bt_stream->prev_packet_idx) +
1015 (hw_packet_ptr - INT_MIN) + 1;
1016 }
1017 bt_stream->prev_packet_idx = hw_packet_ptr;
1018
1019 /* increased bytes */
1020 byte = packet_diff * bt_stream->packet_size;
1021
1022 frame = btcvsd_bytes_to_frame(substream, byte);
1023 frame += bt_stream->prev_frame;
1024 frame %= substream->runtime->buffer_size;
1025
1026 bt_stream->prev_frame = frame;
1027
1028 spin_unlock_irqrestore(lock, flags);
1029
1030 return frame;
1031 }
1032
mtk_pcm_btcvsd_copy(struct snd_soc_component * component,struct snd_pcm_substream * substream,int channel,unsigned long pos,void __user * buf,unsigned long count)1033 static int mtk_pcm_btcvsd_copy(struct snd_soc_component *component,
1034 struct snd_pcm_substream *substream,
1035 int channel, unsigned long pos,
1036 void __user *buf, unsigned long count)
1037 {
1038 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
1039
1040 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1041 mtk_btcvsd_snd_write(bt, buf, count);
1042 else
1043 mtk_btcvsd_snd_read(bt, buf, count);
1044
1045 return 0;
1046 }
1047
1048 /* kcontrol */
1049 static const char *const btsco_band_str[] = {"NB", "WB"};
1050
1051 static const struct soc_enum btcvsd_enum[] = {
1052 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(btsco_band_str), btsco_band_str),
1053 };
1054
btcvsd_band_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1055 static int btcvsd_band_get(struct snd_kcontrol *kcontrol,
1056 struct snd_ctl_elem_value *ucontrol)
1057 {
1058 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1059 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1060
1061 ucontrol->value.integer.value[0] = bt->band;
1062 return 0;
1063 }
1064
btcvsd_band_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1065 static int btcvsd_band_set(struct snd_kcontrol *kcontrol,
1066 struct snd_ctl_elem_value *ucontrol)
1067 {
1068 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1069 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1070 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1071
1072 if (ucontrol->value.enumerated.item[0] >= e->items)
1073 return -EINVAL;
1074
1075 bt->band = ucontrol->value.integer.value[0];
1076 dev_dbg(bt->dev, "%s(), band %d\n", __func__, bt->band);
1077 return 0;
1078 }
1079
btcvsd_loopback_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1080 static int btcvsd_loopback_get(struct snd_kcontrol *kcontrol,
1081 struct snd_ctl_elem_value *ucontrol)
1082 {
1083 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1084 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1085 bool lpbk_en = bt->tx->state == BT_SCO_STATE_LOOPBACK;
1086
1087 ucontrol->value.integer.value[0] = lpbk_en;
1088 return 0;
1089 }
1090
btcvsd_loopback_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1091 static int btcvsd_loopback_set(struct snd_kcontrol *kcontrol,
1092 struct snd_ctl_elem_value *ucontrol)
1093 {
1094 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1095 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1096
1097 if (ucontrol->value.integer.value[0]) {
1098 mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_LOOPBACK);
1099 mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_LOOPBACK);
1100 } else {
1101 mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_RUNNING);
1102 mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_RUNNING);
1103 }
1104 return 0;
1105 }
1106
btcvsd_tx_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1107 static int btcvsd_tx_mute_get(struct snd_kcontrol *kcontrol,
1108 struct snd_ctl_elem_value *ucontrol)
1109 {
1110 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1111 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1112
1113 if (!bt->tx) {
1114 ucontrol->value.integer.value[0] = 0;
1115 return 0;
1116 }
1117
1118 ucontrol->value.integer.value[0] = bt->tx->mute;
1119 return 0;
1120 }
1121
btcvsd_tx_mute_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1122 static int btcvsd_tx_mute_set(struct snd_kcontrol *kcontrol,
1123 struct snd_ctl_elem_value *ucontrol)
1124 {
1125 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1126 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1127
1128 if (!bt->tx)
1129 return 0;
1130
1131 bt->tx->mute = ucontrol->value.integer.value[0];
1132 return 0;
1133 }
1134
btcvsd_rx_irq_received_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1135 static int btcvsd_rx_irq_received_get(struct snd_kcontrol *kcontrol,
1136 struct snd_ctl_elem_value *ucontrol)
1137 {
1138 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1139 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1140
1141 if (!bt->rx)
1142 return 0;
1143
1144 ucontrol->value.integer.value[0] = bt->rx->rw_cnt ? 1 : 0;
1145 return 0;
1146 }
1147
btcvsd_rx_timeout_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1148 static int btcvsd_rx_timeout_get(struct snd_kcontrol *kcontrol,
1149 struct snd_ctl_elem_value *ucontrol)
1150 {
1151 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1152 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1153
1154 if (!bt->rx)
1155 return 0;
1156
1157 ucontrol->value.integer.value[0] = bt->rx->timeout;
1158 bt->rx->timeout = 0;
1159 return 0;
1160 }
1161
btcvsd_rx_timestamp_get(struct snd_kcontrol * kcontrol,unsigned int __user * data,unsigned int size)1162 static int btcvsd_rx_timestamp_get(struct snd_kcontrol *kcontrol,
1163 unsigned int __user *data, unsigned int size)
1164 {
1165 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1166 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1167 int ret = 0;
1168 struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_rx;
1169
1170 if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
1171 return -EINVAL;
1172
1173 get_rx_time_stamp(bt, &time_buffer_info_rx);
1174
1175 dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
1176 __func__,
1177 time_buffer_info_rx.time_stamp_us,
1178 time_buffer_info_rx.data_count_equi_time);
1179
1180 if (copy_to_user(data, &time_buffer_info_rx,
1181 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
1182 dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
1183 ret = -EFAULT;
1184 }
1185
1186 return ret;
1187 }
1188
btcvsd_tx_irq_received_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1189 static int btcvsd_tx_irq_received_get(struct snd_kcontrol *kcontrol,
1190 struct snd_ctl_elem_value *ucontrol)
1191 {
1192 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1193 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1194
1195 if (!bt->tx)
1196 return 0;
1197
1198 ucontrol->value.integer.value[0] = bt->tx->rw_cnt ? 1 : 0;
1199 return 0;
1200 }
1201
btcvsd_tx_timeout_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1202 static int btcvsd_tx_timeout_get(struct snd_kcontrol *kcontrol,
1203 struct snd_ctl_elem_value *ucontrol)
1204 {
1205 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1206 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1207
1208 ucontrol->value.integer.value[0] = bt->tx->timeout;
1209 return 0;
1210 }
1211
btcvsd_tx_timestamp_get(struct snd_kcontrol * kcontrol,unsigned int __user * data,unsigned int size)1212 static int btcvsd_tx_timestamp_get(struct snd_kcontrol *kcontrol,
1213 unsigned int __user *data, unsigned int size)
1214 {
1215 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1216 struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1217 int ret = 0;
1218 struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_tx;
1219
1220 if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
1221 return -EINVAL;
1222
1223 get_tx_time_stamp(bt, &time_buffer_info_tx);
1224
1225 dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
1226 __func__,
1227 time_buffer_info_tx.time_stamp_us,
1228 time_buffer_info_tx.data_count_equi_time);
1229
1230 if (copy_to_user(data, &time_buffer_info_tx,
1231 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
1232 dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
1233 ret = -EFAULT;
1234 }
1235
1236 return ret;
1237 }
1238
1239 static const struct snd_kcontrol_new mtk_btcvsd_snd_controls[] = {
1240 SOC_ENUM_EXT("BTCVSD Band", btcvsd_enum[0],
1241 btcvsd_band_get, btcvsd_band_set),
1242 SOC_SINGLE_BOOL_EXT("BTCVSD Loopback Switch", 0,
1243 btcvsd_loopback_get, btcvsd_loopback_set),
1244 SOC_SINGLE_BOOL_EXT("BTCVSD Tx Mute Switch", 0,
1245 btcvsd_tx_mute_get, btcvsd_tx_mute_set),
1246 SOC_SINGLE_BOOL_EXT("BTCVSD Tx Irq Received Switch", 0,
1247 btcvsd_tx_irq_received_get, NULL),
1248 SOC_SINGLE_BOOL_EXT("BTCVSD Tx Timeout Switch", 0,
1249 btcvsd_tx_timeout_get, NULL),
1250 SOC_SINGLE_BOOL_EXT("BTCVSD Rx Irq Received Switch", 0,
1251 btcvsd_rx_irq_received_get, NULL),
1252 SOC_SINGLE_BOOL_EXT("BTCVSD Rx Timeout Switch", 0,
1253 btcvsd_rx_timeout_get, NULL),
1254 SND_SOC_BYTES_TLV("BTCVSD Rx Timestamp",
1255 sizeof(struct mtk_btcvsd_snd_time_buffer_info),
1256 btcvsd_rx_timestamp_get, NULL),
1257 SND_SOC_BYTES_TLV("BTCVSD Tx Timestamp",
1258 sizeof(struct mtk_btcvsd_snd_time_buffer_info),
1259 btcvsd_tx_timestamp_get, NULL),
1260 };
1261
mtk_btcvsd_snd_component_probe(struct snd_soc_component * component)1262 static int mtk_btcvsd_snd_component_probe(struct snd_soc_component *component)
1263 {
1264 return snd_soc_add_component_controls(component,
1265 mtk_btcvsd_snd_controls,
1266 ARRAY_SIZE(mtk_btcvsd_snd_controls));
1267 }
1268
1269 static const struct snd_soc_component_driver mtk_btcvsd_snd_platform = {
1270 .name = BTCVSD_SND_NAME,
1271 .probe = mtk_btcvsd_snd_component_probe,
1272 .open = mtk_pcm_btcvsd_open,
1273 .close = mtk_pcm_btcvsd_close,
1274 .hw_params = mtk_pcm_btcvsd_hw_params,
1275 .hw_free = mtk_pcm_btcvsd_hw_free,
1276 .prepare = mtk_pcm_btcvsd_prepare,
1277 .trigger = mtk_pcm_btcvsd_trigger,
1278 .pointer = mtk_pcm_btcvsd_pointer,
1279 .copy_user = mtk_pcm_btcvsd_copy,
1280 };
1281
mtk_btcvsd_snd_probe(struct platform_device * pdev)1282 static int mtk_btcvsd_snd_probe(struct platform_device *pdev)
1283 {
1284 int ret = 0;
1285 int irq_id;
1286 u32 offset[5] = {0, 0, 0, 0, 0};
1287 struct mtk_btcvsd_snd *btcvsd;
1288 struct device *dev = &pdev->dev;
1289
1290 /* init btcvsd private data */
1291 btcvsd = devm_kzalloc(dev, sizeof(*btcvsd), GFP_KERNEL);
1292 if (!btcvsd)
1293 return -ENOMEM;
1294 platform_set_drvdata(pdev, btcvsd);
1295 btcvsd->dev = dev;
1296
1297 /* init tx/rx */
1298 btcvsd->rx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->rx), GFP_KERNEL);
1299 if (!btcvsd->rx)
1300 return -ENOMEM;
1301
1302 btcvsd->tx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->tx), GFP_KERNEL);
1303 if (!btcvsd->tx)
1304 return -ENOMEM;
1305
1306 spin_lock_init(&btcvsd->tx_lock);
1307 spin_lock_init(&btcvsd->rx_lock);
1308
1309 init_waitqueue_head(&btcvsd->tx_wait);
1310 init_waitqueue_head(&btcvsd->rx_wait);
1311
1312 mtk_btcvsd_snd_tx_init(btcvsd);
1313 mtk_btcvsd_snd_rx_init(btcvsd);
1314
1315 /* irq */
1316 irq_id = platform_get_irq(pdev, 0);
1317 if (irq_id <= 0)
1318 return irq_id < 0 ? irq_id : -ENXIO;
1319
1320 ret = devm_request_irq(dev, irq_id, mtk_btcvsd_snd_irq_handler,
1321 IRQF_TRIGGER_LOW, "BTCVSD_ISR_Handle",
1322 (void *)btcvsd);
1323 if (ret) {
1324 dev_err(dev, "could not request_irq for BTCVSD_ISR_Handle\n");
1325 return ret;
1326 }
1327
1328 btcvsd->irq_id = irq_id;
1329
1330 /* iomap */
1331 btcvsd->bt_pkv_base = of_iomap(dev->of_node, 0);
1332 if (!btcvsd->bt_pkv_base) {
1333 dev_err(dev, "iomap bt_pkv_base fail\n");
1334 return -EIO;
1335 }
1336
1337 btcvsd->bt_sram_bank2_base = of_iomap(dev->of_node, 1);
1338 if (!btcvsd->bt_sram_bank2_base) {
1339 dev_err(dev, "iomap bt_sram_bank2_base fail\n");
1340 return -EIO;
1341 }
1342
1343 btcvsd->infra = syscon_regmap_lookup_by_phandle(dev->of_node,
1344 "mediatek,infracfg");
1345 if (IS_ERR(btcvsd->infra)) {
1346 dev_err(dev, "cannot find infra controller: %ld\n",
1347 PTR_ERR(btcvsd->infra));
1348 return PTR_ERR(btcvsd->infra);
1349 }
1350
1351 /* get offset */
1352 ret = of_property_read_u32_array(dev->of_node, "mediatek,offset",
1353 offset,
1354 ARRAY_SIZE(offset));
1355 if (ret) {
1356 dev_warn(dev, "%s(), get offset fail, ret %d\n", __func__, ret);
1357 return ret;
1358 }
1359 btcvsd->infra_misc_offset = offset[0];
1360 btcvsd->conn_bt_cvsd_mask = offset[1];
1361 btcvsd->cvsd_mcu_read_offset = offset[2];
1362 btcvsd->cvsd_mcu_write_offset = offset[3];
1363 btcvsd->cvsd_packet_indicator = offset[4];
1364
1365 btcvsd->bt_reg_pkt_r = btcvsd->bt_pkv_base +
1366 btcvsd->cvsd_mcu_read_offset;
1367 btcvsd->bt_reg_pkt_w = btcvsd->bt_pkv_base +
1368 btcvsd->cvsd_mcu_write_offset;
1369 btcvsd->bt_reg_ctl = btcvsd->bt_pkv_base +
1370 btcvsd->cvsd_packet_indicator;
1371
1372 /* init state */
1373 mtk_btcvsd_snd_set_state(btcvsd, btcvsd->tx, BT_SCO_STATE_IDLE);
1374 mtk_btcvsd_snd_set_state(btcvsd, btcvsd->rx, BT_SCO_STATE_IDLE);
1375
1376 return devm_snd_soc_register_component(dev, &mtk_btcvsd_snd_platform,
1377 NULL, 0);
1378 }
1379
mtk_btcvsd_snd_remove(struct platform_device * pdev)1380 static int mtk_btcvsd_snd_remove(struct platform_device *pdev)
1381 {
1382 struct mtk_btcvsd_snd *btcvsd = dev_get_drvdata(&pdev->dev);
1383
1384 iounmap(btcvsd->bt_pkv_base);
1385 iounmap(btcvsd->bt_sram_bank2_base);
1386 return 0;
1387 }
1388
1389 static const struct of_device_id mtk_btcvsd_snd_dt_match[] = {
1390 { .compatible = "mediatek,mtk-btcvsd-snd", },
1391 {},
1392 };
1393 MODULE_DEVICE_TABLE(of, mtk_btcvsd_snd_dt_match);
1394
1395 static struct platform_driver mtk_btcvsd_snd_driver = {
1396 .driver = {
1397 .name = "mtk-btcvsd-snd",
1398 .of_match_table = mtk_btcvsd_snd_dt_match,
1399 },
1400 .probe = mtk_btcvsd_snd_probe,
1401 .remove = mtk_btcvsd_snd_remove,
1402 };
1403
1404 module_platform_driver(mtk_btcvsd_snd_driver);
1405
1406 MODULE_DESCRIPTION("Mediatek ALSA BT SCO CVSD/MSBC Driver");
1407 MODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>");
1408 MODULE_LICENSE("GPL v2");
1409