aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpio/gpio-msm-smp2p-test.c
blob: 1067c4aade39d2809f3f790456064770b605ad06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* drivers/gpio/gpio-msm-smp2p-test.c
 *
 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/debugfs.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/bitmap.h>
#include "../soc/qcom/smp2p_private.h"
#include "../soc/qcom/smp2p_test_common.h"

/* Interrupt callback data */
struct gpio_info {
	int gpio_base_id;
	int irq_base_id;

	bool initialized;
	struct completion cb_completion;
	int cb_count;
	DECLARE_BITMAP(triggered_irqs, SMP2P_BITS_PER_ENTRY);
};

/* GPIO Inbound/Outbound callback info */
struct gpio_inout {
	struct gpio_info in;
	struct gpio_info out;
};

static struct gpio_inout gpio_info[SMP2P_NUM_PROCS];

/**
 * Init/reset the callback data.
 *
 * @info: Pointer to callback data
 */
static void cb_data_reset(struct gpio_info *info)
{
	int n;

	if (!info)
		return;

	if (!info->initialized) {
		init_completion(&info->cb_completion);
		info->initialized = true;
	}
	info->cb_count = 0;

	for (n = 0; n < SMP2P_BITS_PER_ENTRY; ++n)
		clear_bit(n,  info->triggered_irqs);

	reinit_completion(&info->cb_completion);
}

