hc
2024-08-16 62c46c9150c4afde7e5b25436263fddf79d66f0b
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
/*drivers/spi/spi-rockchip-test.c -spi test driver
 *
 *
 * 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.
 */
 
/* how to test spi
* echo write 0 10 255 > /dev/spi_misc_test
* echo write 0 10 255 init.rc > /dev/spi_misc_test
* echo read 0 10 255 > /dev/spi_misc_test
* echo loop 0 10 255 > /dev/spi_misc_test
* echo setspeed 0 1000000 > /dev/spi_misc_test
*/
 
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/spi/spi.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/miscdevice.h>
#include <linux/hrtimer.h>
#include <linux/platform_data/spi-rockchip.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
 
#define MAX_SPI_DEV_NUM 10
#define SPI_MAX_SPEED_HZ    12000000
 
struct spi_test_data {
   struct device    *dev;
   struct spi_device *spi;
   char *rx_buf;
   int rx_len;
   char *tx_buf;
   int tx_len;
};
 
static struct spi_test_data *g_spi_test_data[MAX_SPI_DEV_NUM];
static u32 bit_per_word = 8;
 
int spi_write_slt(int id, const void *txbuf, size_t n)
{
   int ret = -1;
   struct spi_device *spi = NULL;
   struct spi_transfer     t = {
           .tx_buf         = txbuf,
           .len            = n,
           .bits_per_word = bit_per_word,
       };
   struct spi_message      m;
 
   if (id >= MAX_SPI_DEV_NUM)
       return ret;
   if (!g_spi_test_data[id]) {
       pr_err("g_spi.%d is NULL\n", id);
       return ret;
   } else {
       spi = g_spi_test_data[id]->spi;
   }
 
   spi_message_init(&m);
   spi_message_add_tail(&t, &m);
   ret = spi_sync(spi, &m);
   if (m.actual_length && m.actual_length != n)
       pr_err("%s len=%d actual_length=%d\n", __func__, n, m.actual_length);
 
   return ret;
}
 
int spi_read_slt(int id, void *rxbuf, size_t n)
{
   int ret = -1;
   struct spi_device *spi = NULL;
   struct spi_transfer     t = {
           .rx_buf         = rxbuf,
           .len            = n,
           .bits_per_word = bit_per_word,
       };
   struct spi_message      m;
 
   if (id >= MAX_SPI_DEV_NUM)
       return ret;
   if (!g_spi_test_data[id]) {
       pr_err("g_spi.%d is NULL\n", id);
       return ret;
   } else {
       spi = g_spi_test_data[id]->spi;
   }
 
   spi_message_init(&m);
   spi_message_add_tail(&t, &m);
   ret = spi_sync(spi, &m);
   if (m.actual_length && m.actual_length != n)
       pr_err("%s len=%d actual_length=%d\n", __func__, n, m.actual_length);
 
   return ret;
}
 
int spi_write_then_read_slt(int id, const void *txbuf, unsigned n_tx,
       void *rxbuf, unsigned n_rx)
{
   int ret = -1;
   struct spi_device *spi = NULL;
 
   if (id >= MAX_SPI_DEV_NUM)
       return ret;
   if (!g_spi_test_data[id]) {
       pr_err("g_spi.%d is NULL\n", id);
       return ret;
   } else {
       spi = g_spi_test_data[id]->spi;
   }
 
   ret = spi_write_then_read(spi, txbuf, n_tx, rxbuf, n_rx);
   return ret;
}
 
int spi_write_and_read_slt(int id, const void *tx_buf,
           void *rx_buf, size_t len)
{
   int ret = -1;
   struct spi_device *spi = NULL;
   struct spi_transfer     t = {
           .tx_buf         = tx_buf,
           .rx_buf         = rx_buf,
           .len            = len,
       };
   struct spi_message      m;
 
   if (id >= MAX_SPI_DEV_NUM)
       return ret;
   if (!g_spi_test_data[id]) {
       pr_err("g_spi.%d is NULL\n", id);
       return ret;
   } else {
       spi = g_spi_test_data[id]->spi;
   }
 
   spi_message_init(&m);
   spi_message_add_tail(&t, &m);
   return spi_sync(spi, &m);
}
 
