blob: 4c6986a536eb9b0923bcb41aff2c679d8535c512 [file] [log] [blame]
Satish Kodishalaf542e4b2018-03-05 17:53:10 +05301/* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/of_gpio.h>
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/debugfs.h>
19#include <linux/slimbus/slimbus.h>
20#include <linux/ratelimit.h>
21#include <linux/slab.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26#include <sound/tlv.h>
27#include <btfm_slim.h>
28
Satish Kodishala8e4e9622017-05-18 19:19:30 +053029static int bt_soc_enable_status;
Satish Kodishalaf542e4b2018-03-05 17:53:10 +053030int btfm_feedback_ch_setting;
Satish Kodishala8e4e9622017-05-18 19:19:30 +053031
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -080032static int btfm_slim_codec_write(struct snd_soc_codec *codec, unsigned int reg,
33 unsigned int value)
34{
35 return 0;
36}
37
38static unsigned int btfm_slim_codec_read(struct snd_soc_codec *codec,
39 unsigned int reg)
40{
41 return 0;
42}
43
Satish Kodishala8e4e9622017-05-18 19:19:30 +053044static int bt_soc_status_get(struct snd_kcontrol *kcontrol,
45 struct snd_ctl_elem_value *ucontrol)
46{
47 ucontrol->value.integer.value[0] = bt_soc_enable_status;
48 return 1;
49}
50
51static int bt_soc_status_put(struct snd_kcontrol *kcontrol,
52 struct snd_ctl_elem_value *ucontrol)
53{
54 return 1;
55}
56
Satish Kodishalaf542e4b2018-03-05 17:53:10 +053057static int btfm_get_feedback_ch_setting(struct snd_kcontrol *kcontrol,
58 struct snd_ctl_elem_value *ucontrol)
59{
60 ucontrol->value.integer.value[0] = btfm_feedback_ch_setting;
61 return 1;
62}
63
64static int btfm_put_feedback_ch_setting(struct snd_kcontrol *kcontrol,
65 struct snd_ctl_elem_value *ucontrol)
66{
67 btfm_feedback_ch_setting = ucontrol->value.integer.value[0];
68 return 1;
69}
70
Satish Kodishala8e4e9622017-05-18 19:19:30 +053071static const struct snd_kcontrol_new status_controls[] = {
72 SOC_SINGLE_EXT("BT SOC status", 0, 0, 1, 0,
73 bt_soc_status_get,
Satish Kodishalaf542e4b2018-03-05 17:53:10 +053074 bt_soc_status_put),
75 SOC_SINGLE_EXT("BT set feedback channel", 0, 0, 1, 0,
76 btfm_get_feedback_ch_setting,
77 btfm_put_feedback_ch_setting)
Satish Kodishala8e4e9622017-05-18 19:19:30 +053078
79};
80
81
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -080082static int btfm_slim_codec_probe(struct snd_soc_codec *codec)
83{
Satish Kodishala8e4e9622017-05-18 19:19:30 +053084 snd_soc_add_codec_controls(codec, status_controls,
85 ARRAY_SIZE(status_controls));
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -080086 return 0;
87}
88
89static int btfm_slim_codec_remove(struct snd_soc_codec *codec)
90{
91 return 0;
92}
93
94static int btfm_slim_dai_startup(struct snd_pcm_substream *substream,
95 struct snd_soc_dai *dai)
96{
97 int ret;
98 struct btfmslim *btfmslim = dai->dev->platform_data;
99
Sungjun Park9b33e942017-02-23 12:50:03 -0800100 BTFMSLIM_DBG("substream = %s stream = %d dai->name = %s",
Rupesh Tatiya3815c792017-02-17 12:58:01 +0530101 substream->name, substream->stream, dai->name);
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800102 ret = btfm_slim_hw_init(btfmslim);
103 return ret;
104}
105
106static void btfm_slim_dai_shutdown(struct snd_pcm_substream *substream,
107 struct snd_soc_dai *dai)
108{
Sungjun Park9b33e942017-02-23 12:50:03 -0800109 int i;
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800110 struct btfmslim *btfmslim = dai->dev->platform_data;
Sungjun Park9b33e942017-02-23 12:50:03 -0800111 struct btfmslim_ch *ch;
112 uint8_t rxport, grp = false, nchan = 1;
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800113
Sungjun Park9b33e942017-02-23 12:50:03 -0800114 BTFMSLIM_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
115 dai->id, dai->rate);
116
117 switch (dai->id) {
118 case BTFM_FM_SLIM_TX:
119 grp = true; nchan = 2;
120 ch = btfmslim->tx_chs;
121 rxport = 0;
122 break;
123 case BTFM_BT_SCO_SLIM_TX:
124 ch = btfmslim->tx_chs;
125 rxport = 0;
126 break;
127 case BTFM_BT_SCO_A2DP_SLIM_RX:
128 case BTFM_BT_SPLIT_A2DP_SLIM_RX:
129 ch = btfmslim->rx_chs;
130 rxport = 1;
131 break;
132 case BTFM_SLIM_NUM_CODEC_DAIS:
133 default:
134 BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
135 return;
136 }
137
138 /* Search for dai->id matched port handler */
139 for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
140 (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
141 (ch->id != dai->id); ch++, i++)
142 ;
143
144 if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
145 (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
146 BTFMSLIM_ERR("ch is invalid!!");
147 return;
148 }
149
150 btfm_slim_disable_ch(btfmslim, ch, rxport, grp, nchan);
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800151 btfm_slim_hw_deinit(btfmslim);
152}
153
154static int btfm_slim_dai_hw_params(struct snd_pcm_substream *substream,
155 struct snd_pcm_hw_params *params,
156 struct snd_soc_dai *dai)
157{
Sungjun Park9b33e942017-02-23 12:50:03 -0800158 BTFMSLIM_DBG("dai->name = %s DAI-ID %x rate %d num_ch %d",
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800159 dai->name, dai->id, params_rate(params),
160 params_channels(params));
161
162 return 0;
163}
164
Sungjun Park9b33e942017-02-23 12:50:03 -0800165static int btfm_slim_dai_prepare(struct snd_pcm_substream *substream,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800166 struct snd_soc_dai *dai)
167{
168 int i, ret = -EINVAL;
169 struct btfmslim *btfmslim = dai->dev->platform_data;
170 struct btfmslim_ch *ch;
171 uint8_t rxport, grp = false, nchan = 1;
Satish Kodishala8e4e9622017-05-18 19:19:30 +0530172 bt_soc_enable_status = 0;
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800173
Sungjun Park9b33e942017-02-23 12:50:03 -0800174 BTFMSLIM_DBG("dai->name: %s, dai->id: %d, dai->rate: %d", dai->name,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800175 dai->id, dai->rate);
176
Satish Kodishala93194f82017-05-18 20:11:20 +0530177 /* save sample rate */
178 btfmslim->sample_rate = dai->rate;
179
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800180 switch (dai->id) {
181 case BTFM_FM_SLIM_TX:
182 grp = true; nchan = 2;
183 ch = btfmslim->tx_chs;
184 rxport = 0;
185 break;
186 case BTFM_BT_SCO_SLIM_TX:
187 ch = btfmslim->tx_chs;
188 rxport = 0;
189 break;
190 case BTFM_BT_SCO_A2DP_SLIM_RX:
191 case BTFM_BT_SPLIT_A2DP_SLIM_RX:
192 ch = btfmslim->rx_chs;
193 rxport = 1;
194 break;
195 case BTFM_SLIM_NUM_CODEC_DAIS:
196 default:
197 BTFMSLIM_ERR("dai->id is invalid:%d", dai->id);
198 return ret;
199 }
200
201 /* Search for dai->id matched port handler */
202 for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) &&
203 (ch->id != BTFM_SLIM_NUM_CODEC_DAIS) &&
204 (ch->id != dai->id); ch++, i++)
205 ;
206
207 if ((ch->port == BTFM_SLIM_PGD_PORT_LAST) ||
208 (ch->id == BTFM_SLIM_NUM_CODEC_DAIS)) {
209 BTFMSLIM_ERR("ch is invalid!!");
210 return ret;
211 }
212
213 ret = btfm_slim_enable_ch(btfmslim, ch, rxport, dai->rate, grp, nchan);
Satish Kodishala8e4e9622017-05-18 19:19:30 +0530214
215 /* save the enable channel status */
216 if (ret == 0)
217 bt_soc_enable_status = 1;
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800218 return ret;
219}
220
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800221/* This function will be called once during boot up */
222static int btfm_slim_dai_set_channel_map(struct snd_soc_dai *dai,
223 unsigned int tx_num, unsigned int *tx_slot,
224 unsigned int rx_num, unsigned int *rx_slot)
225{
226 int ret = -EINVAL, i;
227 struct btfmslim *btfmslim = dai->dev->platform_data;
228 struct btfmslim_ch *rx_chs;
229 struct btfmslim_ch *tx_chs;
230
231 BTFMSLIM_DBG("");
232
233 if (!btfmslim)
234 return ret;
235
236 rx_chs = btfmslim->rx_chs;
237 tx_chs = btfmslim->tx_chs;
238
239 if (!rx_chs || !tx_chs)
240 return ret;
241
242 BTFMSLIM_DBG("Rx: id\tname\tport\thdl\tch\tch_hdl");
243 for (i = 0; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < rx_num);
244 i++, rx_chs++) {
245 /* Set Rx Channel number from machine driver and
246 * get channel handler from slimbus driver
247 */
248 rx_chs->ch = *(uint8_t *)(rx_slot + i);
249 ret = slim_query_ch(btfmslim->slim_pgd, rx_chs->ch,
250 &rx_chs->ch_hdl);
251 if (ret < 0) {
252 BTFMSLIM_ERR("slim_query_ch failure ch#%d - ret[%d]",
253 rx_chs->ch, ret);
254 goto error;
255 }
256 BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", rx_chs->id,
257 rx_chs->name, rx_chs->port, rx_chs->port_hdl,
258 rx_chs->ch, rx_chs->ch_hdl);
259 }
260
261 BTFMSLIM_DBG("Tx: id\tname\tport\thdl\tch\tch_hdl");
262 for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) && (i < tx_num);
263 i++, tx_chs++) {
264 /* Set Tx Channel number from machine driver and
265 * get channel handler from slimbus driver
266 */
267 tx_chs->ch = *(uint8_t *)(tx_slot + i);
268 ret = slim_query_ch(btfmslim->slim_pgd, tx_chs->ch,
269 &tx_chs->ch_hdl);
270 if (ret < 0) {
271 BTFMSLIM_ERR("slim_query_ch failure ch#%d - ret[%d]",
272 tx_chs->ch, ret);
273 goto error;
274 }
275 BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", tx_chs->id,
276 tx_chs->name, tx_chs->port, tx_chs->port_hdl,
277 tx_chs->ch, tx_chs->ch_hdl);
278 }
279
280error:
281 return ret;
282}
283
284static int btfm_slim_dai_get_channel_map(struct snd_soc_dai *dai,
285 unsigned int *tx_num, unsigned int *tx_slot,
286 unsigned int *rx_num, unsigned int *rx_slot)
287{
288 int i, ret = -EINVAL, *slot = NULL, j = 0, num = 1;
289 struct btfmslim *btfmslim = dai->dev->platform_data;
290 struct btfmslim_ch *ch = NULL;
291
292 if (!btfmslim)
293 return ret;
294
295 switch (dai->id) {
296 case BTFM_FM_SLIM_TX:
297 num = 2;
298 case BTFM_BT_SCO_SLIM_TX:
299 if (!tx_slot || !tx_num) {
300 BTFMSLIM_ERR("Invalid tx_slot %p or tx_num %p",
301 tx_slot, tx_num);
302 return -EINVAL;
303 }
304 ch = btfmslim->tx_chs;
305 if (!ch)
306 return -EINVAL;
307 slot = tx_slot;
308 *rx_slot = 0;
309 *tx_num = num;
310 *rx_num = 0;
311 break;
312 case BTFM_BT_SCO_A2DP_SLIM_RX:
313 case BTFM_BT_SPLIT_A2DP_SLIM_RX:
314 if (!rx_slot || !rx_num) {
315 BTFMSLIM_ERR("Invalid rx_slot %p or rx_num %p",
316 rx_slot, rx_num);
317 return -EINVAL;
318 }
319 ch = btfmslim->rx_chs;
320 if (!ch)
321 return -EINVAL;
322 slot = rx_slot;
323 *tx_slot = 0;
324 *tx_num = 0;
325 *rx_num = num;
326 break;
Rupesh Tatiya03a5d4f2017-05-08 10:36:19 +0530327 default:
328 BTFMSLIM_ERR("Unsupported DAI %d", dai->id);
329 return -EINVAL;
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800330 }
331
332 do {
333 if (!ch)
334 return -EINVAL;
335 for (i = 0; (i < BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id !=
336 BTFM_SLIM_NUM_CODEC_DAIS) && (ch->id != dai->id);
337 ch++, i++)
338 ;
339
340 if (ch->id == BTFM_SLIM_NUM_CODEC_DAIS ||
341 i == BTFM_SLIM_NUM_CODEC_DAIS) {
342 BTFMSLIM_ERR(
343 "No channel has been allocated for dai (%d)",
344 dai->id);
345 return -EINVAL;
346 }
347 if (!slot)
348 return -EINVAL;
349 *(slot + j) = ch->ch;
350 BTFMSLIM_DBG("id:%d, port:%d, ch:%d, slot: %d", ch->id,
351 ch->port, ch->ch, *(slot + j));
352
353 /* In case it has mulitiple channels */
354 if (++j < num)
355 ch++;
356 } while (j < num);
357
358 return 0;
359}
360
361static struct snd_soc_dai_ops btfmslim_dai_ops = {
362 .startup = btfm_slim_dai_startup,
363 .shutdown = btfm_slim_dai_shutdown,
364 .hw_params = btfm_slim_dai_hw_params,
365 .prepare = btfm_slim_dai_prepare,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800366 .set_channel_map = btfm_slim_dai_set_channel_map,
367 .get_channel_map = btfm_slim_dai_get_channel_map,
368};
369
370static struct snd_soc_dai_driver btfmslim_dai[] = {
371 { /* FM Audio data multiple channel : FM -> qdsp */
372 .name = "btfm_fm_slim_tx",
373 .id = BTFM_FM_SLIM_TX,
374 .capture = {
375 .stream_name = "FM TX Capture",
376 .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
377 .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
378 .rate_max = 48000,
379 .rate_min = 48000,
380 .channels_min = 1,
381 .channels_max = 2,
382 },
383 .ops = &btfmslim_dai_ops,
384 },
385 { /* Bluetooth SCO voice uplink: bt -> modem */
386 .name = "btfm_bt_sco_slim_tx",
387 .id = BTFM_BT_SCO_SLIM_TX,
388 .capture = {
389 .stream_name = "SCO TX Capture",
390 /* 8 KHz or 16 KHz */
Satish Kodishalaf542e4b2018-03-05 17:53:10 +0530391 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
392 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
393 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800394 .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
Satish Kodishalaf542e4b2018-03-05 17:53:10 +0530395 .rate_max = 96000,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800396 .rate_min = 8000,
397 .channels_min = 1,
398 .channels_max = 1,
399 },
400 .ops = &btfmslim_dai_ops,
401 },
402 { /* Bluetooth SCO voice downlink: modem -> bt or A2DP Playback */
403 .name = "btfm_bt_sco_a2dp_slim_rx",
404 .id = BTFM_BT_SCO_A2DP_SLIM_RX,
405 .playback = {
406 .stream_name = "SCO A2DP RX Playback",
Sungjun Parkea258e42017-10-18 19:08:25 -0700407 /* 8/16/44.1/48/88.2/96 Khz */
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800408 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000
Sungjun Parkea258e42017-10-18 19:08:25 -0700409 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000
410 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800411 .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
Sungjun Parkea258e42017-10-18 19:08:25 -0700412 .rate_max = 96000,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800413 .rate_min = 8000,
414 .channels_min = 1,
415 .channels_max = 1,
416 },
417 .ops = &btfmslim_dai_ops,
418 },
419 { /* Bluetooth Split A2DP data: qdsp -> bt */
420 .name = "btfm_bt_split_a2dp_slim_rx",
421 .id = BTFM_BT_SPLIT_A2DP_SLIM_RX,
422 .playback = {
423 .stream_name = "SPLIT A2DP Playback",
424 .rates = SNDRV_PCM_RATE_48000, /* 48 KHz */
425 .formats = SNDRV_PCM_FMTBIT_S16_LE, /* 16 bits */
426 .rate_max = 48000,
427 .rate_min = 48000,
428 .channels_min = 1,
429 .channels_max = 1,
430 },
431 .ops = &btfmslim_dai_ops,
432 },
433};
434
435static struct snd_soc_codec_driver btfmslim_codec = {
436 .probe = btfm_slim_codec_probe,
437 .remove = btfm_slim_codec_remove,
Rupesh Tatiya3815c792017-02-17 12:58:01 +0530438 .read = btfm_slim_codec_read,
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800439 .write = btfm_slim_codec_write,
440};
441
442int btfm_slim_register_codec(struct device *dev)
443{
444 int ret = 0;
445
446 BTFMSLIM_DBG("");
447 /* Register Codec driver */
448 ret = snd_soc_register_codec(dev, &btfmslim_codec,
449 btfmslim_dai, ARRAY_SIZE(btfmslim_dai));
450
451 if (ret)
452 BTFMSLIM_ERR("failed to register codec (%d)", ret);
453
454 return ret;
455}
456
Satish kumar sugasibcf528a2017-07-27 13:37:22 -0700457void btfm_slim_unregister_codec(struct device *dev)
458{
459 BTFMSLIM_DBG("");
460 /* Unregister Codec driver */
461 snd_soc_unregister_codec(dev);
462}
463
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800464MODULE_DESCRIPTION("BTFM Slimbus Codec driver");
465MODULE_LICENSE("GPL v2");