static int smp2p_gpio_test_probe(struct platform_device *pdev)
{
	int id;
	int cnt;
	struct device_node *node = pdev->dev.of_node;
	struct gpio_info *gpio_info_ptr = NULL;

	/*
	 * NOTE:  This does a string-lookup of the GPIO pin name and doesn't
	 * actually directly link to the SMP2P GPIO driver since all
	 * GPIO/Interrupt access must be through standard
	 * Linux GPIO / Interrupt APIs.
	 */
	if (strcmp("qcom,smp2pgpio_test_smp2p_1_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_MODEM_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_1_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_MODEM_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_2_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_AUDIO_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_2_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_AUDIO_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_3_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_SENSOR_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_3_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_SENSOR_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_4_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_WIRELESS_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_4_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_WIRELESS_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_5_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_CDSP_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_5_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_CDSP_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_7_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_TZ_PROC].in;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_7_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_TZ_PROC].out;
	} else if (strcmp("qcom,smp2pgpio_test_smp2p_15_in", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_REMOTE_MOCK_PROC].in;
	} else if (
		strcmp("qcom,smp2pgpio_test_smp2p_15_out", node->name) == 0) {
		gpio_info_ptr = &gpio_info[SMP2P_REMOTE_MOCK_PROC].out;
	} else {
		pr_err("%s: unable to match device type '%s'\n",
				__func__, node->name);
		return -ENODEV;
	}

	/* retrieve the GPIO and interrupt ID's */
	cnt = of_gpio_count(node);
	if (cnt && gpio_info_ptr) {
		/*
		 * Instead of looping through all 32-bits, we can just get the
		 * first pin to get the base IDs.  This saves on the verbosity
		 * of the device tree nodes as well.
		 */
		id = of_get_gpio(node, 0);
		if (id == -EPROBE_DEFER)
			return id;
		gpio_info_ptr->gpio_base_id = id;
		gpio_info_ptr->irq_base_id = gpio_to_irq(id);
	}
	return 0;
}

/*
 * NOTE:  Instead of match table and device driver, you may be able to just
 * call of_find_compatible_node() in your init function.
 */
static const struct of_device_id msm_smp2p_match_table[] = {
	/* modem */
	{.compatible = "qcom,smp2pgpio_test_smp2p_1_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_1_in", },

	/* audio (adsp) */
	{.compatible = "qcom,smp2pgpio_test_smp2p_2_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_2_in", },

	/* sensor */
	{.compatible = "qcom,smp2pgpio_test_smp2p_3_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_3_in", },

	/* wcnss */
	{.compatible = "qcom,smp2pgpio_test_smp2p_4_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_4_in", },

	/* CDSP */
	{.compatible = "qcom,smp2pgpio_test_smp2p_5_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_5_in", },

	/* TZ */
	{.compatible = "qcom,smp2pgpio_test_smp2p_7_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_7_in", },

	/* mock loopback */
	{.compatible = "qcom,smp2pgpio_test_smp2p_15_out", },
	{.compatible = "qcom,smp2pgpio_test_smp2p_15_in", },
	{},
};

static struct platform_driver smp2p_gpio_driver = {
	.probe = smp2p_gpio_test_probe,
	.driver = {
		.name = "smp2pgpio_test",
		.owner = THIS_MODULE,
		.of_match_table = msm_smp2p_match_table,
	},
};

/**
 * smp2p_ut_local_gpio_out - Verify outbound functionality.
 *
 * @s:   pointer to output file
 */
static void smp2p_ut_local_gpio_out(struct seq_file *s)
{
	int failed = 0;
	struct gpio_info *cb_info = &gpio_info[SMP2P_REMOTE_MOCK_PROC].out;
	int ret;
	int id;
	struct msm_smp2p_remote_mock *mock;

	seq_printf(s, "Running %s\n", __func__);
	do {
		/* initialize mock edge */
		ret = smp2p_reset_mock_edge();
		UT_ASSERT_INT(ret, ==, 0);

		mock = msm_smp2p_get_remote_mock();
		UT_ASSERT_PTR(mock, !=, NULL);

		mock->rx_interrupt_count = 0;
		memset(&mock->remote_item, 0,
			sizeof(struct smp2p_smem_item));
		smp2p_init_header((struct smp2p_smem *)&mock->remote_item,
			SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC,
			0, 1);
		strlcpy(mock->remote_item.entries[0].name, "smp2p",
			SMP2P_MAX_ENTRY_NAME);
		SMP2P_SET_ENT_VALID(
			mock->remote_item.header.valid_total_ent, 1);
		msm_smp2p_set_remote_mock_exists(true);
		mock->tx_interrupt();

		/* open GPIO entry */
		smp2p_gpio_open_test_entry("smp2p",
				SMP2P_REMOTE_MOCK_PROC, true);

		/* verify set/get functions */
		UT_ASSERT_INT(0, <, cb_info->gpio_base_id);
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			int pin = cb_info->gpio_base_id + id;

			mock->rx_interrupt_count = 0;
			gpio_set_value(pin, 1);
			UT_ASSERT_INT(1, ==, mock->rx_interrupt_count);
			UT_ASSERT_INT(1, ==, gpio_get_value(pin));

			gpio_set_value(pin, 0);
			UT_ASSERT_INT(2, ==, mock->rx_interrupt_count);
			UT_ASSERT_INT(0, ==, gpio_get_value(pin));
		}
		if (failed)
			break;

		seq_puts(s, "\tOK\n");
	} while (0);

	if (failed) {
		pr_err("%s: Failed\n", __func__);
		seq_puts(s, "\tFailed\n");
	}

	smp2p_gpio_open_test_entry("smp2p",
			SMP2P_REMOTE_MOCK_PROC, false);
}

/**
 * smp2p_gpio_irq - Interrupt handler for inbound entries.
 *
 * @irq:         Virtual IRQ being triggered
 * @data:        Cookie data (struct gpio_info * in this case)
 * @returns:     Number of bytes written
 */
static irqreturn_t smp2p_gpio_irq(int irq, void *data)
{
	struct gpio_info *gpio_ptr = (struct gpio_info *)data;
	int offset;

	if (!gpio_ptr) {
		pr_err("%s: gpio_ptr is NULL for irq %d\n", __func__, irq);
		return IRQ_HANDLED;
	}

	offset = irq - gpio_ptr->irq_base_id;
	if (offset >= 0 &&  offset < SMP2P_BITS_PER_ENTRY)
		set_bit(offset, gpio_ptr->triggered_irqs);
	else
		pr_err("%s: invalid irq offset base %d; irq %d\n",
			__func__, gpio_ptr->irq_base_id, irq);

	++gpio_ptr->cb_count;
	complete(&gpio_ptr->cb_completion);
	return IRQ_HANDLED;
}

/**
 * smp2p_ut_local_gpio_in - Verify inbound functionality.
 *
 * @s:   pointer to output file
 */