static ssize_t spi_test_write(struct file *file,
           const char __user *buf, size_t n, loff_t *offset)
{
   int argc = 0, i;
   char tmp[64];
   char *argv[16];
   char *cmd, *data;
   unsigned int id = 0, times = 0, size = 0;
   unsigned long us = 0, bytes = 0;
   char *txbuf = NULL, *rxbuf = NULL;
   ktime_t start_time;
   ktime_t end_time;
   ktime_t cost_time;
 
   memset(tmp, 0, sizeof(tmp));
   if (copy_from_user(tmp, buf, n))
       return -EFAULT;
   cmd = tmp;
   data = tmp;
 
   while (data < (tmp + n)) {
       data = strstr(data, " ");
       if (!data)
           break;
       *data = 0;
       argv[argc] = ++data;
       argc++;
       if (argc >= 16)
           break;
   }
 
   tmp[n - 1] = 0;
 
   if (!strcmp(cmd, "setspeed")) {
       int id = 0, val;
       struct spi_device *spi = NULL;
 
       sscanf(argv[0], "%d", &id);
       sscanf(argv[1], "%d", &val);
 
       if (id >= MAX_SPI_DEV_NUM)
           return n;
       if (!g_spi_test_data[id]) {
           pr_err("g_spi.%d is NULL\n", id);
           return n;
       } else {
           spi = g_spi_test_data[id]->spi;
       }
       spi->max_speed_hz = val;
   } else if (!strcmp(cmd, "write")) {
       sscanf(argv[0], "%d", &id);
       sscanf(argv[1], "%d", &times);
       sscanf(argv[2], "%d", &size);
 
       txbuf = kzalloc(size, GFP_KERNEL);
       if (!txbuf) {
           printk("spi write alloc buf size %d fail\n", size);
           return n;
       }
 
       for (i = 0; i < size; i++)
           txbuf[i] = i % 256;
 
       start_time = ktime_get();
       for (i = 0; i < times; i++)
           spi_write_slt(id, txbuf, size);
       end_time = ktime_get();
       cost_time = ktime_sub(end_time, start_time);
       us = ktime_to_us(cost_time);
 
       bytes = size * times * 1;
       bytes = bytes * 1000 / us;
       printk("spi write %d*%d cost %ldus speed:%ldKB/S\n", size, times, us, bytes);
 
       kfree(txbuf);
   } else if (!strcmp(cmd, "read")) {
       sscanf(argv[0], "%d", &id);
       sscanf(argv[1], "%d", &times);
       sscanf(argv[2], "%d", &size);
 
       rxbuf = kzalloc(size, GFP_KERNEL);
       if (!rxbuf) {
           printk("spi read alloc buf size %d fail\n", size);
           return n;
       }
 
       start_time = ktime_get();
       for (i = 0; i < times; i++)
           spi_read_slt(id, rxbuf, size);
       end_time = ktime_get();
       cost_time = ktime_sub(end_time, start_time);
       us = ktime_to_us(cost_time);
 
       bytes = size * times * 1;
       bytes = bytes * 1000 / us;
       printk("spi read %d*%d cost %ldus speed:%ldKB/S\n", size, times, us, bytes);
       print_hex_dump(KERN_ERR, "SPI RX: ",
                  DUMP_PREFIX_OFFSET,
                  16,
                  1,
                  rxbuf,
                  size,
                  1);
 
       kfree(rxbuf);
   } else if (!strcmp(cmd, "loop")) {
       sscanf(argv[0], "%d", &id);
       sscanf(argv[1], "%d", &times);
       sscanf(argv[2], "%d", &size);
 
       txbuf = kzalloc(size, GFP_KERNEL);
       if (!txbuf) {
           printk("spi tx alloc buf size %d fail\n", size);
           return n;
       }
 
       rxbuf = kzalloc(size, GFP_KERNEL);
       if (!rxbuf) {
           kfree(txbuf);
           printk("spi rx alloc buf size %d fail\n", size);
           return n;
       }
 
       for (i = 0; i < size; i++)
           txbuf[i] = i % 256;
 
       start_time = ktime_get();
       for (i = 0; i < times; i++) {
           spi_write_and_read_slt(id, txbuf, rxbuf, size);
           if (memcmp(txbuf, rxbuf, size)) {
               printk("spi loop test fail\n");
               break;
           }
       }
 
       end_time = ktime_get();
       cost_time = ktime_sub(end_time, start_time);
       us = ktime_to_us(cost_time);
 
       bytes = size * times;
       bytes = bytes * 1000 / us;
       printk("spi loop %d*%d cost %ldus speed:%ldKB/S\n", size, times, us, bytes);
 
       kfree(txbuf);
       kfree(rxbuf);
   } else if (!strcmp(cmd, "config")) {
       int width;
 
       sscanf(argv[0], "%d", &width);
 
       if (width == 16)
           bit_per_word = 16;
       else
           bit_per_word = 8;
   } else {
       printk("echo id number size > /dev/spi_misc_test\n");
       printk("echo write 0 10 255 > /dev/spi_misc_test\n");
       printk("echo write 0 10 255 init.rc > /dev/spi_misc_test\n");
       printk("echo read 0 10 255 > /dev/spi_misc_test\n");
       printk("echo loop 0 10 255 > /dev/spi_misc_test\n");
       printk("echo setspeed 0 1000000 > /dev/spi_misc_test\n");
       printk("echo config 8 > /dev/spi_misc_test\n");
   }
 
   return n;
}
 
