| .. | .. |
|---|
| 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 { |
|---|
| .. | .. |
|---|
| 138 | 110 | return ret; |
|---|
| 139 | 111 | } |
|---|
| 140 | 112 | |
|---|
| 141 | | -int spi_write_then_read_slt(int id, const void *txbuf, unsigned n_tx, |
|---|
| 142 | | - 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) |
|---|
| 143 | 115 | { |
|---|
| 144 | 116 | int ret = -1; |
|---|
| 145 | 117 | struct spi_device *spi = NULL; |
|---|
| .. | .. |
|---|
| 233 | 205 | } |
|---|
| 234 | 206 | spi->max_speed_hz = val; |
|---|
| 235 | 207 | } else if (!strcmp(cmd, "write")) { |
|---|
| 236 | | - char name[64]; |
|---|
| 237 | | - int fd; |
|---|
| 238 | | - mm_segment_t old_fs = get_fs(); |
|---|
| 239 | | - |
|---|
| 240 | 208 | sscanf(argv[0], "%d", &id); |
|---|
| 241 | 209 | sscanf(argv[1], "%d", ×); |
|---|
| 242 | 210 | sscanf(argv[2], "%d", &size); |
|---|
| 243 | | - if (argc > 3) { |
|---|
| 244 | | - sscanf(argv[3], "%s", name); |
|---|
| 245 | | - set_fs(KERNEL_DS); |
|---|
| 246 | | - } |
|---|
| 247 | 211 | |
|---|
| 248 | 212 | txbuf = kzalloc(size, GFP_KERNEL); |
|---|
| 249 | 213 | if (!txbuf) { |
|---|
| .. | .. |
|---|
| 251 | 215 | return n; |
|---|
| 252 | 216 | } |
|---|
| 253 | 217 | |
|---|
| 254 | | - if (argc > 3) { |
|---|
| 255 | | - fd = ksys_open(name, O_RDONLY, 0); |
|---|
| 256 | | - if (fd < 0) { |
|---|
| 257 | | - printk("open %s fail\n", name); |
|---|
| 258 | | - } else { |
|---|
| 259 | | - ksys_read(fd, (char __user *)txbuf, size); |
|---|
| 260 | | - ksys_close(fd); |
|---|
| 261 | | - } |
|---|
| 262 | | - set_fs(old_fs); |
|---|
| 263 | | - } else { |
|---|
| 264 | | - for (i = 0; i < size; i++) |
|---|
| 265 | | - txbuf[i] = i % 256; |
|---|
| 266 | | - } |
|---|
| 218 | + for (i = 0; i < size; i++) |
|---|
| 219 | + txbuf[i] = i % 256; |
|---|
| 267 | 220 | |
|---|
| 268 | 221 | start_time = ktime_get(); |
|---|
| 269 | 222 | for (i = 0; i < times; i++) |
|---|
| .. | .. |
|---|
| 348 | 301 | kfree(txbuf); |
|---|
| 349 | 302 | kfree(rxbuf); |
|---|
| 350 | 303 | } else if (!strcmp(cmd, "config")) { |
|---|
| 351 | | - int width; |
|---|
| 304 | + int width, mode; |
|---|
| 352 | 305 | |
|---|
| 353 | | - 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); |
|---|
| 354 | 309 | |
|---|
| 355 | 310 | if (width == 16) |
|---|
| 356 | 311 | bit_per_word = 16; |
|---|
| 357 | 312 | else |
|---|
| 358 | 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 | + } |
|---|
| 359 | 319 | } else { |
|---|
| 360 | 320 | printk("echo id number size > /dev/spi_misc_test\n"); |
|---|
| 361 | 321 | printk("echo write 0 10 255 > /dev/spi_misc_test\n"); |
|---|
| .. | .. |
|---|
| 363 | 323 | printk("echo read 0 10 255 > /dev/spi_misc_test\n"); |
|---|
| 364 | 324 | printk("echo loop 0 10 255 > /dev/spi_misc_test\n"); |
|---|
| 365 | 325 | printk("echo setspeed 0 1000000 > /dev/spi_misc_test\n"); |
|---|
| 366 | | - printk("echo config 8 > /dev/spi_misc_test\n"); |
|---|
| 326 | + printk("echo config 0 8 mode=0xb > /dev/spi_misc_test\n"); |
|---|
| 367 | 327 | } |
|---|
| 368 | 328 | |
|---|
| 369 | 329 | return n; |
|---|
| .. | .. |
|---|
| 388 | 348 | if (!spi) |
|---|
| 389 | 349 | return -ENOMEM; |
|---|
| 390 | 350 | |
|---|
| 391 | | - if (!spi->dev.of_node) |
|---|
| 392 | | - return -ENOMEM; |
|---|
| 393 | | - |
|---|
| 394 | 351 | spi_test_data = (struct spi_test_data *)kzalloc(sizeof(struct spi_test_data), GFP_KERNEL); |
|---|
| 395 | 352 | if (!spi_test_data) { |
|---|
| 396 | 353 | dev_err(&spi->dev, "ERR: no memory for spi_test_data\n"); |
|---|
| .. | .. |
|---|
| 407 | 364 | return -1; |
|---|
| 408 | 365 | } |
|---|
| 409 | 366 | |
|---|
| 410 | | - if (of_property_read_u32(spi->dev.of_node, "id", &id)) { |
|---|
| 367 | + if (device_property_read_u32(&spi->dev, "id", &id)) { |
|---|
| 411 | 368 | dev_warn(&spi->dev, "fail to get id, default set 0\n"); |
|---|
| 412 | 369 | id = 0; |
|---|
| 413 | 370 | } |
|---|