static void smp2p_ut_local_gpio_in(struct seq_file *s)
{
	int failed = 0;
	struct gpio_info *cb_info = &gpio_info[SMP2P_REMOTE_MOCK_PROC].in;
	int id;
	int ret;
	int virq;
	struct msm_smp2p_remote_mock *mock;

	seq_printf(s, "Running %s\n", __func__);

	cb_data_reset(cb_info);
	do {
		/* initialize mock edge */
		ret = smp2p_reset_mock_edge();
		UT_ASSERT_INT(ret, ==, 0);

		mock = msm_smp2p_get_remote_mock();
		UT_ASSERT_PTR(mock, !=, NULL);

		mock->rx_interrupt_count = 0;
		memset(&mock->remote_item, 0,
			sizeof(struct smp2p_smem_item));
		smp2p_init_header((struct smp2p_smem *)&mock->remote_item,
			SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC,
			0, 1);
		strlcpy(mock->remote_item.entries[0].name, "smp2p",
			SMP2P_MAX_ENTRY_NAME);
		SMP2P_SET_ENT_VALID(
			mock->remote_item.header.valid_total_ent, 1);
		msm_smp2p_set_remote_mock_exists(true);
		mock->tx_interrupt();

		smp2p_gpio_open_test_entry("smp2p",
				SMP2P_REMOTE_MOCK_PROC, true);

		/* verify set/get functions locally */
		UT_ASSERT_INT(0, <, cb_info->gpio_base_id);
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			int pin;
			int current_value;

			/* verify pin value cannot be set */
			pin = cb_info->gpio_base_id + id;
			current_value = gpio_get_value(pin);

			gpio_set_value(pin, 0);
			UT_ASSERT_INT(current_value, ==, gpio_get_value(pin));
			gpio_set_value(pin, 1);
			UT_ASSERT_INT(current_value, ==, gpio_get_value(pin));

			/* verify no interrupts */
			UT_ASSERT_INT(0, ==, cb_info->cb_count);
		}
		if (failed)
			break;

		/* register for interrupts */
		UT_ASSERT_INT(0, <, cb_info->irq_base_id);
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;
			UT_ASSERT_PTR(NULL, !=, irq_to_desc(virq));
			ret = request_irq(virq,
					smp2p_gpio_irq,	IRQF_TRIGGER_RISING,
					"smp2p_test", cb_info);
			UT_ASSERT_INT(0, ==, ret);
		}
		if (failed)
			break;

		/* verify both rising and falling edge interrupts */
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;
			irq_set_irq_type(virq, IRQ_TYPE_EDGE_BOTH);
			cb_data_reset(cb_info);

			/* verify rising-edge interrupt */
			mock->remote_item.entries[0].entry = 1 << id;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 1);
			UT_ASSERT_INT(0, <,
				test_bit(id, cb_info->triggered_irqs));
			test_bit(id, cb_info->triggered_irqs);

			/* verify falling-edge interrupt */
			mock->remote_item.entries[0].entry = 0;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 2);
			UT_ASSERT_INT(0, <,
					test_bit(id, cb_info->triggered_irqs));
		}
		if (failed)
			break;

		/* verify rising-edge interrupts */
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;
			irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
			cb_data_reset(cb_info);

			/* verify only rising-edge interrupt is triggered */
			mock->remote_item.entries[0].entry = 1 << id;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 1);
			UT_ASSERT_INT(0, <,
				test_bit(id, cb_info->triggered_irqs));
			test_bit(id, cb_info->triggered_irqs);

			mock->remote_item.entries[0].entry = 0;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 1);
			UT_ASSERT_INT(0, <,
				test_bit(id, cb_info->triggered_irqs));
		}
		if (failed)
			break;

		/* verify falling-edge interrupts */
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;
			irq_set_irq_type(virq, IRQ_TYPE_EDGE_FALLING);
			cb_data_reset(cb_info);

			/* verify only rising-edge interrupt is triggered */
			mock->remote_item.entries[0].entry = 1 << id;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 0);
			UT_ASSERT_INT(0, ==,
				test_bit(id, cb_info->triggered_irqs));

			mock->remote_item.entries[0].entry = 0;
			mock->tx_interrupt();
			UT_ASSERT_INT(cb_info->cb_count, ==, 1);
			UT_ASSERT_INT(0, <,
				test_bit(id, cb_info->triggered_irqs));
		}
		if (failed)
			break;

		seq_puts(s, "\tOK\n");
	} while (0);

	if (failed) {
		pr_err("%s: Failed\n", __func__);
		seq_puts(s, "\tFailed\n");
	}

	/* unregister for interrupts */
	if (cb_info->irq_base_id) {
		for (id = 0; id < SMP2P_BITS_PER_ENTRY; ++id)
			free_irq(cb_info->irq_base_id + id, cb_info);
	}

	smp2p_gpio_open_test_entry("smp2p",
			SMP2P_REMOTE_MOCK_PROC, false);
}

