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,struct iov_iter * buf,size_t count)698 static ssize_t mtk_btcvsd_snd_read(struct mtk_btcvsd_snd *bt,
699 				   struct iov_iter *buf,
700 				   size_t count)
701 {
702 	ssize_t read_size = 0, read_count = 0, cur_read_idx, cont;
703 	unsigned long avail;
704 	unsigned long flags;
705 	unsigned int packet_size = bt->rx->packet_size;
706 
707 	while (count) {
708 		spin_lock_irqsave(&bt->rx_lock, flags);
709 		/* available data in RX packet buffer */
710 		avail = (bt->rx->packet_w - bt->rx->packet_r) * packet_size;
711 
712 		cur_read_idx = (bt->rx->packet_r & SCO_RX_PACKET_MASK) *
713 			       packet_size;
714 		spin_unlock_irqrestore(&bt->rx_lock, flags);
715 
716 		if (!avail) {
717 			int ret = wait_for_bt_irq(bt, bt->rx);
718 
719 			if (ret)
720 				return read_count;
721 
722 			continue;
723 		}
724 
725 		/* count must be multiple of packet_size */
726 		if (count % packet_size != 0 ||
727 		    avail % packet_size != 0) {
728 			dev_warn(bt->dev, "%s(), count %zu or d %lu is not multiple of packet_size %dd\n",
729 				 __func__, count, avail, packet_size);
730 
731 			count -= count % packet_size;
732 			avail -= avail % packet_size;
733 		}
734 
735 		if (count > avail)
736 			read_size = avail;
737 		else
738 			read_size = count;
739 
740 		/* calculate continue space */
741 		cont = bt->rx->buf_size - cur_read_idx;
742 		if (read_size > cont)
743 			read_size = cont;
744 
745 		if (copy_to_iter(bt->rx_packet_buf + cur_read_idx,
746 				 read_size, buf) != read_size) {
747 			dev_warn(bt->dev, "%s(), copy_to_iter fail\n",
748 				 __func__);
749 			return -EFAULT;
750 		}
751 
752 		spin_lock_irqsave(&bt->rx_lock, flags);
753 		bt->rx->packet_r += read_size / packet_size;
754 		spin_unlock_irqrestore(&bt->rx_lock, flags);
755 
756 		read_count += read_size;
757 		count -= read_size;
758 	}
759 
760 	/*
761 	 * save current timestamp & buffer time in times_tamp and
762 	 * buf_data_equivalent_time
763 	 */
764 	bt->rx->time_stamp = sched_clock();
765 	bt->rx->buf_data_equivalent_time =
766 		(unsigned long long)(bt->rx->packet_w - bt->rx->packet_r) *
767 		SCO_RX_PLC_SIZE * 16 * 1000 / 2 / 64;
768 	bt->rx->buf_data_equivalent_time += read_count * SCO_RX_PLC_SIZE *
769 					    16 * 1000 / packet_size / 2 / 64;
770 	/* return equivalent time(us) to data count */
771 	bt->rx->buf_data_equivalent_time *= 1000;
772 
773 	return read_count;
774 }
775 
mtk_btcvsd_snd_write(struct mtk_btcvsd_snd * bt,struct iov_iter * buf,size_t count)776 static ssize_t mtk_btcvsd_snd_write(struct mtk_btcvsd_snd *bt,
777 				    struct iov_iter *buf,
778 				    size_t count)
779 {
780 	int written_size = count, avail, cur_write_idx, write_size, cont;
781 	unsigned long flags;
782 	unsigned int packet_size = bt->tx->packet_size;
783 
784 	/*
785 	 * save current timestamp & buffer time in time_stamp and
786 	 * buf_data_equivalent_time
787 	 */
788 	bt->tx->time_stamp = sched_clock();
789 	bt->tx->buf_data_equivalent_time =
790 		(unsigned long long)(bt->tx->packet_w - bt->tx->packet_r) *
791 		packet_size * 16 * 1000 / 2 / 64;
792 
793 	/* return equivalent time(us) to data count */
794 	bt->tx->buf_data_equivalent_time *= 1000;
795 
796 	while (count) {
797 		spin_lock_irqsave(&bt->tx_lock, flags);
798 		/* free space of TX packet buffer */
799 		avail = bt->tx->buf_size -
800 			(bt->tx->packet_w - bt->tx->packet_r) * packet_size;
801 
802 		cur_write_idx = (bt->tx->packet_w % SCO_TX_PACKER_BUF_NUM) *
803 				packet_size;
804 		spin_unlock_irqrestore(&bt->tx_lock, flags);
805 
806 		if (!avail) {
807 			int ret = wait_for_bt_irq(bt, bt->tx);
808 
809 			if (ret)
810 				return written_size;
811 
812 			continue;
813 		}
814 
815 		/* count must be multiple of bt->tx->packet_size */
816 		if (count % packet_size != 0 ||
817 		    avail % packet_size != 0) {
818 			dev_warn(bt->dev, "%s(), count %zu or avail %d is not multiple of packet_size %d\n",
819 				 __func__, count, avail, packet_size);
820 			count -= count % packet_size;
821 			avail -= avail % packet_size;
822 		}
823 
824 		if (count > avail)
825 			write_size = avail;
826 		else
827 			write_size = count;
828 
829 		/* calculate continue space */
830 		cont = bt->tx->buf_size - cur_write_idx;
831 		if (write_size > cont)
832 			write_size = cont;
833 
834 		if (copy_from_iter(bt->tx_packet_buf + cur_write_idx,
835 				   write_size, buf) != write_size) {
836 			dev_warn(bt->dev, "%s(), copy_from_iter fail\n",
837 				 __func__);
838 			return -EFAULT;
839 		}
840 
841 		spin_lock_irqsave(&bt->tx_lock, flags);
842 		bt->tx->packet_w += write_size / packet_size;
843 		spin_unlock_irqrestore(&bt->tx_lock, flags);
844 		count -= write_size;
845 	}
846 
847 	return written_size;
848 }
849 
get_bt_stream(struct mtk_btcvsd_snd * bt,struct snd_pcm_substream * substream)850 static struct mtk_btcvsd_snd_stream *get_bt_stream
851 	(struct mtk_btcvsd_snd *bt, struct snd_pcm_substream *substream)
852 {
853 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
854 		return bt->tx;
855 	else
856 		return bt->rx;
857 }
858 
859 /* pcm ops */
860 static const struct snd_pcm_hardware mtk_btcvsd_hardware = {
861 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
862 		 SNDRV_PCM_INFO_RESUME),
863 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
864 	.buffer_bytes_max = 24 * 1024,
865 	.period_bytes_max = 24 * 1024,
866 	.periods_min = 2,
867 	.periods_max = 16,
868 	.fifo_size = 0,
869 };
870 
mtk_pcm_btcvsd_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)871 static int mtk_pcm_btcvsd_open(struct snd_soc_component *component,
872 			       struct snd_pcm_substream *substream)
873 {
874 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
875 	int ret;
876 
877 	dev_dbg(bt->dev, "%s(), stream %d, substream %p\n",
878 		__func__, substream->stream, substream);
879 
880 	snd_soc_set_runtime_hwparams(substream, &mtk_btcvsd_hardware);
881 
882 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
883 		ret = mtk_btcvsd_snd_tx_init(bt);
884 		bt->tx->substream = substream;
885 	} else {
886 		ret = mtk_btcvsd_snd_rx_init(bt);
887 		bt->rx->substream = substream;
888 	}
889 
890 	return ret;
891 }
892 
mtk_pcm_btcvsd_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)893 static int mtk_pcm_btcvsd_close(struct snd_soc_component *component,
894 				struct snd_pcm_substream *substream)
895 {
896 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
897 	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
898 
899 	dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);
900 
901 	mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_IDLE);
902 	bt_stream->substream = NULL;
903 	return 0;
904 }
905 
mtk_pcm_btcvsd_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)906 static int mtk_pcm_btcvsd_hw_params(struct snd_soc_component *component,
907 				    struct snd_pcm_substream *substream,
908 				    struct snd_pcm_hw_params *hw_params)
909 {
910 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
911 
912 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
913 	    params_buffer_bytes(hw_params) % bt->tx->packet_size != 0) {
914 		dev_warn(bt->dev, "%s(), error, buffer size %d not valid\n",
915 			 __func__,
916 			 params_buffer_bytes(hw_params));
917 		return -EINVAL;
918 	}
919 
920 	substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
921 	return 0;
922 }
923 
mtk_pcm_btcvsd_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)924 static int mtk_pcm_btcvsd_hw_free(struct snd_soc_component *component,
925 				  struct snd_pcm_substream *substream)
926 {
927 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
928 
929 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
930 		btcvsd_tx_clean_buffer(bt);
931 
932 	return 0;
933 }
934 
mtk_pcm_btcvsd_prepare(struct snd_soc_component * component,struct snd_pcm_substream * substream)935 static int mtk_pcm_btcvsd_prepare(struct snd_soc_component *component,
936 				  struct snd_pcm_substream *substream)
937 {
938 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
939 	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
940 
941 	dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);
942 
943 	mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_RUNNING);
944 	return 0;
945 }
946 
mtk_pcm_btcvsd_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)947 static int mtk_pcm_btcvsd_trigger(struct snd_soc_component *component,
948 				  struct snd_pcm_substream *substream, int cmd)
949 {
950 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
951 	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
952 	int stream = substream->stream;
953 	int hw_packet_ptr;
954 
955 	dev_dbg(bt->dev, "%s(), stream %d, cmd %d\n",
956 		__func__, substream->stream, cmd);
957 
958 	switch (cmd) {
959 	case SNDRV_PCM_TRIGGER_START:
960 	case SNDRV_PCM_TRIGGER_RESUME:
961 		hw_packet_ptr = stream == SNDRV_PCM_STREAM_PLAYBACK ?
962 				bt_stream->packet_r : bt_stream->packet_w;
963 		bt_stream->prev_packet_idx = hw_packet_ptr;
964 		bt_stream->prev_frame = 0;
965 		bt_stream->trigger_start = 1;
966 		return 0;
967 	case SNDRV_PCM_TRIGGER_STOP:
968 	case SNDRV_PCM_TRIGGER_SUSPEND:
969 		bt_stream->trigger_start = 0;
970 		mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_ENDING);
971 		return 0;
972 	default:
973 		return -EINVAL;
974 	}
975 }
976 
mtk_pcm_btcvsd_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)977 static snd_pcm_uframes_t mtk_pcm_btcvsd_pointer(
978 	struct snd_soc_component *component,
979 	struct snd_pcm_substream *substream)
980 {
981 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
982 	struct mtk_btcvsd_snd_stream *bt_stream;
983 	snd_pcm_uframes_t frame = 0;
984 	int byte = 0;
985 	int hw_packet_ptr;
986 	int packet_diff;
987 	spinlock_t *lock;	/* spinlock for bt stream control */
988 	unsigned long flags;
989 
990 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
991 		lock = &bt->tx_lock;
992 		bt_stream = bt->tx;
993 	} else {
994 		lock = &bt->rx_lock;
995 		bt_stream = bt->rx;
996 	}
997 
998 	spin_lock_irqsave(lock, flags);
999 	hw_packet_ptr = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1000 			bt->tx->packet_r : bt->rx->packet_w;
1001 
1002 	/* get packet diff from last time */
1003 	if (hw_packet_ptr >= bt_stream->prev_packet_idx) {
1004 		packet_diff = hw_packet_ptr - bt_stream->prev_packet_idx;
1005 	} else {
1006 		/* integer overflow */
1007 		packet_diff = (INT_MAX - bt_stream->prev_packet_idx) +
1008 			      (hw_packet_ptr - INT_MIN) + 1;
1009 	}
1010 	bt_stream->prev_packet_idx = hw_packet_ptr;
1011 
1012 	/* increased bytes */
1013 	byte = packet_diff * bt_stream->packet_size;
1014 
1015 	frame = btcvsd_bytes_to_frame(substream, byte);
1016 	frame += bt_stream->prev_frame;
1017 	frame %= substream->runtime->buffer_size;
1018 
1019 	bt_stream->prev_frame = frame;
1020 
1021 	spin_unlock_irqrestore(lock, flags);
1022 
1023 	return frame;
1024 }
1025 
mtk_pcm_btcvsd_copy(struct snd_soc_component * component,struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * buf,unsigned long count)1026 static int mtk_pcm_btcvsd_copy(struct snd_soc_component *component,
1027 			       struct snd_pcm_substream *substream,
1028 			       int channel, unsigned long pos,
1029 			       struct iov_iter *buf, unsigned long count)
1030 {
1031 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
1032 
1033 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1034 		return mtk_btcvsd_snd_write(bt, buf, count);
1035 	else
1036 		return mtk_btcvsd_snd_read(bt, buf, count);
1037 }
1038 
1039 /* kcontrol */
1040 static const char *const btsco_band_str[] = {"NB", "WB"};
1041 
1042 static const struct soc_enum btcvsd_enum[] = {
1043 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(btsco_band_str), btsco_band_str),
1044 };
1045 
btcvsd_band_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1046 static int btcvsd_band_get(struct snd_kcontrol *kcontrol,
1047 			   struct snd_ctl_elem_value *ucontrol)
1048 {
1049 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1050 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1051 
1052 	ucontrol->value.integer.value[0] = bt->band;
1053 	return 0;
1054 }
1055 
btcvsd_band_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1056 static int btcvsd_band_set(struct snd_kcontrol *kcontrol,
1057 			   struct snd_ctl_elem_value *ucontrol)
1058 {
1059 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1060 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1061 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1062 
1063 	if (ucontrol->value.enumerated.item[0] >= e->items)
1064 		return -EINVAL;
1065 
1066 	bt->band = ucontrol->value.integer.value[0];
1067 	dev_dbg(bt->dev, "%s(), band %d\n", __func__, bt->band);
1068 	return 0;
1069 }
1070 
btcvsd_loopback_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1071 static int btcvsd_loopback_get(struct snd_kcontrol *kcontrol,
1072 			       struct snd_ctl_elem_value *ucontrol)
1073 {
1074 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1075 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1076 	bool lpbk_en = bt->tx->state == BT_SCO_STATE_LOOPBACK;
1077 
1078 	ucontrol->value.integer.value[0] = lpbk_en;
1079 	return 0;
1080 }
1081 
btcvsd_loopback_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1082 static int btcvsd_loopback_set(struct snd_kcontrol *kcontrol,
1083 			       struct snd_ctl_elem_value *ucontrol)
1084 {
1085 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1086 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1087 
1088 	if (ucontrol->value.integer.value[0]) {
1089 		mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_LOOPBACK);
1090 		mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_LOOPBACK);
1091 	} else {
1092 		mtk_btcvsd_snd_set_state(bt, bt->tx, BT_SCO_STATE_RUNNING);
1093 		mtk_btcvsd_snd_set_state(bt, bt->rx, BT_SCO_STATE_RUNNING);
1094 	}
1095 	return 0;
1096 }
1097 
btcvsd_tx_mute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1098 static int btcvsd_tx_mute_get(struct snd_kcontrol *kcontrol,
1099 			      struct snd_ctl_elem_value *ucontrol)
1100 {
1101 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1102 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1103 
1104 	if (!bt->tx) {
1105 		ucontrol->value.integer.value[0] = 0;
1106 		return 0;
1107 	}
1108 
1109 	ucontrol->value.integer.value[0] = bt->tx->mute;
1110 	return 0;
1111 }
1112 
btcvsd_tx_mute_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1113 static int btcvsd_tx_mute_set(struct snd_kcontrol *kcontrol,
1114 			      struct snd_ctl_elem_value *ucontrol)
1115 {
1116 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1117 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1118 
1119 	if (!bt->tx)
1120 		return 0;
1121 
1122 	bt->tx->mute = ucontrol->value.integer.value[0];
1123 	return 0;
1124 }
1125 
btcvsd_rx_irq_received_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1126 static int btcvsd_rx_irq_received_get(struct snd_kcontrol *kcontrol,
1127 				      struct snd_ctl_elem_value *ucontrol)
1128 {
1129 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1130 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1131 
1132 	if (!bt->rx)
1133 		return 0;
1134 
1135 	ucontrol->value.integer.value[0] = bt->rx->rw_cnt ? 1 : 0;
1136 	return 0;
1137 }
1138 
btcvsd_rx_timeout_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1139 static int btcvsd_rx_timeout_get(struct snd_kcontrol *kcontrol,
1140 				 struct snd_ctl_elem_value *ucontrol)
1141 {
1142 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1143 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1144 
1145 	if (!bt->rx)
1146 		return 0;
1147 
1148 	ucontrol->value.integer.value[0] = bt->rx->timeout;
1149 	bt->rx->timeout = 0;
1150 	return 0;
1151 }
1152 
btcvsd_rx_timestamp_get(struct snd_kcontrol * kcontrol,unsigned int __user * data,unsigned int size)1153 static int btcvsd_rx_timestamp_get(struct snd_kcontrol *kcontrol,
1154 				   unsigned int __user *data, unsigned int size)
1155 {
1156 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1157 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1158 	int ret = 0;
1159 	struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_rx;
1160 
1161 	if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
1162 		return -EINVAL;
1163 
1164 	get_rx_time_stamp(bt, &time_buffer_info_rx);
1165 
1166 	dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
1167 		__func__,
1168 		time_buffer_info_rx.time_stamp_us,
1169 		time_buffer_info_rx.data_count_equi_time);
1170 
1171 	if (copy_to_user(data, &time_buffer_info_rx,
1172 			 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
1173 		dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
1174 		ret = -EFAULT;
1175 	}
1176 
1177 	return ret;
1178 }
1179 
btcvsd_tx_irq_received_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1180 static int btcvsd_tx_irq_received_get(struct snd_kcontrol *kcontrol,
1181 				      struct snd_ctl_elem_value *ucontrol)
1182 {
1183 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1184 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1185 
1186 	if (!bt->tx)
1187 		return 0;
1188 
1189 	ucontrol->value.integer.value[0] = bt->tx->rw_cnt ? 1 : 0;
1190 	return 0;
1191 }
1192 
btcvsd_tx_timeout_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1193 static int btcvsd_tx_timeout_get(struct snd_kcontrol *kcontrol,
1194 				 struct snd_ctl_elem_value *ucontrol)
1195 {
1196 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1197 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1198 
1199 	ucontrol->value.integer.value[0] = bt->tx->timeout;
1200 	return 0;
1201 }
1202 
btcvsd_tx_timestamp_get(struct snd_kcontrol * kcontrol,unsigned int __user * data,unsigned int size)1203 static int btcvsd_tx_timestamp_get(struct snd_kcontrol *kcontrol,
1204 				   unsigned int __user *data, unsigned int size)
1205 {
1206 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
1207 	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
1208 	int ret = 0;
1209 	struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_tx;
1210 
1211 	if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
1212 		return -EINVAL;
1213 
1214 	get_tx_time_stamp(bt, &time_buffer_info_tx);
1215 
1216 	dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
1217 		__func__,
1218 		time_buffer_info_tx.time_stamp_us,
1219 		time_buffer_info_tx.data_count_equi_time);
1220 
1221 	if (copy_to_user(data, &time_buffer_info_tx,
1222 			 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
1223 		dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
1224 		ret = -EFAULT;
1225 	}
1226 
1227 	return ret;
1228 }
1229 
1230 static const struct snd_kcontrol_new mtk_btcvsd_snd_controls[] = {
1231 	SOC_ENUM_EXT("BTCVSD Band", btcvsd_enum[0],
1232 		     btcvsd_band_get, btcvsd_band_set),
1233 	SOC_SINGLE_BOOL_EXT("BTCVSD Loopback Switch", 0,
1234 			    btcvsd_loopback_get, btcvsd_loopback_set),
1235 	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Mute Switch", 0,
1236 			    btcvsd_tx_mute_get, btcvsd_tx_mute_set),
1237 	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Irq Received Switch", 0,
1238 			    btcvsd_tx_irq_received_get, NULL),
1239 	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Timeout Switch", 0,
1240 			    btcvsd_tx_timeout_get, NULL),
1241 	SOC_SINGLE_BOOL_EXT("BTCVSD Rx Irq Received Switch", 0,
1242 			    btcvsd_rx_irq_received_get, NULL),
1243 	SOC_SINGLE_BOOL_EXT("BTCVSD Rx Timeout Switch", 0,
1244 			    btcvsd_rx_timeout_get, NULL),
1245 	SND_SOC_BYTES_TLV("BTCVSD Rx Timestamp",
1246 			  sizeof(struct mtk_btcvsd_snd_time_buffer_info),
1247 			  btcvsd_rx_timestamp_get, NULL),
1248 	SND_SOC_BYTES_TLV("BTCVSD Tx Timestamp",
1249 			  sizeof(struct mtk_btcvsd_snd_time_buffer_info),
1250 			  btcvsd_tx_timestamp_get, NULL),
1251 };
1252 
mtk_btcvsd_snd_component_probe(struct snd_soc_component * component)1253 static int mtk_btcvsd_snd_component_probe(struct snd_soc_component *component)
1254 {
1255 	return snd_soc_add_component_controls(component,
1256 		mtk_btcvsd_snd_controls,
1257 		ARRAY_SIZE(mtk_btcvsd_snd_controls));
1258 }
1259 
1260 static const struct snd_soc_component_driver mtk_btcvsd_snd_platform = {
1261 	.name		= BTCVSD_SND_NAME,
1262 	.probe		= mtk_btcvsd_snd_component_probe,
1263 	.open		= mtk_pcm_btcvsd_open,
1264 	.close		= mtk_pcm_btcvsd_close,
1265 	.hw_params	= mtk_pcm_btcvsd_hw_params,
1266 	.hw_free	= mtk_pcm_btcvsd_hw_free,
1267 	.prepare	= mtk_pcm_btcvsd_prepare,
1268 	.trigger	= mtk_pcm_btcvsd_trigger,
1269 	.pointer	= mtk_pcm_btcvsd_pointer,
1270 	.copy		= mtk_pcm_btcvsd_copy,
1271 };
1272 
mtk_btcvsd_snd_probe(struct platform_device * pdev)1273 static int mtk_btcvsd_snd_probe(struct platform_device *pdev)
1274 {
1275 	int ret;
1276 	int irq_id;
1277 	u32 offset[5] = {0, 0, 0, 0, 0};
1278 	struct mtk_btcvsd_snd *btcvsd;
1279 	struct device *dev = &pdev->dev;
1280 
1281 	/* init btcvsd private data */
1282 	btcvsd = devm_kzalloc(dev, sizeof(*btcvsd), GFP_KERNEL);
1283 	if (!btcvsd)
1284 		return -ENOMEM;
1285 	platform_set_drvdata(pdev, btcvsd);
1286 	btcvsd->dev = dev;
1287 
1288 	/* init tx/rx */
1289 	btcvsd->rx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->rx), GFP_KERNEL);
1290 	if (!btcvsd->rx)
1291 		return -ENOMEM;
1292 
1293 	btcvsd->tx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->tx), GFP_KERNEL);
1294 	if (!btcvsd->tx)
1295 		return -ENOMEM;
1296 
1297 	spin_lock_init(&btcvsd->tx_lock);
1298 	spin_lock_init(&btcvsd->rx_lock);
1299 
1300 	init_waitqueue_head(&btcvsd->tx_wait);
1301 	init_waitqueue_head(&btcvsd->rx_wait);
1302 
1303 	mtk_btcvsd_snd_tx_init(btcvsd);
1304 	mtk_btcvsd_snd_rx_init(btcvsd);
1305 
1306 	/* irq */
1307 	irq_id = platform_get_irq(pdev, 0);
1308 	if (irq_id <= 0)
1309 		return irq_id < 0 ? irq_id : -ENXIO;
1310 
1311 	ret = devm_request_irq(dev, irq_id, mtk_btcvsd_snd_irq_handler,
1312 			       IRQF_TRIGGER_LOW, "BTCVSD_ISR_Handle",
1313 			       (void *)btcvsd);
1314 	if (ret) {
1315 		dev_err(dev, "could not request_irq for BTCVSD_ISR_Handle\n");
1316 		return ret;
1317 	}
1318 
1319 	btcvsd->irq_id = irq_id;
1320 
1321 	/* iomap */
1322 	btcvsd->bt_pkv_base = of_iomap(dev->of_node, 0);
1323 	if (!btcvsd->bt_pkv_base) {
1324 		dev_err(dev, "iomap bt_pkv_base fail\n");
1325 		return -EIO;
1326 	}
1327 
1328 	btcvsd->bt_sram_bank2_base = of_iomap(dev->of_node, 1);
1329 	if (!btcvsd->bt_sram_bank2_base) {
1330 		dev_err(dev, "iomap bt_sram_bank2_base fail\n");
1331 		ret = -EIO;
1332 		goto unmap_pkv_err;
1333 	}
1334 
1335 	btcvsd->infra = syscon_regmap_lookup_by_phandle(dev->of_node,
1336 							"mediatek,infracfg");
1337 	if (IS_ERR(btcvsd->infra)) {
1338 		dev_err(dev, "cannot find infra controller: %ld\n",
1339 			PTR_ERR(btcvsd->infra));
1340 		ret = PTR_ERR(btcvsd->infra);
1341 		goto unmap_bank2_err;
1342 	}
1343 
1344 	/* get offset */
1345 	ret = of_property_read_u32_array(dev->of_node, "mediatek,offset",
1346 					 offset,
1347 					 ARRAY_SIZE(offset));
1348 	if (ret) {
1349 		dev_warn(dev, "%s(), get offset fail, ret %d\n", __func__, ret);
1350 		goto unmap_bank2_err;
1351 	}
1352 	btcvsd->infra_misc_offset = offset[0];
1353 	btcvsd->conn_bt_cvsd_mask = offset[1];
1354 	btcvsd->cvsd_mcu_read_offset = offset[2];
1355 	btcvsd->cvsd_mcu_write_offset = offset[3];
1356 	btcvsd->cvsd_packet_indicator = offset[4];
1357 
1358 	btcvsd->bt_reg_pkt_r = btcvsd->bt_pkv_base +
1359 			       btcvsd->cvsd_mcu_read_offset;
1360 	btcvsd->bt_reg_pkt_w = btcvsd->bt_pkv_base +
1361 			       btcvsd->cvsd_mcu_write_offset;
1362 	btcvsd->bt_reg_ctl = btcvsd->bt_pkv_base +
1363 			     btcvsd->cvsd_packet_indicator;
1364 
1365 	/* init state */
1366 	mtk_btcvsd_snd_set_state(btcvsd, btcvsd->tx, BT_SCO_STATE_IDLE);
1367 	mtk_btcvsd_snd_set_state(btcvsd, btcvsd->rx, BT_SCO_STATE_IDLE);
1368 
1369 	ret = devm_snd_soc_register_component(dev, &mtk_btcvsd_snd_platform,
1370 					      NULL, 0);
1371 	if (ret)
1372 		goto unmap_bank2_err;
1373 
1374 	return 0;
1375 
1376 unmap_bank2_err:
1377 	iounmap(btcvsd->bt_sram_bank2_base);
1378 unmap_pkv_err:
1379 	iounmap(btcvsd->bt_pkv_base);
1380 	return ret;
1381 }
1382 
mtk_btcvsd_snd_remove(struct platform_device * pdev)1383 static void mtk_btcvsd_snd_remove(struct platform_device *pdev)
1384 {
1385 	struct mtk_btcvsd_snd *btcvsd = dev_get_drvdata(&pdev->dev);
1386 
1387 	iounmap(btcvsd->bt_pkv_base);
1388 	iounmap(btcvsd->bt_sram_bank2_base);
1389 }
1390 
1391 static const struct of_device_id mtk_btcvsd_snd_dt_match[] = {
1392 	{ .compatible = "mediatek,mtk-btcvsd-snd", },
1393 	{},
1394 };
1395 MODULE_DEVICE_TABLE(of, mtk_btcvsd_snd_dt_match);
1396 
1397 static struct platform_driver mtk_btcvsd_snd_driver = {
1398 	.driver = {
1399 		.name = "mtk-btcvsd-snd",
1400 		.of_match_table = mtk_btcvsd_snd_dt_match,
1401 	},
1402 	.probe = mtk_btcvsd_snd_probe,
1403 	.remove_new = mtk_btcvsd_snd_remove,
1404 };
1405 
1406 module_platform_driver(mtk_btcvsd_snd_driver);
1407 
1408 MODULE_DESCRIPTION("Mediatek ALSA BT SCO CVSD/MSBC Driver");
1409 MODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>");
1410 MODULE_LICENSE("GPL v2");
1411