blob: 8f0e6325f1c795fcfc4042fa0047a6da3ad8b55f [file] [log] [blame]
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -08001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/of_gpio.h>
16#include <linux/delay.h>
17#include <linux/gpio.h>
18#include <linux/debugfs.h>
19#include <linux/ratelimit.h>
20#include <linux/slab.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
25#include <sound/tlv.h>
26#include <btfm_slim.h>
27#include <btfm_slim_wcn3990.h>
28#include <linux/bluetooth-power.h>
29
30int btfm_slim_write(struct btfmslim *btfmslim,
31 uint16_t reg, int bytes, void *src, uint8_t pgd)
32{
33 int ret, i;
34 struct slim_ele_access msg;
35 int slim_write_tries = SLIM_SLAVE_RW_MAX_TRIES;
36
37 BTFMSLIM_DBG("Write to %s", pgd?"PGD":"IFD");
38 msg.start_offset = SLIM_SLAVE_REG_OFFSET + reg;
39 msg.num_bytes = bytes;
40 msg.comp = NULL;
41
42 for ( ; slim_write_tries != 0; slim_write_tries--) {
43 mutex_lock(&btfmslim->xfer_lock);
44 ret = slim_change_val_element(pgd ? btfmslim->slim_pgd :
45 &btfmslim->slim_ifd, &msg, src, bytes);
46 mutex_unlock(&btfmslim->xfer_lock);
47 if (ret == 0)
48 break;
49 usleep_range(5000, 5100);
50 }
51
52 if (ret) {
53 BTFMSLIM_ERR("failed (%d)", ret);
54 return ret;
55 }
56
57 for (i = 0; i < bytes; i++)
58 BTFMSLIM_DBG("Write 0x%02x to reg 0x%x", ((uint8_t *)src)[i],
59 reg + i);
60 return 0;
61}
62
63int btfm_slim_write_pgd(struct btfmslim *btfmslim,
64 uint16_t reg, int bytes, void *src)
65{
66 return btfm_slim_write(btfmslim, reg, bytes, src, PGD);
67}
68
69int btfm_slim_write_inf(struct btfmslim *btfmslim,
70 uint16_t reg, int bytes, void *src)
71{
72 return btfm_slim_write(btfmslim, reg, bytes, src, IFD);
73}
74
75int btfm_slim_read(struct btfmslim *btfmslim, unsigned short reg,
76 int bytes, void *dest, uint8_t pgd)
77{
78 int ret, i;
79 struct slim_ele_access msg;
80 int slim_read_tries = SLIM_SLAVE_RW_MAX_TRIES;
81
82 BTFMSLIM_DBG("Read from %s", pgd?"PGD":"IFD");
83 msg.start_offset = SLIM_SLAVE_REG_OFFSET + reg;
84 msg.num_bytes = bytes;
85 msg.comp = NULL;
86
87 for ( ; slim_read_tries != 0; slim_read_tries--) {
88 mutex_lock(&btfmslim->xfer_lock);
89 ret = slim_request_val_element(pgd ? btfmslim->slim_pgd :
90 &btfmslim->slim_ifd, &msg, dest, bytes);
91 mutex_unlock(&btfmslim->xfer_lock);
92 if (ret == 0)
93 break;
94 usleep_range(5000, 5100);
95 }
96
97 if (ret)
98 BTFMSLIM_ERR("failed (%d)", ret);
99
100 for (i = 0; i < bytes; i++)
101 BTFMSLIM_DBG("Read 0x%02x from reg 0x%x", ((uint8_t *)dest)[i],
102 reg + i);
103
104 return 0;
105}
106
107int btfm_slim_read_pgd(struct btfmslim *btfmslim,
108 uint16_t reg, int bytes, void *dest)
109{
110 return btfm_slim_read(btfmslim, reg, bytes, dest, PGD);
111}
112
113int btfm_slim_read_inf(struct btfmslim *btfmslim,
114 uint16_t reg, int bytes, void *dest)
115{
116 return btfm_slim_read(btfmslim, reg, bytes, dest, IFD);
117}
118
119int btfm_slim_enable_ch(struct btfmslim *btfmslim, struct btfmslim_ch *ch,
120 uint8_t rxport, uint32_t rates, uint8_t grp, uint8_t nchan)
121{
122 int ret, i;
123 struct slim_ch prop;
124 struct btfmslim_ch *chan = ch;
125 uint16_t ch_h[2];
126
127 if (!btfmslim || !ch)
128 return -EINVAL;
129
Rupesh Tatiya3815c792017-02-17 12:58:01 +0530130 BTFMSLIM_DBG("port: %d ch: %d", ch->port, ch->ch);
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800131
132 /* Define the channel with below parameters */
133 prop.prot = SLIM_AUTO_ISO;
134 prop.baser = SLIM_RATE_4000HZ;
135 prop.dataf = (rates == 48000) ? SLIM_CH_DATAF_NOT_DEFINED
136 : SLIM_CH_DATAF_LPCM_AUDIO;
137 prop.auxf = SLIM_CH_AUXF_NOT_APPLICABLE;
138 prop.ratem = (rates/4000);
139 prop.sampleszbits = 16;
140
141 ch_h[0] = ch->ch_hdl;
142 ch_h[1] = (grp) ? (ch+1)->ch_hdl : 0;
143
144 ret = slim_define_ch(btfmslim->slim_pgd, &prop, ch_h, nchan, grp,
145 &ch->grph);
146 if (ret < 0) {
147 BTFMSLIM_ERR("slim_define_ch failed ret[%d]", ret);
148 goto error;
149 }
150
151 for (i = 0; i < nchan; i++, ch++) {
152 /* Enable port through registration setting */
153 if (btfmslim->vendor_port_en) {
154 ret = btfmslim->vendor_port_en(btfmslim, ch->port,
155 rxport, 1);
156 if (ret < 0) {
157 BTFMSLIM_ERR("vendor_port_en failed ret[%d]",
158 ret);
159 goto error;
160 }
161 }
162
163 if (rxport) {
164 BTFMSLIM_INFO("slim_connect_sink(port: %d, ch: %d)",
165 ch->port, ch->ch);
166 /* Connect Port with channel given by Machine driver*/
167 ret = slim_connect_sink(btfmslim->slim_pgd,
168 &ch->port_hdl, 1, ch->ch_hdl);
169 if (ret < 0) {
170 BTFMSLIM_ERR("slim_connect_sink failed ret[%d]",
171 ret);
172 goto remove_channel;
173 }
174
175 } else {
176 BTFMSLIM_INFO("slim_connect_src(port: %d, ch: %d)",
177 ch->port, ch->ch);
178 /* Connect Port with channel given by Machine driver*/
179 ret = slim_connect_src(btfmslim->slim_pgd, ch->port_hdl,
180 ch->ch_hdl);
181 if (ret < 0) {
182 BTFMSLIM_ERR("slim_connect_src failed ret[%d]",
183 ret);
184 goto remove_channel;
185 }
186 }
187 }
188
189 /* Activate the channel immediately */
190 BTFMSLIM_INFO(
191 "port: %d, ch: %d, grp: %d, ch->grph: 0x%x, ch_hdl: 0x%x",
192 chan->port, chan->ch, grp, chan->grph, chan->ch_hdl);
193 ret = slim_control_ch(btfmslim->slim_pgd, (grp ? chan->grph :
194 chan->ch_hdl), SLIM_CH_ACTIVATE, true);
195 if (ret < 0) {
196 BTFMSLIM_ERR("slim_control_ch failed ret[%d]", ret);
197 goto remove_channel;
198 }
199
200error:
201 return ret;
202
203remove_channel:
204 /* Remove the channel immediately*/
205 ret = slim_control_ch(btfmslim->slim_pgd, (grp ? ch->grph : ch->ch_hdl),
206 SLIM_CH_REMOVE, true);
207 if (ret < 0)
208 BTFMSLIM_ERR("slim_control_ch failed ret[%d]", ret);
209
210 return ret;
211}
212
213int btfm_slim_disable_ch(struct btfmslim *btfmslim, struct btfmslim_ch *ch,
214 uint8_t rxport, uint8_t grp, uint8_t nchan)
215{
216 int ret, i;
217
218 if (!btfmslim || !ch)
219 return -EINVAL;
220
221 BTFMSLIM_INFO("port:%d, grp: %d, ch->grph:0x%x, ch->ch_hdl:0x%x ",
222 ch->port, grp, ch->grph, ch->ch_hdl);
223 /* Remove the channel immediately*/
224 ret = slim_control_ch(btfmslim->slim_pgd, (grp ? ch->grph : ch->ch_hdl),
225 SLIM_CH_REMOVE, true);
226 if (ret < 0) {
227 BTFMSLIM_ERR("slim_control_ch failed ret[%d]", ret);
228 ret = slim_disconnect_ports(btfmslim->slim_pgd,
229 &ch->port_hdl, 1);
230 if (ret < 0) {
231 BTFMSLIM_ERR("slim_disconnect_ports failed ret[%d]",
232 ret);
233 goto error;
234 }
235 }
236
237 /* Disable port through registration setting */
238 for (i = 0; i < nchan; i++, ch++) {
239 if (btfmslim->vendor_port_en) {
240 ret = btfmslim->vendor_port_en(btfmslim, ch->port,
241 rxport, 0);
242 if (ret < 0) {
243 BTFMSLIM_ERR("vendor_port_en failed ret[%d]",
244 ret);
245 break;
246 }
247 }
248 }
249error:
250 return ret;
251}
252static int btfm_slim_get_logical_addr(struct slim_device *slim)
253{
254 int ret = 0;
255 const unsigned long timeout = jiffies +
256 msecs_to_jiffies(SLIM_SLAVE_PRESENT_TIMEOUT);
257
258 do {
259 ret = slim_get_logical_addr(slim, slim->e_addr,
260 ARRAY_SIZE(slim->e_addr), &slim->laddr);
261 if (!ret) {
262 BTFMSLIM_DBG("Assigned l-addr: 0x%x", slim->laddr);
263 break;
264 }
265 /* Give SLIMBUS time to report present and be ready. */
266 usleep_range(1000, 1100);
267 BTFMSLIM_DBG("retyring get logical addr");
268 } while (time_before(jiffies, timeout));
269
270 return ret;
271}
272
273static int btfm_slim_alloc_port(struct btfmslim *btfmslim)
274{
275 int ret = -EINVAL, i;
276 struct btfmslim_ch *rx_chs;
277 struct btfmslim_ch *tx_chs;
278
279 if (!btfmslim)
280 return ret;
281
282 rx_chs = btfmslim->rx_chs;
283 tx_chs = btfmslim->tx_chs;
284
285 if (!rx_chs || !tx_chs)
286 return ret;
287
288 BTFMSLIM_DBG("Rx: id\tname\tport\thdl\tch\tch_hdl");
289 for (i = 0 ; (rx_chs->port != BTFM_SLIM_PGD_PORT_LAST) &&
290 (i < BTFM_SLIM_NUM_CODEC_DAIS); i++, rx_chs++) {
291
292 /* Get Rx port handler from slimbus driver based
293 * on port number
294 */
295 ret = slim_get_slaveport(btfmslim->slim_pgd->laddr,
296 rx_chs->port, &rx_chs->port_hdl, SLIM_SINK);
297 if (ret < 0) {
298 BTFMSLIM_ERR("slave port failure port#%d - ret[%d]",
299 rx_chs->port, SLIM_SINK);
300 return ret;
301 }
302 BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", rx_chs->id,
303 rx_chs->name, rx_chs->port, rx_chs->port_hdl,
304 rx_chs->ch, rx_chs->ch_hdl);
305 }
306
307 BTFMSLIM_DBG("Tx: id\tname\tport\thdl\tch\tch_hdl");
308 for (i = 0; (tx_chs->port != BTFM_SLIM_PGD_PORT_LAST) &&
309 (i < BTFM_SLIM_NUM_CODEC_DAIS); i++, tx_chs++) {
310
311 /* Get Tx port handler from slimbus driver based
312 * on port number
313 */
314 ret = slim_get_slaveport(btfmslim->slim_pgd->laddr,
315 tx_chs->port, &tx_chs->port_hdl, SLIM_SRC);
316 if (ret < 0) {
317 BTFMSLIM_ERR("slave port failure port#%d - ret[%d]",
318 tx_chs->port, SLIM_SRC);
319 return ret;
320 }
321 BTFMSLIM_DBG(" %d\t%s\t%d\t%x\t%d\t%x", tx_chs->id,
322 tx_chs->name, tx_chs->port, tx_chs->port_hdl,
323 tx_chs->ch, tx_chs->ch_hdl);
324 }
325 return ret;
326}
327
328int btfm_slim_hw_init(struct btfmslim *btfmslim)
329{
330 int ret;
331
332 BTFMSLIM_DBG("");
333 if (!btfmslim)
334 return -EINVAL;
335
336 if (btfmslim->enabled) {
337 BTFMSLIM_DBG("Already enabled");
338 return 0;
339 }
340 mutex_lock(&btfmslim->io_lock);
341
342 /* Assign Logical Address for PGD (Ported Generic Device)
343 * enumeration address
344 */
345 ret = btfm_slim_get_logical_addr(btfmslim->slim_pgd);
346 if (ret) {
347 BTFMSLIM_ERR("failed to get slimbus %s logical address: %d",
348 btfmslim->slim_pgd->name, ret);
349 goto error;
350 }
351
352 /* Assign Logical Address for Ported Generic Device
353 * enumeration address
354 */
355 ret = btfm_slim_get_logical_addr(&btfmslim->slim_ifd);
356 if (ret) {
357 BTFMSLIM_ERR("failed to get slimbus %s logical address: %d",
358 btfmslim->slim_ifd.name, ret);
359 goto error;
360 }
361
362 /* Allocate ports with logical address to get port handler from
363 * slimbus driver
364 */
365 ret = btfm_slim_alloc_port(btfmslim);
366 if (ret)
367 goto error;
368
369 /* Start vendor specific initialization and get port information */
370 if (btfmslim->vendor_init)
371 ret = btfmslim->vendor_init(btfmslim);
372
373 /* Only when all registers read/write successfully, it set to
374 * enabled status
375 */
376 btfmslim->enabled = 1;
377error:
378 mutex_unlock(&btfmslim->io_lock);
379 return ret;
380}
381
382
383int btfm_slim_hw_deinit(struct btfmslim *btfmslim)
384{
385 int ret = 0;
386
387 if (!btfmslim)
388 return -EINVAL;
389
390 if (!btfmslim->enabled) {
391 BTFMSLIM_DBG("Already disabled");
392 return 0;
393 }
394 mutex_lock(&btfmslim->io_lock);
395 btfmslim->enabled = 0;
396 mutex_unlock(&btfmslim->io_lock);
397 return ret;
398}
399
400static int btfm_slim_get_dt_info(struct btfmslim *btfmslim)
401{
402 int ret = 0;
403 struct slim_device *slim = btfmslim->slim_pgd;
404 struct slim_device *slim_ifd = &btfmslim->slim_ifd;
405 struct property *prop;
406
407 if (!slim || !slim_ifd)
408 return -EINVAL;
409
410 if (slim->dev.of_node) {
411 BTFMSLIM_DBG("Platform data from device tree (%s)",
412 slim->name);
413 ret = of_property_read_string(slim->dev.of_node,
414 "qcom,btfm-slim-ifd", &slim_ifd->name);
415 if (ret) {
416 BTFMSLIM_ERR("Looking up %s property in node %s failed",
417 "qcom,btfm-slim-ifd",
418 slim->dev.of_node->full_name);
419 return -ENODEV;
420 }
421 BTFMSLIM_DBG("qcom,btfm-slim-ifd (%s)", slim_ifd->name);
422
423 prop = of_find_property(slim->dev.of_node,
424 "qcom,btfm-slim-ifd-elemental-addr", NULL);
425 if (!prop) {
426 BTFMSLIM_ERR("Looking up %s property in node %s failed",
427 "qcom,btfm-slim-ifd-elemental-addr",
428 slim->dev.of_node->full_name);
429 return -ENODEV;
430 } else if (prop->length != 6) {
431 BTFMSLIM_ERR(
432 "invalid codec slim ifd addr. addr length= %d",
433 prop->length);
434 return -ENODEV;
435 }
436 memcpy(slim_ifd->e_addr, prop->value, 6);
437 BTFMSLIM_DBG(
438 "PGD Enum Addr: %.02x:%.02x:%.02x:%.02x:%.02x: %.02x",
439 slim->e_addr[0], slim->e_addr[1], slim->e_addr[2],
440 slim->e_addr[3], slim->e_addr[4], slim->e_addr[5]);
441 BTFMSLIM_DBG(
442 "IFD Enum Addr: %.02x:%.02x:%.02x:%.02x:%.02x: %.02x",
443 slim_ifd->e_addr[0], slim_ifd->e_addr[1],
444 slim_ifd->e_addr[2], slim_ifd->e_addr[3],
445 slim_ifd->e_addr[4], slim_ifd->e_addr[5]);
446 } else {
447 BTFMSLIM_ERR("Platform data is not valid");
448 }
449
450 return ret;
451}
452
453static int btfm_slim_probe(struct slim_device *slim)
454{
455 int ret = 0;
456 struct btfmslim *btfm_slim;
457
458 BTFMSLIM_DBG("");
459 if (!slim->ctrl)
460 return -EINVAL;
461
462 /* Allocation btfmslim data pointer */
463 btfm_slim = kzalloc(sizeof(struct btfmslim), GFP_KERNEL);
464 if (btfm_slim == NULL) {
465 BTFMSLIM_ERR("error, allocation failed");
466 return -ENOMEM;
467 }
468 /* BTFM Slimbus driver control data configuration */
469 btfm_slim->slim_pgd = slim;
470
471 /* Assign vendor specific function */
472 btfm_slim->rx_chs = SLIM_SLAVE_RXPORT;
473 btfm_slim->tx_chs = SLIM_SLAVE_TXPORT;
474 btfm_slim->vendor_init = SLIM_SLAVE_INIT;
475 btfm_slim->vendor_port_en = SLIM_SLAVE_PORT_EN;
476
477 /* Created Mutex for slimbus data transfer */
478 mutex_init(&btfm_slim->io_lock);
479 mutex_init(&btfm_slim->xfer_lock);
480
481 /* Get Device tree node for Interface Device enumeration address */
482 ret = btfm_slim_get_dt_info(btfm_slim);
483 if (ret)
484 goto dealloc;
485
486 /* Add Interface Device for slimbus driver */
487 ret = slim_add_device(btfm_slim->slim_pgd->ctrl, &btfm_slim->slim_ifd);
488 if (ret) {
489 BTFMSLIM_ERR("error, adding SLIMBUS device failed");
490 goto dealloc;
491 }
492
493 /* Platform driver data allocation */
494 slim->dev.platform_data = btfm_slim;
495
496 /* Driver specific data allocation */
497 btfm_slim->dev = &slim->dev;
498 ret = btfm_slim_register_codec(&slim->dev);
Satish kumar sugasibcf528a2017-07-27 13:37:22 -0700499 if (ret) {
500 BTFMSLIM_ERR("error, registering slimbus codec failed");
501 goto free;
502 }
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800503 ret = bt_register_slimdev(&slim->dev);
Satish kumar sugasibcf528a2017-07-27 13:37:22 -0700504 if (ret < 0) {
505 btfm_slim_unregister_codec(&slim->dev);
506 goto free;
507 }
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800508 return ret;
Satish kumar sugasibcf528a2017-07-27 13:37:22 -0700509free:
510 slim_remove_device(&btfm_slim->slim_ifd);
Mahesh Kumar Sharma41a4d382017-01-17 17:00:51 -0800511dealloc:
512 mutex_destroy(&btfm_slim->io_lock);
513 mutex_destroy(&btfm_slim->xfer_lock);
514 kfree(btfm_slim);
515 return ret;
516}
517static int btfm_slim_remove(struct slim_device *slim)
518{
519 struct btfmslim *btfm_slim = slim->dev.platform_data;
520
521 BTFMSLIM_DBG("");
522 mutex_destroy(&btfm_slim->io_lock);
523 mutex_destroy(&btfm_slim->xfer_lock);
524 snd_soc_unregister_codec(&slim->dev);
525
526 BTFMSLIM_DBG("slim_remove_device() - btfm_slim->slim_ifd");
527 slim_remove_device(&btfm_slim->slim_ifd);
528
529 kfree(btfm_slim);
530
531 BTFMSLIM_DBG("slim_remove_device() - btfm_slim->slim_pgd");
532 slim_remove_device(slim);
533 return 0;
534}
535
536static const struct slim_device_id btfm_slim_id[] = {
537 {SLIM_SLAVE_COMPATIBLE_STR, 0},
538 {}
539};
540
541static struct slim_driver btfm_slim_driver = {
542 .driver = {
543 .name = "btfmslim-driver",
544 .owner = THIS_MODULE,
545 },
546 .probe = btfm_slim_probe,
547 .remove = btfm_slim_remove,
548 .id_table = btfm_slim_id
549};
550
551static int __init btfm_slim_init(void)
552{
553 int ret;
554
555 BTFMSLIM_DBG("");
556 ret = slim_driver_register(&btfm_slim_driver);
557 if (ret)
558 BTFMSLIM_ERR("Failed to register slimbus driver: %d", ret);
559 return ret;
560}
561
562static void __exit btfm_slim_exit(void)
563{
564 BTFMSLIM_DBG("");
565 slim_driver_unregister(&btfm_slim_driver);
566}
567
568module_init(btfm_slim_init);
569module_exit(btfm_slim_exit);
570
571MODULE_LICENSE("GPL v2");
572MODULE_DESCRIPTION("BTFM Slimbus Slave driver");