/**
 * smp2p_ut_local_gpio_in_update_open - Verify combined open/update.
 *
 * @s:   pointer to output file
 *
 * If the remote side updates the SMP2P bits and sends before negotiation is
 * complete, then the UPDATE event will have to be delayed until negotiation is
 * complete.  This should result in both the OPEN and UPDATE events coming in
 * right after each other and the behavior should be transparent to the clients
 * of SMP2P GPIO.
 */
static void smp2p_ut_local_gpio_in_update_open(struct seq_file *s)
{
	int failed = 0;
	struct gpio_info *cb_info = &gpio_info[SMP2P_REMOTE_MOCK_PROC].in;
	int id;
	int ret;
	int virq;
	struct msm_smp2p_remote_mock *mock;

	seq_printf(s, "Running %s\n", __func__);

	cb_data_reset(cb_info);
	do {
		/* initialize mock edge */
		ret = smp2p_reset_mock_edge();
		UT_ASSERT_INT(ret, ==, 0);

		mock = msm_smp2p_get_remote_mock();
		UT_ASSERT_PTR(mock, !=, NULL);

		mock->rx_interrupt_count = 0;
		memset(&mock->remote_item, 0,
			sizeof(struct smp2p_smem_item));
		smp2p_init_header((struct smp2p_smem *)&mock->remote_item,
			SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC,
			0, 1);
		strlcpy(mock->remote_item.entries[0].name, "smp2p",
			SMP2P_MAX_ENTRY_NAME);
		SMP2P_SET_ENT_VALID(
			mock->remote_item.header.valid_total_ent, 1);

		/* register for interrupts */
		smp2p_gpio_open_test_entry("smp2p",
				SMP2P_REMOTE_MOCK_PROC, true);

		UT_ASSERT_INT(0, <, cb_info->irq_base_id);
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;
			UT_ASSERT_PTR(NULL, !=, irq_to_desc(virq));
			ret = request_irq(virq,
					smp2p_gpio_irq,	IRQ_TYPE_EDGE_BOTH,
					"smp2p_test", cb_info);
			UT_ASSERT_INT(0, ==, ret);
		}
		if (failed)
			break;

		/* update the state value and complete negotiation */
		mock->remote_item.entries[0].entry = 0xDEADDEAD;
		msm_smp2p_set_remote_mock_exists(true);
		mock->tx_interrupt();

		/* verify delayed state updates were processed */
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			virq = cb_info->irq_base_id + id;

			UT_ASSERT_INT(cb_info->cb_count, >, 0);
			if (0x1 & (0xDEADDEAD >> id)) {
				/* rising edge should have been triggered */
				if (!test_bit(id, cb_info->triggered_irqs)) {
					seq_printf(s, "%s:%d bit %d clear, ",
						__func__, __LINE__, id);
					seq_puts(s, "expected set\n");
					failed = 1;
					break;
				}
			} else {
				/* edge should not have been triggered */
				if (test_bit(id, cb_info->triggered_irqs)) {
					seq_printf(s, "%s:%d bit %d set, ",
						__func__, __LINE__, id);
					seq_puts(s, "expected clear\n");
					failed = 1;
					break;
				}
			}
		}
		if (failed)
			break;

		seq_puts(s, "\tOK\n");
	} while (0);

	if (failed) {
		pr_err("%s: Failed\n", __func__);
		seq_puts(s, "\tFailed\n");
	}

	/* unregister for interrupts */
	if (cb_info->irq_base_id) {
		for (id = 0; id < SMP2P_BITS_PER_ENTRY; ++id)
			free_irq(cb_info->irq_base_id + id, cb_info);
	}

	smp2p_gpio_open_test_entry("smp2p",
			SMP2P_REMOTE_MOCK_PROC, false);
}

