| .. | .. |
|---|
| 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 | 113 | int spi_write_then_read_slt(int id, const void *txbuf, unsigned n_tx, |
|---|
| .. | .. |
|---|
| 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++) |
|---|
| .. | .. |
|---|
| 380 | 341 | if (!spi) |
|---|
| 381 | 342 | return -ENOMEM; |
|---|
| 382 | 343 | |
|---|
| 383 | | - if (!spi->dev.of_node) |
|---|
| 384 | | - return -ENOMEM; |
|---|
| 385 | | - |
|---|
| 386 | 344 | spi_test_data = (struct spi_test_data *)kzalloc(sizeof(struct spi_test_data), GFP_KERNEL); |
|---|
| 387 | 345 | if (!spi_test_data) { |
|---|
| 388 | 346 | dev_err(&spi->dev, "ERR: no memory for spi_test_data\n"); |
|---|
| .. | .. |
|---|
| 399 | 357 | return -1; |
|---|
| 400 | 358 | } |
|---|
| 401 | 359 | |
|---|
| 402 | | - if (of_property_read_u32(spi->dev.of_node, "id", &id)) { |
|---|
| 360 | + if (device_property_read_u32(&spi->dev, "id", &id)) { |
|---|
| 403 | 361 | dev_warn(&spi->dev, "fail to get id, default set 0\n"); |
|---|
| 404 | 362 | id = 0; |
|---|
| 405 | 363 | } |
|---|