static const struct file_operations spi_test_fops = {
   .write = spi_test_write,
};
 
static struct miscdevice spi_test_misc = {
   .minor = MISC_DYNAMIC_MINOR,
   .name = "spi_misc_test",
   .fops = &spi_test_fops,
};
 
static int rockchip_spi_test_probe(struct spi_device *spi)
{
   int ret;
   int id = 0;
   struct spi_test_data *spi_test_data = NULL;
 
   if (!spi)
       return -ENOMEM;
 
   spi_test_data = (struct spi_test_data *)kzalloc(sizeof(struct spi_test_data), GFP_KERNEL);
   if (!spi_test_data) {
       dev_err(&spi->dev, "ERR: no memory for spi_test_data\n");
       return -ENOMEM;
   }
   spi->bits_per_word = 8;
 
   spi_test_data->spi = spi;
   spi_test_data->dev = &spi->dev;
 
   ret = spi_setup(spi);
   if (ret < 0) {
       dev_err(spi_test_data->dev, "ERR: fail to setup spi\n");
       return -1;
   }
 
   if (device_property_read_u32(&spi->dev, "id", &id)) {
       dev_warn(&spi->dev, "fail to get id, default set 0\n");
       id = 0;
   }
 
   g_spi_test_data[id] = spi_test_data;
 
   printk("%s:name=%s,bus_num=%d,cs=%d,mode=%d,speed=%d\n", __func__, spi->modalias, spi->master->bus_num, spi->chip_select, spi->mode, spi->max_speed_hz);
 
   return ret;
}
 
static int rockchip_spi_test_remove(struct spi_device *spi)
{
   printk("%s\n", __func__);
   return 0;
}
 
#ifdef CONFIG_OF
static const struct of_device_id rockchip_spi_test_dt_match[] = {
   { .compatible = "rockchip,spi_test_bus0_cs0", },
   { .compatible = "rockchip,spi_test_bus0_cs1", },
   { .compatible = "rockchip,spi_test_bus1_cs0", },
   { .compatible = "rockchip,spi_test_bus1_cs1", },
   { .compatible = "rockchip,spi_test_bus2_cs0", },
   { .compatible = "rockchip,spi_test_bus2_cs1", },
   { .compatible = "rockchip,spi_test_bus3_cs0", },
   { .compatible = "rockchip,spi_test_bus3_cs1", },
   { .compatible = "rockchip,spi_test_bus4_cs0", },
   { .compatible = "rockchip,spi_test_bus4_cs1", },
   {},
};
MODULE_DEVICE_TABLE(of, rockchip_spi_test_dt_match);
 
#endif /* CONFIG_OF */
 
static struct spi_driver spi_rockchip_test_driver = {
   .driver = {
       .name    = "spi_test",
       .owner = THIS_MODULE,
       .of_match_table = of_match_ptr(rockchip_spi_test_dt_match),
   },
   .probe = rockchip_spi_test_probe,
   .remove = rockchip_spi_test_remove,
};
 
static int __init spi_rockchip_test_init(void)
{
   int ret = 0;
 
   misc_register(&spi_test_misc);
   ret = spi_register_driver(&spi_rockchip_test_driver);
   return ret;
}
module_init(spi_rockchip_test_init);
 
static void __exit spi_rockchip_test_exit(void)
{
   misc_deregister(&spi_test_misc);
   return spi_unregister_driver(&spi_rockchip_test_driver);
}
module_exit(spi_rockchip_test_exit);
 
MODULE_AUTHOR("Luo Wei <lw@rock-chips.com>");
MODULE_AUTHOR("Huibin Hong <hhb@rock-chips.com>");
MODULE_DESCRIPTION("ROCKCHIP SPI TEST Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:spi_test");