aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/parrot/spi/spimtest_loopback.c
blob: 109d4651d71bf984e7b413cfceffd35b399722dc (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
/*
 * @file spis_22_master.c
 * @brief Receive a big buffer by sending multiple times the same messages.
 *
 * Copyright (C) 2012 Parrot S.A.
 *
 * @author     damien.riegel.ext@parrot.com
 * @date       Dec 2012
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/platform_device.h>
#include <linux/ioport.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/module.h> 
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/dma-mapping.h>

struct wrapper_spi_info {
	struct spi_message* mesg;
	struct spi_device*	spi;
	int 				mesg_position;
	int 				nb_send;
	int					target_send;
	unsigned char		offset;
	int					fail_at;
};

static unsigned int nb_messages = 1;
module_param(nb_messages, uint, 0644);

static unsigned int message_size = 256;
module_param(message_size, uint, 0644);

static unsigned int total_len = 256;
module_param(total_len, uint, 0644);

static unsigned int offset = 0x2A;
module_param(offset, uint, 0644);

static unsigned int speed_hz = 26 * 1000 * 1000;
module_param(speed_hz, uint, 0644);

static struct spi_message *mesgs = NULL;
static struct spi_transfer *xfers = NULL;
static struct wrapper_spi_info* infos = NULL;
static unsigned int nb_send = 2;
static int total_messages_received;
static struct completion spimtest_completion;
static unsigned int should_exit = 0;

#define spimtest_print(level, format, arg...) \
	printk(level "[SPIMTEST LOOPBACK][MSTR] " format, ##arg)

#define spimtest_info(format, arg...) \
	spimtest_print(KERN_INFO, format, ##arg)

#define spimtest_err(format, arg...) \
	spimtest_print(KERN_ERR, format, ##arg)


static int spimtest_allocate(void)
{
	int err = -ENOMEM;
	int i;

	infos = kzalloc(nb_messages * sizeof(struct spi_message), GFP_KERNEL);
	if (! infos) {
		goto exit;
	}

	mesgs = kzalloc(nb_messages * sizeof(struct spi_message), GFP_KERNEL);
	if (! mesgs) {
		goto free_infos;
	}

	xfers = kzalloc(nb_messages * sizeof(struct spi_transfer), GFP_KERNEL);
	if (! xfers) {
		goto free_mesgs;
	}

	for(i = 0; i < nb_messages; i++) {
		int j, loc_offset;
		u8 *buf;

		spi_message_init(&mesgs[i]);
		memset(&xfers[i], 0, sizeof(struct spi_transfer));
		xfers[i].len = message_size;
		xfers[i].speed_hz = speed_hz;

		buf = kzalloc(message_size, GFP_KERNEL);
		if (! buf ) {
			goto free_buffers;
		}

		loc_offset = (offset + (i * message_size)) % 256;
		for(j = 0; j < message_size; j++) {
			buf[j] = loc_offset++ % 256;
		}

		xfers[i].tx_buf = buf;

		spi_message_add_tail(&xfers[i], &mesgs[i]);
	}

	return 0;

free_buffers:
	for(i = 0; i < nb_messages; i++) {

		if (xfers[i].tx_buf) {
			kfree(xfers[i].tx_buf);
		}
	}
	kfree(xfers);

free_mesgs:
	kfree(mesgs);
free_infos:
	kfree(infos);
exit:
	return err;
}

static void spimtest_receive(void* context)
{
	struct wrapper_spi_info* info = context;
	struct spi_message* mesg = info->mesg;

	if (mesg->status) {
		spimtest_err("-FAIL- Msg %d (round %d) received with error code %d\n",
				info->mesg_position, info->nb_send, mesg->status);
		should_exit++;
		goto completion;
	}

	info->nb_send++;
	if (info->nb_send < info->target_send) {
		spi_async(info->spi, mesg);
	}

completion:
	total_messages_received++;
	if (total_messages_received == nb_send || should_exit == nb_messages)
		complete(&spimtest_completion);
}

static int __devinit spimtest_probe(struct spi_device *spi) 
{
	int err = 0, i;
	int send_per_message;
	int remaining_send;

	total_messages_received = 0;
	nb_send = total_len / message_size;
	if (total_len % message_size) {
		spimtest_err("total_len (%u) must be a multiple of message_size (%u)\n",
				total_len, message_size);
		return -EINVAL;
	}

	if (((message_size * nb_messages) % 256) != 0) {
		spimtest_err("(message_size * nb_messages) must be a multiple of 256\n");
		return -EINVAL;
	}

	/* some messages are going to be sent "send_per_message" times */
	send_per_message = nb_send / nb_messages;
	/* and some "send_per_message" + 1 times */
	remaining_send = nb_send % nb_messages;

	init_completion(&spimtest_completion);
	err = spimtest_allocate();
	if (err) {
		goto exit;
	}

	/*
	 * Send each message once, the rest is done in completion function
	 */
	for(i = 0; i < nb_messages; i++) {
		infos[i].mesg = &mesgs[i];
		infos[i].spi = spi;
		infos[i].nb_send = 0;
		infos[i].target_send = send_per_message;
		if (remaining_send) {
			infos[i].target_send++;
			remaining_send--;
		}
		infos[i].fail_at = 0;
		infos[i].mesg_position = i;

		mesgs[i].complete = spimtest_receive;
		mesgs[i].context = &infos[i];
		err = spi_async(spi, &mesgs[i]);
		if (err) {
			spimtest_err("-FAIL- Can't send a message (%d)\n",
					err);
		}
	}
	
	wait_for_completion(&spimtest_completion);
	spimtest_info("%s message pool %d msg. %d msgs sent\n",
			should_exit ? "-FAIL-" : "-PASS-",
			nb_messages, nb_send);

exit:
	for(i = 0; i < nb_messages; i++) {
		kfree(xfers[i].tx_buf);
	}

	kfree(mesgs);
	kfree(xfers);
	kfree(infos);
	return err;
}

static int __devexit spimtest_remove(struct spi_device *spi)
{
	return 0;
}


static struct spi_driver spimtest_driver = {
	.driver = {
		.name	= "spidev",
		.bus	= &spi_bus_type,
		.owner	= THIS_MODULE,
	},
	.probe	= spimtest_probe,
	.remove	= __devexit_p(spimtest_remove),
};

module_spi_driver(spimtest_driver);
MODULE_DESCRIPTION( "spimtest loopback module" );
MODULE_LICENSE( "GPL" );