.. | .. |
---|
7 | 7 | * GNU General Public License for more details. |
---|
8 | 8 | */ |
---|
9 | 9 | |
---|
10 | | -/* dts config |
---|
11 | | -&spi0 { |
---|
12 | | - status = "okay"; |
---|
13 | | - max-freq = <48000000>; //spi internal clk, don't modify |
---|
14 | | - //dma-names = "tx", "rx"; //enable dma |
---|
15 | | - pinctrl-names = "default"; //pinctrl according to you board |
---|
16 | | - pinctrl-0 = <&spi0_clk &spi0_tx &spi0_rx &spi0_cs0 &spi0_cs1>; |
---|
17 | | - spi_test@00 { |
---|
18 | | - compatible = "rockchip,spi_test_bus0_cs0"; |
---|
19 | | - reg = <0>; //chip select 0:cs0 1:cs1 |
---|
20 | | - id = <0>; |
---|
21 | | - spi-max-frequency = <24000000>; //spi output clock |
---|
22 | | - //spi-cpha; not support |
---|
23 | | - //spi-cpol; //if the property is here it is 1:clk is high, else 0:clk is low when idle |
---|
24 | | - }; |
---|
25 | | - |
---|
26 | | - spi_test@01 { |
---|
27 | | - compatible = "rockchip,spi_test_bus0_cs1"; |
---|
28 | | - reg = <1>; |
---|
29 | | - id = <1>; |
---|
30 | | - spi-max-frequency = <24000000>; |
---|
31 | | - spi-cpha; |
---|
32 | | - spi-cpol; |
---|
33 | | - }; |
---|
34 | | -}; |
---|
35 | | -*/ |
---|
36 | | - |
---|
37 | 10 | /* how to test spi |
---|
38 | 11 | * echo write 0 10 255 > /dev/spi_misc_test |
---|
39 | 12 | * echo write 0 10 255 init.rc > /dev/spi_misc_test |
---|
.. | .. |
---|
50 | 23 | #include <linux/interrupt.h> |
---|
51 | 24 | #include <linux/delay.h> |
---|
52 | 25 | #include <linux/clk.h> |
---|
53 | | -#include <linux/fs.h> |
---|
54 | 26 | #include <linux/dma-mapping.h> |
---|
55 | 27 | #include <linux/dmaengine.h> |
---|
56 | 28 | #include <linux/platform_device.h> |
---|
.. | .. |
---|
65 | 37 | #include <linux/uaccess.h> |
---|
66 | 38 | #include <linux/syscalls.h> |
---|
67 | 39 | |
---|
68 | | -#define MAX_SPI_DEV_NUM 8 |
---|
| 40 | +#define MAX_SPI_DEV_NUM 10 |
---|
69 | 41 | #define SPI_MAX_SPEED_HZ 12000000 |
---|
70 | 42 | |
---|
71 | 43 | struct spi_test_data { |
---|
.. | .. |
---|
102 | 74 | |
---|
103 | 75 | spi_message_init(&m); |
---|
104 | 76 | spi_message_add_tail(&t, &m); |
---|
105 | | - return spi_sync(spi, &m); |
---|
| 77 | + ret = spi_sync(spi, &m); |
---|
| 78 | + if (m.actual_length && m.actual_length != n) |
---|
| 79 | + pr_err("%s len=%d actual_length=%d\n", __func__, n, m.actual_length); |
---|
| 80 | + |
---|
| 81 | + return ret; |
---|
106 | 82 | } |
---|
107 | 83 | |
---|
108 | 84 | int spi_read_slt(int id, void *rxbuf, size_t n) |
---|
.. | .. |
---|
127 | 103 | |
---|
128 | 104 | spi_message_init(&m); |
---|
129 | 105 | spi_message_add_tail(&t, &m); |
---|
130 | | - return spi_sync(spi, &m); |
---|
| 106 | + ret = spi_sync(spi, &m); |
---|
| 107 | + if (m.actual_length && m.actual_length != n) |
---|
| 108 | + pr_err("%s len=%d actual_length=%d\n", __func__, n, m.actual_length); |
---|
| 109 | + |
---|
| 110 | + return ret; |
---|
131 | 111 | } |
---|
132 | 112 | |
---|
133 | | -int spi_write_then_read_slt(int id, const void *txbuf, unsigned n_tx, |
---|
134 | | - void *rxbuf, unsigned n_rx) |
---|
| 113 | +int spi_write_then_read_slt(int id, const void *txbuf, unsigned int n_tx, |
---|
| 114 | + void *rxbuf, unsigned int n_rx) |
---|
135 | 115 | { |
---|
136 | 116 | int ret = -1; |
---|
137 | 117 | struct spi_device *spi = NULL; |
---|
.. | .. |
---|
225 | 205 | } |
---|
226 | 206 | spi->max_speed_hz = val; |
---|
227 | 207 | } else if (!strcmp(cmd, "write")) { |
---|
228 | | - char name[64]; |
---|
229 | | - int fd; |
---|
230 | | - mm_segment_t old_fs = get_fs(); |
---|
231 | | - |
---|
232 | 208 | sscanf(argv[0], "%d", &id); |
---|
233 | 209 | sscanf(argv[1], "%d", ×); |
---|
234 | 210 | sscanf(argv[2], "%d", &size); |
---|
235 | | - if (argc > 3) { |
---|
236 | | - sscanf(argv[3], "%s", name); |
---|
237 | | - set_fs(KERNEL_DS); |
---|
238 | | - } |
---|
239 | 211 | |
---|
240 | 212 | txbuf = kzalloc(size, GFP_KERNEL); |
---|
241 | 213 | if (!txbuf) { |
---|
.. | .. |
---|
243 | 215 | return n; |
---|
244 | 216 | } |
---|
245 | 217 | |
---|
246 | | - if (argc > 3) { |
---|
247 | | - fd = ksys_open(name, O_RDONLY, 0); |
---|
248 | | - if (fd < 0) { |
---|
249 | | - printk("open %s fail\n", name); |
---|
250 | | - } else { |
---|
251 | | - ksys_read(fd, (char __user *)txbuf, size); |
---|
252 | | - ksys_close(fd); |
---|
253 | | - } |
---|
254 | | - set_fs(old_fs); |
---|
255 | | - } else { |
---|
256 | | - for (i = 0; i < size; i++) |
---|
257 | | - txbuf[i] = i % 256; |
---|
258 | | - } |
---|
| 218 | + for (i = 0; i < size; i++) |
---|
| 219 | + txbuf[i] = i % 256; |
---|
259 | 220 | |
---|
260 | 221 | start_time = ktime_get(); |
---|
261 | 222 | for (i = 0; i < times; i++) |
---|
.. | .. |
---|
340 | 301 | kfree(txbuf); |
---|
341 | 302 | kfree(rxbuf); |
---|
342 | 303 | } else if (!strcmp(cmd, "config")) { |
---|
343 | | - int width; |
---|
| 304 | + int width, mode; |
---|
344 | 305 | |
---|
345 | | - sscanf(argv[0], "%d", &width); |
---|
| 306 | + sscanf(argv[0], "%d", &id); |
---|
| 307 | + sscanf(argv[1], "%d", &width); |
---|
| 308 | + sscanf(argv[2], "mode=0x%x", &mode); |
---|
346 | 309 | |
---|
347 | 310 | if (width == 16) |
---|
348 | 311 | bit_per_word = 16; |
---|
349 | 312 | else |
---|
350 | 313 | bit_per_word = 8; |
---|
| 314 | + |
---|
| 315 | + if (mode) { |
---|
| 316 | + g_spi_test_data[id]->spi->mode = mode; |
---|
| 317 | + spi_setup(g_spi_test_data[id]->spi); |
---|
| 318 | + } |
---|
351 | 319 | } else { |
---|
352 | 320 | printk("echo id number size > /dev/spi_misc_test\n"); |
---|
353 | 321 | printk("echo write 0 10 255 > /dev/spi_misc_test\n"); |
---|
.. | .. |
---|
355 | 323 | printk("echo read 0 10 255 > /dev/spi_misc_test\n"); |
---|
356 | 324 | printk("echo loop 0 10 255 > /dev/spi_misc_test\n"); |
---|
357 | 325 | printk("echo setspeed 0 1000000 > /dev/spi_misc_test\n"); |
---|
358 | | - printk("echo config 8 > /dev/spi_misc_test\n"); |
---|
| 326 | + printk("echo config 0 8 mode=0xb > /dev/spi_misc_test\n"); |
---|
359 | 327 | } |
---|
360 | 328 | |
---|
361 | 329 | return n; |
---|
.. | .. |
---|
380 | 348 | if (!spi) |
---|
381 | 349 | return -ENOMEM; |
---|
382 | 350 | |
---|
383 | | - if (!spi->dev.of_node) |
---|
384 | | - return -ENOMEM; |
---|
385 | | - |
---|
386 | 351 | spi_test_data = (struct spi_test_data *)kzalloc(sizeof(struct spi_test_data), GFP_KERNEL); |
---|
387 | 352 | if (!spi_test_data) { |
---|
388 | 353 | dev_err(&spi->dev, "ERR: no memory for spi_test_data\n"); |
---|
.. | .. |
---|
399 | 364 | return -1; |
---|
400 | 365 | } |
---|
401 | 366 | |
---|
402 | | - if (of_property_read_u32(spi->dev.of_node, "id", &id)) { |
---|
| 367 | + if (device_property_read_u32(&spi->dev, "id", &id)) { |
---|
403 | 368 | dev_warn(&spi->dev, "fail to get id, default set 0\n"); |
---|
404 | 369 | id = 0; |
---|
405 | 370 | } |
---|