/**
 * smp2p_gpio_write_bits - writes value to each GPIO pin specified in mask.
 *
 * @gpio: gpio test structure
 * @mask: 1 = write gpio_value to this GPIO pin
 * @gpio_value: value to write to GPIO pin
 */
static void smp2p_gpio_write_bits(struct gpio_info *gpio, uint32_t mask,
	int gpio_value)
{
	int n;

	for (n = 0; n < SMP2P_BITS_PER_ENTRY; ++n) {
		if (mask & 0x1)
			gpio_set_value(gpio->gpio_base_id + n, gpio_value);
		mask >>= 1;
	}
}

static void smp2p_gpio_set_bits(struct gpio_info *gpio, uint32_t mask)
{
	smp2p_gpio_write_bits(gpio, mask, 1);
}

static void smp2p_gpio_clr_bits(struct gpio_info *gpio, uint32_t mask)
{
	smp2p_gpio_write_bits(gpio, mask, 0);
}

/**
 * smp2p_gpio_get_value - reads entire 32-bits of GPIO
 *
 * @gpio: gpio structure
 * @returns: 32 bit value of GPIO pins
 */
static uint32_t smp2p_gpio_get_value(struct gpio_info *gpio)
{
	int n;
	uint32_t value = 0;

	for (n = 0; n < SMP2P_BITS_PER_ENTRY; ++n) {
		if (gpio_get_value(gpio->gpio_base_id + n))
			value |= 1 << n;
	}
	return value;
}

/**
 * smp2p_ut_remote_inout_core - Verify inbound/outbound functionality.
 *
 * @s:   pointer to output file
 * @remote_pid:  Remote processor to test
 * @name:        Name of the test for reporting
 *
 * This test verifies inbound/outbound functionality for the remote processor.
 */
