Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/init.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/cdev.h> |
Shraddha Barke | 7f9eb1f | 2015-09-10 09:03:01 +0530 | [diff] [blame] | 8 | #include <linux/uaccess.h> |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/spi/spi.h> |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 11 | #include <linux/of_gpio.h> |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 12 | |
Arnd Bergmann | 6703992 | 2015-11-16 15:05:02 +0100 | [diff] [blame] | 13 | #include "linux_wlan_spi.h" |
Arnd Bergmann | b03314e | 2015-11-16 15:05:01 +0100 | [diff] [blame] | 14 | #include "wilc_wfi_netdevice.h" |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 15 | #include "linux_wlan_common.h" |
Arnd Bergmann | b03314e | 2015-11-16 15:05:01 +0100 | [diff] [blame] | 16 | #include "wilc_wlan_if.h" |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 17 | |
| 18 | #define USE_SPI_DMA 0 /* johnny add */ |
| 19 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 20 | static const struct wilc1000_ops wilc1000_spi_ops; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 21 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 22 | static int wilc_bus_probe(struct spi_device *spi) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 23 | { |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 24 | int ret, gpio; |
| 25 | struct wilc *wilc; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 26 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 27 | gpio = of_get_gpio(spi->dev.of_node, 0); |
| 28 | if (gpio < 0) |
| 29 | gpio = GPIO_NUM; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 30 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 31 | ret = wilc_netdev_init(&wilc, NULL, HIF_SPI, GPIO_NUM, &wilc_hif_spi); |
| 32 | if (ret) |
| 33 | return ret; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 34 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 35 | spi_set_drvdata(spi, wilc); |
| 36 | wilc->dev = &spi->dev; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 37 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 41 | static int wilc_bus_remove(struct spi_device *spi) |
| 42 | { |
| 43 | wilc_netdev_cleanup(spi_get_drvdata(spi)); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 47 | static const struct of_device_id wilc1000_of_match[] = { |
| 48 | { .compatible = "atmel,wilc_spi", }, |
| 49 | {} |
| 50 | }; |
| 51 | MODULE_DEVICE_TABLE(of, wilc1000_of_match); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 52 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 53 | struct spi_driver wilc1000_spi_driver = { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 54 | .driver = { |
| 55 | .name = MODALIAS, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 56 | .of_match_table = wilc1000_of_match, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 57 | }, |
| 58 | .probe = wilc_bus_probe, |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 59 | .remove = wilc_bus_remove, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 60 | }; |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 61 | module_spi_driver(wilc1000_spi_driver); |
| 62 | MODULE_LICENSE("GPL"); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 63 | |
Arnd Bergmann | 0e1af73 | 2015-11-16 15:04:54 +0100 | [diff] [blame] | 64 | int wilc_spi_init(void) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 65 | { |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 66 | return 1; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Arnd Bergmann | 0e1af73 | 2015-11-16 15:04:54 +0100 | [diff] [blame] | 69 | int wilc_spi_write(u8 *b, u32 len) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 70 | { |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 71 | struct spi_device *spi = to_spi_device(wilc_dev->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 72 | int ret; |
| 73 | struct spi_message msg; |
| 74 | |
| 75 | if (len > 0 && b != NULL) { |
| 76 | struct spi_transfer tr = { |
| 77 | .tx_buf = b, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 78 | .len = len, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 79 | .delay_usecs = 0, |
| 80 | }; |
Hari Prasath Gujulan Elango | 37bc15d | 2015-06-02 12:48:05 +0000 | [diff] [blame] | 81 | char *r_buffer = kzalloc(len, GFP_KERNEL); |
Chaehyun Lim | a0e5fac | 2015-09-17 16:48:45 +0900 | [diff] [blame] | 82 | if (!r_buffer) |
| 83 | return -ENOMEM; |
Chaehyun Lim | 8dfaafd | 2015-08-18 23:18:11 +0900 | [diff] [blame] | 84 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 85 | tr.rx_buf = r_buffer; |
| 86 | PRINT_D(BUS_DBG, "Request writing %d bytes\n", len); |
| 87 | |
| 88 | memset(&msg, 0, sizeof(msg)); |
| 89 | spi_message_init(&msg); |
| 90 | /* [[johnny add */ |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 91 | msg.spi = spi; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 92 | msg.is_dma_mapped = USE_SPI_DMA; |
| 93 | /* ]] */ |
| 94 | spi_message_add_tail(&tr, &msg); |
| 95 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 96 | ret = spi_sync(spi, &msg); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 97 | if (ret < 0) { |
| 98 | PRINT_ER("SPI transaction failed\n"); |
| 99 | } |
| 100 | |
| 101 | kfree(r_buffer); |
| 102 | } else { |
| 103 | PRINT_ER("can't write data with the following length: %d\n", len); |
| 104 | PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len); |
| 105 | ret = -1; |
| 106 | } |
| 107 | |
| 108 | /* change return value to match WILC interface */ |
| 109 | (ret < 0) ? (ret = 0) : (ret = 1); |
| 110 | |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Arnd Bergmann | 0e1af73 | 2015-11-16 15:04:54 +0100 | [diff] [blame] | 115 | int wilc_spi_read(u8 *rb, u32 rlen) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 116 | { |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 117 | struct spi_device *spi = to_spi_device(wilc_dev->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 118 | int ret; |
| 119 | |
| 120 | if (rlen > 0) { |
| 121 | struct spi_message msg; |
| 122 | struct spi_transfer tr = { |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 123 | .rx_buf = rb, |
| 124 | .len = rlen, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 125 | .delay_usecs = 0, |
| 126 | |
| 127 | }; |
Hari Prasath Gujulan Elango | 37bc15d | 2015-06-02 12:48:05 +0000 | [diff] [blame] | 128 | char *t_buffer = kzalloc(rlen, GFP_KERNEL); |
Chaehyun Lim | a0e5fac | 2015-09-17 16:48:45 +0900 | [diff] [blame] | 129 | if (!t_buffer) |
| 130 | return -ENOMEM; |
Chaehyun Lim | 8dfaafd | 2015-08-18 23:18:11 +0900 | [diff] [blame] | 131 | |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 132 | tr.tx_buf = t_buffer; |
| 133 | |
| 134 | memset(&msg, 0, sizeof(msg)); |
| 135 | spi_message_init(&msg); |
| 136 | /* [[ johnny add */ |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 137 | msg.spi = spi; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 138 | msg.is_dma_mapped = USE_SPI_DMA; |
| 139 | /* ]] */ |
| 140 | spi_message_add_tail(&tr, &msg); |
| 141 | |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 142 | ret = spi_sync(spi, &msg); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 143 | if (ret < 0) { |
| 144 | PRINT_ER("SPI transaction failed\n"); |
| 145 | } |
| 146 | kfree(t_buffer); |
| 147 | } else { |
Stanislav Kholmanskikh | 6b69b4f | 2015-10-08 21:23:13 +0300 | [diff] [blame] | 148 | PRINT_ER("can't read data with the following length: %u\n", rlen); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 149 | ret = -1; |
| 150 | } |
| 151 | /* change return value to match WILC interface */ |
| 152 | (ret < 0) ? (ret = 0) : (ret = 1); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Arnd Bergmann | 0e1af73 | 2015-11-16 15:04:54 +0100 | [diff] [blame] | 157 | int wilc_spi_write_read(u8 *wb, u8 *rb, u32 rlen) |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 158 | { |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 159 | struct spi_device *spi = to_spi_device(wilc_dev->dev); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 160 | int ret; |
| 161 | |
| 162 | if (rlen > 0) { |
| 163 | struct spi_message msg; |
| 164 | struct spi_transfer tr = { |
| 165 | .rx_buf = rb, |
| 166 | .tx_buf = wb, |
| 167 | .len = rlen, |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 168 | .bits_per_word = 8, |
| 169 | .delay_usecs = 0, |
| 170 | |
| 171 | }; |
| 172 | |
| 173 | memset(&msg, 0, sizeof(msg)); |
| 174 | spi_message_init(&msg); |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 175 | msg.spi = spi; |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 176 | msg.is_dma_mapped = USE_SPI_DMA; |
| 177 | |
| 178 | spi_message_add_tail(&tr, &msg); |
Arnd Bergmann | c94f05e | 2015-11-16 15:05:09 +0100 | [diff] [blame] | 179 | ret = spi_sync(spi, &msg); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 180 | if (ret < 0) { |
| 181 | PRINT_ER("SPI transaction failed\n"); |
| 182 | } |
| 183 | } else { |
Stanislav Kholmanskikh | 6b69b4f | 2015-10-08 21:23:13 +0300 | [diff] [blame] | 184 | PRINT_ER("can't read data with the following length: %u\n", rlen); |
Johnny Kim | c5c77ba | 2015-05-11 14:30:56 +0900 | [diff] [blame] | 185 | ret = -1; |
| 186 | } |
| 187 | /* change return value to match WILC interface */ |
| 188 | (ret < 0) ? (ret = 0) : (ret = 1); |
| 189 | |
| 190 | return ret; |
| 191 | } |