static void smp2p_ut_remote_inout_core(struct seq_file *s, int remote_pid,
		const char *name)
{
	int failed = 0;
	uint32_t request;
	uint32_t response;
	struct gpio_info *cb_in;
	struct gpio_info *cb_out;
	int id;
	int ret;

	seq_printf(s, "Running %s for '%s' remote pid %d\n",
		   __func__, smp2p_pid_to_name(remote_pid), remote_pid);

	cb_in = &gpio_info[remote_pid].in;
	cb_out = &gpio_info[remote_pid].out;
	cb_data_reset(cb_in);
	cb_data_reset(cb_out);
	do {
		/* open test entries */
		msm_smp2p_deinit_rmt_lpb_proc(remote_pid);
		smp2p_gpio_open_test_entry("smp2p", remote_pid, true);

		/* register for interrupts */
		UT_ASSERT_INT(0, <, cb_in->gpio_base_id);
		UT_ASSERT_INT(0, <, cb_in->irq_base_id);
		for (id = 0; id < SMP2P_BITS_PER_ENTRY && !failed; ++id) {
			int virq = cb_in->irq_base_id + id;

			UT_ASSERT_PTR(NULL, !=, irq_to_desc(virq));
			ret = request_irq(virq,
				smp2p_gpio_irq,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"smp2p_test", cb_in);
			UT_ASSERT_INT(0, ==, ret);
		}
		if (failed)
			break;

		/* write echo of data value 0 */
		UT_ASSERT_INT(0, <, cb_out->gpio_base_id);
		request = 0x0;
		SMP2P_SET_RMT_CMD_TYPE(request, 1);
		SMP2P_SET_RMT_CMD(request, SMP2P_LB_CMD_ECHO);
		SMP2P_SET_RMT_DATA(request, 0x0);

		smp2p_gpio_set_bits(cb_out, SMP2P_RMT_IGNORE_MASK);
		smp2p_gpio_clr_bits(cb_out, ~SMP2P_RMT_IGNORE_MASK);
		smp2p_gpio_set_bits(cb_out, request);

		UT_ASSERT_INT(cb_in->cb_count, ==, 0);
		smp2p_gpio_clr_bits(cb_out, SMP2P_RMT_IGNORE_MASK);

		/* verify response */
		do {
			/* wait for up to 32 changes */
			if (wait_for_completion_timeout(
					&cb_in->cb_completion, HZ / 2) == 0)
				break;
			reinit_completion(&cb_in->cb_completion);
		} while (cb_in->cb_count < 32);
		UT_ASSERT_INT(cb_in->cb_count, >, 0);
		response = smp2p_gpio_get_value(cb_in);
		SMP2P_SET_RMT_CMD_TYPE(request, 0);
		UT_ASSERT_HEX(request, ==, response);

		/* write echo of data value of all 1's */
		request = 0x0;
		SMP2P_SET_RMT_CMD_TYPE(request, 1);
		SMP2P_SET_RMT_CMD(request, SMP2P_LB_CMD_ECHO);
		SMP2P_SET_RMT_DATA(request, ~0);

		smp2p_gpio_set_bits(cb_out, SMP2P_RMT_IGNORE_MASK);
		cb_data_reset(cb_in);
		smp2p_gpio_clr_bits(cb_out, ~SMP2P_RMT_IGNORE_MASK);
		smp2p_gpio_set_bits(cb_out, request);

		UT_ASSERT_INT(cb_in->cb_count, ==, 0);
		smp2p_gpio_clr_bits(cb_out, SMP2P_RMT_IGNORE_MASK);

		/* verify response including 24 interrupts */
		do {
			UT_ASSERT_INT(
				(int)wait_for_completion_timeout(
					&cb_in->cb_completion, HZ / 2),
			   >, 0);
			reinit_completion(&cb_in->cb_completion);
		} while (cb_in->cb_count < 24);
		response = smp2p_gpio_get_value(cb_in);
		SMP2P_SET_RMT_CMD_TYPE(request, 0);
		UT_ASSERT_HEX(request, ==, response);
		UT_ASSERT_INT(24, ==, cb_in->cb_count);

		seq_puts(s, "\tOK\n");
	} while (0);

	if (failed) {
		pr_err("%s: Failed\n", name);
		seq_puts(s, "\tFailed\n");
	}

	/* unregister for interrupts */
	if (cb_in->irq_base_id) {
		for (id = 0; id < SMP2P_BITS_PER_ENTRY; ++id)
			free_irq(cb_in->irq_base_id + id, cb_in);
	}

	smp2p_gpio_open_test_entry("smp2p",	remote_pid, false);
	msm_smp2p_init_rmt_lpb_proc(remote_pid);
}

/**
 * smp2p_ut_remote_inout - Verify inbound/outbound functionality for all.
 *
 * @s:   pointer to output file
 *
 * This test verifies inbound and outbound functionality for all
 * configured remote processor.
 */
static void smp2p_ut_remote_inout(struct seq_file *s)
{
	struct smp2p_interrupt_config *int_cfg;
	int pid;

	int_cfg = smp2p_get_interrupt_config();
	if (!int_cfg) {
		seq_puts(s, "Remote processor config unavailable\n");
		return;
	}

	for (pid = 0; pid < SMP2P_NUM_PROCS; ++pid) {
		if (!int_cfg[pid].is_configured)
			continue;

		smp2p_ut_remote_inout_core(s, pid, __func__);
	}
}

static int __init smp2p_debugfs_init(void)
{
	/* register GPIO pins */
	(void)platform_driver_register(&smp2p_gpio_driver);

	/*
	 * Add Unit Test entries.
	 *
	 * The idea with unit tests is that you can run all of them
	 * from ADB shell by doing:
	 *  adb shell
	 *  cat ut*
	 *
	 * And if particular tests fail, you can then repeatedly run the
	 * failing tests as you debug and resolve the failing test.
	 */
	smp2p_debug_create("ut_local_gpio_out", smp2p_ut_local_gpio_out);
	smp2p_debug_create("ut_local_gpio_in", smp2p_ut_local_gpio_in);
	smp2p_debug_create("ut_local_gpio_in_update_open",
		smp2p_ut_local_gpio_in_update_open);
	smp2p_debug_create("ut_remote_gpio_inout", smp2p_ut_remote_inout);
	return 0;
}
late_initcall(smp2p_debugfs_init);